| 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 | 75cc9f2 | 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 | 75cc9f2 | 2019-08-02 14:53:38 -0700 | [diff] [blame] | 24 | #include <android-base/properties.h> | 
| Tianjie Xu | 75cc9f2 | 2019-08-02 14:53:38 -0700 | [diff] [blame] | 25 | #include <statslog.h> | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | #include "update_engine/common/constants.h" | 
| Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 28 |  | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 29 | namespace { | 
| Tianjie Xu | 75cc9f2 | 2019-08-02 14:53:38 -0700 | [diff] [blame] | 30 | // A number offset adds on top of the enum value. e.g. ErrorCode::SUCCESS will | 
|  | 31 | // be reported as 10000, and AttemptResult::UPDATE_CANCELED will be reported as | 
| Tianjie Xu | 6230025 | 2020-01-15 20:57:45 -0800 | [diff] [blame] | 32 | // 10011. This keeps the ordering of update engine's enum definition when statsd | 
| Tianjie Xu | 75cc9f2 | 2019-08-02 14:53:38 -0700 | [diff] [blame] | 33 | // atoms reserve the value 0 for unknown state. | 
|  | 34 | constexpr auto kMetricsReporterEnumOffset = 10000; | 
|  | 35 |  | 
|  | 36 | int32_t GetStatsdEnumValue(int32_t value) { | 
|  | 37 | return kMetricsReporterEnumOffset + value; | 
|  | 38 | } | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 39 | }  // namespace | 
|  | 40 |  | 
| Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 41 | namespace chromeos_update_engine { | 
|  | 42 |  | 
|  | 43 | namespace metrics { | 
|  | 44 |  | 
| Tianjie Xu | d4c5deb | 2017-10-24 11:17:03 -0700 | [diff] [blame] | 45 | std::unique_ptr<MetricsReporterInterface> CreateMetricsReporter() { | 
|  | 46 | return std::make_unique<MetricsReporterAndroid>(); | 
|  | 47 | } | 
|  | 48 |  | 
| Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 49 | }  // namespace metrics | 
|  | 50 |  | 
|  | 51 | void MetricsReporterAndroid::ReportUpdateAttemptMetrics( | 
|  | 52 | SystemState* /* system_state */, | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 53 | int attempt_number, | 
|  | 54 | PayloadType payload_type, | 
|  | 55 | base::TimeDelta duration, | 
|  | 56 | base::TimeDelta duration_uptime, | 
|  | 57 | int64_t payload_size, | 
|  | 58 | metrics::AttemptResult attempt_result, | 
| Tianjie Xu | 1f93d09 | 2017-10-09 12:13:29 -0700 | [diff] [blame] | 59 | ErrorCode error_code) { | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 60 | int64_t payload_size_mib = payload_size / kNumBytesInOneMiB; | 
| Tianjie Xu | 75cc9f2 | 2019-08-02 14:53:38 -0700 | [diff] [blame] | 61 | android::util::stats_write( | 
|  | 62 | android::util::UPDATE_ENGINE_UPDATE_ATTEMPT_REPORTED, | 
|  | 63 | attempt_number, | 
|  | 64 | GetStatsdEnumValue(static_cast<int32_t>(payload_type)), | 
|  | 65 | duration.InMinutes(), | 
|  | 66 | duration_uptime.InMinutes(), | 
|  | 67 | payload_size_mib, | 
|  | 68 | GetStatsdEnumValue(static_cast<int32_t>(attempt_result)), | 
|  | 69 | GetStatsdEnumValue(static_cast<int32_t>(error_code)), | 
|  | 70 | android::base::GetProperty("ro.build.fingerprint", "").c_str()); | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
| Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 73 | void MetricsReporterAndroid::ReportUpdateAttemptDownloadMetrics( | 
|  | 74 | int64_t payload_bytes_downloaded, | 
|  | 75 | int64_t /* payload_download_speed_bps */, | 
|  | 76 | DownloadSource /* download_source */, | 
|  | 77 | metrics::DownloadErrorCode /* payload_download_error_code */, | 
|  | 78 | metrics::ConnectionType /* connection_type */) { | 
| Tianjie Xu | 6230025 | 2020-01-15 20:57:45 -0800 | [diff] [blame] | 79 | // TODO(xunchang) add statsd reporting | 
|  | 80 | LOG(INFO) << "Current update attempt downloads " | 
|  | 81 | << payload_bytes_downloaded / kNumBytesInOneMiB << " bytes data"; | 
| Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 84 | void MetricsReporterAndroid::ReportSuccessfulUpdateMetrics( | 
|  | 85 | int attempt_count, | 
|  | 86 | int /* updates_abandoned_count */, | 
|  | 87 | PayloadType payload_type, | 
|  | 88 | int64_t payload_size, | 
| Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 89 | int64_t num_bytes_downloaded[kNumDownloadSources], | 
|  | 90 | int download_overhead_percentage, | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 91 | base::TimeDelta total_duration, | 
| Sen Jiang | 8712e96 | 2018-05-08 12:12:28 -0700 | [diff] [blame] | 92 | base::TimeDelta /* total_duration_uptime */, | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 93 | int reboot_count, | 
|  | 94 | int /* url_switch_count */) { | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 95 | int64_t payload_size_mib = payload_size / kNumBytesInOneMiB; | 
| Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 96 | int64_t total_bytes_downloaded = 0; | 
|  | 97 | for (size_t i = 0; i < kNumDownloadSources; i++) { | 
|  | 98 | total_bytes_downloaded += num_bytes_downloaded[i] / kNumBytesInOneMiB; | 
|  | 99 | } | 
| Tianjie Xu | 75cc9f2 | 2019-08-02 14:53:38 -0700 | [diff] [blame] | 100 |  | 
|  | 101 | android::util::stats_write( | 
|  | 102 | android::util::UPDATE_ENGINE_SUCCESSFUL_UPDATE_REPORTED, | 
|  | 103 | attempt_count, | 
|  | 104 | GetStatsdEnumValue(static_cast<int32_t>(payload_type)), | 
|  | 105 | payload_size_mib, | 
|  | 106 | total_bytes_downloaded, | 
|  | 107 | download_overhead_percentage, | 
|  | 108 | total_duration.InMinutes(), | 
|  | 109 | reboot_count); | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
|  | 112 | void MetricsReporterAndroid::ReportAbnormallyTerminatedUpdateAttemptMetrics() { | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 113 | int attempt_result = | 
|  | 114 | static_cast<int>(metrics::AttemptResult::kAbnormalTermination); | 
| Tianjie Xu | 6230025 | 2020-01-15 20:57:45 -0800 | [diff] [blame] | 115 | // TODO(xunchang) add statsd reporting | 
|  | 116 | LOG(INFO) << "Abnormally terminated update attempt result " << attempt_result; | 
| Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
|  | 119 | };  // namespace chromeos_update_engine |