]>
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 both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
7 #if !defined(OS_WIN) && !defined(ROCKSDB_LITE)
11 int main() { fprintf(stderr
, "Please install gflags to run tools\n"); }
17 #include <unordered_map>
21 #include "port/port_posix.h"
22 #include "rocksdb/env.h"
23 #include "util/gflags_compat.h"
24 #include "util/mutexlock.h"
25 #include "util/random.h"
26 #include "utilities/persistent_cache/hash_table.h"
30 DEFINE_int32(nsec
, 10, "nsec");
31 DEFINE_int32(nthread_write
, 1, "insert %");
32 DEFINE_int32(nthread_read
, 1, "lookup %");
33 DEFINE_int32(nthread_erase
, 1, "erase %");
35 namespace ROCKSDB_NAMESPACE
{
38 // HashTableImpl interface
40 // Abstraction of a hash table implementation
41 template <class Key
, class Value
>
44 virtual ~HashTableImpl() {}
46 virtual bool Insert(const Key
& key
, const Value
& val
) = 0;
47 virtual bool Erase(const Key
& key
) = 0;
48 virtual bool Lookup(const Key
& key
, Value
* val
) = 0;
53 // Abstraction to test a given hash table implementation. The test mostly
54 // focus on insert, lookup and erase. The test can operate in test mode and
56 class HashTableBenchmark
{
58 explicit HashTableBenchmark(HashTableImpl
<size_t, std::string
>* impl
,
59 const size_t sec
= 10,
60 const size_t nthread_write
= 1,
61 const size_t nthread_read
= 1,
62 const size_t nthread_erase
= 1)
72 StartThreads(nthread_write
, WriteMain
);
73 StartThreads(nthread_read
, ReadMain
);
74 StartThreads(nthread_erase
, EraseMain
);
76 uint64_t start
= NowInMillSec();
78 quit_
= NowInMillSec() - start
> sec_
* 1000;
79 /* sleep override */ sleep(1);
82 Env
* env
= Env::Default();
88 printf("insert/sec = %f \n", ninserts_
/ static_cast<double>(sec_
));
89 printf("read/sec = %f \n", nreads_
/ static_cast<double>(sec_
));
90 printf("erases/sec = %f \n", nerases_
/ static_cast<double>(sec_
));
91 const uint64_t ops
= ninserts_
+ nreads_
+ nerases_
;
92 printf("ops/sec = %f \n", ops
/ static_cast<double>(sec_
));
93 printf("erase fail = %d (%f%%)\n", static_cast<int>(nerases_failed_
),
94 static_cast<float>(nerases_failed_
/ nerases_
* 100));
101 size_t k
= insert_key_
++;
102 std::string
tmp(1000, k
% 255);
103 bool status
= impl_
->Insert(k
, tmp
);
110 Random64
rgen(time(nullptr));
113 size_t k
= rgen
.Next() % max_prepop_key
;
114 bool status
= impl_
->Lookup(k
, &s
);
116 assert(s
== std::string(1000, k
% 255));
123 size_t k
= erase_key_
++;
124 bool status
= impl_
->Erase(k
);
125 nerases_failed_
+= !status
;
131 // Start threads for a given function
132 void StartThreads(const size_t n
, void (*fn
)(void*)) {
133 Env
* env
= Env::Default();
134 for (size_t i
= 0; i
< n
; ++i
) {
135 env
->StartThread(fn
, this);
139 // Prepop the hash table with 1M keys
141 for (size_t i
= 0; i
< max_prepop_key
; ++i
) {
142 bool status
= impl_
->Insert(i
, std::string(1000, i
% 255));
146 erase_key_
= insert_key_
= max_prepop_key
;
148 for (size_t i
= 0; i
< 10 * max_prepop_key
; ++i
) {
149 bool status
= impl_
->Insert(insert_key_
++, std::string(1000, 'x'));
154 static uint64_t NowInMillSec() {
156 gettimeofday(&tv
, /*tz=*/nullptr);
157 return tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
161 // Wrapper functions for thread entry
163 static void WriteMain(void* args
) {
164 reinterpret_cast<HashTableBenchmark
*>(args
)->RunWrite();
167 static void ReadMain(void* args
) {
168 reinterpret_cast<HashTableBenchmark
*>(args
)->RunRead();
171 static void EraseMain(void* args
) {
172 reinterpret_cast<HashTableBenchmark
*>(args
)->RunErase();
175 HashTableImpl
<size_t, std::string
>* impl_
; // Implementation to test
176 const size_t sec_
; // Test time
177 const size_t max_prepop_key
= 1ULL * 1024 * 1024; // Max prepop key
178 std::atomic
<size_t> insert_key_
; // Last inserted key
179 std::atomic
<size_t> erase_key_
; // Erase key
180 std::atomic
<size_t> ninserts_
; // Number of inserts
181 std::atomic
<size_t> nreads_
; // Number of reads
182 std::atomic
<size_t> nerases_
; // Number of erases
183 std::atomic
<size_t> nerases_failed_
; // Number of erases failed
184 bool quit_
; // Should the threads quit ?
189 // Lock safe unordered_map implementation
190 class SimpleImpl
: public HashTableImpl
<size_t, string
> {
192 bool Insert(const size_t& key
, const string
& val
) override
{
193 WriteLock
_(&rwlock_
);
194 map_
.insert(make_pair(key
, val
));
198 bool Erase(const size_t& key
) override
{
199 WriteLock
_(&rwlock_
);
200 auto it
= map_
.find(key
);
201 if (it
== map_
.end()) {
208 bool Lookup(const size_t& key
, string
* val
) override
{
209 ReadLock
_(&rwlock_
);
210 auto it
= map_
.find(key
);
211 if (it
!= map_
.end()) {
214 return it
!= map_
.end();
218 port::RWMutex rwlock_
;
219 std::unordered_map
<size_t, string
> map_
;
224 // Thread safe custom RocksDB implementation of hash table with granular
226 class GranularLockImpl
: public HashTableImpl
<size_t, string
> {
228 bool Insert(const size_t& key
, const string
& val
) override
{
230 return impl_
.Insert(n
);
233 bool Erase(const size_t& key
) override
{
234 Node
n(key
, string());
235 return impl_
.Erase(n
, nullptr);
238 bool Lookup(const size_t& key
, string
* val
) override
{
239 Node
n(key
, string());
240 port::RWMutex
* rlock
;
241 bool status
= impl_
.Find(n
, &n
, &rlock
);
251 explicit Node(const size_t key
, const string
& val
) : key_(key
), val_(val
) {}
258 uint64_t operator()(const Node
& node
) {
259 return std::hash
<uint64_t>()(node
.key_
);
264 bool operator()(const Node
& lhs
, const Node
& rhs
) {
265 return lhs
.key_
== rhs
.key_
;
269 HashTable
<Node
, Hash
, Equal
> impl_
;
272 } // namespace ROCKSDB_NAMESPACE
277 int main(int argc
, char** argv
) {
278 GFLAGS_NAMESPACE::SetUsageMessage(std::string("\nUSAGE:\n") +
279 std::string(argv
[0]) + " [OPTIONS]...");
280 GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc
, &argv
, false);
283 // Micro benchmark unordered_map
285 printf("Micro benchmarking std::unordered_map \n");
287 ROCKSDB_NAMESPACE::SimpleImpl impl
;
288 ROCKSDB_NAMESPACE::HashTableBenchmark
_(
289 &impl
, FLAGS_nsec
, FLAGS_nthread_write
, FLAGS_nthread_read
,
290 FLAGS_nthread_erase
);
293 // Micro benchmark scalable hash table
295 printf("Micro benchmarking scalable hash map \n");
297 ROCKSDB_NAMESPACE::GranularLockImpl impl
;
298 ROCKSDB_NAMESPACE::HashTableBenchmark
_(
299 &impl
, FLAGS_nsec
, FLAGS_nthread_write
, FLAGS_nthread_read
,
300 FLAGS_nthread_erase
);
305 #endif // #ifndef GFLAGS
307 int main(int /*argc*/, char** /*argv*/) { return 0; }