blob: 18424d5fdfc585dcab08aef5e90c3544394fd23a [file] [log] [blame]
Tianjie Xu1b661142017-09-28 14:03:42 -07001//
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 Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/aosp/metrics_reporter_android.h"
Tianjie Xu1b661142017-09-28 14:03:42 -070018
Tianjie Xua215b592019-08-02 14:53:38 -070019#include <stdint.h>
20
Tianjie Xud4c5deb2017-10-24 11:17:03 -070021#include <memory>
Tianjie Xu52c678c2017-10-18 15:52:27 -070022#include <string>
23
Tianjie Xua215b592019-08-02 14:53:38 -070024#include <android-base/properties.h>
Alessio Balsinie2ad4d32020-05-26 22:18:09 +010025#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 Xua215b592019-08-02 14:53:38 -070030#include <statslog.h>
Tianjie Xu52c678c2017-10-18 15:52:27 -070031
32#include "update_engine/common/constants.h"
Tianjie Xu1b661142017-09-28 14:03:42 -070033
Alessio Balsinie2ad4d32020-05-26 22:18:09 +010034using android::fs_mgr::GetPartitionGroupName;
35using android::fs_mgr::LpMetadata;
36using android::fs_mgr::MetadataBuilder;
37using android::fs_mgr::ReadMetadata;
38using android::fs_mgr::SlotNumberForSlotSuffix;
39using base::EndsWith;
40
Tianjie Xu52c678c2017-10-18 15:52:27 -070041namespace {
Tianjie Xua215b592019-08-02 14:53:38 -070042// 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 Xu62300252020-01-15 20:57:45 -080044// 10011. This keeps the ordering of update engine's enum definition when statsd
Tianjie Xua215b592019-08-02 14:53:38 -070045// atoms reserve the value 0 for unknown state.
46constexpr auto kMetricsReporterEnumOffset = 10000;
47
48int32_t GetStatsdEnumValue(int32_t value) {
49 return kMetricsReporterEnumOffset + value;
50}
Tianjie Xu52c678c2017-10-18 15:52:27 -070051} // namespace
52
Tianjie Xu1b661142017-09-28 14:03:42 -070053namespace chromeos_update_engine {
54
55namespace metrics {
56
Yifan Hongc514f662021-02-04 11:18:43 -080057std::unique_ptr<MetricsReporterInterface> CreateMetricsReporter(
58 DynamicPartitionControlInterface* dynamic_partition_control) {
59 return std::make_unique<MetricsReporterAndroid>(dynamic_partition_control);
Tianjie Xud4c5deb2017-10-24 11:17:03 -070060}
61
Tianjie Xu1b661142017-09-28 14:03:42 -070062} // namespace metrics
63
64void MetricsReporterAndroid::ReportUpdateAttemptMetrics(
Tianjie Xu52c678c2017-10-18 15:52:27 -070065 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 Xu1f93d092017-10-09 12:13:29 -070071 ErrorCode error_code) {
Tianjie Xu52c678c2017-10-18 15:52:27 -070072 int64_t payload_size_mib = payload_size / kNumBytesInOneMiB;
Alessio Balsinie2ad4d32020-05-26 22:18:09 +010073
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 Xua215b592019-08-02 14:53:38 -0700109 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 Balsinie2ad4d32020-05-26 22:18:09 +0100118 android::base::GetProperty("ro.build.fingerprint", "").c_str(),
119 super_partition_size_bytes,
120 slot_size_bytes,
121 super_free_space);
Tianjie Xu52c678c2017-10-18 15:52:27 -0700122}
123
Tianjie Xu82d99102017-11-02 22:26:19 -0700124void 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 Xu62300252020-01-15 20:57:45 -0800130 // TODO(xunchang) add statsd reporting
131 LOG(INFO) << "Current update attempt downloads "
132 << payload_bytes_downloaded / kNumBytesInOneMiB << " bytes data";
Tianjie Xu82d99102017-11-02 22:26:19 -0700133}
134
Tianjie Xu52c678c2017-10-18 15:52:27 -0700135void MetricsReporterAndroid::ReportSuccessfulUpdateMetrics(
136 int attempt_count,
137 int /* updates_abandoned_count */,
138 PayloadType payload_type,
139 int64_t payload_size,
Tianjie Xu82d99102017-11-02 22:26:19 -0700140 int64_t num_bytes_downloaded[kNumDownloadSources],
141 int download_overhead_percentage,
Tianjie Xu52c678c2017-10-18 15:52:27 -0700142 base::TimeDelta total_duration,
Sen Jiang8712e962018-05-08 12:12:28 -0700143 base::TimeDelta /* total_duration_uptime */,
Tianjie Xu52c678c2017-10-18 15:52:27 -0700144 int reboot_count,
145 int /* url_switch_count */) {
Tianjie Xu52c678c2017-10-18 15:52:27 -0700146 int64_t payload_size_mib = payload_size / kNumBytesInOneMiB;
Tianjie Xu82d99102017-11-02 22:26:19 -0700147 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 Xua215b592019-08-02 14:53:38 -0700151
152 android::util::stats_write(
153 android::util::UPDATE_ENGINE_SUCCESSFUL_UPDATE_REPORTED,
Jeff Sharkey635792b2019-12-04 09:54:47 -0700154 static_cast<int32_t>(attempt_count),
Tianjie Xua215b592019-08-02 14:53:38 -0700155 GetStatsdEnumValue(static_cast<int32_t>(payload_type)),
Jeff Sharkey635792b2019-12-04 09:54:47 -0700156 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 Xu52c678c2017-10-18 15:52:27 -0700161}
162
163void MetricsReporterAndroid::ReportAbnormallyTerminatedUpdateAttemptMetrics() {
Tianjie Xu52c678c2017-10-18 15:52:27 -0700164 int attempt_result =
165 static_cast<int>(metrics::AttemptResult::kAbnormalTermination);
Tianjie Xu62300252020-01-15 20:57:45 -0800166 // TODO(xunchang) add statsd reporting
167 LOG(INFO) << "Abnormally terminated update attempt result " << attempt_result;
Tianjie Xu1b661142017-09-28 14:03:42 -0700168}
169
170}; // namespace chromeos_update_engine