blob: 2f79140a2cd8690697d90569e6dfaef74a836b6a [file] [log] [blame]
Alex Deymo38429cf2015-11-11 18:27:22 -08001//
2// Copyright (C) 2015 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#ifndef UPDATE_ENGINE_METRICS_UTILS_H_
18#define UPDATE_ENGINE_METRICS_UTILS_H_
19
Kelvin Zhang32a73a92022-12-19 12:27:41 -080020#include <chrono>
Tianjie Xu90aaa102017-10-10 17:39:03 -070021#include <string>
Kelvin Zhang32a73a92022-12-19 12:27:41 -080022#include <string_view>
23#include <type_traits>
24#include <utility>
Tianjie Xu90aaa102017-10-10 17:39:03 -070025
Tianjie Xu282aa1f2017-09-05 13:42:45 -070026#include <base/time/time.h>
27
Tianjie Xu90aaa102017-10-10 17:39:03 -070028#include "update_engine/common/clock_interface.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070029#include "update_engine/common/connection_utils.h"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070030#include "update_engine/common/error_code.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070031#include "update_engine/common/metrics_constants.h"
32#include "update_engine/common/metrics_reporter_interface.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070033#include "update_engine/common/prefs_interface.h"
Alex Deymo38429cf2015-11-11 18:27:22 -080034
35namespace chromeos_update_engine {
Alex Deymoa2591792015-11-17 00:39:40 -030036
Alex Deymo38429cf2015-11-11 18:27:22 -080037namespace metrics_utils {
38
39// Transforms a ErrorCode value into a metrics::DownloadErrorCode.
40// This obviously only works for errors related to downloading so if |code|
41// is e.g. |ErrorCode::kFilesystemCopierError| then
42// |kDownloadErrorCodeInputMalformed| is returned.
43metrics::DownloadErrorCode GetDownloadErrorCode(ErrorCode code);
44
45// Transforms a ErrorCode value into a metrics::AttemptResult.
46//
47// If metrics::AttemptResult::kPayloadDownloadError is returned, you
48// can use utils::GetDownloadError() to get more detail.
49metrics::AttemptResult GetAttemptResult(ErrorCode code);
50
51// Calculates the internet connection type given |type| and |tethering|.
Sen Jiang255e22b2016-05-20 16:15:29 -070052metrics::ConnectionType GetConnectionType(ConnectionType type,
53 ConnectionTethering tethering);
Alex Deymo38429cf2015-11-11 18:27:22 -080054
Tianjie Xu90aaa102017-10-10 17:39:03 -070055// Returns the persisted value from prefs for the given key. It also
56// validates that the value returned is non-negative.
Kelvin Zhang1c86a922021-05-13 10:30:48 -040057int64_t GetPersistedValue(std::string_view key, PrefsInterface* prefs);
Tianjie Xu90aaa102017-10-10 17:39:03 -070058
59// Persists the reboot count of the update attempt to |kPrefsNumReboots|.
60void SetNumReboots(int64_t num_reboots, PrefsInterface* prefs);
61
62// Persists the payload attempt number to |kPrefsPayloadAttemptNumber|.
63void SetPayloadAttemptNumber(int64_t payload_attempt_number,
64 PrefsInterface* prefs);
65
66// Persists the finished time of an update to the |kPrefsSystemUpdatedMarker|.
67void SetSystemUpdatedMarker(ClockInterface* clock, PrefsInterface* prefs);
68
Tianjie Xu2a0ea632018-08-06 12:59:23 -070069// Persists the start monotonic time of an update to
70// |kPrefsUpdateTimestampStart|.
Tianjie Xu90aaa102017-10-10 17:39:03 -070071void SetUpdateTimestampStart(const base::Time& update_start_time,
72 PrefsInterface* prefs);
73
Tianjie Xu2a0ea632018-08-06 12:59:23 -070074// Persists the start boot time of an update to
75// |kPrefsUpdateBootTimestampStart|.
76void SetUpdateBootTimestampStart(const base::Time& update_start_boot_time,
77 PrefsInterface* prefs);
78
Tianjie Xu90aaa102017-10-10 17:39:03 -070079// Called at program startup if the device booted into a new update.
80// The |time_to_reboot| parameter contains the (monotonic-clock) duration
81// from when the update successfully completed (the value in
82// |kPrefsSystemUpdatedMarker|) until the device was booted into the update
83// (current monotonic-clock time).
84bool LoadAndReportTimeToReboot(MetricsReporterInterface* metrics_reporter,
85 PrefsInterface* prefs,
86 ClockInterface* clock);
87
Kelvin Zhang32a73a92022-12-19 12:27:41 -080088template <typename T>
89class PersistedValue {
90 public:
91 PersistedValue(std::string_view key, PrefsInterface* prefs)
92 : key_(key), prefs_(prefs) {
93 val_ = metrics_utils::GetPersistedValue(key, prefs);
94 }
95 ~PersistedValue() { Flush(true); }
96 void Delete() {
97 val_ = {};
98 prefs_->Delete(key_);
99 }
100 T get() const { return val_; }
101 using clock = std::chrono::system_clock;
102 using time_point = clock::time_point;
103 // prefix increment
104 PersistedValue<T>& operator++() {
105 ++val_;
106 Flush();
107 return *this;
108 }
109 PersistedValue<T>& operator--() {
110 --val_;
111 Flush();
112 return *this;
113 }
114 PersistedValue<T>& operator+=(T&& t) {
115 val_ += std::forward<T>(t);
116 Flush();
117 return *this;
118 }
119 PersistedValue<T>& operator-=(T&& t) {
120 val_ -= std::forward<T>(t);
121 Flush();
122 return *this;
123 }
124 PersistedValue<T>& operator=(T&& t) {
125 val_ = std::forward<T>(t);
126 Flush();
127 return *this;
128 }
129 void Flush(bool force = false) {
130 auto now = clock::now();
131 if (now - last_save_ > metrics::kMetricFlushInterval || force) {
132 last_save_ = now;
133 if (std::is_integral_v<T>) {
134 prefs_->SetInt64(key_, val_);
135 } else if (std::is_same_v<T, bool>) {
136 prefs_->SetBoolean(key_, val_);
137 } else {
138 auto value = std::to_string(val_);
139 prefs_->SetString(key_, value);
140 }
141 }
142 }
143
144 private:
145 const std::string_view key_;
146 PrefsInterface* prefs_;
147 T val_;
148 time_point last_save_{};
149};
150
Alex Deymo38429cf2015-11-11 18:27:22 -0800151} // namespace metrics_utils
152} // namespace chromeos_update_engine
153
154#endif // UPDATE_ENGINE_METRICS_UTILS_H_