filiptepper-leveldb-ruby 0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/LICENSE +24 -0
  2. data/README +72 -0
  3. data/ext/leveldb/extconf.rb +14 -0
  4. data/ext/leveldb/leveldb.cc +530 -0
  5. data/ext/leveldb/platform.rb +83 -0
  6. data/leveldb/Makefile +191 -0
  7. data/leveldb/build_detect_platform +160 -0
  8. data/leveldb/db/builder.cc +88 -0
  9. data/leveldb/db/builder.h +34 -0
  10. data/leveldb/db/c.cc +581 -0
  11. data/leveldb/db/corruption_test.cc +359 -0
  12. data/leveldb/db/db_bench.cc +970 -0
  13. data/leveldb/db/db_impl.cc +1448 -0
  14. data/leveldb/db/db_impl.h +194 -0
  15. data/leveldb/db/db_iter.cc +299 -0
  16. data/leveldb/db/db_iter.h +26 -0
  17. data/leveldb/db/db_test.cc +1901 -0
  18. data/leveldb/db/dbformat.cc +140 -0
  19. data/leveldb/db/dbformat.h +227 -0
  20. data/leveldb/db/dbformat_test.cc +112 -0
  21. data/leveldb/db/filename.cc +139 -0
  22. data/leveldb/db/filename.h +80 -0
  23. data/leveldb/db/filename_test.cc +122 -0
  24. data/leveldb/db/log_format.h +35 -0
  25. data/leveldb/db/log_reader.cc +259 -0
  26. data/leveldb/db/log_reader.h +108 -0
  27. data/leveldb/db/log_test.cc +500 -0
  28. data/leveldb/db/log_writer.cc +103 -0
  29. data/leveldb/db/log_writer.h +48 -0
  30. data/leveldb/db/memtable.cc +145 -0
  31. data/leveldb/db/memtable.h +91 -0
  32. data/leveldb/db/repair.cc +389 -0
  33. data/leveldb/db/skiplist.h +379 -0
  34. data/leveldb/db/skiplist_test.cc +378 -0
  35. data/leveldb/db/snapshot.h +66 -0
  36. data/leveldb/db/table_cache.cc +121 -0
  37. data/leveldb/db/table_cache.h +61 -0
  38. data/leveldb/db/version_edit.cc +266 -0
  39. data/leveldb/db/version_edit.h +107 -0
  40. data/leveldb/db/version_edit_test.cc +46 -0
  41. data/leveldb/db/version_set.cc +1402 -0
  42. data/leveldb/db/version_set.h +370 -0
  43. data/leveldb/db/version_set_test.cc +179 -0
  44. data/leveldb/db/write_batch.cc +147 -0
  45. data/leveldb/db/write_batch_internal.h +49 -0
  46. data/leveldb/db/write_batch_test.cc +120 -0
  47. data/leveldb/helpers/memenv/memenv.cc +374 -0
  48. data/leveldb/helpers/memenv/memenv.h +20 -0
  49. data/leveldb/helpers/memenv/memenv_test.cc +232 -0
  50. data/leveldb/include/leveldb/c.h +275 -0
  51. data/leveldb/include/leveldb/cache.h +99 -0
  52. data/leveldb/include/leveldb/comparator.h +63 -0
  53. data/leveldb/include/leveldb/db.h +161 -0
  54. data/leveldb/include/leveldb/env.h +323 -0
  55. data/leveldb/include/leveldb/filter_policy.h +70 -0
  56. data/leveldb/include/leveldb/iterator.h +100 -0
  57. data/leveldb/include/leveldb/options.h +195 -0
  58. data/leveldb/include/leveldb/slice.h +109 -0
  59. data/leveldb/include/leveldb/status.h +106 -0
  60. data/leveldb/include/leveldb/table.h +85 -0
  61. data/leveldb/include/leveldb/table_builder.h +92 -0
  62. data/leveldb/include/leveldb/write_batch.h +64 -0
  63. data/leveldb/port/atomic_pointer.h +144 -0
  64. data/leveldb/port/port.h +21 -0
  65. data/leveldb/port/port_android.cc +64 -0
  66. data/leveldb/port/port_android.h +159 -0
  67. data/leveldb/port/port_example.h +125 -0
  68. data/leveldb/port/port_posix.cc +50 -0
  69. data/leveldb/port/port_posix.h +129 -0
  70. data/leveldb/port/win/stdint.h +24 -0
  71. data/leveldb/table/block.cc +267 -0
  72. data/leveldb/table/block.h +44 -0
  73. data/leveldb/table/block_builder.cc +109 -0
  74. data/leveldb/table/block_builder.h +57 -0
  75. data/leveldb/table/filter_block.cc +111 -0
  76. data/leveldb/table/filter_block.h +68 -0
  77. data/leveldb/table/filter_block_test.cc +128 -0
  78. data/leveldb/table/format.cc +145 -0
  79. data/leveldb/table/format.h +108 -0
  80. data/leveldb/table/iterator.cc +67 -0
  81. data/leveldb/table/iterator_wrapper.h +63 -0
  82. data/leveldb/table/merger.cc +197 -0
  83. data/leveldb/table/merger.h +26 -0
  84. data/leveldb/table/table.cc +276 -0
  85. data/leveldb/table/table_builder.cc +270 -0
  86. data/leveldb/table/table_test.cc +838 -0
  87. data/leveldb/table/two_level_iterator.cc +182 -0
  88. data/leveldb/table/two_level_iterator.h +34 -0
  89. data/leveldb/util/arena.cc +68 -0
  90. data/leveldb/util/arena.h +68 -0
  91. data/leveldb/util/arena_test.cc +68 -0
  92. data/leveldb/util/bloom.cc +95 -0
  93. data/leveldb/util/bloom_test.cc +159 -0
  94. data/leveldb/util/cache.cc +328 -0
  95. data/leveldb/util/cache_test.cc +186 -0
  96. data/leveldb/util/coding.cc +194 -0
  97. data/leveldb/util/coding.h +104 -0
  98. data/leveldb/util/coding_test.cc +173 -0
  99. data/leveldb/util/comparator.cc +76 -0
  100. data/leveldb/util/crc32c.cc +332 -0
  101. data/leveldb/util/crc32c.h +45 -0
  102. data/leveldb/util/crc32c_test.cc +72 -0
  103. data/leveldb/util/env.cc +96 -0
  104. data/leveldb/util/env_posix.cc +609 -0
  105. data/leveldb/util/env_test.cc +104 -0
  106. data/leveldb/util/filter_policy.cc +11 -0
  107. data/leveldb/util/hash.cc +45 -0
  108. data/leveldb/util/hash.h +19 -0
  109. data/leveldb/util/histogram.cc +139 -0
  110. data/leveldb/util/histogram.h +42 -0
  111. data/leveldb/util/logging.cc +81 -0
  112. data/leveldb/util/logging.h +47 -0
  113. data/leveldb/util/mutexlock.h +39 -0
  114. data/leveldb/util/options.cc +29 -0
  115. data/leveldb/util/posix_logger.h +98 -0
  116. data/leveldb/util/random.h +59 -0
  117. data/leveldb/util/status.cc +75 -0
  118. data/leveldb/util/testharness.cc +77 -0
  119. data/leveldb/util/testharness.h +138 -0
  120. data/leveldb/util/testutil.cc +51 -0
  121. data/leveldb/util/testutil.h +53 -0
  122. data/lib/leveldb.rb +76 -0
  123. metadata +175 -0
