-
Notifications
You must be signed in to change notification settings - Fork 13
/
hashtable.c
1187 lines (971 loc) · 38.3 KB
/
hashtable.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
#include "hashtable.h"
#include "item.h"
#include "seg.h"
#include <cc_mm.h>
#define XXH_INLINE_ALL
#include <hash/xxhash.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sysexits.h>
#include <x86intrin.h>
/* TODO(jason): use static allocated array
* TODO(jason): add bucket array shrink
* TODO(juncheng): one way to increase hash table load is to use cuckoo hash
* with bucket
* */
extern int n_thread;
/* the size of bucket in bytes, used in malloc alignment */
#define N_BYTE_PER_BUCKET 64
/* the number of slots in one bucket */
#define N_SLOT_PER_BUCKET 8u
/* the number of slots expressed in number of bits 8 == 2^3 */
#define N_SLOT_PER_BUCKET_LOG2 3u
/* mask for item_info */
#define TAG_MASK 0xfff0000000000000ul
#define FREQ_MASK 0x000ff00000000000ul
#define SEG_ID_MASK 0x00000ffffff00000ul
#define OFFSET_MASK 0x00000000000ffffful
#define TAG_BIT_SHIFT 52ul
#define FREQ_BIT_SHIFT 44ul
#define SEG_ID_BIT_SHIFT 20ul
#define OFFSET_UNIT_IN_BIT 3ul /* offset is in 8-byte unit */
/* this bit indicates whether the frequency has increased in the current sec */
#define FREQ_INC_INDICATOR_MASK 0x0008000000000000ul
#define CLEAR_FREQ_SMOOTH_MASK 0xfff7fffffffffffful
/* mast for bucket info */
#define LOCK_MASK 0xff00000000000000ul
#define BUCKET_CHAIN_LEN_MASK 0x00ff000000000000ul
#define TS_MASK 0x0000ffff00000000ul /* ts in bucket info */
#define CAS_MASK 0x00000000fffffffful
#define LOCK_BIT_SHIFT 56ul
#define BUCKET_CHAIN_LEN_BIT_SHIFT 48ul
#define TS_BIT_SHIFT 32ul
/* ts from proc ts, we only need 16-bit */
#define PROC_TS_MASK 0x000000000000fffful
/* a per-bucket spin lock */
#define LOCKED 0x0100000000000000ul
#define UNLOCKED 0x0000000000000000ul
extern seg_metrics_st *seg_metrics;
static struct hash_table hash_table;
static bool hash_table_initialized = false;
static __thread __uint128_t g_lehmer64_state = 1;
#define HASHSIZE(_n) (1ULL << (_n))
#define HASHMASK(_n) (HASHSIZE(_n) - 1)
#define CAL_HV(key, klen) _get_hv_xxhash(key, klen)
/* tags is calculated in two places,
* one is extracted from hash value, the other is extracted from item info,
* we use the top 12 bits of hash value as tag and
* store it in the top 12 bits of item info,
* note that we make tag to start from 1, so when we calculate the true tag
* we perform OR with 0x0001000000000000ul */
#define GET_TAG(item_info) ((item_info) & TAG_MASK)
#define GET_FREQ(item_info) (((item_info) & FREQ_MASK) >> FREQ_BIT_SHIFT)
#define GET_SEG_ID(item_info) (((item_info) & SEG_ID_MASK) >> SEG_ID_BIT_SHIFT)
#define GET_SEG_ID_NON_DECR(item_info) (((item_info) & SEG_ID_MASK) >> SEG_ID_BIT_SHIFT)
#if defined DEBUG_MODE
#undef GET_SEG_ID
#undef GET_SEG_ID_NON_DECR
#define GET_SEG_ID_NON_DECR(item_info) (((item_info) & SEG_ID_MASK) >> SEG_ID_BIT_SHIFT)
#define GET_SEG_ID(item_info) ((((item_info) & SEG_ID_MASK) >> SEG_ID_BIT_SHIFT) % heap.max_nseg)
#endif
#define GET_OFFSET(item_info) (((item_info) & OFFSET_MASK) << OFFSET_UNIT_IN_BIT)
#define CLEAR_FREQ(item_info) ((item_info) & (~FREQ_MASK))
#define CAL_TAG_FROM_HV(hv) (((hv) & TAG_MASK) | 0x0010000000000000ul)
#define GET_BUCKET(hv) (&hash_table.table[((hv) & (hash_table.hash_mask))])
#define GET_TS(bucket_ptr) (((*(bucket_ptr)) & TS_MASK) >> TS_BIT_SHIFT)
#define GET_CAS(bucket_ptr) ((*(bucket_ptr)) & CAS_MASK)
/* calculate the number of buckets in the bucket chain */
#define GET_BUCKET_CHAIN_LEN(bucket_ptr) \
((((*(bucket_ptr)) & BUCKET_CHAIN_LEN_MASK) >> BUCKET_CHAIN_LEN_BIT_SHIFT) + 1)
#define INCR_BUCKET_CHAIN_LEN(bucket_ptr) \
((*(bucket_ptr)) += 0x0001000000000000ul)
#define CAS_SLOT(slot_ptr, expect_ptr, new_val) \
__atomic_compare_exchange_n( \
(slot_ptr), (expect_ptr), (new_val), false, \
__ATOMIC_RELEASE, __ATOMIC_RELAXED \
)
#define use_atomic_set
/* we assume little-endian here */
#define lock(bucket_ptr) \
do { \
uint8_t locked = 0; \
while (!CAS_SLOT(((uint8_t *)bucket_ptr + 7), &locked, 1)) { \
ASSERT(locked == 1); \
locked = 0; \
; \
} \
} while (0)
#define unlock(bucket_ptr) \
do { \
ASSERT(((*bucket_ptr) & LOCK_MASK) == LOCKED); \
*bucket_ptr ^= LOCKED; \
} while (0)
#define unlock_and_update_cas(bucket_ptr) \
do { \
ASSERT(((*bucket_ptr) & LOCK_MASK) != 0); \
*bucket_ptr = (*bucket_ptr + 1) ^ LOCKED; \
} while (0)
#ifdef use_atomic_set
#undef lock
#undef unlock
#undef unlock_and_update_cas
#define lock(bucket_ptr) \
do { \
while (__atomic_test_and_set( \
((uint8_t *)(bucket_ptr) + 7), __ATOMIC_RELAXED)) { \
; \
} \
} while (0)
#define unlock(bucket_ptr) \
do { \
__atomic_clear(((uint8_t *)(bucket_ptr) + 7), __ATOMIC_RELAXED); \
} while (0)
#define unlock_and_update_cas(bucket_ptr) \
do { \
*bucket_ptr += 1; \
__atomic_clear(((uint8_t *)(bucket_ptr) + 7), __ATOMIC_RELAXED); \
} while (0)
#endif
#ifdef no_lock
#undef lock
#undef unlock
#undef unlock_and_update_cas
#define lock(bucket_ptr)
#define unlock(bucket_ptr)
#define unlock_and_update_cas(bucket_ptr) ((*(bucket_ptr)) += 1)
#endif
/**
* this is placed here because it is called within bucket lock and it
* needs to parse item_info
*
*/
static inline struct item *
_info_to_item(uint64_t item_info)
{
uint64_t seg_id = GET_SEG_ID(item_info);
uint64_t offset = GET_OFFSET(item_info);
#if defined DEBUG_MODE
seg_id = seg_id % heap.max_nseg;
#endif
ASSERT(seg_id < heap.max_nseg);
ASSERT(offset < heap.seg_size);
return (struct item *) (heap.base + heap.seg_size * seg_id + offset);
}
static inline void
_item_free(uint64_t item_info, bool mark_tombstone)
{
struct item *it;
uint64_t seg_id = GET_SEG_ID(item_info);
uint64_t offset = GET_OFFSET(item_info);
it = (struct item *) (heap.base + heap.seg_size * seg_id + offset);
uint32_t sz = item_ntotal(it);
__atomic_fetch_sub(&heap.segs[seg_id].live_bytes, sz, __ATOMIC_RELAXED);
__atomic_fetch_sub(&heap.segs[seg_id].n_live_item, 1, __ATOMIC_RELAXED);
#ifdef DEBUG_MODE
__atomic_fetch_add(&heap.segs[seg_id].n_rm_item, 1, __ATOMIC_RELAXED);
__atomic_fetch_add(&heap.segs[seg_id].n_rm_bytes, sz, __ATOMIC_RELAXED);
#endif
ASSERT(__atomic_load_n(&heap.segs[seg_id].n_live_item, __ATOMIC_RELAXED) >= 0);
ASSERT(__atomic_load_n(&heap.segs[seg_id].live_bytes, __ATOMIC_RELAXED) >= 0);
// if (mark_tombstone) {
// it->deleted = true;
// }
/* let's always mark the tombstone */
it->deleted = true;
}
static inline bool
_same_item(const char *key, uint32_t klen, uint64_t item_info)
{
struct item *oit = _info_to_item(item_info);
return ((oit->klen == klen) && cc_memcmp(item_key(oit), key, klen) == 0);
}
static inline uint64_t
_build_item_info(uint64_t tag, uint64_t seg_id, uint64_t offset)
{
ASSERT(offset % 8 == 0);
uint64_t item_info = tag | (seg_id << 20u) | (offset >> 3u);
return item_info;
}
#define SET_BIT(u64, pos) ((u64) | (1ul << (pos)))
#define GET_BIT(u64, pos) ((u64) & (1ul << (pos)))
#define CHECK_BIT(u64, pos) GET_BIT(u64, pos)
#define CLEAR_BIT(u64, pos) ((u64) & (~(1ul << (pos))))
static inline uint64_t
prand(void)
{
g_lehmer64_state *= 0xda942042e4dd58b5;
return (uint64_t) g_lehmer64_state;
}
static inline uint64_t
_get_hv_xxhash(const char *key, size_t klen)
{
return XXH3_64bits(key, klen);
}
/*
* Allocate table given size
*/
static inline uint64_t *
_hashtable_alloc(uint64_t n_slot)
{
uint64_t *table =
aligned_alloc(N_BYTE_PER_BUCKET, sizeof(uint64_t) * n_slot);
if (table == NULL) {
log_crit("cannot create hash table");
exit(EX_CONFIG);
}
cc_memset(table, 0, sizeof(uint64_t) * n_slot);
#ifdef MADV_HUGEPAGE
/* USE_HUGEPAGE */
madvise(table, sizeof(uint64_t) * n_slot, MADV_HUGEPAGE);
#endif
return table;
}
void
hashtable_setup(uint32_t hash_power)
{
ASSERT(hash_power > 0);
if (hash_table_initialized) {
log_warn("hash table has been initialized");
hashtable_teardown();
}
/* init members */
hash_table.hash_power = hash_power;
uint64_t n_slot = HASHSIZE(hash_power);
/* N_SLOT_PER_BUCKET slots are in one bucket, so hash_mask last
* N_SLOT_PER_BUCKET_LOG2 bits should be zero */
hash_table.hash_mask =
(n_slot - 1) & (0xfffffffffffffffful << N_SLOT_PER_BUCKET_LOG2);
/* alloc table */
hash_table.table = _hashtable_alloc(n_slot);
hash_table_initialized = true;
log_info("create hash table of %" PRIu64 " entries %" PRIu64 " buckets",
n_slot, n_slot >> N_SLOT_PER_BUCKET_LOG2);
}
void
hashtable_teardown(void)
{
if (!hash_table_initialized) {
log_warn("hash table is not initialized");
return;
}
cc_free(hash_table.table);
hash_table.table = NULL;
hash_table_initialized = false;
}
/**
* insert an item into hash table
* insert has two steps, insert and possibly delete
* insert and delete must be completed in the same pass (atomic or locked),
* otherwise it cannot guarantee correctness
*
* procedure:
* scan through all slots of the head bucket,
* 1. if we found the item, replace with new item
* 2. if we found an empty slot first, we store new item in empty slots and
* 2-1. remove the old item if the old item is in the head bucket,
* 2-2. if we do not find it in the head bucket, we stop searching, and
* let the clean up to eviction time
* 3. if old item is not found in the first bucket nor empty bucket,
* we continue to search
*/
void
hashtable_put(struct item *it, const uint64_t seg_id, const uint64_t offset)
{
const char *key = item_key(it);
const uint32_t klen = item_nkey(it);
uint64_t hv = CAL_HV(key, klen);
uint64_t tag = CAL_TAG_FROM_HV(hv);
uint64_t *head_bkt = GET_BUCKET(hv);
uint64_t *bkt = head_bkt;
INCR(seg_metrics, hash_insert);
/* 12-bit tag, 8-bit counter,
* 24-bit seg id, 20-bit offset (in the unit of 8-byte) */
uint64_t item_info, insert_item_info;
insert_item_info = _build_item_info(tag, seg_id, offset);
lock(head_bkt);
int bkt_chain_len = GET_BUCKET_CHAIN_LEN(head_bkt);
int n_item_slot;
do {
/* the last slot will be a pointer to the next
* bucket if there is next bucket */
n_item_slot = bkt_chain_len > 1 ?
N_SLOT_PER_BUCKET - 1 :
N_SLOT_PER_BUCKET;
for (int i = 0; i < n_item_slot; i++) {
if (bkt == head_bkt && i == 0) {
/* the first slot of the head bucket is bucket into */
continue;
}
item_info = __atomic_load_n(&bkt[i], __ATOMIC_RELAXED);
if (GET_TAG(item_info) != tag) {
if (insert_item_info != 0 && item_info == 0) {
/* store item info in the first empty slot */
__atomic_store_n(&bkt[i], insert_item_info, __ATOMIC_RELAXED);
insert_item_info = 0;
}
continue;
}
/* a potential hit */
if (!_same_item(key, klen, item_info)) {
INCR(seg_metrics, hash_tag_collision);
continue;
}
/* found the item, now atomic update (or delete if already inserted)
* read and write 8-byte on x86 is always atomic */
__atomic_store_n(&bkt[i], insert_item_info, __ATOMIC_RELAXED);
insert_item_info = 0;
/* now mark the old item as deleted, update stat */
_item_free(item_info, false);
/* there could be pointers to stale objects later in the bucket,
* we leave the clean up to future eviction time */
goto finish;
}
if (insert_item_info == 0) {
/* item has been inserted, do not check next bucket to delete
* old item, the info will be gc when item is evicted */
goto finish;
}
/* if there are overflown buckets, we continue to check */
bkt_chain_len -= 1;
if (bkt_chain_len > 0) {
bkt = (uint64_t *) (bkt[N_SLOT_PER_BUCKET - 1]);
}
} while (bkt_chain_len > 0);
/* we have searched every bucket, but have not found the old item
* nor inserted new item - so we need to allocate a new array,
* this is very rare */
INCR(seg_metrics, hash_bucket_alloc);
uint64_t *new_bkt = cc_zalloc(sizeof(uint64_t) * N_SLOT_PER_BUCKET);
/* move the last item from last bucket to new bucket */
new_bkt[0] = bkt[N_SLOT_PER_BUCKET - 1];
new_bkt[1] = insert_item_info;
insert_item_info = 0;
__atomic_store_n(&bkt[N_SLOT_PER_BUCKET - 1], (uint64_t) new_bkt, __ATOMIC_RELAXED);
INCR_BUCKET_CHAIN_LEN(head_bkt);
log_verb("increase bucket chain to len %d", GET_BUCKET_CHAIN_LEN(head_bkt));
/* this is for debugging, chain length in production should not so large */
ASSERT(GET_BUCKET_CHAIN_LEN(head_bkt) <= 16);
finish:
ASSERT(insert_item_info == 0);
unlock_and_update_cas(head_bkt);
}
bool
hashtable_delete(const struct bstring *key)
{
INCR(seg_metrics, hash_remove);
bool deleted = false;
uint64_t item_info;
uint64_t hv = CAL_HV(key->data, key->len);
uint64_t tag = CAL_TAG_FROM_HV(hv);
uint64_t *head_bkt = GET_BUCKET(hv);
uint64_t *bkt = head_bkt;
lock(head_bkt);
int bkt_chain_len = GET_BUCKET_CHAIN_LEN(head_bkt) - 1;
int n_item_slot;
do {
n_item_slot =
bkt_chain_len > 0 ?
N_SLOT_PER_BUCKET - 1 :
N_SLOT_PER_BUCKET;
for (int i = 0; i < n_item_slot; i++) {
if (bkt == head_bkt && i == 0) {
continue;
}
item_info = __atomic_load_n(&bkt[i], __ATOMIC_RELAXED);
if (GET_TAG(item_info) != tag) {
continue;
}
/* a potential hit */
if (!_same_item(key->data, key->len, item_info)) {
INCR(seg_metrics, hash_tag_collision);
continue;
}
/* found the item, now delete */
/* if this is the first and most up-to-date hash table entry
* we need to mark tombstone, this is for recovery */
_item_free(item_info, !deleted);
__atomic_store_n(&bkt[i], 0, __ATOMIC_RELAXED);
deleted = true;
}
bkt_chain_len -= 1;
bkt = (uint64_t *) (bkt[N_SLOT_PER_BUCKET - 1]);
} while (bkt_chain_len >= 0);
unlock(head_bkt);
return deleted;
}
/*
* the difference between delete and evict is that
* delete needs to mark tombstone on the most recent object,
* evict: if the item being evicted
* is the most recent version,
* evict needs to mark tombstone on the second most recent object
* is not the up-to-date version
* evict does not need to mark tombstone, and should not remove
* the up-to-date version
*
* The decision on tombstone is used for recovery, normal usage does not need
* to mark tombstone, while tombstone is used to find out which an object is
* an up-to-date object
*
*/
bool
hashtable_evict(const char *oit_key, const uint32_t oit_klen,
const uint64_t seg_id, const uint64_t offset)
{
INCR(seg_metrics, hash_evict);
uint64_t hv = CAL_HV(oit_key, oit_klen);
uint64_t tag = CAL_TAG_FROM_HV(hv);
uint64_t *head_bkt = GET_BUCKET(hv);
uint64_t *bkt = head_bkt;
uint64_t item_info;
uint64_t oit_info = _build_item_info(tag, seg_id, offset);
bool first_match = true, item_outdated = true, found_oit = false;
/* this is necessary, need more thoughts if we need to use
* opportunistic concurrency control and atomics, see hashtable_relink_it
* basically we need to make sure the slot we store into has not been
* updated since we check */
lock(head_bkt);
int bkt_chain_len = GET_BUCKET_CHAIN_LEN(head_bkt) - 1;
int n_item_slot;
do {
n_item_slot =
bkt_chain_len > 0 ?
N_SLOT_PER_BUCKET - 1 :
N_SLOT_PER_BUCKET;
for (int i = 0; i < n_item_slot; i++) {
if (bkt == head_bkt && i == 0) {
continue;
}
item_info = CLEAR_FREQ(bkt[i]);
if (GET_TAG(item_info) != tag) {
continue;
}
/* a potential hit */
if (!_same_item(oit_key, oit_klen, item_info)) {
INCR(seg_metrics, hash_tag_collision);
continue;
}
if (first_match) {
first_match = false;
if (oit_info == item_info) {
/* item to evict is up-to-date */
_item_free(item_info, false);
__atomic_store_n(&bkt[i], 0, __ATOMIC_RELAXED);
item_outdated = false;
found_oit = true;
}
} else {
/* not first match, delete hash table entry,
* mark tombstone only when oit is the most up-to-date entry */
if (item_info == oit_info) {
ASSERT(found_oit == false);
found_oit = true;
}
_item_free(bkt[i], !item_outdated);
__atomic_store_n(&bkt[i], 0, __ATOMIC_RELAXED);
}
}
bkt_chain_len -= 1;
bkt = (uint64_t *) (bkt[N_SLOT_PER_BUCKET - 1]);
} while (bkt_chain_len >= 0);
unlock(head_bkt);
return found_oit;
}
#ifdef STORE_FREQ_IN_HASHTABLE
struct item *
hashtable_get(const char *key, const uint32_t klen,
int32_t *seg_id,
uint64_t *cas)
{
INCR(seg_metrics, hash_lookup);
uint64_t hv = CAL_HV(key, klen);
uint64_t tag = CAL_TAG_FROM_HV(hv);
uint64_t *first_bkt = GET_BUCKET(hv);
uint64_t *bkt = first_bkt;
uint64_t offset;
struct item *it;
uint64_t item_info;
int bkt_chain_len = GET_BUCKET_CHAIN_LEN(first_bkt) - 1;
int n_item_slot;
uint64_t curr_ts = ((uint64_t) time_proc_sec()) & PROC_TS_MASK;
if (curr_ts != GET_TS(first_bkt)) {
/* clear the indicator of all items in the bucket that
* the frequency has increased in curr sec */
lock(first_bkt);
if (curr_ts != GET_TS(first_bkt)) {
/* update ts */
*first_bkt = ((*first_bkt) & (~TS_MASK)) | (curr_ts << TS_BIT_SHIFT);
do {
n_item_slot = bkt_chain_len > 0 ?
N_SLOT_PER_BUCKET - 1 :
N_SLOT_PER_BUCKET;
for (int i = 0; i < n_item_slot; i++) {
if (bkt == first_bkt && i == 0) {
continue;
}
/* clear the indicator bit */
__atomic_fetch_and(&bkt[i], CLEAR_FREQ_SMOOTH_MASK, __ATOMIC_RELAXED);
}
bkt_chain_len -= 1;
bkt = (uint64_t *) (bkt[N_SLOT_PER_BUCKET - 1]);
} while (bkt_chain_len >= 0);
}
unlock(first_bkt);
/* reset bucket and bucket chain length */
bkt = first_bkt;
bkt_chain_len = GET_BUCKET_CHAIN_LEN(first_bkt) - 1;
}
lock(first_bkt);
/* try to find the item in the hash table */
do {
n_item_slot = bkt_chain_len > 0 ?
N_SLOT_PER_BUCKET - 1 :
N_SLOT_PER_BUCKET;
for (int i = 0; i < n_item_slot; i++) {
if (bkt == first_bkt && i == 0) {
continue;
}
item_info = __atomic_load_n(&bkt[i], __ATOMIC_RELAXED);
if (GET_TAG(item_info) != tag) {
continue;
}
/* a potential hit */
if (!_same_item(key, klen, item_info)) {
INCR(seg_metrics, hash_tag_collision);
continue;
}
if (cas) {
*cas = GET_CAS(first_bkt);
}
#if defined DEBUG_MODE
*seg_id = GET_SEG_ID_NON_DECR(item_info);
ASSERT(heap.segs[GET_SEG_ID(item_info)].seg_id_non_decr == *seg_id);
#else
*seg_id = GET_SEG_ID(item_info);
#endif
struct seg *seg = &heap.segs[GET_SEG_ID(item_info)];
int ref_cnt = __atomic_add_fetch(&seg->r_refcount, 1, __ATOMIC_RELAXED);
ASSERT(ref_cnt <= n_thread);
if (!seg_is_accessible(GET_SEG_ID(item_info)) ||
__atomic_load_n(&bkt[i], __ATOMIC_RELAXED) != item_info) {
/* not accessible: it will be removed by other threads,
* item_info change: updated/deleted/accessed by other thread */
__atomic_sub_fetch(&seg->r_refcount, 1, __ATOMIC_RELAXED);
unlock(first_bkt);
return NULL;
}
offset = GET_OFFSET(item_info);
it = (struct item *) (heap.base + heap.seg_size * GET_SEG_ID(item_info) + offset);
/* item found, try to update the frequency */
uint64_t freq = GET_FREQ(item_info);
if (freq < 127) {
/* counter caps at 127 */
if (freq <= 16 || prand() % freq == 0) {
/* increase frequency by 1
* if freq <= 16 or with prob 1/freq */
freq = ((freq + 1) | 0x80ul) << FREQ_BIT_SHIFT;
}
else {
/* we do not increase frequency, but mark that
* we have already tried at current sec */
freq = (freq | 0x80ul) << FREQ_BIT_SHIFT;
}
uint64_t new_val = (item_info & (~FREQ_MASK)) | freq;
/* we can also use atomics here, but it comes with caveat,
* if we use atomic, then we cannot use compare_exchange in
* other func because the compare will fail due to freq incr */
// lock(first_bkt);
__atomic_compare_exchange_n(&bkt[i], &item_info, new_val, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED);
// unlock(first_bkt);
}
/* done frequency update section */
unlock(first_bkt);
return it;
}
bkt_chain_len -= 1;
bkt = (uint64_t *) (bkt[N_SLOT_PER_BUCKET - 1]);
} while (bkt_chain_len >= 0);
unlock(first_bkt);
return NULL;
}
#else
struct item *
hashtable_get(const char *key, const uint32_t klen,
int32_t *seg_id,
uint64_t *cas)
{
INCR(seg_metrics, hash_lookup);
uint64_t hv = CAL_HV(key, klen);
uint64_t tag = CAL_TAG_FROM_HV(hv);
uint64_t *first_bkt = GET_BUCKET(hv);
uint64_t *bkt = first_bkt;
struct item *it;
int i;
uint64_t item_info;
static uint64_t scan_len_sum = 0;
static uint64_t scan_cnt = 0;
// scan_cnt += 1;
// if (scan_cnt % 100000000 == 0) {
// printf("%.2lf\n", (double) scan_len_sum / scan_cnt);
// }
lock(first_bkt);
int bkt_chain_len = GET_BUCKET_CHAIN_LEN(first_bkt) - 1;
int n_item_slot;
/* try to find the item in the hash table */
do {
n_item_slot = bkt_chain_len > 0 ?
N_SLOT_PER_BUCKET - 1 :
N_SLOT_PER_BUCKET;
for (i = 0; i < n_item_slot; i++) {
if (bkt == first_bkt && i == 0) {
continue;
}
item_info = bkt[i];
if (GET_TAG(item_info) != tag) {
continue;
}
/* a potential hit */
if (!_same_item(key, klen, item_info)) {
INCR(seg_metrics, hash_tag_collision);
continue;
}
if (cas) {
*cas = GET_CAS(first_bkt);
}
#if defined DEBUG_MODE
*seg_id = GET_SEG_ID_NON_DECR(item_info);
ASSERT(heap.segs[GET_SEG_ID(item_info)].seg_id_non_decr == *seg_id);
#else
*seg_id = GET_SEG_ID(item_info);
#endif
struct seg *seg = &heap.segs[GET_SEG_ID(item_info)];
int ref_cnt = __atomic_add_fetch(&seg->r_refcount, 1, __ATOMIC_RELAXED);
ASSERT(ref_cnt <= n_thread);
if (!seg_is_accessible(GET_SEG_ID(item_info)) ||
__atomic_load_n(&bkt[i], __ATOMIC_RELAXED) != item_info) {
/* not accessible: it will be removed by other threads,
* item_info change: updated/deleted/accessed by other thread */
__atomic_sub_fetch(&seg->r_refcount, 1, __ATOMIC_RELAXED);
scan_len_sum += i;
unlock(first_bkt);
return NULL;
}
it = (struct item *) (heap.base + heap.seg_size *
GET_SEG_ID(item_info) + GET_OFFSET(item_info));
scan_len_sum += i;
unlock(first_bkt);
return it;
}
bkt_chain_len -= 1;
bkt = (uint64_t *) (bkt[N_SLOT_PER_BUCKET - 1]);
scan_len_sum += 8;
} while (bkt_chain_len >= 0);
scan_len_sum += i;
unlock(first_bkt);
return NULL;
}
#endif
/**
* get but not increase item frequency
*
**/
struct item *
hashtable_get_no_freq_incr(const char *key, const uint32_t klen,
int32_t *seg_id,
uint64_t *cas)
{
uint64_t hv = CAL_HV(key, klen);
uint64_t tag = CAL_TAG_FROM_HV(hv);
uint64_t *first_bkt = GET_BUCKET(hv);
uint64_t *bkt = first_bkt;
uint64_t offset;
struct item *it;
/* 16-bit tag, 28-bit seg id, 20-bit offset (in the unit of 8-byte) */
uint64_t item_info;
int bkt_chain_len = GET_BUCKET_CHAIN_LEN(first_bkt) - 1;
int n_item_slot;
do {
n_item_slot = bkt_chain_len > 0 ?
N_SLOT_PER_BUCKET - 1 :
N_SLOT_PER_BUCKET;
for (int i = 0; i < n_item_slot; i++) {
if (bkt == first_bkt && i == 0) {
continue;
}
item_info = __atomic_load_n(&bkt[i], __ATOMIC_RELAXED);
if (GET_TAG(item_info) != tag) {
continue;
}
/* a potential hit */
if (!_same_item(key, klen, item_info)) {
INCR(seg_metrics, hash_tag_collision);
continue;
}
if (cas) {
*cas = GET_CAS(first_bkt);
}
*seg_id = GET_SEG_ID(item_info);
offset = GET_OFFSET(item_info);
it = (struct item *) (heap.base + heap.seg_size * (*seg_id)
+ offset);
return it;
}
bkt_chain_len -= 1;
bkt = (uint64_t *) (bkt[N_SLOT_PER_BUCKET - 1]);
} while (bkt_chain_len >= 0);
return NULL;
}
/**
* get item frequency
*
**/
int
hashtable_get_it_freq(const char *it_key, const uint32_t it_klen,
const uint64_t seg_id, const uint64_t offset)
{
uint64_t hv = CAL_HV(it_key, it_klen);
uint64_t tag = CAL_TAG_FROM_HV(hv);
uint64_t *first_bkt = GET_BUCKET(hv);
uint64_t *curr_bkt = first_bkt;
uint64_t curr_item_info;
uint64_t item_info_to_find = _build_item_info(tag, seg_id, offset);
int freq = 0;
int bkt_chain_len = GET_BUCKET_CHAIN_LEN(first_bkt) - 1;
int n_item_slot;
do {
n_item_slot = bkt_chain_len > 0 ?
N_SLOT_PER_BUCKET - 1 :
N_SLOT_PER_BUCKET;
for (int i = 0; i < n_item_slot; i++) {
if (curr_bkt == first_bkt && i == 0) {
continue;
}
curr_item_info = __atomic_load_n(&curr_bkt[i], __ATOMIC_RELAXED);
if (GET_TAG(curr_item_info) != tag) {
continue;
}
curr_item_info = CLEAR_FREQ(curr_item_info);
if (curr_item_info == item_info_to_find) {
curr_item_info = __atomic_load_n(&curr_bkt[i], __ATOMIC_RELAXED);
freq = GET_FREQ(curr_item_info) & 0x7Ful;
return freq;
}
/* a potential hit */
if (!_same_item(it_key, it_klen, curr_item_info)) {
INCR(seg_metrics, hash_tag_collision);
continue;
}
/* the item to find is outdated */
return 0;
}
bkt_chain_len -= 1;
curr_bkt = (uint64_t *) (curr_bkt[N_SLOT_PER_BUCKET - 1]);
} while (bkt_chain_len >= 0);
return -1;
}
/*
* relink is used when the item is moved from one segment to another
*
* a few caveats
* item being relinked could be outdated, in which case we should not relink
*
* TODO(jason): it might be better not clear those old entries?
*/
bool
hashtable_relink_it(const char *oit_key, const uint32_t oit_klen,
const uint64_t old_seg_id, const uint64_t old_offset,
const uint64_t new_seg_id, const uint64_t new_offset)
{
INCR(seg_metrics, hash_relink);
uint64_t hv = CAL_HV(oit_key, oit_klen);
uint64_t tag = CAL_TAG_FROM_HV(hv);
uint64_t *first_bkt = GET_BUCKET(hv);
uint64_t *curr_bkt = first_bkt;
uint64_t item_info, item_info_with_freq;
bool item_outdated = true, first_match = true;
uint64_t oit_info = _build_item_info(tag, old_seg_id, old_offset);
uint64_t nit_info = _build_item_info(tag, new_seg_id, new_offset);
lock(first_bkt);
int bkt_chain_len = GET_BUCKET_CHAIN_LEN(first_bkt) - 1;
int n_item_slot;
do {
n_item_slot = bkt_chain_len > 0 ?
N_SLOT_PER_BUCKET - 1 :
N_SLOT_PER_BUCKET;
for (int i = 0; i < n_item_slot; i++) {
if (curr_bkt == first_bkt && i == 0) {
continue;
}
item_info_with_freq = __atomic_load_n(&curr_bkt[i], __ATOMIC_RELAXED);
item_info = CLEAR_FREQ(item_info_with_freq);
if (GET_TAG(item_info) != tag) {
continue;
}
/* a potential hit */
if (!_same_item(oit_key, oit_klen, item_info)) {
INCR(seg_metrics, hash_tag_collision);
continue;
}
if (first_match) {
if (oit_info == item_info) {
/* item is not updated, but do notice that if we do not use
* locking in hashtable_get when incr frequency, we could
* have item_info change due to frequency */
// lock(first_bkt);
if (CLEAR_FREQ(__atomic_load_n(
&curr_bkt[i], __ATOMIC_RELAXED)) == oit_info) {
__atomic_store_n(&curr_bkt[i], nit_info, __ATOMIC_RELAXED);
item_outdated = false;
_item_free(oit_info, false);
}
// unlock(first_bkt);
}
first_match = false;
} else {
/* not first match, delete */
_item_free(curr_bkt[i], false);
__atomic_store_n(&curr_bkt[i], 0, __ATOMIC_RELAXED);
}
}