forked from rethinkdb/rethinkdb_rebirth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.cc
395 lines (332 loc) · 11.8 KB
/
profile.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "rdb_protocol/profile.hpp"
#include <inttypes.h>
#include <limits>
#include "errors.hpp"
#include <boost/variant/static_visitor.hpp>
#include "containers/archive/stl_types.hpp"
#include "logger.hpp"
#include "rdb_protocol/math_utils.hpp"
#include "rdb_protocol/datum.hpp"
#include "rdb_protocol/env.hpp"
namespace profile {
start_t::start_t() { }
start_t::start_t(const std::string &description)
: description_(description), when_(get_ticks()) { }
RDB_IMPL_SERIALIZABLE_2_SINCE_v1_13(start_t, description_, when_.nanos);
split_t::split_t() { }
split_t::split_t(size_t n_parallel_jobs)
: n_parallel_jobs_(n_parallel_jobs) { }
RDB_IMPL_SERIALIZABLE_1_SINCE_v1_13(split_t, n_parallel_jobs_);
sample_t::sample_t() { }
sample_t::sample_t(const std::string &description,
ticks_t mean_duration, size_t n_samples)
: description_(description), mean_duration_(mean_duration),
n_samples_(n_samples)
{ }
RDB_IMPL_SERIALIZABLE_3_SINCE_v1_13(sample_t, description_,
mean_duration_.nanos, n_samples_);
stop_t::stop_t()
: when_(get_ticks()) { }
RDB_IMPL_SERIALIZABLE_1_SINCE_v1_13(stop_t, when_.nanos);
ql::datum_t construct_start(
ticks_t duration, std::string description,
ql::datum_t sub_tasks) {
std::map<datum_string_t, ql::datum_t> res;
res[datum_string_t("duration(ms)")] =
ql::datum_t(safe_to_double(duration.nanos) / MILLION);
res[datum_string_t("description")] =
ql::datum_t(datum_string_t(description));
res[datum_string_t("sub_tasks")] = sub_tasks;
return ql::datum_t(std::move(res));
}
ql::datum_t construct_split(
ql::datum_t par_tasks) {
std::map<datum_string_t, ql::datum_t> res;
res[datum_string_t("parallel_tasks")] = par_tasks;
return ql::datum_t(std::move(res));
}
ql::datum_t construct_sample(
const sample_t *sample) {
std::map<datum_string_t, ql::datum_t> res;
double mean_duration = safe_to_double(sample->mean_duration_.nanos) / MILLION;
double n_samples = safe_to_double(sample->n_samples_);
res[datum_string_t("mean_duration(ms)")] = ql::datum_t(mean_duration);
res[datum_string_t("n_samples")] = ql::datum_t(n_samples);
res[datum_string_t("description")] =
ql::datum_t(datum_string_t(sample->description_));
return ql::datum_t(std::move(res));
return ql::datum_t();
}
ql::datum_t construct_datum(
event_log_t::const_iterator *begin,
event_log_t::const_iterator end,
const ql::configured_limits_t &limits);
class construct_datum_visitor_t : public boost::static_visitor<void> {
public:
// N.B.: it is important that the lifetime of this visitor not
// exceed the lifetime of the limits reference: for example
// writing construct_datum_visitor_t v(begin, end,
// ql::configured_limits_t(), &res) is a terrible idea. When in
// doubt, use construct_datum, which ensures that its lifetime is
// a subset of the limits lifetime.
construct_datum_visitor_t(
event_log_t::const_iterator *begin, event_log_t::const_iterator end,
const ql::configured_limits_t *limits,
std::vector<ql::datum_t> *res)
: begin_(begin), end_(end), limits_(limits), res_(res) { }
void operator()(const start_t &start) const {
(*begin_)++;
ql::datum_t sub_tasks = construct_datum(begin_, end_, *limits_);
auto stop = boost::get<stop_t>(&**begin_);
guarantee(stop);
res_->push_back(construct_start(
ticks_t{stop->when_.nanos - start.when_.nanos}, start.description_, sub_tasks));
(*begin_)++;
}
void operator()(const split_t &split) const {
(*begin_)++;
std::vector<ql::datum_t> parallel_tasks;
for (size_t i = 0; i < split.n_parallel_jobs_; ++i) {
parallel_tasks.push_back(construct_datum(begin_, end_, *limits_));
guarantee(boost::get<stop_t>(&**begin_));
(*begin_)++;
}
res_->push_back(construct_split(
ql::datum_t(std::move(parallel_tasks), *limits_)));
}
void operator()(const sample_t &sample) const {
(*begin_)++;
res_->push_back(construct_sample(&sample));
}
void operator()(const stop_t &) const {
//Nothing to do here
}
private:
event_log_t::const_iterator *begin_;
event_log_t::const_iterator end_;
const ql::configured_limits_t *limits_;
std::vector<ql::datum_t> *res_;
};
ql::datum_t construct_datum(
event_log_t::const_iterator *begin,
event_log_t::const_iterator end,
const ql::configured_limits_t &limits) {
std::vector<ql::datum_t> res;
construct_datum_visitor_t visitor(begin, end, &limits, &res);
while (*begin != end && !boost::get<stop_t>(&**begin)) {
boost::apply_visitor(visitor, **begin);
}
return ql::datum_t(std::move(res), limits);
}
class print_event_log_visitor_t : public boost::static_visitor<void> {
public:
void operator()(const start_t &start) const {
logINF("Start: %s.\n", start.description_.c_str());
}
void operator()(const split_t &split) const {
logINF("Split: %zu.\n", split.n_parallel_jobs_);
}
void operator()(const sample_t &) const {
logINF("Sample.");
}
void operator()(const stop_t &) const {
logINF("Stop.\n");
}
};
void print_event_log(const event_log_t &event_log) {
print_event_log_visitor_t visitor;
for (auto it = event_log.begin(); it != event_log.end(); ++it) {
boost::apply_visitor(visitor, *it);
}
}
starter_t::starter_t(const std::string &description, trace_t *parent) {
init(description, parent);
}
starter_t::starter_t(const std::string &description, const scoped_ptr_t<trace_t> &parent) {
init(description, parent.get_or_null());
}
starter_t::~starter_t() {
if (parent_) {
parent_->stop();
}
}
void starter_t::init(const std::string &description, trace_t *parent) {
parent_ = parent;
if (parent_) {
parent_->start(description);
}
}
splitter_t::splitter_t(trace_t *parent) {
init(parent);
}
splitter_t::splitter_t(const scoped_ptr_t<trace_t> &parent) {
init(parent.get_or_null());
}
void splitter_t::give_splits(
size_t n_parallel_jobs, const event_log_t &event_log) {
n_parallel_jobs_ = n_parallel_jobs;
event_log_ = event_log;
}
splitter_t::~splitter_t() {
if (parent_) {
parent_->stop_split(n_parallel_jobs_, event_log_);
}
}
void splitter_t::init(trace_t *parent) {
parent_ = parent;
n_parallel_jobs_ = 0;
if (parent_) {
parent_->start_split();
}
}
sampler_t::sampler_t(const std::string &description, trace_t *parent) {
init(description, parent);
}
sampler_t::sampler_t(const std::string &description, const scoped_ptr_t<trace_t> &parent) {
init(description, parent.get_or_null());
}
void sampler_t::init(const std::string &description, trace_t *parent) {
parent_ = parent;
description_ = description;
total_time_ = ticks_t{0};
n_samples_ = 0;
if (parent_) {
parent_->start_sample(&event_log_);
}
}
ticks_t duration(const event_log_t &event_log) {
guarantee(!event_log.empty());
if (auto start = boost::get<start_t>(&event_log.at(0))) {
auto stop = boost::get<stop_t>(&event_log.back());
guarantee(stop);
return ticks_t{stop->when_.nanos - start->when_.nanos};
} else {
//This is a code path that is currently never hit and that will go a
//way when we implement more meaningful sampling functions.
return ticks_t{0};
}
}
void sampler_t::new_sample() {
if (!event_log_.empty()) {
n_samples_++;
total_time_.nanos += duration(event_log_).nanos;
}
event_log_.clear();
}
sampler_t::~sampler_t() {
new_sample();
if (parent_) {
if (n_samples_ > 0) {
parent_->stop_sample(description_, ticks_t{total_time_.nanos / int64_t(n_samples_)}, n_samples_, &event_log_);
} else {
parent_->stop_sample(&event_log_);
}
}
}
disabler_t::disabler_t(trace_t *parent) {
init(parent);
}
disabler_t::disabler_t(const scoped_ptr_t<trace_t> &parent) {
init(parent.get_or_null());
}
disabler_t::~disabler_t() {
if (parent_) {
parent_->enable();
}
}
void disabler_t::init(trace_t *parent) {
parent_ = parent;
if (parent_) {
parent_->disable();
}
}
trace_t::trace_t()
: redirected_event_log_(NULL), disabled_ref_count_(0) { }
ql::datum_t trace_t::as_datum() const {
guarantee(!redirected_event_log_);
event_log_t::const_iterator begin = event_log_.begin();
// Again, use defaults, as there's no predicting where this could
// come in response to user requests.
return construct_datum(&begin, event_log_.end(),
ql::configured_limits_t());
}
event_log_t trace_t::extract_event_log() RVALUE_THIS {
// These guarantees imply that this trace_t gets left in a default-constructed
// state (which is valid, thereby acceptable for an RVALUE_THIS function).
guarantee(redirected_event_log_ == NULL);
guarantee(disabled_ref_count_ == 0);
return std::move(event_log_);
}
void trace_t::start(const std::string &description) {
if (disabled()) { return; }
//debugf("Start %s %p.\n", description.c_str(), this);
event_log_target()->push_back(start_t(description));
}
void trace_t::stop() {
if (disabled()) { return; }
//debugf("Stop %p.\n", this);
event_log_target()->push_back(stop_t());
}
void trace_t::start_split() {
if (disabled()) { return; }
//debugf("Start split %p.\n", this);
event_log_target()->push_back(split_t());
}
void trace_t::stop_split(size_t n_parallel_jobs_, const event_log_t &par_event_log) {
if (disabled()) { return; }
//debugf("Stop split %zu, %p.\n", n_parallel_jobs_, this);
auto split = boost::get<split_t>(&event_log_target()->back());
guarantee(split);
split->n_parallel_jobs_ = n_parallel_jobs_;
event_log_target()->insert(event_log_target()->end(), par_event_log.begin(), par_event_log.end());
}
void trace_t::start_sample(event_log_t *event_log) {
if (disabled()) { return; }
//debugf("Start sample %p.\n", this);
/* This is a tad hacky. We currently don't allow samples within samples.
* And if someone tries to do it the inner sample winds up just being a
* no-op. We should see if in practice this is something we actually want
* to support. */
if (!redirected_event_log_) {
redirected_event_log_ = event_log;
}
}
void trace_t::stop_sample(const std::string &description,
ticks_t mean_duration, size_t n_samples, event_log_t *event_log) {
if (disabled()) { return; }
//debugf("Stop sample %s, %p.\n", description.c_str(), this);
/* Don't reset the redirected_event_log_ if the sampler_t wasn't
* actually being redirected to. The predicate fails when the
* innter samplers in nested sampler_ts are destructed. */
if (event_log == redirected_event_log_) {
redirected_event_log_ = NULL;
}
event_log_target()->push_back(sample_t(description, mean_duration, n_samples));
}
void trace_t::stop_sample(event_log_t *event_log) {
if (disabled()) { return; }
//debugf("Stop sample %p.\n", this);
/* Don't reset the redirected_event_log_ if the sampler_t wasn't
* actually being redirected to. The predicate fails when the
* innter samplers in nested sampler_ts are destructed. */
if (event_log == redirected_event_log_) {
redirected_event_log_ = NULL;
}
}
void trace_t::disable() {
disabled_ref_count_++;
}
void trace_t::enable() {
disabled_ref_count_--;
}
bool trace_t::disabled() {
return disabled_ref_count_ > 0;
}
event_log_t *trace_t::event_log_target() {
if (redirected_event_log_) {
return redirected_event_log_;
} else {
return &event_log_;
}
}
} //namespace profile