The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | #ifndef _LIBS_UTILS_TIMERS_H |
| 21 | #define _LIBS_UTILS_TIMERS_H |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/time.h> |
| 26 | |
| 27 | // ------------------------------------------------------------------ |
| 28 | // C API |
| 29 | |
| 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
| 34 | typedef int64_t nsecs_t; // nano-seconds |
| 35 | |
| 36 | static inline nsecs_t seconds_to_nanoseconds(nsecs_t secs) |
| 37 | { |
| 38 | return secs*1000000000; |
| 39 | } |
| 40 | |
| 41 | static inline nsecs_t milliseconds_to_nanoseconds(nsecs_t secs) |
| 42 | { |
| 43 | return secs*1000000; |
| 44 | } |
| 45 | |
| 46 | static inline nsecs_t microseconds_to_nanoseconds(nsecs_t secs) |
| 47 | { |
| 48 | return secs*1000; |
| 49 | } |
| 50 | |
| 51 | static inline nsecs_t nanoseconds_to_seconds(nsecs_t secs) |
| 52 | { |
| 53 | return secs/1000000000; |
| 54 | } |
| 55 | |
| 56 | static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs) |
| 57 | { |
| 58 | return secs/1000000; |
| 59 | } |
| 60 | |
| 61 | static inline nsecs_t nanoseconds_to_microseconds(nsecs_t secs) |
| 62 | { |
| 63 | return secs/1000; |
| 64 | } |
| 65 | |
| 66 | static inline nsecs_t s2ns(nsecs_t v) {return seconds_to_nanoseconds(v);} |
| 67 | static inline nsecs_t ms2ns(nsecs_t v) {return milliseconds_to_nanoseconds(v);} |
| 68 | static inline nsecs_t us2ns(nsecs_t v) {return microseconds_to_nanoseconds(v);} |
| 69 | static inline nsecs_t ns2s(nsecs_t v) {return nanoseconds_to_seconds(v);} |
| 70 | static inline nsecs_t ns2ms(nsecs_t v) {return nanoseconds_to_milliseconds(v);} |
| 71 | static inline nsecs_t ns2us(nsecs_t v) {return nanoseconds_to_microseconds(v);} |
| 72 | |
| 73 | static inline nsecs_t seconds(nsecs_t v) { return s2ns(v); } |
| 74 | static inline nsecs_t milliseconds(nsecs_t v) { return ms2ns(v); } |
| 75 | static inline nsecs_t microseconds(nsecs_t v) { return us2ns(v); } |
| 76 | |
| 77 | enum { |
| 78 | SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock |
| 79 | SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point |
| 80 | SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock |
| 81 | SYSTEM_TIME_THREAD = 3 // high-resolution per-thread clock |
| 82 | }; |
| 83 | |
| 84 | // return the system-time according to the specified clock |
| 85 | #ifdef __cplusplus |
| 86 | nsecs_t systemTime(int clock = SYSTEM_TIME_MONOTONIC); |
| 87 | #else |
| 88 | nsecs_t systemTime(int clock); |
| 89 | #endif // def __cplusplus |
| 90 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Returns the number of milliseconds to wait between the reference time and the timeout time. |
| 93 | * If the timeout is in the past relative to the reference time, returns 0. |
| 94 | * If the timeout is more than INT_MAX milliseconds in the future relative to the reference time, |
| 95 | * such as when timeoutTime == LLONG_MAX, returns -1 to indicate an infinite timeout delay. |
| 96 | * Otherwise, returns the difference between the reference time and timeout time |
| 97 | * rounded up to the next millisecond. |
| 98 | */ |
| 99 | int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime); |
| 100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | #ifdef __cplusplus |
| 102 | } // extern "C" |
| 103 | #endif |
| 104 | |
| 105 | // ------------------------------------------------------------------ |
| 106 | // C++ API |
| 107 | |
| 108 | #ifdef __cplusplus |
| 109 | |
| 110 | namespace android { |
| 111 | /* |
| 112 | * Time the duration of something. |
| 113 | * |
| 114 | * Includes some timeval manipulation functions. |
| 115 | */ |
| 116 | class DurationTimer { |
| 117 | public: |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 118 | DurationTimer() {} |
| 119 | ~DurationTimer() {} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | |
| 121 | // Start the timer. |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 122 | void start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | // Stop the timer. |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 124 | void stop(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | // Get the duration in microseconds. |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 126 | long long durationUsecs() const; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | |
| 128 | // Subtract two timevals. Returns the difference (ptv1-ptv2) in |
| 129 | // microseconds. |
| 130 | static long long subtractTimevals(const struct timeval* ptv1, |
| 131 | const struct timeval* ptv2); |
| 132 | |
| 133 | // Add the specified amount of time to the timeval. |
| 134 | static void addToTimeval(struct timeval* ptv, long usec); |
| 135 | |
| 136 | private: |
| 137 | struct timeval mStartWhen; |
| 138 | struct timeval mStopWhen; |
| 139 | }; |
| 140 | |
| 141 | }; // android |
| 142 | #endif // def __cplusplus |
| 143 | |
| 144 | #endif // _LIBS_UTILS_TIMERS_H |