blob: a947a7e4bb8e9f350879f866221247b134c21e17 [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#include "update_engine/system_state.h"
27
28using base::Time;
29using base::TimeDelta;
30
Alex Deymo38429cf2015-11-11 18:27:22 -080031namespace chromeos_update_engine {
32namespace metrics_utils {
33
34metrics::AttemptResult GetAttemptResult(ErrorCode code) {
35 ErrorCode base_code = static_cast<ErrorCode>(
36 static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
37
38 switch (base_code) {
39 case ErrorCode::kSuccess:
40 return metrics::AttemptResult::kUpdateSucceeded;
41
Sen Jiangfe522822017-10-31 15:14:11 -070042 case ErrorCode::kUpdatedButNotActive:
43 return metrics::AttemptResult::kUpdateSucceededNotActive;
44
Alex Deymo38429cf2015-11-11 18:27:22 -080045 case ErrorCode::kDownloadTransferError:
Amin Hassanid3d84212019-08-17 00:27:44 -070046 case ErrorCode::kInternalLibCurlError:
47 case ErrorCode::kUnresolvedHostError:
48 case ErrorCode::kUnresolvedHostRecovered:
Alex Deymo38429cf2015-11-11 18:27:22 -080049 return metrics::AttemptResult::kPayloadDownloadError;
50
51 case ErrorCode::kDownloadInvalidMetadataSize:
52 case ErrorCode::kDownloadInvalidMetadataMagicString:
53 case ErrorCode::kDownloadMetadataSignatureError:
54 case ErrorCode::kDownloadMetadataSignatureVerificationError:
55 case ErrorCode::kPayloadMismatchedType:
56 case ErrorCode::kUnsupportedMajorPayloadVersion:
57 case ErrorCode::kUnsupportedMinorPayloadVersion:
58 case ErrorCode::kDownloadNewPartitionInfoError:
59 case ErrorCode::kDownloadSignatureMissingInManifest:
60 case ErrorCode::kDownloadManifestParseError:
61 case ErrorCode::kDownloadOperationHashMissingError:
62 return metrics::AttemptResult::kMetadataMalformed;
63
64 case ErrorCode::kDownloadOperationHashMismatch:
65 case ErrorCode::kDownloadOperationHashVerificationError:
66 return metrics::AttemptResult::kOperationMalformed;
67
68 case ErrorCode::kDownloadOperationExecutionError:
69 case ErrorCode::kInstallDeviceOpenError:
70 case ErrorCode::kKernelDeviceOpenError:
71 case ErrorCode::kDownloadWriteError:
72 case ErrorCode::kFilesystemCopierError:
73 case ErrorCode::kFilesystemVerifierError:
Sen Jiang57f91802017-11-14 17:42:13 -080074 case ErrorCode::kVerityCalculationError:
Yifan Hong46e37d42019-12-13 12:01:42 -080075 case ErrorCode::kNotEnoughSpace:
Yifan Hong77279902019-12-17 16:38:21 -080076 case ErrorCode::kDeviceCorrupted:
Alex Deymo38429cf2015-11-11 18:27:22 -080077 return metrics::AttemptResult::kOperationExecutionError;
78
79 case ErrorCode::kDownloadMetadataSignatureMismatch:
80 return metrics::AttemptResult::kMetadataVerificationFailed;
81
82 case ErrorCode::kPayloadSizeMismatchError:
83 case ErrorCode::kPayloadHashMismatchError:
84 case ErrorCode::kDownloadPayloadVerificationError:
85 case ErrorCode::kSignedDeltaPayloadExpectedError:
86 case ErrorCode::kDownloadPayloadPubKeyVerificationError:
Sen Jiang8e768e92017-06-28 17:13:19 -070087 case ErrorCode::kPayloadTimestampError:
Alex Deymo38429cf2015-11-11 18:27:22 -080088 return metrics::AttemptResult::kPayloadVerificationFailed;
89
90 case ErrorCode::kNewRootfsVerificationError:
91 case ErrorCode::kNewKernelVerificationError:
Marton Hunyady199152d2018-05-07 19:08:48 +020092 case ErrorCode::kRollbackNotPossible:
Alex Deymo38429cf2015-11-11 18:27:22 -080093 return metrics::AttemptResult::kVerificationFailed;
94
95 case ErrorCode::kPostinstallRunnerError:
96 case ErrorCode::kPostinstallBootedFromFirmwareB:
97 case ErrorCode::kPostinstallFirmwareRONotUpdatable:
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -040098 case ErrorCode::kPostInstallMountError:
Alex Deymo38429cf2015-11-11 18:27:22 -080099 return metrics::AttemptResult::kPostInstallFailed;
100
Alex Deymo1f19dcc2016-02-03 09:22:17 -0800101 case ErrorCode::kUserCanceled:
102 return metrics::AttemptResult::kUpdateCanceled;
103
Alex Deymo38429cf2015-11-11 18:27:22 -0800104 // We should never get these errors in the update-attempt stage so
105 // return internal error if this happens.
106 case ErrorCode::kError:
107 case ErrorCode::kOmahaRequestXMLParseError:
108 case ErrorCode::kOmahaRequestError:
109 case ErrorCode::kOmahaResponseHandlerError:
110 case ErrorCode::kDownloadStateInitializationError:
111 case ErrorCode::kOmahaRequestEmptyResponseError:
112 case ErrorCode::kDownloadInvalidMetadataSignature:
113 case ErrorCode::kOmahaResponseInvalid:
114 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
Alex Deymo38429cf2015-11-11 18:27:22 -0800115 case ErrorCode::kOmahaErrorInHTTPResponse:
116 case ErrorCode::kDownloadMetadataSignatureMissingError:
117 case ErrorCode::kOmahaUpdateDeferredForBackoff:
118 case ErrorCode::kPostinstallPowerwashError:
119 case ErrorCode::kUpdateCanceledByChannelChange:
120 case ErrorCode::kOmahaRequestXMLHasEntityDecl:
Weidong Guo421ff332017-04-17 10:08:38 -0700121 case ErrorCode::kOmahaUpdateIgnoredOverCellular:
Sen Jiang89e24c12018-03-22 18:05:44 -0700122 case ErrorCode::kNoUpdate:
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700123 case ErrorCode::kFirstActiveOmahaPingSentPersistenceError:
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -0700124 case ErrorCode::kPackageExcludedFromUpdate:
Alex Deymo38429cf2015-11-11 18:27:22 -0800125 return metrics::AttemptResult::kInternalError;
126
Andrewb57c16e2020-07-22 14:32:39 -0700127 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
128 case ErrorCode::kNonCriticalUpdateInOOBE:
129 return metrics::AttemptResult::kUpdateSkipped;
130
Alex Deymo38429cf2015-11-11 18:27:22 -0800131 // Special flags. These can't happen (we mask them out above) but
132 // the compiler doesn't know that. Just break out so we can warn and
133 // return |kInternalError|.
134 case ErrorCode::kUmaReportedMax:
135 case ErrorCode::kOmahaRequestHTTPResponseBase:
136 case ErrorCode::kDevModeFlag:
137 case ErrorCode::kResumedFlag:
138 case ErrorCode::kTestImageFlag:
139 case ErrorCode::kTestOmahaUrlFlag:
140 case ErrorCode::kSpecialFlags:
141 break;
142 }
143
144 LOG(ERROR) << "Unexpected error code " << base_code;
145 return metrics::AttemptResult::kInternalError;
146}
147
148metrics::DownloadErrorCode GetDownloadErrorCode(ErrorCode code) {
149 ErrorCode base_code = static_cast<ErrorCode>(
150 static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
151
152 if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) {
153 int http_status =
154 static_cast<int>(base_code) -
155 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase);
156 if (http_status >= 200 && http_status <= 599) {
157 return static_cast<metrics::DownloadErrorCode>(
158 static_cast<int>(metrics::DownloadErrorCode::kHttpStatus200) +
159 http_status - 200);
160 } else if (http_status == 0) {
161 // The code is using HTTP Status 0 for "Unable to get http
162 // response code."
163 return metrics::DownloadErrorCode::kDownloadError;
164 }
165 LOG(WARNING) << "Unexpected HTTP status code " << http_status;
166 return metrics::DownloadErrorCode::kHttpStatusOther;
167 }
168
169 switch (base_code) {
170 // Unfortunately, ErrorCode::kDownloadTransferError is returned for a wide
171 // variety of errors (proxy errors, host not reachable, timeouts etc.).
172 //
173 // For now just map that to kDownloading. See http://crbug.com/355745
174 // for how we plan to add more detail in the future.
175 case ErrorCode::kDownloadTransferError:
176 return metrics::DownloadErrorCode::kDownloadError;
177
Amin Hassanid3d84212019-08-17 00:27:44 -0700178 case ErrorCode::kInternalLibCurlError:
179 return metrics::DownloadErrorCode::kInternalLibCurlError;
180 case ErrorCode::kUnresolvedHostError:
181 return metrics::DownloadErrorCode::kUnresolvedHostError;
182 case ErrorCode::kUnresolvedHostRecovered:
183 return metrics::DownloadErrorCode::kUnresolvedHostRecovered;
184
Alex Deymo38429cf2015-11-11 18:27:22 -0800185 // All of these error codes are not related to downloading so break
186 // out so we can warn and return InputMalformed.
187 case ErrorCode::kSuccess:
188 case ErrorCode::kError:
189 case ErrorCode::kOmahaRequestError:
190 case ErrorCode::kOmahaResponseHandlerError:
191 case ErrorCode::kFilesystemCopierError:
192 case ErrorCode::kPostinstallRunnerError:
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400193 case ErrorCode::kPostInstallMountError:
Alex Deymo38429cf2015-11-11 18:27:22 -0800194 case ErrorCode::kPayloadMismatchedType:
195 case ErrorCode::kInstallDeviceOpenError:
196 case ErrorCode::kKernelDeviceOpenError:
197 case ErrorCode::kPayloadHashMismatchError:
198 case ErrorCode::kPayloadSizeMismatchError:
199 case ErrorCode::kDownloadPayloadVerificationError:
200 case ErrorCode::kDownloadNewPartitionInfoError:
201 case ErrorCode::kDownloadWriteError:
202 case ErrorCode::kNewRootfsVerificationError:
203 case ErrorCode::kNewKernelVerificationError:
204 case ErrorCode::kSignedDeltaPayloadExpectedError:
205 case ErrorCode::kDownloadPayloadPubKeyVerificationError:
206 case ErrorCode::kPostinstallBootedFromFirmwareB:
207 case ErrorCode::kDownloadStateInitializationError:
208 case ErrorCode::kDownloadInvalidMetadataMagicString:
209 case ErrorCode::kDownloadSignatureMissingInManifest:
210 case ErrorCode::kDownloadManifestParseError:
211 case ErrorCode::kDownloadMetadataSignatureError:
212 case ErrorCode::kDownloadMetadataSignatureVerificationError:
213 case ErrorCode::kDownloadMetadataSignatureMismatch:
214 case ErrorCode::kDownloadOperationHashVerificationError:
215 case ErrorCode::kDownloadOperationExecutionError:
216 case ErrorCode::kDownloadOperationHashMismatch:
217 case ErrorCode::kOmahaRequestEmptyResponseError:
218 case ErrorCode::kOmahaRequestXMLParseError:
219 case ErrorCode::kDownloadInvalidMetadataSize:
220 case ErrorCode::kDownloadInvalidMetadataSignature:
221 case ErrorCode::kOmahaResponseInvalid:
222 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
223 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
Kevin Cernekee2494e282016-03-29 18:03:53 -0700224 case ErrorCode::kNonCriticalUpdateInOOBE:
Alex Deymo38429cf2015-11-11 18:27:22 -0800225 case ErrorCode::kOmahaErrorInHTTPResponse:
226 case ErrorCode::kDownloadOperationHashMissingError:
227 case ErrorCode::kDownloadMetadataSignatureMissingError:
228 case ErrorCode::kOmahaUpdateDeferredForBackoff:
229 case ErrorCode::kPostinstallPowerwashError:
230 case ErrorCode::kUpdateCanceledByChannelChange:
231 case ErrorCode::kPostinstallFirmwareRONotUpdatable:
232 case ErrorCode::kUnsupportedMajorPayloadVersion:
233 case ErrorCode::kUnsupportedMinorPayloadVersion:
234 case ErrorCode::kOmahaRequestXMLHasEntityDecl:
235 case ErrorCode::kFilesystemVerifierError:
Alex Deymo1f19dcc2016-02-03 09:22:17 -0800236 case ErrorCode::kUserCanceled:
Weidong Guo421ff332017-04-17 10:08:38 -0700237 case ErrorCode::kOmahaUpdateIgnoredOverCellular:
Sen Jiang8e768e92017-06-28 17:13:19 -0700238 case ErrorCode::kPayloadTimestampError:
Sen Jiangfe522822017-10-31 15:14:11 -0700239 case ErrorCode::kUpdatedButNotActive:
Sen Jiang89e24c12018-03-22 18:05:44 -0700240 case ErrorCode::kNoUpdate:
Marton Hunyady199152d2018-05-07 19:08:48 +0200241 case ErrorCode::kRollbackNotPossible:
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700242 case ErrorCode::kFirstActiveOmahaPingSentPersistenceError:
Sen Jiang57f91802017-11-14 17:42:13 -0800243 case ErrorCode::kVerityCalculationError:
Yifan Hong46e37d42019-12-13 12:01:42 -0800244 case ErrorCode::kNotEnoughSpace:
Yifan Hong77279902019-12-17 16:38:21 -0800245 case ErrorCode::kDeviceCorrupted:
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -0700246 case ErrorCode::kPackageExcludedFromUpdate:
Alex Deymo38429cf2015-11-11 18:27:22 -0800247 break;
248
249 // Special flags. These can't happen (we mask them out above) but
250 // the compiler doesn't know that. Just break out so we can warn and
251 // return |kInputMalformed|.
252 case ErrorCode::kUmaReportedMax:
253 case ErrorCode::kOmahaRequestHTTPResponseBase:
254 case ErrorCode::kDevModeFlag:
255 case ErrorCode::kResumedFlag:
256 case ErrorCode::kTestImageFlag:
257 case ErrorCode::kTestOmahaUrlFlag:
258 case ErrorCode::kSpecialFlags:
259 LOG(ERROR) << "Unexpected error code " << base_code;
260 break;
261 }
262
263 return metrics::DownloadErrorCode::kInputMalformed;
264}
265
Sen Jiang255e22b2016-05-20 16:15:29 -0700266metrics::ConnectionType GetConnectionType(ConnectionType type,
267 ConnectionTethering tethering) {
Alex Deymo38429cf2015-11-11 18:27:22 -0800268 switch (type) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700269 case ConnectionType::kUnknown:
Alex Deymo38429cf2015-11-11 18:27:22 -0800270 return metrics::ConnectionType::kUnknown;
271
Colin Howesc9e98d62018-09-18 10:35:20 -0700272 case ConnectionType::kDisconnected:
273 return metrics::ConnectionType::kDisconnected;
274
Sen Jiang255e22b2016-05-20 16:15:29 -0700275 case ConnectionType::kEthernet:
276 if (tethering == ConnectionTethering::kConfirmed)
Alex Deymo38429cf2015-11-11 18:27:22 -0800277 return metrics::ConnectionType::kTetheredEthernet;
278 else
279 return metrics::ConnectionType::kEthernet;
280
Sen Jiang255e22b2016-05-20 16:15:29 -0700281 case ConnectionType::kWifi:
282 if (tethering == ConnectionTethering::kConfirmed)
Alex Deymo38429cf2015-11-11 18:27:22 -0800283 return metrics::ConnectionType::kTetheredWifi;
284 else
285 return metrics::ConnectionType::kWifi;
286
Sen Jiang255e22b2016-05-20 16:15:29 -0700287 case ConnectionType::kCellular:
Alex Deymo38429cf2015-11-11 18:27:22 -0800288 return metrics::ConnectionType::kCellular;
289 }
290
291 LOG(ERROR) << "Unexpected network connection type: type="
292 << static_cast<int>(type)
293 << ", tethering=" << static_cast<int>(tethering);
294
295 return metrics::ConnectionType::kUnknown;
296}
297
Alex Deymoa2591792015-11-17 00:39:40 -0300298bool WallclockDurationHelper(SystemState* system_state,
299 const std::string& state_variable_key,
300 TimeDelta* out_duration) {
301 bool ret = false;
302
303 Time now = system_state->clock()->GetWallclockTime();
304 int64_t stored_value;
305 if (system_state->prefs()->GetInt64(state_variable_key, &stored_value)) {
306 Time stored_time = Time::FromInternalValue(stored_value);
307 if (stored_time > now) {
308 LOG(ERROR) << "Stored time-stamp used for " << state_variable_key
309 << " is in the future.";
310 } else {
311 *out_duration = now - stored_time;
312 ret = true;
313 }
314 }
315
316 if (!system_state->prefs()->SetInt64(state_variable_key,
317 now.ToInternalValue())) {
318 LOG(ERROR) << "Error storing time-stamp in " << state_variable_key;
319 }
320
321 return ret;
322}
323
324bool MonotonicDurationHelper(SystemState* system_state,
325 int64_t* storage,
326 TimeDelta* out_duration) {
327 bool ret = false;
328
329 Time now = system_state->clock()->GetMonotonicTime();
330 if (*storage != 0) {
331 Time stored_time = Time::FromInternalValue(*storage);
332 *out_duration = now - stored_time;
333 ret = true;
334 }
335 *storage = now.ToInternalValue();
336
337 return ret;
338}
339
Tianjie Xu90aaa102017-10-10 17:39:03 -0700340int64_t GetPersistedValue(const std::string& key, PrefsInterface* prefs) {
341 CHECK(prefs);
342 if (!prefs->Exists(key))
343 return 0;
344
345 int64_t stored_value;
346 if (!prefs->GetInt64(key, &stored_value))
347 return 0;
348
349 if (stored_value < 0) {
350 LOG(ERROR) << key << ": Invalid value (" << stored_value
351 << ") in persisted state. Defaulting to 0";
352 return 0;
353 }
354
355 return stored_value;
356}
357
358void SetNumReboots(int64_t num_reboots, PrefsInterface* prefs) {
359 CHECK(prefs);
360 prefs->SetInt64(kPrefsNumReboots, num_reboots);
361 LOG(INFO) << "Number of Reboots during current update attempt = "
362 << num_reboots;
363}
364
365void SetPayloadAttemptNumber(int64_t payload_attempt_number,
366 PrefsInterface* prefs) {
367 CHECK(prefs);
368 prefs->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number);
369 LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number;
370}
371
372void SetSystemUpdatedMarker(ClockInterface* clock, PrefsInterface* prefs) {
373 CHECK(prefs);
374 CHECK(clock);
375 Time update_finish_time = clock->GetMonotonicTime();
376 prefs->SetInt64(kPrefsSystemUpdatedMarker,
377 update_finish_time.ToInternalValue());
378 LOG(INFO) << "Updated Marker = " << utils::ToString(update_finish_time);
379}
380
381void SetUpdateTimestampStart(const Time& update_start_time,
382 PrefsInterface* prefs) {
383 CHECK(prefs);
384 prefs->SetInt64(kPrefsUpdateTimestampStart,
385 update_start_time.ToInternalValue());
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700386 LOG(INFO) << "Update Monotonic Timestamp Start = "
Tianjie Xu90aaa102017-10-10 17:39:03 -0700387 << utils::ToString(update_start_time);
388}
389
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700390void SetUpdateBootTimestampStart(const base::Time& update_start_boot_time,
391 PrefsInterface* prefs) {
392 CHECK(prefs);
393 prefs->SetInt64(kPrefsUpdateBootTimestampStart,
394 update_start_boot_time.ToInternalValue());
395 LOG(INFO) << "Update Boot Timestamp Start = "
396 << utils::ToString(update_start_boot_time);
397}
398
Tianjie Xu90aaa102017-10-10 17:39:03 -0700399bool LoadAndReportTimeToReboot(MetricsReporterInterface* metrics_reporter,
400 PrefsInterface* prefs,
401 ClockInterface* clock) {
402 CHECK(prefs);
403 CHECK(clock);
404 int64_t stored_value = GetPersistedValue(kPrefsSystemUpdatedMarker, prefs);
405 if (stored_value == 0)
406 return false;
407
408 Time system_updated_at = Time::FromInternalValue(stored_value);
409 base::TimeDelta time_to_reboot =
410 clock->GetMonotonicTime() - system_updated_at;
411 if (time_to_reboot.ToInternalValue() < 0) {
412 LOG(ERROR) << "time_to_reboot is negative - system_updated_at: "
413 << utils::ToString(system_updated_at);
414 return false;
415 }
416 metrics_reporter->ReportTimeToReboot(time_to_reboot.InMinutes());
417 return true;
418}
419
Alex Deymo38429cf2015-11-11 18:27:22 -0800420} // namespace metrics_utils
421} // namespace chromeos_update_engine