YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/dynamic_bloom.cc
Line
Count
Source (jump to first uncovered line)
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: rocksdb::DynamicBloom::DynamicBloom(rocksdb::Allocator*, unsigned int, unsigned int, unsigned int, unsigned int (*)(yb::Slice const&), unsigned long, rocksdb::Logger*)
rocksdb::DynamicBloom::DynamicBloom(rocksdb::Allocator*, unsigned int, unsigned int, unsigned int, unsigned int (*)(yb::Slice const&), unsigned long, rocksdb::Logger*)
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.28k
      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.83k
                                Logger* logger) {
72
2.83k
  kTotalBits = (locality > 0) ? 
GetTotalBitsForLocality(total_bits)40
73
2.83k
                              : 
(total_bits + 7) / 8 * 82.79k
;
74
2.83k
  kNumBlocks = (locality > 0) ? 
(kTotalBits / (40
CACHE_LINE_SIZE40
* 8)) :
02.79k
;
75
76
2.83k
  assert(kNumBlocks > 0 || kTotalBits > 0);
77
0
  assert(kNumProbes > 0);
78
79
0
  uint32_t sz = kTotalBits / 8;
80
2.83k
  if (kNumBlocks > 0) {
81
40
    sz += CACHE_LINE_SIZE - 1;
82
40
  }
83
2.83k
  assert(allocator);
84
85
0
  char* raw = allocator->AllocateAligned(sz, huge_page_tlb_size, logger);
86
2.83k
  memset(raw, 0, sz);
87
2.83k
  auto cache_line_offset = reinterpret_cast<uintptr_t>(raw) % CACHE_LINE_SIZE;
88
2.83k
  if (kNumBlocks > 0 && 
cache_line_offset > 040
) {
89
31
    raw += CACHE_LINE_SIZE - cache_line_offset;
90
31
  }
91
2.83k
  data_ = reinterpret_cast<std::atomic<uint8_t>*>(raw);
92
2.83k
}
93
94
}  // namespace rocksdb