Loading base/chrono_utils.cpp +5 −0 Original line number Diff line number Diff line Loading @@ -33,5 +33,10 @@ boot_clock::time_point boot_clock::now() { #endif // __ANDROID__ } std::ostream& operator<<(std::ostream& os, const Timer& t) { os << t.duration().count() << "ms"; return os; } } // namespace base } // namespace android base/chrono_utils_test.cpp +35 −1 Original line number Diff line number Diff line Loading @@ -19,6 +19,9 @@ #include <time.h> #include <chrono> #include <sstream> #include <string> #include <thread> #include <gtest/gtest.h> Loading @@ -42,5 +45,36 @@ TEST(ChronoUtilsTest, BootClockNowSeconds) { EXPECT_EQ(now, boot_seconds); } template <typename T> void ExpectAboutEqual(T expected, T actual) { auto expected_upper_bound = expected * 1.05f; auto expected_lower_bound = expected * .95; EXPECT_GT(expected_upper_bound, actual); EXPECT_LT(expected_lower_bound, actual); } TEST(ChronoUtilsTest, TimerDurationIsSane) { auto start = boot_clock::now(); Timer t; std::this_thread::sleep_for(50ms); auto stop = boot_clock::now(); auto stop_timer = t.duration(); auto expected = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start); ExpectAboutEqual(expected, stop_timer); } TEST(ChronoUtilsTest, TimerOstream) { Timer t; std::this_thread::sleep_for(50ms); auto stop_timer = t.duration().count(); std::stringstream os; os << t; decltype(stop_timer) stop_timer_from_stream; os >> stop_timer_from_stream; EXPECT_NE(0, stop_timer); ExpectAboutEqual(stop_timer, stop_timer_from_stream); } } // namespace base } // namespace android base/include/android-base/chrono_utils.h +17 −0 Original line number Diff line number Diff line Loading @@ -18,6 +18,9 @@ #define ANDROID_BASE_CHRONO_UTILS_H #include <chrono> #include <sstream> using namespace std::chrono_literals; namespace android { namespace base { Loading @@ -31,6 +34,20 @@ class boot_clock { static time_point now(); }; class Timer { public: Timer() : start_(boot_clock::now()) {} std::chrono::milliseconds duration() const { return std::chrono::duration_cast<std::chrono::milliseconds>(boot_clock::now() - start_); } private: boot_clock::time_point start_; }; std::ostream& operator<<(std::ostream& os, const Timer& t); } // namespace base } // namespace android Loading init/action.cpp +6 −6 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ #include "action.h" #include <android-base/chrono_utils.h> #include <android-base/logging.h> #include <android-base/properties.h> #include <android-base/strings.h> Loading Loading @@ -90,19 +91,18 @@ void Action::ExecuteAllCommands() const { } void Action::ExecuteCommand(const Command& command) const { Timer t; android::base::Timer t; int result = command.InvokeFunc(); double duration_ms = t.duration_s() * 1000; auto duration = t.duration(); // Any action longer than 50ms will be warned to user as slow operation if (duration_ms > 50.0 || android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { if (duration > 50ms || android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { std::string trigger_name = BuildTriggersString(); std::string cmd_str = command.BuildCommandString(); LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_ << ":" << command.line() << ") returned " << result << " took " << duration_ms << "ms."; << ":" << command.line() << ") returned " << result << " took " << duration.count() << "ms."; } } Loading init/builtins.cpp +3 −2 Original line number Diff line number Diff line Loading @@ -39,6 +39,7 @@ #include <sys/wait.h> #include <unistd.h> #include <android-base/chrono_utils.h> #include <android-base/file.h> #include <android-base/logging.h> #include <android-base/parseint.h> Loading Loading @@ -538,9 +539,9 @@ static int do_mount_all(const std::vector<std::string>& args) { } std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix; Timer t; android::base::Timer t; int ret = mount_fstab(fstabfile, mount_mode); property_set(prop_name, std::to_string(t.duration_ms())); property_set(prop_name, std::to_string(t.duration().count())); if (import_rc) { /* Paths of .rc files are specified at the 2nd argument and beyond */ Loading Loading
base/chrono_utils.cpp +5 −0 Original line number Diff line number Diff line Loading @@ -33,5 +33,10 @@ boot_clock::time_point boot_clock::now() { #endif // __ANDROID__ } std::ostream& operator<<(std::ostream& os, const Timer& t) { os << t.duration().count() << "ms"; return os; } } // namespace base } // namespace android
base/chrono_utils_test.cpp +35 −1 Original line number Diff line number Diff line Loading @@ -19,6 +19,9 @@ #include <time.h> #include <chrono> #include <sstream> #include <string> #include <thread> #include <gtest/gtest.h> Loading @@ -42,5 +45,36 @@ TEST(ChronoUtilsTest, BootClockNowSeconds) { EXPECT_EQ(now, boot_seconds); } template <typename T> void ExpectAboutEqual(T expected, T actual) { auto expected_upper_bound = expected * 1.05f; auto expected_lower_bound = expected * .95; EXPECT_GT(expected_upper_bound, actual); EXPECT_LT(expected_lower_bound, actual); } TEST(ChronoUtilsTest, TimerDurationIsSane) { auto start = boot_clock::now(); Timer t; std::this_thread::sleep_for(50ms); auto stop = boot_clock::now(); auto stop_timer = t.duration(); auto expected = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start); ExpectAboutEqual(expected, stop_timer); } TEST(ChronoUtilsTest, TimerOstream) { Timer t; std::this_thread::sleep_for(50ms); auto stop_timer = t.duration().count(); std::stringstream os; os << t; decltype(stop_timer) stop_timer_from_stream; os >> stop_timer_from_stream; EXPECT_NE(0, stop_timer); ExpectAboutEqual(stop_timer, stop_timer_from_stream); } } // namespace base } // namespace android
base/include/android-base/chrono_utils.h +17 −0 Original line number Diff line number Diff line Loading @@ -18,6 +18,9 @@ #define ANDROID_BASE_CHRONO_UTILS_H #include <chrono> #include <sstream> using namespace std::chrono_literals; namespace android { namespace base { Loading @@ -31,6 +34,20 @@ class boot_clock { static time_point now(); }; class Timer { public: Timer() : start_(boot_clock::now()) {} std::chrono::milliseconds duration() const { return std::chrono::duration_cast<std::chrono::milliseconds>(boot_clock::now() - start_); } private: boot_clock::time_point start_; }; std::ostream& operator<<(std::ostream& os, const Timer& t); } // namespace base } // namespace android Loading
init/action.cpp +6 −6 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ #include "action.h" #include <android-base/chrono_utils.h> #include <android-base/logging.h> #include <android-base/properties.h> #include <android-base/strings.h> Loading Loading @@ -90,19 +91,18 @@ void Action::ExecuteAllCommands() const { } void Action::ExecuteCommand(const Command& command) const { Timer t; android::base::Timer t; int result = command.InvokeFunc(); double duration_ms = t.duration_s() * 1000; auto duration = t.duration(); // Any action longer than 50ms will be warned to user as slow operation if (duration_ms > 50.0 || android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { if (duration > 50ms || android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { std::string trigger_name = BuildTriggersString(); std::string cmd_str = command.BuildCommandString(); LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_ << ":" << command.line() << ") returned " << result << " took " << duration_ms << "ms."; << ":" << command.line() << ") returned " << result << " took " << duration.count() << "ms."; } } Loading
init/builtins.cpp +3 −2 Original line number Diff line number Diff line Loading @@ -39,6 +39,7 @@ #include <sys/wait.h> #include <unistd.h> #include <android-base/chrono_utils.h> #include <android-base/file.h> #include <android-base/logging.h> #include <android-base/parseint.h> Loading Loading @@ -538,9 +539,9 @@ static int do_mount_all(const std::vector<std::string>& args) { } std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix; Timer t; android::base::Timer t; int ret = mount_fstab(fstabfile, mount_mode); property_set(prop_name, std::to_string(t.duration_ms())); property_set(prop_name, std::to_string(t.duration().count())); if (import_rc) { /* Paths of .rc files are specified at the 2nd argument and beyond */ Loading