OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/time/time.h" | 5 #include "base/time/time.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <sys/time.h> | 8 #include <sys/time.h> |
9 #include <time.h> | 9 #include <time.h> |
10 #if defined(OS_ANDROID) && !defined(__LP64__) | 10 #if defined(OS_ANDROID) && !defined(__LP64__) |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 return result.ValueOrDie(); | 86 return result.ValueOrDie(); |
87 } | 87 } |
88 | 88 |
89 // Helper function to get results from clock_gettime() and convert to a | 89 // Helper function to get results from clock_gettime() and convert to a |
90 // microsecond timebase. Minimum requirement is MONOTONIC_CLOCK to be supported | 90 // microsecond timebase. Minimum requirement is MONOTONIC_CLOCK to be supported |
91 // on the system. FreeBSD 6 has CLOCK_MONOTONIC but defines | 91 // on the system. FreeBSD 6 has CLOCK_MONOTONIC but defines |
92 // _POSIX_MONOTONIC_CLOCK to -1. | 92 // _POSIX_MONOTONIC_CLOCK to -1. |
93 #if (defined(OS_POSIX) && \ | 93 #if (defined(OS_POSIX) && \ |
94 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ | 94 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ |
95 defined(OS_BSD) || defined(OS_ANDROID) | 95 defined(OS_BSD) || defined(OS_ANDROID) |
| 96 |
| 97 const char* kLinuxMonotonicClockId = "LINUX_CLOCK_MONOTONIC_MICROS"; |
| 98 |
96 int64_t ClockNow(clockid_t clk_id) { | 99 int64_t ClockNow(clockid_t clk_id) { |
97 struct timespec ts; | 100 struct timespec ts; |
98 if (clock_gettime(clk_id, &ts) != 0) { | 101 if (clock_gettime(clk_id, &ts) != 0) { |
99 NOTREACHED() << "clock_gettime(" << clk_id << ") failed."; | 102 NOTREACHED() << "clock_gettime(" << clk_id << ") failed."; |
100 return 0; | 103 return 0; |
101 } | 104 } |
102 return ConvertTimespecToMicros(ts); | 105 return ConvertTimespecToMicros(ts); |
103 } | 106 } |
104 #else // _POSIX_MONOTONIC_CLOCK | 107 #else // _POSIX_MONOTONIC_CLOCK |
105 #error No usable tick clock function on this platform. | 108 #error No usable tick clock function on this platform. |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 kWindowsEpochDeltaMicroseconds); | 308 kWindowsEpochDeltaMicroseconds); |
306 } | 309 } |
307 | 310 |
308 // TimeTicks ------------------------------------------------------------------ | 311 // TimeTicks ------------------------------------------------------------------ |
309 // static | 312 // static |
310 TimeTicks TimeTicks::Now() { | 313 TimeTicks TimeTicks::Now() { |
311 return TimeTicks(ClockNow(CLOCK_MONOTONIC)); | 314 return TimeTicks(ClockNow(CLOCK_MONOTONIC)); |
312 } | 315 } |
313 | 316 |
314 // static | 317 // static |
| 318 std::string TimeTicks::ClockId() { |
| 319 return kLinuxMonotonicClockId; |
| 320 } |
| 321 |
| 322 // static |
315 bool TimeTicks::IsHighResolution() { | 323 bool TimeTicks::IsHighResolution() { |
316 return true; | 324 return true; |
317 } | 325 } |
318 | 326 |
319 // static | 327 // static |
320 ThreadTicks ThreadTicks::Now() { | 328 ThreadTicks ThreadTicks::Now() { |
321 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ | 329 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ |
322 defined(OS_ANDROID) | 330 defined(OS_ANDROID) |
323 return ThreadTicks(ClockNow(CLOCK_THREAD_CPUTIME_ID)); | 331 return ThreadTicks(ClockNow(CLOCK_THREAD_CPUTIME_ID)); |
324 #else | 332 #else |
(...skipping 29 matching lines...) Expand all Loading... |
354 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | 362 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
355 return result; | 363 return result; |
356 } | 364 } |
357 int64_t us = us_ - kTimeTToMicrosecondsOffset; | 365 int64_t us = us_ - kTimeTToMicrosecondsOffset; |
358 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 366 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
359 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 367 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
360 return result; | 368 return result; |
361 } | 369 } |
362 | 370 |
363 } // namespace base | 371 } // namespace base |
OLD | NEW |