blob: eae93672348e390de8468829dfd301f2ac638f26 [file] [log] [blame]
Eric Holkf1a2c0e2020-09-29 11:13:55 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "metrics.h"
18
19#include "gtest/gtest.h"
Eric Holk096bef82020-10-19 12:04:39 -070020#include "metrics_test.h"
Eric Holkf1a2c0e2020-09-29 11:13:55 -070021
22#pragma clang diagnostic push
23#pragma clang diagnostic error "-Wconversion"
24
25namespace art {
26namespace metrics {
27
Eric Holk096bef82020-10-19 12:04:39 -070028using test::CounterValue;
29using test::GetBuckets;
30using test::TestBackendBase;
31
Eric Holkf1a2c0e2020-09-29 11:13:55 -070032class MetricsTest : public testing::Test {};
33
34TEST_F(MetricsTest, SimpleCounter) {
Eric Holk096bef82020-10-19 12:04:39 -070035 MetricsCounter<DatumId::kClassVerificationTotalTime> test_counter;
Eric Holkf1a2c0e2020-09-29 11:13:55 -070036
Eric Holk096bef82020-10-19 12:04:39 -070037 EXPECT_EQ(0u, CounterValue(test_counter));
Eric Holkf1a2c0e2020-09-29 11:13:55 -070038
39 test_counter.AddOne();
Eric Holk096bef82020-10-19 12:04:39 -070040 EXPECT_EQ(1u, CounterValue(test_counter));
Eric Holkf1a2c0e2020-09-29 11:13:55 -070041
42 test_counter.Add(5);
Eric Holk096bef82020-10-19 12:04:39 -070043 EXPECT_EQ(6u, CounterValue(test_counter));
Eric Holkf1a2c0e2020-09-29 11:13:55 -070044}
45
Eric Holk1cd030f2020-09-30 11:42:34 -070046TEST_F(MetricsTest, CounterTimer) {
Eric Holk096bef82020-10-19 12:04:39 -070047 MetricsCounter<DatumId::kClassVerificationTotalTime> test_counter;
Eric Holk1cd030f2020-09-30 11:42:34 -070048 {
49 AutoTimer timer{&test_counter};
50 // Sleep for 2µs so the counter will be greater than 0.
51 NanoSleep(2'000);
52 }
Eric Holk096bef82020-10-19 12:04:39 -070053 EXPECT_GT(CounterValue(test_counter), 0u);
Eric Holk1cd030f2020-09-30 11:42:34 -070054}
55
56TEST_F(MetricsTest, CounterTimerExplicitStop) {
Eric Holk096bef82020-10-19 12:04:39 -070057 MetricsCounter<DatumId::kClassVerificationTotalTime> test_counter;
Eric Holk1cd030f2020-09-30 11:42:34 -070058 AutoTimer timer{&test_counter};
59 // Sleep for 2µs so the counter will be greater than 0.
60 NanoSleep(2'000);
61 timer.Stop();
Eric Holk096bef82020-10-19 12:04:39 -070062 EXPECT_GT(CounterValue(test_counter), 0u);
Eric Holk1cd030f2020-09-30 11:42:34 -070063}
64
65TEST_F(MetricsTest, CounterTimerExplicitStart) {
Eric Holk096bef82020-10-19 12:04:39 -070066 MetricsCounter<DatumId::kClassVerificationTotalTime> test_counter;
Eric Holk1cd030f2020-09-30 11:42:34 -070067 {
68 AutoTimer timer{&test_counter, /*autostart=*/false};
69 // Sleep for 2µs so the counter will be greater than 0.
70 NanoSleep(2'000);
71 }
Eric Holk096bef82020-10-19 12:04:39 -070072 EXPECT_EQ(CounterValue(test_counter), 0u);
Eric Holk1cd030f2020-09-30 11:42:34 -070073
74 {
75 AutoTimer timer{&test_counter, /*autostart=*/false};
76 timer.Start();
77 // Sleep for 2µs so the counter will be greater than 0.
78 NanoSleep(2'000);
79 }
Eric Holk096bef82020-10-19 12:04:39 -070080 EXPECT_GT(CounterValue(test_counter), 0u);
Eric Holk1cd030f2020-09-30 11:42:34 -070081}
82
83TEST_F(MetricsTest, CounterTimerExplicitStartStop) {
Eric Holk096bef82020-10-19 12:04:39 -070084 MetricsCounter<DatumId::kClassVerificationTotalTime> test_counter;
Eric Holk1cd030f2020-09-30 11:42:34 -070085 AutoTimer timer{&test_counter, /*autostart=*/false};
86 // Sleep for 2µs so the counter will be greater than 0.
87 timer.Start();
88 NanoSleep(2'000);
89 timer.Stop();
Eric Holk096bef82020-10-19 12:04:39 -070090 EXPECT_GT(CounterValue(test_counter), 0u);
Eric Holk1cd030f2020-09-30 11:42:34 -070091}
92
Eric Holk1043aa22021-03-09 15:41:11 -080093TEST_F(MetricsTest, AccumulatorMetric) {
94 MetricsAccumulator<DatumId::kClassLoadingTotalTime, uint64_t, std::max> accumulator;
95
96 std::vector<std::thread> threads;
97
98 constexpr uint64_t kMaxValue = 100;
99
100 for (uint64_t i = 0; i <= kMaxValue; i++) {
101 threads.emplace_back(std::thread{[&accumulator, i]() {
102 accumulator.Add(i);
103 }});
104 }
105
106 for (auto& thread : threads) {
107 thread.join();
108 }
109
110 EXPECT_EQ(CounterValue(accumulator), kMaxValue);
111}
112
Eric Holkf1a2c0e2020-09-29 11:13:55 -0700113TEST_F(MetricsTest, DatumName) {
114 EXPECT_EQ("ClassVerificationTotalTime", DatumName(DatumId::kClassVerificationTotalTime));
115}
116
Eric Holkd02435d2020-09-29 11:16:24 -0700117TEST_F(MetricsTest, SimpleHistogramTest) {
Eric Holk096bef82020-10-19 12:04:39 -0700118 MetricsHistogram<DatumId::kJitMethodCompileTime, 5, 0, 100> histogram;
Eric Holkd02435d2020-09-29 11:16:24 -0700119
120 // bucket 0: 0-19
121 histogram.Add(10);
122
123 // bucket 1: 20-39
124 histogram.Add(20);
125 histogram.Add(25);
126
127 // bucket 2: 40-59
128 histogram.Add(56);
129 histogram.Add(57);
130 histogram.Add(58);
131 histogram.Add(59);
132
133 // bucket 3: 60-79
134 histogram.Add(70);
135 histogram.Add(70);
136 histogram.Add(70);
137
138 // bucket 4: 80-99
139 // leave this bucket empty
140
Eric Holk096bef82020-10-19 12:04:39 -0700141 std::vector<uint32_t> buckets{GetBuckets(histogram)};
Eric Holkd02435d2020-09-29 11:16:24 -0700142 EXPECT_EQ(1u, buckets[0u]);
143 EXPECT_EQ(2u, buckets[1u]);
144 EXPECT_EQ(4u, buckets[2u]);
145 EXPECT_EQ(3u, buckets[3u]);
146 EXPECT_EQ(0u, buckets[4u]);
147}
148
149// Make sure values added outside the range of the histogram go into the first or last bucket.
150TEST_F(MetricsTest, HistogramOutOfRangeTest) {
Eric Holk096bef82020-10-19 12:04:39 -0700151 MetricsHistogram<DatumId::kJitMethodCompileTime, 2, 0, 100> histogram;
Eric Holkd02435d2020-09-29 11:16:24 -0700152
153 // bucket 0: 0-49
154 histogram.Add(-500);
155
156 // bucket 1: 50-99
157 histogram.Add(250);
158 histogram.Add(1000);
159
Eric Holk096bef82020-10-19 12:04:39 -0700160 std::vector<uint32_t> buckets{GetBuckets(histogram)};
Eric Holkd02435d2020-09-29 11:16:24 -0700161 EXPECT_EQ(1u, buckets[0u]);
162 EXPECT_EQ(2u, buckets[1u]);
163}
164
165// Test adding values to ArtMetrics and reporting them through a test backend.
166TEST_F(MetricsTest, ArtMetricsReport) {
167 ArtMetrics metrics;
168
169 // Collect some data
170 static constexpr uint64_t verification_time = 42;
171 metrics.ClassVerificationTotalTime()->Add(verification_time);
172 // Add a negative value so we are guaranteed that it lands in the first bucket.
173 metrics.JitMethodCompileTime()->Add(-5);
174
175 // Report and check the data
176 class TestBackend : public TestBackendBase {
177 public:
178 ~TestBackend() {
179 EXPECT_TRUE(found_counter_);
180 EXPECT_TRUE(found_histogram_);
181 }
182
183 void ReportCounter(DatumId counter_type, uint64_t value) override {
184 if (counter_type == DatumId::kClassVerificationTotalTime) {
185 EXPECT_EQ(value, verification_time);
186 found_counter_ = true;
187 } else {
188 EXPECT_EQ(value, 0u);
189 }
190 }
191
192 void ReportHistogram(DatumId histogram_type,
193 int64_t,
194 int64_t,
195 const std::vector<uint32_t>& buckets) override {
196 if (histogram_type == DatumId::kJitMethodCompileTime) {
197 EXPECT_EQ(buckets[0], 1u);
198 for (size_t i = 1; i < buckets.size(); ++i) {
199 EXPECT_EQ(buckets[i], 0u);
200 }
201 found_histogram_ = true;
202 } else {
203 for (size_t i = 0; i < buckets.size(); ++i) {
204 EXPECT_EQ(buckets[i], 0u);
205 }
206 }
207 }
208
209 private:
210 bool found_counter_{false};
211 bool found_histogram_{false};
212 } backend;
213
214 metrics.ReportAllMetrics(&backend);
215}
216
Eric Holk1cd030f2020-09-30 11:42:34 -0700217TEST_F(MetricsTest, HistogramTimer) {
Eric Holk096bef82020-10-19 12:04:39 -0700218 MetricsHistogram<DatumId::kJitMethodCompileTime, 1, 0, 100> test_histogram;
Eric Holk1cd030f2020-09-30 11:42:34 -0700219 {
220 AutoTimer timer{&test_histogram};
221 // Sleep for 2µs so the counter will be greater than 0.
222 NanoSleep(2'000);
223 }
224
Eric Holk096bef82020-10-19 12:04:39 -0700225 EXPECT_GT(GetBuckets(test_histogram)[0], 0u);
Eric Holk1cd030f2020-09-30 11:42:34 -0700226}
227
Eric Holk61c71ef2020-10-19 12:04:39 -0700228// Makes sure all defined metrics are included when dumping through StreamBackend.
229TEST_F(MetricsTest, StreamBackendDumpAllMetrics) {
230 ArtMetrics metrics;
Eric Holkc7ac91b2021-02-04 21:44:01 +0000231 StringBackend backend;
Eric Holk61c71ef2020-10-19 12:04:39 -0700232
233 metrics.ReportAllMetrics(&backend);
234
Eric Holka4c87952021-03-05 17:58:17 -0800235 // Make sure the resulting string lists all the metrics.
Eric Holkc7ac91b2021-02-04 21:44:01 +0000236 const std::string result = backend.GetAndResetBuffer();
Eric Holka4c87952021-03-05 17:58:17 -0800237#define METRIC(name, type, ...) \
Eric Holk74584e62021-02-18 14:39:17 -0800238 EXPECT_NE(result.find(DatumName(DatumId::k##name)), std::string::npos);
Eric Holka4c87952021-03-05 17:58:17 -0800239 ART_METRICS(METRIC);
240#undef METRIC
Eric Holk61c71ef2020-10-19 12:04:39 -0700241}
242
Eric Holkd91328f2021-03-17 16:21:51 -0700243TEST_F(MetricsTest, ResetMetrics) {
244 ArtMetrics metrics;
245
246 // Add something to each of the metrics.
247#define METRIC(name, type, ...) metrics.name()->Add(42);
248 ART_METRICS(METRIC)
249#undef METRIC
250
251 class NonZeroBackend : public TestBackendBase {
252 public:
253 void ReportCounter(DatumId, uint64_t value) override {
254 EXPECT_NE(value, 0u);
255 }
256
257 void ReportHistogram(DatumId, int64_t, int64_t, const std::vector<uint32_t>& buckets) override {
258 bool nonzero = false;
259 for (const auto value : buckets) {
260 nonzero |= (value != 0u);
261 }
262 EXPECT_TRUE(nonzero);
263 }
264 } non_zero_backend;
265
266 // Make sure the metrics all have a nonzero value.
267 metrics.ReportAllMetrics(&non_zero_backend);
268
269 // Reset the metrics and make sure they are all zero again
270 metrics.Reset();
271
272 class ZeroBackend : public TestBackendBase {
273 public:
274 void ReportCounter(DatumId, uint64_t value) override {
275 EXPECT_EQ(value, 0u);
276 }
277
278 void ReportHistogram(DatumId, int64_t, int64_t, const std::vector<uint32_t>& buckets) override {
279 for (const auto value : buckets) {
280 EXPECT_EQ(value, 0u);
281 }
282 }
283 } zero_backend;
284
285 metrics.ReportAllMetrics(&zero_backend);
286}
287
Eric Holkf1a2c0e2020-09-29 11:13:55 -0700288} // namespace metrics
289} // namespace art
290
291#pragma clang diagnostic pop // -Wconversion