• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 #include <utils/Log.h>
24 #include <utils/Errors.h>
25 #include <utils/StopWatch.h>
26 
27 /*****************************************************************************/
28 
29 namespace android {
30 
31 
StopWatch(const char * name,int clock,uint32_t flags)32 StopWatch::StopWatch(const char *name, int clock, uint32_t flags)
33     :   mName(name), mClock(clock), mFlags(flags),
34         mStartTime(0), mNumLaps(0)
35 {
36     mStartTime = systemTime(mClock);
37 }
38 
~StopWatch()39 StopWatch::~StopWatch()
40 {
41     nsecs_t elapsed = elapsedTime();
42     const int n = mNumLaps;
43     LOGD("StopWatch %s (us): %lld ", mName, ns2us(elapsed));
44     for (int i=0 ; i<n ; i++) {
45         const nsecs_t soFar = mLaps[i].soFar;
46         const nsecs_t thisLap = mLaps[i].thisLap;
47         LOGD(" [%d: %lld, %lld]", i, ns2us(soFar), ns2us(thisLap));
48     }
49 }
50 
name() const51 const char* StopWatch::name() const
52 {
53     return mName;
54 }
55 
lap()56 nsecs_t StopWatch::lap()
57 {
58     nsecs_t elapsed = elapsedTime();
59     if (mNumLaps >= 8) {
60         elapsed = 0;
61     } else {
62         const int n = mNumLaps;
63         mLaps[n].soFar   = elapsed;
64         mLaps[n].thisLap = n ? (elapsed - mLaps[n-1].soFar) : elapsed;
65         mNumLaps = n+1;
66     }
67     return elapsed;
68 }
69 
elapsedTime() const70 nsecs_t StopWatch::elapsedTime() const
71 {
72     return systemTime(mClock) - mStartTime;
73 }
74 
75 
76 /*****************************************************************************/
77 
78 }; // namespace android
79 
80