db_stress_stat.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright (c) 2011-present, 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).
  5. #pragma once
  6. #include <cinttypes>
  7. #include <memory>
  8. #include <queue>
  9. #include <unordered_set>
  10. #include "monitoring/histogram.h"
  11. #include "port/port.h"
  12. #include "rocksdb/env.h"
  13. #include "rocksdb/snapshot.h"
  14. #include "rocksdb/statistics.h"
  15. #include "util/gflags_compat.h"
  16. #include "util/random.h"
  17. DECLARE_bool(histogram);
  18. DECLARE_bool(progress_reports);
  19. namespace ROCKSDB_NAMESPACE {
  20. // Database statistics
  21. static std::shared_ptr<ROCKSDB_NAMESPACE::Statistics> dbstats;
  22. static std::shared_ptr<ROCKSDB_NAMESPACE::Statistics> dbstats_secondaries;
  23. class Stats {
  24. private:
  25. uint64_t start_;
  26. uint64_t finish_;
  27. double seconds_;
  28. long done_;
  29. long gets_;
  30. long prefixes_;
  31. long writes_;
  32. long deletes_;
  33. size_t single_deletes_;
  34. long iterator_size_sums_;
  35. long founds_;
  36. long iterations_;
  37. long range_deletions_;
  38. long covered_by_range_deletions_;
  39. long errors_;
  40. long num_compact_files_succeed_;
  41. long num_compact_files_failed_;
  42. int next_report_;
  43. size_t bytes_;
  44. uint64_t last_op_finish_;
  45. HistogramImpl hist_;
  46. public:
  47. Stats() {}
  48. void Start() {
  49. next_report_ = 100;
  50. hist_.Clear();
  51. done_ = 0;
  52. gets_ = 0;
  53. prefixes_ = 0;
  54. writes_ = 0;
  55. deletes_ = 0;
  56. single_deletes_ = 0;
  57. iterator_size_sums_ = 0;
  58. founds_ = 0;
  59. iterations_ = 0;
  60. range_deletions_ = 0;
  61. covered_by_range_deletions_ = 0;
  62. errors_ = 0;
  63. bytes_ = 0;
  64. seconds_ = 0;
  65. num_compact_files_succeed_ = 0;
  66. num_compact_files_failed_ = 0;
  67. start_ = Env::Default()->NowMicros();
  68. last_op_finish_ = start_;
  69. finish_ = start_;
  70. }
  71. void Merge(const Stats& other) {
  72. hist_.Merge(other.hist_);
  73. done_ += other.done_;
  74. gets_ += other.gets_;
  75. prefixes_ += other.prefixes_;
  76. writes_ += other.writes_;
  77. deletes_ += other.deletes_;
  78. single_deletes_ += other.single_deletes_;
  79. iterator_size_sums_ += other.iterator_size_sums_;
  80. founds_ += other.founds_;
  81. iterations_ += other.iterations_;
  82. range_deletions_ += other.range_deletions_;
  83. covered_by_range_deletions_ = other.covered_by_range_deletions_;
  84. errors_ += other.errors_;
  85. bytes_ += other.bytes_;
  86. seconds_ += other.seconds_;
  87. num_compact_files_succeed_ += other.num_compact_files_succeed_;
  88. num_compact_files_failed_ += other.num_compact_files_failed_;
  89. if (other.start_ < start_) start_ = other.start_;
  90. if (other.finish_ > finish_) finish_ = other.finish_;
  91. }
  92. void Stop() {
  93. finish_ = Env::Default()->NowMicros();
  94. seconds_ = (finish_ - start_) * 1e-6;
  95. }
  96. void FinishedSingleOp() {
  97. if (FLAGS_histogram) {
  98. auto now = Env::Default()->NowMicros();
  99. auto micros = now - last_op_finish_;
  100. hist_.Add(micros);
  101. if (micros > 20000) {
  102. fprintf(stdout, "long op: %" PRIu64 " micros%30s\r", micros, "");
  103. }
  104. last_op_finish_ = now;
  105. }
  106. done_++;
  107. if (FLAGS_progress_reports) {
  108. if (done_ >= next_report_) {
  109. if (next_report_ < 1000)
  110. next_report_ += 100;
  111. else if (next_report_ < 5000)
  112. next_report_ += 500;
  113. else if (next_report_ < 10000)
  114. next_report_ += 1000;
  115. else if (next_report_ < 50000)
  116. next_report_ += 5000;
  117. else if (next_report_ < 100000)
  118. next_report_ += 10000;
  119. else if (next_report_ < 500000)
  120. next_report_ += 50000;
  121. else
  122. next_report_ += 100000;
  123. fprintf(stdout, "... finished %ld ops%30s\r", done_, "");
  124. }
  125. }
  126. }
  127. void AddBytesForWrites(long nwrites, size_t nbytes) {
  128. writes_ += nwrites;
  129. bytes_ += nbytes;
  130. }
  131. void AddGets(long ngets, long nfounds) {
  132. founds_ += nfounds;
  133. gets_ += ngets;
  134. }
  135. void AddPrefixes(long nprefixes, long count) {
  136. prefixes_ += nprefixes;
  137. iterator_size_sums_ += count;
  138. }
  139. void AddIterations(long n) { iterations_ += n; }
  140. void AddDeletes(long n) { deletes_ += n; }
  141. void AddSingleDeletes(size_t n) { single_deletes_ += n; }
  142. void AddRangeDeletions(long n) { range_deletions_ += n; }
  143. void AddCoveredByRangeDeletions(long n) { covered_by_range_deletions_ += n; }
  144. void AddErrors(long n) { errors_ += n; }
  145. void AddNumCompactFilesSucceed(long n) { num_compact_files_succeed_ += n; }
  146. void AddNumCompactFilesFailed(long n) { num_compact_files_failed_ += n; }
  147. void Report(const char* name) {
  148. std::string extra;
  149. if (bytes_ < 1 || done_ < 1) {
  150. fprintf(stderr, "No writes or ops?\n");
  151. return;
  152. }
  153. double elapsed = (finish_ - start_) * 1e-6;
  154. double bytes_mb = bytes_ / 1048576.0;
  155. double rate = bytes_mb / elapsed;
  156. double throughput = (double)done_ / elapsed;
  157. fprintf(stdout, "%-12s: ", name);
  158. fprintf(stdout, "%.3f micros/op %ld ops/sec\n", seconds_ * 1e6 / done_,
  159. (long)throughput);
  160. fprintf(stdout, "%-12s: Wrote %.2f MB (%.2f MB/sec) (%ld%% of %ld ops)\n",
  161. "", bytes_mb, rate, (100 * writes_) / done_, done_);
  162. fprintf(stdout, "%-12s: Wrote %ld times\n", "", writes_);
  163. fprintf(stdout, "%-12s: Deleted %ld times\n", "", deletes_);
  164. fprintf(stdout, "%-12s: Single deleted %" ROCKSDB_PRIszt " times\n", "",
  165. single_deletes_);
  166. fprintf(stdout, "%-12s: %ld read and %ld found the key\n", "", gets_,
  167. founds_);
  168. fprintf(stdout, "%-12s: Prefix scanned %ld times\n", "", prefixes_);
  169. fprintf(stdout, "%-12s: Iterator size sum is %ld\n", "",
  170. iterator_size_sums_);
  171. fprintf(stdout, "%-12s: Iterated %ld times\n", "", iterations_);
  172. fprintf(stdout, "%-12s: Deleted %ld key-ranges\n", "", range_deletions_);
  173. fprintf(stdout, "%-12s: Range deletions covered %ld keys\n", "",
  174. covered_by_range_deletions_);
  175. fprintf(stdout, "%-12s: Got errors %ld times\n", "", errors_);
  176. fprintf(stdout, "%-12s: %ld CompactFiles() succeed\n", "",
  177. num_compact_files_succeed_);
  178. fprintf(stdout, "%-12s: %ld CompactFiles() did not succeed\n", "",
  179. num_compact_files_failed_);
  180. if (FLAGS_histogram) {
  181. fprintf(stdout, "Microseconds per op:\n%s\n", hist_.ToString().c_str());
  182. }
  183. fflush(stdout);
  184. }
  185. };
  186. } // namespace ROCKSDB_NAMESPACE