blob: 77652a78e7e2a9f9c8e6fd01783f631158a1ba2a [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
Jay Srinivasand29695d2013-04-08 15:08:05 -070013#include "update_engine/constants.h"
Jay Srinivasan19409b72013-04-12 19:23:36 -070014#include "update_engine/prefs.h"
15#include "update_engine/system_state.h"
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080016#include "update_engine/utils.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080017
Jay Srinivasan08262882012-12-28 19:29:43 -080018using base::Time;
19using base::TimeDelta;
20using std::min;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080021using std::string;
22
23namespace chromeos_update_engine {
24
David Zeuthen9a017f22013-04-11 16:10:26 -070025const TimeDelta PayloadState::kDurationSlack = TimeDelta::FromSeconds(600);
26
Jay Srinivasan08262882012-12-28 19:29:43 -080027// We want to upperbound backoffs to 16 days
28static const uint32_t kMaxBackoffDays = 16;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080029
Jay Srinivasan08262882012-12-28 19:29:43 -080030// We want to randomize retry attempts after the backoff by +/- 6 hours.
31static const uint32_t kMaxBackoffFuzzMinutes = 12 * 60;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080032
Jay Srinivasan19409b72013-04-12 19:23:36 -070033PayloadState::PayloadState()
34 : prefs_(NULL),
35 payload_attempt_number_(0),
36 url_index_(0),
David Zeuthencc6f9962013-04-18 11:57:24 -070037 url_failure_count_(0),
38 url_switch_count_(0) {
Jay Srinivasan19409b72013-04-12 19:23:36 -070039 for (int i = 0; i <= kNumDownloadSources; i++)
40 total_bytes_downloaded_[i] = current_bytes_downloaded_[i] = 0;
41}
42
43bool PayloadState::Initialize(SystemState* system_state) {
44 system_state_ = system_state;
45 prefs_ = system_state_->prefs();
Jay Srinivasan08262882012-12-28 19:29:43 -080046 LoadResponseSignature();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080047 LoadPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080048 LoadUrlIndex();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080049 LoadUrlFailureCount();
David Zeuthencc6f9962013-04-18 11:57:24 -070050 LoadUrlSwitchCount();
Jay Srinivasan08262882012-12-28 19:29:43 -080051 LoadBackoffExpiryTime();
David Zeuthen9a017f22013-04-11 16:10:26 -070052 LoadUpdateTimestampStart();
53 // The LoadUpdateDurationUptime() method relies on LoadUpdateTimestampStart()
54 // being called before it. Don't reorder.
55 LoadUpdateDurationUptime();
Jay Srinivasan19409b72013-04-12 19:23:36 -070056 for (int i = 0; i < kNumDownloadSources; i++) {
57 DownloadSource source = static_cast<DownloadSource>(i);
58 LoadCurrentBytesDownloaded(source);
59 LoadTotalBytesDownloaded(source);
60 }
Chris Sosabe45bef2013-04-09 18:25:12 -070061 LoadNumReboots();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080062 return true;
63}
64
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080065void PayloadState::SetResponse(const OmahaResponse& omaha_response) {
Jay Srinivasan08262882012-12-28 19:29:43 -080066 // Always store the latest response.
67 response_ = omaha_response;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080068
Jay Srinivasan08262882012-12-28 19:29:43 -080069 // Check if the "signature" of this response (i.e. the fields we care about)
70 // has changed.
71 string new_response_signature = CalculateResponseSignature();
72 bool has_response_changed = (response_signature_ != new_response_signature);
73
74 // If the response has changed, we should persist the new signature and
75 // clear away all the existing state.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080076 if (has_response_changed) {
Jay Srinivasan08262882012-12-28 19:29:43 -080077 LOG(INFO) << "Resetting all persisted state as this is a new response";
78 SetResponseSignature(new_response_signature);
79 ResetPersistedState();
80 return;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080081 }
82
Jay Srinivasan08262882012-12-28 19:29:43 -080083 // This is the earliest point at which we can validate whether the URL index
84 // we loaded from the persisted state is a valid value. If the response
85 // hasn't changed but the URL index is invalid, it's indicative of some
86 // tampering of the persisted state.
87 if (url_index_ >= GetNumUrls()) {
88 LOG(INFO) << "Resetting all payload state as the url index seems to have "
89 "been tampered with";
90 ResetPersistedState();
91 return;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080092 }
Jay Srinivasan19409b72013-04-12 19:23:36 -070093
94 // Update the current download source which depends on the latest value of
95 // the response.
96 UpdateCurrentDownloadSource();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080097}
98
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080099void PayloadState::DownloadComplete() {
100 LOG(INFO) << "Payload downloaded successfully";
101 IncrementPayloadAttemptNumber();
102}
103
104void PayloadState::DownloadProgress(size_t count) {
105 if (count == 0)
106 return;
107
David Zeuthen9a017f22013-04-11 16:10:26 -0700108 CalculateUpdateDurationUptime();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700109 UpdateBytesDownloaded(count);
David Zeuthen9a017f22013-04-11 16:10:26 -0700110
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800111 // We've received non-zero bytes from a recent download operation. Since our
112 // URL failure count is meant to penalize a URL only for consecutive
113 // failures, downloading bytes successfully means we should reset the failure
114 // count (as we know at least that the URL is working). In future, we can
115 // design this to be more sophisticated to check for more intelligent failure
116 // patterns, but right now, even 1 byte downloaded will mark the URL to be
117 // good unless it hits 10 (or configured number of) consecutive failures
118 // again.
119
120 if (GetUrlFailureCount() == 0)
121 return;
122
123 LOG(INFO) << "Resetting failure count of Url" << GetUrlIndex()
124 << " to 0 as we received " << count << " bytes successfully";
125 SetUrlFailureCount(0);
126}
127
Chris Sosabe45bef2013-04-09 18:25:12 -0700128void PayloadState::UpdateResumed() {
129 LOG(INFO) << "Resuming an update that was previously started.";
130 UpdateNumReboots();
131}
132
Jay Srinivasan19409b72013-04-12 19:23:36 -0700133void PayloadState::UpdateRestarted() {
134 LOG(INFO) << "Starting a new update";
135 ResetDownloadSourcesOnNewUpdate();
Chris Sosabe45bef2013-04-09 18:25:12 -0700136 SetNumReboots(0);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700137}
138
David Zeuthen9a017f22013-04-11 16:10:26 -0700139void PayloadState::UpdateSucceeded() {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700140 // Send the relevant metrics that are tracked in this class to UMA.
David Zeuthen9a017f22013-04-11 16:10:26 -0700141 CalculateUpdateDurationUptime();
142 SetUpdateTimestampEnd(Time::Now());
Jay Srinivasan19409b72013-04-12 19:23:36 -0700143 ReportBytesDownloadedMetrics();
David Zeuthencc6f9962013-04-18 11:57:24 -0700144 ReportUpdateUrlSwitchesMetric();
Chris Sosabe45bef2013-04-09 18:25:12 -0700145 ReportRebootMetrics();
David Zeuthen674c3182013-04-18 14:05:20 -0700146 ReportDurationMetrics();
David Zeuthen9a017f22013-04-11 16:10:26 -0700147}
148
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800149void PayloadState::UpdateFailed(ActionExitCode error) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800150 ActionExitCode base_error = utils::GetBaseErrorCode(error);
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800151 LOG(INFO) << "Updating payload state for error code: " << base_error
152 << " (" << utils::CodeToString(base_error) << ")";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800153
Jay Srinivasan08262882012-12-28 19:29:43 -0800154 if (GetNumUrls() == 0) {
155 // This means we got this error even before we got a valid Omaha response.
156 // So we should not advance the url_index_ in such cases.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800157 LOG(INFO) << "Ignoring failures until we get a valid Omaha response.";
158 return;
159 }
160
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800161 switch (base_error) {
162 // Errors which are good indicators of a problem with a particular URL or
163 // the protocol used in the URL or entities in the communication channel
164 // (e.g. proxies). We should try the next available URL in the next update
165 // check to quickly recover from these errors.
166 case kActionCodePayloadHashMismatchError:
167 case kActionCodePayloadSizeMismatchError:
168 case kActionCodeDownloadPayloadVerificationError:
169 case kActionCodeDownloadPayloadPubKeyVerificationError:
170 case kActionCodeSignedDeltaPayloadExpectedError:
171 case kActionCodeDownloadInvalidMetadataMagicString:
172 case kActionCodeDownloadSignatureMissingInManifest:
173 case kActionCodeDownloadManifestParseError:
174 case kActionCodeDownloadMetadataSignatureError:
175 case kActionCodeDownloadMetadataSignatureVerificationError:
176 case kActionCodeDownloadMetadataSignatureMismatch:
177 case kActionCodeDownloadOperationHashVerificationError:
178 case kActionCodeDownloadOperationExecutionError:
179 case kActionCodeDownloadOperationHashMismatch:
180 case kActionCodeDownloadInvalidMetadataSize:
181 case kActionCodeDownloadInvalidMetadataSignature:
182 case kActionCodeDownloadOperationHashMissingError:
183 case kActionCodeDownloadMetadataSignatureMissingError:
184 IncrementUrlIndex();
185 break;
186
187 // Errors which seem to be just transient network/communication related
188 // failures and do not indicate any inherent problem with the URL itself.
189 // So, we should keep the current URL but just increment the
190 // failure count to give it more chances. This way, while we maximize our
191 // chances of downloading from the URLs that appear earlier in the response
192 // (because download from a local server URL that appears earlier in a
193 // response is preferable than downloading from the next URL which could be
194 // a internet URL and thus could be more expensive).
195 case kActionCodeError:
196 case kActionCodeDownloadTransferError:
197 case kActionCodeDownloadWriteError:
198 case kActionCodeDownloadStateInitializationError:
199 case kActionCodeOmahaErrorInHTTPResponse: // Aggregate code for HTTP errors.
200 IncrementFailureCount();
201 break;
202
203 // Errors which are not specific to a URL and hence shouldn't result in
204 // the URL being penalized. This can happen in two cases:
205 // 1. We haven't started downloading anything: These errors don't cost us
206 // anything in terms of actual payload bytes, so we should just do the
207 // regular retries at the next update check.
208 // 2. We have successfully downloaded the payload: In this case, the
209 // payload attempt number would have been incremented and would take care
Jay Srinivasan08262882012-12-28 19:29:43 -0800210 // of the backoff at the next update check.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800211 // In either case, there's no need to update URL index or failure count.
212 case kActionCodeOmahaRequestError:
213 case kActionCodeOmahaResponseHandlerError:
214 case kActionCodePostinstallRunnerError:
215 case kActionCodeFilesystemCopierError:
216 case kActionCodeInstallDeviceOpenError:
217 case kActionCodeKernelDeviceOpenError:
218 case kActionCodeDownloadNewPartitionInfoError:
219 case kActionCodeNewRootfsVerificationError:
220 case kActionCodeNewKernelVerificationError:
221 case kActionCodePostinstallBootedFromFirmwareB:
222 case kActionCodeOmahaRequestEmptyResponseError:
223 case kActionCodeOmahaRequestXMLParseError:
224 case kActionCodeOmahaResponseInvalid:
225 case kActionCodeOmahaUpdateIgnoredPerPolicy:
226 case kActionCodeOmahaUpdateDeferredPerPolicy:
Jay Srinivasan08262882012-12-28 19:29:43 -0800227 case kActionCodeOmahaUpdateDeferredForBackoff:
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700228 case kActionCodePostinstallPowerwashError:
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700229 case kActionCodeUpdateCanceledByChannelChange:
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800230 LOG(INFO) << "Not incrementing URL index or failure count for this error";
231 break;
232
233 case kActionCodeSuccess: // success code
234 case kActionCodeSetBootableFlagError: // unused
235 case kActionCodeUmaReportedMax: // not an error code
236 case kActionCodeOmahaRequestHTTPResponseBase: // aggregated already
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800237 case kActionCodeDevModeFlag: // not an error code
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800238 case kActionCodeResumedFlag: // not an error code
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800239 case kActionCodeTestImageFlag: // not an error code
240 case kActionCodeTestOmahaUrlFlag: // not an error code
241 case kSpecialFlags: // not an error code
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800242 // These shouldn't happen. Enumerating these explicitly here so that we
243 // can let the compiler warn about new error codes that are added to
244 // action_processor.h but not added here.
245 LOG(WARNING) << "Unexpected error code for UpdateFailed";
246 break;
247
248 // Note: Not adding a default here so as to let the compiler warn us of
249 // any new enums that were added in the .h but not listed in this switch.
250 }
251}
252
Jay Srinivasan08262882012-12-28 19:29:43 -0800253bool PayloadState::ShouldBackoffDownload() {
254 if (response_.disable_payload_backoff) {
255 LOG(INFO) << "Payload backoff logic is disabled. "
256 "Can proceed with the download";
257 return false;
258 }
259
260 if (response_.is_delta_payload) {
261 // If delta payloads fail, we want to fallback quickly to full payloads as
262 // they are more likely to succeed. Exponential backoffs would greatly
263 // slow down the fallback to full payloads. So we don't backoff for delta
264 // payloads.
265 LOG(INFO) << "No backoffs for delta payloads. "
266 << "Can proceed with the download";
267 return false;
268 }
269
270 if (!utils::IsOfficialBuild()) {
271 // Backoffs are needed only for official builds. We do not want any delays
272 // or update failures due to backoffs during testing or development.
273 LOG(INFO) << "No backoffs for test/dev images. "
274 << "Can proceed with the download";
275 return false;
276 }
277
278 if (backoff_expiry_time_.is_null()) {
279 LOG(INFO) << "No backoff expiry time has been set. "
280 << "Can proceed with the download";
281 return false;
282 }
283
284 if (backoff_expiry_time_ < Time::Now()) {
285 LOG(INFO) << "The backoff expiry time ("
286 << utils::ToString(backoff_expiry_time_)
287 << ") has elapsed. Can proceed with the download";
288 return false;
289 }
290
291 LOG(INFO) << "Cannot proceed with downloads as we need to backoff until "
292 << utils::ToString(backoff_expiry_time_);
293 return true;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800294}
295
296void PayloadState::IncrementPayloadAttemptNumber() {
Jay Srinivasan08262882012-12-28 19:29:43 -0800297 if (response_.is_delta_payload) {
298 LOG(INFO) << "Not incrementing payload attempt number for delta payloads";
299 return;
300 }
301
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800302 LOG(INFO) << "Incrementing the payload attempt number";
303 SetPayloadAttemptNumber(GetPayloadAttemptNumber() + 1);
Jay Srinivasan08262882012-12-28 19:29:43 -0800304 UpdateBackoffExpiryTime();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800305}
306
307void PayloadState::IncrementUrlIndex() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800308 uint32_t next_url_index = GetUrlIndex() + 1;
Jay Srinivasan08262882012-12-28 19:29:43 -0800309 if (next_url_index < GetNumUrls()) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800310 LOG(INFO) << "Incrementing the URL index for next attempt";
311 SetUrlIndex(next_url_index);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800312 } else {
313 LOG(INFO) << "Resetting the current URL index (" << GetUrlIndex() << ") to "
Jay Srinivasan08262882012-12-28 19:29:43 -0800314 << "0 as we only have " << GetNumUrls() << " URL(s)";
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800315 SetUrlIndex(0);
316 IncrementPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800317 }
Jay Srinivasan08262882012-12-28 19:29:43 -0800318
David Zeuthencc6f9962013-04-18 11:57:24 -0700319 // If we have multiple URLs, record that we just switched to another one
320 if (GetNumUrls() > 1)
321 SetUrlSwitchCount(url_switch_count_ + 1);
322
Jay Srinivasan08262882012-12-28 19:29:43 -0800323 // Whenever we update the URL index, we should also clear the URL failure
324 // count so we can start over fresh for the new URL.
325 SetUrlFailureCount(0);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800326}
327
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800328void PayloadState::IncrementFailureCount() {
329 uint32_t next_url_failure_count = GetUrlFailureCount() + 1;
Jay Srinivasan08262882012-12-28 19:29:43 -0800330 if (next_url_failure_count < response_.max_failure_count_per_url) {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800331 LOG(INFO) << "Incrementing the URL failure count";
332 SetUrlFailureCount(next_url_failure_count);
333 } else {
334 LOG(INFO) << "Reached max number of failures for Url" << GetUrlIndex()
335 << ". Trying next available URL";
336 IncrementUrlIndex();
337 }
338}
339
Jay Srinivasan08262882012-12-28 19:29:43 -0800340void PayloadState::UpdateBackoffExpiryTime() {
341 if (response_.disable_payload_backoff) {
342 LOG(INFO) << "Resetting backoff expiry time as payload backoff is disabled";
343 SetBackoffExpiryTime(Time());
344 return;
345 }
346
347 if (GetPayloadAttemptNumber() == 0) {
348 SetBackoffExpiryTime(Time());
349 return;
350 }
351
352 // Since we're doing left-shift below, make sure we don't shift more
353 // than this. E.g. if uint32_t is 4-bytes, don't left-shift more than 30 bits,
354 // since we don't expect value of kMaxBackoffDays to be more than 100 anyway.
355 uint32_t num_days = 1; // the value to be shifted.
356 const uint32_t kMaxShifts = (sizeof(num_days) * 8) - 2;
357
358 // Normal backoff days is 2 raised to (payload_attempt_number - 1).
359 // E.g. if payload_attempt_number is over 30, limit power to 30.
360 uint32_t power = min(GetPayloadAttemptNumber() - 1, kMaxShifts);
361
362 // The number of days is the minimum of 2 raised to (payload_attempt_number
363 // - 1) or kMaxBackoffDays.
364 num_days = min(num_days << power, kMaxBackoffDays);
365
366 // We don't want all retries to happen exactly at the same time when
367 // retrying after backoff. So add some random minutes to fuzz.
368 int fuzz_minutes = utils::FuzzInt(0, kMaxBackoffFuzzMinutes);
369 TimeDelta next_backoff_interval = TimeDelta::FromDays(num_days) +
370 TimeDelta::FromMinutes(fuzz_minutes);
371 LOG(INFO) << "Incrementing the backoff expiry time by "
372 << utils::FormatTimeDelta(next_backoff_interval);
373 SetBackoffExpiryTime(Time::Now() + next_backoff_interval);
374}
375
Jay Srinivasan19409b72013-04-12 19:23:36 -0700376void PayloadState::UpdateCurrentDownloadSource() {
377 current_download_source_ = kNumDownloadSources;
378
379 if (GetUrlIndex() < response_.payload_urls.size()) {
380 string current_url = response_.payload_urls[GetUrlIndex()];
381 if (StartsWithASCII(current_url, "https://", false))
382 current_download_source_ = kDownloadSourceHttpsServer;
383 else if (StartsWithASCII(current_url, "http://", false))
384 current_download_source_ = kDownloadSourceHttpServer;
385 }
386
387 LOG(INFO) << "Current download source: "
388 << utils::ToString(current_download_source_);
389}
390
391void PayloadState::UpdateBytesDownloaded(size_t count) {
392 SetCurrentBytesDownloaded(
393 current_download_source_,
394 GetCurrentBytesDownloaded(current_download_source_) + count,
395 false);
396 SetTotalBytesDownloaded(
397 current_download_source_,
398 GetTotalBytesDownloaded(current_download_source_) + count,
399 false);
400}
401
402void PayloadState::ReportBytesDownloadedMetrics() {
403 // Report metrics collected from all known download sources to UMA.
404 // The reported data is in Megabytes in order to represent a larger
405 // sample range.
406 for (int i = 0; i < kNumDownloadSources; i++) {
407 DownloadSource source = static_cast<DownloadSource>(i);
408 const int kMaxMiBs = 10240; // Anything above 10GB goes in the last bucket.
409
410 string metric = "Installer.SuccessfulMBsDownloadedFrom" +
411 utils::ToString(source);
412 uint64_t mbs = GetCurrentBytesDownloaded(source) / kNumBytesInOneMiB;
413 LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric;
414 system_state_->metrics_lib()->SendToUMA(metric,
415 mbs,
416 0, // min
417 kMaxMiBs,
418 kNumDefaultUmaBuckets);
419 SetCurrentBytesDownloaded(source, 0, true);
420
421 metric = "Installer.TotalMBsDownloadedFrom" + utils::ToString(source);
422 mbs = GetTotalBytesDownloaded(source) / kNumBytesInOneMiB;
423 LOG(INFO) << "Uploading " << mbs << " (MBs) for metric " << metric;
424 system_state_->metrics_lib()->SendToUMA(metric,
425 mbs,
426 0, // min
427 kMaxMiBs,
428 kNumDefaultUmaBuckets);
429
430 SetTotalBytesDownloaded(source, 0, true);
431 }
432}
433
David Zeuthencc6f9962013-04-18 11:57:24 -0700434void PayloadState::ReportUpdateUrlSwitchesMetric() {
435 string metric = "Installer.UpdateURLSwitches";
436 int value = static_cast<int>(url_switch_count_);
437
438 LOG(INFO) << "Uploading " << value << " (count) for metric " << metric;
439 system_state_->metrics_lib()->SendToUMA(
440 metric,
441 value,
442 0, // min value
443 100, // max value
444 kNumDefaultUmaBuckets);
445}
446
Chris Sosabe45bef2013-04-09 18:25:12 -0700447void PayloadState::ReportRebootMetrics() {
448 // Report the number of num_reboots.
449 string metric = "Installer.UpdateNumReboots";
450 uint32_t num_reboots = GetNumReboots();
451 LOG(INFO) << "Uploading reboot count of " << num_reboots << " for metric "
452 << metric;
453 system_state_->metrics_lib()->SendToUMA(
454 metric,
455 static_cast<int>(num_reboots), // sample
456 0, // min = 0.
457 50, // max
458 25); // buckets
459 SetNumReboots(0);
460}
461
462void PayloadState::UpdateNumReboots() {
463 // We only update the reboot count when the system has been detected to have
464 // been rebooted.
465 if (!system_state_->system_rebooted()) {
466 return;
467 }
468
469 SetNumReboots(GetNumReboots() + 1);
470}
471
472void PayloadState::SetNumReboots(uint32_t num_reboots) {
473 CHECK(prefs_);
474 num_reboots_ = num_reboots;
475 prefs_->SetInt64(kPrefsNumReboots, num_reboots);
476 LOG(INFO) << "Number of Reboots during current update attempt = "
477 << num_reboots_;
478}
479
Jay Srinivasan08262882012-12-28 19:29:43 -0800480void PayloadState::ResetPersistedState() {
481 SetPayloadAttemptNumber(0);
482 SetUrlIndex(0);
483 SetUrlFailureCount(0);
David Zeuthencc6f9962013-04-18 11:57:24 -0700484 SetUrlSwitchCount(0);
Jay Srinivasan08262882012-12-28 19:29:43 -0800485 UpdateBackoffExpiryTime(); // This will reset the backoff expiry time.
David Zeuthen9a017f22013-04-11 16:10:26 -0700486 SetUpdateTimestampStart(Time::Now());
487 SetUpdateTimestampEnd(Time()); // Set to null time
488 SetUpdateDurationUptime(TimeDelta::FromSeconds(0));
Jay Srinivasan19409b72013-04-12 19:23:36 -0700489 ResetDownloadSourcesOnNewUpdate();
490}
491
492void PayloadState::ResetDownloadSourcesOnNewUpdate() {
493 for (int i = 0; i < kNumDownloadSources; i++) {
494 DownloadSource source = static_cast<DownloadSource>(i);
495 SetCurrentBytesDownloaded(source, 0, true);
496 // Note: Not resetting the TotalBytesDownloaded as we want that metric
497 // to count the bytes downloaded across various update attempts until
498 // we have successfully applied the update.
499 }
500}
501
502int64_t PayloadState::GetPersistedValue(const string& key) {
503 CHECK(prefs_);
504 if (!prefs_->Exists(key))
505 return 0;
506
507 int64_t stored_value;
508 if (!prefs_->GetInt64(key, &stored_value))
509 return 0;
510
511 if (stored_value < 0) {
512 LOG(ERROR) << key << ": Invalid value (" << stored_value
513 << ") in persisted state. Defaulting to 0";
514 return 0;
515 }
516
517 return stored_value;
Jay Srinivasan08262882012-12-28 19:29:43 -0800518}
519
520string PayloadState::CalculateResponseSignature() {
521 string response_sign = StringPrintf("NumURLs = %d\n",
522 response_.payload_urls.size());
523
524 for (size_t i = 0; i < response_.payload_urls.size(); i++)
525 response_sign += StringPrintf("Url%d = %s\n",
526 i, response_.payload_urls[i].c_str());
527
528 response_sign += StringPrintf("Payload Size = %llu\n"
529 "Payload Sha256 Hash = %s\n"
530 "Metadata Size = %llu\n"
531 "Metadata Signature = %s\n"
532 "Is Delta Payload = %d\n"
533 "Max Failure Count Per Url = %d\n"
534 "Disable Payload Backoff = %d\n",
535 response_.size,
536 response_.hash.c_str(),
537 response_.metadata_size,
538 response_.metadata_signature.c_str(),
539 response_.is_delta_payload,
540 response_.max_failure_count_per_url,
541 response_.disable_payload_backoff);
542 return response_sign;
543}
544
545void PayloadState::LoadResponseSignature() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800546 CHECK(prefs_);
547 string stored_value;
Jay Srinivasan08262882012-12-28 19:29:43 -0800548 if (prefs_->Exists(kPrefsCurrentResponseSignature) &&
549 prefs_->GetString(kPrefsCurrentResponseSignature, &stored_value)) {
550 SetResponseSignature(stored_value);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800551 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800552}
553
Jay Srinivasan19409b72013-04-12 19:23:36 -0700554void PayloadState::SetResponseSignature(const string& response_signature) {
Jay Srinivasan08262882012-12-28 19:29:43 -0800555 CHECK(prefs_);
556 response_signature_ = response_signature;
557 LOG(INFO) << "Current Response Signature = \n" << response_signature_;
558 prefs_->SetString(kPrefsCurrentResponseSignature, response_signature_);
559}
560
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800561void PayloadState::LoadPayloadAttemptNumber() {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700562 SetPayloadAttemptNumber(GetPersistedValue(kPrefsPayloadAttemptNumber));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800563}
564
565void PayloadState::SetPayloadAttemptNumber(uint32_t payload_attempt_number) {
566 CHECK(prefs_);
567 payload_attempt_number_ = payload_attempt_number;
568 LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number_;
569 prefs_->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number_);
570}
571
572void PayloadState::LoadUrlIndex() {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700573 SetUrlIndex(GetPersistedValue(kPrefsCurrentUrlIndex));
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800574}
575
576void PayloadState::SetUrlIndex(uint32_t url_index) {
577 CHECK(prefs_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800578 url_index_ = url_index;
579 LOG(INFO) << "Current URL Index = " << url_index_;
580 prefs_->SetInt64(kPrefsCurrentUrlIndex, url_index_);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700581
582 // Also update the download source, which is purely dependent on the
583 // current URL index alone.
584 UpdateCurrentDownloadSource();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800585}
586
David Zeuthencc6f9962013-04-18 11:57:24 -0700587void PayloadState::LoadUrlSwitchCount() {
588 SetUrlSwitchCount(GetPersistedValue(kPrefsUrlSwitchCount));
589}
590
591void PayloadState::SetUrlSwitchCount(uint32_t url_switch_count) {
592 CHECK(prefs_);
593 url_switch_count_ = url_switch_count;
594 LOG(INFO) << "URL Switch Count = " << url_switch_count_;
595 prefs_->SetInt64(kPrefsUrlSwitchCount, url_switch_count_);
596}
597
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800598void PayloadState::LoadUrlFailureCount() {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700599 SetUrlFailureCount(GetPersistedValue(kPrefsCurrentUrlFailureCount));
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800600}
601
602void PayloadState::SetUrlFailureCount(uint32_t url_failure_count) {
603 CHECK(prefs_);
604 url_failure_count_ = url_failure_count;
605 LOG(INFO) << "Current URL (Url" << GetUrlIndex()
606 << ")'s Failure Count = " << url_failure_count_;
607 prefs_->SetInt64(kPrefsCurrentUrlFailureCount, url_failure_count_);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800608}
609
Jay Srinivasan08262882012-12-28 19:29:43 -0800610void PayloadState::LoadBackoffExpiryTime() {
611 CHECK(prefs_);
612 int64_t stored_value;
613 if (!prefs_->Exists(kPrefsBackoffExpiryTime))
614 return;
615
616 if (!prefs_->GetInt64(kPrefsBackoffExpiryTime, &stored_value))
617 return;
618
619 Time stored_time = Time::FromInternalValue(stored_value);
620 if (stored_time > Time::Now() + TimeDelta::FromDays(kMaxBackoffDays)) {
621 LOG(ERROR) << "Invalid backoff expiry time ("
622 << utils::ToString(stored_time)
623 << ") in persisted state. Resetting.";
624 stored_time = Time();
625 }
626 SetBackoffExpiryTime(stored_time);
627}
628
629void PayloadState::SetBackoffExpiryTime(const Time& new_time) {
630 CHECK(prefs_);
631 backoff_expiry_time_ = new_time;
632 LOG(INFO) << "Backoff Expiry Time = "
633 << utils::ToString(backoff_expiry_time_);
634 prefs_->SetInt64(kPrefsBackoffExpiryTime,
635 backoff_expiry_time_.ToInternalValue());
636}
637
David Zeuthen9a017f22013-04-11 16:10:26 -0700638TimeDelta PayloadState::GetUpdateDuration() {
639 Time end_time = update_timestamp_end_.is_null() ? Time::Now() :
640 update_timestamp_end_;
641 return end_time - update_timestamp_start_;
642}
643
644void PayloadState::LoadUpdateTimestampStart() {
645 int64_t stored_value;
646 Time stored_time;
647
648 CHECK(prefs_);
649
650 Time now = Time::Now();
651
652 if (!prefs_->Exists(kPrefsUpdateTimestampStart)) {
653 // The preference missing is not unexpected - in that case, just
654 // use the current time as start time
655 stored_time = now;
656 } else if (!prefs_->GetInt64(kPrefsUpdateTimestampStart, &stored_value)) {
657 LOG(ERROR) << "Invalid UpdateTimestampStart value. Resetting.";
658 stored_time = now;
659 } else {
660 stored_time = Time::FromInternalValue(stored_value);
661 }
662
663 // Sanity check: If the time read from disk is in the future
664 // (modulo some slack to account for possible NTP drift
665 // adjustments), something is fishy and we should report and
666 // reset.
667 TimeDelta duration_according_to_stored_time = now - stored_time;
668 if (duration_according_to_stored_time < -kDurationSlack) {
669 LOG(ERROR) << "The UpdateTimestampStart value ("
670 << utils::ToString(stored_time)
671 << ") in persisted state is "
David Zeuthen674c3182013-04-18 14:05:20 -0700672 << utils::FormatTimeDelta(duration_according_to_stored_time)
673 << " in the future. Resetting.";
David Zeuthen9a017f22013-04-11 16:10:26 -0700674 stored_time = now;
675 }
676
677 SetUpdateTimestampStart(stored_time);
678}
679
680void PayloadState::SetUpdateTimestampStart(const Time& value) {
681 CHECK(prefs_);
682 update_timestamp_start_ = value;
683 prefs_->SetInt64(kPrefsUpdateTimestampStart,
684 update_timestamp_start_.ToInternalValue());
685 LOG(INFO) << "Update Timestamp Start = "
686 << utils::ToString(update_timestamp_start_);
687}
688
689void PayloadState::SetUpdateTimestampEnd(const Time& value) {
690 update_timestamp_end_ = value;
691 LOG(INFO) << "Update Timestamp End = "
692 << utils::ToString(update_timestamp_end_);
693}
694
695TimeDelta PayloadState::GetUpdateDurationUptime() {
696 return update_duration_uptime_;
697}
698
699void PayloadState::LoadUpdateDurationUptime() {
700 int64_t stored_value;
701 TimeDelta stored_delta;
702
703 CHECK(prefs_);
704
705 if (!prefs_->Exists(kPrefsUpdateDurationUptime)) {
706 // The preference missing is not unexpected - in that case, just
707 // we'll use zero as the delta
708 } else if (!prefs_->GetInt64(kPrefsUpdateDurationUptime, &stored_value)) {
709 LOG(ERROR) << "Invalid UpdateDurationUptime value. Resetting.";
710 stored_delta = TimeDelta::FromSeconds(0);
711 } else {
712 stored_delta = TimeDelta::FromInternalValue(stored_value);
713 }
714
715 // Sanity-check: Uptime can never be greater than the wall-clock
716 // difference (modulo some slack). If it is, report and reset
717 // to the wall-clock difference.
718 TimeDelta diff = GetUpdateDuration() - stored_delta;
719 if (diff < -kDurationSlack) {
720 LOG(ERROR) << "The UpdateDurationUptime value ("
David Zeuthen674c3182013-04-18 14:05:20 -0700721 << utils::FormatTimeDelta(stored_delta)
David Zeuthen9a017f22013-04-11 16:10:26 -0700722 << ") in persisted state is "
David Zeuthen674c3182013-04-18 14:05:20 -0700723 << utils::FormatTimeDelta(diff)
724 << " larger than the wall-clock delta. Resetting.";
David Zeuthen9a017f22013-04-11 16:10:26 -0700725 stored_delta = update_duration_current_;
726 }
727
728 SetUpdateDurationUptime(stored_delta);
729}
730
Chris Sosabe45bef2013-04-09 18:25:12 -0700731void PayloadState::LoadNumReboots() {
732 SetNumReboots(GetPersistedValue(kPrefsNumReboots));
733}
734
David Zeuthen9a017f22013-04-11 16:10:26 -0700735void PayloadState::SetUpdateDurationUptimeExtended(const TimeDelta& value,
736 const Time& timestamp,
737 bool use_logging) {
738 CHECK(prefs_);
739 update_duration_uptime_ = value;
740 update_duration_uptime_timestamp_ = timestamp;
741 prefs_->SetInt64(kPrefsUpdateDurationUptime,
742 update_duration_uptime_.ToInternalValue());
743 if (use_logging) {
744 LOG(INFO) << "Update Duration Uptime = "
David Zeuthen674c3182013-04-18 14:05:20 -0700745 << utils::FormatTimeDelta(update_duration_uptime_);
David Zeuthen9a017f22013-04-11 16:10:26 -0700746 }
747}
748
749void PayloadState::SetUpdateDurationUptime(const TimeDelta& value) {
750 SetUpdateDurationUptimeExtended(value, utils::GetMonotonicTime(), true);
751}
752
753void PayloadState::CalculateUpdateDurationUptime() {
754 Time now = utils::GetMonotonicTime();
755 TimeDelta uptime_since_last_update = now - update_duration_uptime_timestamp_;
756 TimeDelta new_uptime = update_duration_uptime_ + uptime_since_last_update;
757 // We're frequently called so avoid logging this write
758 SetUpdateDurationUptimeExtended(new_uptime, now, false);
759}
760
David Zeuthen674c3182013-04-18 14:05:20 -0700761void PayloadState::ReportDurationMetrics() {
762 TimeDelta duration = GetUpdateDuration();
763 TimeDelta duration_uptime = GetUpdateDurationUptime();
764 string metric;
765
766 metric = "Installer.UpdateDurationMinutes";
767 system_state_->metrics_lib()->SendToUMA(
768 metric,
769 static_cast<int>(duration.InMinutes()),
770 1, // min: 1 minute
771 365*24*60, // max: 1 year (approx)
772 kNumDefaultUmaBuckets);
773 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration)
774 << " for metric " << metric;
775
776 metric = "Installer.UpdateDurationUptimeMinutes";
777 system_state_->metrics_lib()->SendToUMA(
778 metric,
779 static_cast<int>(duration_uptime.InMinutes()),
780 1, // min: 1 minute
781 30*24*60, // max: 1 month (approx)
782 kNumDefaultUmaBuckets);
783 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(duration_uptime)
784 << " for metric " << metric;
785
786 prefs_->Delete(kPrefsUpdateTimestampStart);
787 prefs_->Delete(kPrefsUpdateDurationUptime);
788}
789
Jay Srinivasan19409b72013-04-12 19:23:36 -0700790string PayloadState::GetPrefsKey(const string& prefix, DownloadSource source) {
791 return prefix + "-from-" + utils::ToString(source);
792}
793
794void PayloadState::LoadCurrentBytesDownloaded(DownloadSource source) {
795 string key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source);
796 SetCurrentBytesDownloaded(source, GetPersistedValue(key), true);
797}
798
799void PayloadState::SetCurrentBytesDownloaded(
800 DownloadSource source,
801 uint64_t current_bytes_downloaded,
802 bool log) {
803 CHECK(prefs_);
804
805 if (source >= kNumDownloadSources)
806 return;
807
808 // Update the in-memory value.
809 current_bytes_downloaded_[source] = current_bytes_downloaded;
810
811 string prefs_key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source);
812 prefs_->SetInt64(prefs_key, current_bytes_downloaded);
813 LOG_IF(INFO, log) << "Current bytes downloaded for "
814 << utils::ToString(source) << " = "
815 << GetCurrentBytesDownloaded(source);
816}
817
818void PayloadState::LoadTotalBytesDownloaded(DownloadSource source) {
819 string key = GetPrefsKey(kPrefsTotalBytesDownloaded, source);
820 SetTotalBytesDownloaded(source, GetPersistedValue(key), true);
821}
822
823void PayloadState::SetTotalBytesDownloaded(
824 DownloadSource source,
825 uint64_t total_bytes_downloaded,
826 bool log) {
827 CHECK(prefs_);
828
829 if (source >= kNumDownloadSources)
830 return;
831
832 // Update the in-memory value.
833 total_bytes_downloaded_[source] = total_bytes_downloaded;
834
835 // Persist.
836 string prefs_key = GetPrefsKey(kPrefsTotalBytesDownloaded, source);
837 prefs_->SetInt64(prefs_key, total_bytes_downloaded);
838 LOG_IF(INFO, log) << "Total bytes downloaded for "
839 << utils::ToString(source) << " = "
840 << GetTotalBytesDownloaded(source);
841}
842
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800843} // namespace chromeos_update_engine