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

Side by Side Diff: base/time/time_posix.cc

Issue 1122153002: Fixit: Fork base::TimeTicks --> TimeTicks + ThreadTicks + TraceTicks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@FIXIT_timeclasses_1of2
Patch Set: REBASE after it passed CQ but did not commit to tree Created 5 years, 6 months 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 | « base/time/time_mac.cc ('k') | base/time/time_unittest.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 (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
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 int64 ConvertTimespecToMicros(const struct timespec& ts) {
85 // Minimum requirement is MONOTONIC_CLOCK to be supported on the system. 85 base::CheckedNumeric<int64> result(ts.tv_sec);
86 // FreeBSD 6 has CLOCK_MONOTONIC but defines _POSIX_MONOTONIC_CLOCK to -1. 86 result *= base::Time::kMicrosecondsPerSecond;
87 result += (ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond);
88 return result.ValueOrDie();
89 }
90
91 // Helper function to get results from clock_gettime() and convert to a
92 // microsecond timebase. Minimum requirement is MONOTONIC_CLOCK to be supported
93 // on the system. FreeBSD 6 has CLOCK_MONOTONIC but defines
94 // _POSIX_MONOTONIC_CLOCK to -1.
87 #if (defined(OS_POSIX) && \ 95 #if (defined(OS_POSIX) && \
88 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ 96 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \
89 defined(OS_BSD) || defined(OS_ANDROID) 97 defined(OS_BSD) || defined(OS_ANDROID)
90 base::TimeTicks ClockNow(clockid_t clk_id) { 98 int64 ClockNow(clockid_t clk_id) {
91 uint64_t absolute_micro;
92
93 struct timespec ts; 99 struct timespec ts;
94 if (clock_gettime(clk_id, &ts) != 0) { 100 if (clock_gettime(clk_id, &ts) != 0) {
95 NOTREACHED() << "clock_gettime(" << clk_id << ") failed."; 101 NOTREACHED() << "clock_gettime(" << clk_id << ") failed.";
96 return base::TimeTicks(); 102 return 0;
97 } 103 }
98 104 return ConvertTimespecToMicros(ts);
99 absolute_micro =
100 (static_cast<int64>(ts.tv_sec) * base::Time::kMicrosecondsPerSecond) +
101 (static_cast<int64>(ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond));
102
103 return base::TimeTicks::FromInternalValue(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
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
338 struct timespec ts; 337 struct timespec ts;
339 if (clock_gettime(kClockSystemTrace, &ts) != 0) { 338 if (clock_gettime(kClockSystemTrace, &ts) != 0) {
340 // NB: fall-back for a chrome os build running on linux 339 // NB: fall-back for a chrome os build running on linux
341 return Now(); 340 return TraceTicks(ClockNow(CLOCK_MONOTONIC));
342 } 341 }
343 342 return TraceTicks(ConvertTimespecToMicros(ts));
344 absolute_micro =
345 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) +
346 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond);
347
348 return TimeTicks(absolute_micro);
349 } 343 }
350 344
351 #else // !defined(OS_CHROMEOS) 345 #else // !defined(OS_CHROMEOS)
352 346
353 // static 347 // static
354 TimeTicks TimeTicks::NowFromSystemTraceTime() { 348 TraceTicks TraceTicks::Now() {
355 return Now(); 349 return TraceTicks(ClockNow(CLOCK_MONOTONIC));
356 } 350 }
357 351
358 #endif // defined(OS_CHROMEOS) 352 #endif // defined(OS_CHROMEOS)
359 353
360 #endif // !OS_MACOSX 354 #endif // !OS_MACOSX
361 355
362 // static 356 // static
363 Time Time::FromTimeVal(struct timeval t) { 357 Time Time::FromTimeVal(struct timeval t) {
364 DCHECK_LT(t.tv_usec, static_cast<int>(Time::kMicrosecondsPerSecond)); 358 DCHECK_LT(t.tv_usec, static_cast<int>(Time::kMicrosecondsPerSecond));
365 DCHECK_GE(t.tv_usec, 0); 359 DCHECK_GE(t.tv_usec, 0);
(...skipping 20 matching lines...) Expand all
386 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; 380 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1;
387 return result; 381 return result;
388 } 382 }
389 int64 us = us_ - kTimeTToMicrosecondsOffset; 383 int64 us = us_ - kTimeTToMicrosecondsOffset;
390 result.tv_sec = us / Time::kMicrosecondsPerSecond; 384 result.tv_sec = us / Time::kMicrosecondsPerSecond;
391 result.tv_usec = us % Time::kMicrosecondsPerSecond; 385 result.tv_usec = us % Time::kMicrosecondsPerSecond;
392 return result; 386 return result;
393 } 387 }
394 388
395 } // namespace base 389 } // namespace base
OLDNEW
« no previous file with comments | « base/time/time_mac.cc ('k') | base/time/time_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698