-
Notifications
You must be signed in to change notification settings - Fork 13
/
ssd.c
1949 lines (1702 loc) · 102 KB
/
ssd.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:ssd.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 "ssd.h"
#include "inttypes.h"
#include "assert.h"
/********************************************************************************************************************************
1. main函数中initiation()函数用来初始化ssd 2.make_aged()函数使SSD成为aged,aged的ssd相当于使用过一段时间的ssd,里面有失效页,
non_aged的ssd是新的ssd,无失效页,失效页的比例可以在初始化参数中设置 3. pre_process_page()函数提前扫一遍读请求,把读请求
的lpn<--->ppn映射关系事先建立好,写请求的lpn<--->ppn映射关系在写的时候再建立,预处理trace防止读请求时读不到数据 4. simulate()是
核心处理函数,trace文件从读进来到处理完成都由这个函数来完成 5. statistic_output()函数将ssd结构中的信息输出到输出文件,输出的是
统计数据和平均数据,输出文件较小,trace_output文件则很大很详细 6. free_all_node()函数释放整个main函数中申请的节点
*********************************************************************************************************************************/
int main(int argc, char **argv)
{
unsigned int i,j,k,m;
struct ssd_info *ssd;
//printf("enter main\n");
ssd=(struct ssd_info*)malloc(sizeof(struct ssd_info));
alloc_assert(ssd,"ssd");
memset(ssd,0, sizeof(struct ssd_info));
if (argc != 2) {
printf("usage: ssd tracefilename\n");
exit(1);
}
strcpy(ssd->tracefilename, argv[1]);
ssd=initiation(ssd);
if (ssd == NULL)
exit(1);
make_aged(ssd); //set 1/2 threshold pages to be invalid
pre_set_page(ssd); //fill other 1/2 threshold pages through sequential write
pre_process_page(ssd);
/*for (i=0;i<ssd->parameter->channel_number;i++)
for (j=0;j<ssd->parameter->chip_channel[i];j++)
for (k=0;k<ssd->parameter->die_chip;k++)
for (m=0;m<ssd->parameter->plane_die;m++)
fprintf(ssd->gc_ratio,"%.3f\n", (double)ssd->channel_head[i].chip_head[j].die_head[k].plane_head[m].free_page/(ssd->parameter->page_block*ssd->parameter->block_plane));
exit(0);//*/
for (i=0;i<ssd->parameter->channel_number;i++)
{
for (j=0;j<ssd->parameter->die_chip;j++)
{
for (k=0;k<ssd->parameter->plane_die;k++)
{
//printf("%d,0,%d,%d: %5d\n",i,j,k,ssd->channel_head[i].chip_head[0].die_head[j].plane_head[k].free_page);
}
}
}
fprintf(ssd->outputfile,"\t\t\tOUTPUT\n");
fprintf(ssd->outputfile,"********************* TRACE INFO *********************\n");
ssd->start_time = -1;
ssd=simulate(ssd);
//printf("read request count: %d, write request count: %d\n", ssd->read_request_count, ssd->write_request_count);
statistic_output(ssd);
/*free_all_node(ssd);*/
printf("\n");
printf("the simulation is completed!\n\n\n");
return 1;
}
/******************simulate() *********************************************************************
*simulate()是核心处理函数,主要实现的功能包括
*1,从trace文件中获取一条请求,挂到ssd->request
*2,根据ssd是否有dram分别处理读出来的请求,把这些请求处理成为读写子请求,挂到ssd->channel或者ssd上
*3,按照事件的先后来处理这些读写子请求。
*4,输出每条请求的子请求都处理完后的相关信息到outputfile文件中
**************************************************************************************************/
struct ssd_info *simulate(struct ssd_info *ssd)
{
int flag=1,flag1=0;
double output_step=0;
unsigned int a=0,b=0;
//errno_t err;
printf("\n");
printf("begin simulating.......................\n");
printf("\n");
printf("\n");
printf(" ^o^ OK, please wait a moment, and enjoy music and coffee ^o^ \n");
ssd->tracefile = fopen(ssd->tracefilename,"r");
if(ssd->tracefile == NULL)
{
printf("the trace file: %s can't open\n", ssd->tracefilename);
exit(1);
}
fprintf(ssd->outputfile," arrive lsn size ope begin time response time process time\n");
fflush(ssd->outputfile);
int i,j,k,m;
int c = 0;
while(flag!=100)
{
int i, j;
/*printf("\nsnapshot of GC:\n");
for (i=0;i < ssd->parameter->channel_number;i++)
for (j=0;j< ssd->parameter->chip_channel[i];j++)
if (j != ssd->parameter->chip_channel[i] - 1)
printf("%d, ", ssd->dram->gc_monitor[i][j].next_state_predict_time?1:0);
else
printf("%d\n", ssd->dram->gc_monitor[i][j].next_state_predict_time?1:0);
printf("\n");//*/
//printf("before/get_requests/ssd->current_time: %lld\n", ssd->current_time);
flag=get_requests(ssd);
//printf("flag %d, request queue length: %d\n", flag, ssd->request_queue_length);
//printf("after/get_requests/ssd->current_time: %lld\n", ssd->current_time);
for (i=0;i < ssd->parameter->channel_number;i++)
for (j=0;j< ssd->parameter->chip_channel[i];j++)
if (ssd->dram->gc_monitor[i][j].next_state_predict_time <= ssd->current_time && ssd->dram->gc_monitor[i][j].next_state_predict_time > 0) {
ssd->dram->gc_monitor[i][j].next_state_predict_time = 0;
if (ssd->dram->gc_monitor[i][j].complete) {
ssd->dram->gc_monitor[i][j].complete = 0;
ssd->dram->gc_monitor[i][j].gc_predict_complete_time = 0;
ssd->dram->gc_monitor[i][j].gc_interval = ssd->current_time - ssd->dram->gc_monitor[i][j].gc_start_time;
if (ssd->current_time > ssd->stats_time)
ssd->gc_completed++;
}
//printf("gc_monitor[%d][%d] = 0\n", i, j);
}
if(flag == 1) {
//if (ssd->parameter->dram_capacity!=0)
if (ssd->parameter->buffer_management!=0)
{
if (!(ssd->parameter->raid || ssd->parameter->adaptive_read)) {
buffer_management(ssd);
distribute(ssd);
} else {
buffer_management_modified(ssd);
distribute_modified(ssd);
}
if (ssd->parameter->buffer_background_flush)
buffer_background_flush(ssd);
}
else
{
no_buffer_distribute(ssd);
}
}
process(ssd);
trace_output(ssd);
/*c++;
if (c == 2)
exit(1);//*/
//printf("flag: %d, queue length: %d, current_time: %lld\n", flag, ssd->request_queue_length, ssd->current_time);
//if(flag == 0 && ssd->request_queue == NULL)
if (flag == 0 && ssd->request_queue_length <= 32)
flag = 100;
}
/*struct request *req = malloc(sizeof(struct request));
struct sub_request *sub = malloc(sizeof(struct sub_request));
req=ssd->request_queue;
while (req!=NULL) {
printf("req/operation: %d, lsn: %d, size: %d\n", req->operation, req->lsn, req->size);
sub=req->subs;
while (sub!=NULL) {
printf("sub/operation: %d, lpn: %d, size: %d, parity: %d, current_time: %lld, next_state_predict_time: %lld, current_state: %d, next_state: %d\n", sub->operation, sub->lpn, sub->size, sub->parity, sub->current_time, sub->next_state_predict_time, sub->current_state, sub->next_state);
sub=sub->next_subs;
}
req=req->next_node;
}//*/
fclose(ssd->tracefile);
return ssd;
}
/******** get_request ******************************************************
* 1.get requests that arrived already
* 2.add those request node to ssd->reuqest_queue
* return 0: reach the end of the trace
* -1: no request has been added
* 1: add one request to list
*SSD模拟器有三种驱动方式:时钟驱动(精确,太慢) 事件驱动(本程序采用) trace驱动(),
*两种方式推进事件:channel/chip状态改变、trace文件请求达到。
*channel/chip状态改变和trace文件请求到达是散布在时间轴上的点,每次从当前状态到达
*下一个状态都要到达最近的一个状态,每到达一个点执行一次process
********************************************************************************/
int get_requests(struct ssd_info *ssd)
{
char buffer[200];
unsigned int lsn=0;
int device, size, ope, large_lsn, i = 0, j = 0;
struct request *request1;
int flag = 1;
long filepoint;
int64_t time_t = 0;
int64_t nearest_event_time;
//printf("enter get_requests, current time:%lld\n",ssd->current_time);
if(feof(ssd->tracefile))
return 0;
filepoint = ftell(ssd->tracefile);
fgets(buffer, 200, ssd->tracefile);
sscanf(buffer,"%" PRId64 " %d %d %d %d",&time_t,&device,&lsn,&size,&ope);
fprintf(ssd->request_size, "%d\n", size);
if (ssd->start_time == -1)
ssd->start_time = time_t;
if ((device<0)&&(size<0)&&(ope<0))
{
return 100;
}
if (lsn<ssd->min_lsn)
ssd->min_lsn=lsn;
if (lsn>ssd->max_lsn)
ssd->max_lsn=lsn;
/******************************************************************************************************
*上层文件系统发送给SSD的任何读写命令包括两个部分(LSN,size) LSN是逻辑扇区号,对于文件系统而言,它所看到的存
*储空间是一个线性的连续空间。例如,读请求(260,6)表示的是需要读取从扇区号为260的逻辑扇区开始,总共6个扇区。
*large_lsn: channel下面有多少个subpage,即多少个sector。overprovide系数:SSD中并不是所有的空间都可以给用户使用,
*比如32G的SSD可能有10%的空间保留下来留作他用,所以乘以1-provide
***********************************************************************************************************/
large_lsn=(int)((ssd->parameter->subpage_page*ssd->parameter->page_block*ssd->parameter->block_plane*ssd->parameter->plane_die*ssd->parameter->die_chip*ssd->parameter->chip_num)*(1-ssd->parameter->overprovide));
lsn = lsn%large_lsn;
nearest_event_time=find_nearest_event(ssd);
if (nearest_event_time==MAX_INT64)
{
ssd->current_time=time_t;
/*if (ssd->request_queue_length>ssd->parameter->queue_length) //如果请求队列的长度超过了配置文件中所设置的长度
{
printf("error in get request , the queue length is too long\n");
}*/
}
else
{
if(nearest_event_time<time_t)
{
/*******************************************************************************
*回滚,即如果没有把time_t赋给ssd->current_time,则trace文件已读的一条记录回滚
*filepoint记录了执行fgets之前的文件指针位置,回滚到文件头+filepoint处
*int fseek(FILE *stream, long offset, int fromwhere);函数设置文件指针stream的位置。
*如果执行成功,stream将指向以fromwhere(偏移起始位置:文件头0,当前位置1,文件尾2)为基准,
*偏移offset(指针偏移量)个字节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。
*文本文件只能采用文件头0的定位方式,本程序中打开文件方式是"r":以只读方式打开文本文件
**********************************************************************************/
//printf("ssd->current_time: %lld, nearest event time: %lld, time_t: %lld\n", ssd->current_time, nearest_event_time, time_t);
fseek(ssd->tracefile,filepoint,0);
if(ssd->current_time<=nearest_event_time)
ssd->current_time=nearest_event_time;
return -1;
}
else
{
if (ssd->request_queue_length>=ssd->parameter->queue_length)
{
fseek(ssd->tracefile,filepoint,0);
ssd->current_time=nearest_event_time;
return -1;
}
else
{
/*if (time_t < ssd->current_time) {
printf("nearest_event_time: %lld, time_t: %lld\n", nearest_event_time, time_t);
exit(1);
} else {
printf("time_t: %lld\n", time_t);
}*/
if (time_t > ssd->current_time)
ssd->current_time=time_t;
}
}
}
if(time_t < 0)
{
printf("error!\n");
while(1){}
}
if(feof(ssd->tracefile))
{
request1=NULL;
return 0;
}
request1 = (struct request*)malloc(sizeof(struct request));
alloc_assert(request1,"request");
memset(request1,0, sizeof(struct request));
request1->time = time_t;
request1->lsn = lsn;
request1->size = size;
request1->operation = ope;
request1->begin_time = time_t;
request1->response_time = 0;
request1->energy_consumption = 0;
request1->next_node = NULL;
request1->distri_flag = 0; //indicate whether this request has been distributed already
request1->subs = NULL;
request1->need_distr_flag = NULL;
request1->complete_lsn_count=0; //record the count of lsn served by buffer
//filepoint = ftell(ssd->tracefile); //set the file point
int first_sn = ssd->parameter->raid ? request1->lsn/(ssd->parameter->subpage_page*(ssd->parameter->channel_number-1)) : request1->lsn/(ssd->parameter->subpage_page*ssd->parameter->channel_number);
int last_sn = ssd->parameter->raid ? (request1->lsn + request1->size - 1)/(ssd->parameter->subpage_page*(ssd->parameter->channel_number-1)) : (request1->lsn + request1->size - 1)/(ssd->parameter->subpage_page*ssd->parameter->channel_number);
request1->gc_count = calloc(last_sn - first_sn + 1, sizeof(int));
request1->sub_req_dropped = calloc(last_sn - first_sn + 1, sizeof(int));
if (request1->operation && request1->time < ssd->current_time && request1->time > ssd->stats_time)
ssd->delayed_requests++;
if(ssd->request_queue == NULL) //The queue is empty
{
ssd->request_queue = request1;
ssd->request_tail = request1;
ssd->request_queue_length++;
}
else
{
(ssd->request_tail)->next_node = request1;
ssd->request_tail = request1;
ssd->request_queue_length++;
}
if (request1->operation==1) //计算平均请求大小 1为读 0为写
{
ssd->ave_read_size=(ssd->ave_read_size*ssd->read_request_count+request1->size)/(ssd->read_request_count+1);
}
else
{
ssd->ave_write_size=(ssd->ave_write_size*ssd->write_request_count+request1->size)/(ssd->write_request_count+1);
}
filepoint = ftell(ssd->tracefile);
fgets(buffer, 200, ssd->tracefile); //寻找下一条请求的到达时间
sscanf(buffer,"%" PRId64 " %d %d %d %d",&time_t,&device,&lsn,&size,&ope);
ssd->next_request_time=time_t;
fseek(ssd->tracefile,filepoint,0);
return 1;
}
/**********************************************************************************************************************************************
* 首先buffer是个写buffer,就是为写请求服务的,因为读flash的时间tR为20us,写flash的时间tprog为200us,所以为写服务更能节省时间
* 读操作:如果命中了buffer,从buffer读,不占用channel的I/O总线,没有命中buffer,从flash读,占用channel的I/O总线,但是不进buffer了
* 写操作:首先request分成sub_request子请求,如果是动态分配,sub_request挂到ssd->sub_request上,因为不知道要先挂到哪个channel的sub_request上
* 如果是静态分配则sub_request挂到channel的sub_request链上,同时不管动态分配还是静态分配sub_request都要挂到request的sub_request链上
* 因为每处理完一个request,都要在traceoutput文件中输出关于这个request的信息。处理完一个sub_request,就将其从channel的sub_request链
* 或ssd的sub_request链上摘除,但是在traceoutput文件输出一条后再清空request的sub_request链。
* sub_request命中buffer则在buffer里面写就行了,并且将该sub_page提到buffer链头(LRU),若没有命中且buffer满,则先将buffer链尾的sub_request
* 写入flash(这会产生一个sub_request写请求,挂到这个请求request的sub_request链上,同时视动态分配还是静态分配挂到channel或ssd的
* sub_request链上),在将要写的sub_page写入buffer链头
***********************************************************************************************************************************************/
struct ssd_info *buffer_management(struct ssd_info *ssd)
{
unsigned int j,lsn,lpn,last_lpn,first_lpn,index,complete_flag=0, state,full_page;
unsigned int flag=0,need_distb_flag,lsn_flag,flag1=1,active_region_flag=0;
struct request *new_request;
struct buffer_group *buffer_node,key;
unsigned int mask=0,offset1=0,offset2=0;
//printf("enter buffer_management, current time:%lld\n",ssd->current_time);
ssd->dram->current_time=ssd->current_time;
full_page=~(0xffffffff<<ssd->parameter->subpage_page);
new_request=ssd->request_tail;
lsn=new_request->lsn;
lpn=new_request->lsn/ssd->parameter->subpage_page;
last_lpn=(new_request->lsn+new_request->size-1)/ssd->parameter->subpage_page;
first_lpn=new_request->lsn/ssd->parameter->subpage_page;
//new_request->need_distr_flag=(unsigned int*)malloc(sizeof(unsigned int)*((last_lpn-first_lpn+1)*ssd->parameter->subpage_page/32+1));
//unsigned int has 32 bit. each bit is used to represent the state of need_distr of a page.
new_request->need_distr_flag=(unsigned int*)calloc((last_lpn-first_lpn+1)*ssd->parameter->subpage_page/32+1, sizeof(unsigned int));
alloc_assert(new_request->need_distr_flag,"new_request->need_distr_flag");
//memset(new_request->need_distr_flag, 0, sizeof(unsigned int)*((last_lpn-first_lpn+1)*ssd->parameter->subpage_page/32+1));
if(new_request->operation==READ)
{
while(lpn<=last_lpn)
{
/************************************************************************************************
*need_distb_flag表示是否需要执行distribution函数,1表示需要执行,buffer中没有,0表示不需要执行
*即1表示需要分发,0表示不需要分发,对应点初始全部赋为1
*************************************************************************************************/
need_distb_flag=full_page;
key.group=lpn;
buffer_node=(struct buffer_group*)avlTreeFind(ssd->dram->buffer, (TREE_NODE *)&key); // buffer node
//printf("lpn: %d, buffer_node: %d\n", lpn, buffer_node);
while((buffer_node!=NULL)&&(lsn<(lpn+1)*ssd->parameter->subpage_page)&&(lsn<=(new_request->lsn+new_request->size-1)))
{
//printf("lsn: %d, lpn: %d, (lpn+1)*subpage_page: %d\n", lsn, lpn, (lpn+1)*ssd->parameter->subpage_page);
lsn_flag=full_page;
mask=1 << (lsn%ssd->parameter->subpage_page);
//printf("mask: %x, lsn: %d, shift: %d\n", mask, lsn, lsn%ssd->parameter->subpage_page);
//if(mask>31) //2KB page
if (mask > 255) //4KB page
{
printf("the subpage number is larger than 8!\n");
exit(1);
}
else if((buffer_node->stored & mask)==mask)
{
flag=1;
//printf("lsn_flag: %x, mask: %x\n", lsn_flag, mask);
lsn_flag=lsn_flag&(~mask);
}
if(flag==1)
{ //如果该buffer节点不在buffer的队首,需要将这个节点提到队首,实现了LRU算法,这个是一个双向队列。
if(ssd->dram->buffer->buffer_head!=buffer_node)
{
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;
}
ssd->dram->buffer->read_hit++;
new_request->complete_lsn_count++;
}
else if(flag==0)
{
ssd->dram->buffer->read_miss_hit++;
}
need_distb_flag=need_distb_flag&lsn_flag;
flag=0;
lsn++;
}
index=(lpn-first_lpn)/(32/ssd->parameter->subpage_page);
new_request->need_distr_flag[index]=new_request->need_distr_flag[index]|(need_distb_flag<<(((lpn-first_lpn)%(32/ssd->parameter->subpage_page))*ssd->parameter->subpage_page));
//printf("new_request->need_distr_flag[%d]: %x, need_distb_flag: %x\n", index, new_request->need_distr_flag[index], need_distb_flag);
lpn++;
// set lsn to lpn+1 if lpn with lsn does not hit in the buffer
if (lsn != lpn*ssd->parameter->subpage_page)
lsn = lpn*ssd->parameter->subpage_page;//*/
}
}
else if(new_request->operation==WRITE)
{
while(lpn<=last_lpn)
{
//need_distb_flag=full_page;
mask=~(0xffffffff<<(ssd->parameter->subpage_page));
state=mask;
if(lpn==first_lpn)
{
offset1=ssd->parameter->subpage_page-((lpn+1)*ssd->parameter->subpage_page-new_request->lsn);
state=state&(0xffffffff<<offset1);
}
if(lpn==last_lpn)
{
offset2=ssd->parameter->subpage_page-((lpn+1)*ssd->parameter->subpage_page-(new_request->lsn+new_request->size));
state=state&(~(0xffffffff<<offset2));
}
ssd=insert2buffer(ssd, lpn, state,NULL,new_request);
lpn++;
}
}
complete_flag = 1;
for(j=0;j<=(last_lpn-first_lpn+1)*ssd->parameter->subpage_page/32;j++)
{
if(new_request->need_distr_flag[j] != 0)
{
complete_flag = 0;
}
}
/*************************************************************
*如果请求已经被全部由buffer服务,该请求可以被直接响应,输出结果
*这里假设dram的服务时间为1000ns
**************************************************************/
//printf("new_request->subs: %d, lsn: %d, complete_flag: %d, operation: %d, size: %d, complete_lsn_count: %d\n", new_request->subs, new_request->lsn, complete_flag, new_request->operation, new_request->size, new_request->complete_lsn_count);
if((complete_flag == 1)&&(new_request->subs==NULL))
{
new_request->begin_time=ssd->current_time;
new_request->response_time=ssd->current_time+1000;
}
return ssd;
}
/*****************************
*lpn向ppn的转换
******************************/
unsigned int lpn2ppn(struct ssd_info *ssd,unsigned int lsn)
{
int lpn, ppn;
struct entry *p_map = ssd->dram->map->map_entry;
//printf("enter lpn2ppn, current time:%lld\n",ssd->current_time);
lpn = lsn/ssd->parameter->subpage_page; //lpn
ppn = (p_map[lpn]).pn;
return ppn;
}
/**********************************************************************************
*读请求分配子请求函数,这里只处理读请求,写请求已经在buffer_management()函数中处理了
*根据请求队列和buffer命中的检查,将每个请求分解成子请求,将子请求队列挂在channel上,
*不同的channel有自己的子请求队列
**********************************************************************************/
struct ssd_info *distribute(struct ssd_info *ssd)
{
unsigned int start, end, first_lsn,last_lsn,lpn,flag=0,flag_attached=0,full_page;
unsigned int j, k, sub_size;
int i=0;
struct request *req;
struct sub_request *sub;
int* complt;
//printf("enter distribute, current time:%lld\n",ssd->current_time);
full_page=~(0xffffffff<<ssd->parameter->subpage_page);
req = ssd->request_tail;
if(req->response_time != 0){
return ssd;
}
if (req->operation==WRITE)
{
return ssd;
}
if(req != NULL)
{
if(req->distri_flag == 0)
{
//如果还有一些读请求需要处理
if(req->complete_lsn_count != ssd->request_tail->size)
{
first_lsn = req->lsn;
last_lsn = first_lsn + req->size;
complt = (int *)req->need_distr_flag;
start = first_lsn - first_lsn % ssd->parameter->subpage_page;
//end = (last_lsn/ssd->parameter->subpage_page + 1) * ssd->parameter->subpage_page;
end = last_lsn%ssd->parameter->subpage_page != 0 ? (last_lsn/ssd->parameter->subpage_page + 1) * ssd->parameter->subpage_page : last_lsn;
i = (end - start)/32;
while(i >= 0)
{
/*************************************************************************************
*一个32位的整型数据的每一位代表一个子页,32/ssd->parameter->subpage_page就表示有多少页,
*这里的每一页的状态都存放在了 req->need_distr_flag中,也就是complt中,通过比较complt的
*每一项与full_page,就可以知道,这一页是否处理完成。如果没处理完成则通过creat_sub_request
函数创建子请求。
*************************************************************************************/
for(j=0; j<32/ssd->parameter->subpage_page; j++)
{
k = (complt[((end-start)/32-i)] >>(ssd->parameter->subpage_page*j)) & full_page;
if (k !=0)
{
lpn = start/ssd->parameter->subpage_page + ((end-start)/32-i)*32/ssd->parameter->subpage_page + j;
sub_size=transfer_size(ssd,k,lpn,req);
if (sub_size==0)
{
continue;
}
else
{
//sub=creat_sub_request(ssd,lpn,sub_size,0,req,req->operation);
sub=creat_sub_request(ssd,lpn,sub_size,0,req,req->operation,0,0);
}
}
}
i = i-1;
}
}
else
{
req->begin_time=ssd->current_time;
req->response_time=ssd->current_time+1000;
}
}
}
return ssd;
}
/**********************************************************************
*trace_output()函数是在每一条请求的所有子请求经过process()函数处理完后,
*打印输出相关的运行结果到outputfile文件中,这里的结果主要是运行的时间
**********************************************************************/
void trace_output(struct ssd_info* ssd){
int flag = 1;
int64_t start_time, end_time;
struct request *req, *pre_node;
struct sub_request *sub, *tmp;
//printf("enter trace_output, current time:%lld\n",ssd->current_time);
pre_node=NULL;
req = ssd->request_queue;
start_time = 0;
end_time = 0;
if(req == NULL)
return;
while(req != NULL)
{
sub = req->subs;
flag = 1;
start_time = 0;
end_time = 0;
int first_sn = req->lsn/(ssd->parameter->subpage_page*(ssd->parameter->channel_number-1));
int last_sn = (req->lsn + req->size - 1)/(ssd->parameter->subpage_page*(ssd->parameter->channel_number-1));
//int seqs = last_sn - first_sn + 1;
if(req->response_time != 0)
{
fprintf(ssd->outputfile,"%16" PRId64 " %10d %6d %2d %16" PRId64 " %16" PRId64 " %10" PRId64 "\n",req->time,req->lsn, req->size, req->operation, req->begin_time, req->response_time, req->response_time-req->time);
fflush(ssd->outputfile);
//printf("trace_output/%lld, %d, %d, %d, %lld, %lld, %lld\n",req->time,req->lsn, req->size, req->operation, req->begin_time, req->response_time, req->response_time-req->time);
if (req->time > ssd->stats_time) { //for double workload experiment
fprintf(ssd->process_time, "%" PRId64 "\n", req->response_time - req->time);
if (req->operation)
fprintf(ssd->read_time, "%" PRId64 "\n", req->response_time - req->time);
//fprintf(ssd->read_time, "%lld\t%lld\n", req->time, req->response_time - req->time);
else
fprintf(ssd->write_time, "%" PRId64 "\n", req->response_time - req->time);
if (req->operation)
ssd->reqs_without_gc++;
}
if(req->response_time-req->begin_time==0)
{
printf("the response time is 0??\n");
exit(1);
}
if (req->time > ssd->stats_time) {
if (req->operation==READ)
{
ssd->read_request_count++;
ssd->read_avg=ssd->read_avg+(req->response_time-req->time);
}
else
{
ssd->write_request_count++;
ssd->write_avg=ssd->write_avg+(req->response_time-req->time);
}
}
if(pre_node == NULL)
{
if(req->next_node == NULL)
{
free(req->need_distr_flag);
req->need_distr_flag=NULL;
free(req->gc_count);
free(req->sub_req_dropped);
req->gc_count = NULL;
req->sub_req_dropped = NULL;
free(req);
req = NULL;
ssd->request_queue = NULL;
ssd->request_tail = NULL;
ssd->request_queue_length--;
}
else
{
ssd->request_queue = req->next_node;
pre_node = req;
req = req->next_node;
free(pre_node->need_distr_flag);
pre_node->need_distr_flag=NULL;
free(pre_node->gc_count);
free(pre_node->sub_req_dropped);
pre_node->gc_count = NULL;
pre_node->sub_req_dropped = NULL;
free((void *)pre_node);
pre_node = NULL;
ssd->request_queue_length--;
}
}
else
{
if(req->next_node == NULL)
{
pre_node->next_node = NULL;
free(req->need_distr_flag);
req->need_distr_flag=NULL;
free(req->gc_count);
free(req->sub_req_dropped);
req->gc_count = NULL;
req->sub_req_dropped = NULL;
free(req);
req = NULL;
ssd->request_tail = pre_node;
ssd->request_queue_length--;
}
else
{
pre_node->next_node = req->next_node;
free(req->need_distr_flag);
req->need_distr_flag=NULL;
free(req->gc_count);
free(req->sub_req_dropped);
req->gc_count = NULL;
req->sub_req_dropped = NULL;
free((void *)req);
req = pre_node->next_node;
ssd->request_queue_length--;
}
}
}
else
{
flag=1;
/*int count = 0;
int changed = -1;
int sec_last_finished_first = 0; //second last returned sub_request finishes first in the seq read
int64_t end_time_seq = 0;
int64_t end_time_seq_ = 0;
int64_t *end_times = malloc(seqs*sizeof(int64_t));*/
while(sub != NULL)
{
if(start_time == 0)
start_time = sub->begin_time;
if(start_time > sub->begin_time)
start_time = sub->begin_time;
if(end_time < sub->complete_time)
end_time = sub->complete_time;
/*if(end_time_seq < sub->complete_time) {
end_time_seq_ = end_time_seq;
end_time_seq = sub->complete_time;
changed++;
}
if (!end_time_seq_)
end_time_seq_ = sub->complete_time;
if ((sub->complete_time < end_time_seq && sub->complete_time > end_time_seq_) || end_time_seq_ == end_time_seq)
end_time_seq_= sub->complete_time;
if (end_time_seq == sub->complete_time && !changed)
sec_last_finished_first++;
count++;
if (count%ssd->parameter->channel_number == 0) {
if (sec_last_finished_first == ssd->parameter->channel_number - 1)
end_time_seq_ = end_time_seq;
end_times[count/ssd->parameter->channel_number - 1] = end_time_seq_;
end_time_seq = 0;
sec_last_finished_first = 0;
changed = -1;
}*/
//printf("seq: %d, seq_: %d\n", end_time_seq, end_time_seq_);
if((sub->current_state == SR_COMPLETE)||((sub->next_state==SR_COMPLETE)&&(sub->next_state_predict_time<=ssd->current_time))) // if any sub-request is not completed, the request is not completed
{
//printf("trace_output/r/w: %d, parity: %d, lpn: %d, start time: %lld, end time: %lld, begin time: %lld, complete time: %lld, process time: %lld\n", sub->operation, sub->parity, sub->lpn, start_time, end_time, sub->begin_time, sub->complete_time, sub->complete_time - sub->begin_time);
//printf("req_time: %d, start_time: %d, end_time: %d\n", req->time, start_time, end_time);
sub = sub->next_subs;
}
else
{
//printf("trace_output/r/w: %d, sub->lpn: %d, parity: %d\n", sub->operation, sub->lpn, sub->parity);
sub = sub->next_subs;
flag=0;
//break;
}
}
if (flag == 1)
{
//N/(N+1) is applied when either it's a proactive_read or writing phase of adaptive_read
/*if (ssd->parameter->proactive_read || (ssd->parameter->adaptive_read && !req->operation)) {
int i;
int64_t max = 0;
for (i = 0; i < seqs; i++) {
//printf("seqs: %d, time: %d\n", i, end_times[i]);
if (end_times[i] > max)
max = end_times[i];
}
end_time = max;
}*/
//printf("end_time: %lld\n", end_time);
//statistcs for read request
if (req->operation && req->time > ssd->stats_time) {
int i, k;
double j = 0;
k = last_sn - first_sn + 1;
ssd->groups_with_gc += k;
for (i = 0; i < last_sn - first_sn + 1; i++) {
j += req->gc_count[i];
fprintf(ssd->gc_per_group,"%d\n", req->gc_count[i]);
if (!req->gc_count[i]) {
k--;
ssd->groups_with_gc--;
ssd->groups_without_gc++;
}
}
fprintf(ssd->gc_per_req,"%d\n", (int)j);
if (j > 0) {
ssd->avg_gc_count += j/k;
ssd->reqs_with_gc++;
} else {
ssd->reqs_without_gc++;
}
}
//fprintf(ssd->outputfile,"%10I64u %10u %6u %2u %16I64u %16I64u %10I64u\n",req->time,req->lsn, req->size, req->operation, start_time, end_time, end_time-req->time);
fprintf(ssd->outputfile,"%16" PRId64 " %10d %6d %2d %16" PRId64 " %16" PRId64 " %10" PRId64 "\n",req->time,req->lsn, req->size, req->operation, start_time, end_time, end_time-req->time);
fflush(ssd->outputfile);
//printf("trace_output/%lld, %d, %d, %d, %lld, %lld, %lld\n",req->time,req->lsn, req->size, req->operation, start_time, end_time, end_time - req->time);
if (req->time > ssd->stats_time) {
fprintf(ssd->process_time, "%" PRId64 "\n", end_time - req->time);
if (req->operation == 1)
fprintf(ssd->read_time, "%" PRId64 "\n", end_time - req->time);
//fprintf(ssd->read_time, "%lld\t%lld\n", req->time, end_time - req->time);
else
fprintf(ssd->write_time, "%" PRId64 "\n", end_time - req->time);
}
if(end_time-req->time > 0.5*1E6)
;//exit(1);
if(end_time-start_time==0)
{
printf("the response time is 0??\n");
exit(1);
}
if (req->time > ssd->stats_time) {
if (req->operation==READ)
{
ssd->read_request_count++;
ssd->read_avg=ssd->read_avg+(end_time-req->time);
}
else
{
ssd->write_request_count++;
ssd->write_avg=ssd->write_avg+(end_time-req->time);
}
}
while(req->subs!=NULL)
{
tmp = req->subs;
//printf("lpn: %d, parity_read: %x, update: %x, parity: %d\n", tmp->lpn, tmp->parity_read, tmp->update, tmp->parity);
req->subs = tmp->next_subs;
if (tmp->update!=NULL)
//if (tmp->update!=NULL && tmp->current_state == SR_COMPLETE)
//if (tmp->update!=NULL && (ssd->parameter->advanced_commands&AD_COPYBACK)!=AD_COPYBACK) //since copyback is used, update is for read sub_request produced by parity update, which would later be freed by freeing parity_read.
{
free(tmp->update->location);
tmp->update->location=NULL;
free(tmp->update);
tmp->update=NULL;
}
//free space allocated to parity read
/*struct sub_request *curr;
while ((curr = tmp->parity_read) != NULL) {
tmp->parity_read = tmp->parity_read->next_subs;
free(curr);
}*/
//if (tmp->current_state == SR_COMPLETE) {
free(tmp->location);
tmp->location=NULL;
free(tmp);
tmp=NULL;
//}
}
if(pre_node == NULL)
{
if(req->next_node == NULL)
{
free(req->need_distr_flag);
req->need_distr_flag=NULL;
free(req->gc_count);
free(req->sub_req_dropped);
req->gc_count = NULL;
req->sub_req_dropped = NULL;
free(req);
req = NULL;
ssd->request_queue = NULL;
ssd->request_tail = NULL;
ssd->request_queue_length--;
}
else
{
ssd->request_queue = req->next_node;
pre_node = req;
req = req->next_node;
free(pre_node->gc_count);
free(pre_node->sub_req_dropped);
pre_node->gc_count = NULL;
pre_node->sub_req_dropped = NULL;
free(pre_node->need_distr_flag);
pre_node->need_distr_flag = NULL;
free(pre_node);
pre_node = NULL;
ssd->request_queue_length--;
}
}
else
{
if(req->next_node == NULL)
{
pre_node->next_node = NULL;
free(req->need_distr_flag);
req->need_distr_flag=NULL;
free(req->gc_count);
free(req->sub_req_dropped);
req->gc_count = NULL;
req->sub_req_dropped = NULL;
free(req);
req = NULL;
ssd->request_tail = pre_node;
ssd->request_queue_length--;
}
else
{
pre_node->next_node = req->next_node;
free(req->need_distr_flag);