Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/payload_state.h" |
| 6 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 9 | #include <base/logging.h> |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 10 | #include "base/string_util.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 11 | #include <base/stringprintf.h> |
| 12 | |
Jay Srinivasan | d29695d | 2013-04-08 15:08:05 -0700 | [diff] [blame] | 13 | #include "update_engine/constants.h" |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 14 | #include "update_engine/prefs.h" |
| 15 | #include "update_engine/system_state.h" |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 16 | #include "update_engine/utils.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 17 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 18 | using base::Time; |
| 19 | using base::TimeDelta; |
| 20 | using std::min; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 21 | using std::string; |
| 22 | |
| 23 | namespace chromeos_update_engine { |
| 24 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 25 | const TimeDelta PayloadState::kDurationSlack = TimeDelta::FromSeconds(600); |
| 26 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 27 | // We want to upperbound backoffs to 16 days |
| 28 | static const uint32_t kMaxBackoffDays = 16; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 29 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 30 | // We want to randomize retry attempts after the backoff by +/- 6 hours. |
| 31 | static const uint32_t kMaxBackoffFuzzMinutes = 12 * 60; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 32 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 33 | PayloadState::PayloadState() |
| 34 | : prefs_(NULL), |
| 35 | payload_attempt_number_(0), |
| 36 | url_index_(0), |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 37 | url_failure_count_(0), |
| 38 | url_switch_count_(0) { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 39 | for (int i = 0; i <= kNumDownloadSources; i++) |
| 40 | total_bytes_downloaded_[i] = current_bytes_downloaded_[i] = 0; |
| 41 | } |
| 42 | |
| 43 | bool PayloadState::Initialize(SystemState* system_state) { |
| 44 | system_state_ = system_state; |
| 45 | prefs_ = system_state_->prefs(); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 46 | LoadResponseSignature(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 47 | LoadPayloadAttemptNumber(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 48 | LoadUrlIndex(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 49 | LoadUrlFailureCount(); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 50 | LoadUrlSwitchCount(); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 51 | LoadBackoffExpiryTime(); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 52 | LoadUpdateTimestampStart(); |
| 53 | // The LoadUpdateDurationUptime() method relies on LoadUpdateTimestampStart() |
| 54 | // being called before it. Don't reorder. |
| 55 | LoadUpdateDurationUptime(); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 56 | for (int i = 0; i < kNumDownloadSources; i++) { |
| 57 | DownloadSource source = static_cast<DownloadSource>(i); |
| 58 | LoadCurrentBytesDownloaded(source); |
| 59 | LoadTotalBytesDownloaded(source); |
| 60 | } |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 61 | LoadNumReboots(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 65 | void PayloadState::SetResponse(const OmahaResponse& omaha_response) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 66 | // Always store the latest response. |
| 67 | response_ = omaha_response; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 68 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 69 | // Check if the "signature" of this response (i.e. the fields we care about) |
| 70 | // has changed. |
| 71 | string new_response_signature = CalculateResponseSignature(); |
| 72 | bool has_response_changed = (response_signature_ != new_response_signature); |
| 73 | |
| 74 | // If the response has changed, we should persist the new signature and |
| 75 | // clear away all the existing state. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 76 | if (has_response_changed) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 77 | LOG(INFO) << "Resetting all persisted state as this is a new response"; |
| 78 | SetResponseSignature(new_response_signature); |
| 79 | ResetPersistedState(); |
| 80 | return; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 83 | // This is the earliest point at which we can validate whether the URL index |
| 84 | // we loaded from the persisted state is a valid value. If the response |
| 85 | // hasn't changed but the URL index is invalid, it's indicative of some |
| 86 | // tampering of the persisted state. |
| 87 | if (url_index_ >= GetNumUrls()) { |
| 88 | LOG(INFO) << "Resetting all payload state as the url index seems to have " |
| 89 | "been tampered with"; |
| 90 | ResetPersistedState(); |
| 91 | return; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 92 | } |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 93 | |
| 94 | // Update the current download source which depends on the latest value of |
| 95 | // the response. |
| 96 | UpdateCurrentDownloadSource(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 99 | void PayloadState::DownloadComplete() { |
| 100 | LOG(INFO) << "Payload downloaded successfully"; |
| 101 | IncrementPayloadAttemptNumber(); |
| 102 | } |
| 103 | |
| 104 | void PayloadState::DownloadProgress(size_t count) { |
| 105 | if (count == 0) |
| 106 | return; |
| 107 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 108 | CalculateUpdateDurationUptime(); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 109 | UpdateBytesDownloaded(count); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 110 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 111 | // We've received non-zero bytes from a recent download operation. Since our |
| 112 | // URL failure count is meant to penalize a URL only for consecutive |
| 113 | // failures, downloading bytes successfully means we should reset the failure |
| 114 | // count (as we know at least that the URL is working). In future, we can |
| 115 | // design this to be more sophisticated to check for more intelligent failure |
| 116 | // patterns, but right now, even 1 byte downloaded will mark the URL to be |
| 117 | // good unless it hits 10 (or configured number of) consecutive failures |
| 118 | // again. |
| 119 | |
| 120 | if (GetUrlFailureCount() == 0) |
| 121 | return; |
| 122 | |
| 123 | LOG(INFO) << "Resetting failure count of Url" << GetUrlIndex() |
| 124 | << " to 0 as we received " << count << " bytes successfully"; |
| 125 | SetUrlFailureCount(0); |
| 126 | } |
| 127 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 128 | void PayloadState::UpdateResumed() { |
| 129 | LOG(INFO) << "Resuming an update that was previously started."; |
| 130 | UpdateNumReboots(); |
| 131 | } |
| 132 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 133 | void PayloadState::UpdateRestarted() { |
| 134 | LOG(INFO) << "Starting a new update"; |
| 135 | ResetDownloadSourcesOnNewUpdate(); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 136 | SetNumReboots(0); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 139 | void PayloadState::UpdateSucceeded() { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 140 | // Send the relevant metrics that are tracked in this class to UMA. |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 141 | CalculateUpdateDurationUptime(); |
| 142 | SetUpdateTimestampEnd(Time::Now()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 143 | ReportBytesDownloadedMetrics(); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 144 | ReportUpdateUrlSwitchesMetric(); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 145 | ReportRebootMetrics(); |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame^] | 146 | ReportDurationMetrics(); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 149 | void PayloadState::UpdateFailed(ActionExitCode error) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 150 | ActionExitCode base_error = utils::GetBaseErrorCode(error); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 151 | LOG(INFO) << "Updating payload state for error code: " << base_error |
| 152 | << " (" << utils::CodeToString(base_error) << ")"; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 153 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 154 | if (GetNumUrls() == 0) { |
| 155 | // This means we got this error even before we got a valid Omaha response. |
| 156 | // So we should not advance the url_index_ in such cases. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 157 | LOG(INFO) << "Ignoring failures until we get a valid Omaha response."; |
| 158 | return; |
| 159 | } |
| 160 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 161 | switch (base_error) { |
| 162 | // Errors which are good indicators of a problem with a particular URL or |
| 163 | // the protocol used in the URL or entities in the communication channel |
| 164 | // (e.g. proxies). We should try the next available URL in the next update |
| 165 | // check to quickly recover from these errors. |
| 166 | case kActionCodePayloadHashMismatchError: |
| 167 | case kActionCodePayloadSizeMismatchError: |
| 168 | case kActionCodeDownloadPayloadVerificationError: |
| 169 | case kActionCodeDownloadPayloadPubKeyVerificationError: |
| 170 | case kActionCodeSignedDeltaPayloadExpectedError: |
| 171 | case kActionCodeDownloadInvalidMetadataMagicString: |
| 172 | case kActionCodeDownloadSignatureMissingInManifest: |
| 173 | case kActionCodeDownloadManifestParseError: |
| 174 | case kActionCodeDownloadMetadataSignatureError: |
| 175 | case kActionCodeDownloadMetadataSignatureVerificationError: |
| 176 | case kActionCodeDownloadMetadataSignatureMismatch: |
| 177 | case kActionCodeDownloadOperationHashVerificationError: |
| 178 | case kActionCodeDownloadOperationExecutionError: |
| 179 | case kActionCodeDownloadOperationHashMismatch: |
| 180 | case kActionCodeDownloadInvalidMetadataSize: |
| 181 | case kActionCodeDownloadInvalidMetadataSignature: |
| 182 | case kActionCodeDownloadOperationHashMissingError: |
| 183 | case kActionCodeDownloadMetadataSignatureMissingError: |
| 184 | IncrementUrlIndex(); |
| 185 | break; |
| 186 | |
| 187 | // Errors which seem to be just transient network/communication related |
| 188 | // failures and do not indicate any inherent problem with the URL itself. |
| 189 | // So, we should keep the current URL but just increment the |
| 190 | // failure count to give it more chances. This way, while we maximize our |
| 191 | // chances of downloading from the URLs that appear earlier in the response |
| 192 | // (because download from a local server URL that appears earlier in a |
| 193 | // response is preferable than downloading from the next URL which could be |
| 194 | // a internet URL and thus could be more expensive). |
| 195 | case kActionCodeError: |
| 196 | case kActionCodeDownloadTransferError: |
| 197 | case kActionCodeDownloadWriteError: |
| 198 | case kActionCodeDownloadStateInitializationError: |
| 199 | case kActionCodeOmahaErrorInHTTPResponse: // Aggregate code for HTTP errors. |
| 200 | IncrementFailureCount(); |
| 201 | break; |
| 202 | |
| 203 | // Errors which are not specific to a URL and hence shouldn't result in |
| 204 | // the URL being penalized. This can happen in two cases: |
| 205 | // 1. We haven't started downloading anything: These errors don't cost us |
| 206 | // anything in terms of actual payload bytes, so we should just do the |
| 207 | // regular retries at the next update check. |
| 208 | // 2. We have successfully downloaded the payload: In this case, the |
| 209 | // payload attempt number would have been incremented and would take care |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 210 | // of the backoff at the next update check. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 211 | // In either case, there's no need to update URL index or failure count. |
| 212 | case kActionCodeOmahaRequestError: |
| 213 | case kActionCodeOmahaResponseHandlerError: |
| 214 | case kActionCodePostinstallRunnerError: |
| 215 | case kActionCodeFilesystemCopierError: |
| 216 | case kActionCodeInstallDeviceOpenError: |
| 217 | case kActionCodeKernelDeviceOpenError: |
| 218 | case kActionCodeDownloadNewPartitionInfoError: |
| 219 | case kActionCodeNewRootfsVerificationError: |
| 220 | case kActionCodeNewKernelVerificationError: |
| 221 | case kActionCodePostinstallBootedFromFirmwareB: |
| 222 | case kActionCodeOmahaRequestEmptyResponseError: |
| 223 | case kActionCodeOmahaRequestXMLParseError: |
| 224 | case kActionCodeOmahaResponseInvalid: |
| 225 | case kActionCodeOmahaUpdateIgnoredPerPolicy: |
| 226 | case kActionCodeOmahaUpdateDeferredPerPolicy: |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 227 | case kActionCodeOmahaUpdateDeferredForBackoff: |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 228 | case kActionCodePostinstallPowerwashError: |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 229 | case kActionCodeUpdateCanceledByChannelChange: |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 230 | LOG(INFO) << "Not incrementing URL index or failure count for this error"; |
| 231 | break; |
| 232 | |
| 233 | case kActionCodeSuccess: // success code |
| 234 | case kActionCodeSetBootableFlagError: // unused |
| 235 | case kActionCodeUmaReportedMax: // not an error code |
| 236 | case kActionCodeOmahaRequestHTTPResponseBase: // aggregated already |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 237 | case kActionCodeDevModeFlag: // not an error code |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 238 | case kActionCodeResumedFlag: // not an error code |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 239 | case kActionCodeTestImageFlag: // not an error code |
| 240 | case kActionCodeTestOmahaUrlFlag: // not an error code |
| 241 | case kSpecialFlags: // not an error code |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 242 | // These shouldn't happen. Enumerating these explicitly here so that we |
| 243 | // can let the compiler warn about new error codes that are added to |
| 244 | // action_processor.h but not added here. |
| 245 | LOG(WARNING) << "Unexpected error code for UpdateFailed"; |
| 246 | break; |
| 247 | |
| 248 | // Note: Not adding a default here so as to let the compiler warn us of |
| 249 | // any new enums that were added in the .h but not listed in this switch. |
| 250 | } |
| 251 | } |
| 252 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 253 | bool PayloadState::ShouldBackoffDownload() { |
| 254 | if (response_.disable_payload_backoff) { |
| 255 | LOG(INFO) << "Payload backoff logic is disabled. " |
| 256 | "Can proceed with the download"; |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | if (response_.is_delta_payload) { |
| 261 | // If delta payloads fail, we want to fallback quickly to full payloads as |
| 262 | // they are more likely to succeed. Exponential backoffs would greatly |
| 263 | // slow down the fallback to full payloads. So we don't backoff for delta |
| 264 | // payloads. |
| 265 | LOG(INFO) << "No backoffs for delta payloads. " |
| 266 | << "Can proceed with the download"; |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | if (!utils::IsOfficialBuild()) { |
| 271 | // Backoffs are needed only for official builds. We do not want any delays |
| 272 | // or update failures due to backoffs during testing or development. |
| 273 | LOG(INFO) << "No backoffs for test/dev images. " |
| 274 | << "Can proceed with the download"; |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | if (backoff_expiry_time_.is_null()) { |
| 279 | LOG(INFO) << "No backoff expiry time has been set. " |
| 280 | << "Can proceed with the download"; |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | if (backoff_expiry_time_ < Time::Now()) { |
| 285 | LOG(INFO) << "The backoff expiry time (" |
| 286 | << utils::ToString(backoff_expiry_time_) |
| 287 | << ") has elapsed. Can proceed with the download"; |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | LOG(INFO) << "Cannot proceed with downloads as we need to backoff until " |
| 292 | << utils::ToString(backoff_expiry_time_); |
| 293 | return true; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void PayloadState::IncrementPayloadAttemptNumber() { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 297 | if (response_.is_delta_payload) { |
| 298 | LOG(INFO) << "Not incrementing payload attempt number for delta payloads"; |
| 299 | return; |
| 300 | } |
| 301 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 302 | LOG(INFO) << "Incrementing the payload attempt number"; |
| 303 | SetPayloadAttemptNumber(GetPayloadAttemptNumber() + 1); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 304 | UpdateBackoffExpiryTime(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void PayloadState::IncrementUrlIndex() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 308 | uint32_t next_url_index = GetUrlIndex() + 1; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 309 | if (next_url_index < GetNumUrls()) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 310 | LOG(INFO) << "Incrementing the URL index for next attempt"; |
| 311 | SetUrlIndex(next_url_index); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 312 | } else { |
| 313 | LOG(INFO) << "Resetting the current URL index (" << GetUrlIndex() << ") to " |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 314 | << "0 as we only have " << GetNumUrls() << " URL(s)"; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 315 | SetUrlIndex(0); |
| 316 | IncrementPayloadAttemptNumber(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 317 | } |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 318 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 319 | // If we have multiple URLs, record that we just switched to another one |
| 320 | if (GetNumUrls() > 1) |
| 321 | SetUrlSwitchCount(url_switch_count_ + 1); |
| 322 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 323 | // Whenever we update the URL index, we should also clear the URL failure |
| 324 | // count so we can start over fresh for the new URL. |
| 325 | SetUrlFailureCount(0); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 328 | void PayloadState::IncrementFailureCount() { |
| 329 | uint32_t next_url_failure_count = GetUrlFailureCount() + 1; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 330 | if (next_url_failure_count < response_.max_failure_count_per_url) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 331 | LOG(INFO) << "Incrementing the URL failure count"; |
| 332 | SetUrlFailureCount(next_url_failure_count); |
| 333 | } else { |
| 334 | LOG(INFO) << "Reached max number of failures for Url" << GetUrlIndex() |
| 335 | << ". Trying next available URL"; |
| 336 | IncrementUrlIndex(); |
| 337 | } |
| 338 | } |
| 339 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 340 | void PayloadState::UpdateBackoffExpiryTime() { |
| 341 | if (response_.disable_payload_backoff) { |
| 342 | LOG(INFO) << "Resetting backoff expiry time as payload backoff is disabled"; |
| 343 | SetBackoffExpiryTime(Time()); |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | if (GetPayloadAttemptNumber() == 0) { |
| 348 | SetBackoffExpiryTime(Time()); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | // Since we're doing left-shift below, make sure we don't shift more |
| 353 | // than this. E.g. if uint32_t is 4-bytes, don't left-shift more than 30 bits, |
| 354 | // since we don't expect value of kMaxBackoffDays to be more than 100 anyway. |
| 355 | uint32_t num_days = 1; // the value to be shifted. |
| 356 | const uint32_t kMaxShifts = (sizeof(num_days) * 8) - 2; |
| 357 | |
| 358 | // Normal backoff days is 2 raised to (payload_attempt_number - 1). |
| 359 | // E.g. if payload_attempt_number is over 30, limit power to 30. |
| 360 | uint32_t power = min(GetPayloadAttemptNumber() - 1, kMaxShifts); |
| 361 | |
| 362 | // The number of days is the minimum of 2 raised to (payload_attempt_number |
| 363 | // - 1) or kMaxBackoffDays. |
| 364 | num_days = min(num_days << power, kMaxBackoffDays); |
| 365 | |
| 366 | // We don't want all retries to happen exactly at the same time when |
| 367 | // retrying after backoff. So add some random minutes to fuzz. |
| 368 | int fuzz_minutes = utils::FuzzInt(0, kMaxBackoffFuzzMinutes); |
| 369 | TimeDelta next_backoff_interval = TimeDelta::FromDays(num_days) + |
| 370 | TimeDelta::FromMinutes(fuzz_minutes); |
| 371 | LOG(INFO) << "Incrementing the backoff expiry time by " |
| 372 | << utils::FormatTimeDelta(next_backoff_interval); |
| 373 | SetBackoffExpiryTime(Time::Now() + next_backoff_interval); |
| 374 | } |
| 375 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 376 | void PayloadState::UpdateCurrentDownloadSource() { |
| 377 | current_download_source_ = kNumDownloadSources; |
| 378 | |
| 379 | if (GetUrlIndex() < response_.payload_urls.size()) { |
| 380 | string current_url = response_.payload_urls[GetUrlIndex()]; |
| 381 | if (StartsWithASCII(current_url, "https://", false)) |
| 382 | current_download_source_ = kDownloadSourceHttpsServer; |
| 383 | else if (StartsWithASCII(current_url, "http://", false)) |
| 384 | current_download_source_ = kDownloadSourceHttpServer; |
| 385 | } |
| 386 | |
| 387 | LOG(INFO) << "Current download source: " |
| 388 | << utils::ToString(current_download_source_); |
| 389 | } |
| 390 | |
| 391 | void PayloadState::UpdateBytesDownloaded(size_t count) { |
| 392 | SetCurrentBytesDownloaded( |
| 393 | current_download_source_, |
| 394 | GetCurrentBytesDownloaded(current_download_source_) + count, |
| 395 | false); |
| 396 | SetTotalBytesDownloaded( |
| 397 | current_download_source_, |
| 398 | GetTotalBytesDownloaded(current_download_source_) + count, |
| 399 | false); |
| 400 | } |
| 401 | |
| 402 | void PayloadState::ReportBytesDownloadedMetrics() { |
| 403 | // Report metrics collected from all known download sources to UMA. |
| 404 | // The reported data is in Megabytes in order to represent a larger |
| 405 | // sample range. |
| 406 | for (int i = 0; i < kNumDownloadSources; i++) { |
| 407 | DownloadSource source = static_cast<DownloadSource>(i); |
| 408 | const int kMaxMiBs = 10240; // Anything above 10GB goes in the last bucket. |
| 409 | |
| 410 | string metric = "Installer.SuccessfulMBsDownloadedFrom" + |
| 411 | utils::ToString(source); |
| 412 | uint64_t mbs = GetCurrentBytesDownloaded(source) / kNumBytesInOneMiB; |
| 413 | LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric; |
| 414 | system_state_->metrics_lib()->SendToUMA(metric, |
| 415 | mbs, |
| 416 | 0, // min |
| 417 | kMaxMiBs, |
| 418 | kNumDefaultUmaBuckets); |
| 419 | SetCurrentBytesDownloaded(source, 0, true); |
| 420 | |
| 421 | metric = "Installer.TotalMBsDownloadedFrom" + utils::ToString(source); |
| 422 | mbs = GetTotalBytesDownloaded(source) / kNumBytesInOneMiB; |
| 423 | LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric; |
| 424 | system_state_->metrics_lib()->SendToUMA(metric, |
| 425 | mbs, |
| 426 | 0, // min |
| 427 | kMaxMiBs, |
| 428 | kNumDefaultUmaBuckets); |
| 429 | |
| 430 | SetTotalBytesDownloaded(source, 0, true); |
| 431 | } |
| 432 | } |
| 433 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 434 | void PayloadState::ReportUpdateUrlSwitchesMetric() { |
| 435 | string metric = "Installer.UpdateURLSwitches"; |
| 436 | int value = static_cast<int>(url_switch_count_); |
| 437 | |
| 438 | LOG(INFO) << "Uploading " << value << " (count) for metric " << metric; |
| 439 | system_state_->metrics_lib()->SendToUMA( |
| 440 | metric, |
| 441 | value, |
| 442 | 0, // min value |
| 443 | 100, // max value |
| 444 | kNumDefaultUmaBuckets); |
| 445 | } |
| 446 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 447 | void PayloadState::ReportRebootMetrics() { |
| 448 | // Report the number of num_reboots. |
| 449 | string metric = "Installer.UpdateNumReboots"; |
| 450 | uint32_t num_reboots = GetNumReboots(); |
| 451 | LOG(INFO) << "Uploading reboot count of " << num_reboots << " for metric " |
| 452 | << metric; |
| 453 | system_state_->metrics_lib()->SendToUMA( |
| 454 | metric, |
| 455 | static_cast<int>(num_reboots), // sample |
| 456 | 0, // min = 0. |
| 457 | 50, // max |
| 458 | 25); // buckets |
| 459 | SetNumReboots(0); |
| 460 | } |
| 461 | |
| 462 | void PayloadState::UpdateNumReboots() { |
| 463 | // We only update the reboot count when the system has been detected to have |
| 464 | // been rebooted. |
| 465 | if (!system_state_->system_rebooted()) { |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | SetNumReboots(GetNumReboots() + 1); |
| 470 | } |
| 471 | |
| 472 | void PayloadState::SetNumReboots(uint32_t num_reboots) { |
| 473 | CHECK(prefs_); |
| 474 | num_reboots_ = num_reboots; |
| 475 | prefs_->SetInt64(kPrefsNumReboots, num_reboots); |
| 476 | LOG(INFO) << "Number of Reboots during current update attempt = " |
| 477 | << num_reboots_; |
| 478 | } |
| 479 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 480 | void PayloadState::ResetPersistedState() { |
| 481 | SetPayloadAttemptNumber(0); |
| 482 | SetUrlIndex(0); |
| 483 | SetUrlFailureCount(0); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 484 | SetUrlSwitchCount(0); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 485 | UpdateBackoffExpiryTime(); // This will reset the backoff expiry time. |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 486 | SetUpdateTimestampStart(Time::Now()); |
| 487 | SetUpdateTimestampEnd(Time()); // Set to null time |
| 488 | SetUpdateDurationUptime(TimeDelta::FromSeconds(0)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 489 | ResetDownloadSourcesOnNewUpdate(); |
| 490 | } |
| 491 | |
| 492 | void PayloadState::ResetDownloadSourcesOnNewUpdate() { |
| 493 | for (int i = 0; i < kNumDownloadSources; i++) { |
| 494 | DownloadSource source = static_cast<DownloadSource>(i); |
| 495 | SetCurrentBytesDownloaded(source, 0, true); |
| 496 | // Note: Not resetting the TotalBytesDownloaded as we want that metric |
| 497 | // to count the bytes downloaded across various update attempts until |
| 498 | // we have successfully applied the update. |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | int64_t PayloadState::GetPersistedValue(const string& key) { |
| 503 | CHECK(prefs_); |
| 504 | if (!prefs_->Exists(key)) |
| 505 | return 0; |
| 506 | |
| 507 | int64_t stored_value; |
| 508 | if (!prefs_->GetInt64(key, &stored_value)) |
| 509 | return 0; |
| 510 | |
| 511 | if (stored_value < 0) { |
| 512 | LOG(ERROR) << key << ": Invalid value (" << stored_value |
| 513 | << ") in persisted state. Defaulting to 0"; |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | return stored_value; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | string PayloadState::CalculateResponseSignature() { |
| 521 | string response_sign = StringPrintf("NumURLs = %d\n", |
| 522 | response_.payload_urls.size()); |
| 523 | |
| 524 | for (size_t i = 0; i < response_.payload_urls.size(); i++) |
| 525 | response_sign += StringPrintf("Url%d = %s\n", |
| 526 | i, response_.payload_urls[i].c_str()); |
| 527 | |
| 528 | response_sign += StringPrintf("Payload Size = %llu\n" |
| 529 | "Payload Sha256 Hash = %s\n" |
| 530 | "Metadata Size = %llu\n" |
| 531 | "Metadata Signature = %s\n" |
| 532 | "Is Delta Payload = %d\n" |
| 533 | "Max Failure Count Per Url = %d\n" |
| 534 | "Disable Payload Backoff = %d\n", |
| 535 | response_.size, |
| 536 | response_.hash.c_str(), |
| 537 | response_.metadata_size, |
| 538 | response_.metadata_signature.c_str(), |
| 539 | response_.is_delta_payload, |
| 540 | response_.max_failure_count_per_url, |
| 541 | response_.disable_payload_backoff); |
| 542 | return response_sign; |
| 543 | } |
| 544 | |
| 545 | void PayloadState::LoadResponseSignature() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 546 | CHECK(prefs_); |
| 547 | string stored_value; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 548 | if (prefs_->Exists(kPrefsCurrentResponseSignature) && |
| 549 | prefs_->GetString(kPrefsCurrentResponseSignature, &stored_value)) { |
| 550 | SetResponseSignature(stored_value); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 551 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 552 | } |
| 553 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 554 | void PayloadState::SetResponseSignature(const string& response_signature) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 555 | CHECK(prefs_); |
| 556 | response_signature_ = response_signature; |
| 557 | LOG(INFO) << "Current Response Signature = \n" << response_signature_; |
| 558 | prefs_->SetString(kPrefsCurrentResponseSignature, response_signature_); |
| 559 | } |
| 560 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 561 | void PayloadState::LoadPayloadAttemptNumber() { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 562 | SetPayloadAttemptNumber(GetPersistedValue(kPrefsPayloadAttemptNumber)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | void PayloadState::SetPayloadAttemptNumber(uint32_t payload_attempt_number) { |
| 566 | CHECK(prefs_); |
| 567 | payload_attempt_number_ = payload_attempt_number; |
| 568 | LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number_; |
| 569 | prefs_->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number_); |
| 570 | } |
| 571 | |
| 572 | void PayloadState::LoadUrlIndex() { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 573 | SetUrlIndex(GetPersistedValue(kPrefsCurrentUrlIndex)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | void PayloadState::SetUrlIndex(uint32_t url_index) { |
| 577 | CHECK(prefs_); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 578 | url_index_ = url_index; |
| 579 | LOG(INFO) << "Current URL Index = " << url_index_; |
| 580 | prefs_->SetInt64(kPrefsCurrentUrlIndex, url_index_); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 581 | |
| 582 | // Also update the download source, which is purely dependent on the |
| 583 | // current URL index alone. |
| 584 | UpdateCurrentDownloadSource(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 585 | } |
| 586 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 587 | void PayloadState::LoadUrlSwitchCount() { |
| 588 | SetUrlSwitchCount(GetPersistedValue(kPrefsUrlSwitchCount)); |
| 589 | } |
| 590 | |
| 591 | void PayloadState::SetUrlSwitchCount(uint32_t url_switch_count) { |
| 592 | CHECK(prefs_); |
| 593 | url_switch_count_ = url_switch_count; |
| 594 | LOG(INFO) << "URL Switch Count = " << url_switch_count_; |
| 595 | prefs_->SetInt64(kPrefsUrlSwitchCount, url_switch_count_); |
| 596 | } |
| 597 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 598 | void PayloadState::LoadUrlFailureCount() { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 599 | SetUrlFailureCount(GetPersistedValue(kPrefsCurrentUrlFailureCount)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | void PayloadState::SetUrlFailureCount(uint32_t url_failure_count) { |
| 603 | CHECK(prefs_); |
| 604 | url_failure_count_ = url_failure_count; |
| 605 | LOG(INFO) << "Current URL (Url" << GetUrlIndex() |
| 606 | << ")'s Failure Count = " << url_failure_count_; |
| 607 | prefs_->SetInt64(kPrefsCurrentUrlFailureCount, url_failure_count_); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 610 | void PayloadState::LoadBackoffExpiryTime() { |
| 611 | CHECK(prefs_); |
| 612 | int64_t stored_value; |
| 613 | if (!prefs_->Exists(kPrefsBackoffExpiryTime)) |
| 614 | return; |
| 615 | |
| 616 | if (!prefs_->GetInt64(kPrefsBackoffExpiryTime, &stored_value)) |
| 617 | return; |
| 618 | |
| 619 | Time stored_time = Time::FromInternalValue(stored_value); |
| 620 | if (stored_time > Time::Now() + TimeDelta::FromDays(kMaxBackoffDays)) { |
| 621 | LOG(ERROR) << "Invalid backoff expiry time (" |
| 622 | << utils::ToString(stored_time) |
| 623 | << ") in persisted state. Resetting."; |
| 624 | stored_time = Time(); |
| 625 | } |
| 626 | SetBackoffExpiryTime(stored_time); |
| 627 | } |
| 628 | |
| 629 | void PayloadState::SetBackoffExpiryTime(const Time& new_time) { |
| 630 | CHECK(prefs_); |
| 631 | backoff_expiry_time_ = new_time; |
| 632 | LOG(INFO) << "Backoff Expiry Time = " |
| 633 | << utils::ToString(backoff_expiry_time_); |
| 634 | prefs_->SetInt64(kPrefsBackoffExpiryTime, |
| 635 | backoff_expiry_time_.ToInternalValue()); |
| 636 | } |
| 637 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 638 | TimeDelta PayloadState::GetUpdateDuration() { |
| 639 | Time end_time = update_timestamp_end_.is_null() ? Time::Now() : |
| 640 | update_timestamp_end_; |
| 641 | return end_time - update_timestamp_start_; |
| 642 | } |
| 643 | |
| 644 | void PayloadState::LoadUpdateTimestampStart() { |
| 645 | int64_t stored_value; |
| 646 | Time stored_time; |
| 647 | |
| 648 | CHECK(prefs_); |
| 649 | |
| 650 | Time now = Time::Now(); |
| 651 | |
| 652 | if (!prefs_->Exists(kPrefsUpdateTimestampStart)) { |
| 653 | // The preference missing is not unexpected - in that case, just |
| 654 | // use the current time as start time |
| 655 | stored_time = now; |
| 656 | } else if (!prefs_->GetInt64(kPrefsUpdateTimestampStart, &stored_value)) { |
| 657 | LOG(ERROR) << "Invalid UpdateTimestampStart value. Resetting."; |
| 658 | stored_time = now; |
| 659 | } else { |
| 660 | stored_time = Time::FromInternalValue(stored_value); |
| 661 | } |
| 662 | |
| 663 | // Sanity check: If the time read from disk is in the future |
| 664 | // (modulo some slack to account for possible NTP drift |
| 665 | // adjustments), something is fishy and we should report and |
| 666 | // reset. |
| 667 | TimeDelta duration_according_to_stored_time = now - stored_time; |
| 668 | if (duration_according_to_stored_time < -kDurationSlack) { |
| 669 | LOG(ERROR) << "The UpdateTimestampStart value (" |
| 670 | << utils::ToString(stored_time) |
| 671 | << ") in persisted state is " |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame^] | 672 | << utils::FormatTimeDelta(duration_according_to_stored_time) |
| 673 | << " in the future. Resetting."; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 674 | stored_time = now; |
| 675 | } |
| 676 | |
| 677 | SetUpdateTimestampStart(stored_time); |
| 678 | } |
| 679 | |
| 680 | void PayloadState::SetUpdateTimestampStart(const Time& value) { |
| 681 | CHECK(prefs_); |
| 682 | update_timestamp_start_ = value; |
| 683 | prefs_->SetInt64(kPrefsUpdateTimestampStart, |
| 684 | update_timestamp_start_.ToInternalValue()); |
| 685 | LOG(INFO) << "Update Timestamp Start = " |
| 686 | << utils::ToString(update_timestamp_start_); |
| 687 | } |
| 688 | |
| 689 | void PayloadState::SetUpdateTimestampEnd(const Time& value) { |
| 690 | update_timestamp_end_ = value; |
| 691 | LOG(INFO) << "Update Timestamp End = " |
| 692 | << utils::ToString(update_timestamp_end_); |
| 693 | } |
| 694 | |
| 695 | TimeDelta PayloadState::GetUpdateDurationUptime() { |
| 696 | return update_duration_uptime_; |
| 697 | } |
| 698 | |
| 699 | void PayloadState::LoadUpdateDurationUptime() { |
| 700 | int64_t stored_value; |
| 701 | TimeDelta stored_delta; |
| 702 | |
| 703 | CHECK(prefs_); |
| 704 | |
| 705 | if (!prefs_->Exists(kPrefsUpdateDurationUptime)) { |
| 706 | // The preference missing is not unexpected - in that case, just |
| 707 | // we'll use zero as the delta |
| 708 | } else if (!prefs_->GetInt64(kPrefsUpdateDurationUptime, &stored_value)) { |
| 709 | LOG(ERROR) << "Invalid UpdateDurationUptime value. Resetting."; |
| 710 | stored_delta = TimeDelta::FromSeconds(0); |
| 711 | } else { |
| 712 | stored_delta = TimeDelta::FromInternalValue(stored_value); |
| 713 | } |
| 714 | |
| 715 | // Sanity-check: Uptime can never be greater than the wall-clock |
| 716 | // difference (modulo some slack). If it is, report and reset |
| 717 | // to the wall-clock difference. |
| 718 | TimeDelta diff = GetUpdateDuration() - stored_delta; |
| 719 | if (diff < -kDurationSlack) { |
| 720 | LOG(ERROR) << "The UpdateDurationUptime value (" |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame^] | 721 | << utils::FormatTimeDelta(stored_delta) |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 722 | << ") in persisted state is " |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame^] | 723 | << utils::FormatTimeDelta(diff) |
| 724 | << " larger than the wall-clock delta. Resetting."; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 725 | stored_delta = update_duration_current_; |
| 726 | } |
| 727 | |
| 728 | SetUpdateDurationUptime(stored_delta); |
| 729 | } |
| 730 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 731 | void PayloadState::LoadNumReboots() { |
| 732 | SetNumReboots(GetPersistedValue(kPrefsNumReboots)); |
| 733 | } |
| 734 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 735 | void PayloadState::SetUpdateDurationUptimeExtended(const TimeDelta& value, |
| 736 | const Time& timestamp, |
| 737 | bool use_logging) { |
| 738 | CHECK(prefs_); |
| 739 | update_duration_uptime_ = value; |
| 740 | update_duration_uptime_timestamp_ = timestamp; |
| 741 | prefs_->SetInt64(kPrefsUpdateDurationUptime, |
| 742 | update_duration_uptime_.ToInternalValue()); |
| 743 | if (use_logging) { |
| 744 | LOG(INFO) << "Update Duration Uptime = " |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame^] | 745 | << utils::FormatTimeDelta(update_duration_uptime_); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
| 749 | void PayloadState::SetUpdateDurationUptime(const TimeDelta& value) { |
| 750 | SetUpdateDurationUptimeExtended(value, utils::GetMonotonicTime(), true); |
| 751 | } |
| 752 | |
| 753 | void PayloadState::CalculateUpdateDurationUptime() { |
| 754 | Time now = utils::GetMonotonicTime(); |
| 755 | TimeDelta uptime_since_last_update = now - update_duration_uptime_timestamp_; |
| 756 | TimeDelta new_uptime = update_duration_uptime_ + uptime_since_last_update; |
| 757 | // We're frequently called so avoid logging this write |
| 758 | SetUpdateDurationUptimeExtended(new_uptime, now, false); |
| 759 | } |
| 760 | |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame^] | 761 | void PayloadState::ReportDurationMetrics() { |
| 762 | TimeDelta duration = GetUpdateDuration(); |
| 763 | TimeDelta duration_uptime = GetUpdateDurationUptime(); |
| 764 | string metric; |
| 765 | |
| 766 | metric = "Installer.UpdateDurationMinutes"; |
| 767 | system_state_->metrics_lib()->SendToUMA( |
| 768 | metric, |
| 769 | static_cast<int>(duration.InMinutes()), |
| 770 | 1, // min: 1 minute |
| 771 | 365*24*60, // max: 1 year (approx) |
| 772 | kNumDefaultUmaBuckets); |
| 773 | LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration) |
| 774 | << " for metric " << metric; |
| 775 | |
| 776 | metric = "Installer.UpdateDurationUptimeMinutes"; |
| 777 | system_state_->metrics_lib()->SendToUMA( |
| 778 | metric, |
| 779 | static_cast<int>(duration_uptime.InMinutes()), |
| 780 | 1, // min: 1 minute |
| 781 | 30*24*60, // max: 1 month (approx) |
| 782 | kNumDefaultUmaBuckets); |
| 783 | LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration_uptime) |
| 784 | << " for metric " << metric; |
| 785 | |
| 786 | prefs_->Delete(kPrefsUpdateTimestampStart); |
| 787 | prefs_->Delete(kPrefsUpdateDurationUptime); |
| 788 | } |
| 789 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 790 | string PayloadState::GetPrefsKey(const string& prefix, DownloadSource source) { |
| 791 | return prefix + "-from-" + utils::ToString(source); |
| 792 | } |
| 793 | |
| 794 | void PayloadState::LoadCurrentBytesDownloaded(DownloadSource source) { |
| 795 | string key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source); |
| 796 | SetCurrentBytesDownloaded(source, GetPersistedValue(key), true); |
| 797 | } |
| 798 | |
| 799 | void PayloadState::SetCurrentBytesDownloaded( |
| 800 | DownloadSource source, |
| 801 | uint64_t current_bytes_downloaded, |
| 802 | bool log) { |
| 803 | CHECK(prefs_); |
| 804 | |
| 805 | if (source >= kNumDownloadSources) |
| 806 | return; |
| 807 | |
| 808 | // Update the in-memory value. |
| 809 | current_bytes_downloaded_[source] = current_bytes_downloaded; |
| 810 | |
| 811 | string prefs_key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source); |
| 812 | prefs_->SetInt64(prefs_key, current_bytes_downloaded); |
| 813 | LOG_IF(INFO, log) << "Current bytes downloaded for " |
| 814 | << utils::ToString(source) << " = " |
| 815 | << GetCurrentBytesDownloaded(source); |
| 816 | } |
| 817 | |
| 818 | void PayloadState::LoadTotalBytesDownloaded(DownloadSource source) { |
| 819 | string key = GetPrefsKey(kPrefsTotalBytesDownloaded, source); |
| 820 | SetTotalBytesDownloaded(source, GetPersistedValue(key), true); |
| 821 | } |
| 822 | |
| 823 | void PayloadState::SetTotalBytesDownloaded( |
| 824 | DownloadSource source, |
| 825 | uint64_t total_bytes_downloaded, |
| 826 | bool log) { |
| 827 | CHECK(prefs_); |
| 828 | |
| 829 | if (source >= kNumDownloadSources) |
| 830 | return; |
| 831 | |
| 832 | // Update the in-memory value. |
| 833 | total_bytes_downloaded_[source] = total_bytes_downloaded; |
| 834 | |
| 835 | // Persist. |
| 836 | string prefs_key = GetPrefsKey(kPrefsTotalBytesDownloaded, source); |
| 837 | prefs_->SetInt64(prefs_key, total_bytes_downloaded); |
| 838 | LOG_IF(INFO, log) << "Total bytes downloaded for " |
| 839 | << utils::ToString(source) << " = " |
| 840 | << GetTotalBytesDownloaded(source); |
| 841 | } |
| 842 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 843 | } // namespace chromeos_update_engine |