| 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 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 DCHECK(!ticks.IsNull()); | 513 DCHECK(!ticks.IsNull()); |
| 514 return ticks; | 514 return ticks; |
| 515 } | 515 } |
| 516 | 516 |
| 517 | 517 |
| 518 // static | 518 // static |
| 519 bool TimeTicks::IsHighResolutionClockWorking() { | 519 bool TimeTicks::IsHighResolutionClockWorking() { |
| 520 return high_res_tick_clock.Pointer()->IsHighResolution(); | 520 return high_res_tick_clock.Pointer()->IsHighResolution(); |
| 521 } | 521 } |
| 522 | 522 |
| 523 | |
| 524 // static | |
| 525 TimeTicks TimeTicks::KernelTimestampNow() { return TimeTicks(0); } | |
| 526 | |
| 527 | |
| 528 // static | |
| 529 bool TimeTicks::KernelTimestampAvailable() { return false; } | |
| 530 | |
| 531 #else // V8_OS_WIN | 523 #else // V8_OS_WIN |
| 532 | 524 |
| 533 TimeTicks TimeTicks::Now() { | 525 TimeTicks TimeTicks::Now() { |
| 534 return HighResolutionNow(); | 526 return HighResolutionNow(); |
| 535 } | 527 } |
| 536 | 528 |
| 537 | 529 |
| 538 TimeTicks TimeTicks::HighResolutionNow() { | 530 TimeTicks TimeTicks::HighResolutionNow() { |
| 539 int64_t ticks; | 531 int64_t ticks; |
| 540 #if V8_OS_MACOSX | 532 #if V8_OS_MACOSX |
| (...skipping 18 matching lines...) Expand all Loading... |
| 559 // Make sure we never return 0 here. | 551 // Make sure we never return 0 here. |
| 560 return TimeTicks(ticks + 1); | 552 return TimeTicks(ticks + 1); |
| 561 } | 553 } |
| 562 | 554 |
| 563 | 555 |
| 564 // static | 556 // static |
| 565 bool TimeTicks::IsHighResolutionClockWorking() { | 557 bool TimeTicks::IsHighResolutionClockWorking() { |
| 566 return true; | 558 return true; |
| 567 } | 559 } |
| 568 | 560 |
| 569 | |
| 570 #if V8_OS_LINUX | |
| 571 | |
| 572 class KernelTimestampClock { | |
| 573 public: | |
| 574 KernelTimestampClock() : clock_fd_(-1), clock_id_(kClockInvalid) { | |
| 575 clock_fd_ = open(kTraceClockDevice, O_RDONLY); | |
| 576 if (clock_fd_ == -1) { | |
| 577 return; | |
| 578 } | |
| 579 clock_id_ = get_clockid(clock_fd_); | |
| 580 } | |
| 581 | |
| 582 virtual ~KernelTimestampClock() { | |
| 583 if (clock_fd_ != -1) { | |
| 584 close(clock_fd_); | |
| 585 } | |
| 586 } | |
| 587 | |
| 588 int64_t Now() { | |
| 589 if (clock_id_ == kClockInvalid) { | |
| 590 return 0; | |
| 591 } | |
| 592 | |
| 593 struct timespec ts; | |
| 594 | |
| 595 clock_gettime(clock_id_, &ts); | |
| 596 return ((int64_t)ts.tv_sec * kNsecPerSec) + ts.tv_nsec; | |
| 597 } | |
| 598 | |
| 599 bool Available() { return clock_id_ != kClockInvalid; } | |
| 600 | |
| 601 private: | |
| 602 static const clockid_t kClockInvalid = -1; | |
| 603 static const char kTraceClockDevice[]; | |
| 604 static const uint64_t kNsecPerSec = 1000000000; | |
| 605 | |
| 606 int clock_fd_; | |
| 607 clockid_t clock_id_; | |
| 608 | |
| 609 static int get_clockid(int fd) { return ((~(clockid_t)(fd) << 3) | 3); } | |
| 610 }; | |
| 611 | |
| 612 | |
| 613 // Timestamp module name | |
| 614 const char KernelTimestampClock::kTraceClockDevice[] = "/dev/trace_clock"; | |
| 615 | |
| 616 #else | |
| 617 | |
| 618 class KernelTimestampClock { | |
| 619 public: | |
| 620 KernelTimestampClock() {} | |
| 621 | |
| 622 int64_t Now() { return 0; } | |
| 623 bool Available() { return false; } | |
| 624 }; | |
| 625 | |
| 626 #endif // V8_OS_LINUX | |
| 627 | |
| 628 static LazyStaticInstance<KernelTimestampClock, | |
| 629 DefaultConstructTrait<KernelTimestampClock>, | |
| 630 ThreadSafeInitOnceTrait>::type kernel_tick_clock = | |
| 631 LAZY_STATIC_INSTANCE_INITIALIZER; | |
| 632 | |
| 633 | |
| 634 // static | |
| 635 TimeTicks TimeTicks::KernelTimestampNow() { | |
| 636 return TimeTicks(kernel_tick_clock.Pointer()->Now()); | |
| 637 } | |
| 638 | |
| 639 | |
| 640 // static | |
| 641 bool TimeTicks::KernelTimestampAvailable() { | |
| 642 return kernel_tick_clock.Pointer()->Available(); | |
| 643 } | |
| 644 | |
| 645 #endif // V8_OS_WIN | 561 #endif // V8_OS_WIN |
| 646 | 562 |
| 647 } // namespace base | 563 } // namespace base |
| 648 } // namespace v8 | 564 } // namespace v8 |
| OLD | NEW |