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
|
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "metrics.h"
#include "android-base/logging.h"
#include "base/macros.h"
#include "runtime.h"
#include "thread-current-inl.h"
#pragma clang diagnostic push
#pragma clang diagnostic error "-Wconversion"
namespace art {
namespace metrics {
std::string DatumName(DatumId datum) {
switch (datum) {
#define ART_COUNTER(name) \
case DatumId::k##name: \
return #name;
ART_COUNTERS(ART_COUNTER)
#undef ART_COUNTER
#define ART_HISTOGRAM(name, num_buckets, low_value, high_value) \
case DatumId::k##name: \
return #name;
ART_HISTOGRAMS(ART_HISTOGRAM)
#undef ART_HISTOGRAM
default:
LOG(FATAL) << "Unknown datum id: " << static_cast<unsigned>(datum);
UNREACHABLE();
}
}
ArtMetrics::ArtMetrics() : unused_ {}
#define ART_COUNTER(name) \
, name##_ {}
ART_COUNTERS(ART_COUNTER)
#undef ART_COUNTER
#define ART_HISTOGRAM(name, num_buckets, low_value, high_value) \
, name##_ {}
ART_HISTOGRAMS(ART_HISTOGRAM)
#undef ART_HISTOGRAM
{
}
void ArtMetrics::ReportAllMetrics(MetricsBackend* backend) const {
// Dump counters
#define ART_COUNTER(name) name()->Report(backend);
ART_COUNTERS(ART_COUNTER)
#undef ART_COUNTERS
// Dump histograms
#define ART_HISTOGRAM(name, num_buckets, low_value, high_value) name()->Report(backend);
ART_HISTOGRAMS(ART_HISTOGRAM)
#undef ART_HISTOGRAM
}
void ArtMetrics::DumpForSigQuit(std::ostream& os) const {
os << "\n*** ART internal metrics ***\n\n";
StreamBackend backend{os};
ReportAllMetrics(&backend);
os << "\n*** Done dumping ART internal metrics ***\n";
}
StreamBackend::StreamBackend(std::ostream& os) : os_{os} {}
void StreamBackend::BeginSession([[maybe_unused]] const SessionData& session_data) {
// Not needed for now.
}
void StreamBackend::EndSession() {
// Not needed for now.
}
void StreamBackend::ReportCounter(DatumId counter_type, uint64_t value) {
os_ << DatumName(counter_type) << ": count = " << value << "\n";
}
void StreamBackend::ReportHistogram(DatumId histogram_type,
int64_t minimum_value_,
int64_t maximum_value_,
const std::vector<uint32_t>& buckets) {
os_ << DatumName(histogram_type) << ": range = " << minimum_value_ << "..." << maximum_value_;
if (buckets.size() > 0) {
os_ << ", buckets: ";
bool first = true;
for (const auto& count : buckets) {
if (!first) {
os_ << ",";
}
first = false;
os_ << count;
}
os_ << "\n";
} else {
os_ << ", no buckets\n";
}
}
std::unique_ptr<MetricsReporter> MetricsReporter::Create(ReportingConfig config, Runtime* runtime) {
std::unique_ptr<MetricsBackend> backend;
// We can't use std::make_unique here because the MetricsReporter constructor is private.
return std::unique_ptr<MetricsReporter>{new MetricsReporter{config, runtime}};
}
MetricsReporter::MetricsReporter(ReportingConfig config, Runtime* runtime)
: config_{config}, runtime_{runtime} {}
MetricsReporter::~MetricsReporter() { StopBackgroundThreadIfRunning(); }
void MetricsReporter::StartBackgroundThreadIfNeeded() {
if (config_.BackgroundReportingEnabled()) {
CHECK(!thread_.has_value());
thread_.emplace(&MetricsReporter::BackgroundThreadRun, this);
}
}
void MetricsReporter::StopBackgroundThreadIfRunning() {
if (thread_.has_value()) {
messages_.SendMessage(ShutdownRequestedMessage{});
thread_->join();
}
// Do one final metrics report, if enabled.
if (config_.report_metrics_on_shutdown) {
ReportMetrics();
}
}
void MetricsReporter::BackgroundThreadRun() {
runtime_->AttachCurrentThread("Metrics Background Reporting Thread",
/*as_daemon=*/true,
runtime_->GetSystemThreadGroup(),
/*create_peer=*/true);
LOG_STREAM(DEBUG) << "Metrics reporting thread started";
bool running = true;
ResetTimeoutIfNeeded();
while (running) {
messages_.SwitchReceive(
[&]([[maybe_unused]] ShutdownRequestedMessage message) {
LOG_STREAM(DEBUG) << "Shutdown request received";
running = false;
},
[&]([[maybe_unused]] TimeoutExpiredMessage message) {
LOG_STREAM(DEBUG) << "Timer expired, reporting metrics";
ReportMetrics();
ResetTimeoutIfNeeded();
});
}
runtime_->DetachCurrentThread();
LOG_STREAM(DEBUG) << "Metrics reporting thread terminating";
}
void MetricsReporter::ResetTimeoutIfNeeded() {
if (config_.periodic_report_seconds.has_value()) {
messages_.SetTimeout(SecondsToMs(config_.periodic_report_seconds.value()));
}
}
void MetricsReporter::ReportMetrics() const {
if (config_.dump_to_logcat) {
LOG_STREAM(INFO) << "\n*** ART internal metrics ***\n\n";
// LOG_STREAM(INFO) destroys the stream at the end of the statement, which makes it tricky pass
// it to store as a field in StreamBackend. To get around this, we use an immediately-invoked
// lambda expression to act as a let-binding, letting us access the stream for long enough to
// dump the metrics.
[this](std::ostream& os) {
StreamBackend backend{os};
runtime_->GetMetrics()->ReportAllMetrics(&backend);
}(LOG_STREAM(INFO));
LOG_STREAM(INFO) << "\n*** Done dumping ART internal metrics ***\n";
}
}
} // namespace metrics
} // namespace art
#pragma clang diagnostic pop // -Wconversion
|