Index: src/base/platform/time.cc |
diff --git a/src/base/platform/time.cc b/src/base/platform/time.cc |
index 6d5e538970cfd8e8b3608c196b79c361a44c4107..14a0d1e6db97be0cb97ca91630bd1d99e55f67f8 100644 |
--- a/src/base/platform/time.cc |
+++ b/src/base/platform/time.cc |
@@ -10,6 +10,7 @@ |
#include <unistd.h> |
#endif |
#if V8_OS_MACOSX |
+#include <mach/mach.h> |
#include <mach/mach_time.h> |
#endif |
@@ -19,11 +20,66 @@ |
#if V8_OS_WIN |
#include "src/base/atomicops.h" |
#include "src/base/lazy-instance.h" |
-#include "src/base/win32-headers.h" |
fmeawad
2016/05/09 21:50:32
This line should be untouched in this CL.
lpy
2016/05/09 22:13:41
Done.
|
#endif |
#include "src/base/cpu.h" |
#include "src/base/logging.h" |
#include "src/base/platform/platform.h" |
+#include "src/base/safe_math.h" |
+ |
+namespace { |
+ |
+#if V8_OS_MACOSX |
+int64_t ComputeThreadTicks() { |
+ mach_port_t thread = mach_thread_self(); |
+ mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT; |
+ thread_basic_info_data_t thread_info_data; |
+ |
+ if (thread == MACH_PORT_NULL) { |
+ return 0; |
+ } |
+ |
+ kern_return_t kr = thread_info( |
+ thread, |
+ THREAD_BASIC_INFO, |
+ reinterpret_cast<thread_info_t>(&thread_info_data), |
+ &thread_info_count); |
+ USE(kr); |
fmeawad
2016/05/09 21:50:32
You are loosing the check here that kr == KERN_SUC
lpy
2016/05/09 22:13:41
Done.
|
+ |
+ v8::base::CheckedNumeric<int64_t> absolute_micros( |
+ thread_info_data.user_time.seconds); |
+ absolute_micros *= v8::base::Time::kMicrosecondsPerSecond; |
+ absolute_micros += thread_info_data.user_time.microseconds; |
+ return absolute_micros.ValueOrDie(); |
+} |
+#elif V8_OS_POSIX |
+int64_t ConvertTimespecToMicros(const struct timespec& ts) { |
+ v8::base::internal::CheckedNumeric<int64_t> result(ts.tv_sec); |
+ result *= v8::base::Time::kMicrosecondsPerSecond; |
+ result += (ts.tv_nsec / v8::base::Time::kNanosecondsPerMicrosecond); |
+ return result.ValueOrDie(); |
+} |
+ |
+// Helper function to get results from clock_gettime() and convert to a |
+// microsecond timebase. Minimum requirement is MONOTONIC_CLOCK to be supported |
+// on the system. FreeBSD 6 has CLOCK_MONOTONIC but defines |
+// _POSIX_MONOTONIC_CLOCK to -1. |
+int64_t ClockNow(clockid_t clk_id) { |
fmeawad
2016/05/09 21:50:32
If this method is only used for CLOCK_THREAD_CPU,
lpy
2016/05/09 22:13:41
Done.
|
+#if (defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ |
+ defined(V8_OS_BSD) || defined(V8_OS_ANDROID) |
+ struct timespec ts; |
+ if (clock_gettime(clk_id, &ts) != 0) { |
+ UNREACHABLE(); |
+ return 0; |
+ } |
+ return ConvertTimespecToMicros(ts); |
+#else // Monotonic clock not supported. |
+ return 0; |
+#endif |
+} |
+#endif // V8_OS_MACOSX |
+ |
+ |
+} // namespace |
namespace v8 { |
namespace base { |
@@ -560,5 +616,28 @@ bool TimeTicks::IsHighResolutionClockWorking() { |
#endif // V8_OS_WIN |
+ |
+bool ThreadTicks::IsSupported() { |
+#if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ |
+ defined(V8_OS_MACOSX) || defined(V8_OS_ANDROID) |
+ return true; |
+#else |
+ return false; |
+#endif |
+} |
+ |
+ |
+ThreadTicks ThreadTicks::Now() { |
+#if V8_OS_MACOSX |
+ return ThreadTicks(ComputeThreadTicks()); |
+#elif(defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ |
+ defined(V8_OS_ANDROID) |
+ return ThreadTicks(ClockNow(CLOCK_THREAD_CPUTIME_ID)); |
+#else |
+ UNREACHABLE(); |
+ return ThreadTicks(); |
+#endif |
+} |
+ |
} // namespace base |
} // namespace v8 |