-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblkiotest.c
More file actions
1785 lines (1599 loc) · 45.2 KB
/
blkiotest.c
File metadata and controls
1785 lines (1599 loc) · 45.2 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
/*
* Copyright (c) 2016--2021 Wu, Xingbo <wuxb45@gmail.com>
*
* All rights reserved. No warranty, explicit or implicit, provided.
*/
#define _GNU_SOURCE
#include "lib.h"
#include "blkio.h"
#include "ctypes.h"
#include <fcntl.h>
#include <sys/ioctl.h>
#if defined(__linux__)
#include <linux/fs.h>
#endif
#if defined(SPDK)
#include <spdk/stdinc.h>
#include <spdk/nvme.h>
#include <spdk/env.h>
#include <spdk/log.h>
#include <rte_log.h>
#endif // SPDK
#ifndef O_DIRECT
#define O_DIRECT 0
#endif
// rcache {{{
#define RC_BATCH ((1000lu))
static au64 rcache_nr_op = 0;
static void *
rcache_worker(void * ptr)
{
srandom_u64(time_nsec());
u64 nr_op = 0;
struct rcache * const c = (typeof(c))ptr;
u64 * ptrs[16] = {};
char fn[64];
for (u64 e = 0; e < RC_BATCH; e++) {
const u64 fid = random_u64() & 0x1fflu;
sprintf(fn, "/tmp/rc-%lu.txt", fid);
int fd = open(fn, O_RDONLY);
if (fd < 0)
debug_die();
for (u64 i = 0; i < RC_BATCH; i++) {
const u64 x = random_u64() & 0xflu;
if (ptrs[x]) {
//usleep(1);
if ((i & 0xflu) == 0) {
rcache_retain(c, (const u8 *)(ptrs[x]));
rcache_release(c, (const u8 *)(ptrs[x]));
}
rcache_release(c, (const u8 *)(ptrs[x]));
ptrs[x] = NULL;
} else {
const u32 r = (u32)(random_u64() & 0x1fflu);
ptrs[x] = rcache_acquire(c, fd, r);
if (ptrs[x][0] != fid || ptrs[x][1] != r) {
debug_die();
}
nr_op++;
}
}
for (u64 i = 0; i < 16; i++) {
if (ptrs[i]) {
rcache_release(c, (const u8 *)(ptrs[i]));
ptrs[i] = NULL;
}
}
rcache_close(c, fd);
}
rcache_nr_op += nr_op;
return NULL;
}
static void
test_rcache(void)
{
char * const mem = calloc(1, PGSZ);
// 4k x 512 x 512 (1GB total)
for (u64 i = 0; i < 512; i++) {
sprintf(mem, "/tmp/rc-%lu.txt", i);
int fd = open(mem, O_WRONLY|O_CREAT, 00644);
for (u64 j = 0; j < 512; j++) {
*(u64*)mem = i;
*(u64*)(mem+8) = j;
write(fd, mem, PGSZ);
}
close(fd);
}
struct rcache * const c = rcache_create(4, 4);
debug_perf_switch();
thread_fork_join(4, rcache_worker, false, c);
for (u32 t = 1; t < 8; t++) {
rcache_nr_op = 0;
const u64 dt = thread_fork_join(t, rcache_worker, false, c);
const double ops = (double)(rcache_nr_op) / ((double)dt) * 1e9;
printf("nr_th %u dt-ms %lu; %.3lf op/s\n", t, dt / (u64)1e6, ops);
debug_perf_switch();
}
rcache_destroy(c);
for (u64 i = 0; i < 512; i++) { // clean up
sprintf(mem, "/tmp/rc-%lu.txt", i);
unlink(mem);
}
free(mem);
}
// }}} rcache
// rwcache {{{
#define RC2_NF ((512))
#define RC2_NP ((512))
#define RC2_NWORKER ((4))
static void *
rwcache_worker(void * ptr)
{
srandom_u64(time_nsec());
struct rwcache * const c = (typeof(c))ptr;
u64 * ptrs[16] = {};
char fn[64];
u64 nr_op = 0;
for (u64 e = 0; e < RC_BATCH; e++) {
const u64 fid = random_u64() % RC2_NF;
sprintf(fn, "/tmp/rc-%lu.txt", fid);
int fd = open(fn, O_RDONLY);
if (fd < 0)
debug_die();
for (u64 i = 0; i < RC_BATCH; i++) {
const u64 x = random_u64() % 16;
if (ptrs[x]) {
//usleep(1);
rwcache_release(c, ptrs[x]);
ptrs[x] = NULL;
} else {
const u32 r = (u32)(random_u64() % RC2_NP);
ptrs[x] = rwcache_acquire(c, fd, r);
if (ptrs[x][0] != fid || ptrs[x][1] != r) {
debug_die();
}
nr_op++;
}
}
for (u64 i = 0; i < 16; i++) {
if (ptrs[i]) {
rwcache_release(c, ptrs[i]);
ptrs[i] = NULL;
}
}
rwcache_close(c, fd);
}
rcache_nr_op += nr_op;
return NULL;
}
static au64 rwcache_seq = 0;
static void *
rwcache_worker2(void * ptr)
{
struct rwcache * const c = (typeof(c))ptr;
char fn[64];
const u64 seq = atomic_fetch_add(&rwcache_seq, 1);
for (u64 e = 0; e < 1000; e++) {
if (!seq) {
fprintf(stderr, "%lu\n", e);
}
for (u64 i = seq; i < RC2_NF; i += RC2_NWORKER) {
sprintf(fn, "/tmp/rc-%lu.txt", i);
int fd = open(fn, O_RDWR); // RW
if (fd < 0)
debug_die();
for (u32 k = 0; k < RC2_NP; k++) {
u64 * const mem = rwcache_acquire(c, fd, k);
if (mem[0] != i || mem[1] != k || mem[2] != e) {
printf("e %lu i %lu k %u -- in file %lu %lu %lu\n", e, i, k, mem[2], mem[0], mem[1]);
debug_die();
}
mem[2] = e + 1;
rwcache_dirty(c, mem);
if (random_u64() & 0x800)
rwcache_sync(c, mem);
rwcache_release(c, mem);
}
rwcache_close(c, fd);
}
}
return NULL;
}
static void
test_rwcache(void)
{
char * const mem = calloc(1, PGSZ);
// 4k x 512 x 512 (1GB total)
for (u64 i = 0; i < RC2_NF; i++) {
sprintf(mem, "/tmp/rc-%lu.txt", i);
int fd = open(mem, O_WRONLY|O_CREAT, 00644);
for (u64 j = 0; j < RC2_NP; j++) {
((u64*)mem)[0] = i;
((u64*)mem)[1] = j;
write(fd, mem, PGSZ);
}
close(fd);
}
free(mem);
struct rwcache * const c = rwcache_create(1, 12);
thread_fork_join(4, rwcache_worker, false, c);
for (u32 t = 1; t < 8; t++) {
rcache_nr_op = 0;
const u64 dt = thread_fork_join(t, rwcache_worker, false, c);
const double ops = (double)(rcache_nr_op) / ((double)dt) * 1000000000lu;
printf("nr_th %u dt %lu; %.3lf op/s\n", t, dt / (u64)1e6, ops);
}
// update
const u64 dtw = thread_fork_join(RC2_NWORKER, rwcache_worker2, false, c);
printf("update %lu\n", dtw);
rwcache_destroy(c);
}
// }}} rwcache
#if defined(LIBURING)
// coqrw {{{
struct coqrw_worker_info {
u64 bufsize;
u64 rand_mask;
u64 end_time;
u64 cpt;
int fd;
au64 seq; // reader if atomic_fetch_add(1) < nr_r
u64 nr_r;
au64 io_r; // stat, output
au64 io_w; // stat, output
};
struct coqrw_co_info {
struct coqrw_worker_info * wi;
struct coq * coq;
struct io_uring * ring;
};
static void
coqrw_co_worker(void)
{
struct coqrw_co_info * const ci = (typeof(ci))co_priv();
struct coqrw_worker_info * const wi = ci->wi;
printf("%s %p\n", __func__, ci);
const bool is_reader = atomic_fetch_add(&(wi->seq), 1) < wi->nr_r;
const int fd = wi->fd;
u8 * const buf = xalloc(512, wi->bufsize);
u64 succ = 0;
u64 err = 0;
do {
for (u64 i = 0; i < 100; i++) {
const u64 blknum = random_u64() & wi->rand_mask;
const u64 offset = blknum * wi->bufsize;
const ssize_t ret = is_reader ?
coq_pread_uring(ci->coq, ci->ring, fd, buf, wi->bufsize, (off_t)offset):
coq_pwrite_uring(ci->coq, ci->ring, fd, buf, wi->bufsize, (off_t)offset);
if (ret < 0)
err++;
else
succ++;
}
} while (time_nsec() < wi->end_time);
printf("%s succ %lu err %lu\n", __func__, succ, err);
atomic_fetch_add(is_reader ? (&(wi->io_r)) : (&(wi->io_w)), succ);
}
static void *
coqrw_th_worker(void * const ptr)
{
struct coqrw_worker_info * const wi = (typeof(wi))ptr;
srandom_u64(time_nsec());
struct coqrw_co_info ci;
ci.coq = coq_create();
ci.ring = coq_uring_create(64);
ci.wi = wi;
u64 hostrsp = 0;
for (u64 i = 0; i < wi->cpt; i++) {
struct co * const co = co_create(8192*8, coqrw_co_worker, &ci, &hostrsp);
corq_enqueue(ci.coq, co);
}
coq_run(ci.coq);
coq_uring_destroy(ci.ring);
coq_destroy(ci.coq);
return NULL;
}
static int
test_coqrw(int argc, char ** argv)
{
if (argc < 8) {
fprintf(stderr, "usage: %s <dev> <nr_r> <nr_w> <time> <buflen_power> <range_power> <co-per-th>\n", argv[0]);
fprintf(stderr, "example: %s /dev/sdb 4 0 10 12 5 10 4\n", argv[0]);
exit(1);
}
//const int fd = open(argv[1], O_RDWR | O_DIRECT);
const int fd = open(argv[1], O_RDWR);
if (fd < 0) {
fprintf(stderr, "open %s failed\n", argv[1]);
exit(1);
}
const u32 nr_r = a2u32(argv[2]);
const u32 nr_w = a2u32(argv[3]);
const u64 sec = a2u64(argv[4]);
const u64 p0 = a2u64(argv[5]);
const u64 p1 = (p0 < 12) ? 12 : p0;
const u64 p2 = (p1 > 25) ? 25 : p1;
const u64 bufsize = 1lu << p2;
const u64 r0 = a2u64(argv[6]);
const u64 r1 = (r0 < p2) ? p2 : r0;
const u64 mask = (1lu << (r1 - p2)) - 1;
const u64 cpt = a2u64(argv[7]);
printf("buflen power %lu range power %lu cpt %lu\n", p2, r1, cpt);
// warning: buf is currently shared between all readers/writers
struct coqrw_worker_info wi = {
.bufsize = bufsize,
.rand_mask = mask,
.end_time = time_nsec() + (sec * 1000000000lu),
.cpt = cpt,
.fd = fd,
.seq = 0,
.nr_r = nr_r * cpt,
.io_r = 0,
.io_w = 0
};
const u64 dt = thread_fork_join(nr_r+nr_w, coqrw_th_worker, false, &wi);
const double miops = (double)(wi.io_r + wi.io_w) * 1e3 / ((double)dt);
printf("million IOPS %.3lf\n", miops);
return 0;
}
// }}} coqrw
// rawcoring {{{
// use once to open a file for testing
static int
prep_big_file(const char * const filename, const bool dio, const u64 size)
{
printf("Preparing %s\n", filename);
struct stat st;
int ret = stat(filename, &st);
bool is_blk = (ret == 0) && S_ISBLK(st.st_mode);
if (is_blk) {
// blk device
int fd = open(filename, O_RDWR|(dio?O_DIRECT:0));
if (fd < 0) {
printf("open blk file failed\n");
return -1;
}
u64 devsize = 0;
ioctl(fd, BLKGETSIZE64, &devsize);
if (devsize < size) {
printf("device size < access range\n");
close(fd);
return -1;
}
return fd;
} else {
// file
int fd = open(filename, O_CREAT|O_RDWR, 00666);
if (fd < 0) {
printf("open regular file failed\n");
return -1;
}
if ((u64)st.st_size >= size) {
return fd;
} else {
printf("fallocate %ld -> %lu\n", st.st_size, size);
ret = fallocate(fd, 0, st.st_size, (off_t)size-st.st_size);
if (ret != 0) {
printf("fallocate err: %d %s\n", ret, strerror(-errno));
close(fd);
return -1;
}
}
if (dio) { // reopen for Direct IO
close(fd);
fd = open(filename, O_RDWR|O_DIRECT);
}
return fd;
}
}
// private metadata for a worker coroutine
struct rawcoring_worker_info {
struct io_uring * ring;
struct co * co;
struct iovec vec; // registered iovec
u32 iosize;
int vecidx; // index of the buffer
u64 nrblk;
u64 depth;
u64 * pseq;
u64 nr_err; // out
u64 nr_op; // out
};
// the worker thread performs one query task and return
// a query task need a few I/O operations
static void
rawcoring_worker(void)
{
struct rawcoring_worker_info * const wi = co_priv();
for (u64 i = 0; i < 64; i++) {
struct io_uring_sqe * const sqe = io_uring_get_sqe(wi->ring);
debug_assert(sqe); // may fail if queue size was initialized to be very small
const u64 r = random_u64() % wi->nrblk;
// fixed requires the vec index
io_uring_prep_read_fixed(sqe, 0, wi->vec.iov_base, wi->iosize, (off_t)(r*wi->iosize), wi->vecidx);
io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE); // the 0 above means the index
io_uring_sqe_set_data(sqe, wi);
// submit the request to uring
(*wi->pseq)++;
if ((*wi->pseq) == wi->depth) {
const int nsub = io_uring_submit(wi->ring); // it enters kernel on demand
// the test programs in liburing usually don't care...
if ((u64)nsub != wi->depth)
debug_die();
*wi->pseq = 0;
}
// wait for completion--returns to the host routine now
// when the host routine received a cqe for the request, this coroutine will resume
// the return value from co_back is the cqe to be consumed
struct io_uring_cqe * const cqe = (typeof(cqe))co_back(0);
if (cqe->res <= 0) {
printf("cqe %p data %p res %d flags %u\n", cqe, (void *)cqe->user_data, cqe->res, cqe->flags);
exit(-1);
}
// consume the data in iovec ...
// do nothing
io_uring_cqe_seen(wi->ring, cqe);
wi->nr_op++;
}
}
static int
test_rawcoring(int argc, char ** argv)
{
if (argc < 8) {
printf("Usage: <file> <iosize (bytes)> <nrblk> <depth> <runtime> <sqpoll (0/1)> <dio (0/1)>\n");
printf("<file> will be sized to <iosize> x <nrblk>\n");
printf("kcpu will be used only in sqpoll mode; need SYSADMIN cap to run\n");
exit(0);
}
const char * const filename = argv[1];
const u64 iosize = a2u64(argv[2]);
const u64 nrblk = a2u64(argv[3]);
const u32 depth = a2u32(argv[4]);
const u64 runtime = a2u64(argv[5]);
const bool sqpoll = a2u64(argv[6]) ? true : false;
const bool dio = a2u64(argv[7]) ? true : false;
const int fdrw = prep_big_file(filename, dio, iosize * nrblk);
if (fdrw < 0) {
printf("prep-file failed\n");
exit(-1);
}
struct io_uring_params p = {};
if (sqpoll) {
p.flags |= IORING_SETUP_SQPOLL | IORING_SETUP_SQ_AFF;
p.sq_thread_cpu = 1; // hard-coded
}
struct io_uring ring = {};
// at least doubled sqes
const u32 qdepth = (u32)bits_p2_up_u64((depth < 4 ? 4 : depth) * 2);
int ret = io_uring_queue_init_params(qdepth, &ring, &p);
if (ret) {
printf("mmap err %d\n", ret);
exit(-1);
}
// register the file
ret = io_uring_register_files(&ring, &fdrw, 1);
if (ret) {
printf("reg file err %d\n", ret);
exit(-1);
}
// iovecs & registering
struct iovec * vecs = malloc(sizeof(*vecs) * depth);
for (u64 i = 0; i < depth; i++) {
vecs[i].iov_base = xalloc(512, iosize);
vecs[i].iov_len = iosize;
}
ret = io_uring_register_buffers(&ring, vecs, depth);
if (ret) {
printf("reg vecs err %d\n", ret);
exit(-1);
}
// parameters are ready
// start the first batch of requests
struct rawcoring_worker_info * const wis = calloc(depth, sizeof(wis[0]));
u64 seq = 0;
u64 hostrsp = 0;
const u64 t0 = time_nsec();
const u64 endtime = t0 + (runtime * 1000000000lu);
// init wi and fill the queue
for (u32 i = 0; i < depth; i++) {
struct rawcoring_worker_info * const wi = &(wis[i]);
wi->ring = ˚
wi->vec = vecs[i];
wi->vecidx = (int)i;
wi->iosize = (u32)iosize;
wi->nrblk = nrblk;
wi->depth = depth;
wi->pseq = &seq;
wi->co = co_create(8192, rawcoring_worker, wi, &hostrsp);
(void)co_enter(wi->co, 0); // submit job and return
}
// keep the queue full until time out
do {
for (u64 i = 0; i < 100; i++) {
struct io_uring_cqe * cqe = NULL;
ret = io_uring_wait_cqe(&ring, &cqe);
// wait error
if (ret) {
printf("wait_cqe err %d\n", ret);
exit(-1);
}
// obtain the worker
struct rawcoring_worker_info * const wi = (typeof(wi))io_uring_cqe_get_data(cqe);
co_enter(wi->co, (u64)cqe);
while (!co_valid(wi->co)) {
// submit a new task
co_reuse(wi->co, rawcoring_worker, wi, &hostrsp);
co_enter(wi->co, 0);
}
}
} while (time_nsec() < endtime);
// about to finish. wait for completion of all flying queries
u64 todo = depth;
while (todo) {
struct io_uring_cqe * cqe = NULL;
ret = io_uring_wait_cqe(&ring, &cqe);
// wait error
if (ret) {
printf("wait_cqe err %d\n", ret);
exit(-1);
}
// obtain the worker
struct rawcoring_worker_info * const wi = (typeof(wi))io_uring_cqe_get_data(cqe);
co_enter(wi->co, (u64)cqe);
if (!co_valid(wi->co))
todo--;
// else: the query is still running
}
const u64 dt = time_diff_nsec(t0);
u64 nr_op = 0;
u64 nr_err = 0;
for (u64 i = 0; i < depth; i++) {
printf("%lu\n", wis[i].nr_op);
nr_op += wis[i].nr_op;
nr_err += wis[i].nr_err;
}
const double iops = ((double)nr_op) * 1000000000lu / ((double)dt);
printf("nops %lu iops %.0lf tpt %.3lf MB/s err %lu\n", nr_op, iops, (iops * (double)iosize) / 1048576, nr_err);
// TODO: clean up
exit(0);
}
// }}} rawcoring
// ring {{{
struct ring_worker_info {
int fd;
bool iopoll;
bool sqpoll;
u64 nrblk;
u32 iosize;
u32 depth;
u32 ucpu;
u32 kcpu;
u64 endtime;
u64 nr_op;
};
static u64
ring_wait_one(struct io_uring * const ring)
{
struct io_uring_cqe *cqe = NULL;
int ret = io_uring_wait_cqe(ring, &cqe);
if (ret) {
printf("wait_cqe err %d\n", ret);
exit(-1);
}
debug_assert(cqe); // high user address
if (cqe->res <= 0) {
printf("cqe %p data %lu res %d flags %u\n", cqe, (u64)cqe->user_data, cqe->res, cqe->flags);
exit(-1);
}
const u64 id = (u64) io_uring_cqe_get_data(cqe);
io_uring_cqe_seen(ring, cqe);
return id;
}
static void *
pread_worker(void * const ptr)
{
srandom_u64(time_nsec());
struct ring_worker_info * const wi = (typeof(wi))ptr;
thread_pin(wi->ucpu);
void * const buf = xalloc(512, wi->iosize);
u64 complete = 0;
u64 nerr = 0;
// keep the queue full
do {
for (u64 i = 0; i < 1000; i++) {
const u64 r = random_u64() % wi->nrblk;
if (wi->iosize != (u32)pread(wi->fd, buf, wi->iosize, (off_t)(r*wi->iosize)))
nerr++;
complete++;
}
} while (time_nsec() < wi->endtime);
if (nerr)
printf("nsub #err %lu\n", nerr);
wi->nr_op = complete;
return NULL;
}
static void *
ring_worker(void * const ptr)
{
srandom_u64(time_nsec());
struct ring_worker_info * const wi = (typeof(wi))ptr;
thread_pin(wi->ucpu);
struct iovec * vecs = malloc(sizeof(*vecs) * wi->depth);
for (u32 i = 0; i < wi->depth; i++) {
vecs[i].iov_base = xalloc(512, wi->iosize);
vecs[i].iov_len = wi->iosize;
}
struct io_uring_params p = {};
// sqpoll needs CAP_SYS_ADMIN or root user
if (wi->sqpoll) {
p.flags |= IORING_SETUP_SQPOLL | IORING_SETUP_SQ_AFF;
p.sq_thread_cpu = wi->kcpu;
}
//p.sq_thread_idle = 0; // kernel defaults to 1s
struct io_uring ring = {};
int ret = io_uring_queue_init_params(wi->depth*2, &ring, &p);
if (ret) {
printf("mmap err %d\n", ret);
exit(-1);
}
ret = io_uring_register_files(&ring, &wi->fd, 1);
if (ret) {
printf("reg fd err %d\n", ret);
exit(-1);
}
ret = io_uring_register_buffers(&ring, vecs, wi->depth);
if (ret) {
printf("reg vecs err %d\n", ret);
exit(-1);
}
//printf("sqes %p cqes %p\n", ring.sq.sqes, ring.cq.cqes);
// fill the queue
for (u32 i = 0; i < wi->depth; i++) {
struct io_uring_sqe * sqe = io_uring_get_sqe(&ring);
debug_assert(sqe);
const u64 r = random_u64() % wi->nrblk;
io_uring_prep_read_fixed(sqe, 0, vecs[i].iov_base, wi->iosize, (off_t)(r*wi->iosize), (int)i);
io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE); // fd above replaced with the index
io_uring_sqe_set_data(sqe, (void *)(u64)i);
}
const u32 nsub = (u32)io_uring_submit(&ring); // it enters kernel on demand
if (nsub != wi->depth) {
printf("first submit %u/%u\n", nsub, wi->depth);
exit(-1);
}
u64 complete = 0;
u64 nerr = 0;
// keep the queue full
do {
for (u64 i = 0; i < 1000; i++) {
const u64 id = ring_wait_one(&ring);
complete++;
struct io_uring_sqe * const sqe = io_uring_get_sqe(&ring);
debug_assert(sqe);
const u64 r = random_u64() % wi->nrblk;
io_uring_prep_read_fixed(sqe, 0, vecs[id].iov_base, wi->iosize, (off_t)(r*wi->iosize), (int)id);
io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE);
io_uring_sqe_set_data(sqe, (void *)id);
const int nsub1 = io_uring_submit(&ring);
(void)nsub1;
// SQPOLL: occasionally has nsub1 > 1, usually many 2s and a few 3s
if (nsub1 != 1)
nerr++;
}
} while (time_nsec() < wi->endtime);
// empty the queue
for (u32 i = 0; i < wi->depth; i++) {
const u64 id = ring_wait_one(&ring);
(void)id;
complete++;
}
if (nerr)
printf("nsub #err %lu\n", nerr);
ret = io_uring_unregister_files(&ring);
if (ret) {
printf("unreg fd err %d\n", ret);
exit(-1);
}
ret = io_uring_unregister_buffers(&ring);
if (ret) {
printf("unreg vecs err %d\n", ret);
exit(-1);
}
io_uring_queue_exit(&ring);
wi->nr_op = complete;
return NULL;
}
static int
test_ring(int argc, char ** argv)
{
if (argc < 9) {
printf("Read-only I/O tests with io_uring. Can use a dev or a file path\n");
printf("Usage: <path> <iosize (bytes)> <nrblk> <depth> <runtime> <sqpoll (0/1)> <dio (0/1)> {<ucpu> <kcpu>} ...\n");
printf("<file> will be sized to <iosize> x <nrblk>\n");
printf("kcpu will be used only in sqpoll mode; sqpoll needs root or CAP_SYS_ADMIN\n");
exit(0);
}
const char * const filename = argv[1];
const u32 iosize = a2u32(argv[2]);
const u64 nrblk = a2u64(argv[3]);
const u32 depth = a2u32(argv[4]);
const u64 runtime = a2u64(argv[5]);
const bool sqpoll = a2u64(argv[6]) ? true : false;
const bool dio = a2u64(argv[7]) ? true : false;
const u32 nrth = (u32)((argc - 8)/2);
if (nrth == 0 || nrth >= 64) {
printf("need 1 to 64 <ucpu> <kcpu> pairs\n");
exit(-1);
}
const int fdrw = prep_big_file(filename, dio, iosize * nrblk);
if (fdrw < 0) {
printf("prep-file failed\n");
exit(-1);
}
struct ring_worker_info wis[64];
struct ring_worker_info * pwis[64];
const u64 endtime = runtime * 1000000000lu + time_nsec();
for (u32 i = 0; i < nrth; i++) {
wis[i].fd = dup(fdrw);
wis[i].sqpoll = sqpoll;
wis[i].iosize = iosize;
wis[i].nrblk = nrblk;
wis[i].depth = depth;
wis[i].ucpu = a2u32(argv[8+i*2]);
wis[i].kcpu = a2u32(argv[9+i*2]);
wis[i].endtime = endtime;
wis[i].nr_op = 0;
pwis[i] = &wis[i];
}
const u64 dt = thread_fork_join(nrth, depth ? ring_worker : pread_worker, true, pwis);
u64 ops = 0;
for (u32 i = 0; i < nrth; i++) {
ops += wis[i].nr_op;
close(wis[i].fd);
}
const double iops = ((double)ops) * 1000000000lu / ((double)dt);
printf("iops %.0lf tpt %.3lf MB/s\n", iops, (iops * (double)iosize) / 1048576);
exit(0);
}
// }}} ring
// coqread {{{
struct coqread_worker_info {
struct coq * coq;
struct io_uring * ring;
int fd;
u64 mask;
};
static void
coq_worker(void)
{
struct coqread_worker_info * const wi = (typeof(wi))co_priv();
const u64 x = 100000 + (random_u64() & 0xff);
const double t0 = time_sec();
u8 * const buf = xalloc(PGSZ, PGSZ);
for (u64 i = 0; i < x; i++) {
const u64 r = random_u64() & wi->mask;
const ssize_t n = coq_pread_uring(wi->coq, wi->ring, wi->fd, buf, PGSZ, (off_t)(r * PGSZ));
if (n != PGSZ) {
printf("%ld != PGSZ\n", (s64)n);
exit(0);
}
}
const double dt = time_diff_sec(t0);
printf("done %p nop %lu iops %.3lf\n", co_self(), x, (double)x / dt);
}
static void
test_coqread(int argc, char ** argv)
{
srandom_u64(time_nsec());
if (argc < 5) {
printf("Usage: <existig-file/dev> <dio(0|1)> <nr-coroutines> <power>\n");
exit(0);
}
char * const path = argv[1];
const bool dio = argv[2][0] == '1';
const u64 nt = a2u64(argv[3]);
const u64 power = a2u64(argv[4]);
const int fd = open(path, O_RDONLY | (dio ? O_DIRECT : 0), 00666);
if (fd < 0) {
printf("open failed\n");
exit(0);
}
// create separate coq and ring
//struct coq * const coq = coq_create();
//struct io_uring * const ring = coq_uring_create(64);
//struct coqread_worker_info wi = {.coq = coq, .ring = ring, .fd = fd, .mask = (1lu << power) - 1, };
// create bundled coq+uring
struct coq * const coq = coq_uring_create_pair(64);
struct coqread_worker_info wi = {.coq = coq, .ring = NULL, .fd = fd, .mask = (1lu << power) - 1, };
u64 hostrsp = 0;
for (u64 i = 0; i < nt; i++) {
struct co * const co = co_create(PGSZ * 7, coq_worker, &wi, &hostrsp);
corq_enqueue(coq, co);
}
coq_run(coq);
close(fd);
}
// }}} coqread
#endif // LIBURING
// wring {{{
static int
test_wring(int argc, char ** argv)
{
if (argc < 3) {
fprintf(stderr, "usage: <path> <direct?>\n");
return 0;
}
int flags = O_CREAT| O_RDWR;
if (argv[2][0] == '1')
flags |= O_DIRECT;
int fd = open(argv[1], flags, 00644);
debug_assert(fd >= 0);
#define WRING_DP ((64))
char zeroes[PGSZ] = {};
for (u32 i = 0; i < WRING_DP; i++)
write(fd, zeroes, PGSZ);
struct wring * const wring = wring_create(fd, PGSZ, WRING_DP);
wring_fsync(wring);
for (u64 i = 0; i < WRING_DP; i++) {
u8 * const buf = wring_acquire(wring);
buf[0] = (u8)i;
wring_write(wring, (off_t)(i*PGSZ), buf);
}
wring_fsync(wring);
wring_flush(wring);
u8 * buffer = xalloc(PGSZ, PGSZ);
for (u64 i = 0; i < WRING_DP; i++) {
pread(fd, buffer, 1, (off_t)(i*PGSZ));
if (buffer[0] != i)
printf("err!\n");
}
#define WRING_NR ((8192))
for (u32 i = 0; i < WRING_DP; i++)
write(fd, zeroes, PGSZ);
wring_fsync(wring);
for (u64 e = 0; e < 3; e++) {
u64 t0, dt;
t0 = time_nsec();
for (u64 i = 0; i < WRING_NR; i++) {
u8 * const buf = wring_acquire(wring);
buf[0]=(u8)i;
wring_write(wring, (off_t)(i*PGSZ), buf);
//usleep(1);
}
dt = time_diff_nsec(t0);
wring_fsync(wring);
wring_flush(wring);
printf("ns/op %lu\n", dt/WRING_NR);
t0 = time_nsec();
for (u64 i = 0; i < WRING_NR; i++) {
buffer[0]=(u8)i;
pwrite(fd, buffer, PGSZ, (off_t)(i*PGSZ));
//usleep(1);
}
dt = time_diff_nsec(t0);
wring_fsync(wring);
printf("ns/op %lu\n", dt/WRING_NR);
}
free(buffer);
wring_flush(wring);
close(fd);
wring_destroy(wring);
return 0;
}
// }}} wring
#if defined(SPDK)
// nvme {{{
static void
coq_process_nvme_cb(void * const priv, const struct spdk_nvme_cpl * const cpl)
{
struct co * const co = (typeof(co))priv;
debug_assert(co);
const u64 res = spdk_nvme_cpl_is_error(cpl) ? UINT64_MAX : 0;
co_enter(co, res); // 0 means ok
if (!co_valid(co))
co_destroy(co);
}
// may process mutliple completions
static bool
cowq_process_nvme(void * const priv)
{
struct spdk_nvme_qpair * const qp = (typeof(qp))priv;
do {
const int r = spdk_nvme_qpair_process_completions(qp, 0); // 0 or 1 cpl
if (r > 0)
return true;
else if (r < 0)
return false;
// got nothing; retry
cpu_pause();
} while (true);
}
bool
coq_pread_nvme(struct coq * const q, struct spdk_nvme_qpair * const qp,
struct spdk_nvme_ns * const ns, void * const buf, const u64 lba, const u32 nr)
{
const int rc = spdk_nvme_ns_cmd_read(ns, qp, buf, lba, nr, coq_process_nvme_cb, co_self(), 0);
if (rc)
return false;
// callback
const u32 i = cowq_enqueue(q, cowq_process_nvme, (void *)qp);
const u64 res = co_back(0);
cowq_remove(q, i);
return res == 0;
}
bool
coq_pwrite_nvme(struct coq * const q, struct spdk_nvme_qpair * const qp,