blob: 7505b96fde45b5d141ac4a7bc673644cdef601f1 [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:
Don Garrett4d039442013-10-28 18:40:06 -0700223 case kErrorCodeUnsupportedMajorPayloadVersion:
224 case kErrorCodeUnsupportedMinorPayloadVersion:
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800225 IncrementUrlIndex();
226 break;
227
228 // Errors which seem to be just transient network/communication related
229 // failures and do not indicate any inherent problem with the URL itself.
230 // So, we should keep the current URL but just increment the
231 // failure count to give it more chances. This way, while we maximize our
232 // chances of downloading from the URLs that appear earlier in the response
233 // (because download from a local server URL that appears earlier in a
234 // response is preferable than downloading from the next URL which could be
235 // a internet URL and thus could be more expensive).
David Zeuthena99981f2013-04-29 13:42:47 -0700236 case kErrorCodeError:
237 case kErrorCodeDownloadTransferError:
238 case kErrorCodeDownloadWriteError:
239 case kErrorCodeDownloadStateInitializationError:
240 case kErrorCodeOmahaErrorInHTTPResponse: // Aggregate code for HTTP errors.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800241 IncrementFailureCount();
242 break;
243
244 // Errors which are not specific to a URL and hence shouldn't result in
245 // the URL being penalized. This can happen in two cases:
246 // 1. We haven't started downloading anything: These errors don't cost us
247 // anything in terms of actual payload bytes, so we should just do the
248 // regular retries at the next update check.
249 // 2. We have successfully downloaded the payload: In this case, the
250 // payload attempt number would have been incremented and would take care
Jay Srinivasan08262882012-12-28 19:29:43 -0800251 // of the backoff at the next update check.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800252 // In either case, there's no need to update URL index or failure count.
David Zeuthena99981f2013-04-29 13:42:47 -0700253 case kErrorCodeOmahaRequestError:
254 case kErrorCodeOmahaResponseHandlerError:
255 case kErrorCodePostinstallRunnerError:
256 case kErrorCodeFilesystemCopierError:
257 case kErrorCodeInstallDeviceOpenError:
258 case kErrorCodeKernelDeviceOpenError:
259 case kErrorCodeDownloadNewPartitionInfoError:
260 case kErrorCodeNewRootfsVerificationError:
261 case kErrorCodeNewKernelVerificationError:
262 case kErrorCodePostinstallBootedFromFirmwareB:
Don Garrett81018e02013-07-30 18:46:31 -0700263 case kErrorCodePostinstallFirmwareRONotUpdatable:
David Zeuthena99981f2013-04-29 13:42:47 -0700264 case kErrorCodeOmahaRequestEmptyResponseError:
265 case kErrorCodeOmahaRequestXMLParseError:
266 case kErrorCodeOmahaResponseInvalid:
267 case kErrorCodeOmahaUpdateIgnoredPerPolicy:
268 case kErrorCodeOmahaUpdateDeferredPerPolicy:
269 case kErrorCodeOmahaUpdateDeferredForBackoff:
270 case kErrorCodePostinstallPowerwashError:
271 case kErrorCodeUpdateCanceledByChannelChange:
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800272 LOG(INFO) << "Not incrementing URL index or failure count for this error";
273 break;
274
David Zeuthena99981f2013-04-29 13:42:47 -0700275 case kErrorCodeSuccess: // success code
David Zeuthena99981f2013-04-29 13:42:47 -0700276 case kErrorCodeUmaReportedMax: // not an error code
277 case kErrorCodeOmahaRequestHTTPResponseBase: // aggregated already
278 case kErrorCodeDevModeFlag: // not an error code
279 case kErrorCodeResumedFlag: // not an error code
280 case kErrorCodeTestImageFlag: // not an error code
281 case kErrorCodeTestOmahaUrlFlag: // not an error code
282 case kErrorCodeSpecialFlags: // not an error code
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800283 // These shouldn't happen. Enumerating these explicitly here so that we
284 // can let the compiler warn about new error codes that are added to
285 // action_processor.h but not added here.
286 LOG(WARNING) << "Unexpected error code for UpdateFailed";
287 break;
288
289 // Note: Not adding a default here so as to let the compiler warn us of
290 // any new enums that were added in the .h but not listed in this switch.
291 }
292}
293
Jay Srinivasan08262882012-12-28 19:29:43 -0800294bool PayloadState::ShouldBackoffDownload() {
295 if (response_.disable_payload_backoff) {
296 LOG(INFO) << "Payload backoff logic is disabled. "
297 "Can proceed with the download";
298 return false;
299 }
Chris Sosa20f005c2013-09-05 13:53:08 -0700300 if (system_state_->request_params()->use_p2p_for_downloading() &&
301 !system_state_->request_params()->p2p_url().empty()) {
302 LOG(INFO) << "Payload backoff logic is disabled because download "
303 << "will happen from local peer (via p2p).";
304 return false;
305 }
306 if (system_state_->request_params()->interactive()) {
307 LOG(INFO) << "Payload backoff disabled for interactive update checks.";
308 return false;
309 }
Jay Srinivasan08262882012-12-28 19:29:43 -0800310 if (response_.is_delta_payload) {
311 // If delta payloads fail, we want to fallback quickly to full payloads as
312 // they are more likely to succeed. Exponential backoffs would greatly
313 // slow down the fallback to full payloads. So we don't backoff for delta
314 // payloads.
315 LOG(INFO) << "No backoffs for delta payloads. "
316 << "Can proceed with the download";
317 return false;
318 }
319
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700320 if (!system_state_->hardware()->IsOfficialBuild()) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800321 // Backoffs are needed only for official builds. We do not want any delays
322 // or update failures due to backoffs during testing or development.
323 LOG(INFO) << "No backoffs for test/dev images. "
324 << "Can proceed with the download";
325 return false;
326 }
327
328 if (backoff_expiry_time_.is_null()) {
329 LOG(INFO) << "No backoff expiry time has been set. "
330 << "Can proceed with the download";
331 return false;
332 }
333
334 if (backoff_expiry_time_ < Time::Now()) {
335 LOG(INFO) << "The backoff expiry time ("
336 << utils::ToString(backoff_expiry_time_)
337 << ") has elapsed. Can proceed with the download";
338 return false;
339 }
340
341 LOG(INFO) << "Cannot proceed with downloads as we need to backoff until "
342 << utils::ToString(backoff_expiry_time_);
343 return true;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800344}
345
Chris Sosaaa18e162013-06-20 13:20:30 -0700346void PayloadState::Rollback() {
347 SetRollbackVersion(system_state_->request_params()->app_version());
348}
349
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800350void PayloadState::IncrementPayloadAttemptNumber() {
Alex Deymo820cc702013-06-28 15:43:46 -0700351 // Update the payload attempt number for both payload types: full and delta.
352 SetPayloadAttemptNumber(GetPayloadAttemptNumber() + 1);
Alex Deymo29b51d92013-07-09 15:26:24 -0700353
354 // Report the metric every time the value is incremented.
355 string metric = "Installer.PayloadAttemptNumber";
356 int value = GetPayloadAttemptNumber();
357
358 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
359 system_state_->metrics_lib()->SendToUMA(
360 metric,
361 value,
362 1, // min value
363 50, // max value
364 kNumDefaultUmaBuckets);
Alex Deymo820cc702013-06-28 15:43:46 -0700365}
366
367void PayloadState::IncrementFullPayloadAttemptNumber() {
368 // Update the payload attempt number for full payloads and the backoff time.
Jay Srinivasan08262882012-12-28 19:29:43 -0800369 if (response_.is_delta_payload) {
370 LOG(INFO) << "Not incrementing payload attempt number for delta payloads";
371 return;
372 }
373
Alex Deymo29b51d92013-07-09 15:26:24 -0700374 LOG(INFO) << "Incrementing the full payload attempt number";
Alex Deymo820cc702013-06-28 15:43:46 -0700375 SetFullPayloadAttemptNumber(GetFullPayloadAttemptNumber() + 1);
Jay Srinivasan08262882012-12-28 19:29:43 -0800376 UpdateBackoffExpiryTime();
Alex Deymo29b51d92013-07-09 15:26:24 -0700377
378 // Report the metric every time the value is incremented.
379 string metric = "Installer.FullPayloadAttemptNumber";
380 int value = GetFullPayloadAttemptNumber();
381
382 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
383 system_state_->metrics_lib()->SendToUMA(
384 metric,
385 value,
386 1, // min value
387 50, // max value
388 kNumDefaultUmaBuckets);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800389}
390
391void PayloadState::IncrementUrlIndex() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800392 uint32_t next_url_index = GetUrlIndex() + 1;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700393 if (next_url_index < candidate_urls_.size()) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800394 LOG(INFO) << "Incrementing the URL index for next attempt";
395 SetUrlIndex(next_url_index);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800396 } else {
397 LOG(INFO) << "Resetting the current URL index (" << GetUrlIndex() << ") to "
Jay Srinivasan53173b92013-05-17 17:13:01 -0700398 << "0 as we only have " << candidate_urls_.size()
399 << " candidate URL(s)";
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800400 SetUrlIndex(0);
Alex Deymo29b51d92013-07-09 15:26:24 -0700401 IncrementPayloadAttemptNumber();
402 IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800403 }
Jay Srinivasan08262882012-12-28 19:29:43 -0800404
David Zeuthencc6f9962013-04-18 11:57:24 -0700405 // If we have multiple URLs, record that we just switched to another one
Jay Srinivasan53173b92013-05-17 17:13:01 -0700406 if (candidate_urls_.size() > 1)
David Zeuthencc6f9962013-04-18 11:57:24 -0700407 SetUrlSwitchCount(url_switch_count_ + 1);
408
Jay Srinivasan08262882012-12-28 19:29:43 -0800409 // Whenever we update the URL index, we should also clear the URL failure
410 // count so we can start over fresh for the new URL.
411 SetUrlFailureCount(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800412}
413
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800414void PayloadState::IncrementFailureCount() {
415 uint32_t next_url_failure_count = GetUrlFailureCount() + 1;
Jay Srinivasan08262882012-12-28 19:29:43 -0800416 if (next_url_failure_count < response_.max_failure_count_per_url) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800417 LOG(INFO) << "Incrementing the URL failure count";
418 SetUrlFailureCount(next_url_failure_count);
419 } else {
420 LOG(INFO) << "Reached max number of failures for Url" << GetUrlIndex()
421 << ". Trying next available URL";
422 IncrementUrlIndex();
423 }
424}
425
Jay Srinivasan08262882012-12-28 19:29:43 -0800426void PayloadState::UpdateBackoffExpiryTime() {
427 if (response_.disable_payload_backoff) {
428 LOG(INFO) << "Resetting backoff expiry time as payload backoff is disabled";
429 SetBackoffExpiryTime(Time());
430 return;
431 }
432
Alex Deymo820cc702013-06-28 15:43:46 -0700433 if (GetFullPayloadAttemptNumber() == 0) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800434 SetBackoffExpiryTime(Time());
435 return;
436 }
437
438 // Since we're doing left-shift below, make sure we don't shift more
Alex Deymo820cc702013-06-28 15:43:46 -0700439 // than this. E.g. if int is 4-bytes, don't left-shift more than 30 bits,
Jay Srinivasan08262882012-12-28 19:29:43 -0800440 // since we don't expect value of kMaxBackoffDays to be more than 100 anyway.
Alex Deymo820cc702013-06-28 15:43:46 -0700441 int num_days = 1; // the value to be shifted.
442 const int kMaxShifts = (sizeof(num_days) * 8) - 2;
Jay Srinivasan08262882012-12-28 19:29:43 -0800443
444 // Normal backoff days is 2 raised to (payload_attempt_number - 1).
445 // E.g. if payload_attempt_number is over 30, limit power to 30.
Alex Deymo820cc702013-06-28 15:43:46 -0700446 int power = min(GetFullPayloadAttemptNumber() - 1, kMaxShifts);
Jay Srinivasan08262882012-12-28 19:29:43 -0800447
448 // The number of days is the minimum of 2 raised to (payload_attempt_number
449 // - 1) or kMaxBackoffDays.
450 num_days = min(num_days << power, kMaxBackoffDays);
451
452 // We don't want all retries to happen exactly at the same time when
453 // retrying after backoff. So add some random minutes to fuzz.
454 int fuzz_minutes = utils::FuzzInt(0, kMaxBackoffFuzzMinutes);
455 TimeDelta next_backoff_interval = TimeDelta::FromDays(num_days) +
456 TimeDelta::FromMinutes(fuzz_minutes);
457 LOG(INFO) << "Incrementing the backoff expiry time by "
458 << utils::FormatTimeDelta(next_backoff_interval);
459 SetBackoffExpiryTime(Time::Now() + next_backoff_interval);
460}
461
Jay Srinivasan19409b72013-04-12 19:23:36 -0700462void PayloadState::UpdateCurrentDownloadSource() {
463 current_download_source_ = kNumDownloadSources;
464
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700465 if (using_p2p_for_downloading_) {
466 current_download_source_ = kDownloadSourceHttpPeer;
467 } else if (GetUrlIndex() < candidate_urls_.size()) {
Jay Srinivasan53173b92013-05-17 17:13:01 -0700468 string current_url = candidate_urls_[GetUrlIndex()];
Jay Srinivasan19409b72013-04-12 19:23:36 -0700469 if (StartsWithASCII(current_url, "https://", false))
470 current_download_source_ = kDownloadSourceHttpsServer;
471 else if (StartsWithASCII(current_url, "http://", false))
472 current_download_source_ = kDownloadSourceHttpServer;
473 }
474
475 LOG(INFO) << "Current download source: "
476 << utils::ToString(current_download_source_);
477}
478
479void PayloadState::UpdateBytesDownloaded(size_t count) {
480 SetCurrentBytesDownloaded(
481 current_download_source_,
482 GetCurrentBytesDownloaded(current_download_source_) + count,
483 false);
484 SetTotalBytesDownloaded(
485 current_download_source_,
486 GetTotalBytesDownloaded(current_download_source_) + count,
487 false);
488}
489
490void PayloadState::ReportBytesDownloadedMetrics() {
491 // Report metrics collected from all known download sources to UMA.
492 // The reported data is in Megabytes in order to represent a larger
493 // sample range.
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700494 int download_sources_used = 0;
495 string metric;
496 uint64_t successful_mbs = 0;
497 uint64_t total_mbs = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700498 for (int i = 0; i < kNumDownloadSources; i++) {
499 DownloadSource source = static_cast<DownloadSource>(i);
500 const int kMaxMiBs = 10240; // Anything above 10GB goes in the last bucket.
David Zeuthen44848602013-06-24 13:32:14 -0700501 uint64_t mbs;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700502
David Zeuthen44848602013-06-24 13:32:14 -0700503 // Only consider this download source (and send byte counts) as
504 // having been used if we downloaded a non-trivial amount of bytes
505 // (e.g. at least 1 MiB) that contributed to the final success of
506 // the update. Otherwise we're going to end up with a lot of
507 // zero-byte events in the histogram.
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700508
David Zeuthen44848602013-06-24 13:32:14 -0700509 mbs = GetCurrentBytesDownloaded(source) / kNumBytesInOneMiB;
510 if (mbs > 0) {
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700511 download_sources_used |= (1 << source);
512
David Zeuthen44848602013-06-24 13:32:14 -0700513 metric = "Installer.SuccessfulMBsDownloadedFrom" +
514 utils::ToString(source);
515 successful_mbs += mbs;
516 LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric;
517 system_state_->metrics_lib()->SendToUMA(metric,
518 mbs,
519 0, // min
520 kMaxMiBs,
521 kNumDefaultUmaBuckets);
522 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700523 SetCurrentBytesDownloaded(source, 0, true);
524
Jay Srinivasan19409b72013-04-12 19:23:36 -0700525 mbs = GetTotalBytesDownloaded(source) / kNumBytesInOneMiB;
David Zeuthen44848602013-06-24 13:32:14 -0700526 if (mbs > 0) {
527 metric = "Installer.TotalMBsDownloadedFrom" + utils::ToString(source);
528 total_mbs += mbs;
529 LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric;
530 system_state_->metrics_lib()->SendToUMA(metric,
531 mbs,
532 0, // min
533 kMaxMiBs,
534 kNumDefaultUmaBuckets);
535 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700536 SetTotalBytesDownloaded(source, 0, true);
537 }
Jay Srinivasandbd9ea22013-04-22 17:45:19 -0700538
539 metric = "Installer.DownloadSourcesUsed";
540 LOG(INFO) << "Uploading 0x" << std::hex << download_sources_used
541 << " (bit flags) for metric " << metric;
542 int num_buckets = std::min(1 << kNumDownloadSources, kNumDefaultUmaBuckets);
543 system_state_->metrics_lib()->SendToUMA(metric,
544 download_sources_used,
545 0, // min
546 1 << kNumDownloadSources,
547 num_buckets);
548
549 if (successful_mbs) {
550 metric = "Installer.DownloadOverheadPercentage";
551 int percent_overhead = (total_mbs - successful_mbs) * 100 / successful_mbs;
552 LOG(INFO) << "Uploading " << percent_overhead << "% for metric " << metric;
553 system_state_->metrics_lib()->SendToUMA(metric,
554 percent_overhead,
555 0, // min: 0% overhead
556 1000, // max: 1000% overhead
557 kNumDefaultUmaBuckets);
558 }
Jay Srinivasan19409b72013-04-12 19:23:36 -0700559}
560
David Zeuthencc6f9962013-04-18 11:57:24 -0700561void PayloadState::ReportUpdateUrlSwitchesMetric() {
562 string metric = "Installer.UpdateURLSwitches";
563 int value = static_cast<int>(url_switch_count_);
564
565 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
566 system_state_->metrics_lib()->SendToUMA(
567 metric,
568 value,
569 0, // min value
570 100, // max value
571 kNumDefaultUmaBuckets);
572}
573
Chris Sosabe45bef2013-04-09 18:25:12 -0700574void PayloadState::ReportRebootMetrics() {
575 // Report the number of num_reboots.
576 string metric = "Installer.UpdateNumReboots";
577 uint32_t num_reboots = GetNumReboots();
578 LOG(INFO) << "Uploading reboot count of " << num_reboots << " for metric "
579 << metric;
580 system_state_->metrics_lib()->SendToUMA(
581 metric,
582 static_cast<int>(num_reboots), // sample
583 0, // min = 0.
584 50, // max
585 25); // buckets
586 SetNumReboots(0);
587}
588
589void PayloadState::UpdateNumReboots() {
590 // We only update the reboot count when the system has been detected to have
591 // been rebooted.
592 if (!system_state_->system_rebooted()) {
593 return;
594 }
595
596 SetNumReboots(GetNumReboots() + 1);
597}
598
599void PayloadState::SetNumReboots(uint32_t num_reboots) {
600 CHECK(prefs_);
601 num_reboots_ = num_reboots;
602 prefs_->SetInt64(kPrefsNumReboots, num_reboots);
603 LOG(INFO) << "Number of Reboots during current update attempt = "
604 << num_reboots_;
605}
606
Jay Srinivasan08262882012-12-28 19:29:43 -0800607void PayloadState::ResetPersistedState() {
608 SetPayloadAttemptNumber(0);
Alex Deymo820cc702013-06-28 15:43:46 -0700609 SetFullPayloadAttemptNumber(0);
Jay Srinivasan08262882012-12-28 19:29:43 -0800610 SetUrlIndex(0);
611 SetUrlFailureCount(0);
David Zeuthencc6f9962013-04-18 11:57:24 -0700612 SetUrlSwitchCount(0);
Jay Srinivasan08262882012-12-28 19:29:43 -0800613 UpdateBackoffExpiryTime(); // This will reset the backoff expiry time.
David Zeuthenf413fe52013-04-22 14:04:39 -0700614 SetUpdateTimestampStart(system_state_->clock()->GetWallclockTime());
David Zeuthen9a017f22013-04-11 16:10:26 -0700615 SetUpdateTimestampEnd(Time()); // Set to null time
616 SetUpdateDurationUptime(TimeDelta::FromSeconds(0));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700617 ResetDownloadSourcesOnNewUpdate();
Chris Sosaaa18e162013-06-20 13:20:30 -0700618 ResetRollbackVersion();
David Zeuthendcba8092013-08-06 12:16:35 -0700619 SetP2PNumAttempts(0);
620 SetP2PFirstAttemptTimestamp(Time()); // Set to null time
Chris Sosaaa18e162013-06-20 13:20:30 -0700621}
622
623void PayloadState::ResetRollbackVersion() {
624 CHECK(powerwash_safe_prefs_);
625 rollback_version_ = "";
626 powerwash_safe_prefs_->Delete(kPrefsRollbackVersion);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700627}
628
629void PayloadState::ResetDownloadSourcesOnNewUpdate() {
630 for (int i = 0; i < kNumDownloadSources; i++) {
631 DownloadSource source = static_cast<DownloadSource>(i);
632 SetCurrentBytesDownloaded(source, 0, true);
633 // Note: Not resetting the TotalBytesDownloaded as we want that metric
634 // to count the bytes downloaded across various update attempts until
635 // we have successfully applied the update.
636 }
637}
638
Chris Sosab3dcdb32013-09-04 15:22:12 -0700639int64_t PayloadState::GetPersistedValue(const string& key) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700640 CHECK(prefs_);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700641 if (!prefs_->Exists(key))
Jay Srinivasan19409b72013-04-12 19:23:36 -0700642 return 0;
643
644 int64_t stored_value;
Chris Sosab3dcdb32013-09-04 15:22:12 -0700645 if (!prefs_->GetInt64(key, &stored_value))
Jay Srinivasan19409b72013-04-12 19:23:36 -0700646 return 0;
647
648 if (stored_value < 0) {
649 LOG(ERROR) << key << ": Invalid value (" << stored_value
650 << ") in persisted state. Defaulting to 0";
651 return 0;
652 }
653
654 return stored_value;
Jay Srinivasan08262882012-12-28 19:29:43 -0800655}
656
657string PayloadState::CalculateResponseSignature() {
658 string response_sign = StringPrintf("NumURLs = %d\n",
Jay Srinivasan53173b92013-05-17 17:13:01 -0700659 candidate_urls_.size());
Jay Srinivasan08262882012-12-28 19:29:43 -0800660
Jay Srinivasan53173b92013-05-17 17:13:01 -0700661 for (size_t i = 0; i < candidate_urls_.size(); i++)
662 response_sign += StringPrintf("Candidate Url%d = %s\n",
663 i, candidate_urls_[i].c_str());
Jay Srinivasan08262882012-12-28 19:29:43 -0800664
665 response_sign += StringPrintf("Payload Size = %llu\n"
666 "Payload Sha256 Hash = %s\n"
667 "Metadata Size = %llu\n"
668 "Metadata Signature = %s\n"
669 "Is Delta Payload = %d\n"
670 "Max Failure Count Per Url = %d\n"
671 "Disable Payload Backoff = %d\n",
672 response_.size,
673 response_.hash.c_str(),
674 response_.metadata_size,
675 response_.metadata_signature.c_str(),
676 response_.is_delta_payload,
677 response_.max_failure_count_per_url,
678 response_.disable_payload_backoff);
679 return response_sign;
680}
681
682void PayloadState::LoadResponseSignature() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800683 CHECK(prefs_);
684 string stored_value;
Jay Srinivasan08262882012-12-28 19:29:43 -0800685 if (prefs_->Exists(kPrefsCurrentResponseSignature) &&
686 prefs_->GetString(kPrefsCurrentResponseSignature, &stored_value)) {
687 SetResponseSignature(stored_value);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800688 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800689}
690
Jay Srinivasan19409b72013-04-12 19:23:36 -0700691void PayloadState::SetResponseSignature(const string& response_signature) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800692 CHECK(prefs_);
693 response_signature_ = response_signature;
694 LOG(INFO) << "Current Response Signature = \n" << response_signature_;
695 prefs_->SetString(kPrefsCurrentResponseSignature, response_signature_);
696}
697
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800698void PayloadState::LoadPayloadAttemptNumber() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700699 SetPayloadAttemptNumber(GetPersistedValue(kPrefsPayloadAttemptNumber));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800700}
701
Alex Deymo820cc702013-06-28 15:43:46 -0700702void PayloadState::LoadFullPayloadAttemptNumber() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700703 SetFullPayloadAttemptNumber(GetPersistedValue(
704 kPrefsFullPayloadAttemptNumber));
Alex Deymo820cc702013-06-28 15:43:46 -0700705}
706
707void PayloadState::SetPayloadAttemptNumber(int payload_attempt_number) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800708 CHECK(prefs_);
709 payload_attempt_number_ = payload_attempt_number;
710 LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number_;
711 prefs_->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number_);
712}
713
Alex Deymo820cc702013-06-28 15:43:46 -0700714void PayloadState::SetFullPayloadAttemptNumber(
715 int full_payload_attempt_number) {
716 CHECK(prefs_);
717 full_payload_attempt_number_ = full_payload_attempt_number;
718 LOG(INFO) << "Full Payload Attempt Number = " << full_payload_attempt_number_;
719 prefs_->SetInt64(kPrefsFullPayloadAttemptNumber,
720 full_payload_attempt_number_);
721}
722
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800723void PayloadState::LoadUrlIndex() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700724 SetUrlIndex(GetPersistedValue(kPrefsCurrentUrlIndex));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800725}
726
727void PayloadState::SetUrlIndex(uint32_t url_index) {
728 CHECK(prefs_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800729 url_index_ = url_index;
730 LOG(INFO) << "Current URL Index = " << url_index_;
731 prefs_->SetInt64(kPrefsCurrentUrlIndex, url_index_);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700732
733 // Also update the download source, which is purely dependent on the
734 // current URL index alone.
735 UpdateCurrentDownloadSource();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800736}
737
David Zeuthencc6f9962013-04-18 11:57:24 -0700738void PayloadState::LoadUrlSwitchCount() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700739 SetUrlSwitchCount(GetPersistedValue(kPrefsUrlSwitchCount));
David Zeuthencc6f9962013-04-18 11:57:24 -0700740}
741
742void PayloadState::SetUrlSwitchCount(uint32_t url_switch_count) {
743 CHECK(prefs_);
744 url_switch_count_ = url_switch_count;
745 LOG(INFO) << "URL Switch Count = " << url_switch_count_;
746 prefs_->SetInt64(kPrefsUrlSwitchCount, url_switch_count_);
747}
748
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800749void PayloadState::LoadUrlFailureCount() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700750 SetUrlFailureCount(GetPersistedValue(kPrefsCurrentUrlFailureCount));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800751}
752
753void PayloadState::SetUrlFailureCount(uint32_t url_failure_count) {
754 CHECK(prefs_);
755 url_failure_count_ = url_failure_count;
756 LOG(INFO) << "Current URL (Url" << GetUrlIndex()
757 << ")'s Failure Count = " << url_failure_count_;
758 prefs_->SetInt64(kPrefsCurrentUrlFailureCount, url_failure_count_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800759}
760
Jay Srinivasan08262882012-12-28 19:29:43 -0800761void PayloadState::LoadBackoffExpiryTime() {
762 CHECK(prefs_);
763 int64_t stored_value;
764 if (!prefs_->Exists(kPrefsBackoffExpiryTime))
765 return;
766
767 if (!prefs_->GetInt64(kPrefsBackoffExpiryTime, &stored_value))
768 return;
769
770 Time stored_time = Time::FromInternalValue(stored_value);
771 if (stored_time > Time::Now() + TimeDelta::FromDays(kMaxBackoffDays)) {
772 LOG(ERROR) << "Invalid backoff expiry time ("
773 << utils::ToString(stored_time)
774 << ") in persisted state. Resetting.";
775 stored_time = Time();
776 }
777 SetBackoffExpiryTime(stored_time);
778}
779
780void PayloadState::SetBackoffExpiryTime(const Time& new_time) {
781 CHECK(prefs_);
782 backoff_expiry_time_ = new_time;
783 LOG(INFO) << "Backoff Expiry Time = "
784 << utils::ToString(backoff_expiry_time_);
785 prefs_->SetInt64(kPrefsBackoffExpiryTime,
786 backoff_expiry_time_.ToInternalValue());
787}
788
David Zeuthen9a017f22013-04-11 16:10:26 -0700789TimeDelta PayloadState::GetUpdateDuration() {
David Zeuthenf413fe52013-04-22 14:04:39 -0700790 Time end_time = update_timestamp_end_.is_null()
791 ? system_state_->clock()->GetWallclockTime() :
792 update_timestamp_end_;
David Zeuthen9a017f22013-04-11 16:10:26 -0700793 return end_time - update_timestamp_start_;
794}
795
796void PayloadState::LoadUpdateTimestampStart() {
797 int64_t stored_value;
798 Time stored_time;
799
800 CHECK(prefs_);
801
David Zeuthenf413fe52013-04-22 14:04:39 -0700802 Time now = system_state_->clock()->GetWallclockTime();
David Zeuthen9a017f22013-04-11 16:10:26 -0700803
804 if (!prefs_->Exists(kPrefsUpdateTimestampStart)) {
805 // The preference missing is not unexpected - in that case, just
806 // use the current time as start time
807 stored_time = now;
808 } else if (!prefs_->GetInt64(kPrefsUpdateTimestampStart, &stored_value)) {
809 LOG(ERROR) << "Invalid UpdateTimestampStart value. Resetting.";
810 stored_time = now;
811 } else {
812 stored_time = Time::FromInternalValue(stored_value);
813 }
814
815 // Sanity check: If the time read from disk is in the future
816 // (modulo some slack to account for possible NTP drift
817 // adjustments), something is fishy and we should report and
818 // reset.
819 TimeDelta duration_according_to_stored_time = now - stored_time;
820 if (duration_according_to_stored_time < -kDurationSlack) {
821 LOG(ERROR) << "The UpdateTimestampStart value ("
822 << utils::ToString(stored_time)
823 << ") in persisted state is "
David Zeuthen674c3182013-04-18 14:05:20 -0700824 << utils::FormatTimeDelta(duration_according_to_stored_time)
825 << " in the future. Resetting.";
David Zeuthen9a017f22013-04-11 16:10:26 -0700826 stored_time = now;
827 }
828
829 SetUpdateTimestampStart(stored_time);
830}
831
832void PayloadState::SetUpdateTimestampStart(const Time& value) {
833 CHECK(prefs_);
834 update_timestamp_start_ = value;
835 prefs_->SetInt64(kPrefsUpdateTimestampStart,
836 update_timestamp_start_.ToInternalValue());
837 LOG(INFO) << "Update Timestamp Start = "
838 << utils::ToString(update_timestamp_start_);
839}
840
841void PayloadState::SetUpdateTimestampEnd(const Time& value) {
842 update_timestamp_end_ = value;
843 LOG(INFO) << "Update Timestamp End = "
844 << utils::ToString(update_timestamp_end_);
845}
846
847TimeDelta PayloadState::GetUpdateDurationUptime() {
848 return update_duration_uptime_;
849}
850
851void PayloadState::LoadUpdateDurationUptime() {
852 int64_t stored_value;
853 TimeDelta stored_delta;
854
855 CHECK(prefs_);
856
857 if (!prefs_->Exists(kPrefsUpdateDurationUptime)) {
858 // The preference missing is not unexpected - in that case, just
859 // we'll use zero as the delta
860 } else if (!prefs_->GetInt64(kPrefsUpdateDurationUptime, &stored_value)) {
861 LOG(ERROR) << "Invalid UpdateDurationUptime value. Resetting.";
862 stored_delta = TimeDelta::FromSeconds(0);
863 } else {
864 stored_delta = TimeDelta::FromInternalValue(stored_value);
865 }
866
867 // Sanity-check: Uptime can never be greater than the wall-clock
868 // difference (modulo some slack). If it is, report and reset
869 // to the wall-clock difference.
870 TimeDelta diff = GetUpdateDuration() - stored_delta;
871 if (diff < -kDurationSlack) {
872 LOG(ERROR) << "The UpdateDurationUptime value ("
David Zeuthen674c3182013-04-18 14:05:20 -0700873 << utils::FormatTimeDelta(stored_delta)
David Zeuthen9a017f22013-04-11 16:10:26 -0700874 << ") in persisted state is "
David Zeuthen674c3182013-04-18 14:05:20 -0700875 << utils::FormatTimeDelta(diff)
876 << " larger than the wall-clock delta. Resetting.";
David Zeuthen9a017f22013-04-11 16:10:26 -0700877 stored_delta = update_duration_current_;
878 }
879
880 SetUpdateDurationUptime(stored_delta);
881}
882
Chris Sosabe45bef2013-04-09 18:25:12 -0700883void PayloadState::LoadNumReboots() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700884 SetNumReboots(GetPersistedValue(kPrefsNumReboots));
Chris Sosaaa18e162013-06-20 13:20:30 -0700885}
886
887void PayloadState::LoadRollbackVersion() {
Chris Sosab3dcdb32013-09-04 15:22:12 -0700888 CHECK(powerwash_safe_prefs_);
889 string rollback_version;
890 if (powerwash_safe_prefs_->GetString(kPrefsRollbackVersion,
891 &rollback_version)) {
892 SetRollbackVersion(rollback_version);
893 }
Chris Sosaaa18e162013-06-20 13:20:30 -0700894}
895
896void PayloadState::SetRollbackVersion(const string& rollback_version) {
897 CHECK(powerwash_safe_prefs_);
898 LOG(INFO) << "Blacklisting version "<< rollback_version;
899 rollback_version_ = rollback_version;
900 powerwash_safe_prefs_->SetString(kPrefsRollbackVersion, rollback_version);
Chris Sosabe45bef2013-04-09 18:25:12 -0700901}
902
David Zeuthen9a017f22013-04-11 16:10:26 -0700903void PayloadState::SetUpdateDurationUptimeExtended(const TimeDelta& value,
904 const Time& timestamp,
905 bool use_logging) {
906 CHECK(prefs_);
907 update_duration_uptime_ = value;
908 update_duration_uptime_timestamp_ = timestamp;
909 prefs_->SetInt64(kPrefsUpdateDurationUptime,
910 update_duration_uptime_.ToInternalValue());
911 if (use_logging) {
912 LOG(INFO) << "Update Duration Uptime = "
David Zeuthen674c3182013-04-18 14:05:20 -0700913 << utils::FormatTimeDelta(update_duration_uptime_);
David Zeuthen9a017f22013-04-11 16:10:26 -0700914 }
915}
916
917void PayloadState::SetUpdateDurationUptime(const TimeDelta& value) {
David Zeuthenf413fe52013-04-22 14:04:39 -0700918 Time now = system_state_->clock()->GetMonotonicTime();
919 SetUpdateDurationUptimeExtended(value, now, true);
David Zeuthen9a017f22013-04-11 16:10:26 -0700920}
921
922void PayloadState::CalculateUpdateDurationUptime() {
David Zeuthenf413fe52013-04-22 14:04:39 -0700923 Time now = system_state_->clock()->GetMonotonicTime();
David Zeuthen9a017f22013-04-11 16:10:26 -0700924 TimeDelta uptime_since_last_update = now - update_duration_uptime_timestamp_;
925 TimeDelta new_uptime = update_duration_uptime_ + uptime_since_last_update;
926 // We're frequently called so avoid logging this write
927 SetUpdateDurationUptimeExtended(new_uptime, now, false);
928}
929
David Zeuthen674c3182013-04-18 14:05:20 -0700930void PayloadState::ReportDurationMetrics() {
931 TimeDelta duration = GetUpdateDuration();
932 TimeDelta duration_uptime = GetUpdateDurationUptime();
933 string metric;
934
935 metric = "Installer.UpdateDurationMinutes";
936 system_state_->metrics_lib()->SendToUMA(
937 metric,
938 static_cast<int>(duration.InMinutes()),
939 1, // min: 1 minute
940 365*24*60, // max: 1 year (approx)
941 kNumDefaultUmaBuckets);
942 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration)
943 << " for metric " << metric;
944
945 metric = "Installer.UpdateDurationUptimeMinutes";
946 system_state_->metrics_lib()->SendToUMA(
947 metric,
948 static_cast<int>(duration_uptime.InMinutes()),
949 1, // min: 1 minute
950 30*24*60, // max: 1 month (approx)
951 kNumDefaultUmaBuckets);
952 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration_uptime)
953 << " for metric " << metric;
954
955 prefs_->Delete(kPrefsUpdateTimestampStart);
956 prefs_->Delete(kPrefsUpdateDurationUptime);
957}
958
Alex Deymo1c656c42013-06-28 11:02:14 -0700959void PayloadState::ReportPayloadTypeMetric() {
960 string metric;
961 PayloadType uma_payload_type;
962 OmahaRequestParams* params = system_state_->request_params();
963
964 if (response_.is_delta_payload) {
965 uma_payload_type = kPayloadTypeDelta;
966 } else if (params->delta_okay()) {
967 uma_payload_type = kPayloadTypeFull;
968 } else { // Full payload, delta was not allowed by request.
969 uma_payload_type = kPayloadTypeForcedFull;
970 }
971
972 metric = "Installer.PayloadFormat";
973 system_state_->metrics_lib()->SendEnumToUMA(
974 metric,
975 uma_payload_type,
976 kNumPayloadTypes);
977 LOG(INFO) << "Uploading " << utils::ToString(uma_payload_type)
978 << " for metric " << metric;
979}
980
Alex Deymo820cc702013-06-28 15:43:46 -0700981void PayloadState::ReportAttemptsCountMetrics() {
982 string metric;
983 int total_attempts = GetPayloadAttemptNumber();
984
985 metric = "Installer.AttemptsCount.Total";
986 system_state_->metrics_lib()->SendToUMA(
987 metric,
988 total_attempts,
989 1, // min
990 50, // max
991 kNumDefaultUmaBuckets);
992 LOG(INFO) << "Uploading " << total_attempts
993 << " for metric " << metric;
994}
995
Jay Srinivasan19409b72013-04-12 19:23:36 -0700996string PayloadState::GetPrefsKey(const string& prefix, DownloadSource source) {
997 return prefix + "-from-" + utils::ToString(source);
998}
999
1000void PayloadState::LoadCurrentBytesDownloaded(DownloadSource source) {
1001 string key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source);
Chris Sosab3dcdb32013-09-04 15:22:12 -07001002 SetCurrentBytesDownloaded(source, GetPersistedValue(key), true);
Jay Srinivasan19409b72013-04-12 19:23:36 -07001003}
1004
1005void PayloadState::SetCurrentBytesDownloaded(
1006 DownloadSource source,
1007 uint64_t current_bytes_downloaded,
1008 bool log) {
1009 CHECK(prefs_);
1010
1011 if (source >= kNumDownloadSources)
1012 return;
1013
1014 // Update the in-memory value.
1015 current_bytes_downloaded_[source] = current_bytes_downloaded;
1016
1017 string prefs_key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source);
1018 prefs_->SetInt64(prefs_key, current_bytes_downloaded);
1019 LOG_IF(INFO, log) << "Current bytes downloaded for "
1020 << utils::ToString(source) << " = "
1021 << GetCurrentBytesDownloaded(source);
1022}
1023
1024void PayloadState::LoadTotalBytesDownloaded(DownloadSource source) {
1025 string key = GetPrefsKey(kPrefsTotalBytesDownloaded, source);
Chris Sosab3dcdb32013-09-04 15:22:12 -07001026 SetTotalBytesDownloaded(source, GetPersistedValue(key), true);
Jay Srinivasan19409b72013-04-12 19:23:36 -07001027}
1028
1029void PayloadState::SetTotalBytesDownloaded(
1030 DownloadSource source,
1031 uint64_t total_bytes_downloaded,
1032 bool log) {
1033 CHECK(prefs_);
1034
1035 if (source >= kNumDownloadSources)
1036 return;
1037
1038 // Update the in-memory value.
1039 total_bytes_downloaded_[source] = total_bytes_downloaded;
1040
1041 // Persist.
1042 string prefs_key = GetPrefsKey(kPrefsTotalBytesDownloaded, source);
1043 prefs_->SetInt64(prefs_key, total_bytes_downloaded);
1044 LOG_IF(INFO, log) << "Total bytes downloaded for "
1045 << utils::ToString(source) << " = "
1046 << GetTotalBytesDownloaded(source);
1047}
1048
David Zeuthena573d6f2013-06-14 16:13:36 -07001049void PayloadState::LoadNumResponsesSeen() {
Chris Sosab3dcdb32013-09-04 15:22:12 -07001050 SetNumResponsesSeen(GetPersistedValue(kPrefsNumResponsesSeen));
David Zeuthena573d6f2013-06-14 16:13:36 -07001051}
1052
1053void PayloadState::SetNumResponsesSeen(int num_responses_seen) {
1054 CHECK(prefs_);
1055 num_responses_seen_ = num_responses_seen;
1056 LOG(INFO) << "Num Responses Seen = " << num_responses_seen_;
1057 prefs_->SetInt64(kPrefsNumResponsesSeen, num_responses_seen_);
1058}
1059
1060void PayloadState::ReportUpdatesAbandonedCountMetric() {
1061 string metric = "Installer.UpdatesAbandonedCount";
1062 int value = num_responses_seen_ - 1;
1063
1064 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
1065 system_state_->metrics_lib()->SendToUMA(
1066 metric,
1067 value,
1068 0, // min value
1069 100, // max value
1070 kNumDefaultUmaBuckets);
1071}
1072
Alex Deymob33b0f02013-08-08 21:10:02 -07001073void PayloadState::ReportUpdatesAbandonedEventCountMetric() {
1074 string metric = "Installer.UpdatesAbandonedEventCount";
1075 int value = num_responses_seen_ - 1;
1076
1077 // Do not send an "abandoned" event when 0 payloads were abandoned since the
1078 // last successful update.
1079 if (value == 0)
1080 return;
1081
1082 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
1083 system_state_->metrics_lib()->SendToUMA(
1084 metric,
1085 value,
1086 0, // min value
1087 100, // max value
1088 kNumDefaultUmaBuckets);
1089}
1090
Jay Srinivasan53173b92013-05-17 17:13:01 -07001091void PayloadState::ComputeCandidateUrls() {
Chris Sosaf7d80042013-08-22 16:45:17 -07001092 bool http_url_ok = true;
Jay Srinivasan53173b92013-05-17 17:13:01 -07001093
J. Richard Barnette056b0ab2013-10-29 15:24:56 -07001094 if (system_state_->hardware()->IsOfficialBuild()) {
Jay Srinivasan53173b92013-05-17 17:13:01 -07001095 const policy::DevicePolicy* policy = system_state_->device_policy();
Chris Sosaf7d80042013-08-22 16:45:17 -07001096 if (policy && policy->GetHttpDownloadsEnabled(&http_url_ok) && !http_url_ok)
Jay Srinivasan53173b92013-05-17 17:13:01 -07001097 LOG(INFO) << "Downloads via HTTP Url are not enabled by device policy";
1098 } else {
1099 LOG(INFO) << "Allowing HTTP downloads for unofficial builds";
1100 http_url_ok = true;
1101 }
1102
1103 candidate_urls_.clear();
1104 for (size_t i = 0; i < response_.payload_urls.size(); i++) {
1105 string candidate_url = response_.payload_urls[i];
1106 if (StartsWithASCII(candidate_url, "http://", false) && !http_url_ok)
1107 continue;
1108 candidate_urls_.push_back(candidate_url);
1109 LOG(INFO) << "Candidate Url" << (candidate_urls_.size() - 1)
1110 << ": " << candidate_url;
1111 }
1112
1113 LOG(INFO) << "Found " << candidate_urls_.size() << " candidate URLs "
1114 << "out of " << response_.payload_urls.size() << " URLs supplied";
1115}
1116
David Zeuthene4c58bf2013-06-18 17:26:50 -07001117void PayloadState::CreateSystemUpdatedMarkerFile() {
1118 CHECK(prefs_);
1119 int64_t value = system_state_->clock()->GetWallclockTime().ToInternalValue();
1120 prefs_->SetInt64(kPrefsSystemUpdatedMarker, value);
1121}
1122
1123void PayloadState::BootedIntoUpdate(TimeDelta time_to_reboot) {
1124 // Send |time_to_reboot| as a UMA stat.
1125 string metric = "Installer.TimeToRebootMinutes";
1126 system_state_->metrics_lib()->SendToUMA(metric,
1127 time_to_reboot.InMinutes(),
1128 0, // min: 0 minute
1129 30*24*60, // max: 1 month (approx)
1130 kNumDefaultUmaBuckets);
1131 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(time_to_reboot)
1132 << " for metric " << metric;
1133}
1134
1135void PayloadState::UpdateEngineStarted() {
Alex Deymo569c4242013-07-24 12:01:01 -07001136 // Avoid the UpdateEngineStarted actions if this is not the first time we
1137 // run the update engine since reboot.
1138 if (!system_state_->system_rebooted())
1139 return;
1140
David Zeuthene4c58bf2013-06-18 17:26:50 -07001141 // Figure out if we just booted into a new update
1142 if (prefs_->Exists(kPrefsSystemUpdatedMarker)) {
1143 int64_t stored_value;
1144 if (prefs_->GetInt64(kPrefsSystemUpdatedMarker, &stored_value)) {
1145 Time system_updated_at = Time::FromInternalValue(stored_value);
1146 if (!system_updated_at.is_null()) {
1147 TimeDelta time_to_reboot =
1148 system_state_->clock()->GetWallclockTime() - system_updated_at;
1149 if (time_to_reboot.ToInternalValue() < 0) {
1150 LOG(ERROR) << "time_to_reboot is negative - system_updated_at: "
1151 << utils::ToString(system_updated_at);
1152 } else {
1153 BootedIntoUpdate(time_to_reboot);
1154 }
1155 }
1156 }
1157 prefs_->Delete(kPrefsSystemUpdatedMarker);
1158 }
Alex Deymo42432912013-07-12 20:21:15 -07001159 // Check if it is needed to send metrics about a failed reboot into a new
1160 // version.
1161 ReportFailedBootIfNeeded();
1162}
1163
1164void PayloadState::ReportFailedBootIfNeeded() {
1165 // If the kPrefsTargetVersionInstalledFrom is present, a successfully applied
1166 // payload was marked as ready immediately before the last reboot, and we
1167 // need to check if such payload successfully rebooted or not.
1168 if (prefs_->Exists(kPrefsTargetVersionInstalledFrom)) {
Alex Vakulenko4f5b1442014-02-21 12:19:44 -08001169 int64_t installed_from = 0;
1170 if (!prefs_->GetInt64(kPrefsTargetVersionInstalledFrom, &installed_from)) {
Alex Deymo42432912013-07-12 20:21:15 -07001171 LOG(ERROR) << "Error reading TargetVersionInstalledFrom on reboot.";
1172 return;
1173 }
Alex Vakulenko4f5b1442014-02-21 12:19:44 -08001174 if (int(installed_from) ==
1175 utils::GetPartitionNumber(system_state_->hardware()->BootDevice())) {
Alex Deymo42432912013-07-12 20:21:15 -07001176 // A reboot was pending, but the chromebook is again in the same
1177 // BootDevice where the update was installed from.
1178 int64_t target_attempt;
1179 if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt)) {
1180 LOG(ERROR) << "Error reading TargetVersionAttempt when "
1181 "TargetVersionInstalledFrom was present.";
1182 target_attempt = 1;
1183 }
1184
1185 // Report the UMA metric of the current boot failure.
1186 string metric = "Installer.RebootToNewPartitionAttempt";
1187
1188 LOG(INFO) << "Uploading " << target_attempt
1189 << " (count) for metric " << metric;
1190 system_state_->metrics_lib()->SendToUMA(
1191 metric,
1192 target_attempt,
1193 1, // min value
1194 50, // max value
1195 kNumDefaultUmaBuckets);
1196 } else {
1197 prefs_->Delete(kPrefsTargetVersionAttempt);
1198 prefs_->Delete(kPrefsTargetVersionUniqueId);
1199 }
1200 prefs_->Delete(kPrefsTargetVersionInstalledFrom);
1201 }
1202}
1203
1204void PayloadState::ExpectRebootInNewVersion(const string& target_version_uid) {
1205 // Expect to boot into the new partition in the next reboot setting the
1206 // TargetVersion* flags in the Prefs.
1207 string stored_target_version_uid;
1208 string target_version_id;
1209 string target_partition;
1210 int64_t target_attempt;
1211
1212 if (prefs_->Exists(kPrefsTargetVersionUniqueId) &&
1213 prefs_->GetString(kPrefsTargetVersionUniqueId,
1214 &stored_target_version_uid) &&
1215 stored_target_version_uid == target_version_uid) {
1216 if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt))
1217 target_attempt = 0;
1218 } else {
1219 prefs_->SetString(kPrefsTargetVersionUniqueId, target_version_uid);
1220 target_attempt = 0;
1221 }
1222 prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt + 1);
1223
Alex Vakulenko4f5b1442014-02-21 12:19:44 -08001224 prefs_->SetInt64(kPrefsTargetVersionInstalledFrom,
1225 utils::GetPartitionNumber(
Alex Deymo42432912013-07-12 20:21:15 -07001226 system_state_->hardware()->BootDevice()));
1227}
1228
1229void PayloadState::ResetUpdateStatus() {
1230 // Remove the TargetVersionInstalledFrom pref so that if the machine is
1231 // rebooted the next boot is not flagged as failed to rebooted into the
1232 // new applied payload.
1233 prefs_->Delete(kPrefsTargetVersionInstalledFrom);
1234
1235 // Also decrement the attempt number if it exists.
1236 int64_t target_attempt;
1237 if (prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt))
1238 prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt-1);
David Zeuthene4c58bf2013-06-18 17:26:50 -07001239}
1240
David Zeuthendcba8092013-08-06 12:16:35 -07001241int PayloadState::GetP2PNumAttempts() {
1242 return p2p_num_attempts_;
1243}
1244
1245void PayloadState::SetP2PNumAttempts(int value) {
1246 p2p_num_attempts_ = value;
1247 LOG(INFO) << "p2p Num Attempts = " << p2p_num_attempts_;
1248 CHECK(prefs_);
1249 prefs_->SetInt64(kPrefsP2PNumAttempts, value);
1250}
1251
1252void PayloadState::LoadP2PNumAttempts() {
Chris Sosab3dcdb32013-09-04 15:22:12 -07001253 SetP2PNumAttempts(GetPersistedValue(kPrefsP2PNumAttempts));
David Zeuthendcba8092013-08-06 12:16:35 -07001254}
1255
1256Time PayloadState::GetP2PFirstAttemptTimestamp() {
1257 return p2p_first_attempt_timestamp_;
1258}
1259
1260void PayloadState::SetP2PFirstAttemptTimestamp(const Time& time) {
1261 p2p_first_attempt_timestamp_ = time;
1262 LOG(INFO) << "p2p First Attempt Timestamp = "
1263 << utils::ToString(p2p_first_attempt_timestamp_);
1264 CHECK(prefs_);
1265 int64_t stored_value = time.ToInternalValue();
1266 prefs_->SetInt64(kPrefsP2PFirstAttemptTimestamp, stored_value);
1267}
1268
1269void PayloadState::LoadP2PFirstAttemptTimestamp() {
Chris Sosab3dcdb32013-09-04 15:22:12 -07001270 int64_t stored_value = GetPersistedValue(kPrefsP2PFirstAttemptTimestamp);
David Zeuthendcba8092013-08-06 12:16:35 -07001271 Time stored_time = Time::FromInternalValue(stored_value);
1272 SetP2PFirstAttemptTimestamp(stored_time);
1273}
1274
1275void PayloadState::P2PNewAttempt() {
1276 CHECK(prefs_);
1277 // Set timestamp, if it hasn't been set already
1278 if (p2p_first_attempt_timestamp_.is_null()) {
1279 SetP2PFirstAttemptTimestamp(system_state_->clock()->GetWallclockTime());
1280 }
1281 // Increase number of attempts
1282 SetP2PNumAttempts(GetP2PNumAttempts() + 1);
1283}
1284
1285bool PayloadState::P2PAttemptAllowed() {
1286 if (p2p_num_attempts_ > kMaxP2PAttempts) {
1287 LOG(INFO) << "Number of p2p attempts is " << p2p_num_attempts_
1288 << " which is greater than "
1289 << kMaxP2PAttempts
1290 << " - disallowing p2p.";
1291 return false;
1292 }
1293
1294 if (!p2p_first_attempt_timestamp_.is_null()) {
1295 Time now = system_state_->clock()->GetWallclockTime();
1296 TimeDelta time_spent_attempting_p2p = now - p2p_first_attempt_timestamp_;
1297 if (time_spent_attempting_p2p.InSeconds() < 0) {
1298 LOG(ERROR) << "Time spent attempting p2p is negative"
1299 << " - disallowing p2p.";
1300 return false;
1301 }
1302 if (time_spent_attempting_p2p.InSeconds() > kMaxP2PAttemptTimeSeconds) {
1303 LOG(INFO) << "Time spent attempting p2p is "
1304 << utils::FormatTimeDelta(time_spent_attempting_p2p)
1305 << " which is greater than "
1306 << utils::FormatTimeDelta(TimeDelta::FromSeconds(
1307 kMaxP2PAttemptTimeSeconds))
1308 << " - disallowing p2p.";
1309 return false;
1310 }
1311 }
1312
1313 return true;
1314}
1315
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001316} // namespace chromeos_update_engine