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 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/aosp/metrics_reporter_android.h" |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 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> |
Alessio Balsini | e2ad4d3 | 2020-05-26 22:18:09 +0100 | [diff] [blame] | 25 | #include <base/strings/string_util.h> |
| 26 | #include <fs_mgr.h> |
| 27 | #include <libdm/dm.h> |
| 28 | #include <liblp/builder.h> |
| 29 | #include <liblp/liblp.h> |
Tianjie Xu | a215b59 | 2019-08-02 14:53:38 -0700 | [diff] [blame] | 30 | #include <statslog.h> |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 31 | |
| 32 | #include "update_engine/common/constants.h" |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 33 | |
Alessio Balsini | e2ad4d3 | 2020-05-26 22:18:09 +0100 | [diff] [blame] | 34 | using android::fs_mgr::GetPartitionGroupName; |
| 35 | using android::fs_mgr::LpMetadata; |
| 36 | using android::fs_mgr::MetadataBuilder; |
| 37 | using android::fs_mgr::ReadMetadata; |
| 38 | using android::fs_mgr::SlotNumberForSlotSuffix; |
| 39 | using base::EndsWith; |
| 40 | |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 41 | namespace { |
Tianjie Xu | a215b59 | 2019-08-02 14:53:38 -0700 | [diff] [blame] | 42 | // A number offset adds on top of the enum value. e.g. ErrorCode::SUCCESS will |
| 43 | // be reported as 10000, and AttemptResult::UPDATE_CANCELED will be reported as |
Tianjie Xu | 6230025 | 2020-01-15 20:57:45 -0800 | [diff] [blame] | 44 | // 10011. This keeps the ordering of update engine's enum definition when statsd |
Tianjie Xu | a215b59 | 2019-08-02 14:53:38 -0700 | [diff] [blame] | 45 | // atoms reserve the value 0 for unknown state. |
| 46 | constexpr auto kMetricsReporterEnumOffset = 10000; |
| 47 | |
| 48 | int32_t GetStatsdEnumValue(int32_t value) { |
| 49 | return kMetricsReporterEnumOffset + value; |
| 50 | } |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 51 | } // namespace |
| 52 | |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 53 | namespace chromeos_update_engine { |
| 54 | |
| 55 | namespace metrics { |
| 56 | |
Yifan Hong | c514f66 | 2021-02-04 11:18:43 -0800 | [diff] [blame^] | 57 | std::unique_ptr<MetricsReporterInterface> CreateMetricsReporter( |
| 58 | DynamicPartitionControlInterface* dynamic_partition_control) { |
| 59 | return std::make_unique<MetricsReporterAndroid>(dynamic_partition_control); |
Tianjie Xu | d4c5deb | 2017-10-24 11:17:03 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 62 | } // namespace metrics |
| 63 | |
| 64 | void MetricsReporterAndroid::ReportUpdateAttemptMetrics( |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 65 | int attempt_number, |
| 66 | PayloadType payload_type, |
| 67 | base::TimeDelta duration, |
| 68 | base::TimeDelta duration_uptime, |
| 69 | int64_t payload_size, |
| 70 | metrics::AttemptResult attempt_result, |
Tianjie Xu | 1f93d09 | 2017-10-09 12:13:29 -0700 | [diff] [blame] | 71 | ErrorCode error_code) { |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 72 | int64_t payload_size_mib = payload_size / kNumBytesInOneMiB; |
Alessio Balsini | e2ad4d3 | 2020-05-26 22:18:09 +0100 | [diff] [blame] | 73 | |
| 74 | int64_t super_partition_size_bytes = 0; |
| 75 | int64_t super_free_space = 0; |
| 76 | int64_t slot_size_bytes = 0; |
| 77 | |
| 78 | if (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) { |
| 79 | uint32_t slot = SlotNumberForSlotSuffix(fs_mgr_get_slot_suffix()); |
| 80 | auto super_device = fs_mgr_get_super_partition_name(); |
| 81 | std::unique_ptr<LpMetadata> metadata = ReadMetadata(super_device, slot); |
| 82 | if (metadata) { |
| 83 | super_partition_size_bytes = GetTotalSuperPartitionSize(*metadata); |
| 84 | |
| 85 | for (const auto& group : metadata->groups) { |
| 86 | if (EndsWith(GetPartitionGroupName(group), |
| 87 | fs_mgr_get_slot_suffix(), |
| 88 | base::CompareCase::SENSITIVE)) { |
| 89 | slot_size_bytes += group.maximum_size; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | auto metadata_builder = MetadataBuilder::New(*metadata); |
| 94 | if (metadata_builder) { |
| 95 | auto free_regions = metadata_builder->GetFreeRegions(); |
| 96 | for (const auto& interval : free_regions) { |
| 97 | super_free_space += interval.length(); |
| 98 | } |
| 99 | super_free_space *= android::dm::kSectorSize; |
| 100 | } else { |
| 101 | LOG(ERROR) << "Cannot create metadata builder."; |
| 102 | } |
| 103 | } else { |
| 104 | LOG(ERROR) << "Could not read dynamic partition metadata for device: " |
| 105 | << super_device; |
| 106 | } |
| 107 | } |
| 108 | |
Tianjie Xu | a215b59 | 2019-08-02 14:53:38 -0700 | [diff] [blame] | 109 | android::util::stats_write( |
| 110 | android::util::UPDATE_ENGINE_UPDATE_ATTEMPT_REPORTED, |
| 111 | attempt_number, |
| 112 | GetStatsdEnumValue(static_cast<int32_t>(payload_type)), |
| 113 | duration.InMinutes(), |
| 114 | duration_uptime.InMinutes(), |
| 115 | payload_size_mib, |
| 116 | GetStatsdEnumValue(static_cast<int32_t>(attempt_result)), |
| 117 | GetStatsdEnumValue(static_cast<int32_t>(error_code)), |
Alessio Balsini | e2ad4d3 | 2020-05-26 22:18:09 +0100 | [diff] [blame] | 118 | android::base::GetProperty("ro.build.fingerprint", "").c_str(), |
| 119 | super_partition_size_bytes, |
| 120 | slot_size_bytes, |
| 121 | super_free_space); |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 124 | void MetricsReporterAndroid::ReportUpdateAttemptDownloadMetrics( |
| 125 | int64_t payload_bytes_downloaded, |
| 126 | int64_t /* payload_download_speed_bps */, |
| 127 | DownloadSource /* download_source */, |
| 128 | metrics::DownloadErrorCode /* payload_download_error_code */, |
| 129 | metrics::ConnectionType /* connection_type */) { |
Tianjie Xu | 6230025 | 2020-01-15 20:57:45 -0800 | [diff] [blame] | 130 | // TODO(xunchang) add statsd reporting |
| 131 | LOG(INFO) << "Current update attempt downloads " |
| 132 | << payload_bytes_downloaded / kNumBytesInOneMiB << " bytes data"; |
Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 135 | void MetricsReporterAndroid::ReportSuccessfulUpdateMetrics( |
| 136 | int attempt_count, |
| 137 | int /* updates_abandoned_count */, |
| 138 | PayloadType payload_type, |
| 139 | int64_t payload_size, |
Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 140 | int64_t num_bytes_downloaded[kNumDownloadSources], |
| 141 | int download_overhead_percentage, |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 142 | base::TimeDelta total_duration, |
Sen Jiang | 8712e96 | 2018-05-08 12:12:28 -0700 | [diff] [blame] | 143 | base::TimeDelta /* total_duration_uptime */, |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 144 | int reboot_count, |
| 145 | int /* url_switch_count */) { |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 146 | int64_t payload_size_mib = payload_size / kNumBytesInOneMiB; |
Tianjie Xu | 82d9910 | 2017-11-02 22:26:19 -0700 | [diff] [blame] | 147 | int64_t total_bytes_downloaded = 0; |
| 148 | for (size_t i = 0; i < kNumDownloadSources; i++) { |
| 149 | total_bytes_downloaded += num_bytes_downloaded[i] / kNumBytesInOneMiB; |
| 150 | } |
Tianjie Xu | a215b59 | 2019-08-02 14:53:38 -0700 | [diff] [blame] | 151 | |
| 152 | android::util::stats_write( |
| 153 | android::util::UPDATE_ENGINE_SUCCESSFUL_UPDATE_REPORTED, |
Jeff Sharkey | 635792b | 2019-12-04 09:54:47 -0700 | [diff] [blame] | 154 | static_cast<int32_t>(attempt_count), |
Tianjie Xu | a215b59 | 2019-08-02 14:53:38 -0700 | [diff] [blame] | 155 | GetStatsdEnumValue(static_cast<int32_t>(payload_type)), |
Jeff Sharkey | 635792b | 2019-12-04 09:54:47 -0700 | [diff] [blame] | 156 | static_cast<int32_t>(payload_size_mib), |
| 157 | static_cast<int32_t>(total_bytes_downloaded), |
| 158 | static_cast<int32_t>(download_overhead_percentage), |
| 159 | static_cast<int32_t>(total_duration.InMinutes()), |
| 160 | static_cast<int32_t>(reboot_count)); |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void MetricsReporterAndroid::ReportAbnormallyTerminatedUpdateAttemptMetrics() { |
Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 164 | int attempt_result = |
| 165 | static_cast<int>(metrics::AttemptResult::kAbnormalTermination); |
Tianjie Xu | 6230025 | 2020-01-15 20:57:45 -0800 | [diff] [blame] | 166 | // TODO(xunchang) add statsd reporting |
| 167 | LOG(INFO) << "Abnormally terminated update attempt result " << attempt_result; |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | }; // namespace chromeos_update_engine |