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