/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/dynamic_bloom.cc
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | |
21 | | #include "yb/rocksdb/util/dynamic_bloom.h" |
22 | | |
23 | | #include "yb/rocksdb/util/allocator.h" |
24 | | #include "yb/rocksdb/util/hash.h" |
25 | | |
26 | | namespace rocksdb { |
27 | | |
28 | | namespace { |
29 | | |
30 | 40 | uint32_t GetTotalBitsForLocality(uint32_t total_bits) { |
31 | 40 | uint32_t num_blocks = |
32 | 40 | (total_bits + CACHE_LINE_SIZE * 8 - 1) / (CACHE_LINE_SIZE * 8); |
33 | | |
34 | | // Make num_blocks an odd number to make sure more bits are involved |
35 | | // when determining which block. |
36 | 40 | if (num_blocks % 2 == 0) { |
37 | 22 | num_blocks++; |
38 | 22 | } |
39 | | |
40 | 40 | return num_blocks * (CACHE_LINE_SIZE * 8); |
41 | 40 | } |
42 | | |
43 | | } // namespace |
44 | | |
45 | | DynamicBloom::DynamicBloom(Allocator* allocator, uint32_t total_bits, |
46 | | uint32_t locality, uint32_t num_probes, |
47 | | uint32_t (*hash_func)(const Slice& key), |
48 | | size_t huge_page_tlb_size, |
49 | | Logger* logger) |
50 | 129 | : DynamicBloom(num_probes, hash_func) { |
51 | 129 | SetTotalBits(allocator, total_bits, locality, huge_page_tlb_size, logger); |
52 | 129 | } Unexecuted instantiation: _ZN7rocksdb12DynamicBloomC2EPNS_9AllocatorEjjjPFjRKN2yb5SliceEEmPNS_6LoggerE _ZN7rocksdb12DynamicBloomC1EPNS_9AllocatorEjjjPFjRKN2yb5SliceEEmPNS_6LoggerE Line | Count | Source | 50 | 129 | : DynamicBloom(num_probes, hash_func) { | 51 | 129 | SetTotalBits(allocator, total_bits, locality, huge_page_tlb_size, logger); | 52 | 129 | } |
|
53 | | |
54 | | DynamicBloom::DynamicBloom(uint32_t num_probes, |
55 | | uint32_t (*hash_func)(const Slice& key)) |
56 | | : kTotalBits(0), |
57 | | kNumBlocks(0), |
58 | | kNumProbes(num_probes), |
59 | 5.30k | hash_func_(hash_func == nullptr ? &BloomHash : hash_func) {} |
60 | | |
61 | | void DynamicBloom::SetRawData(unsigned char* raw_data, uint32_t total_bits, |
62 | 64 | uint32_t num_blocks) { |
63 | 64 | data_ = reinterpret_cast<std::atomic<uint8_t>*>(raw_data); |
64 | 64 | kTotalBits = total_bits; |
65 | 64 | kNumBlocks = num_blocks; |
66 | 64 | } |
67 | | |
68 | | void DynamicBloom::SetTotalBits(Allocator* allocator, |
69 | | uint32_t total_bits, uint32_t locality, |
70 | | size_t huge_page_tlb_size, |
71 | 2.84k | Logger* logger) { |
72 | 40 | kTotalBits = (locality > 0) ? GetTotalBitsForLocality(total_bits) |
73 | 2.80k | : (total_bits + 7) / 8 * 8; |
74 | 2.80k | kNumBlocks = (locality > 0) ? (kTotalBits / (CACHE_LINE_SIZE * 8)) : 0; |
75 | | |
76 | 2.84k | assert(kNumBlocks > 0 || kTotalBits > 0); |
77 | 2.84k | assert(kNumProbes > 0); |
78 | | |
79 | 2.84k | uint32_t sz = kTotalBits / 8; |
80 | 2.84k | if (kNumBlocks > 0) { |
81 | 40 | sz += CACHE_LINE_SIZE - 1; |
82 | 40 | } |
83 | 2.84k | assert(allocator); |
84 | | |
85 | 2.84k | char* raw = allocator->AllocateAligned(sz, huge_page_tlb_size, logger); |
86 | 2.84k | memset(raw, 0, sz); |
87 | 2.84k | auto cache_line_offset = reinterpret_cast<uintptr_t>(raw) % CACHE_LINE_SIZE; |
88 | 2.84k | if (kNumBlocks > 0 && cache_line_offset > 0) { |
89 | 3 | raw += CACHE_LINE_SIZE - cache_line_offset; |
90 | 3 | } |
91 | 2.84k | data_ = reinterpret_cast<std::atomic<uint8_t>*>(raw); |
92 | 2.84k | } |
93 | | |
94 | | } // namespace rocksdb |