-
Notifications
You must be signed in to change notification settings - Fork 13
/
flash.c
4742 lines (4270 loc) · 275 KB
/
flash.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************************************************************
This project was supported by the National Basic Research 973 Program of China under Grant No.2011CB302301
Huazhong University of Science and Technology (HUST) Wuhan National Laboratory for Optoelectronics
FileName: flash.c
Author: Hu Yang Version: 2.1 Date:2011/12/02
Description:
History:
<contributor> <time> <version> <desc> <e-mail>
Yang Hu 2009/09/25 1.0 Creat SSDsim [email protected]
2010/05/01 2.x Change
Zhiming Zhu 2011/07/01 2.0 Change [email protected]
Shuangwu Zhang 2011/11/01 2.1 Change [email protected]
Chao Ren 2011/07/01 2.0 Change [email protected]
Hao Luo 2011/01/01 2.0 Change [email protected]
*****************************************************************************************************************************/
#include "flash.h"
#include "inttypes.h"
int ceiling(int64_t x, int64_t y) {
return (x + y - 1)/y;
}
//get candidate location info for lpn that has not be written (different from find_location)
void get_location(struct ssd_info*ssd, unsigned int *channel, unsigned int *chip, unsigned int lpn, int parity) {
unsigned int channel_num=0,chip_num=0,die_num=0,plane_num=0;
channel_num=ssd->parameter->channel_number;
chip_num=ssd->parameter->chip_channel[0];
die_num=ssd->parameter->die_chip;
plane_num=ssd->parameter->plane_die;
if (ssd->parameter->adaptive_read || ssd->parameter->raid)
channel_num -= 1;
*channel=lpn%channel_num;
*chip=(lpn/(plane_num*die_num*channel_num))%chip_num;
if (ssd->parameter->adaptive_read || ssd->parameter->raid) {
int parity_channel = (7 - *chip) - (lpn/(channel_num*chip_num))%(channel_num+1);
if (parity_channel < 0)
parity_channel = 8 + parity_channel;
if (parity) {
*channel = parity_channel;
} else {
if ((int)(*channel) > parity_channel - 1)
(*channel)++;
}
}
}
/**********************
*这个函数只作用于写请求
***********************/
//Status allocate_location(struct ssd_info * ssd ,struct sub_request *sub_req)
Status allocate_location(struct ssd_info * ssd ,struct sub_request *sub_req, int parity)
{
struct sub_request * update=NULL;
unsigned int channel_num=0,chip_num=0,die_num=0,plane_num=0;
struct local *location=NULL;
channel_num=ssd->parameter->channel_number;
chip_num=ssd->parameter->chip_channel[0];
die_num=ssd->parameter->die_chip;
plane_num=ssd->parameter->plane_die;
if (ssd->parameter->allocation_scheme==0) /*动态分配的情况*/
{
/******************************************************************
* 在动态分配中,因为页的更新操作使用不了copyback操作,
*需要产生一个读请求,并且只有这个读请求完成后才能进行这个页的写操作
*******************************************************************/
if (ssd->dram->map->map_entry[sub_req->lpn].state!=0)
{
if ((sub_req->state&ssd->dram->map->map_entry[sub_req->lpn].state)!=ssd->dram->map->map_entry[sub_req->lpn].state)
{
ssd->read_count++;
ssd->update_read_count++;
update=(struct sub_request *)malloc(sizeof(struct sub_request));
alloc_assert(update,"update");
memset(update,0, sizeof(struct sub_request));
if(update==NULL)
{
return ERROR;
}
update->location=NULL;
update->next_node=NULL;
update->next_subs=NULL;
update->update=NULL;
location = find_location(ssd,ssd->dram->map->map_entry[sub_req->lpn].pn);
update->location=location;
update->begin_time = ssd->current_time;
update->current_state = SR_WAIT;
update->current_time=MAX_INT64;
update->next_state = SR_R_C_A_TRANSFER;
update->next_state_predict_time=MAX_INT64;
update->lpn = sub_req->lpn;
update->state=((ssd->dram->map->map_entry[sub_req->lpn].state^sub_req->state)&0x7fffffff);
update->size=size(update->state);
update->ppn = ssd->dram->map->map_entry[sub_req->lpn].pn;
update->operation = READ;
if (ssd->channel_head[location->channel].subs_r_tail!=NULL) /*产生新的读请求,并且挂到channel的subs_r_tail队列尾*/
{
ssd->channel_head[location->channel].subs_r_tail->next_node=update;
ssd->channel_head[location->channel].subs_r_tail=update;
}
else
{
ssd->channel_head[location->channel].subs_r_tail=update;
ssd->channel_head[location->channel].subs_r_head=update;
}
}
}
/***************************************
*一下是动态分配的几种情况
*0:全动态分配
*1:表示channel定package,die,plane动态
****************************************/
switch(ssd->parameter->dynamic_allocation)
{
case 0:
{
sub_req->location->channel=-1;
sub_req->location->chip=-1;
sub_req->location->die=-1;
sub_req->location->plane=-1;
sub_req->location->block=-1;
sub_req->location->page=-1;
if (ssd->subs_w_tail!=NULL)
{
ssd->subs_w_tail->next_node=sub_req;
ssd->subs_w_tail=sub_req;
}
else
{
ssd->subs_w_tail=sub_req;
ssd->subs_w_head=sub_req;
}
if (update!=NULL)
{
sub_req->update=update;
}
break;
}
case 1:
{
sub_req->location->channel=sub_req->lpn%ssd->parameter->channel_number;
sub_req->location->chip=-1;
sub_req->location->die=-1;
sub_req->location->plane=-1;
sub_req->location->block=-1;
sub_req->location->page=-1;
if (update!=NULL)
{
sub_req->update=update;
}
break;
}
case 2:
{
break;
}
case 3:
{
break;
}
}
}
else
{ /***************************************************************************
*是静态分配方式,所以可以将这个子请求的最终channel,chip,die,plane全部得出
*总共有0,1,2,3,4,5,这六种静态分配方式。
****************************************************************************/
switch (ssd->parameter->static_allocation)
{
case 0: //no striping static allocation
{
sub_req->location->channel=(sub_req->lpn/(plane_num*die_num*chip_num))%channel_num;
sub_req->location->chip=sub_req->lpn%chip_num;
sub_req->location->die=(sub_req->lpn/chip_num)%die_num;
sub_req->location->plane=(sub_req->lpn/(die_num*chip_num))%plane_num;
break;
}
case 1:
{
sub_req->location->channel=sub_req->lpn%channel_num;
sub_req->location->chip=(sub_req->lpn/channel_num)%chip_num;
sub_req->location->die=(sub_req->lpn/(chip_num*channel_num))%die_num;
sub_req->location->plane=(sub_req->lpn/(die_num*chip_num*channel_num))%plane_num;
break;
}
case 2:
{
sub_req->location->channel=sub_req->lpn%channel_num;
sub_req->location->chip=(sub_req->lpn/(plane_num*channel_num))%chip_num;
sub_req->location->die=(sub_req->lpn/(plane_num*chip_num*channel_num))%die_num;
sub_req->location->plane=(sub_req->lpn/channel_num)%plane_num;
break;
}
case 3:
{
sub_req->location->channel=sub_req->lpn%channel_num;
sub_req->location->chip=(sub_req->lpn/(die_num*channel_num))%chip_num;
sub_req->location->die=(sub_req->lpn/channel_num)%die_num;
sub_req->location->plane=(sub_req->lpn/(die_num*chip_num*channel_num))%plane_num;
break;
}
case 4:
{
sub_req->location->channel=sub_req->lpn%channel_num;
sub_req->location->chip=(sub_req->lpn/(plane_num*die_num*channel_num))%chip_num;
sub_req->location->die=(sub_req->lpn/(plane_num*channel_num))%die_num;
sub_req->location->plane=(sub_req->lpn/channel_num)%plane_num;
break;
}
case 5:
{
//builds a raid if raid is set or adaptive_read is enabled
if (ssd->parameter->adaptive_read || ssd->parameter->raid)
channel_num -= 1;
sub_req->location->channel=sub_req->lpn%channel_num;
sub_req->location->chip=(sub_req->lpn/(plane_num*die_num*channel_num))%chip_num;
sub_req->location->die=(sub_req->lpn/channel_num)%die_num;
sub_req->location->plane=(sub_req->lpn/(die_num*channel_num))%plane_num;
if (ssd->parameter->adaptive_read || ssd->parameter->raid) {
int parity_channel = (7 - sub_req->location->chip) - (sub_req->lpn/(channel_num*chip_num))%(channel_num+1);
if (parity_channel < 0)
parity_channel = 8 + parity_channel;
if (parity) {
sub_req->location->channel = parity_channel;
} else {
if ((int)(sub_req->location->channel) > parity_channel - 1)
sub_req->location->channel++;
}
if (parity)
sub_req->lpn = (sub_req->lpn-6)/7;
//printf("allocate_location/channel: %d, chip: %d, parity: %d, lpn: %d\n", sub_req->location->channel, sub_req->location->chip, parity, sub_req->lpn);
ssd->dram->gc_monitor[sub_req->location->channel][sub_req->location->chip].write_interval = ssd->current_time - ssd->dram->gc_monitor[sub_req->location->channel][sub_req->location->chip].last_write_time;
ssd->dram->gc_monitor[sub_req->location->channel][sub_req->location->chip].last_write_time = ssd->current_time;
}
if (ssd->parameter->nonblocking_gc)
sub_req->next_state = SR_W_TRANSFER; //not initialized in original code, but will be checked later
break;
}
default : return ERROR;
}
struct entry e;
if (parity)
e = ssd->dram->map->parity_entry[sub_req->lpn];
else
e = ssd->dram->map->map_entry[sub_req->lpn];
//if (ssd->dram->map->map_entry[sub_req->lpn].state!=0)
if (e.state!=0)
{
// read-modify-write
/*这个写回的子请求的逻辑页不可以覆盖之前被写回的数据 需要产生读请求*/
//if ((sub_req->state&ssd->dram->map->map_entry[sub_req->lpn].state)!=ssd->dram->map->map_entry[sub_req->lpn].state)
if ((sub_req->state&e.state)!=e.state && (ssd->parameter->advanced_commands&AD_COPYBACK)!=AD_COPYBACK) //with AD_COPYBACK, read can be processed without taking up channel (i.e., read to register on chip and merged with write data, then write back to flash). So, we don't have to add read sub_request
{
ssd->read_count++;
ssd->update_read_count++;
update=(struct sub_request *)malloc(sizeof(struct sub_request));
alloc_assert(update,"update");
memset(update,0, sizeof(struct sub_request));
if(update==NULL)
{
return ERROR;
}
update->location=NULL;
update->next_node=NULL;
update->next_subs=NULL;
update->update=NULL;
//location = find_location(ssd,ssd->dram->map->map_entry[sub_req->lpn].pn);
location = find_location(ssd,e.pn);
update->location=location;
update->begin_time = ssd->current_time;
update->current_state = SR_WAIT;
update->current_time=MAX_INT64;
update->next_state = SR_R_C_A_TRANSFER;
update->next_state_predict_time=MAX_INT64;
update->lpn = sub_req->lpn;
//update->state=((ssd->dram->map->map_entry[sub_req->lpn].state^sub_req->state)&0x7fffffff);
update->state=((e.state^sub_req->state)&0x7fffffff);
update->size=size(update->state);
//update->ppn = ssd->dram->map->map_entry[sub_req->lpn].pn;
update->ppn = e.pn;
update->operation = READ;
if (ssd->channel_head[location->channel].subs_r_tail!=NULL)
{
ssd->channel_head[location->channel].subs_r_tail->next_node=update;
ssd->channel_head[location->channel].subs_r_tail=update;
}
else
{
ssd->channel_head[location->channel].subs_r_tail=update;
ssd->channel_head[location->channel].subs_r_head=update;
}
}
if (update!=NULL)
{
sub_req->update=update;
sub_req->state=(sub_req->state|update->state);
sub_req->size=size(sub_req->state);
}
}
}
if ((ssd->parameter->allocation_scheme!=0)||(ssd->parameter->dynamic_allocation!=0))
{
if (ssd->channel_head[sub_req->location->channel].subs_w_tail!=NULL)
{
ssd->channel_head[sub_req->location->channel].subs_w_tail->next_node=sub_req;
ssd->channel_head[sub_req->location->channel].subs_w_tail=sub_req;
}
else
{
ssd->channel_head[sub_req->location->channel].subs_w_tail=sub_req;
ssd->channel_head[sub_req->location->channel].subs_w_head=sub_req;
}
}
return SUCCESS;
}
/*******************************************************************************
*insert2buffer这个函数是专门为写请求分配子请求服务的在buffer_management中被调用。
********************************************************************************/
struct ssd_info *insert2buffer(struct ssd_info *ssd,unsigned int lpn,int state,struct sub_request *sub,struct request *req)
{
int write_back_count,flag=0; /*flag表示为写入新数据腾空间是否完成,0表示需要进一步腾,1表示已经腾空*/
unsigned int i,lsn,hit_flag,add_flag,sector_count,active_region_flag=0,free_sector=0;
struct buffer_group *buffer_node=NULL,*pt,*new_node=NULL,key;
struct sub_request *sub_req=NULL,*update=NULL;
unsigned int sub_req_state=0, sub_req_size=0,sub_req_lpn=0;
//printf("enter insert2buffer, current time:%lld, lpn:%d, state:%d,\n",ssd->current_time,lpn,state);
sector_count=size(state); /*需要写到buffer的sector个数*/
key.group=lpn;
buffer_node=(struct buffer_group*)avlTreeFind(ssd->dram->buffer, (TREE_NODE *)&key); /*在平衡二叉树中寻找buffer node*/
/************************************************************************************************
*没有命中。
*第一步根据这个lpn有多少子页需要写到buffer,去除已写回的lsn,为该lpn腾出位置,
*首先即要计算出free sector(表示还有多少可以直接写的buffer节点)。
*如果free_sector>=sector_count,即有多余的空间够lpn子请求写,不需要产生写回请求
*否则,没有多余的空间供lpn子请求写,这时需要释放一部分空间,产生写回请求。就要creat_sub_request()
*************************************************************************************************/
if(buffer_node==NULL)
{
free_sector=ssd->dram->buffer->max_buffer_sector-ssd->dram->buffer->buffer_sector_count;
if(free_sector>=sector_count)
{
flag=1;
}
if(flag==0)
{
write_back_count=sector_count-free_sector;
ssd->dram->buffer->write_miss_hit=ssd->dram->buffer->write_miss_hit+write_back_count;
while(write_back_count>0)
{
sub_req=NULL;
sub_req_state=ssd->dram->buffer->buffer_tail->stored;
sub_req_size=size(ssd->dram->buffer->buffer_tail->stored);
sub_req_lpn=ssd->dram->buffer->buffer_tail->group;
sub_req=creat_sub_request(ssd,sub_req_lpn,sub_req_size,sub_req_state,req,WRITE,0,0);
/**********************************************************************************
*req不为空,表示这个insert2buffer函数是在buffer_management中调用,传递了request进来
*req为空,表示这个函数是在process函数中处理一对多映射关系的读的时候,需要将这个读出
*的数据加到buffer中,这可能产生实时的写回操作,需要将这个实时的写回操作的子请求挂在
*这个读请求的总请求上
***********************************************************************************/
if(req!=NULL)
{
}
else
{
sub_req->next_subs=sub->next_subs;
sub->next_subs=sub_req;
}
/*********************************************************************
*写请求插入到了平衡二叉树,这时就要修改dram的buffer_sector_count;
*维持平衡二叉树调用avlTreeDel()和AVL_TREENODE_FREE()函数;维持LRU算法;
**********************************************************************/
ssd->dram->buffer->buffer_sector_count=ssd->dram->buffer->buffer_sector_count-sub_req->size;
pt = ssd->dram->buffer->buffer_tail;
avlTreeDel(ssd->dram->buffer, (TREE_NODE *) pt);
if(ssd->dram->buffer->buffer_head->LRU_link_next == NULL){
ssd->dram->buffer->buffer_head = NULL;
ssd->dram->buffer->buffer_tail = NULL;
}else{
ssd->dram->buffer->buffer_tail=ssd->dram->buffer->buffer_tail->LRU_link_pre;
ssd->dram->buffer->buffer_tail->LRU_link_next=NULL;
}
pt->LRU_link_next=NULL;
pt->LRU_link_pre=NULL;
AVL_TREENODE_FREE(ssd->dram->buffer, (TREE_NODE *) pt);
pt = NULL;
write_back_count=write_back_count-sub_req->size; /*因为产生了实时写回操作,需要将主动写回操作区域增加*/
}
}
/******************************************************************************
*生成一个buffer node,根据这个页的情况分别赋值个各个成员,添加到队首和二叉树中
*******************************************************************************/
new_node=NULL;
new_node=(struct buffer_group *)malloc(sizeof(struct buffer_group));
alloc_assert(new_node,"buffer_group_node");
memset(new_node,0, sizeof(struct buffer_group));
new_node->group=lpn;
new_node->stored=state;
new_node->dirty_clean=state;
new_node->LRU_link_pre = NULL;
new_node->LRU_link_next=ssd->dram->buffer->buffer_head;
if(ssd->dram->buffer->buffer_head != NULL){
ssd->dram->buffer->buffer_head->LRU_link_pre=new_node;
}else{
ssd->dram->buffer->buffer_tail = new_node;
}
ssd->dram->buffer->buffer_head=new_node;
new_node->LRU_link_pre=NULL;
avlTreeAdd(ssd->dram->buffer, (TREE_NODE *) new_node);
ssd->dram->buffer->buffer_sector_count += sector_count;
}
/****************************************************************************************
*在buffer中命中的情况
*虽然命中了,但是命中的只是lpn,有可能新来的写请求,只是需要写lpn这一page的某几个sub_page
*这时有需要进一步的判断
*****************************************************************************************/
else
{
for(i=0;i<ssd->parameter->subpage_page;i++)
{
/*************************************************************
*判断state第i位是不是1
*并且判断第i个sector是否存在buffer中,1表示存在,0表示不存在。
**************************************************************/
if((state>>i)%2!=0)
{
lsn=lpn*ssd->parameter->subpage_page+i;
hit_flag=0;
hit_flag=(buffer_node->stored)&(0x00000001<<i);
if(hit_flag!=0) /*命中了,需要将该节点移到buffer的队首,并且将命中的lsn进行标记*/
{
active_region_flag=1; /*用来记录在这个buffer node中的lsn是否被命中,用于后面对阈值的判定*/
if(req!=NULL)
{
if(ssd->dram->buffer->buffer_head!=buffer_node)
{
if(ssd->dram->buffer->buffer_tail==buffer_node)
{
ssd->dram->buffer->buffer_tail=buffer_node->LRU_link_pre;
buffer_node->LRU_link_pre->LRU_link_next=NULL;
}
else if(buffer_node != ssd->dram->buffer->buffer_head)
{
buffer_node->LRU_link_pre->LRU_link_next=buffer_node->LRU_link_next;
buffer_node->LRU_link_next->LRU_link_pre=buffer_node->LRU_link_pre;
}
buffer_node->LRU_link_next=ssd->dram->buffer->buffer_head;
ssd->dram->buffer->buffer_head->LRU_link_pre=buffer_node;
buffer_node->LRU_link_pre=NULL;
ssd->dram->buffer->buffer_head=buffer_node;
}
ssd->dram->buffer->write_hit++;
req->complete_lsn_count++; /*关键 当在buffer中命中时 就用req->complete_lsn_count++表示往buffer中写了数据。*/
}
else
{
}
}
else
{
/************************************************************************************************************
*该lsn没有命中,但是节点在buffer中,需要将这个lsn加到buffer的对应节点中
*从buffer的末端找一个节点,将一个已经写回的lsn从节点中删除(如果找到的话),更改这个节点的状态,同时将这个新的
*lsn加到相应的buffer节点中,该节点可能在buffer头,不在的话,将其移到头部。如果没有找到已经写回的lsn,在buffer
*节点找一个group整体写回,将这个子请求挂在这个请求上。可以提前挂在一个channel上。
*第一步:将buffer队尾的已经写回的节点删除一个,为新的lsn腾出空间,这里需要修改队尾某节点的stored状态这里还需要
* 增加,当没有可以之间删除的lsn时,需要产生新的写子请求,写回LRU最后的节点。
*第二步:将新的lsn加到所述的buffer节点中。
*************************************************************************************************************/
ssd->dram->buffer->write_miss_hit++;
if(ssd->dram->buffer->buffer_sector_count>=ssd->dram->buffer->max_buffer_sector)
{
if (buffer_node==ssd->dram->buffer->buffer_tail) /*如果命中的节点是buffer中最后一个节点,交换最后两个节点*/
{
pt = ssd->dram->buffer->buffer_tail->LRU_link_pre;
ssd->dram->buffer->buffer_tail->LRU_link_pre=pt->LRU_link_pre;
ssd->dram->buffer->buffer_tail->LRU_link_pre->LRU_link_next=ssd->dram->buffer->buffer_tail;
ssd->dram->buffer->buffer_tail->LRU_link_next=pt;
pt->LRU_link_next=NULL;
pt->LRU_link_pre=ssd->dram->buffer->buffer_tail;
ssd->dram->buffer->buffer_tail=pt;
}
sub_req=NULL;
sub_req_state=ssd->dram->buffer->buffer_tail->stored;
sub_req_size=size(ssd->dram->buffer->buffer_tail->stored);
sub_req_lpn=ssd->dram->buffer->buffer_tail->group;
sub_req=creat_sub_request(ssd,sub_req_lpn,sub_req_size,sub_req_state,req,WRITE,0,0);
if(req!=NULL)
{
}
else if(req==NULL)
{
sub_req->next_subs=sub->next_subs;
sub->next_subs=sub_req;
}
ssd->dram->buffer->buffer_sector_count=ssd->dram->buffer->buffer_sector_count-sub_req->size;
pt = ssd->dram->buffer->buffer_tail;
avlTreeDel(ssd->dram->buffer, (TREE_NODE *) pt);
/************************************************************************/
/* 改: 挂在了子请求,buffer的节点不应立即删除, */
/* 需等到写回了之后才能删除 */
/************************************************************************/
if(ssd->dram->buffer->buffer_head->LRU_link_next == NULL)
{
ssd->dram->buffer->buffer_head = NULL;
ssd->dram->buffer->buffer_tail = NULL;
}else{
ssd->dram->buffer->buffer_tail=ssd->dram->buffer->buffer_tail->LRU_link_pre;
ssd->dram->buffer->buffer_tail->LRU_link_next=NULL;
}
pt->LRU_link_next=NULL;
pt->LRU_link_pre=NULL;
AVL_TREENODE_FREE(ssd->dram->buffer, (TREE_NODE *) pt);
pt = NULL;
}
/*第二步:将新的lsn加到所述的buffer节点中*/
add_flag=0x00000001<<(lsn%ssd->parameter->subpage_page);
if(ssd->dram->buffer->buffer_head!=buffer_node) /*如果该buffer节点不在buffer的队首,需要将这个节点提到队首*/
{
if(ssd->dram->buffer->buffer_tail==buffer_node)
{
buffer_node->LRU_link_pre->LRU_link_next=NULL;
ssd->dram->buffer->buffer_tail=buffer_node->LRU_link_pre;
}
else
{
buffer_node->LRU_link_pre->LRU_link_next=buffer_node->LRU_link_next;
buffer_node->LRU_link_next->LRU_link_pre=buffer_node->LRU_link_pre;
}
buffer_node->LRU_link_next=ssd->dram->buffer->buffer_head;
ssd->dram->buffer->buffer_head->LRU_link_pre=buffer_node;
buffer_node->LRU_link_pre=NULL;
ssd->dram->buffer->buffer_head=buffer_node;
}
buffer_node->stored=buffer_node->stored|add_flag;
buffer_node->dirty_clean=buffer_node->dirty_clean|add_flag;
ssd->dram->buffer->buffer_sector_count++;
}
}
}
}
return ssd;
}
/**************************************************************************************
*函数的功能是寻找活跃块,应为每个plane中都只有一个活跃块,只有这个活跃块中才能进行操作
***************************************************************************************/
Status find_active_block(struct ssd_info *ssd,unsigned int channel,unsigned int chip,unsigned int die,unsigned int plane)
{
unsigned int active_block;
unsigned int free_page_num=0;
unsigned int count=0;
active_block=ssd->channel_head[channel].chip_head[chip].die_head[die].plane_head[plane].active_block;
free_page_num=ssd->channel_head[channel].chip_head[chip].die_head[die].plane_head[plane].blk_head[active_block].free_page_num;
//last_write_page=ssd->channel_head[channel].chip_head[chip].die_head[die].plane_head[plane].blk_head[active_block].free_page_num;
while((free_page_num==0)&&(count<ssd->parameter->block_plane))
{
active_block=(active_block+1)%ssd->parameter->block_plane;
free_page_num=ssd->channel_head[channel].chip_head[chip].die_head[die].plane_head[plane].blk_head[active_block].free_page_num;
count++;
}
ssd->channel_head[channel].chip_head[chip].die_head[die].plane_head[plane].active_block=active_block;
if(count<ssd->parameter->block_plane)
{
return SUCCESS;
}
else
{
return FAILURE;
}
}
/*************************************************
*这个函数的功能就是一个模拟一个实实在在的写操作
*就是更改这个page的相关参数,以及整个ssd的统计参数
**************************************************/
Status write_page(struct ssd_info *ssd,unsigned int channel,unsigned int chip,unsigned int die,unsigned int plane,unsigned int active_block,unsigned int *ppn)
{
int last_write_page=0;
last_write_page=++(ssd->channel_head[channel].chip_head[chip].die_head[die].plane_head[plane].blk_head[active_block].last_write_page);
if(last_write_page>=(int)ssd->parameter->page_block)
{
ssd->channel_head[channel].chip_head[chip].die_head[die].plane_head[plane].blk_head[active_block].last_write_page=0;
printf("error! the last write page larger than %d!!\n",ssd->parameter->page_block);
//return ERROR;
exit(1);
}
ssd->channel_head[channel].chip_head[chip].die_head[die].plane_head[plane].blk_head[active_block].free_page_num--;
ssd->channel_head[channel].chip_head[chip].die_head[die].plane_head[plane].free_page--;
ssd->channel_head[channel].chip_head[chip].die_head[die].plane_head[plane].blk_head[active_block].page_head[last_write_page].written_count++;
ssd->write_flash_count++;
*ppn=find_ppn(ssd,channel,chip,die,plane,active_block,last_write_page);
return SUCCESS;
}
/**********************************************
*这个函数的功能是根据lpn,size,state创建子请求
**********************************************/
struct sub_request *creat_sub_request(struct ssd_info * ssd,unsigned int lpn,int size,unsigned int state,struct request * req,unsigned int operation, int parity, int seq)
{
//struct sub_request* sub=NULL,* sub_r=NULL;
struct sub_request* sub=NULL,* sub_r=NULL,* sub_m=NULL;
struct channel_info * p_ch=NULL;
struct local * loc=NULL;
unsigned int flag=0;
sub = (struct sub_request*)malloc(sizeof(struct sub_request)); /*申请一个子请求的结构*/
alloc_assert(sub,"sub_request");
memset(sub,0, sizeof(struct sub_request));
if(sub==NULL)
return NULL;
sub->location=NULL;
sub->next_node=NULL;
sub->next_subs=NULL;
sub->update=NULL;
sub->parity_read = NULL;
if(req!=NULL)
{
sub->next_subs = req->subs;
req->subs = sub;
//printf("req->subs: %x\n", req->subs);
}
/*************************************************************************************
*在读操作的情况下,有一点非常重要就是要预先判断读子请求队列中是否有与这个子请求相同的,
*有的话,新子请求就不必再执行了,将新的子请求直接赋为完成
**************************************************************************************/
if (operation == READ)
{
struct entry e;
if (parity) {
sub->parity = 1;
e = ssd->dram->map->parity_entry[lpn];
} else {
sub->parity = 0;
e = ssd->dram->map->map_entry[lpn];
}
//loc = find_location(ssd,ssd->dram->map->map_entry[lpn].pn);
loc = find_location(ssd,e.pn);
sub->location=loc;
sub->begin_time = ssd->current_time;
sub->current_state = SR_WAIT;
sub->current_time= MAX_INT64;
sub->next_state = SR_R_C_A_TRANSFER;
sub->next_state_predict_time=MAX_INT64;
sub->lpn = lpn;
sub->size=size; /*需要计算出该子请求的请求大小*/
//printf("creat_sub_request/parity: %d, channel: %d, chip: %d, lpn %d, ppn %d, size: %d\n", sub->parity, sub->location->channel, sub->location->chip, lpn, e.pn, size);
//calculate the index to record statistics of GC/column. first_sn = req->lsn/(ssd->parameter->subpage_page*(ssd->parameter->channel_number-1))
unsigned int i = sub->parity ? sub->lpn - req->lsn/(ssd->parameter->subpage_page*(ssd->parameter->channel_number-1)) : sub->lpn/(ssd->parameter->channel_number - 1) - req->lsn/(ssd->parameter->subpage_page*(ssd->parameter->channel_number-1));
if (!ssd->parameter->raid)
i = sub->lpn/ssd->parameter->channel_number - req->lsn/(ssd->parameter->subpage_page*ssd->parameter->channel_number);
if (ssd->parameter->nonblocking_gc) {
//either there is a nonblocking GC going or erase opeartion for that chip (could be direct erase)
if (ssd->dram->gc_monitor[loc->channel][loc->chip].next_state_predict_time || ssd->channel_head[loc->channel].gc_chip==loc->chip) {
//printf("i: %lld, sub->lpn: %lld, req->lsn: %lld\n", i, sub->lpn, req->lsn);
//if (ssd->dram->gc_monitor[loc->channel][loc->chip].next_state_predict_time) {
if (req->time > ssd->stats_time)
req->gc_count[i]++;
//if there is no sub requests have been dropped before, we can drop 1 sub request and read parity instead
if (req->sub_req_dropped[i] == 0 && ssd->parameter->adaptive_read && seq) {//only works for a RAID group read
//if there are multiple GC happening across channels, we drop the GC that takes the most of time
if (loc->channel == ssd->dram->gc_max[loc->chip].channel || ssd->parameter->rotating_gc) {
req->sub_req_dropped[i]++;
req->gc_count[i]--;
req->subs=sub->next_subs;
free(sub->location);
free(sub);
return NULL;
}
}
//printf("parity: %d, channel: %d, chip: %d, sub->lpn: %d, req->lsn/8: %d, gc_count[%d]: %d\n", sub->parity, loc->channel, loc->chip, sub->lpn, req->lsn/8, i, req->gc_count[i]);
}
} else {
int j;
for (j = 0; j < 8; j++) //hardcoded. for a channel, there are 8 chips
if ((j != loc->chip && ssd->dram->gc_monitor[loc->channel][j].next_state_predict_time - ssd->parameter->time_characteristics.tBERS > 0) || (j == loc->chip && ssd->dram->gc_monitor[loc->channel][j].next_state_predict_time)) {
if (req->time > ssd->stats_time)
req->gc_count[i]++;//either channel is blocked due to copybacks or chip to read is doing GC
break;
}
}
//if no sub requests have been dropped, then we don't create extra read sub request to parity
if (sub->parity && req->sub_req_dropped[i] == 0 && ssd->parameter->adaptive_read && seq) {
req->subs=sub->next_subs;
free(sub->location);
free(sub);
return NULL;
}
p_ch = &ssd->channel_head[loc->channel];
//sub->ppn = ssd->dram->map->map_entry[lpn].pn;
sub->ppn = e.pn;
sub->operation = READ;
//sub->state=(ssd->dram->map->map_entry[lpn].state&0x7fffffff);
sub->state=(e.state&0x7fffffff);
sub_r=p_ch->subs_r_head;
/*以下几行包括flag用于判断该读子请求队列中是否有与这个子请求相同的,有的话,将新的子请求直接赋为完成*/
flag=0;
while (sub_r!=NULL)
{
if (sub_r->ppn==sub->ppn)
{
flag=1;
break;
}
sub_r=sub_r->next_node;
}
if (flag==0)
{
if (p_ch->subs_r_tail!=NULL)
{
p_ch->subs_r_tail->next_node=sub;
p_ch->subs_r_tail=sub;
}
else
{
p_ch->subs_r_head=sub;
p_ch->subs_r_tail=sub;
}
}
else
{
sub->current_state = SR_R_DATA_TRANSFER;
sub->current_time=ssd->current_time;
sub->next_state = SR_COMPLETE;
sub->next_state_predict_time=ssd->current_time+1000;
sub->complete_time=ssd->current_time+1000;
}
}
/*************************************************************************************
*写请求的情况下,就需要利用到函数allocate_location(ssd ,sub)来处理静态分配和动态分配了
**************************************************************************************/
else if(operation == WRITE)
{
//sub->ppn=0;
sub->ppn = -1; //-1 means not allocated yet
sub->operation = WRITE;
sub->location=(struct local *)malloc(sizeof(struct local));
alloc_assert(sub->location,"sub->location");
memset(sub->location,0, sizeof(struct local));
sub->current_state=SR_WAIT;
sub->current_time=ssd->current_time;
sub->lpn=lpn;
sub->size=size;
sub->state=state;
sub->begin_time=ssd->current_time;
if (parity)
sub->parity = 1;
else
sub->parity = 0;
if (allocate_location(ssd ,sub, parity)==ERROR)
{
free(sub->location);
sub->location=NULL;
free(sub);
sub=NULL;
return NULL;
}
}
else
{
free(sub->location);
sub->location=NULL;
free(sub);
sub=NULL;
//printf("\nERROR! Unexpected command.\n");
//return NULL;
printf("error! unexpected command\n");
exit(1);
}
return sub;
}
/******************************************************
*函数的功能是在给出的channel,chip,die上面寻找读子请求
*这个子请求的ppn要与相应的plane的寄存器里面的ppn相符
*******************************************************/
struct sub_request * find_read_sub_request(struct ssd_info * ssd, unsigned int channel, unsigned int chip, unsigned int die)
{
unsigned int plane=0;
unsigned int address_ppn=0;
struct sub_request *sub=NULL, *p=NULL;
for(plane=0;plane<ssd->parameter->plane_die;plane++)
{
address_ppn=ssd->channel_head[channel].chip_head[chip].die_head[die].plane_head[plane].add_reg_ppn;
if(address_ppn!=-1)
{
sub=ssd->channel_head[channel].subs_r_head;
/*if(sub->ppn==address_ppn)
{
if(sub->next_node==NULL)
{
ssd->channel_head[channel].subs_r_head=NULL;
ssd->channel_head[channel].subs_r_tail=NULL;
}
ssd->channel_head[channel].subs_r_head=sub->next_node;
}
while((sub->ppn!=address_ppn)&&(sub->next_node!=NULL))
{
if(sub->next_node->ppn==address_ppn)
{
p=sub->next_node;
if(p->next_node==NULL)
{
sub->next_node=NULL;
ssd->channel_head[channel].subs_r_tail=sub;
}
else
{
sub->next_node=p->next_node;
}
sub=p;
break;
}
sub=sub->next_node;
}//*/
while (sub->next_node != NULL) {
if (sub->ppn == address_ppn)
break;
sub = sub->next_node;
}//*/
if(sub->ppn==address_ppn)
{
//sub->next_node=NULL;
return sub;
}
else
{
//printf("Error! Can't find the sub request.\n");
printf("error! can't find the sub request\n");
exit(1);
}
}
}
return NULL;
}
/*******************************************************************************
*函数的功能是寻找写子请求。
*分两种情况1,要是是完全动态分配就在ssd->subs_w_head队列上找
*2,要是不是完全动态分配那么就在ssd->channel_head[channel].subs_w_head队列上查找
********************************************************************************/
struct sub_request * find_write_sub_request(struct ssd_info * ssd, unsigned int channel)
{
struct sub_request * sub=NULL,* p=NULL;
if ((ssd->parameter->allocation_scheme==0)&&(ssd->parameter->dynamic_allocation==0)) /*是完全的动态分配*/
{
sub=ssd->subs_w_head;
while(sub!=NULL)
{
if(sub->current_state==SR_WAIT)
{
if (sub->update!=NULL) /*如果有需要提前读出的页*/
{
if ((sub->update->current_state==SR_COMPLETE)||((sub->update->next_state==SR_COMPLETE)&&(sub->update->next_state_predict_time<=ssd->current_time))) //被更新的页已经被读出
{
break;
}
}
else
{
break;
}
}
p=sub;
sub=sub->next_node;
}
if (sub==NULL) /*如果没有找到可以服务的子请求,跳出这个for循环*/
{
return NULL;
}
if (sub!=ssd->subs_w_head)
{
if (sub!=ssd->subs_w_tail)
{
p->next_node=sub->next_node;
}
else
{
ssd->subs_w_tail=p;
ssd->subs_w_tail->next_node=NULL;
}
}
else
{
if (sub->next_node!=NULL)
{
ssd->subs_w_head=sub->next_node;
}
else
{
ssd->subs_w_head=NULL;
ssd->subs_w_tail=NULL;
}
}
sub->next_node=NULL;
if (ssd->channel_head[channel].subs_w_tail!=NULL)
{
ssd->channel_head[channel].subs_w_tail->next_node=sub;
ssd->channel_head[channel].subs_w_tail=sub;
}
else
{
ssd->channel_head[channel].subs_w_tail=sub;
ssd->channel_head[channel].subs_w_head=sub;
}
}