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