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

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

Issue 68203004: linux: use CLOCK_{REALTIME,MONOTONIC}_COARSE (Closed)
Patch Set: Refactoring. Fix compilation with AOSP. Created 7 years, 1 month 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/platform/time.h ('k') | no next file » | 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
(...skipping 25 matching lines...) Expand all
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 // Make sure CLOCK_{MONOTONIC,REALTIME}_COARSE is defined on Linux.
47 #if V8_OS_LINUX
48 # if !defined(CLOCK_REALTIME_COARSE)
49 # define CLOCK_REALTIME_COARSE 5 // 2.6.32 and up.
50 # endif
51 # if !defined(CLOCK_MONOTONIC_COARSE)
52 # define CLOCK_MONOTONIC_COARSE 6 // 2.6.32 and up.
53 # endif
54 #endif // V8_OS_LINUX
55
56
46 namespace v8 { 57 namespace v8 {
47 namespace internal { 58 namespace internal {
48 59
49 TimeDelta TimeDelta::FromDays(int days) { 60 TimeDelta TimeDelta::FromDays(int days) {
50 return TimeDelta(days * Time::kMicrosecondsPerDay); 61 return TimeDelta(days * Time::kMicrosecondsPerDay);
51 } 62 }
52 63
53 64
54 TimeDelta TimeDelta::FromHours(int hours) { 65 TimeDelta TimeDelta::FromHours(int hours) {
55 return TimeDelta(hours * Time::kMicrosecondsPerHour); 66 return TimeDelta(hours * Time::kMicrosecondsPerHour);
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 } 275 }
265 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10; 276 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10;
266 ft.dwLowDateTime = static_cast<DWORD>(us); 277 ft.dwLowDateTime = static_cast<DWORD>(us);
267 ft.dwHighDateTime = static_cast<DWORD>(us >> 32); 278 ft.dwHighDateTime = static_cast<DWORD>(us >> 32);
268 return ft; 279 return ft;
269 } 280 }
270 281
271 #elif V8_OS_POSIX 282 #elif V8_OS_POSIX
272 283
273 Time Time::Now() { 284 Time Time::Now() {
285 #if V8_LIBRT_NOT_AVAILABLE
286 // TODO(bmeurer): This is a temporary hack to support cross-compiling
287 // Chrome for Android in AOSP. Remove this once AOSP is fixed, also
288 // cleanup the tools/gyp/v8.gyp file.
274 struct timeval tv; 289 struct timeval tv;
275 int result = gettimeofday(&tv, NULL); 290 int result = gettimeofday(&tv, NULL);
276 ASSERT_EQ(0, result); 291 ASSERT_EQ(0, result);
277 USE(result); 292 USE(result);
278 return FromTimeval(tv); 293 return FromTimeval(tv);
294 #elif defined(CLOCK_REALTIME_COARSE)
295 struct timespec ts;
296 // Use CLOCK_REALTIME_COARSE if it's available and has a precision of 1ms
297 // or higher. It's serviced from the vDSO with no system call overhead.
298 static clock_t clock_id = static_cast<clock_t>(0);
299 if (!clock_id) {
300 if (clock_getres(CLOCK_REALTIME_COARSE, &ts) == 0
301 && ts.tv_nsec <= kNanosecondsPerMillisecond)
302 clock_id = CLOCK_REALTIME_COARSE;
303 else
304 clock_id = CLOCK_REALTIME;
305 }
306 int result = clock_gettime(clock_id, &ts);
307 ASSERT_EQ(0, result);
308 USE(result);
309 return FromTimespec(ts);
310 #else
311 struct timeval tv;
312 int result = gettimeofday(&tv, NULL);
313 ASSERT_EQ(0, result);
314 USE(result);
315 return FromTimeval(tv);
316 #endif // V8_LIBRT_NOT_AVAILABLE
279 } 317 }
280 318
281 319
282 Time Time::NowFromSystemTime() { 320 Time Time::NowFromSystemTime() {
283 return Now(); 321 return Now();
284 } 322 }
285 323
286 324
287 Time Time::FromTimespec(struct timespec ts) { 325 Time Time::FromTimespec(struct timespec ts) {
288 ASSERT(ts.tv_nsec >= 0); 326 ASSERT(ts.tv_nsec >= 0);
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 // TODO(bmeurer): This is a temporary hack to support cross-compiling 601 // TODO(bmeurer): This is a temporary hack to support cross-compiling
564 // Chrome for Android in AOSP. Remove this once AOSP is fixed, also 602 // Chrome for Android in AOSP. Remove this once AOSP is fixed, also
565 // cleanup the tools/gyp/v8.gyp file. 603 // cleanup the tools/gyp/v8.gyp file.
566 struct timeval tv; 604 struct timeval tv;
567 int result = gettimeofday(&tv, NULL); 605 int result = gettimeofday(&tv, NULL);
568 ASSERT_EQ(0, result); 606 ASSERT_EQ(0, result);
569 USE(result); 607 USE(result);
570 ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec); 608 ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec);
571 #elif V8_OS_POSIX 609 #elif V8_OS_POSIX
572 struct timespec ts; 610 struct timespec ts;
573 int result = clock_gettime(CLOCK_MONOTONIC, &ts); 611 #if defined(CLOCK_MONOTONIC_COARSE)
612 // Use CLOCK_MONOTONIC_COARSE if it's available and has a precision of 1ms
613 // or higher. It's serviced from the vDSO with no system call overhead.
614 static clock_t clock_id = static_cast<clock_t>(0);
615 if (!clock_id) {
616 if (clock_getres(CLOCK_MONOTONIC_COARSE, &ts) == 0
617 && ts.tv_nsec <= Time::kNanosecondsPerMillisecond)
618 clock_id = CLOCK_MONOTONIC_COARSE;
619 else
620 clock_id = CLOCK_MONOTONIC;
621 }
622 #else
623 static const clock_t clock_id = CLOCK_MONOTONIC;
624 #endif // defined(CLOCK_MONOTONIC_COARSE)
625 int result = clock_gettime(clock_id, &ts);
574 ASSERT_EQ(0, result); 626 ASSERT_EQ(0, result);
575 USE(result); 627 USE(result);
576 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + 628 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond +
577 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); 629 ts.tv_nsec / Time::kNanosecondsPerMicrosecond);
578 #endif // V8_OS_MACOSX 630 #endif // V8_OS_MACOSX
579 // Make sure we never return 0 here. 631 // Make sure we never return 0 here.
580 return TimeTicks(ticks + 1); 632 return TimeTicks(ticks + 1);
581 } 633 }
582 634
583 635
584 // static 636 // static
585 bool TimeTicks::IsHighResolutionClockWorking() { 637 bool TimeTicks::IsHighResolutionClockWorking() {
586 return true; 638 return true;
587 } 639 }
588 640
589 #endif // V8_OS_WIN 641 #endif // V8_OS_WIN
590 642
591 } } // namespace v8::internal 643 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform/time.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698