-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
318 lines (297 loc) · 8.19 KB
/
Copy pathmap.cpp
File metadata and controls
318 lines (297 loc) · 8.19 KB
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
#include "settings.h"
#include "map.h"
#include "gc.h"
extern int RPOLICY;
extern uint32_t target_ppa;
extern queue<int> freeq;
extern list<int> lba_list;
extern bool BIP;
#define SKIP_FULL 1
int counter = 0;
/*check mapping table hit
* if miss, do replacement (or load)
*/
int check_mtable(uint32_t lba, SSD* ssd, STATS* stats) {
int entry_index=-1;
/* check cached mapping table
*/
entry_index = ssd->lkuptable[lba];
/*
for (int i=0;i<ssd->mtable_size; i++) {
if (ssd->mtable[i].lba == lba) {
entry_index = i;
break;
}
}*/
if (entry_index == -1) {
/* need replacement
*/
if (SKIP_FULL) {
if (ssd->mtable_free > 0) {
ssd->mtable_free--;
}else {
stats->cache_miss++;
}
} else {
stats->cache_miss++;
}
/*select replacement policy
/ */
if (RPOLICY == 0) { //FIFO
entry_index = m_fifo(lba, ssd, stats);
} else if (RPOLICY == 1) { //LRU
if ((int) lba_list.size() < ssd->mtable_size){ //Do not need to replacement
entry_index = m_fifo(lba, ssd, stats);
update_lba_list(lba, ssd, stats, true);
}else{
entry_index = m_lru(lba, ssd, stats);
}
} else if (RPOLICY == 2){ //cNRU
entry_index = m_cnru(lba, ssd, stats);
} else if (RPOLICY == 3){
counter ++ ; // For checking m_table is full. If it isn't, we don't need replacement. (Using fifo for adding elements)
if(counter <= ssd->mtable_size){
entry_index = m_fifo(lba, ssd, stats);
}else{
entry_index = m_rand(lba, ssd, stats);
}
}
//TODO add flash access latency
ssd->lkuptable[lba] = entry_index;
ssd->mtable[entry_index].lba = lba;
ssd->mtable[entry_index].ppa = ssd->fmtable[lba];
ssd->mtable[entry_index].recently_used = true;
} else {
/*cache hit
*/
stats->cache_hit++;
if (RPOLICY == 1){
update_lba_list(lba, ssd, stats, false);
}
else if (RPOLICY == 2){
ssd->mtable[entry_index].recently_used = true;
}
}
return entry_index;
}
int update_mtable(int index, uint32_t ppa, SSD* ssd) {
ssd->mtable[index].ppa = ppa;
ssd->mtable[index].dirty = true;
ssd->mtable[index].recently_used = true;
return 0;
}
int update_gc_table(uint32_t lba, uint32_t ppa, SSD* ssd) {
int entry_index=-1;
/* check cached mapping table
*/
entry_index = ssd->lkuptable[lba];
if (entry_index != -1) {
//cache hit
ssd->mtable[entry_index].ppa = ppa;
ssd->mtable[entry_index].dirty = true;
} else {
//cache miss
ssd->fmtable[lba] = ppa;
}
return 0;
}
uint32_t update_lba_list(uint32_t lba, SSD* ssd, STATS* stats, bool ismiss){ // LRU, update lba-Linked list for finding lru-lba as victim
uint32_t victim_lba = -1;
list<int>::iterator it;
bool find_flag = false;
if((int) lba_list.size() < ssd->mtable_size){
if (ismiss == false) {
it = find(lba_list.begin(), lba_list.end(), lba);
if (it != lba_list.end()){
lba_list.erase(it);
// printf("find lba\n");
}
}
if (BIP && (rand() < RAND_MAX / 32)){
lba_list.push_back(lba);
}else{
lba_list.push_front(lba);
}
}else{
if (ismiss){
victim_lba = lba_list.back();
lba_list.pop_back();
}
if(victim_lba == lba){
if (BIP && (rand() < RAND_MAX / 32)){
lba_list.push_back(lba);
}else{
lba_list.push_front(lba);
}
}else{
if (ismiss == false) {
it = find(lba_list.begin(), lba_list.end(), lba);
if (it != lba_list.end()){
lba_list.erase(it);
if (BIP && (rand() < RAND_MAX / 32)){
lba_list.push_back(lba);
}else{
lba_list.push_front(lba);
}
find_flag = true;
// printf("find lba\n");
}
}
if(!find_flag){
if (BIP && (rand() < RAND_MAX / 32)){
lba_list.push_back(lba);
}else{
lba_list.push_front(lba);
}
}
}
}
return victim_lba;
}
int m_lru(uint32_t lba, SSD* ssd, STATS* stats){
//TODO. Apply Hashing, too!
int victim_lba = update_lba_list(lba, ssd, stats, true);
// printf("victim_lba: %lld\n");
int enindex = -1;
/*
for (int i=0;i<ssd->mtable_size; i++) {
if ((int) ssd->mtable[i].lba == victim_lba) {
enindex = i;
if(ssd->mtable[i].dirty == true){
ssd->fmtable[ssd->mtable[i].lba] = ssd->mtable[enindex].ppa;
stats->writeback++;
}
break;
}
}*/
enindex = ssd->lkuptable[victim_lba];
if (enindex != -1){
if(ssd->mtable[enindex].dirty == true){
ssd->fmtable[ssd->mtable[enindex].lba] = ssd->mtable[enindex].ppa;
stats->writeback++;
}
}else if(enindex == -1){
//printf("someting is wrong in LRU!!!! victim lba: %d\n", victim_lba);
}
if(ssd->mtable[enindex].lba != UINT_MAX) ssd->lkuptable[ssd->mtable[enindex].lba] = -1;
ssd->mtable[enindex].dirty = false;
ssd->mtable[enindex].ppa = UINT_MAX;
ssd->mtable[enindex].lba = UINT_MAX;
//if (enindex == (ssd->mtable_size)) enindex = 0;
return enindex;
}
/* select victim using FIFO
*/
int findex=0;
int m_fifo(uint32_t lba, SSD* ssd, STATS* stats) {
if (ssd->mtable[findex].dirty == true) {
//writeback dirty data
//TODO scaling time value when accessing fmtable
ssd->fmtable[ssd->mtable[findex].lba] = ssd->mtable[findex].ppa;
stats->writeback++;
}
if(ssd->mtable[findex].lba != UINT_MAX) ssd->lkuptable[ssd->mtable[findex].lba] = -1;
ssd->mtable[findex].dirty = false;
ssd->mtable[findex].ppa = UINT_MAX;
ssd->mtable[findex].lba = UINT_MAX;
int ret = findex;
findex++;
if (findex == (ssd->mtable_size)) findex = 0;
return ret;
}
int m_rand(uint32_t lba, SSD* ssd, STATS* stats){
int rindex = rand()%(ssd->mtable_size);
if(ssd->mtable[rindex].dirty == true){
ssd->fmtable[ssd->mtable[rindex].lba] = ssd->mtable[rindex].ppa;
stats->writeback++;
}
if (ssd->mtable[rindex].lba != UINT_MAX) ssd->lkuptable[ssd->mtable[rindex].lba] = -1;
ssd->mtable[rindex].dirty = false;
ssd->mtable[rindex].ppa = UINT_MAX;
ssd->mtable[rindex].lba = UINT_MAX;
ssd->mtable[rindex].recently_used = false;
return rindex;
}
int n_findex=0;
int m_cnru(uint32_t lba, SSD* ssd, STATS* stats) {
int ret;
while(1){
if(ssd->mtable[n_findex].recently_used == true){
ssd->mtable[n_findex].recently_used = false;
n_findex++;
if (n_findex == (ssd->mtable_size)) n_findex = 0;
}else{
if (ssd->mtable[n_findex].dirty == true) {
//writeback dirty data
//TODO scaling time value when accessing fmtable
ssd->fmtable[ssd->mtable[n_findex].lba] = ssd->mtable[n_findex].ppa;
stats->writeback++;
}
if (ssd->mtable[n_findex].lba != UINT_MAX) ssd->lkuptable[ssd->mtable[n_findex].lba] = -1;
ssd->mtable[n_findex].dirty = false;
ssd->mtable[n_findex].ppa = UINT_MAX;
ssd->mtable[n_findex].lba = UINT_MAX;
ssd->mtable[n_findex].recently_used = false;
ret = n_findex;
n_findex++;
if (n_findex == (ssd->mtable_size)) n_findex = 0;
break;
}
}
return ret;
}
void read(uint32_t lba, SSD *ssd, STATS* stats) {
stats->read++;
//update_lba_list(lba, ssd, stats);
int mindex = check_mtable(lba, ssd, stats);
uint32_t ppa = ssd->mtable[mindex].ppa;
if (ppa == UINT_MAX) {
//printf("READ) There is no ppa info in LBA: %u\n", lba);
return;
}
//TODO add flash access latency
return;
}
void write(uint32_t lba, SSD* ssd, STATS* stats) {
stats->write++;
//update_lba_list(lba, ssd, stats);
int mindex = check_mtable(lba, ssd, stats);
uint32_t ppa = ssd->mtable[mindex].ppa;
if (ppa == target_ppa) printf("prev ppa is %u, lba: %u\n", target_ppa, lba);
if (ppa != UINT_MAX) {
invalidate_ppa(ppa, ssd);
}
uint32_t new_ppa = get_ppa(ssd, stats);
if (new_ppa == target_ppa) {
printf("lba: %u, prev ppa: %u, new ppa: %u\n", lba, ppa, new_ppa);
}
//printf("%d\n", ppa);
update_mtable(mindex, new_ppa, ssd);
validate_ppa(new_ppa, lba, ssd);
if (freeq.size() == 0) do_gc(ssd, stats);
//TODO add flash access latency
}
void trim(uint32_t lba, SSD* ssd, STATS* stats) {
stats->trim++;
int mindex = check_mtable(lba, ssd, stats);
uint32_t ppa = ssd->mtable[mindex].ppa;
if (ppa != UINT_MAX) invalidate_ppa(ppa, ssd);
update_mtable(mindex, UINT_MAX, ssd);
}
int submit_io(SSD *ssd, STATS *stats, user_request *req) {
int req_num = req->io_size/(int)PGSIZE;
if (req->op == 0) {
//read
for (int i=0;i<req_num;i++) read(req->lba+i, ssd, stats);
} else if (req->op == 1) {
//write
for (int i=0;i<req_num;i++) write(req->lba+i, ssd, stats);
} else if (req->op == 3) {
//discard
for (int i=0;i<req_num;i++) trim(req->lba+i, ssd, stats);
}else {
stats->trash++;
return 1;
}
return 0;
}