blob: c4651e919491d339ddd2a78735b9d327e2a32ca5 [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();
David Zeuthena99981f2013-04-29 13:42:47 -070041 virtual void UpdateFailed(ErrorCode error);
Jay Srinivasan08262882012-12-28 19:29:43 -080042 virtual bool ShouldBackoffDownload();
Chris Sosaaa18e162013-06-20 13:20:30 -070043 virtual void Rollback();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080044
Jay Srinivasan08262882012-12-28 19:29:43 -080045 virtual inline std::string GetResponseSignature() {
46 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080047 }
48
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080049 virtual inline uint32_t GetPayloadAttemptNumber() {
50 return payload_attempt_number_;
51 }
52
Jay Srinivasan53173b92013-05-17 17:13:01 -070053 virtual inline std::string GetCurrentUrl() {
54 return candidate_urls_.size() ? candidate_urls_[url_index_] : "";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080055 }
56
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080057 virtual inline uint32_t GetUrlFailureCount() {
58 return url_failure_count_;
59 }
60
David Zeuthencc6f9962013-04-18 11:57:24 -070061 virtual inline uint32_t GetUrlSwitchCount() {
62 return url_switch_count_;
63 }
64
David Zeuthena573d6f2013-06-14 16:13:36 -070065 virtual inline int GetNumResponsesSeen() {
66 return num_responses_seen_;
67 }
68
Jay Srinivasan08262882012-12-28 19:29:43 -080069 virtual inline base::Time GetBackoffExpiryTime() {
70 return backoff_expiry_time_;
71 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080072
David Zeuthen9a017f22013-04-11 16:10:26 -070073 virtual base::TimeDelta GetUpdateDuration();
74
75 virtual base::TimeDelta GetUpdateDurationUptime();
76
Jay Srinivasan19409b72013-04-12 19:23:36 -070077 virtual inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) {
78 return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
79 }
80
81 virtual inline uint64_t GetTotalBytesDownloaded(DownloadSource source) {
82 return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
83 }
84
Chris Sosabe45bef2013-04-09 18:25:12 -070085 virtual inline uint32_t GetNumReboots() {
86 return num_reboots_;
87 }
88
David Zeuthene4c58bf2013-06-18 17:26:50 -070089 virtual void UpdateEngineStarted();
90
Chris Sosaaa18e162013-06-20 13:20:30 -070091 virtual inline std::string GetRollbackVersion() {
92 return rollback_version_;
93 }
94
Jay Srinivasan08262882012-12-28 19:29:43 -080095 private:
96 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080097 // at the time of the next update check.
98 void IncrementPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080099
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800100 // Advances the current URL index to the next available one. If all URLs have
101 // been exhausted during the current payload download attempt (as indicated
102 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -0700103 // number and wrap around again with the first URL in the list. This also
104 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800105 void IncrementUrlIndex();
106
107 // Increments the failure count of the current URL. If the configured max
108 // failure count is reached for this URL, it advances the current URL index
109 // to the next URL and resets the failure count for that URL.
110 void IncrementFailureCount();
111
Jay Srinivasan08262882012-12-28 19:29:43 -0800112 // Updates the backoff expiry time exponentially based on the current
113 // payload attempt number.
114 void UpdateBackoffExpiryTime();
115
Jay Srinivasan19409b72013-04-12 19:23:36 -0700116 // Updates the value of current download source based on the current URL
117 // index. If the download source is not one of the known sources, it's set
118 // to kNumDownloadSources.
119 void UpdateCurrentDownloadSource();
120
121 // Updates the various metrics corresponding with the given number of bytes
122 // that were downloaded recently.
123 void UpdateBytesDownloaded(size_t count);
124
125 // Reports the various metrics related to the number of bytes downloaded.
126 void ReportBytesDownloadedMetrics();
127
David Zeuthencc6f9962013-04-18 11:57:24 -0700128 // Reports the metric related to number of URL switches.
129 void ReportUpdateUrlSwitchesMetric();
130
Chris Sosabe45bef2013-04-09 18:25:12 -0700131 // Reports the various metrics related to rebooting during an update.
132 void ReportRebootMetrics();
133
David Zeuthen674c3182013-04-18 14:05:20 -0700134 // Reports the various metrics related to update duration.
135 void ReportDurationMetrics();
136
Alex Deymo1c656c42013-06-28 11:02:14 -0700137 // Reports the metric related to the applied payload type.
138 void ReportPayloadTypeMetric();
139
Jay Srinivasan08262882012-12-28 19:29:43 -0800140 // Resets all the persisted state values which are maintained relative to the
141 // current response signature. The response signature itself is not reset.
142 void ResetPersistedState();
143
Jay Srinivasan19409b72013-04-12 19:23:36 -0700144 // Resets the appropriate state related to download sources that need to be
145 // reset on a new update.
146 void ResetDownloadSourcesOnNewUpdate();
147
148 // Returns the persisted value for the given key. It also validates that
Chris Sosaaa18e162013-06-20 13:20:30 -0700149 // the value returned is non-negative. If |across_powerwash| is True,
150 // get the value that will persist across a powerwash.
151 int64_t GetPersistedValue(const std::string& key, bool across_powerwash);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700152
Jay Srinivasan08262882012-12-28 19:29:43 -0800153 // Calculates the response "signature", which is basically a string composed
154 // of the subset of the fields in the current response that affect the
155 // behavior of the PayloadState.
156 std::string CalculateResponseSignature();
157
158 // Initializes the current response signature from the persisted state.
159 void LoadResponseSignature();
160
161 // Sets the response signature to the given value. Also persists the value
162 // being set so that we resume from the save value in case of a process
163 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700164 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800165
166 // Initializes the payload attempt number from the persisted state.
167 void LoadPayloadAttemptNumber();
168
169 // Sets the payload attempt number to the given value. Also persists the
170 // value being set so that we resume from the same value in case of a process
171 // restart.
172 void SetPayloadAttemptNumber(uint32_t payload_attempt_number);
173
174 // Initializes the current URL index from the persisted state.
175 void LoadUrlIndex();
176
177 // Sets the current URL index to the given value. Also persists the value
178 // being set so that we resume from the same value in case of a process
179 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800180 void SetUrlIndex(uint32_t url_index);
181
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800182 // Initializes the current URL's failure count from the persisted stae.
183 void LoadUrlFailureCount();
184
185 // Sets the current URL's failure count to the given value. Also persists the
186 // value being set so that we resume from the same value in case of a process
187 // restart.
188 void SetUrlFailureCount(uint32_t url_failure_count);
189
David Zeuthencc6f9962013-04-18 11:57:24 -0700190 // Sets |url_switch_count_| to the given value and persists the value.
191 void SetUrlSwitchCount(uint32_t url_switch_count);
192
193 // Initializes |url_switch_count_| from the persisted stae.
194 void LoadUrlSwitchCount();
195
Jay Srinivasan08262882012-12-28 19:29:43 -0800196 // Initializes the backoff expiry time from the persisted state.
197 void LoadBackoffExpiryTime();
198
199 // Sets the backoff expiry time to the given value. Also persists the value
200 // being set so that we resume from the same value in case of a process
201 // restart.
202 void SetBackoffExpiryTime(const base::Time& new_time);
203
David Zeuthen9a017f22013-04-11 16:10:26 -0700204 // Initializes |update_timestamp_start_| from the persisted state.
205 void LoadUpdateTimestampStart();
206
207 // Sets |update_timestamp_start_| to the given value and persists the value.
208 void SetUpdateTimestampStart(const base::Time& value);
209
210 // Sets |update_timestamp_end_| to the given value. This is not persisted
211 // as it happens at the end of the update process where state is deleted
212 // anyway.
213 void SetUpdateTimestampEnd(const base::Time& value);
214
215 // Initializes |update_duration_uptime_| from the persisted state.
216 void LoadUpdateDurationUptime();
217
218 // Helper method used in SetUpdateDurationUptime() and
219 // CalculateUpdateDurationUptime().
220 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
221 const base::Time& timestamp,
222 bool use_logging);
223
224 // Sets |update_duration_uptime_| to the given value and persists
225 // the value and sets |update_duration_uptime_timestamp_| to the
226 // current monotonic time.
227 void SetUpdateDurationUptime(const base::TimeDelta& value);
228
229 // Adds the difference between current monotonic time and
230 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
231 // sets |update_duration_uptime_timestamp_| to current monotonic time.
232 void CalculateUpdateDurationUptime();
233
Jay Srinivasan19409b72013-04-12 19:23:36 -0700234 // Returns the full key for a download source given the prefix.
235 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
236
237 // Loads the number of bytes that have been currently downloaded through the
238 // previous attempts from the persisted state for the given source. It's
239 // reset to 0 everytime we begin a full update and is continued from previous
240 // attempt if we're resuming the update.
241 void LoadCurrentBytesDownloaded(DownloadSource source);
242
243 // Sets the number of bytes that have been currently downloaded for the
244 // given source. This value is also persisted.
245 void SetCurrentBytesDownloaded(DownloadSource source,
246 uint64_t current_bytes_downloaded,
247 bool log);
248
249 // Loads the total number of bytes that have been downloaded (since the last
250 // successful update) from the persisted state for the given source. It's
251 // reset to 0 everytime we successfully apply an update and counts the bytes
252 // downloaded for both successful and failed attempts since then.
253 void LoadTotalBytesDownloaded(DownloadSource source);
254
255 // Sets the total number of bytes that have been downloaded so far for the
256 // given source. This value is also persisted.
257 void SetTotalBytesDownloaded(DownloadSource source,
258 uint64_t total_bytes_downloaded,
259 bool log);
260
Chris Sosaaa18e162013-06-20 13:20:30 -0700261 // Loads the blacklisted version from our prefs file.
262 void LoadRollbackVersion();
263
264 // Blacklists this version from getting AU'd to until we receive a new update
265 // response.
266 void SetRollbackVersion(const std::string& rollback_version);
267
268 // Clears any blacklisted version.
269 void ResetRollbackVersion();
270
Jay Srinivasan53173b92013-05-17 17:13:01 -0700271 inline uint32_t GetUrlIndex() {
272 return url_index_;
273 }
274
275 // Computes the list of candidate URLs from the total list of payload URLs in
276 // the Omaha response.
277 void ComputeCandidateUrls();
278
David Zeuthena573d6f2013-06-14 16:13:36 -0700279 // Sets |num_responses_seen_| and persist it to disk.
280 void SetNumResponsesSeen(int num_responses_seen);
281
282 // Initializes |num_responses_seen_| from persisted state.
283 void LoadNumResponsesSeen();
284
285 // Reports metric conveying how many times updates were abandoned
286 // before an update was applied.
287 void ReportUpdatesAbandonedCountMetric();
288
Jay Srinivasan19409b72013-04-12 19:23:36 -0700289 // The global state of the system.
290 SystemState* system_state_;
291
Chris Sosabe45bef2013-04-09 18:25:12 -0700292 // Initializes |num_reboots_| from the persisted state.
293 void LoadNumReboots();
294
295 // Sets |num_reboots| for the update attempt. Also persists the
296 // value being set so that we resume from the same value in case of a process
297 // restart.
298 void SetNumReboots(uint32_t num_reboots);
299
300 // Checks to see if the device rebooted since the last call and if so
301 // increments num_reboots.
302 void UpdateNumReboots();
303
David Zeuthene4c58bf2013-06-18 17:26:50 -0700304 // Writes the current wall-clock time to the kPrefsSystemUpdatedMarker
305 // state variable.
306 void CreateSystemUpdatedMarkerFile();
307
308 // Called at program startup if the device booted into a new update.
309 // The |time_to_reboot| parameter contains the (wall-clock) duration
310 // from when the update successfully completed (the value written
311 // into the kPrefsSystemUpdatedMarker state variable) until the device
312 // was booted into the update (current wall-clock time).
313 void BootedIntoUpdate(base::TimeDelta time_to_reboot);
314
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800315 // Interface object with which we read/write persisted state. This must
316 // be set by calling the Initialize method before calling any other method.
317 PrefsInterface* prefs_;
318
Chris Sosaaa18e162013-06-20 13:20:30 -0700319 // Interface object with which we read/write persisted state. This must
320 // be set by calling the Initialize method before calling any other method.
321 // This object persists across powerwashes.
322 PrefsInterface* powerwash_safe_prefs_;
323
Jay Srinivasan08262882012-12-28 19:29:43 -0800324 // This is the current response object from Omaha.
325 OmahaResponse response_;
326
327 // This stores a "signature" of the current response. The signature here
328 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800329 // this value is persisted so we resume from the same value in case of a
330 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800331 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800332
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800333 // The number of times we've tried to download the payload in full. This is
334 // incremented each time we download the payload in full successsfully or
335 // when we exhaust all failure limits for all URLs and are about to wrap
336 // around back to the first URL. Each update to this value is persisted so
337 // we resume from the same value in case of a process restart.
338 uint32_t payload_attempt_number_;
339
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800340 // The index of the current URL. This type is different from the one in the
341 // accessor methods because PrefsInterface supports only int64_t but we want
342 // to provide a stronger abstraction of uint32_t. Each update to this value
343 // is persisted so we resume from the same value in case of a process
344 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800345 int64_t url_index_;
346
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800347 // The count of failures encountered in the current attempt to download using
348 // the current URL (specified by url_index_). Each update to this value is
349 // persisted so we resume from the same value in case of a process restart.
350 int64_t url_failure_count_;
351
David Zeuthencc6f9962013-04-18 11:57:24 -0700352 // The number of times we've switched URLs.
353 int32_t url_switch_count_;
354
Jay Srinivasan19409b72013-04-12 19:23:36 -0700355 // The current download source based on the current URL. This value is
356 // not persisted as it can be recomputed everytime we update the URL.
357 // We're storing this so as not to recompute this on every few bytes of
358 // data we read from the socket.
359 DownloadSource current_download_source_;
360
David Zeuthena573d6f2013-06-14 16:13:36 -0700361 // The number of different Omaha responses seen. Increases every time
362 // a new response is seen. Resets to 0 only when the system has been
363 // successfully updated.
364 int num_responses_seen_;
365
Chris Sosabe45bef2013-04-09 18:25:12 -0700366 // The number of system reboots during an update attempt. Technically since
367 // we don't go out of our way to not update it when not attempting an update,
368 // also records the number of reboots before the next update attempt starts.
369 uint32_t num_reboots_;
370
Jay Srinivasan08262882012-12-28 19:29:43 -0800371 // The timestamp until which we've to wait before attempting to download the
372 // payload again, so as to backoff repeated downloads.
373 base::Time backoff_expiry_time_;
374
David Zeuthen9a017f22013-04-11 16:10:26 -0700375 // The most recently calculated value of the update duration.
376 base::TimeDelta update_duration_current_;
377
378 // The point in time (wall-clock) that the update was started.
379 base::Time update_timestamp_start_;
380
381 // The point in time (wall-clock) that the update ended. If the update
382 // is still in progress, this is set to the Epoch (e.g. 0).
383 base::Time update_timestamp_end_;
384
385 // The update duration uptime
386 base::TimeDelta update_duration_uptime_;
387
388 // The monotonic time when |update_duration_uptime_| was last set
389 base::Time update_duration_uptime_timestamp_;
390
Jay Srinivasan19409b72013-04-12 19:23:36 -0700391 // The number of bytes that have been downloaded for each source for each new
392 // update attempt. If we resume an update, we'll continue from the previous
393 // value, but if we get a new response or if the previous attempt failed,
394 // we'll reset this to 0 to start afresh. Each update to this value is
395 // persisted so we resume from the same value in case of a process restart.
396 // The extra index in the array is to no-op accidental access in case the
397 // return value from GetCurrentDownloadSource is used without validation.
398 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
399
400 // The number of bytes that have been downloaded for each source since the
401 // the last successful update. This is used to compute the overhead we incur.
402 // Each update to this value is persisted so we resume from the same value in
403 // case of a process restart.
404 // The extra index in the array is to no-op accidental access in case the
405 // return value from GetCurrentDownloadSource is used without validation.
406 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
407
David Zeuthen9a017f22013-04-11 16:10:26 -0700408 // A small timespan used when comparing wall-clock times for coping
409 // with the fact that clocks drift and consequently are adjusted
410 // (either forwards or backwards) via NTP.
411 static const base::TimeDelta kDurationSlack;
412
Jay Srinivasan53173b92013-05-17 17:13:01 -0700413 // The ordered list of the subset of payload URL candidates which are
414 // allowed as per device policy.
415 std::vector<std::string> candidate_urls_;
416
Chris Sosaaa18e162013-06-20 13:20:30 -0700417 // This stores a blacklisted version set as part of rollback. When we rollback
418 // we store the version of the os from which we are rolling back from in order
419 // to guarantee that we do not re-update to it on the next au attempt after
420 // reboot.
421 std::string rollback_version_;
422
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800423 DISALLOW_COPY_AND_ASSIGN(PayloadState);
424};
425
426} // namespace chromeos_update_engine
427
428#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__