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/env.h"
15 #include "rocksdb/snapshot.h"
16 #include "rocksdb/statistics.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
{
24 // Database statistics
25 static std::shared_ptr
<ROCKSDB_NAMESPACE::Statistics
> dbstats
;
26 static std::shared_ptr
<ROCKSDB_NAMESPACE::Statistics
> dbstats_secondaries
;
38 size_t single_deletes_
;
39 long iterator_size_sums_
;
42 long range_deletions_
;
43 long covered_by_range_deletions_
;
45 long verified_errors_
;
46 long num_compact_files_succeed_
;
47 long num_compact_files_failed_
;
50 uint64_t last_op_finish_
;
65 iterator_size_sums_
= 0;
69 covered_by_range_deletions_
= 0;
74 num_compact_files_succeed_
= 0;
75 num_compact_files_failed_
= 0;
76 start_
= Env::Default()->NowMicros();
77 last_op_finish_
= start_
;
81 void Merge(const Stats
& other
) {
82 hist_
.Merge(other
.hist_
);
85 prefixes_
+= other
.prefixes_
;
86 writes_
+= other
.writes_
;
87 deletes_
+= other
.deletes_
;
88 single_deletes_
+= other
.single_deletes_
;
89 iterator_size_sums_
+= other
.iterator_size_sums_
;
90 founds_
+= other
.founds_
;
91 iterations_
+= other
.iterations_
;
92 range_deletions_
+= other
.range_deletions_
;
93 covered_by_range_deletions_
= other
.covered_by_range_deletions_
;
94 errors_
+= other
.errors_
;
95 verified_errors_
+= other
.verified_errors_
;
96 bytes_
+= other
.bytes_
;
97 seconds_
+= other
.seconds_
;
98 num_compact_files_succeed_
+= other
.num_compact_files_succeed_
;
99 num_compact_files_failed_
+= other
.num_compact_files_failed_
;
100 if (other
.start_
< start_
) start_
= other
.start_
;
101 if (other
.finish_
> finish_
) finish_
= other
.finish_
;
105 finish_
= Env::Default()->NowMicros();
106 seconds_
= (finish_
- start_
) * 1e-6;
109 void FinishedSingleOp() {
110 if (FLAGS_histogram
) {
111 auto now
= Env::Default()->NowMicros();
112 auto micros
= now
- last_op_finish_
;
114 if (micros
> 20000) {
115 fprintf(stdout
, "long op: %" PRIu64
" micros%30s\r", micros
, "");
117 last_op_finish_
= now
;
121 if (FLAGS_progress_reports
) {
122 if (done_
>= next_report_
) {
123 if (next_report_
< 1000)
125 else if (next_report_
< 5000)
127 else if (next_report_
< 10000)
128 next_report_
+= 1000;
129 else if (next_report_
< 50000)
130 next_report_
+= 5000;
131 else if (next_report_
< 100000)
132 next_report_
+= 10000;
133 else if (next_report_
< 500000)
134 next_report_
+= 50000;
136 next_report_
+= 100000;
137 fprintf(stdout
, "... finished %ld ops%30s\r", done_
, "");
142 void AddBytesForWrites(long nwrites
, size_t nbytes
) {
147 void AddGets(long ngets
, long nfounds
) {
152 void AddPrefixes(long nprefixes
, long count
) {
153 prefixes_
+= nprefixes
;
154 iterator_size_sums_
+= count
;
157 void AddIterations(long n
) { iterations_
+= n
; }
159 void AddDeletes(long n
) { deletes_
+= n
; }
161 void AddSingleDeletes(size_t n
) { single_deletes_
+= n
; }
163 void AddRangeDeletions(long n
) { range_deletions_
+= n
; }
165 void AddCoveredByRangeDeletions(long n
) { covered_by_range_deletions_
+= n
; }
167 void AddErrors(long n
) { errors_
+= n
; }
169 void AddVerifiedErrors(long n
) { verified_errors_
+= n
; }
171 void AddNumCompactFilesSucceed(long n
) { num_compact_files_succeed_
+= n
; }
173 void AddNumCompactFilesFailed(long n
) { num_compact_files_failed_
+= n
; }
175 void Report(const char* name
) {
177 if (bytes_
< 1 || done_
< 1) {
178 fprintf(stderr
, "No writes or ops?\n");
182 double elapsed
= (finish_
- start_
) * 1e-6;
183 double bytes_mb
= bytes_
/ 1048576.0;
184 double rate
= bytes_mb
/ elapsed
;
185 double throughput
= (double)done_
/ elapsed
;
187 fprintf(stdout
, "%-12s: ", name
);
188 fprintf(stdout
, "%.3f micros/op %ld ops/sec\n", seconds_
* 1e6
/ done_
,
190 fprintf(stdout
, "%-12s: Wrote %.2f MB (%.2f MB/sec) (%ld%% of %ld ops)\n",
191 "", bytes_mb
, rate
, (100 * writes_
) / done_
, done_
);
192 fprintf(stdout
, "%-12s: Wrote %ld times\n", "", writes_
);
193 fprintf(stdout
, "%-12s: Deleted %ld times\n", "", deletes_
);
194 fprintf(stdout
, "%-12s: Single deleted %" ROCKSDB_PRIszt
" times\n", "",
196 fprintf(stdout
, "%-12s: %ld read and %ld found the key\n", "", gets_
,
198 fprintf(stdout
, "%-12s: Prefix scanned %ld times\n", "", prefixes_
);
199 fprintf(stdout
, "%-12s: Iterator size sum is %ld\n", "",
200 iterator_size_sums_
);
201 fprintf(stdout
, "%-12s: Iterated %ld times\n", "", iterations_
);
202 fprintf(stdout
, "%-12s: Deleted %ld key-ranges\n", "", range_deletions_
);
203 fprintf(stdout
, "%-12s: Range deletions covered %ld keys\n", "",
204 covered_by_range_deletions_
);
206 fprintf(stdout
, "%-12s: Got errors %ld times\n", "", errors_
);
207 fprintf(stdout
, "%-12s: %ld CompactFiles() succeed\n", "",
208 num_compact_files_succeed_
);
209 fprintf(stdout
, "%-12s: %ld CompactFiles() did not succeed\n", "",
210 num_compact_files_failed_
);
212 if (FLAGS_histogram
) {
213 fprintf(stdout
, "Microseconds per op:\n%s\n", hist_
.ToString().c_str());
218 } // namespace ROCKSDB_NAMESPACE