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