• 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 <utils/StopWatch.h>
20 
21 /* for PRId64 */
22 #ifndef __STDC_FORMAT_MACROS
23 #define __STDC_FORMAT_MACROS 1
24 #endif
25 #include <inttypes.h>
26 
27 #include <utils/Log.h>
28 
29 /*****************************************************************************/
30 
31 namespace android {
32 
33 
StopWatch(const char * name,int clock,uint32_t flags)34 StopWatch::StopWatch(const char *name, int clock, uint32_t flags)
35     :   mName(name), mClock(clock), mFlags(flags)
36 {
37     reset();
38 }
39 
~StopWatch()40 StopWatch::~StopWatch()
41 {
42     nsecs_t elapsed = elapsedTime();
43     const int n = mNumLaps;
44     ALOGD("StopWatch %s (us): %" PRId64 " ", mName, ns2us(elapsed));
45     for (int i=0 ; i<n ; i++) {
46         const nsecs_t soFar = mLaps[i].soFar;
47         const nsecs_t thisLap = mLaps[i].thisLap;
48         ALOGD(" [%d: %" PRId64 ", %" PRId64, i, ns2us(soFar), ns2us(thisLap));
49     }
50 }
51 
name() const52 const char* StopWatch::name() const
53 {
54     return mName;
55 }
56 
lap()57 nsecs_t StopWatch::lap()
58 {
59     nsecs_t elapsed = elapsedTime();
60     if (mNumLaps >= 8) {
61         elapsed = 0;
62     } else {
63         const int n = mNumLaps;
64         mLaps[n].soFar   = elapsed;
65         mLaps[n].thisLap = n ? (elapsed - mLaps[n-1].soFar) : elapsed;
66         mNumLaps = n+1;
67     }
68     return elapsed;
69 }
70 
elapsedTime() const71 nsecs_t StopWatch::elapsedTime() const
72 {
73     return systemTime(mClock) - mStartTime;
74 }
75 
reset()76 void StopWatch::reset()
77 {
78     mNumLaps = 0;
79     mStartTime = systemTime(mClock);
80 }
81 
82 
83 /*****************************************************************************/
84 
85 }; // namespace android
86 
87