michael@0: /* michael@0: * Copyright (C) 2005 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: // michael@0: // Timer functions. michael@0: // michael@0: #ifndef _LIBS_UTILS_TIMERS_H michael@0: #define _LIBS_UTILS_TIMERS_H michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: // ------------------------------------------------------------------ michael@0: // C API michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: typedef int64_t nsecs_t; // nano-seconds michael@0: michael@0: static inline nsecs_t seconds_to_nanoseconds(nsecs_t secs) michael@0: { michael@0: return secs*1000000000; michael@0: } michael@0: michael@0: static inline nsecs_t milliseconds_to_nanoseconds(nsecs_t secs) michael@0: { michael@0: return secs*1000000; michael@0: } michael@0: michael@0: static inline nsecs_t microseconds_to_nanoseconds(nsecs_t secs) michael@0: { michael@0: return secs*1000; michael@0: } michael@0: michael@0: static inline nsecs_t nanoseconds_to_seconds(nsecs_t secs) michael@0: { michael@0: return secs/1000000000; michael@0: } michael@0: michael@0: static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs) michael@0: { michael@0: return secs/1000000; michael@0: } michael@0: michael@0: static inline nsecs_t nanoseconds_to_microseconds(nsecs_t secs) michael@0: { michael@0: return secs/1000; michael@0: } michael@0: michael@0: static inline nsecs_t s2ns(nsecs_t v) {return seconds_to_nanoseconds(v);} michael@0: static inline nsecs_t ms2ns(nsecs_t v) {return milliseconds_to_nanoseconds(v);} michael@0: static inline nsecs_t us2ns(nsecs_t v) {return microseconds_to_nanoseconds(v);} michael@0: static inline nsecs_t ns2s(nsecs_t v) {return nanoseconds_to_seconds(v);} michael@0: static inline nsecs_t ns2ms(nsecs_t v) {return nanoseconds_to_milliseconds(v);} michael@0: static inline nsecs_t ns2us(nsecs_t v) {return nanoseconds_to_microseconds(v);} michael@0: michael@0: static inline nsecs_t seconds(nsecs_t v) { return s2ns(v); } michael@0: static inline nsecs_t milliseconds(nsecs_t v) { return ms2ns(v); } michael@0: static inline nsecs_t microseconds(nsecs_t v) { return us2ns(v); } michael@0: michael@0: enum { michael@0: SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock michael@0: SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point michael@0: SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock michael@0: SYSTEM_TIME_THREAD = 3 // high-resolution per-thread clock michael@0: }; michael@0: michael@0: // return the system-time according to the specified clock michael@0: #ifdef __cplusplus michael@0: nsecs_t systemTime(int clock = SYSTEM_TIME_MONOTONIC); michael@0: #else michael@0: nsecs_t systemTime(int clock); michael@0: #endif // def __cplusplus michael@0: michael@0: /** michael@0: * Returns the number of milliseconds to wait between the reference time and the timeout time. michael@0: * If the timeout is in the past relative to the reference time, returns 0. michael@0: * If the timeout is more than INT_MAX milliseconds in the future relative to the reference time, michael@0: * such as when timeoutTime == LLONG_MAX, returns -1 to indicate an infinite timeout delay. michael@0: * Otherwise, returns the difference between the reference time and timeout time michael@0: * rounded up to the next millisecond. michael@0: */ michael@0: int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime); michael@0: michael@0: #ifdef __cplusplus michael@0: } // extern "C" michael@0: #endif michael@0: michael@0: // ------------------------------------------------------------------ michael@0: // C++ API michael@0: michael@0: #ifdef __cplusplus michael@0: michael@0: namespace android { michael@0: /* michael@0: * Time the duration of something. michael@0: * michael@0: * Includes some timeval manipulation functions. michael@0: */ michael@0: class DurationTimer { michael@0: public: michael@0: DurationTimer() {} michael@0: ~DurationTimer() {} michael@0: michael@0: // Start the timer. michael@0: void start(); michael@0: // Stop the timer. michael@0: void stop(); michael@0: // Get the duration in microseconds. michael@0: long long durationUsecs() const; michael@0: michael@0: // Subtract two timevals. Returns the difference (ptv1-ptv2) in michael@0: // microseconds. michael@0: static long long subtractTimevals(const struct timeval* ptv1, michael@0: const struct timeval* ptv2); michael@0: michael@0: // Add the specified amount of time to the timeval. michael@0: static void addToTimeval(struct timeval* ptv, long usec); michael@0: michael@0: private: michael@0: struct timeval mStartWhen; michael@0: struct timeval mStopWhen; michael@0: }; michael@0: michael@0: }; // android michael@0: #endif // def __cplusplus michael@0: michael@0: #endif // _LIBS_UTILS_TIMERS_H