summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Gao2017-04-28 14:39:48 -0500
committerJosh Gao2017-04-28 15:00:00 -0500
commitae29339ca138648eeaee68f801feb02d85ee2abf (patch)
treefae72a160e4812f326207e25b40a167980d649fd
parent263e1e95093b9f66b7d62cbd622e09b6a8b9252f (diff)
downloadplatform-system-core-ae29339ca138648eeaee68f801feb02d85ee2abf.tar.gz
platform-system-core-ae29339ca138648eeaee68f801feb02d85ee2abf.tar.xz
platform-system-core-ae29339ca138648eeaee68f801feb02d85ee2abf.zip
base: make boot_clock work on host linux, hide it on non-linux.
boot_clock was previously returning zero on any platform that doesn't define __ANDROID__, including host bionic. Instead of returning a bogus value, just hide it on non-Linux platforms. Bug: http://b/37758947 Test: libbase_test32/64 on linux Change-Id: I96e1d8b92dc44c6308408900cf0d27e1e7db5569
-rw-r--r--base/Android.bp5
-rw-r--r--base/chrono_utils.cpp7
-rw-r--r--base/chrono_utils_test.cpp9
-rw-r--r--base/include/android-base/chrono_utils.h2
4 files changed, 12 insertions, 11 deletions
diff --git a/base/Android.bp b/base/Android.bp
index 81b96db43..14bfe36ee 100644
--- a/base/Android.bp
+++ b/base/Android.bp
@@ -42,6 +42,7 @@ cc_library {
42 clang: true, 42 clang: true,
43 host_supported: true, 43 host_supported: true,
44 srcs: [ 44 srcs: [
45 "chrono_utils.cpp",
45 "file.cpp", 46 "file.cpp",
46 "logging.cpp", 47 "logging.cpp",
47 "parsenetaddress.cpp", 48 "parsenetaddress.cpp",
@@ -64,7 +65,6 @@ cc_library {
64 srcs: [ 65 srcs: [
65 "errors_unix.cpp", 66 "errors_unix.cpp",
66 "properties.cpp", 67 "properties.cpp",
67 "chrono_utils.cpp",
68 ], 68 ],
69 cppflags: ["-Wexit-time-destructors"], 69 cppflags: ["-Wexit-time-destructors"],
70 sanitize: { 70 sanitize: {
@@ -74,14 +74,12 @@ cc_library {
74 }, 74 },
75 darwin: { 75 darwin: {
76 srcs: [ 76 srcs: [
77 "chrono_utils.cpp",
78 "errors_unix.cpp", 77 "errors_unix.cpp",
79 ], 78 ],
80 cppflags: ["-Wexit-time-destructors"], 79 cppflags: ["-Wexit-time-destructors"],
81 }, 80 },
82 linux_bionic: { 81 linux_bionic: {
83 srcs: [ 82 srcs: [
84 "chrono_utils.cpp",
85 "errors_unix.cpp", 83 "errors_unix.cpp",
86 ], 84 ],
87 cppflags: ["-Wexit-time-destructors"], 85 cppflags: ["-Wexit-time-destructors"],
@@ -89,7 +87,6 @@ cc_library {
89 }, 87 },
90 linux: { 88 linux: {
91 srcs: [ 89 srcs: [
92 "chrono_utils.cpp",
93 "errors_unix.cpp", 90 "errors_unix.cpp",
94 ], 91 ],
95 cppflags: ["-Wexit-time-destructors"], 92 cppflags: ["-Wexit-time-destructors"],
diff --git a/base/chrono_utils.cpp b/base/chrono_utils.cpp
index 5eedf3bce..d73b551b5 100644
--- a/base/chrono_utils.cpp
+++ b/base/chrono_utils.cpp
@@ -21,17 +21,14 @@
21namespace android { 21namespace android {
22namespace base { 22namespace base {
23 23
24#if defined(__linux__)
24boot_clock::time_point boot_clock::now() { 25boot_clock::time_point boot_clock::now() {
25#ifdef __ANDROID__
26 timespec ts; 26 timespec ts;
27 clock_gettime(CLOCK_BOOTTIME, &ts); 27 clock_gettime(CLOCK_BOOTTIME, &ts);
28 return boot_clock::time_point(std::chrono::seconds(ts.tv_sec) + 28 return boot_clock::time_point(std::chrono::seconds(ts.tv_sec) +
29 std::chrono::nanoseconds(ts.tv_nsec)); 29 std::chrono::nanoseconds(ts.tv_nsec));
30#else
31 // Darwin does not support clock_gettime.
32 return boot_clock::time_point();
33#endif // __ANDROID__
34} 30}
31#endif
35 32
36} // namespace base 33} // namespace base
37} // namespace android 34} // namespace android
diff --git a/base/chrono_utils_test.cpp b/base/chrono_utils_test.cpp
index 057132d9f..1f1ce15b1 100644
--- a/base/chrono_utils_test.cpp
+++ b/base/chrono_utils_test.cpp
@@ -16,6 +16,7 @@
16 16
17#include "android-base/chrono_utils.h" 17#include "android-base/chrono_utils.h"
18 18
19#include <err.h>
19#include <time.h> 20#include <time.h>
20 21
21#include <chrono> 22#include <chrono>
@@ -25,9 +26,12 @@
25namespace android { 26namespace android {
26namespace base { 27namespace base {
27 28
29#if defined(__linux__)
28std::chrono::seconds GetBootTimeSeconds() { 30std::chrono::seconds GetBootTimeSeconds() {
29 struct timespec now; 31 struct timespec now;
30 clock_gettime(CLOCK_BOOTTIME, &now); 32 if (clock_gettime(CLOCK_BOOTTIME, &now) != 0) {
33 err(1, "clock_gettime failed");
34 }
31 35
32 auto now_tp = boot_clock::time_point(std::chrono::seconds(now.tv_sec) + 36 auto now_tp = boot_clock::time_point(std::chrono::seconds(now.tv_sec) +
33 std::chrono::nanoseconds(now.tv_nsec)); 37 std::chrono::nanoseconds(now.tv_nsec));
@@ -41,6 +45,7 @@ TEST(ChronoUtilsTest, BootClockNowSeconds) {
41 std::chrono::duration_cast<std::chrono::seconds>(boot_clock::now().time_since_epoch()); 45 std::chrono::duration_cast<std::chrono::seconds>(boot_clock::now().time_since_epoch());
42 EXPECT_EQ(now, boot_seconds); 46 EXPECT_EQ(now, boot_seconds);
43} 47}
48#endif // defined(__linux__)
44 49
45} // namespace base 50} // namespace base
46} // namespace android \ No newline at end of file 51} // namespace android
diff --git a/base/include/android-base/chrono_utils.h b/base/include/android-base/chrono_utils.h
index 0086425e5..795225b9c 100644
--- a/base/include/android-base/chrono_utils.h
+++ b/base/include/android-base/chrono_utils.h
@@ -22,6 +22,7 @@
22namespace android { 22namespace android {
23namespace base { 23namespace base {
24 24
25#if defined(__linux__)
25// A std::chrono clock based on CLOCK_BOOTTIME. 26// A std::chrono clock based on CLOCK_BOOTTIME.
26class boot_clock { 27class boot_clock {
27 public: 28 public:
@@ -30,6 +31,7 @@ class boot_clock {
30 31
31 static time_point now(); 32 static time_point now();
32}; 33};
34#endif // defined(__linux__)
33 35
34} // namespace base 36} // namespace base
35} // namespace android 37} // namespace android