34 #include <mach/mach_time.h>
49 TimeDelta TimeDelta::FromDays(
int days) {
50 return TimeDelta(days * Time::kMicrosecondsPerDay);
54 TimeDelta TimeDelta::FromHours(
int hours) {
55 return TimeDelta(hours * Time::kMicrosecondsPerHour);
59 TimeDelta TimeDelta::FromMinutes(
int minutes) {
60 return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
64 TimeDelta TimeDelta::FromSeconds(int64_t seconds) {
65 return TimeDelta(seconds * Time::kMicrosecondsPerSecond);
69 TimeDelta TimeDelta::FromMilliseconds(int64_t milliseconds) {
70 return TimeDelta(milliseconds * Time::kMicrosecondsPerMillisecond);
74 TimeDelta TimeDelta::FromNanoseconds(int64_t nanoseconds) {
75 return TimeDelta(nanoseconds / Time::kNanosecondsPerMicrosecond);
79 int TimeDelta::InDays()
const {
80 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
84 int TimeDelta::InHours()
const {
85 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
89 int TimeDelta::InMinutes()
const {
90 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
94 double TimeDelta::InSecondsF()
const {
95 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
99 int64_t TimeDelta::InSeconds()
const {
100 return delta_ / Time::kMicrosecondsPerSecond;
104 double TimeDelta::InMillisecondsF()
const {
105 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
109 int64_t TimeDelta::InMilliseconds()
const {
110 return delta_ / Time::kMicrosecondsPerMillisecond;
114 int64_t TimeDelta::InNanoseconds()
const {
115 return delta_ * Time::kNanosecondsPerMicrosecond;
121 TimeDelta TimeDelta::FromMachTimespec(
struct mach_timespec ts) {
124 static_cast<long>(Time::kNanosecondsPerSecond));
125 return TimeDelta(ts.tv_sec * Time::kMicrosecondsPerSecond +
126 ts.tv_nsec / Time::kNanosecondsPerMicrosecond);
130 struct mach_timespec TimeDelta::ToMachTimespec() const {
131 struct mach_timespec ts;
133 ts.tv_sec = delta_ / Time::kMicrosecondsPerSecond;
134 ts.tv_nsec = (delta_ % Time::kMicrosecondsPerSecond) *
135 Time::kNanosecondsPerMicrosecond;
139 #endif // V8_OS_MACOSX
144 TimeDelta TimeDelta::FromTimespec(
struct timespec ts) {
147 static_cast<long>(Time::kNanosecondsPerSecond));
148 return TimeDelta(ts.tv_sec * Time::kMicrosecondsPerSecond +
149 ts.tv_nsec / Time::kNanosecondsPerMicrosecond);
153 struct timespec TimeDelta::ToTimespec() const {
155 ts.tv_sec = delta_ / Time::kMicrosecondsPerSecond;
156 ts.tv_nsec = (delta_ % Time::kMicrosecondsPerSecond) *
157 Time::kNanosecondsPerMicrosecond;
161 #endif // V8_OS_POSIX
171 Clock() : initial_ticks_(GetSystemTicks()), initial_time_(GetSystemTime()) {}
175 const TimeDelta kMaxElapsedTime = TimeDelta::FromMinutes(1);
177 LockGuard<Mutex> lock_guard(&mutex_);
180 TimeTicks ticks = GetSystemTicks();
181 Time time = GetSystemTime();
185 TimeDelta elapsed = ticks - initial_ticks_;
186 if (time < initial_time_ || elapsed > kMaxElapsedTime) {
187 initial_ticks_ = ticks;
188 initial_time_ = time;
192 return initial_time_ + elapsed;
195 Time NowFromSystemTime() {
196 LockGuard<Mutex> lock_guard(&mutex_);
197 initial_ticks_ = GetSystemTicks();
198 initial_time_ = GetSystemTime();
199 return initial_time_;
203 static TimeTicks GetSystemTicks() {
204 return TimeTicks::Now();
207 static Time GetSystemTime() {
209 ::GetSystemTimeAsFileTime(&ft);
210 return Time::FromFiletime(ft);
213 TimeTicks initial_ticks_;
219 static LazyStaticInstance<Clock,
220 DefaultConstructTrait<Clock>,
225 return clock.Pointer()->Now();
229 Time Time::NowFromSystemTime() {
230 return clock.Pointer()->NowFromSystemTime();
235 static const int64_t kTimeToEpochInMicroseconds =
V8_INT64_C(11644473600000000);
238 Time Time::FromFiletime(FILETIME ft) {
239 if (ft.dwLowDateTime == 0 && ft.dwHighDateTime == 0) {
242 if (ft.dwLowDateTime == std::numeric_limits<DWORD>::max() &&
243 ft.dwHighDateTime == std::numeric_limits<DWORD>::max()) {
246 int64_t us = (
static_cast<uint64_t
>(ft.dwLowDateTime) +
247 (static_cast<uint64_t>(ft.dwHighDateTime) << 32)) / 10;
248 return Time(us - kTimeToEpochInMicroseconds);
252 FILETIME Time::ToFiletime()
const {
256 ft.dwLowDateTime = 0;
257 ft.dwHighDateTime = 0;
261 ft.dwLowDateTime = std::numeric_limits<DWORD>::max();
262 ft.dwHighDateTime = std::numeric_limits<DWORD>::max();
265 uint64_t us =
static_cast<uint64_t
>(us_ + kTimeToEpochInMicroseconds) * 10;
266 ft.dwLowDateTime =
static_cast<DWORD>(us);
267 ft.dwHighDateTime =
static_cast<DWORD>(us >> 32);
275 int result = gettimeofday(&tv,
NULL);
278 return FromTimeval(tv);
282 Time Time::NowFromSystemTime() {
287 Time Time::FromTimespec(
struct timespec ts) {
289 ASSERT(ts.tv_nsec < static_cast<long>(kNanosecondsPerSecond));
290 if (ts.tv_nsec == 0 && ts.tv_sec == 0) {
293 if (ts.tv_nsec == static_cast<long>(kNanosecondsPerSecond - 1) &&
294 ts.tv_sec == std::numeric_limits<time_t>::max()) {
297 return Time(ts.tv_sec * kMicrosecondsPerSecond +
298 ts.tv_nsec / kNanosecondsPerMicrosecond);
302 struct timespec Time::ToTimespec() const {
310 ts.tv_sec = std::numeric_limits<time_t>::max();
311 ts.tv_nsec =
static_cast<long>(kNanosecondsPerSecond - 1);
314 ts.tv_sec = us_ / kMicrosecondsPerSecond;
315 ts.tv_nsec = (us_ % kMicrosecondsPerSecond) * kNanosecondsPerMicrosecond;
320 Time Time::FromTimeval(
struct timeval tv) {
322 ASSERT(tv.tv_usec < static_cast<suseconds_t>(kMicrosecondsPerSecond));
323 if (tv.tv_usec == 0 && tv.tv_sec == 0) {
326 if (tv.tv_usec == static_cast<suseconds_t>(kMicrosecondsPerSecond - 1) &&
327 tv.tv_sec == std::numeric_limits<time_t>::max()) {
330 return Time(tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec);
334 struct timeval Time::ToTimeval() const {
342 tv.tv_sec = std::numeric_limits<time_t>::max();
343 tv.tv_usec =
static_cast<suseconds_t
>(kMicrosecondsPerSecond - 1);
346 tv.tv_sec = us_ / kMicrosecondsPerSecond;
347 tv.tv_usec = us_ % kMicrosecondsPerSecond;
354 Time Time::FromJsTime(
double ms_since_epoch) {
357 if (ms_since_epoch == std::numeric_limits<double>::max()) {
361 static_cast<int64_t>(ms_since_epoch * kMicrosecondsPerMillisecond));
365 double Time::ToJsTime()
const {
372 return std::numeric_limits<double>::max();
374 return static_cast<double>(us_) / kMicrosecondsPerMillisecond;
382 virtual ~TickClock() {}
383 virtual int64_t Now() = 0;
384 virtual bool IsHighResolution() = 0;
416 class HighResolutionTickClock
V8_FINAL :
public TickClock {
418 explicit HighResolutionTickClock(int64_t ticks_per_second)
419 : ticks_per_second_(ticks_per_second) {
422 virtual ~HighResolutionTickClock() {}
426 BOOL result = QueryPerformanceCounter(&now);
432 int64_t whole_seconds = now.QuadPart / ticks_per_second_;
433 int64_t leftover_ticks = now.QuadPart % ticks_per_second_;
434 int64_t ticks = (whole_seconds * Time::kMicrosecondsPerSecond) +
435 ((leftover_ticks * Time::kMicrosecondsPerSecond) / ticks_per_second_);
447 int64_t ticks_per_second_;
451 class RolloverProtectedTickClock
V8_FINAL :
public TickClock {
455 RolloverProtectedTickClock() : last_seen_now_(0), rollover_ms_(1) {}
456 virtual ~RolloverProtectedTickClock() {}
459 LockGuard<Mutex> lock_guard(&mutex_);
469 DWORD now = timeGetTime();
470 if (now < last_seen_now_) {
473 last_seen_now_ = now;
474 return (now + rollover_ms_) * Time::kMicrosecondsPerMillisecond;
483 DWORD last_seen_now_;
484 int64_t rollover_ms_;
488 static LazyStaticInstance<RolloverProtectedTickClock,
489 DefaultConstructTrait<RolloverProtectedTickClock>,
490 ThreadSafeInitOnceTrait>::type tick_clock =
494 struct CreateHighResTickClockTrait {
495 static TickClock* Create() {
498 LARGE_INTEGER ticks_per_second;
499 if (!QueryPerformanceFrequency(&ticks_per_second)) {
500 return tick_clock.Pointer();
506 if (strcmp(cpu.vendor(),
"AuthenticAMD") == 0 && cpu.family() == 15) {
507 return tick_clock.Pointer();
510 return new HighResolutionTickClock(ticks_per_second.QuadPart);
515 static LazyDynamicInstance<TickClock,
516 CreateHighResTickClockTrait,
517 ThreadSafeInitOnceTrait>::type high_res_tick_clock =
521 TimeTicks TimeTicks::Now() {
523 TimeTicks ticks(tick_clock.Pointer()->Now());
529 TimeTicks TimeTicks::HighResolutionNow() {
531 TimeTicks ticks(high_res_tick_clock.Pointer()->Now());
538 bool TimeTicks::IsHighResolutionClockWorking() {
539 return high_res_tick_clock.Pointer()->IsHighResolution();
544 TimeTicks TimeTicks::Now() {
545 return HighResolutionNow();
549 TimeTicks TimeTicks::HighResolutionNow() {
552 static struct mach_timebase_info
info;
553 if (
info.denom == 0) {
554 kern_return_t result = mach_timebase_info(&
info);
558 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond *
561 ticks = (gethrtime() / Time::kNanosecondsPerMicrosecond);
562 #elif V8_LIBRT_NOT_AVAILABLE
567 int result = gettimeofday(&tv,
NULL);
570 ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec);
573 int result = clock_gettime(CLOCK_MONOTONIC, &ts);
576 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond +
577 ts.tv_nsec / Time::kNanosecondsPerMicrosecond);
578 #endif // V8_OS_MACOSX
580 return TimeTicks(ticks + 1);
585 bool TimeTicks::IsHighResolutionClockWorking() {
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
#define LAZY_STATIC_INSTANCE_INITIALIZER
#define ASSERT(condition)
#define ASSERT_GE(v1, v2)
typedef DWORD(__stdcall *DLL_FUNC_TYPE(SymGetOptions))(VOID)
#define LAZY_DYNAMIC_INSTANCE_INITIALIZER
typedef BOOL(__stdcall *DLL_FUNC_TYPE(SymInitialize))(IN HANDLE hProcess
#define ASSERT_LT(v1, v2)
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function info
#define ASSERT_EQ(v1, v2)