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 3 // found in the LICENSE file. 4 5 #include "src/platform/time.h" 6 7 #if V8_OS_POSIX 8 #include <sys/time.h> 9 #endif 10 #if V8_OS_MACOSX 11 #include <mach/mach_time.h> 12 #endif 13 14 #include <string.h> 15 16 #if V8_OS_WIN 17 #include "src/base/lazy-instance.h" 18 #include "src/base/win32-headers.h" 19 #endif 20 #include "src/checks.h" 21 #include "src/cpu.h" 22 #include "src/platform.h" 23 24 namespace v8 { 25 namespace internal { 26 27 TimeDelta TimeDelta::FromDays(int days) { 28 return TimeDelta(days * Time::kMicrosecondsPerDay); 29 } 30 31 32 TimeDelta TimeDelta::FromHours(int hours) { 33 return TimeDelta(hours * Time::kMicrosecondsPerHour); 34 } 35 36 37 TimeDelta TimeDelta::FromMinutes(int minutes) { 38 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); 39 } 40 41 42 TimeDelta TimeDelta::FromSeconds(int64_t seconds) { 43 return TimeDelta(seconds * Time::kMicrosecondsPerSecond); 44 } 45 46 47 TimeDelta TimeDelta::FromMilliseconds(int64_t milliseconds) { 48 return TimeDelta(milliseconds * Time::kMicrosecondsPerMillisecond); 49 } 50 51 52 TimeDelta TimeDelta::FromNanoseconds(int64_t nanoseconds) { 53 return TimeDelta(nanoseconds / Time::kNanosecondsPerMicrosecond); 54 } 55 56 57 int TimeDelta::InDays() const { 58 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); 59 } 60 61 62 int TimeDelta::InHours() const { 63 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour); 64 } 65 66 67 int TimeDelta::InMinutes() const { 68 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute); 69 } 70 71 72 double TimeDelta::InSecondsF() const { 73 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond; 74 } 75 76 77 int64_t TimeDelta::InSeconds() const { 78 return delta_ / Time::kMicrosecondsPerSecond; 79 } 80 81 82 double TimeDelta::InMillisecondsF() const { 83 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond; 84 } 85 86 87 int64_t TimeDelta::InMilliseconds() const { 88 return delta_ / Time::kMicrosecondsPerMillisecond; 89 } 90 91 92 int64_t TimeDelta::InNanoseconds() const { 93 return delta_ * Time::kNanosecondsPerMicrosecond; 94 } 95 96 97 #if V8_OS_MACOSX 98 99 TimeDelta TimeDelta::FromMachTimespec(struct mach_timespec ts) { 100 ASSERT_GE(ts.tv_nsec, 0); 101 ASSERT_LT(ts.tv_nsec, 102 static_cast<long>(Time::kNanosecondsPerSecond)); // NOLINT 103 return TimeDelta(ts.tv_sec * Time::kMicrosecondsPerSecond + 104 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); 105 } 106 107 108 struct mach_timespec TimeDelta::ToMachTimespec() const { 109 struct mach_timespec ts; 110 ASSERT(delta_ >= 0); 111 ts.tv_sec = delta_ / Time::kMicrosecondsPerSecond; 112 ts.tv_nsec = (delta_ % Time::kMicrosecondsPerSecond) * 113 Time::kNanosecondsPerMicrosecond; 114 return ts; 115 } 116 117 #endif // V8_OS_MACOSX 118 119 120 #if V8_OS_POSIX 121 122 TimeDelta TimeDelta::FromTimespec(struct timespec ts) { 123 ASSERT_GE(ts.tv_nsec, 0); 124 ASSERT_LT(ts.tv_nsec, 125 static_cast<long>(Time::kNanosecondsPerSecond)); // NOLINT 126 return TimeDelta(ts.tv_sec * Time::kMicrosecondsPerSecond + 127 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); 128 } 129 130 131 struct timespec TimeDelta::ToTimespec() const { 132 struct timespec ts; 133 ts.tv_sec = delta_ / Time::kMicrosecondsPerSecond; 134 ts.tv_nsec = (delta_ % Time::kMicrosecondsPerSecond) * 135 Time::kNanosecondsPerMicrosecond; 136 return ts; 137 } 138 139 #endif // V8_OS_POSIX 140 141 142 #if V8_OS_WIN 143 144 // We implement time using the high-resolution timers so that we can get 145 // timeouts which are smaller than 10-15ms. To avoid any drift, we 146 // periodically resync the internal clock to the system clock. 147 class Clock V8_FINAL { 148 public: 149 Clock() : initial_ticks_(GetSystemTicks()), initial_time_(GetSystemTime()) {} 150 151 Time Now() { 152 // Time between resampling the un-granular clock for this API (1 minute). 153 const TimeDelta kMaxElapsedTime = TimeDelta::FromMinutes(1); 154 155 LockGuard<Mutex> lock_guard(&mutex_); 156 157 // Determine current time and ticks. 158 TimeTicks ticks = GetSystemTicks(); 159 Time time = GetSystemTime(); 160 161 // Check if we need to synchronize with the system clock due to a backwards 162 // time change or the amount of time elapsed. 163 TimeDelta elapsed = ticks - initial_ticks_; 164 if (time < initial_time_ || elapsed > kMaxElapsedTime) { 165 initial_ticks_ = ticks; 166 initial_time_ = time; 167 return time; 168 } 169 170 return initial_time_ + elapsed; 171 } 172 173 Time NowFromSystemTime() { 174 LockGuard<Mutex> lock_guard(&mutex_); 175 initial_ticks_ = GetSystemTicks(); 176 initial_time_ = GetSystemTime(); 177 return initial_time_; 178 } 179 180 private: 181 static TimeTicks GetSystemTicks() { 182 return TimeTicks::Now(); 183 } 184 185 static Time GetSystemTime() { 186 FILETIME ft; 187 ::GetSystemTimeAsFileTime(&ft); 188 return Time::FromFiletime(ft); 189 } 190 191 TimeTicks initial_ticks_; 192 Time initial_time_; 193 Mutex mutex_; 194 }; 195 196 197 static base::LazyStaticInstance<Clock, base::DefaultConstructTrait<Clock>, 198 base::ThreadSafeInitOnceTrait>::type clock = 199 LAZY_STATIC_INSTANCE_INITIALIZER; 200 201 202 Time Time::Now() { 203 return clock.Pointer()->Now(); 204 } 205 206 207 Time Time::NowFromSystemTime() { 208 return clock.Pointer()->NowFromSystemTime(); 209 } 210 211 212 // Time between windows epoch and standard epoch. 213 static const int64_t kTimeToEpochInMicroseconds = V8_INT64_C(11644473600000000); 214 215 216 Time Time::FromFiletime(FILETIME ft) { 217 if (ft.dwLowDateTime == 0 && ft.dwHighDateTime == 0) { 218 return Time(); 219 } 220 if (ft.dwLowDateTime == std::numeric_limits<DWORD>::max() && 221 ft.dwHighDateTime == std::numeric_limits<DWORD>::max()) { 222 return Max(); 223 } 224 int64_t us = (static_cast<uint64_t>(ft.dwLowDateTime) + 225 (static_cast<uint64_t>(ft.dwHighDateTime) << 32)) / 10; 226 return Time(us - kTimeToEpochInMicroseconds); 227 } 228 229 230 FILETIME Time::ToFiletime() const { 231 ASSERT(us_ >= 0); 232 FILETIME ft; 233 if (IsNull()) { 234 ft.dwLowDateTime = 0; 235 ft.dwHighDateTime = 0; 236 return ft; 237 } 238 if (IsMax()) { 239 ft.dwLowDateTime = std::numeric_limits<DWORD>::max(); 240 ft.dwHighDateTime = std::numeric_limits<DWORD>::max(); 241 return ft; 242 } 243 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10; 244 ft.dwLowDateTime = static_cast<DWORD>(us); 245 ft.dwHighDateTime = static_cast<DWORD>(us >> 32); 246 return ft; 247 } 248 249 #elif V8_OS_POSIX 250 251 Time Time::Now() { 252 struct timeval tv; 253 int result = gettimeofday(&tv, NULL); 254 ASSERT_EQ(0, result); 255 USE(result); 256 return FromTimeval(tv); 257 } 258 259 260 Time Time::NowFromSystemTime() { 261 return Now(); 262 } 263 264 265 Time Time::FromTimespec(struct timespec ts) { 266 ASSERT(ts.tv_nsec >= 0); 267 ASSERT(ts.tv_nsec < static_cast<long>(kNanosecondsPerSecond)); // NOLINT 268 if (ts.tv_nsec == 0 && ts.tv_sec == 0) { 269 return Time(); 270 } 271 if (ts.tv_nsec == static_cast<long>(kNanosecondsPerSecond - 1) && // NOLINT 272 ts.tv_sec == std::numeric_limits<time_t>::max()) { 273 return Max(); 274 } 275 return Time(ts.tv_sec * kMicrosecondsPerSecond + 276 ts.tv_nsec / kNanosecondsPerMicrosecond); 277 } 278 279 280 struct timespec Time::ToTimespec() const { 281 struct timespec ts; 282 if (IsNull()) { 283 ts.tv_sec = 0; 284 ts.tv_nsec = 0; 285 return ts; 286 } 287 if (IsMax()) { 288 ts.tv_sec = std::numeric_limits<time_t>::max(); 289 ts.tv_nsec = static_cast<long>(kNanosecondsPerSecond - 1); // NOLINT 290 return ts; 291 } 292 ts.tv_sec = us_ / kMicrosecondsPerSecond; 293 ts.tv_nsec = (us_ % kMicrosecondsPerSecond) * kNanosecondsPerMicrosecond; 294 return ts; 295 } 296 297 298 Time Time::FromTimeval(struct timeval tv) { 299 ASSERT(tv.tv_usec >= 0); 300 ASSERT(tv.tv_usec < static_cast<suseconds_t>(kMicrosecondsPerSecond)); 301 if (tv.tv_usec == 0 && tv.tv_sec == 0) { 302 return Time(); 303 } 304 if (tv.tv_usec == static_cast<suseconds_t>(kMicrosecondsPerSecond - 1) && 305 tv.tv_sec == std::numeric_limits<time_t>::max()) { 306 return Max(); 307 } 308 return Time(tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec); 309 } 310 311 312 struct timeval Time::ToTimeval() const { 313 struct timeval tv; 314 if (IsNull()) { 315 tv.tv_sec = 0; 316 tv.tv_usec = 0; 317 return tv; 318 } 319 if (IsMax()) { 320 tv.tv_sec = std::numeric_limits<time_t>::max(); 321 tv.tv_usec = static_cast<suseconds_t>(kMicrosecondsPerSecond - 1); 322 return tv; 323 } 324 tv.tv_sec = us_ / kMicrosecondsPerSecond; 325 tv.tv_usec = us_ % kMicrosecondsPerSecond; 326 return tv; 327 } 328 329 #endif // V8_OS_WIN 330 331 332 Time Time::FromJsTime(double ms_since_epoch) { 333 // The epoch is a valid time, so this constructor doesn't interpret 334 // 0 as the null time. 335 if (ms_since_epoch == std::numeric_limits<double>::max()) { 336 return Max(); 337 } 338 return Time( 339 static_cast<int64_t>(ms_since_epoch * kMicrosecondsPerMillisecond)); 340 } 341 342 343 double Time::ToJsTime() const { 344 if (IsNull()) { 345 // Preserve 0 so the invalid result doesn't depend on the platform. 346 return 0; 347 } 348 if (IsMax()) { 349 // Preserve max without offset to prevent overflow. 350 return std::numeric_limits<double>::max(); 351 } 352 return static_cast<double>(us_) / kMicrosecondsPerMillisecond; 353 } 354 355 356 #if V8_OS_WIN 357 358 class TickClock { 359 public: 360 virtual ~TickClock() {} 361 virtual int64_t Now() = 0; 362 virtual bool IsHighResolution() = 0; 363 }; 364 365 366 // Overview of time counters: 367 // (1) CPU cycle counter. (Retrieved via RDTSC) 368 // The CPU counter provides the highest resolution time stamp and is the least 369 // expensive to retrieve. However, the CPU counter is unreliable and should not 370 // be used in production. Its biggest issue is that it is per processor and it 371 // is not synchronized between processors. Also, on some computers, the counters 372 // will change frequency due to thermal and power changes, and stop in some 373 // states. 374 // 375 // (2) QueryPerformanceCounter (QPC). The QPC counter provides a high- 376 // resolution (100 nanoseconds) time stamp but is comparatively more expensive 377 // to retrieve. What QueryPerformanceCounter actually does is up to the HAL. 378 // (with some help from ACPI). 379 // According to http://blogs.msdn.com/oldnewthing/archive/2005/09/02/459952.aspx 380 // in the worst case, it gets the counter from the rollover interrupt on the 381 // programmable interrupt timer. In best cases, the HAL may conclude that the 382 // RDTSC counter runs at a constant frequency, then it uses that instead. On 383 // multiprocessor machines, it will try to verify the values returned from 384 // RDTSC on each processor are consistent with each other, and apply a handful 385 // of workarounds for known buggy hardware. In other words, QPC is supposed to 386 // give consistent result on a multiprocessor computer, but it is unreliable in 387 // reality due to bugs in BIOS or HAL on some, especially old computers. 388 // With recent updates on HAL and newer BIOS, QPC is getting more reliable but 389 // it should be used with caution. 390 // 391 // (3) System time. The system time provides a low-resolution (typically 10ms 392 // to 55 milliseconds) time stamp but is comparatively less expensive to 393 // retrieve and more reliable. 394 class HighResolutionTickClock V8_FINAL : public TickClock { 395 public: 396 explicit HighResolutionTickClock(int64_t ticks_per_second) 397 : ticks_per_second_(ticks_per_second) { 398 ASSERT_LT(0, ticks_per_second); 399 } 400 virtual ~HighResolutionTickClock() {} 401 402 virtual int64_t Now() V8_OVERRIDE { 403 LARGE_INTEGER now; 404 BOOL result = QueryPerformanceCounter(&now); 405 ASSERT(result); 406 USE(result); 407 408 // Intentionally calculate microseconds in a round about manner to avoid 409 // overflow and precision issues. Think twice before simplifying! 410 int64_t whole_seconds = now.QuadPart / ticks_per_second_; 411 int64_t leftover_ticks = now.QuadPart % ticks_per_second_; 412 int64_t ticks = (whole_seconds * Time::kMicrosecondsPerSecond) + 413 ((leftover_ticks * Time::kMicrosecondsPerSecond) / ticks_per_second_); 414 415 // Make sure we never return 0 here, so that TimeTicks::HighResolutionNow() 416 // will never return 0. 417 return ticks + 1; 418 } 419 420 virtual bool IsHighResolution() V8_OVERRIDE { 421 return true; 422 } 423 424 private: 425 int64_t ticks_per_second_; 426 }; 427 428 429 class RolloverProtectedTickClock V8_FINAL : public TickClock { 430 public: 431 // We initialize rollover_ms_ to 1 to ensure that we will never 432 // return 0 from TimeTicks::HighResolutionNow() and TimeTicks::Now() below. 433 RolloverProtectedTickClock() : last_seen_now_(0), rollover_ms_(1) {} 434 virtual ~RolloverProtectedTickClock() {} 435 436 virtual int64_t Now() V8_OVERRIDE { 437 LockGuard<Mutex> lock_guard(&mutex_); 438 // We use timeGetTime() to implement TimeTicks::Now(), which rolls over 439 // every ~49.7 days. We try to track rollover ourselves, which works if 440 // TimeTicks::Now() is called at least every 49 days. 441 // Note that we do not use GetTickCount() here, since timeGetTime() gives 442 // more predictable delta values, as described here: 443 // http://blogs.msdn.com/b/larryosterman/archive/2009/09/02/what-s-the-difference-between-gettickcount-and-timegettime.aspx 444 // timeGetTime() provides 1ms granularity when combined with 445 // timeBeginPeriod(). If the host application for V8 wants fast timers, it 446 // can use timeBeginPeriod() to increase the resolution. 447 DWORD now = timeGetTime(); 448 if (now < last_seen_now_) { 449 rollover_ms_ += V8_INT64_C(0x100000000); // ~49.7 days. 450 } 451 last_seen_now_ = now; 452 return (now + rollover_ms_) * Time::kMicrosecondsPerMillisecond; 453 } 454 455 virtual bool IsHighResolution() V8_OVERRIDE { 456 return false; 457 } 458 459 private: 460 Mutex mutex_; 461 DWORD last_seen_now_; 462 int64_t rollover_ms_; 463 }; 464 465 466 static base::LazyStaticInstance< 467 RolloverProtectedTickClock, 468 base::DefaultConstructTrait<RolloverProtectedTickClock>, 469 base::ThreadSafeInitOnceTrait>::type tick_clock = 470 LAZY_STATIC_INSTANCE_INITIALIZER; 471 472 473 struct CreateHighResTickClockTrait { 474 static TickClock* Create() { 475 // Check if the installed hardware supports a high-resolution performance 476 // counter, and if not fallback to the low-resolution tick clock. 477 LARGE_INTEGER ticks_per_second; 478 if (!QueryPerformanceFrequency(&ticks_per_second)) { 479 return tick_clock.Pointer(); 480 } 481 482 // On Athlon X2 CPUs (e.g. model 15) the QueryPerformanceCounter 483 // is unreliable, fallback to the low-resolution tick clock. 484 CPU cpu; 485 if (strcmp(cpu.vendor(), "AuthenticAMD") == 0 && cpu.family() == 15) { 486 return tick_clock.Pointer(); 487 } 488 489 return new HighResolutionTickClock(ticks_per_second.QuadPart); 490 } 491 }; 492 493 494 static base::LazyDynamicInstance<TickClock, 495 CreateHighResTickClockTrait, 496 base::ThreadSafeInitOnceTrait>::type high_res_tick_clock = 497 LAZY_DYNAMIC_INSTANCE_INITIALIZER; 498 499 500 TimeTicks TimeTicks::Now() { 501 // Make sure we never return 0 here. 502 TimeTicks ticks(tick_clock.Pointer()->Now()); 503 ASSERT(!ticks.IsNull()); 504 return ticks; 505 } 506 507 508 TimeTicks TimeTicks::HighResolutionNow() { 509 // Make sure we never return 0 here. 510 TimeTicks ticks(high_res_tick_clock.Pointer()->Now()); 511 ASSERT(!ticks.IsNull()); 512 return ticks; 513 } 514 515 516 // static 517 bool TimeTicks::IsHighResolutionClockWorking() { 518 return high_res_tick_clock.Pointer()->IsHighResolution(); 519 } 520 521 #else // V8_OS_WIN 522 523 TimeTicks TimeTicks::Now() { 524 return HighResolutionNow(); 525 } 526 527 528 TimeTicks TimeTicks::HighResolutionNow() { 529 int64_t ticks; 530 #if V8_OS_MACOSX 531 static struct mach_timebase_info info; 532 if (info.denom == 0) { 533 kern_return_t result = mach_timebase_info(&info); 534 ASSERT_EQ(KERN_SUCCESS, result); 535 USE(result); 536 } 537 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond * 538 info.numer / info.denom); 539 #elif V8_OS_SOLARIS 540 ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond); 541 #elif V8_LIBRT_NOT_AVAILABLE 542 // TODO(bmeurer): This is a temporary hack to support cross-compiling 543 // Chrome for Android in AOSP. Remove this once AOSP is fixed, also 544 // cleanup the tools/gyp/v8.gyp file. 545 struct timeval tv; 546 int result = gettimeofday(&tv, NULL); 547 ASSERT_EQ(0, result); 548 USE(result); 549 ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec); 550 #elif V8_OS_POSIX 551 struct timespec ts; 552 int result = clock_gettime(CLOCK_MONOTONIC, &ts); 553 ASSERT_EQ(0, result); 554 USE(result); 555 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + 556 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); 557 #endif // V8_OS_MACOSX 558 // Make sure we never return 0 here. 559 return TimeTicks(ticks + 1); 560 } 561 562 563 // static 564 bool TimeTicks::IsHighResolutionClockWorking() { 565 return true; 566 } 567 568 #endif // V8_OS_WIN 569 570 } } // namespace v8::internal 571