blob: 018f2e49ee816bb9c7947b639f9708d4297568dc [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 Jiang02c49422017-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:
46 return metrics::AttemptResult::kPayloadDownloadError;
47
48 case ErrorCode::kDownloadInvalidMetadataSize:
49 case ErrorCode::kDownloadInvalidMetadataMagicString:
50 case ErrorCode::kDownloadMetadataSignatureError:
51 case ErrorCode::kDownloadMetadataSignatureVerificationError:
52 case ErrorCode::kPayloadMismatchedType:
53 case ErrorCode::kUnsupportedMajorPayloadVersion:
54 case ErrorCode::kUnsupportedMinorPayloadVersion:
55 case ErrorCode::kDownloadNewPartitionInfoError:
56 case ErrorCode::kDownloadSignatureMissingInManifest:
57 case ErrorCode::kDownloadManifestParseError:
58 case ErrorCode::kDownloadOperationHashMissingError:
59 return metrics::AttemptResult::kMetadataMalformed;
60
61 case ErrorCode::kDownloadOperationHashMismatch:
62 case ErrorCode::kDownloadOperationHashVerificationError:
63 return metrics::AttemptResult::kOperationMalformed;
64
65 case ErrorCode::kDownloadOperationExecutionError:
66 case ErrorCode::kInstallDeviceOpenError:
67 case ErrorCode::kKernelDeviceOpenError:
68 case ErrorCode::kDownloadWriteError:
69 case ErrorCode::kFilesystemCopierError:
70 case ErrorCode::kFilesystemVerifierError:
71 return metrics::AttemptResult::kOperationExecutionError;
72
73 case ErrorCode::kDownloadMetadataSignatureMismatch:
74 return metrics::AttemptResult::kMetadataVerificationFailed;
75
76 case ErrorCode::kPayloadSizeMismatchError:
77 case ErrorCode::kPayloadHashMismatchError:
78 case ErrorCode::kDownloadPayloadVerificationError:
79 case ErrorCode::kSignedDeltaPayloadExpectedError:
80 case ErrorCode::kDownloadPayloadPubKeyVerificationError:
81 return metrics::AttemptResult::kPayloadVerificationFailed;
82
83 case ErrorCode::kNewRootfsVerificationError:
84 case ErrorCode::kNewKernelVerificationError:
Marton Hunyady199152d2018-05-07 19:08:48 +020085 case ErrorCode::kRollbackNotPossible:
Alex Deymo38429cf2015-11-11 18:27:22 -080086 return metrics::AttemptResult::kVerificationFailed;
87
88 case ErrorCode::kPostinstallRunnerError:
89 case ErrorCode::kPostinstallBootedFromFirmwareB:
90 case ErrorCode::kPostinstallFirmwareRONotUpdatable:
91 return metrics::AttemptResult::kPostInstallFailed;
92
Alex Deymo1f19dcc2016-02-03 09:22:17 -080093 case ErrorCode::kUserCanceled:
94 return metrics::AttemptResult::kUpdateCanceled;
95
Alex Deymo38429cf2015-11-11 18:27:22 -080096 // We should never get these errors in the update-attempt stage so
97 // return internal error if this happens.
98 case ErrorCode::kError:
99 case ErrorCode::kOmahaRequestXMLParseError:
100 case ErrorCode::kOmahaRequestError:
101 case ErrorCode::kOmahaResponseHandlerError:
102 case ErrorCode::kDownloadStateInitializationError:
103 case ErrorCode::kOmahaRequestEmptyResponseError:
104 case ErrorCode::kDownloadInvalidMetadataSignature:
105 case ErrorCode::kOmahaResponseInvalid:
106 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
Kevin Cernekee2494e282016-03-29 18:03:53 -0700107 // TODO(deymo): The next two items belong in their own category; they
108 // should not be counted as internal errors. b/27112092
Alex Deymo38429cf2015-11-11 18:27:22 -0800109 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
Kevin Cernekee2494e282016-03-29 18:03:53 -0700110 case ErrorCode::kNonCriticalUpdateInOOBE:
Alex Deymo38429cf2015-11-11 18:27:22 -0800111 case ErrorCode::kOmahaErrorInHTTPResponse:
112 case ErrorCode::kDownloadMetadataSignatureMissingError:
113 case ErrorCode::kOmahaUpdateDeferredForBackoff:
114 case ErrorCode::kPostinstallPowerwashError:
115 case ErrorCode::kUpdateCanceledByChannelChange:
116 case ErrorCode::kOmahaRequestXMLHasEntityDecl:
Weidong Guo421ff332017-04-17 10:08:38 -0700117 case ErrorCode::kOmahaUpdateIgnoredOverCellular:
Sen Jiang3978ddd2018-03-22 18:05:44 -0700118 case ErrorCode::kNoUpdate:
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700119 case ErrorCode::kFirstActiveOmahaPingSentPersistenceError:
Alex Deymo38429cf2015-11-11 18:27:22 -0800120 return metrics::AttemptResult::kInternalError;
121
122 // Special flags. These can't happen (we mask them out above) but
123 // the compiler doesn't know that. Just break out so we can warn and
124 // return |kInternalError|.
125 case ErrorCode::kUmaReportedMax:
126 case ErrorCode::kOmahaRequestHTTPResponseBase:
127 case ErrorCode::kDevModeFlag:
128 case ErrorCode::kResumedFlag:
129 case ErrorCode::kTestImageFlag:
130 case ErrorCode::kTestOmahaUrlFlag:
131 case ErrorCode::kSpecialFlags:
132 break;
133 }
134
135 LOG(ERROR) << "Unexpected error code " << base_code;
136 return metrics::AttemptResult::kInternalError;
137}
138
139metrics::DownloadErrorCode GetDownloadErrorCode(ErrorCode code) {
140 ErrorCode base_code = static_cast<ErrorCode>(
141 static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
142
143 if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) {
144 int http_status =
145 static_cast<int>(base_code) -
146 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase);
147 if (http_status >= 200 && http_status <= 599) {
148 return static_cast<metrics::DownloadErrorCode>(
149 static_cast<int>(metrics::DownloadErrorCode::kHttpStatus200) +
150 http_status - 200);
151 } else if (http_status == 0) {
152 // The code is using HTTP Status 0 for "Unable to get http
153 // response code."
154 return metrics::DownloadErrorCode::kDownloadError;
155 }
156 LOG(WARNING) << "Unexpected HTTP status code " << http_status;
157 return metrics::DownloadErrorCode::kHttpStatusOther;
158 }
159
160 switch (base_code) {
161 // Unfortunately, ErrorCode::kDownloadTransferError is returned for a wide
162 // variety of errors (proxy errors, host not reachable, timeouts etc.).
163 //
164 // For now just map that to kDownloading. See http://crbug.com/355745
165 // for how we plan to add more detail in the future.
166 case ErrorCode::kDownloadTransferError:
167 return metrics::DownloadErrorCode::kDownloadError;
168
169 // All of these error codes are not related to downloading so break
170 // out so we can warn and return InputMalformed.
171 case ErrorCode::kSuccess:
172 case ErrorCode::kError:
173 case ErrorCode::kOmahaRequestError:
174 case ErrorCode::kOmahaResponseHandlerError:
175 case ErrorCode::kFilesystemCopierError:
176 case ErrorCode::kPostinstallRunnerError:
177 case ErrorCode::kPayloadMismatchedType:
178 case ErrorCode::kInstallDeviceOpenError:
179 case ErrorCode::kKernelDeviceOpenError:
180 case ErrorCode::kPayloadHashMismatchError:
181 case ErrorCode::kPayloadSizeMismatchError:
182 case ErrorCode::kDownloadPayloadVerificationError:
183 case ErrorCode::kDownloadNewPartitionInfoError:
184 case ErrorCode::kDownloadWriteError:
185 case ErrorCode::kNewRootfsVerificationError:
186 case ErrorCode::kNewKernelVerificationError:
187 case ErrorCode::kSignedDeltaPayloadExpectedError:
188 case ErrorCode::kDownloadPayloadPubKeyVerificationError:
189 case ErrorCode::kPostinstallBootedFromFirmwareB:
190 case ErrorCode::kDownloadStateInitializationError:
191 case ErrorCode::kDownloadInvalidMetadataMagicString:
192 case ErrorCode::kDownloadSignatureMissingInManifest:
193 case ErrorCode::kDownloadManifestParseError:
194 case ErrorCode::kDownloadMetadataSignatureError:
195 case ErrorCode::kDownloadMetadataSignatureVerificationError:
196 case ErrorCode::kDownloadMetadataSignatureMismatch:
197 case ErrorCode::kDownloadOperationHashVerificationError:
198 case ErrorCode::kDownloadOperationExecutionError:
199 case ErrorCode::kDownloadOperationHashMismatch:
200 case ErrorCode::kOmahaRequestEmptyResponseError:
201 case ErrorCode::kOmahaRequestXMLParseError:
202 case ErrorCode::kDownloadInvalidMetadataSize:
203 case ErrorCode::kDownloadInvalidMetadataSignature:
204 case ErrorCode::kOmahaResponseInvalid:
205 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
206 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
Kevin Cernekee2494e282016-03-29 18:03:53 -0700207 case ErrorCode::kNonCriticalUpdateInOOBE:
Alex Deymo38429cf2015-11-11 18:27:22 -0800208 case ErrorCode::kOmahaErrorInHTTPResponse:
209 case ErrorCode::kDownloadOperationHashMissingError:
210 case ErrorCode::kDownloadMetadataSignatureMissingError:
211 case ErrorCode::kOmahaUpdateDeferredForBackoff:
212 case ErrorCode::kPostinstallPowerwashError:
213 case ErrorCode::kUpdateCanceledByChannelChange:
214 case ErrorCode::kPostinstallFirmwareRONotUpdatable:
215 case ErrorCode::kUnsupportedMajorPayloadVersion:
216 case ErrorCode::kUnsupportedMinorPayloadVersion:
217 case ErrorCode::kOmahaRequestXMLHasEntityDecl:
218 case ErrorCode::kFilesystemVerifierError:
Alex Deymo1f19dcc2016-02-03 09:22:17 -0800219 case ErrorCode::kUserCanceled:
Weidong Guo421ff332017-04-17 10:08:38 -0700220 case ErrorCode::kOmahaUpdateIgnoredOverCellular:
Sen Jiang02c49422017-10-31 15:14:11 -0700221 case ErrorCode::kUpdatedButNotActive:
Sen Jiang3978ddd2018-03-22 18:05:44 -0700222 case ErrorCode::kNoUpdate:
Marton Hunyady199152d2018-05-07 19:08:48 +0200223 case ErrorCode::kRollbackNotPossible:
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700224 case ErrorCode::kFirstActiveOmahaPingSentPersistenceError:
Alex Deymo38429cf2015-11-11 18:27:22 -0800225 break;
226
227 // Special flags. These can't happen (we mask them out above) but
228 // the compiler doesn't know that. Just break out so we can warn and
229 // return |kInputMalformed|.
230 case ErrorCode::kUmaReportedMax:
231 case ErrorCode::kOmahaRequestHTTPResponseBase:
232 case ErrorCode::kDevModeFlag:
233 case ErrorCode::kResumedFlag:
234 case ErrorCode::kTestImageFlag:
235 case ErrorCode::kTestOmahaUrlFlag:
236 case ErrorCode::kSpecialFlags:
237 LOG(ERROR) << "Unexpected error code " << base_code;
238 break;
239 }
240
241 return metrics::DownloadErrorCode::kInputMalformed;
242}
243
Sen Jiang255e22b2016-05-20 16:15:29 -0700244metrics::ConnectionType GetConnectionType(ConnectionType type,
245 ConnectionTethering tethering) {
Alex Deymo38429cf2015-11-11 18:27:22 -0800246 switch (type) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700247 case ConnectionType::kUnknown:
Alex Deymo38429cf2015-11-11 18:27:22 -0800248 return metrics::ConnectionType::kUnknown;
249
Colin Howesc9e98d62018-09-18 10:35:20 -0700250 case ConnectionType::kDisconnected:
251 return metrics::ConnectionType::kDisconnected;
252
Sen Jiang255e22b2016-05-20 16:15:29 -0700253 case ConnectionType::kEthernet:
254 if (tethering == ConnectionTethering::kConfirmed)
Alex Deymo38429cf2015-11-11 18:27:22 -0800255 return metrics::ConnectionType::kTetheredEthernet;
256 else
257 return metrics::ConnectionType::kEthernet;
258
Sen Jiang255e22b2016-05-20 16:15:29 -0700259 case ConnectionType::kWifi:
260 if (tethering == ConnectionTethering::kConfirmed)
Alex Deymo38429cf2015-11-11 18:27:22 -0800261 return metrics::ConnectionType::kTetheredWifi;
262 else
263 return metrics::ConnectionType::kWifi;
264
Sen Jiang255e22b2016-05-20 16:15:29 -0700265 case ConnectionType::kWimax:
Alex Deymo38429cf2015-11-11 18:27:22 -0800266 return metrics::ConnectionType::kWimax;
267
Sen Jiang255e22b2016-05-20 16:15:29 -0700268 case ConnectionType::kBluetooth:
Alex Deymo38429cf2015-11-11 18:27:22 -0800269 return metrics::ConnectionType::kBluetooth;
270
Sen Jiang255e22b2016-05-20 16:15:29 -0700271 case ConnectionType::kCellular:
Alex Deymo38429cf2015-11-11 18:27:22 -0800272 return metrics::ConnectionType::kCellular;
273 }
274
275 LOG(ERROR) << "Unexpected network connection type: type="
276 << static_cast<int>(type)
277 << ", tethering=" << static_cast<int>(tethering);
278
279 return metrics::ConnectionType::kUnknown;
280}
281
Alex Deymoa2591792015-11-17 00:39:40 -0300282bool WallclockDurationHelper(SystemState* system_state,
283 const std::string& state_variable_key,
284 TimeDelta* out_duration) {
285 bool ret = false;
286
287 Time now = system_state->clock()->GetWallclockTime();
288 int64_t stored_value;
289 if (system_state->prefs()->GetInt64(state_variable_key, &stored_value)) {
290 Time stored_time = Time::FromInternalValue(stored_value);
291 if (stored_time > now) {
292 LOG(ERROR) << "Stored time-stamp used for " << state_variable_key
293 << " is in the future.";
294 } else {
295 *out_duration = now - stored_time;
296 ret = true;
297 }
298 }
299
300 if (!system_state->prefs()->SetInt64(state_variable_key,
301 now.ToInternalValue())) {
302 LOG(ERROR) << "Error storing time-stamp in " << state_variable_key;
303 }
304
305 return ret;
306}
307
308bool MonotonicDurationHelper(SystemState* system_state,
309 int64_t* storage,
310 TimeDelta* out_duration) {
311 bool ret = false;
312
313 Time now = system_state->clock()->GetMonotonicTime();
314 if (*storage != 0) {
315 Time stored_time = Time::FromInternalValue(*storage);
316 *out_duration = now - stored_time;
317 ret = true;
318 }
319 *storage = now.ToInternalValue();
320
321 return ret;
322}
323
Tianjie Xu90aaa102017-10-10 17:39:03 -0700324int64_t GetPersistedValue(const std::string& key, PrefsInterface* prefs) {
325 CHECK(prefs);
326 if (!prefs->Exists(key))
327 return 0;
328
329 int64_t stored_value;
330 if (!prefs->GetInt64(key, &stored_value))
331 return 0;
332
333 if (stored_value < 0) {
334 LOG(ERROR) << key << ": Invalid value (" << stored_value
335 << ") in persisted state. Defaulting to 0";
336 return 0;
337 }
338
339 return stored_value;
340}
341
342void SetNumReboots(int64_t num_reboots, PrefsInterface* prefs) {
343 CHECK(prefs);
344 prefs->SetInt64(kPrefsNumReboots, num_reboots);
345 LOG(INFO) << "Number of Reboots during current update attempt = "
346 << num_reboots;
347}
348
349void SetPayloadAttemptNumber(int64_t payload_attempt_number,
350 PrefsInterface* prefs) {
351 CHECK(prefs);
352 prefs->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number);
353 LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number;
354}
355
356void SetSystemUpdatedMarker(ClockInterface* clock, PrefsInterface* prefs) {
357 CHECK(prefs);
358 CHECK(clock);
359 Time update_finish_time = clock->GetMonotonicTime();
360 prefs->SetInt64(kPrefsSystemUpdatedMarker,
361 update_finish_time.ToInternalValue());
362 LOG(INFO) << "Updated Marker = " << utils::ToString(update_finish_time);
363}
364
365void SetUpdateTimestampStart(const Time& update_start_time,
366 PrefsInterface* prefs) {
367 CHECK(prefs);
368 prefs->SetInt64(kPrefsUpdateTimestampStart,
369 update_start_time.ToInternalValue());
370 LOG(INFO) << "Update Timestamp Start = "
371 << utils::ToString(update_start_time);
372}
373
374bool LoadAndReportTimeToReboot(MetricsReporterInterface* metrics_reporter,
375 PrefsInterface* prefs,
376 ClockInterface* clock) {
377 CHECK(prefs);
378 CHECK(clock);
379 int64_t stored_value = GetPersistedValue(kPrefsSystemUpdatedMarker, prefs);
380 if (stored_value == 0)
381 return false;
382
383 Time system_updated_at = Time::FromInternalValue(stored_value);
384 base::TimeDelta time_to_reboot =
385 clock->GetMonotonicTime() - system_updated_at;
386 if (time_to_reboot.ToInternalValue() < 0) {
387 LOG(ERROR) << "time_to_reboot is negative - system_updated_at: "
388 << utils::ToString(system_updated_at);
389 return false;
390 }
391 metrics_reporter->ReportTimeToReboot(time_to_reboot.InMinutes());
392 return true;
393}
394
Alex Deymo38429cf2015-11-11 18:27:22 -0800395} // namespace metrics_utils
396} // namespace chromeos_update_engine