1 /*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 //
18 // Timer functions.
19 //
20 #include <utils/Timers.h>
21 #include <utils/Log.h>
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/time.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <limits.h>
30
31 #ifdef HAVE_WIN32_THREADS
32 #include <windows.h>
33 #endif
34
systemTime(int clock)35 nsecs_t systemTime(int clock)
36 {
37 #if defined(HAVE_POSIX_CLOCKS)
38 static const clockid_t clocks[] = {
39 CLOCK_REALTIME,
40 CLOCK_MONOTONIC,
41 CLOCK_PROCESS_CPUTIME_ID,
42 CLOCK_THREAD_CPUTIME_ID
43 };
44 struct timespec t;
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;
48 #else
49 // we don't support the clocks here.
50 struct timeval t;
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;
54 #endif
55 }
56
toMillisecondTimeoutDelay(nsecs_t referenceTime,nsecs_t timeoutTime)57 int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime)
58 {
59 int timeoutDelayMillis;
60 if (timeoutTime > referenceTime) {
61 uint64_t timeoutDelay = uint64_t(timeoutTime - referenceTime);
62 if (timeoutDelay > uint64_t((INT_MAX - 1) * 1000000LL)) {
63 timeoutDelayMillis = -1;
64 } else {
65 timeoutDelayMillis = (timeoutDelay + 999999LL) / 1000000LL;
66 }
67 } else {
68 timeoutDelayMillis = 0;
69 }
70 return timeoutDelayMillis;
71 }
72
73
74 /*
75 * ===========================================================================
76 * DurationTimer
77 * ===========================================================================
78 */
79
80 using namespace android;
81
82 // Start the timer.
start(void)83 void DurationTimer::start(void)
84 {
85 gettimeofday(&mStartWhen, NULL);
86 }
87
88 // Stop the timer.
stop(void)89 void DurationTimer::stop(void)
90 {
91 gettimeofday(&mStopWhen, NULL);
92 }
93
94 // Get the duration in microseconds.
durationUsecs(void) const95 long long DurationTimer::durationUsecs(void) const
96 {
97 return (long) subtractTimevals(&mStopWhen, &mStartWhen);
98 }
99
100 // Subtract two timevals. Returns the difference (ptv1-ptv2) in
101 // microseconds.
subtractTimevals(const struct timeval * ptv1,const struct timeval * ptv2)102 /*static*/ long long DurationTimer::subtractTimevals(const struct timeval* ptv1,
103 const struct timeval* ptv2)
104 {
105 long long stop = ((long long) ptv1->tv_sec) * 1000000LL +
106 ((long long) ptv1->tv_usec);
107 long long start = ((long long) ptv2->tv_sec) * 1000000LL +
108 ((long long) ptv2->tv_usec);
109 return stop - start;
110 }
111
112 // Add the specified amount of time to the timeval.
addToTimeval(struct timeval * ptv,long usec)113 /*static*/ void DurationTimer::addToTimeval(struct timeval* ptv, long usec)
114 {
115 if (usec < 0) {
116 ALOG(LOG_WARN, "", "Negative values not supported in addToTimeval\n");
117 return;
118 }
119
120 // normalize tv_usec if necessary
121 if (ptv->tv_usec >= 1000000) {
122 ptv->tv_sec += ptv->tv_usec / 1000000;
123 ptv->tv_usec %= 1000000;
124 }
125
126 ptv->tv_usec += usec % 1000000;
127 if (ptv->tv_usec >= 1000000) {
128 ptv->tv_usec -= 1000000;
129 ptv->tv_sec++;
130 }
131 ptv->tv_sec += usec / 1000000;
132 }
133
134