forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcast_runtime_histogram_flattener.cc
77 lines (60 loc) · 2.54 KB
/
cast_runtime_histogram_flattener.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
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/cast_core/cast_runtime_histogram_flattener.h"
#include "base/metrics/histogram_flattener.h"
#include "base/metrics/histogram_snapshot_manager.h"
#include "base/metrics/statistics_recorder.h"
namespace chromecast {
namespace {
// This is an ephemeral utility class for interacting with StatisticsRecorder
// and HistogramSnapshotManager. It collects histogram samples via
// PrepareDeltas and then puts them in cast::metrics::Histogram form.
class CastRuntimeHistogramFlattener final : public base::HistogramFlattener {
public:
CastRuntimeHistogramFlattener() : snapshot_manager_(this) {}
~CastRuntimeHistogramFlattener() override = default;
CastRuntimeHistogramFlattener(const CastRuntimeHistogramFlattener&) = delete;
CastRuntimeHistogramFlattener& operator=(
const CastRuntimeHistogramFlattener&) = delete;
std::vector<cast::metrics::Histogram> GetDeltas() {
DCHECK(deltas_.empty());
// Gather all histogram deltas, which will be sent to RecordDelta() and
// buffered in |deltas_|.
base::StatisticsRecorder::PrepareDeltas(
// Only return in-memory/non-persisted histograms.
false,
// Do not set flags on histograms.
base::Histogram::kNoFlags,
// Only upload metrics marked for UMA upload.
base::Histogram::kUmaTargetedHistogramFlag, &snapshot_manager_);
return std::move(deltas_);
}
private:
void RecordDelta(const base::HistogramBase& histogram,
const base::HistogramSamples& samples) override {
DCHECK_NE(0, samples.TotalCount());
cast::metrics::Histogram converted;
converted.set_name(histogram.histogram_name());
converted.set_sum(samples.sum());
for (std::unique_ptr<base::SampleCountIterator> it = samples.Iterator();
!it->Done(); it->Next()) {
base::Histogram::Sample min;
int64_t max = 0;
base::Histogram::Count count;
it->Get(&min, &max, &count);
cast::metrics::HistogramBucket* bucket = converted.add_bucket();
bucket->set_min(min);
bucket->set_max(max);
bucket->set_count(count);
}
deltas_.push_back(std::move(converted));
}
base::HistogramSnapshotManager snapshot_manager_;
std::vector<cast::metrics::Histogram> deltas_;
};
} // namespace
std::vector<cast::metrics::Histogram> GetHistogramDeltas() {
return CastRuntimeHistogramFlattener().GetDeltas();
}
} // namespace chromecast