2 * Copyright (C) 2005 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <utils/Timers.h>
21 #include <utils/ported.h> // may need usleep
22 #include <utils/Log.h>
31 #ifdef HAVE_WIN32_THREADS
35 nsecs_t systemTime(int clock)
37 #if defined(HAVE_POSIX_CLOCKS)
38 static const clockid_t clocks[] = {
41 CLOCK_PROCESS_CPUTIME_ID,
42 CLOCK_THREAD_CPUTIME_ID
45 t.tv_sec = t.tv_nsec = 0;
46 clock_gettime(clocks[clock], &t);
47 return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec;
49 // we don't support the clocks here.
51 t.tv_sec = t.tv_usec = 0;
52 gettimeofday(&t, NULL);
53 return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
57 //#define MONITOR_USLEEP
60 * Sleep long enough that we'll wake up "interval" milliseconds after
61 * the previous snooze.
63 * The "nextTick" argument is updated on each call, and should be passed
64 * in every time. Set its fields to zero on the first call.
66 * Returns the #of intervals we have overslept, which will be zero if we're
67 * on time. [Currently just returns 0 or 1.]
69 int sleepForInterval(long interval, struct timeval* pNextTick)
72 long long timeBeforeNext;
74 bool overSlept = false;
79 * Linux likes to add 9000ms or so.
80 * [not using this for now]
82 //usleepBias = USLEEP_BIAS;
85 gettimeofday(&now, NULL);
87 if (pNextTick->tv_sec == 0) {
88 /* special-case for first time through */
91 android::DurationTimer::addToTimeval(pNextTick, interval);
94 * Compute how much time there is before the next tick. If this
95 * value is negative, we've run over. If we've run over a little
96 * bit we can shorten the next frame to keep the pace steady, but
97 * if we've dramatically overshot we need to re-sync.
99 timeBeforeNext = android::DurationTimer::subtractTimevals(pNextTick, &now);
100 //printf("TOP: now=%ld.%ld next=%ld.%ld diff=%ld\n",
101 // now.tv_sec, now.tv_usec, pNextTick->tv_sec, pNextTick->tv_usec,
102 // (long) timeBeforeNext);
103 if (timeBeforeNext < -interval) {
108 } else if (timeBeforeNext <= 0) {
109 /* slightly over, keep the pace steady */
112 } else if (timeBeforeNext <= interval) {
113 /* right on schedule */
114 sleepTime = timeBeforeNext;
115 } else if (timeBeforeNext > interval && timeBeforeNext <= 2*interval) {
116 /* sleep call returned early; do a longer sleep this time */
117 sleepTime = timeBeforeNext;
118 } else if (timeBeforeNext > interval) {
119 /* we went back in time -- somebody updated system clock? */
120 /* (could also be a *seriously* broken usleep()) */
122 " Impossible: timeBeforeNext = %ld\n", (long)timeBeforeNext);
126 android::DurationTimer::addToTimeval(pNextTick, interval);
128 //printf(" Before sleep: now=%ld.%ld next=%ld.%ld sleepTime=%ld\n",
129 // now.tv_sec, now.tv_usec, pNextTick->tv_sec, pNextTick->tv_usec,
133 * Sleep for the designated period of time.
135 * Linux tends to sleep for longer than requested, often by 17-18ms.
136 * MinGW tends to sleep for less than requested, by as much as 14ms,
137 * but occasionally oversleeps for 40+ms (looks like some external
138 * factors plus round-off on a 64Hz clock). Cygwin is pretty steady.
140 * If you start the MinGW version, and then launch the Cygwin version,
141 * the MinGW clock becomes more erratic. Not entirely sure why.
143 * (There's a lot of stuff here; it's really just a usleep() call with
144 * a bunch of instrumentation.)
147 #if defined(MONITOR_USLEEP)
148 struct timeval before, after;
151 gettimeofday(&before, NULL);
152 usleep((long) sleepTime);
153 gettimeofday(&after, NULL);
155 /* check usleep() accuracy; default Linux threads are pretty sloppy */
156 actual = android::DurationTimer::subtractTimevals(&after, &before);
157 if ((long) actual < sleepTime - 14000 /*(sleepTime/10)*/ ||
158 (long) actual > sleepTime + 20000 /*(sleepTime/10)*/)
160 LOG(LOG_DEBUG, "", " Odd usleep: req=%ld, actual=%ld\n", sleepTime,
164 #ifdef HAVE_WIN32_THREADS
165 Sleep( sleepTime/1000 );
167 usleep((long) sleepTime);
172 //printf("slept %d\n", sleepTime);
175 return 1; // close enough
182 * ===========================================================================
184 * ===========================================================================
187 using namespace android;
190 void DurationTimer::start(void)
192 gettimeofday(&mStartWhen, NULL);
196 void DurationTimer::stop(void)
198 gettimeofday(&mStopWhen, NULL);
201 // Get the duration in microseconds.
202 long long DurationTimer::durationUsecs(void) const
204 return (long) subtractTimevals(&mStopWhen, &mStartWhen);
207 // Subtract two timevals. Returns the difference (ptv1-ptv2) in
209 /*static*/ long long DurationTimer::subtractTimevals(const struct timeval* ptv1,
210 const struct timeval* ptv2)
212 long long stop = ((long long) ptv1->tv_sec) * 1000000LL +
213 ((long long) ptv1->tv_usec);
214 long long start = ((long long) ptv2->tv_sec) * 1000000LL +
215 ((long long) ptv2->tv_usec);
219 // Add the specified amount of time to the timeval.
220 /*static*/ void DurationTimer::addToTimeval(struct timeval* ptv, long usec)
223 LOG(LOG_WARN, "", "Negative values not supported in addToTimeval\n");
227 // normalize tv_usec if necessary
228 if (ptv->tv_usec >= 1000000) {
229 ptv->tv_sec += ptv->tv_usec / 1000000;
230 ptv->tv_usec %= 1000000;
233 ptv->tv_usec += usec % 1000000;
234 if (ptv->tv_usec >= 1000000) {
235 ptv->tv_usec -= 1000000;
238 ptv->tv_sec += usec / 1000000;