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