]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/persistent_cache/hash_table_bench.cc
1 // Copyright (c) 2013, 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.
7 #if !defined(OS_WIN) && !defined(ROCKSDB_LITE)
11 int main() { fprintf(stderr
, "Please install gflags to run tools\n"); }
13 #include <gflags/gflags.h>
18 #include <unordered_map>
20 #include "port/port_posix.h"
21 #include "rocksdb/env.h"
22 #include "util/mutexlock.h"
23 #include "utilities/persistent_cache/hash_table.h"
27 DEFINE_int32(nsec
, 10, "nsec");
28 DEFINE_int32(nthread_write
, 1, "insert %");
29 DEFINE_int32(nthread_read
, 1, "lookup %");
30 DEFINE_int32(nthread_erase
, 1, "erase %");
35 // HashTableImpl interface
37 // Abstraction of a hash table implementation
38 template <class Key
, class Value
>
41 virtual ~HashTableImpl() {}
43 virtual bool Insert(const Key
& key
, const Value
& val
) = 0;
44 virtual bool Erase(const Key
& key
) = 0;
45 virtual bool Lookup(const Key
& key
, Value
* val
) = 0;
50 // Abstraction to test a given hash table implementation. The test mostly
51 // focus on insert, lookup and erase. The test can operate in test mode and
53 class HashTableBenchmark
{
55 explicit HashTableBenchmark(HashTableImpl
<size_t, std::string
>* impl
,
56 const size_t sec
= 10,
57 const size_t nthread_write
= 1,
58 const size_t nthread_read
= 1,
59 const size_t nthread_erase
= 1)
69 StartThreads(nthread_write
, WriteMain
);
70 StartThreads(nthread_read
, ReadMain
);
71 StartThreads(nthread_erase
, EraseMain
);
73 uint64_t start
= NowInMillSec();
75 quit_
= NowInMillSec() - start
> sec_
* 1000;
76 /* sleep override */ sleep(1);
79 Env
* env
= Env::Default();
85 printf("insert/sec = %f \n", ninserts_
/ static_cast<double>(sec_
));
86 printf("read/sec = %f \n", nreads_
/ static_cast<double>(sec_
));
87 printf("erases/sec = %f \n", nerases_
/ static_cast<double>(sec_
));
88 const uint64_t ops
= ninserts_
+ nreads_
+ nerases_
;
89 printf("ops/sec = %f \n", ops
/ static_cast<double>(sec_
));
90 printf("erase fail = %d (%f%%)\n", static_cast<int>(nerases_failed_
),
91 static_cast<float>(nerases_failed_
/ nerases_
* 100));
98 size_t k
= insert_key_
++;
99 std::string
tmp(1000, k
% 255);
100 bool status
= impl_
->Insert(k
, tmp
);
107 Random64
rgen(time(nullptr));
110 size_t k
= rgen
.Next() % max_prepop_key
;
111 bool status
= impl_
->Lookup(k
, &s
);
113 assert(s
== std::string(1000, k
% 255));
120 size_t k
= erase_key_
++;
121 bool status
= impl_
->Erase(k
);
122 nerases_failed_
+= !status
;
128 // Start threads for a given function
129 void StartThreads(const size_t n
, void (*fn
)(void*)) {
130 Env
* env
= Env::Default();
131 for (size_t i
= 0; i
< n
; ++i
) {
132 env
->StartThread(fn
, this);
136 // Prepop the hash table with 1M keys
138 for (size_t i
= 0; i
< max_prepop_key
; ++i
) {
139 bool status
= impl_
->Insert(i
, std::string(1000, i
% 255));
143 erase_key_
= insert_key_
= max_prepop_key
;
145 for (size_t i
= 0; i
< 10 * max_prepop_key
; ++i
) {
146 bool status
= impl_
->Insert(insert_key_
++, std::string(1000, 'x'));
151 static uint64_t NowInMillSec() {
153 gettimeofday(&tv
, /*tz=*/nullptr);
154 return tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
158 // Wrapper functions for thread entry
160 static void WriteMain(void* args
) {
161 reinterpret_cast<HashTableBenchmark
*>(args
)->RunWrite();
164 static void ReadMain(void* args
) {
165 reinterpret_cast<HashTableBenchmark
*>(args
)->RunRead();
168 static void EraseMain(void* args
) {
169 reinterpret_cast<HashTableBenchmark
*>(args
)->RunErase();
172 HashTableImpl
<size_t, std::string
>* impl_
; // Implementation to test
173 const size_t sec_
; // Test time
174 const size_t max_prepop_key
= 1ULL * 1024 * 1024; // Max prepop key
175 std::atomic
<size_t> insert_key_
; // Last inserted key
176 std::atomic
<size_t> erase_key_
; // Erase key
177 std::atomic
<size_t> ninserts_
; // Number of inserts
178 std::atomic
<size_t> nreads_
; // Number of reads
179 std::atomic
<size_t> nerases_
; // Number of erases
180 std::atomic
<size_t> nerases_failed_
; // Number of erases failed
181 bool quit_
; // Should the threads quit ?
186 // Lock safe unordered_map implementation
187 class SimpleImpl
: public HashTableImpl
<size_t, string
> {
189 bool Insert(const size_t& key
, const string
& val
) override
{
190 WriteLock
_(&rwlock_
);
191 map_
.insert(make_pair(key
, val
));
195 bool Erase(const size_t& key
) override
{
196 WriteLock
_(&rwlock_
);
197 auto it
= map_
.find(key
);
198 if (it
== map_
.end()) {
205 bool Lookup(const size_t& key
, string
* val
) override
{
206 ReadLock
_(&rwlock_
);
207 auto it
= map_
.find(key
);
208 if (it
!= map_
.end()) {
211 return it
!= map_
.end();
215 port::RWMutex rwlock_
;
216 std::unordered_map
<size_t, string
> map_
;
221 // Thread safe custom RocksDB implementation of hash table with granular
223 class GranularLockImpl
: public HashTableImpl
<size_t, string
> {
225 bool Insert(const size_t& key
, const string
& val
) override
{
227 return impl_
.Insert(n
);
230 bool Erase(const size_t& key
) override
{
231 Node
n(key
, string());
232 return impl_
.Erase(n
, nullptr);
235 bool Lookup(const size_t& key
, string
* val
) override
{
236 Node
n(key
, string());
237 port::RWMutex
* rlock
;
238 bool status
= impl_
.Find(n
, &n
, &rlock
);
248 explicit Node(const size_t key
, const string
& val
) : key_(key
), val_(val
) {}
255 uint64_t operator()(const Node
& node
) {
256 return std::hash
<uint64_t>()(node
.key_
);
261 bool operator()(const Node
& lhs
, const Node
& rhs
) {
262 return lhs
.key_
== rhs
.key_
;
266 HashTable
<Node
, Hash
, Equal
> impl_
;
269 } // namespace rocksdb
274 int main(int argc
, char** argv
) {
275 GFLAGS::SetUsageMessage(std::string("\nUSAGE:\n") + std::string(argv
[0]) +
277 GFLAGS::ParseCommandLineFlags(&argc
, &argv
, false);
280 // Micro benchmark unordered_map
282 printf("Micro benchmarking std::unordered_map \n");
284 rocksdb::SimpleImpl impl
;
285 rocksdb::HashTableBenchmark
_(&impl
, FLAGS_nsec
, FLAGS_nthread_write
,
286 FLAGS_nthread_read
, FLAGS_nthread_erase
);
289 // Micro benchmark scalable hash table
291 printf("Micro benchmarking scalable hash map \n");
293 rocksdb::GranularLockImpl impl
;
294 rocksdb::HashTableBenchmark
_(&impl
, FLAGS_nsec
, FLAGS_nthread_write
,
295 FLAGS_nthread_read
, FLAGS_nthread_erase
);
300 #endif // #ifndef GFLAGS
302 int main(int /*argc*/, char** /*argv*/) { return 0; }