blob: 219c13c6779c7304ac929f6edf6a108bf3a977d7 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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 Agopian22dbf392017-02-28 15:06:51 -080019#include <utils/StopWatch.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080020
Andrew Hsieh24e57d52012-02-27 18:50:55 -080021/* for PRId64 */
Mark Salyzyn5bed8032014-04-30 11:10:46 -070022#ifndef __STDC_FORMAT_MACROS
Andrew Hsieh24e57d52012-02-27 18:50:55 -080023#define __STDC_FORMAT_MACROS 1
Mark Salyzyn5bed8032014-04-30 11:10:46 -070024#endif
Andrew Hsieh24e57d52012-02-27 18:50:55 -080025#include <inttypes.h>
26
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080027#include <utils/Log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080028
29/*****************************************************************************/
30
31namespace android {
32
33
34StopWatch::StopWatch(const char *name, int clock, uint32_t flags)
Jeff Brown66db6892010-04-22 18:58:52 -070035 : mName(name), mClock(clock), mFlags(flags)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080036{
Jeff Brown66db6892010-04-22 18:58:52 -070037 reset();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080038}
39
40StopWatch::~StopWatch()
41{
42 nsecs_t elapsed = elapsedTime();
43 const int n = mNumLaps;
Andrew Hsieh24e57d52012-02-27 18:50:55 -080044 ALOGD("StopWatch %s (us): %" PRId64 " ", mName, ns2us(elapsed));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080045 for (int i=0 ; i<n ; i++) {
46 const nsecs_t soFar = mLaps[i].soFar;
47 const nsecs_t thisLap = mLaps[i].thisLap;
Andrew Hsieh24e57d52012-02-27 18:50:55 -080048 ALOGD(" [%d: %" PRId64 ", %" PRId64, i, ns2us(soFar), ns2us(thisLap));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080049 }
50}
51
52const char* StopWatch::name() const
53{
54 return mName;
55}
56
57nsecs_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
71nsecs_t StopWatch::elapsedTime() const
72{
73 return systemTime(mClock) - mStartTime;
74}
75
Jeff Brown66db6892010-04-22 18:58:52 -070076void StopWatch::reset()
77{
78 mNumLaps = 0;
79 mStartTime = systemTime(mClock);
80}
81
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080082
83/*****************************************************************************/
84
85}; // namespace android
86