Chromium Code Reviews| Index: src/base/platform/time.cc |
| diff --git a/src/base/platform/time.cc b/src/base/platform/time.cc |
| index 6d5e538970cfd8e8b3608c196b79c361a44c4107..8d56f5552d6db55a41e0f87c55d4449f13be2317 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 |
| @@ -25,6 +26,61 @@ |
| #include "src/base/logging.h" |
| #include "src/base/platform/platform.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); |
| + DCHECK(kr == KERN_SUCCESS); |
| + |
| + 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) { |
|
fmeawad
2016/05/09 23:22:33
I would inline this method as well since it is onl
lpy
2016/05/09 23:51:15
Done.
fmeawad
2016/05/10 18:12:48
I meant move the code to where it is called.
|
| + 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) { |
| +#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 { |
| @@ -541,12 +597,7 @@ TimeTicks TimeTicks::HighResolutionNow() { |
| #elif V8_OS_SOLARIS |
| ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond); |
| #elif V8_OS_POSIX |
| - struct timespec ts; |
| - int result = clock_gettime(CLOCK_MONOTONIC, &ts); |
| - DCHECK_EQ(0, result); |
| - USE(result); |
| - ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + |
| - ts.tv_nsec / Time::kNanosecondsPerMicrosecond); |
| + ticks = ClockNow(CLOCK_MONOTONIC); |
| #endif // V8_OS_MACOSX |
| // Make sure we never return 0 here. |
| return TimeTicks(ticks + 1); |
| @@ -560,5 +611,28 @@ bool TimeTicks::IsHighResolutionClockWorking() { |
| #endif // V8_OS_WIN |
| + |
| +bool ThreadTicks::IsSupported() { |
| +#if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ |
|
fmeawad
2016/05/09 23:22:33
Add comment with the bug number that tracks Window
lpy
2016/05/09 23:51:15
Done.
|
| + 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 |