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