2013-11-27 22:27:02 +00:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
|
|
|
#include "dynamic_bloom.h"
|
|
|
|
|
2014-03-28 16:21:20 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "port/port.h"
|
2013-11-27 22:27:02 +00:00
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "util/hash.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
namespace {
|
2014-06-02 23:52:29 +00:00
|
|
|
|
2014-06-09 19:30:19 +00:00
|
|
|
uint32_t GetTotalBitsForLocality(uint32_t total_bits) {
|
|
|
|
uint32_t num_blocks =
|
|
|
|
(total_bits + CACHE_LINE_SIZE * 8 - 1) / (CACHE_LINE_SIZE * 8);
|
|
|
|
|
2014-06-02 23:52:29 +00:00
|
|
|
// Make num_blocks an odd number to make sure more bits are involved
|
|
|
|
// when determining which block.
|
|
|
|
if (num_blocks % 2 == 0) {
|
|
|
|
num_blocks++;
|
|
|
|
}
|
2014-06-09 19:30:19 +00:00
|
|
|
|
|
|
|
return num_blocks * (CACHE_LINE_SIZE * 8);
|
2014-06-02 23:52:29 +00:00
|
|
|
}
|
2013-11-27 22:27:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-30 22:54:31 +00:00
|
|
|
DynamicBloom::DynamicBloom(Arena* arena, uint32_t total_bits, uint32_t locality,
|
2014-03-28 16:21:20 +00:00
|
|
|
uint32_t num_probes,
|
2014-05-04 20:55:53 +00:00
|
|
|
uint32_t (*hash_func)(const Slice& key),
|
2014-06-30 22:54:31 +00:00
|
|
|
size_t huge_page_tlb_size,
|
|
|
|
Logger* logger)
|
2014-06-09 19:30:19 +00:00
|
|
|
: DynamicBloom(num_probes, hash_func) {
|
2014-06-30 22:54:31 +00:00
|
|
|
SetTotalBits(arena, total_bits, locality, huge_page_tlb_size, logger);
|
2014-06-09 19:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicBloom::DynamicBloom(uint32_t num_probes,
|
|
|
|
uint32_t (*hash_func)(const Slice& key))
|
|
|
|
: kTotalBits(0),
|
|
|
|
kNumBlocks(0),
|
2014-05-04 20:55:53 +00:00
|
|
|
kNumProbes(num_probes),
|
2014-06-09 19:30:19 +00:00
|
|
|
hash_func_(hash_func == nullptr ? &BloomHash : hash_func) {}
|
|
|
|
|
2014-07-18 23:58:13 +00:00
|
|
|
void DynamicBloom::SetRawData(unsigned char* raw_data, uint32_t total_bits,
|
|
|
|
uint32_t num_blocks) {
|
|
|
|
data_ = raw_data;
|
|
|
|
kTotalBits = total_bits;
|
|
|
|
kNumBlocks = num_blocks;
|
|
|
|
}
|
|
|
|
|
2014-06-30 22:54:31 +00:00
|
|
|
void DynamicBloom::SetTotalBits(Arena* arena,
|
|
|
|
uint32_t total_bits, uint32_t locality,
|
|
|
|
size_t huge_page_tlb_size,
|
|
|
|
Logger* logger) {
|
2014-06-09 19:30:19 +00:00
|
|
|
kTotalBits = (locality > 0) ? GetTotalBitsForLocality(total_bits)
|
|
|
|
: (total_bits + 7) / 8 * 8;
|
|
|
|
kNumBlocks = (locality > 0) ? (kTotalBits / (CACHE_LINE_SIZE * 8)) : 0;
|
|
|
|
|
2014-06-02 23:52:29 +00:00
|
|
|
assert(kNumBlocks > 0 || kTotalBits > 0);
|
2014-01-15 05:30:13 +00:00
|
|
|
assert(kNumProbes > 0);
|
2013-11-27 22:27:02 +00:00
|
|
|
|
2014-03-28 16:21:20 +00:00
|
|
|
uint32_t sz = kTotalBits / 8;
|
2014-06-02 23:52:29 +00:00
|
|
|
if (kNumBlocks > 0) {
|
2014-03-28 16:21:20 +00:00
|
|
|
sz += CACHE_LINE_SIZE - 1;
|
|
|
|
}
|
2014-06-30 22:54:31 +00:00
|
|
|
assert(arena);
|
2014-05-04 20:55:53 +00:00
|
|
|
raw_ = reinterpret_cast<unsigned char*>(
|
2014-06-30 22:54:31 +00:00
|
|
|
arena->AllocateAligned(sz, huge_page_tlb_size, logger));
|
2014-05-04 20:55:53 +00:00
|
|
|
memset(raw_, 0, sz);
|
2014-06-02 23:52:29 +00:00
|
|
|
if (kNumBlocks > 0 && (reinterpret_cast<uint64_t>(raw_) % CACHE_LINE_SIZE)) {
|
2014-03-28 16:21:20 +00:00
|
|
|
data_ = raw_ + CACHE_LINE_SIZE -
|
2014-06-02 23:52:29 +00:00
|
|
|
reinterpret_cast<uint64_t>(raw_) % CACHE_LINE_SIZE;
|
2014-03-28 16:21:20 +00:00
|
|
|
} else {
|
|
|
|
data_ = raw_;
|
|
|
|
}
|
2013-11-27 22:27:02 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 05:30:13 +00:00
|
|
|
} // rocksdb
|