Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_METRICS_METRICS_H_ |
| 18 | #define ART_LIBARTBASE_BASE_METRICS_METRICS_H_ |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <array> |
Eric Holk | c4adf54 | 2020-10-02 13:46:28 -0700 | [diff] [blame] | 23 | #include <atomic> |
Eric Holk | 5bb354f | 2021-01-13 20:38:34 +0000 | [diff] [blame] | 24 | #include <optional> |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 25 | #include <sstream> |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 26 | #include <string_view> |
Eric Holk | 0b986f7 | 2021-01-20 22:24:06 +0000 | [diff] [blame] | 27 | #include <thread> |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 28 | #include <vector> |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 29 | |
Eric Holk | 1cd030f | 2020-09-30 11:42:34 -0700 | [diff] [blame] | 30 | #include "android-base/logging.h" |
Lokesh Gidra | e99a858 | 2021-02-23 19:07:39 -0800 | [diff] [blame] | 31 | #include "base/bit_utils.h" |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 32 | #include "base/compiler_filter.h" |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 33 | #include "base/time_utils.h" |
| 34 | |
| 35 | #pragma clang diagnostic push |
| 36 | #pragma clang diagnostic error "-Wconversion" |
| 37 | |
Eric Holk | a4c8795 | 2021-03-05 17:58:17 -0800 | [diff] [blame] | 38 | // See README.md in this directory for how to define metrics. |
| 39 | #define ART_METRICS(METRIC) \ |
| 40 | METRIC(ClassLoadingTotalTime, MetricsCounter) \ |
| 41 | METRIC(ClassVerificationTotalTime, MetricsCounter) \ |
Eric Holk | e7ff7ef | 2021-03-17 16:42:24 -0700 | [diff] [blame] | 42 | METRIC(ClassVerificationCount, MetricsCounter) \ |
Eric Holk | a4c8795 | 2021-03-05 17:58:17 -0800 | [diff] [blame] | 43 | METRIC(MutatorPauseTimeDuringGC, MetricsCounter) \ |
| 44 | METRIC(YoungGcCount, MetricsCounter) \ |
| 45 | METRIC(FullGcCount, MetricsCounter) \ |
| 46 | METRIC(TotalBytesAllocated, MetricsCounter) \ |
| 47 | METRIC(TotalGcMetaDataSize, MetricsCounter) \ |
| 48 | METRIC(JitMethodCompileTime, MetricsHistogram, 15, 0, 1'000'000) \ |
Eric Holk | b2a1416 | 2021-03-30 16:54:13 -0700 | [diff] [blame] | 49 | METRIC(JitMethodCompileCount, MetricsCounter) \ |
Eric Holk | a4c8795 | 2021-03-05 17:58:17 -0800 | [diff] [blame] | 50 | METRIC(YoungGcCollectionTime, MetricsHistogram, 15, 0, 60'000) \ |
| 51 | METRIC(FullGcCollectionTime, MetricsHistogram, 15, 0, 60'000) \ |
| 52 | METRIC(YoungGcThroughput, MetricsHistogram, 15, 0, 1'000) \ |
| 53 | METRIC(FullGcThroughput, MetricsHistogram, 15, 0, 1'000) |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 54 | |
| 55 | // A lot of the metrics implementation code is generated by passing one-off macros into ART_COUNTERS |
| 56 | // and ART_HISTOGRAMS. This means metrics.h and metrics.cc are very #define-heavy, which can be |
| 57 | // challenging to read. The alternative was to require a lot of boilerplate code for each new metric |
| 58 | // added, all of which would need to be rewritten if the metrics implementation changed. Using |
| 59 | // macros lets us add new metrics by adding a single line to either ART_COUNTERS or ART_HISTOGRAMS, |
| 60 | // and modifying the implementation only requires changing the implementation once, instead of once |
| 61 | // per metric. |
| 62 | |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 63 | namespace art { |
Eric Holk | 5bb354f | 2021-01-13 20:38:34 +0000 | [diff] [blame] | 64 | |
| 65 | class Runtime; |
| 66 | struct RuntimeArgumentMap; |
| 67 | |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 68 | namespace metrics { |
| 69 | |
| 70 | /** |
| 71 | * An enumeration of all ART counters and histograms. |
| 72 | */ |
| 73 | enum class DatumId { |
Eric Holk | a4c8795 | 2021-03-05 17:58:17 -0800 | [diff] [blame] | 74 | #define METRIC(name, type, ...) k##name, |
| 75 | ART_METRICS(METRIC) |
| 76 | #undef METRIC |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 79 | // We log compilation reasons as part of the metadata we report. Since elsewhere compilation reasons |
| 80 | // are specified as a string, we define them as an enum here which indicates the reasons that we |
| 81 | // support. |
| 82 | enum class CompilationReason { |
| 83 | kError, |
| 84 | kUnknown, |
| 85 | kFirstBoot, |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 86 | kBootAfterOTA, |
| 87 | kPostBoot, |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 88 | kInstall, |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 89 | kInstallFast, |
| 90 | kInstallBulk, |
| 91 | kInstallBulkSecondary, |
| 92 | kInstallBulkDowngraded, |
| 93 | kInstallBulkSecondaryDowngraded, |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 94 | kBgDexopt, |
| 95 | kABOTA, |
| 96 | kInactive, |
| 97 | kShared, |
| 98 | kInstallWithDexMetadata, |
Eric Holk | b772ec8 | 2021-05-13 17:18:34 -0700 | [diff] [blame^] | 99 | kPrebuilt, |
| 100 | kCmdLine |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | constexpr const char* CompilationReasonName(CompilationReason reason) { |
| 104 | switch (reason) { |
| 105 | case CompilationReason::kError: |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 106 | return "error"; |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 107 | case CompilationReason::kUnknown: |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 108 | return "unknown"; |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 109 | case CompilationReason::kFirstBoot: |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 110 | return "first-boot"; |
| 111 | case CompilationReason::kBootAfterOTA: |
| 112 | return "boot-after-ota"; |
| 113 | case CompilationReason::kPostBoot: |
| 114 | return "post-boot"; |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 115 | case CompilationReason::kInstall: |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 116 | return "install"; |
| 117 | case CompilationReason::kInstallFast: |
| 118 | return "install-fast"; |
| 119 | case CompilationReason::kInstallBulk: |
| 120 | return "install-bulk"; |
| 121 | case CompilationReason::kInstallBulkSecondary: |
| 122 | return "install-bulk-secondary"; |
| 123 | case CompilationReason::kInstallBulkDowngraded: |
| 124 | return "install-bulk-downgraded"; |
| 125 | case CompilationReason::kInstallBulkSecondaryDowngraded: |
| 126 | return "install-bulk-secondary-downgraded"; |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 127 | case CompilationReason::kBgDexopt: |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 128 | return "bg-dexopt"; |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 129 | case CompilationReason::kABOTA: |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 130 | return "ab-ota"; |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 131 | case CompilationReason::kInactive: |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 132 | return "inactive"; |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 133 | case CompilationReason::kShared: |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 134 | return "shared"; |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 135 | case CompilationReason::kInstallWithDexMetadata: |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 136 | return "install-with-dex-metadata"; |
Eric Holk | b772ec8 | 2021-05-13 17:18:34 -0700 | [diff] [blame^] | 137 | case CompilationReason::kPrebuilt: |
| 138 | return "prebuilt"; |
| 139 | case CompilationReason::kCmdLine: |
| 140 | return "cmdline"; |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 144 | constexpr CompilationReason CompilationReasonFromName(std::string_view name) { |
| 145 | // Names come from PackageManagerServiceCompilerMapping.java |
| 146 | if (name == "unknown") { |
| 147 | return CompilationReason::kUnknown; |
| 148 | } |
| 149 | if (name == "first-boot") { |
| 150 | return CompilationReason::kFirstBoot; |
| 151 | } |
| 152 | if (name == "boot-after-ota") { |
| 153 | return CompilationReason::kBootAfterOTA; |
| 154 | } |
| 155 | if (name == "post-boot") { |
| 156 | return CompilationReason::kPostBoot; |
| 157 | } |
| 158 | if (name == "install") { |
| 159 | return CompilationReason::kInstall; |
| 160 | } |
| 161 | if (name == "install-fast") { |
| 162 | return CompilationReason::kInstallFast; |
| 163 | } |
| 164 | if (name == "install-bulk") { |
| 165 | return CompilationReason::kInstallBulk; |
| 166 | } |
| 167 | if (name == "install-bulk-secondary") { |
| 168 | return CompilationReason::kInstallBulkSecondary; |
| 169 | } |
| 170 | if (name == "install-bulk-downgraded") { |
| 171 | return CompilationReason::kInstallBulkDowngraded; |
| 172 | } |
| 173 | if (name == "install-bulk-secondary-downgraded") { |
| 174 | return CompilationReason::kInstallBulkSecondaryDowngraded; |
| 175 | } |
| 176 | if (name == "bg-dexopt") { |
| 177 | return CompilationReason::kBgDexopt; |
| 178 | } |
| 179 | if (name == "ab-ota") { |
| 180 | return CompilationReason::kABOTA; |
| 181 | } |
| 182 | if (name == "inactive") { |
| 183 | return CompilationReason::kInactive; |
| 184 | } |
| 185 | if (name == "shared") { |
| 186 | return CompilationReason::kShared; |
| 187 | } |
| 188 | if (name == "install-with-dex-metadata") { |
| 189 | return CompilationReason::kInstallWithDexMetadata; |
| 190 | } |
Eric Holk | b772ec8 | 2021-05-13 17:18:34 -0700 | [diff] [blame^] | 191 | if (name == "prebuilt") { |
| 192 | return CompilationReason::kPrebuilt; |
| 193 | } |
| 194 | if (name == "cmdline") { |
| 195 | return CompilationReason::kCmdLine; |
| 196 | } |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 197 | return CompilationReason::kError; |
| 198 | } |
| 199 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 200 | // SessionData contains metadata about a metrics session (basically the lifetime of an ART process). |
| 201 | // This information should not change for the lifetime of the session. |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 202 | struct SessionData { |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 203 | static SessionData CreateDefault(); |
| 204 | |
| 205 | static constexpr int64_t kInvalidSessionId = -1; |
| 206 | static constexpr int32_t kInvalidUserId = -1; |
| 207 | |
| 208 | int64_t session_id; |
| 209 | int32_t uid; |
| 210 | CompilationReason compilation_reason; |
| 211 | std::optional<CompilerFilter::Filter> compiler_filter; |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | // MetricsBackends are used by a metrics reporter to write metrics to some external location. For |
| 215 | // example, a backend might write to logcat, or to a file, or to statsd. |
| 216 | class MetricsBackend { |
| 217 | public: |
| 218 | virtual ~MetricsBackend() {} |
| 219 | |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 220 | // Begins an ART metrics session. |
| 221 | // |
| 222 | // This is called by the metrics reporter when the runtime is starting up. The session_data |
| 223 | // includes a session id which is used to correlate any metric reports with the same instance of |
| 224 | // the ART runtime. Additionally, session_data includes useful metadata such as the package name |
| 225 | // for this process. |
| 226 | virtual void BeginSession(const SessionData& session_data) = 0; |
| 227 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 228 | protected: |
| 229 | // Called by the metrics reporter to indicate that a new metrics report is starting. |
Eric Holk | 39d529f | 2021-02-17 12:48:53 -0800 | [diff] [blame] | 230 | virtual void BeginReport(uint64_t timestamp_since_start_ms) = 0; |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 231 | |
| 232 | // Called by the metrics reporter to give the current value of the counter with id counter_type. |
| 233 | // |
| 234 | // This will be called multiple times for each counter based on when the metrics reporter chooses |
| 235 | // to report metrics. For example, the metrics reporter may call this at shutdown or every N |
| 236 | // minutes. Counters are not reset in between invocations, so the value should represent the |
| 237 | // total count at the point this method is called. |
| 238 | virtual void ReportCounter(DatumId counter_type, uint64_t value) = 0; |
| 239 | |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 240 | // Called by the metrics reporter to report a histogram. |
| 241 | // |
| 242 | // This is called similarly to ReportCounter, but instead of receiving a single value, it receives |
| 243 | // a vector of the value in each bucket. Additionally, the function receives the lower and upper |
| 244 | // limit for the histogram. Note that these limits are the allowed limits, and not the observed |
| 245 | // range. Values below the lower limit will be counted in the first bucket, and values above the |
| 246 | // upper limit will be counted in the last bucket. Backends should store the minimum and maximum |
| 247 | // values to allow comparisons across module versions, since the minimum and maximum values may |
| 248 | // change over time. |
| 249 | virtual void ReportHistogram(DatumId histogram_type, |
| 250 | int64_t minimum_value, |
| 251 | int64_t maximum_value, |
| 252 | const std::vector<uint32_t>& buckets) = 0; |
| 253 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 254 | // Called by the metrics reporter to indicate that the current metrics report is complete. |
| 255 | virtual void EndReport() = 0; |
| 256 | |
Lokesh Gidra | e99a858 | 2021-02-23 19:07:39 -0800 | [diff] [blame] | 257 | template <DatumId counter_type, typename T> |
Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 258 | friend class MetricsCounter; |
| 259 | template <DatumId histogram_type, size_t num_buckets, int64_t low_value, int64_t high_value> |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 260 | friend class MetricsHistogram; |
Eric Holk | 1043aa2 | 2021-03-09 15:41:11 -0800 | [diff] [blame] | 261 | template <DatumId datum_id, typename T, const T& AccumulatorFunction(const T&, const T&)> |
| 262 | friend class MetricsAccumulator; |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 263 | friend class ArtMetrics; |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
Lokesh Gidra | e99a858 | 2021-02-23 19:07:39 -0800 | [diff] [blame] | 266 | template <typename value_t> |
| 267 | class MetricsBase { |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 268 | public: |
Lokesh Gidra | e99a858 | 2021-02-23 19:07:39 -0800 | [diff] [blame] | 269 | virtual void Add(value_t value) = 0; |
| 270 | virtual ~MetricsBase() { } |
| 271 | }; |
Eric Holk | 1cd030f | 2020-09-30 11:42:34 -0700 | [diff] [blame] | 272 | |
Lokesh Gidra | e99a858 | 2021-02-23 19:07:39 -0800 | [diff] [blame] | 273 | template <DatumId counter_type, typename T = uint64_t> |
| 274 | class MetricsCounter final : public MetricsBase<T> { |
| 275 | public: |
| 276 | using value_t = T; |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 277 | explicit constexpr MetricsCounter(uint64_t value = 0) : value_{value} { |
| 278 | // Ensure we do not have any unnecessary data in this class. |
Lokesh Gidra | e99a858 | 2021-02-23 19:07:39 -0800 | [diff] [blame] | 279 | // Adding intptr_t to accommodate vtable, and rounding up to incorporate |
| 280 | // padding. |
| 281 | static_assert(RoundUp(sizeof(*this), sizeof(uint64_t)) |
| 282 | == RoundUp(sizeof(intptr_t) + sizeof(value_t), sizeof(uint64_t))); |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 283 | } |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 284 | |
Eric Holk | c4adf54 | 2020-10-02 13:46:28 -0700 | [diff] [blame] | 285 | void AddOne() { Add(1u); } |
Eric Holk | 1cd030f | 2020-09-30 11:42:34 -0700 | [diff] [blame] | 286 | void Add(value_t value) { value_.fetch_add(value, std::memory_order::memory_order_relaxed); } |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 287 | |
Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 288 | void Report(MetricsBackend* backend) const { backend->ReportCounter(counter_type, Value()); } |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 289 | |
Eric Holk | d91328f | 2021-03-17 16:21:51 -0700 | [diff] [blame] | 290 | protected: |
| 291 | void Reset() { |
| 292 | value_ = 0; |
| 293 | } |
| 294 | |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 295 | private: |
Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 296 | value_t Value() const { return value_.load(std::memory_order::memory_order_relaxed); } |
| 297 | |
Eric Holk | 1cd030f | 2020-09-30 11:42:34 -0700 | [diff] [blame] | 298 | std::atomic<value_t> value_; |
| 299 | static_assert(std::atomic<value_t>::is_always_lock_free); |
Eric Holk | d91328f | 2021-03-17 16:21:51 -0700 | [diff] [blame] | 300 | |
| 301 | friend class ArtMetrics; |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 302 | }; |
| 303 | |
Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 304 | template <DatumId histogram_type_, |
| 305 | size_t num_buckets_, |
| 306 | int64_t minimum_value_, |
| 307 | int64_t maximum_value_> |
Lokesh Gidra | e99a858 | 2021-02-23 19:07:39 -0800 | [diff] [blame] | 308 | class MetricsHistogram final : public MetricsBase<int64_t> { |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 309 | static_assert(num_buckets_ >= 1); |
| 310 | static_assert(minimum_value_ < maximum_value_); |
| 311 | |
| 312 | public: |
Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 313 | using value_t = uint32_t; |
Eric Holk | 1cd030f | 2020-09-30 11:42:34 -0700 | [diff] [blame] | 314 | |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 315 | constexpr MetricsHistogram() : buckets_{} { |
| 316 | // Ensure we do not have any unnecessary data in this class. |
Lokesh Gidra | e99a858 | 2021-02-23 19:07:39 -0800 | [diff] [blame] | 317 | // Adding intptr_t to accommodate vtable, and rounding up to incorporate |
| 318 | // padding. |
| 319 | static_assert(RoundUp(sizeof(*this), sizeof(uint64_t)) |
| 320 | == RoundUp(sizeof(intptr_t) + sizeof(value_t) * num_buckets_, sizeof(uint64_t))); |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void Add(int64_t value) { |
| 324 | const size_t i = FindBucketId(value); |
Eric Holk | c4adf54 | 2020-10-02 13:46:28 -0700 | [diff] [blame] | 325 | buckets_[i].fetch_add(1u, std::memory_order::memory_order_relaxed); |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 328 | void Report(MetricsBackend* backend) const { |
| 329 | backend->ReportHistogram(histogram_type_, minimum_value_, maximum_value_, GetBuckets()); |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Eric Holk | d91328f | 2021-03-17 16:21:51 -0700 | [diff] [blame] | 332 | protected: |
| 333 | void Reset() { |
| 334 | for (auto& bucket : buckets_) { |
| 335 | bucket = 0; |
| 336 | } |
| 337 | } |
| 338 | |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 339 | private: |
| 340 | inline constexpr size_t FindBucketId(int64_t value) const { |
| 341 | // Values below the minimum are clamped into the first bucket. |
| 342 | if (value <= minimum_value_) { |
| 343 | return 0; |
| 344 | } |
| 345 | // Values above the maximum are clamped into the last bucket. |
| 346 | if (value >= maximum_value_) { |
| 347 | return num_buckets_ - 1; |
| 348 | } |
| 349 | // Otherise, linearly interpolate the value into the right bucket |
| 350 | constexpr size_t bucket_width = maximum_value_ - minimum_value_; |
| 351 | return static_cast<size_t>(value - minimum_value_) * num_buckets_ / bucket_width; |
| 352 | } |
| 353 | |
Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 354 | std::vector<value_t> GetBuckets() const { |
| 355 | // The loads from buckets_ will all be memory_order_seq_cst, which means they will be acquire |
| 356 | // loads. This is a stricter memory order than is needed, but this should not be a |
| 357 | // performance-critical section of code. |
| 358 | return std::vector<value_t>{buckets_.begin(), buckets_.end()}; |
| 359 | } |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 360 | |
Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 361 | std::array<std::atomic<value_t>, num_buckets_> buckets_; |
| 362 | static_assert(std::atomic<value_t>::is_always_lock_free); |
Eric Holk | d91328f | 2021-03-17 16:21:51 -0700 | [diff] [blame] | 363 | |
| 364 | friend class ArtMetrics; |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 365 | }; |
| 366 | |
Eric Holk | 1043aa2 | 2021-03-09 15:41:11 -0800 | [diff] [blame] | 367 | template <DatumId datum_id, typename T, const T& AccumulatorFunction(const T&, const T&)> |
| 368 | class MetricsAccumulator final : MetricsBase<T> { |
| 369 | public: |
| 370 | explicit constexpr MetricsAccumulator(T value = 0) : value_{value} { |
| 371 | // Ensure we do not have any unnecessary data in this class. |
| 372 | // Adding intptr_t to accommodate vtable, and rounding up to incorporate |
| 373 | // padding. |
| 374 | static_assert(RoundUp(sizeof(*this), sizeof(uint64_t)) == |
| 375 | RoundUp(sizeof(intptr_t) + sizeof(T), sizeof(uint64_t))); |
| 376 | } |
| 377 | |
| 378 | void Add(T value) { |
| 379 | T current = value_.load(std::memory_order::memory_order_relaxed); |
| 380 | T new_value; |
| 381 | do { |
| 382 | new_value = AccumulatorFunction(current, value); |
| 383 | // If the value didn't change, don't bother storing it. |
| 384 | if (current == new_value) { |
| 385 | break; |
| 386 | } |
| 387 | } while (!value_.compare_exchange_weak( |
| 388 | current, new_value, std::memory_order::memory_order_relaxed)); |
| 389 | } |
| 390 | |
| 391 | // Report the metric as a counter, since this has only a single value. |
| 392 | void Report(MetricsBackend* backend) const { |
| 393 | backend->ReportCounter(datum_id, static_cast<uint64_t>(Value())); |
| 394 | } |
| 395 | |
| 396 | protected: |
| 397 | void Reset() { |
| 398 | value_ = 0; |
| 399 | } |
| 400 | |
| 401 | private: |
| 402 | T Value() const { return value_.load(std::memory_order::memory_order_relaxed); } |
| 403 | |
| 404 | std::atomic<T> value_; |
| 405 | |
| 406 | friend class ArtMetrics; |
| 407 | }; |
| 408 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 409 | // A backend that writes metrics in a human-readable format to a string. |
| 410 | // |
| 411 | // This is used as a base for LogBackend and FileBackend. |
| 412 | class StringBackend : public MetricsBackend { |
Eric Holk | 61c71ef | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 413 | public: |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 414 | StringBackend(); |
Eric Holk | 61c71ef | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 415 | |
| 416 | void BeginSession(const SessionData& session_data) override; |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 417 | |
| 418 | void BeginReport(uint64_t timestamp_millis) override; |
Eric Holk | 61c71ef | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 419 | |
| 420 | void ReportCounter(DatumId counter_type, uint64_t value) override; |
| 421 | |
| 422 | void ReportHistogram(DatumId histogram_type, |
| 423 | int64_t low_value, |
| 424 | int64_t high_value, |
| 425 | const std::vector<uint32_t>& buckets) override; |
| 426 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 427 | void EndReport() override; |
| 428 | |
| 429 | std::string GetAndResetBuffer(); |
| 430 | |
Eric Holk | 61c71ef | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 431 | private: |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 432 | std::ostringstream os_; |
| 433 | std::optional<SessionData> session_data_; |
| 434 | }; |
| 435 | |
| 436 | // A backend that writes metrics in human-readable format to the log (i.e. logcat). |
| 437 | class LogBackend : public StringBackend { |
| 438 | public: |
| 439 | explicit LogBackend(android::base::LogSeverity level); |
| 440 | |
| 441 | void BeginReport(uint64_t timestamp_millis) override; |
| 442 | void EndReport() override; |
| 443 | |
| 444 | private: |
| 445 | android::base::LogSeverity level_; |
| 446 | }; |
| 447 | |
| 448 | // A backend that writes metrics to a file. |
| 449 | // |
| 450 | // These are currently written in the same human-readable format used by StringBackend and |
| 451 | // LogBackend, but we will probably want a more machine-readable format in the future. |
| 452 | class FileBackend : public StringBackend { |
| 453 | public: |
Greg Kaiser | 271662c | 2021-02-10 06:44:08 -0800 | [diff] [blame] | 454 | explicit FileBackend(const std::string& filename); |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 455 | |
| 456 | void BeginReport(uint64_t timestamp_millis) override; |
| 457 | void EndReport() override; |
| 458 | |
| 459 | private: |
| 460 | std::string filename_; |
Eric Holk | 61c71ef | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 461 | }; |
| 462 | |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 463 | /** |
Eric Holk | 1cd030f | 2020-09-30 11:42:34 -0700 | [diff] [blame] | 464 | * AutoTimer simplifies time-based metrics collection. |
| 465 | * |
| 466 | * Several modes are supported. In the default case, the timer starts immediately and stops when it |
| 467 | * goes out of scope. Example: |
| 468 | * |
| 469 | * { |
| 470 | * AutoTimer timer{metric}; |
| 471 | * DoStuff(); |
| 472 | * // timer stops and updates metric automatically here. |
| 473 | * } |
| 474 | * |
| 475 | * You can also stop the timer early: |
| 476 | * |
| 477 | * timer.Stop(); |
| 478 | * |
| 479 | * Finally, you can choose to not automatically start the timer at the beginning by passing false as |
| 480 | * the second argument to the constructor: |
| 481 | * |
| 482 | * AutoTimer timer{metric, false}; |
| 483 | * DoNotTimeThis(); |
| 484 | * timer.Start(); |
| 485 | * TimeThis(); |
| 486 | * |
| 487 | * Manually started timers will still automatically stop in the destructor, but they can be manually |
| 488 | * stopped as well. |
| 489 | * |
| 490 | * Note that AutoTimer makes calls to MicroTime(), so this may not be suitable on critical paths, or |
| 491 | * in cases where the counter needs to be started and stopped on different threads. |
| 492 | */ |
| 493 | template <typename Metric> |
| 494 | class AutoTimer { |
| 495 | public: |
| 496 | explicit AutoTimer(Metric* metric, bool autostart = true) |
| 497 | : running_{false}, start_time_microseconds_{}, metric_{metric} { |
| 498 | if (autostart) { |
| 499 | Start(); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | ~AutoTimer() { |
| 504 | if (running_) { |
| 505 | Stop(); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | void Start() { |
| 510 | DCHECK(!running_); |
| 511 | running_ = true; |
| 512 | start_time_microseconds_ = MicroTime(); |
| 513 | } |
| 514 | |
Eric Holk | a79872b | 2020-10-01 13:09:53 -0700 | [diff] [blame] | 515 | // Stops a running timer. Returns the time elapsed since starting the timer in microseconds. |
| 516 | uint64_t Stop() { |
Eric Holk | 1cd030f | 2020-09-30 11:42:34 -0700 | [diff] [blame] | 517 | DCHECK(running_); |
| 518 | uint64_t stop_time_microseconds = MicroTime(); |
| 519 | running_ = false; |
| 520 | |
Eric Holk | a79872b | 2020-10-01 13:09:53 -0700 | [diff] [blame] | 521 | uint64_t elapsed_time = stop_time_microseconds - start_time_microseconds_; |
| 522 | metric_->Add(static_cast<typename Metric::value_t>(elapsed_time)); |
| 523 | return elapsed_time; |
Eric Holk | 1cd030f | 2020-09-30 11:42:34 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | private: |
| 527 | bool running_; |
| 528 | uint64_t start_time_microseconds_; |
| 529 | Metric* metric_; |
| 530 | }; |
| 531 | |
| 532 | /** |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 533 | * This struct contains all of the metrics that ART reports. |
| 534 | */ |
| 535 | class ArtMetrics { |
| 536 | public: |
| 537 | ArtMetrics(); |
| 538 | |
| 539 | void ReportAllMetrics(MetricsBackend* backend) const; |
Eric Holk | 61c71ef | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 540 | void DumpForSigQuit(std::ostream& os) const; |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 541 | |
Eric Holk | d91328f | 2021-03-17 16:21:51 -0700 | [diff] [blame] | 542 | // Resets all metrics to their initial value. This is intended to be used after forking from the |
| 543 | // zygote so we don't attribute parent values to the child process. |
| 544 | void Reset(); |
| 545 | |
Eric Holk | a4c8795 | 2021-03-05 17:58:17 -0800 | [diff] [blame] | 546 | #define METRIC_ACCESSORS(name, Kind, ...) \ |
| 547 | Kind<DatumId::k##name, ##__VA_ARGS__>* name() { return &name##_; } \ |
| 548 | const Kind<DatumId::k##name, ##__VA_ARGS__>* name() const { return &name##_; } |
| 549 | ART_METRICS(METRIC_ACCESSORS) |
| 550 | #undef METRIC_ACCESSORS |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 551 | |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 552 | private: |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 553 | uint64_t beginning_timestamp_; |
Eric Holk | d02435d | 2020-09-29 11:16:24 -0700 | [diff] [blame] | 554 | |
Eric Holk | a4c8795 | 2021-03-05 17:58:17 -0800 | [diff] [blame] | 555 | #define METRIC(name, Kind, ...) Kind<DatumId::k##name, ##__VA_ARGS__> name##_; |
| 556 | ART_METRICS(METRIC) |
| 557 | #undef METRIC |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 558 | }; |
| 559 | |
| 560 | // Returns a human readable name for the given DatumId. |
| 561 | std::string DatumName(DatumId datum); |
| 562 | |
Eric Holk | 39d529f | 2021-02-17 12:48:53 -0800 | [diff] [blame] | 563 | // We also log the thread type for metrics so we can distinguish things that block the UI thread |
| 564 | // from things that happen on the background thread. This enum keeps track of what thread types we |
| 565 | // support. |
| 566 | enum class ThreadType { |
| 567 | kMain, |
| 568 | kBackground, |
| 569 | }; |
| 570 | |
Eric Holk | f1a2c0e | 2020-09-29 11:13:55 -0700 | [diff] [blame] | 571 | } // namespace metrics |
| 572 | } // namespace art |
| 573 | |
| 574 | #pragma clang diagnostic pop // -Wconversion |
| 575 | |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 576 | #endif // ART_LIBARTBASE_BASE_METRICS_METRICS_H_ |