blob: 21529bc2e068c10350a5999b41afd7480480e3e0 [file] [log] [blame]
scroggo@google.com9a412522012-09-07 15:21:18 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
scroggo@google.com9a412522012-09-07 15:21:18 +00007#include "TimerData.h"
8
mtklein9ac68ee2014-06-20 11:29:20 -07009#include "Timer.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000010#include <limits>
11
mtklein9e64b782014-06-20 10:43:07 -070012TimerData::TimerData(int maxNumTimings)
mtklein9ac68ee2014-06-20 11:29:20 -070013 : fMaxNumTimings(maxNumTimings)
14 , fCurrTiming(0)
15 , fWallTimes(maxNumTimings)
16 , fTruncatedWallTimes(maxNumTimings)
17 , fCpuTimes(maxNumTimings)
18 , fTruncatedCpuTimes(maxNumTimings)
19 , fGpuTimes(maxNumTimings) {}
mtklein9e64b782014-06-20 10:43:07 -070020
mtklein9ac68ee2014-06-20 11:29:20 -070021bool TimerData::appendTimes(Timer* timer) {
scroggo@google.com9a412522012-09-07 15:21:18 +000022 SkASSERT(timer != NULL);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000023 if (fCurrTiming >= fMaxNumTimings) {
24 return false;
scroggo@google.com9a412522012-09-07 15:21:18 +000025 }
scroggo@google.com9a412522012-09-07 15:21:18 +000026
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000027 fWallTimes[fCurrTiming] = timer->fWall;
28 fTruncatedWallTimes[fCurrTiming] = timer->fTruncatedWall;
29 fCpuTimes[fCurrTiming] = timer->fCpu;
30 fTruncatedCpuTimes[fCurrTiming] = timer->fTruncatedCpu;
31 fGpuTimes[fCurrTiming] = timer->fGpu;
scroggo@google.com9a412522012-09-07 15:21:18 +000032
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000033 ++fCurrTiming;
scroggo@google.com9a412522012-09-07 15:21:18 +000034
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000035 return true;
scroggo@google.com9a412522012-09-07 15:21:18 +000036}
37
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000038SkString TimerData::getResult(const char* doubleFormat,
39 Result result,
40 const char *configName,
41 uint32_t timerFlags,
42 int itersPerTiming) {
43 SkASSERT(itersPerTiming >= 1);
44
45 if (!fCurrTiming) {
46 return SkString("");
scroggo@google.com9a412522012-09-07 15:21:18 +000047 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000048
49 int numTimings = fCurrTiming;
50
51 SkString wallStr(" msecs = ");
52 SkString truncWallStr(" Wmsecs = ");
53 SkString cpuStr(" cmsecs = ");
54 SkString truncCpuStr(" Cmsecs = ");
55 SkString gpuStr(" gmsecs = ");
56
57 double wallMin = std::numeric_limits<double>::max();
58 double truncWallMin = std::numeric_limits<double>::max();
59 double cpuMin = std::numeric_limits<double>::max();
60 double truncCpuMin = std::numeric_limits<double>::max();
61 double gpuMin = std::numeric_limits<double>::max();
62
63 double wallSum = 0;
64 double truncWallSum = 0;
65 double cpuSum = 0;
66 double truncCpuSum = 0;
67 double gpuSum = 0;
68
69 for (int i = 0; i < numTimings; ++i) {
70 if (kPerIter_Result == result) {
robertphillips@google.combbd893d2013-12-17 16:32:51 +000071 wallStr.appendf(doubleFormat, fWallTimes[i] / itersPerTiming);
72 truncWallStr.appendf(doubleFormat, fTruncatedWallTimes[i] / itersPerTiming);
73 cpuStr.appendf(doubleFormat, fCpuTimes[i] / itersPerTiming);
74 truncCpuStr.appendf(doubleFormat, fTruncatedCpuTimes[i] / itersPerTiming);
75 gpuStr.appendf(doubleFormat, fGpuTimes[i] / itersPerTiming);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +000076
77 if (i != numTimings - 1) {
78 static const char kSep[] = ", ";
79 wallStr.append(kSep);
80 truncWallStr.append(kSep);
81 cpuStr.append(kSep);
82 truncCpuStr.append(kSep);
83 gpuStr.append(kSep);
84 }
85 } else if (kMin_Result == result) {
86 wallMin = SkTMin(wallMin, fWallTimes[i]);
87 truncWallMin = SkTMin(truncWallMin, fTruncatedWallTimes[i]);
88 cpuMin = SkTMin(cpuMin, fCpuTimes[i]);
89 truncCpuMin = SkTMin(truncCpuMin, fTruncatedCpuTimes[i]);
90 gpuMin = SkTMin(gpuMin, fGpuTimes[i]);
91 } else {
92 SkASSERT(kAvg_Result == result);
93 wallSum += fWallTimes[i];
94 truncWallSum += fTruncatedWallTimes[i];
95 cpuSum += fCpuTimes[i];
96 truncCpuSum += fTruncatedCpuTimes[i];
97 }
98
99 // We always track the GPU sum because whether it is non-zero indicates if valid gpu times
100 // were recorded at all.
101 gpuSum += fGpuTimes[i];
102 }
103
104 if (kMin_Result == result) {
105 wallStr.appendf(doubleFormat, wallMin / itersPerTiming);
106 truncWallStr.appendf(doubleFormat, truncWallMin / itersPerTiming);
107 cpuStr.appendf(doubleFormat, cpuMin / itersPerTiming);
108 truncCpuStr.appendf(doubleFormat, truncCpuMin / itersPerTiming);
109 gpuStr.appendf(doubleFormat, gpuMin / itersPerTiming);
110 } else if (kAvg_Result == result) {
111 int divisor = numTimings * itersPerTiming;
112 wallStr.appendf(doubleFormat, wallSum / divisor);
113 truncWallStr.appendf(doubleFormat, truncWallSum / divisor);
114 cpuStr.appendf(doubleFormat, cpuSum / divisor);
115 truncCpuStr.appendf(doubleFormat, truncCpuSum / divisor);
116 gpuStr.appendf(doubleFormat, gpuSum / divisor);
117 }
118
scroggo@google.com9a412522012-09-07 15:21:18 +0000119 SkString str;
120 str.printf(" %4s:", configName);
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000121 if (timerFlags & kWall_Flag) {
122 str += wallStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000123 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000124 if (timerFlags & kTruncatedWall_Flag) {
125 str += truncWallStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000126 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000127 if (timerFlags & kCpu_Flag) {
128 str += cpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000129 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000130 if (timerFlags & kTruncatedCpu_Flag) {
131 str += truncCpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000132 }
commit-bot@chromium.org55fd6122013-07-31 20:00:56 +0000133 if ((timerFlags & kGpu_Flag) && gpuSum > 0) {
134 str += gpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000135 }
136 return str;
137}
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000138
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000139Json::Value TimerData::getJSON(uint32_t timerFlags,
commit-bot@chromium.org37c772a2014-05-29 17:10:24 +0000140 Result result,
141 int itersPerTiming) {
142 SkASSERT(itersPerTiming >= 1);
143 Json::Value dataNode;
144 Json::Value wallNode, truncWall, cpuNode, truncCpu, gpuNode;
145 if (!fCurrTiming) {
146 return dataNode;
147 }
148
149 int numTimings = fCurrTiming;
150
151 double wallMin = std::numeric_limits<double>::max();
152 double truncWallMin = std::numeric_limits<double>::max();
153 double cpuMin = std::numeric_limits<double>::max();
154 double truncCpuMin = std::numeric_limits<double>::max();
155 double gpuMin = std::numeric_limits<double>::max();
156
157 double wallSum = 0;
158 double truncWallSum = 0;
159 double cpuSum = 0;
160 double truncCpuSum = 0;
161 double gpuSum = 0;
162
163 for (int i = 0; i < numTimings; ++i) {
164 if (kPerIter_Result == result) {
165 wallNode.append(fWallTimes[i] / itersPerTiming);
166 truncWall.append(fTruncatedWallTimes[i] / itersPerTiming);
167 cpuNode.append(fCpuTimes[i] / itersPerTiming);
168 truncCpu.append(fTruncatedCpuTimes[i] / itersPerTiming);
169 gpuNode.append(fGpuTimes[i] / itersPerTiming);
170 } else if (kMin_Result == result) {
171 wallMin = SkTMin(wallMin, fWallTimes[i]);
172 truncWallMin = SkTMin(truncWallMin, fTruncatedWallTimes[i]);
173 cpuMin = SkTMin(cpuMin, fCpuTimes[i]);
174 truncCpuMin = SkTMin(truncCpuMin, fTruncatedCpuTimes[i]);
175 gpuMin = SkTMin(gpuMin, fGpuTimes[i]);
176 } else {
177 SkASSERT(kAvg_Result == result);
178 wallSum += fWallTimes[i];
179 truncWallSum += fTruncatedWallTimes[i];
180 cpuSum += fCpuTimes[i];
181 truncCpuSum += fTruncatedCpuTimes[i];
182 }
183
184 // We always track the GPU sum because whether it is non-zero indicates if valid gpu times
185 // were recorded at all.
186 gpuSum += fGpuTimes[i];
187 }
188
189 if (kMin_Result == result) {
190 wallNode.append(wallMin / itersPerTiming);
191 truncWall.append(truncWallMin / itersPerTiming);
192 cpuNode.append(cpuMin / itersPerTiming);
193 truncCpu.append(truncCpuMin / itersPerTiming);
194 gpuNode.append(gpuMin / itersPerTiming);
195 } else if (kAvg_Result == result) {
196 int divisor = numTimings * itersPerTiming;
197 wallNode.append(wallSum / divisor);
198 truncWall.append(truncWallSum / divisor);
199 cpuNode.append(cpuSum / divisor);
200 truncCpu.append(truncCpuSum / divisor);
201 gpuNode.append(gpuSum / divisor);
202 }
203
204 if (timerFlags & kWall_Flag) {
205 dataNode["wall"] = wallNode;
206 }
207 if (timerFlags & kTruncatedWall_Flag) {
208 dataNode["truncWall"] = truncWall;
209 }
210 if (timerFlags & kCpu_Flag) {
211 dataNode["cpu"] = cpuNode;
212 }
213 if (timerFlags & kTruncatedCpu_Flag) {
214 dataNode["trucCpu"] = truncCpu;
215 }
216 if ((timerFlags & kGpu_Flag) && gpuSum > 0) {
217 dataNode["gpu"] = gpuNode;
218 }
219 return dataNode;
220}