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> |
14 #endif | 15 #endif |
15 | 16 |
16 #include <cstring> | 17 #include <cstring> |
17 #include <ostream> | 18 #include <ostream> |
18 | 19 |
19 #if V8_OS_WIN | 20 #if V8_OS_WIN |
20 #include "src/base/atomicops.h" | 21 #include "src/base/atomicops.h" |
21 #include "src/base/lazy-instance.h" | 22 #include "src/base/lazy-instance.h" |
22 #include "src/base/win32-headers.h" | 23 #include "src/base/win32-headers.h" |
23 #endif | 24 #endif |
24 #include "src/base/cpu.h" | 25 #include "src/base/cpu.h" |
25 #include "src/base/logging.h" | 26 #include "src/base/logging.h" |
26 #include "src/base/platform/platform.h" | 27 #include "src/base/platform/platform.h" |
27 | 28 |
29 namespace { | |
30 | |
31 #if V8_OS_MACOSX | |
32 int64_t ComputeThreadTicks() { | |
33 mach_port_t thread = mach_thread_self(); | |
Robert Sesek
2016/05/10 18:58:07
The thread port is getting leaked here.
lpy
2016/05/10 19:10:39
Done.
| |
34 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT; | |
35 thread_basic_info_data_t thread_info_data; | |
36 | |
37 if (thread == MACH_PORT_NULL) { | |
38 return 0; | |
39 } | |
40 | |
41 kern_return_t kr = thread_info( | |
42 thread, | |
43 THREAD_BASIC_INFO, | |
44 reinterpret_cast<thread_info_t>(&thread_info_data), | |
45 &thread_info_count); | |
46 DCHECK(kr == KERN_SUCCESS); | |
47 | |
48 v8::base::CheckedNumeric<int64_t> absolute_micros( | |
49 thread_info_data.user_time.seconds); | |
50 absolute_micros *= v8::base::Time::kMicrosecondsPerSecond; | |
51 absolute_micros += thread_info_data.user_time.microseconds; | |
52 return absolute_micros.ValueOrDie(); | |
53 } | |
54 #elif V8_OS_POSIX | |
55 // Helper function to get results from clock_gettime() and convert to a | |
56 // microsecond timebase. Minimum requirement is MONOTONIC_CLOCK to be supported | |
57 // on the system. FreeBSD 6 has CLOCK_MONOTONIC but defines | |
58 // _POSIX_MONOTONIC_CLOCK to -1. | |
59 inline int64_t ClockNow(clockid_t clk_id) { | |
60 #if (defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ | |
61 defined(V8_OS_BSD) || defined(V8_OS_ANDROID) | |
62 struct timespec ts; | |
63 if (clock_gettime(clk_id, &ts) != 0) { | |
64 UNREACHABLE(); | |
65 return 0; | |
66 } | |
67 v8::base::internal::CheckedNumeric<int64_t> result(ts.tv_sec); | |
68 result *= v8::base::Time::kMicrosecondsPerSecond; | |
69 result += (ts.tv_nsec / v8::base::Time::kNanosecondsPerMicrosecond); | |
70 return result.ValueOrDie(); | |
71 #else // Monotonic clock not supported. | |
72 return 0; | |
73 #endif | |
74 } | |
75 #endif // V8_OS_MACOSX | |
76 | |
77 | |
78 } // namespace | |
79 | |
28 namespace v8 { | 80 namespace v8 { |
29 namespace base { | 81 namespace base { |
30 | 82 |
31 TimeDelta TimeDelta::FromDays(int days) { | 83 TimeDelta TimeDelta::FromDays(int days) { |
32 return TimeDelta(days * Time::kMicrosecondsPerDay); | 84 return TimeDelta(days * Time::kMicrosecondsPerDay); |
33 } | 85 } |
34 | 86 |
35 | 87 |
36 TimeDelta TimeDelta::FromHours(int hours) { | 88 TimeDelta TimeDelta::FromHours(int hours) { |
37 return TimeDelta(hours * Time::kMicrosecondsPerHour); | 89 return TimeDelta(hours * Time::kMicrosecondsPerHour); |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
534 if (info.denom == 0) { | 586 if (info.denom == 0) { |
535 kern_return_t result = mach_timebase_info(&info); | 587 kern_return_t result = mach_timebase_info(&info); |
536 DCHECK_EQ(KERN_SUCCESS, result); | 588 DCHECK_EQ(KERN_SUCCESS, result); |
537 USE(result); | 589 USE(result); |
538 } | 590 } |
539 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond * | 591 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond * |
540 info.numer / info.denom); | 592 info.numer / info.denom); |
541 #elif V8_OS_SOLARIS | 593 #elif V8_OS_SOLARIS |
542 ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond); | 594 ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond); |
543 #elif V8_OS_POSIX | 595 #elif V8_OS_POSIX |
544 struct timespec ts; | 596 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 | 597 #endif // V8_OS_MACOSX |
551 // Make sure we never return 0 here. | 598 // Make sure we never return 0 here. |
552 return TimeTicks(ticks + 1); | 599 return TimeTicks(ticks + 1); |
553 } | 600 } |
554 | 601 |
555 | 602 |
556 // static | 603 // static |
557 bool TimeTicks::IsHighResolutionClockWorking() { | 604 bool TimeTicks::IsHighResolutionClockWorking() { |
558 return true; | 605 return true; |
559 } | 606 } |
560 | 607 |
561 #endif // V8_OS_WIN | 608 #endif // V8_OS_WIN |
562 | 609 |
610 | |
611 // TODO(lpy): For windows ThreadTicks implementation, | |
612 // see http://crbug.com/v8/5000 | |
613 bool ThreadTicks::IsSupported() { | |
614 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ | |
615 defined(V8_OS_MACOSX) || defined(V8_OS_ANDROID) | |
616 return true; | |
617 #else | |
618 return false; | |
619 #endif | |
620 } | |
621 | |
622 | |
623 ThreadTicks ThreadTicks::Now() { | |
624 #if V8_OS_MACOSX | |
625 return ThreadTicks(ComputeThreadTicks()); | |
626 #elif(defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ | |
627 defined(V8_OS_ANDROID) | |
628 return ThreadTicks(ClockNow(CLOCK_THREAD_CPUTIME_ID)); | |
629 #else | |
630 UNREACHABLE(); | |
631 return ThreadTicks(); | |
632 #endif | |
633 } | |
634 | |
563 } // namespace base | 635 } // namespace base |
564 } // namespace v8 | 636 } // namespace v8 |
OLD | NEW |