Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(471)

Side by Side Diff: src/base/platform/time.cc

Issue 1959103004: Implement CPU time for OS X and POSIX. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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();
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 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.
56 v8::base::internal::CheckedNumeric<int64_t> result(ts.tv_sec);
57 result *= v8::base::Time::kMicrosecondsPerSecond;
58 result += (ts.tv_nsec / v8::base::Time::kNanosecondsPerMicrosecond);
59 return result.ValueOrDie();
60 }
61
62 // Helper function to get results from clock_gettime() and convert to a
63 // microsecond timebase. Minimum requirement is MONOTONIC_CLOCK to be supported
64 // on the system. FreeBSD 6 has CLOCK_MONOTONIC but defines
65 // _POSIX_MONOTONIC_CLOCK to -1.
66 int64_t ClockNow(clockid_t clk_id) {
67 #if (defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \
68 defined(V8_OS_BSD) || defined(V8_OS_ANDROID)
69 struct timespec ts;
70 if (clock_gettime(clk_id, &ts) != 0) {
71 UNREACHABLE();
72 return 0;
73 }
74 return ConvertTimespecToMicros(ts);
75 #else // Monotonic clock not supported.
76 return 0;
77 #endif
78 }
79 #endif // V8_OS_MACOSX
80
81
82 } // namespace
83
28 namespace v8 { 84 namespace v8 {
29 namespace base { 85 namespace base {
30 86
31 TimeDelta TimeDelta::FromDays(int days) { 87 TimeDelta TimeDelta::FromDays(int days) {
32 return TimeDelta(days * Time::kMicrosecondsPerDay); 88 return TimeDelta(days * Time::kMicrosecondsPerDay);
33 } 89 }
34 90
35 91
36 TimeDelta TimeDelta::FromHours(int hours) { 92 TimeDelta TimeDelta::FromHours(int hours) {
37 return TimeDelta(hours * Time::kMicrosecondsPerHour); 93 return TimeDelta(hours * Time::kMicrosecondsPerHour);
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 if (info.denom == 0) { 590 if (info.denom == 0) {
535 kern_return_t result = mach_timebase_info(&info); 591 kern_return_t result = mach_timebase_info(&info);
536 DCHECK_EQ(KERN_SUCCESS, result); 592 DCHECK_EQ(KERN_SUCCESS, result);
537 USE(result); 593 USE(result);
538 } 594 }
539 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond * 595 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond *
540 info.numer / info.denom); 596 info.numer / info.denom);
541 #elif V8_OS_SOLARIS 597 #elif V8_OS_SOLARIS
542 ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond); 598 ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond);
543 #elif V8_OS_POSIX 599 #elif V8_OS_POSIX
544 struct timespec ts; 600 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 601 #endif // V8_OS_MACOSX
551 // Make sure we never return 0 here. 602 // Make sure we never return 0 here.
552 return TimeTicks(ticks + 1); 603 return TimeTicks(ticks + 1);
553 } 604 }
554 605
555 606
556 // static 607 // static
557 bool TimeTicks::IsHighResolutionClockWorking() { 608 bool TimeTicks::IsHighResolutionClockWorking() {
558 return true; 609 return true;
559 } 610 }
560 611
561 #endif // V8_OS_WIN 612 #endif // V8_OS_WIN
562 613
614
615 bool ThreadTicks::IsSupported() {
616 #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.
617 defined(V8_OS_MACOSX) || defined(V8_OS_ANDROID)
618 return true;
619 #else
620 return false;
621 #endif
622 }
623
624
625 ThreadTicks ThreadTicks::Now() {
626 #if V8_OS_MACOSX
627 return ThreadTicks(ComputeThreadTicks());
628 #elif(defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
629 defined(V8_OS_ANDROID)
630 return ThreadTicks(ClockNow(CLOCK_THREAD_CPUTIME_ID));
631 #else
632 UNREACHABLE();
633 return ThreadTicks();
634 #endif
635 }
636
563 } // namespace base 637 } // namespace base
564 } // namespace v8 638 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698