blob: 162848a240f441d668d988647442c823dd7e9ec6 [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 }
Chris Sosa20f005c2013-09-05 13:53:08 -0700298 if (system_state_->request_params()->use_p2p_for_downloading() &&
299 !system_state_->request_params()->p2p_url().empty()) {
300 LOG(INFO) << "Payload backoff logic is disabled because download "
301 << "will happen from local peer (via p2p).";
302 return false;
303 }
304 if (system_state_->request_params()->interactive()) {
305 LOG(INFO) << "Payload backoff disabled for interactive update checks.";
306 return false;
307 }
Jay Srinivasan08262882012-12-28 19:29:43 -0800308 if (response_.is_delta_payload) {
309 // If delta payloads fail, we want to fallback quickly to full payloads as
310 // they are more likely to succeed. Exponential backoffs would greatly
311 // slow down the fallback to full payloads. So we don't backoff for delta
312 // payloads.
313 LOG(INFO) << "No backoffs for delta payloads. "
314 << "Can proceed with the download";
315 return false;
316 }
317
318 if (!utils::IsOfficialBuild()) {
319 // Backoffs are needed only for official builds. We do not want any delays
320 // or update failures due to backoffs during testing or development.
321 LOG(INFO) << "No backoffs for test/dev images. "
322 << "Can proceed with the download";
323 return false;
324 }
325
326 if (backoff_expiry_time_.is_null()) {
327 LOG(INFO) << "No backoff expiry time has been set. "
328 << "Can proceed with the download";
329 return false;
330 }
331
332 if (backoff_expiry_time_ < Time::Now()) {
333 LOG(INFO) << "The backoff expiry time ("
334 << utils::ToString(backoff_expiry_time_)
335 << ") has elapsed. Can proceed with the download";
336 return false;
337 }
338
339 LOG(INFO) << "Cannot proceed with downloads as we need to backoff until "
340 << utils::ToString(backoff_expiry_time_);
341 return true;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800342}
343
Chris Sosaaa18e162013-06-20 13:20:30 -0700344void PayloadState::Rollback() {
345 SetRollbackVersion(system_state_->request_params()->app_version());
346}
347
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800348void PayloadState::IncrementPayloadAttemptNumber() {
Alex Deymo820cc702013-06-28 15:43:46 -0700349 // Update the payload attempt number for both payload types: full and delta.
350 SetPayloadAttemptNumber(GetPayloadAttemptNumber() + 1);
Alex Deymo29b51d92013-07-09 15:26:24 -0700351
352 // Report the metric every time the value is incremented.
353 string metric = "Installer.PayloadAttemptNumber";
354 int value = GetPayloadAttemptNumber();
355
356 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
357 system_state_->metrics_lib()->SendToUMA(
358 metric,
359 value,
360 1, // min value
361 50, // max value
362 kNumDefaultUmaBuckets);
Alex Deymo820cc702013-06-28 15:43:46 -0700363}
364
365void PayloadState::IncrementFullPayloadAttemptNumber() {
366 // Update the payload attempt number for full payloads and the backoff time.
Jay Srinivasan08262882012-12-28 19:29:43 -0800367 if (response_.is_delta_payload) {
368 LOG(INFO) << "Not incrementing payload attempt number for delta payloads";
369 return;
370 }
371
Alex Deymo29b51d92013-07-09 15:26:24 -0700372 LOG(INFO) << "Incrementing the full payload attempt number";
Alex Deymo820cc702013-06-28 15:43:46 -0700373 SetFullPayloadAttemptNumber(GetFullPayloadAttemptNumber() + 1);
Jay Srinivasan08262882012-12-28 19:29:43 -0800374 UpdateBackoffExpiryTime();
Alex Deymo29b51d92013-07-09 15:26:24 -0700375
376 // Report the metric every time the value is incremented.
377 string metric = "Installer.FullPayloadAttemptNumber";
378 int value = GetFullPayloadAttemptNumber();
379
380 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
381 system_state_->metrics_lib()->SendToUMA(
382 metric,
383 value,
384 1, // min value
385 50, // max value
386 kNumDefaultUmaBuckets);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800387}
388
389void PayloadState::IncrementUrlIndex() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800390 uint32_t next_url_index = GetUrlIndex() + 1;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700391 if (next_url_index < candidate_urls_.size()) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800392 LOG(INFO) << "Incrementing the URL index for next attempt";
393 SetUrlIndex(next_url_index);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800394 } else {
395 LOG(INFO) << "Resetting the current URL index (" << GetUrlIndex() << ") to "
Jay Srinivasan53173b92013-05-17 17:13:01 -0700396 << "0 as we only have " << candidate_urls_.size()
397 << " candidate URL(s)";
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800398 SetUrlIndex(0);
Alex Deymo29b51d92013-07-09 15:26:24 -0700399 IncrementPayloadAttemptNumber();
400 IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800401 }
Jay Srinivasan08262882012-12-28 19:29:43 -0800402
David Zeuthencc6f9962013-04-18 11:57:24 -0700403 // If we have multiple URLs, record that we just switched to another one
Jay Srinivasan53173b92013-05-17 17:13:01 -0700404 if (candidate_urls_.size() > 1)
David Zeuthencc6f9962013-04-18 11:57:24 -0700405 SetUrlSwitchCount(url_switch_count_ + 1);
406
Jay Srinivasan08262882012-12-28 19:29:43 -0800407 // Whenever we update the URL index, we should also clear the URL failure
408 // count so we can start over fresh for the new URL.
409 SetUrlFailureCount(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800410}
411
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800412void PayloadState::IncrementFailureCount() {
413 uint32_t next_url_failure_count = GetUrlFailureCount() + 1;
Jay Srinivasan08262882012-12-28 19:29:43 -0800414 if (next_url_failure_count < response_.max_failure_count_per_url) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800415 LOG(INFO) << "Incrementing the URL failure count";
416 SetUrlFailureCount(next_url_failure_count);
417 } else {
418 LOG(INFO) << "Reached max number of failures for Url" << GetUrlIndex()
419 << ". Trying next available URL";
420 IncrementUrlIndex();
421 }
422}
423
Jay Srinivasan08262882012-12-28 19:29:43 -0800424void PayloadState::UpdateBackoffExpiryTime() {
425 if (response_.disable_payload_backoff) {
426 LOG(INFO) << "Resetting backoff expiry time as payload backoff is disabled";
427 SetBackoffExpiryTime(Time());
428 return;
429 }
430
Alex Deymo820cc702013-06-28 15:43:46 -0700431 if (GetFullPayloadAttemptNumber() == 0) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800432 SetBackoffExpiryTime(Time());
433 return;
434 }
435
436 // Since we're doing left-shift below, make sure we don't shift more
Alex Deymo820cc702013-06-28 15:43:46 -0700437 // than this. E.g. if int is 4-bytes, don't left-shift more than 30 bits,
Jay Srinivasan08262882012-12-28 19:29:43 -0800438 // since we don't expect value of kMaxBackoffDays to be more than 100 anyway.
Alex Deymo820cc702013-06-28 15:43:46 -0700439 int num_days = 1; // the value to be shifted.
440 const int kMaxShifts = (sizeof(num_days) * 8) - 2;
Jay Srinivasan08262882012-12-28 19:29:43 -0800441
442 // Normal backoff days is 2 raised to (payload_attempt_number - 1).
443 // E.g. if payload_attempt_number is over 30, limit power to 30.
Alex Deymo820cc702013-06-28 15:43:46 -0700444 int power = min(GetFullPayloadAttemptNumber() - 1, kMaxShifts);
Jay Srinivasan08262882012-12-28 19:29:43 -0800445
446 // The number of days is the minimum of 2 raised to (payload_attempt_number
447 // - 1) or kMaxBackoffDays.
448 num_days = min(num_days << power, kMaxBackoffDays);
449
450 // We don't want all retries to happen exactly at the same time when
451 // retrying after backoff. So add some random minutes to fuzz.
452 int fuzz_minutes = utils::FuzzInt(0, kMaxBackoffFuzzMinutes);
453 TimeDelta next_backoff_interval = TimeDelta::FromDays(num_days) +
454 TimeDelta::FromMinutes(fuzz_minutes);
455 LOG(INFO) << "Incrementing the backoff expiry time by "
456 << utils::FormatTimeDelta(next_backoff_interval);
457 SetBackoffExpiryTime(Time::Now() + next_backoff_interval);
458}
459
Jay Srinivasan19409b72013-04-12 19:23:36 -0700460void PayloadState::UpdateCurrentDownloadSource() {
461 current_download_source_ = kNumDownloadSources;
462
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700463 if (using_p2p_for_downloading_) {
464 current_download_source_ = kDownloadSourceHttpPeer;
465 } else if (GetUrlIndex() < candidate_urls_.size()) {
Jay Srinivasan53173b92013-05-17 17:13:01 -0700466 string current_url = candidate_urls_[GetUrlIndex()];
Jay Srinivasan19409b72013-04-12 19:23:36 -0700467 if (StartsWithASCII(current_url, "https://", false))
468 current_download_source_ = kDownloadSourceHttpsServer;
469 else if (StartsWithASCII(current_url, "http://", false))
470 current_download_source_ = kDownloadSourceHttpServer;
471 }
472
473 LOG(INFO) << "Current download source: "
474 << utils::ToString(current_download_source_);
475}
476
477void PayloadState::UpdateBytesDownloaded(size_t count) {
478 SetCurrentBytesDownloaded(
479 current_download_source_,
480 GetCurrentBytesDownloaded(current_download_source_) + count,
481 false);
482 SetTotalBytesDownloaded(
483 current_download_source_,
484 GetTotalBytesDownloaded(current_download_source_) + count,
485 false);
486}
487
488void PayloadState::ReportBytesDownloadedMetrics() {
489 // Report metrics collected from all known download sources to UMA.
490 // The reported data is in Megabytes in order to represent a larger
491 // sample range.
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700492 int download_sources_used = 0;
493 string metric;
494 uint64_t successful_mbs = 0;
495 uint64_t total_mbs = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700496 for (int i = 0; i < kNumDownloadSources; i++) {
497 DownloadSource source = static_cast<DownloadSource>(i);
498 const int kMaxMiBs = 10240; // Anything above 10GB goes in the last bucket.
David Zeuthen44848602013-06-24 13:32:14 -0700499 uint64_t mbs;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700500
David Zeuthen44848602013-06-24 13:32:14 -0700501 // Only consider this download source (and send byte counts) as
502 // having been used if we downloaded a non-trivial amount of bytes
503 // (e.g. at least 1 MiB) that contributed to the final success of
504 // the update. Otherwise we're going to end up with a lot of
505 // zero-byte events in the histogram.
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700506
David Zeuthen44848602013-06-24 13:32:14 -0700507 mbs = GetCurrentBytesDownloaded(source) / kNumBytesInOneMiB;
508 if (mbs > 0) {
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700509 download_sources_used |= (1 << source);
510
David Zeuthen44848602013-06-24 13:32:14 -0700511 metric = "Installer.SuccessfulMBsDownloadedFrom" +
512 utils::ToString(source);
513 successful_mbs += mbs;
514 LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric;
515 system_state_->metrics_lib()->SendToUMA(metric,
516 mbs,
517 0, // min
518 kMaxMiBs,
519 kNumDefaultUmaBuckets);
520 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700521 SetCurrentBytesDownloaded(source, 0, true);
522
Jay Srinivasan19409b72013-04-12 19:23:36 -0700523 mbs = GetTotalBytesDownloaded(source) / kNumBytesInOneMiB;
David Zeuthen44848602013-06-24 13:32:14 -0700524 if (mbs > 0) {
525 metric = "Installer.TotalMBsDownloadedFrom" + utils::ToString(source);
526 total_mbs += mbs;
527 LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric;
528 system_state_->metrics_lib()->SendToUMA(metric,
529 mbs,
530 0, // min
531 kMaxMiBs,
532 kNumDefaultUmaBuckets);
533 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700534 SetTotalBytesDownloaded(source, 0, true);
535 }
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700536
537 metric = "Installer.DownloadSourcesUsed";
538 LOG(INFO) << "Uploading 0x" << std::hex << download_sources_used
539 << " (bit flags) for metric " << metric;
540 int num_buckets = std::min(1 << kNumDownloadSources, kNumDefaultUmaBuckets);
541 system_state_->metrics_lib()->SendToUMA(metric,
542 download_sources_used,
543 0, // min
544 1 << kNumDownloadSources,
545 num_buckets);
546
547 if (successful_mbs) {
548 metric = "Installer.DownloadOverheadPercentage";
549 int percent_overhead = (total_mbs - successful_mbs) * 100 / successful_mbs;
550 LOG(INFO) << "Uploading " << percent_overhead << "% for metric " << metric;
551 system_state_->metrics_lib()->SendToUMA(metric,
552 percent_overhead,
553 0, // min: 0% overhead
554 1000, // max: 1000% overhead
555 kNumDefaultUmaBuckets);
556 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700557}
558
David Zeuthencc6f9962013-04-18 11:57:24 -0700559void PayloadState::ReportUpdateUrlSwitchesMetric() {
560 string metric = "Installer.UpdateURLSwitches";
561 int value = static_cast<int>(url_switch_count_);
562
563 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
564 system_state_->metrics_lib()->SendToUMA(
565 metric,
566 value,
567 0, // min value
568 100, // max value
569 kNumDefaultUmaBuckets);
570}
571
Chris Sosabe45bef2013-04-09 18:25:12 -0700572void PayloadState::ReportRebootMetrics() {
573 // Report the number of num_reboots.
574 string metric = "Installer.UpdateNumReboots";
575 uint32_t num_reboots = GetNumReboots();
576 LOG(INFO) << "Uploading reboot count of " << num_reboots << " for metric "
577 << metric;
578 system_state_->metrics_lib()->SendToUMA(
579 metric,
580 static_cast<int>(num_reboots), // sample
581 0, // min = 0.
582 50, // max
583 25); // buckets
584 SetNumReboots(0);
585}
586
587void PayloadState::UpdateNumReboots() {
588 // We only update the reboot count when the system has been detected to have
589 // been rebooted.
590 if (!system_state_->system_rebooted()) {
591 return;
592 }
593
594 SetNumReboots(GetNumReboots() + 1);
595}
596
597void PayloadState::SetNumReboots(uint32_t num_reboots) {
598 CHECK(prefs_);
599 num_reboots_ = num_reboots;
600 prefs_->SetInt64(kPrefsNumReboots, num_reboots);
601 LOG(INFO) << "Number of Reboots during current update attempt = "
602 << num_reboots_;
603}
604
Jay Srinivasan08262882012-12-28 19:29:43 -0800605void PayloadState::ResetPersistedState() {
606 SetPayloadAttemptNumber(0);
Alex Deymo820cc702013-06-28 15:43:46 -0700607 SetFullPayloadAttemptNumber(0);
Jay Srinivasan08262882012-12-28 19:29:43 -0800608 SetUrlIndex(0);
609 SetUrlFailureCount(0);
David Zeuthencc6f9962013-04-18 11:57:24 -0700610 SetUrlSwitchCount(0);
Jay Srinivasan08262882012-12-28 19:29:43 -0800611 UpdateBackoffExpiryTime(); // This will reset the backoff expiry time.
David Zeuthenf413fe52013-04-22 14:04:39 -0700612 SetUpdateTimestampStart(system_state_->clock()->GetWallclockTime());
David Zeuthen9a017f22013-04-11 16:10:26 -0700613 SetUpdateTimestampEnd(Time()); // Set to null time
614 SetUpdateDurationUptime(TimeDelta::FromSeconds(0));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700615 ResetDownloadSourcesOnNewUpdate();
Chris Sosaaa18e162013-06-20 13:20:30 -0700616 ResetRollbackVersion();
David Zeuthendcba8092013-08-06 12:16:35 -0700617 SetP2PNumAttempts(0);
618 SetP2PFirstAttemptTimestamp(Time()); // Set to null time
Chris Sosaaa18e162013-06-20 13:20:30 -0700619}
620
621void PayloadState::ResetRollbackVersion() {
622 CHECK(powerwash_safe_prefs_);
623 rollback_version_ = "";
624 powerwash_safe_prefs_->Delete(kPrefsRollbackVersion);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700625}
626
627void PayloadState::ResetDownloadSourcesOnNewUpdate() {
628 for (int i = 0; i < kNumDownloadSources; i++) {
629 DownloadSource source = static_cast<DownloadSource>(i);
630 SetCurrentBytesDownloaded(source, 0, true);
631 // Note: Not resetting the TotalBytesDownloaded as we want that metric
632 // to count the bytes downloaded across various update attempts until
633 // we have successfully applied the update.
634 }
635}
636
Chris Sosab3dcdb32013-09-04 15:22:12 -0700637int64_t PayloadState::GetPersistedValue(const string& key) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700638 CHECK(prefs_);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700639 if (!prefs_->Exists(key))
Jay Srinivasan19409b72013-04-12 19:23:36 -0700640 return 0;
641
642 int64_t stored_value;
Chris Sosab3dcdb32013-09-04 15:22:12 -0700643 if (!prefs_->GetInt64(key, &stored_value))
Jay Srinivasan19409b72013-04-12 19:23:36 -0700644 return 0;
645
646 if (stored_value < 0) {
647 LOG(ERROR) << key << ": Invalid value (" << stored_value
648 << ") in persisted state. Defaulting to 0";
649 return 0;
650 }
651
652 return stored_value;
Jay Srinivasan08262882012-12-28 19:29:43 -0800653}
654
655string PayloadState::CalculateResponseSignature() {
656 string response_sign = StringPrintf("NumURLs = %d\n",
Jay Srinivasan53173b92013-05-17 17:13:01 -0700657 candidate_urls_.size());
Jay Srinivasan08262882012-12-28 19:29:43 -0800658
Jay Srinivasan53173b92013-05-17 17:13:01 -0700659 for (size_t i = 0; i < candidate_urls_.size(); i++)
660 response_sign += StringPrintf("Candidate Url%d = %s\n",
661 i, candidate_urls_[i].c_str());
Jay Srinivasan08262882012-12-28 19:29:43 -0800662
663 response_sign += StringPrintf("Payload Size = %llu\n"
664 "Payload Sha256 Hash = %s\n"
665 "Metadata Size = %llu\n"
666 "Metadata Signature = %s\n"
667 "Is Delta Payload = %d\n"
668 "Max Failure Count Per Url = %d\n"
669 "Disable Payload Backoff = %d\n",
670 response_.size,
671 response_.hash.c_str(),
672 response_.metadata_size,
673 response_.metadata_signature.c_str(),
674 response_.is_delta_payload,
675 response_.max_failure_count_per_url,
676 response_.disable_payload_backoff);
677 return response_sign;
678}
679
680void PayloadState::LoadResponseSignature() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800681 CHECK(prefs_);
682 string stored_value;
Jay Srinivasan08262882012-12-28 19:29:43 -0800683 if (prefs_->Exists(kPrefsCurrentResponseSignature) &&
684 prefs_->GetString(kPrefsCurrentResponseSignature, &stored_value)) {
685 SetResponseSignature(stored_value);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800686 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800687}
688
Jay Srinivasan19409b72013-04-12 19:23:36 -0700689void PayloadState::SetResponseSignature(const string& response_signature) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800690 CHECK(prefs_);
691 response_signature_ = response_signature;
692 LOG(INFO) << "Current Response Signature = \n" << response_signature_;
693 prefs_->SetString(kPrefsCurrentResponseSignature, response_signature_);
694}
695
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800696void PayloadState::LoadPayloadAttemptNumber() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700697 SetPayloadAttemptNumber(GetPersistedValue(kPrefsPayloadAttemptNumber));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800698}
699
Alex Deymo820cc702013-06-28 15:43:46 -0700700void PayloadState::LoadFullPayloadAttemptNumber() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700701 SetFullPayloadAttemptNumber(GetPersistedValue(
702 kPrefsFullPayloadAttemptNumber));
Alex Deymo820cc702013-06-28 15:43:46 -0700703}
704
705void PayloadState::SetPayloadAttemptNumber(int payload_attempt_number) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800706 CHECK(prefs_);
707 payload_attempt_number_ = payload_attempt_number;
708 LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number_;
709 prefs_->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number_);
710}
711
Alex Deymo820cc702013-06-28 15:43:46 -0700712void PayloadState::SetFullPayloadAttemptNumber(
713 int full_payload_attempt_number) {
714 CHECK(prefs_);
715 full_payload_attempt_number_ = full_payload_attempt_number;
716 LOG(INFO) << "Full Payload Attempt Number = " << full_payload_attempt_number_;
717 prefs_->SetInt64(kPrefsFullPayloadAttemptNumber,
718 full_payload_attempt_number_);
719}
720
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800721void PayloadState::LoadUrlIndex() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700722 SetUrlIndex(GetPersistedValue(kPrefsCurrentUrlIndex));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800723}
724
725void PayloadState::SetUrlIndex(uint32_t url_index) {
726 CHECK(prefs_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800727 url_index_ = url_index;
728 LOG(INFO) << "Current URL Index = " << url_index_;
729 prefs_->SetInt64(kPrefsCurrentUrlIndex, url_index_);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700730
731 // Also update the download source, which is purely dependent on the
732 // current URL index alone.
733 UpdateCurrentDownloadSource();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800734}
735
David Zeuthencc6f9962013-04-18 11:57:24 -0700736void PayloadState::LoadUrlSwitchCount() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700737 SetUrlSwitchCount(GetPersistedValue(kPrefsUrlSwitchCount));
David Zeuthencc6f9962013-04-18 11:57:24 -0700738}
739
740void PayloadState::SetUrlSwitchCount(uint32_t url_switch_count) {
741 CHECK(prefs_);
742 url_switch_count_ = url_switch_count;
743 LOG(INFO) << "URL Switch Count = " << url_switch_count_;
744 prefs_->SetInt64(kPrefsUrlSwitchCount, url_switch_count_);
745}
746
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800747void PayloadState::LoadUrlFailureCount() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700748 SetUrlFailureCount(GetPersistedValue(kPrefsCurrentUrlFailureCount));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800749}
750
751void PayloadState::SetUrlFailureCount(uint32_t url_failure_count) {
752 CHECK(prefs_);
753 url_failure_count_ = url_failure_count;
754 LOG(INFO) << "Current URL (Url" << GetUrlIndex()
755 << ")'s Failure Count = " << url_failure_count_;
756 prefs_->SetInt64(kPrefsCurrentUrlFailureCount, url_failure_count_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800757}
758
Jay Srinivasan08262882012-12-28 19:29:43 -0800759void PayloadState::LoadBackoffExpiryTime() {
760 CHECK(prefs_);
761 int64_t stored_value;
762 if (!prefs_->Exists(kPrefsBackoffExpiryTime))
763 return;
764
765 if (!prefs_->GetInt64(kPrefsBackoffExpiryTime, &stored_value))
766 return;
767
768 Time stored_time = Time::FromInternalValue(stored_value);
769 if (stored_time > Time::Now() + TimeDelta::FromDays(kMaxBackoffDays)) {
770 LOG(ERROR) << "Invalid backoff expiry time ("
771 << utils::ToString(stored_time)
772 << ") in persisted state. Resetting.";
773 stored_time = Time();
774 }
775 SetBackoffExpiryTime(stored_time);
776}
777
778void PayloadState::SetBackoffExpiryTime(const Time& new_time) {
779 CHECK(prefs_);
780 backoff_expiry_time_ = new_time;
781 LOG(INFO) << "Backoff Expiry Time = "
782 << utils::ToString(backoff_expiry_time_);
783 prefs_->SetInt64(kPrefsBackoffExpiryTime,
784 backoff_expiry_time_.ToInternalValue());
785}
786
David Zeuthen9a017f22013-04-11 16:10:26 -0700787TimeDelta PayloadState::GetUpdateDuration() {
David Zeuthenf413fe52013-04-22 14:04:39 -0700788 Time end_time = update_timestamp_end_.is_null()
789 ? system_state_->clock()->GetWallclockTime() :
790 update_timestamp_end_;
David Zeuthen9a017f22013-04-11 16:10:26 -0700791 return end_time - update_timestamp_start_;
792}
793
794void PayloadState::LoadUpdateTimestampStart() {
795 int64_t stored_value;
796 Time stored_time;
797
798 CHECK(prefs_);
799
David Zeuthenf413fe52013-04-22 14:04:39 -0700800 Time now = system_state_->clock()->GetWallclockTime();
David Zeuthen9a017f22013-04-11 16:10:26 -0700801
802 if (!prefs_->Exists(kPrefsUpdateTimestampStart)) {
803 // The preference missing is not unexpected - in that case, just
804 // use the current time as start time
805 stored_time = now;
806 } else if (!prefs_->GetInt64(kPrefsUpdateTimestampStart, &stored_value)) {
807 LOG(ERROR) << "Invalid UpdateTimestampStart value. Resetting.";
808 stored_time = now;
809 } else {
810 stored_time = Time::FromInternalValue(stored_value);
811 }
812
813 // Sanity check: If the time read from disk is in the future
814 // (modulo some slack to account for possible NTP drift
815 // adjustments), something is fishy and we should report and
816 // reset.
817 TimeDelta duration_according_to_stored_time = now - stored_time;
818 if (duration_according_to_stored_time < -kDurationSlack) {
819 LOG(ERROR) << "The UpdateTimestampStart value ("
820 << utils::ToString(stored_time)
821 << ") in persisted state is "
David Zeuthen674c3182013-04-18 14:05:20 -0700822 << utils::FormatTimeDelta(duration_according_to_stored_time)
823 << " in the future. Resetting.";
David Zeuthen9a017f22013-04-11 16:10:26 -0700824 stored_time = now;
825 }
826
827 SetUpdateTimestampStart(stored_time);
828}
829
830void PayloadState::SetUpdateTimestampStart(const Time& value) {
831 CHECK(prefs_);
832 update_timestamp_start_ = value;
833 prefs_->SetInt64(kPrefsUpdateTimestampStart,
834 update_timestamp_start_.ToInternalValue());
835 LOG(INFO) << "Update Timestamp Start = "
836 << utils::ToString(update_timestamp_start_);
837}
838
839void PayloadState::SetUpdateTimestampEnd(const Time& value) {
840 update_timestamp_end_ = value;
841 LOG(INFO) << "Update Timestamp End = "
842 << utils::ToString(update_timestamp_end_);
843}
844
845TimeDelta PayloadState::GetUpdateDurationUptime() {
846 return update_duration_uptime_;
847}
848
849void PayloadState::LoadUpdateDurationUptime() {
850 int64_t stored_value;
851 TimeDelta stored_delta;
852
853 CHECK(prefs_);
854
855 if (!prefs_->Exists(kPrefsUpdateDurationUptime)) {
856 // The preference missing is not unexpected - in that case, just
857 // we'll use zero as the delta
858 } else if (!prefs_->GetInt64(kPrefsUpdateDurationUptime, &stored_value)) {
859 LOG(ERROR) << "Invalid UpdateDurationUptime value. Resetting.";
860 stored_delta = TimeDelta::FromSeconds(0);
861 } else {
862 stored_delta = TimeDelta::FromInternalValue(stored_value);
863 }
864
865 // Sanity-check: Uptime can never be greater than the wall-clock
866 // difference (modulo some slack). If it is, report and reset
867 // to the wall-clock difference.
868 TimeDelta diff = GetUpdateDuration() - stored_delta;
869 if (diff < -kDurationSlack) {
870 LOG(ERROR) << "The UpdateDurationUptime value ("
David Zeuthen674c3182013-04-18 14:05:20 -0700871 << utils::FormatTimeDelta(stored_delta)
David Zeuthen9a017f22013-04-11 16:10:26 -0700872 << ") in persisted state is "
David Zeuthen674c3182013-04-18 14:05:20 -0700873 << utils::FormatTimeDelta(diff)
874 << " larger than the wall-clock delta. Resetting.";
David Zeuthen9a017f22013-04-11 16:10:26 -0700875 stored_delta = update_duration_current_;
876 }
877
878 SetUpdateDurationUptime(stored_delta);
879}
880
Chris Sosabe45bef2013-04-09 18:25:12 -0700881void PayloadState::LoadNumReboots() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700882 SetNumReboots(GetPersistedValue(kPrefsNumReboots));
Chris Sosaaa18e162013-06-20 13:20:30 -0700883}
884
885void PayloadState::LoadRollbackVersion() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700886 CHECK(powerwash_safe_prefs_);
887 string rollback_version;
888 if (powerwash_safe_prefs_->GetString(kPrefsRollbackVersion,
889 &rollback_version)) {
890 SetRollbackVersion(rollback_version);
891 }
Chris Sosaaa18e162013-06-20 13:20:30 -0700892}
893
894void PayloadState::SetRollbackVersion(const string& rollback_version) {
895 CHECK(powerwash_safe_prefs_);
896 LOG(INFO) << "Blacklisting version "<< rollback_version;
897 rollback_version_ = rollback_version;
898 powerwash_safe_prefs_->SetString(kPrefsRollbackVersion, rollback_version);
Chris Sosabe45bef2013-04-09 18:25:12 -0700899}
900
David Zeuthen9a017f22013-04-11 16:10:26 -0700901void PayloadState::SetUpdateDurationUptimeExtended(const TimeDelta& value,
902 const Time& timestamp,
903 bool use_logging) {
904 CHECK(prefs_);
905 update_duration_uptime_ = value;
906 update_duration_uptime_timestamp_ = timestamp;
907 prefs_->SetInt64(kPrefsUpdateDurationUptime,
908 update_duration_uptime_.ToInternalValue());
909 if (use_logging) {
910 LOG(INFO) << "Update Duration Uptime = "
David Zeuthen674c3182013-04-18 14:05:20 -0700911 << utils::FormatTimeDelta(update_duration_uptime_);
David Zeuthen9a017f22013-04-11 16:10:26 -0700912 }
913}
914
915void PayloadState::SetUpdateDurationUptime(const TimeDelta& value) {
David Zeuthenf413fe52013-04-22 14:04:39 -0700916 Time now = system_state_->clock()->GetMonotonicTime();
917 SetUpdateDurationUptimeExtended(value, now, true);
David Zeuthen9a017f22013-04-11 16:10:26 -0700918}
919
920void PayloadState::CalculateUpdateDurationUptime() {
David Zeuthenf413fe52013-04-22 14:04:39 -0700921 Time now = system_state_->clock()->GetMonotonicTime();
David Zeuthen9a017f22013-04-11 16:10:26 -0700922 TimeDelta uptime_since_last_update = now - update_duration_uptime_timestamp_;
923 TimeDelta new_uptime = update_duration_uptime_ + uptime_since_last_update;
924 // We're frequently called so avoid logging this write
925 SetUpdateDurationUptimeExtended(new_uptime, now, false);
926}
927
David Zeuthen674c3182013-04-18 14:05:20 -0700928void PayloadState::ReportDurationMetrics() {
929 TimeDelta duration = GetUpdateDuration();
930 TimeDelta duration_uptime = GetUpdateDurationUptime();
931 string metric;
932
933 metric = "Installer.UpdateDurationMinutes";
934 system_state_->metrics_lib()->SendToUMA(
935 metric,
936 static_cast<int>(duration.InMinutes()),
937 1, // min: 1 minute
938 365*24*60, // max: 1 year (approx)
939 kNumDefaultUmaBuckets);
940 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration)
941 << " for metric " << metric;
942
943 metric = "Installer.UpdateDurationUptimeMinutes";
944 system_state_->metrics_lib()->SendToUMA(
945 metric,
946 static_cast<int>(duration_uptime.InMinutes()),
947 1, // min: 1 minute
948 30*24*60, // max: 1 month (approx)
949 kNumDefaultUmaBuckets);
950 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration_uptime)
951 << " for metric " << metric;
952
953 prefs_->Delete(kPrefsUpdateTimestampStart);
954 prefs_->Delete(kPrefsUpdateDurationUptime);
955}
956
Alex Deymo1c656c42013-06-28 11:02:14 -0700957void PayloadState::ReportPayloadTypeMetric() {
958 string metric;
959 PayloadType uma_payload_type;
960 OmahaRequestParams* params = system_state_->request_params();
961
962 if (response_.is_delta_payload) {
963 uma_payload_type = kPayloadTypeDelta;
964 } else if (params->delta_okay()) {
965 uma_payload_type = kPayloadTypeFull;
966 } else { // Full payload, delta was not allowed by request.
967 uma_payload_type = kPayloadTypeForcedFull;
968 }
969
970 metric = "Installer.PayloadFormat";
971 system_state_->metrics_lib()->SendEnumToUMA(
972 metric,
973 uma_payload_type,
974 kNumPayloadTypes);
975 LOG(INFO) << "Uploading " << utils::ToString(uma_payload_type)
976 << " for metric " << metric;
977}
978
Alex Deymo820cc702013-06-28 15:43:46 -0700979void PayloadState::ReportAttemptsCountMetrics() {
980 string metric;
981 int total_attempts = GetPayloadAttemptNumber();
982
983 metric = "Installer.AttemptsCount.Total";
984 system_state_->metrics_lib()->SendToUMA(
985 metric,
986 total_attempts,
987 1, // min
988 50, // max
989 kNumDefaultUmaBuckets);
990 LOG(INFO) << "Uploading " << total_attempts
991 << " for metric " << metric;
992}
993
Jay Srinivasan19409b72013-04-12 19:23:36 -0700994string PayloadState::GetPrefsKey(const string& prefix, DownloadSource source) {
995 return prefix + "-from-" + utils::ToString(source);
996}
997
998void PayloadState::LoadCurrentBytesDownloaded(DownloadSource source) {
999 string key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source);
Chris Sosab3dcdb32013-09-04 15:22:12 -07001000 SetCurrentBytesDownloaded(source, GetPersistedValue(key), true);
Jay Srinivasan19409b72013-04-12 19:23:36 -07001001}
1002
1003void PayloadState::SetCurrentBytesDownloaded(
1004 DownloadSource source,
1005 uint64_t current_bytes_downloaded,
1006 bool log) {
1007 CHECK(prefs_);
1008
1009 if (source >= kNumDownloadSources)
1010 return;
1011
1012 // Update the in-memory value.
1013 current_bytes_downloaded_[source] = current_bytes_downloaded;
1014
1015 string prefs_key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source);
1016 prefs_->SetInt64(prefs_key, current_bytes_downloaded);
1017 LOG_IF(INFO, log) << "Current bytes downloaded for "
1018 << utils::ToString(source) << " = "
1019 << GetCurrentBytesDownloaded(source);
1020}
1021
1022void PayloadState::LoadTotalBytesDownloaded(DownloadSource source) {
1023 string key = GetPrefsKey(kPrefsTotalBytesDownloaded, source);
Chris Sosab3dcdb32013-09-04 15:22:12 -07001024 SetTotalBytesDownloaded(source, GetPersistedValue(key), true);
Jay Srinivasan19409b72013-04-12 19:23:36 -07001025}
1026
1027void PayloadState::SetTotalBytesDownloaded(
1028 DownloadSource source,
1029 uint64_t total_bytes_downloaded,
1030 bool log) {
1031 CHECK(prefs_);
1032
1033 if (source >= kNumDownloadSources)
1034 return;
1035
1036 // Update the in-memory value.
1037 total_bytes_downloaded_[source] = total_bytes_downloaded;
1038
1039 // Persist.
1040 string prefs_key = GetPrefsKey(kPrefsTotalBytesDownloaded, source);
1041 prefs_->SetInt64(prefs_key, total_bytes_downloaded);
1042 LOG_IF(INFO, log) << "Total bytes downloaded for "
1043 << utils::ToString(source) << " = "
1044 << GetTotalBytesDownloaded(source);
1045}
1046
David Zeuthena573d6f2013-06-14 16:13:36 -07001047void PayloadState::LoadNumResponsesSeen() {
Chris Sosab3dcdb32013-09-04 15:22:12 -07001048 SetNumResponsesSeen(GetPersistedValue(kPrefsNumResponsesSeen));
David Zeuthena573d6f2013-06-14 16:13:36 -07001049}
1050
1051void PayloadState::SetNumResponsesSeen(int num_responses_seen) {
1052 CHECK(prefs_);
1053 num_responses_seen_ = num_responses_seen;
1054 LOG(INFO) << "Num Responses Seen = " << num_responses_seen_;
1055 prefs_->SetInt64(kPrefsNumResponsesSeen, num_responses_seen_);
1056}
1057
1058void PayloadState::ReportUpdatesAbandonedCountMetric() {
1059 string metric = "Installer.UpdatesAbandonedCount";
1060 int value = num_responses_seen_ - 1;
1061
1062 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
1063 system_state_->metrics_lib()->SendToUMA(
1064 metric,
1065 value,
1066 0, // min value
1067 100, // max value
1068 kNumDefaultUmaBuckets);
1069}
1070
Alex Deymob33b0f02013-08-08 21:10:02 -07001071void PayloadState::ReportUpdatesAbandonedEventCountMetric() {
1072 string metric = "Installer.UpdatesAbandonedEventCount";
1073 int value = num_responses_seen_ - 1;
1074
1075 // Do not send an "abandoned" event when 0 payloads were abandoned since the
1076 // last successful update.
1077 if (value == 0)
1078 return;
1079
1080 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
1081 system_state_->metrics_lib()->SendToUMA(
1082 metric,
1083 value,
1084 0, // min value
1085 100, // max value
1086 kNumDefaultUmaBuckets);
1087}
1088
Jay Srinivasan53173b92013-05-17 17:13:01 -07001089void PayloadState::ComputeCandidateUrls() {
Chris Sosaf7d80042013-08-22 16:45:17 -07001090 bool http_url_ok = true;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001091
1092 if (system_state_->IsOfficialBuild()) {
1093 const policy::DevicePolicy* policy = system_state_->device_policy();
Chris Sosaf7d80042013-08-22 16:45:17 -07001094 if (policy && policy->GetHttpDownloadsEnabled(&http_url_ok) && !http_url_ok)
Jay Srinivasan53173b92013-05-17 17:13:01 -07001095 LOG(INFO) << "Downloads via HTTP Url are not enabled by device policy";
1096 } else {
1097 LOG(INFO) << "Allowing HTTP downloads for unofficial builds";
1098 http_url_ok = true;
1099 }
1100
1101 candidate_urls_.clear();
1102 for (size_t i = 0; i < response_.payload_urls.size(); i++) {
1103 string candidate_url = response_.payload_urls[i];
1104 if (StartsWithASCII(candidate_url, "http://", false) && !http_url_ok)
1105 continue;
1106 candidate_urls_.push_back(candidate_url);
1107 LOG(INFO) << "Candidate Url" << (candidate_urls_.size() - 1)
1108 << ": " << candidate_url;
1109 }
1110
1111 LOG(INFO) << "Found " << candidate_urls_.size() << " candidate URLs "
1112 << "out of " << response_.payload_urls.size() << " URLs supplied";
1113}
1114
David Zeuthene4c58bf2013-06-18 17:26:50 -07001115void PayloadState::CreateSystemUpdatedMarkerFile() {
1116 CHECK(prefs_);
1117 int64_t value = system_state_->clock()->GetWallclockTime().ToInternalValue();
1118 prefs_->SetInt64(kPrefsSystemUpdatedMarker, value);
1119}
1120
1121void PayloadState::BootedIntoUpdate(TimeDelta time_to_reboot) {
1122 // Send |time_to_reboot| as a UMA stat.
1123 string metric = "Installer.TimeToRebootMinutes";
1124 system_state_->metrics_lib()->SendToUMA(metric,
1125 time_to_reboot.InMinutes(),
1126 0, // min: 0 minute
1127 30*24*60, // max: 1 month (approx)
1128 kNumDefaultUmaBuckets);
1129 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(time_to_reboot)
1130 << " for metric " << metric;
1131}
1132
1133void PayloadState::UpdateEngineStarted() {
Alex Deymo569c4242013-07-24 12:01:01 -07001134 // Avoid the UpdateEngineStarted actions if this is not the first time we
1135 // run the update engine since reboot.
1136 if (!system_state_->system_rebooted())
1137 return;
1138
David Zeuthene4c58bf2013-06-18 17:26:50 -07001139 // Figure out if we just booted into a new update
1140 if (prefs_->Exists(kPrefsSystemUpdatedMarker)) {
1141 int64_t stored_value;
1142 if (prefs_->GetInt64(kPrefsSystemUpdatedMarker, &stored_value)) {
1143 Time system_updated_at = Time::FromInternalValue(stored_value);
1144 if (!system_updated_at.is_null()) {
1145 TimeDelta time_to_reboot =
1146 system_state_->clock()->GetWallclockTime() - system_updated_at;
1147 if (time_to_reboot.ToInternalValue() < 0) {
1148 LOG(ERROR) << "time_to_reboot is negative - system_updated_at: "
1149 << utils::ToString(system_updated_at);
1150 } else {
1151 BootedIntoUpdate(time_to_reboot);
1152 }
1153 }
1154 }
1155 prefs_->Delete(kPrefsSystemUpdatedMarker);
1156 }
Alex Deymo42432912013-07-12 20:21:15 -07001157 // Check if it is needed to send metrics about a failed reboot into a new
1158 // version.
1159 ReportFailedBootIfNeeded();
1160}
1161
1162void PayloadState::ReportFailedBootIfNeeded() {
1163 // If the kPrefsTargetVersionInstalledFrom is present, a successfully applied
1164 // payload was marked as ready immediately before the last reboot, and we
1165 // need to check if such payload successfully rebooted or not.
1166 if (prefs_->Exists(kPrefsTargetVersionInstalledFrom)) {
1167 string installed_from;
1168 if (!prefs_->GetString(kPrefsTargetVersionInstalledFrom, &installed_from)) {
1169 LOG(ERROR) << "Error reading TargetVersionInstalledFrom on reboot.";
1170 return;
1171 }
1172 if (installed_from ==
1173 utils::PartitionNumber(system_state_->hardware()->BootDevice())) {
1174 // A reboot was pending, but the chromebook is again in the same
1175 // BootDevice where the update was installed from.
1176 int64_t target_attempt;
1177 if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt)) {
1178 LOG(ERROR) << "Error reading TargetVersionAttempt when "
1179 "TargetVersionInstalledFrom was present.";
1180 target_attempt = 1;
1181 }
1182
1183 // Report the UMA metric of the current boot failure.
1184 string metric = "Installer.RebootToNewPartitionAttempt";
1185
1186 LOG(INFO) << "Uploading " << target_attempt
1187 << " (count) for metric " << metric;
1188 system_state_->metrics_lib()->SendToUMA(
1189 metric,
1190 target_attempt,
1191 1, // min value
1192 50, // max value
1193 kNumDefaultUmaBuckets);
1194 } else {
1195 prefs_->Delete(kPrefsTargetVersionAttempt);
1196 prefs_->Delete(kPrefsTargetVersionUniqueId);
1197 }
1198 prefs_->Delete(kPrefsTargetVersionInstalledFrom);
1199 }
1200}
1201
1202void PayloadState::ExpectRebootInNewVersion(const string& target_version_uid) {
1203 // Expect to boot into the new partition in the next reboot setting the
1204 // TargetVersion* flags in the Prefs.
1205 string stored_target_version_uid;
1206 string target_version_id;
1207 string target_partition;
1208 int64_t target_attempt;
1209
1210 if (prefs_->Exists(kPrefsTargetVersionUniqueId) &&
1211 prefs_->GetString(kPrefsTargetVersionUniqueId,
1212 &stored_target_version_uid) &&
1213 stored_target_version_uid == target_version_uid) {
1214 if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt))
1215 target_attempt = 0;
1216 } else {
1217 prefs_->SetString(kPrefsTargetVersionUniqueId, target_version_uid);
1218 target_attempt = 0;
1219 }
1220 prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt + 1);
1221
1222 prefs_->SetString(kPrefsTargetVersionInstalledFrom,
1223 utils::PartitionNumber(
1224 system_state_->hardware()->BootDevice()));
1225}
1226
1227void PayloadState::ResetUpdateStatus() {
1228 // Remove the TargetVersionInstalledFrom pref so that if the machine is
1229 // rebooted the next boot is not flagged as failed to rebooted into the
1230 // new applied payload.
1231 prefs_->Delete(kPrefsTargetVersionInstalledFrom);
1232
1233 // Also decrement the attempt number if it exists.
1234 int64_t target_attempt;
1235 if (prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt))
1236 prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt-1);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001237}
1238
David Zeuthendcba8092013-08-06 12:16:35 -07001239int PayloadState::GetP2PNumAttempts() {
1240 return p2p_num_attempts_;
1241}
1242
1243void PayloadState::SetP2PNumAttempts(int value) {
1244 p2p_num_attempts_ = value;
1245 LOG(INFO) << "p2p Num Attempts = " << p2p_num_attempts_;
1246 CHECK(prefs_);
1247 prefs_->SetInt64(kPrefsP2PNumAttempts, value);
1248}
1249
1250void PayloadState::LoadP2PNumAttempts() {
Chris Sosab3dcdb32013-09-04 15:22:12 -07001251 SetP2PNumAttempts(GetPersistedValue(kPrefsP2PNumAttempts));
David Zeuthendcba8092013-08-06 12:16:35 -07001252}
1253
1254Time PayloadState::GetP2PFirstAttemptTimestamp() {
1255 return p2p_first_attempt_timestamp_;
1256}
1257
1258void PayloadState::SetP2PFirstAttemptTimestamp(const Time& time) {
1259 p2p_first_attempt_timestamp_ = time;
1260 LOG(INFO) << "p2p First Attempt Timestamp = "
1261 << utils::ToString(p2p_first_attempt_timestamp_);
1262 CHECK(prefs_);
1263 int64_t stored_value = time.ToInternalValue();
1264 prefs_->SetInt64(kPrefsP2PFirstAttemptTimestamp, stored_value);
1265}
1266
1267void PayloadState::LoadP2PFirstAttemptTimestamp() {
Chris Sosab3dcdb32013-09-04 15:22:12 -07001268 int64_t stored_value = GetPersistedValue(kPrefsP2PFirstAttemptTimestamp);
David Zeuthendcba8092013-08-06 12:16:35 -07001269 Time stored_time = Time::FromInternalValue(stored_value);
1270 SetP2PFirstAttemptTimestamp(stored_time);
1271}
1272
1273void PayloadState::P2PNewAttempt() {
1274 CHECK(prefs_);
1275 // Set timestamp, if it hasn't been set already
1276 if (p2p_first_attempt_timestamp_.is_null()) {
1277 SetP2PFirstAttemptTimestamp(system_state_->clock()->GetWallclockTime());
1278 }
1279 // Increase number of attempts
1280 SetP2PNumAttempts(GetP2PNumAttempts() + 1);
1281}
1282
1283bool PayloadState::P2PAttemptAllowed() {
1284 if (p2p_num_attempts_ > kMaxP2PAttempts) {
1285 LOG(INFO) << "Number of p2p attempts is " << p2p_num_attempts_
1286 << " which is greater than "
1287 << kMaxP2PAttempts
1288 << " - disallowing p2p.";
1289 return false;
1290 }
1291
1292 if (!p2p_first_attempt_timestamp_.is_null()) {
1293 Time now = system_state_->clock()->GetWallclockTime();
1294 TimeDelta time_spent_attempting_p2p = now - p2p_first_attempt_timestamp_;
1295 if (time_spent_attempting_p2p.InSeconds() < 0) {
1296 LOG(ERROR) << "Time spent attempting p2p is negative"
1297 << " - disallowing p2p.";
1298 return false;
1299 }
1300 if (time_spent_attempting_p2p.InSeconds() > kMaxP2PAttemptTimeSeconds) {
1301 LOG(INFO) << "Time spent attempting p2p is "
1302 << utils::FormatTimeDelta(time_spent_attempting_p2p)
1303 << " which is greater than "
1304 << utils::FormatTimeDelta(TimeDelta::FromSeconds(
1305 kMaxP2PAttemptTimeSeconds))
1306 << " - disallowing p2p.";
1307 return false;
1308 }
1309 }
1310
1311 return true;
1312}
1313
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001314} // namespace chromeos_update_engine