From 62d54809843b72045bb203cbfc70d3d0f6125432 Mon Sep 17 00:00:00 2001 From: krad Date: Thu, 2 Jun 2016 13:42:41 -0700 Subject: [PATCH] Add persistent cache to Windows build system Summary: Add hash table (under persistent cache) to CMake list Test Plan: Run hash_test in windows and make check in Linux Reviewers: sdong Subscribers: andrewkr, dhruba, leveldb Differential Revision: https://reviews.facebook.net/D59151 --- CMakeLists.txt | 2 ++ utilities/persistent_cache/hash_table.h | 14 +++++++++++--- utilities/persistent_cache/hash_table_bench.cc | 10 +++++++--- utilities/persistent_cache/hash_table_evictable.h | 3 ++- utilities/persistent_cache/hash_table_test.cc | 5 ++++- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 03fc788dd..be9b2ddd6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -322,6 +322,7 @@ set(APPS tools/dump/rocksdb_dump.cc tools/dump/rocksdb_undump.cc util/cache_bench.cc + utilities/persistent_cache/hash_table_bench.cc ) set(C_TESTS db/c_test.c) @@ -424,6 +425,7 @@ set(TESTS utilities/memory/memory_test.cc utilities/merge_operators/string_append/stringappend_test.cc utilities/options/options_util_test.cc + utilities/persistent_cache/hash_table_test.cc utilities/redis/redis_lists_test.cc utilities/spatialdb/spatial_db_test.cc utilities/table_properties_collectors/compact_on_deletion_collector_test.cc diff --git a/utilities/persistent_cache/hash_table.h b/utilities/persistent_cache/hash_table.h index 54df48c07..c68263d34 100644 --- a/utilities/persistent_cache/hash_table.h +++ b/utilities/persistent_cache/hash_table.h @@ -8,12 +8,14 @@ #ifndef ROCKSDB_LITE #include -#include #include #include +#ifdef OS_LINUX +#include +#endif + #include "include/rocksdb/env.h" -#include "port/port_posix.h" #include "util/mutexlock.h" namespace rocksdb { @@ -64,7 +66,9 @@ class HashTable { public: explicit HashTable(const size_t capacity = 1024 * 1024, const float load_factor = 2.0, const uint32_t nlocks = 256) - : nbuckets_(load_factor ? capacity / load_factor : 0), nlocks_(nlocks) { + : nbuckets_( + static_cast(load_factor ? capacity / load_factor : 0)), + nlocks_(nlocks) { // pre-conditions assert(capacity); assert(load_factor); @@ -72,11 +76,15 @@ class HashTable { assert(nlocks_); buckets_.reset(new Bucket[nbuckets_]); +#ifdef OS_LINUX mlock(buckets_.get(), nbuckets_ * sizeof(Bucket)); +#endif // initialize locks locks_.reset(new port::RWMutex[nlocks_]); +#ifdef OS_LINUX mlock(locks_.get(), nlocks_ * sizeof(port::RWMutex)); +#endif // post-conditions assert(buckets_); diff --git a/utilities/persistent_cache/hash_table_bench.cc b/utilities/persistent_cache/hash_table_bench.cc index 957af4b71..dc1441ab6 100644 --- a/utilities/persistent_cache/hash_table_bench.cc +++ b/utilities/persistent_cache/hash_table_bench.cc @@ -6,9 +6,12 @@ #ifndef ROCKSDB_LITE +#ifndef GFLAGS +#include +int main() { fprintf(stderr, "Please install gflags to run tools\n"); } +#else #include -#include -#include +#endif #include #include @@ -102,9 +105,10 @@ class HashTableBenchmark { } void RunRead() { + Random64 rgen(time(nullptr)); while (!quit_) { std::string s; - size_t k = random() % max_prepop_key; + size_t k = rgen.Next() % max_prepop_key; bool status = impl_->Lookup(k, &s); assert(status); assert(s == std::string(1000, k % 255)); diff --git a/utilities/persistent_cache/hash_table_evictable.h b/utilities/persistent_cache/hash_table_evictable.h index 98f44a19f..4ad32e610 100644 --- a/utilities/persistent_cache/hash_table_evictable.h +++ b/utilities/persistent_cache/hash_table_evictable.h @@ -75,7 +75,7 @@ class EvictableHashTable : private HashTable { // Evict one of the least recently used object // T* Evict(const std::function& fn = nullptr) { - const size_t start_idx = random() % hash_table::nlocks_; + const size_t start_idx = rand_.Next() % hash_table::nlocks_; T* t = nullptr; // iterate from start_idx .. 0 .. start_idx @@ -156,6 +156,7 @@ class EvictableHashTable : private HashTable { return hash_table::locks_[lock_idx]; } + Random64 rand_{static_cast(time(nullptr))}; std::unique_ptr lru_lists_; }; diff --git a/utilities/persistent_cache/hash_table_test.cc b/utilities/persistent_cache/hash_table_test.cc index e05460faf..eb050604b 100644 --- a/utilities/persistent_cache/hash_table_test.cc +++ b/utilities/persistent_cache/hash_table_test.cc @@ -3,12 +3,14 @@ // 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 #include #include #include #include "db/db_test_util.h" #include "util/arena.h" +#include "util/random.h" #include "util/testharness.h" #include "utilities/persistent_cache/hash_table.h" #include "utilities/persistent_cache/hash_table_evictable.h" @@ -101,10 +103,11 @@ TEST_F(HashTableTest, TestErase) { map_.Insert(Node(k, std::string(1000, k % 255))); } + auto rand = Random64(time(nullptr)); // erase a few keys randomly std::set erased; for (int i = 0; i < 1024; ++i) { - uint64_t k = random() % max_keys; + uint64_t k = rand.Next() % max_keys; if (erased.find(k) != erased.end()) { continue; }