| The Android Open Source Project | cbb1011 | 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 | #define LOG_TAG "StopWatch" | 
|  | 18 |  | 
|  | 19 | #include <string.h> | 
|  | 20 | #include <stdlib.h> | 
|  | 21 | #include <stdio.h> | 
|  | 22 |  | 
| Andrew Hsieh | 24e57d5 | 2012-02-27 18:50:55 -0800 | [diff] [blame] | 23 | /* for PRId64 */ | 
| Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 24 | #ifndef __STDC_FORMAT_MACROS | 
| Andrew Hsieh | 24e57d5 | 2012-02-27 18:50:55 -0800 | [diff] [blame] | 25 | #define __STDC_FORMAT_MACROS 1 | 
| Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 26 | #endif | 
| Andrew Hsieh | 24e57d5 | 2012-02-27 18:50:55 -0800 | [diff] [blame] | 27 | #include <inttypes.h> | 
|  | 28 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include <utils/Log.h> | 
|  | 30 | #include <utils/Errors.h> | 
|  | 31 | #include <utils/StopWatch.h> | 
|  | 32 |  | 
|  | 33 | /*****************************************************************************/ | 
|  | 34 |  | 
|  | 35 | namespace android { | 
|  | 36 |  | 
|  | 37 |  | 
|  | 38 | StopWatch::StopWatch(const char *name, int clock, uint32_t flags) | 
| Jeff Brown | 66db689 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 39 | :   mName(name), mClock(clock), mFlags(flags) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | { | 
| Jeff Brown | 66db689 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 41 | reset(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | } | 
|  | 43 |  | 
|  | 44 | StopWatch::~StopWatch() | 
|  | 45 | { | 
|  | 46 | nsecs_t elapsed = elapsedTime(); | 
|  | 47 | const int n = mNumLaps; | 
| Andrew Hsieh | 24e57d5 | 2012-02-27 18:50:55 -0800 | [diff] [blame] | 48 | ALOGD("StopWatch %s (us): %" PRId64 " ", mName, ns2us(elapsed)); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | for (int i=0 ; i<n ; i++) { | 
|  | 50 | const nsecs_t soFar = mLaps[i].soFar; | 
|  | 51 | const nsecs_t thisLap = mLaps[i].thisLap; | 
| Andrew Hsieh | 24e57d5 | 2012-02-27 18:50:55 -0800 | [diff] [blame] | 52 | ALOGD(" [%d: %" PRId64 ", %" PRId64, i, ns2us(soFar), ns2us(thisLap)); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | } | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | const char* StopWatch::name() const | 
|  | 57 | { | 
|  | 58 | return mName; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | nsecs_t StopWatch::lap() | 
|  | 62 | { | 
|  | 63 | nsecs_t elapsed = elapsedTime(); | 
|  | 64 | if (mNumLaps >= 8) { | 
|  | 65 | elapsed = 0; | 
|  | 66 | } else { | 
|  | 67 | const int n = mNumLaps; | 
|  | 68 | mLaps[n].soFar   = elapsed; | 
|  | 69 | mLaps[n].thisLap = n ? (elapsed - mLaps[n-1].soFar) : elapsed; | 
|  | 70 | mNumLaps = n+1; | 
|  | 71 | } | 
|  | 72 | return elapsed; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | nsecs_t StopWatch::elapsedTime() const | 
|  | 76 | { | 
|  | 77 | return systemTime(mClock) - mStartTime; | 
|  | 78 | } | 
|  | 79 |  | 
| Jeff Brown | 66db689 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 80 | void StopWatch::reset() | 
|  | 81 | { | 
|  | 82 | mNumLaps = 0; | 
|  | 83 | mStartTime = systemTime(mClock); | 
|  | 84 | } | 
|  | 85 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 |  | 
|  | 87 | /*****************************************************************************/ | 
|  | 88 |  | 
|  | 89 | }; // namespace android | 
|  | 90 |  |