1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/payload_state.h"
18
19 #include <algorithm>
20 #include <string>
21
22 #include <base/logging.h>
23 #include <base/strings/string_util.h>
24 #include <base/strings/stringprintf.h>
25 #include <metrics/metrics_library.h>
26 #include <policy/device_policy.h>
27
28 #include "update_engine/common/clock.h"
29 #include "update_engine/common/constants.h"
30 #include "update_engine/common/error_code_utils.h"
31 #include "update_engine/common/hardware_interface.h"
32 #include "update_engine/common/prefs.h"
33 #include "update_engine/common/utils.h"
34 #include "update_engine/connection_manager_interface.h"
35 #include "update_engine/metrics_utils.h"
36 #include "update_engine/omaha_request_params.h"
37 #include "update_engine/payload_consumer/install_plan.h"
38 #include "update_engine/system_state.h"
39
40 using base::Time;
41 using base::TimeDelta;
42 using std::min;
43 using std::string;
44
45 namespace chromeos_update_engine {
46
47 const TimeDelta PayloadState::kDurationSlack = TimeDelta::FromSeconds(600);
48
49 // We want to upperbound backoffs to 16 days
50 static const int kMaxBackoffDays = 16;
51
52 // We want to randomize retry attempts after the backoff by +/- 6 hours.
53 static const uint32_t kMaxBackoffFuzzMinutes = 12 * 60;
54
PayloadState()55 PayloadState::PayloadState()
56 : prefs_(nullptr),
57 using_p2p_for_downloading_(false),
58 p2p_num_attempts_(0),
59 payload_attempt_number_(0),
60 full_payload_attempt_number_(0),
61 url_index_(0),
62 url_failure_count_(0),
63 url_switch_count_(0),
64 attempt_num_bytes_downloaded_(0),
65 attempt_connection_type_(metrics::ConnectionType::kUnknown),
66 attempt_error_code_(ErrorCode::kSuccess),
67 attempt_type_(AttemptType::kUpdate) {
68 for (int i = 0; i <= kNumDownloadSources; i++)
69 total_bytes_downloaded_[i] = current_bytes_downloaded_[i] = 0;
70 }
71
Initialize(SystemState * system_state)72 bool PayloadState::Initialize(SystemState* system_state) {
73 system_state_ = system_state;
74 prefs_ = system_state_->prefs();
75 powerwash_safe_prefs_ = system_state_->powerwash_safe_prefs();
76 LoadResponseSignature();
77 LoadPayloadAttemptNumber();
78 LoadFullPayloadAttemptNumber();
79 LoadUrlIndex();
80 LoadUrlFailureCount();
81 LoadUrlSwitchCount();
82 LoadBackoffExpiryTime();
83 LoadUpdateTimestampStart();
84 // The LoadUpdateDurationUptime() method relies on LoadUpdateTimestampStart()
85 // being called before it. Don't reorder.
86 LoadUpdateDurationUptime();
87 for (int i = 0; i < kNumDownloadSources; i++) {
88 DownloadSource source = static_cast<DownloadSource>(i);
89 LoadCurrentBytesDownloaded(source);
90 LoadTotalBytesDownloaded(source);
91 }
92 LoadNumReboots();
93 LoadNumResponsesSeen();
94 LoadRollbackVersion();
95 LoadP2PFirstAttemptTimestamp();
96 LoadP2PNumAttempts();
97 return true;
98 }
99
SetResponse(const OmahaResponse & omaha_response)100 void PayloadState::SetResponse(const OmahaResponse& omaha_response) {
101 // Always store the latest response.
102 response_ = omaha_response;
103
104 // Compute the candidate URLs first as they are used to calculate the
105 // response signature so that a change in enterprise policy for
106 // HTTP downloads being enabled or not could be honored as soon as the
107 // next update check happens.
108 ComputeCandidateUrls();
109
110 // Check if the "signature" of this response (i.e. the fields we care about)
111 // has changed.
112 string new_response_signature = CalculateResponseSignature();
113 bool has_response_changed = (response_signature_ != new_response_signature);
114
115 // If the response has changed, we should persist the new signature and
116 // clear away all the existing state.
117 if (has_response_changed) {
118 LOG(INFO) << "Resetting all persisted state as this is a new response";
119 SetNumResponsesSeen(num_responses_seen_ + 1);
120 SetResponseSignature(new_response_signature);
121 ResetPersistedState();
122 return;
123 }
124
125 // Always start from payload index 0, even for resume, to download partition
126 // info from previous payloads.
127 payload_index_ = 0;
128
129 // This is the earliest point at which we can validate whether the URL index
130 // we loaded from the persisted state is a valid value. If the response
131 // hasn't changed but the URL index is invalid, it's indicative of some
132 // tampering of the persisted state.
133 if (payload_index_ >= candidate_urls_.size() ||
134 url_index_ >= candidate_urls_[payload_index_].size()) {
135 LOG(INFO) << "Resetting all payload state as the url index seems to have "
136 "been tampered with";
137 ResetPersistedState();
138 return;
139 }
140
141 // Update the current download source which depends on the latest value of
142 // the response.
143 UpdateCurrentDownloadSource();
144 }
145
SetUsingP2PForDownloading(bool value)146 void PayloadState::SetUsingP2PForDownloading(bool value) {
147 using_p2p_for_downloading_ = value;
148 // Update the current download source which depends on whether we are
149 // using p2p or not.
150 UpdateCurrentDownloadSource();
151 }
152
DownloadComplete()153 void PayloadState::DownloadComplete() {
154 LOG(INFO) << "Payload downloaded successfully";
155 IncrementPayloadAttemptNumber();
156 IncrementFullPayloadAttemptNumber();
157 }
158
DownloadProgress(size_t count)159 void PayloadState::DownloadProgress(size_t count) {
160 if (count == 0)
161 return;
162
163 CalculateUpdateDurationUptime();
164 UpdateBytesDownloaded(count);
165
166 // We've received non-zero bytes from a recent download operation. Since our
167 // URL failure count is meant to penalize a URL only for consecutive
168 // failures, downloading bytes successfully means we should reset the failure
169 // count (as we know at least that the URL is working). In future, we can
170 // design this to be more sophisticated to check for more intelligent failure
171 // patterns, but right now, even 1 byte downloaded will mark the URL to be
172 // good unless it hits 10 (or configured number of) consecutive failures
173 // again.
174
175 if (GetUrlFailureCount() == 0)
176 return;
177
178 LOG(INFO) << "Resetting failure count of Url" << GetUrlIndex()
179 << " to 0 as we received " << count << " bytes successfully";
180 SetUrlFailureCount(0);
181 }
182
AttemptStarted(AttemptType attempt_type)183 void PayloadState::AttemptStarted(AttemptType attempt_type) {
184 // Flush previous state from abnormal attempt failure, if any.
185 ReportAndClearPersistedAttemptMetrics();
186
187 attempt_type_ = attempt_type;
188
189 ClockInterface *clock = system_state_->clock();
190 attempt_start_time_boot_ = clock->GetBootTime();
191 attempt_start_time_monotonic_ = clock->GetMonotonicTime();
192 attempt_num_bytes_downloaded_ = 0;
193
194 metrics::ConnectionType type;
195 ConnectionType network_connection_type;
196 ConnectionTethering tethering;
197 ConnectionManagerInterface* connection_manager =
198 system_state_->connection_manager();
199 if (!connection_manager->GetConnectionProperties(&network_connection_type,
200 &tethering)) {
201 LOG(ERROR) << "Failed to determine connection type.";
202 type = metrics::ConnectionType::kUnknown;
203 } else {
204 type = metrics_utils::GetConnectionType(network_connection_type, tethering);
205 }
206 attempt_connection_type_ = type;
207
208 if (attempt_type == AttemptType::kUpdate)
209 PersistAttemptMetrics();
210 }
211
UpdateResumed()212 void PayloadState::UpdateResumed() {
213 LOG(INFO) << "Resuming an update that was previously started.";
214 UpdateNumReboots();
215 AttemptStarted(AttemptType::kUpdate);
216 }
217
UpdateRestarted()218 void PayloadState::UpdateRestarted() {
219 LOG(INFO) << "Starting a new update";
220 ResetDownloadSourcesOnNewUpdate();
221 SetNumReboots(0);
222 AttemptStarted(AttemptType::kUpdate);
223 }
224
UpdateSucceeded()225 void PayloadState::UpdateSucceeded() {
226 // Send the relevant metrics that are tracked in this class to UMA.
227 CalculateUpdateDurationUptime();
228 SetUpdateTimestampEnd(system_state_->clock()->GetWallclockTime());
229
230 switch (attempt_type_) {
231 case AttemptType::kUpdate:
232 CollectAndReportAttemptMetrics(ErrorCode::kSuccess);
233 CollectAndReportSuccessfulUpdateMetrics();
234 ClearPersistedAttemptMetrics();
235 break;
236
237 case AttemptType::kRollback:
238 metrics::ReportRollbackMetrics(system_state_,
239 metrics::RollbackResult::kSuccess);
240 break;
241 }
242 attempt_error_code_ = ErrorCode::kSuccess;
243
244 // Reset the number of responses seen since it counts from the last
245 // successful update, e.g. now.
246 SetNumResponsesSeen(0);
247 SetPayloadIndex(0);
248
249 CreateSystemUpdatedMarkerFile();
250 }
251
UpdateFailed(ErrorCode error)252 void PayloadState::UpdateFailed(ErrorCode error) {
253 ErrorCode base_error = utils::GetBaseErrorCode(error);
254 LOG(INFO) << "Updating payload state for error code: " << base_error
255 << " (" << utils::ErrorCodeToString(base_error) << ")";
256 attempt_error_code_ = base_error;
257
258 if (candidate_urls_.size() == 0) {
259 // This means we got this error even before we got a valid Omaha response
260 // or don't have any valid candidates in the Omaha response.
261 // So we should not advance the url_index_ in such cases.
262 LOG(INFO) << "Ignoring failures until we get a valid Omaha response.";
263 return;
264 }
265
266 switch (attempt_type_) {
267 case AttemptType::kUpdate:
268 CollectAndReportAttemptMetrics(base_error);
269 ClearPersistedAttemptMetrics();
270 break;
271
272 case AttemptType::kRollback:
273 metrics::ReportRollbackMetrics(system_state_,
274 metrics::RollbackResult::kFailed);
275 break;
276 }
277
278
279 switch (base_error) {
280 // Errors which are good indicators of a problem with a particular URL or
281 // the protocol used in the URL or entities in the communication channel
282 // (e.g. proxies). We should try the next available URL in the next update
283 // check to quickly recover from these errors.
284 case ErrorCode::kPayloadHashMismatchError:
285 case ErrorCode::kPayloadSizeMismatchError:
286 case ErrorCode::kDownloadPayloadVerificationError:
287 case ErrorCode::kDownloadPayloadPubKeyVerificationError:
288 case ErrorCode::kSignedDeltaPayloadExpectedError:
289 case ErrorCode::kDownloadInvalidMetadataMagicString:
290 case ErrorCode::kDownloadSignatureMissingInManifest:
291 case ErrorCode::kDownloadManifestParseError:
292 case ErrorCode::kDownloadMetadataSignatureError:
293 case ErrorCode::kDownloadMetadataSignatureVerificationError:
294 case ErrorCode::kDownloadMetadataSignatureMismatch:
295 case ErrorCode::kDownloadOperationHashVerificationError:
296 case ErrorCode::kDownloadOperationExecutionError:
297 case ErrorCode::kDownloadOperationHashMismatch:
298 case ErrorCode::kDownloadInvalidMetadataSize:
299 case ErrorCode::kDownloadInvalidMetadataSignature:
300 case ErrorCode::kDownloadOperationHashMissingError:
301 case ErrorCode::kDownloadMetadataSignatureMissingError:
302 case ErrorCode::kPayloadMismatchedType:
303 case ErrorCode::kUnsupportedMajorPayloadVersion:
304 case ErrorCode::kUnsupportedMinorPayloadVersion:
305 case ErrorCode::kPayloadTimestampError:
306 IncrementUrlIndex();
307 break;
308
309 // Errors which seem to be just transient network/communication related
310 // failures and do not indicate any inherent problem with the URL itself.
311 // So, we should keep the current URL but just increment the
312 // failure count to give it more chances. This way, while we maximize our
313 // chances of downloading from the URLs that appear earlier in the response
314 // (because download from a local server URL that appears earlier in a
315 // response is preferable than downloading from the next URL which could be
316 // a internet URL and thus could be more expensive).
317
318 case ErrorCode::kError:
319 case ErrorCode::kDownloadTransferError:
320 case ErrorCode::kDownloadWriteError:
321 case ErrorCode::kDownloadStateInitializationError:
322 case ErrorCode::kOmahaErrorInHTTPResponse: // Aggregate for HTTP errors.
323 IncrementFailureCount();
324 break;
325
326 // Errors which are not specific to a URL and hence shouldn't result in
327 // the URL being penalized. This can happen in two cases:
328 // 1. We haven't started downloading anything: These errors don't cost us
329 // anything in terms of actual payload bytes, so we should just do the
330 // regular retries at the next update check.
331 // 2. We have successfully downloaded the payload: In this case, the
332 // payload attempt number would have been incremented and would take care
333 // of the backoff at the next update check.
334 // In either case, there's no need to update URL index or failure count.
335 case ErrorCode::kOmahaRequestError:
336 case ErrorCode::kOmahaResponseHandlerError:
337 case ErrorCode::kPostinstallRunnerError:
338 case ErrorCode::kFilesystemCopierError:
339 case ErrorCode::kInstallDeviceOpenError:
340 case ErrorCode::kKernelDeviceOpenError:
341 case ErrorCode::kDownloadNewPartitionInfoError:
342 case ErrorCode::kNewRootfsVerificationError:
343 case ErrorCode::kNewKernelVerificationError:
344 case ErrorCode::kPostinstallBootedFromFirmwareB:
345 case ErrorCode::kPostinstallFirmwareRONotUpdatable:
346 case ErrorCode::kOmahaRequestEmptyResponseError:
347 case ErrorCode::kOmahaRequestXMLParseError:
348 case ErrorCode::kOmahaResponseInvalid:
349 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
350 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
351 case ErrorCode::kNonCriticalUpdateInOOBE:
352 case ErrorCode::kOmahaUpdateDeferredForBackoff:
353 case ErrorCode::kPostinstallPowerwashError:
354 case ErrorCode::kUpdateCanceledByChannelChange:
355 case ErrorCode::kOmahaRequestXMLHasEntityDecl:
356 case ErrorCode::kFilesystemVerifierError:
357 case ErrorCode::kUserCanceled:
358 LOG(INFO) << "Not incrementing URL index or failure count for this error";
359 break;
360
361 case ErrorCode::kSuccess: // success code
362 case ErrorCode::kUmaReportedMax: // not an error code
363 case ErrorCode::kOmahaRequestHTTPResponseBase: // aggregated already
364 case ErrorCode::kDevModeFlag: // not an error code
365 case ErrorCode::kResumedFlag: // not an error code
366 case ErrorCode::kTestImageFlag: // not an error code
367 case ErrorCode::kTestOmahaUrlFlag: // not an error code
368 case ErrorCode::kSpecialFlags: // not an error code
369 // These shouldn't happen. Enumerating these explicitly here so that we
370 // can let the compiler warn about new error codes that are added to
371 // action_processor.h but not added here.
372 LOG(WARNING) << "Unexpected error code for UpdateFailed";
373 break;
374
375 // Note: Not adding a default here so as to let the compiler warn us of
376 // any new enums that were added in the .h but not listed in this switch.
377 }
378 }
379
ShouldBackoffDownload()380 bool PayloadState::ShouldBackoffDownload() {
381 if (response_.disable_payload_backoff) {
382 LOG(INFO) << "Payload backoff logic is disabled. "
383 "Can proceed with the download";
384 return false;
385 }
386 if (GetUsingP2PForDownloading() && !GetP2PUrl().empty()) {
387 LOG(INFO) << "Payload backoff logic is disabled because download "
388 << "will happen from local peer (via p2p).";
389 return false;
390 }
391 if (system_state_->request_params()->interactive()) {
392 LOG(INFO) << "Payload backoff disabled for interactive update checks.";
393 return false;
394 }
395 for (const auto& package : response_.packages) {
396 if (package.is_delta) {
397 // If delta payloads fail, we want to fallback quickly to full payloads as
398 // they are more likely to succeed. Exponential backoffs would greatly
399 // slow down the fallback to full payloads. So we don't backoff for delta
400 // payloads.
401 LOG(INFO) << "No backoffs for delta payloads. "
402 << "Can proceed with the download";
403 return false;
404 }
405 }
406
407 if (!system_state_->hardware()->IsOfficialBuild()) {
408 // Backoffs are needed only for official builds. We do not want any delays
409 // or update failures due to backoffs during testing or development.
410 LOG(INFO) << "No backoffs for test/dev images. "
411 << "Can proceed with the download";
412 return false;
413 }
414
415 if (backoff_expiry_time_.is_null()) {
416 LOG(INFO) << "No backoff expiry time has been set. "
417 << "Can proceed with the download";
418 return false;
419 }
420
421 if (backoff_expiry_time_ < Time::Now()) {
422 LOG(INFO) << "The backoff expiry time ("
423 << utils::ToString(backoff_expiry_time_)
424 << ") has elapsed. Can proceed with the download";
425 return false;
426 }
427
428 LOG(INFO) << "Cannot proceed with downloads as we need to backoff until "
429 << utils::ToString(backoff_expiry_time_);
430 return true;
431 }
432
Rollback()433 void PayloadState::Rollback() {
434 SetRollbackVersion(system_state_->request_params()->app_version());
435 AttemptStarted(AttemptType::kRollback);
436 }
437
IncrementPayloadAttemptNumber()438 void PayloadState::IncrementPayloadAttemptNumber() {
439 // Update the payload attempt number for both payload types: full and delta.
440 SetPayloadAttemptNumber(GetPayloadAttemptNumber() + 1);
441 }
442
IncrementFullPayloadAttemptNumber()443 void PayloadState::IncrementFullPayloadAttemptNumber() {
444 // Update the payload attempt number for full payloads and the backoff time.
445 if (response_.packages[payload_index_].is_delta) {
446 LOG(INFO) << "Not incrementing payload attempt number for delta payloads";
447 return;
448 }
449
450 LOG(INFO) << "Incrementing the full payload attempt number";
451 SetFullPayloadAttemptNumber(GetFullPayloadAttemptNumber() + 1);
452 UpdateBackoffExpiryTime();
453 }
454
IncrementUrlIndex()455 void PayloadState::IncrementUrlIndex() {
456 size_t next_url_index = url_index_ + 1;
457 size_t max_url_size = 0;
458 for (const auto& urls : candidate_urls_)
459 max_url_size = std::max(max_url_size, urls.size());
460 if (next_url_index < max_url_size) {
461 LOG(INFO) << "Incrementing the URL index for next attempt";
462 SetUrlIndex(next_url_index);
463 } else {
464 LOG(INFO) << "Resetting the current URL index (" << url_index_ << ") to "
465 << "0 as we only have " << max_url_size << " candidate URL(s)";
466 SetUrlIndex(0);
467 IncrementPayloadAttemptNumber();
468 IncrementFullPayloadAttemptNumber();
469 }
470
471 // If we have multiple URLs, record that we just switched to another one
472 if (max_url_size > 1)
473 SetUrlSwitchCount(url_switch_count_ + 1);
474
475 // Whenever we update the URL index, we should also clear the URL failure
476 // count so we can start over fresh for the new URL.
477 SetUrlFailureCount(0);
478 }
479
IncrementFailureCount()480 void PayloadState::IncrementFailureCount() {
481 uint32_t next_url_failure_count = GetUrlFailureCount() + 1;
482 if (next_url_failure_count < response_.max_failure_count_per_url) {
483 LOG(INFO) << "Incrementing the URL failure count";
484 SetUrlFailureCount(next_url_failure_count);
485 } else {
486 LOG(INFO) << "Reached max number of failures for Url" << GetUrlIndex()
487 << ". Trying next available URL";
488 IncrementUrlIndex();
489 }
490 }
491
UpdateBackoffExpiryTime()492 void PayloadState::UpdateBackoffExpiryTime() {
493 if (response_.disable_payload_backoff) {
494 LOG(INFO) << "Resetting backoff expiry time as payload backoff is disabled";
495 SetBackoffExpiryTime(Time());
496 return;
497 }
498
499 if (GetFullPayloadAttemptNumber() == 0) {
500 SetBackoffExpiryTime(Time());
501 return;
502 }
503
504 // Since we're doing left-shift below, make sure we don't shift more
505 // than this. E.g. if int is 4-bytes, don't left-shift more than 30 bits,
506 // since we don't expect value of kMaxBackoffDays to be more than 100 anyway.
507 int num_days = 1; // the value to be shifted.
508 const int kMaxShifts = (sizeof(num_days) * 8) - 2;
509
510 // Normal backoff days is 2 raised to (payload_attempt_number - 1).
511 // E.g. if payload_attempt_number is over 30, limit power to 30.
512 int power = min(GetFullPayloadAttemptNumber() - 1, kMaxShifts);
513
514 // The number of days is the minimum of 2 raised to (payload_attempt_number
515 // - 1) or kMaxBackoffDays.
516 num_days = min(num_days << power, kMaxBackoffDays);
517
518 // We don't want all retries to happen exactly at the same time when
519 // retrying after backoff. So add some random minutes to fuzz.
520 int fuzz_minutes = utils::FuzzInt(0, kMaxBackoffFuzzMinutes);
521 TimeDelta next_backoff_interval = TimeDelta::FromDays(num_days) +
522 TimeDelta::FromMinutes(fuzz_minutes);
523 LOG(INFO) << "Incrementing the backoff expiry time by "
524 << utils::FormatTimeDelta(next_backoff_interval);
525 SetBackoffExpiryTime(Time::Now() + next_backoff_interval);
526 }
527
UpdateCurrentDownloadSource()528 void PayloadState::UpdateCurrentDownloadSource() {
529 current_download_source_ = kNumDownloadSources;
530
531 if (using_p2p_for_downloading_) {
532 current_download_source_ = kDownloadSourceHttpPeer;
533 } else if (payload_index_ < candidate_urls_.size() &&
534 candidate_urls_[payload_index_].size() != 0) {
535 const string& current_url = candidate_urls_[payload_index_][GetUrlIndex()];
536 if (base::StartsWith(
537 current_url, "https://", base::CompareCase::INSENSITIVE_ASCII)) {
538 current_download_source_ = kDownloadSourceHttpsServer;
539 } else if (base::StartsWith(current_url,
540 "http://",
541 base::CompareCase::INSENSITIVE_ASCII)) {
542 current_download_source_ = kDownloadSourceHttpServer;
543 }
544 }
545
546 LOG(INFO) << "Current download source: "
547 << utils::ToString(current_download_source_);
548 }
549
UpdateBytesDownloaded(size_t count)550 void PayloadState::UpdateBytesDownloaded(size_t count) {
551 SetCurrentBytesDownloaded(
552 current_download_source_,
553 GetCurrentBytesDownloaded(current_download_source_) + count,
554 false);
555 SetTotalBytesDownloaded(
556 current_download_source_,
557 GetTotalBytesDownloaded(current_download_source_) + count,
558 false);
559
560 attempt_num_bytes_downloaded_ += count;
561 }
562
CalculatePayloadType()563 PayloadType PayloadState::CalculatePayloadType() {
564 for (const auto& package : response_.packages) {
565 if (package.is_delta) {
566 return kPayloadTypeDelta;
567 }
568 }
569 OmahaRequestParams* params = system_state_->request_params();
570 if (params->delta_okay()) {
571 return kPayloadTypeFull;
572 }
573 // Full payload, delta was not allowed by request.
574 return kPayloadTypeForcedFull;
575 }
576
577 // TODO(zeuthen): Currently we don't report the UpdateEngine.Attempt.*
578 // metrics if the attempt ends abnormally, e.g. if the update_engine
579 // process crashes or the device is rebooted. See
580 // http://crbug.com/357676
CollectAndReportAttemptMetrics(ErrorCode code)581 void PayloadState::CollectAndReportAttemptMetrics(ErrorCode code) {
582 int attempt_number = GetPayloadAttemptNumber();
583
584 PayloadType payload_type = CalculatePayloadType();
585
586 int64_t payload_size = GetPayloadSize();
587
588 int64_t payload_bytes_downloaded = attempt_num_bytes_downloaded_;
589
590 ClockInterface *clock = system_state_->clock();
591 TimeDelta duration = clock->GetBootTime() - attempt_start_time_boot_;
592 TimeDelta duration_uptime = clock->GetMonotonicTime() -
593 attempt_start_time_monotonic_;
594
595 int64_t payload_download_speed_bps = 0;
596 int64_t usec = duration_uptime.InMicroseconds();
597 if (usec > 0) {
598 double sec = static_cast<double>(usec) / Time::kMicrosecondsPerSecond;
599 double bps = static_cast<double>(payload_bytes_downloaded) / sec;
600 payload_download_speed_bps = static_cast<int64_t>(bps);
601 }
602
603 DownloadSource download_source = current_download_source_;
604
605 metrics::DownloadErrorCode payload_download_error_code =
606 metrics::DownloadErrorCode::kUnset;
607 ErrorCode internal_error_code = ErrorCode::kSuccess;
608 metrics::AttemptResult attempt_result = metrics_utils::GetAttemptResult(code);
609
610 // Add additional detail to AttemptResult
611 switch (attempt_result) {
612 case metrics::AttemptResult::kPayloadDownloadError:
613 payload_download_error_code = metrics_utils::GetDownloadErrorCode(code);
614 break;
615
616 case metrics::AttemptResult::kInternalError:
617 internal_error_code = code;
618 break;
619
620 // Explicit fall-through for cases where we do not have additional
621 // detail. We avoid the default keyword to force people adding new
622 // AttemptResult values to visit this code and examine whether
623 // additional detail is needed.
624 case metrics::AttemptResult::kUpdateSucceeded:
625 case metrics::AttemptResult::kMetadataMalformed:
626 case metrics::AttemptResult::kOperationMalformed:
627 case metrics::AttemptResult::kOperationExecutionError:
628 case metrics::AttemptResult::kMetadataVerificationFailed:
629 case metrics::AttemptResult::kPayloadVerificationFailed:
630 case metrics::AttemptResult::kVerificationFailed:
631 case metrics::AttemptResult::kPostInstallFailed:
632 case metrics::AttemptResult::kAbnormalTermination:
633 case metrics::AttemptResult::kUpdateCanceled:
634 case metrics::AttemptResult::kNumConstants:
635 case metrics::AttemptResult::kUnset:
636 break;
637 }
638
639 metrics::ReportUpdateAttemptMetrics(system_state_,
640 attempt_number,
641 payload_type,
642 duration,
643 duration_uptime,
644 payload_size,
645 payload_bytes_downloaded,
646 payload_download_speed_bps,
647 download_source,
648 attempt_result,
649 internal_error_code,
650 payload_download_error_code,
651 attempt_connection_type_);
652 }
653
PersistAttemptMetrics()654 void PayloadState::PersistAttemptMetrics() {
655 // TODO(zeuthen): For now we only persist whether an attempt was in
656 // progress and not values/metrics related to the attempt. This
657 // means that when this happens, of all the UpdateEngine.Attempt.*
658 // metrics, only UpdateEngine.Attempt.Result is reported (with the
659 // value |kAbnormalTermination|). In the future we might want to
660 // persist more data so we can report other metrics in the
661 // UpdateEngine.Attempt.* namespace when this happens.
662 prefs_->SetBoolean(kPrefsAttemptInProgress, true);
663 }
664
ClearPersistedAttemptMetrics()665 void PayloadState::ClearPersistedAttemptMetrics() {
666 prefs_->Delete(kPrefsAttemptInProgress);
667 }
668
ReportAndClearPersistedAttemptMetrics()669 void PayloadState::ReportAndClearPersistedAttemptMetrics() {
670 bool attempt_in_progress = false;
671 if (!prefs_->GetBoolean(kPrefsAttemptInProgress, &attempt_in_progress))
672 return;
673 if (!attempt_in_progress)
674 return;
675
676 metrics::ReportAbnormallyTerminatedUpdateAttemptMetrics(system_state_);
677
678 ClearPersistedAttemptMetrics();
679 }
680
CollectAndReportSuccessfulUpdateMetrics()681 void PayloadState::CollectAndReportSuccessfulUpdateMetrics() {
682 string metric;
683
684 // Report metrics collected from all known download sources to UMA.
685 int64_t total_bytes_by_source[kNumDownloadSources];
686 int64_t successful_bytes = 0;
687 int64_t total_bytes = 0;
688 int64_t successful_mbs = 0;
689 int64_t total_mbs = 0;
690
691 for (int i = 0; i < kNumDownloadSources; i++) {
692 DownloadSource source = static_cast<DownloadSource>(i);
693 int64_t bytes;
694
695 // Only consider this download source (and send byte counts) as
696 // having been used if we downloaded a non-trivial amount of bytes
697 // (e.g. at least 1 MiB) that contributed to the final success of
698 // the update. Otherwise we're going to end up with a lot of
699 // zero-byte events in the histogram.
700
701 bytes = GetCurrentBytesDownloaded(source);
702 successful_bytes += bytes;
703 successful_mbs += bytes / kNumBytesInOneMiB;
704 SetCurrentBytesDownloaded(source, 0, true);
705
706 bytes = GetTotalBytesDownloaded(source);
707 total_bytes_by_source[i] = bytes;
708 total_bytes += bytes;
709 total_mbs += bytes / kNumBytesInOneMiB;
710 SetTotalBytesDownloaded(source, 0, true);
711 }
712
713 int download_overhead_percentage = 0;
714 if (successful_bytes > 0) {
715 download_overhead_percentage = (total_bytes - successful_bytes) * 100ULL /
716 successful_bytes;
717 }
718
719 int url_switch_count = static_cast<int>(url_switch_count_);
720
721 int reboot_count = GetNumReboots();
722
723 SetNumReboots(0);
724
725 TimeDelta duration = GetUpdateDuration();
726
727 prefs_->Delete(kPrefsUpdateTimestampStart);
728 prefs_->Delete(kPrefsUpdateDurationUptime);
729
730 PayloadType payload_type = CalculatePayloadType();
731
732 int64_t payload_size = GetPayloadSize();
733
734 int attempt_count = GetPayloadAttemptNumber();
735
736 int updates_abandoned_count = num_responses_seen_ - 1;
737
738 metrics::ReportSuccessfulUpdateMetrics(system_state_,
739 attempt_count,
740 updates_abandoned_count,
741 payload_type,
742 payload_size,
743 total_bytes_by_source,
744 download_overhead_percentage,
745 duration,
746 reboot_count,
747 url_switch_count);
748 }
749
UpdateNumReboots()750 void PayloadState::UpdateNumReboots() {
751 // We only update the reboot count when the system has been detected to have
752 // been rebooted.
753 if (!system_state_->system_rebooted()) {
754 return;
755 }
756
757 SetNumReboots(GetNumReboots() + 1);
758 }
759
SetNumReboots(uint32_t num_reboots)760 void PayloadState::SetNumReboots(uint32_t num_reboots) {
761 CHECK(prefs_);
762 num_reboots_ = num_reboots;
763 prefs_->SetInt64(kPrefsNumReboots, num_reboots);
764 LOG(INFO) << "Number of Reboots during current update attempt = "
765 << num_reboots_;
766 }
767
ResetPersistedState()768 void PayloadState::ResetPersistedState() {
769 SetPayloadAttemptNumber(0);
770 SetFullPayloadAttemptNumber(0);
771 SetPayloadIndex(0);
772 SetUrlIndex(0);
773 SetUrlFailureCount(0);
774 SetUrlSwitchCount(0);
775 UpdateBackoffExpiryTime(); // This will reset the backoff expiry time.
776 SetUpdateTimestampStart(system_state_->clock()->GetWallclockTime());
777 SetUpdateTimestampEnd(Time()); // Set to null time
778 SetUpdateDurationUptime(TimeDelta::FromSeconds(0));
779 ResetDownloadSourcesOnNewUpdate();
780 ResetRollbackVersion();
781 SetP2PNumAttempts(0);
782 SetP2PFirstAttemptTimestamp(Time()); // Set to null time
783 SetScatteringWaitPeriod(TimeDelta());
784 }
785
ResetRollbackVersion()786 void PayloadState::ResetRollbackVersion() {
787 CHECK(powerwash_safe_prefs_);
788 rollback_version_ = "";
789 powerwash_safe_prefs_->Delete(kPrefsRollbackVersion);
790 }
791
ResetDownloadSourcesOnNewUpdate()792 void PayloadState::ResetDownloadSourcesOnNewUpdate() {
793 for (int i = 0; i < kNumDownloadSources; i++) {
794 DownloadSource source = static_cast<DownloadSource>(i);
795 SetCurrentBytesDownloaded(source, 0, true);
796 // Note: Not resetting the TotalBytesDownloaded as we want that metric
797 // to count the bytes downloaded across various update attempts until
798 // we have successfully applied the update.
799 }
800 }
801
GetPersistedValue(const string & key)802 int64_t PayloadState::GetPersistedValue(const string& key) {
803 CHECK(prefs_);
804 if (!prefs_->Exists(key))
805 return 0;
806
807 int64_t stored_value;
808 if (!prefs_->GetInt64(key, &stored_value))
809 return 0;
810
811 if (stored_value < 0) {
812 LOG(ERROR) << key << ": Invalid value (" << stored_value
813 << ") in persisted state. Defaulting to 0";
814 return 0;
815 }
816
817 return stored_value;
818 }
819
CalculateResponseSignature()820 string PayloadState::CalculateResponseSignature() {
821 string response_sign;
822 for (size_t i = 0; i < response_.packages.size(); i++) {
823 const auto& package = response_.packages[i];
824 response_sign += base::StringPrintf(
825 "Payload %zu:\n"
826 " Size = %ju\n"
827 " Sha256 Hash = %s\n"
828 " Metadata Size = %ju\n"
829 " Metadata Signature = %s\n"
830 " Is Delta = %d\n"
831 " NumURLs = %zu\n",
832 i,
833 static_cast<uintmax_t>(package.size),
834 package.hash.c_str(),
835 static_cast<uintmax_t>(package.metadata_size),
836 package.metadata_signature.c_str(),
837 package.is_delta,
838 candidate_urls_[i].size());
839
840 for (size_t j = 0; j < candidate_urls_[i].size(); j++)
841 response_sign += base::StringPrintf(
842 " Candidate Url%zu = %s\n", j, candidate_urls_[i][j].c_str());
843 }
844
845 response_sign += base::StringPrintf(
846 "Max Failure Count Per Url = %d\n"
847 "Disable Payload Backoff = %d\n",
848 response_.max_failure_count_per_url,
849 response_.disable_payload_backoff);
850 return response_sign;
851 }
852
LoadResponseSignature()853 void PayloadState::LoadResponseSignature() {
854 CHECK(prefs_);
855 string stored_value;
856 if (prefs_->Exists(kPrefsCurrentResponseSignature) &&
857 prefs_->GetString(kPrefsCurrentResponseSignature, &stored_value)) {
858 SetResponseSignature(stored_value);
859 }
860 }
861
SetResponseSignature(const string & response_signature)862 void PayloadState::SetResponseSignature(const string& response_signature) {
863 CHECK(prefs_);
864 response_signature_ = response_signature;
865 LOG(INFO) << "Current Response Signature = \n" << response_signature_;
866 prefs_->SetString(kPrefsCurrentResponseSignature, response_signature_);
867 }
868
LoadPayloadAttemptNumber()869 void PayloadState::LoadPayloadAttemptNumber() {
870 SetPayloadAttemptNumber(GetPersistedValue(kPrefsPayloadAttemptNumber));
871 }
872
LoadFullPayloadAttemptNumber()873 void PayloadState::LoadFullPayloadAttemptNumber() {
874 SetFullPayloadAttemptNumber(GetPersistedValue(
875 kPrefsFullPayloadAttemptNumber));
876 }
877
SetPayloadAttemptNumber(int payload_attempt_number)878 void PayloadState::SetPayloadAttemptNumber(int payload_attempt_number) {
879 CHECK(prefs_);
880 payload_attempt_number_ = payload_attempt_number;
881 LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number_;
882 prefs_->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number_);
883 }
884
SetFullPayloadAttemptNumber(int full_payload_attempt_number)885 void PayloadState::SetFullPayloadAttemptNumber(
886 int full_payload_attempt_number) {
887 CHECK(prefs_);
888 full_payload_attempt_number_ = full_payload_attempt_number;
889 LOG(INFO) << "Full Payload Attempt Number = " << full_payload_attempt_number_;
890 prefs_->SetInt64(kPrefsFullPayloadAttemptNumber,
891 full_payload_attempt_number_);
892 }
893
SetPayloadIndex(size_t payload_index)894 void PayloadState::SetPayloadIndex(size_t payload_index) {
895 CHECK(prefs_);
896 payload_index_ = payload_index;
897 LOG(INFO) << "Payload Index = " << payload_index_;
898 prefs_->SetInt64(kPrefsUpdateStatePayloadIndex, payload_index_);
899 }
900
NextPayload()901 bool PayloadState::NextPayload() {
902 if (payload_index_ + 1 >= candidate_urls_.size())
903 return false;
904 SetPayloadIndex(payload_index_ + 1);
905 return true;
906 }
907
LoadUrlIndex()908 void PayloadState::LoadUrlIndex() {
909 SetUrlIndex(GetPersistedValue(kPrefsCurrentUrlIndex));
910 }
911
SetUrlIndex(uint32_t url_index)912 void PayloadState::SetUrlIndex(uint32_t url_index) {
913 CHECK(prefs_);
914 url_index_ = url_index;
915 LOG(INFO) << "Current URL Index = " << url_index_;
916 prefs_->SetInt64(kPrefsCurrentUrlIndex, url_index_);
917
918 // Also update the download source, which is purely dependent on the
919 // current URL index alone.
920 UpdateCurrentDownloadSource();
921 }
922
LoadScatteringWaitPeriod()923 void PayloadState::LoadScatteringWaitPeriod() {
924 SetScatteringWaitPeriod(
925 TimeDelta::FromSeconds(GetPersistedValue(kPrefsWallClockWaitPeriod)));
926 }
927
SetScatteringWaitPeriod(TimeDelta wait_period)928 void PayloadState::SetScatteringWaitPeriod(TimeDelta wait_period) {
929 CHECK(prefs_);
930 scattering_wait_period_ = wait_period;
931 LOG(INFO) << "Scattering Wait Period (seconds) = "
932 << scattering_wait_period_.InSeconds();
933 if (scattering_wait_period_.InSeconds() > 0) {
934 prefs_->SetInt64(kPrefsWallClockWaitPeriod,
935 scattering_wait_period_.InSeconds());
936 } else {
937 prefs_->Delete(kPrefsWallClockWaitPeriod);
938 }
939 }
940
LoadUrlSwitchCount()941 void PayloadState::LoadUrlSwitchCount() {
942 SetUrlSwitchCount(GetPersistedValue(kPrefsUrlSwitchCount));
943 }
944
SetUrlSwitchCount(uint32_t url_switch_count)945 void PayloadState::SetUrlSwitchCount(uint32_t url_switch_count) {
946 CHECK(prefs_);
947 url_switch_count_ = url_switch_count;
948 LOG(INFO) << "URL Switch Count = " << url_switch_count_;
949 prefs_->SetInt64(kPrefsUrlSwitchCount, url_switch_count_);
950 }
951
LoadUrlFailureCount()952 void PayloadState::LoadUrlFailureCount() {
953 SetUrlFailureCount(GetPersistedValue(kPrefsCurrentUrlFailureCount));
954 }
955
SetUrlFailureCount(uint32_t url_failure_count)956 void PayloadState::SetUrlFailureCount(uint32_t url_failure_count) {
957 CHECK(prefs_);
958 url_failure_count_ = url_failure_count;
959 LOG(INFO) << "Current URL (Url" << GetUrlIndex()
960 << ")'s Failure Count = " << url_failure_count_;
961 prefs_->SetInt64(kPrefsCurrentUrlFailureCount, url_failure_count_);
962 }
963
LoadBackoffExpiryTime()964 void PayloadState::LoadBackoffExpiryTime() {
965 CHECK(prefs_);
966 int64_t stored_value;
967 if (!prefs_->Exists(kPrefsBackoffExpiryTime))
968 return;
969
970 if (!prefs_->GetInt64(kPrefsBackoffExpiryTime, &stored_value))
971 return;
972
973 Time stored_time = Time::FromInternalValue(stored_value);
974 if (stored_time > Time::Now() + TimeDelta::FromDays(kMaxBackoffDays)) {
975 LOG(ERROR) << "Invalid backoff expiry time ("
976 << utils::ToString(stored_time)
977 << ") in persisted state. Resetting.";
978 stored_time = Time();
979 }
980 SetBackoffExpiryTime(stored_time);
981 }
982
SetBackoffExpiryTime(const Time & new_time)983 void PayloadState::SetBackoffExpiryTime(const Time& new_time) {
984 CHECK(prefs_);
985 backoff_expiry_time_ = new_time;
986 LOG(INFO) << "Backoff Expiry Time = "
987 << utils::ToString(backoff_expiry_time_);
988 prefs_->SetInt64(kPrefsBackoffExpiryTime,
989 backoff_expiry_time_.ToInternalValue());
990 }
991
GetUpdateDuration()992 TimeDelta PayloadState::GetUpdateDuration() {
993 Time end_time = update_timestamp_end_.is_null()
994 ? system_state_->clock()->GetWallclockTime() :
995 update_timestamp_end_;
996 return end_time - update_timestamp_start_;
997 }
998
LoadUpdateTimestampStart()999 void PayloadState::LoadUpdateTimestampStart() {
1000 int64_t stored_value;
1001 Time stored_time;
1002
1003 CHECK(prefs_);
1004
1005 Time now = system_state_->clock()->GetWallclockTime();
1006
1007 if (!prefs_->Exists(kPrefsUpdateTimestampStart)) {
1008 // The preference missing is not unexpected - in that case, just
1009 // use the current time as start time
1010 stored_time = now;
1011 } else if (!prefs_->GetInt64(kPrefsUpdateTimestampStart, &stored_value)) {
1012 LOG(ERROR) << "Invalid UpdateTimestampStart value. Resetting.";
1013 stored_time = now;
1014 } else {
1015 stored_time = Time::FromInternalValue(stored_value);
1016 }
1017
1018 // Sanity check: If the time read from disk is in the future
1019 // (modulo some slack to account for possible NTP drift
1020 // adjustments), something is fishy and we should report and
1021 // reset.
1022 TimeDelta duration_according_to_stored_time = now - stored_time;
1023 if (duration_according_to_stored_time < -kDurationSlack) {
1024 LOG(ERROR) << "The UpdateTimestampStart value ("
1025 << utils::ToString(stored_time)
1026 << ") in persisted state is "
1027 << utils::FormatTimeDelta(duration_according_to_stored_time)
1028 << " in the future. Resetting.";
1029 stored_time = now;
1030 }
1031
1032 SetUpdateTimestampStart(stored_time);
1033 }
1034
SetUpdateTimestampStart(const Time & value)1035 void PayloadState::SetUpdateTimestampStart(const Time& value) {
1036 CHECK(prefs_);
1037 update_timestamp_start_ = value;
1038 prefs_->SetInt64(kPrefsUpdateTimestampStart,
1039 update_timestamp_start_.ToInternalValue());
1040 LOG(INFO) << "Update Timestamp Start = "
1041 << utils::ToString(update_timestamp_start_);
1042 }
1043
SetUpdateTimestampEnd(const Time & value)1044 void PayloadState::SetUpdateTimestampEnd(const Time& value) {
1045 update_timestamp_end_ = value;
1046 LOG(INFO) << "Update Timestamp End = "
1047 << utils::ToString(update_timestamp_end_);
1048 }
1049
GetUpdateDurationUptime()1050 TimeDelta PayloadState::GetUpdateDurationUptime() {
1051 return update_duration_uptime_;
1052 }
1053
LoadUpdateDurationUptime()1054 void PayloadState::LoadUpdateDurationUptime() {
1055 int64_t stored_value;
1056 TimeDelta stored_delta;
1057
1058 CHECK(prefs_);
1059
1060 if (!prefs_->Exists(kPrefsUpdateDurationUptime)) {
1061 // The preference missing is not unexpected - in that case, just
1062 // we'll use zero as the delta
1063 } else if (!prefs_->GetInt64(kPrefsUpdateDurationUptime, &stored_value)) {
1064 LOG(ERROR) << "Invalid UpdateDurationUptime value. Resetting.";
1065 stored_delta = TimeDelta::FromSeconds(0);
1066 } else {
1067 stored_delta = TimeDelta::FromInternalValue(stored_value);
1068 }
1069
1070 // Sanity-check: Uptime can never be greater than the wall-clock
1071 // difference (modulo some slack). If it is, report and reset
1072 // to the wall-clock difference.
1073 TimeDelta diff = GetUpdateDuration() - stored_delta;
1074 if (diff < -kDurationSlack) {
1075 LOG(ERROR) << "The UpdateDurationUptime value ("
1076 << utils::FormatTimeDelta(stored_delta)
1077 << ") in persisted state is "
1078 << utils::FormatTimeDelta(diff)
1079 << " larger than the wall-clock delta. Resetting.";
1080 stored_delta = update_duration_current_;
1081 }
1082
1083 SetUpdateDurationUptime(stored_delta);
1084 }
1085
LoadNumReboots()1086 void PayloadState::LoadNumReboots() {
1087 SetNumReboots(GetPersistedValue(kPrefsNumReboots));
1088 }
1089
LoadRollbackVersion()1090 void PayloadState::LoadRollbackVersion() {
1091 CHECK(powerwash_safe_prefs_);
1092 string rollback_version;
1093 if (powerwash_safe_prefs_->GetString(kPrefsRollbackVersion,
1094 &rollback_version)) {
1095 SetRollbackVersion(rollback_version);
1096 }
1097 }
1098
SetRollbackVersion(const string & rollback_version)1099 void PayloadState::SetRollbackVersion(const string& rollback_version) {
1100 CHECK(powerwash_safe_prefs_);
1101 LOG(INFO) << "Blacklisting version "<< rollback_version;
1102 rollback_version_ = rollback_version;
1103 powerwash_safe_prefs_->SetString(kPrefsRollbackVersion, rollback_version);
1104 }
1105
SetUpdateDurationUptimeExtended(const TimeDelta & value,const Time & timestamp,bool use_logging)1106 void PayloadState::SetUpdateDurationUptimeExtended(const TimeDelta& value,
1107 const Time& timestamp,
1108 bool use_logging) {
1109 CHECK(prefs_);
1110 update_duration_uptime_ = value;
1111 update_duration_uptime_timestamp_ = timestamp;
1112 prefs_->SetInt64(kPrefsUpdateDurationUptime,
1113 update_duration_uptime_.ToInternalValue());
1114 if (use_logging) {
1115 LOG(INFO) << "Update Duration Uptime = "
1116 << utils::FormatTimeDelta(update_duration_uptime_);
1117 }
1118 }
1119
SetUpdateDurationUptime(const TimeDelta & value)1120 void PayloadState::SetUpdateDurationUptime(const TimeDelta& value) {
1121 Time now = system_state_->clock()->GetMonotonicTime();
1122 SetUpdateDurationUptimeExtended(value, now, true);
1123 }
1124
CalculateUpdateDurationUptime()1125 void PayloadState::CalculateUpdateDurationUptime() {
1126 Time now = system_state_->clock()->GetMonotonicTime();
1127 TimeDelta uptime_since_last_update = now - update_duration_uptime_timestamp_;
1128 TimeDelta new_uptime = update_duration_uptime_ + uptime_since_last_update;
1129 // We're frequently called so avoid logging this write
1130 SetUpdateDurationUptimeExtended(new_uptime, now, false);
1131 }
1132
GetPrefsKey(const string & prefix,DownloadSource source)1133 string PayloadState::GetPrefsKey(const string& prefix, DownloadSource source) {
1134 return prefix + "-from-" + utils::ToString(source);
1135 }
1136
LoadCurrentBytesDownloaded(DownloadSource source)1137 void PayloadState::LoadCurrentBytesDownloaded(DownloadSource source) {
1138 string key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source);
1139 SetCurrentBytesDownloaded(source, GetPersistedValue(key), true);
1140 }
1141
SetCurrentBytesDownloaded(DownloadSource source,uint64_t current_bytes_downloaded,bool log)1142 void PayloadState::SetCurrentBytesDownloaded(
1143 DownloadSource source,
1144 uint64_t current_bytes_downloaded,
1145 bool log) {
1146 CHECK(prefs_);
1147
1148 if (source >= kNumDownloadSources)
1149 return;
1150
1151 // Update the in-memory value.
1152 current_bytes_downloaded_[source] = current_bytes_downloaded;
1153
1154 string prefs_key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source);
1155 prefs_->SetInt64(prefs_key, current_bytes_downloaded);
1156 LOG_IF(INFO, log) << "Current bytes downloaded for "
1157 << utils::ToString(source) << " = "
1158 << GetCurrentBytesDownloaded(source);
1159 }
1160
LoadTotalBytesDownloaded(DownloadSource source)1161 void PayloadState::LoadTotalBytesDownloaded(DownloadSource source) {
1162 string key = GetPrefsKey(kPrefsTotalBytesDownloaded, source);
1163 SetTotalBytesDownloaded(source, GetPersistedValue(key), true);
1164 }
1165
SetTotalBytesDownloaded(DownloadSource source,uint64_t total_bytes_downloaded,bool log)1166 void PayloadState::SetTotalBytesDownloaded(
1167 DownloadSource source,
1168 uint64_t total_bytes_downloaded,
1169 bool log) {
1170 CHECK(prefs_);
1171
1172 if (source >= kNumDownloadSources)
1173 return;
1174
1175 // Update the in-memory value.
1176 total_bytes_downloaded_[source] = total_bytes_downloaded;
1177
1178 // Persist.
1179 string prefs_key = GetPrefsKey(kPrefsTotalBytesDownloaded, source);
1180 prefs_->SetInt64(prefs_key, total_bytes_downloaded);
1181 LOG_IF(INFO, log) << "Total bytes downloaded for "
1182 << utils::ToString(source) << " = "
1183 << GetTotalBytesDownloaded(source);
1184 }
1185
LoadNumResponsesSeen()1186 void PayloadState::LoadNumResponsesSeen() {
1187 SetNumResponsesSeen(GetPersistedValue(kPrefsNumResponsesSeen));
1188 }
1189
SetNumResponsesSeen(int num_responses_seen)1190 void PayloadState::SetNumResponsesSeen(int num_responses_seen) {
1191 CHECK(prefs_);
1192 num_responses_seen_ = num_responses_seen;
1193 LOG(INFO) << "Num Responses Seen = " << num_responses_seen_;
1194 prefs_->SetInt64(kPrefsNumResponsesSeen, num_responses_seen_);
1195 }
1196
ComputeCandidateUrls()1197 void PayloadState::ComputeCandidateUrls() {
1198 bool http_url_ok = true;
1199
1200 if (system_state_->hardware()->IsOfficialBuild()) {
1201 const policy::DevicePolicy* policy = system_state_->device_policy();
1202 if (policy && policy->GetHttpDownloadsEnabled(&http_url_ok) && !http_url_ok)
1203 LOG(INFO) << "Downloads via HTTP Url are not enabled by device policy";
1204 } else {
1205 LOG(INFO) << "Allowing HTTP downloads for unofficial builds";
1206 http_url_ok = true;
1207 }
1208
1209 candidate_urls_.clear();
1210 for (const auto& package : response_.packages) {
1211 candidate_urls_.emplace_back();
1212 for (const string& candidate_url : package.payload_urls) {
1213 if (base::StartsWith(
1214 candidate_url, "http://", base::CompareCase::INSENSITIVE_ASCII) &&
1215 !http_url_ok) {
1216 continue;
1217 }
1218 candidate_urls_.back().push_back(candidate_url);
1219 LOG(INFO) << "Candidate Url" << (candidate_urls_.back().size() - 1)
1220 << ": " << candidate_url;
1221 }
1222 LOG(INFO) << "Found " << candidate_urls_.back().size() << " candidate URLs "
1223 << "out of " << package.payload_urls.size()
1224 << " URLs supplied in package " << candidate_urls_.size() - 1;
1225 }
1226 }
1227
CreateSystemUpdatedMarkerFile()1228 void PayloadState::CreateSystemUpdatedMarkerFile() {
1229 CHECK(prefs_);
1230 int64_t value = system_state_->clock()->GetWallclockTime().ToInternalValue();
1231 prefs_->SetInt64(kPrefsSystemUpdatedMarker, value);
1232 }
1233
BootedIntoUpdate(TimeDelta time_to_reboot)1234 void PayloadState::BootedIntoUpdate(TimeDelta time_to_reboot) {
1235 // Send |time_to_reboot| as a UMA stat.
1236 string metric = metrics::kMetricTimeToRebootMinutes;
1237 system_state_->metrics_lib()->SendToUMA(metric,
1238 time_to_reboot.InMinutes(),
1239 0, // min: 0 minute
1240 30*24*60, // max: 1 month (approx)
1241 kNumDefaultUmaBuckets);
1242 LOG(INFO) << "Uploading " << utils::FormatTimeDelta(time_to_reboot)
1243 << " for metric " << metric;
1244 }
1245
UpdateEngineStarted()1246 void PayloadState::UpdateEngineStarted() {
1247 // Flush previous state from abnormal attempt failure, if any.
1248 ReportAndClearPersistedAttemptMetrics();
1249
1250 // Avoid the UpdateEngineStarted actions if this is not the first time we
1251 // run the update engine since reboot.
1252 if (!system_state_->system_rebooted())
1253 return;
1254
1255 // Figure out if we just booted into a new update
1256 if (prefs_->Exists(kPrefsSystemUpdatedMarker)) {
1257 int64_t stored_value;
1258 if (prefs_->GetInt64(kPrefsSystemUpdatedMarker, &stored_value)) {
1259 Time system_updated_at = Time::FromInternalValue(stored_value);
1260 if (!system_updated_at.is_null()) {
1261 TimeDelta time_to_reboot =
1262 system_state_->clock()->GetWallclockTime() - system_updated_at;
1263 if (time_to_reboot.ToInternalValue() < 0) {
1264 LOG(ERROR) << "time_to_reboot is negative - system_updated_at: "
1265 << utils::ToString(system_updated_at);
1266 } else {
1267 BootedIntoUpdate(time_to_reboot);
1268 }
1269 }
1270 }
1271 prefs_->Delete(kPrefsSystemUpdatedMarker);
1272 }
1273 // Check if it is needed to send metrics about a failed reboot into a new
1274 // version.
1275 ReportFailedBootIfNeeded();
1276 }
1277
ReportFailedBootIfNeeded()1278 void PayloadState::ReportFailedBootIfNeeded() {
1279 // If the kPrefsTargetVersionInstalledFrom is present, a successfully applied
1280 // payload was marked as ready immediately before the last reboot, and we
1281 // need to check if such payload successfully rebooted or not.
1282 if (prefs_->Exists(kPrefsTargetVersionInstalledFrom)) {
1283 int64_t installed_from = 0;
1284 if (!prefs_->GetInt64(kPrefsTargetVersionInstalledFrom, &installed_from)) {
1285 LOG(ERROR) << "Error reading TargetVersionInstalledFrom on reboot.";
1286 return;
1287 }
1288 // Old Chrome OS devices will write 2 or 4 in this setting, with the
1289 // partition number. We are now using slot numbers (0 or 1) instead, so
1290 // the following comparison will not match if we are comparing an old
1291 // partition number against a new slot number, which is the correct outcome
1292 // since we successfully booted the new update in that case. If the boot
1293 // failed, we will read this value from the same version, so it will always
1294 // be compatible.
1295 if (installed_from == system_state_->boot_control()->GetCurrentSlot()) {
1296 // A reboot was pending, but the chromebook is again in the same
1297 // BootDevice where the update was installed from.
1298 int64_t target_attempt;
1299 if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt)) {
1300 LOG(ERROR) << "Error reading TargetVersionAttempt when "
1301 "TargetVersionInstalledFrom was present.";
1302 target_attempt = 1;
1303 }
1304
1305 // Report the UMA metric of the current boot failure.
1306 string metric = metrics::kMetricFailedUpdateCount;
1307 LOG(INFO) << "Uploading " << target_attempt
1308 << " (count) for metric " << metric;
1309 system_state_->metrics_lib()->SendToUMA(
1310 metric,
1311 target_attempt,
1312 1, // min value
1313 50, // max value
1314 kNumDefaultUmaBuckets);
1315 } else {
1316 prefs_->Delete(kPrefsTargetVersionAttempt);
1317 prefs_->Delete(kPrefsTargetVersionUniqueId);
1318 }
1319 prefs_->Delete(kPrefsTargetVersionInstalledFrom);
1320 }
1321 }
1322
ExpectRebootInNewVersion(const string & target_version_uid)1323 void PayloadState::ExpectRebootInNewVersion(const string& target_version_uid) {
1324 // Expect to boot into the new partition in the next reboot setting the
1325 // TargetVersion* flags in the Prefs.
1326 string stored_target_version_uid;
1327 string target_version_id;
1328 string target_partition;
1329 int64_t target_attempt;
1330
1331 if (prefs_->Exists(kPrefsTargetVersionUniqueId) &&
1332 prefs_->GetString(kPrefsTargetVersionUniqueId,
1333 &stored_target_version_uid) &&
1334 stored_target_version_uid == target_version_uid) {
1335 if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt))
1336 target_attempt = 0;
1337 } else {
1338 prefs_->SetString(kPrefsTargetVersionUniqueId, target_version_uid);
1339 target_attempt = 0;
1340 }
1341 prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt + 1);
1342
1343 prefs_->SetInt64(kPrefsTargetVersionInstalledFrom,
1344 system_state_->boot_control()->GetCurrentSlot());
1345 }
1346
ResetUpdateStatus()1347 void PayloadState::ResetUpdateStatus() {
1348 // Remove the TargetVersionInstalledFrom pref so that if the machine is
1349 // rebooted the next boot is not flagged as failed to rebooted into the
1350 // new applied payload.
1351 prefs_->Delete(kPrefsTargetVersionInstalledFrom);
1352
1353 // Also decrement the attempt number if it exists.
1354 int64_t target_attempt;
1355 if (prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt))
1356 prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt - 1);
1357 }
1358
GetP2PNumAttempts()1359 int PayloadState::GetP2PNumAttempts() {
1360 return p2p_num_attempts_;
1361 }
1362
SetP2PNumAttempts(int value)1363 void PayloadState::SetP2PNumAttempts(int value) {
1364 p2p_num_attempts_ = value;
1365 LOG(INFO) << "p2p Num Attempts = " << p2p_num_attempts_;
1366 CHECK(prefs_);
1367 prefs_->SetInt64(kPrefsP2PNumAttempts, value);
1368 }
1369
LoadP2PNumAttempts()1370 void PayloadState::LoadP2PNumAttempts() {
1371 SetP2PNumAttempts(GetPersistedValue(kPrefsP2PNumAttempts));
1372 }
1373
GetP2PFirstAttemptTimestamp()1374 Time PayloadState::GetP2PFirstAttemptTimestamp() {
1375 return p2p_first_attempt_timestamp_;
1376 }
1377
SetP2PFirstAttemptTimestamp(const Time & time)1378 void PayloadState::SetP2PFirstAttemptTimestamp(const Time& time) {
1379 p2p_first_attempt_timestamp_ = time;
1380 LOG(INFO) << "p2p First Attempt Timestamp = "
1381 << utils::ToString(p2p_first_attempt_timestamp_);
1382 CHECK(prefs_);
1383 int64_t stored_value = time.ToInternalValue();
1384 prefs_->SetInt64(kPrefsP2PFirstAttemptTimestamp, stored_value);
1385 }
1386
LoadP2PFirstAttemptTimestamp()1387 void PayloadState::LoadP2PFirstAttemptTimestamp() {
1388 int64_t stored_value = GetPersistedValue(kPrefsP2PFirstAttemptTimestamp);
1389 Time stored_time = Time::FromInternalValue(stored_value);
1390 SetP2PFirstAttemptTimestamp(stored_time);
1391 }
1392
P2PNewAttempt()1393 void PayloadState::P2PNewAttempt() {
1394 CHECK(prefs_);
1395 // Set timestamp, if it hasn't been set already
1396 if (p2p_first_attempt_timestamp_.is_null()) {
1397 SetP2PFirstAttemptTimestamp(system_state_->clock()->GetWallclockTime());
1398 }
1399 // Increase number of attempts
1400 SetP2PNumAttempts(GetP2PNumAttempts() + 1);
1401 }
1402
P2PAttemptAllowed()1403 bool PayloadState::P2PAttemptAllowed() {
1404 if (p2p_num_attempts_ > kMaxP2PAttempts) {
1405 LOG(INFO) << "Number of p2p attempts is " << p2p_num_attempts_
1406 << " which is greater than "
1407 << kMaxP2PAttempts
1408 << " - disallowing p2p.";
1409 return false;
1410 }
1411
1412 if (!p2p_first_attempt_timestamp_.is_null()) {
1413 Time now = system_state_->clock()->GetWallclockTime();
1414 TimeDelta time_spent_attempting_p2p = now - p2p_first_attempt_timestamp_;
1415 if (time_spent_attempting_p2p.InSeconds() < 0) {
1416 LOG(ERROR) << "Time spent attempting p2p is negative"
1417 << " - disallowing p2p.";
1418 return false;
1419 }
1420 if (time_spent_attempting_p2p.InSeconds() > kMaxP2PAttemptTimeSeconds) {
1421 LOG(INFO) << "Time spent attempting p2p is "
1422 << utils::FormatTimeDelta(time_spent_attempting_p2p)
1423 << " which is greater than "
1424 << utils::FormatTimeDelta(TimeDelta::FromSeconds(
1425 kMaxP2PAttemptTimeSeconds))
1426 << " - disallowing p2p.";
1427 return false;
1428 }
1429 }
1430
1431 return true;
1432 }
1433
GetPayloadSize()1434 int64_t PayloadState::GetPayloadSize() {
1435 int64_t payload_size = 0;
1436 for (const auto& package : response_.packages)
1437 payload_size += package.size;
1438 return payload_size;
1439 }
1440
1441 } // namespace chromeos_update_engine
1442