1 // 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 20 #include <string> 21 22 #include <base/time/time.h> 23 24 #include "update_engine/common/clock_interface.h" 25 #include "update_engine/common/error_code.h" 26 #include "update_engine/common/prefs_interface.h" 27 #include "update_engine/connection_utils.h" 28 #include "update_engine/metrics_constants.h" 29 #include "update_engine/metrics_reporter_interface.h" 30 31 namespace chromeos_update_engine { 32 33 class SystemState; 34 35 namespace metrics_utils { 36 37 // Transforms a ErrorCode value into a metrics::DownloadErrorCode. 38 // This obviously only works for errors related to downloading so if |code| 39 // is e.g. |ErrorCode::kFilesystemCopierError| then 40 // |kDownloadErrorCodeInputMalformed| is returned. 41 metrics::DownloadErrorCode GetDownloadErrorCode(ErrorCode code); 42 43 // Transforms a ErrorCode value into a metrics::AttemptResult. 44 // 45 // If metrics::AttemptResult::kPayloadDownloadError is returned, you 46 // can use utils::GetDownloadError() to get more detail. 47 metrics::AttemptResult GetAttemptResult(ErrorCode code); 48 49 // Calculates the internet connection type given |type| and |tethering|. 50 metrics::ConnectionType GetConnectionType(ConnectionType type, 51 ConnectionTethering tethering); 52 53 // This function returns the duration on the wallclock since the last 54 // time it was called for the same |state_variable_key| value. 55 // 56 // If the function returns |true|, the duration (always non-negative) 57 // is returned in |out_duration|. If the function returns |false| 58 // something went wrong or there was no previous measurement. 59 bool WallclockDurationHelper(SystemState* system_state, 60 const std::string& state_variable_key, 61 base::TimeDelta* out_duration); 62 63 // This function returns the duration on the monotonic clock since the 64 // last time it was called for the same |storage| pointer. 65 // 66 // You should pass a pointer to a 64-bit integer in |storage| which 67 // should be initialized to 0. 68 // 69 // If the function returns |true|, the duration (always non-negative) 70 // is returned in |out_duration|. If the function returns |false| 71 // something went wrong or there was no previous measurement. 72 bool MonotonicDurationHelper(SystemState* system_state, 73 int64_t* storage, 74 base::TimeDelta* out_duration); 75 76 // Returns the persisted value from prefs for the given key. It also 77 // validates that the value returned is non-negative. 78 int64_t GetPersistedValue(const std::string& key, PrefsInterface* prefs); 79 80 // Persists the reboot count of the update attempt to |kPrefsNumReboots|. 81 void SetNumReboots(int64_t num_reboots, PrefsInterface* prefs); 82 83 // Persists the payload attempt number to |kPrefsPayloadAttemptNumber|. 84 void SetPayloadAttemptNumber(int64_t payload_attempt_number, 85 PrefsInterface* prefs); 86 87 // Persists the finished time of an update to the |kPrefsSystemUpdatedMarker|. 88 void SetSystemUpdatedMarker(ClockInterface* clock, PrefsInterface* prefs); 89 90 // Persists the start monotonic time of an update to 91 // |kPrefsUpdateTimestampStart|. 92 void SetUpdateTimestampStart(const base::Time& update_start_time, 93 PrefsInterface* prefs); 94 95 // Persists the start boot time of an update to 96 // |kPrefsUpdateBootTimestampStart|. 97 void SetUpdateBootTimestampStart(const base::Time& update_start_boot_time, 98 PrefsInterface* prefs); 99 100 // Called at program startup if the device booted into a new update. 101 // The |time_to_reboot| parameter contains the (monotonic-clock) duration 102 // from when the update successfully completed (the value in 103 // |kPrefsSystemUpdatedMarker|) until the device was booted into the update 104 // (current monotonic-clock time). 105 bool LoadAndReportTimeToReboot(MetricsReporterInterface* metrics_reporter, 106 PrefsInterface* prefs, 107 ClockInterface* clock); 108 109 } // namespace metrics_utils 110 } // namespace chromeos_update_engine 111 112 #endif // UPDATE_ENGINE_METRICS_UTILS_H_ 113