2 * Copyright (C) 2009 The Android Open Source Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "stopwatch.h"
35 #define SNPRINTF_OR_RETURN(str, size, format, ...) { \
36 int len = snprintf((str), (size), (format), ## __VA_ARGS__); \
37 if (len < 0) return; \
38 if (len > static_cast<int>(size)) { \
39 fprintf(stderr, "Not enough space\n"); \
42 (size) -= len; (str) += len; \
47 const bool kVerbose = false;
48 bool printRaw = false;
51 namespace android_test {
53 StopWatch::StopWatch(const char *name, size_t capacity)
54 : mName(strdup(name)), mNum(0), mData(NULL), mDataLen(0), mCapacity(capacity * 2),
55 mSizeKbytes(0), mAlreadyPrinted(false), mPrintRaw(false),
57 mMinDuration(0.0), mMinIdx(0),
58 mMaxDuration(0.0), mMaxIdx(0),
59 mDeltas(NULL), mUsed(false)
63 mData = (Measurement *) malloc(mCapacity * sizeof(Measurement));
66 StopWatch::~StopWatch()
68 if (mUsed && !mAlreadyPrinted)
70 fprintf(stderr, "Discarding data for %s\n", mName);
77 void StopWatch::start()
80 clock_gettime(CLOCK_MONOTONIC, &mData[mDataLen].mTime);
81 mData[mDataLen].mIsStart = true;
84 mStart = mData[mDataLen].mTime; // mDataLen should be 0
91 void StopWatch::stop()
94 clock_gettime(CLOCK_MONOTONIC, &mData[mDataLen].mTime);
95 mData[mDataLen].mIsStart = false;
99 void StopWatch::setPrintRawMode(bool raw)
105 void StopWatch::sprint(char **str, size_t *size)
107 if (kVerbose) fprintf(stderr, "printing\n");
108 mAlreadyPrinted = true;
113 if (mDataLen > 0 && mData[mDataLen - 1].mIsStart)
117 if (kVerbose) SNPRINTF_OR_RETURN(*str, *size, "# Got %d samples for %s\n", mDataLen, mName);
120 SNPRINTF_OR_RETURN(*str, *size, "# StopWatch %s total/cumulative duration %f. Samples: %d\n",
121 mName, mDuration, mNum);
122 printThroughput(str, size);
123 printAverageMinMax(str, size);
127 // print comment header and summary values.
129 SNPRINTF_OR_RETURN(*str, *size, "# Name Iterations Duration Min MinIdx Max MaxIdx SizeMbytes\n");
130 SNPRINTF_OR_RETURN(*str, *size, "%s %d %f %f %d %f %d %d\n", mName, mNum, mDuration,
131 mMinDuration, mMinIdx, mMaxDuration, mMaxIdx, mSizeKbytes);
132 // print each duration sample
133 for (size_t i = 0; i < mDataLen / 2; ++i)
135 long second = mData[i * 2].mTime.tv_sec - mStart.tv_sec;
136 long nano = mData[i * 2].mTime.tv_nsec - mStart.tv_nsec;
138 SNPRINTF_OR_RETURN(*str, *size, "%f %f\n", double(second) + double(nano) / 1.0e9, mDeltas[i]);
144 // Normally we should have enough capacity but if we have to
145 // reallocate the measurement buffer (e.g start and stop called more
146 // than once in an iteration) we let the user know. She should provide
147 // a capacity when building the StopWatch.
148 void StopWatch::checkCapacity()
150 if (mDataLen >= mCapacity)
153 fprintf(stderr, "# Increased capacity to %d for %s. Measurement affected.\n",
155 mData = (Measurement *)realloc(mData, mCapacity * sizeof(Measurement));
160 // Go over all the samples and compute the diffs between a start and
161 // stop pair. The diff is accumulated in mDuration and inserted in
163 // The min and max values for a diff are also tracked.
164 void StopWatch::processSamples()
166 if (kVerbose) fprintf(stderr, "processing samples\n");
167 mDeltas= new double[mDataLen / 2];
169 for (size_t i = 0; i < mDataLen; i += 2) // even: start odd: stop
171 long second = mData[i + 1].mTime.tv_sec - mData[i].mTime.tv_sec;
172 long nano = mData[i + 1].mTime.tv_nsec - mData[i].mTime.tv_nsec;
174 mDeltas[i / 2] = double(second) + double(nano) / 1.0e9;
177 for (size_t i = 0; i < mDataLen / 2; ++i)
181 mMinDuration = mMaxDuration = mDeltas[i];
185 if (mMaxDuration < mDeltas[i])
187 mMaxDuration = mDeltas[i];
190 if (mMinDuration > mDeltas[i])
192 mMinDuration = mDeltas[i];
196 mDuration += mDeltas[i];
200 double StopWatch::timespecToDouble(const struct timespec& time)
202 double val = double(time.tv_nsec) / 1.0e9 + double(time.tv_sec);
203 return val < 0.0 ? -val : val; // sometimes 0.00 is -0.00
207 // If we have only 2 values, don't bother printing anything.
208 void StopWatch::printAverageMinMax(char **str, size_t *size)
210 if (mDataLen > 2) // if there is only one sample, avg, min, max are trivial.
212 SNPRINTF_OR_RETURN(*str, *size, "# Average %s duration %f s/op\n", mName, mDuration / mNum);
213 SNPRINTF_OR_RETURN(*str, *size, "# Min %s duration %f [%d]\n", mName, mMinDuration, mMinIdx);
214 SNPRINTF_OR_RETURN(*str, *size, "# Max %s duration %f [%d]\n", mName, mMaxDuration, mMaxIdx);
218 void StopWatch::printThroughput(char **str, size_t *size)
220 if (0 != mSizeKbytes)
222 SNPRINTF_OR_RETURN(*str, *size, "# Size: %d Kbytes Total: %d\n", mSizeKbytes, mNum);
223 SNPRINTF_OR_RETURN(*str, *size, "# Speed %f Kbyte/s\n", double(mSizeKbytes) * mNum / mDuration);
226 } // namespace android_test