blob: 3522c4acb228ad60a26f33a5c6b38299cd2cc8fb [file] [log] [blame]
mtkleinf3723212014-06-25 14:08:00 -07001/*
2 * Copyright 2014 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
mtkleinbb6a0282014-07-01 08:43:42 -07008#include <ctype.h>
9
mtkleinf3723212014-06-25 14:08:00 -070010#include "Benchmark.h"
11#include "CrashHandler.h"
mtkleine714e752014-07-31 12:13:48 -070012#include "GMBench.h"
mtklein60317d0f2014-07-14 11:30:37 -070013#include "ResultsWriter.h"
mtklein92007582014-08-01 07:46:52 -070014#include "SKPBench.h"
mtkleinf3723212014-06-25 14:08:00 -070015#include "Stats.h"
16#include "Timer.h"
17
mtklein92007582014-08-01 07:46:52 -070018#include "SkOSFile.h"
mtkleinf3723212014-06-25 14:08:00 -070019#include "SkCanvas.h"
caryclark17f0b6d2014-07-22 10:15:34 -070020#include "SkCommonFlags.h"
mtkleinf3723212014-06-25 14:08:00 -070021#include "SkForceLinking.h"
22#include "SkGraphics.h"
23#include "SkString.h"
24#include "SkSurface.h"
25
mtkleinbb6a0282014-07-01 08:43:42 -070026#if SK_SUPPORT_GPU
jcgregoriobf5e5232014-07-17 13:14:16 -070027 #include "gl/GrGLDefines.h"
mtkleinbb6a0282014-07-01 08:43:42 -070028 #include "GrContextFactory.h"
krajcevski69a55602014-08-13 10:46:31 -070029 SkAutoTDelete<GrContextFactory> gGrFactory;
mtkleinbb6a0282014-07-01 08:43:42 -070030#endif
31
mtkleinf3723212014-06-25 14:08:00 -070032__SK_FORCE_IMAGE_DECODER_LINKING;
33
bsalomon6eb03cc2014-08-07 14:28:50 -070034static const int kAutoTuneLoops = -1;
35
mtkleinb5110422014-08-07 15:20:02 -070036static const int kDefaultLoops =
bsalomon6eb03cc2014-08-07 14:28:50 -070037#ifdef SK_DEBUG
38 1;
mtkleina189ccd2014-07-14 12:28:47 -070039#else
bsalomon6eb03cc2014-08-07 14:28:50 -070040 kAutoTuneLoops;
mtkleina189ccd2014-07-14 12:28:47 -070041#endif
42
bsalomon6eb03cc2014-08-07 14:28:50 -070043static SkString loops_help_txt() {
44 SkString help;
45 help.printf("Number of times to run each bench. Set this to %d to auto-"
46 "tune for each bench. Timings are only reported when auto-tuning.",
47 kAutoTuneLoops);
48 return help;
49}
50
51DEFINE_int32(loops, kDefaultLoops, loops_help_txt().c_str());
52
mtkleinf3723212014-06-25 14:08:00 -070053DEFINE_int32(samples, 10, "Number of samples to measure for each bench.");
54DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead.");
55DEFINE_double(overheadGoal, 0.0001,
56 "Loop until timer overhead is at most this fraction of our measurments.");
mtkleinbb6a0282014-07-01 08:43:42 -070057DEFINE_double(gpuMs, 5, "Target bench time in millseconds for GPU.");
58DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU allows to lag.");
mtkleinf3723212014-06-25 14:08:00 -070059
mtklein60317d0f2014-07-14 11:30:37 -070060DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtklein55b0ffc2014-07-17 08:38:23 -070061DEFINE_int32(maxCalibrationAttempts, 3,
62 "Try up to this many times to guess loops for a bench, or skip the bench.");
63DEFINE_int32(maxLoops, 1000000, "Never run a bench more times than this.");
jcgregoriobf5e5232014-07-17 13:14:16 -070064DEFINE_string(key, "", "Space-separated key/value pairs to add to JSON.");
65DEFINE_string(gitHash, "", "Git hash to add to JSON.");
mtklein60317d0f2014-07-14 11:30:37 -070066
mtklein92007582014-08-01 07:46:52 -070067DEFINE_string(clip, "0,0,1000,1000", "Clip for SKPs.");
68DEFINE_string(scales, "1.0", "Space-separated scales for SKPs.");
69
mtkleinf3723212014-06-25 14:08:00 -070070static SkString humanize(double ms) {
71 if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3);
72 if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6);
mtklein62386882014-07-15 10:30:31 -070073#ifdef SK_BUILD_FOR_WIN
74 if (ms < 1) return SkStringPrintf("%.3gus", ms*1e3);
75#else
mtkleinf3723212014-06-25 14:08:00 -070076 if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3);
mtklein62386882014-07-15 10:30:31 -070077#endif
mtkleinf3723212014-06-25 14:08:00 -070078 return SkStringPrintf("%.3gms", ms);
79}
mtklein55b0ffc2014-07-17 08:38:23 -070080#define HUMANIZE(ms) humanize(ms).c_str()
mtkleinf3723212014-06-25 14:08:00 -070081
mtkleinbb6a0282014-07-01 08:43:42 -070082static double time(int loops, Benchmark* bench, SkCanvas* canvas, SkGLContextHelper* gl) {
bsalomon6eb03cc2014-08-07 14:28:50 -070083 if (canvas) {
84 canvas->clear(SK_ColorWHITE);
85 }
mtkleinbb6a0282014-07-01 08:43:42 -070086 WallTimer timer;
87 timer.start();
88 if (bench) {
89 bench->draw(loops, canvas);
90 }
91 if (canvas) {
92 canvas->flush();
93 }
94#if SK_SUPPORT_GPU
95 if (gl) {
96 SK_GL(*gl, Flush());
97 gl->swapBuffers();
98 }
99#endif
100 timer.end();
101 return timer.fWall;
102}
103
mtkleinf3723212014-06-25 14:08:00 -0700104static double estimate_timer_overhead() {
105 double overhead = 0;
mtkleinf3723212014-06-25 14:08:00 -0700106 for (int i = 0; i < FLAGS_overheadLoops; i++) {
mtkleinbb6a0282014-07-01 08:43:42 -0700107 overhead += time(1, NULL, NULL, NULL);
mtkleinf3723212014-06-25 14:08:00 -0700108 }
109 return overhead / FLAGS_overheadLoops;
110}
111
mtklein55b0ffc2014-07-17 08:38:23 -0700112static int clamp_loops(int loops) {
113 if (loops < 1) {
114 SkDebugf("ERROR: clamping loops from %d to 1.\n", loops);
115 return 1;
116 }
117 if (loops > FLAGS_maxLoops) {
118 SkDebugf("WARNING: clamping loops from %d to FLAGS_maxLoops, %d.\n", loops, FLAGS_maxLoops);
119 return FLAGS_maxLoops;
120 }
121 return loops;
122}
123
bsalomon6eb03cc2014-08-07 14:28:50 -0700124static bool write_canvas_png(SkCanvas* canvas, const SkString& filename) {
125 if (filename.isEmpty()) {
126 return false;
127 }
128 if (kUnknown_SkColorType == canvas->imageInfo().fColorType) {
129 return false;
130 }
131 SkBitmap bmp;
132 bmp.setInfo(canvas->imageInfo());
133 if (!canvas->readPixels(&bmp, 0, 0)) {
134 SkDebugf("Can't read canvas pixels.\n");
135 return false;
136 }
137 SkString dir = SkOSPath::Dirname(filename.c_str());
138 if (!sk_mkdir(dir.c_str())) {
139 SkDebugf("Can't make dir %s.\n", dir.c_str());
140 return false;
141 }
142 SkFILEWStream stream(filename.c_str());
143 if (!stream.isValid()) {
144 SkDebugf("Can't write %s.\n", filename.c_str());
145 return false;
146 }
147 if (!SkImageEncoder::EncodeStream(&stream, bmp, SkImageEncoder::kPNG_Type, 100)) {
148 SkDebugf("Can't encode a PNG.\n");
149 return false;
150 }
151 return true;
152}
153
154static int kFailedLoops = -2;
mtkleinbb6a0282014-07-01 08:43:42 -0700155static int cpu_bench(const double overhead, Benchmark* bench, SkCanvas* canvas, double* samples) {
156 // First figure out approximately how many loops of bench it takes to make overhead negligible.
mtklein2069e222014-08-04 13:57:39 -0700157 double bench_plus_overhead = 0.0;
mtklein55b0ffc2014-07-17 08:38:23 -0700158 int round = 0;
bsalomon6eb03cc2014-08-07 14:28:50 -0700159 if (kAutoTuneLoops == FLAGS_loops) {
160 while (bench_plus_overhead < overhead) {
161 if (round++ == FLAGS_maxCalibrationAttempts) {
162 SkDebugf("WARNING: Can't estimate loops for %s (%s vs. %s); skipping.\n",
163 bench->getName(), HUMANIZE(bench_plus_overhead), HUMANIZE(overhead));
164 return kFailedLoops;
165 }
166 bench_plus_overhead = time(1, bench, canvas, NULL);
mtklein55b0ffc2014-07-17 08:38:23 -0700167 }
mtklein2069e222014-08-04 13:57:39 -0700168 }
mtkleinf3723212014-06-25 14:08:00 -0700169
mtkleinbb6a0282014-07-01 08:43:42 -0700170 // Later we'll just start and stop the timer once but loop N times.
mtkleinf3723212014-06-25 14:08:00 -0700171 // We'll pick N to make timer overhead negligible:
172 //
mtkleinbb6a0282014-07-01 08:43:42 -0700173 // overhead
174 // ------------------------- < FLAGS_overheadGoal
175 // overhead + N * Bench Time
mtkleinf3723212014-06-25 14:08:00 -0700176 //
mtkleinbb6a0282014-07-01 08:43:42 -0700177 // where bench_plus_overhead ≈ overhead + Bench Time.
mtkleinf3723212014-06-25 14:08:00 -0700178 //
179 // Doing some math, we get:
180 //
mtkleinbb6a0282014-07-01 08:43:42 -0700181 // (overhead / FLAGS_overheadGoal) - overhead
182 // ------------------------------------------ < N
183 // bench_plus_overhead - overhead)
mtkleinf3723212014-06-25 14:08:00 -0700184 //
185 // Luckily, this also works well in practice. :)
bsalomon6eb03cc2014-08-07 14:28:50 -0700186 int loops = FLAGS_loops;
187 if (kAutoTuneLoops == loops) {
188 const double numer = overhead / FLAGS_overheadGoal - overhead;
189 const double denom = bench_plus_overhead - overhead;
190 loops = (int)ceil(numer / denom);
191 }
192 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700193
194 for (int i = 0; i < FLAGS_samples; i++) {
195 samples[i] = time(loops, bench, canvas, NULL) / loops;
196 }
197 return loops;
mtkleinf3723212014-06-25 14:08:00 -0700198}
199
mtkleinbb6a0282014-07-01 08:43:42 -0700200#if SK_SUPPORT_GPU
201static int gpu_bench(SkGLContextHelper* gl,
202 Benchmark* bench,
203 SkCanvas* canvas,
204 double* samples) {
bsalomonc2553372014-07-22 13:09:05 -0700205 gl->makeCurrent();
mtkleinbb6a0282014-07-01 08:43:42 -0700206 // Make sure we're done with whatever came before.
mtklein9bc86ed2014-07-01 10:02:42 -0700207 SK_GL(*gl, Finish());
mtkleinbb6a0282014-07-01 08:43:42 -0700208
209 // First, figure out how many loops it'll take to get a frame up to FLAGS_gpuMs.
bsalomon6eb03cc2014-08-07 14:28:50 -0700210 int loops = FLAGS_loops;
211 if (kAutoTuneLoops == loops) {
212 loops = 1;
mtkleina189ccd2014-07-14 12:28:47 -0700213 double elapsed = 0;
214 do {
215 loops *= 2;
216 // If the GPU lets frames lag at all, we need to make sure we're timing
217 // _this_ round, not still timing last round. We force this by looping
218 // more times than any reasonable GPU will allow frames to lag.
219 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
220 elapsed = time(loops, bench, canvas, gl);
221 }
222 } while (elapsed < FLAGS_gpuMs);
mtkleinbb6a0282014-07-01 08:43:42 -0700223
mtkleina189ccd2014-07-14 12:28:47 -0700224 // We've overshot at least a little. Scale back linearly.
225 loops = (int)ceil(loops * FLAGS_gpuMs / elapsed);
mtkleinbb6a0282014-07-01 08:43:42 -0700226
mtkleina189ccd2014-07-14 12:28:47 -0700227 // Might as well make sure we're not still timing our calibration.
228 SK_GL(*gl, Finish());
229 }
mtklein55b0ffc2014-07-17 08:38:23 -0700230 loops = clamp_loops(loops);
mtkleinbb6a0282014-07-01 08:43:42 -0700231
232 // Pretty much the same deal as the calibration: do some warmup to make
233 // sure we're timing steady-state pipelined frames.
234 for (int i = 0; i < FLAGS_gpuFrameLag; i++) {
235 time(loops, bench, canvas, gl);
mtkleinf3723212014-06-25 14:08:00 -0700236 }
mtkleinbb6a0282014-07-01 08:43:42 -0700237
238 // Now, actually do the timing!
239 for (int i = 0; i < FLAGS_samples; i++) {
240 samples[i] = time(loops, bench, canvas, gl) / loops;
241 }
242 return loops;
243}
244#endif
245
246static SkString to_lower(const char* str) {
247 SkString lower(str);
248 for (size_t i = 0; i < lower.size(); i++) {
249 lower[i] = tolower(lower[i]);
250 }
251 return lower;
mtkleinf3723212014-06-25 14:08:00 -0700252}
253
bsalomonc2553372014-07-22 13:09:05 -0700254struct Config {
255 const char* name;
mtkleinbb6a0282014-07-01 08:43:42 -0700256 Benchmark::Backend backend;
bsalomonc2553372014-07-22 13:09:05 -0700257 SkColorType color;
258 SkAlphaType alpha;
259 int samples;
260#if SK_SUPPORT_GPU
261 GrContextFactory::GLContextType ctxType;
262#else
263 int bogusInt;
264#endif
265};
266
267struct Target {
268 explicit Target(const Config& c) : config(c) {}
269 const Config config;
mtkleinbb6a0282014-07-01 08:43:42 -0700270 SkAutoTDelete<SkSurface> surface;
271#if SK_SUPPORT_GPU
272 SkGLContextHelper* gl;
273#endif
274};
mtkleinf3723212014-06-25 14:08:00 -0700275
bsalomonc2553372014-07-22 13:09:05 -0700276static bool is_cpu_config_allowed(const char* name) {
mtkleinbb6a0282014-07-01 08:43:42 -0700277 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomonc2553372014-07-22 13:09:05 -0700278 if (to_lower(FLAGS_config[i]).equals(name)) {
279 return true;
mtkleinf3723212014-06-25 14:08:00 -0700280 }
281 }
bsalomonc2553372014-07-22 13:09:05 -0700282 return false;
mtkleinbb6a0282014-07-01 08:43:42 -0700283}
284
bsalomonc2553372014-07-22 13:09:05 -0700285#if SK_SUPPORT_GPU
286static bool is_gpu_config_allowed(const char* name, GrContextFactory::GLContextType ctxType,
287 int sampleCnt) {
288 if (!is_cpu_config_allowed(name)) {
289 return false;
290 }
krajcevski69a55602014-08-13 10:46:31 -0700291 if (const GrContext* ctx = gGrFactory->get(ctxType)) {
bsalomonc2553372014-07-22 13:09:05 -0700292 return sampleCnt <= ctx->getMaxSampleCount();
293 }
294 return false;
295}
296#endif
mtkleinbb6a0282014-07-01 08:43:42 -0700297
bsalomonc2553372014-07-22 13:09:05 -0700298#if SK_SUPPORT_GPU
299#define kBogusGLContextType GrContextFactory::kNative_GLContextType
300#else
301#define kBogusGLContextType 0
mtkleine714e752014-07-31 12:13:48 -0700302#endif
bsalomonc2553372014-07-22 13:09:05 -0700303
304// Append all configs that are enabled and supported.
305static void create_configs(SkTDArray<Config>* configs) {
306 #define CPU_CONFIG(name, backend, color, alpha) \
307 if (is_cpu_config_allowed(#name)) { \
308 Config config = { #name, Benchmark::backend, color, alpha, 0, kBogusGLContextType }; \
309 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700310 }
mtkleine714e752014-07-31 12:13:48 -0700311
mtklein40b32be2014-07-09 08:46:49 -0700312 if (FLAGS_cpu) {
bsalomonc2553372014-07-22 13:09:05 -0700313 CPU_CONFIG(nonrendering, kNonRendering_Backend, kUnknown_SkColorType, kUnpremul_SkAlphaType)
314 CPU_CONFIG(8888, kRaster_Backend, kN32_SkColorType, kPremul_SkAlphaType)
315 CPU_CONFIG(565, kRaster_Backend, kRGB_565_SkColorType, kOpaque_SkAlphaType)
mtklein40b32be2014-07-09 08:46:49 -0700316 }
mtkleinbb6a0282014-07-01 08:43:42 -0700317
318#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700319 #define GPU_CONFIG(name, ctxType, samples) \
320 if (is_gpu_config_allowed(#name, GrContextFactory::ctxType, samples)) { \
321 Config config = { \
322 #name, \
323 Benchmark::kGPU_Backend, \
324 kN32_SkColorType, \
325 kPremul_SkAlphaType, \
326 samples, \
327 GrContextFactory::ctxType }; \
328 configs->push(config); \
mtkleinbb6a0282014-07-01 08:43:42 -0700329 }
mtkleine714e752014-07-31 12:13:48 -0700330
mtklein40b32be2014-07-09 08:46:49 -0700331 if (FLAGS_gpu) {
bsalomonc2553372014-07-22 13:09:05 -0700332 GPU_CONFIG(gpu, kNative_GLContextType, 0)
333 GPU_CONFIG(msaa4, kNative_GLContextType, 4)
334 GPU_CONFIG(msaa16, kNative_GLContextType, 16)
335 GPU_CONFIG(nvprmsaa4, kNVPR_GLContextType, 4)
336 GPU_CONFIG(nvprmsaa16, kNVPR_GLContextType, 16)
337 GPU_CONFIG(debug, kDebug_GLContextType, 0)
338 GPU_CONFIG(nullgpu, kNull_GLContextType, 0)
bsalomon3b4d0772014-08-06 10:52:33 -0700339#ifdef SK_ANGLE
340 GPU_CONFIG(angle, kANGLE_GLContextType, 0)
341#endif
mtklein40b32be2014-07-09 08:46:49 -0700342 }
mtkleinbb6a0282014-07-01 08:43:42 -0700343#endif
mtkleinf3723212014-06-25 14:08:00 -0700344}
345
bsalomonc2553372014-07-22 13:09:05 -0700346// If bench is enabled for config, returns a Target* for it, otherwise NULL.
347static Target* is_enabled(Benchmark* bench, const Config& config) {
348 if (!bench->isSuitableFor(config.backend)) {
349 return NULL;
350 }
351
352 SkImageInfo info;
353 info.fAlphaType = config.alpha;
354 info.fColorType = config.color;
355 info.fWidth = bench->getSize().fX;
356 info.fHeight = bench->getSize().fY;
357
358 Target* target = new Target(config);
359
360 if (Benchmark::kRaster_Backend == config.backend) {
361 target->surface.reset(SkSurface::NewRaster(info));
362 }
363#if SK_SUPPORT_GPU
364 else if (Benchmark::kGPU_Backend == config.backend) {
krajcevski69a55602014-08-13 10:46:31 -0700365 target->surface.reset(SkSurface::NewRenderTarget(gGrFactory->get(config.ctxType), info,
bsalomonc2553372014-07-22 13:09:05 -0700366 config.samples));
krajcevski69a55602014-08-13 10:46:31 -0700367 target->gl = gGrFactory->getGLContext(config.ctxType);
bsalomonc2553372014-07-22 13:09:05 -0700368 }
369#endif
370
371 if (Benchmark::kNonRendering_Backend != config.backend && !target->surface.get()) {
372 delete target;
373 return NULL;
374 }
375 return target;
376}
377
378// Creates targets for a benchmark and a set of configs.
379static void create_targets(SkTDArray<Target*>* targets, Benchmark* b,
380 const SkTDArray<Config>& configs) {
381 for (int i = 0; i < configs.count(); ++i) {
382 if (Target* t = is_enabled(b, configs[i])) {
383 targets->push(t);
384 }
mtkleine714e752014-07-31 12:13:48 -0700385
bsalomonc2553372014-07-22 13:09:05 -0700386 }
387}
388
mtklein60317d0f2014-07-14 11:30:37 -0700389static void fill_static_options(ResultsWriter* log) {
390#if defined(SK_BUILD_FOR_WIN32)
391 log->option("system", "WIN32");
392#elif defined(SK_BUILD_FOR_MAC)
393 log->option("system", "MAC");
394#elif defined(SK_BUILD_FOR_ANDROID)
395 log->option("system", "ANDROID");
396#elif defined(SK_BUILD_FOR_UNIX)
397 log->option("system", "UNIX");
398#else
399 log->option("system", "other");
400#endif
mtklein60317d0f2014-07-14 11:30:37 -0700401}
402
jcgregoriobf5e5232014-07-17 13:14:16 -0700403#if SK_SUPPORT_GPU
404static void fill_gpu_options(ResultsWriter* log, SkGLContextHelper* ctx) {
jcgregorio05c45602014-07-17 13:54:36 -0700405 const GrGLubyte* version;
jcgregoriobf5e5232014-07-17 13:14:16 -0700406 SK_GL_RET(*ctx, version, GetString(GR_GL_VERSION));
407 log->configOption("GL_VERSION", (const char*)(version));
408
409 SK_GL_RET(*ctx, version, GetString(GR_GL_RENDERER));
410 log->configOption("GL_RENDERER", (const char*) version);
411
412 SK_GL_RET(*ctx, version, GetString(GR_GL_VENDOR));
413 log->configOption("GL_VENDOR", (const char*) version);
414
415 SK_GL_RET(*ctx, version, GetString(GR_GL_SHADING_LANGUAGE_VERSION));
416 log->configOption("GL_SHADING_LANGUAGE_VERSION", (const char*) version);
417}
418#endif
419
mtkleine714e752014-07-31 12:13:48 -0700420class BenchmarkStream {
421public:
mtklein92007582014-08-01 07:46:52 -0700422 BenchmarkStream() : fBenches(BenchRegistry::Head())
423 , fGMs(skiagm::GMRegistry::Head())
424 , fCurrentScale(0)
425 , fCurrentSKP(0) {
426 for (int i = 0; i < FLAGS_skps.count(); i++) {
427 if (SkStrEndsWith(FLAGS_skps[i], ".skp")) {
428 fSKPs.push_back() = FLAGS_skps[i];
429 } else {
430 SkOSFile::Iter it(FLAGS_skps[i], ".skp");
431 SkString path;
432 while (it.next(&path)) {
433 fSKPs.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str());
434 }
435 }
436 }
mtkleine714e752014-07-31 12:13:48 -0700437
mtklein92007582014-08-01 07:46:52 -0700438 if (4 != sscanf(FLAGS_clip[0], "%d,%d,%d,%d",
439 &fClip.fLeft, &fClip.fTop, &fClip.fRight, &fClip.fBottom)) {
440 SkDebugf("Can't parse %s from --clip as an SkIRect.\n", FLAGS_clip[0]);
441 exit(1);
442 }
443
444 for (int i = 0; i < FLAGS_scales.count(); i++) {
445 if (1 != sscanf(FLAGS_scales[i], "%f", &fScales.push_back())) {
446 SkDebugf("Can't parse %s from --scales as an SkScalar.\n", FLAGS_scales[i]);
447 exit(1);
448 }
449 }
450 }
451
452 Benchmark* next() {
mtkleine714e752014-07-31 12:13:48 -0700453 if (fBenches) {
454 Benchmark* bench = fBenches->factory()(NULL);
455 fBenches = fBenches->next();
mtklein92007582014-08-01 07:46:52 -0700456 fSourceType = "bench";
mtkleine714e752014-07-31 12:13:48 -0700457 return bench;
458 }
mtklein92007582014-08-01 07:46:52 -0700459
mtkleine714e752014-07-31 12:13:48 -0700460 while (fGMs) {
461 SkAutoTDelete<skiagm::GM> gm(fGMs->factory()(NULL));
462 fGMs = fGMs->next();
463 if (gm->getFlags() & skiagm::GM::kAsBench_Flag) {
mtklein92007582014-08-01 07:46:52 -0700464 fSourceType = "gm";
mtkleine714e752014-07-31 12:13:48 -0700465 return SkNEW_ARGS(GMBench, (gm.detach()));
466 }
467 }
mtklein92007582014-08-01 07:46:52 -0700468
469 while (fCurrentScale < fScales.count()) {
470 while (fCurrentSKP < fSKPs.count()) {
471 const SkString& path = fSKPs[fCurrentSKP++];
472
473 // Not strictly necessary, as it will be checked again later,
474 // but helps to avoid a lot of pointless work if we're going to skip it.
475 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, path.c_str())) {
476 continue;
477 }
478
479 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path.c_str()));
480 if (stream.get() == NULL) {
481 SkDebugf("Could not read %s.\n", path.c_str());
482 exit(1);
483 }
484
485 SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream.get()));
486 if (pic.get() == NULL) {
487 SkDebugf("Could not read %s as an SkPicture.\n", path.c_str());
488 exit(1);
489 }
490
491 SkString name = SkOSPath::Basename(path.c_str());
492
493 fSourceType = "skp";
494 return SkNEW_ARGS(SKPBench,
495 (name.c_str(), pic.get(), fClip, fScales[fCurrentScale]));
496 }
497 fCurrentSKP = 0;
498 fCurrentScale++;
499 }
500
mtkleine714e752014-07-31 12:13:48 -0700501 return NULL;
502 }
mtklein92007582014-08-01 07:46:52 -0700503
504 void fillCurrentOptions(ResultsWriter* log) const {
505 log->configOption("source_type", fSourceType);
506 if (0 == strcmp(fSourceType, "skp")) {
507 log->configOption("clip",
508 SkStringPrintf("%d %d %d %d", fClip.fLeft, fClip.fTop,
509 fClip.fRight, fClip.fBottom).c_str());
510 log->configOption("scale", SkStringPrintf("%.2g", fScales[fCurrentScale]).c_str());
511 }
512 }
513
mtkleine714e752014-07-31 12:13:48 -0700514private:
515 const BenchRegistry* fBenches;
516 const skiagm::GMRegistry* fGMs;
mtklein92007582014-08-01 07:46:52 -0700517 SkIRect fClip;
518 SkTArray<SkScalar> fScales;
519 SkTArray<SkString> fSKPs;
520
521 const char* fSourceType;
522 int fCurrentScale;
523 int fCurrentSKP;
mtkleine714e752014-07-31 12:13:48 -0700524};
525
caryclark17f0b6d2014-07-22 10:15:34 -0700526int nanobench_main();
527int nanobench_main() {
mtkleinf3723212014-06-25 14:08:00 -0700528 SetupCrashHandler();
529 SkAutoGraphics ag;
mtkleinf3723212014-06-25 14:08:00 -0700530
krajcevski69a55602014-08-13 10:46:31 -0700531#if SK_SUPPORT_GPU
532 gGrFactory.reset(SkNEW_ARGS(GrContextFactory, (GrContext::Options())));
533#endif
534
bsalomon6eb03cc2014-08-07 14:28:50 -0700535 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700536 FLAGS_samples = 1;
537 FLAGS_gpuFrameLag = 0;
538 }
539
bsalomon6eb03cc2014-08-07 14:28:50 -0700540 if (!FLAGS_writePath.isEmpty()) {
541 SkDebugf("Writing files to %s.\n", FLAGS_writePath[0]);
542 if (!sk_mkdir(FLAGS_writePath[0])) {
543 SkDebugf("Could not create %s. Files won't be written.\n", FLAGS_writePath[0]);
544 FLAGS_writePath.set(0, NULL);
545 }
546 }
547
mtklein60317d0f2014-07-14 11:30:37 -0700548 MultiResultsWriter log;
jcgregoriobf5e5232014-07-17 13:14:16 -0700549 SkAutoTDelete<NanoJSONResultsWriter> json;
mtklein60317d0f2014-07-14 11:30:37 -0700550 if (!FLAGS_outResultsFile.isEmpty()) {
jcgregoriobf5e5232014-07-17 13:14:16 -0700551 const char* gitHash = FLAGS_gitHash.isEmpty() ? "unknown-revision" : FLAGS_gitHash[0];
552 json.reset(SkNEW(NanoJSONResultsWriter(FLAGS_outResultsFile[0], gitHash)));
mtklein60317d0f2014-07-14 11:30:37 -0700553 log.add(json.get());
554 }
555 CallEnd<MultiResultsWriter> ender(log);
jcgregoriobf5e5232014-07-17 13:14:16 -0700556
557 if (1 == FLAGS_key.count() % 2) {
558 SkDebugf("ERROR: --key must be passed with an even number of arguments.\n");
559 return 1;
560 }
561 for (int i = 1; i < FLAGS_key.count(); i += 2) {
562 log.key(FLAGS_key[i-1], FLAGS_key[i]);
563 }
mtklein60317d0f2014-07-14 11:30:37 -0700564 fill_static_options(&log);
565
mtkleinf3723212014-06-25 14:08:00 -0700566 const double overhead = estimate_timer_overhead();
mtklein55b0ffc2014-07-17 08:38:23 -0700567 SkDebugf("Timer overhead: %s\n", HUMANIZE(overhead));
Mike Klein91294772014-07-16 19:59:32 -0400568
mtkleinbb6a0282014-07-01 08:43:42 -0700569 SkAutoTMalloc<double> samples(FLAGS_samples);
570
bsalomon6eb03cc2014-08-07 14:28:50 -0700571 if (kAutoTuneLoops != FLAGS_loops) {
572 SkDebugf("Fixed number of loops; times would only be misleading so we won't print them.\n");
mtkleina189ccd2014-07-14 12:28:47 -0700573 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700574 // No header.
575 } else if (FLAGS_quiet) {
mtklein40b32be2014-07-09 08:46:49 -0700576 SkDebugf("median\tbench\tconfig\n");
mtkleinf3723212014-06-25 14:08:00 -0700577 } else {
mtklein5d9d10e2014-07-11 11:57:07 -0700578 SkDebugf("loops\tmin\tmedian\tmean\tmax\tstddev\tsamples\tconfig\tbench\n");
mtkleinf3723212014-06-25 14:08:00 -0700579 }
580
bsalomonc2553372014-07-22 13:09:05 -0700581 SkTDArray<Config> configs;
582 create_configs(&configs);
583
mtklein92007582014-08-01 07:46:52 -0700584 BenchmarkStream benchStream;
585 while (Benchmark* b = benchStream.next()) {
mtkleine714e752014-07-31 12:13:48 -0700586 SkAutoTDelete<Benchmark> bench(b);
mtkleinf3723212014-06-25 14:08:00 -0700587 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) {
588 continue;
589 }
590
mtkleinbb6a0282014-07-01 08:43:42 -0700591 SkTDArray<Target*> targets;
bsalomonc2553372014-07-22 13:09:05 -0700592 create_targets(&targets, bench.get(), configs);
mtkleinf3723212014-06-25 14:08:00 -0700593
jcgregoriobf5e5232014-07-17 13:14:16 -0700594 if (!targets.isEmpty()) {
595 log.bench(bench->getName(), bench->getSize().fX, bench->getSize().fY);
596 bench->preDraw();
597 }
mtkleinbb6a0282014-07-01 08:43:42 -0700598 for (int j = 0; j < targets.count(); j++) {
599 SkCanvas* canvas = targets[j]->surface.get() ? targets[j]->surface->getCanvas() : NULL;
bsalomonc2553372014-07-22 13:09:05 -0700600 const char* config = targets[j]->config.name;
mtkleinf3723212014-06-25 14:08:00 -0700601
mtklein92007582014-08-01 07:46:52 -0700602#if SK_DEBUG
603 // skia:2797 Some SKPs SkASSERT in debug mode. Skip them for now.
mtklein6e33e232014-08-01 08:23:39 -0700604 if (0 == strcmp("565", config) && SkStrContains(bench->getName(), ".skp")) {
605 SkDebugf("Skipping 565 %s. See skia:2797\n", bench->getName());
mtklein92007582014-08-01 07:46:52 -0700606 continue;
607 }
608#endif
609
mtkleinbb6a0282014-07-01 08:43:42 -0700610 const int loops =
611#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700612 Benchmark::kGPU_Backend == targets[j]->config.backend
mtkleinbb6a0282014-07-01 08:43:42 -0700613 ? gpu_bench(targets[j]->gl, bench.get(), canvas, samples.get())
614 :
615#endif
616 cpu_bench( overhead, bench.get(), canvas, samples.get());
mtkleinf3723212014-06-25 14:08:00 -0700617
bsalomon6eb03cc2014-08-07 14:28:50 -0700618 if (canvas && !FLAGS_writePath.isEmpty() && NULL != FLAGS_writePath[0]) {
619 SkString pngFilename = SkOSPath::Join(FLAGS_writePath[0], config);
620 pngFilename = SkOSPath::Join(pngFilename.c_str(), bench->getName());
621 pngFilename.append(".png");
622 write_canvas_png(canvas, pngFilename);
623 }
624
625 if (kFailedLoops == loops) {
mtklein2069e222014-08-04 13:57:39 -0700626 // Can't be timed. A warning note has already been printed.
Mike Kleine3631362014-07-15 17:56:37 -0400627 continue;
628 }
629
mtkleinf3723212014-06-25 14:08:00 -0700630 Stats stats(samples.get(), FLAGS_samples);
mtklein60317d0f2014-07-14 11:30:37 -0700631 log.config(config);
mtklein92007582014-08-01 07:46:52 -0700632 benchStream.fillCurrentOptions(&log);
jcgregoriobf5e5232014-07-17 13:14:16 -0700633#if SK_SUPPORT_GPU
bsalomonc2553372014-07-22 13:09:05 -0700634 if (Benchmark::kGPU_Backend == targets[j]->config.backend) {
jcgregoriobf5e5232014-07-17 13:14:16 -0700635 fill_gpu_options(&log, targets[j]->gl);
636 }
637#endif
mtklein60317d0f2014-07-14 11:30:37 -0700638 log.timer("min_ms", stats.min);
639 log.timer("median_ms", stats.median);
640 log.timer("mean_ms", stats.mean);
641 log.timer("max_ms", stats.max);
642 log.timer("stddev_ms", sqrt(stats.var));
643
bsalomon6eb03cc2014-08-07 14:28:50 -0700644 if (kAutoTuneLoops != FLAGS_loops) {
mtkleina189ccd2014-07-14 12:28:47 -0700645 if (targets.count() == 1) {
646 config = ""; // Only print the config if we run the same bench on more than one.
647 }
648 SkDebugf("%s\t%s\n", bench->getName(), config);
649 } else if (FLAGS_verbose) {
mtkleinf3723212014-06-25 14:08:00 -0700650 for (int i = 0; i < FLAGS_samples; i++) {
mtklein55b0ffc2014-07-17 08:38:23 -0700651 SkDebugf("%s ", HUMANIZE(samples[i]));
mtkleinf3723212014-06-25 14:08:00 -0700652 }
653 SkDebugf("%s\n", bench->getName());
654 } else if (FLAGS_quiet) {
mtkleinbb6a0282014-07-01 08:43:42 -0700655 if (targets.count() == 1) {
mtkleinf3723212014-06-25 14:08:00 -0700656 config = ""; // Only print the config if we run the same bench on more than one.
657 }
mtklein55b0ffc2014-07-17 08:38:23 -0700658 SkDebugf("%s\t%s\t%s\n", HUMANIZE(stats.median), bench->getName(), config);
mtkleinf3723212014-06-25 14:08:00 -0700659 } else {
660 const double stddev_percent = 100 * sqrt(stats.var) / stats.mean;
mtklein5d9d10e2014-07-11 11:57:07 -0700661 SkDebugf("%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n"
mtkleinf3723212014-06-25 14:08:00 -0700662 , loops
mtklein55b0ffc2014-07-17 08:38:23 -0700663 , HUMANIZE(stats.min)
664 , HUMANIZE(stats.median)
665 , HUMANIZE(stats.mean)
666 , HUMANIZE(stats.max)
mtkleinf3723212014-06-25 14:08:00 -0700667 , stddev_percent
mtklein5d9d10e2014-07-11 11:57:07 -0700668 , stats.plot.c_str()
mtkleinf3723212014-06-25 14:08:00 -0700669 , config
mtkleinbb6a0282014-07-01 08:43:42 -0700670 , bench->getName()
mtkleinf3723212014-06-25 14:08:00 -0700671 );
672 }
673 }
mtkleinbb6a0282014-07-01 08:43:42 -0700674 targets.deleteAll();
Mike Klein3944a1d2014-07-15 13:40:19 -0400675
676 #if SK_SUPPORT_GPU
bsalomon2354f842014-07-28 13:48:36 -0700677 if (FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700678 gGrFactory->abandonContexts();
bsalomon2354f842014-07-28 13:48:36 -0700679 }
680 if (FLAGS_resetGpuContext || FLAGS_abandonGpuContext) {
krajcevski69a55602014-08-13 10:46:31 -0700681 gGrFactory->destroyContexts();
Mike Klein3944a1d2014-07-15 13:40:19 -0400682 }
683 #endif
mtkleinf3723212014-06-25 14:08:00 -0700684 }
685
686 return 0;
687}
688
689#if !defined SK_BUILD_FOR_IOS
caryclark17f0b6d2014-07-22 10:15:34 -0700690int main(int argc, char** argv) {
691 SkCommandLineFlags::Parse(argc, argv);
692 return nanobench_main();
mtkleinf3723212014-06-25 14:08:00 -0700693}
694#endif