-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler_adaptive.c
242 lines (214 loc) · 7.62 KB
/
scheduler_adaptive.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
#include <stdio.h>
#include "utlist.h"
#include "utils.h"
#include <stdbool.h>
#include "memory_controller.h"
#include "params.h"
/* A basic FCFS policy augmented with a not-so-clever close-page policy.
If the memory controller is unable to issue a command this cycle, find
a bank that recently serviced a column-rd/wr and close it (precharge it). */
int cnt=7;
int high=10;
int low=4;
int row,bank,chanel,rank;
extern long long int CYCLE_VAL;
bool cMode = false;
/* A data structure to see if a bank is a candidate for precharge. */
int recent_colacc[MAX_NUM_CHANNELS][MAX_NUM_RANKS][MAX_NUM_BANKS];
/* Keeping track of how many preemptive precharges are performed. */
long long int num_aggr_precharge = 0;
void init_scheduler_vars()
{
// initialize all scheduler variables here
int i, j, k;
for (i=0; i<MAX_NUM_CHANNELS; i++) {
for (j=0; j<MAX_NUM_RANKS; j++) {
for (k=0; k<MAX_NUM_BANKS; k++) {
recent_colacc[i][j][k] = 0;
}
}
}
return;
}
// write queue high water mark; begin draining writes if write queue exceeds this value
#define HI_WM 40
// end write queue drain once write queue has this many writes in it
#define LO_WM 20
// 1 means we are in write-drain mode for that channel
int drain_writes[MAX_NUM_CHANNELS];
void schedule(int channel)
{
if(cMode){
request_t * rd_ptr = NULL;
request_t * wr_ptr = NULL;
int i, j;
// 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])
{
LL_FOREACH(write_queue_head[channel], wr_ptr)
{
if(wr_ptr->command_issuable)
{
/* Before issuing the command, see if this bank is now a candidate for closure (if it just did a column-rd/wr).
If the bank just did an activate or precharge, it is not a candidate for closure. */
if (wr_ptr->next_command == COL_WRITE_CMD) {
recent_colacc[channel][wr_ptr->dram_addr.rank][wr_ptr->dram_addr.bank] = 1;
if(chanel == channel && wr_ptr->dram_addr.rank == rank && wr_ptr->dram_addr.bank == bank){
cnt--;
if(cnt < low){
cMode = false;
chanel = -1;
rank = -1;
bank = -1;
}
}
}
if (wr_ptr->next_command == ACT_CMD) {
recent_colacc[channel][wr_ptr->dram_addr.rank][wr_ptr->dram_addr.bank] = 0;
}
if (wr_ptr->next_command == PRE_CMD) {
recent_colacc[channel][wr_ptr->dram_addr.rank][wr_ptr->dram_addr.bank] = 0;
}
issue_request_command(wr_ptr);
break;
}
}
}
// 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])
{
LL_FOREACH(read_queue_head[channel],rd_ptr)
{
if(rd_ptr->command_issuable)
{
/* Before issuing the command, see if this bank is now a candidate for closure (if it just did a column-rd/wr).
If the bank just did an activate or precharge, it is not a candidate for closure. */
if (rd_ptr->next_command == COL_READ_CMD) {
recent_colacc[channel][rd_ptr->dram_addr.rank][rd_ptr->dram_addr.bank] = 1;
if(chanel == channel && rd_ptr->dram_addr.rank == rank && rd_ptr->dram_addr.bank == bank){
cnt--;
if(cnt < low){
cMode = false;
chanel = -1;
rank = -1;
bank = -1;
}
}
}
if (rd_ptr->next_command == ACT_CMD) {
recent_colacc[channel][rd_ptr->dram_addr.rank][rd_ptr->dram_addr.bank] = 0;
}
if (rd_ptr->next_command == PRE_CMD) {
recent_colacc[channel][rd_ptr->dram_addr.rank][rd_ptr->dram_addr.bank] = 0;
}
issue_request_command(rd_ptr);
break;
}
}
}
/* If a command hasn't yet been issued to this channel in this cycle, issue a precharge. */
if (!command_issued_current_cycle[channel]) {
for (i=0; i<NUM_RANKS; i++) {
for (j=0; j<NUM_BANKS; j++) { /* For all banks on the channel.. */
if (recent_colacc[channel][i][j]) { /* See if this bank is a candidate. */
if (is_precharge_allowed(channel,i,j)) { /* See if precharge is doable. */
if (issue_precharge_command(channel,i,j)) {
num_aggr_precharge++;
recent_colacc[channel][i][j] = 0;
}
}
}
}
}
}
}
else{
request_t * rd_ptr = NULL;
request_t * wr_ptr = NULL;
// 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])
{
LL_FOREACH(write_queue_head[channel], wr_ptr)
{
if(wr_ptr->command_issuable)
{
if(wr_ptr->next_command == ACT_CMD){
cnt++;
if(cnt > high){
cMode = true;
}
}
issue_request_command(wr_ptr);
break;
}
}
return;
}
// 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])
{
LL_FOREACH(read_queue_head[channel],rd_ptr)
{
if(rd_ptr->command_issuable)
{
issue_request_command(rd_ptr);
break;
}
}
return;
}
}
}
void scheduler_stats()
{
/* Nothing to print for now. */
printf("Number of aggressive precharges: %lld\n", num_aggr_precharge);
}