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