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_time.h> | 13 #include <mach/mach_time.h> |
14 #endif | 14 #endif |
15 | 15 |
16 #include <cstring> | 16 #include <cstring> |
17 #include <ostream> | 17 #include <ostream> |
18 | 18 |
19 #if V8_OS_WIN | 19 #if V8_OS_WIN |
20 #include "src/base/lazy-instance.h" | 20 #include "src/base/lazy-instance.h" |
21 #include "src/base/win32-headers.h" | 21 #include "src/base/win32-headers.h" |
22 #endif | 22 #endif |
23 #include "src/base/atomicops.h" | |
24 #include "src/base/cpu.h" | 23 #include "src/base/cpu.h" |
25 #include "src/base/logging.h" | 24 #include "src/base/logging.h" |
26 #include "src/base/platform/platform.h" | 25 #include "src/base/platform/platform.h" |
27 | 26 |
28 // CLOCK_REALTIME_COARSE was added in Linux 2.6.32. | |
29 #if V8_OS_LINUX && !defined(CLOCK_REALTIME_COARSE) | |
30 #define CLOCK_REALTIME_COARSE 5 | |
31 #endif | |
32 | |
33 namespace v8 { | 27 namespace v8 { |
34 namespace base { | 28 namespace base { |
35 | 29 |
36 TimeDelta TimeDelta::FromDays(int days) { | 30 TimeDelta TimeDelta::FromDays(int days) { |
37 return TimeDelta(days * Time::kMicrosecondsPerDay); | 31 return TimeDelta(days * Time::kMicrosecondsPerDay); |
38 } | 32 } |
39 | 33 |
40 | 34 |
41 TimeDelta TimeDelta::FromHours(int hours) { | 35 TimeDelta TimeDelta::FromHours(int hours) { |
42 return TimeDelta(hours * Time::kMicrosecondsPerHour); | 36 return TimeDelta(hours * Time::kMicrosecondsPerHour); |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 } | 245 } |
252 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10; | 246 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10; |
253 ft.dwLowDateTime = static_cast<DWORD>(us); | 247 ft.dwLowDateTime = static_cast<DWORD>(us); |
254 ft.dwHighDateTime = static_cast<DWORD>(us >> 32); | 248 ft.dwHighDateTime = static_cast<DWORD>(us >> 32); |
255 return ft; | 249 return ft; |
256 } | 250 } |
257 | 251 |
258 #elif V8_OS_POSIX | 252 #elif V8_OS_POSIX |
259 | 253 |
260 Time Time::Now() { | 254 Time Time::Now() { |
261 #ifdef CLOCK_REALTIME_COARSE | |
262 struct timespec ts; | |
263 static const clockid_t kInvalidClockId = static_cast<clockid_t>(-1); | |
264 static clockid_t cached_clock_id = kInvalidClockId; | |
265 clockid_t clock_id = NoBarrier_Load(&cached_clock_id); | |
266 if (V8_UNLIKELY(clock_id == kInvalidClockId)) { | |
267 // Use CLOCK_REALTIME_COARSE when available (linux >= 2.6.32) and precise | |
268 // enough. Unlike CLOCK_REALTIME, its coarse cousin is normally serviced | |
269 // from the vDSO, without making an actual system call. | |
270 // | |
271 // CLOCK_REALTIME_COARSE precision is determined by CONFIG_HZ, the number | |
272 // of ticks per second that the kernel runs at. Granularity can be as low | |
273 // as one update every few hundred milliseconds so we need to ensure it is | |
274 // not _too_ coarse. Most kernels are built with CONFIG_HZ=1000, providing | |
275 // a one millisecond precision that is good enough for our purposes. | |
276 if (clock_getres(CLOCK_REALTIME_COARSE, &ts) < 0 || ts.tv_sec > 0 || | |
277 ts.tv_nsec > 1000 * 1000) { | |
278 clock_id = CLOCK_REALTIME; // Not available or not suitable. | |
279 } else { | |
280 clock_id = CLOCK_REALTIME_COARSE; | |
281 } | |
282 NoBarrier_Store(&cached_clock_id, clock_id); | |
283 } | |
284 int result = clock_gettime(clock_id, &ts); | |
285 DCHECK_EQ(0, result); | |
286 USE(result); | |
287 return FromTimespec(ts); | |
288 #else | |
289 struct timeval tv; | 255 struct timeval tv; |
290 int result = gettimeofday(&tv, NULL); | 256 int result = gettimeofday(&tv, NULL); |
291 DCHECK_EQ(0, result); | 257 DCHECK_EQ(0, result); |
292 USE(result); | 258 USE(result); |
293 return FromTimeval(tv); | 259 return FromTimeval(tv); |
294 #endif | |
295 } | 260 } |
296 | 261 |
297 | 262 |
298 Time Time::NowFromSystemTime() { | 263 Time Time::NowFromSystemTime() { |
299 return Now(); | 264 return Now(); |
300 } | 265 } |
301 | 266 |
302 | 267 |
303 Time Time::FromTimespec(struct timespec ts) { | 268 Time Time::FromTimespec(struct timespec ts) { |
304 DCHECK(ts.tv_nsec >= 0); | 269 DCHECK(ts.tv_nsec >= 0); |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 | 638 |
674 | 639 |
675 // static | 640 // static |
676 bool TimeTicks::KernelTimestampAvailable() { | 641 bool TimeTicks::KernelTimestampAvailable() { |
677 return kernel_tick_clock.Pointer()->Available(); | 642 return kernel_tick_clock.Pointer()->Available(); |
678 } | 643 } |
679 | 644 |
680 #endif // V8_OS_WIN | 645 #endif // V8_OS_WIN |
681 | 646 |
682 } } // namespace v8::base | 647 } } // namespace v8::base |
OLD | NEW |