blob: 90dc3d33ebfd74e61ffca26af770a95ce60a589e [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>
Jay Srinivasan19409b72013-04-12 19:23:36 -070010#include "base/string_util.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080011#include <base/stringprintf.h>
12
David Zeuthenf413fe52013-04-22 14:04:39 -070013#include "update_engine/clock.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070014#include "update_engine/constants.h"
Alex Deymo42432912013-07-12 20:21:15 -070015#include "update_engine/hardware_interface.h"
16#include "update_engine/install_plan.h"
Jay Srinivasan19409b72013-04-12 19:23:36 -070017#include "update_engine/prefs.h"
18#include "update_engine/system_state.h"
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080019#include "update_engine/utils.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080020
Jay Srinivasan08262882012-12-28 19:29:43 -080021using base::Time;
22using base::TimeDelta;
23using std::min;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080024using std::string;
25
26namespace chromeos_update_engine {
27
David Zeuthen9a017f22013-04-11 16:10:26 -070028const TimeDelta PayloadState::kDurationSlack = TimeDelta::FromSeconds(600);
29
Jay Srinivasan08262882012-12-28 19:29:43 -080030// We want to upperbound backoffs to 16 days
Alex Deymo820cc702013-06-28 15:43:46 -070031static const int kMaxBackoffDays = 16;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080032
Jay Srinivasan08262882012-12-28 19:29:43 -080033// We want to randomize retry attempts after the backoff by +/- 6 hours.
34static const uint32_t kMaxBackoffFuzzMinutes = 12 * 60;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080035
Jay Srinivasan19409b72013-04-12 19:23:36 -070036PayloadState::PayloadState()
37 : prefs_(NULL),
David Zeuthenbb8bdc72013-09-03 13:43:48 -070038 using_p2p_for_downloading_(false),
Jay Srinivasan19409b72013-04-12 19:23:36 -070039 payload_attempt_number_(0),
Alex Deymo820cc702013-06-28 15:43:46 -070040 full_payload_attempt_number_(0),
Jay Srinivasan19409b72013-04-12 19:23:36 -070041 url_index_(0),
David Zeuthencc6f9962013-04-18 11:57:24 -070042 url_failure_count_(0),
David Zeuthendcba8092013-08-06 12:16:35 -070043 url_switch_count_(0),
44 p2p_num_attempts_(0) {
Jay Srinivasan19409b72013-04-12 19:23:36 -070045 for (int i = 0; i <= kNumDownloadSources; i++)
46 total_bytes_downloaded_[i] = current_bytes_downloaded_[i] = 0;
47}
48
49bool PayloadState::Initialize(SystemState* system_state) {
50 system_state_ = system_state;
51 prefs_ = system_state_->prefs();
Chris Sosaaa18e162013-06-20 13:20:30 -070052 powerwash_safe_prefs_ = system_state_->powerwash_safe_prefs();
Jay Srinivasan08262882012-12-28 19:29:43 -080053 LoadResponseSignature();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080054 LoadPayloadAttemptNumber();
Alex Deymo820cc702013-06-28 15:43:46 -070055 LoadFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080056 LoadUrlIndex();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080057 LoadUrlFailureCount();
David Zeuthencc6f9962013-04-18 11:57:24 -070058 LoadUrlSwitchCount();
Jay Srinivasan08262882012-12-28 19:29:43 -080059 LoadBackoffExpiryTime();
David Zeuthen9a017f22013-04-11 16:10:26 -070060 LoadUpdateTimestampStart();
61 // The LoadUpdateDurationUptime() method relies on LoadUpdateTimestampStart()
62 // being called before it. Don't reorder.
63 LoadUpdateDurationUptime();
Jay Srinivasan19409b72013-04-12 19:23:36 -070064 for (int i = 0; i < kNumDownloadSources; i++) {
65 DownloadSource source = static_cast<DownloadSource>(i);
66 LoadCurrentBytesDownloaded(source);
67 LoadTotalBytesDownloaded(source);
68 }
Chris Sosabe45bef2013-04-09 18:25:12 -070069 LoadNumReboots();
David Zeuthena573d6f2013-06-14 16:13:36 -070070 LoadNumResponsesSeen();
Chris Sosaaa18e162013-06-20 13:20:30 -070071 LoadRollbackVersion();
David Zeuthendcba8092013-08-06 12:16:35 -070072 LoadP2PFirstAttemptTimestamp();
73 LoadP2PNumAttempts();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080074 return true;
75}
76
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080077void PayloadState::SetResponse(const OmahaResponse& omaha_response) {
Jay Srinivasan08262882012-12-28 19:29:43 -080078 // Always store the latest response.
79 response_ = omaha_response;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080080
Jay Srinivasan53173b92013-05-17 17:13:01 -070081 // Compute the candidate URLs first as they are used to calculate the
82 // response signature so that a change in enterprise policy for
83 // HTTP downloads being enabled or not could be honored as soon as the
84 // next update check happens.
85 ComputeCandidateUrls();
86
Jay Srinivasan08262882012-12-28 19:29:43 -080087 // Check if the "signature" of this response (i.e. the fields we care about)
88 // has changed.
89 string new_response_signature = CalculateResponseSignature();
90 bool has_response_changed = (response_signature_ != new_response_signature);
91
92 // If the response has changed, we should persist the new signature and
93 // clear away all the existing state.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080094 if (has_response_changed) {
Jay Srinivasan08262882012-12-28 19:29:43 -080095 LOG(INFO) << "Resetting all persisted state as this is a new response";
David Zeuthena573d6f2013-06-14 16:13:36 -070096 SetNumResponsesSeen(num_responses_seen_ + 1);
Jay Srinivasan08262882012-12-28 19:29:43 -080097 SetResponseSignature(new_response_signature);
98 ResetPersistedState();
Alex Deymob33b0f02013-08-08 21:10:02 -070099 ReportUpdatesAbandonedEventCountMetric();
Jay Srinivasan08262882012-12-28 19:29:43 -0800100 return;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800101 }
102
Jay Srinivasan08262882012-12-28 19:29:43 -0800103 // This is the earliest point at which we can validate whether the URL index
104 // we loaded from the persisted state is a valid value. If the response
105 // hasn't changed but the URL index is invalid, it's indicative of some
106 // tampering of the persisted state.
Jay Srinivasan53173b92013-05-17 17:13:01 -0700107 if (static_cast<uint32_t>(url_index_) >= candidate_urls_.size()) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800108 LOG(INFO) << "Resetting all payload state as the url index seems to have "
109 "been tampered with";
110 ResetPersistedState();
111 return;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800112 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700113
114 // Update the current download source which depends on the latest value of
115 // the response.
116 UpdateCurrentDownloadSource();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800117}
118
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700119void PayloadState::SetUsingP2PForDownloading(bool value) {
120 using_p2p_for_downloading_ = value;
121 // Update the current download source which depends on whether we are
122 // using p2p or not.
123 UpdateCurrentDownloadSource();
124}
125
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800126void PayloadState::DownloadComplete() {
127 LOG(INFO) << "Payload downloaded successfully";
128 IncrementPayloadAttemptNumber();
Alex Deymo820cc702013-06-28 15:43:46 -0700129 IncrementFullPayloadAttemptNumber();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800130}
131
132void PayloadState::DownloadProgress(size_t count) {
133 if (count == 0)
134 return;
135
David Zeuthen9a017f22013-04-11 16:10:26 -0700136 CalculateUpdateDurationUptime();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700137 UpdateBytesDownloaded(count);
David Zeuthen9a017f22013-04-11 16:10:26 -0700138
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800139 // We've received non-zero bytes from a recent download operation. Since our
140 // URL failure count is meant to penalize a URL only for consecutive
141 // failures, downloading bytes successfully means we should reset the failure
142 // count (as we know at least that the URL is working). In future, we can
143 // design this to be more sophisticated to check for more intelligent failure
144 // patterns, but right now, even 1 byte downloaded will mark the URL to be
145 // good unless it hits 10 (or configured number of) consecutive failures
146 // again.
147
148 if (GetUrlFailureCount() == 0)
149 return;
150
151 LOG(INFO) << "Resetting failure count of Url" << GetUrlIndex()
152 << " to 0 as we received " << count << " bytes successfully";
153 SetUrlFailureCount(0);
154}
155
Chris Sosabe45bef2013-04-09 18:25:12 -0700156void PayloadState::UpdateResumed() {
157 LOG(INFO) << "Resuming an update that was previously started.";
158 UpdateNumReboots();
159}
160
Jay Srinivasan19409b72013-04-12 19:23:36 -0700161void PayloadState::UpdateRestarted() {
162 LOG(INFO) << "Starting a new update";
163 ResetDownloadSourcesOnNewUpdate();
Chris Sosabe45bef2013-04-09 18:25:12 -0700164 SetNumReboots(0);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700165}
166
David Zeuthen9a017f22013-04-11 16:10:26 -0700167void PayloadState::UpdateSucceeded() {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700168 // Send the relevant metrics that are tracked in this class to UMA.
David Zeuthen9a017f22013-04-11 16:10:26 -0700169 CalculateUpdateDurationUptime();
David Zeuthenf413fe52013-04-22 14:04:39 -0700170 SetUpdateTimestampEnd(system_state_->clock()->GetWallclockTime());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700171 ReportBytesDownloadedMetrics();
David Zeuthencc6f9962013-04-18 11:57:24 -0700172 ReportUpdateUrlSwitchesMetric();
Chris Sosabe45bef2013-04-09 18:25:12 -0700173 ReportRebootMetrics();
David Zeuthen674c3182013-04-18 14:05:20 -0700174 ReportDurationMetrics();
David Zeuthena573d6f2013-06-14 16:13:36 -0700175 ReportUpdatesAbandonedCountMetric();
Alex Deymo1c656c42013-06-28 11:02:14 -0700176 ReportPayloadTypeMetric();
Alex Deymo820cc702013-06-28 15:43:46 -0700177 ReportAttemptsCountMetrics();
David Zeuthena573d6f2013-06-14 16:13:36 -0700178
179 // Reset the number of responses seen since it counts from the last
180 // successful update, e.g. now.
181 SetNumResponsesSeen(0);
David Zeuthene4c58bf2013-06-18 17:26:50 -0700182
183 CreateSystemUpdatedMarkerFile();
David Zeuthen9a017f22013-04-11 16:10:26 -0700184}
185
David Zeuthena99981f2013-04-29 13:42:47 -0700186void PayloadState::UpdateFailed(ErrorCode error) {
187 ErrorCode base_error = utils::GetBaseErrorCode(error);
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800188 LOG(INFO) << "Updating payload state for error code: " << base_error
189 << " (" << utils::CodeToString(base_error) << ")";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800190
Jay Srinivasan53173b92013-05-17 17:13:01 -0700191 if (candidate_urls_.size() == 0) {
192 // This means we got this error even before we got a valid Omaha response
193 // or don't have any valid candidates in the Omaha response.
Jay Srinivasan08262882012-12-28 19:29:43 -0800194 // So we should not advance the url_index_ in such cases.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800195 LOG(INFO) << "Ignoring failures until we get a valid Omaha response.";
196 return;
197 }
198
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800199 switch (base_error) {
200 // Errors which are good indicators of a problem with a particular URL or
201 // the protocol used in the URL or entities in the communication channel
202 // (e.g. proxies). We should try the next available URL in the next update
203 // check to quickly recover from these errors.
David Zeuthena99981f2013-04-29 13:42:47 -0700204 case kErrorCodePayloadHashMismatchError:
205 case kErrorCodePayloadSizeMismatchError:
206 case kErrorCodeDownloadPayloadVerificationError:
207 case kErrorCodeDownloadPayloadPubKeyVerificationError:
208 case kErrorCodeSignedDeltaPayloadExpectedError:
209 case kErrorCodeDownloadInvalidMetadataMagicString:
210 case kErrorCodeDownloadSignatureMissingInManifest:
211 case kErrorCodeDownloadManifestParseError:
212 case kErrorCodeDownloadMetadataSignatureError:
213 case kErrorCodeDownloadMetadataSignatureVerificationError:
214 case kErrorCodeDownloadMetadataSignatureMismatch:
215 case kErrorCodeDownloadOperationHashVerificationError:
216 case kErrorCodeDownloadOperationExecutionError:
217 case kErrorCodeDownloadOperationHashMismatch:
218 case kErrorCodeDownloadInvalidMetadataSize:
219 case kErrorCodeDownloadInvalidMetadataSignature:
220 case kErrorCodeDownloadOperationHashMissingError:
221 case kErrorCodeDownloadMetadataSignatureMissingError:
Gilad Arnold21504f02013-05-24 08:51:22 -0700222 case kErrorCodePayloadMismatchedType:
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800223 IncrementUrlIndex();
224 break;
225
226 // Errors which seem to be just transient network/communication related
227 // failures and do not indicate any inherent problem with the URL itself.
228 // So, we should keep the current URL but just increment the
229 // failure count to give it more chances. This way, while we maximize our
230 // chances of downloading from the URLs that appear earlier in the response
231 // (because download from a local server URL that appears earlier in a
232 // response is preferable than downloading from the next URL which could be
233 // a internet URL and thus could be more expensive).
David Zeuthena99981f2013-04-29 13:42:47 -0700234 case kErrorCodeError:
235 case kErrorCodeDownloadTransferError:
236 case kErrorCodeDownloadWriteError:
237 case kErrorCodeDownloadStateInitializationError:
238 case kErrorCodeOmahaErrorInHTTPResponse: // Aggregate code for HTTP errors.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800239 IncrementFailureCount();
240 break;
241
242 // Errors which are not specific to a URL and hence shouldn't result in
243 // the URL being penalized. This can happen in two cases:
244 // 1. We haven't started downloading anything: These errors don't cost us
245 // anything in terms of actual payload bytes, so we should just do the
246 // regular retries at the next update check.
247 // 2. We have successfully downloaded the payload: In this case, the
248 // payload attempt number would have been incremented and would take care
Jay Srinivasan08262882012-12-28 19:29:43 -0800249 // of the backoff at the next update check.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800250 // In either case, there's no need to update URL index or failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700251 case kErrorCodeOmahaRequestError:
252 case kErrorCodeOmahaResponseHandlerError:
253 case kErrorCodePostinstallRunnerError:
254 case kErrorCodeFilesystemCopierError:
255 case kErrorCodeInstallDeviceOpenError:
256 case kErrorCodeKernelDeviceOpenError:
257 case kErrorCodeDownloadNewPartitionInfoError:
258 case kErrorCodeNewRootfsVerificationError:
259 case kErrorCodeNewKernelVerificationError:
260 case kErrorCodePostinstallBootedFromFirmwareB:
Don Garrett81018e02013-07-30 18:46:31 -0700261 case kErrorCodePostinstallFirmwareRONotUpdatable:
David Zeuthena99981f2013-04-29 13:42:47 -0700262 case kErrorCodeOmahaRequestEmptyResponseError:
263 case kErrorCodeOmahaRequestXMLParseError:
264 case kErrorCodeOmahaResponseInvalid:
265 case kErrorCodeOmahaUpdateIgnoredPerPolicy:
266 case kErrorCodeOmahaUpdateDeferredPerPolicy:
267 case kErrorCodeOmahaUpdateDeferredForBackoff:
268 case kErrorCodePostinstallPowerwashError:
269 case kErrorCodeUpdateCanceledByChannelChange:
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800270 LOG(INFO) << "Not incrementing URL index or failure count for this error";
271 break;
272
David Zeuthena99981f2013-04-29 13:42:47 -0700273 case kErrorCodeSuccess: // success code
David Zeuthena99981f2013-04-29 13:42:47 -0700274 case kErrorCodeUmaReportedMax: // not an error code
275 case kErrorCodeOmahaRequestHTTPResponseBase: // aggregated already
276 case kErrorCodeDevModeFlag: // not an error code
277 case kErrorCodeResumedFlag: // not an error code
278 case kErrorCodeTestImageFlag: // not an error code
279 case kErrorCodeTestOmahaUrlFlag: // not an error code
280 case kErrorCodeSpecialFlags: // not an error code
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800281 // These shouldn't happen. Enumerating these explicitly here so that we
282 // can let the compiler warn about new error codes that are added to
283 // action_processor.h but not added here.
284 LOG(WARNING) << "Unexpected error code for UpdateFailed";
285 break;
286
287 // Note: Not adding a default here so as to let the compiler warn us of
288 // any new enums that were added in the .h but not listed in this switch.
289 }
290}
291
Jay Srinivasan08262882012-12-28 19:29:43 -0800292bool PayloadState::ShouldBackoffDownload() {
293 if (response_.disable_payload_backoff) {
294 LOG(INFO) << "Payload backoff logic is disabled. "
295 "Can proceed with the download";
296 return false;
297 }
298
299 if (response_.is_delta_payload) {
300 // If delta payloads fail, we want to fallback quickly to full payloads as
301 // they are more likely to succeed. Exponential backoffs would greatly
302 // slow down the fallback to full payloads. So we don't backoff for delta
303 // payloads.
304 LOG(INFO) << "No backoffs for delta payloads. "
305 << "Can proceed with the download";
306 return false;
307 }
308
309 if (!utils::IsOfficialBuild()) {
310 // Backoffs are needed only for official builds. We do not want any delays
311 // or update failures due to backoffs during testing or development.
312 LOG(INFO) << "No backoffs for test/dev images. "
313 << "Can proceed with the download";
314 return false;
315 }
316
317 if (backoff_expiry_time_.is_null()) {
318 LOG(INFO) << "No backoff expiry time has been set. "
319 << "Can proceed with the download";
320 return false;
321 }
322
323 if (backoff_expiry_time_ < Time::Now()) {
324 LOG(INFO) << "The backoff expiry time ("
325 << utils::ToString(backoff_expiry_time_)
326 << ") has elapsed. Can proceed with the download";
327 return false;
328 }
329
330 LOG(INFO) << "Cannot proceed with downloads as we need to backoff until "
331 << utils::ToString(backoff_expiry_time_);
332 return true;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800333}
334
Chris Sosaaa18e162013-06-20 13:20:30 -0700335void PayloadState::Rollback() {
336 SetRollbackVersion(system_state_->request_params()->app_version());
337}
338
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800339void PayloadState::IncrementPayloadAttemptNumber() {
Alex Deymo820cc702013-06-28 15:43:46 -0700340 // Update the payload attempt number for both payload types: full and delta.
341 SetPayloadAttemptNumber(GetPayloadAttemptNumber() + 1);
Alex Deymo29b51d92013-07-09 15:26:24 -0700342
343 // Report the metric every time the value is incremented.
344 string metric = "Installer.PayloadAttemptNumber";
345 int value = GetPayloadAttemptNumber();
346
347 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
348 system_state_->metrics_lib()->SendToUMA(
349 metric,
350 value,
351 1, // min value
352 50, // max value
353 kNumDefaultUmaBuckets);
Alex Deymo820cc702013-06-28 15:43:46 -0700354}
355
356void PayloadState::IncrementFullPayloadAttemptNumber() {
357 // Update the payload attempt number for full payloads and the backoff time.
Jay Srinivasan08262882012-12-28 19:29:43 -0800358 if (response_.is_delta_payload) {
359 LOG(INFO) << "Not incrementing payload attempt number for delta payloads";
360 return;
361 }
362
Alex Deymo29b51d92013-07-09 15:26:24 -0700363 LOG(INFO) << "Incrementing the full payload attempt number";
Alex Deymo820cc702013-06-28 15:43:46 -0700364 SetFullPayloadAttemptNumber(GetFullPayloadAttemptNumber() + 1);
Jay Srinivasan08262882012-12-28 19:29:43 -0800365 UpdateBackoffExpiryTime();
Alex Deymo29b51d92013-07-09 15:26:24 -0700366
367 // Report the metric every time the value is incremented.
368 string metric = "Installer.FullPayloadAttemptNumber";
369 int value = GetFullPayloadAttemptNumber();
370
371 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
372 system_state_->metrics_lib()->SendToUMA(
373 metric,
374 value,
375 1, // min value
376 50, // max value
377 kNumDefaultUmaBuckets);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800378}
379
380void PayloadState::IncrementUrlIndex() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800381 uint32_t next_url_index = GetUrlIndex() + 1;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700382 if (next_url_index < candidate_urls_.size()) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800383 LOG(INFO) << "Incrementing the URL index for next attempt";
384 SetUrlIndex(next_url_index);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800385 } else {
386 LOG(INFO) << "Resetting the current URL index (" << GetUrlIndex() << ") to "
Jay Srinivasan53173b92013-05-17 17:13:01 -0700387 << "0 as we only have " << candidate_urls_.size()
388 << " candidate URL(s)";
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800389 SetUrlIndex(0);
Alex Deymo29b51d92013-07-09 15:26:24 -0700390 IncrementPayloadAttemptNumber();
391 IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800392 }
Jay Srinivasan08262882012-12-28 19:29:43 -0800393
David Zeuthencc6f9962013-04-18 11:57:24 -0700394 // If we have multiple URLs, record that we just switched to another one
Jay Srinivasan53173b92013-05-17 17:13:01 -0700395 if (candidate_urls_.size() > 1)
David Zeuthencc6f9962013-04-18 11:57:24 -0700396 SetUrlSwitchCount(url_switch_count_ + 1);
397
Jay Srinivasan08262882012-12-28 19:29:43 -0800398 // Whenever we update the URL index, we should also clear the URL failure
399 // count so we can start over fresh for the new URL.
400 SetUrlFailureCount(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800401}
402
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800403void PayloadState::IncrementFailureCount() {
404 uint32_t next_url_failure_count = GetUrlFailureCount() + 1;
Jay Srinivasan08262882012-12-28 19:29:43 -0800405 if (next_url_failure_count < response_.max_failure_count_per_url) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800406 LOG(INFO) << "Incrementing the URL failure count";
407 SetUrlFailureCount(next_url_failure_count);
408 } else {
409 LOG(INFO) << "Reached max number of failures for Url" << GetUrlIndex()
410 << ". Trying next available URL";
411 IncrementUrlIndex();
412 }
413}
414
Jay Srinivasan08262882012-12-28 19:29:43 -0800415void PayloadState::UpdateBackoffExpiryTime() {
416 if (response_.disable_payload_backoff) {
417 LOG(INFO) << "Resetting backoff expiry time as payload backoff is disabled";
418 SetBackoffExpiryTime(Time());
419 return;
420 }
421
Alex Deymo820cc702013-06-28 15:43:46 -0700422 if (GetFullPayloadAttemptNumber() == 0) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800423 SetBackoffExpiryTime(Time());
424 return;
425 }
426
427 // Since we're doing left-shift below, make sure we don't shift more
Alex Deymo820cc702013-06-28 15:43:46 -0700428 // than this. E.g. if int is 4-bytes, don't left-shift more than 30 bits,
Jay Srinivasan08262882012-12-28 19:29:43 -0800429 // since we don't expect value of kMaxBackoffDays to be more than 100 anyway.
Alex Deymo820cc702013-06-28 15:43:46 -0700430 int num_days = 1; // the value to be shifted.
431 const int kMaxShifts = (sizeof(num_days) * 8) - 2;
Jay Srinivasan08262882012-12-28 19:29:43 -0800432
433 // Normal backoff days is 2 raised to (payload_attempt_number - 1).
434 // E.g. if payload_attempt_number is over 30, limit power to 30.
Alex Deymo820cc702013-06-28 15:43:46 -0700435 int power = min(GetFullPayloadAttemptNumber() - 1, kMaxShifts);
Jay Srinivasan08262882012-12-28 19:29:43 -0800436
437 // The number of days is the minimum of 2 raised to (payload_attempt_number
438 // - 1) or kMaxBackoffDays.
439 num_days = min(num_days << power, kMaxBackoffDays);
440
441 // We don't want all retries to happen exactly at the same time when
442 // retrying after backoff. So add some random minutes to fuzz.
443 int fuzz_minutes = utils::FuzzInt(0, kMaxBackoffFuzzMinutes);
444 TimeDelta next_backoff_interval = TimeDelta::FromDays(num_days) +
445 TimeDelta::FromMinutes(fuzz_minutes);
446 LOG(INFO) << "Incrementing the backoff expiry time by "
447 << utils::FormatTimeDelta(next_backoff_interval);
448 SetBackoffExpiryTime(Time::Now() + next_backoff_interval);
449}
450
Jay Srinivasan19409b72013-04-12 19:23:36 -0700451void PayloadState::UpdateCurrentDownloadSource() {
452 current_download_source_ = kNumDownloadSources;
453
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700454 if (using_p2p_for_downloading_) {
455 current_download_source_ = kDownloadSourceHttpPeer;
456 } else if (GetUrlIndex() < candidate_urls_.size()) {
Jay Srinivasan53173b92013-05-17 17:13:01 -0700457 string current_url = candidate_urls_[GetUrlIndex()];
Jay Srinivasan19409b72013-04-12 19:23:36 -0700458 if (StartsWithASCII(current_url, "https://", false))
459 current_download_source_ = kDownloadSourceHttpsServer;
460 else if (StartsWithASCII(current_url, "http://", false))
461 current_download_source_ = kDownloadSourceHttpServer;
462 }
463
464 LOG(INFO) << "Current download source: "
465 << utils::ToString(current_download_source_);
466}
467
468void PayloadState::UpdateBytesDownloaded(size_t count) {
469 SetCurrentBytesDownloaded(
470 current_download_source_,
471 GetCurrentBytesDownloaded(current_download_source_) + count,
472 false);
473 SetTotalBytesDownloaded(
474 current_download_source_,
475 GetTotalBytesDownloaded(current_download_source_) + count,
476 false);
477}
478
479void PayloadState::ReportBytesDownloadedMetrics() {
480 // Report metrics collected from all known download sources to UMA.
481 // The reported data is in Megabytes in order to represent a larger
482 // sample range.
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700483 int download_sources_used = 0;
484 string metric;
485 uint64_t successful_mbs = 0;
486 uint64_t total_mbs = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700487 for (int i = 0; i < kNumDownloadSources; i++) {
488 DownloadSource source = static_cast<DownloadSource>(i);
489 const int kMaxMiBs = 10240; // Anything above 10GB goes in the last bucket.
David Zeuthen44848602013-06-24 13:32:14 -0700490 uint64_t mbs;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700491
David Zeuthen44848602013-06-24 13:32:14 -0700492 // Only consider this download source (and send byte counts) as
493 // having been used if we downloaded a non-trivial amount of bytes
494 // (e.g. at least 1 MiB) that contributed to the final success of
495 // the update. Otherwise we're going to end up with a lot of
496 // zero-byte events in the histogram.
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700497
David Zeuthen44848602013-06-24 13:32:14 -0700498 mbs = GetCurrentBytesDownloaded(source) / kNumBytesInOneMiB;
499 if (mbs > 0) {
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700500 download_sources_used |= (1 << source);
501
David Zeuthen44848602013-06-24 13:32:14 -0700502 metric = "Installer.SuccessfulMBsDownloadedFrom" +
503 utils::ToString(source);
504 successful_mbs += mbs;
505 LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric;
506 system_state_->metrics_lib()->SendToUMA(metric,
507 mbs,
508 0, // min
509 kMaxMiBs,
510 kNumDefaultUmaBuckets);
511 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700512 SetCurrentBytesDownloaded(source, 0, true);
513
Jay Srinivasan19409b72013-04-12 19:23:36 -0700514 mbs = GetTotalBytesDownloaded(source) / kNumBytesInOneMiB;
David Zeuthen44848602013-06-24 13:32:14 -0700515 if (mbs > 0) {
516 metric = "Installer.TotalMBsDownloadedFrom" + utils::ToString(source);
517 total_mbs += mbs;
518 LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric;
519 system_state_->metrics_lib()->SendToUMA(metric,
520 mbs,
521 0, // min
522 kMaxMiBs,
523 kNumDefaultUmaBuckets);
524 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700525 SetTotalBytesDownloaded(source, 0, true);
526 }
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700527
528 metric = "Installer.DownloadSourcesUsed";
529 LOG(INFO) << "Uploading 0x" << std::hex << download_sources_used
530 << " (bit flags) for metric " << metric;
531 int num_buckets = std::min(1 << kNumDownloadSources, kNumDefaultUmaBuckets);
532 system_state_->metrics_lib()->SendToUMA(metric,
533 download_sources_used,
534 0, // min
535 1 << kNumDownloadSources,
536 num_buckets);
537
538 if (successful_mbs) {
539 metric = "Installer.DownloadOverheadPercentage";
540 int percent_overhead = (total_mbs - successful_mbs) * 100 / successful_mbs;
541 LOG(INFO) << "Uploading " << percent_overhead << "% for metric " << metric;
542 system_state_->metrics_lib()->SendToUMA(metric,
543 percent_overhead,
544 0, // min: 0% overhead
545 1000, // max: 1000% overhead
546 kNumDefaultUmaBuckets);
547 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700548}
549
David Zeuthencc6f9962013-04-18 11:57:24 -0700550void PayloadState::ReportUpdateUrlSwitchesMetric() {
551 string metric = "Installer.UpdateURLSwitches";
552 int value = static_cast<int>(url_switch_count_);
553
554 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
555 system_state_->metrics_lib()->SendToUMA(
556 metric,
557 value,
558 0, // min value
559 100, // max value
560 kNumDefaultUmaBuckets);
561}
562
Chris Sosabe45bef2013-04-09 18:25:12 -0700563void PayloadState::ReportRebootMetrics() {
564 // Report the number of num_reboots.
565 string metric = "Installer.UpdateNumReboots";
566 uint32_t num_reboots = GetNumReboots();
567 LOG(INFO) << "Uploading reboot count of " << num_reboots << " for metric "
568 << metric;
569 system_state_->metrics_lib()->SendToUMA(
570 metric,
571 static_cast<int>(num_reboots), // sample
572 0, // min = 0.
573 50, // max
574 25); // buckets
575 SetNumReboots(0);
576}
577
578void PayloadState::UpdateNumReboots() {
579 // We only update the reboot count when the system has been detected to have
580 // been rebooted.
581 if (!system_state_->system_rebooted()) {
582 return;
583 }
584
585 SetNumReboots(GetNumReboots() + 1);
586}
587
588void PayloadState::SetNumReboots(uint32_t num_reboots) {
589 CHECK(prefs_);
590 num_reboots_ = num_reboots;
591 prefs_->SetInt64(kPrefsNumReboots, num_reboots);
592 LOG(INFO) << "Number of Reboots during current update attempt = "
593 << num_reboots_;
594}
595
Jay Srinivasan08262882012-12-28 19:29:43 -0800596void PayloadState::ResetPersistedState() {
597 SetPayloadAttemptNumber(0);
Alex Deymo820cc702013-06-28 15:43:46 -0700598 SetFullPayloadAttemptNumber(0);
Jay Srinivasan08262882012-12-28 19:29:43 -0800599 SetUrlIndex(0);
600 SetUrlFailureCount(0);
David Zeuthencc6f9962013-04-18 11:57:24 -0700601 SetUrlSwitchCount(0);
Jay Srinivasan08262882012-12-28 19:29:43 -0800602 UpdateBackoffExpiryTime(); // This will reset the backoff expiry time.
David Zeuthenf413fe52013-04-22 14:04:39 -0700603 SetUpdateTimestampStart(system_state_->clock()->GetWallclockTime());
David Zeuthen9a017f22013-04-11 16:10:26 -0700604 SetUpdateTimestampEnd(Time()); // Set to null time
605 SetUpdateDurationUptime(TimeDelta::FromSeconds(0));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700606 ResetDownloadSourcesOnNewUpdate();
Chris Sosaaa18e162013-06-20 13:20:30 -0700607 ResetRollbackVersion();
David Zeuthendcba8092013-08-06 12:16:35 -0700608 SetP2PNumAttempts(0);
609 SetP2PFirstAttemptTimestamp(Time()); // Set to null time
Chris Sosaaa18e162013-06-20 13:20:30 -0700610}
611
612void PayloadState::ResetRollbackVersion() {
613 CHECK(powerwash_safe_prefs_);
614 rollback_version_ = "";
615 powerwash_safe_prefs_->Delete(kPrefsRollbackVersion);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700616}
617
618void PayloadState::ResetDownloadSourcesOnNewUpdate() {
619 for (int i = 0; i < kNumDownloadSources; i++) {
620 DownloadSource source = static_cast<DownloadSource>(i);
621 SetCurrentBytesDownloaded(source, 0, true);
622 // Note: Not resetting the TotalBytesDownloaded as we want that metric
623 // to count the bytes downloaded across various update attempts until
624 // we have successfully applied the update.
625 }
626}
627
Chris Sosab3dcdb32013-09-04 15:22:12 -0700628int64_t PayloadState::GetPersistedValue(const string& key) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700629 CHECK(prefs_);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700630 if (!prefs_->Exists(key))
Jay Srinivasan19409b72013-04-12 19:23:36 -0700631 return 0;
632
633 int64_t stored_value;
Chris Sosab3dcdb32013-09-04 15:22:12 -0700634 if (!prefs_->GetInt64(key, &stored_value))
Jay Srinivasan19409b72013-04-12 19:23:36 -0700635 return 0;
636
637 if (stored_value < 0) {
638 LOG(ERROR) << key << ": Invalid value (" << stored_value
639 << ") in persisted state. Defaulting to 0";
640 return 0;
641 }
642
643 return stored_value;
Jay Srinivasan08262882012-12-28 19:29:43 -0800644}
645
646string PayloadState::CalculateResponseSignature() {
647 string response_sign = StringPrintf("NumURLs = %d\n",
Jay Srinivasan53173b92013-05-17 17:13:01 -0700648 candidate_urls_.size());
Jay Srinivasan08262882012-12-28 19:29:43 -0800649
Jay Srinivasan53173b92013-05-17 17:13:01 -0700650 for (size_t i = 0; i < candidate_urls_.size(); i++)
651 response_sign += StringPrintf("Candidate Url%d = %s\n",
652 i, candidate_urls_[i].c_str());
Jay Srinivasan08262882012-12-28 19:29:43 -0800653
654 response_sign += StringPrintf("Payload Size = %llu\n"
655 "Payload Sha256 Hash = %s\n"
656 "Metadata Size = %llu\n"
657 "Metadata Signature = %s\n"
658 "Is Delta Payload = %d\n"
659 "Max Failure Count Per Url = %d\n"
660 "Disable Payload Backoff = %d\n",
661 response_.size,
662 response_.hash.c_str(),
663 response_.metadata_size,
664 response_.metadata_signature.c_str(),
665 response_.is_delta_payload,
666 response_.max_failure_count_per_url,
667 response_.disable_payload_backoff);
668 return response_sign;
669}
670
671void PayloadState::LoadResponseSignature() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800672 CHECK(prefs_);
673 string stored_value;
Jay Srinivasan08262882012-12-28 19:29:43 -0800674 if (prefs_->Exists(kPrefsCurrentResponseSignature) &&
675 prefs_->GetString(kPrefsCurrentResponseSignature, &stored_value)) {
676 SetResponseSignature(stored_value);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800677 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800678}
679
Jay Srinivasan19409b72013-04-12 19:23:36 -0700680void PayloadState::SetResponseSignature(const string& response_signature) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800681 CHECK(prefs_);
682 response_signature_ = response_signature;
683 LOG(INFO) << "Current Response Signature = \n" << response_signature_;
684 prefs_->SetString(kPrefsCurrentResponseSignature, response_signature_);
685}
686
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800687void PayloadState::LoadPayloadAttemptNumber() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700688 SetPayloadAttemptNumber(GetPersistedValue(kPrefsPayloadAttemptNumber));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800689}
690
Alex Deymo820cc702013-06-28 15:43:46 -0700691void PayloadState::LoadFullPayloadAttemptNumber() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700692 SetFullPayloadAttemptNumber(GetPersistedValue(
693 kPrefsFullPayloadAttemptNumber));
Alex Deymo820cc702013-06-28 15:43:46 -0700694}
695
696void PayloadState::SetPayloadAttemptNumber(int payload_attempt_number) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800697 CHECK(prefs_);
698 payload_attempt_number_ = payload_attempt_number;
699 LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number_;
700 prefs_->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number_);
701}
702
Alex Deymo820cc702013-06-28 15:43:46 -0700703void PayloadState::SetFullPayloadAttemptNumber(
704 int full_payload_attempt_number) {
705 CHECK(prefs_);
706 full_payload_attempt_number_ = full_payload_attempt_number;
707 LOG(INFO) << "Full Payload Attempt Number = " << full_payload_attempt_number_;
708 prefs_->SetInt64(kPrefsFullPayloadAttemptNumber,
709 full_payload_attempt_number_);
710}
711
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800712void PayloadState::LoadUrlIndex() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700713 SetUrlIndex(GetPersistedValue(kPrefsCurrentUrlIndex));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800714}
715
716void PayloadState::SetUrlIndex(uint32_t url_index) {
717 CHECK(prefs_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800718 url_index_ = url_index;
719 LOG(INFO) << "Current URL Index = " << url_index_;
720 prefs_->SetInt64(kPrefsCurrentUrlIndex, url_index_);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700721
722 // Also update the download source, which is purely dependent on the
723 // current URL index alone.
724 UpdateCurrentDownloadSource();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800725}
726
David Zeuthencc6f9962013-04-18 11:57:24 -0700727void PayloadState::LoadUrlSwitchCount() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700728 SetUrlSwitchCount(GetPersistedValue(kPrefsUrlSwitchCount));
David Zeuthencc6f9962013-04-18 11:57:24 -0700729}
730
731void PayloadState::SetUrlSwitchCount(uint32_t url_switch_count) {
732 CHECK(prefs_);
733 url_switch_count_ = url_switch_count;
734 LOG(INFO) << "URL Switch Count = " << url_switch_count_;
735 prefs_->SetInt64(kPrefsUrlSwitchCount, url_switch_count_);
736}
737
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800738void PayloadState::LoadUrlFailureCount() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700739 SetUrlFailureCount(GetPersistedValue(kPrefsCurrentUrlFailureCount));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800740}
741
742void PayloadState::SetUrlFailureCount(uint32_t url_failure_count) {
743 CHECK(prefs_);
744 url_failure_count_ = url_failure_count;
745 LOG(INFO) << "Current URL (Url" << GetUrlIndex()
746 << ")'s Failure Count = " << url_failure_count_;
747 prefs_->SetInt64(kPrefsCurrentUrlFailureCount, url_failure_count_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800748}
749
Jay Srinivasan08262882012-12-28 19:29:43 -0800750void PayloadState::LoadBackoffExpiryTime() {
751 CHECK(prefs_);
752 int64_t stored_value;
753 if (!prefs_->Exists(kPrefsBackoffExpiryTime))
754 return;
755
756 if (!prefs_->GetInt64(kPrefsBackoffExpiryTime, &stored_value))
757 return;
758
759 Time stored_time = Time::FromInternalValue(stored_value);
760 if (stored_time > Time::Now() + TimeDelta::FromDays(kMaxBackoffDays)) {
761 LOG(ERROR) << "Invalid backoff expiry time ("
762 << utils::ToString(stored_time)
763 << ") in persisted state. Resetting.";
764 stored_time = Time();
765 }
766 SetBackoffExpiryTime(stored_time);
767}
768
769void PayloadState::SetBackoffExpiryTime(const Time& new_time) {
770 CHECK(prefs_);
771 backoff_expiry_time_ = new_time;
772 LOG(INFO) << "Backoff Expiry Time = "
773 << utils::ToString(backoff_expiry_time_);
774 prefs_->SetInt64(kPrefsBackoffExpiryTime,
775 backoff_expiry_time_.ToInternalValue());
776}
777
David Zeuthen9a017f22013-04-11 16:10:26 -0700778TimeDelta PayloadState::GetUpdateDuration() {
David Zeuthenf413fe52013-04-22 14:04:39 -0700779 Time end_time = update_timestamp_end_.is_null()
780 ? system_state_->clock()->GetWallclockTime() :
781 update_timestamp_end_;
David Zeuthen9a017f22013-04-11 16:10:26 -0700782 return end_time - update_timestamp_start_;
783}
784
785void PayloadState::LoadUpdateTimestampStart() {
786 int64_t stored_value;
787 Time stored_time;
788
789 CHECK(prefs_);
790
David Zeuthenf413fe52013-04-22 14:04:39 -0700791 Time now = system_state_->clock()->GetWallclockTime();
David Zeuthen9a017f22013-04-11 16:10:26 -0700792
793 if (!prefs_->Exists(kPrefsUpdateTimestampStart)) {
794 // The preference missing is not unexpected - in that case, just
795 // use the current time as start time
796 stored_time = now;
797 } else if (!prefs_->GetInt64(kPrefsUpdateTimestampStart, &stored_value)) {
798 LOG(ERROR) << "Invalid UpdateTimestampStart value. Resetting.";
799 stored_time = now;
800 } else {
801 stored_time = Time::FromInternalValue(stored_value);
802 }
803
804 // Sanity check: If the time read from disk is in the future
805 // (modulo some slack to account for possible NTP drift
806 // adjustments), something is fishy and we should report and
807 // reset.
808 TimeDelta duration_according_to_stored_time = now - stored_time;
809 if (duration_according_to_stored_time < -kDurationSlack) {
810 LOG(ERROR) << "The UpdateTimestampStart value ("
811 << utils::ToString(stored_time)
812 << ") in persisted state is "
David Zeuthen674c3182013-04-18 14:05:20 -0700813 << utils::FormatTimeDelta(duration_according_to_stored_time)
814 << " in the future. Resetting.";
David Zeuthen9a017f22013-04-11 16:10:26 -0700815 stored_time = now;
816 }
817
818 SetUpdateTimestampStart(stored_time);
819}
820
821void PayloadState::SetUpdateTimestampStart(const Time& value) {
822 CHECK(prefs_);
823 update_timestamp_start_ = value;
824 prefs_->SetInt64(kPrefsUpdateTimestampStart,
825 update_timestamp_start_.ToInternalValue());
826 LOG(INFO) << "Update Timestamp Start = "
827 << utils::ToString(update_timestamp_start_);
828}
829
830void PayloadState::SetUpdateTimestampEnd(const Time& value) {
831 update_timestamp_end_ = value;
832 LOG(INFO) << "Update Timestamp End = "
833 << utils::ToString(update_timestamp_end_);
834}
835
836TimeDelta PayloadState::GetUpdateDurationUptime() {
837 return update_duration_uptime_;
838}
839
840void PayloadState::LoadUpdateDurationUptime() {
841 int64_t stored_value;
842 TimeDelta stored_delta;
843
844 CHECK(prefs_);
845
846 if (!prefs_->Exists(kPrefsUpdateDurationUptime)) {
847 // The preference missing is not unexpected - in that case, just
848 // we'll use zero as the delta
849 } else if (!prefs_->GetInt64(kPrefsUpdateDurationUptime, &stored_value)) {
850 LOG(ERROR) << "Invalid UpdateDurationUptime value. Resetting.";
851 stored_delta = TimeDelta::FromSeconds(0);
852 } else {
853 stored_delta = TimeDelta::FromInternalValue(stored_value);
854 }
855
856 // Sanity-check: Uptime can never be greater than the wall-clock
857 // difference (modulo some slack). If it is, report and reset
858 // to the wall-clock difference.
859 TimeDelta diff = GetUpdateDuration() - stored_delta;
860 if (diff < -kDurationSlack) {
861 LOG(ERROR) << "The UpdateDurationUptime value ("
David Zeuthen674c3182013-04-18 14:05:20 -0700862 << utils::FormatTimeDelta(stored_delta)
David Zeuthen9a017f22013-04-11 16:10:26 -0700863 << ") in persisted state is "
David Zeuthen674c3182013-04-18 14:05:20 -0700864 << utils::FormatTimeDelta(diff)
865 << " larger than the wall-clock delta. Resetting.";
David Zeuthen9a017f22013-04-11 16:10:26 -0700866 stored_delta = update_duration_current_;
867 }
868
869 SetUpdateDurationUptime(stored_delta);
870}
871
Chris Sosabe45bef2013-04-09 18:25:12 -0700872void PayloadState::LoadNumReboots() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700873 SetNumReboots(GetPersistedValue(kPrefsNumReboots));
Chris Sosaaa18e162013-06-20 13:20:30 -0700874}
875
876void PayloadState::LoadRollbackVersion() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700877 CHECK(powerwash_safe_prefs_);
878 string rollback_version;
879 if (powerwash_safe_prefs_->GetString(kPrefsRollbackVersion,
880 &rollback_version)) {
881 SetRollbackVersion(rollback_version);
882 }
Chris Sosaaa18e162013-06-20 13:20:30 -0700883}
884
885void PayloadState::SetRollbackVersion(const string& rollback_version) {
886 CHECK(powerwash_safe_prefs_);
887 LOG(INFO) << "Blacklisting version "<< rollback_version;
888 rollback_version_ = rollback_version;
889 powerwash_safe_prefs_->SetString(kPrefsRollbackVersion, rollback_version);
Chris Sosabe45bef2013-04-09 18:25:12 -0700890}
891
David Zeuthen9a017f22013-04-11 16:10:26 -0700892void PayloadState::SetUpdateDurationUptimeExtended(const TimeDelta& value,
893 const Time& timestamp,
894 bool use_logging) {
895 CHECK(prefs_);
896 update_duration_uptime_ = value;
897 update_duration_uptime_timestamp_ = timestamp;
898 prefs_->SetInt64(kPrefsUpdateDurationUptime,
899 update_duration_uptime_.ToInternalValue());
900 if (use_logging) {
901 LOG(INFO) << "Update Duration Uptime = "
David Zeuthen674c3182013-04-18 14:05:20 -0700902 << utils::FormatTimeDelta(update_duration_uptime_);
David Zeuthen9a017f22013-04-11 16:10:26 -0700903 }
904}
905
906void PayloadState::SetUpdateDurationUptime(const TimeDelta& value) {
David Zeuthenf413fe52013-04-22 14:04:39 -0700907 Time now = system_state_->clock()->GetMonotonicTime();
908 SetUpdateDurationUptimeExtended(value, now, true);
David Zeuthen9a017f22013-04-11 16:10:26 -0700909}
910
911void PayloadState::CalculateUpdateDurationUptime() {
David Zeuthenf413fe52013-04-22 14:04:39 -0700912 Time now = system_state_->clock()->GetMonotonicTime();
David Zeuthen9a017f22013-04-11 16:10:26 -0700913 TimeDelta uptime_since_last_update = now - update_duration_uptime_timestamp_;
914 TimeDelta new_uptime = update_duration_uptime_ + uptime_since_last_update;
915 // We're frequently called so avoid logging this write
916 SetUpdateDurationUptimeExtended(new_uptime, now, false);
917}
918
David Zeuthen674c3182013-04-18 14:05:20 -0700919void PayloadState::ReportDurationMetrics() {
920 TimeDelta duration = GetUpdateDuration();
921 TimeDelta duration_uptime = GetUpdateDurationUptime();
922 string metric;
923
924 metric = "Installer.UpdateDurationMinutes";
925 system_state_->metrics_lib()->SendToUMA(
926 metric,
927 static_cast<int>(duration.InMinutes()),
928 1, // min: 1 minute
929 365*24*60, // max: 1 year (approx)
930 kNumDefaultUmaBuckets);
931 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration)
932 << " for metric " << metric;
933
934 metric = "Installer.UpdateDurationUptimeMinutes";
935 system_state_->metrics_lib()->SendToUMA(
936 metric,
937 static_cast<int>(duration_uptime.InMinutes()),
938 1, // min: 1 minute
939 30*24*60, // max: 1 month (approx)
940 kNumDefaultUmaBuckets);
941 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration_uptime)
942 << " for metric " << metric;
943
944 prefs_->Delete(kPrefsUpdateTimestampStart);
945 prefs_->Delete(kPrefsUpdateDurationUptime);
946}
947
Alex Deymo1c656c42013-06-28 11:02:14 -0700948void PayloadState::ReportPayloadTypeMetric() {
949 string metric;
950 PayloadType uma_payload_type;
951 OmahaRequestParams* params = system_state_->request_params();
952
953 if (response_.is_delta_payload) {
954 uma_payload_type = kPayloadTypeDelta;
955 } else if (params->delta_okay()) {
956 uma_payload_type = kPayloadTypeFull;
957 } else { // Full payload, delta was not allowed by request.
958 uma_payload_type = kPayloadTypeForcedFull;
959 }
960
961 metric = "Installer.PayloadFormat";
962 system_state_->metrics_lib()->SendEnumToUMA(
963 metric,
964 uma_payload_type,
965 kNumPayloadTypes);
966 LOG(INFO) << "Uploading " << utils::ToString(uma_payload_type)
967 << " for metric " << metric;
968}
969
Alex Deymo820cc702013-06-28 15:43:46 -0700970void PayloadState::ReportAttemptsCountMetrics() {
971 string metric;
972 int total_attempts = GetPayloadAttemptNumber();
973
974 metric = "Installer.AttemptsCount.Total";
975 system_state_->metrics_lib()->SendToUMA(
976 metric,
977 total_attempts,
978 1, // min
979 50, // max
980 kNumDefaultUmaBuckets);
981 LOG(INFO) << "Uploading " << total_attempts
982 << " for metric " << metric;
983}
984
Jay Srinivasan19409b72013-04-12 19:23:36 -0700985string PayloadState::GetPrefsKey(const string& prefix, DownloadSource source) {
986 return prefix + "-from-" + utils::ToString(source);
987}
988
989void PayloadState::LoadCurrentBytesDownloaded(DownloadSource source) {
990 string key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700991 SetCurrentBytesDownloaded(source, GetPersistedValue(key), true);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700992}
993
994void PayloadState::SetCurrentBytesDownloaded(
995 DownloadSource source,
996 uint64_t current_bytes_downloaded,
997 bool log) {
998 CHECK(prefs_);
999
1000 if (source >= kNumDownloadSources)
1001 return;
1002
1003 // Update the in-memory value.
1004 current_bytes_downloaded_[source] = current_bytes_downloaded;
1005
1006 string prefs_key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source);
1007 prefs_->SetInt64(prefs_key, current_bytes_downloaded);
1008 LOG_IF(INFO, log) << "Current bytes downloaded for "
1009 << utils::ToString(source) << " = "
1010 << GetCurrentBytesDownloaded(source);
1011}
1012
1013void PayloadState::LoadTotalBytesDownloaded(DownloadSource source) {
1014 string key = GetPrefsKey(kPrefsTotalBytesDownloaded, source);
Chris Sosab3dcdb32013-09-04 15:22:12 -07001015 SetTotalBytesDownloaded(source, GetPersistedValue(key), true);
Jay Srinivasan19409b72013-04-12 19:23:36 -07001016}
1017
1018void PayloadState::SetTotalBytesDownloaded(
1019 DownloadSource source,
1020 uint64_t total_bytes_downloaded,
1021 bool log) {
1022 CHECK(prefs_);
1023
1024 if (source >= kNumDownloadSources)
1025 return;
1026
1027 // Update the in-memory value.
1028 total_bytes_downloaded_[source] = total_bytes_downloaded;
1029
1030 // Persist.
1031 string prefs_key = GetPrefsKey(kPrefsTotalBytesDownloaded, source);
1032 prefs_->SetInt64(prefs_key, total_bytes_downloaded);
1033 LOG_IF(INFO, log) << "Total bytes downloaded for "
1034 << utils::ToString(source) << " = "
1035 << GetTotalBytesDownloaded(source);
1036}
1037
David Zeuthena573d6f2013-06-14 16:13:36 -07001038void PayloadState::LoadNumResponsesSeen() {
Chris Sosab3dcdb32013-09-04 15:22:12 -07001039 SetNumResponsesSeen(GetPersistedValue(kPrefsNumResponsesSeen));
David Zeuthena573d6f2013-06-14 16:13:36 -07001040}
1041
1042void PayloadState::SetNumResponsesSeen(int num_responses_seen) {
1043 CHECK(prefs_);
1044 num_responses_seen_ = num_responses_seen;
1045 LOG(INFO) << "Num Responses Seen = " << num_responses_seen_;
1046 prefs_->SetInt64(kPrefsNumResponsesSeen, num_responses_seen_);
1047}
1048
1049void PayloadState::ReportUpdatesAbandonedCountMetric() {
1050 string metric = "Installer.UpdatesAbandonedCount";
1051 int value = num_responses_seen_ - 1;
1052
1053 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
1054 system_state_->metrics_lib()->SendToUMA(
1055 metric,
1056 value,
1057 0, // min value
1058 100, // max value
1059 kNumDefaultUmaBuckets);
1060}
1061
Alex Deymob33b0f02013-08-08 21:10:02 -07001062void PayloadState::ReportUpdatesAbandonedEventCountMetric() {
1063 string metric = "Installer.UpdatesAbandonedEventCount";
1064 int value = num_responses_seen_ - 1;
1065
1066 // Do not send an "abandoned" event when 0 payloads were abandoned since the
1067 // last successful update.
1068 if (value == 0)
1069 return;
1070
1071 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
1072 system_state_->metrics_lib()->SendToUMA(
1073 metric,
1074 value,
1075 0, // min value
1076 100, // max value
1077 kNumDefaultUmaBuckets);
1078}
1079
Jay Srinivasan53173b92013-05-17 17:13:01 -07001080void PayloadState::ComputeCandidateUrls() {
Chris Sosaf7d80042013-08-22 16:45:17 -07001081 bool http_url_ok = true;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001082
1083 if (system_state_->IsOfficialBuild()) {
1084 const policy::DevicePolicy* policy = system_state_->device_policy();
Chris Sosaf7d80042013-08-22 16:45:17 -07001085 if (policy && policy->GetHttpDownloadsEnabled(&http_url_ok) && !http_url_ok)
Jay Srinivasan53173b92013-05-17 17:13:01 -07001086 LOG(INFO) << "Downloads via HTTP Url are not enabled by device policy";
1087 } else {
1088 LOG(INFO) << "Allowing HTTP downloads for unofficial builds";
1089 http_url_ok = true;
1090 }
1091
1092 candidate_urls_.clear();
1093 for (size_t i = 0; i < response_.payload_urls.size(); i++) {
1094 string candidate_url = response_.payload_urls[i];
1095 if (StartsWithASCII(candidate_url, "http://", false) && !http_url_ok)
1096 continue;
1097 candidate_urls_.push_back(candidate_url);
1098 LOG(INFO) << "Candidate Url" << (candidate_urls_.size() - 1)
1099 << ": " << candidate_url;
1100 }
1101
1102 LOG(INFO) << "Found " << candidate_urls_.size() << " candidate URLs "
1103 << "out of " << response_.payload_urls.size() << " URLs supplied";
1104}
1105
David Zeuthene4c58bf2013-06-18 17:26:50 -07001106void PayloadState::CreateSystemUpdatedMarkerFile() {
1107 CHECK(prefs_);
1108 int64_t value = system_state_->clock()->GetWallclockTime().ToInternalValue();
1109 prefs_->SetInt64(kPrefsSystemUpdatedMarker, value);
1110}
1111
1112void PayloadState::BootedIntoUpdate(TimeDelta time_to_reboot) {
1113 // Send |time_to_reboot| as a UMA stat.
1114 string metric = "Installer.TimeToRebootMinutes";
1115 system_state_->metrics_lib()->SendToUMA(metric,
1116 time_to_reboot.InMinutes(),
1117 0, // min: 0 minute
1118 30*24*60, // max: 1 month (approx)
1119 kNumDefaultUmaBuckets);
1120 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(time_to_reboot)
1121 << " for metric " << metric;
1122}
1123
1124void PayloadState::UpdateEngineStarted() {
Alex Deymo569c4242013-07-24 12:01:01 -07001125 // Avoid the UpdateEngineStarted actions if this is not the first time we
1126 // run the update engine since reboot.
1127 if (!system_state_->system_rebooted())
1128 return;
1129
David Zeuthene4c58bf2013-06-18 17:26:50 -07001130 // Figure out if we just booted into a new update
1131 if (prefs_->Exists(kPrefsSystemUpdatedMarker)) {
1132 int64_t stored_value;
1133 if (prefs_->GetInt64(kPrefsSystemUpdatedMarker, &stored_value)) {
1134 Time system_updated_at = Time::FromInternalValue(stored_value);
1135 if (!system_updated_at.is_null()) {
1136 TimeDelta time_to_reboot =
1137 system_state_->clock()->GetWallclockTime() - system_updated_at;
1138 if (time_to_reboot.ToInternalValue() < 0) {
1139 LOG(ERROR) << "time_to_reboot is negative - system_updated_at: "
1140 << utils::ToString(system_updated_at);
1141 } else {
1142 BootedIntoUpdate(time_to_reboot);
1143 }
1144 }
1145 }
1146 prefs_->Delete(kPrefsSystemUpdatedMarker);
1147 }
Alex Deymo42432912013-07-12 20:21:15 -07001148 // Check if it is needed to send metrics about a failed reboot into a new
1149 // version.
1150 ReportFailedBootIfNeeded();
1151}
1152
1153void PayloadState::ReportFailedBootIfNeeded() {
1154 // If the kPrefsTargetVersionInstalledFrom is present, a successfully applied
1155 // payload was marked as ready immediately before the last reboot, and we
1156 // need to check if such payload successfully rebooted or not.
1157 if (prefs_->Exists(kPrefsTargetVersionInstalledFrom)) {
1158 string installed_from;
1159 if (!prefs_->GetString(kPrefsTargetVersionInstalledFrom, &installed_from)) {
1160 LOG(ERROR) << "Error reading TargetVersionInstalledFrom on reboot.";
1161 return;
1162 }
1163 if (installed_from ==
1164 utils::PartitionNumber(system_state_->hardware()->BootDevice())) {
1165 // A reboot was pending, but the chromebook is again in the same
1166 // BootDevice where the update was installed from.
1167 int64_t target_attempt;
1168 if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt)) {
1169 LOG(ERROR) << "Error reading TargetVersionAttempt when "
1170 "TargetVersionInstalledFrom was present.";
1171 target_attempt = 1;
1172 }
1173
1174 // Report the UMA metric of the current boot failure.
1175 string metric = "Installer.RebootToNewPartitionAttempt";
1176
1177 LOG(INFO) << "Uploading " << target_attempt
1178 << " (count) for metric " << metric;
1179 system_state_->metrics_lib()->SendToUMA(
1180 metric,
1181 target_attempt,
1182 1, // min value
1183 50, // max value
1184 kNumDefaultUmaBuckets);
1185 } else {
1186 prefs_->Delete(kPrefsTargetVersionAttempt);
1187 prefs_->Delete(kPrefsTargetVersionUniqueId);
1188 }
1189 prefs_->Delete(kPrefsTargetVersionInstalledFrom);
1190 }
1191}
1192
1193void PayloadState::ExpectRebootInNewVersion(const string& target_version_uid) {
1194 // Expect to boot into the new partition in the next reboot setting the
1195 // TargetVersion* flags in the Prefs.
1196 string stored_target_version_uid;
1197 string target_version_id;
1198 string target_partition;
1199 int64_t target_attempt;
1200
1201 if (prefs_->Exists(kPrefsTargetVersionUniqueId) &&
1202 prefs_->GetString(kPrefsTargetVersionUniqueId,
1203 &stored_target_version_uid) &&
1204 stored_target_version_uid == target_version_uid) {
1205 if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt))
1206 target_attempt = 0;
1207 } else {
1208 prefs_->SetString(kPrefsTargetVersionUniqueId, target_version_uid);
1209 target_attempt = 0;
1210 }
1211 prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt + 1);
1212
1213 prefs_->SetString(kPrefsTargetVersionInstalledFrom,
1214 utils::PartitionNumber(
1215 system_state_->hardware()->BootDevice()));
1216}
1217
1218void PayloadState::ResetUpdateStatus() {
1219 // Remove the TargetVersionInstalledFrom pref so that if the machine is
1220 // rebooted the next boot is not flagged as failed to rebooted into the
1221 // new applied payload.
1222 prefs_->Delete(kPrefsTargetVersionInstalledFrom);
1223
1224 // Also decrement the attempt number if it exists.
1225 int64_t target_attempt;
1226 if (prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt))
1227 prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt-1);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001228}
1229
David Zeuthendcba8092013-08-06 12:16:35 -07001230int PayloadState::GetP2PNumAttempts() {
1231 return p2p_num_attempts_;
1232}
1233
1234void PayloadState::SetP2PNumAttempts(int value) {
1235 p2p_num_attempts_ = value;
1236 LOG(INFO) << "p2p Num Attempts = " << p2p_num_attempts_;
1237 CHECK(prefs_);
1238 prefs_->SetInt64(kPrefsP2PNumAttempts, value);
1239}
1240
1241void PayloadState::LoadP2PNumAttempts() {
Chris Sosab3dcdb32013-09-04 15:22:12 -07001242 SetP2PNumAttempts(GetPersistedValue(kPrefsP2PNumAttempts));
David Zeuthendcba8092013-08-06 12:16:35 -07001243}
1244
1245Time PayloadState::GetP2PFirstAttemptTimestamp() {
1246 return p2p_first_attempt_timestamp_;
1247}
1248
1249void PayloadState::SetP2PFirstAttemptTimestamp(const Time& time) {
1250 p2p_first_attempt_timestamp_ = time;
1251 LOG(INFO) << "p2p First Attempt Timestamp = "
1252 << utils::ToString(p2p_first_attempt_timestamp_);
1253 CHECK(prefs_);
1254 int64_t stored_value = time.ToInternalValue();
1255 prefs_->SetInt64(kPrefsP2PFirstAttemptTimestamp, stored_value);
1256}
1257
1258void PayloadState::LoadP2PFirstAttemptTimestamp() {
Chris Sosab3dcdb32013-09-04 15:22:12 -07001259 int64_t stored_value = GetPersistedValue(kPrefsP2PFirstAttemptTimestamp);
David Zeuthendcba8092013-08-06 12:16:35 -07001260 Time stored_time = Time::FromInternalValue(stored_value);
1261 SetP2PFirstAttemptTimestamp(stored_time);
1262}
1263
1264void PayloadState::P2PNewAttempt() {
1265 CHECK(prefs_);
1266 // Set timestamp, if it hasn't been set already
1267 if (p2p_first_attempt_timestamp_.is_null()) {
1268 SetP2PFirstAttemptTimestamp(system_state_->clock()->GetWallclockTime());
1269 }
1270 // Increase number of attempts
1271 SetP2PNumAttempts(GetP2PNumAttempts() + 1);
1272}
1273
1274bool PayloadState::P2PAttemptAllowed() {
1275 if (p2p_num_attempts_ > kMaxP2PAttempts) {
1276 LOG(INFO) << "Number of p2p attempts is " << p2p_num_attempts_
1277 << " which is greater than "
1278 << kMaxP2PAttempts
1279 << " - disallowing p2p.";
1280 return false;
1281 }
1282
1283 if (!p2p_first_attempt_timestamp_.is_null()) {
1284 Time now = system_state_->clock()->GetWallclockTime();
1285 TimeDelta time_spent_attempting_p2p = now - p2p_first_attempt_timestamp_;
1286 if (time_spent_attempting_p2p.InSeconds() < 0) {
1287 LOG(ERROR) << "Time spent attempting p2p is negative"
1288 << " - disallowing p2p.";
1289 return false;
1290 }
1291 if (time_spent_attempting_p2p.InSeconds() > kMaxP2PAttemptTimeSeconds) {
1292 LOG(INFO) << "Time spent attempting p2p is "
1293 << utils::FormatTimeDelta(time_spent_attempting_p2p)
1294 << " which is greater than "
1295 << utils::FormatTimeDelta(TimeDelta::FromSeconds(
1296 kMaxP2PAttemptTimeSeconds))
1297 << " - disallowing p2p.";
1298 return false;
1299 }
1300 }
1301
1302 return true;
1303}
1304
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001305} // namespace chromeos_update_engine