Move metrics time helpers to metrics_utils.
The metrics module reports metrics periodically, for which it needs to
keep track of the duration since some events (for example, the update
finished). These helpers were in the common/utils.h, but they rely on
the global SystemState to access both Prefs and Clock.
Since these helpers are specific to the metric reporting, this CL moves
them to the metrics_utils.h module.
Bug: 25197634
TEST=FEATURES=test emerge-link update_engine; mmma system/update_engine
Change-Id: Ia48091adbdc56c339c69c86c91c5c01aa58c54fb
diff --git a/metrics_utils.cc b/metrics_utils.cc
index 354d137..eb99c7d 100644
--- a/metrics_utils.cc
+++ b/metrics_utils.cc
@@ -16,6 +16,17 @@
#include "update_engine/metrics_utils.h"
+#include <string>
+
+#include <base/time/time.h>
+
+#include "update_engine/common/clock_interface.h"
+#include "update_engine/common/prefs_interface.h"
+#include "update_engine/system_state.h"
+
+using base::Time;
+using base::TimeDelta;
+
namespace chromeos_update_engine {
namespace metrics_utils {
@@ -244,5 +255,47 @@
return metrics::ConnectionType::kUnknown;
}
+bool WallclockDurationHelper(SystemState* system_state,
+ const std::string& state_variable_key,
+ TimeDelta* out_duration) {
+ bool ret = false;
+
+ Time now = system_state->clock()->GetWallclockTime();
+ int64_t stored_value;
+ if (system_state->prefs()->GetInt64(state_variable_key, &stored_value)) {
+ Time stored_time = Time::FromInternalValue(stored_value);
+ if (stored_time > now) {
+ LOG(ERROR) << "Stored time-stamp used for " << state_variable_key
+ << " is in the future.";
+ } else {
+ *out_duration = now - stored_time;
+ ret = true;
+ }
+ }
+
+ if (!system_state->prefs()->SetInt64(state_variable_key,
+ now.ToInternalValue())) {
+ LOG(ERROR) << "Error storing time-stamp in " << state_variable_key;
+ }
+
+ return ret;
+}
+
+bool MonotonicDurationHelper(SystemState* system_state,
+ int64_t* storage,
+ TimeDelta* out_duration) {
+ bool ret = false;
+
+ Time now = system_state->clock()->GetMonotonicTime();
+ if (*storage != 0) {
+ Time stored_time = Time::FromInternalValue(*storage);
+ *out_duration = now - stored_time;
+ ret = true;
+ }
+ *storage = now.ToInternalValue();
+
+ return ret;
+}
+
} // namespace metrics_utils
} // namespace chromeos_update_engine