blob: 7b7cdbbeb76abf92ac5f9b7a6512f607ace5ee5c [file] [log] [blame]
joshualitt189aef72015-09-08 07:08:11 -07001/*
2 * Copyright 2015 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 *
7 */
8
9#include "VisualLightweightBenchModule.h"
10
11#include "ProcStats.h"
12#include "SkApplication.h"
13#include "SkCanvas.h"
14#include "SkCommandLineFlags.h"
15#include "SkForceLinking.h"
16#include "SkGraphics.h"
17#include "SkGr.h"
18#include "SkImageDecoder.h"
19#include "SkOSFile.h"
20#include "SkStream.h"
21#include "Stats.h"
22#include "gl/GrGLInterface.h"
23
24__SK_FORCE_IMAGE_DECODER_LINKING;
25
26// Between samples we reset context
27// Between frames we swap buffers
28
29DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
30DEFINE_int32(samples, 10, "Number of times to time each skp.");
31DEFINE_int32(frames, 5, "Number of frames of each skp to render per sample.");
32DEFINE_double(loopMs, 5, "Target loop time in millseconds.");
33DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
joshualitt189aef72015-09-08 07:08:11 -070034DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtkleinca6f43b2015-09-15 13:29:20 -070035DEFINE_string(key, "",
36 "Space-separated key/value pairs to add to JSON identifying this builder.");
joshualitt189aef72015-09-08 07:08:11 -070037DEFINE_string(properties, "",
38 "Space-separated key/value pairs to add to JSON identifying this run.");
39
40static SkString humanize(double ms) {
41 if (FLAGS_verbose) {
42 return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
43 }
44 return HumanizeMs(ms);
45}
46
47#define HUMANIZE(time) humanize(time).c_str()
48
49VisualLightweightBenchModule::VisualLightweightBenchModule(VisualBench* owner)
50 : fCurrentSample(0)
51 , fCurrentFrame(0)
52 , fLoops(1)
53 , fState(kPreWarmLoops_State)
54 , fBenchmark(nullptr)
55 , fOwner(SkRef(owner))
56 , fResults(new ResultsWriter) {
57 fBenchmarkStream.reset(new VisualBenchmarkStream);
58
59 // Print header
60 SkDebugf("curr/maxrss\tloops\tflushes\tmin\tmedian\tmean\tmax\tstddev\tbench\n");
61
62 // setup json logging if required
63 if (!FLAGS_outResultsFile.isEmpty()) {
64 fResults.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
65 }
66
mtkleinca6f43b2015-09-15 13:29:20 -070067 if (1 == FLAGS_key.count() % 2) {
68 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
69 } else {
70 for (int i = 1; i < FLAGS_key.count(); i += 2) {
71 fResults->key(FLAGS_key[i - 1], FLAGS_key[i]);
72 }
73 }
74
joshualitt189aef72015-09-08 07:08:11 -070075 if (1 == FLAGS_properties.count() % 2) {
76 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
77 } else {
78 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
79 fResults->property(FLAGS_properties[i - 1], FLAGS_properties[i]);
80 }
81 }
82}
83
84inline void VisualLightweightBenchModule::renderFrame(SkCanvas* canvas) {
85 fBenchmark->draw(fLoops, canvas);
86 canvas->flush();
87 fOwner->present();
88}
89
90void VisualLightweightBenchModule::printStats() {
91 const SkTArray<double>& measurements = fRecords.back().fMeasurements;
92 const char* shortName = fBenchmark->getUniqueName();
93
94 // update log
95 // Note: We currently log only the minimum. It would be interesting to log more information
96 SkString configName;
97 if (FLAGS_msaa > 0) {
98 configName.appendf("msaa_%d", FLAGS_msaa);
99 } else {
100 configName.appendf("gpu");
101 }
102 fResults->config(configName.c_str());
103 fResults->configOption("name", fBenchmark->getUniqueName());
104 SkASSERT(measurements.count());
105 Stats stats(measurements);
106 fResults->metric("min_ms", stats.min);
107
108 // Print output
109 if (FLAGS_verbose) {
110 for (int i = 0; i < measurements.count(); i++) {
111 SkDebugf("%s ", HUMANIZE(measurements[i]));
112 }
113 SkDebugf("%s\n", shortName);
114 } else {
115 const double stdDevPercent = 100 * sqrt(stats.var) / stats.mean;
116 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\n",
117 sk_tools::getCurrResidentSetSizeMB(),
118 sk_tools::getMaxResidentSetSizeMB(),
119 fLoops,
120 HUMANIZE(stats.min),
121 HUMANIZE(stats.median),
122 HUMANIZE(stats.mean),
123 HUMANIZE(stats.max),
124 stdDevPercent,
125 shortName);
126 }
127}
128
129bool VisualLightweightBenchModule::advanceRecordIfNecessary(SkCanvas* canvas) {
130 if (fBenchmark) {
131 return true;
132 }
133
134 fBenchmark.reset(fBenchmarkStream->next());
135 if (!fBenchmark) {
136 return false;
137 }
138
jvanverthf5d1b2d2015-09-15 07:40:56 -0700139 fOwner->clear(canvas, SK_ColorWHITE, 2);
140
141
joshualitt189aef72015-09-08 07:08:11 -0700142 fBenchmark->preDraw();
143 fRecords.push_back();
144
145 // Log bench name
146 fResults->bench(fBenchmark->getUniqueName(), fBenchmark->getSize().fX,
147 fBenchmark->getSize().fY);
148 return true;
149}
150
151inline void VisualLightweightBenchModule::nextState(State nextState) {
152 fState = nextState;
153}
154
155void VisualLightweightBenchModule::perCanvasPreDraw(SkCanvas* canvas, State nextState) {
156 fBenchmark->perCanvasPreDraw(canvas);
157 fCurrentFrame = 0;
158 this->nextState(nextState);
159}
160
161void VisualLightweightBenchModule::preWarm(State nextState) {
162 if (fCurrentFrame >= FLAGS_gpuFrameLag) {
163 // we currently time across all frames to make sure we capture all GPU work
164 this->nextState(nextState);
165 fCurrentFrame = 0;
166 fTimer.start();
167 } else {
168 fCurrentFrame++;
169 }
170}
171
172void VisualLightweightBenchModule::draw(SkCanvas* canvas) {
173 if (!this->advanceRecordIfNecessary(canvas)) {
174 SkDebugf("Exiting VisualBench successfully\n");
175 fOwner->closeWindow();
176 return;
177 }
178 this->renderFrame(canvas);
179 switch (fState) {
180 case kPreWarmLoopsPerCanvasPreDraw_State: {
181 this->perCanvasPreDraw(canvas, kPreWarmLoops_State);
182 break;
183 }
184 case kPreWarmLoops_State: {
185 this->preWarm(kTuneLoops_State);
186 break;
187 }
188 case kTuneLoops_State: {
189 this->tuneLoops();
190 break;
191 }
192 case kPreWarmTimingPerCanvasPreDraw_State: {
193 this->perCanvasPreDraw(canvas, kPreWarmTiming_State);
194 break;
195 }
196 case kPreWarmTiming_State: {
197 this->preWarm(kTiming_State);
198 break;
199 }
200 case kTiming_State: {
201 this->timing(canvas);
202 break;
203 }
204 }
205}
206
207inline double VisualLightweightBenchModule::elapsed() {
208 fTimer.end();
209 return fTimer.fWall;
210}
211
212void VisualLightweightBenchModule::resetTimingState() {
213 fCurrentFrame = 0;
214 fTimer = WallTimer();
215 fOwner->reset();
216}
217
218void VisualLightweightBenchModule::scaleLoops(double elapsedMs) {
219 // Scale back the number of loops
220 fLoops = (int)ceil(fLoops * FLAGS_loopMs / elapsedMs);
221}
222
223inline void VisualLightweightBenchModule::tuneLoops() {
224 if (1 << 30 == fLoops) {
225 // We're about to wrap. Something's wrong with the bench.
226 SkDebugf("InnerLoops wrapped\n");
227 fLoops = 0;
228 } else {
229 double elapsedMs = this->elapsed();
230 if (elapsedMs > FLAGS_loopMs) {
231 this->scaleLoops(elapsedMs);
232 this->nextState(kPreWarmTimingPerCanvasPreDraw_State);
233 } else {
234 fLoops *= 2;
235 this->nextState(kPreWarmLoops_State);
236 }
237 this->resetTimingState();
238 }
239}
240
241void VisualLightweightBenchModule::recordMeasurement() {
242 double measurement = this->elapsed() / (FLAGS_frames * fLoops);
243 fRecords.back().fMeasurements.push_back(measurement);
244}
245
246void VisualLightweightBenchModule::postDraw(SkCanvas* canvas) {
247 fBenchmark->perCanvasPostDraw(canvas);
248 fBenchmark.reset(nullptr);
249 fCurrentSample = 0;
250 fLoops = 1;
251}
252
253inline void VisualLightweightBenchModule::timing(SkCanvas* canvas) {
254 if (fCurrentFrame >= FLAGS_frames) {
255 this->recordMeasurement();
256 if (fCurrentSample++ >= FLAGS_samples) {
257 this->printStats();
258 this->postDraw(canvas);
259 this->nextState(kPreWarmLoopsPerCanvasPreDraw_State);
260 } else {
261 this->nextState(kPreWarmTimingPerCanvasPreDraw_State);
262 }
263 this->resetTimingState();
264 } else {
265 fCurrentFrame++;
266 }
267}
jvanverthf5d1b2d2015-09-15 07:40:56 -0700268
269bool VisualLightweightBenchModule::onHandleChar(SkUnichar c) {
270 return true;
271}