blob: 642264d9a22517d5601a34b2e259eb7a5d501974 [file] [log] [blame]
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/payload_state.h"
6
Jay Srinivasan08262882012-12-28 19:29:43 -08007#include <algorithm>
8
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08009#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070010#include <base/strings/string_util.h>
11#include <base/strings/stringprintf.h>
12#include <base/format_macros.h>
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080013
David Zeuthenf413fe52013-04-22 14:04:39 -070014#include "update_engine/clock.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070015#include "update_engine/constants.h"
Alex Deymo42432912013-07-12 20:21:15 -070016#include "update_engine/hardware_interface.h"
17#include "update_engine/install_plan.h"
Jay Srinivasan19409b72013-04-12 19:23:36 -070018#include "update_engine/prefs.h"
19#include "update_engine/system_state.h"
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080020#include "update_engine/utils.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080021
Jay Srinivasan08262882012-12-28 19:29:43 -080022using base::Time;
23using base::TimeDelta;
24using std::min;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080025using std::string;
26
27namespace chromeos_update_engine {
28
David Zeuthen9a017f22013-04-11 16:10:26 -070029const TimeDelta PayloadState::kDurationSlack = TimeDelta::FromSeconds(600);
30
Jay Srinivasan08262882012-12-28 19:29:43 -080031// We want to upperbound backoffs to 16 days
Alex Deymo820cc702013-06-28 15:43:46 -070032static const int kMaxBackoffDays = 16;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080033
Jay Srinivasan08262882012-12-28 19:29:43 -080034// We want to randomize retry attempts after the backoff by +/- 6 hours.
35static const uint32_t kMaxBackoffFuzzMinutes = 12 * 60;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080036
Jay Srinivasan19409b72013-04-12 19:23:36 -070037PayloadState::PayloadState()
38 : prefs_(NULL),
David Zeuthenbb8bdc72013-09-03 13:43:48 -070039 using_p2p_for_downloading_(false),
Jay Srinivasan19409b72013-04-12 19:23:36 -070040 payload_attempt_number_(0),
Alex Deymo820cc702013-06-28 15:43:46 -070041 full_payload_attempt_number_(0),
Jay Srinivasan19409b72013-04-12 19:23:36 -070042 url_index_(0),
David Zeuthencc6f9962013-04-18 11:57:24 -070043 url_failure_count_(0),
David Zeuthendcba8092013-08-06 12:16:35 -070044 url_switch_count_(0),
45 p2p_num_attempts_(0) {
Jay Srinivasan19409b72013-04-12 19:23:36 -070046 for (int i = 0; i <= kNumDownloadSources; i++)
47 total_bytes_downloaded_[i] = current_bytes_downloaded_[i] = 0;
48}
49
50bool PayloadState::Initialize(SystemState* system_state) {
51 system_state_ = system_state;
52 prefs_ = system_state_->prefs();
Chris Sosaaa18e162013-06-20 13:20:30 -070053 powerwash_safe_prefs_ = system_state_->powerwash_safe_prefs();
Jay Srinivasan08262882012-12-28 19:29:43 -080054 LoadResponseSignature();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080055 LoadPayloadAttemptNumber();
Alex Deymo820cc702013-06-28 15:43:46 -070056 LoadFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080057 LoadUrlIndex();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080058 LoadUrlFailureCount();
David Zeuthencc6f9962013-04-18 11:57:24 -070059 LoadUrlSwitchCount();
Jay Srinivasan08262882012-12-28 19:29:43 -080060 LoadBackoffExpiryTime();
David Zeuthen9a017f22013-04-11 16:10:26 -070061 LoadUpdateTimestampStart();
62 // The LoadUpdateDurationUptime() method relies on LoadUpdateTimestampStart()
63 // being called before it. Don't reorder.
64 LoadUpdateDurationUptime();
Jay Srinivasan19409b72013-04-12 19:23:36 -070065 for (int i = 0; i < kNumDownloadSources; i++) {
66 DownloadSource source = static_cast<DownloadSource>(i);
67 LoadCurrentBytesDownloaded(source);
68 LoadTotalBytesDownloaded(source);
69 }
Chris Sosabe45bef2013-04-09 18:25:12 -070070 LoadNumReboots();
David Zeuthena573d6f2013-06-14 16:13:36 -070071 LoadNumResponsesSeen();
Chris Sosaaa18e162013-06-20 13:20:30 -070072 LoadRollbackVersion();
David Zeuthendcba8092013-08-06 12:16:35 -070073 LoadP2PFirstAttemptTimestamp();
74 LoadP2PNumAttempts();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080075 return true;
76}
77
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080078void PayloadState::SetResponse(const OmahaResponse& omaha_response) {
Jay Srinivasan08262882012-12-28 19:29:43 -080079 // Always store the latest response.
80 response_ = omaha_response;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080081
Jay Srinivasan53173b92013-05-17 17:13:01 -070082 // Compute the candidate URLs first as they are used to calculate the
83 // response signature so that a change in enterprise policy for
84 // HTTP downloads being enabled or not could be honored as soon as the
85 // next update check happens.
86 ComputeCandidateUrls();
87
Jay Srinivasan08262882012-12-28 19:29:43 -080088 // Check if the "signature" of this response (i.e. the fields we care about)
89 // has changed.
90 string new_response_signature = CalculateResponseSignature();
91 bool has_response_changed = (response_signature_ != new_response_signature);
92
93 // If the response has changed, we should persist the new signature and
94 // clear away all the existing state.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080095 if (has_response_changed) {
Jay Srinivasan08262882012-12-28 19:29:43 -080096 LOG(INFO) << "Resetting all persisted state as this is a new response";
David Zeuthena573d6f2013-06-14 16:13:36 -070097 SetNumResponsesSeen(num_responses_seen_ + 1);
Jay Srinivasan08262882012-12-28 19:29:43 -080098 SetResponseSignature(new_response_signature);
99 ResetPersistedState();
Alex Deymob33b0f02013-08-08 21:10:02 -0700100 ReportUpdatesAbandonedEventCountMetric();
Jay Srinivasan08262882012-12-28 19:29:43 -0800101 return;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800102 }
103
Jay Srinivasan08262882012-12-28 19:29:43 -0800104 // This is the earliest point at which we can validate whether the URL index
105 // we loaded from the persisted state is a valid value. If the response
106 // hasn't changed but the URL index is invalid, it's indicative of some
107 // tampering of the persisted state.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700108 if (static_cast<uint32_t>(url_index_) >= candidate_urls_.size()) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800109 LOG(INFO) << "Resetting all payload state as the url index seems to have "
110 "been tampered with";
111 ResetPersistedState();
112 return;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800113 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700114
115 // Update the current download source which depends on the latest value of
116 // the response.
117 UpdateCurrentDownloadSource();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800118}
119
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700120void PayloadState::SetUsingP2PForDownloading(bool value) {
121 using_p2p_for_downloading_ = value;
122 // Update the current download source which depends on whether we are
123 // using p2p or not.
124 UpdateCurrentDownloadSource();
125}
126
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800127void PayloadState::DownloadComplete() {
128 LOG(INFO) << "Payload downloaded successfully";
129 IncrementPayloadAttemptNumber();
Alex Deymo820cc702013-06-28 15:43:46 -0700130 IncrementFullPayloadAttemptNumber();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800131}
132
133void PayloadState::DownloadProgress(size_t count) {
134 if (count == 0)
135 return;
136
David Zeuthen9a017f22013-04-11 16:10:26 -0700137 CalculateUpdateDurationUptime();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700138 UpdateBytesDownloaded(count);
David Zeuthen9a017f22013-04-11 16:10:26 -0700139
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800140 // We've received non-zero bytes from a recent download operation. Since our
141 // URL failure count is meant to penalize a URL only for consecutive
142 // failures, downloading bytes successfully means we should reset the failure
143 // count (as we know at least that the URL is working). In future, we can
144 // design this to be more sophisticated to check for more intelligent failure
145 // patterns, but right now, even 1 byte downloaded will mark the URL to be
146 // good unless it hits 10 (or configured number of) consecutive failures
147 // again.
148
149 if (GetUrlFailureCount() == 0)
150 return;
151
152 LOG(INFO) << "Resetting failure count of Url" << GetUrlIndex()
153 << " to 0 as we received " << count << " bytes successfully";
154 SetUrlFailureCount(0);
155}
156
Chris Sosabe45bef2013-04-09 18:25:12 -0700157void PayloadState::UpdateResumed() {
158 LOG(INFO) << "Resuming an update that was previously started.";
159 UpdateNumReboots();
160}
161
Jay Srinivasan19409b72013-04-12 19:23:36 -0700162void PayloadState::UpdateRestarted() {
163 LOG(INFO) << "Starting a new update";
164 ResetDownloadSourcesOnNewUpdate();
Chris Sosabe45bef2013-04-09 18:25:12 -0700165 SetNumReboots(0);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700166}
167
David Zeuthen9a017f22013-04-11 16:10:26 -0700168void PayloadState::UpdateSucceeded() {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700169 // Send the relevant metrics that are tracked in this class to UMA.
David Zeuthen9a017f22013-04-11 16:10:26 -0700170 CalculateUpdateDurationUptime();
David Zeuthenf413fe52013-04-22 14:04:39 -0700171 SetUpdateTimestampEnd(system_state_->clock()->GetWallclockTime());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700172 ReportBytesDownloadedMetrics();
David Zeuthencc6f9962013-04-18 11:57:24 -0700173 ReportUpdateUrlSwitchesMetric();
Chris Sosabe45bef2013-04-09 18:25:12 -0700174 ReportRebootMetrics();
David Zeuthen674c3182013-04-18 14:05:20 -0700175 ReportDurationMetrics();
David Zeuthena573d6f2013-06-14 16:13:36 -0700176 ReportUpdatesAbandonedCountMetric();
Alex Deymo1c656c42013-06-28 11:02:14 -0700177 ReportPayloadTypeMetric();
Alex Deymo820cc702013-06-28 15:43:46 -0700178 ReportAttemptsCountMetrics();
David Zeuthena573d6f2013-06-14 16:13:36 -0700179
180 // Reset the number of responses seen since it counts from the last
181 // successful update, e.g. now.
182 SetNumResponsesSeen(0);
David Zeuthene4c58bf2013-06-18 17:26:50 -0700183
184 CreateSystemUpdatedMarkerFile();
David Zeuthen9a017f22013-04-11 16:10:26 -0700185}
186
David Zeuthena99981f2013-04-29 13:42:47 -0700187void PayloadState::UpdateFailed(ErrorCode error) {
188 ErrorCode base_error = utils::GetBaseErrorCode(error);
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800189 LOG(INFO) << "Updating payload state for error code: " << base_error
190 << " (" << utils::CodeToString(base_error) << ")";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800191
Jay Srinivasan53173b92013-05-17 17:13:01 -0700192 if (candidate_urls_.size() == 0) {
193 // This means we got this error even before we got a valid Omaha response
194 // or don't have any valid candidates in the Omaha response.
Jay Srinivasan08262882012-12-28 19:29:43 -0800195 // So we should not advance the url_index_ in such cases.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800196 LOG(INFO) << "Ignoring failures until we get a valid Omaha response.";
197 return;
198 }
199
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800200 switch (base_error) {
201 // Errors which are good indicators of a problem with a particular URL or
202 // the protocol used in the URL or entities in the communication channel
203 // (e.g. proxies). We should try the next available URL in the next update
204 // check to quickly recover from these errors.
David Zeuthena99981f2013-04-29 13:42:47 -0700205 case kErrorCodePayloadHashMismatchError:
206 case kErrorCodePayloadSizeMismatchError:
207 case kErrorCodeDownloadPayloadVerificationError:
208 case kErrorCodeDownloadPayloadPubKeyVerificationError:
209 case kErrorCodeSignedDeltaPayloadExpectedError:
210 case kErrorCodeDownloadInvalidMetadataMagicString:
211 case kErrorCodeDownloadSignatureMissingInManifest:
212 case kErrorCodeDownloadManifestParseError:
213 case kErrorCodeDownloadMetadataSignatureError:
214 case kErrorCodeDownloadMetadataSignatureVerificationError:
215 case kErrorCodeDownloadMetadataSignatureMismatch:
216 case kErrorCodeDownloadOperationHashVerificationError:
217 case kErrorCodeDownloadOperationExecutionError:
218 case kErrorCodeDownloadOperationHashMismatch:
219 case kErrorCodeDownloadInvalidMetadataSize:
220 case kErrorCodeDownloadInvalidMetadataSignature:
221 case kErrorCodeDownloadOperationHashMissingError:
222 case kErrorCodeDownloadMetadataSignatureMissingError:
Gilad Arnold21504f02013-05-24 08:51:22 -0700223 case kErrorCodePayloadMismatchedType:
Don Garrett4d039442013-10-28 18:40:06 -0700224 case kErrorCodeUnsupportedMajorPayloadVersion:
225 case kErrorCodeUnsupportedMinorPayloadVersion:
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800226 IncrementUrlIndex();
227 break;
228
229 // Errors which seem to be just transient network/communication related
230 // failures and do not indicate any inherent problem with the URL itself.
231 // So, we should keep the current URL but just increment the
232 // failure count to give it more chances. This way, while we maximize our
233 // chances of downloading from the URLs that appear earlier in the response
234 // (because download from a local server URL that appears earlier in a
235 // response is preferable than downloading from the next URL which could be
236 // a internet URL and thus could be more expensive).
David Zeuthena99981f2013-04-29 13:42:47 -0700237 case kErrorCodeError:
238 case kErrorCodeDownloadTransferError:
239 case kErrorCodeDownloadWriteError:
240 case kErrorCodeDownloadStateInitializationError:
241 case kErrorCodeOmahaErrorInHTTPResponse: // Aggregate code for HTTP errors.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800242 IncrementFailureCount();
243 break;
244
245 // Errors which are not specific to a URL and hence shouldn't result in
246 // the URL being penalized. This can happen in two cases:
247 // 1. We haven't started downloading anything: These errors don't cost us
248 // anything in terms of actual payload bytes, so we should just do the
249 // regular retries at the next update check.
250 // 2. We have successfully downloaded the payload: In this case, the
251 // payload attempt number would have been incremented and would take care
Jay Srinivasan08262882012-12-28 19:29:43 -0800252 // of the backoff at the next update check.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800253 // In either case, there's no need to update URL index or failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700254 case kErrorCodeOmahaRequestError:
255 case kErrorCodeOmahaResponseHandlerError:
256 case kErrorCodePostinstallRunnerError:
257 case kErrorCodeFilesystemCopierError:
258 case kErrorCodeInstallDeviceOpenError:
259 case kErrorCodeKernelDeviceOpenError:
260 case kErrorCodeDownloadNewPartitionInfoError:
261 case kErrorCodeNewRootfsVerificationError:
262 case kErrorCodeNewKernelVerificationError:
263 case kErrorCodePostinstallBootedFromFirmwareB:
Don Garrett81018e02013-07-30 18:46:31 -0700264 case kErrorCodePostinstallFirmwareRONotUpdatable:
David Zeuthena99981f2013-04-29 13:42:47 -0700265 case kErrorCodeOmahaRequestEmptyResponseError:
266 case kErrorCodeOmahaRequestXMLParseError:
267 case kErrorCodeOmahaResponseInvalid:
268 case kErrorCodeOmahaUpdateIgnoredPerPolicy:
269 case kErrorCodeOmahaUpdateDeferredPerPolicy:
270 case kErrorCodeOmahaUpdateDeferredForBackoff:
271 case kErrorCodePostinstallPowerwashError:
272 case kErrorCodeUpdateCanceledByChannelChange:
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800273 LOG(INFO) << "Not incrementing URL index or failure count for this error";
274 break;
275
David Zeuthena99981f2013-04-29 13:42:47 -0700276 case kErrorCodeSuccess: // success code
David Zeuthena99981f2013-04-29 13:42:47 -0700277 case kErrorCodeUmaReportedMax: // not an error code
278 case kErrorCodeOmahaRequestHTTPResponseBase: // aggregated already
279 case kErrorCodeDevModeFlag: // not an error code
280 case kErrorCodeResumedFlag: // not an error code
281 case kErrorCodeTestImageFlag: // not an error code
282 case kErrorCodeTestOmahaUrlFlag: // not an error code
283 case kErrorCodeSpecialFlags: // not an error code
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800284 // These shouldn't happen. Enumerating these explicitly here so that we
285 // can let the compiler warn about new error codes that are added to
286 // action_processor.h but not added here.
287 LOG(WARNING) << "Unexpected error code for UpdateFailed";
288 break;
289
290 // Note: Not adding a default here so as to let the compiler warn us of
291 // any new enums that were added in the .h but not listed in this switch.
292 }
293}
294
Jay Srinivasan08262882012-12-28 19:29:43 -0800295bool PayloadState::ShouldBackoffDownload() {
296 if (response_.disable_payload_backoff) {
297 LOG(INFO) << "Payload backoff logic is disabled. "
298 "Can proceed with the download";
299 return false;
300 }
Chris Sosa20f005c2013-09-05 13:53:08 -0700301 if (system_state_->request_params()->use_p2p_for_downloading() &&
302 !system_state_->request_params()->p2p_url().empty()) {
303 LOG(INFO) << "Payload backoff logic is disabled because download "
304 << "will happen from local peer (via p2p).";
305 return false;
306 }
307 if (system_state_->request_params()->interactive()) {
308 LOG(INFO) << "Payload backoff disabled for interactive update checks.";
309 return false;
310 }
Jay Srinivasan08262882012-12-28 19:29:43 -0800311 if (response_.is_delta_payload) {
312 // If delta payloads fail, we want to fallback quickly to full payloads as
313 // they are more likely to succeed. Exponential backoffs would greatly
314 // slow down the fallback to full payloads. So we don't backoff for delta
315 // payloads.
316 LOG(INFO) << "No backoffs for delta payloads. "
317 << "Can proceed with the download";
318 return false;
319 }
320
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700321 if (!system_state_->hardware()->IsOfficialBuild()) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800322 // Backoffs are needed only for official builds. We do not want any delays
323 // or update failures due to backoffs during testing or development.
324 LOG(INFO) << "No backoffs for test/dev images. "
325 << "Can proceed with the download";
326 return false;
327 }
328
329 if (backoff_expiry_time_.is_null()) {
330 LOG(INFO) << "No backoff expiry time has been set. "
331 << "Can proceed with the download";
332 return false;
333 }
334
335 if (backoff_expiry_time_ < Time::Now()) {
336 LOG(INFO) << "The backoff expiry time ("
337 << utils::ToString(backoff_expiry_time_)
338 << ") has elapsed. Can proceed with the download";
339 return false;
340 }
341
342 LOG(INFO) << "Cannot proceed with downloads as we need to backoff until "
343 << utils::ToString(backoff_expiry_time_);
344 return true;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800345}
346
Chris Sosaaa18e162013-06-20 13:20:30 -0700347void PayloadState::Rollback() {
348 SetRollbackVersion(system_state_->request_params()->app_version());
349}
350
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800351void PayloadState::IncrementPayloadAttemptNumber() {
Alex Deymo820cc702013-06-28 15:43:46 -0700352 // Update the payload attempt number for both payload types: full and delta.
353 SetPayloadAttemptNumber(GetPayloadAttemptNumber() + 1);
Alex Deymo29b51d92013-07-09 15:26:24 -0700354
355 // Report the metric every time the value is incremented.
356 string metric = "Installer.PayloadAttemptNumber";
357 int value = GetPayloadAttemptNumber();
358
359 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
360 system_state_->metrics_lib()->SendToUMA(
361 metric,
362 value,
363 1, // min value
364 50, // max value
365 kNumDefaultUmaBuckets);
Alex Deymo820cc702013-06-28 15:43:46 -0700366}
367
368void PayloadState::IncrementFullPayloadAttemptNumber() {
369 // Update the payload attempt number for full payloads and the backoff time.
Jay Srinivasan08262882012-12-28 19:29:43 -0800370 if (response_.is_delta_payload) {
371 LOG(INFO) << "Not incrementing payload attempt number for delta payloads";
372 return;
373 }
374
Alex Deymo29b51d92013-07-09 15:26:24 -0700375 LOG(INFO) << "Incrementing the full payload attempt number";
Alex Deymo820cc702013-06-28 15:43:46 -0700376 SetFullPayloadAttemptNumber(GetFullPayloadAttemptNumber() + 1);
Jay Srinivasan08262882012-12-28 19:29:43 -0800377 UpdateBackoffExpiryTime();
Alex Deymo29b51d92013-07-09 15:26:24 -0700378
379 // Report the metric every time the value is incremented.
380 string metric = "Installer.FullPayloadAttemptNumber";
381 int value = GetFullPayloadAttemptNumber();
382
383 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
384 system_state_->metrics_lib()->SendToUMA(
385 metric,
386 value,
387 1, // min value
388 50, // max value
389 kNumDefaultUmaBuckets);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800390}
391
392void PayloadState::IncrementUrlIndex() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800393 uint32_t next_url_index = GetUrlIndex() + 1;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700394 if (next_url_index < candidate_urls_.size()) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800395 LOG(INFO) << "Incrementing the URL index for next attempt";
396 SetUrlIndex(next_url_index);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800397 } else {
398 LOG(INFO) << "Resetting the current URL index (" << GetUrlIndex() << ") to "
Jay Srinivasan53173b92013-05-17 17:13:01 -0700399 << "0 as we only have " << candidate_urls_.size()
400 << " candidate URL(s)";
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800401 SetUrlIndex(0);
Alex Deymo29b51d92013-07-09 15:26:24 -0700402 IncrementPayloadAttemptNumber();
403 IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800404 }
Jay Srinivasan08262882012-12-28 19:29:43 -0800405
David Zeuthencc6f9962013-04-18 11:57:24 -0700406 // If we have multiple URLs, record that we just switched to another one
Jay Srinivasan53173b92013-05-17 17:13:01 -0700407 if (candidate_urls_.size() > 1)
David Zeuthencc6f9962013-04-18 11:57:24 -0700408 SetUrlSwitchCount(url_switch_count_ + 1);
409
Jay Srinivasan08262882012-12-28 19:29:43 -0800410 // Whenever we update the URL index, we should also clear the URL failure
411 // count so we can start over fresh for the new URL.
412 SetUrlFailureCount(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800413}
414
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800415void PayloadState::IncrementFailureCount() {
416 uint32_t next_url_failure_count = GetUrlFailureCount() + 1;
Jay Srinivasan08262882012-12-28 19:29:43 -0800417 if (next_url_failure_count < response_.max_failure_count_per_url) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800418 LOG(INFO) << "Incrementing the URL failure count";
419 SetUrlFailureCount(next_url_failure_count);
420 } else {
421 LOG(INFO) << "Reached max number of failures for Url" << GetUrlIndex()
422 << ". Trying next available URL";
423 IncrementUrlIndex();
424 }
425}
426
Jay Srinivasan08262882012-12-28 19:29:43 -0800427void PayloadState::UpdateBackoffExpiryTime() {
428 if (response_.disable_payload_backoff) {
429 LOG(INFO) << "Resetting backoff expiry time as payload backoff is disabled";
430 SetBackoffExpiryTime(Time());
431 return;
432 }
433
Alex Deymo820cc702013-06-28 15:43:46 -0700434 if (GetFullPayloadAttemptNumber() == 0) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800435 SetBackoffExpiryTime(Time());
436 return;
437 }
438
439 // Since we're doing left-shift below, make sure we don't shift more
Alex Deymo820cc702013-06-28 15:43:46 -0700440 // than this. E.g. if int is 4-bytes, don't left-shift more than 30 bits,
Jay Srinivasan08262882012-12-28 19:29:43 -0800441 // since we don't expect value of kMaxBackoffDays to be more than 100 anyway.
Alex Deymo820cc702013-06-28 15:43:46 -0700442 int num_days = 1; // the value to be shifted.
443 const int kMaxShifts = (sizeof(num_days) * 8) - 2;
Jay Srinivasan08262882012-12-28 19:29:43 -0800444
445 // Normal backoff days is 2 raised to (payload_attempt_number - 1).
446 // E.g. if payload_attempt_number is over 30, limit power to 30.
Alex Deymo820cc702013-06-28 15:43:46 -0700447 int power = min(GetFullPayloadAttemptNumber() - 1, kMaxShifts);
Jay Srinivasan08262882012-12-28 19:29:43 -0800448
449 // The number of days is the minimum of 2 raised to (payload_attempt_number
450 // - 1) or kMaxBackoffDays.
451 num_days = min(num_days << power, kMaxBackoffDays);
452
453 // We don't want all retries to happen exactly at the same time when
454 // retrying after backoff. So add some random minutes to fuzz.
455 int fuzz_minutes = utils::FuzzInt(0, kMaxBackoffFuzzMinutes);
456 TimeDelta next_backoff_interval = TimeDelta::FromDays(num_days) +
457 TimeDelta::FromMinutes(fuzz_minutes);
458 LOG(INFO) << "Incrementing the backoff expiry time by "
459 << utils::FormatTimeDelta(next_backoff_interval);
460 SetBackoffExpiryTime(Time::Now() + next_backoff_interval);
461}
462
Jay Srinivasan19409b72013-04-12 19:23:36 -0700463void PayloadState::UpdateCurrentDownloadSource() {
464 current_download_source_ = kNumDownloadSources;
465
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700466 if (using_p2p_for_downloading_) {
467 current_download_source_ = kDownloadSourceHttpPeer;
468 } else if (GetUrlIndex() < candidate_urls_.size()) {
Jay Srinivasan53173b92013-05-17 17:13:01 -0700469 string current_url = candidate_urls_[GetUrlIndex()];
Jay Srinivasan19409b72013-04-12 19:23:36 -0700470 if (StartsWithASCII(current_url, "https://", false))
471 current_download_source_ = kDownloadSourceHttpsServer;
472 else if (StartsWithASCII(current_url, "http://", false))
473 current_download_source_ = kDownloadSourceHttpServer;
474 }
475
476 LOG(INFO) << "Current download source: "
477 << utils::ToString(current_download_source_);
478}
479
480void PayloadState::UpdateBytesDownloaded(size_t count) {
481 SetCurrentBytesDownloaded(
482 current_download_source_,
483 GetCurrentBytesDownloaded(current_download_source_) + count,
484 false);
485 SetTotalBytesDownloaded(
486 current_download_source_,
487 GetTotalBytesDownloaded(current_download_source_) + count,
488 false);
489}
490
491void PayloadState::ReportBytesDownloadedMetrics() {
492 // Report metrics collected from all known download sources to UMA.
493 // The reported data is in Megabytes in order to represent a larger
494 // sample range.
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700495 int download_sources_used = 0;
496 string metric;
497 uint64_t successful_mbs = 0;
498 uint64_t total_mbs = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700499 for (int i = 0; i < kNumDownloadSources; i++) {
500 DownloadSource source = static_cast<DownloadSource>(i);
501 const int kMaxMiBs = 10240; // Anything above 10GB goes in the last bucket.
David Zeuthen44848602013-06-24 13:32:14 -0700502 uint64_t mbs;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700503
David Zeuthen44848602013-06-24 13:32:14 -0700504 // Only consider this download source (and send byte counts) as
505 // having been used if we downloaded a non-trivial amount of bytes
506 // (e.g. at least 1 MiB) that contributed to the final success of
507 // the update. Otherwise we're going to end up with a lot of
508 // zero-byte events in the histogram.
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700509
David Zeuthen44848602013-06-24 13:32:14 -0700510 mbs = GetCurrentBytesDownloaded(source) / kNumBytesInOneMiB;
511 if (mbs > 0) {
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700512 download_sources_used |= (1 << source);
513
David Zeuthen44848602013-06-24 13:32:14 -0700514 metric = "Installer.SuccessfulMBsDownloadedFrom" +
515 utils::ToString(source);
516 successful_mbs += mbs;
517 LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric;
518 system_state_->metrics_lib()->SendToUMA(metric,
519 mbs,
520 0, // min
521 kMaxMiBs,
522 kNumDefaultUmaBuckets);
523 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700524 SetCurrentBytesDownloaded(source, 0, true);
525
Jay Srinivasan19409b72013-04-12 19:23:36 -0700526 mbs = GetTotalBytesDownloaded(source) / kNumBytesInOneMiB;
David Zeuthen44848602013-06-24 13:32:14 -0700527 if (mbs > 0) {
528 metric = "Installer.TotalMBsDownloadedFrom" + utils::ToString(source);
529 total_mbs += mbs;
530 LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric;
531 system_state_->metrics_lib()->SendToUMA(metric,
532 mbs,
533 0, // min
534 kMaxMiBs,
535 kNumDefaultUmaBuckets);
536 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700537 SetTotalBytesDownloaded(source, 0, true);
538 }
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700539
540 metric = "Installer.DownloadSourcesUsed";
541 LOG(INFO) << "Uploading 0x" << std::hex << download_sources_used
542 << " (bit flags) for metric " << metric;
543 int num_buckets = std::min(1 << kNumDownloadSources, kNumDefaultUmaBuckets);
544 system_state_->metrics_lib()->SendToUMA(metric,
545 download_sources_used,
546 0, // min
547 1 << kNumDownloadSources,
548 num_buckets);
549
550 if (successful_mbs) {
551 metric = "Installer.DownloadOverheadPercentage";
552 int percent_overhead = (total_mbs - successful_mbs) * 100 / successful_mbs;
553 LOG(INFO) << "Uploading " << percent_overhead << "% for metric " << metric;
554 system_state_->metrics_lib()->SendToUMA(metric,
555 percent_overhead,
556 0, // min: 0% overhead
557 1000, // max: 1000% overhead
558 kNumDefaultUmaBuckets);
559 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700560}
561
David Zeuthencc6f9962013-04-18 11:57:24 -0700562void PayloadState::ReportUpdateUrlSwitchesMetric() {
563 string metric = "Installer.UpdateURLSwitches";
564 int value = static_cast<int>(url_switch_count_);
565
566 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
567 system_state_->metrics_lib()->SendToUMA(
568 metric,
569 value,
570 0, // min value
571 100, // max value
572 kNumDefaultUmaBuckets);
573}
574
Chris Sosabe45bef2013-04-09 18:25:12 -0700575void PayloadState::ReportRebootMetrics() {
576 // Report the number of num_reboots.
577 string metric = "Installer.UpdateNumReboots";
578 uint32_t num_reboots = GetNumReboots();
579 LOG(INFO) << "Uploading reboot count of " << num_reboots << " for metric "
580 << metric;
581 system_state_->metrics_lib()->SendToUMA(
582 metric,
583 static_cast<int>(num_reboots), // sample
584 0, // min = 0.
585 50, // max
586 25); // buckets
587 SetNumReboots(0);
588}
589
590void PayloadState::UpdateNumReboots() {
591 // We only update the reboot count when the system has been detected to have
592 // been rebooted.
593 if (!system_state_->system_rebooted()) {
594 return;
595 }
596
597 SetNumReboots(GetNumReboots() + 1);
598}
599
600void PayloadState::SetNumReboots(uint32_t num_reboots) {
601 CHECK(prefs_);
602 num_reboots_ = num_reboots;
603 prefs_->SetInt64(kPrefsNumReboots, num_reboots);
604 LOG(INFO) << "Number of Reboots during current update attempt = "
605 << num_reboots_;
606}
607
Jay Srinivasan08262882012-12-28 19:29:43 -0800608void PayloadState::ResetPersistedState() {
609 SetPayloadAttemptNumber(0);
Alex Deymo820cc702013-06-28 15:43:46 -0700610 SetFullPayloadAttemptNumber(0);
Jay Srinivasan08262882012-12-28 19:29:43 -0800611 SetUrlIndex(0);
612 SetUrlFailureCount(0);
David Zeuthencc6f9962013-04-18 11:57:24 -0700613 SetUrlSwitchCount(0);
Jay Srinivasan08262882012-12-28 19:29:43 -0800614 UpdateBackoffExpiryTime(); // This will reset the backoff expiry time.
David Zeuthenf413fe52013-04-22 14:04:39 -0700615 SetUpdateTimestampStart(system_state_->clock()->GetWallclockTime());
David Zeuthen9a017f22013-04-11 16:10:26 -0700616 SetUpdateTimestampEnd(Time()); // Set to null time
617 SetUpdateDurationUptime(TimeDelta::FromSeconds(0));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700618 ResetDownloadSourcesOnNewUpdate();
Chris Sosaaa18e162013-06-20 13:20:30 -0700619 ResetRollbackVersion();
David Zeuthendcba8092013-08-06 12:16:35 -0700620 SetP2PNumAttempts(0);
621 SetP2PFirstAttemptTimestamp(Time()); // Set to null time
Chris Sosaaa18e162013-06-20 13:20:30 -0700622}
623
624void PayloadState::ResetRollbackVersion() {
625 CHECK(powerwash_safe_prefs_);
626 rollback_version_ = "";
627 powerwash_safe_prefs_->Delete(kPrefsRollbackVersion);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700628}
629
630void PayloadState::ResetDownloadSourcesOnNewUpdate() {
631 for (int i = 0; i < kNumDownloadSources; i++) {
632 DownloadSource source = static_cast<DownloadSource>(i);
633 SetCurrentBytesDownloaded(source, 0, true);
634 // Note: Not resetting the TotalBytesDownloaded as we want that metric
635 // to count the bytes downloaded across various update attempts until
636 // we have successfully applied the update.
637 }
638}
639
Chris Sosab3dcdb32013-09-04 15:22:12 -0700640int64_t PayloadState::GetPersistedValue(const string& key) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700641 CHECK(prefs_);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700642 if (!prefs_->Exists(key))
Jay Srinivasan19409b72013-04-12 19:23:36 -0700643 return 0;
644
645 int64_t stored_value;
Chris Sosab3dcdb32013-09-04 15:22:12 -0700646 if (!prefs_->GetInt64(key, &stored_value))
Jay Srinivasan19409b72013-04-12 19:23:36 -0700647 return 0;
648
649 if (stored_value < 0) {
650 LOG(ERROR) << key << ": Invalid value (" << stored_value
651 << ") in persisted state. Defaulting to 0";
652 return 0;
653 }
654
655 return stored_value;
Jay Srinivasan08262882012-12-28 19:29:43 -0800656}
657
658string PayloadState::CalculateResponseSignature() {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700659 string response_sign = base::StringPrintf(
660 "NumURLs = %d\n", static_cast<int>(candidate_urls_.size()));
Jay Srinivasan08262882012-12-28 19:29:43 -0800661
Jay Srinivasan53173b92013-05-17 17:13:01 -0700662 for (size_t i = 0; i < candidate_urls_.size(); i++)
Alex Vakulenko75039d72014-03-25 12:36:28 -0700663 response_sign += base::StringPrintf("Candidate Url%d = %s\n",
664 static_cast<int>(i),
665 candidate_urls_[i].c_str());
Jay Srinivasan08262882012-12-28 19:29:43 -0800666
Alex Vakulenko75039d72014-03-25 12:36:28 -0700667 response_sign += base::StringPrintf(
668 "Payload Size = %ju\n"
669 "Payload Sha256 Hash = %s\n"
670 "Metadata Size = %ju\n"
671 "Metadata Signature = %s\n"
672 "Is Delta Payload = %d\n"
673 "Max Failure Count Per Url = %d\n"
674 "Disable Payload Backoff = %d\n",
675 static_cast<uintmax_t>(response_.size),
676 response_.hash.c_str(),
677 static_cast<uintmax_t>(response_.metadata_size),
678 response_.metadata_signature.c_str(),
679 response_.is_delta_payload,
680 response_.max_failure_count_per_url,
681 response_.disable_payload_backoff);
Jay Srinivasan08262882012-12-28 19:29:43 -0800682 return response_sign;
683}
684
685void PayloadState::LoadResponseSignature() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800686 CHECK(prefs_);
687 string stored_value;
Jay Srinivasan08262882012-12-28 19:29:43 -0800688 if (prefs_->Exists(kPrefsCurrentResponseSignature) &&
689 prefs_->GetString(kPrefsCurrentResponseSignature, &stored_value)) {
690 SetResponseSignature(stored_value);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800691 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800692}
693
Jay Srinivasan19409b72013-04-12 19:23:36 -0700694void PayloadState::SetResponseSignature(const string& response_signature) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800695 CHECK(prefs_);
696 response_signature_ = response_signature;
697 LOG(INFO) << "Current Response Signature = \n" << response_signature_;
698 prefs_->SetString(kPrefsCurrentResponseSignature, response_signature_);
699}
700
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800701void PayloadState::LoadPayloadAttemptNumber() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700702 SetPayloadAttemptNumber(GetPersistedValue(kPrefsPayloadAttemptNumber));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800703}
704
Alex Deymo820cc702013-06-28 15:43:46 -0700705void PayloadState::LoadFullPayloadAttemptNumber() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700706 SetFullPayloadAttemptNumber(GetPersistedValue(
707 kPrefsFullPayloadAttemptNumber));
Alex Deymo820cc702013-06-28 15:43:46 -0700708}
709
710void PayloadState::SetPayloadAttemptNumber(int payload_attempt_number) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800711 CHECK(prefs_);
712 payload_attempt_number_ = payload_attempt_number;
713 LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number_;
714 prefs_->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number_);
715}
716
Alex Deymo820cc702013-06-28 15:43:46 -0700717void PayloadState::SetFullPayloadAttemptNumber(
718 int full_payload_attempt_number) {
719 CHECK(prefs_);
720 full_payload_attempt_number_ = full_payload_attempt_number;
721 LOG(INFO) << "Full Payload Attempt Number = " << full_payload_attempt_number_;
722 prefs_->SetInt64(kPrefsFullPayloadAttemptNumber,
723 full_payload_attempt_number_);
724}
725
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800726void PayloadState::LoadUrlIndex() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700727 SetUrlIndex(GetPersistedValue(kPrefsCurrentUrlIndex));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800728}
729
730void PayloadState::SetUrlIndex(uint32_t url_index) {
731 CHECK(prefs_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800732 url_index_ = url_index;
733 LOG(INFO) << "Current URL Index = " << url_index_;
734 prefs_->SetInt64(kPrefsCurrentUrlIndex, url_index_);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700735
736 // Also update the download source, which is purely dependent on the
737 // current URL index alone.
738 UpdateCurrentDownloadSource();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800739}
740
David Zeuthencc6f9962013-04-18 11:57:24 -0700741void PayloadState::LoadUrlSwitchCount() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700742 SetUrlSwitchCount(GetPersistedValue(kPrefsUrlSwitchCount));
David Zeuthencc6f9962013-04-18 11:57:24 -0700743}
744
745void PayloadState::SetUrlSwitchCount(uint32_t url_switch_count) {
746 CHECK(prefs_);
747 url_switch_count_ = url_switch_count;
748 LOG(INFO) << "URL Switch Count = " << url_switch_count_;
749 prefs_->SetInt64(kPrefsUrlSwitchCount, url_switch_count_);
750}
751
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800752void PayloadState::LoadUrlFailureCount() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700753 SetUrlFailureCount(GetPersistedValue(kPrefsCurrentUrlFailureCount));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800754}
755
756void PayloadState::SetUrlFailureCount(uint32_t url_failure_count) {
757 CHECK(prefs_);
758 url_failure_count_ = url_failure_count;
759 LOG(INFO) << "Current URL (Url" << GetUrlIndex()
760 << ")'s Failure Count = " << url_failure_count_;
761 prefs_->SetInt64(kPrefsCurrentUrlFailureCount, url_failure_count_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800762}
763
Jay Srinivasan08262882012-12-28 19:29:43 -0800764void PayloadState::LoadBackoffExpiryTime() {
765 CHECK(prefs_);
766 int64_t stored_value;
767 if (!prefs_->Exists(kPrefsBackoffExpiryTime))
768 return;
769
770 if (!prefs_->GetInt64(kPrefsBackoffExpiryTime, &stored_value))
771 return;
772
773 Time stored_time = Time::FromInternalValue(stored_value);
774 if (stored_time > Time::Now() + TimeDelta::FromDays(kMaxBackoffDays)) {
775 LOG(ERROR) << "Invalid backoff expiry time ("
776 << utils::ToString(stored_time)
777 << ") in persisted state. Resetting.";
778 stored_time = Time();
779 }
780 SetBackoffExpiryTime(stored_time);
781}
782
783void PayloadState::SetBackoffExpiryTime(const Time& new_time) {
784 CHECK(prefs_);
785 backoff_expiry_time_ = new_time;
786 LOG(INFO) << "Backoff Expiry Time = "
787 << utils::ToString(backoff_expiry_time_);
788 prefs_->SetInt64(kPrefsBackoffExpiryTime,
789 backoff_expiry_time_.ToInternalValue());
790}
791
David Zeuthen9a017f22013-04-11 16:10:26 -0700792TimeDelta PayloadState::GetUpdateDuration() {
David Zeuthenf413fe52013-04-22 14:04:39 -0700793 Time end_time = update_timestamp_end_.is_null()
794 ? system_state_->clock()->GetWallclockTime() :
795 update_timestamp_end_;
David Zeuthen9a017f22013-04-11 16:10:26 -0700796 return end_time - update_timestamp_start_;
797}
798
799void PayloadState::LoadUpdateTimestampStart() {
800 int64_t stored_value;
801 Time stored_time;
802
803 CHECK(prefs_);
804
David Zeuthenf413fe52013-04-22 14:04:39 -0700805 Time now = system_state_->clock()->GetWallclockTime();
David Zeuthen9a017f22013-04-11 16:10:26 -0700806
807 if (!prefs_->Exists(kPrefsUpdateTimestampStart)) {
808 // The preference missing is not unexpected - in that case, just
809 // use the current time as start time
810 stored_time = now;
811 } else if (!prefs_->GetInt64(kPrefsUpdateTimestampStart, &stored_value)) {
812 LOG(ERROR) << "Invalid UpdateTimestampStart value. Resetting.";
813 stored_time = now;
814 } else {
815 stored_time = Time::FromInternalValue(stored_value);
816 }
817
818 // Sanity check: If the time read from disk is in the future
819 // (modulo some slack to account for possible NTP drift
820 // adjustments), something is fishy and we should report and
821 // reset.
822 TimeDelta duration_according_to_stored_time = now - stored_time;
823 if (duration_according_to_stored_time < -kDurationSlack) {
824 LOG(ERROR) << "The UpdateTimestampStart value ("
825 << utils::ToString(stored_time)
826 << ") in persisted state is "
David Zeuthen674c3182013-04-18 14:05:20 -0700827 << utils::FormatTimeDelta(duration_according_to_stored_time)
828 << " in the future. Resetting.";
David Zeuthen9a017f22013-04-11 16:10:26 -0700829 stored_time = now;
830 }
831
832 SetUpdateTimestampStart(stored_time);
833}
834
835void PayloadState::SetUpdateTimestampStart(const Time& value) {
836 CHECK(prefs_);
837 update_timestamp_start_ = value;
838 prefs_->SetInt64(kPrefsUpdateTimestampStart,
839 update_timestamp_start_.ToInternalValue());
840 LOG(INFO) << "Update Timestamp Start = "
841 << utils::ToString(update_timestamp_start_);
842}
843
844void PayloadState::SetUpdateTimestampEnd(const Time& value) {
845 update_timestamp_end_ = value;
846 LOG(INFO) << "Update Timestamp End = "
847 << utils::ToString(update_timestamp_end_);
848}
849
850TimeDelta PayloadState::GetUpdateDurationUptime() {
851 return update_duration_uptime_;
852}
853
854void PayloadState::LoadUpdateDurationUptime() {
855 int64_t stored_value;
856 TimeDelta stored_delta;
857
858 CHECK(prefs_);
859
860 if (!prefs_->Exists(kPrefsUpdateDurationUptime)) {
861 // The preference missing is not unexpected - in that case, just
862 // we'll use zero as the delta
863 } else if (!prefs_->GetInt64(kPrefsUpdateDurationUptime, &stored_value)) {
864 LOG(ERROR) << "Invalid UpdateDurationUptime value. Resetting.";
865 stored_delta = TimeDelta::FromSeconds(0);
866 } else {
867 stored_delta = TimeDelta::FromInternalValue(stored_value);
868 }
869
870 // Sanity-check: Uptime can never be greater than the wall-clock
871 // difference (modulo some slack). If it is, report and reset
872 // to the wall-clock difference.
873 TimeDelta diff = GetUpdateDuration() - stored_delta;
874 if (diff < -kDurationSlack) {
875 LOG(ERROR) << "The UpdateDurationUptime value ("
David Zeuthen674c3182013-04-18 14:05:20 -0700876 << utils::FormatTimeDelta(stored_delta)
David Zeuthen9a017f22013-04-11 16:10:26 -0700877 << ") in persisted state is "
David Zeuthen674c3182013-04-18 14:05:20 -0700878 << utils::FormatTimeDelta(diff)
879 << " larger than the wall-clock delta. Resetting.";
David Zeuthen9a017f22013-04-11 16:10:26 -0700880 stored_delta = update_duration_current_;
881 }
882
883 SetUpdateDurationUptime(stored_delta);
884}
885
Chris Sosabe45bef2013-04-09 18:25:12 -0700886void PayloadState::LoadNumReboots() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700887 SetNumReboots(GetPersistedValue(kPrefsNumReboots));
Chris Sosaaa18e162013-06-20 13:20:30 -0700888}
889
890void PayloadState::LoadRollbackVersion() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700891 CHECK(powerwash_safe_prefs_);
892 string rollback_version;
893 if (powerwash_safe_prefs_->GetString(kPrefsRollbackVersion,
894 &rollback_version)) {
895 SetRollbackVersion(rollback_version);
896 }
Chris Sosaaa18e162013-06-20 13:20:30 -0700897}
898
899void PayloadState::SetRollbackVersion(const string& rollback_version) {
900 CHECK(powerwash_safe_prefs_);
901 LOG(INFO) << "Blacklisting version "<< rollback_version;
902 rollback_version_ = rollback_version;
903 powerwash_safe_prefs_->SetString(kPrefsRollbackVersion, rollback_version);
Chris Sosabe45bef2013-04-09 18:25:12 -0700904}
905
David Zeuthen9a017f22013-04-11 16:10:26 -0700906void PayloadState::SetUpdateDurationUptimeExtended(const TimeDelta& value,
907 const Time& timestamp,
908 bool use_logging) {
909 CHECK(prefs_);
910 update_duration_uptime_ = value;
911 update_duration_uptime_timestamp_ = timestamp;
912 prefs_->SetInt64(kPrefsUpdateDurationUptime,
913 update_duration_uptime_.ToInternalValue());
914 if (use_logging) {
915 LOG(INFO) << "Update Duration Uptime = "
David Zeuthen674c3182013-04-18 14:05:20 -0700916 << utils::FormatTimeDelta(update_duration_uptime_);
David Zeuthen9a017f22013-04-11 16:10:26 -0700917 }
918}
919
920void PayloadState::SetUpdateDurationUptime(const TimeDelta& value) {
David Zeuthenf413fe52013-04-22 14:04:39 -0700921 Time now = system_state_->clock()->GetMonotonicTime();
922 SetUpdateDurationUptimeExtended(value, now, true);
David Zeuthen9a017f22013-04-11 16:10:26 -0700923}
924
925void PayloadState::CalculateUpdateDurationUptime() {
David Zeuthenf413fe52013-04-22 14:04:39 -0700926 Time now = system_state_->clock()->GetMonotonicTime();
David Zeuthen9a017f22013-04-11 16:10:26 -0700927 TimeDelta uptime_since_last_update = now - update_duration_uptime_timestamp_;
928 TimeDelta new_uptime = update_duration_uptime_ + uptime_since_last_update;
929 // We're frequently called so avoid logging this write
930 SetUpdateDurationUptimeExtended(new_uptime, now, false);
931}
932
David Zeuthen674c3182013-04-18 14:05:20 -0700933void PayloadState::ReportDurationMetrics() {
934 TimeDelta duration = GetUpdateDuration();
935 TimeDelta duration_uptime = GetUpdateDurationUptime();
936 string metric;
937
938 metric = "Installer.UpdateDurationMinutes";
939 system_state_->metrics_lib()->SendToUMA(
940 metric,
941 static_cast<int>(duration.InMinutes()),
942 1, // min: 1 minute
943 365*24*60, // max: 1 year (approx)
944 kNumDefaultUmaBuckets);
945 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration)
946 << " for metric " << metric;
947
948 metric = "Installer.UpdateDurationUptimeMinutes";
949 system_state_->metrics_lib()->SendToUMA(
950 metric,
951 static_cast<int>(duration_uptime.InMinutes()),
952 1, // min: 1 minute
953 30*24*60, // max: 1 month (approx)
954 kNumDefaultUmaBuckets);
955 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration_uptime)
956 << " for metric " << metric;
957
958 prefs_->Delete(kPrefsUpdateTimestampStart);
959 prefs_->Delete(kPrefsUpdateDurationUptime);
960}
961
Alex Deymo1c656c42013-06-28 11:02:14 -0700962void PayloadState::ReportPayloadTypeMetric() {
963 string metric;
964 PayloadType uma_payload_type;
965 OmahaRequestParams* params = system_state_->request_params();
966
967 if (response_.is_delta_payload) {
968 uma_payload_type = kPayloadTypeDelta;
969 } else if (params->delta_okay()) {
970 uma_payload_type = kPayloadTypeFull;
971 } else { // Full payload, delta was not allowed by request.
972 uma_payload_type = kPayloadTypeForcedFull;
973 }
974
975 metric = "Installer.PayloadFormat";
976 system_state_->metrics_lib()->SendEnumToUMA(
977 metric,
978 uma_payload_type,
979 kNumPayloadTypes);
980 LOG(INFO) << "Uploading " << utils::ToString(uma_payload_type)
981 << " for metric " << metric;
982}
983
Alex Deymo820cc702013-06-28 15:43:46 -0700984void PayloadState::ReportAttemptsCountMetrics() {
985 string metric;
986 int total_attempts = GetPayloadAttemptNumber();
987
988 metric = "Installer.AttemptsCount.Total";
989 system_state_->metrics_lib()->SendToUMA(
990 metric,
991 total_attempts,
992 1, // min
993 50, // max
994 kNumDefaultUmaBuckets);
995 LOG(INFO) << "Uploading " << total_attempts
996 << " for metric " << metric;
997}
998
Jay Srinivasan19409b72013-04-12 19:23:36 -0700999string PayloadState::GetPrefsKey(const string& prefix, DownloadSource source) {
1000 return prefix + "-from-" + utils::ToString(source);
1001}
1002
1003void PayloadState::LoadCurrentBytesDownloaded(DownloadSource source) {
1004 string key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source);
Chris Sosab3dcdb32013-09-04 15:22:12 -07001005 SetCurrentBytesDownloaded(source, GetPersistedValue(key), true);
Jay Srinivasan19409b72013-04-12 19:23:36 -07001006}
1007
1008void PayloadState::SetCurrentBytesDownloaded(
1009 DownloadSource source,
1010 uint64_t current_bytes_downloaded,
1011 bool log) {
1012 CHECK(prefs_);
1013
1014 if (source >= kNumDownloadSources)
1015 return;
1016
1017 // Update the in-memory value.
1018 current_bytes_downloaded_[source] = current_bytes_downloaded;
1019
1020 string prefs_key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source);
1021 prefs_->SetInt64(prefs_key, current_bytes_downloaded);
1022 LOG_IF(INFO, log) << "Current bytes downloaded for "
1023 << utils::ToString(source) << " = "
1024 << GetCurrentBytesDownloaded(source);
1025}
1026
1027void PayloadState::LoadTotalBytesDownloaded(DownloadSource source) {
1028 string key = GetPrefsKey(kPrefsTotalBytesDownloaded, source);
Chris Sosab3dcdb32013-09-04 15:22:12 -07001029 SetTotalBytesDownloaded(source, GetPersistedValue(key), true);
Jay Srinivasan19409b72013-04-12 19:23:36 -07001030}
1031
1032void PayloadState::SetTotalBytesDownloaded(
1033 DownloadSource source,
1034 uint64_t total_bytes_downloaded,
1035 bool log) {
1036 CHECK(prefs_);
1037
1038 if (source >= kNumDownloadSources)
1039 return;
1040
1041 // Update the in-memory value.
1042 total_bytes_downloaded_[source] = total_bytes_downloaded;
1043
1044 // Persist.
1045 string prefs_key = GetPrefsKey(kPrefsTotalBytesDownloaded, source);
1046 prefs_->SetInt64(prefs_key, total_bytes_downloaded);
1047 LOG_IF(INFO, log) << "Total bytes downloaded for "
1048 << utils::ToString(source) << " = "
1049 << GetTotalBytesDownloaded(source);
1050}
1051
David Zeuthena573d6f2013-06-14 16:13:36 -07001052void PayloadState::LoadNumResponsesSeen() {
Chris Sosab3dcdb32013-09-04 15:22:12 -07001053 SetNumResponsesSeen(GetPersistedValue(kPrefsNumResponsesSeen));
David Zeuthena573d6f2013-06-14 16:13:36 -07001054}
1055
1056void PayloadState::SetNumResponsesSeen(int num_responses_seen) {
1057 CHECK(prefs_);
1058 num_responses_seen_ = num_responses_seen;
1059 LOG(INFO) << "Num Responses Seen = " << num_responses_seen_;
1060 prefs_->SetInt64(kPrefsNumResponsesSeen, num_responses_seen_);
1061}
1062
1063void PayloadState::ReportUpdatesAbandonedCountMetric() {
1064 string metric = "Installer.UpdatesAbandonedCount";
1065 int value = num_responses_seen_ - 1;
1066
1067 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
1068 system_state_->metrics_lib()->SendToUMA(
1069 metric,
1070 value,
1071 0, // min value
1072 100, // max value
1073 kNumDefaultUmaBuckets);
1074}
1075
Alex Deymob33b0f02013-08-08 21:10:02 -07001076void PayloadState::ReportUpdatesAbandonedEventCountMetric() {
1077 string metric = "Installer.UpdatesAbandonedEventCount";
1078 int value = num_responses_seen_ - 1;
1079
1080 // Do not send an "abandoned" event when 0 payloads were abandoned since the
1081 // last successful update.
1082 if (value == 0)
1083 return;
1084
1085 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
1086 system_state_->metrics_lib()->SendToUMA(
1087 metric,
1088 value,
1089 0, // min value
1090 100, // max value
1091 kNumDefaultUmaBuckets);
1092}
1093
Jay Srinivasan53173b92013-05-17 17:13:01 -07001094void PayloadState::ComputeCandidateUrls() {
Chris Sosaf7d80042013-08-22 16:45:17 -07001095 bool http_url_ok = true;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001096
J. Richard Barnette056b0ab2013-10-29 15:24:56 -07001097 if (system_state_->hardware()->IsOfficialBuild()) {
Jay Srinivasan53173b92013-05-17 17:13:01 -07001098 const policy::DevicePolicy* policy = system_state_->device_policy();
Chris Sosaf7d80042013-08-22 16:45:17 -07001099 if (policy && policy->GetHttpDownloadsEnabled(&http_url_ok) && !http_url_ok)
Jay Srinivasan53173b92013-05-17 17:13:01 -07001100 LOG(INFO) << "Downloads via HTTP Url are not enabled by device policy";
1101 } else {
1102 LOG(INFO) << "Allowing HTTP downloads for unofficial builds";
1103 http_url_ok = true;
1104 }
1105
1106 candidate_urls_.clear();
1107 for (size_t i = 0; i < response_.payload_urls.size(); i++) {
1108 string candidate_url = response_.payload_urls[i];
1109 if (StartsWithASCII(candidate_url, "http://", false) && !http_url_ok)
1110 continue;
1111 candidate_urls_.push_back(candidate_url);
1112 LOG(INFO) << "Candidate Url" << (candidate_urls_.size() - 1)
1113 << ": " << candidate_url;
1114 }
1115
1116 LOG(INFO) << "Found " << candidate_urls_.size() << " candidate URLs "
1117 << "out of " << response_.payload_urls.size() << " URLs supplied";
1118}
1119
David Zeuthene4c58bf2013-06-18 17:26:50 -07001120void PayloadState::CreateSystemUpdatedMarkerFile() {
1121 CHECK(prefs_);
1122 int64_t value = system_state_->clock()->GetWallclockTime().ToInternalValue();
1123 prefs_->SetInt64(kPrefsSystemUpdatedMarker, value);
1124}
1125
1126void PayloadState::BootedIntoUpdate(TimeDelta time_to_reboot) {
1127 // Send |time_to_reboot| as a UMA stat.
1128 string metric = "Installer.TimeToRebootMinutes";
1129 system_state_->metrics_lib()->SendToUMA(metric,
1130 time_to_reboot.InMinutes(),
1131 0, // min: 0 minute
1132 30*24*60, // max: 1 month (approx)
1133 kNumDefaultUmaBuckets);
1134 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(time_to_reboot)
1135 << " for metric " << metric;
1136}
1137
1138void PayloadState::UpdateEngineStarted() {
Alex Deymo569c4242013-07-24 12:01:01 -07001139 // Avoid the UpdateEngineStarted actions if this is not the first time we
1140 // run the update engine since reboot.
1141 if (!system_state_->system_rebooted())
1142 return;
1143
David Zeuthene4c58bf2013-06-18 17:26:50 -07001144 // Figure out if we just booted into a new update
1145 if (prefs_->Exists(kPrefsSystemUpdatedMarker)) {
1146 int64_t stored_value;
1147 if (prefs_->GetInt64(kPrefsSystemUpdatedMarker, &stored_value)) {
1148 Time system_updated_at = Time::FromInternalValue(stored_value);
1149 if (!system_updated_at.is_null()) {
1150 TimeDelta time_to_reboot =
1151 system_state_->clock()->GetWallclockTime() - system_updated_at;
1152 if (time_to_reboot.ToInternalValue() < 0) {
1153 LOG(ERROR) << "time_to_reboot is negative - system_updated_at: "
1154 << utils::ToString(system_updated_at);
1155 } else {
1156 BootedIntoUpdate(time_to_reboot);
1157 }
1158 }
1159 }
1160 prefs_->Delete(kPrefsSystemUpdatedMarker);
1161 }
Alex Deymo42432912013-07-12 20:21:15 -07001162 // Check if it is needed to send metrics about a failed reboot into a new
1163 // version.
1164 ReportFailedBootIfNeeded();
1165}
1166
1167void PayloadState::ReportFailedBootIfNeeded() {
1168 // If the kPrefsTargetVersionInstalledFrom is present, a successfully applied
1169 // payload was marked as ready immediately before the last reboot, and we
1170 // need to check if such payload successfully rebooted or not.
1171 if (prefs_->Exists(kPrefsTargetVersionInstalledFrom)) {
Alex Vakulenko4f5b1442014-02-21 12:19:44 -08001172 int64_t installed_from = 0;
1173 if (!prefs_->GetInt64(kPrefsTargetVersionInstalledFrom, &installed_from)) {
Alex Deymo42432912013-07-12 20:21:15 -07001174 LOG(ERROR) << "Error reading TargetVersionInstalledFrom on reboot.";
1175 return;
1176 }
Alex Vakulenko4f5b1442014-02-21 12:19:44 -08001177 if (int(installed_from) ==
1178 utils::GetPartitionNumber(system_state_->hardware()->BootDevice())) {
Alex Deymo42432912013-07-12 20:21:15 -07001179 // A reboot was pending, but the chromebook is again in the same
1180 // BootDevice where the update was installed from.
1181 int64_t target_attempt;
1182 if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt)) {
1183 LOG(ERROR) << "Error reading TargetVersionAttempt when "
1184 "TargetVersionInstalledFrom was present.";
1185 target_attempt = 1;
1186 }
1187
1188 // Report the UMA metric of the current boot failure.
1189 string metric = "Installer.RebootToNewPartitionAttempt";
1190
1191 LOG(INFO) << "Uploading " << target_attempt
1192 << " (count) for metric " << metric;
1193 system_state_->metrics_lib()->SendToUMA(
1194 metric,
1195 target_attempt,
1196 1, // min value
1197 50, // max value
1198 kNumDefaultUmaBuckets);
1199 } else {
1200 prefs_->Delete(kPrefsTargetVersionAttempt);
1201 prefs_->Delete(kPrefsTargetVersionUniqueId);
1202 }
1203 prefs_->Delete(kPrefsTargetVersionInstalledFrom);
1204 }
1205}
1206
1207void PayloadState::ExpectRebootInNewVersion(const string& target_version_uid) {
1208 // Expect to boot into the new partition in the next reboot setting the
1209 // TargetVersion* flags in the Prefs.
1210 string stored_target_version_uid;
1211 string target_version_id;
1212 string target_partition;
1213 int64_t target_attempt;
1214
1215 if (prefs_->Exists(kPrefsTargetVersionUniqueId) &&
1216 prefs_->GetString(kPrefsTargetVersionUniqueId,
1217 &stored_target_version_uid) &&
1218 stored_target_version_uid == target_version_uid) {
1219 if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt))
1220 target_attempt = 0;
1221 } else {
1222 prefs_->SetString(kPrefsTargetVersionUniqueId, target_version_uid);
1223 target_attempt = 0;
1224 }
1225 prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt + 1);
1226
Alex Vakulenko4f5b1442014-02-21 12:19:44 -08001227 prefs_->SetInt64(kPrefsTargetVersionInstalledFrom,
1228 utils::GetPartitionNumber(
Alex Deymo42432912013-07-12 20:21:15 -07001229 system_state_->hardware()->BootDevice()));
1230}
1231
1232void PayloadState::ResetUpdateStatus() {
1233 // Remove the TargetVersionInstalledFrom pref so that if the machine is
1234 // rebooted the next boot is not flagged as failed to rebooted into the
1235 // new applied payload.
1236 prefs_->Delete(kPrefsTargetVersionInstalledFrom);
1237
1238 // Also decrement the attempt number if it exists.
1239 int64_t target_attempt;
1240 if (prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt))
1241 prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt-1);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001242}
1243
David Zeuthendcba8092013-08-06 12:16:35 -07001244int PayloadState::GetP2PNumAttempts() {
1245 return p2p_num_attempts_;
1246}
1247
1248void PayloadState::SetP2PNumAttempts(int value) {
1249 p2p_num_attempts_ = value;
1250 LOG(INFO) << "p2p Num Attempts = " << p2p_num_attempts_;
1251 CHECK(prefs_);
1252 prefs_->SetInt64(kPrefsP2PNumAttempts, value);
1253}
1254
1255void PayloadState::LoadP2PNumAttempts() {
Chris Sosab3dcdb32013-09-04 15:22:12 -07001256 SetP2PNumAttempts(GetPersistedValue(kPrefsP2PNumAttempts));
David Zeuthendcba8092013-08-06 12:16:35 -07001257}
1258
1259Time PayloadState::GetP2PFirstAttemptTimestamp() {
1260 return p2p_first_attempt_timestamp_;
1261}
1262
1263void PayloadState::SetP2PFirstAttemptTimestamp(const Time& time) {
1264 p2p_first_attempt_timestamp_ = time;
1265 LOG(INFO) << "p2p First Attempt Timestamp = "
1266 << utils::ToString(p2p_first_attempt_timestamp_);
1267 CHECK(prefs_);
1268 int64_t stored_value = time.ToInternalValue();
1269 prefs_->SetInt64(kPrefsP2PFirstAttemptTimestamp, stored_value);
1270}
1271
1272void PayloadState::LoadP2PFirstAttemptTimestamp() {
Chris Sosab3dcdb32013-09-04 15:22:12 -07001273 int64_t stored_value = GetPersistedValue(kPrefsP2PFirstAttemptTimestamp);
David Zeuthendcba8092013-08-06 12:16:35 -07001274 Time stored_time = Time::FromInternalValue(stored_value);
1275 SetP2PFirstAttemptTimestamp(stored_time);
1276}
1277
1278void PayloadState::P2PNewAttempt() {
1279 CHECK(prefs_);
1280 // Set timestamp, if it hasn't been set already
1281 if (p2p_first_attempt_timestamp_.is_null()) {
1282 SetP2PFirstAttemptTimestamp(system_state_->clock()->GetWallclockTime());
1283 }
1284 // Increase number of attempts
1285 SetP2PNumAttempts(GetP2PNumAttempts() + 1);
1286}
1287
1288bool PayloadState::P2PAttemptAllowed() {
1289 if (p2p_num_attempts_ > kMaxP2PAttempts) {
1290 LOG(INFO) << "Number of p2p attempts is " << p2p_num_attempts_
1291 << " which is greater than "
1292 << kMaxP2PAttempts
1293 << " - disallowing p2p.";
1294 return false;
1295 }
1296
1297 if (!p2p_first_attempt_timestamp_.is_null()) {
1298 Time now = system_state_->clock()->GetWallclockTime();
1299 TimeDelta time_spent_attempting_p2p = now - p2p_first_attempt_timestamp_;
1300 if (time_spent_attempting_p2p.InSeconds() < 0) {
1301 LOG(ERROR) << "Time spent attempting p2p is negative"
1302 << " - disallowing p2p.";
1303 return false;
1304 }
1305 if (time_spent_attempting_p2p.InSeconds() > kMaxP2PAttemptTimeSeconds) {
1306 LOG(INFO) << "Time spent attempting p2p is "
1307 << utils::FormatTimeDelta(time_spent_attempting_p2p)
1308 << " which is greater than "
1309 << utils::FormatTimeDelta(TimeDelta::FromSeconds(
1310 kMaxP2PAttemptTimeSeconds))
1311 << " - disallowing p2p.";
1312 return false;
1313 }
1314 }
1315
1316 return true;
1317}
1318
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001319} // namespace chromeos_update_engine