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).
10 #include <unordered_set>
12 #include "monitoring/histogram.h"
13 #include "port/port.h"
14 #include "rocksdb/snapshot.h"
15 #include "rocksdb/statistics.h"
16 #include "rocksdb/system_clock.h"
17 #include "util/gflags_compat.h"
18 #include "util/random.h"
20 DECLARE_bool(histogram
);
21 DECLARE_bool(progress_reports
);
23 namespace ROCKSDB_NAMESPACE
{
25 // Database statistics
26 extern std::shared_ptr
<ROCKSDB_NAMESPACE::Statistics
> dbstats
;
27 extern std::shared_ptr
<ROCKSDB_NAMESPACE::Statistics
> dbstats_secondaries
;
39 size_t single_deletes_
;
40 long iterator_size_sums_
;
43 long range_deletions_
;
44 long covered_by_range_deletions_
;
46 long verified_errors_
;
47 long num_compact_files_succeed_
;
48 long num_compact_files_failed_
;
51 uint64_t last_op_finish_
;
66 iterator_size_sums_
= 0;
70 covered_by_range_deletions_
= 0;
75 num_compact_files_succeed_
= 0;
76 num_compact_files_failed_
= 0;
77 start_
= SystemClock::Default()->NowMicros();
78 last_op_finish_
= start_
;
82 void Merge(const Stats
& other
) {
83 hist_
.Merge(other
.hist_
);
86 prefixes_
+= other
.prefixes_
;
87 writes_
+= other
.writes_
;
88 deletes_
+= other
.deletes_
;
89 single_deletes_
+= other
.single_deletes_
;
90 iterator_size_sums_
+= other
.iterator_size_sums_
;
91 founds_
+= other
.founds_
;
92 iterations_
+= other
.iterations_
;
93 range_deletions_
+= other
.range_deletions_
;
94 covered_by_range_deletions_
= other
.covered_by_range_deletions_
;
95 errors_
+= other
.errors_
;
96 verified_errors_
+= other
.verified_errors_
;
97 bytes_
+= other
.bytes_
;
98 seconds_
+= other
.seconds_
;
99 num_compact_files_succeed_
+= other
.num_compact_files_succeed_
;
100 num_compact_files_failed_
+= other
.num_compact_files_failed_
;
101 if (other
.start_
< start_
) start_
= other
.start_
;
102 if (other
.finish_
> finish_
) finish_
= other
.finish_
;
106 finish_
= SystemClock::Default()->NowMicros();
107 seconds_
= (finish_
- start_
) * 1e-6;
110 void FinishedSingleOp() {
111 if (FLAGS_histogram
) {
112 auto now
= SystemClock::Default()->NowMicros();
113 auto micros
= now
- last_op_finish_
;
115 if (micros
> 20000) {
116 fprintf(stdout
, "long op: %" PRIu64
" micros%30s\r", micros
, "");
118 last_op_finish_
= now
;
122 if (FLAGS_progress_reports
) {
123 if (done_
>= next_report_
) {
124 if (next_report_
< 1000)
126 else if (next_report_
< 5000)
128 else if (next_report_
< 10000)
129 next_report_
+= 1000;
130 else if (next_report_
< 50000)
131 next_report_
+= 5000;
132 else if (next_report_
< 100000)
133 next_report_
+= 10000;
134 else if (next_report_
< 500000)
135 next_report_
+= 50000;
137 next_report_
+= 100000;
138 fprintf(stdout
, "... finished %ld ops%30s\r", done_
, "");
143 void AddBytesForWrites(long nwrites
, size_t nbytes
) {
148 void AddGets(long ngets
, long nfounds
) {
153 void AddPrefixes(long nprefixes
, long count
) {
154 prefixes_
+= nprefixes
;
155 iterator_size_sums_
+= count
;
158 void AddIterations(long n
) { iterations_
+= n
; }
160 void AddDeletes(long n
) { deletes_
+= n
; }
162 void AddSingleDeletes(size_t n
) { single_deletes_
+= n
; }
164 void AddRangeDeletions(long n
) { range_deletions_
+= n
; }
166 void AddCoveredByRangeDeletions(long n
) { covered_by_range_deletions_
+= n
; }
168 void AddErrors(long n
) { errors_
+= n
; }
170 void AddVerifiedErrors(long n
) { verified_errors_
+= n
; }
172 void AddNumCompactFilesSucceed(long n
) { num_compact_files_succeed_
+= n
; }
174 void AddNumCompactFilesFailed(long n
) { num_compact_files_failed_
+= n
; }
176 void Report(const char* name
) {
178 if (bytes_
< 1 || done_
< 1) {
179 fprintf(stderr
, "No writes or ops?\n");
183 double elapsed
= (finish_
- start_
) * 1e-6;
184 double bytes_mb
= bytes_
/ 1048576.0;
185 double rate
= bytes_mb
/ elapsed
;
186 double throughput
= (double)done_
/ elapsed
;
188 fprintf(stdout
, "%-12s: ", name
);
189 fprintf(stdout
, "%.3f micros/op %ld ops/sec\n", seconds_
* 1e6
/ done_
,
191 fprintf(stdout
, "%-12s: Wrote %.2f MB (%.2f MB/sec) (%ld%% of %ld ops)\n",
192 "", bytes_mb
, rate
, (100 * writes_
) / done_
, done_
);
193 fprintf(stdout
, "%-12s: Wrote %ld times\n", "", writes_
);
194 fprintf(stdout
, "%-12s: Deleted %ld times\n", "", deletes_
);
195 fprintf(stdout
, "%-12s: Single deleted %" ROCKSDB_PRIszt
" times\n", "",
197 fprintf(stdout
, "%-12s: %ld read and %ld found the key\n", "", gets_
,
199 fprintf(stdout
, "%-12s: Prefix scanned %ld times\n", "", prefixes_
);
200 fprintf(stdout
, "%-12s: Iterator size sum is %ld\n", "",
201 iterator_size_sums_
);
202 fprintf(stdout
, "%-12s: Iterated %ld times\n", "", iterations_
);
203 fprintf(stdout
, "%-12s: Deleted %ld key-ranges\n", "", range_deletions_
);
204 fprintf(stdout
, "%-12s: Range deletions covered %ld keys\n", "",
205 covered_by_range_deletions_
);
207 fprintf(stdout
, "%-12s: Got errors %ld times\n", "", errors_
);
208 fprintf(stdout
, "%-12s: %ld CompactFiles() succeed\n", "",
209 num_compact_files_succeed_
);
210 fprintf(stdout
, "%-12s: %ld CompactFiles() did not succeed\n", "",
211 num_compact_files_failed_
);
213 if (FLAGS_histogram
) {
214 fprintf(stdout
, "Microseconds per op:\n%s\n", hist_
.ToString().c_str());
219 } // namespace ROCKSDB_NAMESPACE