blob: 24740c8a0fd9188eabd318b9b78f245a50436d32 [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
17#include "update_engine/metrics_reporter_android.h"
18
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>
Tianjie Xua215b592019-08-02 14:53:38 -070025#include <statslog.h>
Tianjie Xu52c678c2017-10-18 15:52:27 -070026
27#include "update_engine/common/constants.h"
Tianjie Xu1b661142017-09-28 14:03:42 -070028
Tianjie Xu52c678c2017-10-18 15:52:27 -070029namespace {
Tianjie Xua215b592019-08-02 14:53:38 -070030// 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 Xu62300252020-01-15 20:57:45 -080032// 10011. This keeps the ordering of update engine's enum definition when statsd
Tianjie Xua215b592019-08-02 14:53:38 -070033// atoms reserve the value 0 for unknown state.
34constexpr auto kMetricsReporterEnumOffset = 10000;
35
36int32_t GetStatsdEnumValue(int32_t value) {
37 return kMetricsReporterEnumOffset + value;
38}
Tianjie Xu52c678c2017-10-18 15:52:27 -070039} // namespace
40
Tianjie Xu1b661142017-09-28 14:03:42 -070041namespace chromeos_update_engine {
42
43namespace metrics {
44
Tianjie Xud4c5deb2017-10-24 11:17:03 -070045std::unique_ptr<MetricsReporterInterface> CreateMetricsReporter() {
46 return std::make_unique<MetricsReporterAndroid>();
47}
48
Tianjie Xu1b661142017-09-28 14:03:42 -070049} // namespace metrics
50
51void MetricsReporterAndroid::ReportUpdateAttemptMetrics(
52 SystemState* /* system_state */,
Tianjie Xu52c678c2017-10-18 15:52:27 -070053 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 Xu1f93d092017-10-09 12:13:29 -070059 ErrorCode error_code) {
Tianjie Xu52c678c2017-10-18 15:52:27 -070060 int64_t payload_size_mib = payload_size / kNumBytesInOneMiB;
Tianjie Xua215b592019-08-02 14:53:38 -070061 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 Xu52c678c2017-10-18 15:52:27 -070071}
72
Tianjie Xu82d99102017-11-02 22:26:19 -070073void 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 Xu62300252020-01-15 20:57:45 -080079 // TODO(xunchang) add statsd reporting
80 LOG(INFO) << "Current update attempt downloads "
81 << payload_bytes_downloaded / kNumBytesInOneMiB << " bytes data";
Tianjie Xu82d99102017-11-02 22:26:19 -070082}
83
Tianjie Xu52c678c2017-10-18 15:52:27 -070084void MetricsReporterAndroid::ReportSuccessfulUpdateMetrics(
85 int attempt_count,
86 int /* updates_abandoned_count */,
87 PayloadType payload_type,
88 int64_t payload_size,
Tianjie Xu82d99102017-11-02 22:26:19 -070089 int64_t num_bytes_downloaded[kNumDownloadSources],
90 int download_overhead_percentage,
Tianjie Xu52c678c2017-10-18 15:52:27 -070091 base::TimeDelta total_duration,
Sen Jiang8712e962018-05-08 12:12:28 -070092 base::TimeDelta /* total_duration_uptime */,
Tianjie Xu52c678c2017-10-18 15:52:27 -070093 int reboot_count,
94 int /* url_switch_count */) {
Tianjie Xu52c678c2017-10-18 15:52:27 -070095 int64_t payload_size_mib = payload_size / kNumBytesInOneMiB;
Tianjie Xu82d99102017-11-02 22:26:19 -070096 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 Xua215b592019-08-02 14:53:38 -0700100
101 android::util::stats_write(
102 android::util::UPDATE_ENGINE_SUCCESSFUL_UPDATE_REPORTED,
Jeff Sharkey635792b2019-12-04 09:54:47 -0700103 static_cast<int32_t>(attempt_count),
Tianjie Xua215b592019-08-02 14:53:38 -0700104 GetStatsdEnumValue(static_cast<int32_t>(payload_type)),
Jeff Sharkey635792b2019-12-04 09:54:47 -0700105 static_cast<int32_t>(payload_size_mib),
106 static_cast<int32_t>(total_bytes_downloaded),
107 static_cast<int32_t>(download_overhead_percentage),
108 static_cast<int32_t>(total_duration.InMinutes()),
109 static_cast<int32_t>(reboot_count));
Tianjie Xu52c678c2017-10-18 15:52:27 -0700110}
111
112void MetricsReporterAndroid::ReportAbnormallyTerminatedUpdateAttemptMetrics() {
Tianjie Xu52c678c2017-10-18 15:52:27 -0700113 int attempt_result =
114 static_cast<int>(metrics::AttemptResult::kAbnormalTermination);
Tianjie Xu62300252020-01-15 20:57:45 -0800115 // TODO(xunchang) add statsd reporting
116 LOG(INFO) << "Abnormally terminated update attempt result " << attempt_result;
Tianjie Xu1b661142017-09-28 14:03:42 -0700117}
118
119}; // namespace chromeos_update_engine