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

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

Issue 51333007: Use CLOCK_MONOTONIC_COARSE and CLOCK_REALTIME_COARSE on Linux if available. (Closed)
Patch Set: linux: use CLOCK_{REALTIME,MONOTONIC}_COARSE, v2 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 | « no previous file | 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 #if V8_OS_LINUX && !V8_OS_ANDROID
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 && !V8_OS_ANDROID
54
46 namespace v8 { 55 namespace v8 {
47 namespace internal { 56 namespace internal {
48 57
49 TimeDelta TimeDelta::FromDays(int days) { 58 TimeDelta TimeDelta::FromDays(int days) {
50 return TimeDelta(days * Time::kMicrosecondsPerDay); 59 return TimeDelta(days * Time::kMicrosecondsPerDay);
51 } 60 }
52 61
53 62
54 TimeDelta TimeDelta::FromHours(int hours) { 63 TimeDelta TimeDelta::FromHours(int hours) {
55 return TimeDelta(hours * Time::kMicrosecondsPerHour); 64 return TimeDelta(hours * Time::kMicrosecondsPerHour);
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 } 273 }
265 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10; 274 uint64_t us = static_cast<uint64_t>(us_ + kTimeToEpochInMicroseconds) * 10;
266 ft.dwLowDateTime = static_cast<DWORD>(us); 275 ft.dwLowDateTime = static_cast<DWORD>(us);
267 ft.dwHighDateTime = static_cast<DWORD>(us >> 32); 276 ft.dwHighDateTime = static_cast<DWORD>(us >> 32);
268 return ft; 277 return ft;
269 } 278 }
270 279
271 #elif V8_OS_POSIX 280 #elif V8_OS_POSIX
272 281
273 Time Time::Now() { 282 Time Time::Now() {
283 #if V8_OS_LINUX && !V8_OS_ANDROID
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 && !V8_OS_ANDROID
274 struct timeval tv; 300 struct timeval tv;
275 int result = gettimeofday(&tv, NULL); 301 int result = gettimeofday(&tv, NULL);
276 ASSERT_EQ(0, result); 302 ASSERT_EQ(0, result);
277 USE(result); 303 USE(result);
278 return FromTimeval(tv); 304 return FromTimeval(tv);
305 #endif // V8_OS_LINUX && !V8_OS_ANDROID
279 } 306 }
280 307
281 308
282 Time Time::NowFromSystemTime() { 309 Time Time::NowFromSystemTime() {
283 return Now(); 310 return Now();
284 } 311 }
285 312
286 313
287 Time Time::FromTimespec(struct timespec ts) { 314 Time Time::FromTimespec(struct timespec ts) {
288 ASSERT(ts.tv_nsec >= 0); 315 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 590 // TODO(bmeurer): This is a temporary hack to support cross-compiling
564 // Chrome for Android in AOSP. Remove this once AOSP is fixed, also 591 // Chrome for Android in AOSP. Remove this once AOSP is fixed, also
565 // cleanup the tools/gyp/v8.gyp file. 592 // cleanup the tools/gyp/v8.gyp file.
566 struct timeval tv; 593 struct timeval tv;
567 int result = gettimeofday(&tv, NULL); 594 int result = gettimeofday(&tv, NULL);
568 ASSERT_EQ(0, result); 595 ASSERT_EQ(0, result);
569 USE(result); 596 USE(result);
570 ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec); 597 ticks = (tv.tv_sec * Time::kMicrosecondsPerSecond + tv.tv_usec);
571 #elif V8_OS_POSIX 598 #elif V8_OS_POSIX
572 struct timespec ts; 599 struct timespec ts;
600 #if V8_OS_LINUX && !V8_OS_ANDROID
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 // V8_OS_LINUX && !V8_OS_ANDROID
573 int result = clock_gettime(CLOCK_MONOTONIC, &ts); 613 int result = clock_gettime(CLOCK_MONOTONIC, &ts);
614 #endif // V8_OS_LINUX && !V8_OS_ANDROID
574 ASSERT_EQ(0, result); 615 ASSERT_EQ(0, result);
575 USE(result); 616 USE(result);
576 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond + 617 ticks = (ts.tv_sec * Time::kMicrosecondsPerSecond +
577 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); 618 ts.tv_nsec / Time::kNanosecondsPerMicrosecond);
578 #endif // V8_OS_MACOSX 619 #endif // V8_OS_MACOSX
579 // Make sure we never return 0 here. 620 // Make sure we never return 0 here.
580 return TimeTicks(ticks + 1); 621 return TimeTicks(ticks + 1);
581 } 622 }
582 623
583 624
584 // static 625 // static
585 bool TimeTicks::IsHighResolutionClockWorking() { 626 bool TimeTicks::IsHighResolutionClockWorking() {
586 return true; 627 return true;
587 } 628 }
588 629
589 #endif // V8_OS_WIN 630 #endif // V8_OS_WIN
590 631
591 } } // namespace v8::internal 632 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698