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 | #include <utils/Timers.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 30 | #ifdef HAVE_WIN32_THREADS |
| 31 | #include <windows.h> |
| 32 | #endif |
| 33 | |
| 34 | nsecs_t systemTime(int clock) |
| 35 | { |
| 36 | #if defined(HAVE_POSIX_CLOCKS) |
| 37 | static const clockid_t clocks[] = { |
| 38 | CLOCK_REALTIME, |
| 39 | CLOCK_MONOTONIC, |
| 40 | CLOCK_PROCESS_CPUTIME_ID, |
| 41 | CLOCK_THREAD_CPUTIME_ID |
| 42 | }; |
| 43 | struct timespec t; |
| 44 | t.tv_sec = t.tv_nsec = 0; |
| 45 | clock_gettime(clocks[clock], &t); |
| 46 | return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec; |
| 47 | #else |
| 48 | // we don't support the clocks here. |
| 49 | struct timeval t; |
| 50 | t.tv_sec = t.tv_usec = 0; |
| 51 | gettimeofday(&t, NULL); |
| 52 | return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL; |
| 53 | #endif |
| 54 | } |
| 55 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | /* |
| 57 | * =========================================================================== |
| 58 | * DurationTimer |
| 59 | * =========================================================================== |
| 60 | */ |
| 61 | |
| 62 | using namespace android; |
| 63 | |
| 64 | // Start the timer. |
| 65 | void DurationTimer::start(void) |
| 66 | { |
| 67 | gettimeofday(&mStartWhen, NULL); |
| 68 | } |
| 69 | |
| 70 | // Stop the timer. |
| 71 | void DurationTimer::stop(void) |
| 72 | { |
| 73 | gettimeofday(&mStopWhen, NULL); |
| 74 | } |
| 75 | |
| 76 | // Get the duration in microseconds. |
| 77 | long long DurationTimer::durationUsecs(void) const |
| 78 | { |
| 79 | return (long) subtractTimevals(&mStopWhen, &mStartWhen); |
| 80 | } |
| 81 | |
| 82 | // Subtract two timevals. Returns the difference (ptv1-ptv2) in |
| 83 | // microseconds. |
| 84 | /*static*/ long long DurationTimer::subtractTimevals(const struct timeval* ptv1, |
| 85 | const struct timeval* ptv2) |
| 86 | { |
| 87 | long long stop = ((long long) ptv1->tv_sec) * 1000000LL + |
| 88 | ((long long) ptv1->tv_usec); |
| 89 | long long start = ((long long) ptv2->tv_sec) * 1000000LL + |
| 90 | ((long long) ptv2->tv_usec); |
| 91 | return stop - start; |
| 92 | } |
| 93 | |
| 94 | // Add the specified amount of time to the timeval. |
| 95 | /*static*/ void DurationTimer::addToTimeval(struct timeval* ptv, long usec) |
| 96 | { |
| 97 | if (usec < 0) { |
| 98 | LOG(LOG_WARN, "", "Negative values not supported in addToTimeval\n"); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // normalize tv_usec if necessary |
| 103 | if (ptv->tv_usec >= 1000000) { |
| 104 | ptv->tv_sec += ptv->tv_usec / 1000000; |
| 105 | ptv->tv_usec %= 1000000; |
| 106 | } |
| 107 | |
| 108 | ptv->tv_usec += usec % 1000000; |
| 109 | if (ptv->tv_usec >= 1000000) { |
| 110 | ptv->tv_usec -= 1000000; |
| 111 | ptv->tv_sec++; |
| 112 | } |
| 113 | ptv->tv_sec += usec / 1000000; |
| 114 | } |
| 115 | |