db_stress_stat.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/snapshot.h"
  13. #include "rocksdb/statistics.h"
  14. #include "rocksdb/system_clock.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. class Stats {
  21. private:
  22. uint64_t start_;
  23. uint64_t finish_;
  24. double seconds_;
  25. long done_;
  26. long gets_;
  27. long prefixes_;
  28. long writes_;
  29. long deletes_;
  30. size_t single_deletes_;
  31. long iterator_size_sums_;
  32. long founds_;
  33. long iterations_;
  34. long range_deletions_;
  35. long covered_by_range_deletions_;
  36. long errors_;
  37. long verified_errors_;
  38. long num_compact_files_succeed_;
  39. long num_compact_files_failed_;
  40. int next_report_;
  41. size_t bytes_;
  42. uint64_t last_op_finish_;
  43. HistogramImpl hist_;
  44. public:
  45. Stats() {}
  46. void Start() {
  47. next_report_ = 100;
  48. hist_.Clear();
  49. done_ = 0;
  50. gets_ = 0;
  51. prefixes_ = 0;
  52. writes_ = 0;
  53. deletes_ = 0;
  54. single_deletes_ = 0;
  55. iterator_size_sums_ = 0;
  56. founds_ = 0;
  57. iterations_ = 0;
  58. range_deletions_ = 0;
  59. covered_by_range_deletions_ = 0;
  60. errors_ = 0;
  61. verified_errors_ = 0;
  62. bytes_ = 0;
  63. seconds_ = 0;
  64. num_compact_files_succeed_ = 0;
  65. num_compact_files_failed_ = 0;
  66. start_ = SystemClock::Default()->NowMicros();
  67. last_op_finish_ = start_;
  68. finish_ = start_;
  69. }
  70. void Merge(const Stats& other) {
  71. hist_.Merge(other.hist_);
  72. done_ += other.done_;
  73. gets_ += other.gets_;
  74. prefixes_ += other.prefixes_;
  75. writes_ += other.writes_;
  76. deletes_ += other.deletes_;
  77. single_deletes_ += other.single_deletes_;
  78. iterator_size_sums_ += other.iterator_size_sums_;
  79. founds_ += other.founds_;
  80. iterations_ += other.iterations_;
  81. range_deletions_ += other.range_deletions_;
  82. covered_by_range_deletions_ = other.covered_by_range_deletions_;
  83. errors_ += other.errors_;
  84. verified_errors_ += other.verified_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_ = SystemClock::Default()->NowMicros();
  94. seconds_ = (finish_ - start_) * 1e-6;
  95. }
  96. void FinishedSingleOp() {
  97. if (FLAGS_histogram) {
  98. auto now = SystemClock::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 AddVerifiedErrors(long n) { verified_errors_ += n; }
  146. void AddNumCompactFilesSucceed(long n) { num_compact_files_succeed_ += n; }
  147. void AddNumCompactFilesFailed(long n) { num_compact_files_failed_ += n; }
  148. void Report(const char* name) {
  149. std::string extra;
  150. if (bytes_ < 1 || done_ < 1) {
  151. fprintf(stderr, "No writes or ops?\n");
  152. return;
  153. }
  154. double elapsed = (finish_ - start_) * 1e-6;
  155. double bytes_mb = bytes_ / 1048576.0;
  156. double rate = bytes_mb / elapsed;
  157. double throughput = (double)done_ / elapsed;
  158. fprintf(stdout, "%-12s: ", name);
  159. fprintf(stdout, "%.3f micros/op %ld ops/sec\n", seconds_ * 1e6 / done_,
  160. (long)throughput);
  161. fprintf(stdout, "%-12s: Wrote %.2f MB (%.2f MB/sec) (%ld%% of %ld ops)\n",
  162. "", bytes_mb, rate, (100 * writes_) / done_, done_);
  163. fprintf(stdout, "%-12s: Wrote %ld times\n", "", writes_);
  164. fprintf(stdout, "%-12s: Deleted %ld times\n", "", deletes_);
  165. fprintf(stdout, "%-12s: Single deleted %" ROCKSDB_PRIszt " times\n", "",
  166. single_deletes_);
  167. fprintf(stdout, "%-12s: %ld read and %ld found the key\n", "", gets_,
  168. founds_);
  169. fprintf(stdout, "%-12s: Prefix scanned %ld times\n", "", prefixes_);
  170. fprintf(stdout, "%-12s: Iterator size sum is %ld\n", "",
  171. iterator_size_sums_);
  172. fprintf(stdout, "%-12s: Iterated %ld times\n", "", iterations_);
  173. fprintf(stdout, "%-12s: Deleted %ld key-ranges\n", "", range_deletions_);
  174. fprintf(stdout, "%-12s: Range deletions covered %ld keys\n", "",
  175. covered_by_range_deletions_);
  176. fprintf(stdout, "%-12s: Got errors %ld times\n", "", errors_);
  177. fprintf(stdout, "%-12s: %ld CompactFiles() succeed\n", "",
  178. num_compact_files_succeed_);
  179. fprintf(stdout, "%-12s: %ld CompactFiles() did not succeed\n", "",
  180. num_compact_files_failed_);
  181. if (FLAGS_histogram) {
  182. fprintf(stdout, "Microseconds per op:\n%s\n", hist_.ToString().c_str());
  183. }
  184. fflush(stdout);
  185. }
  186. };
  187. } // namespace ROCKSDB_NAMESPACE