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
This commit is contained in:
parent
c65f842cd1
commit
e78ea77f69
|
@ -42,24 +42,36 @@ cc_library {
|
|||
srcs: [
|
||||
"errors_unix.cpp",
|
||||
"properties.cpp",
|
||||
"chrono_utils.cpp",
|
||||
],
|
||||
cppflags: ["-Wexit-time-destructors"],
|
||||
sanitize: {
|
||||
misc_undefined: ["integer"],
|
||||
},
|
||||
|
||||
},
|
||||
darwin: {
|
||||
srcs: ["errors_unix.cpp"],
|
||||
srcs: [
|
||||
"chrono_utils.cpp",
|
||||
"errors_unix.cpp",
|
||||
],
|
||||
cppflags: ["-Wexit-time-destructors"],
|
||||
},
|
||||
linux_bionic: {
|
||||
srcs: ["errors_unix.cpp"],
|
||||
srcs: [
|
||||
"chrono_utils.cpp",
|
||||
"errors_unix.cpp",
|
||||
],
|
||||
cppflags: ["-Wexit-time-destructors"],
|
||||
enabled: true,
|
||||
},
|
||||
linux: {
|
||||
srcs: ["errors_unix.cpp"],
|
||||
srcs: [
|
||||
"chrono_utils.cpp",
|
||||
"errors_unix.cpp",
|
||||
],
|
||||
cppflags: ["-Wexit-time-destructors"],
|
||||
host_ldlibs: ["-lrt"],
|
||||
},
|
||||
windows: {
|
||||
srcs: [
|
||||
|
@ -92,11 +104,18 @@ cc_test {
|
|||
],
|
||||
target: {
|
||||
android: {
|
||||
srcs: ["properties_test.cpp"],
|
||||
srcs: [
|
||||
"chrono_utils_test.cpp",
|
||||
"properties_test.cpp"
|
||||
],
|
||||
sanitize: {
|
||||
misc_undefined: ["integer"],
|
||||
},
|
||||
},
|
||||
linux: {
|
||||
srcs: ["chrono_utils_test.cpp"],
|
||||
host_ldlibs: ["-lrt"],
|
||||
},
|
||||
windows: {
|
||||
srcs: ["utf8_test.cpp"],
|
||||
enabled: true,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -14,25 +14,24 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "uptime_parser.h"
|
||||
#include "android-base/chrono_utils.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <android-base/file.h>
|
||||
#include <android-base/logging.h>
|
||||
|
||||
namespace bootstat {
|
||||
namespace android {
|
||||
namespace base {
|
||||
|
||||
time_t ParseUptime() {
|
||||
std::string uptime_str;
|
||||
if (!android::base::ReadFileToString("/proc/uptime", &uptime_str)) {
|
||||
PLOG(ERROR) << "Failed to read /proc/uptime";
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Cast intentionally rounds down.
|
||||
return static_cast<time_t>(strtod(uptime_str.c_str(), NULL));
|
||||
boot_clock::time_point boot_clock::now() {
|
||||
#ifdef __ANDROID__
|
||||
timespec ts;
|
||||
clock_gettime(CLOCK_BOOTTIME, &ts);
|
||||
return boot_clock::time_point(std::chrono::seconds(ts.tv_sec) +
|
||||
std::chrono::nanoseconds(ts.tv_nsec));
|
||||
#else
|
||||
// Darwin does not support clock_gettime.
|
||||
return boot_clock::time_point();
|
||||
#endif // __ANDROID__
|
||||
}
|
||||
|
||||
} // namespace bootstat
|
||||
} // namespace base
|
||||
} // namespace android
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "android-base/chrono_utils.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace android {
|
||||
namespace base {
|
||||
|
||||
std::chrono::seconds GetBootTimeSeconds() {
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_BOOTTIME, &now);
|
||||
|
||||
auto now_tp = boot_clock::time_point(std::chrono::seconds(now.tv_sec) +
|
||||
std::chrono::nanoseconds(now.tv_nsec));
|
||||
return std::chrono::duration_cast<std::chrono::seconds>(now_tp.time_since_epoch());
|
||||
}
|
||||
|
||||
// Tests (at least) the seconds accuracy of the boot_clock::now() method.
|
||||
TEST(ChronoUtilsTest, BootClockNowSeconds) {
|
||||
auto now = GetBootTimeSeconds();
|
||||
auto boot_seconds =
|
||||
std::chrono::duration_cast<std::chrono::seconds>(boot_clock::now().time_since_epoch());
|
||||
EXPECT_EQ(now, boot_seconds);
|
||||
}
|
||||
|
||||
} // namespace base
|
||||
} // namespace android
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -14,16 +14,24 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef UPTIME_PARSER_H_
|
||||
#define UPTIME_PARSER_H_
|
||||
#ifndef ANDROID_BASE_CHRONO_UTILS_H
|
||||
#define ANDROID_BASE_CHRONO_UTILS_H
|
||||
|
||||
#include <time.h>
|
||||
#include <chrono>
|
||||
|
||||
namespace bootstat {
|
||||
namespace android {
|
||||
namespace base {
|
||||
|
||||
// Returns the number of seconds the system has been on since reboot.
|
||||
time_t ParseUptime();
|
||||
// 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;
|
||||
|
||||
} // namespace bootstat
|
||||
static time_point now();
|
||||
};
|
||||
|
||||
#endif // UPTIME_PARSER_H_
|
||||
} // namespace base
|
||||
} // namespace android
|
||||
|
||||
#endif // ANDROID_BASE_CHRONO_UTILS_H
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
bootstat_lib_src_files = [
|
||||
"boot_event_record_store.cpp",
|
||||
"uptime_parser.cpp",
|
||||
]
|
||||
|
||||
cc_defaults {
|
||||
|
|
|
@ -20,13 +20,16 @@
|
|||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <utime.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <android-base/chrono_utils.h>
|
||||
#include <android-base/file.h>
|
||||
#include <android-base/logging.h>
|
||||
#include <android-base/parseint.h>
|
||||
#include "uptime_parser.h"
|
||||
|
||||
namespace {
|
||||
|
||||
|
@ -55,7 +58,9 @@ BootEventRecordStore::BootEventRecordStore() {
|
|||
}
|
||||
|
||||
void BootEventRecordStore::AddBootEvent(const std::string& event) {
|
||||
AddBootEventWithValue(event, bootstat::ParseUptime());
|
||||
auto uptime = std::chrono::duration_cast<std::chrono::seconds>(
|
||||
android::base::boot_clock::now().time_since_epoch());
|
||||
AddBootEventWithValue(event, uptime.count());
|
||||
}
|
||||
|
||||
// The implementation of AddBootEventValue makes use of the mtime file
|
||||
|
|
|
@ -21,15 +21,18 @@
|
|||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <android-base/chrono_utils.h>
|
||||
#include <android-base/file.h>
|
||||
#include <android-base/logging.h>
|
||||
#include <android-base/test_utils.h>
|
||||
#include <android-base/unique_fd.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <gmock/gmock.h>
|
||||
#include "uptime_parser.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using testing::UnorderedElementsAreArray;
|
||||
|
||||
|
@ -89,6 +92,13 @@ void DeleteDirectory(const std::string& path) {
|
|||
rmdir(path.c_str());
|
||||
}
|
||||
|
||||
// Returns the time in seconds since boot.
|
||||
time_t GetUptimeSeconds() {
|
||||
return std::chrono::duration_cast<std::chrono::seconds>(
|
||||
android::base::boot_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
|
||||
class BootEventRecordStoreTest : public ::testing::Test {
|
||||
public:
|
||||
BootEventRecordStoreTest() {
|
||||
|
@ -126,7 +136,7 @@ TEST_F(BootEventRecordStoreTest, AddSingleBootEvent) {
|
|||
BootEventRecordStore store;
|
||||
store.SetStorePath(GetStorePathForTesting());
|
||||
|
||||
time_t uptime = bootstat::ParseUptime();
|
||||
time_t uptime = GetUptimeSeconds();
|
||||
ASSERT_NE(-1, uptime);
|
||||
|
||||
store.AddBootEvent("cenozoic");
|
||||
|
@ -141,7 +151,7 @@ TEST_F(BootEventRecordStoreTest, AddMultipleBootEvents) {
|
|||
BootEventRecordStore store;
|
||||
store.SetStorePath(GetStorePathForTesting());
|
||||
|
||||
time_t uptime = bootstat::ParseUptime();
|
||||
time_t uptime = GetUptimeSeconds();
|
||||
ASSERT_NE(-1, uptime);
|
||||
|
||||
store.AddBootEvent("cretaceous");
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include <getopt.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
|
@ -30,15 +31,15 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <android/log.h>
|
||||
#include <android-base/chrono_utils.h>
|
||||
#include <android-base/logging.h>
|
||||
#include <android-base/parseint.h>
|
||||
#include <android-base/strings.h>
|
||||
#include <android/log.h>
|
||||
#include <cutils/properties.h>
|
||||
#include <metricslogger/metrics_logger.h>
|
||||
|
||||
#include "boot_event_record_store.h"
|
||||
#include "uptime_parser.h"
|
||||
|
||||
namespace {
|
||||
|
||||
|
@ -255,7 +256,8 @@ void RecordBootComplete() {
|
|||
BootEventRecordStore boot_event_store;
|
||||
BootEventRecordStore::BootEventRecord record;
|
||||
|
||||
time_t uptime = bootstat::ParseUptime();
|
||||
auto uptime = std::chrono::duration_cast<std::chrono::seconds>(
|
||||
android::base::boot_clock::now().time_since_epoch());
|
||||
time_t current_time_utc = time(nullptr);
|
||||
|
||||
if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
|
||||
|
@ -282,22 +284,21 @@ void RecordBootComplete() {
|
|||
// Log the amount of time elapsed until the device is decrypted, which
|
||||
// includes the variable amount of time the user takes to enter the
|
||||
// decryption password.
|
||||
boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime);
|
||||
boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime.count());
|
||||
|
||||
// Subtract the decryption time to normalize the boot cycle timing.
|
||||
time_t boot_complete = uptime - record.second;
|
||||
std::chrono::seconds boot_complete = std::chrono::seconds(uptime.count() - record.second);
|
||||
boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
|
||||
boot_complete);
|
||||
|
||||
boot_complete.count());
|
||||
|
||||
} else {
|
||||
boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
|
||||
uptime);
|
||||
boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
|
||||
uptime.count());
|
||||
}
|
||||
|
||||
// Record the total time from device startup to boot complete, regardless of
|
||||
// encryption state.
|
||||
boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime);
|
||||
boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime.count());
|
||||
|
||||
RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init");
|
||||
RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux");
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 @@ class Service {
|
|||
|
||||
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_;
|
||||
|
|
|
@ -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 @@ bool write_file(const char* path, const char* content) {
|
|||
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];
|
||||
|
|
37
init/util.h
37
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 @@ int create_socket(const char *name, int type, mode_t perm,
|
|||
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);
|
||||
|
|
Loading…
Reference in New Issue