forked from wuxb45/remixdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blkio.c
1130 lines (999 loc) · 25.3 KB
/
blkio.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
/*
* Copyright (c) 2016--2021 Wu, Xingbo <[email protected]>
*
* All rights reserved. No warranty, explicit or implicit, provided.
*/
#define _GNU_SOURCE
// include {{{
#include "lib.h"
#include "ctypes.h"
#include <sys/ioctl.h>
#include <assert.h>
#include <aio.h>
#include "blkio.h"
// }}} include
// wring {{{
// wring is NOT thread-safe
struct wring {
void * mem;
void * head;
size_t memsz;
u32 iosz;
int fd;
#if defined(LIBURING)
u32 batch; // submit when pending >= batch
u32 nring; // number of pending + submitted
u32 pending; // number of unsubmitted sqes
bool fixed_file; // use this fd for write (0 for registered file)
bool fixed_mem; // use write_fixed()
bool padding[2];
struct io_uring uring;
#else
u32 off_mask;
u32 off_submit;
u32 off_finish; // nothing to finish if == submit
u32 reserved;
struct {
struct aiocb aiocb;
void * data;
} aring[0];
#endif // LIBURING
};
struct wring *
wring_create(const int fd, const u32 iosz, const u32 depth0)
{
debug_assert(depth0);
#if defined(LIBURING)
const u32 depth = depth0 < 64 ? bits_p2_up_u32(depth0) : 64;
struct wring * const wring = calloc(1, sizeof(*wring));
#else // POSIX AIO
const u32 depth = depth0 < 8 ? bits_p2_up_u32(depth0) : 8;
struct wring * const wring = calloc(1, sizeof(*wring) + (sizeof(wring->aring[0]) * depth));
#endif // LIBURING
const u64 iosz_64 = iosz; // memsz can be larger
if (!wring)
return NULL;
const size_t memsz = bits_round_up(iosz_64 * depth, 21); // a multiple of 2MB
// 2MB buffer (initialized to zero by mmap)
u8 * const mem = pages_alloc_best(memsz, false, &wring->memsz);
if (!mem) {
free(wring);
return NULL;
}
// link all
for (u64 i = 0; i < depth-1; i++)
*(u64 *)(mem + (iosz_64 * i)) = (u64)(mem + (iosz_64 * (i+1)));
wring->mem = mem;
wring->head = mem;
wring->iosz = iosz;
wring->fd = fd;
#if defined(LIBURING)
wring->batch = depth >> 2; // 1/4
struct io_uring_params p = {};
// uncomment to use sqpoll (must use root or sys_admin)
//p.flags = IORING_SETUP_SQPOLL | IORING_SETUP_SQ_AFF;
//p.sq_thread_cpu = 2;
if (io_uring_queue_init_params(depth << 1, &wring->uring, &p)) {
pages_unmap(wring->mem, wring->memsz);
free(wring);
return NULL;
}
// register memory and file
struct iovec vecs = {mem, wring->memsz};
// enable memlock in /etc/security/limits.conf
wring->fixed_mem = io_uring_register_buffers(&wring->uring, &vecs, 1) == 0;
// this usually works
wring->fixed_file = io_uring_register_files(&wring->uring, &fd, 1) == 0;
#else
wring->off_mask = depth - 1;
#endif // LIBURING
return wring;
}
void
wring_update_fd(struct wring * const wring, const int fd)
{
wring->fd = fd;
#if defined(LIBURING)
if (wring->fixed_file) {
const int r = io_uring_register_files_update(&wring->uring, 0, &wring->fd, 1);
if (r != 1)
wring->fixed_file = false;
}
#endif // LIBURING
}
void
wring_destroy(struct wring * const wring)
{
wring_flush(wring);
#if defined(LIBURING)
io_uring_queue_exit(&wring->uring);
#endif // LIBURING
pages_unmap(wring->mem, wring->memsz);
free(wring);
}
static void *
wring_wait(struct wring * const wring)
{
#if defined(LIBURING)
debug_assert(wring->nring);
struct io_uring_cqe * cqe = NULL;
// wait and directly return buffer
int ret = io_uring_wait_cqe(&wring->uring, &cqe);
if (ret)
debug_die();
if (cqe->res < 0)
debug_die();
void * const ptr = io_uring_cqe_get_data(cqe);
io_uring_cqe_seen(&wring->uring, cqe);
wring->nring--;
#else // AIO
debug_assert(wring->off_submit != wring->off_finish);
const u32 i = wring->off_finish & wring->off_mask;
struct aiocb * const cb = &(wring->aring[i].aiocb);
do {
const int r = aio_error(cb);
if (r == 0)
break;
else if (r != EINPROGRESS)
debug_die_perror();
cpu_pause();
} while (true);
const ssize_t ret = aio_return(cb);
if (ret != (ssize_t)cb->aio_nbytes)
debug_die();
void * const ptr = wring->aring[i].data;
wring->off_finish++;
#endif // LIBURING
return ptr;
}
static void *
wring_wait_buf(struct wring * const wring)
{
do {
void * const buf = wring_wait(wring);
if (buf)
return buf;
} while (true);
}
void *
wring_acquire(struct wring * const wring)
{
if (wring->head == NULL)
return wring_wait_buf(wring);
// use the free list
void * const ptr = wring->head;
debug_assert(ptr);
wring->head = (void *)(*(u64*)ptr);
return ptr;
}
static void
wring_finish(struct wring * const wring)
{
void * const ptr = wring_wait(wring);
if (ptr) { // may return NULL for fsync
*(u64*)ptr = (u64)(wring->head);
wring->head = ptr;
}
}
#if defined(LIBURING)
static void
wring_submit(struct wring * const wring)
{
const int n = io_uring_submit(&wring->uring);
if (unlikely(n < 0))
debug_die();
debug_assert(n > 0 && (u32)n <= wring->pending);
wring->pending -= (u32)n;
}
#else
static void
wring_wait_slot(struct wring * const wring)
{
// test if the ring is already full
if ((wring->off_finish + wring->off_mask) == wring->off_submit)
wring_finish(wring);
}
#endif // LIBURING
// write a 4kB page
void
wring_write_partial(struct wring * const wring, const off_t off,
void * const buf, const size_t buf_off, const u32 size)
{
debug_assert((buf_off + size) <= wring->iosz);
u8 * const wbuf = ((u8 *)buf) + buf_off;
#if defined(LIBURING)
struct io_uring_sqe * const sqe = io_uring_get_sqe(&wring->uring);
debug_assert(sqe);
const int fd = wring->fixed_file ? 0 : wring->fd;
if (wring->fixed_mem) {
io_uring_prep_write_fixed(sqe, fd, wbuf, size, off, 0);
} else {
io_uring_prep_write(sqe, fd, wbuf, size, off);
}
if (wring->fixed_file)
io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE);
io_uring_sqe_set_data(sqe, buf);
wring->pending++;
wring->nring++;
if (wring->pending >= wring->batch)
wring_submit(wring);
#else // AIO
wring_wait_slot(wring);
const u32 i = wring->off_submit & wring->off_mask;
struct aiocb * const cb = &wring->aring[i].aiocb;
cb->aio_fildes = wring->fd;
cb->aio_buf = wbuf;
cb->aio_nbytes = size;
cb->aio_offset = off;
wring->aring[i].data = buf;
const int r = aio_write(cb);
if (r != 0)
debug_die_perror();
wring->off_submit++;
#endif // LIBURING
}
inline void
wring_write(struct wring * const wring, const off_t off, void * const buf)
{
wring_write_partial(wring, off, buf, 0, wring->iosz);
}
static bool
wring_empty(struct wring * const wring)
{
#if defined(LIBURING)
return wring->nring == 0;
#else
return wring->off_submit == wring->off_finish;
#endif // LIBURING
}
void
wring_flush(struct wring * const wring)
{
#if defined(LIBURING)
while (wring->pending)
wring_submit(wring);
#endif // LIBURING
while (!wring_empty(wring))
wring_finish(wring);
}
void
wring_fsync(struct wring * const wring)
{
#if defined(LIBURING)
struct io_uring_sqe * const sqe = io_uring_get_sqe(&wring->uring);
io_uring_prep_fsync(sqe, wring->fd, IORING_FSYNC_DATASYNC);
//io_uring_sqe_set_data(sqe, NULL); // NULL by prep
wring->pending++;
wring->nring++;
wring_submit(wring); // just submit
#else
wring_wait_slot(wring);
const u32 i = wring->off_submit & wring->off_mask;
struct aiocb * const cb = &wring->aring[i].aiocb;
cb->aio_fildes = wring->fd;
cb->aio_buf = NULL;
cb->aio_nbytes = 0;
wring->aring[i].data = NULL;
#if defined(__FreeBSD__)
const int flags = O_SYNC;
#else
const int flags = O_DSYNC;
#endif
const int r = aio_fsync(flags, cb);
if (r != 0)
debug_die_perror();
wring->off_submit++;
#endif // LIBURING
}
// }}} wring
// coq {{{
// struct {{{
#define COQ_NR ((128u))
#define COQ_MASK ((COQ_NR - 1u))
#define COQ_MAX ((COQ_MASK))
static_assert((COQ_NR & COQ_MASK) == 0, "COQ_NR");
// the wait queue
struct cowqe {
cowq_func func;
void * priv;
};
struct coht {
au32 head; // consume the head
au32 tail; // append to the tail
};
struct coq {
struct coht rqht;
struct coht wqht;
struct co * rq[COQ_NR];
struct cowqe wq[COQ_NR];
#if defined(LIBURING)
struct io_uring uring[0]; // optional uring at the end
#endif // LIBURING
};
// }}} struct
// helpers {{{
static inline u32
coht_nr(struct coht * const ht)
{
const u32 nr = ht->tail - ht->head;
debug_assert(nr < COQ_NR);
return nr;
}
static inline bool
coht_full(struct coht * const ht)
{
return coht_nr(ht) == COQ_MAX;
}
static inline bool
coht_empty(struct coht * const ht)
{
return coht_nr(ht) == 0;
}
static inline u32
coht_enqueue(struct coht * const ht)
{
debug_assert(!coht_full(ht));
const u32 i = ht->tail & COQ_MASK;
ht->tail++;
cpu_cfence();
return i;
}
static inline u32
coht_dequeue(struct coht * const ht)
{
debug_assert(!coht_empty(ht));
const u32 i = ht->head & COQ_MASK;
ht->head++;
cpu_cfence();
return i;
}
static inline u32
corq_nr(struct coq * const q)
{
return coht_nr(&(q->rqht));
}
static inline u32
cowq_nr(struct coq * const q)
{
return coht_nr(&(q->wqht));
}
static inline bool
corq_full(struct coq * const q)
{
return coht_full(&(q->rqht));
}
static inline bool
cowq_full(struct coq * const q)
{
return coht_full(&(q->wqht));
}
static inline bool
corq_empty(struct coq * const q)
{
return coht_empty(&(q->rqht));
}
static inline bool
cowq_empty(struct coq * const q)
{
return coht_empty(&(q->wqht));
}
// }}} helpers
// coq {{{
struct coq *
coq_create(void)
{
struct coq * const q = calloc(1, sizeof(*q));
return q;
}
void
coq_destroy(struct coq * const coq)
{
free(coq);
}
struct coq *
coq_create_auto(const u32 depth)
{
#if defined(LIBURING)
return coq_uring_create_pair(depth);
#else
(void)depth;
return coq_create();
#endif // LIBURING
}
void
coq_destroy_auto(struct coq * const coq)
{
#if defined(LIBURING)
coq_uring_destroy_pair(coq);
#else
coq_destroy(coq);
#endif // LIBURING
}
// return the position in the queue
u32
corq_enqueue(struct coq * const q, struct co * const co)
{
if (corq_full(q))
return UINT32_MAX;
const u32 i = coht_enqueue(&(q->rqht));
q->rq[i] = co;
return i;
}
// return the position in the queue
// the func should do clean up for the target coroutine
u32
cowq_enqueue(struct coq * const q, cowq_func func, void * const priv)
{
if (cowq_full(q))
return UINT32_MAX;
const u32 i = coht_enqueue(&(q->wqht));
q->wq[i].func = func;
q->wq[i].priv = priv;
return i;
}
static void
corq_process(struct coq * const q)
{
if (corq_empty(q))
return;
const u32 i = coht_dequeue(&(q->rqht));
struct co * const co = q->rq[i];
co_enter(co, 0);
if (!co_valid(co))
co_destroy(co);
}
static void
cowq_process(struct coq * const q)
{
do {
const u32 i = coht_dequeue(&q->wqht);
cowq_func func = q->wq[i].func;
if (func) { // skip empty entries
func(q->wq[i].priv);
return;
}
} while (cowq_nr(q));
}
void
cowq_remove(struct coq * const q, const u32 i)
{
q->wq[i].func = NULL;
}
// will not give control to wq workers
void
coq_yield(struct coq * const q)
{
corq_enqueue(q, co_self());
co_back(0);
}
static bool
coq_process_idle(void * const priv)
{
struct co * const co = (typeof(co))priv;
co_enter(co, 0);
if (!co_valid(co))
co_destroy(co);
return true;
}
// resume after all the wq workers have run
void
coq_idle(struct coq * const q)
{
cowq_enqueue(q, coq_process_idle, co_self());
co_back(0);
}
void
coq_run(struct coq * const q)
{
while (!(corq_empty(q) && cowq_empty(q))) {
// flush the run-queue
while (corq_nr(q))
corq_process(q);
// process work-completion as long as the run-queue is empty
while (cowq_nr(q) && corq_empty(q))
cowq_process(q);
}
}
static __thread struct coq * coq_curr = NULL;
inline void
coq_install(struct coq * const q)
{
if (coq_curr)
debug_die();
coq_curr = q;
}
inline void
coq_uninstall(void)
{
if (coq_curr == NULL)
debug_die();
coq_curr = NULL;
}
inline struct coq *
coq_current(void)
{
return coq_curr;
}
// }}} coq
// aio {{{
static bool
cowq_process_aio(void * const priv)
{
struct co * const co = (typeof(co))priv;
debug_assert(co);
co_enter(co, 0);
if (!co_valid(co))
co_destroy(co);
return true;
}
static ssize_t
coq_wait_aio(struct coq * const q, struct aiocb * const cb, struct co * const self)
{
do {
cowq_enqueue(q, cowq_process_aio, (void *)self);
co_back(0);
const int r = aio_error(cb);
if (r != EINPROGRESS)
return aio_return(cb);
} while (true);
}
ssize_t
coq_pread_aio(struct coq * const q, const int fd, void * const buf, const size_t count, const off_t offset)
{
struct co * const self = co_self();
if (!self)
return pread(fd, buf, count, offset);
struct aiocb cb = { .aio_fildes = fd, .aio_buf = buf, .aio_nbytes = count, .aio_offset = offset};
const int r = aio_read(&cb);
if (unlikely(r))
return -1;
return coq_wait_aio(q, &cb, self);
}
ssize_t
coq_pwrite_aio(struct coq * const q, const int fd, const void * const buf, const size_t count, const off_t offset)
{
struct co * const self = co_self();
if (!self)
return pwrite(fd, buf, count, offset);
struct aiocb cb = { .aio_fildes = fd, .aio_buf = (void *)buf, .aio_nbytes = count, .aio_offset = offset};
const int r = aio_write(&cb);
if (unlikely(r))
return -1;
return coq_wait_aio(q, &cb, self);
}
// }}} aio
// io_uring {{{
#if defined(LIBURING)
static inline bool
coq_uring_init(struct io_uring * const ring, const u32 depth)
{
struct io_uring_params p = {};
return 0 == io_uring_queue_init_params(depth, ring, &p);
}
struct io_uring *
coq_uring_create(const u32 depth)
{
struct io_uring * const ring = malloc(sizeof(*ring));
if (coq_uring_init(ring, depth)) {
return ring;
} else {
free(ring);
return NULL;
}
}
// returns a coq plus a uring at the end
struct coq *
coq_uring_create_pair(const u32 depth)
{
struct coq * const coq = calloc(1, sizeof(struct coq) + sizeof(struct io_uring));
if (coq_uring_init(coq->uring, depth)) {
return coq;
} else {
free(coq);
return NULL;
}
}
void
coq_uring_destroy(struct io_uring * const ring)
{
io_uring_queue_exit(ring);
free(ring);
}
void
coq_uring_destroy_pair(struct coq * const coq)
{
io_uring_queue_exit(coq->uring);
coq_destroy(coq);
}
static bool
cowq_process_uring(void * const priv)
{
struct io_uring * const ring = (typeof(ring))priv;
struct io_uring_cqe * cqe = NULL;
int ret = io_uring_wait_cqe(ring, &cqe);
if (ret)
return false;
struct co * const co = (typeof(co))io_uring_cqe_get_data(cqe);
debug_assert(co);
co_enter(co, (u64)cqe);
if (!co_valid(co))
co_destroy(co);
return true;
}
ssize_t
coq_pread_uring(struct coq * const q, struct io_uring * const ring0,
const int fd, void * const buf, const size_t count, const off_t offset)
{
struct co * const self = co_self();
if (!self)
return pread(fd, buf, count, offset);
struct io_uring * const ring = ring0 ? ring0 : q->uring;
struct io_uring_sqe * const sqe = io_uring_get_sqe(ring);
if (sqe == NULL)
return -1;
struct iovec vec = {.iov_base = buf, .iov_len = count};
io_uring_prep_readv(sqe, fd, &vec, 1, offset);
io_uring_sqe_set_data(sqe, self);
io_uring_submit(ring);
// prepare callback
cowq_enqueue(q, cowq_process_uring, (void *)ring);
// yield
struct io_uring_cqe * const cqe = (typeof(cqe))co_back(0);
debug_assert(cqe);
const ssize_t ret = cqe->res;
io_uring_cqe_seen(ring, cqe);
return ret;
}
ssize_t
coq_pwrite_uring(struct coq * const q, struct io_uring * const ring0,
const int fd, const void * const buf, const size_t count, const off_t offset)
{
struct co * const self = co_self();
if (!self)
return pwrite(fd, buf, count, offset);
struct io_uring * const ring = ring0 ? ring0 : q->uring;
struct io_uring_sqe * const sqe = io_uring_get_sqe(ring);
if (sqe == NULL)
return -1;
struct iovec vec = {.iov_base = (void *)buf, .iov_len = count};
io_uring_prep_writev(sqe, fd, &vec, 1, offset);
io_uring_sqe_set_data(sqe, self);
io_uring_submit(ring);
// prepare callback
cowq_enqueue(q, cowq_process_uring, (void *)ring);
// yield
struct io_uring_cqe * const cqe = (typeof(cqe))co_back(0);
debug_assert(cqe);
const ssize_t ret = cqe->res;
io_uring_cqe_seen(ring, cqe);
return ret;
}
#endif // LIBURING
// }}} io_uring
// }}} coq
// rcache {{{
// read-only cache
#define RCACHE_NWAY ((16u))
#define RCACHE_VSHIFT128 ((2))
#define RCACHE_VSHIFT256 ((3))
#define RCACHE_VSHIFT512 ((4))
#define RCACHE_VWAY128 ((RCACHE_NWAY >> RCACHE_VSHIFT128))
#define RCACHE_VWAY256 ((RCACHE_NWAY >> RCACHE_VSHIFT256))
#define RCACHE_VWAY512 ((RCACHE_NWAY >> RCACHE_VSHIFT512))
#define RCACHE_MASK ((RCACHE_NWAY - 1))
#define RCACHE_MAXHIST ((UINT8_MAX - 1))
struct rcache_group {
u8 hist[RCACHE_NWAY]; // 1x16=16B
spinlock lock; // 4B
au32 valid_bits; // 4B
au32 write_bits; // 4B
au32 dirty_bits; // 4B
au16 refcnt[RCACHE_NWAY]; // 2x16=32B
union {
u32 tag[RCACHE_NWAY]; // 4x16=64B: high x-bit is fd; low y-bit is page-number (256MB max)
m128 tagv128[RCACHE_VWAY128];
#if defined(__AVX2__)
m256 tagv256[RCACHE_VWAY256];
#endif
#if defined(__AVX512F__)
m512 tagv512[RCACHE_VWAY512];
#endif
};
};
static_assert((sizeof(struct rcache_group) % 64) == 0, "rcache_group size");
struct rcache {
u8 * mem;
struct rcache_group * groups;
u32 group_mask;
u32 nr_groups;
u32 fd_shift;
u32 pno_mask;
u64 memsize;
u64 gmemsize;
struct bitmap * close_bm;
};
struct rcache *
rcache_create(const u64 size_mb, const u32 fd_bits)
{
debug_assert(size_mb && fd_bits);
const u64 cachesz = bits_p2_up_u64(size_mb) << 20;
const u64 npages = cachesz / PGSZ;
const u64 ngroups = npages / (u64)RCACHE_NWAY;
if (ngroups > UINT32_MAX)
return NULL;
struct rcache * const c = calloc(1, sizeof(*c));
if (!c)
return NULL;
c->mem = pages_alloc_best(cachesz, true, &c->memsize); // can use 1GB huge page
if (!c->mem) {
free(c);
return NULL;
}
c->groups = pages_alloc_best(ngroups * sizeof(struct rcache_group), false, &c->gmemsize);
if (!c->groups) {
pages_unmap(c->mem, c->memsize);
free(c);
return NULL;
}
c->group_mask = (u32)ngroups - 1;
c->nr_groups = (u32)ngroups;
c->fd_shift = 32 - fd_bits;
c->pno_mask = (1u << c->fd_shift) - 1u;
c->close_bm = bitmap_create(1lu << fd_bits);
debug_assert(c->close_bm);
for (u64 i = 0; i < ngroups; i++)
spinlock_init(&(c->groups[i].lock));
return c;
}
void
rcache_destroy(struct rcache * const c)
{
free(c->close_bm);
pages_unmap(c->mem, c->memsize);
pages_unmap(c->groups, c->gmemsize);
free(c);
}
static inline void
rcache_read(int fd, void *pg, u32 pno)
{
struct coq * const coq = coq_current();
if (coq) {
#if defined(LIBURING)
if (coq_pread_uring(coq, NULL, fd, pg, PGSZ, PGSZ * pno) != PGSZ)
debug_die();
#else
if (coq_pread_aio(coq, fd, pg, PGSZ, PGSZ * pno) != PGSZ)
debug_die();
#endif // LIBURING
} else { // regular pread
if (pread(fd, pg, PGSZ, PGSZ * pno) != PGSZ)
debug_die();
}
}
static inline int
rcache_tag_to_fd(struct rcache * const c, const u32 tag)
{
return (int)(tag >> c->fd_shift);
}
static inline u32
rcache_tag(struct rcache * const c, const int fd, const u32 pno)
{
debug_assert(fd > 0); // please don't use stdin
debug_assert((u32)__builtin_clz((u32)fd) >= c->fd_shift);
debug_assert(pno <= c->pno_mask);
const u32 tag = (((u32)fd) << c->fd_shift) | pno;
return tag;
}
static inline u32
rcache_hash(const u32 tag)
{
return crc32c_u32(0x0D15EA5Eu, tag);
}
static inline u8 *
rcache_page(struct rcache * const c, const u32 gid, const u32 i)
{
return c->mem + (PGSZ * (gid * RCACHE_NWAY + i));
}
// thread-unsafe
void
rcache_close_lazy(struct rcache * const c, const int fd)
{
debug_assert(bitmap_test(c->close_bm, (u64)fd) == false);
bitmap_set1(c->close_bm, (u64)fd);
}
// thread-unsafe
u64
rcache_close_flush(struct rcache * const c)
{
struct bitmap * const bm = c->close_bm;
const u64 count = bitmap_count(bm);
if (count == 0)
return 0;
for (u32 i = 0; i < c->nr_groups; i++) {
struct rcache_group * const g = &(c->groups[i]);
spinlock_lock(&(g->lock));
for (u32 j = 0; j < RCACHE_NWAY; j++) {
const int fd = rcache_tag_to_fd(c, g->tag[j]);
if (bitmap_test(bm, (u64)fd)) {
g->tag[j] = 0;
g->hist[j] = 0;
debug_assert(g->refcnt[j] == 0);
}
}
spinlock_unlock(&(g->lock));
}
while (bitmap_count(bm)) {
const u64 bit = bitmap_first(bm);
close((int)bit);
bitmap_set0(bm, bit);
}
return count;
}
// invalidate cache and close(fd)
void
rcache_close(struct rcache * const c, const int fd)
{
for (u32 i = 0; i < c->nr_groups; i++) {
struct rcache_group * const g = &(c->groups[i]);
spinlock_lock(&(g->lock));
for (u32 j = 0; j < RCACHE_NWAY; j++) {
if (rcache_tag_to_fd(c, g->tag[j]) == fd) {
g->tag[j] = 0;
g->hist[j] = 0;
debug_assert(g->refcnt[j] == 0);
}
}
spinlock_unlock(&(g->lock));
}
close(fd);
}
static inline void
rcache_pause(void)
{
struct coq * const coq = coq_current();
if (coq)
coq_idle(coq);
else
cpu_pause();
}
// lock has been acquired
// read-only; return a page that has zero reference
static u32
rcache_search_victim(struct rcache_group * const g, const u32 i0)
{
// search unused page
#pragma nounroll
do {
u32 imin = i0;
u16 cmin = UINT16_MAX;
u8 hmin = UINT8_MAX;
#pragma nounroll
for (u32 k = 0; k < RCACHE_NWAY; k++) {
const u32 i = (k + i0) & RCACHE_MASK;
if (g->hist[i] < hmin && atomic_load_explicit(&(g->refcnt[i]), MO_CONSUME) == 0) {
// refcnt is 0 but we may still have a better choice
imin = i;
cmin = 0;
hmin = g->hist[i];
}
}
if (cmin == 0) // found a victim
return imin;
rcache_pause();