diff options
author | Alex Ray | 2013-08-02 16:40:08 -0500 |
---|---|---|
committer | Alex Ray | 2013-08-02 16:40:08 -0500 |
commit | d98e07fdf9c338589f263c47ce5c844ed43efad5 (patch) | |
tree | d4ff9849df225df1e4c46386fdabe30407ba5513 /libutils/StopWatch.cpp | |
parent | be06210c508d5878dcc7d185e5613f4c7e38dfe8 (diff) | |
download | platform-system-core-d98e07fdf9c338589f263c47ce5c844ed43efad5.tar.gz platform-system-core-d98e07fdf9c338589f263c47ce5c844ed43efad5.tar.xz platform-system-core-d98e07fdf9c338589f263c47ce5c844ed43efad5.zip |
move libs/utils to libutils
Change-Id: I6cf4268599460791414882f91eeb88a992fbd29d
Diffstat (limited to 'libutils/StopWatch.cpp')
-rw-r--r-- | libutils/StopWatch.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/libutils/StopWatch.cpp b/libutils/StopWatch.cpp new file mode 100644 index 000000000..b1708d62b --- /dev/null +++ b/libutils/StopWatch.cpp | |||
@@ -0,0 +1,88 @@ | |||
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 | /* for PRId64 */ | ||
24 | #define __STDC_FORMAT_MACROS 1 | ||
25 | #include <inttypes.h> | ||
26 | |||
27 | #include <utils/Log.h> | ||
28 | #include <utils/Errors.h> | ||
29 | #include <utils/StopWatch.h> | ||
30 | |||
31 | /*****************************************************************************/ | ||
32 | |||
33 | namespace android { | ||
34 | |||
35 | |||
36 | StopWatch::StopWatch(const char *name, int clock, uint32_t flags) | ||
37 | : mName(name), mClock(clock), mFlags(flags) | ||
38 | { | ||
39 | reset(); | ||
40 | } | ||
41 | |||
42 | StopWatch::~StopWatch() | ||
43 | { | ||
44 | nsecs_t elapsed = elapsedTime(); | ||
45 | const int n = mNumLaps; | ||
46 | ALOGD("StopWatch %s (us): %" PRId64 " ", mName, ns2us(elapsed)); | ||
47 | for (int i=0 ; i<n ; i++) { | ||
48 | const nsecs_t soFar = mLaps[i].soFar; | ||
49 | const nsecs_t thisLap = mLaps[i].thisLap; | ||
50 | ALOGD(" [%d: %" PRId64 ", %" PRId64, i, ns2us(soFar), ns2us(thisLap)); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | const char* StopWatch::name() const | ||
55 | { | ||
56 | return mName; | ||
57 | } | ||
58 | |||
59 | nsecs_t StopWatch::lap() | ||
60 | { | ||
61 | nsecs_t elapsed = elapsedTime(); | ||
62 | if (mNumLaps >= 8) { | ||
63 | elapsed = 0; | ||
64 | } else { | ||
65 | const int n = mNumLaps; | ||
66 | mLaps[n].soFar = elapsed; | ||
67 | mLaps[n].thisLap = n ? (elapsed - mLaps[n-1].soFar) : elapsed; | ||
68 | mNumLaps = n+1; | ||
69 | } | ||
70 | return elapsed; | ||
71 | } | ||
72 | |||
73 | nsecs_t StopWatch::elapsedTime() const | ||
74 | { | ||
75 | return systemTime(mClock) - mStartTime; | ||
76 | } | ||
77 | |||
78 | void StopWatch::reset() | ||
79 | { | ||
80 | mNumLaps = 0; | ||
81 | mStartTime = systemTime(mClock); | ||
82 | } | ||
83 | |||
84 | |||
85 | /*****************************************************************************/ | ||
86 | |||
87 | }; // namespace android | ||
88 | |||