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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 | 74 |
75 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { | 75 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { |
76 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get()); | 76 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get()); |
77 if (is_local) | 77 if (is_local) |
78 localtime_r(&t, timestruct); | 78 localtime_r(&t, timestruct); |
79 else | 79 else |
80 gmtime_r(&t, timestruct); | 80 gmtime_r(&t, timestruct); |
81 } | 81 } |
82 #endif // OS_ANDROID | 82 #endif // OS_ANDROID |
83 | 83 |
84 // Helper function to get results from clock_gettime() as TimeTicks object. | 84 // Helper function to get results from clock_gettime() as TimeTicks object. |
Lei Zhang
2015/05/13 23:31:47
Comment needs updating.
miu
2015/05/19 03:14:32
Done.
| |
85 // Minimum requirement is MONOTONIC_CLOCK to be supported on the system. | 85 // Minimum requirement is MONOTONIC_CLOCK to be supported on the system. |
86 // FreeBSD 6 has CLOCK_MONOTONIC but defines _POSIX_MONOTONIC_CLOCK to -1. | 86 // FreeBSD 6 has CLOCK_MONOTONIC but defines _POSIX_MONOTONIC_CLOCK to -1. |
87 #if (defined(OS_POSIX) && \ | 87 #if (defined(OS_POSIX) && \ |
88 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ | 88 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ |
89 defined(OS_BSD) || defined(OS_ANDROID) | 89 defined(OS_BSD) || defined(OS_ANDROID) |
90 base::TimeTicks ClockNow(clockid_t clk_id) { | 90 int64 ClockNow(clockid_t clk_id) { |
91 uint64_t absolute_micro; | 91 uint64_t absolute_micro; |
Lei Zhang
2015/05/13 23:31:47
Maybe just use CheckedNumeric<int64_t> and forget
miu
2015/05/19 03:14:32
Done.
| |
92 | 92 |
93 struct timespec ts; | 93 struct timespec ts; |
94 if (clock_gettime(clk_id, &ts) != 0) { | 94 if (clock_gettime(clk_id, &ts) != 0) { |
95 NOTREACHED() << "clock_gettime(" << clk_id << ") failed."; | 95 NOTREACHED() << "clock_gettime(" << clk_id << ") failed."; |
96 return base::TimeTicks(); | 96 return 0; |
97 } | 97 } |
98 | 98 |
99 absolute_micro = | 99 absolute_micro = |
100 (static_cast<int64>(ts.tv_sec) * base::Time::kMicrosecondsPerSecond) + | 100 (static_cast<int64>(ts.tv_sec) * base::Time::kMicrosecondsPerSecond) + |
101 (static_cast<int64>(ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond)); | 101 (static_cast<int64>(ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond)); |
102 | 102 DCHECK_LE(absolute_micro, |
103 return base::TimeTicks::FromInternalValue(absolute_micro); | 103 static_cast<uint64_t>(std::numeric_limits<int64>::max())); |
104 return static_cast<int64>(absolute_micro); | |
104 } | 105 } |
105 #else // _POSIX_MONOTONIC_CLOCK | 106 #else // _POSIX_MONOTONIC_CLOCK |
106 #error No usable tick clock function on this platform. | 107 #error No usable tick clock function on this platform. |
107 #endif // _POSIX_MONOTONIC_CLOCK | 108 #endif // _POSIX_MONOTONIC_CLOCK |
108 #endif // !defined(OS_MACOSX) | 109 #endif // !defined(OS_MACOSX) |
109 | 110 |
110 } // namespace | 111 } // namespace |
111 | 112 |
112 namespace base { | 113 namespace base { |
113 | 114 |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 } | 304 } |
304 | 305 |
305 // Adjust from Unix (1970) to Windows (1601) epoch. | 306 // Adjust from Unix (1970) to Windows (1601) epoch. |
306 return Time((milliseconds * kMicrosecondsPerMillisecond) + | 307 return Time((milliseconds * kMicrosecondsPerMillisecond) + |
307 kWindowsEpochDeltaMicroseconds); | 308 kWindowsEpochDeltaMicroseconds); |
308 } | 309 } |
309 | 310 |
310 // TimeTicks ------------------------------------------------------------------ | 311 // TimeTicks ------------------------------------------------------------------ |
311 // static | 312 // static |
312 TimeTicks TimeTicks::Now() { | 313 TimeTicks TimeTicks::Now() { |
313 return ClockNow(CLOCK_MONOTONIC); | 314 return TimeTicks(ClockNow(CLOCK_MONOTONIC)); |
314 } | 315 } |
315 | 316 |
316 // static | 317 // static |
317 bool TimeTicks::IsHighResolution() { | 318 bool TimeTicks::IsHighResolution() { |
318 return true; | 319 return true; |
319 } | 320 } |
320 | 321 |
321 // static | 322 // static |
322 TimeTicks TimeTicks::ThreadNow() { | 323 ThreadTicks ThreadTicks::Now() { |
323 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ | 324 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ |
324 defined(OS_ANDROID) | 325 defined(OS_ANDROID) |
325 return ClockNow(CLOCK_THREAD_CPUTIME_ID); | 326 return ThreadTicks(ClockNow(CLOCK_THREAD_CPUTIME_ID)); |
326 #else | 327 #else |
327 NOTREACHED(); | 328 NOTREACHED(); |
328 return TimeTicks(); | 329 return ThreadTicks(); |
329 #endif | 330 #endif |
330 } | 331 } |
331 | 332 |
332 // Use the Chrome OS specific system-wide clock. | 333 // Use the Chrome OS specific system-wide clock. |
333 #if defined(OS_CHROMEOS) | 334 #if defined(OS_CHROMEOS) |
334 // static | 335 // static |
335 TimeTicks TimeTicks::NowFromSystemTraceTime() { | 336 TraceTicks TraceTicks::Now() { |
336 uint64_t absolute_micro; | 337 uint64_t absolute_micro; |
Lei Zhang
2015/05/13 23:31:47
Also use CheckedNumeric<int64_t> here?
miu
2015/05/19 03:14:32
Done.
| |
337 | 338 |
338 struct timespec ts; | 339 struct timespec ts; |
339 if (clock_gettime(kClockSystemTrace, &ts) != 0) { | 340 if (clock_gettime(kClockSystemTrace, &ts) != 0) { |
340 // NB: fall-back for a chrome os build running on linux | 341 // NB: fall-back for a chrome os build running on linux |
341 return Now(); | 342 return TraceTicks(ClockNow(CLOCK_MONOTONIC)); |
342 } | 343 } |
343 | 344 |
344 absolute_micro = | 345 absolute_micro = |
345 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + | 346 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + |
Lei Zhang
2015/05/13 23:31:47
This is a repeat of line 100-101...
miu
2015/05/19 03:14:32
Done. Factored repeated code into a new ConvertTi
| |
346 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); | 347 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); |
347 | 348 DCHECK_LE(absolute_micro, |
348 return TimeTicks(absolute_micro); | 349 static_cast<uint64_t>(std::numeric_limits<int64>::max())); |
350 return TraceTicks(static_cast<int64>(absolute_micro)); | |
349 } | 351 } |
350 | 352 |
351 #else // !defined(OS_CHROMEOS) | 353 #else // !defined(OS_CHROMEOS) |
352 | 354 |
353 // static | 355 // static |
354 TimeTicks TimeTicks::NowFromSystemTraceTime() { | 356 TraceTicks TraceTicks::Now() { |
355 return Now(); | 357 return TraceTicks(ClockNow(CLOCK_MONOTONIC)); |
356 } | 358 } |
357 | 359 |
358 #endif // defined(OS_CHROMEOS) | 360 #endif // defined(OS_CHROMEOS) |
359 | 361 |
360 #endif // !OS_MACOSX | 362 #endif // !OS_MACOSX |
361 | 363 |
362 // static | 364 // static |
363 Time Time::FromTimeVal(struct timeval t) { | 365 Time Time::FromTimeVal(struct timeval t) { |
364 DCHECK_LT(t.tv_usec, static_cast<int>(Time::kMicrosecondsPerSecond)); | 366 DCHECK_LT(t.tv_usec, static_cast<int>(Time::kMicrosecondsPerSecond)); |
365 DCHECK_GE(t.tv_usec, 0); | 367 DCHECK_GE(t.tv_usec, 0); |
(...skipping 20 matching lines...) Expand all Loading... | |
386 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | 388 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
387 return result; | 389 return result; |
388 } | 390 } |
389 int64 us = us_ - kTimeTToMicrosecondsOffset; | 391 int64 us = us_ - kTimeTToMicrosecondsOffset; |
390 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 392 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
391 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 393 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
392 return result; | 394 return result; |
393 } | 395 } |
394 | 396 |
395 } // namespace base | 397 } // namespace base |
OLD | NEW |