]>
Commit | Line | Data |
---|---|---|
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 | ||
6 | #pragma once | |
7 | #include <cinttypes> | |
8 | #include <memory> | |
9 | #include <queue> | |
10 | #include <unordered_set> | |
11 | ||
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" | |
19 | ||
20 | DECLARE_bool(histogram); | |
21 | DECLARE_bool(progress_reports); | |
22 | ||
23 | namespace ROCKSDB_NAMESPACE { | |
24 | ||
25 | // Database statistics | |
26 | extern std::shared_ptr<ROCKSDB_NAMESPACE::Statistics> dbstats; | |
27 | extern std::shared_ptr<ROCKSDB_NAMESPACE::Statistics> dbstats_secondaries; | |
28 | ||
29 | class Stats { | |
30 | private: | |
31 | uint64_t start_; | |
32 | uint64_t finish_; | |
33 | double seconds_; | |
34 | long done_; | |
35 | long gets_; | |
36 | long prefixes_; | |
37 | long writes_; | |
38 | long deletes_; | |
39 | size_t single_deletes_; | |
40 | long iterator_size_sums_; | |
41 | long founds_; | |
42 | long iterations_; | |
43 | long range_deletions_; | |
44 | long covered_by_range_deletions_; | |
45 | long errors_; | |
46 | long verified_errors_; | |
47 | long num_compact_files_succeed_; | |
48 | long num_compact_files_failed_; | |
49 | int next_report_; | |
50 | size_t bytes_; | |
51 | uint64_t last_op_finish_; | |
52 | HistogramImpl hist_; | |
53 | ||
54 | public: | |
55 | Stats() {} | |
56 | ||
57 | void Start() { | |
58 | next_report_ = 100; | |
59 | hist_.Clear(); | |
60 | done_ = 0; | |
61 | gets_ = 0; | |
62 | prefixes_ = 0; | |
63 | writes_ = 0; | |
64 | deletes_ = 0; | |
65 | single_deletes_ = 0; | |
66 | iterator_size_sums_ = 0; | |
67 | founds_ = 0; | |
68 | iterations_ = 0; | |
69 | range_deletions_ = 0; | |
70 | covered_by_range_deletions_ = 0; | |
71 | errors_ = 0; | |
72 | verified_errors_ = 0; | |
73 | bytes_ = 0; | |
74 | seconds_ = 0; | |
75 | num_compact_files_succeed_ = 0; | |
76 | num_compact_files_failed_ = 0; | |
77 | start_ = SystemClock::Default()->NowMicros(); | |
78 | last_op_finish_ = start_; | |
79 | finish_ = start_; | |
80 | } | |
81 | ||
82 | void Merge(const Stats& other) { | |
83 | hist_.Merge(other.hist_); | |
84 | done_ += other.done_; | |
85 | gets_ += other.gets_; | |
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_; | |
103 | } | |
104 | ||
105 | void Stop() { | |
106 | finish_ = SystemClock::Default()->NowMicros(); | |
107 | seconds_ = (finish_ - start_) * 1e-6; | |
108 | } | |
109 | ||
110 | void FinishedSingleOp() { | |
111 | if (FLAGS_histogram) { | |
112 | auto now = SystemClock::Default()->NowMicros(); | |
113 | auto micros = now - last_op_finish_; | |
114 | hist_.Add(micros); | |
115 | if (micros > 20000) { | |
116 | fprintf(stdout, "long op: %" PRIu64 " micros%30s\r", micros, ""); | |
117 | } | |
118 | last_op_finish_ = now; | |
119 | } | |
120 | ||
121 | done_++; | |
122 | if (FLAGS_progress_reports) { | |
123 | if (done_ >= next_report_) { | |
124 | if (next_report_ < 1000) | |
125 | next_report_ += 100; | |
126 | else if (next_report_ < 5000) | |
127 | next_report_ += 500; | |
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; | |
136 | else | |
137 | next_report_ += 100000; | |
138 | fprintf(stdout, "... finished %ld ops%30s\r", done_, ""); | |
139 | } | |
140 | } | |
141 | } | |
142 | ||
143 | void AddBytesForWrites(long nwrites, size_t nbytes) { | |
144 | writes_ += nwrites; | |
145 | bytes_ += nbytes; | |
146 | } | |
147 | ||
148 | void AddGets(long ngets, long nfounds) { | |
149 | founds_ += nfounds; | |
150 | gets_ += ngets; | |
151 | } | |
152 | ||
153 | void AddPrefixes(long nprefixes, long count) { | |
154 | prefixes_ += nprefixes; | |
155 | iterator_size_sums_ += count; | |
156 | } | |
157 | ||
158 | void AddIterations(long n) { iterations_ += n; } | |
159 | ||
160 | void AddDeletes(long n) { deletes_ += n; } | |
161 | ||
162 | void AddSingleDeletes(size_t n) { single_deletes_ += n; } | |
163 | ||
164 | void AddRangeDeletions(long n) { range_deletions_ += n; } | |
165 | ||
166 | void AddCoveredByRangeDeletions(long n) { covered_by_range_deletions_ += n; } | |
167 | ||
168 | void AddErrors(long n) { errors_ += n; } | |
169 | ||
170 | void AddVerifiedErrors(long n) { verified_errors_ += n; } | |
171 | ||
172 | void AddNumCompactFilesSucceed(long n) { num_compact_files_succeed_ += n; } | |
173 | ||
174 | void AddNumCompactFilesFailed(long n) { num_compact_files_failed_ += n; } | |
175 | ||
176 | void Report(const char* name) { | |
177 | std::string extra; | |
178 | if (bytes_ < 1 || done_ < 1) { | |
179 | fprintf(stderr, "No writes or ops?\n"); | |
180 | return; | |
181 | } | |
182 | ||
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; | |
187 | ||
188 | fprintf(stdout, "%-12s: ", name); | |
189 | fprintf(stdout, "%.3f micros/op %ld ops/sec\n", seconds_ * 1e6 / done_, | |
190 | (long)throughput); | |
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", "", | |
196 | single_deletes_); | |
197 | fprintf(stdout, "%-12s: %ld read and %ld found the key\n", "", gets_, | |
198 | founds_); | |
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_); | |
206 | ||
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_); | |
212 | ||
213 | if (FLAGS_histogram) { | |
214 | fprintf(stdout, "Microseconds per op:\n%s\n", hist_.ToString().c_str()); | |
215 | } | |
216 | fflush(stdout); | |
217 | } | |
218 | }; | |
219 | } // namespace ROCKSDB_NAMESPACE |