blob: d3dbf285247ad42211e246c4649a2b64cfcf78ee [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 */
scroggo@google.com9a412522012-09-07 15:21:18 +00008#include "TimerData.h"
9
10#include "BenchTimer.h"
11#include <limits>
12
13using namespace std;
14
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000015TimerData::TimerData(int maxNumTimings)
16: fMaxNumTimings(maxNumTimings)
17, fCurrTiming(0)
18, fWallTimes(maxNumTimings)
19, fTruncatedWallTimes(maxNumTimings)
20, fCpuTimes(maxNumTimings)
21, fTruncatedCpuTimes(maxNumTimings)
22, fGpuTimes(maxNumTimings){
scroggo@google.com9a412522012-09-07 15:21:18 +000023}
24
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000025bool TimerData::appendTimes(BenchTimer* timer) {
scroggo@google.com9a412522012-09-07 15:21:18 +000026 SkASSERT(timer != NULL);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000027 if (fCurrTiming >= fMaxNumTimings) {
28 return false;
scroggo@google.com9a412522012-09-07 15:21:18 +000029 }
scroggo@google.com9a412522012-09-07 15:21:18 +000030
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000031 fWallTimes[fCurrTiming] = timer->fWall;
32 fTruncatedWallTimes[fCurrTiming] = timer->fTruncatedWall;
33 fCpuTimes[fCurrTiming] = timer->fCpu;
34 fTruncatedCpuTimes[fCurrTiming] = timer->fTruncatedCpu;
35 fGpuTimes[fCurrTiming] = timer->fGpu;
scroggo@google.com9a412522012-09-07 15:21:18 +000036
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000037 ++fCurrTiming;
scroggo@google.com9a412522012-09-07 15:21:18 +000038
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000039 return true;
scroggo@google.com9a412522012-09-07 15:21:18 +000040}
41
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000042SkString TimerData::getResult(const char* doubleFormat,
43 Result result,
44 const char *configName,
45 uint32_t timerFlags,
46 int itersPerTiming) {
47 SkASSERT(itersPerTiming >= 1);
48
49 if (!fCurrTiming) {
50 return SkString("");
scroggo@google.com9a412522012-09-07 15:21:18 +000051 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000052
53 int numTimings = fCurrTiming;
54
55 SkString wallStr(" msecs = ");
56 SkString truncWallStr(" Wmsecs = ");
57 SkString cpuStr(" cmsecs = ");
58 SkString truncCpuStr(" Cmsecs = ");
59 SkString gpuStr(" gmsecs = ");
60
61 double wallMin = std::numeric_limits<double>::max();
62 double truncWallMin = std::numeric_limits<double>::max();
63 double cpuMin = std::numeric_limits<double>::max();
64 double truncCpuMin = std::numeric_limits<double>::max();
65 double gpuMin = std::numeric_limits<double>::max();
66
67 double wallSum = 0;
68 double truncWallSum = 0;
69 double cpuSum = 0;
70 double truncCpuSum = 0;
71 double gpuSum = 0;
72
73 for (int i = 0; i < numTimings; ++i) {
74 if (kPerIter_Result == result) {
robertphillips@google.combbd893d2013-12-17 16:32:51 +000075 wallStr.appendf(doubleFormat, fWallTimes[i] / itersPerTiming);
76 truncWallStr.appendf(doubleFormat, fTruncatedWallTimes[i] / itersPerTiming);
77 cpuStr.appendf(doubleFormat, fCpuTimes[i] / itersPerTiming);
78 truncCpuStr.appendf(doubleFormat, fTruncatedCpuTimes[i] / itersPerTiming);
79 gpuStr.appendf(doubleFormat, fGpuTimes[i] / itersPerTiming);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000080
81 if (i != numTimings - 1) {
82 static const char kSep[] = ", ";
83 wallStr.append(kSep);
84 truncWallStr.append(kSep);
85 cpuStr.append(kSep);
86 truncCpuStr.append(kSep);
87 gpuStr.append(kSep);
88 }
89 } else if (kMin_Result == result) {
90 wallMin = SkTMin(wallMin, fWallTimes[i]);
91 truncWallMin = SkTMin(truncWallMin, fTruncatedWallTimes[i]);
92 cpuMin = SkTMin(cpuMin, fCpuTimes[i]);
93 truncCpuMin = SkTMin(truncCpuMin, fTruncatedCpuTimes[i]);
94 gpuMin = SkTMin(gpuMin, fGpuTimes[i]);
95 } else {
96 SkASSERT(kAvg_Result == result);
97 wallSum += fWallTimes[i];
98 truncWallSum += fTruncatedWallTimes[i];
99 cpuSum += fCpuTimes[i];
100 truncCpuSum += fTruncatedCpuTimes[i];
101 }
102
103 // We always track the GPU sum because whether it is non-zero indicates if valid gpu times
104 // were recorded at all.
105 gpuSum += fGpuTimes[i];
106 }
107
108 if (kMin_Result == result) {
109 wallStr.appendf(doubleFormat, wallMin / itersPerTiming);
110 truncWallStr.appendf(doubleFormat, truncWallMin / itersPerTiming);
111 cpuStr.appendf(doubleFormat, cpuMin / itersPerTiming);
112 truncCpuStr.appendf(doubleFormat, truncCpuMin / itersPerTiming);
113 gpuStr.appendf(doubleFormat, gpuMin / itersPerTiming);
114 } else if (kAvg_Result == result) {
115 int divisor = numTimings * itersPerTiming;
116 wallStr.appendf(doubleFormat, wallSum / divisor);
117 truncWallStr.appendf(doubleFormat, truncWallSum / divisor);
118 cpuStr.appendf(doubleFormat, cpuSum / divisor);
119 truncCpuStr.appendf(doubleFormat, truncCpuSum / divisor);
120 gpuStr.appendf(doubleFormat, gpuSum / divisor);
121 }
122
scroggo@google.com9a412522012-09-07 15:21:18 +0000123 SkString str;
124 str.printf(" %4s:", configName);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000125 if (timerFlags & kWall_Flag) {
126 str += wallStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000127 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000128 if (timerFlags & kTruncatedWall_Flag) {
129 str += truncWallStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000130 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000131 if (timerFlags & kCpu_Flag) {
132 str += cpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000133 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000134 if (timerFlags & kTruncatedCpu_Flag) {
135 str += truncCpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000136 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000137 if ((timerFlags & kGpu_Flag) && gpuSum > 0) {
138 str += gpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000139 }
140 return str;
141}
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000142
143#ifdef SK_BUILD_JSON_WRITER
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000144Json::Value TimerData::getJSON(uint32_t timerFlags,
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000145 Result result,
146 int itersPerTiming) {
147 SkASSERT(itersPerTiming >= 1);
148 Json::Value dataNode;
149 Json::Value wallNode, truncWall, cpuNode, truncCpu, gpuNode;
150 if (!fCurrTiming) {
151 return dataNode;
152 }
153
154 int numTimings = fCurrTiming;
155
156 double wallMin = std::numeric_limits<double>::max();
157 double truncWallMin = std::numeric_limits<double>::max();
158 double cpuMin = std::numeric_limits<double>::max();
159 double truncCpuMin = std::numeric_limits<double>::max();
160 double gpuMin = std::numeric_limits<double>::max();
161
162 double wallSum = 0;
163 double truncWallSum = 0;
164 double cpuSum = 0;
165 double truncCpuSum = 0;
166 double gpuSum = 0;
167
168 for (int i = 0; i < numTimings; ++i) {
169 if (kPerIter_Result == result) {
170 wallNode.append(fWallTimes[i] / itersPerTiming);
171 truncWall.append(fTruncatedWallTimes[i] / itersPerTiming);
172 cpuNode.append(fCpuTimes[i] / itersPerTiming);
173 truncCpu.append(fTruncatedCpuTimes[i] / itersPerTiming);
174 gpuNode.append(fGpuTimes[i] / itersPerTiming);
175 } else if (kMin_Result == result) {
176 wallMin = SkTMin(wallMin, fWallTimes[i]);
177 truncWallMin = SkTMin(truncWallMin, fTruncatedWallTimes[i]);
178 cpuMin = SkTMin(cpuMin, fCpuTimes[i]);
179 truncCpuMin = SkTMin(truncCpuMin, fTruncatedCpuTimes[i]);
180 gpuMin = SkTMin(gpuMin, fGpuTimes[i]);
181 } else {
182 SkASSERT(kAvg_Result == result);
183 wallSum += fWallTimes[i];
184 truncWallSum += fTruncatedWallTimes[i];
185 cpuSum += fCpuTimes[i];
186 truncCpuSum += fTruncatedCpuTimes[i];
187 }
188
189 // We always track the GPU sum because whether it is non-zero indicates if valid gpu times
190 // were recorded at all.
191 gpuSum += fGpuTimes[i];
192 }
193
194 if (kMin_Result == result) {
195 wallNode.append(wallMin / itersPerTiming);
196 truncWall.append(truncWallMin / itersPerTiming);
197 cpuNode.append(cpuMin / itersPerTiming);
198 truncCpu.append(truncCpuMin / itersPerTiming);
199 gpuNode.append(gpuMin / itersPerTiming);
200 } else if (kAvg_Result == result) {
201 int divisor = numTimings * itersPerTiming;
202 wallNode.append(wallSum / divisor);
203 truncWall.append(truncWallSum / divisor);
204 cpuNode.append(cpuSum / divisor);
205 truncCpu.append(truncCpuSum / divisor);
206 gpuNode.append(gpuSum / divisor);
207 }
208
209 if (timerFlags & kWall_Flag) {
210 dataNode["wall"] = wallNode;
211 }
212 if (timerFlags & kTruncatedWall_Flag) {
213 dataNode["truncWall"] = truncWall;
214 }
215 if (timerFlags & kCpu_Flag) {
216 dataNode["cpu"] = cpuNode;
217 }
218 if (timerFlags & kTruncatedCpu_Flag) {
219 dataNode["trucCpu"] = truncCpu;
220 }
221 if ((timerFlags & kGpu_Flag) && gpuSum > 0) {
222 dataNode["gpu"] = gpuNode;
223 }
224 return dataNode;
225}
226#endif // SK_BUILD_JSON_WRITER