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