blob: 34da5a1ad11a7e7d26c8e5ef305bd052551da6e3 [file] [log] [blame]
Alex Deymo38429cf2015-11-11 18:27:22 -08001//
2// Copyright (C) 2015 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_utils.h"
18
Alex Deymoa2591792015-11-17 00:39:40 -030019#include <string>
20
21#include <base/time/time.h>
22
23#include "update_engine/common/clock_interface.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070024#include "update_engine/common/constants.h"
25#include "update_engine/common/utils.h"
Alex Deymoa2591792015-11-17 00:39:40 -030026
27using base::Time;
28using base::TimeDelta;
29
Alex Deymo38429cf2015-11-11 18:27:22 -080030namespace chromeos_update_engine {
31namespace metrics_utils {
32
33metrics::AttemptResult GetAttemptResult(ErrorCode code) {
34 ErrorCode base_code = static_cast<ErrorCode>(
35 static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
36
37 switch (base_code) {
38 case ErrorCode::kSuccess:
39 return metrics::AttemptResult::kUpdateSucceeded;
40
Sen Jiangfe522822017-10-31 15:14:11 -070041 case ErrorCode::kUpdatedButNotActive:
42 return metrics::AttemptResult::kUpdateSucceededNotActive;
43
Alex Deymo38429cf2015-11-11 18:27:22 -080044 case ErrorCode::kDownloadTransferError:
Amin Hassanid3d84212019-08-17 00:27:44 -070045 case ErrorCode::kInternalLibCurlError:
46 case ErrorCode::kUnresolvedHostError:
47 case ErrorCode::kUnresolvedHostRecovered:
Alex Deymo38429cf2015-11-11 18:27:22 -080048 return metrics::AttemptResult::kPayloadDownloadError;
49
50 case ErrorCode::kDownloadInvalidMetadataSize:
51 case ErrorCode::kDownloadInvalidMetadataMagicString:
52 case ErrorCode::kDownloadMetadataSignatureError:
53 case ErrorCode::kDownloadMetadataSignatureVerificationError:
54 case ErrorCode::kPayloadMismatchedType:
55 case ErrorCode::kUnsupportedMajorPayloadVersion:
56 case ErrorCode::kUnsupportedMinorPayloadVersion:
57 case ErrorCode::kDownloadNewPartitionInfoError:
58 case ErrorCode::kDownloadSignatureMissingInManifest:
59 case ErrorCode::kDownloadManifestParseError:
60 case ErrorCode::kDownloadOperationHashMissingError:
61 return metrics::AttemptResult::kMetadataMalformed;
62
63 case ErrorCode::kDownloadOperationHashMismatch:
64 case ErrorCode::kDownloadOperationHashVerificationError:
65 return metrics::AttemptResult::kOperationMalformed;
66
67 case ErrorCode::kDownloadOperationExecutionError:
68 case ErrorCode::kInstallDeviceOpenError:
69 case ErrorCode::kKernelDeviceOpenError:
70 case ErrorCode::kDownloadWriteError:
71 case ErrorCode::kFilesystemCopierError:
72 case ErrorCode::kFilesystemVerifierError:
Sen Jiang57f91802017-11-14 17:42:13 -080073 case ErrorCode::kVerityCalculationError:
Yifan Hong46e37d42019-12-13 12:01:42 -080074 case ErrorCode::kNotEnoughSpace:
Yifan Hong77279902019-12-17 16:38:21 -080075 case ErrorCode::kDeviceCorrupted:
Alex Deymo38429cf2015-11-11 18:27:22 -080076 return metrics::AttemptResult::kOperationExecutionError;
77
78 case ErrorCode::kDownloadMetadataSignatureMismatch:
79 return metrics::AttemptResult::kMetadataVerificationFailed;
80
81 case ErrorCode::kPayloadSizeMismatchError:
82 case ErrorCode::kPayloadHashMismatchError:
83 case ErrorCode::kDownloadPayloadVerificationError:
84 case ErrorCode::kSignedDeltaPayloadExpectedError:
85 case ErrorCode::kDownloadPayloadPubKeyVerificationError:
Sen Jiang8e768e92017-06-28 17:13:19 -070086 case ErrorCode::kPayloadTimestampError:
Alex Deymo38429cf2015-11-11 18:27:22 -080087 return metrics::AttemptResult::kPayloadVerificationFailed;
88
89 case ErrorCode::kNewRootfsVerificationError:
90 case ErrorCode::kNewKernelVerificationError:
Marton Hunyady199152d2018-05-07 19:08:48 +020091 case ErrorCode::kRollbackNotPossible:
Alex Deymo38429cf2015-11-11 18:27:22 -080092 return metrics::AttemptResult::kVerificationFailed;
93
94 case ErrorCode::kPostinstallRunnerError:
95 case ErrorCode::kPostinstallBootedFromFirmwareB:
96 case ErrorCode::kPostinstallFirmwareRONotUpdatable:
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -040097 case ErrorCode::kPostInstallMountError:
Alex Deymo38429cf2015-11-11 18:27:22 -080098 return metrics::AttemptResult::kPostInstallFailed;
99
Alex Deymo1f19dcc2016-02-03 09:22:17 -0800100 case ErrorCode::kUserCanceled:
101 return metrics::AttemptResult::kUpdateCanceled;
102
Alex Deymo38429cf2015-11-11 18:27:22 -0800103 // We should never get these errors in the update-attempt stage so
104 // return internal error if this happens.
105 case ErrorCode::kError:
106 case ErrorCode::kOmahaRequestXMLParseError:
107 case ErrorCode::kOmahaRequestError:
108 case ErrorCode::kOmahaResponseHandlerError:
109 case ErrorCode::kDownloadStateInitializationError:
110 case ErrorCode::kOmahaRequestEmptyResponseError:
111 case ErrorCode::kDownloadInvalidMetadataSignature:
112 case ErrorCode::kOmahaResponseInvalid:
113 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
Alex Deymo38429cf2015-11-11 18:27:22 -0800114 case ErrorCode::kOmahaErrorInHTTPResponse:
115 case ErrorCode::kDownloadMetadataSignatureMissingError:
116 case ErrorCode::kOmahaUpdateDeferredForBackoff:
117 case ErrorCode::kPostinstallPowerwashError:
118 case ErrorCode::kUpdateCanceledByChannelChange:
119 case ErrorCode::kOmahaRequestXMLHasEntityDecl:
Weidong Guo421ff332017-04-17 10:08:38 -0700120 case ErrorCode::kOmahaUpdateIgnoredOverCellular:
Sen Jiang89e24c12018-03-22 18:05:44 -0700121 case ErrorCode::kNoUpdate:
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700122 case ErrorCode::kFirstActiveOmahaPingSentPersistenceError:
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -0700123 case ErrorCode::kPackageExcludedFromUpdate:
Alex Deymo38429cf2015-11-11 18:27:22 -0800124 return metrics::AttemptResult::kInternalError;
125
Andrewb57c16e2020-07-22 14:32:39 -0700126 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
127 case ErrorCode::kNonCriticalUpdateInOOBE:
128 return metrics::AttemptResult::kUpdateSkipped;
129
Alex Deymo38429cf2015-11-11 18:27:22 -0800130 // Special flags. These can't happen (we mask them out above) but
131 // the compiler doesn't know that. Just break out so we can warn and
132 // return |kInternalError|.
133 case ErrorCode::kUmaReportedMax:
134 case ErrorCode::kOmahaRequestHTTPResponseBase:
135 case ErrorCode::kDevModeFlag:
136 case ErrorCode::kResumedFlag:
137 case ErrorCode::kTestImageFlag:
138 case ErrorCode::kTestOmahaUrlFlag:
139 case ErrorCode::kSpecialFlags:
140 break;
141 }
142
143 LOG(ERROR) << "Unexpected error code " << base_code;
144 return metrics::AttemptResult::kInternalError;
145}
146
147metrics::DownloadErrorCode GetDownloadErrorCode(ErrorCode code) {
148 ErrorCode base_code = static_cast<ErrorCode>(
149 static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
150
151 if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) {
152 int http_status =
153 static_cast<int>(base_code) -
154 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase);
155 if (http_status >= 200 && http_status <= 599) {
156 return static_cast<metrics::DownloadErrorCode>(
157 static_cast<int>(metrics::DownloadErrorCode::kHttpStatus200) +
158 http_status - 200);
159 } else if (http_status == 0) {
160 // The code is using HTTP Status 0 for "Unable to get http
161 // response code."
162 return metrics::DownloadErrorCode::kDownloadError;
163 }
164 LOG(WARNING) << "Unexpected HTTP status code " << http_status;
165 return metrics::DownloadErrorCode::kHttpStatusOther;
166 }
167
168 switch (base_code) {
169 // Unfortunately, ErrorCode::kDownloadTransferError is returned for a wide
170 // variety of errors (proxy errors, host not reachable, timeouts etc.).
171 //
172 // For now just map that to kDownloading. See http://crbug.com/355745
173 // for how we plan to add more detail in the future.
174 case ErrorCode::kDownloadTransferError:
175 return metrics::DownloadErrorCode::kDownloadError;
176
Amin Hassanid3d84212019-08-17 00:27:44 -0700177 case ErrorCode::kInternalLibCurlError:
178 return metrics::DownloadErrorCode::kInternalLibCurlError;
179 case ErrorCode::kUnresolvedHostError:
180 return metrics::DownloadErrorCode::kUnresolvedHostError;
181 case ErrorCode::kUnresolvedHostRecovered:
182 return metrics::DownloadErrorCode::kUnresolvedHostRecovered;
183
Alex Deymo38429cf2015-11-11 18:27:22 -0800184 // All of these error codes are not related to downloading so break
185 // out so we can warn and return InputMalformed.
186 case ErrorCode::kSuccess:
187 case ErrorCode::kError:
188 case ErrorCode::kOmahaRequestError:
189 case ErrorCode::kOmahaResponseHandlerError:
190 case ErrorCode::kFilesystemCopierError:
191 case ErrorCode::kPostinstallRunnerError:
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400192 case ErrorCode::kPostInstallMountError:
Alex Deymo38429cf2015-11-11 18:27:22 -0800193 case ErrorCode::kPayloadMismatchedType:
194 case ErrorCode::kInstallDeviceOpenError:
195 case ErrorCode::kKernelDeviceOpenError:
196 case ErrorCode::kPayloadHashMismatchError:
197 case ErrorCode::kPayloadSizeMismatchError:
198 case ErrorCode::kDownloadPayloadVerificationError:
199 case ErrorCode::kDownloadNewPartitionInfoError:
200 case ErrorCode::kDownloadWriteError:
201 case ErrorCode::kNewRootfsVerificationError:
202 case ErrorCode::kNewKernelVerificationError:
203 case ErrorCode::kSignedDeltaPayloadExpectedError:
204 case ErrorCode::kDownloadPayloadPubKeyVerificationError:
205 case ErrorCode::kPostinstallBootedFromFirmwareB:
206 case ErrorCode::kDownloadStateInitializationError:
207 case ErrorCode::kDownloadInvalidMetadataMagicString:
208 case ErrorCode::kDownloadSignatureMissingInManifest:
209 case ErrorCode::kDownloadManifestParseError:
210 case ErrorCode::kDownloadMetadataSignatureError:
211 case ErrorCode::kDownloadMetadataSignatureVerificationError:
212 case ErrorCode::kDownloadMetadataSignatureMismatch:
213 case ErrorCode::kDownloadOperationHashVerificationError:
214 case ErrorCode::kDownloadOperationExecutionError:
215 case ErrorCode::kDownloadOperationHashMismatch:
216 case ErrorCode::kOmahaRequestEmptyResponseError:
217 case ErrorCode::kOmahaRequestXMLParseError:
218 case ErrorCode::kDownloadInvalidMetadataSize:
219 case ErrorCode::kDownloadInvalidMetadataSignature:
220 case ErrorCode::kOmahaResponseInvalid:
221 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
222 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
Kevin Cernekee2494e282016-03-29 18:03:53 -0700223 case ErrorCode::kNonCriticalUpdateInOOBE:
Alex Deymo38429cf2015-11-11 18:27:22 -0800224 case ErrorCode::kOmahaErrorInHTTPResponse:
225 case ErrorCode::kDownloadOperationHashMissingError:
226 case ErrorCode::kDownloadMetadataSignatureMissingError:
227 case ErrorCode::kOmahaUpdateDeferredForBackoff:
228 case ErrorCode::kPostinstallPowerwashError:
229 case ErrorCode::kUpdateCanceledByChannelChange:
230 case ErrorCode::kPostinstallFirmwareRONotUpdatable:
231 case ErrorCode::kUnsupportedMajorPayloadVersion:
232 case ErrorCode::kUnsupportedMinorPayloadVersion:
233 case ErrorCode::kOmahaRequestXMLHasEntityDecl:
234 case ErrorCode::kFilesystemVerifierError:
Alex Deymo1f19dcc2016-02-03 09:22:17 -0800235 case ErrorCode::kUserCanceled:
Weidong Guo421ff332017-04-17 10:08:38 -0700236 case ErrorCode::kOmahaUpdateIgnoredOverCellular:
Sen Jiang8e768e92017-06-28 17:13:19 -0700237 case ErrorCode::kPayloadTimestampError:
Sen Jiangfe522822017-10-31 15:14:11 -0700238 case ErrorCode::kUpdatedButNotActive:
Sen Jiang89e24c12018-03-22 18:05:44 -0700239 case ErrorCode::kNoUpdate:
Marton Hunyady199152d2018-05-07 19:08:48 +0200240 case ErrorCode::kRollbackNotPossible:
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700241 case ErrorCode::kFirstActiveOmahaPingSentPersistenceError:
Sen Jiang57f91802017-11-14 17:42:13 -0800242 case ErrorCode::kVerityCalculationError:
Yifan Hong46e37d42019-12-13 12:01:42 -0800243 case ErrorCode::kNotEnoughSpace:
Yifan Hong77279902019-12-17 16:38:21 -0800244 case ErrorCode::kDeviceCorrupted:
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -0700245 case ErrorCode::kPackageExcludedFromUpdate:
Alex Deymo38429cf2015-11-11 18:27:22 -0800246 break;
247
248 // Special flags. These can't happen (we mask them out above) but
249 // the compiler doesn't know that. Just break out so we can warn and
250 // return |kInputMalformed|.
251 case ErrorCode::kUmaReportedMax:
252 case ErrorCode::kOmahaRequestHTTPResponseBase:
253 case ErrorCode::kDevModeFlag:
254 case ErrorCode::kResumedFlag:
255 case ErrorCode::kTestImageFlag:
256 case ErrorCode::kTestOmahaUrlFlag:
257 case ErrorCode::kSpecialFlags:
258 LOG(ERROR) << "Unexpected error code " << base_code;
259 break;
260 }
261
262 return metrics::DownloadErrorCode::kInputMalformed;
263}
264
Sen Jiang255e22b2016-05-20 16:15:29 -0700265metrics::ConnectionType GetConnectionType(ConnectionType type,
266 ConnectionTethering tethering) {
Alex Deymo38429cf2015-11-11 18:27:22 -0800267 switch (type) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700268 case ConnectionType::kUnknown:
Alex Deymo38429cf2015-11-11 18:27:22 -0800269 return metrics::ConnectionType::kUnknown;
270
Colin Howesc9e98d62018-09-18 10:35:20 -0700271 case ConnectionType::kDisconnected:
272 return metrics::ConnectionType::kDisconnected;
273
Sen Jiang255e22b2016-05-20 16:15:29 -0700274 case ConnectionType::kEthernet:
275 if (tethering == ConnectionTethering::kConfirmed)
Alex Deymo38429cf2015-11-11 18:27:22 -0800276 return metrics::ConnectionType::kTetheredEthernet;
277 else
278 return metrics::ConnectionType::kEthernet;
279
Sen Jiang255e22b2016-05-20 16:15:29 -0700280 case ConnectionType::kWifi:
281 if (tethering == ConnectionTethering::kConfirmed)
Alex Deymo38429cf2015-11-11 18:27:22 -0800282 return metrics::ConnectionType::kTetheredWifi;
283 else
284 return metrics::ConnectionType::kWifi;
285
Sen Jiang255e22b2016-05-20 16:15:29 -0700286 case ConnectionType::kCellular:
Alex Deymo38429cf2015-11-11 18:27:22 -0800287 return metrics::ConnectionType::kCellular;
288 }
289
290 LOG(ERROR) << "Unexpected network connection type: type="
291 << static_cast<int>(type)
292 << ", tethering=" << static_cast<int>(tethering);
293
294 return metrics::ConnectionType::kUnknown;
295}
296
Tianjie Xu90aaa102017-10-10 17:39:03 -0700297int64_t GetPersistedValue(const std::string& key, PrefsInterface* prefs) {
298 CHECK(prefs);
299 if (!prefs->Exists(key))
300 return 0;
301
302 int64_t stored_value;
303 if (!prefs->GetInt64(key, &stored_value))
304 return 0;
305
306 if (stored_value < 0) {
307 LOG(ERROR) << key << ": Invalid value (" << stored_value
308 << ") in persisted state. Defaulting to 0";
309 return 0;
310 }
311
312 return stored_value;
313}
314
315void SetNumReboots(int64_t num_reboots, PrefsInterface* prefs) {
316 CHECK(prefs);
317 prefs->SetInt64(kPrefsNumReboots, num_reboots);
318 LOG(INFO) << "Number of Reboots during current update attempt = "
319 << num_reboots;
320}
321
322void SetPayloadAttemptNumber(int64_t payload_attempt_number,
323 PrefsInterface* prefs) {
324 CHECK(prefs);
325 prefs->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number);
326 LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number;
327}
328
329void SetSystemUpdatedMarker(ClockInterface* clock, PrefsInterface* prefs) {
330 CHECK(prefs);
331 CHECK(clock);
332 Time update_finish_time = clock->GetMonotonicTime();
333 prefs->SetInt64(kPrefsSystemUpdatedMarker,
334 update_finish_time.ToInternalValue());
335 LOG(INFO) << "Updated Marker = " << utils::ToString(update_finish_time);
336}
337
338void SetUpdateTimestampStart(const Time& update_start_time,
339 PrefsInterface* prefs) {
340 CHECK(prefs);
341 prefs->SetInt64(kPrefsUpdateTimestampStart,
342 update_start_time.ToInternalValue());
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700343 LOG(INFO) << "Update Monotonic Timestamp Start = "
Tianjie Xu90aaa102017-10-10 17:39:03 -0700344 << utils::ToString(update_start_time);
345}
346
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700347void SetUpdateBootTimestampStart(const base::Time& update_start_boot_time,
348 PrefsInterface* prefs) {
349 CHECK(prefs);
350 prefs->SetInt64(kPrefsUpdateBootTimestampStart,
351 update_start_boot_time.ToInternalValue());
352 LOG(INFO) << "Update Boot Timestamp Start = "
353 << utils::ToString(update_start_boot_time);
354}
355
Tianjie Xu90aaa102017-10-10 17:39:03 -0700356bool LoadAndReportTimeToReboot(MetricsReporterInterface* metrics_reporter,
357 PrefsInterface* prefs,
358 ClockInterface* clock) {
359 CHECK(prefs);
360 CHECK(clock);
361 int64_t stored_value = GetPersistedValue(kPrefsSystemUpdatedMarker, prefs);
362 if (stored_value == 0)
363 return false;
364
365 Time system_updated_at = Time::FromInternalValue(stored_value);
Amin Hassaniec7bc112020-10-29 16:47:58 -0700366 TimeDelta time_to_reboot = clock->GetMonotonicTime() - system_updated_at;
Tianjie Xu90aaa102017-10-10 17:39:03 -0700367 if (time_to_reboot.ToInternalValue() < 0) {
368 LOG(ERROR) << "time_to_reboot is negative - system_updated_at: "
369 << utils::ToString(system_updated_at);
370 return false;
371 }
372 metrics_reporter->ReportTimeToReboot(time_to_reboot.InMinutes());
373 return true;
374}
375
Alex Deymo38429cf2015-11-11 18:27:22 -0800376} // namespace metrics_utils
377} // namespace chromeos_update_engine