-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathpage_cache.h
2137 lines (1731 loc) · 75 KB
/
page_cache.h
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
#ifndef __PAGE_CACHE_H__
#define __PAGE_CACHE_H__
#ifndef __device__
#define __device__
#endif
#ifndef __host__
#define __host__
#endif
#ifndef __forceinline__
#define __forceinline__ inline
#endif
#include "util.h"
#include "host_util.h"
#include "nvm_types.h"
#include "nvm_util.h"
#include "buffer.h"
#include "ctrl.h"
#include <iostream>
#include "nvm_parallel_queue.h"
#include "nvm_cmd.h"
#define FREE 2
// enum locality {HIGH_SPATIAL, LOW_SPATIAL, MEDIUM_SPATIAL};
// template <typname T>
// stryct array_t {
// range_t<T>* ranges;
// uint32_t n_ranges;
// void add_range(start_idx, end_idx, locality l )
// {
// ranges.push_back(new range(star))
// }
// }
// enum page_state {USE = 1U, USE_DIRTY = ((1U << 31) | 1), VALID_DIRTY = (1U << 31),
// VALID = 0U, INVALID = (UINT_MAX & 0x7fffffff),
// BUSY = ((UINT_MAX & 0x7fffffff)-1)};
enum data_dist_t {REPLICATE = 0, STRIPE = 1};
// #define USE (1ULL)
// #define VALID_DIRTY (1ULL << 31)
// #define USE_DIRTY (VALID_DIRTY | USE)
// #define VALID (0ULL)
// #define INVALID (0xffffffffULL)
// #define BUSY ((0xffffffffULL)-1)
#define ALL_CTRLS 0xffffffffffffffff
//broken
#define VALID_DIRTY (1ULL << 31)
#define USE_DIRTY (VALID_DIRTY | USE)
#define INVALID 0x00000000U
#define VALID 0x80000000U
#define BUSY 0x40000000U
#define DIRTY 0x20000000U
#define CNT_SHIFT (29ULL)
#define CNT_MASK 0x1fffffffU
#define VALID_MASK 0x7
#define BUSY_MASK 0xb
#define DISABLE_BUSY_ENABLE_VALID 0xc0000000U
#define DISABLE_BUSY_MASK 0xbfffffffU
#define NV_NB 0x00U
#define NV_B 0x01U
#define V_NB 0x02U
#define V_B 0x03U
struct page_cache_t;
struct page_cache_d_t;
//typedef padded_struct_pc* page_states_t;
template <typename T>
struct range_t;
template<typename T>
struct array_d_t;
template <typename T>
struct range_d_t;
/*struct data_page_t {
simt::atomic<uint64_t, simt::thread_scope_device> state; //state
//
uint32_t offset;
};
*/
typedef struct __align__(32) {
simt::atomic<uint32_t, simt::thread_scope_device> state; //state
//
uint32_t offset;
//uint8_t pad[32-4-4];
} __attribute__((aligned (32))) data_page_t;
typedef data_page_t* pages_t;
template<typename T>
struct returned_cache_page_t {
T* addr;
uint32_t size;
uint32_t offset;
T operator[](size_t i) const {
if (i < size)
return addr[i];
else
return 0;
}
T& operator[](size_t i) {
if (i < size)
return addr[i];
else
return addr[0];
}
};
#define THREAD_ 0
#define SHARED_ 1
#define GLOBAL_ 2
//#ifdef __CUDACC__
#define TID ( (threadIdx.x + blockDim.x * (threadIdx.y + blockDim.y * threadIdx.z)))
//#else
//#define TID 0
//#endif
//#ifdef __CUDACC__
#define BLKSIZE ( (blockDim.x * blockDim.y * blockDim.z) )
//#else
//#define BLKSIZE 1
//#endif
#ifdef __CUDACC__
#define SYNC (loc != THREAD_ ? __syncthreads() : (void)0)
#else
#define SYNC (void)0
#endif
#define INVALID_ 0x00000000
#define VALID_ 0x80000000
#define BUSY_ 0x40000000
#define CNT_MASK_ 0x3fffffff
#define INVALID_MASK_ 0x7fffffff
#define DISABLE_BUSY_MASK_ 0xbfffffff
template<simt::thread_scope _scope = simt::thread_scope_device>
struct tlb_entry {
uint64_t global_id;
simt::atomic<uint32_t, _scope> state;
data_page_t* page = nullptr;
__forceinline__
__host__ __device__
tlb_entry() { init(); }
__forceinline__
__host__ __device__
void init() {
global_id = 0;
state.store(0, simt::memory_order_relaxed);
page = nullptr;
}
__forceinline__
__device__
void release(const uint32_t count) {
// if (global_id == 515920192)
// printf("--(2)st: %llx\tcount: %llu\n", (unsigned long long) state.load(simt::memory_order_relaxed), (unsigned long long) count);
state.fetch_sub(count, simt::memory_order_release); }
__forceinline__
__device__
void release() { if (page != nullptr) {
// if (global_id == 515920192)
// printf("--(1)st: %llx\tcount: %llu\n", (unsigned long long) state.load(simt::memory_order_relaxed), (unsigned long long) 1);
page->state.fetch_sub(1, simt::memory_order_release); }}
};
template<typename T, size_t n = 32, simt::thread_scope _scope = simt::thread_scope_device, size_t loc = GLOBAL_>
struct tlb {
tlb_entry<_scope> entries[n];
array_d_t<T>* array = nullptr;
__forceinline__
__host__ __device__
tlb() {}
/*
__forceinline__
__device__
tlb(array_d_t<T>* a) { init(a); }
*/
__forceinline__
__device__
void init(array_d_t<T>* a) {
//SYNC;
// __syncthreads();
if (n) {
size_t tid = TID;
if (tid == 0)
array = a;
for (; tid < n; tid+=BLKSIZE)
entries[tid].init();
}
// __syncthreads();
// SYNC;
}
__forceinline__
__device__
void fini() {
// SYNC;
// __syncthreads();
if (n) {
size_t tid = TID;
for (; tid < n; tid+=BLKSIZE)
entries[tid].release();
}
// __syncthreads();
// SYNC;
}
__forceinline__
__device__
~tlb() { }
__forceinline__
__device__
T* acquire(const size_t i, const size_t gid, size_t& start, size_t& end, range_d_t<T>* range, const size_t page_) {
//size_t gid = array->get_page_gid(i);
uint32_t lane = lane_id();
size_t ent = gid % n;
tlb_entry<_scope>* entry = entries + ent;
uint32_t mask = __activemask();
uint32_t eq_mask = __match_any_sync(mask, gid);
eq_mask &= __match_any_sync(mask, (uint64_t)this);
uint32_t master = __ffs(eq_mask) - 1;
uint32_t count = __popc(eq_mask);
uint64_t base_master, base;
if (lane == master) {
uint64_t c = 0;
bool cont = false;
uint32_t st;
do {
//lock;
do {
st = entry->state.fetch_or(VALID_, simt::memory_order_acquire);
if ((st & VALID_) == 0)
break;
__nanosleep(100);
} while (true);
if ((entry->page != nullptr) && (gid == entry->global_id)) {
// if (gid == 515920192)
// printf("++(1)st: %llx\tst&Val: %llx\tcount: %llu\n", (unsigned long long) st, (unsigned long long) (st & VALID_), (unsigned long long) count);
st += count;
base_master = (uint64_t) range->get_cache_page_addr(entry->page->offset);
entry->state.store(st, simt::memory_order_release);
break;
}
else if(((entry->page == nullptr)) || (((st & 0x3fffffff) == 0))) {
// if (gid == 515920192)
// printf("++(2)st: %llx\tst&Val: %llx\tVal: %llx\tcount: %llu\n", (unsigned long long) st, (unsigned long long) (st & VALID_), (unsigned long long) VALID_, (unsigned long long) count);
//
if (entry->page != nullptr)
entry->page->state.fetch_sub(1, simt::memory_order_release);
data_page_t* page = nullptr;// = (data_page_t*)0xffffffffffffffff;
base_master = (uint64_t) array->acquire_page_(i, page, start, end, range, page_);
if (((uint64_t) page == 0xffffffffffffffff) || (page == nullptr))
printf("failure\n");
entry->page = page;
entry->global_id = gid;
st += count;
entry->state.store(st, simt::memory_order_release);
break;
}
else {
if (++c % 100000 == 0)
printf("c: %llu\ttid: %llu\twanted_gid: %llu\tgot_gid: %llu\tst: %llx\tst&0x7: %llx\n", (unsigned long long) c, (unsigned long long) (TID), (unsigned long long) gid, (unsigned long long) entry->global_id, (unsigned long long) st, (unsigned long long) (st & 0x7fffffff));
entry->state.store(st, simt::memory_order_relaxed);
__nanosleep(100);
}
} while(true);
}
base_master = __shfl_sync(eq_mask, base_master, master);
return (T*) base_master;
}
__forceinline__
__device__
void release(const size_t gid) {
//size_t gid = array->get_page_gid(i);
uint32_t lane = lane_id();
uint32_t mask = __activemask();
uint32_t eq_mask = __match_any_sync(mask, gid);
eq_mask &= __match_any_sync(mask, (uint64_t)this);
uint32_t master = __ffs(eq_mask) - 1;
uint32_t count = __popc(eq_mask);
size_t ent = gid % n;
tlb_entry<_scope>* entry = entries + ent;
if (lane == master)
entry->release(count);
__syncwarp(eq_mask);
}
};
template<typename T, size_t n = 32, simt::thread_scope _scope = simt::thread_scope_device, size_t loc = GLOBAL_>
struct bam_ptr_tlb {
tlb<T,n,_scope,loc>* tlb_ = nullptr;
array_d_t<T>* array = nullptr;
range_d_t<T>* range;
size_t page;
size_t start = 0;
size_t end = 0;
size_t gid = 0;
//int64_t range_id = -1;
T* addr = nullptr;
__forceinline__
__host__ __device__
bam_ptr_tlb(array_d_t<T>* a, tlb<T,n,_scope,loc>* t) { init(a, t); }
__forceinline__
__device__
~bam_ptr_tlb() { fini(); }
__forceinline__
__host__ __device__
void init(array_d_t<T>* a, tlb<T,n,_scope,loc>* t) { array = a; tlb_ = t; }
__forceinline__
__device__
void fini(void) {
if (addr) {
tlb_->release(gid);
addr = nullptr;
}
}
__forceinline__
__device__
void update_page(const size_t i) {
////printf("++++acquire: i: %llu\tpage: %llu\tstart: %llu\tend: %llu\trange: %llu\n",
// (unsigned long long) i, (unsigned long long) page, (unsigned long long) start, (unsigned long long) end, (unsigned long long) range_id);
fini(); //destructor
array->get_page_gid(i, range, page, gid);
addr = (T*) tlb_->acquire(i, gid, start, end, range, page);
// //printf("----acquire: i: %llu\tpage: %llu\tstart: %llu\tend: %llu\trange: %llu\n",
// (unsigned long long) i, (unsigned long long) page, (unsigned long long) start, (unsigned long long) end, (unsigned long long) range_id);
}
__forceinline__
__device__
T operator[](const size_t i) const {
if ((i < start) || (i >= end)) {
update_page(i);
}
return addr[i-start];
}
__forceinline__
__device__
T& operator[](const size_t i) {
if ((i < start) || (i >= end)) {
update_page(i);
range->mark_page_dirty(page);
}
return addr[i-start];
}
};
template<typename T>
struct bam_ptr {
data_page_t* page = nullptr;
array_d_t<T>* array = nullptr;
size_t start = 0;
size_t end = 0;
int64_t range_id = -1;
T* addr = nullptr;
__forceinline__
__host__ __device__
bam_ptr(array_d_t<T>* a) { init(a); }
__forceinline__
__host__ __device__
~bam_ptr() { fini(); }
__forceinline__
__host__ __device__
void init(array_d_t<T>* a) { array = a; }
__forceinline__
__host__ __device__
void fini(void) {
if (page) {
array->release_page(page, range_id, start);
page = nullptr;
}
}
__forceinline__
__host__ __device__
T* update_page(const size_t i) {
////printf("++++acquire: i: %llu\tpage: %llu\tstart: %llu\tend: %llu\trange: %llu\n",
// (unsigned long long) i, (unsigned long long) page, (unsigned long long) start, (unsigned long long) end, (unsigned long long) range_id);
fini(); //destructor
addr = (T*) array->acquire_page(i, page, start, end, range_id);
// //printf("----acquire: i: %llu\tpage: %llu\tstart: %llu\tend: %llu\trange: %llu\n",
// (unsigned long long) i, (unsigned long long) page, (unsigned long long) start, (unsigned long long) end, (unsigned long long) range_id);
return addr;
}
__forceinline__
__host__ __device__
T operator[](const size_t i) const {
if ((i < start) || (i >= end)) {
T* tmpaddr = update_page(i);
}
return addr[i-start];
}
__host__ __device__
T* memref(size_t i) {
T* ret_;
if ((i < start) || (i >= end)) {
ret_ = update_page(i);
}
return ret_;
}
__forceinline__
__host__ __device__
T& operator[](const size_t i) {
if ((i < start) || (i >= end)) {
update_page(i);
page->state.fetch_or(DIRTY, simt::memory_order_relaxed);
}
return addr[i-start];
}
};
typedef struct __align__(32) {
simt::atomic<uint32_t, simt::thread_scope_device> page_take_lock; //state
//
uint64_t page_translation;
uint8_t pad[32-8];
} __attribute__((aligned (32))) cache_page_t;
/*
struct cache_page_t {
simt::atomic<uint32_t, simt::thread_scope_device> page_take_lock;
uint32_t page_translation;
uint8_t range_id;
};
*/
struct page_cache_d_t {
uint8_t* base_addr;
uint64_t page_size;
uint64_t page_size_minus_1;
uint64_t page_size_log;
uint64_t n_pages;
uint64_t n_pages_minus_1;
cache_page_t* cache_pages;
//uint32_t* page_translation; //len = num of pages in cache
//padded_struct_pc* page_translation; //len = num of pages in cache
//padded_struct_pc* page_take_lock; //len = num of pages in cache
padded_struct_pc* page_ticket;
uint64_t* prp1; //len = num of pages in cache
uint64_t* prp2; //len = num of pages in cache if page_size = ctrl.page_size *2
//uint64_t* prp_list; //len = num of pages in cache if page_size > ctrl.page_size *2
uint64_t ctrl_page_size;
uint64_t range_cap;
//uint64_t range_count;
pages_t* ranges;
pages_t* h_ranges;
uint64_t n_ranges;
uint64_t n_ranges_bits;
uint64_t n_ranges_mask;
uint64_t n_cachelines_for_states;
uint64_t* ranges_page_starts;
data_dist_t* ranges_dists;
simt::atomic<uint64_t, simt::thread_scope_device>* ctrl_counter;
simt::atomic<uint64_t, simt::thread_scope_device>* q_head;
simt::atomic<uint64_t, simt::thread_scope_device>* q_tail;
simt::atomic<uint64_t, simt::thread_scope_device>* q_lock;
simt::atomic<uint64_t, simt::thread_scope_device>* extra_reads;
Controller** d_ctrls;
uint64_t n_ctrls;
bool prps;
uint64_t n_blocks_per_page;
__forceinline__
__device__
cache_page_t* get_cache_page(const uint32_t page) const;
__forceinline__
__device__
uint32_t find_slot(uint64_t address, uint64_t range_id, const uint32_t queue_);
};
__device__ void read_data(page_cache_d_t* pc, QueuePair* qp, const uint64_t starting_lba, const uint64_t n_blocks, const unsigned long long pc_entry);
__device__ void write_data(page_cache_d_t* pc, QueuePair* qp, const uint64_t starting_lba, const uint64_t n_blocks, const unsigned long long pc_entry);
__forceinline__
__device__
uint64_t get_backing_page_(const uint64_t page_start, const size_t page_offset, const uint64_t n_ctrls, const data_dist_t dist) {
uint64_t page = page_start;
if (dist == STRIPE) {
page += page_offset / n_ctrls;
}
else if (dist == REPLICATE) {
page += page_offset;
}
return page;
}
__forceinline__
__device__
uint64_t get_backing_ctrl_(const size_t page_offset, const uint64_t n_ctrls, const data_dist_t dist) {
uint64_t ctrl;
if (dist == STRIPE) {
ctrl = page_offset % n_ctrls;
}
else if (dist == REPLICATE) {
ctrl = ALL_CTRLS;
}
return ctrl;
}
__global__
void __flush(page_cache_d_t* pc) {
uint64_t page = threadIdx.x + blockIdx.x * blockDim.x;
if (page < pc->n_pages) {
uint64_t previous_global_address = pc->cache_pages[page].page_translation;
//uint8_t previous_range = this->cache_pages[page].range_id;
uint64_t previous_range = previous_global_address & pc->n_ranges_mask;
uint64_t previous_address = previous_global_address >> pc->n_ranges_bits;
//uint32_t new_state = BUSY;
uint32_t expected_state = pc->ranges[previous_range][previous_address].state.load(simt::memory_order_relaxed);
uint32_t d = expected_state & DIRTY;
uint32_t smid = get_smid();
if (d) {
uint64_t ctrl = get_backing_ctrl_(previous_address, pc->n_ctrls, pc->ranges_dists[previous_range]);
//uint64_t get_backing_page(const uint64_t page_start, const size_t page_offset, const uint64_t n_ctrls, const data_dist_t dist) {
uint64_t index = get_backing_page_(pc->ranges_page_starts[previous_range], previous_address, pc->n_ctrls, pc->ranges_dists[previous_range]);
// //printf("Eviciting range_id: %llu\tpage_id: %llu\tctrl: %llx\tindex: %llu\n",
// (unsigned long long) previous_range, (unsigned long long)previous_address,
// (unsigned long long) ctrl, (unsigned long long) index);
if (ctrl == ALL_CTRLS) {
for (ctrl = 0; ctrl < pc->n_ctrls; ctrl++) {
Controller* c = pc->d_ctrls[ctrl];
uint32_t queue = smid % (c->n_qps);
write_data(pc, (c->d_qps)+queue, (index*pc->n_blocks_per_page), pc->n_blocks_per_page, page);
}
}
else {
Controller* c = pc->d_ctrls[ctrl];
uint32_t queue = smid % (c->n_qps);
//index = ranges_page_starts[previous_range] + previous_address;
write_data(pc, (c->d_qps)+queue, (index*pc->n_blocks_per_page), pc->n_blocks_per_page, page);
}
pc->ranges[previous_range][previous_address].state.fetch_and(~DIRTY);
}
}
}
struct page_cache_t {
//void* d_pc;
//BufferPtr prp2_list_buf;
page_cache_d_t pdt;
//void* d_pc;
//BufferPtr prp2_list_buf;
//bool prps;
pages_t* h_ranges;
uint64_t* h_ranges_page_starts;
data_dist_t* h_ranges_dists;
page_cache_d_t* d_pc_ptr;
DmaPtr pages_dma;
DmaPtr prp_list_dma;
BufferPtr prp1_buf;
BufferPtr prp2_buf;
BufferPtr cache_pages_buf;
//BufferPtr page_translation_buf;
//BufferPtr page_take_lock_buf;
BufferPtr ranges_buf;
BufferPtr pc_buff;
BufferPtr d_ctrls_buff;
BufferPtr ranges_page_starts_buf;
BufferPtr ranges_dists_buf;
BufferPtr page_ticket_buf;
BufferPtr ctrl_counter_buf;
BufferPtr q_head_buf;
BufferPtr q_tail_buf;
BufferPtr q_lock_buf;
BufferPtr extra_reads_buf;
void print_reset_stats(void) {
uint64_t v = 0;
cuda_err_chk(cudaMemcpy(&v, pdt.extra_reads, sizeof(simt::atomic<uint64_t, simt::thread_scope_device>), cudaMemcpyDeviceToHost));
cuda_err_chk(cudaMemset(pdt.extra_reads, 0, sizeof(simt::atomic<uint64_t, simt::thread_scope_device>)));
// printf("Cache Extra Reads: %llu\n", v);
}
void flush_cache() {
size_t threads = 64;
size_t n_blocks = (pdt.n_pages + threads - 1) / threads;
__flush<<<n_blocks, threads>>>(d_pc_ptr);
}
template <typename T>
void add_range(range_t<T>* range) {
range->rdt.range_id = pdt.n_ranges++;
h_ranges[range->rdt.range_id] = range->rdt.pages;
h_ranges_page_starts[range->rdt.range_id] = range->rdt.page_start;
h_ranges_dists[range->rdt.range_id] = range->rdt.dist;
cuda_err_chk(cudaMemcpy(pdt.ranges_page_starts, h_ranges_page_starts, pdt.n_ranges * sizeof(uint64_t), cudaMemcpyHostToDevice));
cuda_err_chk(cudaMemcpy(pdt.ranges, h_ranges, pdt.n_ranges* sizeof(pages_t), cudaMemcpyHostToDevice));
cuda_err_chk(cudaMemcpy(pdt.ranges_dists, h_ranges_dists, pdt.n_ranges* sizeof(data_dist_t), cudaMemcpyHostToDevice));
cuda_err_chk(cudaMemcpy(d_pc_ptr, &pdt, sizeof(page_cache_d_t), cudaMemcpyHostToDevice));
}
page_cache_t(const uint64_t ps, const uint64_t np, const uint32_t cudaDevice, const Controller& ctrl, const uint64_t max_range, const std::vector<Controller*>& ctrls) {
ctrl_counter_buf = createBuffer(sizeof(simt::atomic<uint64_t, simt::thread_scope_device>), cudaDevice);
q_head_buf = createBuffer(sizeof(simt::atomic<uint64_t, simt::thread_scope_device>), cudaDevice);
q_tail_buf = createBuffer(sizeof(simt::atomic<uint64_t, simt::thread_scope_device>), cudaDevice);
q_lock_buf = createBuffer(sizeof(simt::atomic<uint64_t, simt::thread_scope_device>), cudaDevice);
extra_reads_buf = createBuffer(sizeof(simt::atomic<uint64_t, simt::thread_scope_device>), cudaDevice);
pdt.ctrl_counter = (simt::atomic<uint64_t, simt::thread_scope_device>*)ctrl_counter_buf.get();
pdt.page_size = ps;
pdt.q_head = (simt::atomic<uint64_t, simt::thread_scope_device>*)q_head_buf.get();
pdt.q_tail = (simt::atomic<uint64_t, simt::thread_scope_device>*)q_tail_buf.get();
pdt.q_lock = (simt::atomic<uint64_t, simt::thread_scope_device>*)q_lock_buf.get();
pdt.extra_reads = (simt::atomic<uint64_t, simt::thread_scope_device>*)extra_reads_buf.get();
pdt.page_size_minus_1 = ps - 1;
pdt.n_pages = np;
pdt.ctrl_page_size = ctrl.ctrl->page_size;
pdt.n_pages_minus_1 = np - 1;
pdt.n_ctrls = ctrls.size();
d_ctrls_buff = createBuffer(pdt.n_ctrls * sizeof(Controller*), cudaDevice);
pdt.d_ctrls = (Controller**) d_ctrls_buff.get();
pdt.n_blocks_per_page = (ps/ctrl.blk_size);
pdt.n_cachelines_for_states = np/STATES_PER_CACHELINE;
for (size_t k = 0; k < pdt.n_ctrls; k++)
cuda_err_chk(cudaMemcpy(pdt.d_ctrls+k, &(ctrls[k]->d_ctrl_ptr), sizeof(Controller*), cudaMemcpyHostToDevice));
//n_ctrls = ctrls.size();
//d_ctrls_buff = createBuffer(n_ctrls * sizeof(Controller*), cudaDevice);
//d_ctrls = (Controller**) d_ctrls_buff.get();
//for (size_t k = 0; k < n_ctrls; k++)
// cuda_err_chk(cudaMemcpy(d_ctrls+k, &(ctrls[k]->d_ctrl_ptr), sizeof(Controller*), cudaMemcpyHostToDevice));
pdt.range_cap = max_range;
pdt.n_ranges = 0;
pdt.n_ranges_bits = (max_range == 1) ? 1 : std::log2(max_range);
pdt.n_ranges_mask = max_range-1;
std::cout << "n_ranges_bits: " << std::dec << pdt.n_ranges_bits << std::endl;
std::cout << "n_ranges_mask: " << std::dec << pdt.n_ranges_mask << std::endl;
pdt.page_size_log = std::log2(ps);
ranges_buf = createBuffer(max_range * sizeof(pages_t), cudaDevice);
pdt.ranges = (pages_t*)ranges_buf.get();
h_ranges = new pages_t[max_range];
h_ranges_page_starts = new uint64_t[max_range];
std::memset(h_ranges_page_starts, 0, max_range * sizeof(uint64_t));
//pages_translation_buf = createBuffer(np * sizeof(uint32_t), cudaDevice);
//pdt.page_translation = (uint32_t*)page_translation_buf.get();
//page_translation_buf = createBuffer(np * sizeof(padded_struct_pc), cudaDevice);
//page_translation = (padded_struct_pc*)page_translation_buf.get();
//page_take_lock_buf = createBuffer(np * sizeof(padded_struct_pc), cudaDevice);
//pdt.page_take_lock = (padded_struct_pc*)page_take_lock_buf.get();
cache_pages_buf = createBuffer(np * sizeof(cache_page_t), cudaDevice);
pdt.cache_pages = (cache_page_t*)cache_pages_buf.get();
ranges_page_starts_buf = createBuffer(max_range * sizeof(uint64_t), cudaDevice);
pdt.ranges_page_starts = (uint64_t*) ranges_page_starts_buf.get();
page_ticket_buf = createBuffer(1 * sizeof(padded_struct_pc), cudaDevice);
pdt.page_ticket = (padded_struct_pc*)page_ticket_buf.get();
//std::vector<padded_struct_pc> tps(np, FREE);
cache_page_t* tps = new cache_page_t[np];
for (size_t i = 0; i < np; i++)
tps[i].page_take_lock = FREE;
cuda_err_chk(cudaMemcpy(pdt.cache_pages, tps, np*sizeof(cache_page_t), cudaMemcpyHostToDevice));
delete tps;
ranges_dists_buf = createBuffer(max_range * sizeof(data_dist_t), cudaDevice);
pdt.ranges_dists = (data_dist_t*)ranges_dists_buf.get();
h_ranges_dists = new data_dist_t[max_range];
uint64_t cache_size = ps*np;
this->pages_dma = createDma(ctrl.ctrl, NVM_PAGE_ALIGN(cache_size, 1UL << 16), cudaDevice);
pdt.base_addr = (uint8_t*) this->pages_dma.get()->vaddr;
std::cout << "pages_dma: " << std::hex << this->pages_dma.get()->vaddr << "\t" << this->pages_dma.get()->ioaddrs[0] << std::endl;
std::cout << "HEREN\n";
const uint32_t uints_per_page = ctrl.ctrl->page_size / sizeof(uint64_t);
if ((pdt.page_size > (ctrl.ctrl->page_size * uints_per_page)) || (np == 0) || (pdt.page_size < ctrl.ns.lba_data_size))
throw error(string("page_cache_t: Can't have such page size or number of pages"));
if (ps <= this->pages_dma.get()->page_size) {
std::cout << "Cond1\n";
uint64_t how_many_in_one = ctrl.ctrl->page_size/ps;
this->prp1_buf = createBuffer(np * sizeof(uint64_t), cudaDevice);
pdt.prp1 = (uint64_t*) this->prp1_buf.get();
std::cout << np << " " << sizeof(uint64_t) << " " << how_many_in_one << " " << this->pages_dma.get()->n_ioaddrs <<std::endl;
uint64_t* temp = new uint64_t[how_many_in_one * this->pages_dma.get()->n_ioaddrs];
std::memset(temp, 0, how_many_in_one * this->pages_dma.get()->n_ioaddrs);
if (temp == NULL)
std::cout << "NULL\n";
for (size_t i = 0; (i < this->pages_dma.get()->n_ioaddrs) ; i++) {
for (size_t j = 0; (j < how_many_in_one); j++) {
temp[i*how_many_in_one + j] = ((uint64_t)this->pages_dma.get()->ioaddrs[i]) + j*ps;
//std::cout << std::dec << "\ti: " << i << "\tj: " << j << "\tindex: "<< (i*how_many_in_one + j) << "\t" << std::hex << (((uint64_t)this->pages_dma.get()->ioaddrs[i]) + j*ps) << std::dec << std::endl;
}
}
cuda_err_chk(cudaMemcpy(pdt.prp1, temp, np * sizeof(uint64_t), cudaMemcpyHostToDevice));
delete temp;
//std::cout << "HERE1\n";
//free(temp);
//std::cout << "HERE2\n";
pdt.prps = false;
}
else if ((ps > this->pages_dma.get()->page_size) && (ps <= (this->pages_dma.get()->page_size * 2))) {
this->prp1_buf = createBuffer(np * sizeof(uint64_t), cudaDevice);
pdt.prp1 = (uint64_t*) this->prp1_buf.get();
this->prp2_buf = createBuffer(np * sizeof(uint64_t), cudaDevice);
pdt.prp2 = (uint64_t*) this->prp2_buf.get();
//uint64_t* temp1 = (uint64_t*) malloc(np * sizeof(uint64_t));
uint64_t* temp1 = new uint64_t[np * sizeof(uint64_t)];
std::memset(temp1, 0, np * sizeof(uint64_t));
//uint64_t* temp2 = (uint64_t*) malloc(np * sizeof(uint64_t));
uint64_t* temp2 = new uint64_t[np * sizeof(uint64_t)];
std::memset(temp2, 0, np * sizeof(uint64_t));
for (size_t i = 0; i < np; i++) {
temp1[i] = ((uint64_t)this->pages_dma.get()->ioaddrs[i*2]);
temp2[i] = ((uint64_t)this->pages_dma.get()->ioaddrs[i*2+1]);
}
cuda_err_chk(cudaMemcpy(pdt.prp1, temp1, np * sizeof(uint64_t), cudaMemcpyHostToDevice));
cuda_err_chk(cudaMemcpy(pdt.prp2, temp2, np * sizeof(uint64_t), cudaMemcpyHostToDevice));
delete temp1;
delete temp2;
pdt.prps = true;
}
else {
this->prp1_buf = createBuffer(np * sizeof(uint64_t), cudaDevice);
pdt.prp1 = (uint64_t*) this->prp1_buf.get();
uint32_t prp_list_size = ctrl.ctrl->page_size * np;
this->prp_list_dma = createDma(ctrl.ctrl, NVM_PAGE_ALIGN(prp_list_size, 1UL << 16), cudaDevice);
this->prp2_buf = createBuffer(np * sizeof(uint64_t), cudaDevice);
pdt.prp2 = (uint64_t*) this->prp2_buf.get();
uint64_t* temp1 = new uint64_t[np * sizeof(uint64_t)];
uint64_t* temp2 = new uint64_t[np * sizeof(uint64_t)];
uint64_t* temp3 = new uint64_t[prp_list_size];
std::memset(temp1, 0, np * sizeof(uint64_t));
std::memset(temp2, 0, np * sizeof(uint64_t));
std::memset(temp3, 0, prp_list_size);
uint32_t how_many_in_one = ps / ctrl.ctrl->page_size ;
for (size_t i = 0; i < np; i++) {
temp1[i] = ((uint64_t) this->pages_dma.get()->ioaddrs[i*how_many_in_one]);
temp2[i] = ((uint64_t) this->prp_list_dma.get()->ioaddrs[i]);
for(size_t j = 0; j < (how_many_in_one-1); j++) {
temp3[i*uints_per_page + j] = ((uint64_t) this->pages_dma.get()->ioaddrs[i*how_many_in_one + j + 1]);
}
}
/*
for (size_t i = 0; i < this->pages_dma.get()->n_ioaddrs; i+=how_many_in_one) {
temp1[i/how_many_in_one] = ((uint64_t)this->pages_dma.get()->ioaddrs[i]);
temp2[i/how_many_in_one] = ((uint64_t)this->prp_list_dma.get()->ioaddrs[i]);
for (size_t j = 0; j < (how_many_in_one-1); j++) {
temp3[(i/how_many_in_one)*uints_per_page + j] = ((uint64_t)this->pages_dma.get()->ioaddrs[i+1+j]);
}
}
*/
std::cout << "Done creating PRP\n";
cuda_err_chk(cudaMemcpy(pdt.prp1, temp1, np * sizeof(uint64_t), cudaMemcpyHostToDevice));
cuda_err_chk(cudaMemcpy(pdt.prp2, temp2, np * sizeof(uint64_t), cudaMemcpyHostToDevice));
cuda_err_chk(cudaMemcpy(this->prp_list_dma.get()->vaddr, temp3, prp_list_size, cudaMemcpyHostToDevice));
delete temp1;
delete temp2;
delete temp3;
pdt.prps = true;
}
pc_buff = createBuffer(sizeof(page_cache_d_t), cudaDevice);
d_pc_ptr = (page_cache_d_t*)pc_buff.get();
cuda_err_chk(cudaMemcpy(d_pc_ptr, &pdt, sizeof(page_cache_d_t), cudaMemcpyHostToDevice));
std::cout << "Finish Making Page Cache\n";
}
~page_cache_t() {
delete h_ranges;
delete h_ranges_page_starts;
delete h_ranges_dists;
}
};
template <typename T>
struct range_d_t {
uint64_t index_start;
uint64_t count;
uint64_t range_id;
uint64_t page_start_offset;
uint64_t page_size;
uint64_t page_start;
uint64_t page_count;
size_t n_elems_per_page;
data_dist_t dist;
uint8_t* src;
simt::atomic<uint64_t, simt::thread_scope_device> access_cnt;
simt::atomic<uint64_t, simt::thread_scope_device> miss_cnt;
simt::atomic<uint64_t, simt::thread_scope_device> hit_cnt;
simt::atomic<uint64_t, simt::thread_scope_device> read_io_cnt;
pages_t pages;
//padded_struct_pc* page_addresses;
//uint32_t* page_addresses;
//padded_struct_pc* page_vals; //len = num of pages for data
//void* self_ptr;
page_cache_d_t cache;
//range_d_t(range_t<T>* rt);
__forceinline__
__device__
uint64_t get_backing_page(const size_t i) const;
__forceinline__
__device__
uint64_t get_backing_ctrl(const size_t i) const;
__forceinline__
__device__
uint64_t get_sector_size() const;
__forceinline__
__device__
uint64_t get_page(const size_t i) const;
__forceinline__
__device__
uint64_t get_subindex(const size_t i) const;
__forceinline__
__device__
uint64_t get_global_address(const size_t page) const;
__forceinline__
__device__
void release_page(const size_t pg) const;
__forceinline__
__device__
void release_page(const size_t pg, const uint32_t count) const;
__forceinline__
__device__
uint64_t acquire_page(const size_t pg, const uint32_t count, const bool write, const uint32_t ctrl, const uint32_t queue) ;
__forceinline__
__device__
void write_done(const size_t pg, const uint32_t count) const;
__forceinline__
__device__
T operator[](const size_t i) ;
__forceinline__
__device__
void operator()(const size_t i, const T val);
__forceinline__
__device__
cache_page_t* get_cache_page(const size_t pg) const;
__forceinline__
__device__
uint64_t get_cache_page_addr(const uint32_t page_trans) const;
__forceinline__
__device__
void mark_page_dirty(const size_t index);
};
template <typename T>
struct range_t {
range_d_t<T> rdt;
range_d_t<T>* d_range_ptr;
page_cache_d_t* cache;
BufferPtr pages_buff;
//BufferPtr page_addresses_buff;
BufferPtr range_buff;
range_t(uint64_t is, uint64_t count, uint64_t ps, uint64_t pc, uint64_t pso, uint64_t p_size, page_cache_t* c_h, uint32_t cudaDevice, data_dist_t dist = REPLICATE);
};
template <typename T>
range_t<T>::range_t(uint64_t is, uint64_t count, uint64_t ps, uint64_t pc, uint64_t pso, uint64_t p_size, page_cache_t* c_h, uint32_t cudaDevice, data_dist_t dist) {
rdt.access_cnt = 0;
rdt.miss_cnt = 0;
rdt.hit_cnt = 0;
rdt.read_io_cnt = 0;
rdt.index_start = is;
rdt.count = count;
//range_id = (c_h->range_count)++;
rdt.page_start = ps;
rdt.page_count = pc;
rdt.page_size = c_h->pdt.page_size;
rdt.page_start_offset = pso;
rdt.dist = dist;
size_t s = pc;//(rdt.page_end-rdt.page_start);//*page_size / c_h->page_size;
rdt.n_elems_per_page = rdt.page_size / sizeof(T);
cache = (page_cache_d_t*) c_h->d_pc_ptr;