diff options
author | Tianjie Xu <xunchang@google.com> | 2017-10-10 17:39:03 -0700 |
---|---|---|
committer | Tianjie Xu <xunchang@google.com> | 2017-10-14 22:44:35 +0000 |
commit | 90aaa109e38f147ec4bfc772439d9949f1b237c0 (patch) | |
tree | 1a12f29bdb847a384dfacd3cce214bb3891d0ab8 /metrics_utils.cc | |
parent | 012aa5bc825f3efe810cc52864840ace6eae8275 (diff) |
Collect metrics for android update attempt
Report the update attempt/result metrics when an upate finishes;
and abnormally terminated updates/time to reboot when the device
reboots.
Bug: 30989466
Test: update_engine_unittest pass
Change-Id: Iea16b4e8003ae3dab5e9b7c65cf4b38d2219d203
Diffstat (limited to 'metrics_utils.cc')
-rw-r--r-- | metrics_utils.cc | 74 |
1 files changed, 73 insertions, 1 deletions
diff --git a/metrics_utils.cc b/metrics_utils.cc index 263bacd2..5cff2938 100644 --- a/metrics_utils.cc +++ b/metrics_utils.cc @@ -21,7 +21,8 @@ #include <base/time/time.h> #include "update_engine/common/clock_interface.h" -#include "update_engine/common/prefs_interface.h" +#include "update_engine/common/constants.h" +#include "update_engine/common/utils.h" #include "update_engine/system_state.h" using base::Time; @@ -305,5 +306,76 @@ bool MonotonicDurationHelper(SystemState* system_state, return ret; } +int64_t GetPersistedValue(const std::string& key, PrefsInterface* prefs) { + CHECK(prefs); + if (!prefs->Exists(key)) + return 0; + + int64_t stored_value; + if (!prefs->GetInt64(key, &stored_value)) + return 0; + + if (stored_value < 0) { + LOG(ERROR) << key << ": Invalid value (" << stored_value + << ") in persisted state. Defaulting to 0"; + return 0; + } + + return stored_value; +} + +void SetNumReboots(int64_t num_reboots, PrefsInterface* prefs) { + CHECK(prefs); + prefs->SetInt64(kPrefsNumReboots, num_reboots); + LOG(INFO) << "Number of Reboots during current update attempt = " + << num_reboots; +} + +void SetPayloadAttemptNumber(int64_t payload_attempt_number, + PrefsInterface* prefs) { + CHECK(prefs); + prefs->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number); + LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number; +} + +void SetSystemUpdatedMarker(ClockInterface* clock, PrefsInterface* prefs) { + CHECK(prefs); + CHECK(clock); + Time update_finish_time = clock->GetMonotonicTime(); + prefs->SetInt64(kPrefsSystemUpdatedMarker, + update_finish_time.ToInternalValue()); + LOG(INFO) << "Updated Marker = " << utils::ToString(update_finish_time); +} + +void SetUpdateTimestampStart(const Time& update_start_time, + PrefsInterface* prefs) { + CHECK(prefs); + prefs->SetInt64(kPrefsUpdateTimestampStart, + update_start_time.ToInternalValue()); + LOG(INFO) << "Update Timestamp Start = " + << utils::ToString(update_start_time); +} + +bool LoadAndReportTimeToReboot(MetricsReporterInterface* metrics_reporter, + PrefsInterface* prefs, + ClockInterface* clock) { + CHECK(prefs); + CHECK(clock); + int64_t stored_value = GetPersistedValue(kPrefsSystemUpdatedMarker, prefs); + if (stored_value == 0) + return false; + + Time system_updated_at = Time::FromInternalValue(stored_value); + base::TimeDelta time_to_reboot = + clock->GetMonotonicTime() - system_updated_at; + if (time_to_reboot.ToInternalValue() < 0) { + LOG(ERROR) << "time_to_reboot is negative - system_updated_at: " + << utils::ToString(system_updated_at); + return false; + } + metrics_reporter->ReportTimeToReboot(time_to_reboot.InMinutes()); + return true; +} + } // namespace metrics_utils } // namespace chromeos_update_engine |