-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler-wow.c
executable file
·648 lines (616 loc) · 15.3 KB
/
scheduler-wow.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "utlist.h"
#include "utils.h"
#include "memory_controller.h"
extern long long int CYCLE_VAL;
extern int WQ_CAPACITY;
extern int NUM_BANKS;
// write queue high water mark; begin draining writes if write queue exceeds this value
#define HI_WM (WQ_CAPACITY - NUM_BANKS)
// end write queue drain once write queue has this many writes in it
#define LO_WM (HI_WM - 8)
#define AUTO_PRECHARGE 0
#define THREAD_PHASE 0
#define TRAFFIC_LIGHT 1
#define LOCALITY 1
#define READ_WRITE 0
#define MAX_ROWS 32768
#define MAX_DISTANCE 13
#define M2C_INTERVAL 970
#define C2C_INTERVAL 220
extern int NUMCORES;
#if THREAD_PHASE
// Thread phase prediction
int *distance;
int *interval;
int *phase;
extern long long int * committed;
#endif
#if TRAFFIC_LIGHT
// Traffic light
int *traffic_light;
int *requests_per_channel;
int *requests_per_rank;
#endif
#if LOCALITY
// row buffer locality between thread
int *localityCounter;
#endif
// 1 means we are in write-drain mode for that channel
int drain_writes[MAX_NUM_CHANNELS];
typedef struct state {
int marked;
int incoming;
} State;
#if THREAD_PHASE
void init_distance_interval() {
for (int i = 0; i < NUMCORES; i++) {
distance[i] = 0;
interval[i] = 0;
phase[i] = 0;
}
}
#endif
void init_scheduler_vars()
{
// initialize all scheduler variables here
#if THREAD_PHASE
distance = (int*)malloc( NUMCORES * sizeof(int));
interval = (int*)malloc( NUMCORES * sizeof(int));
phase = (int*)malloc( NUMCORES * sizeof(int));
init_distance_interval();
#endif
#if TRAFFIC_LIGHT
traffic_light = (int*)malloc( NUMCORES * sizeof(int));
requests_per_channel = (int*)malloc( NUMCORES * MAX_NUM_CHANNELS * sizeof(int));
requests_per_rank = (int*)malloc( NUMCORES * MAX_NUM_CHANNELS * MAX_NUM_BANKS * sizeof(int));
#endif
#if LOCALITY
localityCounter = (int*)malloc( MAX_NUM_RANKS * MAX_NUM_BANKS * MAX_ROWS * sizeof(int));
#endif
return;
}
void stateAssign(int channel) {
request_t * rd_ptr = NULL;
request_t * wr_ptr = NULL;
LL_FOREACH(write_queue_head[channel], wr_ptr)
{
if (wr_ptr->user_ptr == NULL) {
State * st = (State*) malloc (sizeof(State));
st->marked = 0;
st->incoming = 1;
wr_ptr->user_ptr = st;
}
}
LL_FOREACH(read_queue_head[channel], rd_ptr)
{
if (rd_ptr->user_ptr == NULL) {
State * st = (State*) malloc (sizeof(State));
st->marked = 0;
st->incoming = 1;
rd_ptr->user_ptr = st;
}
}
}
#if THREAD_PHASE
void predictThreadPhase(int channel)
{
request_t * req_ptr = NULL;
LL_FOREACH(read_queue_head[channel],req_ptr)
{
// commit count is used by the incoming request only.
State * st = (State*)(req_ptr->user_ptr);
if (st->incoming)
{
int thread_id = req_ptr->thread_id;
if (phase[thread_id] == 1)
{
if (committed[thread_id] > interval[thread_id])
{
distance[thread_id] = 0;
interval[thread_id] = committed[thread_id] + C2C_INTERVAL;
phase[thread_id] = 1;
}
else
{
distance[thread_id] += 1;
if (distance[thread_id] > MAX_DISTANCE) phase[thread_id] = 0;
}
}
else
{
if (committed[thread_id] > interval[thread_id])
{
interval[thread_id] = 0;
interval[thread_id] = committed[thread_id] + M2C_INTERVAL;
phase[thread_id] = 1;
}
else
{
distance[thread_id] += 1;
if(distance[thread_id] > MAX_DISTANCE) phase[thread_id] = 0;
}
}
st->incoming = 0;
}
}
}
#endif
#if TRAFFIC_LIGHT
void updateTrafficLight() {
// TODO: Traffic light
// Iterate through all the ranks and channels to know the request number of each thread toward ranks and channels
// Reset first
for (int ch = 0; ch < NUMCORES * MAX_NUM_CHANNELS; ch++) {
requests_per_channel[ch] = 0;
}
for (int rk = 0; rk < NUMCORES * MAX_NUM_CHANNELS * MAX_NUM_RANKS; rk++) {
requests_per_rank[rk] = 0;
}
for (int coreid = 0; coreid < NUMCORES; coreid++) {
traffic_light[coreid] = 0;
}
// Compute
request_t * rd_ptr = NULL;
for(int ch = 0; ch < MAX_NUM_CHANNELS; ch++){
LL_FOREACH(read_queue_head[ch], rd_ptr){
int channel_index =
rd_ptr->thread_id * MAX_NUM_CHANNELS + ch;
int rank_index =
rd_ptr->thread_id * MAX_NUM_CHANNELS * MAX_NUM_RANKS +
ch * MAX_NUM_RANKS + (rd_ptr->dram_addr).rank;
requests_per_rank[rank_index]++;
requests_per_channel[channel_index]++;
}
}
// Turn on the light
for(int ch = 0; ch < MAX_NUM_CHANNELS; ch++){
if (drain_writes[ch]) {
for (int coreid = 0; coreid < NUMCORES; coreid++) {
if (requests_per_channel[coreid * MAX_NUM_CHANNELS + ch] > 0) {
traffic_light[coreid] = 1;
}
}
}
for (int rk = 0; rk < MAX_NUM_RANKS; rk++) {
if (forced_refresh_mode_on[ch][rk]) {
for (int coreid = 0; coreid < NUMCORES; coreid++) {
if (requests_per_rank[coreid * MAX_NUM_CHANNELS * MAX_NUM_RANKS + ch * MAX_NUM_RANKS + rk] > 0) {
traffic_light[coreid] = 1;
}
}
}
}
}
}
#endif
#if LOCALITY
void updateLocality(int channel) {
// reset localityCounter
/*
for (int i = 0; i < MAX_NUM_RANKS * MAX_NUM_BANKS * MAX_ROWS; i++) {
localityCounter[i] = 0;
}*/
// use reverse reset
request_t * rd_ptr = NULL;
request_t * wr_ptr = NULL;
LL_FOREACH(write_queue_head[channel], wr_ptr)
{
dram_address_t * dram_addr = &(wr_ptr->dram_addr);
localityCounter[dram_addr->rank * MAX_NUM_BANKS * MAX_ROWS + dram_addr->bank * MAX_ROWS + dram_addr->row] = 0;
}
LL_FOREACH(read_queue_head[channel], rd_ptr)
{
dram_address_t * dram_addr = &(rd_ptr->dram_addr);
localityCounter[dram_addr->rank * MAX_NUM_BANKS * MAX_ROWS + dram_addr->bank * MAX_ROWS + dram_addr->row] = 0;
}
LL_FOREACH(write_queue_head[channel], wr_ptr)
{
dram_address_t * dram_addr = &(wr_ptr->dram_addr);
localityCounter[dram_addr->rank * MAX_NUM_BANKS * MAX_ROWS + dram_addr->bank * MAX_ROWS + dram_addr->row]++;
}
LL_FOREACH(read_queue_head[channel], rd_ptr)
{
dram_address_t * dram_addr = &(rd_ptr->dram_addr);
localityCounter[dram_addr->rank * MAX_NUM_BANKS * MAX_ROWS + dram_addr->bank * MAX_ROWS + dram_addr->row]++;
}
}
#endif
int hit(request_t * req)
{
if (req == NULL)
{
return 0;
}
else
{
if (req->operation_type == READ)
{
return req->command_issuable && req->next_command == COL_READ_CMD;
}
else
{
return req->command_issuable && req->next_command == COL_WRITE_CMD;
}
}
}
#if THREAD_PHASE
int compare_phase(request_t * req_a, request_t * req_b)
{
int req_a_core = req_a->thread_id,
req_b_core = req_b->thread_id;
if (phase[req_a_core] && !phase[req_b_core]) return 1;
if (!phase[req_a_core] && phase[req_b_core]) return 0;
return -1;
}
#endif
#if TRAFFIC_LIGHT
int compare_traffic_light(request_t * req_a, request_t * req_b)
{
int req_a_core = req_a->thread_id,
req_b_core = req_b->thread_id;
if (!traffic_light[req_a_core] && traffic_light[req_b_core]) return 1;
if (traffic_light[req_a_core] && !traffic_light[req_b_core]) return 0;
return -1;
}
#endif
#if LOCALITY
int compare_locality(request_t * req_a, request_t * req_b)
{
dram_address_t * dram_addr = &(req_a->dram_addr);
int locality_a = localityCounter[dram_addr->rank * MAX_NUM_BANKS * MAX_ROWS + dram_addr->bank * MAX_ROWS + dram_addr->row];
dram_addr = &(req_b->dram_addr);
int locality_b = localityCounter[dram_addr->rank * MAX_NUM_BANKS * MAX_ROWS + dram_addr->bank * MAX_ROWS + dram_addr->row];
if (locality_a > locality_b) return 1;
if (locality_a < locality_b) return 0;
return -1;
}
#endif
int compare(request_t * req_a, request_t * req_b)
{
if (req_b == NULL) return 1;
if (req_a == NULL)
{
int hit_b = hit(req_b);
if (hit_b) return 0;
return 1;
}
else
{
// Request type should be the same
assert(req_a->operation_type == req_b->operation_type);
int hit_a = hit(req_a),
hit_b = hit(req_b);
if (hit_a && !hit_b) return 1;
if (!hit_a && hit_b) return 0;
if (hit_a && hit_b)
{
#if TRAFFIC_LIGHT
if (compare_traffic_light(req_a, req_b) > 0) return 1;
if (!compare_traffic_light(req_a, req_b)) return 0;
#endif
#if THREAD_PHASE
return compare_phase(req_a, req_b);
#endif
}
return 1;
}
}
void schedule(int channel)
{
request_t * rd_ptr = NULL;
request_t * wr_ptr = NULL;
request_t * issue = NULL;
stateAssign(channel);
#if THREAD_PHASE
predictThreadPhase(channel);
#endif
#if TRAFFIC_LIGHT
updateTrafficLight();
#endif
#if LOCALITY
updateLocality(channel);
#endif
// if in write drain mode, keep draining writes until the
// write queue occupancy drops to LO_WM
if (drain_writes[channel] && (write_queue_length[channel] > LO_WM))
{
drain_writes[channel] = 1; // Keep draining.
}
else
{
drain_writes[channel] = 0; // No need to drain.
}
// initiate write drain if either the write queue occupancy
// has reached the HI_WM , OR, if there are no pending read
// requests
if (write_queue_length[channel] > HI_WM)
{
drain_writes[channel] = 1;
}
else
{
if (!read_queue_length[channel])
drain_writes[channel] = 1;
}
// If in write drain mode, look through all the write queue
// elements (already arranged in the order of arrival), and
// issue the command for the first request that is ready
if (drain_writes[channel])
{
// Row hit write
wr_ptr = NULL;
LL_FOREACH(write_queue_head[channel], wr_ptr)
{
if (!compare(issue, wr_ptr))
{
issue = wr_ptr;
}
}
if (write_queue_length[channel] < HI_WM)
{
#if READ_WRITE
// Row hit read
if (!issue)
{
rd_ptr = NULL;
LL_FOREACH(read_queue_head[channel], rd_ptr)
{
if (!compare(issue, rd_ptr))
{
issue = rd_ptr;
}
}
}
#endif
// Random write
if (!issue)
{
wr_ptr = NULL;
LL_FOREACH(write_queue_head[channel], wr_ptr)
{
if (wr_ptr->command_issuable)
{
if (!issue)
issue = wr_ptr;
#if LOCALITY
else if (!compare_locality(issue, wr_ptr))
issue = wr_ptr;
#endif
}
}
}
#if READ_WRITE
// Random read
if (!issue)
{
rd_ptr = NULL;
LL_FOREACH(read_queue_head[channel], rd_ptr)
{
if (rd_ptr->command_issuable)
{
if (!issue)
issue = rd_ptr;
#if TRAFFIC_LIGHT
int light = compare_traffic_light(issue, rd_ptr);
if (!light)
issue = rd_ptr;
else if (light < 0)
{
#endif
#if THREAD_PHASE
int phase = compare_phase(issue, rd_ptr);
if (!phase) issue = rd_ptr;
else if (phase < 0)
{
#endif
#if LOCALITY
if (!compare_locality(issue, rd_ptr)) issue = rd_ptr;
#endif
#if THREAD_PHASE
}
#endif
#if TRAFFIC_LIGHT
}
#endif
}
}
}
#endif
}
else
{
// Random write
if (!issue)
{
wr_ptr = NULL;
LL_FOREACH(write_queue_head[channel], wr_ptr)
{
if (wr_ptr->command_issuable)
{
if (!issue)
issue = wr_ptr;
#if LOCALITY
else if (!compare_locality(issue, wr_ptr))
issue = wr_ptr;
#endif
}
}
}
#if READ_WRITE
// Row hit read
if (!issue)
{
rd_ptr = NULL;
LL_FOREACH(read_queue_head[channel], rd_ptr)
{
if (!compare(issue, rd_ptr))
{
issue = rd_ptr;
}
}
}
// Random read
if (!issue)
{
rd_ptr = NULL;
LL_FOREACH(read_queue_head[channel], rd_ptr)
{
if (rd_ptr->command_issuable)
{
if (!issue)
issue = rd_ptr;
#if TRAFFIC_LIGHT
int light = compare_traffic_light(issue, rd_ptr);
if (!light)
issue = rd_ptr;
else if (light < 0)
{
#endif
#if THREAD_PHASE
int phase = compare_phase(issue, rd_ptr);
if (!phase) issue = rd_ptr;
else if (phase < 0)
{
#endif
#if LOCALITY
if (!compare_locality(issue, rd_ptr)) issue = rd_ptr;
#endif
#if THREAD_PHASE
}
#endif
#if TRAFFIC_LIGHT
}
#endif
}
}
}
#endif
}
}
// Draining Reads
// look through the queue and find the first request whose
// command can be issued in this cycle and issue it
// Simple FCFS
if (!drain_writes[channel])
{
// Row hit read
rd_ptr = NULL;
LL_FOREACH(read_queue_head[channel],rd_ptr)
{
if (!compare(issue, rd_ptr))
{
issue = rd_ptr;
}
}
#if READ_WRITE
// Row hit write
if (!issue)
{
wr_ptr = NULL;
LL_FOREACH(write_queue_head[channel], wr_ptr)
{
if (!compare(issue, wr_ptr))
{
issue = wr_ptr;
}
}
}
#endif
// Random read
if (!issue)
{
rd_ptr = NULL;
LL_FOREACH(read_queue_head[channel], rd_ptr)
{
if (rd_ptr->command_issuable)
{
if (!issue)
issue = rd_ptr;
#if TRAFFIC_LIGHT
int light = compare_traffic_light(issue, rd_ptr);
if (!light)
issue = rd_ptr;
else if (light < 0)
{
#endif
#if THREAD_PHASE
int phase = compare_phase(issue, rd_ptr);
if (!phase) issue = rd_ptr;
else if (phase < 0)
{
#endif
#if LOCALITY
if (!compare_locality(issue, rd_ptr)) issue = rd_ptr;
#endif
#if THREAD_PHASE
}
#endif
#if TRAFFIC_LIGHT
}
#endif
}
}
}
#if READ_WRITE
// Random write
if (!issue)
{
wr_ptr = NULL;
LL_FOREACH(write_queue_head[channel], wr_ptr)
{
if (wr_ptr->command_issuable)
{
if (!issue)
issue = wr_ptr;
#if LOCALITY
else if (!compare_locality(issue, wr_ptr))
issue = wr_ptr;
#endif
}
}
}
#endif
}
if (issue != NULL && issue->command_issuable)
{
issue_request_command(issue);
#if AUTO_PRECHARGE
if ((issue->next_command == COL_READ_CMD || issue->next_command == COL_WRITE_CMD) &&
is_autoprecharge_allowed(channel, issue->dram_addr.rank, issue->dram_addr.bank))
{
wr_ptr = NULL;
LL_FOREACH(write_queue_head[channel], wr_ptr)
{
if (!wr_ptr->request_served
&& issue->dram_addr.rank == wr_ptr->dram_addr.rank
&& issue->dram_addr.bank == wr_ptr->dram_addr.bank
&& issue->dram_addr.row == wr_ptr->dram_addr.row)
{
return; // has hit, no auto precharge
}
}
rd_ptr = NULL;
LL_FOREACH(read_queue_head[channel], rd_ptr)
{
if (!rd_ptr->request_served
&& issue->dram_addr.rank == rd_ptr->dram_addr.rank
&& issue->dram_addr.bank == rd_ptr->dram_addr.bank
&& issue->dram_addr.row == rd_ptr->dram_addr.row)
{
return; // has hit, no auto precharge
}
}
issue_autoprecharge(channel, issue->dram_addr.rank, issue->dram_addr.bank);
// nothing issuable this cycle
return;
}
#endif
}
}
void scheduler_stats()
{
/* Nothing to print for now. */
}