Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [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 | |
| 17 | #include <sstream> |
| 18 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 19 | #include "android-base/file.h" |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 20 | #include "android-base/logging.h" |
| 21 | #include "base/macros.h" |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 22 | #include "base/scoped_flock.h" |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 23 | #include "metrics.h" |
| 24 | |
| 25 | #pragma clang diagnostic push |
| 26 | #pragma clang diagnostic error "-Wconversion" |
| 27 | |
| 28 | namespace art { |
| 29 | namespace metrics { |
| 30 | |
| 31 | std::string DatumName(DatumId datum) { |
| 32 | switch (datum) { |
Eric Holk | a4c8795 | 2021-03-05 17:58:17 -0800 | [diff] [blame] | 33 | #define ART_METRIC(name, Kind, ...) \ |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 34 | case DatumId::k##name: \ |
| 35 | return #name; |
Eric Holk | a4c8795 | 2021-03-05 17:58:17 -0800 | [diff] [blame] | 36 | ART_METRICS(ART_METRIC) |
| 37 | #undef ART_METRIC |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 38 | |
| 39 | default: |
| 40 | LOG(FATAL) << "Unknown datum id: " << static_cast<unsigned>(datum); |
| 41 | UNREACHABLE(); |
| 42 | } |
| 43 | } |
| 44 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 45 | SessionData SessionData::CreateDefault() { |
| 46 | #ifdef _WIN32 |
| 47 | int32_t uid = kInvalidUserId; // Windows does not support getuid(); |
| 48 | #else |
| 49 | int32_t uid = static_cast<int32_t>(getuid()); |
| 50 | #endif |
| 51 | |
| 52 | return SessionData{ |
| 53 | .compilation_reason = CompilationReason::kUnknown, |
| 54 | .compiler_filter = std::nullopt, |
| 55 | .session_id = kInvalidSessionId, |
| 56 | .uid = uid, |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | ArtMetrics::ArtMetrics() : beginning_timestamp_ {MilliTime()} |
Eric Holk | a4c8795 | 2021-03-05 17:58:17 -0800 | [diff] [blame] | 61 | #define ART_METRIC(name, Kind, ...) \ |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 62 | , name##_ {} |
Eric Holk | a4c8795 | 2021-03-05 17:58:17 -0800 | [diff] [blame] | 63 | ART_METRICS(ART_METRIC) |
| 64 | #undef ART_METRIC |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 65 | { |
| 66 | } |
| 67 | |
| 68 | void ArtMetrics::ReportAllMetrics(MetricsBackend* backend) const { |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 69 | backend->BeginReport(MilliTime() - beginning_timestamp_); |
| 70 | |
Eric Holk | a4c8795 | 2021-03-05 17:58:17 -0800 | [diff] [blame] | 71 | #define ART_METRIC(name, Kind, ...) name()->Report(backend); |
| 72 | ART_METRICS(ART_METRIC) |
Eric Holk | d91328f | 2021-03-17 16:21:51 -0700 | [diff] [blame] | 73 | #undef ART_METRIC |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 74 | |
| 75 | backend->EndReport(); |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void ArtMetrics::DumpForSigQuit(std::ostream& os) const { |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 79 | StringBackend backend; |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 80 | ReportAllMetrics(&backend); |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 81 | os << backend.GetAndResetBuffer(); |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Eric Holk | d91328f | 2021-03-17 16:21:51 -0700 | [diff] [blame] | 84 | void ArtMetrics::Reset() { |
| 85 | beginning_timestamp_ = MilliTime(); |
| 86 | #define ART_METRIC(name, kind, ...) name##_.Reset(); |
| 87 | ART_METRICS(ART_METRIC); |
| 88 | #undef ART_METRIC |
| 89 | } |
| 90 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 91 | StringBackend::StringBackend() {} |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 92 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 93 | std::string StringBackend::GetAndResetBuffer() { |
| 94 | std::string result = os_.str(); |
| 95 | os_.clear(); |
| 96 | os_.str(""); |
| 97 | return result; |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 100 | void StringBackend::BeginSession(const SessionData& session_data) { |
| 101 | session_data_ = session_data; |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 104 | void StringBackend::BeginReport(uint64_t timestamp_since_start_ms) { |
| 105 | os_ << "\n*** ART internal metrics ***\n"; |
| 106 | os_ << " Metadata:\n"; |
| 107 | os_ << " timestamp_since_start_ms: " << timestamp_since_start_ms << "\n"; |
| 108 | if (session_data_.has_value()) { |
| 109 | os_ << " session_id: " << session_data_->session_id << "\n"; |
| 110 | os_ << " uid: " << session_data_->uid << "\n"; |
| 111 | os_ << " compilation_reason: " << CompilationReasonName(session_data_->compilation_reason) |
| 112 | << "\n"; |
| 113 | os_ << " compiler_filter: " |
| 114 | << (session_data_->compiler_filter.has_value() |
| 115 | ? CompilerFilter::NameOfFilter(session_data_->compiler_filter.value()) |
| 116 | : "(unspecified)") |
| 117 | << "\n"; |
| 118 | } |
| 119 | os_ << " Metrics:\n"; |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 122 | void StringBackend::EndReport() { os_ << "*** Done dumping ART internal metrics ***\n"; } |
| 123 | |
| 124 | void StringBackend::ReportCounter(DatumId counter_type, uint64_t value) { |
| 125 | os_ << " " << DatumName(counter_type) << ": count = " << value << "\n"; |
| 126 | } |
| 127 | |
| 128 | void StringBackend::ReportHistogram(DatumId histogram_type, |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 129 | int64_t minimum_value_, |
| 130 | int64_t maximum_value_, |
| 131 | const std::vector<uint32_t>& buckets) { |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 132 | os_ << " " << DatumName(histogram_type) << ": range = " << minimum_value_ << "..." << maximum_value_; |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 133 | if (buckets.size() > 0) { |
| 134 | os_ << ", buckets: "; |
| 135 | bool first = true; |
| 136 | for (const auto& count : buckets) { |
| 137 | if (!first) { |
| 138 | os_ << ","; |
| 139 | } |
| 140 | first = false; |
| 141 | os_ << count; |
| 142 | } |
| 143 | os_ << "\n"; |
| 144 | } else { |
| 145 | os_ << ", no buckets\n"; |
| 146 | } |
| 147 | } |
| 148 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 149 | LogBackend::LogBackend(android::base::LogSeverity level) : level_{level} {} |
| 150 | |
| 151 | void LogBackend::BeginReport(uint64_t timestamp_since_start_ms) { |
| 152 | GetAndResetBuffer(); |
| 153 | StringBackend::BeginReport(timestamp_since_start_ms); |
| 154 | } |
| 155 | |
| 156 | void LogBackend::EndReport() { |
| 157 | StringBackend::EndReport(); |
| 158 | LOG_STREAM(level_) << GetAndResetBuffer(); |
| 159 | } |
| 160 | |
Greg Kaiser | 271662c | 2021-02-10 06:44:08 -0800 | [diff] [blame] | 161 | FileBackend::FileBackend(const std::string& filename) : filename_{filename} {} |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame] | 162 | |
| 163 | void FileBackend::BeginReport(uint64_t timestamp_since_start_ms) { |
| 164 | GetAndResetBuffer(); |
| 165 | StringBackend::BeginReport(timestamp_since_start_ms); |
| 166 | } |
| 167 | |
| 168 | void FileBackend::EndReport() { |
| 169 | StringBackend::EndReport(); |
| 170 | std::string error_message; |
| 171 | auto file{ |
| 172 | LockedFile::Open(filename_.c_str(), O_CREAT | O_WRONLY | O_APPEND, true, &error_message)}; |
| 173 | if (file.get() == nullptr) { |
| 174 | LOG(WARNING) << "Could open metrics file '" << filename_ << "': " << error_message; |
| 175 | } else { |
| 176 | if (!android::base::WriteStringToFd(GetAndResetBuffer(), file.get()->Fd())) { |
| 177 | PLOG(WARNING) << "Error writing metrics to file"; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 182 | // Make sure CompilationReasonName and CompilationReasonForName are inverses. |
| 183 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kError)) == |
| 184 | CompilationReason::kError); |
| 185 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kUnknown)) == |
| 186 | CompilationReason::kUnknown); |
| 187 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kFirstBoot)) == |
| 188 | CompilationReason::kFirstBoot); |
| 189 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kBootAfterOTA)) == |
| 190 | CompilationReason::kBootAfterOTA); |
| 191 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kPostBoot)) == |
| 192 | CompilationReason::kPostBoot); |
| 193 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kInstall)) == |
| 194 | CompilationReason::kInstall); |
| 195 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kInstallFast)) == |
| 196 | CompilationReason::kInstallFast); |
| 197 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kInstallBulk)) == |
| 198 | CompilationReason::kInstallBulk); |
| 199 | static_assert( |
| 200 | CompilationReasonFromName(CompilationReasonName(CompilationReason::kInstallBulkSecondary)) == |
| 201 | CompilationReason::kInstallBulkSecondary); |
| 202 | static_assert( |
| 203 | CompilationReasonFromName(CompilationReasonName(CompilationReason::kInstallBulkDowngraded)) == |
| 204 | CompilationReason::kInstallBulkDowngraded); |
| 205 | static_assert(CompilationReasonFromName( |
| 206 | CompilationReasonName(CompilationReason::kInstallBulkSecondaryDowngraded)) == |
| 207 | CompilationReason::kInstallBulkSecondaryDowngraded); |
| 208 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kBgDexopt)) == |
| 209 | CompilationReason::kBgDexopt); |
| 210 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kABOTA)) == |
| 211 | CompilationReason::kABOTA); |
| 212 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kInactive)) == |
| 213 | CompilationReason::kInactive); |
| 214 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kShared)) == |
| 215 | CompilationReason::kShared); |
| 216 | static_assert( |
| 217 | CompilationReasonFromName(CompilationReasonName(CompilationReason::kInstallWithDexMetadata)) == |
| 218 | CompilationReason::kInstallWithDexMetadata); |
Eric Holk | 7414ec2 | 2021-05-13 17:18:34 -0700 | [diff] [blame^] | 219 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kPrebuilt)) == |
| 220 | CompilationReason::kPrebuilt); |
| 221 | static_assert(CompilationReasonFromName(CompilationReasonName(CompilationReason::kCmdLine)) == |
| 222 | CompilationReason::kCmdLine); |
Eric Holk | 722992f | 2021-04-06 23:12:17 +0000 | [diff] [blame] | 223 | |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 224 | } // namespace metrics |
| 225 | } // namespace art |
| 226 | |
| 227 | #pragma clang diagnostic pop // -Wconversion |