Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1 | // 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 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_PAYLOAD_STATE_H_ |
| 6 | #define UPDATE_ENGINE_PAYLOAD_STATE_H_ |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 7 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 11 | #include <base/time/time.h> |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 12 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 13 | |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 14 | #include "update_engine/metrics.h" |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 15 | #include "update_engine/payload_state_interface.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 16 | #include "update_engine/prefs_interface.h" |
| 17 | |
| 18 | namespace chromeos_update_engine { |
| 19 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 20 | class SystemState; |
| 21 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 22 | // Encapsulates all the payload state required for download. This includes the |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 23 | // state necessary for handling multiple URLs in Omaha response, the backoff |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 24 | // state, etc. All state is persisted so that we use the most recently saved |
| 25 | // value when resuming the update_engine process. All state is also cached in |
| 26 | // memory so that we ensure we always make progress based on last known good |
| 27 | // state even when there's any issue in reading/writing from the file system. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 28 | class PayloadState : public PayloadStateInterface { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 29 | public: |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 30 | PayloadState(); |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 31 | ~PayloadState() override {} |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 32 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 33 | // Initializes a payload state object using the given global system state. |
| 34 | // It performs the initial loading of all persisted state into memory and |
| 35 | // dumps the initial state for debugging purposes. Note: the other methods |
| 36 | // should be called only after calling Initialize on this object. |
| 37 | bool Initialize(SystemState* system_state); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 38 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 39 | // Implementation of PayloadStateInterface methods. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 40 | void SetResponse(const OmahaResponse& response) override; |
| 41 | void DownloadComplete() override; |
| 42 | void DownloadProgress(size_t count) override; |
| 43 | void UpdateResumed() override; |
| 44 | void UpdateRestarted() override; |
| 45 | void UpdateSucceeded() override; |
| 46 | void UpdateFailed(ErrorCode error) override; |
| 47 | void ResetUpdateStatus() override; |
| 48 | bool ShouldBackoffDownload() override; |
| 49 | void Rollback() override; |
| 50 | void ExpectRebootInNewVersion(const std::string& target_version_uid) override; |
| 51 | void SetUsingP2PForDownloading(bool value) override; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 52 | |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 53 | void SetUsingP2PForSharing(bool value) override { |
| 54 | using_p2p_for_sharing_ = value; |
| 55 | } |
| 56 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 57 | inline std::string GetResponseSignature() override { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 58 | return response_signature_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 61 | inline int GetFullPayloadAttemptNumber() override { |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 62 | return full_payload_attempt_number_; |
| 63 | } |
| 64 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 65 | inline int GetPayloadAttemptNumber() override { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 66 | return payload_attempt_number_; |
| 67 | } |
| 68 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 69 | inline std::string GetCurrentUrl() override { |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 70 | return candidate_urls_.size() ? candidate_urls_[url_index_] : ""; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 73 | inline uint32_t GetUrlFailureCount() override { |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 74 | return url_failure_count_; |
| 75 | } |
| 76 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 77 | inline uint32_t GetUrlSwitchCount() override { |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 78 | return url_switch_count_; |
| 79 | } |
| 80 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 81 | inline int GetNumResponsesSeen() override { |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 82 | return num_responses_seen_; |
| 83 | } |
| 84 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 85 | inline base::Time GetBackoffExpiryTime() override { |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 86 | return backoff_expiry_time_; |
| 87 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 88 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 89 | base::TimeDelta GetUpdateDuration() override; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 90 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 91 | base::TimeDelta GetUpdateDurationUptime() override; |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 92 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 93 | inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) override { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 94 | return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0; |
| 95 | } |
| 96 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 97 | inline uint64_t GetTotalBytesDownloaded(DownloadSource source) override { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 98 | return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0; |
| 99 | } |
| 100 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 101 | inline uint32_t GetNumReboots() override { |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 102 | return num_reboots_; |
| 103 | } |
| 104 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 105 | void UpdateEngineStarted() override; |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 106 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 107 | inline std::string GetRollbackVersion() override { |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 108 | return rollback_version_; |
| 109 | } |
| 110 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 111 | int GetP2PNumAttempts() override; |
| 112 | base::Time GetP2PFirstAttemptTimestamp() override; |
| 113 | void P2PNewAttempt() override; |
| 114 | bool P2PAttemptAllowed() override; |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 115 | |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 116 | bool GetUsingP2PForDownloading() const override { |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 117 | return using_p2p_for_downloading_; |
| 118 | } |
| 119 | |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 120 | bool GetUsingP2PForSharing() const override { |
| 121 | return using_p2p_for_sharing_; |
| 122 | } |
| 123 | |
Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 124 | base::TimeDelta GetScatteringWaitPeriod() override { |
| 125 | return scattering_wait_period_; |
| 126 | } |
| 127 | |
| 128 | void SetScatteringWaitPeriod(base::TimeDelta wait_period) override; |
| 129 | |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 130 | void SetP2PUrl(const std::string& url) override { |
| 131 | p2p_url_ = url; |
| 132 | } |
| 133 | |
| 134 | std::string GetP2PUrl() const override { |
| 135 | return p2p_url_; |
| 136 | } |
| 137 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 138 | private: |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 139 | enum class AttemptType { |
| 140 | kUpdate, |
| 141 | kRollback, |
| 142 | }; |
| 143 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 144 | friend class PayloadStateTest; |
| 145 | FRIEND_TEST(PayloadStateTest, RebootAfterUpdateFailedMetric); |
| 146 | FRIEND_TEST(PayloadStateTest, RebootAfterUpdateSucceed); |
| 147 | FRIEND_TEST(PayloadStateTest, RebootAfterCanceledUpdate); |
Chris Sosa | b3dcdb3 | 2013-09-04 15:22:12 -0700 | [diff] [blame] | 148 | FRIEND_TEST(PayloadStateTest, RollbackVersion); |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 149 | FRIEND_TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs); |
| 150 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 151 | // Helper called when an attempt has begun, is called by |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 152 | // UpdateResumed(), UpdateRestarted() and Rollback(). |
| 153 | void AttemptStarted(AttemptType attempt_type); |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 154 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 155 | // Increments the payload attempt number used for metrics. |
| 156 | void IncrementPayloadAttemptNumber(); |
| 157 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 158 | // Increments the payload attempt number which governs the backoff behavior |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 159 | // at the time of the next update check. |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 160 | void IncrementFullPayloadAttemptNumber(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 161 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 162 | // Advances the current URL index to the next available one. If all URLs have |
| 163 | // been exhausted during the current payload download attempt (as indicated |
| 164 | // by the payload attempt number), then it will increment the payload attempt |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 165 | // number and wrap around again with the first URL in the list. This also |
| 166 | // updates the URL switch count, if needed. |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 167 | void IncrementUrlIndex(); |
| 168 | |
| 169 | // Increments the failure count of the current URL. If the configured max |
| 170 | // failure count is reached for this URL, it advances the current URL index |
| 171 | // to the next URL and resets the failure count for that URL. |
| 172 | void IncrementFailureCount(); |
| 173 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 174 | // Updates the backoff expiry time exponentially based on the current |
| 175 | // payload attempt number. |
| 176 | void UpdateBackoffExpiryTime(); |
| 177 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 178 | // Updates the value of current download source based on the current URL |
| 179 | // index. If the download source is not one of the known sources, it's set |
| 180 | // to kNumDownloadSources. |
| 181 | void UpdateCurrentDownloadSource(); |
| 182 | |
| 183 | // Updates the various metrics corresponding with the given number of bytes |
| 184 | // that were downloaded recently. |
| 185 | void UpdateBytesDownloaded(size_t count); |
| 186 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 187 | // Calculates the PayloadType we're using. |
| 188 | PayloadType CalculatePayloadType(); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 189 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 190 | // Collects and reports the various metrics related to an update attempt. |
| 191 | void CollectAndReportAttemptMetrics(ErrorCode code); |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 192 | |
David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 193 | // Persists values related to the UpdateEngine.Attempt.* metrics so |
| 194 | // we can identify later if an update attempt ends abnormally. |
| 195 | void PersistAttemptMetrics(); |
| 196 | |
| 197 | // Clears persistent state previously set using AttemptMetricsPersist(). |
| 198 | void ClearPersistedAttemptMetrics(); |
| 199 | |
| 200 | // Checks if persistent state previously set using AttemptMetricsPersist() |
| 201 | // exists and, if so, emits it with |attempt_result| set to |
| 202 | // metrics::AttemptResult::kAbnormalTermination. |
| 203 | void ReportAndClearPersistedAttemptMetrics(); |
| 204 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 205 | // Collects and reports the various metrics related to a successful update. |
| 206 | void CollectAndReportSuccessfulUpdateMetrics(); |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 207 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 208 | // Checks if we were expecting to be running in the new version but the |
| 209 | // boot into the new version failed for some reason. If that's the case, an |
| 210 | // UMA metric is sent reporting the number of attempts the same applied |
| 211 | // payload was attempted to reboot. This function is called by UpdateAttempter |
| 212 | // every time the update engine starts and there's no reboot pending. |
| 213 | void ReportFailedBootIfNeeded(); |
| 214 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 215 | // Resets all the persisted state values which are maintained relative to the |
| 216 | // current response signature. The response signature itself is not reset. |
| 217 | void ResetPersistedState(); |
| 218 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 219 | // Resets the appropriate state related to download sources that need to be |
| 220 | // reset on a new update. |
| 221 | void ResetDownloadSourcesOnNewUpdate(); |
| 222 | |
Chris Sosa | b3dcdb3 | 2013-09-04 15:22:12 -0700 | [diff] [blame] | 223 | // Returns the persisted value from prefs_ for the given key. It also |
| 224 | // validates that the value returned is non-negative. |
| 225 | int64_t GetPersistedValue(const std::string& key); |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 226 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 227 | // Calculates the response "signature", which is basically a string composed |
| 228 | // of the subset of the fields in the current response that affect the |
| 229 | // behavior of the PayloadState. |
| 230 | std::string CalculateResponseSignature(); |
| 231 | |
| 232 | // Initializes the current response signature from the persisted state. |
| 233 | void LoadResponseSignature(); |
| 234 | |
| 235 | // Sets the response signature to the given value. Also persists the value |
| 236 | // being set so that we resume from the save value in case of a process |
| 237 | // restart. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 238 | void SetResponseSignature(const std::string& response_signature); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 239 | |
| 240 | // Initializes the payload attempt number from the persisted state. |
| 241 | void LoadPayloadAttemptNumber(); |
| 242 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 243 | // Initializes the payload attempt number for full payloads from the persisted |
| 244 | // state. |
| 245 | void LoadFullPayloadAttemptNumber(); |
| 246 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 247 | // Sets the payload attempt number to the given value. Also persists the |
| 248 | // value being set so that we resume from the same value in case of a process |
| 249 | // restart. |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 250 | void SetPayloadAttemptNumber(int payload_attempt_number); |
| 251 | |
| 252 | // Sets the payload attempt number for full updates to the given value. Also |
| 253 | // persists the value being set so that we resume from the same value in case |
| 254 | // of a process restart. |
| 255 | void SetFullPayloadAttemptNumber(int payload_attempt_number); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 256 | |
| 257 | // Initializes the current URL index from the persisted state. |
| 258 | void LoadUrlIndex(); |
| 259 | |
| 260 | // Sets the current URL index to the given value. Also persists the value |
| 261 | // being set so that we resume from the same value in case of a process |
| 262 | // restart. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 263 | void SetUrlIndex(uint32_t url_index); |
| 264 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 265 | // Initializes the current URL's failure count from the persisted stae. |
| 266 | void LoadUrlFailureCount(); |
| 267 | |
| 268 | // Sets the current URL's failure count to the given value. Also persists the |
| 269 | // value being set so that we resume from the same value in case of a process |
| 270 | // restart. |
| 271 | void SetUrlFailureCount(uint32_t url_failure_count); |
| 272 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 273 | // Sets |url_switch_count_| to the given value and persists the value. |
| 274 | void SetUrlSwitchCount(uint32_t url_switch_count); |
| 275 | |
| 276 | // Initializes |url_switch_count_| from the persisted stae. |
| 277 | void LoadUrlSwitchCount(); |
| 278 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 279 | // Initializes the backoff expiry time from the persisted state. |
| 280 | void LoadBackoffExpiryTime(); |
| 281 | |
| 282 | // Sets the backoff expiry time to the given value. Also persists the value |
| 283 | // being set so that we resume from the same value in case of a process |
| 284 | // restart. |
| 285 | void SetBackoffExpiryTime(const base::Time& new_time); |
| 286 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 287 | // Initializes |update_timestamp_start_| from the persisted state. |
| 288 | void LoadUpdateTimestampStart(); |
| 289 | |
| 290 | // Sets |update_timestamp_start_| to the given value and persists the value. |
| 291 | void SetUpdateTimestampStart(const base::Time& value); |
| 292 | |
| 293 | // Sets |update_timestamp_end_| to the given value. This is not persisted |
| 294 | // as it happens at the end of the update process where state is deleted |
| 295 | // anyway. |
| 296 | void SetUpdateTimestampEnd(const base::Time& value); |
| 297 | |
| 298 | // Initializes |update_duration_uptime_| from the persisted state. |
| 299 | void LoadUpdateDurationUptime(); |
| 300 | |
| 301 | // Helper method used in SetUpdateDurationUptime() and |
| 302 | // CalculateUpdateDurationUptime(). |
| 303 | void SetUpdateDurationUptimeExtended(const base::TimeDelta& value, |
| 304 | const base::Time& timestamp, |
| 305 | bool use_logging); |
| 306 | |
| 307 | // Sets |update_duration_uptime_| to the given value and persists |
| 308 | // the value and sets |update_duration_uptime_timestamp_| to the |
| 309 | // current monotonic time. |
| 310 | void SetUpdateDurationUptime(const base::TimeDelta& value); |
| 311 | |
| 312 | // Adds the difference between current monotonic time and |
| 313 | // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and |
| 314 | // sets |update_duration_uptime_timestamp_| to current monotonic time. |
| 315 | void CalculateUpdateDurationUptime(); |
| 316 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 317 | // Returns the full key for a download source given the prefix. |
| 318 | std::string GetPrefsKey(const std::string& prefix, DownloadSource source); |
| 319 | |
| 320 | // Loads the number of bytes that have been currently downloaded through the |
| 321 | // previous attempts from the persisted state for the given source. It's |
| 322 | // reset to 0 everytime we begin a full update and is continued from previous |
| 323 | // attempt if we're resuming the update. |
| 324 | void LoadCurrentBytesDownloaded(DownloadSource source); |
| 325 | |
| 326 | // Sets the number of bytes that have been currently downloaded for the |
| 327 | // given source. This value is also persisted. |
| 328 | void SetCurrentBytesDownloaded(DownloadSource source, |
| 329 | uint64_t current_bytes_downloaded, |
| 330 | bool log); |
| 331 | |
| 332 | // Loads the total number of bytes that have been downloaded (since the last |
| 333 | // successful update) from the persisted state for the given source. It's |
| 334 | // reset to 0 everytime we successfully apply an update and counts the bytes |
| 335 | // downloaded for both successful and failed attempts since then. |
| 336 | void LoadTotalBytesDownloaded(DownloadSource source); |
| 337 | |
| 338 | // Sets the total number of bytes that have been downloaded so far for the |
| 339 | // given source. This value is also persisted. |
| 340 | void SetTotalBytesDownloaded(DownloadSource source, |
| 341 | uint64_t total_bytes_downloaded, |
| 342 | bool log); |
| 343 | |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 344 | // Loads the blacklisted version from our prefs file. |
| 345 | void LoadRollbackVersion(); |
| 346 | |
| 347 | // Blacklists this version from getting AU'd to until we receive a new update |
| 348 | // response. |
| 349 | void SetRollbackVersion(const std::string& rollback_version); |
| 350 | |
| 351 | // Clears any blacklisted version. |
| 352 | void ResetRollbackVersion(); |
| 353 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 354 | inline uint32_t GetUrlIndex() { |
| 355 | return url_index_; |
| 356 | } |
| 357 | |
| 358 | // Computes the list of candidate URLs from the total list of payload URLs in |
| 359 | // the Omaha response. |
| 360 | void ComputeCandidateUrls(); |
| 361 | |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 362 | // Sets |num_responses_seen_| and persist it to disk. |
| 363 | void SetNumResponsesSeen(int num_responses_seen); |
| 364 | |
| 365 | // Initializes |num_responses_seen_| from persisted state. |
| 366 | void LoadNumResponsesSeen(); |
| 367 | |
Alex Deymo | b33b0f0 | 2013-08-08 21:10:02 -0700 | [diff] [blame] | 368 | // Reports metric conveying how many times updates were abandoned since |
| 369 | // the last update was applied. The difference between this metric and the |
| 370 | // previous ReportUpdatesAbandonedCountMetric() one is that this metric is |
| 371 | // reported every time an update is abandoned, as oposed to the mentioned |
| 372 | // metric that is reported once the new update was applied. |
| 373 | void ReportUpdatesAbandonedEventCountMetric(); |
| 374 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 375 | // Initializes |num_reboots_| from the persisted state. |
| 376 | void LoadNumReboots(); |
| 377 | |
| 378 | // Sets |num_reboots| for the update attempt. Also persists the |
| 379 | // value being set so that we resume from the same value in case of a process |
| 380 | // restart. |
| 381 | void SetNumReboots(uint32_t num_reboots); |
| 382 | |
| 383 | // Checks to see if the device rebooted since the last call and if so |
| 384 | // increments num_reboots. |
| 385 | void UpdateNumReboots(); |
| 386 | |
David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 387 | // Writes the current wall-clock time to the kPrefsSystemUpdatedMarker |
| 388 | // state variable. |
| 389 | void CreateSystemUpdatedMarkerFile(); |
| 390 | |
| 391 | // Called at program startup if the device booted into a new update. |
| 392 | // The |time_to_reboot| parameter contains the (wall-clock) duration |
| 393 | // from when the update successfully completed (the value written |
| 394 | // into the kPrefsSystemUpdatedMarker state variable) until the device |
| 395 | // was booted into the update (current wall-clock time). |
| 396 | void BootedIntoUpdate(base::TimeDelta time_to_reboot); |
| 397 | |
David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 398 | // Loads the |kPrefsP2PFirstAttemptTimestamp| state variable from disk |
| 399 | // into |p2p_first_attempt_timestamp_|. |
| 400 | void LoadP2PFirstAttemptTimestamp(); |
| 401 | |
| 402 | // Loads the |kPrefsP2PNumAttempts| state variable into |p2p_num_attempts_|. |
| 403 | void LoadP2PNumAttempts(); |
| 404 | |
| 405 | // Sets the |kPrefsP2PNumAttempts| state variable to |value|. |
| 406 | void SetP2PNumAttempts(int value); |
| 407 | |
| 408 | // Sets the |kPrefsP2PFirstAttemptTimestamp| state variable to |time|. |
| 409 | void SetP2PFirstAttemptTimestamp(const base::Time& time); |
| 410 | |
Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 411 | // Loads the persisted scattering wallclock-based wait period. |
| 412 | void LoadScatteringWaitPeriod(); |
| 413 | |
Gilad Arnold | 6e15aac | 2014-10-02 10:34:14 -0700 | [diff] [blame] | 414 | // The global state of the system. |
| 415 | SystemState* system_state_; |
| 416 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 417 | // Interface object with which we read/write persisted state. This must |
| 418 | // be set by calling the Initialize method before calling any other method. |
| 419 | PrefsInterface* prefs_; |
| 420 | |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 421 | // Interface object with which we read/write persisted state. This must |
| 422 | // be set by calling the Initialize method before calling any other method. |
| 423 | // This object persists across powerwashes. |
| 424 | PrefsInterface* powerwash_safe_prefs_; |
| 425 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 426 | // This is the current response object from Omaha. |
| 427 | OmahaResponse response_; |
| 428 | |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 429 | // Whether P2P is being used for downloading and sharing. |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 430 | bool using_p2p_for_downloading_; |
Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 431 | bool using_p2p_for_sharing_; |
| 432 | |
| 433 | // Stores the P2P download URL, if one is used. |
| 434 | std::string p2p_url_; |
| 435 | |
| 436 | // The cached value of |kPrefsP2PFirstAttemptTimestamp|. |
| 437 | base::Time p2p_first_attempt_timestamp_; |
| 438 | |
| 439 | // The cached value of |kPrefsP2PNumAttempts|. |
| 440 | int p2p_num_attempts_; |
David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 441 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 442 | // This stores a "signature" of the current response. The signature here |
| 443 | // refers to a subset of the current response from Omaha. Each update to |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 444 | // this value is persisted so we resume from the same value in case of a |
| 445 | // process restart. |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 446 | std::string response_signature_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 447 | |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 448 | // The number of times we've tried to download the payload. This is |
| 449 | // incremented each time we download the payload successsfully or when we |
| 450 | // exhaust all failure limits for all URLs and are about to wrap around back |
| 451 | // to the first URL. Each update to this value is persisted so we resume from |
| 452 | // the same value in case of a process restart. |
| 453 | int payload_attempt_number_; |
| 454 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 455 | // The number of times we've tried to download the payload in full. This is |
| 456 | // incremented each time we download the payload in full successsfully or |
| 457 | // when we exhaust all failure limits for all URLs and are about to wrap |
| 458 | // around back to the first URL. Each update to this value is persisted so |
| 459 | // we resume from the same value in case of a process restart. |
Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 460 | int full_payload_attempt_number_; |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 461 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 462 | // The index of the current URL. This type is different from the one in the |
| 463 | // accessor methods because PrefsInterface supports only int64_t but we want |
| 464 | // to provide a stronger abstraction of uint32_t. Each update to this value |
| 465 | // is persisted so we resume from the same value in case of a process |
| 466 | // restart. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 467 | int64_t url_index_; |
| 468 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 469 | // The count of failures encountered in the current attempt to download using |
| 470 | // the current URL (specified by url_index_). Each update to this value is |
| 471 | // persisted so we resume from the same value in case of a process restart. |
| 472 | int64_t url_failure_count_; |
| 473 | |
David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 474 | // The number of times we've switched URLs. |
| 475 | int32_t url_switch_count_; |
| 476 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 477 | // The current download source based on the current URL. This value is |
| 478 | // not persisted as it can be recomputed everytime we update the URL. |
| 479 | // We're storing this so as not to recompute this on every few bytes of |
| 480 | // data we read from the socket. |
| 481 | DownloadSource current_download_source_; |
| 482 | |
David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 483 | // The number of different Omaha responses seen. Increases every time |
| 484 | // a new response is seen. Resets to 0 only when the system has been |
| 485 | // successfully updated. |
| 486 | int num_responses_seen_; |
| 487 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 488 | // The number of system reboots during an update attempt. Technically since |
| 489 | // we don't go out of our way to not update it when not attempting an update, |
| 490 | // also records the number of reboots before the next update attempt starts. |
| 491 | uint32_t num_reboots_; |
| 492 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 493 | // The timestamp until which we've to wait before attempting to download the |
| 494 | // payload again, so as to backoff repeated downloads. |
| 495 | base::Time backoff_expiry_time_; |
| 496 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 497 | // The most recently calculated value of the update duration. |
| 498 | base::TimeDelta update_duration_current_; |
| 499 | |
| 500 | // The point in time (wall-clock) that the update was started. |
| 501 | base::Time update_timestamp_start_; |
| 502 | |
| 503 | // The point in time (wall-clock) that the update ended. If the update |
| 504 | // is still in progress, this is set to the Epoch (e.g. 0). |
| 505 | base::Time update_timestamp_end_; |
| 506 | |
| 507 | // The update duration uptime |
| 508 | base::TimeDelta update_duration_uptime_; |
| 509 | |
| 510 | // The monotonic time when |update_duration_uptime_| was last set |
| 511 | base::Time update_duration_uptime_timestamp_; |
| 512 | |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 513 | // The number of bytes that have been downloaded for each source for each new |
| 514 | // update attempt. If we resume an update, we'll continue from the previous |
| 515 | // value, but if we get a new response or if the previous attempt failed, |
| 516 | // we'll reset this to 0 to start afresh. Each update to this value is |
| 517 | // persisted so we resume from the same value in case of a process restart. |
| 518 | // The extra index in the array is to no-op accidental access in case the |
| 519 | // return value from GetCurrentDownloadSource is used without validation. |
| 520 | uint64_t current_bytes_downloaded_[kNumDownloadSources + 1]; |
| 521 | |
| 522 | // The number of bytes that have been downloaded for each source since the |
| 523 | // the last successful update. This is used to compute the overhead we incur. |
| 524 | // Each update to this value is persisted so we resume from the same value in |
| 525 | // case of a process restart. |
| 526 | // The extra index in the array is to no-op accidental access in case the |
| 527 | // return value from GetCurrentDownloadSource is used without validation. |
| 528 | uint64_t total_bytes_downloaded_[kNumDownloadSources + 1]; |
| 529 | |
David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 530 | // A small timespan used when comparing wall-clock times for coping |
| 531 | // with the fact that clocks drift and consequently are adjusted |
| 532 | // (either forwards or backwards) via NTP. |
| 533 | static const base::TimeDelta kDurationSlack; |
| 534 | |
Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 535 | // The ordered list of the subset of payload URL candidates which are |
| 536 | // allowed as per device policy. |
| 537 | std::vector<std::string> candidate_urls_; |
| 538 | |
Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 539 | // This stores a blacklisted version set as part of rollback. When we rollback |
| 540 | // we store the version of the os from which we are rolling back from in order |
| 541 | // to guarantee that we do not re-update to it on the next au attempt after |
| 542 | // reboot. |
| 543 | std::string rollback_version_; |
| 544 | |
David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 545 | // The number of bytes downloaded per attempt. |
| 546 | int64_t attempt_num_bytes_downloaded_; |
| 547 | |
| 548 | // The boot time when the attempt was started. |
| 549 | base::Time attempt_start_time_boot_; |
| 550 | |
| 551 | // The monotonic time when the attempt was started. |
| 552 | base::Time attempt_start_time_monotonic_; |
| 553 | |
David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 554 | // The connection type when the attempt started. |
| 555 | metrics::ConnectionType attempt_connection_type_; |
| 556 | |
David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 557 | // Whether we're currently rolling back. |
| 558 | AttemptType attempt_type_; |
| 559 | |
Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 560 | // The current scattering wallclock-based wait period. |
| 561 | base::TimeDelta scattering_wait_period_; |
| 562 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 563 | DISALLOW_COPY_AND_ASSIGN(PayloadState); |
| 564 | }; |
| 565 | |
| 566 | } // namespace chromeos_update_engine |
| 567 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 568 | #endif // UPDATE_ENGINE_PAYLOAD_STATE_H_ |