OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/base/platform/time.h" | 5 #include "src/base/platform/time.h" |
6 | 6 |
7 #if V8_OS_POSIX | 7 #if V8_OS_POSIX |
8 #include <fcntl.h> // for O_RDONLY | 8 #include <fcntl.h> // for O_RDONLY |
9 #include <sys/time.h> | 9 #include <sys/time.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 #endif | 11 #endif |
12 #if V8_OS_MACOSX | 12 #if V8_OS_MACOSX |
| 13 #include <mach/mach.h> |
13 #include <mach/mach_time.h> | 14 #include <mach/mach_time.h> |
| 15 #include <pthread.h> |
14 #endif | 16 #endif |
15 | 17 |
16 #include <cstring> | 18 #include <cstring> |
17 #include <ostream> | 19 #include <ostream> |
18 | 20 |
19 #if V8_OS_WIN | 21 #if V8_OS_WIN |
20 #include "src/base/atomicops.h" | 22 #include "src/base/atomicops.h" |
21 #include "src/base/lazy-instance.h" | 23 #include "src/base/lazy-instance.h" |
22 #include "src/base/win32-headers.h" | 24 #include "src/base/win32-headers.h" |
23 #endif | 25 #endif |
24 #include "src/base/cpu.h" | 26 #include "src/base/cpu.h" |
25 #include "src/base/logging.h" | 27 #include "src/base/logging.h" |
26 #include "src/base/platform/platform.h" | 28 #include "src/base/platform/platform.h" |
27 | 29 |
| 30 namespace { |
| 31 |
| 32 #if V8_OS_MACOSX |
| 33 int64_t ComputeThreadTicks() { |
| 34 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT; |
| 35 thread_basic_info_data_t thread_info_data; |
| 36 kern_return_t kr = thread_info( |
| 37 pthread_mach_thread_np(pthread_self()), |
| 38 THREAD_BASIC_INFO, |
| 39 reinterpret_cast<thread_info_t>(&thread_info_data), |
| 40 &thread_info_count); |
| 41 DCHECK(kr == KERN_SUCCESS); |
| 42 |
| 43 v8::base::CheckedNumeric<int64_t> absolute_micros( |
| 44 thread_info_data.user_time.seconds); |
| 45 absolute_micros *= v8::base::Time::kMicrosecondsPerSecond; |
| 46 absolute_micros += thread_info_data.user_time.microseconds; |
| 47 return absolute_micros.ValueOrDie(); |
| 48 } |
| 49 #elif V8_OS_POSIX |
| 50 // Helper function to get results from clock_gettime() and convert to a |
| 51 // microsecond timebase. Minimum requirement is MONOTONIC_CLOCK to be supported |
| 52 // on the system. FreeBSD 6 has CLOCK_MONOTONIC but defines |
| 53 // _POSIX_MONOTONIC_CLOCK to -1. |
| 54 inline int64_t ClockNow(clockid_t clk_id) { |
| 55 #if (defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ |
| 56 defined(V8_OS_BSD) || defined(V8_OS_ANDROID) |
| 57 struct timespec ts; |
| 58 if (clock_gettime(clk_id, &ts) != 0) { |
| 59 UNREACHABLE(); |
| 60 return 0; |
| 61 } |
| 62 v8::base::internal::CheckedNumeric<int64_t> result(ts.tv_sec); |
| 63 result *= v8::base::Time::kMicrosecondsPerSecond; |
| 64 result += (ts.tv_nsec / v8::base::Time::kNanosecondsPerMicrosecond); |
| 65 return result.ValueOrDie(); |
| 66 #else // Monotonic clock not supported. |
| 67 return 0; |
| 68 #endif |
| 69 } |
| 70 #endif // V8_OS_MACOSX |
| 71 |
| 72 |
| 73 } // namespace |
| 74 |
28 namespace v8 { | 75 namespace v8 { |
29 namespace base { | 76 namespace base { |
30 | 77 |
31 TimeDelta TimeDelta::FromDays(int days) { | 78 TimeDelta TimeDelta::FromDays(int days) { |
32 return TimeDelta(days * Time::kMicrosecondsPerDay); | 79 return TimeDelta(days * Time::kMicrosecondsPerDay); |
33 } | 80 } |
34 | 81 |
35 | 82 |
36 TimeDelta TimeDelta::FromHours(int hours) { | 83 TimeDelta TimeDelta::FromHours(int hours) { |
37 return TimeDelta(hours * Time::kMicrosecondsPerHour); | 84 return TimeDelta(hours * Time::kMicrosecondsPerHour); |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 if (info.denom == 0) { | 581 if (info.denom == 0) { |
535 kern_return_t result = mach_timebase_info(&info); | 582 kern_return_t result = mach_timebase_info(&info); |
536 DCHECK_EQ(KERN_SUCCESS, result); | 583 DCHECK_EQ(KERN_SUCCESS, result); |
537 USE(result); | 584 USE(result); |
538 } | 585 } |
539 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond * | 586 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond * |
540 info.numer / info.denom); | 587 info.numer / info.denom); |
541 #elif V8_OS_SOLARIS | 588 #elif V8_OS_SOLARIS |
542 ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond); | 589 ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond); |
543 #elif V8_OS_POSIX | 590 #elif V8_OS_POSIX |
544 struct timespec ts; | 591 ticks = ClockNow(CLOCK_MONOTONIC); |
545 int result = clock_gettime(CLOCK_MONOTONIC, &ts); | |
546 DCHECK_EQ(0, result); | |
547 USE(result); | |
548 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + | |
549 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); | |
550 #endif // V8_OS_MACOSX | 592 #endif // V8_OS_MACOSX |
551 // Make sure we never return 0 here. | 593 // Make sure we never return 0 here. |
552 return TimeTicks(ticks + 1); | 594 return TimeTicks(ticks + 1); |
553 } | 595 } |
554 | 596 |
555 | 597 |
556 // static | 598 // static |
557 bool TimeTicks::IsHighResolutionClockWorking() { | 599 bool TimeTicks::IsHighResolutionClockWorking() { |
558 return true; | 600 return true; |
559 } | 601 } |
560 | 602 |
561 #endif // V8_OS_WIN | 603 #endif // V8_OS_WIN |
562 | 604 |
| 605 |
| 606 // TODO(lpy): For windows ThreadTicks implementation, |
| 607 // see http://crbug.com/v8/5000 |
| 608 bool ThreadTicks::IsSupported() { |
| 609 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ |
| 610 defined(V8_OS_MACOSX) || defined(V8_OS_ANDROID) |
| 611 return true; |
| 612 #else |
| 613 return false; |
| 614 #endif |
| 615 } |
| 616 |
| 617 |
| 618 ThreadTicks ThreadTicks::Now() { |
| 619 #if V8_OS_MACOSX |
| 620 return ThreadTicks(ComputeThreadTicks()); |
| 621 #elif(defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ |
| 622 defined(V8_OS_ANDROID) |
| 623 return ThreadTicks(ClockNow(CLOCK_THREAD_CPUTIME_ID)); |
| 624 #else |
| 625 UNREACHABLE(); |
| 626 return ThreadTicks(); |
| 627 #endif |
| 628 } |
| 629 |
563 } // namespace base | 630 } // namespace base |
564 } // namespace v8 | 631 } // namespace v8 |
OLD | NEW |