]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
2 | // This source code is licensed under both the GPLv2 (found in the |
3 | // COPYING file in the root directory) and Apache 2.0 License | |
4 | // (found in the LICENSE.Apache file in the root directory). | |
7c673cae FG |
5 | |
6 | #include "dynamic_bloom.h" | |
7 | ||
8 | #include <algorithm> | |
9 | ||
10 | #include "port/port.h" | |
11 | #include "rocksdb/slice.h" | |
12 | #include "util/allocator.h" | |
13 | #include "util/hash.h" | |
14 | ||
15 | namespace rocksdb { | |
16 | ||
17 | namespace { | |
18 | ||
19 | uint32_t GetTotalBitsForLocality(uint32_t total_bits) { | |
20 | uint32_t num_blocks = | |
21 | (total_bits + CACHE_LINE_SIZE * 8 - 1) / (CACHE_LINE_SIZE * 8); | |
22 | ||
23 | // Make num_blocks an odd number to make sure more bits are involved | |
24 | // when determining which block. | |
25 | if (num_blocks % 2 == 0) { | |
26 | num_blocks++; | |
27 | } | |
28 | ||
29 | return num_blocks * (CACHE_LINE_SIZE * 8); | |
30 | } | |
31 | } | |
32 | ||
33 | DynamicBloom::DynamicBloom(Allocator* allocator, uint32_t total_bits, | |
34 | uint32_t locality, uint32_t num_probes, | |
494da23a TL |
35 | size_t huge_page_tlb_size, Logger* logger) |
36 | : DynamicBloom(num_probes) { | |
7c673cae FG |
37 | SetTotalBits(allocator, total_bits, locality, huge_page_tlb_size, logger); |
38 | } | |
39 | ||
494da23a TL |
40 | DynamicBloom::DynamicBloom(uint32_t num_probes) |
41 | : kTotalBits(0), kNumBlocks(0), kNumProbes(num_probes), data_(nullptr) {} | |
7c673cae FG |
42 | |
43 | void DynamicBloom::SetRawData(unsigned char* raw_data, uint32_t total_bits, | |
44 | uint32_t num_blocks) { | |
45 | data_ = reinterpret_cast<std::atomic<uint8_t>*>(raw_data); | |
46 | kTotalBits = total_bits; | |
47 | kNumBlocks = num_blocks; | |
48 | } | |
49 | ||
50 | void DynamicBloom::SetTotalBits(Allocator* allocator, | |
51 | uint32_t total_bits, uint32_t locality, | |
52 | size_t huge_page_tlb_size, | |
53 | Logger* logger) { | |
54 | kTotalBits = (locality > 0) ? GetTotalBitsForLocality(total_bits) | |
55 | : (total_bits + 7) / 8 * 8; | |
56 | kNumBlocks = (locality > 0) ? (kTotalBits / (CACHE_LINE_SIZE * 8)) : 0; | |
57 | ||
58 | assert(kNumBlocks > 0 || kTotalBits > 0); | |
59 | assert(kNumProbes > 0); | |
60 | ||
61 | uint32_t sz = kTotalBits / 8; | |
62 | if (kNumBlocks > 0) { | |
63 | sz += CACHE_LINE_SIZE - 1; | |
64 | } | |
65 | assert(allocator); | |
66 | ||
67 | char* raw = allocator->AllocateAligned(sz, huge_page_tlb_size, logger); | |
68 | memset(raw, 0, sz); | |
69 | auto cache_line_offset = reinterpret_cast<uintptr_t>(raw) % CACHE_LINE_SIZE; | |
70 | if (kNumBlocks > 0 && cache_line_offset > 0) { | |
71 | raw += CACHE_LINE_SIZE - cache_line_offset; | |
72 | } | |
73 | data_ = reinterpret_cast<std::atomic<uint8_t>*>(raw); | |
74 | } | |
75 | ||
76 | } // rocksdb |