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