Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2017 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 "update_engine/metrics_reporter_android.h" |
| 18 | |
Tianjie Xu | a215b59 | 2019-08-02 14:53:38 -0700 | [diff] [blame^] | 19 | #include <stdint.h> |
| 20 | |
Tianjie Xu | d4c5deb | 2017-10-24 11:17:03 -0700 | [diff] [blame] | 21 | #include <memory> |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | |
Tianjie Xu | a215b59 | 2019-08-02 14:53:38 -0700 | [diff] [blame^] | 24 | #include <android-base/properties.h> |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 25 | #include <metricslogger/metrics_logger.h> |
Tianjie Xu | a215b59 | 2019-08-02 14:53:38 -0700 | [diff] [blame^] | 26 | #include <statslog.h> |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 27 | |
| 28 | #include "update_engine/common/constants.h" |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 29 | |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 30 | namespace { |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 31 | void LogHistogram(const std::string& metrics, int value) { |
| 32 | android::metricslogger::LogHistogram(metrics, value); |
Tianjie Xu | 8cbf696 | 2017-11-14 16:09:30 -0800 | [diff] [blame] | 33 | LOG(INFO) << "uploading " << value << " to histogram for metric " << metrics; |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 34 | } |
Tianjie Xu | a215b59 | 2019-08-02 14:53:38 -0700 | [diff] [blame^] | 35 | |
| 36 | // A number offset adds on top of the enum value. e.g. ErrorCode::SUCCESS will |
| 37 | // be reported as 10000, and AttemptResult::UPDATE_CANCELED will be reported as |
| 38 | // 10011. The keeps the ordering of update engine's enum definition when statsd |
| 39 | // atoms reserve the value 0 for unknown state. |
| 40 | constexpr auto kMetricsReporterEnumOffset = 10000; |
| 41 | |
| 42 | int32_t GetStatsdEnumValue(int32_t value) { |
| 43 | return kMetricsReporterEnumOffset + value; |
| 44 | } |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 45 | } // namespace |
| 46 | |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 47 | namespace chromeos_update_engine { |
| 48 | |
| 49 | namespace metrics { |
| 50 | |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 51 | // The histograms are defined in: |
| 52 | // depot/google3/analysis/uma/configs/clearcut/TRON/histograms.xml |
| 53 | constexpr char kMetricsUpdateEngineAttemptNumber[] = |
Tianjie Xu | 8cbf696 | 2017-11-14 16:09:30 -0800 | [diff] [blame] | 54 | "ota_update_engine_attempt_number"; |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 55 | constexpr char kMetricsUpdateEngineAttemptResult[] = |
| 56 | "ota_update_engine_attempt_result"; |
| 57 | constexpr char kMetricsUpdateEngineAttemptDurationInMinutes[] = |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 58 | "ota_update_engine_attempt_fixed_duration_boottime_in_minutes"; |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 59 | constexpr char kMetricsUpdateEngineAttemptDurationUptimeInMinutes[] = |
Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 60 | "ota_update_engine_attempt_duration_monotonic_in_minutes"; |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 61 | constexpr char kMetricsUpdateEngineAttemptErrorCode[] = |
| 62 | "ota_update_engine_attempt_error_code"; |
| 63 | constexpr char kMetricsUpdateEngineAttemptPayloadSizeMiB[] = |
| 64 | "ota_update_engine_attempt_payload_size_mib"; |
| 65 | constexpr char kMetricsUpdateEngineAttemptPayloadType[] = |
| 66 | "ota_update_engine_attempt_payload_type"; |
Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 67 | constexpr char kMetricsUpdateEngineAttemptCurrentBytesDownloadedMiB[] = |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 68 | "ota_update_engine_attempt_fixed_current_bytes_downloaded_mib"; |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 69 | |
| 70 | constexpr char kMetricsUpdateEngineSuccessfulUpdateAttemptCount[] = |
| 71 | "ota_update_engine_successful_update_attempt_count"; |
| 72 | constexpr char kMetricsUpdateEngineSuccessfulUpdateTotalDurationInMinutes[] = |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 73 | "ota_update_engine_successful_update_fixed_total_duration_in_minutes"; |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 74 | constexpr char kMetricsUpdateEngineSuccessfulUpdatePayloadSizeMiB[] = |
| 75 | "ota_update_engine_successful_update_payload_size_mib"; |
| 76 | constexpr char kMetricsUpdateEngineSuccessfulUpdatePayloadType[] = |
| 77 | "ota_update_engine_successful_update_payload_type"; |
| 78 | constexpr char kMetricsUpdateEngineSuccessfulUpdateRebootCount[] = |
| 79 | "ota_update_engine_successful_update_reboot_count"; |
Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 80 | constexpr char kMetricsUpdateEngineSuccessfulUpdateTotalBytesDownloadedMiB[] = |
| 81 | "ota_update_engine_successful_update_total_bytes_downloaded_mib"; |
| 82 | constexpr char |
| 83 | kMetricsUpdateEngineSuccessfulUpdateDownloadOverheadPercentage[] = |
| 84 | "ota_update_engine_successful_update_download_overhead_percentage"; |
Tianjie Xu | d4c5deb | 2017-10-24 11:17:03 -0700 | [diff] [blame] | 85 | |
| 86 | std::unique_ptr<MetricsReporterInterface> CreateMetricsReporter() { |
| 87 | return std::make_unique<MetricsReporterAndroid>(); |
| 88 | } |
| 89 | |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 90 | } // namespace metrics |
| 91 | |
| 92 | void MetricsReporterAndroid::ReportUpdateAttemptMetrics( |
| 93 | SystemState* /* system_state */, |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 94 | int attempt_number, |
| 95 | PayloadType payload_type, |
| 96 | base::TimeDelta duration, |
| 97 | base::TimeDelta duration_uptime, |
| 98 | int64_t payload_size, |
| 99 | metrics::AttemptResult attempt_result, |
Tianjie Xu | 1f93d09 | 2017-10-09 12:13:29 -0700 | [diff] [blame] | 100 | ErrorCode error_code) { |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 101 | LogHistogram(metrics::kMetricsUpdateEngineAttemptNumber, attempt_number); |
| 102 | LogHistogram(metrics::kMetricsUpdateEngineAttemptPayloadType, |
| 103 | static_cast<int>(payload_type)); |
| 104 | LogHistogram(metrics::kMetricsUpdateEngineAttemptDurationInMinutes, |
| 105 | duration.InMinutes()); |
| 106 | LogHistogram(metrics::kMetricsUpdateEngineAttemptDurationUptimeInMinutes, |
| 107 | duration_uptime.InMinutes()); |
| 108 | |
| 109 | int64_t payload_size_mib = payload_size / kNumBytesInOneMiB; |
| 110 | LogHistogram(metrics::kMetricsUpdateEngineAttemptPayloadSizeMiB, |
| 111 | payload_size_mib); |
| 112 | |
| 113 | LogHistogram(metrics::kMetricsUpdateEngineAttemptResult, |
| 114 | static_cast<int>(attempt_result)); |
| 115 | LogHistogram(metrics::kMetricsUpdateEngineAttemptErrorCode, |
| 116 | static_cast<int>(error_code)); |
Tianjie Xu | a215b59 | 2019-08-02 14:53:38 -0700 | [diff] [blame^] | 117 | |
| 118 | android::util::stats_write( |
| 119 | android::util::UPDATE_ENGINE_UPDATE_ATTEMPT_REPORTED, |
| 120 | attempt_number, |
| 121 | GetStatsdEnumValue(static_cast<int32_t>(payload_type)), |
| 122 | duration.InMinutes(), |
| 123 | duration_uptime.InMinutes(), |
| 124 | payload_size_mib, |
| 125 | GetStatsdEnumValue(static_cast<int32_t>(attempt_result)), |
| 126 | GetStatsdEnumValue(static_cast<int32_t>(error_code)), |
| 127 | android::base::GetProperty("ro.build.fingerprint", "").c_str()); |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 130 | void MetricsReporterAndroid::ReportUpdateAttemptDownloadMetrics( |
| 131 | int64_t payload_bytes_downloaded, |
| 132 | int64_t /* payload_download_speed_bps */, |
| 133 | DownloadSource /* download_source */, |
| 134 | metrics::DownloadErrorCode /* payload_download_error_code */, |
| 135 | metrics::ConnectionType /* connection_type */) { |
| 136 | LogHistogram(metrics::kMetricsUpdateEngineAttemptCurrentBytesDownloadedMiB, |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 137 | payload_bytes_downloaded / kNumBytesInOneMiB); |
Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 140 | void MetricsReporterAndroid::ReportSuccessfulUpdateMetrics( |
| 141 | int attempt_count, |
| 142 | int /* updates_abandoned_count */, |
| 143 | PayloadType payload_type, |
| 144 | int64_t payload_size, |
Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 145 | int64_t num_bytes_downloaded[kNumDownloadSources], |
| 146 | int download_overhead_percentage, |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 147 | base::TimeDelta total_duration, |
Sen Jiang | 8712e96 | 2018-05-08 12:12:28 -0700 | [diff] [blame] | 148 | base::TimeDelta /* total_duration_uptime */, |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 149 | int reboot_count, |
| 150 | int /* url_switch_count */) { |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 151 | LogHistogram(metrics::kMetricsUpdateEngineSuccessfulUpdateAttemptCount, |
| 152 | attempt_count); |
| 153 | LogHistogram(metrics::kMetricsUpdateEngineSuccessfulUpdatePayloadType, |
| 154 | static_cast<int>(payload_type)); |
| 155 | |
| 156 | int64_t payload_size_mib = payload_size / kNumBytesInOneMiB; |
| 157 | LogHistogram(metrics::kMetricsUpdateEngineSuccessfulUpdatePayloadSizeMiB, |
| 158 | payload_size_mib); |
| 159 | |
Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 160 | int64_t total_bytes_downloaded = 0; |
| 161 | for (size_t i = 0; i < kNumDownloadSources; i++) { |
| 162 | total_bytes_downloaded += num_bytes_downloaded[i] / kNumBytesInOneMiB; |
| 163 | } |
| 164 | LogHistogram( |
| 165 | metrics::kMetricsUpdateEngineSuccessfulUpdateTotalBytesDownloadedMiB, |
| 166 | total_bytes_downloaded); |
| 167 | LogHistogram( |
| 168 | metrics::kMetricsUpdateEngineSuccessfulUpdateDownloadOverheadPercentage, |
| 169 | download_overhead_percentage); |
| 170 | |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 171 | LogHistogram( |
| 172 | metrics::kMetricsUpdateEngineSuccessfulUpdateTotalDurationInMinutes, |
| 173 | total_duration.InMinutes()); |
| 174 | LogHistogram(metrics::kMetricsUpdateEngineSuccessfulUpdateRebootCount, |
| 175 | reboot_count); |
Tianjie Xu | a215b59 | 2019-08-02 14:53:38 -0700 | [diff] [blame^] | 176 | |
| 177 | android::util::stats_write( |
| 178 | android::util::UPDATE_ENGINE_SUCCESSFUL_UPDATE_REPORTED, |
| 179 | attempt_count, |
| 180 | GetStatsdEnumValue(static_cast<int32_t>(payload_type)), |
| 181 | payload_size_mib, |
| 182 | total_bytes_downloaded, |
| 183 | download_overhead_percentage, |
| 184 | total_duration.InMinutes(), |
| 185 | reboot_count); |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void MetricsReporterAndroid::ReportAbnormallyTerminatedUpdateAttemptMetrics() { |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 189 | int attempt_result = |
| 190 | static_cast<int>(metrics::AttemptResult::kAbnormalTermination); |
| 191 | LogHistogram(metrics::kMetricsUpdateEngineAttemptResult, attempt_result); |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | }; // namespace chromeos_update_engine |