-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibtest.c
More file actions
3106 lines (2827 loc) · 78.8 KB
/
libtest.c
File metadata and controls
3106 lines (2827 loc) · 78.8 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 "kv.h"
#include "wh.h"
#include "ht.h"
#include "ord.h"
#include "ctypes.h"
#include <sched.h>
#include <execinfo.h>
#include <signal.h>
#if defined(__FreeBSD__)
#include <pthread_np.h>
#endif
// misc {{{
static void *
printer_th(void * ptr)
{
(void)ptr;
printf("thread %lx\n", (u64)pthread_self());
return NULL;
}
static void *
burning_th(void * ptr)
{
(void)ptr;
here:
goto here;
return NULL;
}
enum dummy {DUMMY};
static void
test_misc(void)
{
printf("sizeof long: %zu\n", sizeof(unsigned long));
printf("sizeof long long: %zu\n", sizeof(unsigned long long));
printf("sizeof size_t: %zu\n", sizeof(size_t));
printf("sizeof pthread_t: %zu\n", sizeof(pthread_t));
printf("sizeof pt_mutex: %zu\n", sizeof(pthread_mutex_t));
printf("sizeof pt_cond: %zu\n", sizeof(pthread_cond_t));
printf("sizeof pt_rwlock: %zu\n", sizeof(pthread_rwlock_t));
printf("sizeof enum: %zu\n", sizeof(enum dummy));
logger_printf(1, "%s %s %d %u\n", __func__, __FILE__, 100, 200u);
logger_printf(2, "%s %s %d %u\n", __func__, __FILE__, 100, 200u);
//printf("sizeof pt_spin: %zu\n", sizeof(pthread_spinlock_t));
u64 stackvar;
printf("symbol %s %p\n", __func__, test_misc);
printf("stackvar %p\n", &stackvar);
u64 t0 = time_nsec();
const double d0 = time_sec();
watch_u64_usr1(&t0);
u32 r32 = bits_reverse_u32(0x12345678u);
printf("reverse u32 0x12345678 -> %x\n", r32);
u64 r64 = bits_reverse_u64(0x123456789abcdef0lu);
printf("reverse u64 0x123456789abcdef0 -> %lx\n", r64);
printf("rss %ldkB\n", process_get_rss());
const u32 cc = process_affinity_count();
printf("affinity_core_count %u\n", cc);
u32 cores[64] = {};
const u64 nc = process_getaffinity_list(64, cores);
printf("affinity cores: ");
for (u64 i = 0; i < nc; i++) {
printf(" %u", cores[i]);
}
printf("\n");
printf("cpu time %lu\n", process_cpu_time_usec());
thread_fork_join(cc, printer_th, false, NULL);
char buf[32] = {};
strhex_32(buf, 0x1234abcdu);
printf("strhex_32 0x1234abcd : %8s\n", buf);
strdec_32(buf, 1234567890u);
printf("strdec_32 1234567890 : %10s\n", buf);
strhex_64(buf, 0x123456789abcdef0lu);
printf("strhex_64 123456789abcdef0 : %16s\n", buf);
strdec_64(buf, 12345678901234567890lu);
printf("strdec_64 12345678901234567890 : %20s\n", buf);
char ** const toks = strtoks(" 1.23 456 799\tok\nnext line looks ok\n", " \t\n\r");
for (u64 i = 0; toks[i]; i++) {
printf("token [%lu]: %s\n", i, toks[i]);
}
free(toks);
void * pg = pages_alloc_1gb(1);
if (pg) {
pages_unmap(pg, 1<<30);
printf("got 1gb page\n");
} else {
printf("no 1gb page\n");
}
pg = pages_alloc_2mb(1);
if (pg) {
pages_unmap(pg, 1<<21);
printf("got 2mb page\n");
} else {
printf("no 2mb page\n");
}
pg = pages_alloc_4kb(1);
if (pg) {
pages_unmap(pg, 1<<12);
printf("got 4kb page\n");
} else {
printf("no 4kb page\n");
}
pg = malloc(1024);
if (pg) {
memset(pg, 0, 1024);
memset(pg, 1, 1024);
qsort_u16(pg, 512);
qsort_u32(pg, 256);
qsort_u64(pg, 128);
free(pg);
} else {
printf("malloc failed\n");
debug_die();
}
u16 v16[4] = {0, 0xfffu, 0x8fffu, 0xffffu};
printf("cmp(%hu,%hu)=%d\n", v16[0], v16[1], compare_u16(v16, v16+1));
printf("cmp(%hu,%hu)=%d\n", v16[0], v16[2], compare_u16(v16, v16+2));
printf("cmp(%hu,%hu)=%d\n", v16[0], v16[3], compare_u16(v16, v16+3));
// done
u64 dt = time_diff_nsec(t0);
double dd = time_diff_sec(d0);
printf("nsec %lu sec %.9lf\n", dt, dd);
dt = time_nsec();
dt = time_diff_nsec(dt);
dd = time_sec();
dd = time_diff_sec(dd);
printf("cost nsec %lu sec %.9lf\n", dt, dd);
printf("Done. Good. wait gdb:\n");
pthread_t pt;
pthread_create(&pt, NULL, burning_th, NULL);
debug_die();
}
static void
test_segv(void)
{
u64 z = 0;
for (u64 i = 0; i < 10000; i++) {
const u64 x = random_u64();
const u64 * const p = (typeof(p))x;
const u64 y = *p;
z ^= y;
}
printf("crazy z = %lu\n", z);
}
static void
test_gcd(void)
{
printf("gcd 0 1 = %lu\n", gcd64(0, 1));
printf("gcd 1 1 = %lu\n", gcd64(1, 1));
printf("gcd 1 10 = %lu\n", gcd64(1, 10));
printf("gcd 10 10 = %lu\n", gcd64(10, 10));
printf("gcd 3 7 = %lu\n", gcd64(3, 7));
printf("gcd 100 99 = %lu\n", gcd64(100, 99));
printf("gcd 100 27 = %lu\n", gcd64(100, 27));
printf("gcd 100 16 = %lu\n", gcd64(100, 16));
printf("gcd 100 1024 = %lu\n", gcd64(100, 1024));
printf("gcd 100 359 = %lu\n", gcd64(100, 359));
printf("gcd 100 360 = %lu\n", gcd64(100, 360));
}
static void
lcp_verify_perf(const u32 len, const u32 lcp)
{
if (lcp > len)
return;
struct kv * const k1 = yalloc(len + sizeof(struct kv));
struct kv * const k2 = yalloc(len + sizeof(struct kv));
k1->klen = len;
k2->klen = len;
u8 * const v1 = k1->kv;
u8 * const v2 = k2->kv;
for (u64 i = 0; i < len; i++) {
v1[i] = (u8)random_u64();
v2[i] = v1[i];
}
if (lcp < len)
v2[lcp] = ~v1[lcp]; // only one diff
const u32 r1 = kv_key_lcp(k1, k2);
if (r1 != lcp) {
printf("WRONG LCP len %u lcp %u r1 %u\n", len, lcp, r1);
exit(0);
}
u64 r = 0;
const double t0 = time_sec();
for (u64 i = 0; i < (1lu << 26); i++) {
if (kv_key_lcp(k1, k2))
r++;
}
const double d0 = time_diff_sec(t0);
printf("%10lu %10u %10u time %7.4lf\n", r, len, lcp, d0);
free(k1);
free(k2);
}
static void
test_lcp(void)
{
for (u32 i = 11; i < 256; i += 11)
for (u32 j = 7; j <= i; j += 7)
lcp_verify_perf(i, j);
}
static void
test_shm(void)
{
u8 * p = NULL;
const int fd = shm_open("/wuxb", O_RDWR, 0600);
if (fd < 0) {
printf("object not found\n");
const int fd1 = shm_open("/wuxb", O_RDWR|O_CREAT, 0600);
if (fd1 < 0) {
printf("create failed\n");
exit(0);
} else {
printf("created /dev/shm/wuxb\n");
}
ftruncate(fd1, 4096);
p = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0);
close(fd1);
if (p == MAP_FAILED) {
printf("map failed\n");
exit(0);
} else {
printf("Run me again to see the saved values (a,b)\n");
}
p[0] = 'a';
p[4000] = 'b';
} else {
p = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) {
printf("map failed\n");
exit(0);
}
close(fd);
printf("read %c %c\n", p[0], p[4000]);
}
if (p) {
munmap(p, 4096);
}
}
// }}} misc
// gen {{{
static void
genperf(struct rgen * const gi, const char * const tag)
{
const u64 min = rgen_min(gi);
const u64 max = rgen_max(gi);
const u64 ts0 = time_nsec();
for (u64 i = 0; i < (1lu << 20); i++)
(void)rgen_next(gi);
const u64 dts = time_diff_nsec(ts0);
const u64 nr_ops = (100000000lu << 20) / dts;
for (u64 x = 0; x < 3; x++) {
printf("[%lu] %7lu %7lu %7lu %7lu %7lu %7lu ",
x, rgen_next(gi), rgen_next(gi), rgen_next(gi), rgen_next(gi), rgen_next(gi), rgen_next(gi));
const double t0 = time_sec();
uint64_t r = 0;
for (uint64_t i = 0; i < nr_ops; i++)
r += rgen_next(gi);
const double dt = time_diff_sec(t0);
(void)r;
printf("min %7lu max %7lu mops %7.3lf %s\n", min, max, ((double)nr_ops)/dt * 1e-6, tag);
}
u64 gmin = UINT64_MAX;
u64 gmax = 0;
for (u64 i = 0; i < 10000000; i++) {
const u64 r = rgen_next(gi);
if (r < min || r > max) {
fprintf(stderr, "%s %s r %lu min %lu max %lu\n",
__func__, tag, r, min, max);
debug_die();
}
if (r < gmin)
gmin = r;
if (r > gmax)
gmax = r;
}
printf("min/max after 10 million next(): %lu (%lx) %lu (%lx)\n", gmin, gmin, gmax, gmax);
rgen_destroy(gi);
}
static void
test_gen(void)
{
genperf(rgen_new_rnd64(), "rnd64");
genperf(rgen_new_rnd64s(1), "rnd64s 1");
genperf(rgen_new_rnd64s(37), "rnd64s 37");
genperf(rgen_new_rnd64s(0xffffffffu), "rnd64s UINT32_MAX");
genperf(rgen_new_uniform(0, (1<< 22)), "uniform");
genperf(rgen_new_uniform(0, (1<< 22) - (1 << 20)), "uniform");
genperf(rgen_new_uniform(0, (1<< 22) - 1), "uniform");
genperf(rgen_new_zipfian(0, 1000000), "zipfian");
genperf(rgen_new_xzipfian(0, 1000000), "xzipfian");
genperf(rgen_new_unizipf(0, 1000000, 100), "unizipf uf=100");
genperf(rgen_new_unizipf(0, 1000000, 100), "zipfuni uf=100");
genperf(rgen_new_latest(100), "latest (just zipfian)");
genperf(rgen_new_incs(0, 1000000), "incs");
genperf(rgen_new_decs(0, 1000000), "decs");
genperf(rgen_new_skips(0, 1000000, 100), "skips +100");
genperf(rgen_new_incu(0, 1000000), "incu");
genperf(rgen_new_decu(0, 1000000), "decu");
genperf(rgen_new_skipu(0, 1000000, 100), "skipu +100");
genperf(rgen_new_skipu(0, 1000000, 1), "skipu +1");
genperf(rgen_new_skipu(0, 1000000, -1), "skipu -1");
genperf(rgen_new_skipu(0, 999999, 200000), "skipu +200000");
genperf(rgen_new_skipu(0, 999999, -200000), "skipu -200000");
genperf(rgen_new_shuffle(0, 0), "shuffle [0,0]");
genperf(rgen_new_shuffle(0, 1), "shuffle [0,1]");
genperf(rgen_new_shuffle(0, 2), "shuffle [0,2]");
genperf(rgen_new_shuffle(0, 3), "shuffle [0,3]");
genperf(rgen_new_shuffle(0, 10), "shuffle [0,10]");
genperf(rgen_new_shuffle(0, 1000000), "shuffle [0,1000000]");
}
// gi will be freed by sample_dist
static void
sample_dist(struct rgen * const gi, const u64 range)
{
u64 * const ctr = calloc(range+1, sizeof(ctr[0]));
const u64 rep = range > 10000000 ? (range * 4) : 40000000;
for (u64 i = 0; i < rep; i++) {
const u64 r = rgen_next(gi);
if (r > range)
debug_die();
ctr[r]++;
}
rgen_destroy(gi);
printf("------\n");
qsort_u64_sample(ctr, range+1, 32, stdout);
free(ctr);
}
static void
test_gendist(void)
{
//printf("====SMALL UNIFORM====\n");
//for (u64 i = 1; i < 20; i++)
// sample_dist(rgen_new_uniform(0,i), i);
// x15
u64 ranges[20] = {100,500,1000,1000000,58644196,
34703230,100000000,172942040,142411471,4502812,
4077290,4179061,192678503,466551,16700946};
for (u64 i = 0; i < 2; i++) {
printf("====UNIFORM====\n");
sample_dist(rgen_new_uniform(0, ranges[i]), ranges[i]);
printf("====ZIPFIAN====\n");
sample_dist(rgen_new_zipfian(0, ranges[i]), ranges[i]);
printf("====XZIPFIAN====\n");
sample_dist(rgen_new_xzipfian(0, ranges[i]), ranges[i]);
}
}
static void
test_gentrace(void)
{
const char * const fn = "/tmp/lib1-test-genhead.bin";
FILE * const out = fopen(fn, "wb");
printf("samples to %s\n", fn);
for (u64 i = 0; i < 40; i++) {
const u32 r = (u32)(random_u64() % 1000lu);
printf(" %u", r);
fwrite(&r, sizeof(r), 1, out);
}
printf("\n\nrandom numbers:\n");
fclose(out);
struct rgen * const gen = rgen_new_trace32(fn, 1024);
for (u64 i = 0; i < 200; i++) {
const u32 r = (u32)rgen_next(gen);
printf(" %u", r);
}
printf("\n");
rgen_destroy(gen);
}
typedef u64(*genfunc)(struct rgen *);
static void
run_gen(genfunc f, struct rgen * ga)
{
rgen_async_wait(ga);
const double t0 = time_sec();
uint64_t r = 0;
const u64 nr_gen = 2000000000;
for (uint64_t i = 0; i < nr_gen; i++)
r += f(ga);
const double dt = time_diff_sec(t0);
(void)r;
const double mops = ((double)nr_gen) / (dt * 1e6);
const double MBps = ((double)nr_gen * sizeof(u32)) / (1024.0 * 1024.0 * dt);
printf("%s dt %.3lf mops %.3lf TP %.3lf MB/s\n", __func__, dt, mops, MBps);
}
static void
test_asyncgen(void)
{
thread_pin(0);
//struct rgen * const g1 = rgen_new_unizipf(0, 1000000, 1024);
struct rgen * const g1 = rgen_new_uniform(0, 1000000);
debug_assert(g1);
struct rgen * const g2 = rgen_fork(g1);
debug_assert(g2);
struct rgen * const ga = rgen_async_create(g2, 1);
debug_assert(ga);
printf("test sync origin\n");
run_gen(rgen_next, g1);
printf("test sync forked\n");
run_gen(rgen_next, g2);
printf("test nowait\n");
run_gen(rgen_next_nowait, ga);
printf("test wait\n");
run_gen(rgen_next, ga);
rgen_destroy(ga);
rgen_join(g2);
rgen_destroy(g1);
}
// }}} gen
// conc {{{
struct lock_worker_info {
void ** plocks;
u64 which_lock;
u64 id_mask;
au64 nr_ops;
u64 end_time;
};
static void *
lock_worker(void * const ptr)
{
struct lock_worker_info * const info = (typeof(info))ptr;
const u64 mask = info->id_mask;
const u64 end_time = info->end_time;
srandom_u64(time_nsec());
u64 count = 0;
#define LOCK_WORKER_BATCH ((4000))
do {
switch (info->which_lock) {
case 1:
for (u64 i = 0; i < LOCK_WORKER_BATCH; i++) {
const u64 x = random_u64() & mask;
spinlock_lock((spinlock *)info->plocks[x]);
spinlock_unlock((spinlock *)info->plocks[x]);
}
break;
case 2:
for (u64 i = 0; i < LOCK_WORKER_BATCH; i++) {
const u64 x = random_u64() & mask;
mutex_lock((mutex *)info->plocks[x]);
mutex_unlock((mutex *)info->plocks[x]);
}
break;
default:
sleep(1);
break;
}
count += LOCK_WORKER_BATCH;
} while (time_nsec() < end_time);
atomic_fetch_add(&(info->nr_ops), count);
return NULL;
}
static void
test_locks(void)
{
printf("locks: 1 spin %zu, 2 mutex %zu\n",
sizeof(spinlock), sizeof(pthread_mutex_t));
const u64 ncpus = process_affinity_count();
for (u64 p = 0; p <= 12; p+=3) { // power of 2 locks
const u64 nlocks = 1lu << p;
struct lock_worker_info info;
info.plocks = malloc(sizeof(info.plocks[0]) * nlocks);
info.id_mask = nlocks - 1;;
for (u64 i = 0; i < nlocks; i++) {
info.plocks[i] = yalloc(64);
}
for (u64 t = 1; t <= 2; t++) { // lock type
switch (t) {
case 1:
for (u64 x = 0; x < nlocks; x++)
spinlock_init(info.plocks[x]);
break;
case 2:
for (u64 x = 0; x < nlocks; x++)
pthread_mutex_init(info.plocks[x], NULL);
break;
}
for (u32 th = 4; th <= ncpus; th += 2) {
atomic_store(&(info.nr_ops), 0);
info.which_lock = t;
info.end_time = time_nsec() + (UINT64_C(2) << 30);
const u64 dt = thread_fork_join(th, lock_worker, false, &info);
const u64 nr = info.nr_ops;
const double mops = ((double)nr) * 1e3 / ((double)dt);
printf("TYPE %lu NTH %3u NLOCKS %3lu TP %6.2lf mops\n", t, th, nlocks, mops);
}
}
for (u64 i = 0; i < nlocks; i++) {
free(info.plocks[i]);
}
free(info.plocks);
}
}
struct rwlock_worker_info {
union {
rwlock * rw;
pthread_rwlock_t * pt;
} * locks;
u64 id_mask;
au64 seq;
au64 nr_w;
au64 nr_r;
u64 nr_writer;
u64 end_time;
};
static void *
rwlock_worker(void * const ptr)
{
struct rwlock_worker_info * const info = (typeof(info))ptr;
const u64 seq = atomic_fetch_add(&(info->seq), 1);
const bool is_writer = seq < info->nr_writer ? true : false;
const u64 mask = info->id_mask;
const u64 end_time = info->end_time;
srandom_u64(time_nsec());
u64 count = 0;
do {
for (u64 i = 0; i < 1024; i++) {
const u64 x = random_u64() & mask;
if (is_writer) {
rwlock_lock_write(info->locks[x].rw);
rwlock_unlock_write(info->locks[x].rw);
} else {
rwlock_lock_read(info->locks[x].rw);
rwlock_unlock_read(info->locks[x].rw);
}
}
count += 1024;
} while (time_nsec() < end_time);
if (is_writer) {
atomic_fetch_add(&(info->nr_w), count);
} else {
atomic_fetch_add(&(info->nr_r), count);
}
return NULL;
}
static void *
hplock_worker(void * const ptr)
{
struct rwlock_worker_info * const info = (typeof(info))ptr;
const u64 seq = atomic_fetch_add(&(info->seq), 1);
const bool is_writer = seq < info->nr_writer ? true : false;
const u64 mask = info->id_mask;
const u64 end_time = info->end_time;
srandom_u64(time_nsec());
u64 count = 0;
do {
for (u64 i = 0; i < 1024; i++) {
const u64 x = random_u64() & mask;
if (is_writer) {
rwlock_lock_write_hp(info->locks[x].rw);
rwlock_unlock_write(info->locks[x].rw);
} else {
rwlock_lock_read(info->locks[x].rw);
rwlock_unlock_read(info->locks[x].rw);
}
}
count += 1024;
} while (time_nsec() < end_time);
if (is_writer) {
atomic_fetch_add(&(info->nr_w), count);
} else {
atomic_fetch_add(&(info->nr_r), count);
}
return NULL;
}
static void *
rwlock_worker_pt(void * const ptr)
{
struct rwlock_worker_info * const info = (typeof(info))ptr;
const u64 seq = atomic_fetch_add(&(info->seq), 1);
const bool is_writer = seq < info->nr_writer ? true : false;
const u64 mask = info->id_mask;
const u64 end_time = info->end_time;
srandom_u64(time_nsec());
u64 count = 0;
do {
for (u64 i = 0; i < 1024; i++) {
const u64 x = random_u64() & mask;
if (is_writer) {
pthread_rwlock_wrlock(info->locks[x].pt);
pthread_rwlock_unlock(info->locks[x].pt);
} else {
pthread_rwlock_rdlock(info->locks[x].pt);
pthread_rwlock_unlock(info->locks[x].pt);
}
}
count += 1024;
} while (time_nsec() < end_time);
if (is_writer) {
atomic_fetch_add(&(info->nr_w), count);
} else {
atomic_fetch_add(&(info->nr_r), count);
}
return NULL;
}
static void
test_rwlock(void)
{
const u64 ncpus = process_affinity_count();
void * (*wkr[3])(void *) = {rwlock_worker, hplock_worker, rwlock_worker_pt};
for (u64 x = 0; x < 3; x++) {
for (u64 w = 0; w < 3; w++) {
for (u64 p = 0; p <= 9; p+=3) {
const u64 nlocks = 1lu << p;
struct rwlock_worker_info info;
info.locks = (typeof(info.locks))malloc(sizeof(info.locks[0]) * nlocks);
for (u64 i = 0; i < nlocks; i++) {
info.locks[i].rw = yalloc(64);
if (x < 2)
rwlock_init(info.locks[i].rw);
else
pthread_rwlock_init(info.locks[i].pt, NULL);
}
info.id_mask = nlocks - 1;;
info.seq = 0;
info.nr_writer = w;
for (u32 i = 4; i <= ncpus; i += 2) {
info.seq = 0;
atomic_store(&(info.nr_w), 0);
atomic_store(&(info.nr_r), 0);
info.end_time = time_nsec() + (UINT64_C(2) << 30);
const u64 dt = thread_fork_join(i, wkr[x], false, &info);
const u64 nr_w = atomic_load(&info.nr_w);
const double mw = ((double)nr_w) * 1e3 / ((double)dt);
const u64 nr_r = atomic_load(&info.nr_r);
const double mr = ((double)nr_r) * 1e3 / ((double)dt);
printf("%lu NTH %3u NW %3lu NL %3lu R %6.2lf W %6.2lf\n", x, i, w, nlocks, mr, mw);
}
for (u64 i = 0; i < nlocks; i++) {
free(info.locks[i].rw);
}
free(info.locks);
}
}
}
}
static void *
rcu_worker(void * ptr_rcu)
{
u64 sum = 0;
u64 cnt = 0;
struct rcu * const rcu = (typeof(rcu))ptr_rcu;
const double t0 = time_sec();
do {
for (u64 i = 0; i < 1024; i++) {
const u64 r = random_u64() % 0xff;
u64 * ptr = (typeof(ptr))rcu_ref(rcu, r);
sum += (*ptr);
rcu_unref(rcu, ptr, r);
cnt++;
}
} while (time_diff_sec(t0) < 5.0);
printf("cnt %20lu sum %20lu\n", cnt, sum);
return NULL;
}
static void
rcu_main(const u64 power)
{
struct rcu * const rcu = rcu_create(1lu << power);
u64 vec[1024] = {};
for (u64 i = 0; i < 1024; i++) {
vec[i] = i;
}
rcu_update(rcu, &(vec[1023]));
const u32 nth = process_affinity_count();
u32 cores[1024];
process_getaffinity_list(nth, cores);
pthread_t pt[1024];
printf("power %lu creating %u threads\n", power, nth);
for (u64 i = 0; i < nth; i++) {
thread_create_at(cores[i], &pt[i], rcu_worker, rcu);
}
const double t0 = time_sec();
u64 cnt = 0;
do {
for (u64 i = 0; i < 64; i++) {
const u64 r1 = random_u64() % 512;
rcu_update(rcu, &(vec[r1]));
const u64 r2 = (random_u64() % 512) + 512;
rcu_update(rcu, &(vec[r2]));
cnt += 2;
}
} while (time_diff_sec(t0) < 5.0);
for (u64 i = 0; i < nth; i++) {
pthread_join(pt[i], NULL);
}
printf("update %lu\n", cnt);
free(rcu);
}
static void
test_rcu(void)
{
for (u64 i = 4; i <= 10; i++) {
rcu_main(i);
}
}
static au64 qsbr_nop = 0;
static void *
qsbr_ref_worker(void * const ptr)
{
struct qsbr * const q = (typeof(q))ptr;
u64 count1 = 0;
const u64 endtime = time_nsec() + (u64)5e9;
do {
struct qsbr_ref qref = {};
for (u64 i = 0; i < 10000; i++) {
qsbr_register(q, &qref);
cpu_cfence();
qsbr_unregister(q, &qref);
}
count1 += 10000;
} while (time_nsec() < endtime);
qsbr_nop += count1;
return NULL;
}
static void *
qsbr_park_worker(void * const ptr)
{
struct qsbr * const q = (typeof(q))ptr;
u64 count1 = 0;
const u64 endtime = time_nsec() + (u64)5e9;
do {
struct qsbr_ref * const qref = malloc(sizeof(*qref));
qsbr_register(q, qref);
for (u64 i = 0; i < 10000; i++) {
qsbr_park(qref);
cpu_cfence();
qsbr_resume(qref);
}
qsbr_unregister(q, qref);
free(qref);
count1 += 10000;
} while (time_nsec() < endtime);
qsbr_nop += count1;
return NULL;
}
static volatile u64 * volatile qsbr_root;
static void *
qsbr_checker(void * const ptr)
{
struct qsbr * const q = (typeof(q))ptr;
const double t0 = time_sec();
u64 count = 0;
do {
struct qsbr_ref qref = {};
qsbr_register(q, &qref);
for (u64 i = 0; i < 4096; i++) {
volatile u64 * const root = qsbr_root;
if (*root)
debug_die();
qsbr_update(&qref, (u64)root);
if (*root)
debug_die();
cpu_pause();
if ((i & 0xff) == 0) {
qsbr_park(&qref);
cpu_pause();
qsbr_resume(&qref);
}
}
qsbr_unregister(q, &qref);
count++;
} while (time_diff_sec(t0) < 5.0);
qsbr_nop += (count * 4096);
return NULL;
}
static void
test_qsbr(void)
{
const u32 nth0 = process_affinity_count();
const u32 nth = nth0 < 64 ? nth0 : 64;
struct qsbr * const q = qsbr_create();
qsbr_nop = 0;
const u64 dt1 = thread_fork_join(1, qsbr_ref_worker, false, q);
printf(" 1 thread {ref/unref} mops %.2lf\n", ((double)qsbr_nop) * 1e3 / ((double)dt1));
qsbr_nop = 0;
const u64 dt4 = thread_fork_join(0, qsbr_ref_worker, false, q);
printf("%3u thread {ref/unref} mops %.2lf\n", nth, ((double)qsbr_nop) * 1e3 / ((double)dt4));
qsbr_nop = 0;
const u64 dt1p = thread_fork_join(1, qsbr_park_worker, false, q);
printf(" 1 thread {park/resume} mops %.2lf\n", ((double)qsbr_nop) * 1e3 / ((double)dt1p));
qsbr_nop = 0;
const u64 dt4p = thread_fork_join(0, qsbr_park_worker, false, q);
printf("%3u thread {park/resume} mops %.2lf\n", nth, ((double)qsbr_nop) * 1e3 / ((double)dt4p));
qsbr_nop = 0;
volatile u64 vars[2] = {0, 1};
qsbr_root = vars;
u32 cores[64];
process_getaffinity_list(nth, cores);
thread_pin(cores[0]);
printf("creating %u threads\n", nth-1);
pthread_t pt[64];
for (u64 i = 1; i < nth; i++)
thread_create_at(cores[i], &pt[i], qsbr_checker, q);
const double t0 = time_sec();
u64 cnt = 0;
do {
for (u64 i = 0; i < 1000; i++) {
vars[1] = 0; // valid data == 0
cpu_cfence();
qsbr_root = vars+1;
qsbr_wait(q, (u64)(vars+1));
vars[0] = 1; // invalidate
cpu_pause();
cpu_cfence();
cpu_pause();
(void)(vars[0]);
(void)(vars[1]);
vars[0] = 0;
cpu_cfence();
qsbr_root = vars;
qsbr_wait(q, (u64)(vars));
vars[1] = 1;
cpu_pause();
cpu_cfence();
cpu_pause();
(void)(vars[0]);
(void)(vars[1]);
}
cnt += 2000;
} while (time_diff_sec(t0) < 5.0);
for (u64 i = 1; i < nth; i++)
pthread_join(pt[i], NULL);
printf("wait per sec %lu ref/unref %lu\n", cnt/5, qsbr_nop);
qsbr_destroy(q);
}
// }}} conc
// statistics {{{
static void
one_damp(struct damp * const d, const double v)
{
char c = damp_add_test(d, v) ? 't':'f';
printf("add %lf result %c avg %lf ravg %lf\n", v, c, damp_avg(d), damp_ravg(d));
}
static void
test_damp(void)
{
struct damp * const d = damp_create(8, 0.004, 0.05);
debug_assert(d);
one_damp(d, 40.0);
one_damp(d, 40.09);
one_damp(d, 40.11);
damp_clean(d);
printf("cleaned\n");
for (u64 i = 0; i < 10; i++) {
one_damp(d, 55.0);
one_damp(d, 57.0);
}
for (u64 i = 0; i < 10; i++) {
one_damp(d, 56.0 + random_double());
one_damp(d, 56.0 - random_double());
}
damp_destroy(d);
}
static u64 fjcount;
static void *
fjworker(void * const ptr)
{
atomic_fetch_add((au64 *)ptr, 1);
return NULL;
}
static void *
fjname_l2(void * const ptr)
{
(void)ptr;
char name[16] = {};
thread_get_name(pthread_self(), name, 16);
printf("thread name: %s\n", name);
return NULL;
}
static void *
fjname_l1(void * const ptr)
{
(void)ptr;
char name[16] = {};
thread_get_name(pthread_self(), name, 16);
printf("thread name: %s\n", name);
thread_fork_join(3, fjname_l2, false, NULL);
return NULL;
}
static void
test_forkjoin(void)
{
thread_fork_join(3, fjname_l1, false, NULL);
for (u32 i = 1; i <= (1lu << 10); i <<= 1) {
printf("fj %u\n", i);
fjcount = 0;
cpu_cfence();
u64 t0 = time_nsec();
thread_fork_join(i, fjworker, false, &fjcount);
u64 dt = time_diff_nsec(t0);
cpu_cfence();
printf("dt %lu fjcount %lu\n", dt, fjcount);
}
for (u64 i = 0; i < 100; i++) {
for (u64 j = 0; j < 100; j++) {
thread_fork_join(64, fjworker, false, &fjcount);
}
printf("RSS %ldkB\n", process_get_rss());
}
}
// }}} statistics
// crc {{{
static void
test_crc32cpfx(void)
{
u32 crc = 0xdeadbeefu;
u8 * string = pages_alloc_4kb(1);
double t0, dt;
for (u64 i = 0; i < 4096; i++)
string[i] = (u8)random_u64();
u32 *crcx = pages_alloc_4kb(1);
t0 = time_sec();
for (u64 x = 0; x < 1000000; x++)
for (u64 i = 0; i < 1024; i++)
crcx[i] ^= (crc = crc32c_u8(crc, string[i]));
dt = time_diff_sec(t0);
for (u64 i = 0; i < 1024; i++)
crc ^= crcx[i];
printf("X%uX seq dt %.3lf\n", crc, dt);
memset(crcx, 0, 4096);
crc = 0xdeadbeefu;
t0 = time_sec();
for (u32 x = 0; x < 1000000; x++) {
for (u32 i = 0; i < 1024; i += 4) {
const u32 tmp = crc32c_u16(crc, *(u16 *)(&string[i]));
crcx[i+1] ^= tmp;