bootstat: Refactor init/utils/boot_clock into base/chrono_utils.
Use this for bootstat and init. This replaces the custom uptime parser in
bootstat.
This is a reland of aosp/338325 with a stubbed implementation for Darwin.
This change also has clang_format fixes (automatic).
Bug: 34352037
Test: chrono_utils_test
Change-Id: I72a62a3ca1ccfc0a4ccc6294ff1776c263144686
diff --git a/init/init.cpp b/init/init.cpp
index 6f3b3a6..e14034f 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -40,6 +40,7 @@
#include <selinux/label.h>
#include <selinux/android.h>
+#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
@@ -69,6 +70,7 @@
#include "util.h"
#include "watchdogd.h"
+using android::base::boot_clock;
using android::base::GetProperty;
using android::base::StringPrintf;
diff --git a/init/service.cpp b/init/service.cpp
index 3db34db..e89de9a 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -32,6 +32,7 @@
#include <selinux/selinux.h>
+#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/parseint.h>
#include <android-base/properties.h>
@@ -48,6 +49,7 @@
#include "property_service.h"
#include "util.h"
+using android::base::boot_clock;
using android::base::ParseInt;
using android::base::StringPrintf;
using android::base::WriteStringToFile;
diff --git a/init/service.h b/init/service.h
index f08a03f..d84ce02 100644
--- a/init/service.h
+++ b/init/service.h
@@ -26,6 +26,8 @@
#include <string>
#include <vector>
+#include <android-base/chrono_utils.h>
+
#include "action.h"
#include "capabilities.h"
#include "descriptors.h"
@@ -144,8 +146,8 @@
unsigned flags_;
pid_t pid_;
- boot_clock::time_point time_started_; // time of last start
- boot_clock::time_point time_crashed_; // first crash within inspection window
+ android::base::boot_clock::time_point time_started_; // time of last start
+ android::base::boot_clock::time_point time_crashed_; // first crash within inspection window
int crash_count_; // number of times crashed within window
uid_t uid_;
diff --git a/init/util.cpp b/init/util.cpp
index 8a19939..14fd7ab 100644
--- a/init/util.cpp
+++ b/init/util.cpp
@@ -52,6 +52,8 @@
#include "reboot.h"
#include "util.h"
+using android::base::boot_clock;
+
static unsigned int do_decode_uid(const char *s)
{
unsigned int v;
@@ -198,13 +200,6 @@
return success;
}
-boot_clock::time_point boot_clock::now() {
- timespec ts;
- clock_gettime(CLOCK_BOOTTIME, &ts);
- return boot_clock::time_point(std::chrono::seconds(ts.tv_sec) +
- std::chrono::nanoseconds(ts.tv_nsec));
-}
-
int mkdir_recursive(const char *pathname, mode_t mode)
{
char buf[128];
diff --git a/init/util.h b/init/util.h
index 81c64d7..23509d3 100644
--- a/init/util.h
+++ b/init/util.h
@@ -25,8 +25,11 @@
#include <ostream>
#include <string>
+#include <android-base/chrono_utils.h>
+
#define COLDBOOT_DONE "/dev/.coldboot_done"
+using android::base::boot_clock;
using namespace std::chrono_literals;
int create_socket(const char *name, int type, mode_t perm,
@@ -35,32 +38,22 @@
bool read_file(const char* path, std::string* content);
bool write_file(const char* path, const char* content);
-// A std::chrono clock based on CLOCK_BOOTTIME.
-class boot_clock {
- public:
- typedef std::chrono::nanoseconds duration;
- typedef std::chrono::time_point<boot_clock, duration> time_point;
- static constexpr bool is_steady = true;
-
- static time_point now();
-};
-
class Timer {
- public:
- Timer() : start_(boot_clock::now()) {
- }
+ public:
+ Timer() : start_(boot_clock::now()) {}
- double duration_s() const {
- typedef std::chrono::duration<double> double_duration;
- return std::chrono::duration_cast<double_duration>(boot_clock::now() - start_).count();
- }
+ double duration_s() const {
+ typedef std::chrono::duration<double> double_duration;
+ return std::chrono::duration_cast<double_duration>(boot_clock::now() - start_).count();
+ }
- int64_t duration_ms() const {
- return std::chrono::duration_cast<std::chrono::milliseconds>(boot_clock::now() - start_).count();
- }
+ int64_t duration_ms() const {
+ return std::chrono::duration_cast<std::chrono::milliseconds>(boot_clock::now() - start_)
+ .count();
+ }
- private:
- boot_clock::time_point start_;
+ private:
+ android::base::boot_clock::time_point start_;
};
std::ostream& operator<<(std::ostream& os, const Timer& t);