blob: 8f9e4881aa0a07cfd3b249f38448c701f88fc8bf [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.");
34DEFINE_string(key, "", ""); // dummy to enable gm tests that have platform-specific names
35DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
36DEFINE_string(properties, "",
37 "Space-separated key/value pairs to add to JSON identifying this run.");
38
39static SkString humanize(double ms) {
40 if (FLAGS_verbose) {
41 return SkStringPrintf("%llu", (uint64_t)(ms*1e6));
42 }
43 return HumanizeMs(ms);
44}
45
46#define HUMANIZE(time) humanize(time).c_str()
47
48VisualLightweightBenchModule::VisualLightweightBenchModule(VisualBench* owner)
49 : fCurrentSample(0)
50 , fCurrentFrame(0)
51 , fLoops(1)
52 , fState(kPreWarmLoops_State)
53 , fBenchmark(nullptr)
54 , fOwner(SkRef(owner))
55 , fResults(new ResultsWriter) {
56 fBenchmarkStream.reset(new VisualBenchmarkStream);
57
58 // Print header
59 SkDebugf("curr/maxrss\tloops\tflushes\tmin\tmedian\tmean\tmax\tstddev\tbench\n");
60
61 // setup json logging if required
62 if (!FLAGS_outResultsFile.isEmpty()) {
63 fResults.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
64 }
65
66 if (1 == FLAGS_properties.count() % 2) {
67 SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n");
68 } else {
69 for (int i = 1; i < FLAGS_properties.count(); i += 2) {
70 fResults->property(FLAGS_properties[i - 1], FLAGS_properties[i]);
71 }
72 }
73}
74
75inline void VisualLightweightBenchModule::renderFrame(SkCanvas* canvas) {
76 fBenchmark->draw(fLoops, canvas);
77 canvas->flush();
78 fOwner->present();
79}
80
81void VisualLightweightBenchModule::printStats() {
82 const SkTArray<double>& measurements = fRecords.back().fMeasurements;
83 const char* shortName = fBenchmark->getUniqueName();
84
85 // update log
86 // Note: We currently log only the minimum. It would be interesting to log more information
87 SkString configName;
88 if (FLAGS_msaa > 0) {
89 configName.appendf("msaa_%d", FLAGS_msaa);
90 } else {
91 configName.appendf("gpu");
92 }
93 fResults->config(configName.c_str());
94 fResults->configOption("name", fBenchmark->getUniqueName());
95 SkASSERT(measurements.count());
96 Stats stats(measurements);
97 fResults->metric("min_ms", stats.min);
98
99 // Print output
100 if (FLAGS_verbose) {
101 for (int i = 0; i < measurements.count(); i++) {
102 SkDebugf("%s ", HUMANIZE(measurements[i]));
103 }
104 SkDebugf("%s\n", shortName);
105 } else {
106 const double stdDevPercent = 100 * sqrt(stats.var) / stats.mean;
107 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\n",
108 sk_tools::getCurrResidentSetSizeMB(),
109 sk_tools::getMaxResidentSetSizeMB(),
110 fLoops,
111 HUMANIZE(stats.min),
112 HUMANIZE(stats.median),
113 HUMANIZE(stats.mean),
114 HUMANIZE(stats.max),
115 stdDevPercent,
116 shortName);
117 }
118}
119
120bool VisualLightweightBenchModule::advanceRecordIfNecessary(SkCanvas* canvas) {
121 if (fBenchmark) {
122 return true;
123 }
124
125 fBenchmark.reset(fBenchmarkStream->next());
126 if (!fBenchmark) {
127 return false;
128 }
129
130 canvas->clear(0xffffffff);
131 fBenchmark->preDraw();
132 fRecords.push_back();
133
134 // Log bench name
135 fResults->bench(fBenchmark->getUniqueName(), fBenchmark->getSize().fX,
136 fBenchmark->getSize().fY);
137 return true;
138}
139
140inline void VisualLightweightBenchModule::nextState(State nextState) {
141 fState = nextState;
142}
143
144void VisualLightweightBenchModule::perCanvasPreDraw(SkCanvas* canvas, State nextState) {
145 fBenchmark->perCanvasPreDraw(canvas);
146 fCurrentFrame = 0;
147 this->nextState(nextState);
148}
149
150void VisualLightweightBenchModule::preWarm(State nextState) {
151 if (fCurrentFrame >= FLAGS_gpuFrameLag) {
152 // we currently time across all frames to make sure we capture all GPU work
153 this->nextState(nextState);
154 fCurrentFrame = 0;
155 fTimer.start();
156 } else {
157 fCurrentFrame++;
158 }
159}
160
161void VisualLightweightBenchModule::draw(SkCanvas* canvas) {
162 if (!this->advanceRecordIfNecessary(canvas)) {
163 SkDebugf("Exiting VisualBench successfully\n");
164 fOwner->closeWindow();
165 return;
166 }
167 this->renderFrame(canvas);
168 switch (fState) {
169 case kPreWarmLoopsPerCanvasPreDraw_State: {
170 this->perCanvasPreDraw(canvas, kPreWarmLoops_State);
171 break;
172 }
173 case kPreWarmLoops_State: {
174 this->preWarm(kTuneLoops_State);
175 break;
176 }
177 case kTuneLoops_State: {
178 this->tuneLoops();
179 break;
180 }
181 case kPreWarmTimingPerCanvasPreDraw_State: {
182 this->perCanvasPreDraw(canvas, kPreWarmTiming_State);
183 break;
184 }
185 case kPreWarmTiming_State: {
186 this->preWarm(kTiming_State);
187 break;
188 }
189 case kTiming_State: {
190 this->timing(canvas);
191 break;
192 }
193 }
194}
195
196inline double VisualLightweightBenchModule::elapsed() {
197 fTimer.end();
198 return fTimer.fWall;
199}
200
201void VisualLightweightBenchModule::resetTimingState() {
202 fCurrentFrame = 0;
203 fTimer = WallTimer();
204 fOwner->reset();
205}
206
207void VisualLightweightBenchModule::scaleLoops(double elapsedMs) {
208 // Scale back the number of loops
209 fLoops = (int)ceil(fLoops * FLAGS_loopMs / elapsedMs);
210}
211
212inline void VisualLightweightBenchModule::tuneLoops() {
213 if (1 << 30 == fLoops) {
214 // We're about to wrap. Something's wrong with the bench.
215 SkDebugf("InnerLoops wrapped\n");
216 fLoops = 0;
217 } else {
218 double elapsedMs = this->elapsed();
219 if (elapsedMs > FLAGS_loopMs) {
220 this->scaleLoops(elapsedMs);
221 this->nextState(kPreWarmTimingPerCanvasPreDraw_State);
222 } else {
223 fLoops *= 2;
224 this->nextState(kPreWarmLoops_State);
225 }
226 this->resetTimingState();
227 }
228}
229
230void VisualLightweightBenchModule::recordMeasurement() {
231 double measurement = this->elapsed() / (FLAGS_frames * fLoops);
232 fRecords.back().fMeasurements.push_back(measurement);
233}
234
235void VisualLightweightBenchModule::postDraw(SkCanvas* canvas) {
236 fBenchmark->perCanvasPostDraw(canvas);
237 fBenchmark.reset(nullptr);
238 fCurrentSample = 0;
239 fLoops = 1;
240}
241
242inline void VisualLightweightBenchModule::timing(SkCanvas* canvas) {
243 if (fCurrentFrame >= FLAGS_frames) {
244 this->recordMeasurement();
245 if (fCurrentSample++ >= FLAGS_samples) {
246 this->printStats();
247 this->postDraw(canvas);
248 this->nextState(kPreWarmLoopsPerCanvasPreDraw_State);
249 } else {
250 this->nextState(kPreWarmTimingPerCanvasPreDraw_State);
251 }
252 this->resetTimingState();
253 } else {
254 fCurrentFrame++;
255 }
256}