OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 #include <cstring> | 37 #include <cstring> |
38 | 38 |
39 #include "checks.h" | 39 #include "checks.h" |
40 #include "cpu.h" | 40 #include "cpu.h" |
41 #include "platform.h" | 41 #include "platform.h" |
42 #if V8_OS_WIN | 42 #if V8_OS_WIN |
43 #include "win32-headers.h" | 43 #include "win32-headers.h" |
44 #endif | 44 #endif |
45 | 45 |
46 #if V8_OS_LINUX | |
47 #if !defined(CLOCK_REALTIME_COARSE) | |
48 #define CLOCK_REALTIME_COARSE 5 // 2.6.32 and up. | |
49 #endif // !defined(CLOCK_REALTIME_COARSE) | |
50 #if !defined(CLOCK_MONOTONIC_COARSE) | |
51 #define CLOCK_MONOTONIC_COARSE 6 // 2.6.32 and up. | |
52 #endif // !defined(CLOCK_MONOTONIC_COARSE) | |
53 #endif // V8_OS_LINUX | |
54 | |
55 namespace v8 { | 46 namespace v8 { |
56 namespace internal { | 47 namespace internal { |
57 | 48 |
58 TimeDelta TimeDelta::FromDays(int days) { | 49 TimeDelta TimeDelta::FromDays(int days) { |
59 return TimeDelta(days * Time::kMicrosecondsPerDay); | 50 return TimeDelta(days * Time::kMicrosecondsPerDay); |
60 } | 51 } |
61 | 52 |
62 | 53 |
63 TimeDelta TimeDelta::FromHours(int hours) { | 54 TimeDelta TimeDelta::FromHours(int hours) { |
64 return TimeDelta(hours * Time::kMicrosecondsPerHour); | 55 return TimeDelta(hours * Time::kMicrosecondsPerHour); |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 } | 264 } |
274 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10; | 265 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10; |
275 ft.dwLowDateTime = static_cast<DWORD>(us); | 266 ft.dwLowDateTime = static_cast<DWORD>(us); |
276 ft.dwHighDateTime = static_cast<DWORD>(us >> 32); | 267 ft.dwHighDateTime = static_cast<DWORD>(us >> 32); |
277 return ft; | 268 return ft; |
278 } | 269 } |
279 | 270 |
280 #elif V8_OS_POSIX | 271 #elif V8_OS_POSIX |
281 | 272 |
282 Time Time::Now() { | 273 Time Time::Now() { |
283 #if V8_OS_LINUX | |
284 // Use CLOCK_REALTIME_COARSE if it's available and has a precision of 1 ms | |
285 // or higher. It's serviced from the vDSO with no system call overhead. | |
286 static clock_t clock_id = -1; | |
287 struct timespec ts; | |
288 if (clock_id == -1) { | |
289 if (clock_getres(CLOCK_REALTIME_COARSE, &ts) || ts.tv_nsec > 1000 * 1000) { | |
290 clock_id = CLOCK_REALTIME; | |
291 } else { | |
292 clock_id = CLOCK_REALTIME_COARSE; | |
293 } | |
294 } | |
295 int result = clock_gettime(clock_id, &ts); | |
296 ASSERT_EQ(0, result); | |
297 USE(result); | |
298 return FromTimespec(ts); | |
299 #else // V8_OS_LINUX | |
300 struct timeval tv; | 274 struct timeval tv; |
301 int result = gettimeofday(&tv, NULL); | 275 int result = gettimeofday(&tv, NULL); |
302 ASSERT_EQ(0, result); | 276 ASSERT_EQ(0, result); |
303 USE(result); | 277 USE(result); |
304 return FromTimeval(tv); | 278 return FromTimeval(tv); |
305 #endif // V8_OS_LINUX | |
306 } | 279 } |
307 | 280 |
308 | 281 |
309 Time Time::NowFromSystemTime() { | 282 Time Time::NowFromSystemTime() { |
310 return Now(); | 283 return Now(); |
311 } | 284 } |
312 | 285 |
313 | 286 |
314 Time Time::FromTimespec(struct timespec ts) { | 287 Time Time::FromTimespec(struct timespec ts) { |
315 ASSERT(ts.tv_nsec >= 0); | 288 ASSERT(ts.tv_nsec >= 0); |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 // TODO(bmeurer): This is a temporary hack to support cross-compiling | 563 // TODO(bmeurer): This is a temporary hack to support cross-compiling |
591 // Chrome for Android in AOSP. Remove this once AOSP is fixed, also | 564 // Chrome for Android in AOSP. Remove this once AOSP is fixed, also |
592 // cleanup the tools/gyp/v8.gyp file. | 565 // cleanup the tools/gyp/v8.gyp file. |
593 struct timeval tv; | 566 struct timeval tv; |
594 int result = gettimeofday(&tv, NULL); | 567 int result = gettimeofday(&tv, NULL); |
595 ASSERT_EQ(0, result); | 568 ASSERT_EQ(0, result); |
596 USE(result); | 569 USE(result); |
597 ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec); | 570 ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec); |
598 #elif V8_OS_POSIX | 571 #elif V8_OS_POSIX |
599 struct timespec ts; | 572 struct timespec ts; |
600 #if V8_OS_LINUX | |
601 // Use CLOCK_MONOTONIC_COARSE if it's available and has a precision of 1 ms | |
602 // or higher. It's serviced from the vDSO with no system call overhead. | |
603 static clock_t clock_id = -1; | |
604 if (clock_id == -1) { | |
605 if (clock_getres(CLOCK_MONOTONIC_COARSE, &ts) || ts.tv_nsec > 1000 * 1000) { | |
606 clock_id = CLOCK_MONOTONIC; | |
607 } else { | |
608 clock_id = CLOCK_MONOTONIC_COARSE; | |
609 } | |
610 } | |
611 int result = clock_gettime(clock_id, &ts); | |
612 #else | |
613 int result = clock_gettime(CLOCK_MONOTONIC, &ts); | 573 int result = clock_gettime(CLOCK_MONOTONIC, &ts); |
614 #endif // V8_OS_LINUX | |
615 ASSERT_EQ(0, result); | 574 ASSERT_EQ(0, result); |
616 USE(result); | 575 USE(result); |
617 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + | 576 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + |
618 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); | 577 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); |
619 #endif // V8_OS_MACOSX | 578 #endif // V8_OS_MACOSX |
620 // Make sure we never return 0 here. | 579 // Make sure we never return 0 here. |
621 return TimeTicks(ticks + 1); | 580 return TimeTicks(ticks + 1); |
622 } | 581 } |
623 | 582 |
624 | 583 |
625 // static | 584 // static |
626 bool TimeTicks::IsHighResolutionClockWorking() { | 585 bool TimeTicks::IsHighResolutionClockWorking() { |
627 return true; | 586 return true; |
628 } | 587 } |
629 | 588 |
630 #endif // V8_OS_WIN | 589 #endif // V8_OS_WIN |
631 | 590 |
632 } } // namespace v8::internal | 591 } } // namespace v8::internal |
OLD | NEW |