@@ -0,0 +1,359 @@
1
+ // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file. See the AUTHORS file for names of contributors.
4
+
5
+ #include "leveldb/db.h"
6
+
7
+ #include <errno.h>
8
+ #include <fcntl.h>
9
+ #include <sys/stat.h>
10
+ #include <sys/types.h>
11
+ #include "leveldb/cache.h"
12
+ #include "leveldb/env.h"
13
+ #include "leveldb/table.h"
14
+ #include "leveldb/write_batch.h"
15
+ #include "db/db_impl.h"
16
+ #include "db/filename.h"
17
+ #include "db/log_format.h"
18
+ #include "db/version_set.h"
19
+ #include "util/logging.h"
20
+ #include "util/testharness.h"
21
+ #include "util/testutil.h"
22
+
23
+ namespace leveldb {
24
+
25
+ static const int kValueSize = 1000;
26
+
27
+ class CorruptionTest {
28
+ public:
29
+ test::ErrorEnv env_;
30
+ std::string dbname_;
31
+ Cache* tiny_cache_;
32
+ Options options_;
33
+ DB* db_;
34
+
35
+ CorruptionTest() {
36
+ tiny_cache_ = NewLRUCache(100);
37
+ options_.env = &env_;
38
+ dbname_ = test::TmpDir() + "/db_test";
39
+ DestroyDB(dbname_, options_);
40
+
41
+ db_ = NULL;
42
+ options_.create_if_missing = true;
43
+ Reopen();
44
+ options_.create_if_missing = false;
45
+ }
46
+
47
+ ~CorruptionTest() {
48
+ delete db_;
49
+ DestroyDB(dbname_, Options());
50
+ delete tiny_cache_;
51
+ }
52
+
53
+ Status TryReopen(Options* options = NULL) {
54
+ delete db_;
55
+ db_ = NULL;
56
+ Options opt = (options ? *options : options_);
57
+ opt.env = &env_;
58
+ opt.block_cache = tiny_cache_;
59
+ return DB::Open(opt, dbname_, &db_);
60
+ }
61
+
62
+ void Reopen(Options* options = NULL) {
63
+ ASSERT_OK(TryReopen(options));
64
+ }
65
+
66
+ void RepairDB() {
67
+ delete db_;
68
+ db_ = NULL;
69
+ ASSERT_OK(::leveldb::RepairDB(dbname_, options_));
70
+ }
71
+
72
+ void Build(int n) {
73
+ std::string key_space, value_space;
74
+ WriteBatch batch;
75
+ for (int i = 0; i < n; i++) {
76
+ //if ((i % 100) == 0) fprintf(stderr, "@ %d of %d\n", i, n);
77
+ Slice key = Key(i, &key_space);
78
+ batch.Clear();
79
+ batch.Put(key, Value(i, &value_space));
80
+ ASSERT_OK(db_->Write(WriteOptions(), &batch));
81
+ }
82
+ }
83
+
84
+ void Check(int min_expected, int max_expected) {
85
+ int next_expected = 0;
86
+ int missed = 0;
87
+ int bad_keys = 0;
88
+ int bad_values = 0;
89
+ int correct = 0;
90
+ std::string value_space;
91
+ Iterator* iter = db_->NewIterator(ReadOptions());
92
+ for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
93
+ uint64_t key;
94
+ Slice in(iter->key());
95
+ if (!ConsumeDecimalNumber(&in, &key) ||
96
+ !in.empty() ||
97
+ key < next_expected) {
98
+ bad_keys++;
99
+ continue;
100
+ }
101
+ missed += (key - next_expected);
102
+ next_expected = key + 1;
103
+ if (iter->value() != Value(key, &value_space)) {
104
+ bad_values++;
105
+ } else {
106
+ correct++;
107
+ }
108
+ }
109
+ delete iter;
110
+
111
+ fprintf(stderr,
112
+ "expected=%d..%d; got=%d; bad_keys=%d; bad_values=%d; missed=%d\n",
113
+ min_expected, max_expected, correct, bad_keys, bad_values, missed);
114
+ ASSERT_LE(min_expected, correct);
115
+ ASSERT_GE(max_expected, correct);
116
+ }
117
+
118
+ void Corrupt(FileType filetype, int offset, int bytes_to_corrupt) {
119
+ // Pick file to corrupt
120
+ std::vector<std::string> filenames;
121
+ ASSERT_OK(env_.GetChildren(dbname_, &filenames));
122
+ uint64_t number;
123
+ FileType type;
124
+ std::string fname;
125
+ int picked_number = -1;
126
+ for (int i = 0; i < filenames.size(); i++) {
127
+ if (ParseFileName(filenames[i], &number, &type) &&
128
+ type == filetype &&
129
+ int(number) > picked_number) { // Pick latest file
130
+ fname = dbname_ + "/" + filenames[i];
131
+ picked_number = number;
132
+ }
133
+ }
134
+ ASSERT_TRUE(!fname.empty()) << filetype;
135
+
136
+ struct stat sbuf;
137
+ if (stat(fname.c_str(), &sbuf) != 0) {
138
+ const char* msg = strerror(errno);
139
+ ASSERT_TRUE(false) << fname << ": " << msg;
140
+ }
141
+
142
+ if (offset < 0) {
143
+ // Relative to end of file; make it absolute
144
+ if (-offset > sbuf.st_size) {
145
+ offset = 0;
146
+ } else {
147
+ offset = sbuf.st_size + offset;
148
+ }
149
+ }
150
+ if (offset > sbuf.st_size) {
151
+ offset = sbuf.st_size;
152
+ }
153
+ if (offset + bytes_to_corrupt > sbuf.st_size) {
154
+ bytes_to_corrupt = sbuf.st_size - offset;
155
+ }
156
+
157
+ // Do it
158
+ std::string contents;
159
+ Status s = ReadFileToString(Env::Default(), fname, &contents);
160
+ ASSERT_TRUE(s.ok()) << s.ToString();
161
+ for (int i = 0; i < bytes_to_corrupt; i++) {
162
+ contents[i + offset] ^= 0x80;
163
+ }
164
+ s = WriteStringToFile(Env::Default(), contents, fname);
165
+ ASSERT_TRUE(s.ok()) << s.ToString();
166
+ }
167
+
168
+ int Property(const std::string& name) {
169
+ std::string property;
170
+ int result;
171
+ if (db_->GetProperty(name, &property) &&
172
+ sscanf(property.c_str(), "%d", &result) == 1) {
173
+ return result;
174
+ } else {
175
+ return -1;
176
+ }
177
+ }
178
+
179
+ // Return the ith key
180
+ Slice Key(int i, std::string* storage) {
181
+ char buf[100];
182
+ snprintf(buf, sizeof(buf), "%016d", i);
183
+ storage->assign(buf, strlen(buf));
184
+ return Slice(*storage);
185
+ }
186
+
187
+ // Return the value to associate with the specified key
188
+ Slice Value(int k, std::string* storage) {
189
+ Random r(k);
190
+ return test::RandomString(&r, kValueSize, storage);
191
+ }
192
+ };
193
+
194
+ TEST(CorruptionTest, Recovery) {
195
+ Build(100);
196
+ Check(100, 100);
197
+ Corrupt(kLogFile, 19, 1); // WriteBatch tag for first record
198
+ Corrupt(kLogFile, log::kBlockSize + 1000, 1); // Somewhere in second block
199
+ Reopen();
200
+
201
+ // The 64 records in the first two log blocks are completely lost.
202
+ Check(36, 36);
203
+ }
204
+
205
+ TEST(CorruptionTest, RecoverWriteError) {
206
+ env_.writable_file_error_ = true;
207
+ Status s = TryReopen();
208
+ ASSERT_TRUE(!s.ok());
209
+ }
210
+
211
+ TEST(CorruptionTest, NewFileErrorDuringWrite) {
212
+ // Do enough writing to force minor compaction
213
+ env_.writable_file_error_ = true;
214
+ const int num = 3 + (Options().write_buffer_size / kValueSize);
215
+ std::string value_storage;
216
+ Status s;
217
+ for (int i = 0; s.ok() && i < num; i++) {
218
+ WriteBatch batch;
219
+ batch.Put("a", Value(100, &value_storage));
220
+ s = db_->Write(WriteOptions(), &batch);
221
+ }
222
+ ASSERT_TRUE(!s.ok());
223
+ ASSERT_GE(env_.num_writable_file_errors_, 1);
224
+ env_.writable_file_error_ = false;
225
+ Reopen();
226
+ }
227
+
228
+ TEST(CorruptionTest, TableFile) {
229
+ Build(100);
230
+ DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
231
+ dbi->TEST_CompactMemTable();
232
+ dbi->TEST_CompactRange(0, NULL, NULL);
233
+ dbi->TEST_CompactRange(1, NULL, NULL);
234
+
235
+ Corrupt(kTableFile, 100, 1);
236
+ Check(99, 99);
237
+ }
238
+
239
+ TEST(CorruptionTest, TableFileIndexData) {
240
+ Build(10000); // Enough to build multiple Tables
241
+ DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
242
+ dbi->TEST_CompactMemTable();
243
+
244
+ Corrupt(kTableFile, -2000, 500);
245
+ Reopen();
246
+ Check(5000, 9999);
247
+ }
248
+
249
+ TEST(CorruptionTest, MissingDescriptor) {
250
+ Build(1000);
251
+ RepairDB();
252
+ Reopen();
253
+ Check(1000, 1000);
254
+ }
255
+
256
+ TEST(CorruptionTest, SequenceNumberRecovery) {
257
+ ASSERT_OK(db_->Put(WriteOptions(), "foo", "v1"));
258
+ ASSERT_OK(db_->Put(WriteOptions(), "foo", "v2"));
259
+ ASSERT_OK(db_->Put(WriteOptions(), "foo", "v3"));
260
+ ASSERT_OK(db_->Put(WriteOptions(), "foo", "v4"));
261
+ ASSERT_OK(db_->Put(WriteOptions(), "foo", "v5"));
262
+ RepairDB();
263
+ Reopen();
264
+ std::string v;
265
+ ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
266
+ ASSERT_EQ("v5", v);
267
+ // Write something. If sequence number was not recovered properly,
268
+ // it will be hidden by an earlier write.
269
+ ASSERT_OK(db_->Put(WriteOptions(), "foo", "v6"));
270
+ ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
271
+ ASSERT_EQ("v6", v);
272
+ Reopen();
273
+ ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
274
+ ASSERT_EQ("v6", v);
275
+ }
276
+
277
+ TEST(CorruptionTest, CorruptedDescriptor) {
278
+ ASSERT_OK(db_->Put(WriteOptions(), "foo", "hello"));
279
+ DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
280
+ dbi->TEST_CompactMemTable();
281
+ dbi->TEST_CompactRange(0, NULL, NULL);
282
+
283
+ Corrupt(kDescriptorFile, 0, 1000);
284
+ Status s = TryReopen();
285
+ ASSERT_TRUE(!s.ok());
286
+
287
+ RepairDB();
288
+ Reopen();
289
+ std::string v;
290
+ ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));
291
+ ASSERT_EQ("hello", v);
292
+ }
293
+
294
+ TEST(CorruptionTest, CompactionInputError) {
295
+ Build(10);
296
+ DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
297
+ dbi->TEST_CompactMemTable();
298
+ const int last = config::kMaxMemCompactLevel;
299
+ ASSERT_EQ(1, Property("leveldb.num-files-at-level" + NumberToString(last)));
300
+
301
+ Corrupt(kTableFile, 100, 1);
302
+ Check(9, 9);
303
+
304
+ // Force compactions by writing lots of values
305
+ Build(10000);
306
+ Check(10000, 10000);
307
+ }
308
+
309
+ TEST(CorruptionTest, CompactionInputErrorParanoid) {
310
+ Options options;
311
+ options.paranoid_checks = true;
312
+ options.write_buffer_size = 1048576;
313
+ Reopen(&options);
314
+ DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
315
+
316
+ // Fill levels >= 1 so memtable compaction outputs to level 1
317
+ for (int level = 1; level < config::kNumLevels; level++) {
318
+ dbi->Put(WriteOptions(), "", "begin");
319
+ dbi->Put(WriteOptions(), "~", "end");
320
+ dbi->TEST_CompactMemTable();
321
+ }
322
+
323
+ Build(10);
324
+ dbi->TEST_CompactMemTable();
325
+ ASSERT_EQ(1, Property("leveldb.num-files-at-level0"));
326
+
327
+ Corrupt(kTableFile, 100, 1);
328
+ Check(9, 9);
329
+
330
+ // Write must eventually fail because of corrupted table
331
+ Status s;
332
+ std::string tmp1, tmp2;
333
+ for (int i = 0; i < 10000 && s.ok(); i++) {
334
+ s = db_->Put(WriteOptions(), Key(i, &tmp1), Value(i, &tmp2));
335
+ }
336
+ ASSERT_TRUE(!s.ok()) << "write did not fail in corrupted paranoid db";
337
+ }
338
+
339
+ TEST(CorruptionTest, UnrelatedKeys) {
340
+ Build(10);
341
+ DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
342
+ dbi->TEST_CompactMemTable();
343
+ Corrupt(kTableFile, 100, 1);
344
+
345
+ std::string tmp1, tmp2;
346
+ ASSERT_OK(db_->Put(WriteOptions(), Key(1000, &tmp1), Value(1000, &tmp2)));
347
+ std::string v;
348
+ ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));
349
+ ASSERT_EQ(Value(1000, &tmp2).ToString(), v);
350
+ dbi->TEST_CompactMemTable();
351
+ ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));
352
+ ASSERT_EQ(Value(1000, &tmp2).ToString(), v);
353
+ }
354
+
355
+ } // namespace leveldb
356
+
357
+ int main(int argc, char** argv) {
358
+ return leveldb::test::RunAllTests();
359
+ }
@@ -0,0 +1,970 @@
1
+ // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file. See the AUTHORS file for names of contributors.
4
+
5
+ #include <sys/types.h>
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+ #include "db/db_impl.h"
9
+ #include "db/version_set.h"
10
+ #include "leveldb/cache.h"
11
+ #include "leveldb/db.h"
12
+ #include "leveldb/env.h"
13
+ #include "leveldb/write_batch.h"
14
+ #include "port/port.h"
15
+ #include "util/crc32c.h"
16
+ #include "util/histogram.h"
17
+ #include "util/mutexlock.h"
18
+ #include "util/random.h"
19
+ #include "util/testutil.h"
20
+
21
+ // Comma-separated list of operations to run in the specified order
22
+ // Actual benchmarks:
23
+ // fillseq -- write N values in sequential key order in async mode
24
+ // fillrandom -- write N values in random key order in async mode
25
+ // overwrite -- overwrite N values in random key order in async mode
26
+ // fillsync -- write N/100 values in random key order in sync mode
27
+ // fill100K -- write N/1000 100K values in random order in async mode
28
+ // deleteseq -- delete N keys in sequential order
29
+ // deleterandom -- delete N keys in random order
30
+ // readseq -- read N times sequentially
31
+ // readreverse -- read N times in reverse order
32
+ // readrandom -- read N times in random order
33
+ // readmissing -- read N missing keys in random order
34
+ // readhot -- read N times in random order from 1% section of DB
35
+ // seekrandom -- N random seeks
36
+ // crc32c -- repeated crc32c of 4K of data
37
+ // acquireload -- load N*1000 times
38
+ // Meta operations:
39
+ // compact -- Compact the entire DB
40
+ // stats -- Print DB stats
41
+ // sstables -- Print sstable info
42
+ // heapprofile -- Dump a heap profile (if supported by this port)
43
+ static const char* FLAGS_benchmarks =
44
+ "fillseq,"
45
+ "fillsync,"
46
+ "fillrandom,"
47
+ "overwrite,"
48
+ "readrandom,"
49
+ "readrandom," // Extra run to allow previous compactions to quiesce
50
+ "readseq,"
51
+ "readreverse,"
52
+ "compact,"
53
+ "readrandom,"
54
+ "readseq,"
55
+ "readreverse,"
56
+ "fill100K,"
57
+ "crc32c,"
58
+ "snappycomp,"
59
+ "snappyuncomp,"
60
+ "acquireload,"
61
+ ;
62
+
63
+ // Number of key/values to place in database
64
+ static int FLAGS_num = 1000000;
65
+
66
+ // Number of read operations to do. If negative, do FLAGS_num reads.
67
+ static int FLAGS_reads = -1;
68
+
69
+ // Number of concurrent threads to run.
70
+ static int FLAGS_threads = 1;
71
+
72
+ // Size of each value
73
+ static int FLAGS_value_size = 100;
74
+
75
+ // Arrange to generate values that shrink to this fraction of
76
+ // their original size after compression
77
+ static double FLAGS_compression_ratio = 0.5;
78
+
79
+ // Print histogram of operation timings
80
+ static bool FLAGS_histogram = false;
81
+
82
+ // Number of bytes to buffer in memtable before compacting
83
+ // (initialized to default value by "main")
84
+ static int FLAGS_write_buffer_size = 0;
85
+
86
+ // Number of bytes to use as a cache of uncompressed data.
87
+ // Negative means use default settings.
88
+ static int FLAGS_cache_size = -1;
89
+
90
+ // Maximum number of files to keep open at the same time (use default if == 0)
91
+ static int FLAGS_open_files = 0;
92
+
93
+ // Bloom filter bits per key.
94
+ // Negative means use default settings.
95
+ static int FLAGS_bloom_bits = -1;
96
+
97
+ // If true, do not destroy the existing database. If you set this
98
+ // flag and also specify a benchmark that wants a fresh database, that
99
+ // benchmark will fail.
100
+ static bool FLAGS_use_existing_db = false;
101
+
102
+ // Use the db with the following name.
103
+ static const char* FLAGS_db = "/tmp/dbbench";
104
+
105
+ namespace leveldb {
106
+
107
+ namespace {
108
+
109
+ // Helper for quickly generating random data.
110
+ class RandomGenerator {
111
+ private:
112
+ std::string data_;
113
+ int pos_;
114
+
115
+ public:
116
+ RandomGenerator() {
117
+ // We use a limited amount of data over and over again and ensure
118
+ // that it is larger than the compression window (32KB), and also
119
+ // large enough to serve all typical value sizes we want to write.
120
+ Random rnd(301);
121
+ std::string piece;
122
+ while (data_.size() < 1048576) {
123
+ // Add a short fragment that is as compressible as specified
124
+ // by FLAGS_compression_ratio.
125
+ test::CompressibleString(&rnd, FLAGS_compression_ratio, 100, &piece);
126
+ data_.append(piece);
127
+ }
128
+ pos_ = 0;
129
+ }
130
+
131
+ Slice Generate(int len) {
132
+ if (pos_ + len > data_.size()) {
133
+ pos_ = 0;
134
+ assert(len < data_.size());
135
+ }
136
+ pos_ += len;
137
+ return Slice(data_.data() + pos_ - len, len);
138
+ }
139
+ };
140
+
141
+ static Slice TrimSpace(Slice s) {
142
+ int start = 0;
143
+ while (start < s.size() && isspace(s[start])) {
144
+ start++;
145
+ }
146
+ int limit = s.size();
147
+ while (limit > start && isspace(s[limit-1])) {
148
+ limit--;
149
+ }
150
+ return Slice(s.data() + start, limit - start);
151
+ }
152
+
153
+ static void AppendWithSpace(std::string* str, Slice msg) {
154
+ if (msg.empty()) return;
155
+ if (!str->empty()) {
156
+ str->push_back(' ');
157
+ }
158
+ str->append(msg.data(), msg.size());
159
+ }
160
+
161
+ class Stats {
162
+ private:
163
+ double start_;
164
+ double finish_;
165
+ double seconds_;
166
+ int done_;
167
+ int next_report_;
168
+ int64_t bytes_;
169
+ double last_op_finish_;
170
+ Histogram hist_;
171
+ std::string message_;
172
+
173
+ public:
174
+ Stats() { Start(); }
175
+
176
+ void Start() {
177
+ next_report_ = 100;
178
+ last_op_finish_ = start_;
179
+ hist_.Clear();
180
+ done_ = 0;
181
+ bytes_ = 0;
182
+ seconds_ = 0;
183
+ start_ = Env::Default()->NowMicros();
184
+ finish_ = start_;
185
+ message_.clear();
186
+ }
187
+
188
+ void Merge(const Stats& other) {
189
+ hist_.Merge(other.hist_);
190
+ done_ += other.done_;
191
+ bytes_ += other.bytes_;
192
+ seconds_ += other.seconds_;
193
+ if (other.start_ < start_) start_ = other.start_;
194
+ if (other.finish_ > finish_) finish_ = other.finish_;
195
+
196
+ // Just keep the messages from one thread
197
+ if (message_.empty()) message_ = other.message_;
198
+ }
199
+
200
+ void Stop() {
201
+ finish_ = Env::Default()->NowMicros();
202
+ seconds_ = (finish_ - start_) * 1e-6;
203
+ }
204
+
205
+ void AddMessage(Slice msg) {
206
+ AppendWithSpace(&message_, msg);
207
+ }
208
+
209
+ void FinishedSingleOp() {
210
+ if (FLAGS_histogram) {
211
+ double now = Env::Default()->NowMicros();
212
+ double micros = now - last_op_finish_;
213
+ hist_.Add(micros);
214
+ if (micros > 20000) {
215
+ fprintf(stderr, "long op: %.1f micros%30s\r", micros, "");
216
+ fflush(stderr);
217
+ }
218
+ last_op_finish_ = now;
219
+ }
220
+
221
+ done_++;
222
+ if (done_ >= next_report_) {
223
+ if (next_report_ < 1000) next_report_ += 100;
224
+ else if (next_report_ < 5000) next_report_ += 500;
225
+ else if (next_report_ < 10000) next_report_ += 1000;
226
+ else if (next_report_ < 50000) next_report_ += 5000;
227
+ else if (next_report_ < 100000) next_report_ += 10000;
228
+ else if (next_report_ < 500000) next_report_ += 50000;
229
+ else next_report_ += 100000;
230
+ fprintf(stderr, "... finished %d ops%30s\r", done_, "");
231
+ fflush(stderr);
232
+ }
233
+ }
234
+
235
+ void AddBytes(int64_t n) {
236
+ bytes_ += n;
237
+ }
238
+
239
+ void Report(const Slice& name) {
240
+ // Pretend at least one op was done in case we are running a benchmark
241
+ // that does not call FinishedSingleOp().
242
+ if (done_ < 1) done_ = 1;
243
+
244
+ std::string extra;
245
+ if (bytes_ > 0) {
246
+ // Rate is computed on actual elapsed time, not the sum of per-thread
247
+ // elapsed times.
248
+ double elapsed = (finish_ - start_) * 1e-6;
249
+ char rate[100];
250
+ snprintf(rate, sizeof(rate), "%6.1f MB/s",
251
+ (bytes_ / 1048576.0) / elapsed);
252
+ extra = rate;
253
+ }
254
+ AppendWithSpace(&extra, message_);
255
+
256
+ fprintf(stdout, "%-12s : %11.3f micros/op;%s%s\n",
257
+ name.ToString().c_str(),
258
+ seconds_ * 1e6 / done_,
259
+ (extra.empty() ? "" : " "),
260
+ extra.c_str());
261
+ if (FLAGS_histogram) {
262
+ fprintf(stdout, "Microseconds per op:\n%s\n", hist_.ToString().c_str());
263
+ }
264
+ fflush(stdout);
265
+ }
266
+ };
267
+
268
+ // State shared by all concurrent executions of the same benchmark.
269
+ struct SharedState {
270
+ port::Mutex mu;
271
+ port::CondVar cv;
272
+ int total;
273
+
274
+ // Each thread goes through the following states:
275
+ // (1) initializing
276
+ // (2) waiting for others to be initialized
277
+ // (3) running
278
+ // (4) done
279
+
280
+ int num_initialized;
281
+ int num_done;
282
+ bool start;
283
+
284
+ SharedState() : cv(&mu) { }
285
+ };
286
+
287
+ // Per-thread state for concurrent executions of the same benchmark.
288
+ struct ThreadState {
289
+ int tid; // 0..n-1 when running in n threads
290
+ Random rand; // Has different seeds for different threads
291
+ Stats stats;
292
+ SharedState* shared;
293
+
294
+ ThreadState(int index)
295
+ : tid(index),
296
+ rand(1000 + index) {
297
+ }
298
+ };
299
+
300
+ } // namespace
301
+
302
+ class Benchmark {
303
+ private:
304
+ Cache* cache_;
305
+ const FilterPolicy* filter_policy_;
306
+ DB* db_;
307
+ int num_;
308
+ int value_size_;
309
+ int entries_per_batch_;
310
+ WriteOptions write_options_;
311
+ int reads_;
312
+ int heap_counter_;
313
+
314
+ void PrintHeader() {
315
+ const int kKeySize = 16;
316
+ PrintEnvironment();
317
+ fprintf(stdout, "Keys: %d bytes each\n", kKeySize);
318
+ fprintf(stdout, "Values: %d bytes each (%d bytes after compression)\n",
319
+ FLAGS_value_size,
320
+ static_cast<int>(FLAGS_value_size * FLAGS_compression_ratio + 0.5));
321
+ fprintf(stdout, "Entries: %d\n", num_);
322
+ fprintf(stdout, "RawSize: %.1f MB (estimated)\n",
323
+ ((static_cast<int64_t>(kKeySize + FLAGS_value_size) * num_)
324
+ / 1048576.0));
325
+ fprintf(stdout, "FileSize: %.1f MB (estimated)\n",
326
+ (((kKeySize + FLAGS_value_size * FLAGS_compression_ratio) * num_)
327
+ / 1048576.0));
328
+ PrintWarnings();
329
+ fprintf(stdout, "------------------------------------------------\n");
330
+ }
331
+
332
+ void PrintWarnings() {
333
+ #if defined(__GNUC__) && !defined(__OPTIMIZE__)
334
+ fprintf(stdout,
335
+ "WARNING: Optimization is disabled: benchmarks unnecessarily slow\n"
336
+ );
337
+ #endif
338
+ #ifndef NDEBUG
339
+ fprintf(stdout,
340
+ "WARNING: Assertions are enabled; benchmarks unnecessarily slow\n");
341
+ #endif
342
+
343
+ // See if snappy is working by attempting to compress a compressible string
344
+ const char text[] = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
345
+ std::string compressed;
346
+ if (!port::Snappy_Compress(text, sizeof(text), &compressed)) {
347
+ fprintf(stdout, "WARNING: Snappy compression is not enabled\n");
348
+ } else if (compressed.size() >= sizeof(text)) {
349
+ fprintf(stdout, "WARNING: Snappy compression is not effective\n");
350
+ }
351
+ }
352
+
353
+ void PrintEnvironment() {
354
+ fprintf(stderr, "LevelDB: version %d.%d\n",
355
+ kMajorVersion, kMinorVersion);
356
+
357
+ #if defined(__linux)
358
+ time_t now = time(NULL);
359
+ fprintf(stderr, "Date: %s", ctime(&now)); // ctime() adds newline
360
+
361
+ FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
362
+ if (cpuinfo != NULL) {
363
+ char line[1000];
364
+ int num_cpus = 0;
365
+ std::string cpu_type;
366
+ std::string cache_size;
367
+ while (fgets(line, sizeof(line), cpuinfo) != NULL) {
368
+ const char* sep = strchr(line, ':');
369
+ if (sep == NULL) {
370
+ continue;
371
+ }
372
+ Slice key = TrimSpace(Slice(line, sep - 1 - line));
373
+ Slice val = TrimSpace(Slice(sep + 1));
374
+ if (key == "model name") {
375
+ ++num_cpus;
376
+ cpu_type = val.ToString();
377
+ } else if (key == "cache size") {
378
+ cache_size = val.ToString();
379
+ }
380
+ }
381
+ fclose(cpuinfo);
382
+ fprintf(stderr, "CPU: %d * %s\n", num_cpus, cpu_type.c_str());
383
+ fprintf(stderr, "CPUCache: %s\n", cache_size.c_str());
384
+ }
385
+ #endif
386
+ }
387
+
388
+ public:
389
+ Benchmark()
390
+ : cache_(FLAGS_cache_size >= 0 ? NewLRUCache(FLAGS_cache_size) : NULL),
391
+ filter_policy_(FLAGS_bloom_bits >= 0
392
+ ? NewBloomFilterPolicy(FLAGS_bloom_bits)
393
+ : NULL),
394
+ db_(NULL),
395
+ num_(FLAGS_num),
396
+ value_size_(FLAGS_value_size),
397
+ entries_per_batch_(1),
398
+ reads_(FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads),
399
+ heap_counter_(0) {
400
+ std::vector<std::string> files;
401
+ Env::Default()->GetChildren(FLAGS_db, &files);
402
+ for (int i = 0; i < files.size(); i++) {
403
+ if (Slice(files[i]).starts_with("heap-")) {
404
+ Env::Default()->DeleteFile(std::string(FLAGS_db) + "/" + files[i]);
405
+ }
406
+ }
407
+ if (!FLAGS_use_existing_db) {
408
+ DestroyDB(FLAGS_db, Options());
409
+ }
410
+ }
411
+
412
+ ~Benchmark() {
413
+ delete db_;
414
+ delete cache_;
415
+ delete filter_policy_;
416
+ }
417
+
418
+ void Run() {
419
+ PrintHeader();
420
+ Open();
421
+
422
+ const char* benchmarks = FLAGS_benchmarks;
423
+ while (benchmarks != NULL) {
424
+ const char* sep = strchr(benchmarks, ',');
425
+ Slice name;
426
+ if (sep == NULL) {
427
+ name = benchmarks;
428
+ benchmarks = NULL;
429
+ } else {
430
+ name = Slice(benchmarks, sep - benchmarks);
431
+ benchmarks = sep + 1;
432
+ }
433
+
434
+ // Reset parameters that may be overriddden bwlow
435
+ num_ = FLAGS_num;
436
+ reads_ = (FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads);
437
+ value_size_ = FLAGS_value_size;
438
+ entries_per_batch_ = 1;
439
+ write_options_ = WriteOptions();
440
+
441
+ void (Benchmark::*method)(ThreadState*) = NULL;
442
+ bool fresh_db = false;
443
+ int num_threads = FLAGS_threads;
444
+
445
+ if (name == Slice("fillseq")) {
446
+ fresh_db = true;
447
+ method = &Benchmark::WriteSeq;
448
+ } else if (name == Slice("fillbatch")) {
449
+ fresh_db = true;
450
+ entries_per_batch_ = 1000;
451
+ method = &Benchmark::WriteSeq;
452
+ } else if (name == Slice("fillrandom")) {
453
+ fresh_db = true;
454
+ method = &Benchmark::WriteRandom;
455
+ } else if (name == Slice("overwrite")) {
456
+ fresh_db = false;
457
+ method = &Benchmark::WriteRandom;
458
+ } else if (name == Slice("fillsync")) {
459
+ fresh_db = true;
460
+ num_ /= 1000;
461
+ write_options_.sync = true;
462
+ method = &Benchmark::WriteRandom;
463
+ } else if (name == Slice("fill100K")) {
464
+ fresh_db = true;
465
+ num_ /= 1000;
466
+ value_size_ = 100 * 1000;
467
+ method = &Benchmark::WriteRandom;
468
+ } else if (name == Slice("readseq")) {
469
+ method = &Benchmark::ReadSequential;
470
+ } else if (name == Slice("readreverse")) {
471
+ method = &Benchmark::ReadReverse;
472
+ } else if (name == Slice("readrandom")) {
473
+ method = &Benchmark::ReadRandom;
474
+ } else if (name == Slice("readmissing")) {
475
+ method = &Benchmark::ReadMissing;
476
+ } else if (name == Slice("seekrandom")) {
477
+ method = &Benchmark::SeekRandom;
478
+ } else if (name == Slice("readhot")) {
479
+ method = &Benchmark::ReadHot;
480
+ } else if (name == Slice("readrandomsmall")) {
481
+ reads_ /= 1000;
482
+ method = &Benchmark::ReadRandom;
483
+ } else if (name == Slice("deleteseq")) {
484
+ method = &Benchmark::DeleteSeq;
485
+ } else if (name == Slice("deleterandom")) {
486
+ method = &Benchmark::DeleteRandom;
487
+ } else if (name == Slice("readwhilewriting")) {
488
+ num_threads++; // Add extra thread for writing
489
+ method = &Benchmark::ReadWhileWriting;
490
+ } else if (name == Slice("compact")) {
491
+ method = &Benchmark::Compact;
492
+ } else if (name == Slice("crc32c")) {
493
+ method = &Benchmark::Crc32c;
494
+ } else if (name == Slice("acquireload")) {
495
+ method = &Benchmark::AcquireLoad;
496
+ } else if (name == Slice("snappycomp")) {
497
+ method = &Benchmark::SnappyCompress;
498
+ } else if (name == Slice("snappyuncomp")) {
499
+ method = &Benchmark::SnappyUncompress;
500
+ } else if (name == Slice("heapprofile")) {
501
+ HeapProfile();
502
+ } else if (name == Slice("stats")) {
503
+ PrintStats("leveldb.stats");
504
+ } else if (name == Slice("sstables")) {
505
+ PrintStats("leveldb.sstables");
506
+ } else {
507
+ if (name != Slice()) { // No error message for empty name
508
+ fprintf(stderr, "unknown benchmark '%s'\n", name.ToString().c_str());
509
+ }
510
+ }
511
+
512
+ if (fresh_db) {
513
+ if (FLAGS_use_existing_db) {
514
+ fprintf(stdout, "%-12s : skipped (--use_existing_db is true)\n",
515
+ name.ToString().c_str());
516
+ method = NULL;
517
+ } else {
518
+ delete db_;
519
+ db_ = NULL;
520
+ DestroyDB(FLAGS_db, Options());
521
+ Open();
522
+ }
523
+ }
524
+
525
+ if (method != NULL) {
526
+ RunBenchmark(num_threads, name, method);
527
+ }
528
+ }
529
+ }
530
+
531
+ private:
532
+ struct ThreadArg {
533
+ Benchmark* bm;
534
+ SharedState* shared;
535
+ ThreadState* thread;
536
+ void (Benchmark::*method)(ThreadState*);
537
+ };
538
+
539
+ static void ThreadBody(void* v) {
540
+ ThreadArg* arg = reinterpret_cast<ThreadArg*>(v);
541
+ SharedState* shared = arg->shared;
542
+ ThreadState* thread = arg->thread;
543
+ {
544
+ MutexLock l(&shared->mu);
545
+ shared->num_initialized++;
546
+ if (shared->num_initialized >= shared->total) {
547
+ shared->cv.SignalAll();
548
+ }
549
+ while (!shared->start) {
550
+ shared->cv.Wait();
551
+ }
552
+ }
553
+
554
+ thread->stats.Start();
555
+ (arg->bm->*(arg->method))(thread);
556
+ thread->stats.Stop();
557
+
558
+ {
559
+ MutexLock l(&shared->mu);
560
+ shared->num_done++;
561
+ if (shared->num_done >= shared->total) {
562
+ shared->cv.SignalAll();
563
+ }
564
+ }
565
+ }
566
+
567
+ void RunBenchmark(int n, Slice name,
568
+ void (Benchmark::*method)(ThreadState*)) {
569
+ SharedState shared;
570
+ shared.total = n;
571
+ shared.num_initialized = 0;
572
+ shared.num_done = 0;
573
+ shared.start = false;
574
+
575
+ ThreadArg* arg = new ThreadArg[n];
576
+ for (int i = 0; i < n; i++) {
577
+ arg[i].bm = this;
578
+ arg[i].method = method;
579
+ arg[i].shared = &shared;
580
+ arg[i].thread = new ThreadState(i);
581
+ arg[i].thread->shared = &shared;
582
+ Env::Default()->StartThread(ThreadBody, &arg[i]);
583
+ }
584
+
585
+ shared.mu.Lock();
586
+ while (shared.num_initialized < n) {
587
+ shared.cv.Wait();
588
+ }
589
+
590
+ shared.start = true;
591
+ shared.cv.SignalAll();
592
+ while (shared.num_done < n) {
593
+ shared.cv.Wait();
594
+ }
595
+ shared.mu.Unlock();
596
+
597
+ for (int i = 1; i < n; i++) {
598
+ arg[0].thread->stats.Merge(arg[i].thread->stats);
599
+ }
600
+ arg[0].thread->stats.Report(name);
601
+
602
+ for (int i = 0; i < n; i++) {
603
+ delete arg[i].thread;
604
+ }
605
+ delete[] arg;
606
+ }
607
+
608
+ void Crc32c(ThreadState* thread) {
609
+ // Checksum about 500MB of data total
610
+ const int size = 4096;
611
+ const char* label = "(4K per op)";
612
+ std::string data(size, 'x');
613
+ int64_t bytes = 0;
614
+ uint32_t crc = 0;
615
+ while (bytes < 500 * 1048576) {
616
+ crc = crc32c::Value(data.data(), size);
617
+ thread->stats.FinishedSingleOp();
618
+ bytes += size;
619
+ }
620
+ // Print so result is not dead
621
+ fprintf(stderr, "... crc=0x%x\r", static_cast<unsigned int>(crc));
622
+
623
+ thread->stats.AddBytes(bytes);
624
+ thread->stats.AddMessage(label);
625
+ }
626
+
627
+ void AcquireLoad(ThreadState* thread) {
628
+ int dummy;
629
+ port::AtomicPointer ap(&dummy);
630
+ int count = 0;
631
+ void *ptr = NULL;
632
+ thread->stats.AddMessage("(each op is 1000 loads)");
633
+ while (count < 100000) {
634
+ for (int i = 0; i < 1000; i++) {
635
+ ptr = ap.Acquire_Load();
636
+ }
637
+ count++;
638
+ thread->stats.FinishedSingleOp();
639
+ }
640
+ if (ptr == NULL) exit(1); // Disable unused variable warning.
641
+ }
642
+
643
+ void SnappyCompress(ThreadState* thread) {
644
+ RandomGenerator gen;
645
+ Slice input = gen.Generate(Options().block_size);
646
+ int64_t bytes = 0;
647
+ int64_t produced = 0;
648
+ bool ok = true;
649
+ std::string compressed;
650
+ while (ok && bytes < 1024 * 1048576) { // Compress 1G
651
+ ok = port::Snappy_Compress(input.data(), input.size(), &compressed);
652
+ produced += compressed.size();
653
+ bytes += input.size();
654
+ thread->stats.FinishedSingleOp();
655
+ }
656
+
657
+ if (!ok) {
658
+ thread->stats.AddMessage("(snappy failure)");
659
+ } else {
660
+ char buf[100];
661
+ snprintf(buf, sizeof(buf), "(output: %.1f%%)",
662
+ (produced * 100.0) / bytes);
663
+ thread->stats.AddMessage(buf);
664
+ thread->stats.AddBytes(bytes);
665
+ }
666
+ }
667
+
668
+ void SnappyUncompress(ThreadState* thread) {
669
+ RandomGenerator gen;
670
+ Slice input = gen.Generate(Options().block_size);
671
+ std::string compressed;
672
+ bool ok = port::Snappy_Compress(input.data(), input.size(), &compressed);
673
+ int64_t bytes = 0;
674
+ char* uncompressed = new char[input.size()];
675
+ while (ok && bytes < 1024 * 1048576) { // Compress 1G
676
+ ok = port::Snappy_Uncompress(compressed.data(), compressed.size(),
677
+ uncompressed);
678
+ bytes += input.size();
679
+ thread->stats.FinishedSingleOp();
680
+ }
681
+ delete[] uncompressed;
682
+
683
+ if (!ok) {
684
+ thread->stats.AddMessage("(snappy failure)");
685
+ } else {
686
+ thread->stats.AddBytes(bytes);
687
+ }
688
+ }
689
+
690
+ void Open() {
691
+ assert(db_ == NULL);
692
+ Options options;
693
+ options.create_if_missing = !FLAGS_use_existing_db;
694
+ options.block_cache = cache_;
695
+ options.write_buffer_size = FLAGS_write_buffer_size;
696
+ options.filter_policy = filter_policy_;
697
+ Status s = DB::Open(options, FLAGS_db, &db_);
698
+ if (!s.ok()) {
699
+ fprintf(stderr, "open error: %s\n", s.ToString().c_str());
700
+ exit(1);
701
+ }
702
+ }
703
+
704
+ void WriteSeq(ThreadState* thread) {
705
+ DoWrite(thread, true);
706
+ }
707
+
708
+ void WriteRandom(ThreadState* thread) {
709
+ DoWrite(thread, false);
710
+ }
711
+
712
+ void DoWrite(ThreadState* thread, bool seq) {
713
+ if (num_ != FLAGS_num) {
714
+ char msg[100];
715
+ snprintf(msg, sizeof(msg), "(%d ops)", num_);
716
+ thread->stats.AddMessage(msg);
717
+ }
718
+
719
+ RandomGenerator gen;
720
+ WriteBatch batch;
721
+ Status s;
722
+ int64_t bytes = 0;
723
+ for (int i = 0; i < num_; i += entries_per_batch_) {
724
+ batch.Clear();
725
+ for (int j = 0; j < entries_per_batch_; j++) {
726
+ const int k = seq ? i+j : (thread->rand.Next() % FLAGS_num);
727
+ char key[100];
728
+ snprintf(key, sizeof(key), "%016d", k);
729
+ batch.Put(key, gen.Generate(value_size_));
730
+ bytes += value_size_ + strlen(key);
731
+ thread->stats.FinishedSingleOp();
732
+ }
733
+ s = db_->Write(write_options_, &batch);
734
+ if (!s.ok()) {
735
+ fprintf(stderr, "put error: %s\n", s.ToString().c_str());
736
+ exit(1);
737
+ }
738
+ }
739
+ thread->stats.AddBytes(bytes);
740
+ }
741
+
742
+ void ReadSequential(ThreadState* thread) {
743
+ Iterator* iter = db_->NewIterator(ReadOptions());
744
+ int i = 0;
745
+ int64_t bytes = 0;
746
+ for (iter->SeekToFirst(); i < reads_ && iter->Valid(); iter->Next()) {
747
+ bytes += iter->key().size() + iter->value().size();
748
+ thread->stats.FinishedSingleOp();
749
+ ++i;
750
+ }
751
+ delete iter;
752
+ thread->stats.AddBytes(bytes);
753
+ }
754
+
755
+ void ReadReverse(ThreadState* thread) {
756
+ Iterator* iter = db_->NewIterator(ReadOptions());
757
+ int i = 0;
758
+ int64_t bytes = 0;
759
+ for (iter->SeekToLast(); i < reads_ && iter->Valid(); iter->Prev()) {
760
+ bytes += iter->key().size() + iter->value().size();
761
+ thread->stats.FinishedSingleOp();
762
+ ++i;
763
+ }
764
+ delete iter;
765
+ thread->stats.AddBytes(bytes);
766
+ }
767
+
768
+ void ReadRandom(ThreadState* thread) {
769
+ ReadOptions options;
770
+ std::string value;
771
+ int found = 0;
772
+ for (int i = 0; i < reads_; i++) {
773
+ char key[100];
774
+ const int k = thread->rand.Next() % FLAGS_num;
775
+ snprintf(key, sizeof(key), "%016d", k);
776
+ if (db_->Get(options, key, &value).ok()) {
777
+ found++;
778
+ }
779
+ thread->stats.FinishedSingleOp();
780
+ }
781
+ char msg[100];
782
+ snprintf(msg, sizeof(msg), "(%d of %d found)", found, num_);
783
+ thread->stats.AddMessage(msg);
784
+ }
785
+
786
+ void ReadMissing(ThreadState* thread) {
787
+ ReadOptions options;
788
+ std::string value;
789
+ for (int i = 0; i < reads_; i++) {
790
+ char key[100];
791
+ const int k = thread->rand.Next() % FLAGS_num;
792
+ snprintf(key, sizeof(key), "%016d.", k);
793
+ db_->Get(options, key, &value);
794
+ thread->stats.FinishedSingleOp();
795
+ }
796
+ }
797
+
798
+ void ReadHot(ThreadState* thread) {
799
+ ReadOptions options;
800
+ std::string value;
801
+ const int range = (FLAGS_num + 99) / 100;
802
+ for (int i = 0; i < reads_; i++) {
803
+ char key[100];
804
+ const int k = thread->rand.Next() % range;
805
+ snprintf(key, sizeof(key), "%016d", k);
806
+ db_->Get(options, key, &value);
807
+ thread->stats.FinishedSingleOp();
808
+ }
809
+ }
810
+
811
+ void SeekRandom(ThreadState* thread) {
812
+ ReadOptions options;
813
+ std::string value;
814
+ int found = 0;
815
+ for (int i = 0; i < reads_; i++) {
816
+ Iterator* iter = db_->NewIterator(options);
817
+ char key[100];
818
+ const int k = thread->rand.Next() % FLAGS_num;
819
+ snprintf(key, sizeof(key), "%016d", k);
820
+ iter->Seek(key);
821
+ if (iter->Valid() && iter->key() == key) found++;
822
+ delete iter;
823
+ thread->stats.FinishedSingleOp();
824
+ }
825
+ char msg[100];
826
+ snprintf(msg, sizeof(msg), "(%d of %d found)", found, num_);
827
+ thread->stats.AddMessage(msg);
828
+ }
829
+
830
+ void DoDelete(ThreadState* thread, bool seq) {
831
+ RandomGenerator gen;
832
+ WriteBatch batch;
833
+ Status s;
834
+ for (int i = 0; i < num_; i += entries_per_batch_) {
835
+ batch.Clear();
836
+ for (int j = 0; j < entries_per_batch_; j++) {
837
+ const int k = seq ? i+j : (thread->rand.Next() % FLAGS_num);
838
+ char key[100];
839
+ snprintf(key, sizeof(key), "%016d", k);
840
+ batch.Delete(key);
841
+ thread->stats.FinishedSingleOp();
842
+ }
843
+ s = db_->Write(write_options_, &batch);
844
+ if (!s.ok()) {
845
+ fprintf(stderr, "del error: %s\n", s.ToString().c_str());
846
+ exit(1);
847
+ }
848
+ }
849
+ }
850
+
851
+ void DeleteSeq(ThreadState* thread) {
852
+ DoDelete(thread, true);
853
+ }
854
+
855
+ void DeleteRandom(ThreadState* thread) {
856
+ DoDelete(thread, false);
857
+ }
858
+
859
+ void ReadWhileWriting(ThreadState* thread) {
860
+ if (thread->tid > 0) {
861
+ ReadRandom(thread);
862
+ } else {
863
+ // Special thread that keeps writing until other threads are done.
864
+ RandomGenerator gen;
865
+ while (true) {
866
+ {
867
+ MutexLock l(&thread->shared->mu);
868
+ if (thread->shared->num_done + 1 >= thread->shared->num_initialized) {
869
+ // Other threads have finished
870
+ break;
871
+ }
872
+ }
873
+
874
+ const int k = thread->rand.Next() % FLAGS_num;
875
+ char key[100];
876
+ snprintf(key, sizeof(key), "%016d", k);
877
+ Status s = db_->Put(write_options_, key, gen.Generate(value_size_));
878
+ if (!s.ok()) {
879
+ fprintf(stderr, "put error: %s\n", s.ToString().c_str());
880
+ exit(1);
881
+ }
882
+ }
883
+
884
+ // Do not count any of the preceding work/delay in stats.
885
+ thread->stats.Start();
886
+ }
887
+ }
888
+
889
+ void Compact(ThreadState* thread) {
890
+ db_->CompactRange(NULL, NULL);
891
+ }
892
+
893
+ void PrintStats(const char* key) {
894
+ std::string stats;
895
+ if (!db_->GetProperty(key, &stats)) {
896
+ stats = "(failed)";
897
+ }
898
+ fprintf(stdout, "\n%s\n", stats.c_str());
899
+ }
900
+
901
+ static void WriteToFile(void* arg, const char* buf, int n) {
902
+ reinterpret_cast<WritableFile*>(arg)->Append(Slice(buf, n));
903
+ }
904
+
905
+ void HeapProfile() {
906
+ char fname[100];
907
+ snprintf(fname, sizeof(fname), "%s/heap-%04d", FLAGS_db, ++heap_counter_);
908
+ WritableFile* file;
909
+ Status s = Env::Default()->NewWritableFile(fname, &file);
910
+ if (!s.ok()) {
911
+ fprintf(stderr, "%s\n", s.ToString().c_str());
912
+ return;
913
+ }
914
+ bool ok = port::GetHeapProfile(WriteToFile, file);
915
+ delete file;
916
+ if (!ok) {
917
+ fprintf(stderr, "heap profiling not supported\n");
918
+ Env::Default()->DeleteFile(fname);
919
+ }
920
+ }
921
+ };
922
+
923
+ } // namespace leveldb
924
+
925
+ int main(int argc, char** argv) {
926
+ FLAGS_write_buffer_size = leveldb::Options().write_buffer_size;
927
+ FLAGS_open_files = leveldb::Options().max_open_files;
928
+
929
+ for (int i = 1; i < argc; i++) {
930
+ double d;
931
+ int n;
932
+ char junk;
933
+ if (leveldb::Slice(argv[i]).starts_with("--benchmarks=")) {
934
+ FLAGS_benchmarks = argv[i] + strlen("--benchmarks=");
935
+ } else if (sscanf(argv[i], "--compression_ratio=%lf%c", &d, &junk) == 1) {
936
+ FLAGS_compression_ratio = d;
937
+ } else if (sscanf(argv[i], "--histogram=%d%c", &n, &junk) == 1 &&
938
+ (n == 0 || n == 1)) {
939
+ FLAGS_histogram = n;
940
+ } else if (sscanf(argv[i], "--use_existing_db=%d%c", &n, &junk) == 1 &&
941
+ (n == 0 || n == 1)) {
942
+ FLAGS_use_existing_db = n;
943
+ } else if (sscanf(argv[i], "--num=%d%c", &n, &junk) == 1) {
944
+ FLAGS_num = n;
945
+ } else if (sscanf(argv[i], "--reads=%d%c", &n, &junk) == 1) {
946
+ FLAGS_reads = n;
947
+ } else if (sscanf(argv[i], "--threads=%d%c", &n, &junk) == 1) {
948
+ FLAGS_threads = n;
949
+ } else if (sscanf(argv[i], "--value_size=%d%c", &n, &junk) == 1) {
950
+ FLAGS_value_size = n;
951
+ } else if (sscanf(argv[i], "--write_buffer_size=%d%c", &n, &junk) == 1) {
952
+ FLAGS_write_buffer_size = n;
953
+ } else if (sscanf(argv[i], "--cache_size=%d%c", &n, &junk) == 1) {
954
+ FLAGS_cache_size = n;
955
+ } else if (sscanf(argv[i], "--bloom_bits=%d%c", &n, &junk) == 1) {
956
+ FLAGS_bloom_bits = n;
957
+ } else if (sscanf(argv[i], "--open_files=%d%c", &n, &junk) == 1) {
958
+ FLAGS_open_files = n;
959
+ } else if (strncmp(argv[i], "--db=", 5) == 0) {
960
+ FLAGS_db = argv[i] + 5;
961
+ } else {
962
+ fprintf(stderr, "Invalid flag '%s'\n", argv[i]);
963
+ exit(1);
964
+ }
965
+ }
966
+
967
+ leveldb::Benchmark benchmark;
968
+ benchmark.Run();
969
+ return 0;
970
+ }