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 | |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 13 | #include "update_engine/clock.h" |
Jay Srinivasan | d29695d | 2013-04-08 15:08:05 -0700 | [diff] [blame] | 14 | #include "update_engine/constants.h" |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 15 | #include "update_engine/prefs.h" |
| 16 | #include "update_engine/system_state.h" |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 17 | #include "update_engine/utils.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 18 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 19 | using base::Time; |
| 20 | using base::TimeDelta; |
| 21 | using std::min; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 22 | using std::string; |
| 23 | |
| 24 | namespace chromeos_update_engine { |
| 25 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 26 | const TimeDelta PayloadState::kDurationSlack = TimeDelta::FromSeconds(600); |
| 27 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 28 | // We want to upperbound backoffs to 16 days |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 29 | static const int kMaxBackoffDays = 16; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 30 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 31 | // We want to randomize retry attempts after the backoff by +/- 6 hours. |
| 32 | static const uint32_t kMaxBackoffFuzzMinutes = 12 * 60; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 33 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 34 | PayloadState::PayloadState() |
| 35 | : prefs_(NULL), |
| 36 | payload_attempt_number_(0), |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 37 | full_payload_attempt_number_(0), |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 38 | url_index_(0), |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 39 | url_failure_count_(0), |
| 40 | url_switch_count_(0) { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 41 | for (int i = 0; i <= kNumDownloadSources; i++) |
| 42 | total_bytes_downloaded_[i] = current_bytes_downloaded_[i] = 0; |
| 43 | } |
| 44 | |
| 45 | bool PayloadState::Initialize(SystemState* system_state) { |
| 46 | system_state_ = system_state; |
| 47 | prefs_ = system_state_->prefs(); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 48 | powerwash_safe_prefs_ = system_state_->powerwash_safe_prefs(); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 49 | LoadResponseSignature(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 50 | LoadPayloadAttemptNumber(); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 51 | LoadFullPayloadAttemptNumber(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 52 | LoadUrlIndex(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 53 | LoadUrlFailureCount(); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 54 | LoadUrlSwitchCount(); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 55 | LoadBackoffExpiryTime(); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 56 | LoadUpdateTimestampStart(); |
| 57 | // The LoadUpdateDurationUptime() method relies on LoadUpdateTimestampStart() |
| 58 | // being called before it. Don't reorder. |
| 59 | LoadUpdateDurationUptime(); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 60 | for (int i = 0; i < kNumDownloadSources; i++) { |
| 61 | DownloadSource source = static_cast<DownloadSource>(i); |
| 62 | LoadCurrentBytesDownloaded(source); |
| 63 | LoadTotalBytesDownloaded(source); |
| 64 | } |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 65 | LoadNumReboots(); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 66 | LoadNumResponsesSeen(); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 67 | LoadRollbackVersion(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 71 | void PayloadState::SetResponse(const OmahaResponse& omaha_response) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 72 | // Always store the latest response. |
| 73 | response_ = omaha_response; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 74 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 75 | // Compute the candidate URLs first as they are used to calculate the |
| 76 | // response signature so that a change in enterprise policy for |
| 77 | // HTTP downloads being enabled or not could be honored as soon as the |
| 78 | // next update check happens. |
| 79 | ComputeCandidateUrls(); |
| 80 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 81 | // Check if the "signature" of this response (i.e. the fields we care about) |
| 82 | // has changed. |
| 83 | string new_response_signature = CalculateResponseSignature(); |
| 84 | bool has_response_changed = (response_signature_ != new_response_signature); |
| 85 | |
| 86 | // If the response has changed, we should persist the new signature and |
| 87 | // clear away all the existing state. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 88 | if (has_response_changed) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 89 | LOG(INFO) << "Resetting all persisted state as this is a new response"; |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 90 | SetNumResponsesSeen(num_responses_seen_ + 1); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 91 | SetResponseSignature(new_response_signature); |
| 92 | ResetPersistedState(); |
| 93 | return; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 96 | // This is the earliest point at which we can validate whether the URL index |
| 97 | // we loaded from the persisted state is a valid value. If the response |
| 98 | // hasn't changed but the URL index is invalid, it's indicative of some |
| 99 | // tampering of the persisted state. |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 100 | if (static_cast<uint32_t>(url_index_) >= candidate_urls_.size()) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 101 | LOG(INFO) << "Resetting all payload state as the url index seems to have " |
| 102 | "been tampered with"; |
| 103 | ResetPersistedState(); |
| 104 | return; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 105 | } |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 106 | |
| 107 | // Update the current download source which depends on the latest value of |
| 108 | // the response. |
| 109 | UpdateCurrentDownloadSource(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 112 | void PayloadState::DownloadComplete() { |
| 113 | LOG(INFO) << "Payload downloaded successfully"; |
| 114 | IncrementPayloadAttemptNumber(); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 115 | IncrementFullPayloadAttemptNumber(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void PayloadState::DownloadProgress(size_t count) { |
| 119 | if (count == 0) |
| 120 | return; |
| 121 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 122 | CalculateUpdateDurationUptime(); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 123 | UpdateBytesDownloaded(count); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 124 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 125 | // We've received non-zero bytes from a recent download operation. Since our |
| 126 | // URL failure count is meant to penalize a URL only for consecutive |
| 127 | // failures, downloading bytes successfully means we should reset the failure |
| 128 | // count (as we know at least that the URL is working). In future, we can |
| 129 | // design this to be more sophisticated to check for more intelligent failure |
| 130 | // patterns, but right now, even 1 byte downloaded will mark the URL to be |
| 131 | // good unless it hits 10 (or configured number of) consecutive failures |
| 132 | // again. |
| 133 | |
| 134 | if (GetUrlFailureCount() == 0) |
| 135 | return; |
| 136 | |
| 137 | LOG(INFO) << "Resetting failure count of Url" << GetUrlIndex() |
| 138 | << " to 0 as we received " << count << " bytes successfully"; |
| 139 | SetUrlFailureCount(0); |
| 140 | } |
| 141 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 142 | void PayloadState::UpdateResumed() { |
| 143 | LOG(INFO) << "Resuming an update that was previously started."; |
| 144 | UpdateNumReboots(); |
| 145 | } |
| 146 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 147 | void PayloadState::UpdateRestarted() { |
| 148 | LOG(INFO) << "Starting a new update"; |
| 149 | ResetDownloadSourcesOnNewUpdate(); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 150 | SetNumReboots(0); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 153 | void PayloadState::UpdateSucceeded() { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 154 | // Send the relevant metrics that are tracked in this class to UMA. |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 155 | CalculateUpdateDurationUptime(); |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 156 | SetUpdateTimestampEnd(system_state_->clock()->GetWallclockTime()); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 157 | ReportBytesDownloadedMetrics(); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 158 | ReportUpdateUrlSwitchesMetric(); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 159 | ReportRebootMetrics(); |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 160 | ReportDurationMetrics(); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 161 | ReportUpdatesAbandonedCountMetric(); |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 162 | ReportPayloadTypeMetric(); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 163 | ReportAttemptsCountMetrics(); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 164 | |
| 165 | // Reset the number of responses seen since it counts from the last |
| 166 | // successful update, e.g. now. |
| 167 | SetNumResponsesSeen(0); |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 168 | |
| 169 | CreateSystemUpdatedMarkerFile(); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 170 | } |
| 171 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 172 | void PayloadState::UpdateFailed(ErrorCode error) { |
| 173 | ErrorCode base_error = utils::GetBaseErrorCode(error); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 174 | LOG(INFO) << "Updating payload state for error code: " << base_error |
| 175 | << " (" << utils::CodeToString(base_error) << ")"; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 176 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 177 | if (candidate_urls_.size() == 0) { |
| 178 | // This means we got this error even before we got a valid Omaha response |
| 179 | // or don't have any valid candidates in the Omaha response. |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 180 | // So we should not advance the url_index_ in such cases. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 181 | LOG(INFO) << "Ignoring failures until we get a valid Omaha response."; |
| 182 | return; |
| 183 | } |
| 184 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 185 | switch (base_error) { |
| 186 | // Errors which are good indicators of a problem with a particular URL or |
| 187 | // the protocol used in the URL or entities in the communication channel |
| 188 | // (e.g. proxies). We should try the next available URL in the next update |
| 189 | // check to quickly recover from these errors. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 190 | case kErrorCodePayloadHashMismatchError: |
| 191 | case kErrorCodePayloadSizeMismatchError: |
| 192 | case kErrorCodeDownloadPayloadVerificationError: |
| 193 | case kErrorCodeDownloadPayloadPubKeyVerificationError: |
| 194 | case kErrorCodeSignedDeltaPayloadExpectedError: |
| 195 | case kErrorCodeDownloadInvalidMetadataMagicString: |
| 196 | case kErrorCodeDownloadSignatureMissingInManifest: |
| 197 | case kErrorCodeDownloadManifestParseError: |
| 198 | case kErrorCodeDownloadMetadataSignatureError: |
| 199 | case kErrorCodeDownloadMetadataSignatureVerificationError: |
| 200 | case kErrorCodeDownloadMetadataSignatureMismatch: |
| 201 | case kErrorCodeDownloadOperationHashVerificationError: |
| 202 | case kErrorCodeDownloadOperationExecutionError: |
| 203 | case kErrorCodeDownloadOperationHashMismatch: |
| 204 | case kErrorCodeDownloadInvalidMetadataSize: |
| 205 | case kErrorCodeDownloadInvalidMetadataSignature: |
| 206 | case kErrorCodeDownloadOperationHashMissingError: |
| 207 | case kErrorCodeDownloadMetadataSignatureMissingError: |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 208 | case kErrorCodePayloadMismatchedType: |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 209 | IncrementUrlIndex(); |
| 210 | break; |
| 211 | |
| 212 | // Errors which seem to be just transient network/communication related |
| 213 | // failures and do not indicate any inherent problem with the URL itself. |
| 214 | // So, we should keep the current URL but just increment the |
| 215 | // failure count to give it more chances. This way, while we maximize our |
| 216 | // chances of downloading from the URLs that appear earlier in the response |
| 217 | // (because download from a local server URL that appears earlier in a |
| 218 | // response is preferable than downloading from the next URL which could be |
| 219 | // a internet URL and thus could be more expensive). |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 220 | case kErrorCodeError: |
| 221 | case kErrorCodeDownloadTransferError: |
| 222 | case kErrorCodeDownloadWriteError: |
| 223 | case kErrorCodeDownloadStateInitializationError: |
| 224 | case kErrorCodeOmahaErrorInHTTPResponse: // Aggregate code for HTTP errors. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 225 | IncrementFailureCount(); |
| 226 | break; |
| 227 | |
| 228 | // Errors which are not specific to a URL and hence shouldn't result in |
| 229 | // the URL being penalized. This can happen in two cases: |
| 230 | // 1. We haven't started downloading anything: These errors don't cost us |
| 231 | // anything in terms of actual payload bytes, so we should just do the |
| 232 | // regular retries at the next update check. |
| 233 | // 2. We have successfully downloaded the payload: In this case, the |
| 234 | // payload attempt number would have been incremented and would take care |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 235 | // of the backoff at the next update check. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 236 | // In either case, there's no need to update URL index or failure count. |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 237 | case kErrorCodeOmahaRequestError: |
| 238 | case kErrorCodeOmahaResponseHandlerError: |
| 239 | case kErrorCodePostinstallRunnerError: |
| 240 | case kErrorCodeFilesystemCopierError: |
| 241 | case kErrorCodeInstallDeviceOpenError: |
| 242 | case kErrorCodeKernelDeviceOpenError: |
| 243 | case kErrorCodeDownloadNewPartitionInfoError: |
| 244 | case kErrorCodeNewRootfsVerificationError: |
| 245 | case kErrorCodeNewKernelVerificationError: |
| 246 | case kErrorCodePostinstallBootedFromFirmwareB: |
| 247 | case kErrorCodeOmahaRequestEmptyResponseError: |
| 248 | case kErrorCodeOmahaRequestXMLParseError: |
| 249 | case kErrorCodeOmahaResponseInvalid: |
| 250 | case kErrorCodeOmahaUpdateIgnoredPerPolicy: |
| 251 | case kErrorCodeOmahaUpdateDeferredPerPolicy: |
| 252 | case kErrorCodeOmahaUpdateDeferredForBackoff: |
| 253 | case kErrorCodePostinstallPowerwashError: |
| 254 | case kErrorCodeUpdateCanceledByChannelChange: |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 255 | LOG(INFO) << "Not incrementing URL index or failure count for this error"; |
| 256 | break; |
| 257 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 258 | case kErrorCodeSuccess: // success code |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 259 | case kErrorCodeUmaReportedMax: // not an error code |
| 260 | case kErrorCodeOmahaRequestHTTPResponseBase: // aggregated already |
| 261 | case kErrorCodeDevModeFlag: // not an error code |
| 262 | case kErrorCodeResumedFlag: // not an error code |
| 263 | case kErrorCodeTestImageFlag: // not an error code |
| 264 | case kErrorCodeTestOmahaUrlFlag: // not an error code |
| 265 | case kErrorCodeSpecialFlags: // not an error code |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 266 | // These shouldn't happen. Enumerating these explicitly here so that we |
| 267 | // can let the compiler warn about new error codes that are added to |
| 268 | // action_processor.h but not added here. |
| 269 | LOG(WARNING) << "Unexpected error code for UpdateFailed"; |
| 270 | break; |
| 271 | |
| 272 | // Note: Not adding a default here so as to let the compiler warn us of |
| 273 | // any new enums that were added in the .h but not listed in this switch. |
| 274 | } |
| 275 | } |
| 276 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 277 | bool PayloadState::ShouldBackoffDownload() { |
| 278 | if (response_.disable_payload_backoff) { |
| 279 | LOG(INFO) << "Payload backoff logic is disabled. " |
| 280 | "Can proceed with the download"; |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | if (response_.is_delta_payload) { |
| 285 | // If delta payloads fail, we want to fallback quickly to full payloads as |
| 286 | // they are more likely to succeed. Exponential backoffs would greatly |
| 287 | // slow down the fallback to full payloads. So we don't backoff for delta |
| 288 | // payloads. |
| 289 | LOG(INFO) << "No backoffs for delta payloads. " |
| 290 | << "Can proceed with the download"; |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | if (!utils::IsOfficialBuild()) { |
| 295 | // Backoffs are needed only for official builds. We do not want any delays |
| 296 | // or update failures due to backoffs during testing or development. |
| 297 | LOG(INFO) << "No backoffs for test/dev images. " |
| 298 | << "Can proceed with the download"; |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | if (backoff_expiry_time_.is_null()) { |
| 303 | LOG(INFO) << "No backoff expiry time has been set. " |
| 304 | << "Can proceed with the download"; |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | if (backoff_expiry_time_ < Time::Now()) { |
| 309 | LOG(INFO) << "The backoff expiry time (" |
| 310 | << utils::ToString(backoff_expiry_time_) |
| 311 | << ") has elapsed. Can proceed with the download"; |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | LOG(INFO) << "Cannot proceed with downloads as we need to backoff until " |
| 316 | << utils::ToString(backoff_expiry_time_); |
| 317 | return true; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 320 | void PayloadState::Rollback() { |
| 321 | SetRollbackVersion(system_state_->request_params()->app_version()); |
| 322 | } |
| 323 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 324 | void PayloadState::IncrementPayloadAttemptNumber() { |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 325 | // Update the payload attempt number for both payload types: full and delta. |
| 326 | SetPayloadAttemptNumber(GetPayloadAttemptNumber() + 1); |
Alex Deymo | 29b51d9 | 2013-07-09 15:26:24 -0700 | [diff] [blame^] | 327 | |
| 328 | // Report the metric every time the value is incremented. |
| 329 | string metric = "Installer.PayloadAttemptNumber"; |
| 330 | int value = GetPayloadAttemptNumber(); |
| 331 | |
| 332 | LOG(INFO) << "Uploading " << value << " (count) for metric " << metric; |
| 333 | system_state_->metrics_lib()->SendToUMA( |
| 334 | metric, |
| 335 | value, |
| 336 | 1, // min value |
| 337 | 50, // max value |
| 338 | kNumDefaultUmaBuckets); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void PayloadState::IncrementFullPayloadAttemptNumber() { |
| 342 | // Update the payload attempt number for full payloads and the backoff time. |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 343 | if (response_.is_delta_payload) { |
| 344 | LOG(INFO) << "Not incrementing payload attempt number for delta payloads"; |
| 345 | return; |
| 346 | } |
| 347 | |
Alex Deymo | 29b51d9 | 2013-07-09 15:26:24 -0700 | [diff] [blame^] | 348 | LOG(INFO) << "Incrementing the full payload attempt number"; |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 349 | SetFullPayloadAttemptNumber(GetFullPayloadAttemptNumber() + 1); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 350 | UpdateBackoffExpiryTime(); |
Alex Deymo | 29b51d9 | 2013-07-09 15:26:24 -0700 | [diff] [blame^] | 351 | |
| 352 | // Report the metric every time the value is incremented. |
| 353 | string metric = "Installer.FullPayloadAttemptNumber"; |
| 354 | int value = GetFullPayloadAttemptNumber(); |
| 355 | |
| 356 | LOG(INFO) << "Uploading " << value << " (count) for metric " << metric; |
| 357 | system_state_->metrics_lib()->SendToUMA( |
| 358 | metric, |
| 359 | value, |
| 360 | 1, // min value |
| 361 | 50, // max value |
| 362 | kNumDefaultUmaBuckets); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void PayloadState::IncrementUrlIndex() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 366 | uint32_t next_url_index = GetUrlIndex() + 1; |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 367 | if (next_url_index < candidate_urls_.size()) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 368 | LOG(INFO) << "Incrementing the URL index for next attempt"; |
| 369 | SetUrlIndex(next_url_index); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 370 | } else { |
| 371 | LOG(INFO) << "Resetting the current URL index (" << GetUrlIndex() << ") to " |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 372 | << "0 as we only have " << candidate_urls_.size() |
| 373 | << " candidate URL(s)"; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 374 | SetUrlIndex(0); |
Alex Deymo | 29b51d9 | 2013-07-09 15:26:24 -0700 | [diff] [blame^] | 375 | IncrementPayloadAttemptNumber(); |
| 376 | IncrementFullPayloadAttemptNumber(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 377 | } |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 378 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 379 | // If we have multiple URLs, record that we just switched to another one |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 380 | if (candidate_urls_.size() > 1) |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 381 | SetUrlSwitchCount(url_switch_count_ + 1); |
| 382 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 383 | // Whenever we update the URL index, we should also clear the URL failure |
| 384 | // count so we can start over fresh for the new URL. |
| 385 | SetUrlFailureCount(0); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 388 | void PayloadState::IncrementFailureCount() { |
| 389 | uint32_t next_url_failure_count = GetUrlFailureCount() + 1; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 390 | if (next_url_failure_count < response_.max_failure_count_per_url) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 391 | LOG(INFO) << "Incrementing the URL failure count"; |
| 392 | SetUrlFailureCount(next_url_failure_count); |
| 393 | } else { |
| 394 | LOG(INFO) << "Reached max number of failures for Url" << GetUrlIndex() |
| 395 | << ". Trying next available URL"; |
| 396 | IncrementUrlIndex(); |
| 397 | } |
| 398 | } |
| 399 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 400 | void PayloadState::UpdateBackoffExpiryTime() { |
| 401 | if (response_.disable_payload_backoff) { |
| 402 | LOG(INFO) << "Resetting backoff expiry time as payload backoff is disabled"; |
| 403 | SetBackoffExpiryTime(Time()); |
| 404 | return; |
| 405 | } |
| 406 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 407 | if (GetFullPayloadAttemptNumber() == 0) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 408 | SetBackoffExpiryTime(Time()); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | // Since we're doing left-shift below, make sure we don't shift more |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 413 | // than this. E.g. if int is 4-bytes, don't left-shift more than 30 bits, |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 414 | // since we don't expect value of kMaxBackoffDays to be more than 100 anyway. |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 415 | int num_days = 1; // the value to be shifted. |
| 416 | const int kMaxShifts = (sizeof(num_days) * 8) - 2; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 417 | |
| 418 | // Normal backoff days is 2 raised to (payload_attempt_number - 1). |
| 419 | // E.g. if payload_attempt_number is over 30, limit power to 30. |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 420 | int power = min(GetFullPayloadAttemptNumber() - 1, kMaxShifts); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 421 | |
| 422 | // The number of days is the minimum of 2 raised to (payload_attempt_number |
| 423 | // - 1) or kMaxBackoffDays. |
| 424 | num_days = min(num_days << power, kMaxBackoffDays); |
| 425 | |
| 426 | // We don't want all retries to happen exactly at the same time when |
| 427 | // retrying after backoff. So add some random minutes to fuzz. |
| 428 | int fuzz_minutes = utils::FuzzInt(0, kMaxBackoffFuzzMinutes); |
| 429 | TimeDelta next_backoff_interval = TimeDelta::FromDays(num_days) + |
| 430 | TimeDelta::FromMinutes(fuzz_minutes); |
| 431 | LOG(INFO) << "Incrementing the backoff expiry time by " |
| 432 | << utils::FormatTimeDelta(next_backoff_interval); |
| 433 | SetBackoffExpiryTime(Time::Now() + next_backoff_interval); |
| 434 | } |
| 435 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 436 | void PayloadState::UpdateCurrentDownloadSource() { |
| 437 | current_download_source_ = kNumDownloadSources; |
| 438 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 439 | if (GetUrlIndex() < candidate_urls_.size()) { |
| 440 | string current_url = candidate_urls_[GetUrlIndex()]; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 441 | if (StartsWithASCII(current_url, "https://", false)) |
| 442 | current_download_source_ = kDownloadSourceHttpsServer; |
| 443 | else if (StartsWithASCII(current_url, "http://", false)) |
| 444 | current_download_source_ = kDownloadSourceHttpServer; |
| 445 | } |
| 446 | |
| 447 | LOG(INFO) << "Current download source: " |
| 448 | << utils::ToString(current_download_source_); |
| 449 | } |
| 450 | |
| 451 | void PayloadState::UpdateBytesDownloaded(size_t count) { |
| 452 | SetCurrentBytesDownloaded( |
| 453 | current_download_source_, |
| 454 | GetCurrentBytesDownloaded(current_download_source_) + count, |
| 455 | false); |
| 456 | SetTotalBytesDownloaded( |
| 457 | current_download_source_, |
| 458 | GetTotalBytesDownloaded(current_download_source_) + count, |
| 459 | false); |
| 460 | } |
| 461 | |
| 462 | void PayloadState::ReportBytesDownloadedMetrics() { |
| 463 | // Report metrics collected from all known download sources to UMA. |
| 464 | // The reported data is in Megabytes in order to represent a larger |
| 465 | // sample range. |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 466 | int download_sources_used = 0; |
| 467 | string metric; |
| 468 | uint64_t successful_mbs = 0; |
| 469 | uint64_t total_mbs = 0; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 470 | for (int i = 0; i < kNumDownloadSources; i++) { |
| 471 | DownloadSource source = static_cast<DownloadSource>(i); |
| 472 | const int kMaxMiBs = 10240; // Anything above 10GB goes in the last bucket. |
David Zeuthen | 4484860 | 2013-06-24 13:32:14 -0700 | [diff] [blame] | 473 | uint64_t mbs; |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 474 | |
David Zeuthen | 4484860 | 2013-06-24 13:32:14 -0700 | [diff] [blame] | 475 | // Only consider this download source (and send byte counts) as |
| 476 | // having been used if we downloaded a non-trivial amount of bytes |
| 477 | // (e.g. at least 1 MiB) that contributed to the final success of |
| 478 | // the update. Otherwise we're going to end up with a lot of |
| 479 | // zero-byte events in the histogram. |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 480 | |
David Zeuthen | 4484860 | 2013-06-24 13:32:14 -0700 | [diff] [blame] | 481 | mbs = GetCurrentBytesDownloaded(source) / kNumBytesInOneMiB; |
| 482 | if (mbs > 0) { |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 483 | download_sources_used |= (1 << source); |
| 484 | |
David Zeuthen | 4484860 | 2013-06-24 13:32:14 -0700 | [diff] [blame] | 485 | metric = "Installer.SuccessfulMBsDownloadedFrom" + |
| 486 | utils::ToString(source); |
| 487 | successful_mbs += mbs; |
| 488 | LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric; |
| 489 | system_state_->metrics_lib()->SendToUMA(metric, |
| 490 | mbs, |
| 491 | 0, // min |
| 492 | kMaxMiBs, |
| 493 | kNumDefaultUmaBuckets); |
| 494 | } |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 495 | SetCurrentBytesDownloaded(source, 0, true); |
| 496 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 497 | mbs = GetTotalBytesDownloaded(source) / kNumBytesInOneMiB; |
David Zeuthen | 4484860 | 2013-06-24 13:32:14 -0700 | [diff] [blame] | 498 | if (mbs > 0) { |
| 499 | metric = "Installer.TotalMBsDownloadedFrom" + utils::ToString(source); |
| 500 | total_mbs += mbs; |
| 501 | LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric; |
| 502 | system_state_->metrics_lib()->SendToUMA(metric, |
| 503 | mbs, |
| 504 | 0, // min |
| 505 | kMaxMiBs, |
| 506 | kNumDefaultUmaBuckets); |
| 507 | } |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 508 | SetTotalBytesDownloaded(source, 0, true); |
| 509 | } |
Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 510 | |
| 511 | metric = "Installer.DownloadSourcesUsed"; |
| 512 | LOG(INFO) << "Uploading 0x" << std::hex << download_sources_used |
| 513 | << " (bit flags) for metric " << metric; |
| 514 | int num_buckets = std::min(1 << kNumDownloadSources, kNumDefaultUmaBuckets); |
| 515 | system_state_->metrics_lib()->SendToUMA(metric, |
| 516 | download_sources_used, |
| 517 | 0, // min |
| 518 | 1 << kNumDownloadSources, |
| 519 | num_buckets); |
| 520 | |
| 521 | if (successful_mbs) { |
| 522 | metric = "Installer.DownloadOverheadPercentage"; |
| 523 | int percent_overhead = (total_mbs - successful_mbs) * 100 / successful_mbs; |
| 524 | LOG(INFO) << "Uploading " << percent_overhead << "% for metric " << metric; |
| 525 | system_state_->metrics_lib()->SendToUMA(metric, |
| 526 | percent_overhead, |
| 527 | 0, // min: 0% overhead |
| 528 | 1000, // max: 1000% overhead |
| 529 | kNumDefaultUmaBuckets); |
| 530 | } |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 531 | } |
| 532 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 533 | void PayloadState::ReportUpdateUrlSwitchesMetric() { |
| 534 | string metric = "Installer.UpdateURLSwitches"; |
| 535 | int value = static_cast<int>(url_switch_count_); |
| 536 | |
| 537 | LOG(INFO) << "Uploading " << value << " (count) for metric " << metric; |
| 538 | system_state_->metrics_lib()->SendToUMA( |
| 539 | metric, |
| 540 | value, |
| 541 | 0, // min value |
| 542 | 100, // max value |
| 543 | kNumDefaultUmaBuckets); |
| 544 | } |
| 545 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 546 | void PayloadState::ReportRebootMetrics() { |
| 547 | // Report the number of num_reboots. |
| 548 | string metric = "Installer.UpdateNumReboots"; |
| 549 | uint32_t num_reboots = GetNumReboots(); |
| 550 | LOG(INFO) << "Uploading reboot count of " << num_reboots << " for metric " |
| 551 | << metric; |
| 552 | system_state_->metrics_lib()->SendToUMA( |
| 553 | metric, |
| 554 | static_cast<int>(num_reboots), // sample |
| 555 | 0, // min = 0. |
| 556 | 50, // max |
| 557 | 25); // buckets |
| 558 | SetNumReboots(0); |
| 559 | } |
| 560 | |
| 561 | void PayloadState::UpdateNumReboots() { |
| 562 | // We only update the reboot count when the system has been detected to have |
| 563 | // been rebooted. |
| 564 | if (!system_state_->system_rebooted()) { |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | SetNumReboots(GetNumReboots() + 1); |
| 569 | } |
| 570 | |
| 571 | void PayloadState::SetNumReboots(uint32_t num_reboots) { |
| 572 | CHECK(prefs_); |
| 573 | num_reboots_ = num_reboots; |
| 574 | prefs_->SetInt64(kPrefsNumReboots, num_reboots); |
| 575 | LOG(INFO) << "Number of Reboots during current update attempt = " |
| 576 | << num_reboots_; |
| 577 | } |
| 578 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 579 | void PayloadState::ResetPersistedState() { |
| 580 | SetPayloadAttemptNumber(0); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 581 | SetFullPayloadAttemptNumber(0); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 582 | SetUrlIndex(0); |
| 583 | SetUrlFailureCount(0); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 584 | SetUrlSwitchCount(0); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 585 | UpdateBackoffExpiryTime(); // This will reset the backoff expiry time. |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 586 | SetUpdateTimestampStart(system_state_->clock()->GetWallclockTime()); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 587 | SetUpdateTimestampEnd(Time()); // Set to null time |
| 588 | SetUpdateDurationUptime(TimeDelta::FromSeconds(0)); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 589 | ResetDownloadSourcesOnNewUpdate(); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 590 | ResetRollbackVersion(); |
| 591 | } |
| 592 | |
| 593 | void PayloadState::ResetRollbackVersion() { |
| 594 | CHECK(powerwash_safe_prefs_); |
| 595 | rollback_version_ = ""; |
| 596 | powerwash_safe_prefs_->Delete(kPrefsRollbackVersion); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void PayloadState::ResetDownloadSourcesOnNewUpdate() { |
| 600 | for (int i = 0; i < kNumDownloadSources; i++) { |
| 601 | DownloadSource source = static_cast<DownloadSource>(i); |
| 602 | SetCurrentBytesDownloaded(source, 0, true); |
| 603 | // Note: Not resetting the TotalBytesDownloaded as we want that metric |
| 604 | // to count the bytes downloaded across various update attempts until |
| 605 | // we have successfully applied the update. |
| 606 | } |
| 607 | } |
| 608 | |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 609 | int64_t PayloadState::GetPersistedValue(const string& key, |
| 610 | bool across_powerwash) { |
| 611 | PrefsInterface* prefs = prefs_; |
| 612 | if (across_powerwash) |
| 613 | prefs = powerwash_safe_prefs_; |
| 614 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 615 | CHECK(prefs_); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 616 | if (!prefs->Exists(key)) |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 617 | return 0; |
| 618 | |
| 619 | int64_t stored_value; |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 620 | if (!prefs->GetInt64(key, &stored_value)) |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 621 | return 0; |
| 622 | |
| 623 | if (stored_value < 0) { |
| 624 | LOG(ERROR) << key << ": Invalid value (" << stored_value |
| 625 | << ") in persisted state. Defaulting to 0"; |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | return stored_value; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | string PayloadState::CalculateResponseSignature() { |
| 633 | string response_sign = StringPrintf("NumURLs = %d\n", |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 634 | candidate_urls_.size()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 635 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 636 | for (size_t i = 0; i < candidate_urls_.size(); i++) |
| 637 | response_sign += StringPrintf("Candidate Url%d = %s\n", |
| 638 | i, candidate_urls_[i].c_str()); |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 639 | |
| 640 | response_sign += StringPrintf("Payload Size = %llu\n" |
| 641 | "Payload Sha256 Hash = %s\n" |
| 642 | "Metadata Size = %llu\n" |
| 643 | "Metadata Signature = %s\n" |
| 644 | "Is Delta Payload = %d\n" |
| 645 | "Max Failure Count Per Url = %d\n" |
| 646 | "Disable Payload Backoff = %d\n", |
| 647 | response_.size, |
| 648 | response_.hash.c_str(), |
| 649 | response_.metadata_size, |
| 650 | response_.metadata_signature.c_str(), |
| 651 | response_.is_delta_payload, |
| 652 | response_.max_failure_count_per_url, |
| 653 | response_.disable_payload_backoff); |
| 654 | return response_sign; |
| 655 | } |
| 656 | |
| 657 | void PayloadState::LoadResponseSignature() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 658 | CHECK(prefs_); |
| 659 | string stored_value; |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 660 | if (prefs_->Exists(kPrefsCurrentResponseSignature) && |
| 661 | prefs_->GetString(kPrefsCurrentResponseSignature, &stored_value)) { |
| 662 | SetResponseSignature(stored_value); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 663 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 666 | void PayloadState::SetResponseSignature(const string& response_signature) { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 667 | CHECK(prefs_); |
| 668 | response_signature_ = response_signature; |
| 669 | LOG(INFO) << "Current Response Signature = \n" << response_signature_; |
| 670 | prefs_->SetString(kPrefsCurrentResponseSignature, response_signature_); |
| 671 | } |
| 672 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 673 | void PayloadState::LoadPayloadAttemptNumber() { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 674 | SetPayloadAttemptNumber(GetPersistedValue(kPrefsPayloadAttemptNumber, |
| 675 | false)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 676 | } |
| 677 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 678 | void PayloadState::LoadFullPayloadAttemptNumber() { |
| 679 | SetFullPayloadAttemptNumber(GetPersistedValue(kPrefsFullPayloadAttemptNumber, |
| 680 | false)); |
| 681 | } |
| 682 | |
| 683 | void PayloadState::SetPayloadAttemptNumber(int payload_attempt_number) { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 684 | CHECK(prefs_); |
| 685 | payload_attempt_number_ = payload_attempt_number; |
| 686 | LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number_; |
| 687 | prefs_->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number_); |
| 688 | } |
| 689 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 690 | void PayloadState::SetFullPayloadAttemptNumber( |
| 691 | int full_payload_attempt_number) { |
| 692 | CHECK(prefs_); |
| 693 | full_payload_attempt_number_ = full_payload_attempt_number; |
| 694 | LOG(INFO) << "Full Payload Attempt Number = " << full_payload_attempt_number_; |
| 695 | prefs_->SetInt64(kPrefsFullPayloadAttemptNumber, |
| 696 | full_payload_attempt_number_); |
| 697 | } |
| 698 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 699 | void PayloadState::LoadUrlIndex() { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 700 | SetUrlIndex(GetPersistedValue(kPrefsCurrentUrlIndex, false)); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | void PayloadState::SetUrlIndex(uint32_t url_index) { |
| 704 | CHECK(prefs_); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 705 | url_index_ = url_index; |
| 706 | LOG(INFO) << "Current URL Index = " << url_index_; |
| 707 | prefs_->SetInt64(kPrefsCurrentUrlIndex, url_index_); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 708 | |
| 709 | // Also update the download source, which is purely dependent on the |
| 710 | // current URL index alone. |
| 711 | UpdateCurrentDownloadSource(); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 712 | } |
| 713 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 714 | void PayloadState::LoadUrlSwitchCount() { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 715 | SetUrlSwitchCount(GetPersistedValue(kPrefsUrlSwitchCount, false)); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | void PayloadState::SetUrlSwitchCount(uint32_t url_switch_count) { |
| 719 | CHECK(prefs_); |
| 720 | url_switch_count_ = url_switch_count; |
| 721 | LOG(INFO) << "URL Switch Count = " << url_switch_count_; |
| 722 | prefs_->SetInt64(kPrefsUrlSwitchCount, url_switch_count_); |
| 723 | } |
| 724 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 725 | void PayloadState::LoadUrlFailureCount() { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 726 | SetUrlFailureCount(GetPersistedValue(kPrefsCurrentUrlFailureCount, |
| 727 | false)); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | void PayloadState::SetUrlFailureCount(uint32_t url_failure_count) { |
| 731 | CHECK(prefs_); |
| 732 | url_failure_count_ = url_failure_count; |
| 733 | LOG(INFO) << "Current URL (Url" << GetUrlIndex() |
| 734 | << ")'s Failure Count = " << url_failure_count_; |
| 735 | prefs_->SetInt64(kPrefsCurrentUrlFailureCount, url_failure_count_); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 736 | } |
| 737 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 738 | void PayloadState::LoadBackoffExpiryTime() { |
| 739 | CHECK(prefs_); |
| 740 | int64_t stored_value; |
| 741 | if (!prefs_->Exists(kPrefsBackoffExpiryTime)) |
| 742 | return; |
| 743 | |
| 744 | if (!prefs_->GetInt64(kPrefsBackoffExpiryTime, &stored_value)) |
| 745 | return; |
| 746 | |
| 747 | Time stored_time = Time::FromInternalValue(stored_value); |
| 748 | if (stored_time > Time::Now() + TimeDelta::FromDays(kMaxBackoffDays)) { |
| 749 | LOG(ERROR) << "Invalid backoff expiry time (" |
| 750 | << utils::ToString(stored_time) |
| 751 | << ") in persisted state. Resetting."; |
| 752 | stored_time = Time(); |
| 753 | } |
| 754 | SetBackoffExpiryTime(stored_time); |
| 755 | } |
| 756 | |
| 757 | void PayloadState::SetBackoffExpiryTime(const Time& new_time) { |
| 758 | CHECK(prefs_); |
| 759 | backoff_expiry_time_ = new_time; |
| 760 | LOG(INFO) << "Backoff Expiry Time = " |
| 761 | << utils::ToString(backoff_expiry_time_); |
| 762 | prefs_->SetInt64(kPrefsBackoffExpiryTime, |
| 763 | backoff_expiry_time_.ToInternalValue()); |
| 764 | } |
| 765 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 766 | TimeDelta PayloadState::GetUpdateDuration() { |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 767 | Time end_time = update_timestamp_end_.is_null() |
| 768 | ? system_state_->clock()->GetWallclockTime() : |
| 769 | update_timestamp_end_; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 770 | return end_time - update_timestamp_start_; |
| 771 | } |
| 772 | |
| 773 | void PayloadState::LoadUpdateTimestampStart() { |
| 774 | int64_t stored_value; |
| 775 | Time stored_time; |
| 776 | |
| 777 | CHECK(prefs_); |
| 778 | |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 779 | Time now = system_state_->clock()->GetWallclockTime(); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 780 | |
| 781 | if (!prefs_->Exists(kPrefsUpdateTimestampStart)) { |
| 782 | // The preference missing is not unexpected - in that case, just |
| 783 | // use the current time as start time |
| 784 | stored_time = now; |
| 785 | } else if (!prefs_->GetInt64(kPrefsUpdateTimestampStart, &stored_value)) { |
| 786 | LOG(ERROR) << "Invalid UpdateTimestampStart value. Resetting."; |
| 787 | stored_time = now; |
| 788 | } else { |
| 789 | stored_time = Time::FromInternalValue(stored_value); |
| 790 | } |
| 791 | |
| 792 | // Sanity check: If the time read from disk is in the future |
| 793 | // (modulo some slack to account for possible NTP drift |
| 794 | // adjustments), something is fishy and we should report and |
| 795 | // reset. |
| 796 | TimeDelta duration_according_to_stored_time = now - stored_time; |
| 797 | if (duration_according_to_stored_time < -kDurationSlack) { |
| 798 | LOG(ERROR) << "The UpdateTimestampStart value (" |
| 799 | << utils::ToString(stored_time) |
| 800 | << ") in persisted state is " |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 801 | << utils::FormatTimeDelta(duration_according_to_stored_time) |
| 802 | << " in the future. Resetting."; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 803 | stored_time = now; |
| 804 | } |
| 805 | |
| 806 | SetUpdateTimestampStart(stored_time); |
| 807 | } |
| 808 | |
| 809 | void PayloadState::SetUpdateTimestampStart(const Time& value) { |
| 810 | CHECK(prefs_); |
| 811 | update_timestamp_start_ = value; |
| 812 | prefs_->SetInt64(kPrefsUpdateTimestampStart, |
| 813 | update_timestamp_start_.ToInternalValue()); |
| 814 | LOG(INFO) << "Update Timestamp Start = " |
| 815 | << utils::ToString(update_timestamp_start_); |
| 816 | } |
| 817 | |
| 818 | void PayloadState::SetUpdateTimestampEnd(const Time& value) { |
| 819 | update_timestamp_end_ = value; |
| 820 | LOG(INFO) << "Update Timestamp End = " |
| 821 | << utils::ToString(update_timestamp_end_); |
| 822 | } |
| 823 | |
| 824 | TimeDelta PayloadState::GetUpdateDurationUptime() { |
| 825 | return update_duration_uptime_; |
| 826 | } |
| 827 | |
| 828 | void PayloadState::LoadUpdateDurationUptime() { |
| 829 | int64_t stored_value; |
| 830 | TimeDelta stored_delta; |
| 831 | |
| 832 | CHECK(prefs_); |
| 833 | |
| 834 | if (!prefs_->Exists(kPrefsUpdateDurationUptime)) { |
| 835 | // The preference missing is not unexpected - in that case, just |
| 836 | // we'll use zero as the delta |
| 837 | } else if (!prefs_->GetInt64(kPrefsUpdateDurationUptime, &stored_value)) { |
| 838 | LOG(ERROR) << "Invalid UpdateDurationUptime value. Resetting."; |
| 839 | stored_delta = TimeDelta::FromSeconds(0); |
| 840 | } else { |
| 841 | stored_delta = TimeDelta::FromInternalValue(stored_value); |
| 842 | } |
| 843 | |
| 844 | // Sanity-check: Uptime can never be greater than the wall-clock |
| 845 | // difference (modulo some slack). If it is, report and reset |
| 846 | // to the wall-clock difference. |
| 847 | TimeDelta diff = GetUpdateDuration() - stored_delta; |
| 848 | if (diff < -kDurationSlack) { |
| 849 | LOG(ERROR) << "The UpdateDurationUptime value (" |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 850 | << utils::FormatTimeDelta(stored_delta) |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 851 | << ") in persisted state is " |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 852 | << utils::FormatTimeDelta(diff) |
| 853 | << " larger than the wall-clock delta. Resetting."; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 854 | stored_delta = update_duration_current_; |
| 855 | } |
| 856 | |
| 857 | SetUpdateDurationUptime(stored_delta); |
| 858 | } |
| 859 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 860 | void PayloadState::LoadNumReboots() { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 861 | SetNumReboots(GetPersistedValue(kPrefsNumReboots, false)); |
| 862 | } |
| 863 | |
| 864 | void PayloadState::LoadRollbackVersion() { |
| 865 | SetNumReboots(GetPersistedValue(kPrefsRollbackVersion, true)); |
| 866 | } |
| 867 | |
| 868 | void PayloadState::SetRollbackVersion(const string& rollback_version) { |
| 869 | CHECK(powerwash_safe_prefs_); |
| 870 | LOG(INFO) << "Blacklisting version "<< rollback_version; |
| 871 | rollback_version_ = rollback_version; |
| 872 | powerwash_safe_prefs_->SetString(kPrefsRollbackVersion, rollback_version); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 873 | } |
| 874 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 875 | void PayloadState::SetUpdateDurationUptimeExtended(const TimeDelta& value, |
| 876 | const Time& timestamp, |
| 877 | bool use_logging) { |
| 878 | CHECK(prefs_); |
| 879 | update_duration_uptime_ = value; |
| 880 | update_duration_uptime_timestamp_ = timestamp; |
| 881 | prefs_->SetInt64(kPrefsUpdateDurationUptime, |
| 882 | update_duration_uptime_.ToInternalValue()); |
| 883 | if (use_logging) { |
| 884 | LOG(INFO) << "Update Duration Uptime = " |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 885 | << utils::FormatTimeDelta(update_duration_uptime_); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
| 889 | void PayloadState::SetUpdateDurationUptime(const TimeDelta& value) { |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 890 | Time now = system_state_->clock()->GetMonotonicTime(); |
| 891 | SetUpdateDurationUptimeExtended(value, now, true); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | void PayloadState::CalculateUpdateDurationUptime() { |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 895 | Time now = system_state_->clock()->GetMonotonicTime(); |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 896 | TimeDelta uptime_since_last_update = now - update_duration_uptime_timestamp_; |
| 897 | TimeDelta new_uptime = update_duration_uptime_ + uptime_since_last_update; |
| 898 | // We're frequently called so avoid logging this write |
| 899 | SetUpdateDurationUptimeExtended(new_uptime, now, false); |
| 900 | } |
| 901 | |
David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 902 | void PayloadState::ReportDurationMetrics() { |
| 903 | TimeDelta duration = GetUpdateDuration(); |
| 904 | TimeDelta duration_uptime = GetUpdateDurationUptime(); |
| 905 | string metric; |
| 906 | |
| 907 | metric = "Installer.UpdateDurationMinutes"; |
| 908 | system_state_->metrics_lib()->SendToUMA( |
| 909 | metric, |
| 910 | static_cast<int>(duration.InMinutes()), |
| 911 | 1, // min: 1 minute |
| 912 | 365*24*60, // max: 1 year (approx) |
| 913 | kNumDefaultUmaBuckets); |
| 914 | LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration) |
| 915 | << " for metric " << metric; |
| 916 | |
| 917 | metric = "Installer.UpdateDurationUptimeMinutes"; |
| 918 | system_state_->metrics_lib()->SendToUMA( |
| 919 | metric, |
| 920 | static_cast<int>(duration_uptime.InMinutes()), |
| 921 | 1, // min: 1 minute |
| 922 | 30*24*60, // max: 1 month (approx) |
| 923 | kNumDefaultUmaBuckets); |
| 924 | LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration_uptime) |
| 925 | << " for metric " << metric; |
| 926 | |
| 927 | prefs_->Delete(kPrefsUpdateTimestampStart); |
| 928 | prefs_->Delete(kPrefsUpdateDurationUptime); |
| 929 | } |
| 930 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 931 | void PayloadState::ReportPayloadTypeMetric() { |
| 932 | string metric; |
| 933 | PayloadType uma_payload_type; |
| 934 | OmahaRequestParams* params = system_state_->request_params(); |
| 935 | |
| 936 | if (response_.is_delta_payload) { |
| 937 | uma_payload_type = kPayloadTypeDelta; |
| 938 | } else if (params->delta_okay()) { |
| 939 | uma_payload_type = kPayloadTypeFull; |
| 940 | } else { // Full payload, delta was not allowed by request. |
| 941 | uma_payload_type = kPayloadTypeForcedFull; |
| 942 | } |
| 943 | |
| 944 | metric = "Installer.PayloadFormat"; |
| 945 | system_state_->metrics_lib()->SendEnumToUMA( |
| 946 | metric, |
| 947 | uma_payload_type, |
| 948 | kNumPayloadTypes); |
| 949 | LOG(INFO) << "Uploading " << utils::ToString(uma_payload_type) |
| 950 | << " for metric " << metric; |
| 951 | } |
| 952 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 953 | void PayloadState::ReportAttemptsCountMetrics() { |
| 954 | string metric; |
| 955 | int total_attempts = GetPayloadAttemptNumber(); |
| 956 | |
| 957 | metric = "Installer.AttemptsCount.Total"; |
| 958 | system_state_->metrics_lib()->SendToUMA( |
| 959 | metric, |
| 960 | total_attempts, |
| 961 | 1, // min |
| 962 | 50, // max |
| 963 | kNumDefaultUmaBuckets); |
| 964 | LOG(INFO) << "Uploading " << total_attempts |
| 965 | << " for metric " << metric; |
| 966 | } |
| 967 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 968 | string PayloadState::GetPrefsKey(const string& prefix, DownloadSource source) { |
| 969 | return prefix + "-from-" + utils::ToString(source); |
| 970 | } |
| 971 | |
| 972 | void PayloadState::LoadCurrentBytesDownloaded(DownloadSource source) { |
| 973 | string key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 974 | SetCurrentBytesDownloaded(source, GetPersistedValue(key, false), true); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | void PayloadState::SetCurrentBytesDownloaded( |
| 978 | DownloadSource source, |
| 979 | uint64_t current_bytes_downloaded, |
| 980 | bool log) { |
| 981 | CHECK(prefs_); |
| 982 | |
| 983 | if (source >= kNumDownloadSources) |
| 984 | return; |
| 985 | |
| 986 | // Update the in-memory value. |
| 987 | current_bytes_downloaded_[source] = current_bytes_downloaded; |
| 988 | |
| 989 | string prefs_key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source); |
| 990 | prefs_->SetInt64(prefs_key, current_bytes_downloaded); |
| 991 | LOG_IF(INFO, log) << "Current bytes downloaded for " |
| 992 | << utils::ToString(source) << " = " |
| 993 | << GetCurrentBytesDownloaded(source); |
| 994 | } |
| 995 | |
| 996 | void PayloadState::LoadTotalBytesDownloaded(DownloadSource source) { |
| 997 | string key = GetPrefsKey(kPrefsTotalBytesDownloaded, source); |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 998 | SetTotalBytesDownloaded(source, GetPersistedValue(key, false), true); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | void PayloadState::SetTotalBytesDownloaded( |
| 1002 | DownloadSource source, |
| 1003 | uint64_t total_bytes_downloaded, |
| 1004 | bool log) { |
| 1005 | CHECK(prefs_); |
| 1006 | |
| 1007 | if (source >= kNumDownloadSources) |
| 1008 | return; |
| 1009 | |
| 1010 | // Update the in-memory value. |
| 1011 | total_bytes_downloaded_[source] = total_bytes_downloaded; |
| 1012 | |
| 1013 | // Persist. |
| 1014 | string prefs_key = GetPrefsKey(kPrefsTotalBytesDownloaded, source); |
| 1015 | prefs_->SetInt64(prefs_key, total_bytes_downloaded); |
| 1016 | LOG_IF(INFO, log) << "Total bytes downloaded for " |
| 1017 | << utils::ToString(source) << " = " |
| 1018 | << GetTotalBytesDownloaded(source); |
| 1019 | } |
| 1020 | |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 1021 | void PayloadState::LoadNumResponsesSeen() { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 1022 | SetNumResponsesSeen(GetPersistedValue(kPrefsNumResponsesSeen, false)); |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | void PayloadState::SetNumResponsesSeen(int num_responses_seen) { |
| 1026 | CHECK(prefs_); |
| 1027 | num_responses_seen_ = num_responses_seen; |
| 1028 | LOG(INFO) << "Num Responses Seen = " << num_responses_seen_; |
| 1029 | prefs_->SetInt64(kPrefsNumResponsesSeen, num_responses_seen_); |
| 1030 | } |
| 1031 | |
| 1032 | void PayloadState::ReportUpdatesAbandonedCountMetric() { |
| 1033 | string metric = "Installer.UpdatesAbandonedCount"; |
| 1034 | int value = num_responses_seen_ - 1; |
| 1035 | |
| 1036 | LOG(INFO) << "Uploading " << value << " (count) for metric " << metric; |
| 1037 | system_state_->metrics_lib()->SendToUMA( |
| 1038 | metric, |
| 1039 | value, |
| 1040 | 0, // min value |
| 1041 | 100, // max value |
| 1042 | kNumDefaultUmaBuckets); |
| 1043 | } |
| 1044 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1045 | void PayloadState::ComputeCandidateUrls() { |
| 1046 | bool http_url_ok = false; |
| 1047 | |
| 1048 | if (system_state_->IsOfficialBuild()) { |
| 1049 | const policy::DevicePolicy* policy = system_state_->device_policy(); |
| 1050 | if (!(policy && policy->GetHttpDownloadsEnabled(&http_url_ok) && |
| 1051 | http_url_ok)) |
| 1052 | LOG(INFO) << "Downloads via HTTP Url are not enabled by device policy"; |
| 1053 | } else { |
| 1054 | LOG(INFO) << "Allowing HTTP downloads for unofficial builds"; |
| 1055 | http_url_ok = true; |
| 1056 | } |
| 1057 | |
| 1058 | candidate_urls_.clear(); |
| 1059 | for (size_t i = 0; i < response_.payload_urls.size(); i++) { |
| 1060 | string candidate_url = response_.payload_urls[i]; |
| 1061 | if (StartsWithASCII(candidate_url, "http://", false) && !http_url_ok) |
| 1062 | continue; |
| 1063 | candidate_urls_.push_back(candidate_url); |
| 1064 | LOG(INFO) << "Candidate Url" << (candidate_urls_.size() - 1) |
| 1065 | << ": " << candidate_url; |
| 1066 | } |
| 1067 | |
| 1068 | LOG(INFO) << "Found " << candidate_urls_.size() << " candidate URLs " |
| 1069 | << "out of " << response_.payload_urls.size() << " URLs supplied"; |
| 1070 | } |
| 1071 | |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1072 | void PayloadState::CreateSystemUpdatedMarkerFile() { |
| 1073 | CHECK(prefs_); |
| 1074 | int64_t value = system_state_->clock()->GetWallclockTime().ToInternalValue(); |
| 1075 | prefs_->SetInt64(kPrefsSystemUpdatedMarker, value); |
| 1076 | } |
| 1077 | |
| 1078 | void PayloadState::BootedIntoUpdate(TimeDelta time_to_reboot) { |
| 1079 | // Send |time_to_reboot| as a UMA stat. |
| 1080 | string metric = "Installer.TimeToRebootMinutes"; |
| 1081 | system_state_->metrics_lib()->SendToUMA(metric, |
| 1082 | time_to_reboot.InMinutes(), |
| 1083 | 0, // min: 0 minute |
| 1084 | 30*24*60, // max: 1 month (approx) |
| 1085 | kNumDefaultUmaBuckets); |
| 1086 | LOG(INFO) << "Uploading " << utils::FormatTimeDelta(time_to_reboot) |
| 1087 | << " for metric " << metric; |
| 1088 | } |
| 1089 | |
| 1090 | void PayloadState::UpdateEngineStarted() { |
| 1091 | // Figure out if we just booted into a new update |
| 1092 | if (prefs_->Exists(kPrefsSystemUpdatedMarker)) { |
| 1093 | int64_t stored_value; |
| 1094 | if (prefs_->GetInt64(kPrefsSystemUpdatedMarker, &stored_value)) { |
| 1095 | Time system_updated_at = Time::FromInternalValue(stored_value); |
| 1096 | if (!system_updated_at.is_null()) { |
| 1097 | TimeDelta time_to_reboot = |
| 1098 | system_state_->clock()->GetWallclockTime() - system_updated_at; |
| 1099 | if (time_to_reboot.ToInternalValue() < 0) { |
| 1100 | LOG(ERROR) << "time_to_reboot is negative - system_updated_at: " |
| 1101 | << utils::ToString(system_updated_at); |
| 1102 | } else { |
| 1103 | BootedIntoUpdate(time_to_reboot); |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | prefs_->Delete(kPrefsSystemUpdatedMarker); |
| 1108 | } |
| 1109 | } |
| 1110 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1111 | } // namespace chromeos_update_engine |