blob: d54c788ae7a7e515f428a46a3ad7f036647b04f4 [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
joshualitt7d4b4582015-09-24 08:08:23 -070029DEFINE_int32(maxWarmupFrames, 100, "maxmium frames to try and tune for sane timings");
joshualitt189aef72015-09-08 07:08:11 -070030DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
31DEFINE_int32(samples, 10, "Number of times to time each skp.");
32DEFINE_int32(frames, 5, "Number of frames of each skp to render per sample.");
33DEFINE_double(loopMs, 5, "Target loop time in millseconds.");
34DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
joshualitt189aef72015-09-08 07:08:11 -070035DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtkleinca6f43b2015-09-15 13:29:20 -070036DEFINE_string(key, "",
37 "Space-separated key/value pairs to add to JSON identifying this builder.");
joshualitt189aef72015-09-08 07:08:11 -070038DEFINE_string(properties, "",
39 "Space-separated key/value pairs to add to JSON identifying this run.");
40
41static SkString humanize(double ms) {
42 if (FLAGS_verbose) {
43 return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
44 }
45 return HumanizeMs(ms);
46}
47
48#define HUMANIZE(time) humanize(time).c_str()
49
joshualitt8db65a62015-09-24 12:42:02 -070050// We draw a big nonAA path to warmup the gpu / cpu
joshualitt7d4b4582015-09-24 08:08:23 -070051class WarmupBench : public Benchmark {
52public:
joshualitt8db65a62015-09-24 12:42:02 -070053 WarmupBench() {
54 make_path(fPath);
55 }
joshualitt7d4b4582015-09-24 08:08:23 -070056private:
joshualitt8db65a62015-09-24 12:42:02 -070057 static void make_path(SkPath& path) {
58 #include "BigPathBench.inc"
59 }
joshualitt7d4b4582015-09-24 08:08:23 -070060 const char* onGetName() override { return "warmupbench"; }
61 void onDraw(const int loops, SkCanvas* canvas) override {
joshualitt8db65a62015-09-24 12:42:02 -070062 SkPaint paint;
63 paint.setStyle(SkPaint::kStroke_Style);
64 paint.setStrokeWidth(2);
joshualitt7d4b4582015-09-24 08:08:23 -070065 for (int i = 0; i < loops; i++) {
joshualitt8db65a62015-09-24 12:42:02 -070066 canvas->drawPath(fPath, paint);
joshualitt7d4b4582015-09-24 08:08:23 -070067 }
68 }
joshualitt8db65a62015-09-24 12:42:02 -070069 SkPath fPath;
joshualitt7d4b4582015-09-24 08:08:23 -070070};
71
joshualitt189aef72015-09-08 07:08:11 -070072VisualLightweightBenchModule::VisualLightweightBenchModule(VisualBench* owner)
73 : fCurrentSample(0)
74 , fCurrentFrame(0)
75 , fLoops(1)
joshualitt7d4b4582015-09-24 08:08:23 -070076 , fState(kWarmup_State)
joshualitt189aef72015-09-08 07:08:11 -070077 , fBenchmark(nullptr)
78 , fOwner(SkRef(owner))
79 , fResults(new ResultsWriter) {
80 fBenchmarkStream.reset(new VisualBenchmarkStream);
81
82 // Print header
joshualitt7d4b4582015-09-24 08:08:23 -070083 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tbench\n", FLAGS_samples,
84 "samples");
joshualitt189aef72015-09-08 07:08:11 -070085
86 // setup json logging if required
87 if (!FLAGS_outResultsFile.isEmpty()) {
88 fResults.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
89 }
90
mtkleinca6f43b2015-09-15 13:29:20 -070091 if (1 == FLAGS_key.count() % 2) {
92 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
93 } else {
94 for (int i = 1; i < FLAGS_key.count(); i += 2) {
95 fResults->key(FLAGS_key[i - 1], FLAGS_key[i]);
96 }
97 }
98
joshualitt189aef72015-09-08 07:08:11 -070099 if (1 == FLAGS_properties.count() % 2) {
100 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
101 } else {
102 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
103 fResults->property(FLAGS_properties[i - 1], FLAGS_properties[i]);
104 }
105 }
106}
107
108inline void VisualLightweightBenchModule::renderFrame(SkCanvas* canvas) {
109 fBenchmark->draw(fLoops, canvas);
110 canvas->flush();
111 fOwner->present();
112}
113
114void VisualLightweightBenchModule::printStats() {
115 const SkTArray<double>& measurements = fRecords.back().fMeasurements;
116 const char* shortName = fBenchmark->getUniqueName();
117
118 // update log
119 // Note: We currently log only the minimum. It would be interesting to log more information
120 SkString configName;
121 if (FLAGS_msaa > 0) {
122 configName.appendf("msaa_%d", FLAGS_msaa);
123 } else {
124 configName.appendf("gpu");
125 }
126 fResults->config(configName.c_str());
joshualitt7d4b4582015-09-24 08:08:23 -0700127 fResults->configOption("name", shortName);
joshualitt189aef72015-09-08 07:08:11 -0700128 SkASSERT(measurements.count());
129 Stats stats(measurements);
joshualitt7d4b4582015-09-24 08:08:23 -0700130 fResults->metric("min_ms", stats.min);
joshualitt189aef72015-09-08 07:08:11 -0700131
132 // Print output
133 if (FLAGS_verbose) {
134 for (int i = 0; i < measurements.count(); i++) {
135 SkDebugf("%s ", HUMANIZE(measurements[i]));
136 }
137 SkDebugf("%s\n", shortName);
138 } else {
139 const double stdDevPercent = 100 * sqrt(stats.var) / stats.mean;
joshualitt7d4b4582015-09-24 08:08:23 -0700140 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\n",
joshualitt189aef72015-09-08 07:08:11 -0700141 sk_tools::getCurrResidentSetSizeMB(),
142 sk_tools::getMaxResidentSetSizeMB(),
143 fLoops,
144 HUMANIZE(stats.min),
145 HUMANIZE(stats.median),
146 HUMANIZE(stats.mean),
147 HUMANIZE(stats.max),
148 stdDevPercent,
joshualitt7d4b4582015-09-24 08:08:23 -0700149 stats.plot.c_str(),
joshualitt189aef72015-09-08 07:08:11 -0700150 shortName);
151 }
152}
153
154bool VisualLightweightBenchModule::advanceRecordIfNecessary(SkCanvas* canvas) {
joshualitt7d4b4582015-09-24 08:08:23 -0700155 if (!fBenchmark && fState == kWarmup_State) {
joshualitt8db65a62015-09-24 12:42:02 -0700156 fOwner->clear(canvas, SK_ColorWHITE, 2);
joshualitt7d4b4582015-09-24 08:08:23 -0700157 fBenchmark.reset(new WarmupBench);
158 return true;
159 }
160
joshualitt189aef72015-09-08 07:08:11 -0700161 if (fBenchmark) {
162 return true;
163 }
164
165 fBenchmark.reset(fBenchmarkStream->next());
166 if (!fBenchmark) {
167 return false;
168 }
169
jvanverthf5d1b2d2015-09-15 07:40:56 -0700170 fOwner->clear(canvas, SK_ColorWHITE, 2);
171
joshualitt8a6697a2015-09-30 12:11:07 -0700172 fBenchmark->delayedSetup();
joshualitt189aef72015-09-08 07:08:11 -0700173 fRecords.push_back();
174
175 // Log bench name
176 fResults->bench(fBenchmark->getUniqueName(), fBenchmark->getSize().fX,
177 fBenchmark->getSize().fY);
178 return true;
179}
180
181inline void VisualLightweightBenchModule::nextState(State nextState) {
182 fState = nextState;
183}
184
185void VisualLightweightBenchModule::perCanvasPreDraw(SkCanvas* canvas, State nextState) {
186 fBenchmark->perCanvasPreDraw(canvas);
joshualitt8a6697a2015-09-30 12:11:07 -0700187 fBenchmark->preDraw(canvas);
joshualitt189aef72015-09-08 07:08:11 -0700188 fCurrentFrame = 0;
189 this->nextState(nextState);
190}
191
joshualitt7d4b4582015-09-24 08:08:23 -0700192void VisualLightweightBenchModule::warmup(SkCanvas* canvas) {
193 if (fCurrentFrame >= FLAGS_maxWarmupFrames) {
194 this->nextState(kPreWarmLoopsPerCanvasPreDraw_State);
195 fBenchmark.reset(nullptr);
196 this->resetTimingState();
197 fLoops = 1;
198 } else {
199 bool isEven = (fCurrentFrame++ % 2) == 0;
200 if (isEven) {
201 fTimer.start();
202 } else {
203 double elapsedMs = this->elapsed();
204 if (elapsedMs < FLAGS_loopMs) {
205 fLoops *= 2;
206 }
207 fTimer = WallTimer();
208 fOwner->reset();
209 }
210 }
211}
212
joshualitt189aef72015-09-08 07:08:11 -0700213void VisualLightweightBenchModule::preWarm(State nextState) {
214 if (fCurrentFrame >= FLAGS_gpuFrameLag) {
215 // we currently time across all frames to make sure we capture all GPU work
216 this->nextState(nextState);
217 fCurrentFrame = 0;
218 fTimer.start();
219 } else {
220 fCurrentFrame++;
221 }
222}
223
224void VisualLightweightBenchModule::draw(SkCanvas* canvas) {
225 if (!this->advanceRecordIfNecessary(canvas)) {
226 SkDebugf("Exiting VisualBench successfully\n");
227 fOwner->closeWindow();
228 return;
229 }
230 this->renderFrame(canvas);
231 switch (fState) {
joshualitt7d4b4582015-09-24 08:08:23 -0700232 case kWarmup_State: {
233 this->warmup(canvas);
234 break;
235 }
joshualitt189aef72015-09-08 07:08:11 -0700236 case kPreWarmLoopsPerCanvasPreDraw_State: {
237 this->perCanvasPreDraw(canvas, kPreWarmLoops_State);
238 break;
239 }
240 case kPreWarmLoops_State: {
241 this->preWarm(kTuneLoops_State);
242 break;
243 }
244 case kTuneLoops_State: {
245 this->tuneLoops();
246 break;
247 }
248 case kPreWarmTimingPerCanvasPreDraw_State: {
249 this->perCanvasPreDraw(canvas, kPreWarmTiming_State);
250 break;
251 }
252 case kPreWarmTiming_State: {
253 this->preWarm(kTiming_State);
254 break;
255 }
256 case kTiming_State: {
257 this->timing(canvas);
258 break;
259 }
260 }
261}
262
263inline double VisualLightweightBenchModule::elapsed() {
264 fTimer.end();
265 return fTimer.fWall;
266}
267
268void VisualLightweightBenchModule::resetTimingState() {
269 fCurrentFrame = 0;
270 fTimer = WallTimer();
271 fOwner->reset();
272}
273
joshualitt189aef72015-09-08 07:08:11 -0700274inline void VisualLightweightBenchModule::tuneLoops() {
275 if (1 << 30 == fLoops) {
276 // We're about to wrap. Something's wrong with the bench.
277 SkDebugf("InnerLoops wrapped\n");
joshualitt7d4b4582015-09-24 08:08:23 -0700278 fLoops = 1;
joshualitt189aef72015-09-08 07:08:11 -0700279 } else {
280 double elapsedMs = this->elapsed();
281 if (elapsedMs > FLAGS_loopMs) {
joshualitt189aef72015-09-08 07:08:11 -0700282 this->nextState(kPreWarmTimingPerCanvasPreDraw_State);
283 } else {
284 fLoops *= 2;
285 this->nextState(kPreWarmLoops_State);
286 }
287 this->resetTimingState();
288 }
289}
290
291void VisualLightweightBenchModule::recordMeasurement() {
292 double measurement = this->elapsed() / (FLAGS_frames * fLoops);
293 fRecords.back().fMeasurements.push_back(measurement);
294}
295
296void VisualLightweightBenchModule::postDraw(SkCanvas* canvas) {
joshualitt8a6697a2015-09-30 12:11:07 -0700297 fBenchmark->postDraw(canvas);
joshualitt189aef72015-09-08 07:08:11 -0700298 fBenchmark->perCanvasPostDraw(canvas);
299 fBenchmark.reset(nullptr);
300 fCurrentSample = 0;
301 fLoops = 1;
302}
303
304inline void VisualLightweightBenchModule::timing(SkCanvas* canvas) {
305 if (fCurrentFrame >= FLAGS_frames) {
306 this->recordMeasurement();
307 if (fCurrentSample++ >= FLAGS_samples) {
308 this->printStats();
309 this->postDraw(canvas);
310 this->nextState(kPreWarmLoopsPerCanvasPreDraw_State);
311 } else {
312 this->nextState(kPreWarmTimingPerCanvasPreDraw_State);
313 }
314 this->resetTimingState();
315 } else {
316 fCurrentFrame++;
317 }
318}
jvanverthf5d1b2d2015-09-15 07:40:56 -0700319
320bool VisualLightweightBenchModule::onHandleChar(SkUnichar c) {
321 return true;
322}