-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiopm.c
More file actions
577 lines (459 loc) · 14.9 KB
/
iopm.c
File metadata and controls
577 lines (459 loc) · 14.9 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
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
#include "iopm.h"
#include "main.h"
#include "error.h"
#include "iopm_map.h"
#include "init.h"
/* read pages started with 'start_LPN' */
void read(int start_LPN, int count) {
int i;
for (i = 0; i<count; i++) {
IOPM_read(start_LPN + i);
}
}
/* read LPN */
void IOPM_read(int LPN) {
/* count the read operation */
READ_count();
int partition = LPN2Partition(LPN, IO_READ);
int PPN;
if (partition == -1) {
// NULL read
}
else {
// calculate the physical address
PPN = Partition2PPN(LPN, partition, IO_READ);
//error check : LPN match with PPN?
error_LPN_PPN(LPN, PPN);
}
}
/* write 'count' pages started with 'start_LPN' */
void write(int start_LPN, int count) {
int i;
for (i = 0; i<count; i++) {
IOPM_write(start_LPN + i, 0);
}
}
/* write LPN
flag = 0 : I/O write
flag = 1 : I/O read
flag = 2 : write during PartitonGC
flag = 3 : write during BlockGC */
void IOPM_write(int LPN, int IO_type) {
/****** COUNTING ******/
WRITE_count(IO_type);
/*********************/
assert(free_partition_pool->free_flag == 1);
// variables
int overwrite_partition = -1;
int overwrite_PPN = -1;
int partition = -1;
/* Check Overwrite */
if (IO_type == IO_WRITE) {
overwrite_partition = LPN2Partition(LPN, IO_type);
if (overwrite_partition != -1) {
//partition_valid_check(overwrite_partition);
overwrite_PPN = Partition2PPN(LPN, overwrite_partition, IO_type);
}
}
/* Select the Stream*/
int stream = select_stream(LPN, IO_type);
int new_partition_flag = 0;
int new_block_flag = 0;
// need to allocate new partition
if (LPN <= SIT[stream].recentLPN || SIT[stream].recentLPN == -1 || (LPN - PVB[SIT[stream].recentPartition].startLPN) >= PAGE_PER_PARTITION) {
new_partition_flag = 1;
}
// need to allocate new block
if ((SIT[stream].recentPPN + 1) % PAGE_PER_BLOCK == 0) {
new_block_flag = 1;
}
if (new_block_flag == 1 && new_partition_flag == 1) {
// close stream
if (SIT[stream].recentPartition != -1) {
close_partition(stream, IO_type);
SIT[stream].recentPartition = -1;
SIT[stream].recentLPN = -1;
}
// allocate new partition
partition = allocate_partition(IO_type);
free_partition--;
int block = allocate_block();
PVB[partition].block[PVB[partition].blocknum] = block;
PVB[partition].startPPN = block * PAGE_PER_BLOCK;
PVB[partition].blocknum++;
SIT[stream].recentPPN = block * PAGE_PER_BLOCK;
PVB[partition].startLPN = LPN;
PVB[partition].startPPN = SIT[stream].recentPPN;
int cluster = allocate_partition_to_cluster(LPN, partition);
CLUSTER[cluster].num_partition++;
SIT[stream].recentPartition = partition;
//partition_valid_check(partition);
BIT[block].partition[BIT[block].num_partition] = partition;
BIT[block].num_partition++;
}
else if (new_block_flag == 0 && new_partition_flag == 1) {
assert(free_partition_pool->free_flag == 1);
// close stream
if (SIT[stream].recentPartition != -1) {
close_partition(stream, IO_type);
SIT[stream].recentPartition = -1;
SIT[stream].recentLPN = -1;
}
SIT[stream].recentPPN = SIT[stream].recentPPN + 1;
assert(free_partition_pool->free_flag == 1);
// allocate new partition
partition = allocate_partition(IO_type);
free_partition--;
int block = SIT[stream].recentPPN / PAGE_PER_BLOCK;
PVB[partition].startLPN = LPN;
PVB[partition].startPPN = SIT[stream].recentPPN;
PVB[partition].block[PVB[partition].blocknum] = SIT[stream].recentPPN/PAGE_PER_BLOCK;
PVB[partition].blocknum++;
assert(free_partition_pool->free_flag == 1);
int cluster = allocate_partition_to_cluster(LPN, partition);
CLUSTER[cluster].num_partition++;
//victim_partition_flag_check(cluster);
SIT[stream].recentPartition = partition;
//partition_valid_check(partition);
BIT[block].partition[BIT[block].num_partition] = partition;
BIT[block].num_partition++;
}
else if (new_block_flag == 1 && new_partition_flag == 0){
partition = SIT[stream].recentPartition;
int block = allocate_block();
PVB[partition].block[PVB[partition].blocknum] = block;
PVB[partition].blocknum++;
SIT[stream].recentPPN = block * PAGE_PER_BLOCK;
//partition_valid_check(partition);
BIT[block].partition[BIT[block].num_partition] = partition;
BIT[block].num_partition++;
}
else {
partition = SIT[stream].recentPartition;
SIT[stream].recentPPN++;
//partition_valid_check(partition);
}
// Set SIT
SIT[stream].recentLPN = LPN;
// Set PVB
insert_bitmap(partition, LPN-PVB[partition].startLPN);
PVB[partition].valid++;
// Set CLUSTER
CLUSTER[PVB[partition].startLPN / PAGE_PER_CLUSTER].valid++;
// Set PB
int block = SIT[stream].recentPPN / PAGE_PER_BLOCK;
int offset = SIT[stream].recentPPN % PAGE_PER_BLOCK;
assert(PB[block].PPN2LPN[offset] == -1);
PB[block].valid[offset] = 1;
PB[block].PPN2LPN[offset] = LPN;
//if(IO_type == IO_WRITE)
// victim_partition_flag_check(PVB[partition].startLPN / PAGE_PER_CLUSTER);
assert(free_partition_pool->free_flag == 1);
// invalid old data
if (overwrite_PPN != -1 && IO_type == IO_WRITE) {
COUNT.overwrite++;
// error check
error_LPN_PPN(LPN, overwrite_PPN);
// invalid PB
PB[overwrite_PPN/PAGE_PER_BLOCK].valid[overwrite_PPN % PAGE_PER_BLOCK] = 0;
PB[overwrite_PPN / PAGE_PER_BLOCK].PPN2LPN[overwrite_PPN % PAGE_PER_BLOCK] = -1;
// invalid PVB
PVB[overwrite_partition].valid--;
// BIT
BIT[overwrite_PPN / PAGE_PER_BLOCK].invalid++;
// invalid cluster
int cluster = PVB[overwrite_partition].startLPN / PAGE_PER_CLUSTER;
CLUSTER[cluster].valid--;
assert(free_partition_pool->free_flag == 1);
// victim_partition_flag_check(cluster);
//cluster_vicitm_valid(cluster);
if (PVB[overwrite_partition].valid == 0) {
invalid_page_cluster(LPN, overwrite_partition);
free_full_invalid_partition(overwrite_partition);
assert(free_partition_pool->free_flag == 1);
}
else {
// remove the cluster - VICTIM VALID인 경우 CHECK
invalid_page_cluster(LPN, overwrite_partition);
assert(free_partition_pool->free_flag == 1);
}
}
//partition_valid_check(partition);
// do partition gc
check_partition_gc(IO_type);
//SIT_debug();
}
#if 0
void BlockGC() {
/*******COUNTING******/
BLOCKGC_count();
/*********************/
/* Select victim block */
int victim_block = select_victim_block();
int i, j;
// get the startPPN in victim block's partition
_partition *temp = BIT[victim_block].partition;
//int *partition_startPPN = (int *)malloc(sizeof(int)*BIT[victim_block].num_partition);
int *partition_startOff = (int *)malloc(sizeof(int)*BIT[victim_block].num_partition);
int *partition_endOff = (int *)malloc(sizeof(int)*BIT[victim_block].num_partition);
int *copy_pages = (int *)malloc(sizeof(int *)* PAGE_PER_BLOCK);
int *index = (int *)malloc(sizeof(int)*BIT[victim_block].num_partition);
for (i = 0; i < BIT[victim_block].num_partition; i++) {
index[i] = -1;
}
int copy_pages_num = 0;
int partition_num = 0;
while (temp != NULL) {
if (PVB[temp->partition_num].startPPN / PAGE_PER_BLOCK != victim_block) {
partition_startOff[partition_num] = 0;
}
else {
partition_startOff[partition_num] = PVB[temp->partition_num].startPPN%PAGE_PER_BLOCK;
}
if (PVB[temp->partition_num].endPPN / PAGE_PER_BLOCK != victim_block) {
partition_endOff[partition_num] = PAGE_PER_BLOCK-1;
}
else {
partition_endOff[partition_num] = PVB[temp->partition_num].endPPN%PAGE_PER_BLOCK;
}
temp = temp->next;
partition_num++;
}
// copy the pages in each partition
for (i = 0; i < partition_num; i++) {
for (j = partition_startOff[i]; j <= partition_endOff[i]; j++) {
if (PB[victim_block].valid[j] == 1) {
if (index[i] == -1) {
index[i] = j;
}
copy_pages[copy_pages_num] = PB[victim_block].PPN2LPN[j];
}
}
}
for(i)
}
#endif
void BlockGC() {
/*******COUNTING******/
BLOCKGC_count();
/*********************/
int victim_block = 0;
int flag = 1; //flag == 0 : get the block in full invalid 1 : get the block in allocated
/* Select victim block */
victim_block = select_victim_block(&flag);
/* Copy the valid pages in Physical Block */
victim_page_num = 0;
if (BIT[victim_block].invalid != PAGE_PER_BLOCK) {
int i;
for (i = 0; i < PAGE_PER_BLOCK; i++) {
if (PB[victim_block].valid[i] == 1) {
BLOCKGC_READ_count();
victim_page_LPN[victim_page_num] = PB[victim_block].PPN2LPN[i];
victim_page_PPN[victim_page_num] = i + victim_block*PAGE_PER_BLOCK;
victim_page_num++;
}
}
}
/* Check that if block is active block*/
int i;
for (i = 0; i<NUMBER_STREAM; i++) {
int active_block = SIT[i].recentPPN / PAGE_PER_BLOCK;
if (active_block == victim_block) {
// close partition
if (SIT[i].recentPartition != -1) {
close_partition(i, BGC);
}
SIT[i].recentPartition = -1;
SIT[i].recentLPN = -1;
SIT[i].recentPPN = -1;
}
}
/* Copy the pages in victim block */
if (BIT[victim_block].invalid != PAGE_PER_BLOCK) {
// reorder the valid pages
quickSort(victim_page_LPN, victim_page_PPN, 0, victim_page_num - 1);
// write&remove the pages
for (i = 0; i < victim_page_num; i++) {
// write page
// 느리다면 여길 바꾸자.
int partition = LPN2Partition_limit(victim_page_LPN[i], victim_block, BGC);
assert(partition != -1);
IOPM_write(victim_page_LPN[i], BGC);
remove_page(partition, victim_page_PPN[i], 0);
}
}
/* invalid victim block in partition */
if(BIT[victim_block].num_partition != 0)
invalid_block_in_partition(victim_block);
/* Init BIT map */
BIT[victim_block].invalid = 0;
//BIT[victim_block].partition;
memset(BIT[victim_block].partition, -1, PAGE_PER_BLOCK);
BIT[victim_block].num_partition = 0;
/* Init PB structure*/
for (i = 0; i < PAGE_PER_BLOCK; i++) {
PB[victim_block].PPN2LPN[i] = -1;
PB[victim_block].valid[i] = -1;
}
/* remove block from allocate to free list*/
allocate2free_block_pool(victim_block, flag);
}
void PartitionGC() {
/*******COUNTING******/
PARTITIONGC_count();
/*********************/
int predict_copy = -1;
/* Select the victim cluster */
int victim_cluster = select_victim_cluster(&predict_copy);
/* Get the victim partitions/pages from cluster */
int first_page = -1;
int last_page = -1;
int num_victim_partition = 0;
_partition *temp_victim_partition = CLUSTER[victim_cluster].victim_partition_list;
victim_page_num = 0;
int copy_index = 0;
int free_partition_prev = free_partition;
//while (num_victim_partition < PARTITION_PER_CLUSTER + 2) {
while(1){
int victim_partition_num = temp_victim_partition->partition_num;
if (PVB[victim_partition_num].active_flag == 1) {
continue;
}
if (num_victim_partition >= PARTITION_PER_CLUSTER + 2) {
if (victim_page_num + PVB[victim_partition_num].valid > PAGE_PER_PARTITION / 4) {
break;
}
}
victim_partition[num_victim_partition] = victim_partition_num;
num_victim_partition++;
copy_index = victim_page_num;
//victim_partition[
// one block in partition
if (PVB[victim_partition_num].blocknum == 1) {
int block = PVB[victim_partition_num].block[0];
if (block != -2) {
int start_off = PVB[victim_partition_num].startPPN%PAGE_PER_BLOCK;
int end_off = PVB[victim_partition_num].endPPN%PAGE_PER_BLOCK;
int i;
for (i = start_off; i <= end_off; i++) {
if (PB[block].valid[i] == 1) {
victim_page_LPN[victim_page_num] = PB[block].PPN2LPN[i];
victim_page_PPN[victim_page_num] = block*PAGE_PER_BLOCK + i;
victim_page_num++;
PARTITIONGC_READ_count();
}
}
}
}
else {
int block = PVB[victim_partition_num].block[0];
int start_off = PVB[victim_partition_num].startPPN%PAGE_PER_BLOCK;
int end_off = PVB[victim_partition_num].endPPN%PAGE_PER_BLOCK;
int i;
if (block != -2) {
// first block
for (i = start_off; i < PAGE_PER_BLOCK; i++) {
if (PB[block].valid[i] == 1) {
victim_page_LPN[victim_page_num] = PB[block].PPN2LPN[i];
victim_page_PPN[victim_page_num] = block*PAGE_PER_BLOCK + i;
victim_page_num++;
PARTITIONGC_READ_count();
}
}
}
// middle
int j;
for (j = 1; j < PVB[victim_partition_num].blocknum - 1; j++) {
block = PVB[victim_partition_num].block[j];
if (block != -2) {
for (i = 0; i < PAGE_PER_BLOCK; i++) {
if (PB[block].valid[i] == 1) {
victim_page_LPN[victim_page_num] = PB[block].PPN2LPN[i];
victim_page_PPN[victim_page_num] = block*PAGE_PER_BLOCK + i;
victim_page_num++;
PARTITIONGC_READ_count();
}
}
}
}
// last block
block = PVB[victim_partition_num].block[PVB[victim_partition_num].blocknum-1];
if (block != -2) {
for (i = 0; i <= end_off; i++) {
if (PB[block].valid[i] == 1) {
victim_page_LPN[victim_page_num] = PB[block].PPN2LPN[i];
victim_page_PPN[victim_page_num] = block*PAGE_PER_BLOCK + i;
victim_page_num++;
PARTITIONGC_READ_count();
}
}
}
}
// Get the first page and last page
int temp_first = victim_page_LPN[copy_index];
int temp_last = victim_page_LPN[victim_page_num-1];
if (first_page == -1) {
first_page = temp_first;
}
else {
if (temp_first < first_page) {
first_page = temp_first;
}
}
if (last_page == -1) {
last_page = temp_last;
}
else {
if (last_page < temp_last) {
last_page = temp_last;
}
}
//first_page = (temp_first < first_page) ? temp_first : first_page;
//last_page = (temp_last < last_page) ? temp_last : last_page;
int need_partition;
if ((last_page - first_page + 1) % PAGE_PER_PARTITION == 0) {
need_partition = (last_page - first_page + 1) / PAGE_PER_PARTITION;
}
else {
need_partition = (last_page - first_page + 1) / PAGE_PER_PARTITION + 1;
}
if (need_partition < num_victim_partition) {
break;
}
temp_victim_partition = temp_victim_partition->next;
}
assert(victim_page_num <= predict_copy);
/* Write the copy pages */
quickSort(victim_page_LPN, victim_page_PPN, 0, victim_page_num - 1);
//victim_cluster_pool_cluster(victim_cluster);
int i;
for (i = 0; i < victim_page_num; i++) {
IOPM_write(victim_page_LPN[i], PGC);
// Invalid the page
PB[victim_page_PPN[i] / PAGE_PER_BLOCK].PPN2LPN[victim_page_PPN[i] % PAGE_PER_BLOCK] = -1;
PB[victim_page_PPN[i] / PAGE_PER_BLOCK].valid[victim_page_PPN[i] % PAGE_PER_BLOCK] = 0;
}
/* Remove the partition information */
for (i = 0; i < num_victim_partition; i++) {
int temp_victim_partition = victim_partition[i];
//PVB[temp_victim_partition].valid--;
int block_num = 0;
int j;
// remove partition in block
for (j = 0; j < PVB[temp_victim_partition].blocknum; j++) {
if (PVB[temp_victim_partition].block[j] != -2) {
remove_block_in_partition[block_num] = PVB[temp_victim_partition].block[j];
block_num++;
}
}
// remove blocks in partition(PVB)
remove_partition_in_block(temp_victim_partition, remove_block_in_partition, block_num);
// cluster 재조정
remove_partition_from_cluster(temp_victim_partition, victim_cluster, PGC);
// init PVB
init_Partition(temp_victim_partition);
free_partition++;
}
//victim_partition_flag_check(victim_cluster);
assert(free_partition_prev < free_partition);
}