11755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org/*
21755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
31755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org *
41755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org *  Use of this source code is governed by a BSD-style license
51755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org *  that can be found in the LICENSE file in the root of the source
61755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org *  tree. An additional intellectual property rights grant can be found
71755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org *  in the file PATENTS.  All contributing project authors may
81755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
91755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org */
101755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
11c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org#include "webrtc/system_wrappers/interface/tick_util.h"
121755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
133f45c2e0ac4cb280f941efa3a3476895795e3dd6pbos@webrtc.org#include <assert.h>
141755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
151755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.orgnamespace webrtc {
161755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
17c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.orgbool TickTime::use_fake_clock_ = false;
18c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgint64_t TickTime::fake_ticks_ = 0;
191755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
20c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgvoid TickTime::UseFakeClock(int64_t start_millisecond) {
21c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  use_fake_clock_ = true;
22c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  fake_ticks_ = MillisecondsToTicks(start_millisecond);
231755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org}
241755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
25c0231afbbf1d7bac40b77da5933715dc63c88144pbos@webrtc.orgvoid TickTime::AdvanceFakeClock(int64_t milliseconds) {
26c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  assert(use_fake_clock_);
27c9dcfa16f1abd6d96ca348fb883886ce515865fbphoglund@webrtc.org  fake_ticks_ += MillisecondsToTicks(milliseconds);
281755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org}
291755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org
30d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.orgint64_t TickTime::QueryOsForTicks() {
31d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  TickTime result;
32d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#if _WIN32
33d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  // TODO(wu): Remove QueryPerformanceCounter implementation.
34d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#ifdef USE_QUERY_PERFORMANCE_COUNTER
35d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  // QueryPerformanceCounter returns the value from the TSC which is
36d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  // incremented at the CPU frequency. The algorithm used requires
37d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  // the CPU frequency to be constant. Technology like speed stepping
38d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  // which has variable CPU frequency will therefore yield unpredictable,
39d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  // incorrect time estimations.
40d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  LARGE_INTEGER qpcnt;
41d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  QueryPerformanceCounter(&qpcnt);
42d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  result.ticks_ = qpcnt.QuadPart;
43d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#else
44d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  static volatile LONG last_time_get_time = 0;
45d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  static volatile int64_t num_wrap_time_get_time = 0;
46d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  volatile LONG* last_time_get_time_ptr = &last_time_get_time;
47d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  DWORD now = timeGetTime();
48d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  // Atomically update the last gotten time
49d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  DWORD old = InterlockedExchange(last_time_get_time_ptr, now);
50d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  if (now < old) {
51d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org    // If now is earlier than old, there may have been a race between
52d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org    // threads.
53d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org    // 0x0fffffff ~3.1 days, the code will not take that long to execute
54d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org    // so it must have been a wrap around.
55d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org    if (old > 0xf0000000 && now < 0x0fffffff) {
56d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org      num_wrap_time_get_time++;
57d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org    }
58d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  }
59d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  result.ticks_ = now + (num_wrap_time_get_time << 32);
60d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#endif
61d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#elif defined(WEBRTC_LINUX)
62d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  struct timespec ts;
63d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  // TODO(wu): Remove CLOCK_REALTIME implementation.
64d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#ifdef WEBRTC_CLOCK_TYPE_REALTIME
65d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  clock_gettime(CLOCK_REALTIME, &ts);
66d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#else
67d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  clock_gettime(CLOCK_MONOTONIC, &ts);
68d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#endif
69d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  result.ticks_ = 1000000000LL * static_cast<int64_t>(ts.tv_sec) +
70d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org      static_cast<int64_t>(ts.tv_nsec);
71d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#elif defined(WEBRTC_MAC)
72d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  static mach_timebase_info_data_t timebase;
73d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  if (timebase.denom == 0) {
74d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org    // Get the timebase if this is the first time we run.
75d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org    // Recommended by Apple's QA1398.
76d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org    kern_return_t retval = mach_timebase_info(&timebase);
77d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org    if (retval != KERN_SUCCESS) {
78d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org      // TODO(wu): Implement CHECK similar to chrome for all the platforms.
79d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org      // Then replace this with a CHECK(retval == KERN_SUCCESS);
80d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#ifndef WEBRTC_IOS
81d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org      asm("int3");
82d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#else
83d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org      __builtin_trap();
84d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#endif  // WEBRTC_IOS
85d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org    }
86d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  }
87d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  // Use timebase to convert absolute time tick units into nanoseconds.
88d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  result.ticks_ = mach_absolute_time() * timebase.numer / timebase.denom;
89d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#else
90d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  struct timeval tv;
91d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  gettimeofday(&tv, NULL);
92d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  result.ticks_ = 1000000LL * static_cast<int64_t>(tv.tv_sec) +
93d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org      static_cast<int64_t>(tv.tv_usec);
94d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org#endif
95d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org  return result.ticks_;
96d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org}
97d305e11d673c2f9e5400628bde7d371bd7b1d010fischman@webrtc.org
981755c2534d4378ecde390d4b7e9189d8e74c7d10phoglund@webrtc.org}  // namespace webrtc
99