| 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> | |
| 14 #include <mach/mach_time.h> | 13 #include <mach/mach_time.h> |
| 15 #include <pthread.h> | |
| 16 #endif | 14 #endif |
| 17 | 15 |
| 18 #include <cstring> | 16 #include <cstring> |
| 19 #include <ostream> | 17 #include <ostream> |
| 20 | 18 |
| 21 #if V8_OS_WIN | 19 #if V8_OS_WIN |
| 22 #include "src/base/atomicops.h" | 20 #include "src/base/atomicops.h" |
| 23 #include "src/base/lazy-instance.h" | 21 #include "src/base/lazy-instance.h" |
| 24 #include "src/base/win32-headers.h" | 22 #include "src/base/win32-headers.h" |
| 25 #endif | 23 #endif |
| 26 #include "src/base/cpu.h" | 24 #include "src/base/cpu.h" |
| 27 #include "src/base/logging.h" | 25 #include "src/base/logging.h" |
| 28 #include "src/base/platform/platform.h" | 26 #include "src/base/platform/platform.h" |
| 29 | 27 |
| 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 CHECK(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 | |
| 75 namespace v8 { | 28 namespace v8 { |
| 76 namespace base { | 29 namespace base { |
| 77 | 30 |
| 78 TimeDelta TimeDelta::FromDays(int days) { | 31 TimeDelta TimeDelta::FromDays(int days) { |
| 79 return TimeDelta(days * Time::kMicrosecondsPerDay); | 32 return TimeDelta(days * Time::kMicrosecondsPerDay); |
| 80 } | 33 } |
| 81 | 34 |
| 82 | 35 |
| 83 TimeDelta TimeDelta::FromHours(int hours) { | 36 TimeDelta TimeDelta::FromHours(int hours) { |
| 84 return TimeDelta(hours * Time::kMicrosecondsPerHour); | 37 return TimeDelta(hours * Time::kMicrosecondsPerHour); |
| (...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 if (info.denom == 0) { | 534 if (info.denom == 0) { |
| 582 kern_return_t result = mach_timebase_info(&info); | 535 kern_return_t result = mach_timebase_info(&info); |
| 583 DCHECK_EQ(KERN_SUCCESS, result); | 536 DCHECK_EQ(KERN_SUCCESS, result); |
| 584 USE(result); | 537 USE(result); |
| 585 } | 538 } |
| 586 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond * | 539 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond * |
| 587 info.numer / info.denom); | 540 info.numer / info.denom); |
| 588 #elif V8_OS_SOLARIS | 541 #elif V8_OS_SOLARIS |
| 589 ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond); | 542 ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond); |
| 590 #elif V8_OS_POSIX | 543 #elif V8_OS_POSIX |
| 591 ticks = ClockNow(CLOCK_MONOTONIC); | 544 struct timespec ts; |
| 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); |
| 592 #endif // V8_OS_MACOSX | 550 #endif // V8_OS_MACOSX |
| 593 // Make sure we never return 0 here. | 551 // Make sure we never return 0 here. |
| 594 return TimeTicks(ticks + 1); | 552 return TimeTicks(ticks + 1); |
| 595 } | 553 } |
| 596 | 554 |
| 597 | 555 |
| 598 // static | 556 // static |
| 599 bool TimeTicks::IsHighResolutionClockWorking() { | 557 bool TimeTicks::IsHighResolutionClockWorking() { |
| 600 return true; | 558 return true; |
| 601 } | 559 } |
| 602 | 560 |
| 603 #endif // V8_OS_WIN | 561 #endif // V8_OS_WIN |
| 604 | 562 |
| 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 | |
| 630 } // namespace base | 563 } // namespace base |
| 631 } // namespace v8 | 564 } // namespace v8 |
| OLD | NEW |