-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathftl.c
More file actions
2663 lines (2316 loc) · 84.5 KB
/
Copy pathftl.c
File metadata and controls
2663 lines (2316 loc) · 84.5 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
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
#include "ftl.h"
//#define FEMU_DEBUG_FTL
static void *ftl_thread(void *arg);
/* FDP forward declarations */
static void mark_page_valid_fdp(struct ssd *ssd, struct ppa *ppa,
FemuReclaimUnit *ru);
static void mark_page_invalid_fdp(struct ssd *ssd, struct ppa *ppa);
static int do_gc_fdp_style(struct ssd *ssd, uint16_t rgid, uint16_t ruhid,
bool force);
static void ssd_init_fdp_params(struct ssdparams *spp, FemuCtrl *n);
static void femu_fdp_ssd_init_reclaim_group(FemuCtrl *n, struct ssd *ssd);
static void femu_fdp_ssd_init_ru_handles(FemuCtrl *n, struct ssd *ssd);
static void ssd_trim_fdp_style(FemuCtrl *n, NvmeRequest *req, uint64_t slba,
uint32_t nlb);
static void ssd_reset_maptbl(struct ssd *ssd);
/*
* ftl_fdp_alloc_event - allocate an FDP event from the FTL layer
* Used by GC to generate controller events.
*/
static NvmeFdpEvent *ftl_fdp_alloc_event(struct ssd *ssd,
NvmeFdpEventBuffer *ebuf)
{
NvmeFdpEvent *ret;
bool is_full = ebuf->next == ebuf->start && ebuf->nelems;
ret = &ebuf->events[ebuf->next++];
if (unlikely(ebuf->next == NVME_FDP_MAX_EVENTS)) {
ebuf->next = 0;
}
if (is_full) {
ebuf->start = ebuf->next;
} else {
ebuf->nelems++;
}
memset(ret, 0, sizeof(NvmeFdpEvent));
return ret;
}
static inline bool should_gc(struct ssd *ssd)
{
return (ssd->lm.free_line_cnt <= ssd->sp.gc_thres_lines);
}
static inline bool should_gc_high(struct ssd *ssd)
{
return (ssd->lm.free_line_cnt <= ssd->sp.gc_thres_lines_high);
}
/* FDP GC decision: returns rg index if GC needed, -1 otherwise */
static inline int16_t should_gc_fdp_style(struct ssd *ssd)
{
for (int i = 0; i < (int)ssd->nrg; i++) {
if (ssd->rg[i].ru_mgmt->free_ru_cnt <=
ssd->rg[i].ru_mgmt->gc_thres_rus) {
return i;
}
}
return -1;
}
static inline int should_gc_high_fdp_style(struct ssd *ssd)
{
for (int i = 0; i < (int)ssd->nrg; i++) {
if (ssd->rg[i].ru_mgmt->free_ru_cnt <=
ssd->rg[i].ru_mgmt->gc_thres_rus_high) {
return i;
}
}
return -1;
}
static inline struct ppa get_maptbl_ent(struct ssd *ssd, uint64_t lpn)
{
return ssd->maptbl[lpn];
}
static inline void set_maptbl_ent(struct ssd *ssd, uint64_t lpn, struct ppa *ppa)
{
ftl_assert(lpn < ssd->sp.tt_pgs);
ssd->maptbl[lpn] = *ppa;
}
static uint64_t ppa2pgidx(struct ssd *ssd, struct ppa *ppa)
{
struct ssdparams *spp = &ssd->sp;
uint64_t pgidx;
pgidx = ppa->g.ch * spp->pgs_per_ch + \
ppa->g.lun * spp->pgs_per_lun + \
ppa->g.pl * spp->pgs_per_pl + \
ppa->g.blk * spp->pgs_per_blk + \
ppa->g.pg;
ftl_assert(pgidx < spp->tt_pgs);
return pgidx;
}
static inline uint64_t get_rmap_ent(struct ssd *ssd, struct ppa *ppa)
{
uint64_t pgidx = ppa2pgidx(ssd, ppa);
return ssd->rmap[pgidx];
}
/* set rmap[page_no(ppa)] -> lpn */
static inline void set_rmap_ent(struct ssd *ssd, uint64_t lpn, struct ppa *ppa)
{
uint64_t pgidx = ppa2pgidx(ssd, ppa);
ssd->rmap[pgidx] = lpn;
}
static inline int victim_line_cmp_pri(pqueue_pri_t next, pqueue_pri_t curr)
{
return (next > curr);
}
static inline pqueue_pri_t victim_line_get_pri(void *a)
{
return ((struct line *)a)->vpc;
}
static inline void victim_line_set_pri(void *a, pqueue_pri_t pri)
{
((struct line *)a)->vpc = pri;
}
static inline size_t victim_line_get_pos(void *a)
{
return ((struct line *)a)->pos;
}
static inline void victim_line_set_pos(void *a, size_t pos)
{
((struct line *)a)->pos = pos;
}
/* FDP: victim RU priority queue callbacks (greedy by vpc) */
static inline int victim_ru_cmp_pri(pqueue_pri_t next, pqueue_pri_t curr)
{
return (next > curr);
}
static inline pqueue_pri_t victim_ru_get_pri(void *a)
{
return ((FemuReclaimUnit *)a)->vpc;
}
static inline void victim_ru_set_pri(void *a, pqueue_pri_t pri)
{
((FemuReclaimUnit *)a)->vpc = pri;
}
static inline size_t victim_ru_get_pos(void *a)
{
return ((FemuReclaimUnit *)a)->pos;
}
static inline void victim_ru_set_pos(void *a, size_t pos)
{
((FemuReclaimUnit *)a)->pos = pos;
}
/* FDP: victim RU priority queue callbacks (cost-benefit by my_cb) */
static inline int victim_ru_cmp_pri_by_cb(pqueue_pri_t next, pqueue_pri_t curr)
{
return (next > curr);
}
static inline pqueue_pri_t victim_ru_get_pri_by_cb(void *a)
{
/* cast float to pqueue_pri_t for ordering */
return (pqueue_pri_t)((FemuReclaimUnit *)a)->my_cb;
}
static inline void victim_ru_set_pri_by_cb(void *a, pqueue_pri_t pri)
{
((FemuReclaimUnit *)a)->my_cb = (float)pri;
}
static void ssd_init_lines(struct ssd *ssd)
{
struct ssdparams *spp = &ssd->sp;
struct line_mgmt *lm = &ssd->lm;
struct line *line;
lm->tt_lines = spp->blks_per_pl;
ftl_assert(lm->tt_lines == spp->tt_lines);
lm->lines = g_malloc0(sizeof(struct line) * lm->tt_lines);
QTAILQ_INIT(&lm->free_line_list);
lm->victim_line_pq = pqueue_init(spp->tt_lines, victim_line_cmp_pri,
victim_line_get_pri, victim_line_set_pri,
victim_line_get_pos, victim_line_set_pos);
QTAILQ_INIT(&lm->full_line_list);
lm->free_line_cnt = 0;
for (int i = 0; i < lm->tt_lines; i++) {
line = &lm->lines[i];
line->id = i;
line->ipc = 0;
line->vpc = 0;
line->pos = 0;
/* initialize all the lines as free lines */
QTAILQ_INSERT_TAIL(&lm->free_line_list, line, entry);
lm->free_line_cnt++;
}
ftl_assert(lm->free_line_cnt == lm->tt_lines);
lm->victim_line_cnt = 0;
lm->full_line_cnt = 0;
}
static void ssd_init_write_pointer(struct ssd *ssd)
{
struct write_pointer *wpp = &ssd->wp;
struct line_mgmt *lm = &ssd->lm;
struct line *curline = NULL;
curline = QTAILQ_FIRST(&lm->free_line_list);
QTAILQ_REMOVE(&lm->free_line_list, curline, entry);
lm->free_line_cnt--;
/* wpp->curline is always our next-to-write super-block */
wpp->curline = curline;
wpp->ch = 0;
wpp->lun = 0;
wpp->pg = 0;
wpp->blk = 0;
wpp->pl = 0;
}
static inline void check_addr(int a, int max)
{
ftl_assert(a >= 0 && a < max);
}
static struct line *get_next_free_line(struct ssd *ssd)
{
struct line_mgmt *lm = &ssd->lm;
struct line *curline = NULL;
curline = QTAILQ_FIRST(&lm->free_line_list);
if (!curline) {
ftl_err("No free lines left in [%s] !!!!\n", ssd->ssdname);
return NULL;
}
QTAILQ_REMOVE(&lm->free_line_list, curline, entry);
lm->free_line_cnt--;
return curline;
}
static void ssd_advance_write_pointer(struct ssd *ssd)
{
struct ssdparams *spp = &ssd->sp;
struct write_pointer *wpp = &ssd->wp;
struct line_mgmt *lm = &ssd->lm;
check_addr(wpp->ch, spp->nchs);
wpp->ch++;
if (wpp->ch == spp->nchs) {
wpp->ch = 0;
check_addr(wpp->lun, spp->luns_per_ch);
wpp->lun++;
/* in this case, we should go to next lun */
if (wpp->lun == spp->luns_per_ch) {
wpp->lun = 0;
/* go to next page in the block */
check_addr(wpp->pg, spp->pgs_per_blk);
wpp->pg++;
if (wpp->pg == spp->pgs_per_blk) {
wpp->pg = 0;
/* move current line to {victim,full} line list */
if (wpp->curline->vpc == spp->pgs_per_line) {
/* all pgs are still valid, move to full line list */
ftl_assert(wpp->curline->ipc == 0);
QTAILQ_INSERT_TAIL(&lm->full_line_list, wpp->curline, entry);
lm->full_line_cnt++;
} else {
ftl_assert(wpp->curline->vpc >= 0 && wpp->curline->vpc < spp->pgs_per_line);
/* there must be some invalid pages in this line */
ftl_assert(wpp->curline->ipc > 0);
pqueue_insert(lm->victim_line_pq, wpp->curline);
lm->victim_line_cnt++;
}
/* current line is used up, pick another empty line */
check_addr(wpp->blk, spp->blks_per_pl);
wpp->curline = NULL;
wpp->curline = get_next_free_line(ssd);
if (!wpp->curline) {
/* TODO */
abort();
}
wpp->blk = wpp->curline->id;
check_addr(wpp->blk, spp->blks_per_pl);
/* make sure we are starting from page 0 in the super block */
ftl_assert(wpp->pg == 0);
ftl_assert(wpp->lun == 0);
ftl_assert(wpp->ch == 0);
/* TODO: assume # of pl_per_lun is 1, fix later */
ftl_assert(wpp->pl == 0);
}
}
}
}
static struct ppa get_new_page(struct ssd *ssd)
{
struct write_pointer *wpp = &ssd->wp;
struct ppa ppa;
ppa.ppa = 0;
ppa.g.ch = wpp->ch;
ppa.g.lun = wpp->lun;
ppa.g.pg = wpp->pg;
ppa.g.blk = wpp->blk;
ppa.g.pl = wpp->pl;
ftl_assert(ppa.g.pl == 0);
return ppa;
}
static void check_params(struct ssdparams *spp)
{
/*
* we are using a general write pointer increment method now, no need to
* force luns_per_ch and nchs to be power of 2
*/
//ftl_assert(is_power_of_2(spp->luns_per_ch));
//ftl_assert(is_power_of_2(spp->nchs));
}
static void ssd_init_params(struct ssdparams *spp, FemuCtrl *n)
{
spp->secsz = n->bb_params.secsz; // 512
spp->secs_per_pg = n->bb_params.secs_per_pg; // 8
spp->pgs_per_blk = n->bb_params.pgs_per_blk; //256
spp->blks_per_pl = n->bb_params.blks_per_pl; /* 256 16GB */
spp->pls_per_lun = n->bb_params.pls_per_lun; // 1
spp->luns_per_ch = n->bb_params.luns_per_ch; // 8
spp->nchs = n->bb_params.nchs; // 8
spp->pg_rd_lat = n->bb_params.pg_rd_lat;
spp->pg_wr_lat = n->bb_params.pg_wr_lat;
spp->blk_er_lat = n->bb_params.blk_er_lat;
spp->ch_xfer_lat = n->bb_params.ch_xfer_lat;
/* calculated values */
spp->secs_per_blk = spp->secs_per_pg * spp->pgs_per_blk;
spp->secs_per_pl = spp->secs_per_blk * spp->blks_per_pl;
spp->secs_per_lun = spp->secs_per_pl * spp->pls_per_lun;
spp->secs_per_ch = spp->secs_per_lun * spp->luns_per_ch;
spp->tt_secs = spp->secs_per_ch * spp->nchs;
spp->pgs_per_pl = spp->pgs_per_blk * spp->blks_per_pl;
spp->pgs_per_lun = spp->pgs_per_pl * spp->pls_per_lun;
spp->pgs_per_ch = spp->pgs_per_lun * spp->luns_per_ch;
spp->tt_pgs = spp->pgs_per_ch * spp->nchs;
spp->blks_per_lun = spp->blks_per_pl * spp->pls_per_lun;
spp->blks_per_ch = spp->blks_per_lun * spp->luns_per_ch;
spp->tt_blks = spp->blks_per_ch * spp->nchs;
spp->pls_per_ch = spp->pls_per_lun * spp->luns_per_ch;
spp->tt_pls = spp->pls_per_ch * spp->nchs;
spp->tt_luns = spp->luns_per_ch * spp->nchs;
/* line is special, put it at the end */
spp->blks_per_line = spp->tt_luns; /* TODO: to fix under multiplanes */
spp->pgs_per_line = spp->blks_per_line * spp->pgs_per_blk;
spp->secs_per_line = spp->pgs_per_line * spp->secs_per_pg;
spp->tt_lines = spp->blks_per_lun; /* TODO: to fix under multiplanes */
spp->gc_thres_pcent = n->bb_params.gc_thres_pcent/100.0;
spp->gc_thres_lines = (int)((1 - spp->gc_thres_pcent) * spp->tt_lines);
spp->gc_thres_pcent_high = n->bb_params.gc_thres_pcent_high/100.0;
spp->gc_thres_lines_high = (int)((1 - spp->gc_thres_pcent_high) * spp->tt_lines);
spp->enable_gc_delay = true;
check_params(spp);
}
static void ssd_init_nand_page(struct nand_page *pg, struct ssdparams *spp)
{
pg->nsecs = spp->secs_per_pg;
pg->sec = g_malloc0(sizeof(nand_sec_status_t) * pg->nsecs);
for (int i = 0; i < pg->nsecs; i++) {
pg->sec[i] = SEC_FREE;
}
pg->status = PG_FREE;
}
static void ssd_init_nand_blk(struct nand_block *blk, struct ssdparams *spp)
{
blk->npgs = spp->pgs_per_blk;
blk->pg = g_malloc0(sizeof(struct nand_page) * blk->npgs);
for (int i = 0; i < blk->npgs; i++) {
ssd_init_nand_page(&blk->pg[i], spp);
}
blk->ipc = 0;
blk->vpc = 0;
blk->erase_cnt = 0;
blk->wp = 0;
}
static void ssd_init_nand_plane(struct nand_plane *pl, struct ssdparams *spp)
{
pl->nblks = spp->blks_per_pl;
pl->blk = g_malloc0(sizeof(struct nand_block) * pl->nblks);
for (int i = 0; i < pl->nblks; i++) {
ssd_init_nand_blk(&pl->blk[i], spp);
}
}
static void ssd_init_nand_lun(struct nand_lun *lun, struct ssdparams *spp)
{
lun->npls = spp->pls_per_lun;
lun->pl = g_malloc0(sizeof(struct nand_plane) * lun->npls);
for (int i = 0; i < lun->npls; i++) {
ssd_init_nand_plane(&lun->pl[i], spp);
}
lun->next_lun_avail_time = 0;
lun->busy = false;
}
static void ssd_init_ch(struct ssd_channel *ch, struct ssdparams *spp)
{
ch->nluns = spp->luns_per_ch;
ch->lun = g_malloc0(sizeof(struct nand_lun) * ch->nluns);
for (int i = 0; i < ch->nluns; i++) {
ssd_init_nand_lun(&ch->lun[i], spp);
}
ch->next_ch_avail_time = 0;
ch->busy = 0;
}
static void ssd_init_maptbl(struct ssd *ssd)
{
struct ssdparams *spp = &ssd->sp;
ssd->maptbl = g_malloc0(sizeof(struct ppa) * spp->tt_pgs);
for (int i = 0; i < spp->tt_pgs; i++) {
ssd->maptbl[i].ppa = UNMAPPED_PPA;
}
}
static void ssd_init_rmap(struct ssd *ssd)
{
struct ssdparams *spp = &ssd->sp;
ssd->rmap = g_malloc0(sizeof(uint64_t) * spp->tt_pgs);
for (int i = 0; i < spp->tt_pgs; i++) {
ssd->rmap[i] = INVALID_LPN;
}
}
void ssd_init(FemuCtrl *n)
{
struct ssd *ssd = n->ssd;
struct ssdparams *spp = &ssd->sp;
ftl_assert(ssd);
ssd->n = n;
ssd_init_params(spp, n);
/* initialize ssd internal layout architecture */
ssd->ch = g_malloc0(sizeof(struct ssd_channel) * spp->nchs);
for (int i = 0; i < spp->nchs; i++) {
ssd_init_ch(&ssd->ch[i], spp);
}
/* initialize maptbl */
ssd_init_maptbl(ssd);
/* initialize rmap */
ssd_init_rmap(ssd);
/* initialize all the lines */
ssd_init_lines(ssd);
/* FDP vs non-FDP init path */
ssd->fdp_enabled = (n->subsys != NULL &&
n->subsys->params.fdp.enabled);
ssd->fdp_debug = (getenv("FEMU_FDP_DEBUG") != NULL);
if (ssd->fdp_enabled) {
ssd_init_fdp_params(spp, n);
ftl_log("FDP: initializing reclaim groups\n");
femu_fdp_ssd_init_reclaim_group(n, ssd);
ftl_log("FDP: initializing RU handles\n");
femu_fdp_ssd_init_ru_handles(n, ssd);
ftl_log("FDP: init complete (nrg=%lu, nruhs=%lu)\n",
ssd->nrg, ssd->nruhs);
} else {
/* non-FDP: use single write pointer */
ssd_init_write_pointer(ssd);
}
qemu_thread_create(&ssd->ftl_thread, "FEMU-FTL-Thread", ftl_thread, n,
QEMU_THREAD_JOINABLE);
}
static inline bool valid_ppa(struct ssd *ssd, struct ppa *ppa)
{
struct ssdparams *spp = &ssd->sp;
int ch = ppa->g.ch;
int lun = ppa->g.lun;
int pl = ppa->g.pl;
int blk = ppa->g.blk;
int pg = ppa->g.pg;
int sec = ppa->g.sec;
if (ch >= 0 && ch < spp->nchs && lun >= 0 && lun < spp->luns_per_ch && pl >=
0 && pl < spp->pls_per_lun && blk >= 0 && blk < spp->blks_per_pl && pg
>= 0 && pg < spp->pgs_per_blk && sec >= 0 && sec < spp->secs_per_pg)
return true;
return false;
}
static inline bool valid_lpn(struct ssd *ssd, uint64_t lpn)
{
return (lpn < ssd->sp.tt_pgs);
}
static inline bool mapped_ppa(struct ppa *ppa)
{
return !(ppa->ppa == UNMAPPED_PPA);
}
/* ===== 데이터 잔존성 실험용 debug 헬퍼 (FTL 동작 변경 없음) ===== */
/* stderr 사용: tee 파이프에서도 무버퍼라 즉시 출력됨 */
#define EXP_LOG(fmt, ...) do { \
if (getenv("FEMU_EXP_LOG")) \
fprintf(stderr, "[EXP] " fmt, ## __VA_ARGS__); \
} while (0)
#define PPA_FMT "ch=%u lun=%u pl=%u blk=%u pg=%u"
#define PPA_ARG(p) (unsigned)(p)->g.ch, (unsigned)(p)->g.lun, \
(unsigned)(p)->g.pl, (unsigned)(p)->g.blk, (unsigned)(p)->g.pg
/* backend(DRAM)에서 한 LPN의 실제 바이트를 hex+ascii로 덤프.
* 주의: 데이터는 PPA가 아니라 LBA(=lpn*page_bytes) 위치에 있다. */
static void femu_dbg_dump_lpn(struct ssd *ssd, uint64_t lpn)
{
struct ssdparams *spp = &ssd->sp;
uint64_t page_bytes = (uint64_t)spp->secs_per_pg * spp->secsz; /* 4096 */
uint64_t off = lpn * page_bytes;
struct ppa ppa = get_maptbl_ent(ssd, lpn);
uint8_t *base;
if (!ssd->n || !ssd->n->mbe || !ssd->n->mbe->logical_space) {
fprintf(stderr, "[DUMP] backend not ready\n");
return;
}
if (off + page_bytes > (uint64_t)ssd->n->mbe->size) {
fprintf(stderr, "[DUMP] lpn=%lu out of range\n", lpn);
return;
}
base = (uint8_t *)ssd->n->mbe->logical_space + off;
fprintf(stderr, "[DUMP] lpn=%lu off=0x%lx mapped=%d", lpn, off,
mapped_ppa(&ppa));
if (mapped_ppa(&ppa))
fprintf(stderr, " " PPA_FMT, PPA_ARG(&ppa));
fprintf(stderr, "\n");
for (uint64_t i = 0; i < page_bytes; i += 16) {
fprintf(stderr, " %08lx ", off + i);
for (int j = 0; j < 16; j++)
fprintf(stderr, "%02x ", base[i + j]);
fprintf(stderr, " |");
for (int j = 0; j < 16; j++) {
uint8_t c = base[i + j];
fprintf(stderr, "%c", (c >= 0x20 && c < 0x7f) ? c : '.');
}
fprintf(stderr, "|\n");
}
}
/* backend 전체에서 FEMU_SECRET 문자열을 찾아 어느 LPN에 남아있는지 출력 */
static void femu_dbg_scan_secret(struct ssd *ssd, const char *tag)
{
const char *sec = getenv("FEMU_SECRET");
struct ssdparams *spp = &ssd->sp;
uint64_t page_bytes, size;
uint8_t *buf;
size_t slen;
int hits = 0;
if (!sec || !ssd->n || !ssd->n->mbe || !ssd->n->mbe->logical_space)
return;
page_bytes = (uint64_t)spp->secs_per_pg * spp->secsz;
buf = (uint8_t *)ssd->n->mbe->logical_space;
size = (uint64_t)ssd->n->mbe->size;
slen = strlen(sec);
for (uint64_t i = 0; i + slen <= size; i++) {
if (buf[i] == (uint8_t)sec[0] && memcmp(buf + i, sec, slen) == 0) {
fprintf(stderr, "[SCAN:%s] FOUND '%s' at off=0x%lx lpn=%lu\n",
tag, sec, i, i / page_bytes);
hits++;
i += slen - 1;
}
}
if (!hits)
fprintf(stderr, "[SCAN:%s] '%s' NOT present in backend\n", tag, sec);
}
/* ---- 비밀 문자열을 담은 LPN/블록만 추적해 로그 노이즈 제거 ---- */
#define EXP_MAX_WATCH 256
static uint64_t exp_watch_lpn[EXP_MAX_WATCH];
static int exp_watch_lpn_cnt = 0;
static uint8_t exp_watch_blk[1 << BLK_BITS]; /* blk(line) id -> watched? */
static bool exp_lpn_watched(uint64_t lpn)
{
for (int i = 0; i < exp_watch_lpn_cnt; i++)
if (exp_watch_lpn[i] == lpn)
return true;
return false;
}
static void exp_watch_lpn_add(uint64_t lpn)
{
if (exp_lpn_watched(lpn))
return;
if (exp_watch_lpn_cnt < EXP_MAX_WATCH)
exp_watch_lpn[exp_watch_lpn_cnt++] = lpn;
}
/* 해당 LPN의 backend 페이지에 FEMU_SECRET 이 들어있는지 검사 */
static bool femu_dbg_lpn_has_secret(struct ssd *ssd, uint64_t lpn)
{
const char *sec = getenv("FEMU_SECRET");
struct ssdparams *spp = &ssd->sp;
uint64_t page_bytes, off;
uint8_t *base;
size_t slen;
if (!sec || !getenv("FEMU_EXP_LOG"))
return false;
if (!ssd->n || !ssd->n->mbe || !ssd->n->mbe->logical_space)
return false;
page_bytes = (uint64_t)spp->secs_per_pg * spp->secsz;
off = lpn * page_bytes;
if (off + page_bytes > (uint64_t)ssd->n->mbe->size)
return false;
base = (uint8_t *)ssd->n->mbe->logical_space + off;
slen = strlen(sec);
for (uint64_t i = 0; i + slen <= page_bytes; i++)
if (base[i] == (uint8_t)sec[0] && memcmp(base + i, sec, slen) == 0)
return true;
return false;
}
static inline struct ssd_channel *get_ch(struct ssd *ssd, struct ppa *ppa)
{
return &(ssd->ch[ppa->g.ch]);
}
static inline struct nand_lun *get_lun(struct ssd *ssd, struct ppa *ppa)
{
struct ssd_channel *ch = get_ch(ssd, ppa);
return &(ch->lun[ppa->g.lun]);
}
static inline struct nand_plane *get_pl(struct ssd *ssd, struct ppa *ppa)
{
struct nand_lun *lun = get_lun(ssd, ppa);
return &(lun->pl[ppa->g.pl]);
}
static inline struct nand_block *get_blk(struct ssd *ssd, struct ppa *ppa)
{
struct nand_plane *pl = get_pl(ssd, ppa);
return &(pl->blk[ppa->g.blk]);
}
static inline struct line *get_line(struct ssd *ssd, struct ppa *ppa)
{
return &(ssd->lm.lines[ppa->g.blk]);
}
static inline struct nand_page *get_pg(struct ssd *ssd, struct ppa *ppa)
{
struct nand_block *blk = get_blk(ssd, ppa);
return &(blk->pg[ppa->g.pg]);
}
static uint64_t ssd_advance_status(struct ssd *ssd, struct ppa *ppa, struct
nand_cmd *ncmd)
{
int c = ncmd->cmd;
uint64_t cmd_stime = (ncmd->stime == 0) ? \
qemu_clock_get_ns(QEMU_CLOCK_REALTIME) : ncmd->stime;
uint64_t nand_stime;
struct ssdparams *spp = &ssd->sp;
struct nand_lun *lun = get_lun(ssd, ppa);
uint64_t lat = 0;
switch (c) {
case NAND_READ:
/* read: perform NAND cmd first */
nand_stime = (lun->next_lun_avail_time < cmd_stime) ? cmd_stime : \
lun->next_lun_avail_time;
lun->next_lun_avail_time = nand_stime + spp->pg_rd_lat;
lat = lun->next_lun_avail_time - cmd_stime;
#if 0
lun->next_lun_avail_time = nand_stime + spp->pg_rd_lat;
/* read: then data transfer through channel */
chnl_stime = (ch->next_ch_avail_time < lun->next_lun_avail_time) ? \
lun->next_lun_avail_time : ch->next_ch_avail_time;
ch->next_ch_avail_time = chnl_stime + spp->ch_xfer_lat;
lat = ch->next_ch_avail_time - cmd_stime;
#endif
break;
case NAND_WRITE:
/* write: transfer data through channel first */
nand_stime = (lun->next_lun_avail_time < cmd_stime) ? cmd_stime : \
lun->next_lun_avail_time;
if (ncmd->type == USER_IO) {
lun->next_lun_avail_time = nand_stime + spp->pg_wr_lat;
} else {
lun->next_lun_avail_time = nand_stime + spp->pg_wr_lat;
}
lat = lun->next_lun_avail_time - cmd_stime;
#if 0
chnl_stime = (ch->next_ch_avail_time < cmd_stime) ? cmd_stime : \
ch->next_ch_avail_time;
ch->next_ch_avail_time = chnl_stime + spp->ch_xfer_lat;
/* write: then do NAND program */
nand_stime = (lun->next_lun_avail_time < ch->next_ch_avail_time) ? \
ch->next_ch_avail_time : lun->next_lun_avail_time;
lun->next_lun_avail_time = nand_stime + spp->pg_wr_lat;
lat = lun->next_lun_avail_time - cmd_stime;
#endif
break;
case NAND_ERASE:
/* erase: only need to advance NAND status */
nand_stime = (lun->next_lun_avail_time < cmd_stime) ? cmd_stime : \
lun->next_lun_avail_time;
lun->next_lun_avail_time = nand_stime + spp->blk_er_lat;
lat = lun->next_lun_avail_time - cmd_stime;
break;
default:
ftl_err("Unsupported NAND command: 0x%x\n", c);
}
return lat;
}
/* update SSD status about one page from PG_VALID -> PG_INVALID */
static void mark_page_invalid(struct ssd *ssd, struct ppa *ppa)
{
struct line_mgmt *lm = &ssd->lm;
struct ssdparams *spp = &ssd->sp;
struct nand_block *blk = NULL;
struct nand_page *pg = NULL;
bool was_full_line = false;
struct line *line;
/* update corresponding page status */
pg = get_pg(ssd, ppa);
ftl_assert(pg->status == PG_VALID);
pg->status = PG_INVALID;
/* update corresponding block status */
blk = get_blk(ssd, ppa);
ftl_assert(blk->ipc >= 0 && blk->ipc < spp->pgs_per_blk);
blk->ipc++;
ftl_assert(blk->vpc > 0 && blk->vpc <= spp->pgs_per_blk);
blk->vpc--;
/* update corresponding line status */
line = get_line(ssd, ppa);
ftl_assert(line->ipc >= 0 && line->ipc < spp->pgs_per_line);
if (line->vpc == spp->pgs_per_line) {
ftl_assert(line->ipc == 0);
was_full_line = true;
}
line->ipc++;
ftl_assert(line->vpc > 0 && line->vpc <= spp->pgs_per_line);
/* Adjust the position of the victime line in the pq under over-writes */
if (line->pos) {
/* Note that line->vpc will be updated by this call */
pqueue_change_priority(lm->victim_line_pq, line->vpc - 1, line);
} else {
line->vpc--;
}
if (was_full_line) {
/* move line: "full" -> "victim" */
QTAILQ_REMOVE(&lm->full_line_list, line, entry);
lm->full_line_cnt--;
pqueue_insert(lm->victim_line_pq, line);
lm->victim_line_cnt++;
}
}
static void mark_page_valid(struct ssd *ssd, struct ppa *ppa)
{
struct nand_block *blk = NULL;
struct nand_page *pg = NULL;
struct line *line;
/* update page status */
pg = get_pg(ssd, ppa);
ftl_assert(pg->status == PG_FREE);
pg->status = PG_VALID;
/* update corresponding block status */
blk = get_blk(ssd, ppa);
ftl_assert(blk->vpc >= 0 && blk->vpc < ssd->sp.pgs_per_blk);
blk->vpc++;
/* update corresponding line status */
line = get_line(ssd, ppa);
ftl_assert(line->vpc >= 0 && line->vpc < ssd->sp.pgs_per_line);
line->vpc++;
}
static void mark_block_free(struct ssd *ssd, struct ppa *ppa)
{
struct ssdparams *spp = &ssd->sp;
struct nand_block *blk = get_blk(ssd, ppa);
struct nand_page *pg = NULL;
for (int i = 0; i < spp->pgs_per_blk; i++) {
/* reset page status */
pg = &blk->pg[i];
ftl_assert(pg->nsecs == spp->secs_per_pg);
pg->status = PG_FREE;
}
/* reset block status */
ftl_assert(blk->npgs == spp->pgs_per_blk);
blk->ipc = 0;
blk->vpc = 0;
blk->erase_cnt++;
if (exp_watch_blk[ppa->g.blk])
EXP_LOG("[ERASE] " PPA_FMT " erase_cnt=%d (vpc/ipc reset)\n",
PPA_ARG(ppa), blk->erase_cnt);
}
static void gc_read_page(struct ssd *ssd, struct ppa *ppa)
{
/* advance ssd status, we don't care about how long it takes */
if (ssd->sp.enable_gc_delay) {
struct nand_cmd gcr;
gcr.type = GC_IO;
gcr.cmd = NAND_READ;
gcr.stime = 0;
ssd_advance_status(ssd, ppa, &gcr);
}
}
/* move valid page data (already in DRAM) from victim line to a new page */
static uint64_t gc_write_page(struct ssd *ssd, struct ppa *old_ppa)
{
struct ppa new_ppa;
struct nand_lun *new_lun;
uint64_t lpn = get_rmap_ent(ssd, old_ppa);
ftl_assert(valid_lpn(ssd, lpn));
new_ppa = get_new_page(ssd);
/* update maptbl */
set_maptbl_ent(ssd, lpn, &new_ppa);
/* update rmap */
set_rmap_ent(ssd, lpn, &new_ppa);
if (exp_lpn_watched(lpn)) {
exp_watch_blk[new_ppa.g.blk] = 1; /* 새 블록도 추적 대상에 추가 */
EXP_LOG("[GC_MOVE] lpn=%lu " PPA_FMT " -> " PPA_FMT "\n",
lpn, PPA_ARG(old_ppa), PPA_ARG(&new_ppa));
}
mark_page_valid(ssd, &new_ppa);
/* need to advance the write pointer here */
ssd_advance_write_pointer(ssd);
if (ssd->sp.enable_gc_delay) {
struct nand_cmd gcw;
gcw.type = GC_IO;
gcw.cmd = NAND_WRITE;
gcw.stime = 0;
ssd_advance_status(ssd, &new_ppa, &gcw);
}
/* advance per-ch gc_endtime as well */
#if 0
new_ch = get_ch(ssd, &new_ppa);
new_ch->gc_endtime = new_ch->next_ch_avail_time;
#endif
new_lun = get_lun(ssd, &new_ppa);
new_lun->gc_endtime = new_lun->next_lun_avail_time;
return 0;
}
static struct line *select_victim_line(struct ssd *ssd, bool force)
{
struct line_mgmt *lm = &ssd->lm;
struct line *victim_line = NULL;
victim_line = pqueue_peek(lm->victim_line_pq);
if (!victim_line) {
return NULL;
}
if (!force && victim_line->ipc < ssd->sp.pgs_per_line / 8) {
return NULL;
}
pqueue_pop(lm->victim_line_pq);
victim_line->pos = 0;
lm->victim_line_cnt--;
/* victim_line is a danggling node now */
return victim_line;
}
/* here ppa identifies the block we want to clean */
static void clean_one_block(struct ssd *ssd, struct ppa *ppa)
{
struct ssdparams *spp = &ssd->sp;
struct nand_page *pg_iter = NULL;
int cnt = 0;
for (int pg = 0; pg < spp->pgs_per_blk; pg++) {
ppa->g.pg = pg;
pg_iter = get_pg(ssd, ppa);
/* there shouldn't be any free page in victim blocks */
ftl_assert(pg_iter->status != PG_FREE);
if (pg_iter->status == PG_VALID) {
gc_read_page(ssd, ppa);
/* delay the maptbl update until "write" happens */
gc_write_page(ssd, ppa);
cnt++;
}
}
ftl_assert(get_blk(ssd, ppa)->vpc == cnt);
}
static void mark_line_free(struct ssd *ssd, struct ppa *ppa)
{
struct line_mgmt *lm = &ssd->lm;
struct line *line = get_line(ssd, ppa);
line->ipc = 0;
line->vpc = 0;
/* move this line to free line list */
QTAILQ_INSERT_TAIL(&lm->free_line_list, line, entry);
lm->free_line_cnt++;
}
static int do_gc(struct ssd *ssd, bool force)
{
struct line *victim_line = NULL;
struct ssdparams *spp = &ssd->sp;
struct nand_lun *lunp;
struct ppa ppa;
int ch, lun;
victim_line = select_victim_line(ssd, force);
if (!victim_line) {
return -1;
}
ppa.g.blk = victim_line->id;
ftl_debug("GC-ing line:%d,ipc=%d,victim=%d,full=%d,free=%d\n", ppa.g.blk,
victim_line->ipc, ssd->lm.victim_line_cnt, ssd->lm.full_line_cnt,
ssd->lm.free_line_cnt);
/* copy back valid data */
for (ch = 0; ch < spp->nchs; ch++) {
for (lun = 0; lun < spp->luns_per_ch; lun++) {
ppa.g.ch = ch;
ppa.g.lun = lun;