-
Notifications
You must be signed in to change notification settings - Fork 24
/
wh.c
3619 lines (3213 loc) · 97.1 KB
/
wh.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
// headers {{{
#include <assert.h> // static_assert
#include "lib.h"
#include "ctypes.h"
#include "kv.h"
#include "wh.h"
// }}} headers
// def {{{
#define WH_HMAPINIT_SIZE ((1u << 12)) // 10: 16KB/64KB 12: 64KB/256KB 14: 256KB/1MB
#define WH_SLABMETA_SIZE ((1lu << 21)) // 2MB
#ifndef HEAPCHECKING
#define WH_SLABLEAF_SIZE ((1lu << 21)) // 2MB is ok
#else
#define WH_SLABLEAF_SIZE ((1lu << 21)) // 2MB for valgrind
#endif
#define WH_KPN ((128u)) // keys per node; power of 2
#define WH_HDIV (((1u << 16)) / WH_KPN)
#define WH_MID ((WH_KPN >> 1)) // ideal cut point for split, the closer the better
#define WH_BKT_NR ((8))
#define WH_KPN2 ((WH_KPN + WH_KPN))
#define WH_KPN_MRG (((WH_KPN + WH_MID) >> 1 )) // 3/4
// FO is fixed at 256. Don't change it
#define WH_FO ((256u)) // index fan-out
// number of bits in a bitmap
#define WH_BMNR ((WH_FO >> 6)) // number of u64
// }}} def
// struct {{{
struct wormmeta {
struct entry13 k13; // kref+klen
struct entry13 l13; // lmost+bitmin+bitmax
struct entry13 r13; // rmost+hash32_lo
struct entry13 p13; // lpath+hash32_hi
u64 bitmap[0]; // 4 if bitmin != bitmax
};
static_assert(sizeof(struct wormmeta) == 32, "sizeof(wormmeta) != 32");
struct wormkv64 { u64 key; void * ptr; }; // u64 keys (whu64)
struct wormleaf {
// first line
rwlock leaflock;
spinlock sortlock; // to protect the seemingly "read-only" iter_seek
au64 lv; // version (dont use the first u64)
struct wormleaf * prev; // prev leaf
struct wormleaf * next; // next leaf
struct kv * anchor;
u32 nr_sorted;
u32 nr_keys;
u64 reserved[2];
struct entry13 hs[WH_KPN]; // sorted by hashes
u8 ss[WH_KPN]; // sorted by keys
};
struct wormslot { u16 t[WH_BKT_NR]; };
static_assert(sizeof(struct wormslot) == 16, "sizeof(wormslot) != 16");
struct wormmbkt { struct wormmeta * e[WH_BKT_NR]; };
static_assert(sizeof(struct wormmbkt) == 64, "sizeof(wormmbkt) != 64");
struct wormhmap {
au64 hv;
struct wormslot * wmap;
struct wormmbkt * pmap;
u32 mask;
u32 maxplen;
u64 msize;
struct slab * slab1;
struct slab * slab2;
struct kv * pbuf;
};
static_assert(sizeof(struct wormhmap) == 64, "sizeof(wormhmap) != 64");
struct wormhole {
// 1 line
union {
volatile au64 hmap_ptr; // safe
struct wormhmap * hmap; // unsafe
};
u64 padding0[6];
struct wormleaf * leaf0; // usually not used
// 1 line
struct kvmap_mm mm;
struct qsbr * qsbr;
struct slab * slab_leaf;
struct kv * pbuf;
u32 leaftype;
u32 padding1;
// 2 lines
struct wormhmap hmap2[2];
// fifth line
rwlock metalock;
u32 padding2[15];
};
struct wormhole_iter {
struct wormref * ref; // safe-iter only
struct wormhole * map;
struct wormleaf * leaf;
u32 is;
};
struct wormref {
struct wormhole * map;
struct qsbr_ref qref;
};
// }}} struct
// helpers {{{
// meta {{{
static inline struct kv *
wormmeta_keyref_load(const struct wormmeta * const meta)
{
return u64_to_ptr(meta->k13.e3);
}
static inline u16
wormmeta_klen_load(const struct wormmeta * const meta)
{
return meta->k13.e1;
}
static inline struct wormleaf *
wormmeta_lmost_load(const struct wormmeta * const meta)
{
return u64_to_ptr(meta->l13.e3 & (~0x3flu));
}
static inline u32
wormmeta_bitmin_load(const struct wormmeta * const meta)
{
return (u32)(meta->l13.v64 & 0x1fflu);
}
static inline u32
wormmeta_bitmax_load(const struct wormmeta * const meta)
{
return (u32)((meta->l13.v64 >> 9) & 0x1fflu);
}
static inline u32
wormmeta_hash32_load(const struct wormmeta * const meta)
{
return ((u32)meta->r13.e1) | (((u32)meta->p13.e1) << 16);
}
static inline struct wormleaf *
wormmeta_rmost_load(const struct wormmeta * const meta)
{
return u64_to_ptr(meta->r13.e3);
}
static inline struct wormleaf *
wormmeta_lpath_load(const struct wormmeta * const meta)
{
return u64_to_ptr(meta->p13.e3);
}
// internal
static inline void
wormmeta_lpath_store(struct wormmeta * const meta, struct wormleaf * const leaf)
{
entry13_update_e3(&meta->p13, ptr_to_u64(leaf));
}
// also updates leaf_klen_eq and
static inline void
wormmeta_lmost_store(struct wormmeta * const meta, struct wormleaf * const leaf)
{
const u64 minmax = meta->l13.v64 & 0x3fffflu;
meta->l13.v64 = (((u64)leaf) << 16) | minmax;
const bool leaf_klen_eq = leaf->anchor->klen == wormmeta_klen_load(meta);
wormmeta_lpath_store(meta, leaf_klen_eq ? leaf : leaf->prev);
}
static inline void
wormmeta_bitmin_store(struct wormmeta * const meta, const u32 bitmin)
{
meta->l13.v64 = (meta->l13.v64 & (~0x1fflu)) | bitmin;
}
static inline void
wormmeta_bitmax_store(struct wormmeta * const meta, const u32 bitmax)
{
meta->l13.v64 = (meta->l13.v64 & (~0x3fe00lu)) | (bitmax << 9);
}
static inline void
wormmeta_rmost_store(struct wormmeta * const meta, struct wormleaf * const leaf)
{
entry13_update_e3(&meta->r13, ptr_to_u64(leaf));
}
// for wormmeta_alloc
static void
wormmeta_init(struct wormmeta * const meta, struct wormleaf * const lrmost,
struct kv * const keyref, const u32 alen, const u32 bit)
{
keyref->refcnt++; // shared
const u32 plen = keyref->klen;
debug_assert(plen <= UINT16_MAX);
meta->k13 = entry13((u16)plen, ptr_to_u64(keyref));
meta->l13.v64 = (ptr_to_u64(lrmost) << 16) | (bit << 9) | bit;
const u32 hash32 = keyref->hashlo;
meta->r13 = entry13((u16)hash32, ptr_to_u64(lrmost));
const bool leaf_klen_eq = alen == plen;
meta->p13 = entry13((u16)(hash32 >> 16), ptr_to_u64(leaf_klen_eq ? lrmost : lrmost->prev));
}
// }}} meta
// meta-bitmap {{{
static inline bool
wormmeta_bm_test(const struct wormmeta * const meta, const u32 id)
{
debug_assert(id < WH_FO);
const u32 bitmin = wormmeta_bitmin_load(meta);
const u32 bitmax = wormmeta_bitmax_load(meta);
if (bitmin == bitmax) { // half node
return bitmin == id;
} else { // full node
return (bool)((meta->bitmap[id >> 6u] >> (id & 0x3fu)) & 1lu);
}
}
// meta must be a full node
static void
wormmeta_bm_set(struct wormmeta * const meta, const u32 id)
{
// need to replace meta
u64 * const ptr = &(meta->bitmap[id >> 6u]);
const u64 bit = 1lu << (id & 0x3fu);
if ((*ptr) & bit)
return;
(*ptr) |= bit;
// min
if (id < wormmeta_bitmin_load(meta))
wormmeta_bitmin_store(meta, id);
// max
const u32 oldmax = wormmeta_bitmax_load(meta);
if (oldmax == WH_FO || id > oldmax)
wormmeta_bitmax_store(meta, id);
}
// find the lowest bit > id0
// return WH_FO if not found
static inline u32
wormmeta_bm_gt(const struct wormmeta * const meta, const u32 id0)
{
u32 ix = id0 >> 6;
u64 bits = meta->bitmap[ix] & ~((1lu << (id0 & 0x3fu)) - 1lu);
if (bits)
return (ix << 6) + (u32)__builtin_ctzl(bits);
while (++ix < WH_BMNR) {
bits = meta->bitmap[ix];
if (bits)
return (ix << 6) + (u32)__builtin_ctzl(bits);
}
return WH_FO;
}
// find the highest bit that is lower than the id0
// return WH_FO if not found
static inline u32
wormmeta_bm_lt(const struct wormmeta * const meta, const u32 id0)
{
u32 ix = id0 >> 6;
u64 bits = meta->bitmap[ix] & ((1lu << (id0 & 0x3fu)) - 1lu);
if (bits)
return (ix << 6) + 63u - (u32)__builtin_clzl(bits);
while (ix--) {
bits = meta->bitmap[ix];
if (bits)
return (ix << 6) + 63u - (u32)__builtin_clzl(bits);
}
return WH_FO;
}
// meta must be a full node
static inline void
wormmeta_bm_clear(struct wormmeta * const meta, const u32 id)
{
debug_assert(wormmeta_bitmin_load(meta) < wormmeta_bitmax_load(meta));
meta->bitmap[id >> 6u] &= (~(1lu << (id & 0x3fu)));
// min
if (id == wormmeta_bitmin_load(meta))
wormmeta_bitmin_store(meta, wormmeta_bm_gt(meta, id));
// max
if (id == wormmeta_bitmax_load(meta))
wormmeta_bitmax_store(meta, wormmeta_bm_lt(meta, id));
}
// }}} meta-bitmap
// key/prefix {{{
static inline u16
wormhole_pkey(const u32 hash32)
{
const u16 pkey0 = ((u16)hash32) ^ ((u16)(hash32 >> 16));
return pkey0 ? pkey0 : 1;
}
static inline u32
wormhole_bswap(const u32 hashlo)
{
return __builtin_bswap32(hashlo);
}
static inline bool
wormhole_key_meta_match(const struct kv * const key, const struct wormmeta * const meta)
{
return (key->klen == wormmeta_klen_load(meta))
&& (!memcmp(key->kv, wormmeta_keyref_load(meta)->kv, key->klen));
}
// called by get_kref_slot
static inline bool
wormhole_kref_meta_match(const struct kref * const kref,
const struct wormmeta * const meta)
{
return (kref->len == wormmeta_klen_load(meta))
&& (!memcmp(kref->ptr, wormmeta_keyref_load(meta)->kv, kref->len));
}
// called from meta_down ... get_kref1_slot
// will access rmost, prefetching is effective here
static inline bool
wormhole_kref1_meta_match(const struct kref * const kref,
const struct wormmeta * const meta, const u8 cid)
{
const u8 * const keybuf = wormmeta_keyref_load(meta)->kv;
const u32 plen = kref->len;
return ((plen + 1) == wormmeta_klen_load(meta))
&& (!memcmp(kref->ptr, keybuf, plen))
&& (keybuf[plen] == cid);
}
// warning: be careful with buffer overflow
static inline void
wormhole_prefix(struct kv * const pfx, const u32 klen)
{
pfx->klen = klen;
kv_update_hash(pfx);
}
// for split
static inline void
wormhole_prefix_inc1(struct kv * const pfx)
{
pfx->hashlo = crc32c_u8(pfx->hashlo, pfx->kv[pfx->klen]);
pfx->klen++;
}
// meta_lcp only
static inline void
wormhole_kref_inc(struct kref * const kref, const u32 len0,
const u32 crc, const u32 inc)
{
kref->hash32 = crc32c_inc(kref->ptr + len0, inc, crc);
kref->len = len0 + inc;
}
// meta_lcp only
static inline void
wormhole_kref_inc_123(struct kref * const kref, const u32 len0,
const u32 crc, const u32 inc)
{
kref->hash32 = crc32c_inc_123(kref->ptr + len0, inc, crc);
kref->len = len0 + inc;
}
// }}} key/prefix
// alloc {{{
static inline struct kv *
wormhole_alloc_akey(const size_t klen)
{
#ifdef ALLOCFAIL
if (alloc_fail())
return NULL;
#endif
return malloc(sizeof(struct kv) + klen);
}
static inline void
wormhole_free_akey(struct kv * const akey)
{
free(akey);
}
static inline struct kv *
wormhole_alloc_mkey(const size_t klen)
{
#ifdef ALLOCFAIL
if (alloc_fail())
return NULL;
#endif
return malloc(sizeof(struct kv) + klen);
}
static inline void
wormhole_free_mkey(struct kv * const mkey)
{
free(mkey);
}
static struct wormleaf *
wormleaf_alloc(struct wormhole * const map, struct wormleaf * const prev,
struct wormleaf * const next, struct kv * const anchor)
{
struct wormleaf * const leaf = slab_alloc_safe(map->slab_leaf);
if (leaf == NULL)
return NULL;
rwlock_init(&(leaf->leaflock));
spinlock_init(&(leaf->sortlock));
// keep the old version; new version will be assigned by split functions
//leaf->lv = 0;
leaf->prev = prev;
leaf->next = next;
leaf->anchor = anchor;
leaf->nr_keys = 0;
leaf->nr_sorted = 0;
// hs requires zero init.
memset(leaf->hs, 0, sizeof(leaf->hs[0]) * WH_KPN);
return leaf;
}
static void
wormleaf_free(struct slab * const slab, struct wormleaf * const leaf)
{
debug_assert(leaf->leaflock.opaque == 0);
wormhole_free_akey(leaf->anchor);
slab_free_safe(slab, leaf);
}
static struct wormmeta *
wormmeta_alloc(struct wormhmap * const hmap, struct wormleaf * const lrmost,
struct kv * const keyref, const u32 alen, const u32 bit)
{
debug_assert(alen <= UINT16_MAX);
debug_assert(lrmost && keyref);
struct wormmeta * const meta = slab_alloc_unsafe(hmap->slab1);
if (meta == NULL)
return NULL;
wormmeta_init(meta, lrmost, keyref, alen, bit);
return meta;
}
static inline bool
wormhole_slab_reserve(struct wormhole * const map, const u32 nr)
{
#ifdef ALLOCFAIL
if (alloc_fail())
return false;
#endif
for (u32 i = 0; i < 2; i++) {
if (!(map->hmap2[i].slab1 && map->hmap2[i].slab2))
continue;
if (!slab_reserve_unsafe(map->hmap2[i].slab1, nr))
return false;
if (!slab_reserve_unsafe(map->hmap2[i].slab2, nr))
return false;
}
return true;
}
static void
wormmeta_keyref_release(struct wormmeta * const meta)
{
struct kv * const keyref = wormmeta_keyref_load(meta);
debug_assert(keyref->refcnt);
keyref->refcnt--;
if (keyref->refcnt == 0)
wormhole_free_mkey(keyref);
}
static void
wormmeta_free(struct wormhmap * const hmap, struct wormmeta * const meta)
{
wormmeta_keyref_release(meta);
slab_free_unsafe(hmap->slab1, meta);
}
// }}} alloc
// lock {{{
static void
wormleaf_lock_write(struct wormleaf * const leaf, struct wormref * const ref)
{
if (!rwlock_trylock_write(&(leaf->leaflock))) {
wormhole_park(ref);
rwlock_lock_write(&(leaf->leaflock));
wormhole_resume(ref);
}
}
static void
wormleaf_lock_read(struct wormleaf * const leaf, struct wormref * const ref)
{
if (!rwlock_trylock_read(&(leaf->leaflock))) {
wormhole_park(ref);
rwlock_lock_read(&(leaf->leaflock));
wormhole_resume(ref);
}
}
static void
wormleaf_unlock_write(struct wormleaf * const leaf)
{
rwlock_unlock_write(&(leaf->leaflock));
}
static void
wormleaf_unlock_read(struct wormleaf * const leaf)
{
rwlock_unlock_read(&(leaf->leaflock));
}
static void
wormhmap_lock(struct wormhole * const map, struct wormref * const ref)
{
if (!rwlock_trylock_write(&(map->metalock))) {
wormhole_park(ref);
rwlock_lock_write(&(map->metalock));
wormhole_resume(ref);
}
}
static inline void
wormhmap_unlock(struct wormhole * const map)
{
rwlock_unlock_write(&(map->metalock));
}
// }}} lock
// hmap-version {{{
static inline struct wormhmap *
wormhmap_switch(struct wormhole * const map, struct wormhmap * const hmap)
{
return (hmap == map->hmap2) ? (hmap + 1) : (hmap - 1);
}
static inline struct wormhmap *
wormhmap_load(struct wormhole * const map)
{
return (struct wormhmap *)atomic_load_explicit(&(map->hmap_ptr), MO_ACQUIRE);
}
static inline void
wormhmap_store(struct wormhole * const map, struct wormhmap * const hmap)
{
atomic_store_explicit(&(map->hmap_ptr), (u64)hmap, MO_RELEASE);
}
static inline u64
wormhmap_version_load(const struct wormhmap * const hmap)
{
// no concurrent access
return atomic_load_explicit(&(hmap->hv), MO_ACQUIRE);
}
static inline void
wormhmap_version_store(struct wormhmap * const hmap, const u64 v)
{
atomic_store_explicit(&(hmap->hv), v, MO_RELEASE);
}
static inline u64
wormleaf_version_load(struct wormleaf * const leaf)
{
return atomic_load_explicit(&(leaf->lv), MO_CONSUME);
}
static inline void
wormleaf_version_store(struct wormleaf * const leaf, const u64 v)
{
atomic_store_explicit(&(leaf->lv), v, MO_RELEASE);
}
// }}} hmap-version
// co {{{
static inline void
wormhmap_prefetch_pmap(const struct wormhmap * const hmap, const u32 idx)
{
#if defined(CORR)
(void)hmap;
(void)idx;
#else
cpu_prefetch0(&(hmap->pmap[idx]));
#endif
}
static inline struct wormmeta *
wormhmap_get_meta(const struct wormhmap * const hmap, const u32 mid, const u32 i)
{
struct wormmeta * const meta = hmap->pmap[mid].e[i];
#if defined(CORR)
cpu_prefetch0(meta);
corr_yield();
#endif
return meta;
}
static inline void
wormleaf_prefetch(struct wormleaf * const leaf, const u32 hashlo)
{
const u32 i = wormhole_pkey(hashlo) / WH_HDIV;
#if defined(CORR)
cpu_prefetch0(leaf);
cpu_prefetch0(&(leaf->hs[i-4]));
cpu_prefetch0(&(leaf->hs[i+4]));
corr_yield();
#else
cpu_prefetch0(&(leaf->hs[i]));
#endif
}
static inline bool
wormhole_kref_kv_match(const struct kref * const key, const struct kv * const curr)
{
#if defined(CORR)
const u8 * const ptr = (typeof(ptr))curr;
cpu_prefetch0(ptr);
cpu_prefetch0(ptr + 64);
if (key->len > 56) {
cpu_prefetch0(ptr + 128);
cpu_prefetch0(ptr + 192);
}
corr_yield();
#endif
return kref_kv_match(key, curr);
}
static inline void
wormhole_qsbr_update_pause(struct wormref * const ref, const u64 v)
{
qsbr_update(&ref->qref, v);
#if defined(CORR)
corr_yield();
#endif
}
// }}} co
// }}} helpers
// hmap {{{
// hmap is the MetaTrieHT of Wormhole
static bool
wormhmap_init(struct wormhmap * const hmap, struct kv * const pbuf)
{
const u64 wsize = sizeof(hmap->wmap[0]) * WH_HMAPINIT_SIZE;
const u64 psize = sizeof(hmap->pmap[0]) * WH_HMAPINIT_SIZE;
u64 msize = wsize + psize;
u8 * const mem = pages_alloc_best(msize, true, &msize);
if (mem == NULL)
return false;
hmap->pmap = (typeof(hmap->pmap))mem;
hmap->wmap = (typeof(hmap->wmap))(mem + psize);
hmap->msize = msize;
hmap->mask = WH_HMAPINIT_SIZE - 1;
wormhmap_version_store(hmap, 0);
hmap->maxplen = 0;
hmap->pbuf = pbuf;
return true;
}
static inline void
wormhmap_deinit(struct wormhmap * const hmap)
{
if (hmap->pmap) {
pages_unmap(hmap->pmap, hmap->msize);
hmap->pmap = NULL;
hmap->wmap = NULL;
}
}
static inline m128
wormhmap_zero(void)
{
#if defined(__x86_64__)
return _mm_setzero_si128();
#elif defined(__aarch64__)
return vdupq_n_u8(0);
#endif
}
static inline m128
wormhmap_m128_pkey(const u16 pkey)
{
#if defined(__x86_64__)
return _mm_set1_epi16((short)pkey);
#elif defined(__aarch64__)
return vreinterpretq_u8_u16(vdupq_n_u16(pkey));
#endif
}
static inline u32
wormhmap_match_mask(const struct wormslot * const s, const m128 skey)
{
#if defined(__x86_64__)
const m128 sv = _mm_load_si128((const void *)s);
return (u32)_mm_movemask_epi8(_mm_cmpeq_epi16(skey, sv));
#elif defined(__aarch64__)
const uint16x8_t sv = vld1q_u16((const u16 *)s); // load 16 bytes at s
const uint16x8_t cmp = vceqq_u16(vreinterpretq_u16_u8(skey), sv); // cmpeq => 0xffff or 0x0000
static const uint16x8_t mbits = {0x3, 0xc, 0x30, 0xc0, 0x300, 0xc00, 0x3000, 0xc000};
return (u32)vaddvq_u16(vandq_u16(cmp, mbits));
#endif
}
static inline bool
wormhmap_match_any(const struct wormslot * const s, const m128 skey)
{
#if defined(__x86_64__)
return wormhmap_match_mask(s, skey) != 0;
#elif defined(__aarch64__)
const uint16x8_t sv = vld1q_u16((const u16 *)s); // load 16 bytes at s
const uint16x8_t cmp = vceqq_u16(vreinterpretq_u16_u8(skey), sv); // cmpeq => 0xffff or 0x0000
return vaddvq_u32(vreinterpretq_u32_u16(cmp)) != 0;
#endif
}
// meta_lcp only
static inline bool
wormhmap_peek(const struct wormhmap * const hmap, const u32 hash32)
{
const m128 sk = wormhmap_m128_pkey(wormhole_pkey(hash32));
const u32 midx = hash32 & hmap->mask;
const u32 midy = wormhole_bswap(hash32) & hmap->mask;
return wormhmap_match_any(&(hmap->wmap[midx]), sk)
|| wormhmap_match_any(&(hmap->wmap[midy]), sk);
}
static inline struct wormmeta *
wormhmap_get_slot(const struct wormhmap * const hmap, const u32 mid,
const m128 skey, const struct kv * const key)
{
u32 mask = wormhmap_match_mask(&(hmap->wmap[mid]), skey);
while (mask) {
const u32 i2 = (u32)__builtin_ctz(mask);
struct wormmeta * const meta = wormhmap_get_meta(hmap, mid, i2>>1);
if (likely(wormhole_key_meta_match(key, meta)))
return meta;
mask ^= (3u << i2);
}
return NULL;
}
static struct wormmeta *
wormhmap_get(const struct wormhmap * const hmap, const struct kv * const key)
{
const u32 hash32 = key->hashlo;
const u32 midx = hash32 & hmap->mask;
wormhmap_prefetch_pmap(hmap, midx);
const u32 midy = wormhole_bswap(hash32) & hmap->mask;
wormhmap_prefetch_pmap(hmap, midy);
const m128 skey = wormhmap_m128_pkey(wormhole_pkey(hash32));
struct wormmeta * const r = wormhmap_get_slot(hmap, midx, skey, key);
if (r)
return r;
return wormhmap_get_slot(hmap, midy, skey, key);
}
// for meta_lcp only
static inline struct wormmeta *
wormhmap_get_kref_slot(const struct wormhmap * const hmap, const u32 mid,
const m128 skey, const struct kref * const kref)
{
u32 mask = wormhmap_match_mask(&(hmap->wmap[mid]), skey);
while (mask) {
const u32 i2 = (u32)__builtin_ctz(mask);
struct wormmeta * const meta = wormhmap_get_meta(hmap, mid, i2>>1);
if (likely(wormhole_kref_meta_match(kref, meta)))
return meta;
mask ^= (3u << i2);
}
return NULL;
}
// for meta_lcp only
static inline struct wormmeta *
wormhmap_get_kref(const struct wormhmap * const hmap, const struct kref * const kref)
{
const u32 hash32 = kref->hash32;
const u32 midx = hash32 & hmap->mask;
wormhmap_prefetch_pmap(hmap, midx);
const u32 midy = wormhole_bswap(hash32) & hmap->mask;
wormhmap_prefetch_pmap(hmap, midy);
const m128 skey = wormhmap_m128_pkey(wormhole_pkey(hash32));
struct wormmeta * const r = wormhmap_get_kref_slot(hmap, midx, skey, kref);
if (r)
return r;
return wormhmap_get_kref_slot(hmap, midy, skey, kref);
}
// for meta_down only
static inline struct wormmeta *
wormhmap_get_kref1_slot(const struct wormhmap * const hmap, const u32 mid,
const m128 skey, const struct kref * const kref, const u8 cid)
{
u32 mask = wormhmap_match_mask(&(hmap->wmap[mid]), skey);
while (mask) {
const u32 i2 = (u32)__builtin_ctz(mask);
struct wormmeta * const meta = wormhmap_get_meta(hmap, mid, i2>>1);
//cpu_prefetch0(wormmeta_rmost_load(meta)); // will access
if (likely(wormhole_kref1_meta_match(kref, meta, cid)))
return meta;
mask ^= (3u << i2);
}
return NULL;
}
// for meta_down only
static inline struct wormmeta *
wormhmap_get_kref1(const struct wormhmap * const hmap,
const struct kref * const kref, const u8 cid)
{
const u32 hash32 = crc32c_u8(kref->hash32, cid);
const u32 midx = hash32 & hmap->mask;
wormhmap_prefetch_pmap(hmap, midx);
const u32 midy = wormhole_bswap(hash32) & hmap->mask;
wormhmap_prefetch_pmap(hmap, midy);
const m128 skey = wormhmap_m128_pkey(wormhole_pkey(hash32));
struct wormmeta * const r = wormhmap_get_kref1_slot(hmap, midx, skey, kref, cid);
if (r)
return r;
return wormhmap_get_kref1_slot(hmap, midy, skey, kref, cid);
}
static inline u32
wormhmap_slot_count(const struct wormslot * const slot)
{
const u32 mask = wormhmap_match_mask(slot, wormhmap_zero());
return mask ? ((u32)__builtin_ctz(mask) >> 1) : 8;
}
static inline void
wormhmap_squeeze(const struct wormhmap * const hmap)
{
struct wormslot * const wmap = hmap->wmap;
struct wormmbkt * const pmap = hmap->pmap;
const u32 mask = hmap->mask;
const u64 nrs64 = ((u64)(hmap->mask)) + 1; // must use u64; u32 can overflow
for (u64 si64 = 0; si64 < nrs64; si64++) { // # of buckets
const u32 si = (u32)si64;
u32 ci = wormhmap_slot_count(&(wmap[si]));
for (u32 ei = ci - 1; ei < WH_BKT_NR; ei--) {
struct wormmeta * const meta = pmap[si].e[ei];
const u32 sj = wormmeta_hash32_load(meta) & mask; // first hash
if (sj == si)
continue;
// move
const u32 ej = wormhmap_slot_count(&(wmap[sj]));
if (ej < WH_BKT_NR) { // has space at home location
wmap[sj].t[ej] = wmap[si].t[ei];
pmap[sj].e[ej] = pmap[si].e[ei];
const u32 ni = ci - 1;
if (ei < ni) {
wmap[si].t[ei] = wmap[si].t[ni];
pmap[si].e[ei] = pmap[si].e[ni];
}
wmap[si].t[ni] = 0;
pmap[si].e[ni] = NULL;
ci--;
}
}
}
}
static void
wormhmap_expand(struct wormhmap * const hmap)
{
// sync expand
const u32 mask0 = hmap->mask;
if (mask0 == UINT32_MAX)
debug_die();
const u32 nr0 = mask0 + 1;
const u32 mask1 = mask0 + nr0;
const u64 nr1 = ((u64)nr0) << 1; // must use u64; u32 can overflow
const u64 wsize = nr1 * sizeof(hmap->wmap[0]);
const u64 psize = nr1 * sizeof(hmap->pmap[0]);
u64 msize = wsize + psize;
u8 * mem = pages_alloc_best(msize, true, &msize);
if (mem == NULL) {
// We are at a very deep call stack from wormhole_put().
// Gracefully handling the failure requires lots of changes.
// Currently we simply wait for available memory
// TODO: gracefully return with insertion failure
char ts[64];
time_stamp(ts, 64);
fprintf(stderr, "%s %s sleep-wait for memory allocation %lukB\n",
__func__, ts, msize >> 10);
do {
sleep(1);
mem = pages_alloc_best(msize, true, &msize);
} while (mem == NULL);
time_stamp(ts, 64);
fprintf(stderr, "%s %s memory allocation done\n", __func__, ts);
}
struct wormhmap hmap1 = *hmap;
hmap1.pmap = (typeof(hmap1.pmap))mem;
hmap1.wmap = (typeof(hmap1.wmap))(mem + psize);
hmap1.msize = msize;
hmap1.mask = mask1;
const struct wormslot * const wmap0 = hmap->wmap;
const struct wormmbkt * const pmap0 = hmap->pmap;
for (u32 s = 0; s < nr0; s++) {
const struct wormmbkt * const bkt = &pmap0[s];
for (u32 i = 0; (i < WH_BKT_NR) && bkt->e[i]; i++) {
const struct wormmeta * const meta = bkt->e[i];
const u32 hash32 = wormmeta_hash32_load(meta);
const u32 idx0 = hash32 & mask0;
const u32 idx1 = ((idx0 == s) ? hash32 : wormhole_bswap(hash32)) & mask1;
const u32 n = wormhmap_slot_count(&(hmap1.wmap[idx1]));
debug_assert(n < 8);
hmap1.wmap[idx1].t[n] = wmap0[s].t[i];
hmap1.pmap[idx1].e[n] = bkt->e[i];
}
}
pages_unmap(hmap->pmap, hmap->msize);
hmap->pmap = hmap1.pmap;
hmap->wmap = hmap1.wmap;
hmap->msize = hmap1.msize;
hmap->mask = hmap1.mask;
wormhmap_squeeze(hmap);
}
static bool
wormhmap_cuckoo(struct wormhmap * const hmap, const u32 mid0,
struct wormmeta * const e0, const u16 s0, const u32 depth)
{
const u32 ii = wormhmap_slot_count(&(hmap->wmap[mid0]));
if (ii < WH_BKT_NR) {
hmap->wmap[mid0].t[ii] = s0;
hmap->pmap[mid0].e[ii] = e0;
return true;
} else if (depth == 0) {
return false;
}
// depth > 0
struct wormmbkt * const bkt = &(hmap->pmap[mid0]);
u16 * const sv = &(hmap->wmap[mid0].t[0]);
for (u32 i = 0; i < WH_BKT_NR; i++) {
const struct wormmeta * const meta = bkt->e[i];
debug_assert(meta);
const u32 hash32 = wormmeta_hash32_load(meta);
const u32 midx = hash32 & hmap->mask;
const u32 midy = wormhole_bswap(hash32) & hmap->mask;
const u32 midt = (midx != mid0) ? midx : midy;
if (midt != mid0) { // possible
// no penalty if moving someone back to its 1st hash location
const u32 depth1 = (midt == midx) ? depth : (depth - 1);
if (wormhmap_cuckoo(hmap, midt, bkt->e[i], sv[i], depth1)) {
bkt->e[i] = e0;
sv[i] = s0;