blob: b2355a33bde275695d13fe49ceb619ddf12911d5 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
3// found in the LICENSE file.
4
5#include "src/base/platform/time.h"
6
7#if V8_OS_POSIX
8#include <fcntl.h> // for O_RDONLY
9#include <sys/time.h>
10#include <unistd.h>
11#endif
12#if V8_OS_MACOSX
Ben Murdochc5610432016-08-08 18:44:38 +010013#include <mach/mach.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include <mach/mach_time.h>
Ben Murdochc5610432016-08-08 18:44:38 +010015#include <pthread.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016#endif
17
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018#include <cstring>
19#include <ostream>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020
21#if V8_OS_WIN
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022#include "src/base/atomicops.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023#include "src/base/lazy-instance.h"
24#include "src/base/win32-headers.h"
25#endif
26#include "src/base/cpu.h"
27#include "src/base/logging.h"
28#include "src/base/platform/platform.h"
29
Ben Murdochc5610432016-08-08 18:44:38 +010030namespace {
31
32#if V8_OS_MACOSX
33int64_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.
54inline 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075namespace v8 {
76namespace base {
77
78TimeDelta TimeDelta::FromDays(int days) {
79 return TimeDelta(days * Time::kMicrosecondsPerDay);
80}
81
82
83TimeDelta TimeDelta::FromHours(int hours) {
84 return TimeDelta(hours * Time::kMicrosecondsPerHour);
85}
86
87
88TimeDelta TimeDelta::FromMinutes(int minutes) {
89 return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
90}
91
92
93TimeDelta TimeDelta::FromSeconds(int64_t seconds) {
94 return TimeDelta(seconds * Time::kMicrosecondsPerSecond);
95}
96
97
98TimeDelta TimeDelta::FromMilliseconds(int64_t milliseconds) {
99 return TimeDelta(milliseconds * Time::kMicrosecondsPerMillisecond);
100}
101
102
103TimeDelta TimeDelta::FromNanoseconds(int64_t nanoseconds) {
104 return TimeDelta(nanoseconds / Time::kNanosecondsPerMicrosecond);
105}
106
107
108int TimeDelta::InDays() const {
109 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
110}
111
112
113int TimeDelta::InHours() const {
114 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
115}
116
117
118int TimeDelta::InMinutes() const {
119 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
120}
121
122
123double TimeDelta::InSecondsF() const {
124 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
125}
126
127
128int64_t TimeDelta::InSeconds() const {
129 return delta_ / Time::kMicrosecondsPerSecond;
130}
131
132
133double TimeDelta::InMillisecondsF() const {
134 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
135}
136
137
138int64_t TimeDelta::InMilliseconds() const {
139 return delta_ / Time::kMicrosecondsPerMillisecond;
140}
141
142
143int64_t TimeDelta::InNanoseconds() const {
144 return delta_ * Time::kNanosecondsPerMicrosecond;
145}
146
147
148#if V8_OS_MACOSX
149
150TimeDelta TimeDelta::FromMachTimespec(struct mach_timespec ts) {
151 DCHECK_GE(ts.tv_nsec, 0);
152 DCHECK_LT(ts.tv_nsec,
153 static_cast<long>(Time::kNanosecondsPerSecond)); // NOLINT
154 return TimeDelta(ts.tv_sec * Time::kMicrosecondsPerSecond +
155 ts.tv_nsec / Time::kNanosecondsPerMicrosecond);
156}
157
158
159struct mach_timespec TimeDelta::ToMachTimespec() const {
160 struct mach_timespec ts;
161 DCHECK(delta_ >= 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000162 ts.tv_sec = static_cast<unsigned>(delta_ / Time::kMicrosecondsPerSecond);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 ts.tv_nsec = (delta_ % Time::kMicrosecondsPerSecond) *
164 Time::kNanosecondsPerMicrosecond;
165 return ts;
166}
167
168#endif // V8_OS_MACOSX
169
170
171#if V8_OS_POSIX
172
173TimeDelta TimeDelta::FromTimespec(struct timespec ts) {
174 DCHECK_GE(ts.tv_nsec, 0);
175 DCHECK_LT(ts.tv_nsec,
176 static_cast<long>(Time::kNanosecondsPerSecond)); // NOLINT
177 return TimeDelta(ts.tv_sec * Time::kMicrosecondsPerSecond +
178 ts.tv_nsec / Time::kNanosecondsPerMicrosecond);
179}
180
181
182struct timespec TimeDelta::ToTimespec() const {
183 struct timespec ts;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000184 ts.tv_sec = static_cast<time_t>(delta_ / Time::kMicrosecondsPerSecond);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185 ts.tv_nsec = (delta_ % Time::kMicrosecondsPerSecond) *
186 Time::kNanosecondsPerMicrosecond;
187 return ts;
188}
189
190#endif // V8_OS_POSIX
191
192
193#if V8_OS_WIN
194
195// We implement time using the high-resolution timers so that we can get
196// timeouts which are smaller than 10-15ms. To avoid any drift, we
197// periodically resync the internal clock to the system clock.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000198class Clock final {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199 public:
200 Clock() : initial_ticks_(GetSystemTicks()), initial_time_(GetSystemTime()) {}
201
202 Time Now() {
203 // Time between resampling the un-granular clock for this API (1 minute).
204 const TimeDelta kMaxElapsedTime = TimeDelta::FromMinutes(1);
205
206 LockGuard<Mutex> lock_guard(&mutex_);
207
208 // Determine current time and ticks.
209 TimeTicks ticks = GetSystemTicks();
210 Time time = GetSystemTime();
211
212 // Check if we need to synchronize with the system clock due to a backwards
213 // time change or the amount of time elapsed.
214 TimeDelta elapsed = ticks - initial_ticks_;
215 if (time < initial_time_ || elapsed > kMaxElapsedTime) {
216 initial_ticks_ = ticks;
217 initial_time_ = time;
218 return time;
219 }
220
221 return initial_time_ + elapsed;
222 }
223
224 Time NowFromSystemTime() {
225 LockGuard<Mutex> lock_guard(&mutex_);
226 initial_ticks_ = GetSystemTicks();
227 initial_time_ = GetSystemTime();
228 return initial_time_;
229 }
230
231 private:
232 static TimeTicks GetSystemTicks() {
233 return TimeTicks::Now();
234 }
235
236 static Time GetSystemTime() {
237 FILETIME ft;
238 ::GetSystemTimeAsFileTime(&ft);
239 return Time::FromFiletime(ft);
240 }
241
242 TimeTicks initial_ticks_;
243 Time initial_time_;
244 Mutex mutex_;
245};
246
247
248static LazyStaticInstance<Clock, DefaultConstructTrait<Clock>,
249 ThreadSafeInitOnceTrait>::type clock =
250 LAZY_STATIC_INSTANCE_INITIALIZER;
251
252
253Time Time::Now() {
254 return clock.Pointer()->Now();
255}
256
257
258Time Time::NowFromSystemTime() {
259 return clock.Pointer()->NowFromSystemTime();
260}
261
262
263// Time between windows epoch and standard epoch.
264static const int64_t kTimeToEpochInMicroseconds = V8_INT64_C(11644473600000000);
265
266
267Time Time::FromFiletime(FILETIME ft) {
268 if (ft.dwLowDateTime == 0 && ft.dwHighDateTime == 0) {
269 return Time();
270 }
271 if (ft.dwLowDateTime == std::numeric_limits<DWORD>::max() &&
272 ft.dwHighDateTime == std::numeric_limits<DWORD>::max()) {
273 return Max();
274 }
275 int64_t us = (static_cast<uint64_t>(ft.dwLowDateTime) +
276 (static_cast<uint64_t>(ft.dwHighDateTime) << 32)) / 10;
277 return Time(us - kTimeToEpochInMicroseconds);
278}
279
280
281FILETIME Time::ToFiletime() const {
282 DCHECK(us_ >= 0);
283 FILETIME ft;
284 if (IsNull()) {
285 ft.dwLowDateTime = 0;
286 ft.dwHighDateTime = 0;
287 return ft;
288 }
289 if (IsMax()) {
290 ft.dwLowDateTime = std::numeric_limits<DWORD>::max();
291 ft.dwHighDateTime = std::numeric_limits<DWORD>::max();
292 return ft;
293 }
294 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10;
295 ft.dwLowDateTime = static_cast<DWORD>(us);
296 ft.dwHighDateTime = static_cast<DWORD>(us >> 32);
297 return ft;
298}
299
300#elif V8_OS_POSIX
301
302Time Time::Now() {
303 struct timeval tv;
304 int result = gettimeofday(&tv, NULL);
305 DCHECK_EQ(0, result);
306 USE(result);
307 return FromTimeval(tv);
308}
309
310
311Time Time::NowFromSystemTime() {
312 return Now();
313}
314
315
316Time Time::FromTimespec(struct timespec ts) {
317 DCHECK(ts.tv_nsec >= 0);
318 DCHECK(ts.tv_nsec < static_cast<long>(kNanosecondsPerSecond)); // NOLINT
319 if (ts.tv_nsec == 0 && ts.tv_sec == 0) {
320 return Time();
321 }
322 if (ts.tv_nsec == static_cast<long>(kNanosecondsPerSecond - 1) && // NOLINT
323 ts.tv_sec == std::numeric_limits<time_t>::max()) {
324 return Max();
325 }
326 return Time(ts.tv_sec * kMicrosecondsPerSecond +
327 ts.tv_nsec / kNanosecondsPerMicrosecond);
328}
329
330
331struct timespec Time::ToTimespec() const {
332 struct timespec ts;
333 if (IsNull()) {
334 ts.tv_sec = 0;
335 ts.tv_nsec = 0;
336 return ts;
337 }
338 if (IsMax()) {
339 ts.tv_sec = std::numeric_limits<time_t>::max();
340 ts.tv_nsec = static_cast<long>(kNanosecondsPerSecond - 1); // NOLINT
341 return ts;
342 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000343 ts.tv_sec = static_cast<time_t>(us_ / kMicrosecondsPerSecond);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000344 ts.tv_nsec = (us_ % kMicrosecondsPerSecond) * kNanosecondsPerMicrosecond;
345 return ts;
346}
347
348
349Time Time::FromTimeval(struct timeval tv) {
350 DCHECK(tv.tv_usec >= 0);
351 DCHECK(tv.tv_usec < static_cast<suseconds_t>(kMicrosecondsPerSecond));
352 if (tv.tv_usec == 0 && tv.tv_sec == 0) {
353 return Time();
354 }
355 if (tv.tv_usec == static_cast<suseconds_t>(kMicrosecondsPerSecond - 1) &&
356 tv.tv_sec == std::numeric_limits<time_t>::max()) {
357 return Max();
358 }
359 return Time(tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec);
360}
361
362
363struct timeval Time::ToTimeval() const {
364 struct timeval tv;
365 if (IsNull()) {
366 tv.tv_sec = 0;
367 tv.tv_usec = 0;
368 return tv;
369 }
370 if (IsMax()) {
371 tv.tv_sec = std::numeric_limits<time_t>::max();
372 tv.tv_usec = static_cast<suseconds_t>(kMicrosecondsPerSecond - 1);
373 return tv;
374 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000375 tv.tv_sec = static_cast<time_t>(us_ / kMicrosecondsPerSecond);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000376 tv.tv_usec = us_ % kMicrosecondsPerSecond;
377 return tv;
378}
379
380#endif // V8_OS_WIN
381
382
383Time Time::FromJsTime(double ms_since_epoch) {
384 // The epoch is a valid time, so this constructor doesn't interpret
385 // 0 as the null time.
386 if (ms_since_epoch == std::numeric_limits<double>::max()) {
387 return Max();
388 }
389 return Time(
390 static_cast<int64_t>(ms_since_epoch * kMicrosecondsPerMillisecond));
391}
392
393
394double Time::ToJsTime() const {
395 if (IsNull()) {
396 // Preserve 0 so the invalid result doesn't depend on the platform.
397 return 0;
398 }
399 if (IsMax()) {
400 // Preserve max without offset to prevent overflow.
401 return std::numeric_limits<double>::max();
402 }
403 return static_cast<double>(us_) / kMicrosecondsPerMillisecond;
404}
405
406
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407std::ostream& operator<<(std::ostream& os, const Time& time) {
408 return os << time.ToJsTime();
409}
410
411
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412#if V8_OS_WIN
413
414class TickClock {
415 public:
416 virtual ~TickClock() {}
417 virtual int64_t Now() = 0;
418 virtual bool IsHighResolution() = 0;
419};
420
421
422// Overview of time counters:
423// (1) CPU cycle counter. (Retrieved via RDTSC)
424// The CPU counter provides the highest resolution time stamp and is the least
425// expensive to retrieve. However, the CPU counter is unreliable and should not
426// be used in production. Its biggest issue is that it is per processor and it
427// is not synchronized between processors. Also, on some computers, the counters
428// will change frequency due to thermal and power changes, and stop in some
429// states.
430//
431// (2) QueryPerformanceCounter (QPC). The QPC counter provides a high-
432// resolution (100 nanoseconds) time stamp but is comparatively more expensive
433// to retrieve. What QueryPerformanceCounter actually does is up to the HAL.
434// (with some help from ACPI).
435// According to http://blogs.msdn.com/oldnewthing/archive/2005/09/02/459952.aspx
436// in the worst case, it gets the counter from the rollover interrupt on the
437// programmable interrupt timer. In best cases, the HAL may conclude that the
438// RDTSC counter runs at a constant frequency, then it uses that instead. On
439// multiprocessor machines, it will try to verify the values returned from
440// RDTSC on each processor are consistent with each other, and apply a handful
441// of workarounds for known buggy hardware. In other words, QPC is supposed to
442// give consistent result on a multiprocessor computer, but it is unreliable in
443// reality due to bugs in BIOS or HAL on some, especially old computers.
444// With recent updates on HAL and newer BIOS, QPC is getting more reliable but
445// it should be used with caution.
446//
447// (3) System time. The system time provides a low-resolution (typically 10ms
448// to 55 milliseconds) time stamp but is comparatively less expensive to
449// retrieve and more reliable.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000450class HighResolutionTickClock final : public TickClock {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000451 public:
452 explicit HighResolutionTickClock(int64_t ticks_per_second)
453 : ticks_per_second_(ticks_per_second) {
454 DCHECK_LT(0, ticks_per_second);
455 }
456 virtual ~HighResolutionTickClock() {}
457
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000458 int64_t Now() override {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000459 LARGE_INTEGER now;
460 BOOL result = QueryPerformanceCounter(&now);
461 DCHECK(result);
462 USE(result);
463
464 // Intentionally calculate microseconds in a round about manner to avoid
465 // overflow and precision issues. Think twice before simplifying!
466 int64_t whole_seconds = now.QuadPart / ticks_per_second_;
467 int64_t leftover_ticks = now.QuadPart % ticks_per_second_;
468 int64_t ticks = (whole_seconds * Time::kMicrosecondsPerSecond) +
469 ((leftover_ticks * Time::kMicrosecondsPerSecond) / ticks_per_second_);
470
471 // Make sure we never return 0 here, so that TimeTicks::HighResolutionNow()
472 // will never return 0.
473 return ticks + 1;
474 }
475
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000476 bool IsHighResolution() override { return true; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477
478 private:
479 int64_t ticks_per_second_;
480};
481
482
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000483class RolloverProtectedTickClock final : public TickClock {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000484 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000485 RolloverProtectedTickClock() : rollover_(0) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 virtual ~RolloverProtectedTickClock() {}
487
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000488 int64_t Now() override {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000489 // We use timeGetTime() to implement TimeTicks::Now(), which rolls over
490 // every ~49.7 days. We try to track rollover ourselves, which works if
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000491 // TimeTicks::Now() is called at least every 24 days.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000492 // Note that we do not use GetTickCount() here, since timeGetTime() gives
493 // more predictable delta values, as described here:
494 // http://blogs.msdn.com/b/larryosterman/archive/2009/09/02/what-s-the-difference-between-gettickcount-and-timegettime.aspx
495 // timeGetTime() provides 1ms granularity when combined with
496 // timeBeginPeriod(). If the host application for V8 wants fast timers, it
497 // can use timeBeginPeriod() to increase the resolution.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000498 // We use a lock-free version because the sampler thread calls it
499 // while having the rest of the world stopped, that could cause a deadlock.
500 base::Atomic32 rollover = base::Acquire_Load(&rollover_);
501 uint32_t now = static_cast<uint32_t>(timeGetTime());
502 if ((now >> 31) != static_cast<uint32_t>(rollover & 1)) {
503 base::Release_CompareAndSwap(&rollover_, rollover, rollover + 1);
504 ++rollover;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000505 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000506 uint64_t ms = (static_cast<uint64_t>(rollover) << 31) | now;
507 return static_cast<int64_t>(ms * Time::kMicrosecondsPerMillisecond);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 }
509
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000510 bool IsHighResolution() override { return false; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000511
512 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000513 base::Atomic32 rollover_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000514};
515
516
517static LazyStaticInstance<RolloverProtectedTickClock,
518 DefaultConstructTrait<RolloverProtectedTickClock>,
519 ThreadSafeInitOnceTrait>::type tick_clock =
520 LAZY_STATIC_INSTANCE_INITIALIZER;
521
522
523struct CreateHighResTickClockTrait {
524 static TickClock* Create() {
525 // Check if the installed hardware supports a high-resolution performance
526 // counter, and if not fallback to the low-resolution tick clock.
527 LARGE_INTEGER ticks_per_second;
528 if (!QueryPerformanceFrequency(&ticks_per_second)) {
529 return tick_clock.Pointer();
530 }
531
532 // On Athlon X2 CPUs (e.g. model 15) the QueryPerformanceCounter
533 // is unreliable, fallback to the low-resolution tick clock.
534 CPU cpu;
535 if (strcmp(cpu.vendor(), "AuthenticAMD") == 0 && cpu.family() == 15) {
536 return tick_clock.Pointer();
537 }
538
539 return new HighResolutionTickClock(ticks_per_second.QuadPart);
540 }
541};
542
543
544static LazyDynamicInstance<TickClock, CreateHighResTickClockTrait,
545 ThreadSafeInitOnceTrait>::type high_res_tick_clock =
546 LAZY_DYNAMIC_INSTANCE_INITIALIZER;
547
548
549TimeTicks TimeTicks::Now() {
550 // Make sure we never return 0 here.
551 TimeTicks ticks(tick_clock.Pointer()->Now());
552 DCHECK(!ticks.IsNull());
553 return ticks;
554}
555
556
557TimeTicks TimeTicks::HighResolutionNow() {
558 // Make sure we never return 0 here.
559 TimeTicks ticks(high_res_tick_clock.Pointer()->Now());
560 DCHECK(!ticks.IsNull());
561 return ticks;
562}
563
564
565// static
566bool TimeTicks::IsHighResolutionClockWorking() {
567 return high_res_tick_clock.Pointer()->IsHighResolution();
568}
569
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000570#else // V8_OS_WIN
571
572TimeTicks TimeTicks::Now() {
573 return HighResolutionNow();
574}
575
576
577TimeTicks TimeTicks::HighResolutionNow() {
578 int64_t ticks;
579#if V8_OS_MACOSX
580 static struct mach_timebase_info info;
581 if (info.denom == 0) {
582 kern_return_t result = mach_timebase_info(&info);
583 DCHECK_EQ(KERN_SUCCESS, result);
584 USE(result);
585 }
586 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond *
587 info.numer / info.denom);
588#elif V8_OS_SOLARIS
589 ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590#elif V8_OS_POSIX
Ben Murdochc5610432016-08-08 18:44:38 +0100591 ticks = ClockNow(CLOCK_MONOTONIC);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000592#endif // V8_OS_MACOSX
593 // Make sure we never return 0 here.
594 return TimeTicks(ticks + 1);
595}
596
597
598// static
599bool TimeTicks::IsHighResolutionClockWorking() {
600 return true;
601}
602
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000603#endif // V8_OS_WIN
604
Ben Murdochc5610432016-08-08 18:44:38 +0100605
606// TODO(lpy): For windows ThreadTicks implementation,
607// see http://crbug.com/v8/5000
608bool 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
618ThreadTicks 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000630} // namespace base
631} // namespace v8