Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 1 | // |
| 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 Deymo | a259179 | 2015-11-17 00:39:40 -0300 | [diff] [blame] | 19 | #include <string> |
| 20 | |
| 21 | #include <base/time/time.h> |
| 22 | |
| 23 | #include "update_engine/common/clock_interface.h" |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 24 | #include "update_engine/common/constants.h" |
| 25 | #include "update_engine/common/utils.h" |
Alex Deymo | a259179 | 2015-11-17 00:39:40 -0300 | [diff] [blame] | 26 | #include "update_engine/system_state.h" |
| 27 | |
| 28 | using base::Time; |
| 29 | using base::TimeDelta; |
| 30 | |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 31 | namespace chromeos_update_engine { |
| 32 | namespace metrics_utils { |
| 33 | |
| 34 | metrics::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 Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 42 | case ErrorCode::kUpdatedButNotActive: |
| 43 | return metrics::AttemptResult::kUpdateSucceededNotActive; |
| 44 | |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 45 | case ErrorCode::kDownloadTransferError: |
Amin Hassani | d3d8421 | 2019-08-17 00:27:44 -0700 | [diff] [blame] | 46 | case ErrorCode::kInternalLibCurlError: |
| 47 | case ErrorCode::kUnresolvedHostError: |
| 48 | case ErrorCode::kUnresolvedHostRecovered: |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 49 | 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 Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 74 | case ErrorCode::kVerityCalculationError: |
Yifan Hong | 46e37d4 | 2019-12-13 12:01:42 -0800 | [diff] [blame] | 75 | case ErrorCode::kNotEnoughSpace: |
Yifan Hong | 7727990 | 2019-12-17 16:38:21 -0800 | [diff] [blame] | 76 | case ErrorCode::kDeviceCorrupted: |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 77 | 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 Jiang | 8e768e9 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 87 | case ErrorCode::kPayloadTimestampError: |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 88 | return metrics::AttemptResult::kPayloadVerificationFailed; |
| 89 | |
| 90 | case ErrorCode::kNewRootfsVerificationError: |
| 91 | case ErrorCode::kNewKernelVerificationError: |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 92 | case ErrorCode::kRollbackNotPossible: |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 93 | return metrics::AttemptResult::kVerificationFailed; |
| 94 | |
| 95 | case ErrorCode::kPostinstallRunnerError: |
| 96 | case ErrorCode::kPostinstallBootedFromFirmwareB: |
| 97 | case ErrorCode::kPostinstallFirmwareRONotUpdatable: |
Kelvin Zhang | 8b1e0dc | 2020-10-26 12:27:53 -0400 | [diff] [blame^] | 98 | case ErrorCode::kPostInstallMountError: |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 99 | return metrics::AttemptResult::kPostInstallFailed; |
| 100 | |
Alex Deymo | 1f19dcc | 2016-02-03 09:22:17 -0800 | [diff] [blame] | 101 | case ErrorCode::kUserCanceled: |
| 102 | return metrics::AttemptResult::kUpdateCanceled; |
| 103 | |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 104 | // 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 Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 115 | case ErrorCode::kOmahaErrorInHTTPResponse: |
| 116 | case ErrorCode::kDownloadMetadataSignatureMissingError: |
| 117 | case ErrorCode::kOmahaUpdateDeferredForBackoff: |
| 118 | case ErrorCode::kPostinstallPowerwashError: |
| 119 | case ErrorCode::kUpdateCanceledByChannelChange: |
| 120 | case ErrorCode::kOmahaRequestXMLHasEntityDecl: |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 121 | case ErrorCode::kOmahaUpdateIgnoredOverCellular: |
Sen Jiang | 89e24c1 | 2018-03-22 18:05:44 -0700 | [diff] [blame] | 122 | case ErrorCode::kNoUpdate: |
Amin Hassani | 80f4d4c | 2018-05-16 13:34:00 -0700 | [diff] [blame] | 123 | case ErrorCode::kFirstActiveOmahaPingSentPersistenceError: |
Jae Hoon Kim | 3e69b4c | 2020-06-16 09:23:39 -0700 | [diff] [blame] | 124 | case ErrorCode::kPackageExcludedFromUpdate: |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 125 | return metrics::AttemptResult::kInternalError; |
| 126 | |
Andrew | b57c16e | 2020-07-22 14:32:39 -0700 | [diff] [blame] | 127 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: |
| 128 | case ErrorCode::kNonCriticalUpdateInOOBE: |
| 129 | return metrics::AttemptResult::kUpdateSkipped; |
| 130 | |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 131 | // 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 | |
| 148 | metrics::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 Hassani | d3d8421 | 2019-08-17 00:27:44 -0700 | [diff] [blame] | 178 | 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 Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 185 | // 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 Zhang | 8b1e0dc | 2020-10-26 12:27:53 -0400 | [diff] [blame^] | 193 | case ErrorCode::kPostInstallMountError: |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 194 | 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 Cernekee | 2494e28 | 2016-03-29 18:03:53 -0700 | [diff] [blame] | 224 | case ErrorCode::kNonCriticalUpdateInOOBE: |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 225 | 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 Deymo | 1f19dcc | 2016-02-03 09:22:17 -0800 | [diff] [blame] | 236 | case ErrorCode::kUserCanceled: |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 237 | case ErrorCode::kOmahaUpdateIgnoredOverCellular: |
Sen Jiang | 8e768e9 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 238 | case ErrorCode::kPayloadTimestampError: |
Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 239 | case ErrorCode::kUpdatedButNotActive: |
Sen Jiang | 89e24c1 | 2018-03-22 18:05:44 -0700 | [diff] [blame] | 240 | case ErrorCode::kNoUpdate: |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 241 | case ErrorCode::kRollbackNotPossible: |
Amin Hassani | 80f4d4c | 2018-05-16 13:34:00 -0700 | [diff] [blame] | 242 | case ErrorCode::kFirstActiveOmahaPingSentPersistenceError: |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 243 | case ErrorCode::kVerityCalculationError: |
Yifan Hong | 46e37d4 | 2019-12-13 12:01:42 -0800 | [diff] [blame] | 244 | case ErrorCode::kNotEnoughSpace: |
Yifan Hong | 7727990 | 2019-12-17 16:38:21 -0800 | [diff] [blame] | 245 | case ErrorCode::kDeviceCorrupted: |
Jae Hoon Kim | 3e69b4c | 2020-06-16 09:23:39 -0700 | [diff] [blame] | 246 | case ErrorCode::kPackageExcludedFromUpdate: |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 247 | 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 Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 266 | metrics::ConnectionType GetConnectionType(ConnectionType type, |
| 267 | ConnectionTethering tethering) { |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 268 | switch (type) { |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 269 | case ConnectionType::kUnknown: |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 270 | return metrics::ConnectionType::kUnknown; |
| 271 | |
Colin Howes | c9e98d6 | 2018-09-18 10:35:20 -0700 | [diff] [blame] | 272 | case ConnectionType::kDisconnected: |
| 273 | return metrics::ConnectionType::kDisconnected; |
| 274 | |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 275 | case ConnectionType::kEthernet: |
| 276 | if (tethering == ConnectionTethering::kConfirmed) |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 277 | return metrics::ConnectionType::kTetheredEthernet; |
| 278 | else |
| 279 | return metrics::ConnectionType::kEthernet; |
| 280 | |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 281 | case ConnectionType::kWifi: |
| 282 | if (tethering == ConnectionTethering::kConfirmed) |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 283 | return metrics::ConnectionType::kTetheredWifi; |
| 284 | else |
| 285 | return metrics::ConnectionType::kWifi; |
| 286 | |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 287 | case ConnectionType::kCellular: |
Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 288 | 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 Deymo | a259179 | 2015-11-17 00:39:40 -0300 | [diff] [blame] | 298 | bool 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 | |
| 324 | bool 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 Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 340 | int64_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 | |
| 358 | void 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 | |
| 365 | void 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 | |
| 372 | void 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 | |
| 381 | void SetUpdateTimestampStart(const Time& update_start_time, |
| 382 | PrefsInterface* prefs) { |
| 383 | CHECK(prefs); |
| 384 | prefs->SetInt64(kPrefsUpdateTimestampStart, |
| 385 | update_start_time.ToInternalValue()); |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 386 | LOG(INFO) << "Update Monotonic Timestamp Start = " |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 387 | << utils::ToString(update_start_time); |
| 388 | } |
| 389 | |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 390 | void 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 Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 399 | bool 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 Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 420 | } // namespace metrics_utils |
| 421 | } // namespace chromeos_update_engine |