Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/platform/default-platform-time.cc

Issue 97073004: Make Time functions go through the Platform layer. (Closed) Base URL: git://github.com/v8/v8.git@bleeding_edge
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/default-platform.h ('k') | src/platform/time.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include "platform/time.h" 28 #include "default-platform.h"
29 29
30 #if V8_OS_POSIX 30 #if V8_OS_POSIX
31 #include <sys/time.h> 31 #include <sys/time.h>
32 #endif 32 #endif
33 #if V8_OS_MACOSX 33 #if V8_OS_MACOSX
34 #include <mach/mach_time.h> 34 #include <mach/mach_time.h>
35 #endif 35 #endif
36 36
37 #include <cstring>
38
39 #include "checks.h" 37 #include "checks.h"
40 #include "cpu.h" 38 #include "cpu.h"
41 #include "platform.h" 39 #include "platform/time.h"
40 #include "v8.h"
42 #if V8_OS_WIN 41 #if V8_OS_WIN
43 #include "win32-headers.h" 42 #include "win32-headers.h"
44 #endif 43 #endif
45 44
46 namespace v8 { 45 namespace v8 {
47 namespace internal { 46 namespace internal {
48 47
49 TimeDelta TimeDelta::FromDays(int days) {
50 return TimeDelta(days * Time::kMicrosecondsPerDay);
51 }
52
53
54 TimeDelta TimeDelta::FromHours(int hours) {
55 return TimeDelta(hours * Time::kMicrosecondsPerHour);
56 }
57
58
59 TimeDelta TimeDelta::FromMinutes(int minutes) {
60 return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
61 }
62
63
64 TimeDelta TimeDelta::FromSeconds(int64_t seconds) {
65 return TimeDelta(seconds * Time::kMicrosecondsPerSecond);
66 }
67
68
69 TimeDelta TimeDelta::FromMilliseconds(int64_t milliseconds) {
70 return TimeDelta(milliseconds * Time::kMicrosecondsPerMillisecond);
71 }
72
73
74 TimeDelta TimeDelta::FromNanoseconds(int64_t nanoseconds) {
75 return TimeDelta(nanoseconds / Time::kNanosecondsPerMicrosecond);
76 }
77
78
79 int TimeDelta::InDays() const {
80 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
81 }
82
83
84 int TimeDelta::InHours() const {
85 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
86 }
87
88
89 int TimeDelta::InMinutes() const {
90 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
91 }
92
93
94 double TimeDelta::InSecondsF() const {
95 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
96 }
97
98
99 int64_t TimeDelta::InSeconds() const {
100 return delta_ / Time::kMicrosecondsPerSecond;
101 }
102
103
104 double TimeDelta::InMillisecondsF() const {
105 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
106 }
107
108
109 int64_t TimeDelta::InMilliseconds() const {
110 return delta_ / Time::kMicrosecondsPerMillisecond;
111 }
112
113
114 int64_t TimeDelta::InNanoseconds() const {
115 return delta_ * Time::kNanosecondsPerMicrosecond;
116 }
117
118
119 #if V8_OS_MACOSX
120
121 TimeDelta TimeDelta::FromMachTimespec(struct mach_timespec ts) {
122 ASSERT_GE(ts.tv_nsec, 0);
123 ASSERT_LT(ts.tv_nsec,
124 static_cast<long>(Time::kNanosecondsPerSecond)); // NOLINT
125 return TimeDelta(ts.tv_sec * Time::kMicrosecondsPerSecond +
126 ts.tv_nsec / Time::kNanosecondsPerMicrosecond);
127 }
128
129
130 struct mach_timespec TimeDelta::ToMachTimespec() const {
131 struct mach_timespec ts;
132 ASSERT(delta_ >= 0);
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_MACOSX
140
141
142 #if V8_OS_POSIX
143
144 TimeDelta TimeDelta::FromTimespec(struct timespec ts) {
145 ASSERT_GE(ts.tv_nsec, 0);
146 ASSERT_LT(ts.tv_nsec,
147 static_cast<long>(Time::kNanosecondsPerSecond)); // NOLINT
148 return TimeDelta(ts.tv_sec * Time::kMicrosecondsPerSecond +
149 ts.tv_nsec / Time::kNanosecondsPerMicrosecond);
150 }
151
152
153 struct timespec TimeDelta::ToTimespec() const {
154 struct timespec ts;
155 ts.tv_sec = delta_ / Time::kMicrosecondsPerSecond;
156 ts.tv_nsec = (delta_ % Time::kMicrosecondsPerSecond) *
157 Time::kNanosecondsPerMicrosecond;
158 return ts;
159 }
160
161 #endif // V8_OS_POSIX
162
163
164 #if V8_OS_WIN 48 #if V8_OS_WIN
165 49
166 // We implement time using the high-resolution timers so that we can get 50 // We implement time using the high-resolution timers so that we can get
167 // timeouts which are smaller than 10-15ms. To avoid any drift, we 51 // timeouts which are smaller than 10-15ms. To avoid any drift, we
168 // periodically resync the internal clock to the system clock. 52 // periodically resync the internal clock to the system clock.
169 class Clock V8_FINAL { 53 class Clock V8_FINAL {
170 public: 54 public:
171 Clock() : initial_ticks_(GetSystemTicks()), initial_time_(GetSystemTime()) {} 55 Clock() : initial_ticks_(GetSystemTicks()), initial_time_(GetSystemTime()) {}
172 56
173 Time Now() { 57 Time Now() {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 Time initial_time_; 98 Time initial_time_;
215 Mutex mutex_; 99 Mutex mutex_;
216 }; 100 };
217 101
218 102
219 static LazyStaticInstance<Clock, 103 static LazyStaticInstance<Clock,
220 DefaultConstructTrait<Clock>, 104 DefaultConstructTrait<Clock>,
221 ThreadSafeInitOnceTrait>::type clock = LAZY_STATIC_INSTANCE_INITIALIZER; 105 ThreadSafeInitOnceTrait>::type clock = LAZY_STATIC_INSTANCE_INITIALIZER;
222 106
223 107
224 Time Time::Now() { 108 double DefaultPlatform::CurrentTime() {
225 return clock.Pointer()->Now(); 109 return clock.Pointer()->Now().ToInternalValue();
226 } 110 }
227 111
228 112
229 Time Time::NowFromSystemTime() { 113 double DefaultPlatform::CurrentTimeFromSystemTime() {
230 return clock.Pointer()->NowFromSystemTime(); 114 return clock.Pointer()->NowFromSystemTime().ToInternalValue();
231 }
232
233
234 // Time between windows epoch and standard epoch.
235 static const int64_t kTimeToEpochInMicroseconds = V8_INT64_C(11644473600000000);
236
237
238 Time Time::FromFiletime(FILETIME ft) {
239 if (ft.dwLowDateTime == 0 && ft.dwHighDateTime == 0) {
240 return Time();
241 }
242 if (ft.dwLowDateTime == std::numeric_limits<DWORD>::max() &&
243 ft.dwHighDateTime == std::numeric_limits<DWORD>::max()) {
244 return Max();
245 }
246 int64_t us = (static_cast<uint64_t>(ft.dwLowDateTime) +
247 (static_cast<uint64_t>(ft.dwHighDateTime) << 32)) / 10;
248 return Time(us - kTimeToEpochInMicroseconds);
249 }
250
251
252 FILETIME Time::ToFiletime() const {
253 ASSERT(us_ >= 0);
254 FILETIME ft;
255 if (IsNull()) {
256 ft.dwLowDateTime = 0;
257 ft.dwHighDateTime = 0;
258 return ft;
259 }
260 if (IsMax()) {
261 ft.dwLowDateTime = std::numeric_limits<DWORD>::max();
262 ft.dwHighDateTime = std::numeric_limits<DWORD>::max();
263 return ft;
264 }
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);
268 return ft;
269 } 115 }
270 116
271 #elif V8_OS_POSIX 117 #elif V8_OS_POSIX
272 118
273 Time Time::Now() { 119 int64_t DefaultPlatform::CurrentTime() {
274 struct timeval tv; 120 struct timeval tv;
275 int result = gettimeofday(&tv, NULL); 121 int result = gettimeofday(&tv, NULL);
276 ASSERT_EQ(0, result); 122 ASSERT_EQ(0, result);
277 USE(result); 123 USE(result);
278 return FromTimeval(tv); 124 return Time::FromTimeval(tv).ToInternalValue();
279 } 125 }
280 126
281 127
282 Time Time::NowFromSystemTime() { 128 int64_t DefaultPlatform::CurrentTimeFromSystemTime() {
283 return Now(); 129 return CurrentTime();
284 }
285
286
287 Time Time::FromTimespec(struct timespec ts) {
288 ASSERT(ts.tv_nsec >= 0);
289 ASSERT(ts.tv_nsec < static_cast<long>(kNanosecondsPerSecond)); // NOLINT
290 if (ts.tv_nsec == 0 && ts.tv_sec == 0) {
291 return Time();
292 }
293 if (ts.tv_nsec == static_cast<long>(kNanosecondsPerSecond - 1) && // NOLINT
294 ts.tv_sec == std::numeric_limits<time_t>::max()) {
295 return Max();
296 }
297 return Time(ts.tv_sec * kMicrosecondsPerSecond +
298 ts.tv_nsec / kNanosecondsPerMicrosecond);
299 }
300
301
302 struct timespec Time::ToTimespec() const {
303 struct timespec ts;
304 if (IsNull()) {
305 ts.tv_sec = 0;
306 ts.tv_nsec = 0;
307 return ts;
308 }
309 if (IsMax()) {
310 ts.tv_sec = std::numeric_limits<time_t>::max();
311 ts.tv_nsec = static_cast<long>(kNanosecondsPerSecond - 1); // NOLINT
312 return ts;
313 }
314 ts.tv_sec = us_ / kMicrosecondsPerSecond;
315 ts.tv_nsec = (us_ % kMicrosecondsPerSecond) * kNanosecondsPerMicrosecond;
316 return ts;
317 }
318
319
320 Time Time::FromTimeval(struct timeval tv) {
321 ASSERT(tv.tv_usec >= 0);
322 ASSERT(tv.tv_usec < static_cast<suseconds_t>(kMicrosecondsPerSecond));
323 if (tv.tv_usec == 0 && tv.tv_sec == 0) {
324 return Time();
325 }
326 if (tv.tv_usec == static_cast<suseconds_t>(kMicrosecondsPerSecond - 1) &&
327 tv.tv_sec == std::numeric_limits<time_t>::max()) {
328 return Max();
329 }
330 return Time(tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec);
331 }
332
333
334 struct timeval Time::ToTimeval() const {
335 struct timeval tv;
336 if (IsNull()) {
337 tv.tv_sec = 0;
338 tv.tv_usec = 0;
339 return tv;
340 }
341 if (IsMax()) {
342 tv.tv_sec = std::numeric_limits<time_t>::max();
343 tv.tv_usec = static_cast<suseconds_t>(kMicrosecondsPerSecond - 1);
344 return tv;
345 }
346 tv.tv_sec = us_ / kMicrosecondsPerSecond;
347 tv.tv_usec = us_ % kMicrosecondsPerSecond;
348 return tv;
349 } 130 }
350 131
351 #endif // V8_OS_WIN 132 #endif // V8_OS_WIN
352 133
353 134
354 Time Time::FromJsTime(double ms_since_epoch) {
355 // The epoch is a valid time, so this constructor doesn't interpret
356 // 0 as the null time.
357 if (ms_since_epoch == std::numeric_limits<double>::max()) {
358 return Max();
359 }
360 return Time(
361 static_cast<int64_t>(ms_since_epoch * kMicrosecondsPerMillisecond));
362 }
363
364
365 double Time::ToJsTime() const {
366 if (IsNull()) {
367 // Preserve 0 so the invalid result doesn't depend on the platform.
368 return 0;
369 }
370 if (IsMax()) {
371 // Preserve max without offset to prevent overflow.
372 return std::numeric_limits<double>::max();
373 }
374 return static_cast<double>(us_) / kMicrosecondsPerMillisecond;
375 }
376
377
378 #if V8_OS_WIN 135 #if V8_OS_WIN
379 136
380 class TickClock { 137 class TickClock {
381 public: 138 public:
382 virtual ~TickClock() {} 139 virtual ~TickClock() {}
383 virtual int64_t Now() = 0; 140 virtual int64_t Now() = 0;
384 virtual bool IsHighResolution() = 0; 141 virtual bool IsHighResolution() = 0;
385 }; 142 };
386 143
387 144
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 // is unreliable, fallback to the low-resolution tick clock. 261 // is unreliable, fallback to the low-resolution tick clock.
505 CPU cpu; 262 CPU cpu;
506 if (strcmp(cpu.vendor(), "AuthenticAMD") == 0 && cpu.family() == 15) { 263 if (strcmp(cpu.vendor(), "AuthenticAMD") == 0 && cpu.family() == 15) {
507 return tick_clock.Pointer(); 264 return tick_clock.Pointer();
508 } 265 }
509 266
510 return new HighResolutionTickClock(ticks_per_second.QuadPart); 267 return new HighResolutionTickClock(ticks_per_second.QuadPart);
511 } 268 }
512 }; 269 };
513 270
514
515 static LazyDynamicInstance<TickClock, 271 static LazyDynamicInstance<TickClock,
516 CreateHighResTickClockTrait, 272 CreateHighResTickClockTrait,
517 ThreadSafeInitOnceTrait>::type high_res_tick_clock = 273 ThreadSafeInitOnceTrait>::type high_res_tick_clock =
518 LAZY_DYNAMIC_INSTANCE_INITIALIZER; 274 LAZY_DYNAMIC_INSTANCE_INITIALIZER;
519 275
520 276
521 TimeTicks TimeTicks::Now() { 277 int64_t DefaultPlatform::TimeTicksNow() {
522 // Make sure we never return 0 here. 278 return tick_clock.Pointer()->Now();
523 TimeTicks ticks(tick_clock.Pointer()->Now());
524 ASSERT(!ticks.IsNull());
525 return ticks;
526 } 279 }
527 280
528 281
529 TimeTicks TimeTicks::HighResolutionNow() { 282 int64_t DefaultPlatform::TimeTicksHighResNow() {
530 // Make sure we never return 0 here. 283 return high_res_tick_clock.Pointer()->Now();
531 TimeTicks ticks(high_res_tick_clock.Pointer()->Now());
532 ASSERT(!ticks.IsNull());
533 return ticks;
534 } 284 }
535 285
536 286
537 // static 287 bool DefaultPlatform::TimeTicksHasHighRes() {
538 bool TimeTicks::IsHighResolutionClockWorking() {
539 return high_res_tick_clock.Pointer()->IsHighResolution(); 288 return high_res_tick_clock.Pointer()->IsHighResolution();
540 } 289 }
541 290
542 #else // V8_OS_WIN 291 #else // V8_OS_WIN
543 292
544 TimeTicks TimeTicks::Now() { 293 int64_t DefaultPlatform::TimeTicksHighResNow() {
545 return HighResolutionNow();
546 }
547
548
549 TimeTicks TimeTicks::HighResolutionNow() {
550 int64_t ticks; 294 int64_t ticks;
551 #if V8_OS_MACOSX 295 #if V8_OS_MACOSX
552 static struct mach_timebase_info info; 296 static struct mach_timebase_info info;
553 if (info.denom == 0) { 297 if (info.denom == 0) {
554 kern_return_t result = mach_timebase_info(&info); 298 kern_return_t result = mach_timebase_info(&info);
555 ASSERT_EQ(KERN_SUCCESS, result); 299 ASSERT_EQ(KERN_SUCCESS, result);
556 USE(result); 300 USE(result);
557 } 301 }
558 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond * 302 ticks = (mach_absolute_time() / Time::kNanosecondsPerMicrosecond *
559 info.numer / info.denom); 303 info.numer / info.denom);
(...skipping 10 matching lines...) Expand all
570 ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec); 314 ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec);
571 #elif V8_OS_POSIX 315 #elif V8_OS_POSIX
572 struct timespec ts; 316 struct timespec ts;
573 int result = clock_gettime(CLOCK_MONOTONIC, &ts); 317 int result = clock_gettime(CLOCK_MONOTONIC, &ts);
574 ASSERT_EQ(0, result); 318 ASSERT_EQ(0, result);
575 USE(result); 319 USE(result);
576 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + 320 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond +
577 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); 321 ts.tv_nsec / Time::kNanosecondsPerMicrosecond);
578 #endif // V8_OS_MACOSX 322 #endif // V8_OS_MACOSX
579 // Make sure we never return 0 here. 323 // Make sure we never return 0 here.
580 return TimeTicks(ticks + 1); 324 return ticks + 1;
581 } 325 }
582 326
583 327
584 // static 328 int64_t DefaultPlatform::TimeTicksNow() {
585 bool TimeTicks::IsHighResolutionClockWorking() { 329 return DefaultPlatform::TimeTicksHighResNow();
330 }
331
332
333 bool DefaultPlatform::TimeTicksHasHighRes() {
586 return true; 334 return true;
587 } 335 }
588 336
589 #endif // V8_OS_WIN 337 #endif // V8_OS_WIN
590 338
591 } } // namespace v8::internal 339 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/default-platform.h ('k') | src/platform/time.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698