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

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"
fmeawad 2016/05/09 21:50:32 This line should be untouched in this CL.
lpy 2016/05/09 22:13:41 Done.
23 #endif 23 #endif
24 #include "src/base/cpu.h" 24 #include "src/base/cpu.h"
25 #include "src/base/logging.h" 25 #include "src/base/logging.h"
26 #include "src/base/platform/platform.h" 26 #include "src/base/platform/platform.h"
27 #include "src/base/safe_math.h"
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 USE(kr);
fmeawad 2016/05/09 21:50:32 You are loosing the check here that kr == KERN_SUC
lpy 2016/05/09 22:13:41 Done.
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) {
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) {
fmeawad 2016/05/09 21:50:32 If this method is only used for CLOCK_THREAD_CPU,
lpy 2016/05/09 22:13:41 Done.
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
27 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) {
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 } 609 }
554 610
555 611
556 // static 612 // static
557 bool TimeTicks::IsHighResolutionClockWorking() { 613 bool TimeTicks::IsHighResolutionClockWorking() {
558 return true; 614 return true;
559 } 615 }
560 616
561 #endif // V8_OS_WIN 617 #endif // V8_OS_WIN
562 618
619
620 bool ThreadTicks::IsSupported() {
621 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
622 defined(V8_OS_MACOSX) || defined(V8_OS_ANDROID)
623 return true;
624 #else
625 return false;
626 #endif
627 }
628
629
630 ThreadTicks ThreadTicks::Now() {
631 #if V8_OS_MACOSX
632 return ThreadTicks(ComputeThreadTicks());
633 #elif(defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
634 defined(V8_OS_ANDROID)
635 return ThreadTicks(ClockNow(CLOCK_THREAD_CPUTIME_ID));
636 #else
637 UNREACHABLE();
638 return ThreadTicks();
639 #endif
640 }
641
563 } // namespace base 642 } // namespace base
564 } // namespace v8 643 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698