Chromium Code Reviews| 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> |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 } | 245 } |
| 246 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10; | 246 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10; |
| 247 ft.dwLowDateTime = static_cast<DWORD>(us); | 247 ft.dwLowDateTime = static_cast<DWORD>(us); |
| 248 ft.dwHighDateTime = static_cast<DWORD>(us >> 32); | 248 ft.dwHighDateTime = static_cast<DWORD>(us >> 32); |
| 249 return ft; | 249 return ft; |
| 250 } | 250 } |
| 251 | 251 |
| 252 #elif V8_OS_POSIX | 252 #elif V8_OS_POSIX |
| 253 | 253 |
| 254 Time Time::Now() { | 254 Time Time::Now() { |
| 255 #if V8_OS_LINUX | |
|
Benedikt Meurer
2015/05/06 11:25:29
How about something like this instead:
#if V8_OS_
| |
| 256 struct timespec ts; | |
| 257 // clockid_t is an int32_t; loads and stores are atomic. | |
| 258 static clockid_t clock_id = -1; | |
| 259 if (clock_id == -1) { | |
| 260 // CLOCK_REALTIME_COARSE is not supported on kernels <= 2.6.31. | |
| 261 // Probe the kernel to see if it's available and has <= 1 ms resolution. | |
| 262 // | |
| 263 // CLOCK_REALTIME_COARSE, unlike CLOCK_REALTIME, can often be serviced | |
| 264 // entirely from the vDSO without the need to make a system call. | |
| 265 // It can have a dramatic impact on applications that frequently | |
| 266 // query the current time. | |
| 267 // | |
| 268 // One caveat is that CLOCK_REALTIME_COARSE is tied to CONFIG_HZ, the | |
| 269 // number of ticks per second that the kernel runs at. Its granularity | |
| 270 // can be as low as one update every 300 ms so we need to make sure that | |
| 271 // it is accurate enough. Fortunately, many if not most kernels are built | |
| 272 // with CONFIG_HZ=1000, giving it a one millisecond precision and that is | |
| 273 // good enough for our purposes. | |
| 274 static const clockid_t kClockRealTimeCoarse = 5; | |
| 275 if (-1 == clock_getres(kClockRealTimeCoarse, &ts) || | |
| 276 ts.tv_nsec > 1000 * 1000 || ts.tv_sec > 0) { | |
| 277 clock_id = CLOCK_REALTIME; // Not available or not suitable. | |
| 278 } else { | |
| 279 clock_id = kClockRealTimeCoarse; | |
| 280 } | |
| 281 } | |
| 282 int result = clock_gettime(clock_id, &ts); | |
| 283 DCHECK_EQ(0, result); | |
| 284 USE(result); | |
| 285 return FromTimespec(ts); | |
| 286 #else | |
| 255 struct timeval tv; | 287 struct timeval tv; |
| 256 int result = gettimeofday(&tv, NULL); | 288 int result = gettimeofday(&tv, NULL); |
| 257 DCHECK_EQ(0, result); | 289 DCHECK_EQ(0, result); |
| 258 USE(result); | 290 USE(result); |
| 259 return FromTimeval(tv); | 291 return FromTimeval(tv); |
| 292 #endif | |
| 260 } | 293 } |
| 261 | 294 |
| 262 | 295 |
| 263 Time Time::NowFromSystemTime() { | 296 Time Time::NowFromSystemTime() { |
| 264 return Now(); | 297 return Now(); |
| 265 } | 298 } |
| 266 | 299 |
| 267 | 300 |
| 268 Time Time::FromTimespec(struct timespec ts) { | 301 Time Time::FromTimespec(struct timespec ts) { |
| 269 DCHECK(ts.tv_nsec >= 0); | 302 DCHECK(ts.tv_nsec >= 0); |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 638 | 671 |
| 639 | 672 |
| 640 // static | 673 // static |
| 641 bool TimeTicks::KernelTimestampAvailable() { | 674 bool TimeTicks::KernelTimestampAvailable() { |
| 642 return kernel_tick_clock.Pointer()->Available(); | 675 return kernel_tick_clock.Pointer()->Available(); |
| 643 } | 676 } |
| 644 | 677 |
| 645 #endif // V8_OS_WIN | 678 #endif // V8_OS_WIN |
| 646 | 679 |
| 647 } } // namespace v8::base | 680 } } // namespace v8::base |
| OLD | NEW |