diff options
author | Tom Cherry | 2017-07-10 13:23:29 -0500 |
---|---|---|
committer | Gerrit Code Review | 2017-07-10 13:23:29 -0500 |
commit | 896297b2ef10a58fc614aebce8724d342d81f85e (patch) | |
tree | 929061c31a06d5793e3567a115d37cffae13c309 | |
parent | 11b91e7c28a4ee677b61ec91cdd58c2c244030aa (diff) | |
parent | ede0d538501dfc78c741fb3b0645406636d1d1fd (diff) | |
download | platform-system-core-896297b2ef10a58fc614aebce8724d342d81f85e.tar.gz platform-system-core-896297b2ef10a58fc614aebce8724d342d81f85e.tar.xz platform-system-core-896297b2ef10a58fc614aebce8724d342d81f85e.zip |
Merge "Move Timer from init to libbase"
-rw-r--r-- | base/chrono_utils.cpp | 5 | ||||
-rw-r--r-- | base/chrono_utils_test.cpp | 36 | ||||
-rw-r--r-- | base/include/android-base/chrono_utils.h | 17 | ||||
-rw-r--r-- | init/action.cpp | 12 | ||||
-rw-r--r-- | init/builtins.cpp | 5 | ||||
-rw-r--r-- | init/firmware_handler.cpp | 4 | ||||
-rw-r--r-- | init/init.cpp | 5 | ||||
-rw-r--r-- | init/init_first_stage.cpp | 3 | ||||
-rw-r--r-- | init/init_parser.cpp | 3 | ||||
-rw-r--r-- | init/init_parser_test.cpp | 9 | ||||
-rw-r--r-- | init/property_service.cpp | 5 | ||||
-rw-r--r-- | init/reboot.cpp | 40 | ||||
-rw-r--r-- | init/service.cpp | 7 | ||||
-rw-r--r-- | init/service.h | 5 | ||||
-rw-r--r-- | init/ueventd.cpp | 5 | ||||
-rw-r--r-- | init/util.cpp | 5 | ||||
-rw-r--r-- | init/util.h | 20 |
17 files changed, 114 insertions, 72 deletions
diff --git a/base/chrono_utils.cpp b/base/chrono_utils.cpp index 5eedf3bce..b6bf701ea 100644 --- a/base/chrono_utils.cpp +++ b/base/chrono_utils.cpp | |||
@@ -33,5 +33,10 @@ boot_clock::time_point boot_clock::now() { | |||
33 | #endif // __ANDROID__ | 33 | #endif // __ANDROID__ |
34 | } | 34 | } |
35 | 35 | ||
36 | std::ostream& operator<<(std::ostream& os, const Timer& t) { | ||
37 | os << t.duration().count() << "ms"; | ||
38 | return os; | ||
39 | } | ||
40 | |||
36 | } // namespace base | 41 | } // namespace base |
37 | } // namespace android | 42 | } // namespace android |
diff --git a/base/chrono_utils_test.cpp b/base/chrono_utils_test.cpp index 057132d9f..da442f455 100644 --- a/base/chrono_utils_test.cpp +++ b/base/chrono_utils_test.cpp | |||
@@ -19,6 +19,9 @@ | |||
19 | #include <time.h> | 19 | #include <time.h> |
20 | 20 | ||
21 | #include <chrono> | 21 | #include <chrono> |
22 | #include <sstream> | ||
23 | #include <string> | ||
24 | #include <thread> | ||
22 | 25 | ||
23 | #include <gtest/gtest.h> | 26 | #include <gtest/gtest.h> |
24 | 27 | ||
@@ -42,5 +45,36 @@ TEST(ChronoUtilsTest, BootClockNowSeconds) { | |||
42 | EXPECT_EQ(now, boot_seconds); | 45 | EXPECT_EQ(now, boot_seconds); |
43 | } | 46 | } |
44 | 47 | ||
48 | template <typename T> | ||
49 | void ExpectAboutEqual(T expected, T actual) { | ||
50 | auto expected_upper_bound = expected * 1.05f; | ||
51 | auto expected_lower_bound = expected * .95; | ||
52 | EXPECT_GT(expected_upper_bound, actual); | ||
53 | EXPECT_LT(expected_lower_bound, actual); | ||
54 | } | ||
55 | |||
56 | TEST(ChronoUtilsTest, TimerDurationIsSane) { | ||
57 | auto start = boot_clock::now(); | ||
58 | Timer t; | ||
59 | std::this_thread::sleep_for(50ms); | ||
60 | auto stop = boot_clock::now(); | ||
61 | auto stop_timer = t.duration(); | ||
62 | |||
63 | auto expected = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start); | ||
64 | ExpectAboutEqual(expected, stop_timer); | ||
65 | } | ||
66 | |||
67 | TEST(ChronoUtilsTest, TimerOstream) { | ||
68 | Timer t; | ||
69 | std::this_thread::sleep_for(50ms); | ||
70 | auto stop_timer = t.duration().count(); | ||
71 | std::stringstream os; | ||
72 | os << t; | ||
73 | decltype(stop_timer) stop_timer_from_stream; | ||
74 | os >> stop_timer_from_stream; | ||
75 | EXPECT_NE(0, stop_timer); | ||
76 | ExpectAboutEqual(stop_timer, stop_timer_from_stream); | ||
77 | } | ||
78 | |||
45 | } // namespace base | 79 | } // namespace base |
46 | } // namespace android \ No newline at end of file | 80 | } // namespace android |
diff --git a/base/include/android-base/chrono_utils.h b/base/include/android-base/chrono_utils.h index 0086425e5..7679d4c94 100644 --- a/base/include/android-base/chrono_utils.h +++ b/base/include/android-base/chrono_utils.h | |||
@@ -18,6 +18,9 @@ | |||
18 | #define ANDROID_BASE_CHRONO_UTILS_H | 18 | #define ANDROID_BASE_CHRONO_UTILS_H |
19 | 19 | ||
20 | #include <chrono> | 20 | #include <chrono> |
21 | #include <sstream> | ||
22 | |||
23 | using namespace std::chrono_literals; | ||
21 | 24 | ||
22 | namespace android { | 25 | namespace android { |
23 | namespace base { | 26 | namespace base { |
@@ -31,6 +34,20 @@ class boot_clock { | |||
31 | static time_point now(); | 34 | static time_point now(); |
32 | }; | 35 | }; |
33 | 36 | ||
37 | class Timer { | ||
38 | public: | ||
39 | Timer() : start_(boot_clock::now()) {} | ||
40 | |||
41 | std::chrono::milliseconds duration() const { | ||
42 | return std::chrono::duration_cast<std::chrono::milliseconds>(boot_clock::now() - start_); | ||
43 | } | ||
44 | |||
45 | private: | ||
46 | boot_clock::time_point start_; | ||
47 | }; | ||
48 | |||
49 | std::ostream& operator<<(std::ostream& os, const Timer& t); | ||
50 | |||
34 | } // namespace base | 51 | } // namespace base |
35 | } // namespace android | 52 | } // namespace android |
36 | 53 | ||
diff --git a/init/action.cpp b/init/action.cpp index 4a3ab4816..4ec5f1739 100644 --- a/init/action.cpp +++ b/init/action.cpp | |||
@@ -16,6 +16,7 @@ | |||
16 | 16 | ||
17 | #include "action.h" | 17 | #include "action.h" |
18 | 18 | ||
19 | #include <android-base/chrono_utils.h> | ||
19 | #include <android-base/logging.h> | 20 | #include <android-base/logging.h> |
20 | #include <android-base/properties.h> | 21 | #include <android-base/properties.h> |
21 | #include <android-base/strings.h> | 22 | #include <android-base/strings.h> |
@@ -90,19 +91,18 @@ void Action::ExecuteAllCommands() const { | |||
90 | } | 91 | } |
91 | 92 | ||
92 | void Action::ExecuteCommand(const Command& command) const { | 93 | void Action::ExecuteCommand(const Command& command) const { |
93 | Timer t; | 94 | android::base::Timer t; |
94 | int result = command.InvokeFunc(); | 95 | int result = command.InvokeFunc(); |
95 | 96 | ||
96 | double duration_ms = t.duration_s() * 1000; | 97 | auto duration = t.duration(); |
97 | // Any action longer than 50ms will be warned to user as slow operation | 98 | // Any action longer than 50ms will be warned to user as slow operation |
98 | if (duration_ms > 50.0 || | 99 | if (duration > 50ms || android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { |
99 | android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { | ||
100 | std::string trigger_name = BuildTriggersString(); | 100 | std::string trigger_name = BuildTriggersString(); |
101 | std::string cmd_str = command.BuildCommandString(); | 101 | std::string cmd_str = command.BuildCommandString(); |
102 | 102 | ||
103 | LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_ | 103 | LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_ |
104 | << ":" << command.line() << ") returned " << result << " took " << duration_ms | 104 | << ":" << command.line() << ") returned " << result << " took " |
105 | << "ms."; | 105 | << duration.count() << "ms."; |
106 | } | 106 | } |
107 | } | 107 | } |
108 | 108 | ||
diff --git a/init/builtins.cpp b/init/builtins.cpp index 75a8f1972..dfd7b73d2 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp | |||
@@ -39,6 +39,7 @@ | |||
39 | #include <sys/wait.h> | 39 | #include <sys/wait.h> |
40 | #include <unistd.h> | 40 | #include <unistd.h> |
41 | 41 | ||
42 | #include <android-base/chrono_utils.h> | ||
42 | #include <android-base/file.h> | 43 | #include <android-base/file.h> |
43 | #include <android-base/logging.h> | 44 | #include <android-base/logging.h> |
44 | #include <android-base/parseint.h> | 45 | #include <android-base/parseint.h> |
@@ -538,9 +539,9 @@ static int do_mount_all(const std::vector<std::string>& args) { | |||
538 | } | 539 | } |
539 | 540 | ||
540 | std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix; | 541 | std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix; |
541 | Timer t; | 542 | android::base::Timer t; |
542 | int ret = mount_fstab(fstabfile, mount_mode); | 543 | int ret = mount_fstab(fstabfile, mount_mode); |
543 | property_set(prop_name, std::to_string(t.duration_ms())); | 544 | property_set(prop_name, std::to_string(t.duration().count())); |
544 | 545 | ||
545 | if (import_rc) { | 546 | if (import_rc) { |
546 | /* Paths of .rc files are specified at the 2nd argument and beyond */ | 547 | /* Paths of .rc files are specified at the 2nd argument and beyond */ |
diff --git a/init/firmware_handler.cpp b/init/firmware_handler.cpp index 860b30bc2..b686885fb 100644 --- a/init/firmware_handler.cpp +++ b/init/firmware_handler.cpp | |||
@@ -24,12 +24,12 @@ | |||
24 | #include <string> | 24 | #include <string> |
25 | #include <thread> | 25 | #include <thread> |
26 | 26 | ||
27 | #include <android-base/chrono_utils.h> | ||
27 | #include <android-base/file.h> | 28 | #include <android-base/file.h> |
28 | #include <android-base/logging.h> | 29 | #include <android-base/logging.h> |
29 | #include <android-base/unique_fd.h> | 30 | #include <android-base/unique_fd.h> |
30 | 31 | ||
31 | #include "util.h" | 32 | using android::base::Timer; |
32 | |||
33 | using android::base::unique_fd; | 33 | using android::base::unique_fd; |
34 | using android::base::WriteFully; | 34 | using android::base::WriteFully; |
35 | 35 | ||
diff --git a/init/init.cpp b/init/init.cpp index d23e1a3c0..bf251ffbe 100644 --- a/init/init.cpp +++ b/init/init.cpp | |||
@@ -73,6 +73,7 @@ using namespace std::string_literals; | |||
73 | 73 | ||
74 | using android::base::boot_clock; | 74 | using android::base::boot_clock; |
75 | using android::base::GetProperty; | 75 | using android::base::GetProperty; |
76 | using android::base::Timer; | ||
76 | 77 | ||
77 | namespace android { | 78 | namespace android { |
78 | namespace init { | 79 | namespace init { |
@@ -232,7 +233,7 @@ static int wait_for_coldboot_done_action(const std::vector<std::string>& args) { | |||
232 | panic(); | 233 | panic(); |
233 | } | 234 | } |
234 | 235 | ||
235 | property_set("ro.boottime.init.cold_boot_wait", std::to_string(t.duration_ms())); | 236 | property_set("ro.boottime.init.cold_boot_wait", std::to_string(t.duration().count())); |
236 | return 0; | 237 | return 0; |
237 | } | 238 | } |
238 | 239 | ||
@@ -870,7 +871,7 @@ static void selinux_initialize(bool in_kernel_domain) { | |||
870 | } | 871 | } |
871 | 872 | ||
872 | // init's first stage can't set properties, so pass the time to the second stage. | 873 | // init's first stage can't set properties, so pass the time to the second stage. |
873 | setenv("INIT_SELINUX_TOOK", std::to_string(t.duration_ms()).c_str(), 1); | 874 | setenv("INIT_SELINUX_TOOK", std::to_string(t.duration().count()).c_str(), 1); |
874 | } else { | 875 | } else { |
875 | selinux_init_all_handles(); | 876 | selinux_init_all_handles(); |
876 | } | 877 | } |
diff --git a/init/init_first_stage.cpp b/init/init_first_stage.cpp index be57f8bad..0f7e38fa6 100644 --- a/init/init_first_stage.cpp +++ b/init/init_first_stage.cpp | |||
@@ -25,6 +25,7 @@ | |||
25 | #include <string> | 25 | #include <string> |
26 | #include <vector> | 26 | #include <vector> |
27 | 27 | ||
28 | #include <android-base/chrono_utils.h> | ||
28 | #include <android-base/file.h> | 29 | #include <android-base/file.h> |
29 | #include <android-base/logging.h> | 30 | #include <android-base/logging.h> |
30 | #include <android-base/strings.h> | 31 | #include <android-base/strings.h> |
@@ -36,7 +37,7 @@ | |||
36 | #include "uevent_listener.h" | 37 | #include "uevent_listener.h" |
37 | #include "util.h" | 38 | #include "util.h" |
38 | 39 | ||
39 | using namespace std::chrono_literals; | 40 | using android::base::Timer; |
40 | 41 | ||
41 | namespace android { | 42 | namespace android { |
42 | namespace init { | 43 | namespace init { |
diff --git a/init/init_parser.cpp b/init/init_parser.cpp index e76d58916..9f7089bcd 100644 --- a/init/init_parser.cpp +++ b/init/init_parser.cpp | |||
@@ -18,6 +18,7 @@ | |||
18 | 18 | ||
19 | #include <dirent.h> | 19 | #include <dirent.h> |
20 | 20 | ||
21 | #include <android-base/chrono_utils.h> | ||
21 | #include <android-base/logging.h> | 22 | #include <android-base/logging.h> |
22 | #include <android-base/stringprintf.h> | 23 | #include <android-base/stringprintf.h> |
23 | #include <android-base/strings.h> | 24 | #include <android-base/strings.h> |
@@ -111,7 +112,7 @@ void Parser::ParseData(const std::string& filename, const std::string& data) { | |||
111 | 112 | ||
112 | bool Parser::ParseConfigFile(const std::string& path) { | 113 | bool Parser::ParseConfigFile(const std::string& path) { |
113 | LOG(INFO) << "Parsing file " << path << "..."; | 114 | LOG(INFO) << "Parsing file " << path << "..."; |
114 | Timer t; | 115 | android::base::Timer t; |
115 | std::string data; | 116 | std::string data; |
116 | std::string err; | 117 | std::string err; |
117 | if (!ReadFile(path, &data, &err)) { | 118 | if (!ReadFile(path, &data, &err)) { |
diff --git a/init/init_parser_test.cpp b/init/init_parser_test.cpp index 38b8275e5..95f269a0e 100644 --- a/init/init_parser_test.cpp +++ b/init/init_parser_test.cpp | |||
@@ -16,13 +16,14 @@ | |||
16 | 16 | ||
17 | #include "init_parser.h" | 17 | #include "init_parser.h" |
18 | 18 | ||
19 | #include "init.h" | 19 | #include <string> |
20 | #include "service.h" | 20 | #include <vector> |
21 | 21 | ||
22 | #include <gtest/gtest.h> | 22 | #include <gtest/gtest.h> |
23 | 23 | ||
24 | #include <string> | 24 | #include "init.h" |
25 | #include <vector> | 25 | #include "service.h" |
26 | #include "util.h" | ||
26 | 27 | ||
27 | namespace android { | 28 | namespace android { |
28 | namespace init { | 29 | namespace init { |
diff --git a/init/property_service.cpp b/init/property_service.cpp index 0af6c2127..925cc9bd7 100644 --- a/init/property_service.cpp +++ b/init/property_service.cpp | |||
@@ -42,6 +42,7 @@ | |||
42 | #include <queue> | 42 | #include <queue> |
43 | #include <vector> | 43 | #include <vector> |
44 | 44 | ||
45 | #include <android-base/chrono_utils.h> | ||
45 | #include <android-base/file.h> | 46 | #include <android-base/file.h> |
46 | #include <android-base/logging.h> | 47 | #include <android-base/logging.h> |
47 | #include <android-base/properties.h> | 48 | #include <android-base/properties.h> |
@@ -55,6 +56,8 @@ | |||
55 | #include "init.h" | 56 | #include "init.h" |
56 | #include "util.h" | 57 | #include "util.h" |
57 | 58 | ||
59 | using android::base::Timer; | ||
60 | |||
58 | #define PERSISTENT_PROPERTY_DIR "/data/property" | 61 | #define PERSISTENT_PROPERTY_DIR "/data/property" |
59 | #define RECOVERY_MOUNT_POINT "/recovery" | 62 | #define RECOVERY_MOUNT_POINT "/recovery" |
60 | 63 | ||
@@ -350,7 +353,7 @@ class SocketConnection { | |||
350 | while (*timeout_ms > 0) { | 353 | while (*timeout_ms > 0) { |
351 | Timer timer; | 354 | Timer timer; |
352 | int nr = poll(ufds, 1, *timeout_ms); | 355 | int nr = poll(ufds, 1, *timeout_ms); |
353 | uint64_t millis = timer.duration_ms(); | 356 | uint64_t millis = timer.duration().count(); |
354 | *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis; | 357 | *timeout_ms = (millis > *timeout_ms) ? 0 : *timeout_ms - millis; |
355 | 358 | ||
356 | if (nr > 0) { | 359 | if (nr > 0) { |
diff --git a/init/reboot.cpp b/init/reboot.cpp index 38d603ad5..969caec9d 100644 --- a/init/reboot.cpp +++ b/init/reboot.cpp | |||
@@ -35,6 +35,7 @@ | |||
35 | #include <thread> | 35 | #include <thread> |
36 | #include <vector> | 36 | #include <vector> |
37 | 37 | ||
38 | #include <android-base/chrono_utils.h> | ||
38 | #include <android-base/file.h> | 39 | #include <android-base/file.h> |
39 | #include <android-base/logging.h> | 40 | #include <android-base/logging.h> |
40 | #include <android-base/macros.h> | 41 | #include <android-base/macros.h> |
@@ -54,6 +55,7 @@ | |||
54 | #include "service.h" | 55 | #include "service.h" |
55 | 56 | ||
56 | using android::base::StringPrintf; | 57 | using android::base::StringPrintf; |
58 | using android::base::Timer; | ||
57 | 59 | ||
58 | namespace android { | 60 | namespace android { |
59 | namespace init { | 61 | namespace init { |
@@ -161,7 +163,8 @@ static void ShutdownVold() { | |||
161 | } | 163 | } |
162 | 164 | ||
163 | static void LogShutdownTime(UmountStat stat, Timer* t) { | 165 | static void LogShutdownTime(UmountStat stat, Timer* t) { |
164 | LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration_ms()) << ":" << stat; | 166 | LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":" |
167 | << stat; | ||
165 | } | 168 | } |
166 | 169 | ||
167 | // Determines whether the system is capable of rebooting. This is conservative, | 170 | // Determines whether the system is capable of rebooting. This is conservative, |
@@ -254,7 +257,7 @@ static void DumpUmountDebuggingInfo(bool dump_all) { | |||
254 | } | 257 | } |
255 | } | 258 | } |
256 | 259 | ||
257 | static UmountStat UmountPartitions(int timeoutMs) { | 260 | static UmountStat UmountPartitions(std::chrono::milliseconds timeout) { |
258 | Timer t; | 261 | Timer t; |
259 | UmountStat stat = UMOUNT_STAT_TIMEOUT; | 262 | UmountStat stat = UMOUNT_STAT_TIMEOUT; |
260 | int retry = 0; | 263 | int retry = 0; |
@@ -272,7 +275,7 @@ static UmountStat UmountPartitions(int timeoutMs) { | |||
272 | stat = UMOUNT_STAT_SUCCESS; | 275 | stat = UMOUNT_STAT_SUCCESS; |
273 | break; | 276 | break; |
274 | } | 277 | } |
275 | if ((timeoutMs < t.duration_ms()) && retry > 0) { // try umount at least once | 278 | if ((timeout < t.duration()) && retry > 0) { // try umount at least once |
276 | stat = UMOUNT_STAT_TIMEOUT; | 279 | stat = UMOUNT_STAT_TIMEOUT; |
277 | break; | 280 | break; |
278 | } | 281 | } |
@@ -301,7 +304,7 @@ static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sy | |||
301 | * | 304 | * |
302 | * return true when umount was successful. false when timed out. | 305 | * return true when umount was successful. false when timed out. |
303 | */ | 306 | */ |
304 | static UmountStat TryUmountAndFsck(bool runFsck, int timeoutMs) { | 307 | static UmountStat TryUmountAndFsck(bool runFsck, std::chrono::milliseconds timeout) { |
305 | Timer t; | 308 | Timer t; |
306 | std::vector<MountEntry> block_devices; | 309 | std::vector<MountEntry> block_devices; |
307 | std::vector<MountEntry> emulated_devices; | 310 | std::vector<MountEntry> emulated_devices; |
@@ -312,13 +315,13 @@ static UmountStat TryUmountAndFsck(bool runFsck, int timeoutMs) { | |||
312 | return UMOUNT_STAT_ERROR; | 315 | return UMOUNT_STAT_ERROR; |
313 | } | 316 | } |
314 | 317 | ||
315 | UmountStat stat = UmountPartitions(timeoutMs - t.duration_ms()); | 318 | UmountStat stat = UmountPartitions(timeout - t.duration()); |
316 | if (stat != UMOUNT_STAT_SUCCESS) { | 319 | if (stat != UMOUNT_STAT_SUCCESS) { |
317 | LOG(INFO) << "umount timeout, last resort, kill all and try"; | 320 | LOG(INFO) << "umount timeout, last resort, kill all and try"; |
318 | if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false); | 321 | if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false); |
319 | KillAllProcesses(); | 322 | KillAllProcesses(); |
320 | // even if it succeeds, still it is timeout and do not run fsck with all processes killed | 323 | // even if it succeeds, still it is timeout and do not run fsck with all processes killed |
321 | UmountPartitions(0); | 324 | UmountPartitions(0ms); |
322 | if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true); | 325 | if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true); |
323 | } | 326 | } |
324 | 327 | ||
@@ -352,15 +355,14 @@ void DoReboot(unsigned int cmd, const std::string& reason, const std::string& re | |||
352 | abort(); | 355 | abort(); |
353 | } | 356 | } |
354 | 357 | ||
355 | constexpr unsigned int shutdownTimeoutDefault = 6; | 358 | auto shutdown_timeout = 0s; |
356 | unsigned int shutdownTimeout = shutdownTimeoutDefault; | 359 | if (!SHUTDOWN_ZERO_TIMEOUT) { |
357 | if (SHUTDOWN_ZERO_TIMEOUT) { // eng build | 360 | constexpr unsigned int shutdown_timeout_default = 6; |
358 | shutdownTimeout = 0; | 361 | auto shutdown_timeout_property = |
359 | } else { | 362 | android::base::GetUintProperty("ro.build.shutdown_timeout", shutdown_timeout_default); |
360 | shutdownTimeout = | 363 | shutdown_timeout = std::chrono::seconds(shutdown_timeout_property); |
361 | android::base::GetUintProperty("ro.build.shutdown_timeout", shutdownTimeoutDefault); | ||
362 | } | 364 | } |
363 | LOG(INFO) << "Shutdown timeout: " << shutdownTimeout; | 365 | LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " seconds"; |
364 | 366 | ||
365 | // keep debugging tools until non critical ones are all gone. | 367 | // keep debugging tools until non critical ones are all gone. |
366 | const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"}; | 368 | const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"}; |
@@ -387,7 +389,7 @@ void DoReboot(unsigned int cmd, const std::string& reason, const std::string& re | |||
387 | 389 | ||
388 | // optional shutdown step | 390 | // optional shutdown step |
389 | // 1. terminate all services except shutdown critical ones. wait for delay to finish | 391 | // 1. terminate all services except shutdown critical ones. wait for delay to finish |
390 | if (shutdownTimeout > 0) { | 392 | if (shutdown_timeout > 0s) { |
391 | LOG(INFO) << "terminating init services"; | 393 | LOG(INFO) << "terminating init services"; |
392 | 394 | ||
393 | // Ask all services to terminate except shutdown critical ones. | 395 | // Ask all services to terminate except shutdown critical ones. |
@@ -396,9 +398,9 @@ void DoReboot(unsigned int cmd, const std::string& reason, const std::string& re | |||
396 | }); | 398 | }); |
397 | 399 | ||
398 | int service_count = 0; | 400 | int service_count = 0; |
399 | // Up to half as long as shutdownTimeout or 3 seconds, whichever is lower. | 401 | // Up to half as long as shutdown_timeout or 3 seconds, whichever is lower. |
400 | unsigned int terminationWaitTimeout = std::min<unsigned int>((shutdownTimeout + 1) / 2, 3); | 402 | auto termination_wait_timeout = std::min((shutdown_timeout + 1s) / 2, 3s); |
401 | while (t.duration_s() < terminationWaitTimeout) { | 403 | while (t.duration() < termination_wait_timeout) { |
402 | ServiceManager::GetInstance().ReapAnyOutstandingChildren(); | 404 | ServiceManager::GetInstance().ReapAnyOutstandingChildren(); |
403 | 405 | ||
404 | service_count = 0; | 406 | service_count = 0; |
@@ -446,7 +448,7 @@ void DoReboot(unsigned int cmd, const std::string& reason, const std::string& re | |||
446 | }); | 448 | }); |
447 | // 4. sync, try umount, and optionally run fsck for user shutdown | 449 | // 4. sync, try umount, and optionally run fsck for user shutdown |
448 | sync(); | 450 | sync(); |
449 | UmountStat stat = TryUmountAndFsck(runFsck, shutdownTimeout * 1000 - t.duration_ms()); | 451 | UmountStat stat = TryUmountAndFsck(runFsck, shutdown_timeout - t.duration()); |
450 | // Follow what linux shutdown is doing: one more sync with little bit delay | 452 | // Follow what linux shutdown is doing: one more sync with little bit delay |
451 | sync(); | 453 | sync(); |
452 | std::this_thread::sleep_for(100ms); | 454 | std::this_thread::sleep_for(100ms); |
diff --git a/init/service.cpp b/init/service.cpp index 6b36526ed..7a657c8ae 100644 --- a/init/service.cpp +++ b/init/service.cpp | |||
@@ -633,10 +633,10 @@ bool Service::ParseLine(const std::vector<std::string>& args, std::string* err) | |||
633 | return (this->*parser)(args, err); | 633 | return (this->*parser)(args, err); |
634 | } | 634 | } |
635 | 635 | ||
636 | bool Service::ExecStart(std::unique_ptr<Timer>* exec_waiter) { | 636 | bool Service::ExecStart(std::unique_ptr<android::base::Timer>* exec_waiter) { |
637 | flags_ |= SVC_EXEC | SVC_ONESHOT; | 637 | flags_ |= SVC_EXEC | SVC_ONESHOT; |
638 | 638 | ||
639 | exec_waiter->reset(new Timer); | 639 | exec_waiter->reset(new android::base::Timer); |
640 | 640 | ||
641 | if (!Start()) { | 641 | if (!Start()) { |
642 | exec_waiter->reset(); | 642 | exec_waiter->reset(); |
@@ -1127,7 +1127,8 @@ bool ServiceManager::ReapOneProcess() { | |||
1127 | if (svc) { | 1127 | if (svc) { |
1128 | name = StringPrintf("Service '%s' (pid %d)", svc->name().c_str(), pid); | 1128 | name = StringPrintf("Service '%s' (pid %d)", svc->name().c_str(), pid); |
1129 | if (svc->flags() & SVC_EXEC) { | 1129 | if (svc->flags() & SVC_EXEC) { |
1130 | wait_string = StringPrintf(" waiting took %f seconds", exec_waiter_->duration_s()); | 1130 | wait_string = StringPrintf(" waiting took %f seconds", |
1131 | exec_waiter_->duration().count() / 1000.0f); | ||
1131 | } | 1132 | } |
1132 | } else { | 1133 | } else { |
1133 | name = StringPrintf("Untracked pid %d", pid); | 1134 | name = StringPrintf("Untracked pid %d", pid); |
diff --git a/init/service.h b/init/service.h index 40b4745d9..f682abd36 100644 --- a/init/service.h +++ b/init/service.h | |||
@@ -32,7 +32,6 @@ | |||
32 | #include "descriptors.h" | 32 | #include "descriptors.h" |
33 | #include "init_parser.h" | 33 | #include "init_parser.h" |
34 | #include "keyword_map.h" | 34 | #include "keyword_map.h" |
35 | #include "util.h" | ||
36 | 35 | ||
37 | #define SVC_DISABLED 0x001 // do not autostart with class | 36 | #define SVC_DISABLED 0x001 // do not autostart with class |
38 | #define SVC_ONESHOT 0x002 // do not restart on exit | 37 | #define SVC_ONESHOT 0x002 // do not restart on exit |
@@ -76,7 +75,7 @@ class Service { | |||
76 | 75 | ||
77 | bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; } | 76 | bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; } |
78 | bool ParseLine(const std::vector<std::string>& args, std::string* err); | 77 | bool ParseLine(const std::vector<std::string>& args, std::string* err); |
79 | bool ExecStart(std::unique_ptr<Timer>* exec_waiter); | 78 | bool ExecStart(std::unique_ptr<android::base::Timer>* exec_waiter); |
80 | bool Start(); | 79 | bool Start(); |
81 | bool StartIfNotDisabled(); | 80 | bool StartIfNotDisabled(); |
82 | bool Enable(); | 81 | bool Enable(); |
@@ -218,7 +217,7 @@ class ServiceManager { | |||
218 | bool ReapOneProcess(); | 217 | bool ReapOneProcess(); |
219 | 218 | ||
220 | static int exec_count_; // Every service needs a unique name. | 219 | static int exec_count_; // Every service needs a unique name. |
221 | std::unique_ptr<Timer> exec_waiter_; | 220 | std::unique_ptr<android::base::Timer> exec_waiter_; |
222 | 221 | ||
223 | std::vector<std::unique_ptr<Service>> services_; | 222 | std::vector<std::unique_ptr<Service>> services_; |
224 | }; | 223 | }; |
diff --git a/init/ueventd.cpp b/init/ueventd.cpp index a713beb0a..c0eae1e9a 100644 --- a/init/ueventd.cpp +++ b/init/ueventd.cpp | |||
@@ -27,6 +27,7 @@ | |||
27 | #include <set> | 27 | #include <set> |
28 | #include <thread> | 28 | #include <thread> |
29 | 29 | ||
30 | #include <android-base/chrono_utils.h> | ||
30 | #include <android-base/logging.h> | 31 | #include <android-base/logging.h> |
31 | #include <android-base/properties.h> | 32 | #include <android-base/properties.h> |
32 | #include <selinux/android.h> | 33 | #include <selinux/android.h> |
@@ -198,7 +199,7 @@ void ColdBoot::WaitForSubProcesses() { | |||
198 | } | 199 | } |
199 | 200 | ||
200 | void ColdBoot::Run() { | 201 | void ColdBoot::Run() { |
201 | Timer cold_boot_timer; | 202 | android::base::Timer cold_boot_timer; |
202 | 203 | ||
203 | RegenerateUevents(); | 204 | RegenerateUevents(); |
204 | 205 | ||
@@ -209,7 +210,7 @@ void ColdBoot::Run() { | |||
209 | WaitForSubProcesses(); | 210 | WaitForSubProcesses(); |
210 | 211 | ||
211 | close(open(COLDBOOT_DONE, O_WRONLY | O_CREAT | O_CLOEXEC, 0000)); | 212 | close(open(COLDBOOT_DONE, O_WRONLY | O_CREAT | O_CLOEXEC, 0000)); |
212 | LOG(INFO) << "Coldboot took " << cold_boot_timer; | 213 | LOG(INFO) << "Coldboot took " << cold_boot_timer.duration().count() / 1000.0f << " seconds"; |
213 | } | 214 | } |
214 | 215 | ||
215 | DeviceHandler CreateDeviceHandler() { | 216 | DeviceHandler CreateDeviceHandler() { |
diff --git a/init/util.cpp b/init/util.cpp index 479179e41..2792794c5 100644 --- a/init/util.cpp +++ b/init/util.cpp | |||
@@ -374,11 +374,6 @@ void panic() { | |||
374 | DoReboot(ANDROID_RB_RESTART2, "reboot", "bootloader", false); | 374 | DoReboot(ANDROID_RB_RESTART2, "reboot", "bootloader", false); |
375 | } | 375 | } |
376 | 376 | ||
377 | std::ostream& operator<<(std::ostream& os, const Timer& t) { | ||
378 | os << t.duration_s() << " seconds"; | ||
379 | return os; | ||
380 | } | ||
381 | |||
382 | // Reads the content of device tree file under kAndroidDtDir directory. | 377 | // Reads the content of device tree file under kAndroidDtDir directory. |
383 | // Returns true if the read is success, false otherwise. | 378 | // Returns true if the read is success, false otherwise. |
384 | bool read_android_dt_file(const std::string& sub_path, std::string* dt_content) { | 379 | bool read_android_dt_file(const std::string& sub_path, std::string* dt_content) { |
diff --git a/init/util.h b/init/util.h index 346953f25..452df2d00 100644 --- a/init/util.h +++ b/init/util.h | |||
@@ -44,26 +44,6 @@ int CreateSocket(const char* name, int type, bool passcred, mode_t perm, uid_t u | |||
44 | bool ReadFile(const std::string& path, std::string* content, std::string* err); | 44 | bool ReadFile(const std::string& path, std::string* content, std::string* err); |
45 | bool WriteFile(const std::string& path, const std::string& content, std::string* err); | 45 | bool WriteFile(const std::string& path, const std::string& content, std::string* err); |
46 | 46 | ||
47 | class Timer { | ||
48 | public: | ||
49 | Timer() : start_(boot_clock::now()) {} | ||
50 | |||
51 | double duration_s() const { | ||
52 | typedef std::chrono::duration<double> double_duration; | ||
53 | return std::chrono::duration_cast<double_duration>(boot_clock::now() - start_).count(); | ||
54 | } | ||
55 | |||
56 | int64_t duration_ms() const { | ||
57 | return std::chrono::duration_cast<std::chrono::milliseconds>(boot_clock::now() - start_) | ||
58 | .count(); | ||
59 | } | ||
60 | |||
61 | private: | ||
62 | android::base::boot_clock::time_point start_; | ||
63 | }; | ||
64 | |||
65 | std::ostream& operator<<(std::ostream& os, const Timer& t); | ||
66 | |||
67 | bool DecodeUid(const std::string& name, uid_t* uid, std::string* err); | 47 | bool DecodeUid(const std::string& name, uid_t* uid, std::string* err); |
68 | 48 | ||
69 | int mkdir_recursive(const std::string& pathname, mode_t mode, selabel_handle* sehandle); | 49 | int mkdir_recursive(const std::string& pathname, mode_t mode, selabel_handle* sehandle); |