blob: a86f29394f722146f183be555c4b45bf92a39424 [file] [log] [blame]
scroggo@google.com9a412522012-09-07 15:21:18 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "TimerData.h"
10
11#include "BenchTimer.h"
12#include <limits>
13
14using namespace std;
15
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000016TimerData::TimerData(int maxNumTimings)
17: fMaxNumTimings(maxNumTimings)
18, fCurrTiming(0)
19, fWallTimes(maxNumTimings)
20, fTruncatedWallTimes(maxNumTimings)
21, fCpuTimes(maxNumTimings)
22, fTruncatedCpuTimes(maxNumTimings)
23, fGpuTimes(maxNumTimings){
scroggo@google.com9a412522012-09-07 15:21:18 +000024}
25
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000026bool TimerData::appendTimes(BenchTimer* timer) {
scroggo@google.com9a412522012-09-07 15:21:18 +000027 SkASSERT(timer != NULL);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000028 if (fCurrTiming >= fMaxNumTimings) {
29 return false;
scroggo@google.com9a412522012-09-07 15:21:18 +000030 }
scroggo@google.com9a412522012-09-07 15:21:18 +000031
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000032 fWallTimes[fCurrTiming] = timer->fWall;
33 fTruncatedWallTimes[fCurrTiming] = timer->fTruncatedWall;
34 fCpuTimes[fCurrTiming] = timer->fCpu;
35 fTruncatedCpuTimes[fCurrTiming] = timer->fTruncatedCpu;
36 fGpuTimes[fCurrTiming] = timer->fGpu;
scroggo@google.com9a412522012-09-07 15:21:18 +000037
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000038 ++fCurrTiming;
scroggo@google.com9a412522012-09-07 15:21:18 +000039
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000040 return true;
scroggo@google.com9a412522012-09-07 15:21:18 +000041}
42
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000043SkString TimerData::getResult(const char* doubleFormat,
44 Result result,
45 const char *configName,
46 uint32_t timerFlags,
47 int itersPerTiming) {
48 SkASSERT(itersPerTiming >= 1);
49
50 if (!fCurrTiming) {
51 return SkString("");
scroggo@google.com9a412522012-09-07 15:21:18 +000052 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000053
54 int numTimings = fCurrTiming;
55
56 SkString wallStr(" msecs = ");
57 SkString truncWallStr(" Wmsecs = ");
58 SkString cpuStr(" cmsecs = ");
59 SkString truncCpuStr(" Cmsecs = ");
60 SkString gpuStr(" gmsecs = ");
61
62 double wallMin = std::numeric_limits<double>::max();
63 double truncWallMin = std::numeric_limits<double>::max();
64 double cpuMin = std::numeric_limits<double>::max();
65 double truncCpuMin = std::numeric_limits<double>::max();
66 double gpuMin = std::numeric_limits<double>::max();
67
68 double wallSum = 0;
69 double truncWallSum = 0;
70 double cpuSum = 0;
71 double truncCpuSum = 0;
72 double gpuSum = 0;
73
74 for (int i = 0; i < numTimings; ++i) {
75 if (kPerIter_Result == result) {
robertphillips@google.combbd893d2013-12-17 16:32:51 +000076 wallStr.appendf(doubleFormat, fWallTimes[i] / itersPerTiming);
77 truncWallStr.appendf(doubleFormat, fTruncatedWallTimes[i] / itersPerTiming);
78 cpuStr.appendf(doubleFormat, fCpuTimes[i] / itersPerTiming);
79 truncCpuStr.appendf(doubleFormat, fTruncatedCpuTimes[i] / itersPerTiming);
80 gpuStr.appendf(doubleFormat, fGpuTimes[i] / itersPerTiming);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000081
82 if (i != numTimings - 1) {
83 static const char kSep[] = ", ";
84 wallStr.append(kSep);
85 truncWallStr.append(kSep);
86 cpuStr.append(kSep);
87 truncCpuStr.append(kSep);
88 gpuStr.append(kSep);
89 }
90 } else if (kMin_Result == result) {
91 wallMin = SkTMin(wallMin, fWallTimes[i]);
92 truncWallMin = SkTMin(truncWallMin, fTruncatedWallTimes[i]);
93 cpuMin = SkTMin(cpuMin, fCpuTimes[i]);
94 truncCpuMin = SkTMin(truncCpuMin, fTruncatedCpuTimes[i]);
95 gpuMin = SkTMin(gpuMin, fGpuTimes[i]);
96 } else {
97 SkASSERT(kAvg_Result == result);
98 wallSum += fWallTimes[i];
99 truncWallSum += fTruncatedWallTimes[i];
100 cpuSum += fCpuTimes[i];
101 truncCpuSum += fTruncatedCpuTimes[i];
102 }
103
104 // We always track the GPU sum because whether it is non-zero indicates if valid gpu times
105 // were recorded at all.
106 gpuSum += fGpuTimes[i];
107 }
108
109 if (kMin_Result == result) {
110 wallStr.appendf(doubleFormat, wallMin / itersPerTiming);
111 truncWallStr.appendf(doubleFormat, truncWallMin / itersPerTiming);
112 cpuStr.appendf(doubleFormat, cpuMin / itersPerTiming);
113 truncCpuStr.appendf(doubleFormat, truncCpuMin / itersPerTiming);
114 gpuStr.appendf(doubleFormat, gpuMin / itersPerTiming);
115 } else if (kAvg_Result == result) {
116 int divisor = numTimings * itersPerTiming;
117 wallStr.appendf(doubleFormat, wallSum / divisor);
118 truncWallStr.appendf(doubleFormat, truncWallSum / divisor);
119 cpuStr.appendf(doubleFormat, cpuSum / divisor);
120 truncCpuStr.appendf(doubleFormat, truncCpuSum / divisor);
121 gpuStr.appendf(doubleFormat, gpuSum / divisor);
122 }
123
scroggo@google.com9a412522012-09-07 15:21:18 +0000124 SkString str;
125 str.printf(" %4s:", configName);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000126 if (timerFlags & kWall_Flag) {
127 str += wallStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000128 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000129 if (timerFlags & kTruncatedWall_Flag) {
130 str += truncWallStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000131 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000132 if (timerFlags & kCpu_Flag) {
133 str += cpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000134 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000135 if (timerFlags & kTruncatedCpu_Flag) {
136 str += truncCpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000137 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000138 if ((timerFlags & kGpu_Flag) && gpuSum > 0) {
139 str += gpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000140 }
141 return str;
142}