blob: 43fe5cc23577ccbad8ffe4b5eba4d363e40f3bdf [file] [log] [blame]
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__
7
Jay Srinivasan08262882012-12-28 19:29:43 -08008#include <base/time.h>
9
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080010#include "update_engine/payload_state_interface.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080011#include "update_engine/prefs_interface.h"
12
13namespace chromeos_update_engine {
14
Jay Srinivasan19409b72013-04-12 19:23:36 -070015class SystemState;
16
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080017// Encapsulates all the payload state required for download. This includes the
Jay Srinivasan08262882012-12-28 19:29:43 -080018// state necessary for handling multiple URLs in Omaha response, the backoff
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080019// state, etc. All state is persisted so that we use the most recently saved
20// value when resuming the update_engine process. All state is also cached in
21// memory so that we ensure we always make progress based on last known good
22// state even when there's any issue in reading/writing from the file system.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080023class PayloadState : public PayloadStateInterface {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080024 public:
Jay Srinivasan19409b72013-04-12 19:23:36 -070025 PayloadState();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080026 virtual ~PayloadState() {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080027
Jay Srinivasan19409b72013-04-12 19:23:36 -070028 // Initializes a payload state object using the given global system state.
29 // It performs the initial loading of all persisted state into memory and
30 // dumps the initial state for debugging purposes. Note: the other methods
31 // should be called only after calling Initialize on this object.
32 bool Initialize(SystemState* system_state);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080033
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080034 // Implementation of PayloadStateInterface methods.
35 virtual void SetResponse(const OmahaResponse& response);
36 virtual void DownloadComplete();
37 virtual void DownloadProgress(size_t count);
Chris Sosabe45bef2013-04-09 18:25:12 -070038 virtual void UpdateResumed();
Jay Srinivasan19409b72013-04-12 19:23:36 -070039 virtual void UpdateRestarted();
David Zeuthen9a017f22013-04-11 16:10:26 -070040 virtual void UpdateSucceeded();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080041 virtual void UpdateFailed(ActionExitCode error);
Jay Srinivasan08262882012-12-28 19:29:43 -080042 virtual bool ShouldBackoffDownload();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080043
Jay Srinivasan08262882012-12-28 19:29:43 -080044 virtual inline std::string GetResponseSignature() {
45 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080046 }
47
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080048 virtual inline uint32_t GetPayloadAttemptNumber() {
49 return payload_attempt_number_;
50 }
51
52 virtual inline uint32_t GetUrlIndex() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080053 return url_index_;
54 }
55
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080056 virtual inline uint32_t GetUrlFailureCount() {
57 return url_failure_count_;
58 }
59
David Zeuthencc6f9962013-04-18 11:57:24 -070060 virtual inline uint32_t GetUrlSwitchCount() {
61 return url_switch_count_;
62 }
63
Jay Srinivasan08262882012-12-28 19:29:43 -080064 virtual inline base::Time GetBackoffExpiryTime() {
65 return backoff_expiry_time_;
66 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080067
David Zeuthen9a017f22013-04-11 16:10:26 -070068 virtual base::TimeDelta GetUpdateDuration();
69
70 virtual base::TimeDelta GetUpdateDurationUptime();
71
Jay Srinivasan19409b72013-04-12 19:23:36 -070072 virtual inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) {
73 return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
74 }
75
76 virtual inline uint64_t GetTotalBytesDownloaded(DownloadSource source) {
77 return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
78 }
79
Chris Sosabe45bef2013-04-09 18:25:12 -070080 virtual inline uint32_t GetNumReboots() {
81 return num_reboots_;
82 }
83
Jay Srinivasan08262882012-12-28 19:29:43 -080084 private:
85 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080086 // at the time of the next update check.
87 void IncrementPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080088
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080089 // Advances the current URL index to the next available one. If all URLs have
90 // been exhausted during the current payload download attempt (as indicated
91 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -070092 // number and wrap around again with the first URL in the list. This also
93 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080094 void IncrementUrlIndex();
95
96 // Increments the failure count of the current URL. If the configured max
97 // failure count is reached for this URL, it advances the current URL index
98 // to the next URL and resets the failure count for that URL.
99 void IncrementFailureCount();
100
Jay Srinivasan08262882012-12-28 19:29:43 -0800101 // Updates the backoff expiry time exponentially based on the current
102 // payload attempt number.
103 void UpdateBackoffExpiryTime();
104
Jay Srinivasan19409b72013-04-12 19:23:36 -0700105 // Updates the value of current download source based on the current URL
106 // index. If the download source is not one of the known sources, it's set
107 // to kNumDownloadSources.
108 void UpdateCurrentDownloadSource();
109
110 // Updates the various metrics corresponding with the given number of bytes
111 // that were downloaded recently.
112 void UpdateBytesDownloaded(size_t count);
113
114 // Reports the various metrics related to the number of bytes downloaded.
115 void ReportBytesDownloadedMetrics();
116
David Zeuthencc6f9962013-04-18 11:57:24 -0700117 // Reports the metric related to number of URL switches.
118 void ReportUpdateUrlSwitchesMetric();
119
Chris Sosabe45bef2013-04-09 18:25:12 -0700120 // Reports the various metrics related to rebooting during an update.
121 void ReportRebootMetrics();
122
Jay Srinivasan08262882012-12-28 19:29:43 -0800123 // Resets all the persisted state values which are maintained relative to the
124 // current response signature. The response signature itself is not reset.
125 void ResetPersistedState();
126
Jay Srinivasan19409b72013-04-12 19:23:36 -0700127 // Resets the appropriate state related to download sources that need to be
128 // reset on a new update.
129 void ResetDownloadSourcesOnNewUpdate();
130
131 // Returns the persisted value for the given key. It also validates that
132 // the value returned is non-negative.
133 int64_t GetPersistedValue(const std::string& key);
134
Jay Srinivasan08262882012-12-28 19:29:43 -0800135 // Calculates the response "signature", which is basically a string composed
136 // of the subset of the fields in the current response that affect the
137 // behavior of the PayloadState.
138 std::string CalculateResponseSignature();
139
140 // Initializes the current response signature from the persisted state.
141 void LoadResponseSignature();
142
143 // Sets the response signature to the given value. Also persists the value
144 // being set so that we resume from the save value in case of a process
145 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700146 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800147
148 // Initializes the payload attempt number from the persisted state.
149 void LoadPayloadAttemptNumber();
150
151 // Sets the payload attempt number to the given value. Also persists the
152 // value being set so that we resume from the same value in case of a process
153 // restart.
154 void SetPayloadAttemptNumber(uint32_t payload_attempt_number);
155
156 // Initializes the current URL index from the persisted state.
157 void LoadUrlIndex();
158
159 // Sets the current URL index to the given value. Also persists the value
160 // being set so that we resume from the same value in case of a process
161 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800162 void SetUrlIndex(uint32_t url_index);
163
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800164 // Initializes the current URL's failure count from the persisted stae.
165 void LoadUrlFailureCount();
166
167 // Sets the current URL's failure count to the given value. Also persists the
168 // value being set so that we resume from the same value in case of a process
169 // restart.
170 void SetUrlFailureCount(uint32_t url_failure_count);
171
David Zeuthencc6f9962013-04-18 11:57:24 -0700172 // Sets |url_switch_count_| to the given value and persists the value.
173 void SetUrlSwitchCount(uint32_t url_switch_count);
174
175 // Initializes |url_switch_count_| from the persisted stae.
176 void LoadUrlSwitchCount();
177
Jay Srinivasan08262882012-12-28 19:29:43 -0800178 // Initializes the backoff expiry time from the persisted state.
179 void LoadBackoffExpiryTime();
180
181 // Sets the backoff expiry time to the given value. Also persists the value
182 // being set so that we resume from the same value in case of a process
183 // restart.
184 void SetBackoffExpiryTime(const base::Time& new_time);
185
David Zeuthen9a017f22013-04-11 16:10:26 -0700186 // Initializes |update_timestamp_start_| from the persisted state.
187 void LoadUpdateTimestampStart();
188
189 // Sets |update_timestamp_start_| to the given value and persists the value.
190 void SetUpdateTimestampStart(const base::Time& value);
191
192 // Sets |update_timestamp_end_| to the given value. This is not persisted
193 // as it happens at the end of the update process where state is deleted
194 // anyway.
195 void SetUpdateTimestampEnd(const base::Time& value);
196
197 // Initializes |update_duration_uptime_| from the persisted state.
198 void LoadUpdateDurationUptime();
199
200 // Helper method used in SetUpdateDurationUptime() and
201 // CalculateUpdateDurationUptime().
202 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
203 const base::Time& timestamp,
204 bool use_logging);
205
206 // Sets |update_duration_uptime_| to the given value and persists
207 // the value and sets |update_duration_uptime_timestamp_| to the
208 // current monotonic time.
209 void SetUpdateDurationUptime(const base::TimeDelta& value);
210
211 // Adds the difference between current monotonic time and
212 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
213 // sets |update_duration_uptime_timestamp_| to current monotonic time.
214 void CalculateUpdateDurationUptime();
215
Jay Srinivasan19409b72013-04-12 19:23:36 -0700216 // Returns the full key for a download source given the prefix.
217 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
218
219 // Loads the number of bytes that have been currently downloaded through the
220 // previous attempts from the persisted state for the given source. It's
221 // reset to 0 everytime we begin a full update and is continued from previous
222 // attempt if we're resuming the update.
223 void LoadCurrentBytesDownloaded(DownloadSource source);
224
225 // Sets the number of bytes that have been currently downloaded for the
226 // given source. This value is also persisted.
227 void SetCurrentBytesDownloaded(DownloadSource source,
228 uint64_t current_bytes_downloaded,
229 bool log);
230
231 // Loads the total number of bytes that have been downloaded (since the last
232 // successful update) from the persisted state for the given source. It's
233 // reset to 0 everytime we successfully apply an update and counts the bytes
234 // downloaded for both successful and failed attempts since then.
235 void LoadTotalBytesDownloaded(DownloadSource source);
236
237 // Sets the total number of bytes that have been downloaded so far for the
238 // given source. This value is also persisted.
239 void SetTotalBytesDownloaded(DownloadSource source,
240 uint64_t total_bytes_downloaded,
241 bool log);
242
243 // The global state of the system.
244 SystemState* system_state_;
245
Chris Sosabe45bef2013-04-09 18:25:12 -0700246 // Initializes |num_reboots_| from the persisted state.
247 void LoadNumReboots();
248
249 // Sets |num_reboots| for the update attempt. Also persists the
250 // value being set so that we resume from the same value in case of a process
251 // restart.
252 void SetNumReboots(uint32_t num_reboots);
253
254 // Checks to see if the device rebooted since the last call and if so
255 // increments num_reboots.
256 void UpdateNumReboots();
257
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800258 // Interface object with which we read/write persisted state. This must
259 // be set by calling the Initialize method before calling any other method.
260 PrefsInterface* prefs_;
261
Jay Srinivasan08262882012-12-28 19:29:43 -0800262 // This is the current response object from Omaha.
263 OmahaResponse response_;
264
265 // This stores a "signature" of the current response. The signature here
266 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800267 // this value is persisted so we resume from the same value in case of a
268 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800269 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800270
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800271 // The number of times we've tried to download the payload in full. This is
272 // incremented each time we download the payload in full successsfully or
273 // when we exhaust all failure limits for all URLs and are about to wrap
274 // around back to the first URL. Each update to this value is persisted so
275 // we resume from the same value in case of a process restart.
276 uint32_t payload_attempt_number_;
277
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800278 // The index of the current URL. This type is different from the one in the
279 // accessor methods because PrefsInterface supports only int64_t but we want
280 // to provide a stronger abstraction of uint32_t. Each update to this value
281 // is persisted so we resume from the same value in case of a process
282 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800283 int64_t url_index_;
284
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800285 // The count of failures encountered in the current attempt to download using
286 // the current URL (specified by url_index_). Each update to this value is
287 // persisted so we resume from the same value in case of a process restart.
288 int64_t url_failure_count_;
289
David Zeuthencc6f9962013-04-18 11:57:24 -0700290 // The number of times we've switched URLs.
291 int32_t url_switch_count_;
292
Jay Srinivasan19409b72013-04-12 19:23:36 -0700293 // The current download source based on the current URL. This value is
294 // not persisted as it can be recomputed everytime we update the URL.
295 // We're storing this so as not to recompute this on every few bytes of
296 // data we read from the socket.
297 DownloadSource current_download_source_;
298
Chris Sosabe45bef2013-04-09 18:25:12 -0700299 // The number of system reboots during an update attempt. Technically since
300 // we don't go out of our way to not update it when not attempting an update,
301 // also records the number of reboots before the next update attempt starts.
302 uint32_t num_reboots_;
303
Jay Srinivasan08262882012-12-28 19:29:43 -0800304 // The timestamp until which we've to wait before attempting to download the
305 // payload again, so as to backoff repeated downloads.
306 base::Time backoff_expiry_time_;
307
David Zeuthen9a017f22013-04-11 16:10:26 -0700308 // The most recently calculated value of the update duration.
309 base::TimeDelta update_duration_current_;
310
311 // The point in time (wall-clock) that the update was started.
312 base::Time update_timestamp_start_;
313
314 // The point in time (wall-clock) that the update ended. If the update
315 // is still in progress, this is set to the Epoch (e.g. 0).
316 base::Time update_timestamp_end_;
317
318 // The update duration uptime
319 base::TimeDelta update_duration_uptime_;
320
321 // The monotonic time when |update_duration_uptime_| was last set
322 base::Time update_duration_uptime_timestamp_;
323
Jay Srinivasan19409b72013-04-12 19:23:36 -0700324 // The number of bytes that have been downloaded for each source for each new
325 // update attempt. If we resume an update, we'll continue from the previous
326 // value, but if we get a new response or if the previous attempt failed,
327 // we'll reset this to 0 to start afresh. Each update to this value is
328 // persisted so we resume from the same value in case of a process restart.
329 // The extra index in the array is to no-op accidental access in case the
330 // return value from GetCurrentDownloadSource is used without validation.
331 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
332
333 // The number of bytes that have been downloaded for each source since the
334 // the last successful update. This is used to compute the overhead we incur.
335 // Each update to this value is persisted so we resume from the same value in
336 // case of a process restart.
337 // The extra index in the array is to no-op accidental access in case the
338 // return value from GetCurrentDownloadSource is used without validation.
339 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
340
Jay Srinivasan08262882012-12-28 19:29:43 -0800341 // Returns the number of URLs in the current response.
342 // Note: This value will be 0 if this method is called before we receive
343 // the first valid Omaha response in this process.
344 uint32_t GetNumUrls() {
345 return response_.payload_urls.size();
346 }
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800347
David Zeuthen9a017f22013-04-11 16:10:26 -0700348 // A small timespan used when comparing wall-clock times for coping
349 // with the fact that clocks drift and consequently are adjusted
350 // (either forwards or backwards) via NTP.
351 static const base::TimeDelta kDurationSlack;
352
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800353 DISALLOW_COPY_AND_ASSIGN(PayloadState);
354};
355
356} // namespace chromeos_update_engine
357
358#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__