25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1138 lines
34 KiB

3 주 전
  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. #include <sys/types.h>
  5. #include <atomic>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include "leveldb/cache.h"
  9. #include "leveldb/comparator.h"
  10. #include "leveldb/db.h"
  11. #include "leveldb/env.h"
  12. #include "leveldb/filter_policy.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. // Comma-separated list of operations to run in the specified order
  21. // Actual benchmarks:
  22. // fillseq -- write N values in sequential key order in async mode
  23. // fillrandom -- write N values in random key order in async mode
  24. // overwrite -- overwrite N values in random key order in async mode
  25. // fillsync -- write N/100 values in random key order in sync mode
  26. // fill100K -- write N/1000 100K values in random order in async mode
  27. // deleteseq -- delete N keys in sequential order
  28. // deleterandom -- delete N keys in random order
  29. // readseq -- read N times sequentially
  30. // readreverse -- read N times in reverse order
  31. // readrandom -- read N times in random order
  32. // readmissing -- read N missing keys in random order
  33. // readhot -- read N times in random order from 1% section of DB
  34. // seekrandom -- N random seeks
  35. // seekordered -- N ordered seeks
  36. // open -- cost of opening a DB
  37. // crc32c -- repeated crc32c of 4K of data
  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. "zstdcomp,"
  61. "zstduncomp,";
  62. // Number of key/values to place in database
  63. static int FLAGS_num = 1000000;
  64. // Number of read operations to do. If negative, do FLAGS_num reads.
  65. static int FLAGS_reads = -1;
  66. // Number of concurrent threads to run.
  67. static int FLAGS_threads = 1;
  68. // Size of each value
  69. static int FLAGS_value_size = 100;
  70. // Arrange to generate values that shrink to this fraction of
  71. // their original size after compression
  72. static double FLAGS_compression_ratio = 0.5;
  73. // Print histogram of operation timings
  74. static bool FLAGS_histogram = false;
  75. // Count the number of string comparisons performed
  76. static bool FLAGS_comparisons = false;
  77. // Number of bytes to buffer in memtable before compacting
  78. // (initialized to default value by "main")
  79. static int FLAGS_write_buffer_size = 0;
  80. // Number of bytes written to each file.
  81. // (initialized to default value by "main")
  82. static int FLAGS_max_file_size = 0;
  83. // Approximate size of user data packed per block (before compression.
  84. // (initialized to default value by "main")
  85. static int FLAGS_block_size = 0;
  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. // Maximum number of files to keep open at the same time (use default if == 0)
  90. static int FLAGS_open_files = 0;
  91. // Bloom filter bits per key.
  92. // Negative means use default settings.
  93. static int FLAGS_bloom_bits = -1;
  94. // Common key prefix length.
  95. static int FLAGS_key_prefix = 0;
  96. // If true, do not destroy the existing database. If you set this
  97. // flag and also specify a benchmark that wants a fresh database, that
  98. // benchmark will fail.
  99. static bool FLAGS_use_existing_db = false;
  100. // If true, reuse existing log/MANIFEST files when re-opening a database.
  101. static bool FLAGS_reuse_logs = false;
  102. // If true, use compression.
  103. static bool FLAGS_compression = true;
  104. // Use the db with the following name.
  105. static const char* FLAGS_db = nullptr;
  106. // ZSTD compression level to try out
  107. static int FLAGS_zstd_compression_level = 1;
  108. namespace leveldb {
  109. namespace {
  110. leveldb::Env* g_env = nullptr;
  111. class CountComparator : public Comparator {
  112. public:
  113. CountComparator(const Comparator* wrapped) : wrapped_(wrapped) {}
  114. ~CountComparator() override {}
  115. int Compare(const Slice& a, const Slice& b) const override {
  116. count_.fetch_add(1, std::memory_order_relaxed);
  117. return wrapped_->Compare(a, b);
  118. }
  119. const char* Name() const override { return wrapped_->Name(); }
  120. void FindShortestSeparator(std::string* start,
  121. const Slice& limit) const override {
  122. wrapped_->FindShortestSeparator(start, limit);
  123. }
  124. void FindShortSuccessor(std::string* key) const override {
  125. return wrapped_->FindShortSuccessor(key);
  126. }
  127. size_t comparisons() const { return count_.load(std::memory_order_relaxed); }
  128. void reset() { count_.store(0, std::memory_order_relaxed); }
  129. private:
  130. mutable std::atomic<size_t> count_{0};
  131. const Comparator* const wrapped_;
  132. };
  133. // Helper for quickly generating random data.
  134. class RandomGenerator {
  135. private:
  136. std::string data_;
  137. int pos_;
  138. public:
  139. RandomGenerator() {
  140. // We use a limited amount of data over and over again and ensure
  141. // that it is larger than the compression window (32KB), and also
  142. // large enough to serve all typical value sizes we want to write.
  143. Random rnd(301);
  144. std::string piece;
  145. while (data_.size() < 1048576) {
  146. // Add a short fragment that is as compressible as specified
  147. // by FLAGS_compression_ratio.
  148. test::CompressibleString(&rnd, FLAGS_compression_ratio, 100, &piece);
  149. data_.append(piece);
  150. }
  151. pos_ = 0;
  152. }
  153. Slice Generate(size_t len) {
  154. if (pos_ + len > data_.size()) {
  155. pos_ = 0;
  156. assert(len < data_.size());
  157. }
  158. pos_ += len;
  159. return Slice(data_.data() + pos_ - len, len);
  160. }
  161. };
  162. class KeyBuffer {
  163. public:
  164. KeyBuffer() {
  165. assert(FLAGS_key_prefix < sizeof(buffer_));
  166. memset(buffer_, 'a', FLAGS_key_prefix);
  167. }
  168. KeyBuffer& operator=(KeyBuffer& other) = delete;
  169. KeyBuffer(KeyBuffer& other) = delete;
  170. void Set(int k) {
  171. std::snprintf(buffer_ + FLAGS_key_prefix,
  172. sizeof(buffer_) - FLAGS_key_prefix, "%016d", k);
  173. }
  174. Slice slice() const { return Slice(buffer_, FLAGS_key_prefix + 16); }
  175. private:
  176. char buffer_[1024];
  177. };
  178. #if defined(__linux)
  179. static Slice TrimSpace(Slice s) {
  180. size_t start = 0;
  181. while (start < s.size() && isspace(s[start])) {
  182. start++;
  183. }
  184. size_t limit = s.size();
  185. while (limit > start && isspace(s[limit - 1])) {
  186. limit--;
  187. }
  188. return Slice(s.data() + start, limit - start);
  189. }
  190. #endif
  191. static void AppendWithSpace(std::string* str, Slice msg) {
  192. if (msg.empty()) return;
  193. if (!str->empty()) {
  194. str->push_back(' ');
  195. }
  196. str->append(msg.data(), msg.size());
  197. }
  198. class Stats {
  199. private:
  200. double start_;
  201. double finish_;
  202. double seconds_;
  203. int done_;
  204. int next_report_;
  205. int64_t bytes_;
  206. double last_op_finish_;
  207. Histogram hist_;
  208. std::string message_;
  209. public:
  210. Stats() { Start(); }
  211. void Start() {
  212. next_report_ = 100;
  213. hist_.Clear();
  214. done_ = 0;
  215. bytes_ = 0;
  216. seconds_ = 0;
  217. message_.clear();
  218. start_ = finish_ = last_op_finish_ = g_env->NowMicros();
  219. }
  220. void Merge(const Stats& other) {
  221. hist_.Merge(other.hist_);
  222. done_ += other.done_;
  223. bytes_ += other.bytes_;
  224. seconds_ += other.seconds_;
  225. if (other.start_ < start_) start_ = other.start_;
  226. if (other.finish_ > finish_) finish_ = other.finish_;
  227. // Just keep the messages from one thread
  228. if (message_.empty()) message_ = other.message_;
  229. }
  230. void Stop() {
  231. finish_ = g_env->NowMicros();
  232. seconds_ = (finish_ - start_) * 1e-6;
  233. }
  234. void AddMessage(Slice msg) { AppendWithSpace(&message_, msg); }
  235. void FinishedSingleOp() {
  236. if (FLAGS_histogram) {
  237. double now = g_env->NowMicros();
  238. double micros = now - last_op_finish_;
  239. hist_.Add(micros);
  240. if (micros > 20000) {
  241. std::fprintf(stderr, "long op: %.1f micros%30s\r", micros, "");
  242. std::fflush(stderr);
  243. }
  244. last_op_finish_ = now;
  245. }
  246. done_++;
  247. if (done_ >= next_report_) {
  248. if (next_report_ < 1000)
  249. next_report_ += 100;
  250. else if (next_report_ < 5000)
  251. next_report_ += 500;
  252. else if (next_report_ < 10000)
  253. next_report_ += 1000;
  254. else if (next_report_ < 50000)
  255. next_report_ += 5000;
  256. else if (next_report_ < 100000)
  257. next_report_ += 10000;
  258. else if (next_report_ < 500000)
  259. next_report_ += 50000;
  260. else
  261. next_report_ += 100000;
  262. std::fprintf(stderr, "... finished %d ops%30s\r", done_, "");
  263. std::fflush(stderr);
  264. }
  265. }
  266. void AddBytes(int64_t n) { bytes_ += n; }
  267. void Report(const Slice& name) {
  268. // Pretend at least one op was done in case we are running a benchmark
  269. // that does not call FinishedSingleOp().
  270. if (done_ < 1) done_ = 1;
  271. std::string extra;
  272. if (bytes_ > 0) {
  273. // Rate is computed on actual elapsed time, not the sum of per-thread
  274. // elapsed times.
  275. double elapsed = (finish_ - start_) * 1e-6;
  276. char rate[100];
  277. std::snprintf(rate, sizeof(rate), "%6.1f MB/s",
  278. (bytes_ / 1048576.0) / elapsed);
  279. extra = rate;
  280. }
  281. AppendWithSpace(&extra, message_);
  282. std::fprintf(stdout, "%-12s : %11.3f micros/op;%s%s\n",
  283. name.ToString().c_str(), seconds_ * 1e6 / done_,
  284. (extra.empty() ? "" : " "), extra.c_str());
  285. if (FLAGS_histogram) {
  286. std::fprintf(stdout, "Microseconds per op:\n%s\n",
  287. hist_.ToString().c_str());
  288. }
  289. std::fflush(stdout);
  290. }
  291. };
  292. // State shared by all concurrent executions of the same benchmark.
  293. struct SharedState {
  294. port::Mutex mu;
  295. port::CondVar cv GUARDED_BY(mu);
  296. int total GUARDED_BY(mu);
  297. // Each thread goes through the following states:
  298. // (1) initializing
  299. // (2) waiting for others to be initialized
  300. // (3) running
  301. // (4) done
  302. int num_initialized GUARDED_BY(mu);
  303. int num_done GUARDED_BY(mu);
  304. bool start GUARDED_BY(mu);
  305. SharedState(int total)
  306. : cv(&mu), total(total), num_initialized(0), num_done(0), start(false) {}
  307. };
  308. // Per-thread state for concurrent executions of the same benchmark.
  309. struct ThreadState {
  310. int tid; // 0..n-1 when running in n threads
  311. Random rand; // Has different seeds for different threads
  312. Stats stats;
  313. SharedState* shared;
  314. ThreadState(int index, int seed) : tid(index), rand(seed), shared(nullptr) {}
  315. };
  316. void Compress(
  317. ThreadState* thread, std::string name,
  318. std::function<bool(const char*, size_t, std::string*)> compress_func) {
  319. RandomGenerator gen;
  320. Slice input = gen.Generate(Options().block_size);
  321. int64_t bytes = 0;
  322. int64_t produced = 0;
  323. bool ok = true;
  324. std::string compressed;
  325. while (ok && bytes < 1024 * 1048576) { // Compress 1G
  326. ok = compress_func(input.data(), input.size(), &compressed);
  327. produced += compressed.size();
  328. bytes += input.size();
  329. thread->stats.FinishedSingleOp();
  330. }
  331. if (!ok) {
  332. thread->stats.AddMessage("(" + name + " failure)");
  333. } else {
  334. char buf[100];
  335. std::snprintf(buf, sizeof(buf), "(output: %.1f%%)",
  336. (produced * 100.0) / bytes);
  337. thread->stats.AddMessage(buf);
  338. thread->stats.AddBytes(bytes);
  339. }
  340. }
  341. void Uncompress(
  342. ThreadState* thread, std::string name,
  343. std::function<bool(const char*, size_t, std::string*)> compress_func,
  344. std::function<bool(const char*, size_t, char*)> uncompress_func) {
  345. RandomGenerator gen;
  346. Slice input = gen.Generate(Options().block_size);
  347. std::string compressed;
  348. bool ok = compress_func(input.data(), input.size(), &compressed);
  349. int64_t bytes = 0;
  350. char* uncompressed = new char[input.size()];
  351. while (ok && bytes < 1024 * 1048576) { // Compress 1G
  352. ok = uncompress_func(compressed.data(), compressed.size(), uncompressed);
  353. bytes += input.size();
  354. thread->stats.FinishedSingleOp();
  355. }
  356. delete[] uncompressed;
  357. if (!ok) {
  358. thread->stats.AddMessage("(" + name + " failure)");
  359. } else {
  360. thread->stats.AddBytes(bytes);
  361. }
  362. }
  363. } // namespace
  364. class Benchmark {
  365. private:
  366. Cache* cache_;
  367. const FilterPolicy* filter_policy_;
  368. DB* db_;
  369. int num_;
  370. int value_size_;
  371. int entries_per_batch_;
  372. WriteOptions write_options_;
  373. int reads_;
  374. int heap_counter_;
  375. CountComparator count_comparator_;
  376. int total_thread_count_;
  377. void PrintHeader() {
  378. const int kKeySize = 16 + FLAGS_key_prefix;
  379. PrintEnvironment();
  380. std::fprintf(stdout, "Keys: %d bytes each\n", kKeySize);
  381. std::fprintf(
  382. stdout, "Values: %d bytes each (%d bytes after compression)\n",
  383. FLAGS_value_size,
  384. static_cast<int>(FLAGS_value_size * FLAGS_compression_ratio + 0.5));
  385. std::fprintf(stdout, "Entries: %d\n", num_);
  386. std::fprintf(stdout, "RawSize: %.1f MB (estimated)\n",
  387. ((static_cast<int64_t>(kKeySize + FLAGS_value_size) * num_) /
  388. 1048576.0));
  389. std::fprintf(
  390. stdout, "FileSize: %.1f MB (estimated)\n",
  391. (((kKeySize + FLAGS_value_size * FLAGS_compression_ratio) * num_) /
  392. 1048576.0));
  393. PrintWarnings();
  394. std::fprintf(stdout, "------------------------------------------------\n");
  395. }
  396. void PrintWarnings() {
  397. #if defined(__GNUC__) && !defined(__OPTIMIZE__)
  398. std::fprintf(
  399. stdout,
  400. "WARNING: Optimization is disabled: benchmarks unnecessarily slow\n");
  401. #endif
  402. #ifndef NDEBUG
  403. std::fprintf(
  404. stdout,
  405. "WARNING: Assertions are enabled; benchmarks unnecessarily slow\n");
  406. #endif
  407. // See if snappy is working by attempting to compress a compressible string
  408. const char text[] = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
  409. std::string compressed;
  410. if (!port::Snappy_Compress(text, sizeof(text), &compressed)) {
  411. std::fprintf(stdout, "WARNING: Snappy compression is not enabled\n");
  412. } else if (compressed.size() >= sizeof(text)) {
  413. std::fprintf(stdout, "WARNING: Snappy compression is not effective\n");
  414. }
  415. }
  416. void PrintEnvironment() {
  417. std::fprintf(stderr, "LevelDB: version %d.%d\n", kMajorVersion,
  418. kMinorVersion);
  419. #if defined(__linux)
  420. time_t now = time(nullptr);
  421. std::fprintf(stderr, "Date: %s",
  422. ctime(&now)); // ctime() adds newline
  423. FILE* cpuinfo = std::fopen("/proc/cpuinfo", "r");
  424. if (cpuinfo != nullptr) {
  425. char line[1000];
  426. int num_cpus = 0;
  427. std::string cpu_type;
  428. std::string cache_size;
  429. while (fgets(line, sizeof(line), cpuinfo) != nullptr) {
  430. const char* sep = strchr(line, ':');
  431. if (sep == nullptr) {
  432. continue;
  433. }
  434. Slice key = TrimSpace(Slice(line, sep - 1 - line));
  435. Slice val = TrimSpace(Slice(sep + 1));
  436. if (key == "model name") {
  437. ++num_cpus;
  438. cpu_type = val.ToString();
  439. } else if (key == "cache size") {
  440. cache_size = val.ToString();
  441. }
  442. }
  443. std::fclose(cpuinfo);
  444. std::fprintf(stderr, "CPU: %d * %s\n", num_cpus, cpu_type.c_str());
  445. std::fprintf(stderr, "CPUCache: %s\n", cache_size.c_str());
  446. }
  447. #endif
  448. }
  449. public:
  450. Benchmark()
  451. : cache_(FLAGS_cache_size >= 0 ? NewLRUCache(FLAGS_cache_size) : nullptr),
  452. filter_policy_(FLAGS_bloom_bits >= 0
  453. ? NewBloomFilterPolicy(FLAGS_bloom_bits)
  454. : nullptr),
  455. db_(nullptr),
  456. num_(FLAGS_num),
  457. value_size_(FLAGS_value_size),
  458. entries_per_batch_(1),
  459. reads_(FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads),
  460. heap_counter_(0),
  461. count_comparator_(BytewiseComparator()),
  462. total_thread_count_(0) {
  463. std::vector<std::string> files;
  464. g_env->GetChildren(FLAGS_db, &files);
  465. for (size_t i = 0; i < files.size(); i++) {
  466. if (Slice(files[i]).starts_with("heap-")) {
  467. g_env->RemoveFile(std::string(FLAGS_db) + "/" + files[i]);
  468. }
  469. }
  470. if (!FLAGS_use_existing_db) {
  471. DestroyDB(FLAGS_db, Options());
  472. }
  473. }
  474. ~Benchmark() {
  475. delete db_;
  476. delete cache_;
  477. delete filter_policy_;
  478. }
  479. void Run() {
  480. PrintHeader();
  481. Open();
  482. const char* benchmarks = FLAGS_benchmarks;
  483. while (benchmarks != nullptr) {
  484. const char* sep = strchr(benchmarks, ',');
  485. Slice name;
  486. if (sep == nullptr) {
  487. name = benchmarks;
  488. benchmarks = nullptr;
  489. } else {
  490. name = Slice(benchmarks, sep - benchmarks);
  491. benchmarks = sep + 1;
  492. }
  493. // Reset parameters that may be overridden below
  494. num_ = FLAGS_num;
  495. reads_ = (FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads);
  496. value_size_ = FLAGS_value_size;
  497. entries_per_batch_ = 1;
  498. write_options_ = WriteOptions();
  499. void (Benchmark::*method)(ThreadState*) = nullptr;
  500. bool fresh_db = false;
  501. int num_threads = FLAGS_threads;
  502. if (name == Slice("open")) {
  503. method = &Benchmark::OpenBench;
  504. num_ /= 10000;
  505. if (num_ < 1) num_ = 1;
  506. } else if (name == Slice("fillseq")) {
  507. fresh_db = true;
  508. method = &Benchmark::WriteSeq;
  509. } else if (name == Slice("fillbatch")) {
  510. fresh_db = true;
  511. entries_per_batch_ = 1000;
  512. method = &Benchmark::WriteSeq;
  513. } else if (name == Slice("fillrandom")) {
  514. fresh_db = true;
  515. method = &Benchmark::WriteRandom;
  516. } else if (name == Slice("overwrite")) {
  517. fresh_db = false;
  518. method = &Benchmark::WriteRandom;
  519. } else if (name == Slice("fillsync")) {
  520. fresh_db = true;
  521. num_ /= 1000;
  522. write_options_.sync = true;
  523. method = &Benchmark::WriteRandom;
  524. } else if (name == Slice("fill100K")) {
  525. fresh_db = true;
  526. num_ /= 1000;
  527. value_size_ = 100 * 1000;
  528. method = &Benchmark::WriteRandom;
  529. } else if (name == Slice("readseq")) {
  530. method = &Benchmark::ReadSequential;
  531. } else if (name == Slice("readreverse")) {
  532. method = &Benchmark::ReadReverse;
  533. } else if (name == Slice("readrandom")) {
  534. method = &Benchmark::ReadRandom;
  535. } else if (name == Slice("readmissing")) {
  536. method = &Benchmark::ReadMissing;
  537. } else if (name == Slice("seekrandom")) {
  538. method = &Benchmark::SeekRandom;
  539. } else if (name == Slice("seekordered")) {
  540. method = &Benchmark::SeekOrdered;
  541. } else if (name == Slice("readhot")) {
  542. method = &Benchmark::ReadHot;
  543. } else if (name == Slice("readrandomsmall")) {
  544. reads_ /= 1000;
  545. method = &Benchmark::ReadRandom;
  546. } else if (name == Slice("deleteseq")) {
  547. method = &Benchmark::DeleteSeq;
  548. } else if (name == Slice("deleterandom")) {
  549. method = &Benchmark::DeleteRandom;
  550. } else if (name == Slice("readwhilewriting")) {
  551. num_threads++; // Add extra thread for writing
  552. method = &Benchmark::ReadWhileWriting;
  553. } else if (name == Slice("compact")) {
  554. method = &Benchmark::Compact;
  555. } else if (name == Slice("crc32c")) {
  556. method = &Benchmark::Crc32c;
  557. } else if (name == Slice("snappycomp")) {
  558. method = &Benchmark::SnappyCompress;
  559. } else if (name == Slice("snappyuncomp")) {
  560. method = &Benchmark::SnappyUncompress;
  561. } else if (name == Slice("zstdcomp")) {
  562. method = &Benchmark::ZstdCompress;
  563. } else if (name == Slice("zstduncomp")) {
  564. method = &Benchmark::ZstdUncompress;
  565. } else if (name == Slice("heapprofile")) {
  566. HeapProfile();
  567. } else if (name == Slice("stats")) {
  568. PrintStats("leveldb.stats");
  569. } else if (name == Slice("sstables")) {
  570. PrintStats("leveldb.sstables");
  571. } else {
  572. if (!name.empty()) { // No error message for empty name
  573. std::fprintf(stderr, "unknown benchmark '%s'\n",
  574. name.ToString().c_str());
  575. }
  576. }
  577. if (fresh_db) {
  578. if (FLAGS_use_existing_db) {
  579. std::fprintf(stdout, "%-12s : skipped (--use_existing_db is true)\n",
  580. name.ToString().c_str());
  581. method = nullptr;
  582. } else {
  583. delete db_;
  584. db_ = nullptr;
  585. DestroyDB(FLAGS_db, Options());
  586. Open();
  587. }
  588. }
  589. if (method != nullptr) {
  590. RunBenchmark(num_threads, name, method);
  591. }
  592. }
  593. }
  594. private:
  595. struct ThreadArg {
  596. Benchmark* bm;
  597. SharedState* shared;
  598. ThreadState* thread;
  599. void (Benchmark::*method)(ThreadState*);
  600. };
  601. static void ThreadBody(void* v) {
  602. ThreadArg* arg = reinterpret_cast<ThreadArg*>(v);
  603. SharedState* shared = arg->shared;
  604. ThreadState* thread = arg->thread;
  605. {
  606. MutexLock l(&shared->mu);
  607. shared->num_initialized++;
  608. if (shared->num_initialized >= shared->total) {
  609. shared->cv.SignalAll();
  610. }
  611. while (!shared->start) {
  612. shared->cv.Wait();
  613. }
  614. }
  615. thread->stats.Start();
  616. (arg->bm->*(arg->method))(thread);
  617. thread->stats.Stop();
  618. {
  619. MutexLock l(&shared->mu);
  620. shared->num_done++;
  621. if (shared->num_done >= shared->total) {
  622. shared->cv.SignalAll();
  623. }
  624. }
  625. }
  626. void RunBenchmark(int n, Slice name,
  627. void (Benchmark::*method)(ThreadState*)) {
  628. SharedState shared(n);
  629. ThreadArg* arg = new ThreadArg[n];
  630. for (int i = 0; i < n; i++) {
  631. arg[i].bm = this;
  632. arg[i].method = method;
  633. arg[i].shared = &shared;
  634. ++total_thread_count_;
  635. // Seed the thread's random state deterministically based upon thread
  636. // creation across all benchmarks. This ensures that the seeds are unique
  637. // but reproducible when rerunning the same set of benchmarks.
  638. arg[i].thread = new ThreadState(i, /*seed=*/1000 + total_thread_count_);
  639. arg[i].thread->shared = &shared;
  640. g_env->StartThread(ThreadBody, &arg[i]);
  641. }
  642. shared.mu.Lock();
  643. while (shared.num_initialized < n) {
  644. shared.cv.Wait();
  645. }
  646. shared.start = true;
  647. shared.cv.SignalAll();
  648. while (shared.num_done < n) {
  649. shared.cv.Wait();
  650. }
  651. shared.mu.Unlock();
  652. for (int i = 1; i < n; i++) {
  653. arg[0].thread->stats.Merge(arg[i].thread->stats);
  654. }
  655. arg[0].thread->stats.Report(name);
  656. if (FLAGS_comparisons) {
  657. fprintf(stdout, "Comparisons: %zu\n", count_comparator_.comparisons());
  658. count_comparator_.reset();
  659. fflush(stdout);
  660. }
  661. for (int i = 0; i < n; i++) {
  662. delete arg[i].thread;
  663. }
  664. delete[] arg;
  665. }
  666. void Crc32c(ThreadState* thread) {
  667. // Checksum about 500MB of data total
  668. const int size = 4096;
  669. const char* label = "(4K per op)";
  670. std::string data(size, 'x');
  671. int64_t bytes = 0;
  672. uint32_t crc = 0;
  673. while (bytes < 500 * 1048576) {
  674. crc = crc32c::Value(data.data(), size);
  675. thread->stats.FinishedSingleOp();
  676. bytes += size;
  677. }
  678. // Print so result is not dead
  679. std::fprintf(stderr, "... crc=0x%x\r", static_cast<unsigned int>(crc));
  680. thread->stats.AddBytes(bytes);
  681. thread->stats.AddMessage(label);
  682. }
  683. void SnappyCompress(ThreadState* thread) {
  684. Compress(thread, "snappy", &port::Snappy_Compress);
  685. }
  686. void SnappyUncompress(ThreadState* thread) {
  687. Uncompress(thread, "snappy", &port::Snappy_Compress,
  688. &port::Snappy_Uncompress);
  689. }
  690. void ZstdCompress(ThreadState* thread) {
  691. Compress(thread, "zstd",
  692. [](const char* input, size_t length, std::string* output) {
  693. return port::Zstd_Compress(FLAGS_zstd_compression_level, input,
  694. length, output);
  695. });
  696. }
  697. void ZstdUncompress(ThreadState* thread) {
  698. Uncompress(
  699. thread, "zstd",
  700. [](const char* input, size_t length, std::string* output) {
  701. return port::Zstd_Compress(FLAGS_zstd_compression_level, input,
  702. length, output);
  703. },
  704. &port::Zstd_Uncompress);
  705. }
  706. void Open() {
  707. assert(db_ == nullptr);
  708. Options options;
  709. options.env = g_env;
  710. options.create_if_missing = !FLAGS_use_existing_db;
  711. options.block_cache = cache_;
  712. options.write_buffer_size = FLAGS_write_buffer_size;
  713. options.max_file_size = FLAGS_max_file_size;
  714. options.block_size = FLAGS_block_size;
  715. if (FLAGS_comparisons) {
  716. options.comparator = &count_comparator_;
  717. }
  718. options.max_open_files = FLAGS_open_files;
  719. options.filter_policy = filter_policy_;
  720. options.reuse_logs = FLAGS_reuse_logs;
  721. options.compression =
  722. FLAGS_compression ? kSnappyCompression : kNoCompression;
  723. Status s = DB::Open(options, FLAGS_db, &db_);
  724. if (!s.ok()) {
  725. std::fprintf(stderr, "open error: %s\n", s.ToString().c_str());
  726. std::exit(1);
  727. }
  728. }
  729. void OpenBench(ThreadState* thread) {
  730. for (int i = 0; i < num_; i++) {
  731. delete db_;
  732. Open();
  733. thread->stats.FinishedSingleOp();
  734. }
  735. }
  736. void WriteSeq(ThreadState* thread) { DoWrite(thread, true); }
  737. void WriteRandom(ThreadState* thread) { DoWrite(thread, false); }
  738. void DoWrite(ThreadState* thread, bool seq) {
  739. if (num_ != FLAGS_num) {
  740. char msg[100];
  741. std::snprintf(msg, sizeof(msg), "(%d ops)", num_);
  742. thread->stats.AddMessage(msg);
  743. }
  744. RandomGenerator gen;
  745. WriteBatch batch;
  746. Status s;
  747. int64_t bytes = 0;
  748. KeyBuffer key;
  749. for (int i = 0; i < num_; i += entries_per_batch_) {
  750. batch.Clear();
  751. for (int j = 0; j < entries_per_batch_; j++) {
  752. const int k = seq ? i + j : thread->rand.Uniform(FLAGS_num);
  753. key.Set(k);
  754. batch.Put(key.slice(), gen.Generate(value_size_));
  755. bytes += value_size_ + key.slice().size();
  756. thread->stats.FinishedSingleOp();
  757. }
  758. s = db_->Write(write_options_, &batch);
  759. if (!s.ok()) {
  760. std::fprintf(stderr, "put error: %s\n", s.ToString().c_str());
  761. std::exit(1);
  762. }
  763. }
  764. thread->stats.AddBytes(bytes);
  765. }
  766. void ReadSequential(ThreadState* thread) {
  767. Iterator* iter = db_->NewIterator(ReadOptions());
  768. int i = 0;
  769. int64_t bytes = 0;
  770. for (iter->SeekToFirst(); i < reads_ && iter->Valid(); iter->Next()) {
  771. bytes += iter->key().size() + iter->value().size();
  772. thread->stats.FinishedSingleOp();
  773. ++i;
  774. }
  775. delete iter;
  776. thread->stats.AddBytes(bytes);
  777. }
  778. void ReadReverse(ThreadState* thread) {
  779. Iterator* iter = db_->NewIterator(ReadOptions());
  780. int i = 0;
  781. int64_t bytes = 0;
  782. for (iter->SeekToLast(); i < reads_ && iter->Valid(); iter->Prev()) {
  783. bytes += iter->key().size() + iter->value().size();
  784. thread->stats.FinishedSingleOp();
  785. ++i;
  786. }
  787. delete iter;
  788. thread->stats.AddBytes(bytes);
  789. }
  790. void ReadRandom(ThreadState* thread) {
  791. ReadOptions options;
  792. std::string value;
  793. int found = 0;
  794. KeyBuffer key;
  795. for (int i = 0; i < reads_; i++) {
  796. const int k = thread->rand.Uniform(FLAGS_num);
  797. key.Set(k);
  798. if (db_->Get(options, key.slice(), &value).ok()) {
  799. found++;
  800. }
  801. thread->stats.FinishedSingleOp();
  802. }
  803. char msg[100];
  804. std::snprintf(msg, sizeof(msg), "(%d of %d found)", found, num_);
  805. thread->stats.AddMessage(msg);
  806. }
  807. void ReadMissing(ThreadState* thread) {
  808. ReadOptions options;
  809. std::string value;
  810. KeyBuffer key;
  811. for (int i = 0; i < reads_; i++) {
  812. const int k = thread->rand.Uniform(FLAGS_num);
  813. key.Set(k);
  814. Slice s = Slice(key.slice().data(), key.slice().size() - 1);
  815. db_->Get(options, s, &value);
  816. thread->stats.FinishedSingleOp();
  817. }
  818. }
  819. void ReadHot(ThreadState* thread) {
  820. ReadOptions options;
  821. std::string value;
  822. const int range = (FLAGS_num + 99) / 100;
  823. KeyBuffer key;
  824. for (int i = 0; i < reads_; i++) {
  825. const int k = thread->rand.Uniform(range);
  826. key.Set(k);
  827. db_->Get(options, key.slice(), &value);
  828. thread->stats.FinishedSingleOp();
  829. }
  830. }
  831. void SeekRandom(ThreadState* thread) {
  832. ReadOptions options;
  833. int found = 0;
  834. KeyBuffer key;
  835. for (int i = 0; i < reads_; i++) {
  836. Iterator* iter = db_->NewIterator(options);
  837. const int k = thread->rand.Uniform(FLAGS_num);
  838. key.Set(k);
  839. iter->Seek(key.slice());
  840. if (iter->Valid() && iter->key() == key.slice()) found++;
  841. delete iter;
  842. thread->stats.FinishedSingleOp();
  843. }
  844. char msg[100];
  845. snprintf(msg, sizeof(msg), "(%d of %d found)", found, num_);
  846. thread->stats.AddMessage(msg);
  847. }
  848. void SeekOrdered(ThreadState* thread) {
  849. ReadOptions options;
  850. Iterator* iter = db_->NewIterator(options);
  851. int found = 0;
  852. int k = 0;
  853. KeyBuffer key;
  854. for (int i = 0; i < reads_; i++) {
  855. k = (k + (thread->rand.Uniform(100))) % FLAGS_num;
  856. key.Set(k);
  857. iter->Seek(key.slice());
  858. if (iter->Valid() && iter->key() == key.slice()) found++;
  859. thread->stats.FinishedSingleOp();
  860. }
  861. delete iter;
  862. char msg[100];
  863. std::snprintf(msg, sizeof(msg), "(%d of %d found)", found, num_);
  864. thread->stats.AddMessage(msg);
  865. }
  866. void DoDelete(ThreadState* thread, bool seq) {
  867. RandomGenerator gen;
  868. WriteBatch batch;
  869. Status s;
  870. KeyBuffer key;
  871. for (int i = 0; i < num_; i += entries_per_batch_) {
  872. batch.Clear();
  873. for (int j = 0; j < entries_per_batch_; j++) {
  874. const int k = seq ? i + j : (thread->rand.Uniform(FLAGS_num));
  875. key.Set(k);
  876. batch.Delete(key.slice());
  877. thread->stats.FinishedSingleOp();
  878. }
  879. s = db_->Write(write_options_, &batch);
  880. if (!s.ok()) {
  881. std::fprintf(stderr, "del error: %s\n", s.ToString().c_str());
  882. std::exit(1);
  883. }
  884. }
  885. }
  886. void DeleteSeq(ThreadState* thread) { DoDelete(thread, true); }
  887. void DeleteRandom(ThreadState* thread) { DoDelete(thread, false); }
  888. void ReadWhileWriting(ThreadState* thread) {
  889. if (thread->tid > 0) {
  890. ReadRandom(thread);
  891. } else {
  892. // Special thread that keeps writing until other threads are done.
  893. RandomGenerator gen;
  894. KeyBuffer key;
  895. while (true) {
  896. {
  897. MutexLock l(&thread->shared->mu);
  898. if (thread->shared->num_done + 1 >= thread->shared->num_initialized) {
  899. // Other threads have finished
  900. break;
  901. }
  902. }
  903. const int k = thread->rand.Uniform(FLAGS_num);
  904. key.Set(k);
  905. Status s =
  906. db_->Put(write_options_, key.slice(), gen.Generate(value_size_));
  907. if (!s.ok()) {
  908. std::fprintf(stderr, "put error: %s\n", s.ToString().c_str());
  909. std::exit(1);
  910. }
  911. }
  912. // Do not count any of the preceding work/delay in stats.
  913. thread->stats.Start();
  914. }
  915. }
  916. void Compact(ThreadState* thread) { db_->CompactRange(nullptr, nullptr); }
  917. void PrintStats(const char* key) {
  918. std::string stats;
  919. if (!db_->GetProperty(key, &stats)) {
  920. stats = "(failed)";
  921. }
  922. std::fprintf(stdout, "\n%s\n", stats.c_str());
  923. }
  924. static void WriteToFile(void* arg, const char* buf, int n) {
  925. reinterpret_cast<WritableFile*>(arg)->Append(Slice(buf, n));
  926. }
  927. void HeapProfile() {
  928. char fname[100];
  929. std::snprintf(fname, sizeof(fname), "%s/heap-%04d", FLAGS_db,
  930. ++heap_counter_);
  931. WritableFile* file;
  932. Status s = g_env->NewWritableFile(fname, &file);
  933. if (!s.ok()) {
  934. std::fprintf(stderr, "%s\n", s.ToString().c_str());
  935. return;
  936. }
  937. bool ok = port::GetHeapProfile(WriteToFile, file);
  938. delete file;
  939. if (!ok) {
  940. std::fprintf(stderr, "heap profiling not supported\n");
  941. g_env->RemoveFile(fname);
  942. }
  943. }
  944. };
  945. } // namespace leveldb
  946. int main(int argc, char** argv) {
  947. FLAGS_write_buffer_size = leveldb::Options().write_buffer_size;
  948. FLAGS_max_file_size = leveldb::Options().max_file_size;
  949. FLAGS_block_size = leveldb::Options().block_size;
  950. FLAGS_open_files = leveldb::Options().max_open_files;
  951. std::string default_db_path;
  952. for (int i = 1; i < argc; i++) {
  953. double d;
  954. int n;
  955. char junk;
  956. if (leveldb::Slice(argv[i]).starts_with("--benchmarks=")) {
  957. FLAGS_benchmarks = argv[i] + strlen("--benchmarks=");
  958. } else if (sscanf(argv[i], "--compression_ratio=%lf%c", &d, &junk) == 1) {
  959. FLAGS_compression_ratio = d;
  960. } else if (sscanf(argv[i], "--histogram=%d%c", &n, &junk) == 1 &&
  961. (n == 0 || n == 1)) {
  962. FLAGS_histogram = n;
  963. } else if (sscanf(argv[i], "--comparisons=%d%c", &n, &junk) == 1 &&
  964. (n == 0 || n == 1)) {
  965. FLAGS_comparisons = n;
  966. } else if (sscanf(argv[i], "--use_existing_db=%d%c", &n, &junk) == 1 &&
  967. (n == 0 || n == 1)) {
  968. FLAGS_use_existing_db = n;
  969. } else if (sscanf(argv[i], "--reuse_logs=%d%c", &n, &junk) == 1 &&
  970. (n == 0 || n == 1)) {
  971. FLAGS_reuse_logs = n;
  972. } else if (sscanf(argv[i], "--compression=%d%c", &n, &junk) == 1 &&
  973. (n == 0 || n == 1)) {
  974. FLAGS_compression = n;
  975. } else if (sscanf(argv[i], "--num=%d%c", &n, &junk) == 1) {
  976. FLAGS_num = n;
  977. } else if (sscanf(argv[i], "--reads=%d%c", &n, &junk) == 1) {
  978. FLAGS_reads = n;
  979. } else if (sscanf(argv[i], "--threads=%d%c", &n, &junk) == 1) {
  980. FLAGS_threads = n;
  981. } else if (sscanf(argv[i], "--value_size=%d%c", &n, &junk) == 1) {
  982. FLAGS_value_size = n;
  983. } else if (sscanf(argv[i], "--write_buffer_size=%d%c", &n, &junk) == 1) {
  984. FLAGS_write_buffer_size = n;
  985. } else if (sscanf(argv[i], "--max_file_size=%d%c", &n, &junk) == 1) {
  986. FLAGS_max_file_size = n;
  987. } else if (sscanf(argv[i], "--block_size=%d%c", &n, &junk) == 1) {
  988. FLAGS_block_size = n;
  989. } else if (sscanf(argv[i], "--key_prefix=%d%c", &n, &junk) == 1) {
  990. FLAGS_key_prefix = n;
  991. } else if (sscanf(argv[i], "--cache_size=%d%c", &n, &junk) == 1) {
  992. FLAGS_cache_size = n;
  993. } else if (sscanf(argv[i], "--bloom_bits=%d%c", &n, &junk) == 1) {
  994. FLAGS_bloom_bits = n;
  995. } else if (sscanf(argv[i], "--open_files=%d%c", &n, &junk) == 1) {
  996. FLAGS_open_files = n;
  997. } else if (strncmp(argv[i], "--db=", 5) == 0) {
  998. FLAGS_db = argv[i] + 5;
  999. } else {
  1000. std::fprintf(stderr, "Invalid flag '%s'\n", argv[i]);
  1001. std::exit(1);
  1002. }
  1003. }
  1004. leveldb::g_env = leveldb::Env::Default();
  1005. // Choose a location for the test database if none given with --db=<path>
  1006. if (FLAGS_db == nullptr) {
  1007. leveldb::g_env->GetTestDirectory(&default_db_path);
  1008. default_db_path += "/dbbench";
  1009. FLAGS_db = default_db_path.c_str();
  1010. }
  1011. leveldb::Benchmark benchmark;
  1012. benchmark.Run();
  1013. return 0;
  1014. }