blob: 00e0b618e740d10c2cf94d768de90e57d9c8a059 [file] [log] [blame]
joshualitt98d2e2f2015-10-05 07:23:30 -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#include "TimingStateMachine.h"
9
10#include "SkCanvas.h"
11#include "SkCommandLineFlags.h"
12
13DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU is allowed to lag.");
14DEFINE_int32(frames, 5, "Number of frames of each skp to render per sample.");
15DEFINE_double(loopMs, 5, "Each benchmark will be tuned until it takes loopsMs millseconds.");
16
mtkleinf27f08b2015-11-03 06:54:24 -080017static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
18
joshualitt98d2e2f2015-10-05 07:23:30 -070019TimingStateMachine::TimingStateMachine()
20 : fCurrentFrame(0)
21 , fLoops(1)
22 , fLastMeasurement(0.)
joshualittcb54e8e2015-10-05 13:58:26 -070023 , fState(kPreWarm_State)
24 , fInnerState(kTuning_InnerState) {
joshualitt98d2e2f2015-10-05 07:23:30 -070025}
26
joshualittcb54e8e2015-10-05 13:58:26 -070027TimingStateMachine::ParentEvents TimingStateMachine::nextFrame(bool preWarmBetweenSamples) {
28 ParentEvents parentEvent = kTiming_ParentEvents;
joshualitt98d2e2f2015-10-05 07:23:30 -070029 switch (fState) {
joshualittcb54e8e2015-10-05 13:58:26 -070030 case kPreWarm_State: {
31 if (fCurrentFrame >= FLAGS_gpuFrameLag) {
32 fCurrentFrame = 0;
mtkleinf27f08b2015-11-03 06:54:24 -080033 fStartTime = now_ms();
joshualittcb54e8e2015-10-05 13:58:26 -070034 fState = kTiming_State;
35 } else {
36 fCurrentFrame++;
37 }
38 break;
39 }
40 case kTiming_State: {
41 switch (fInnerState) {
42 case kTuning_InnerState: {
43 if (1 << 30 == fLoops) {
44 // We're about to wrap. Something's wrong with the bench.
45 SkDebugf("InnerLoops wrapped\n");
46 fLoops = 1;
47 } else {
cdalton10237252015-11-13 08:42:06 -080048 double elapsedMs = now_ms() - fStartTime;
joshualittcb54e8e2015-10-05 13:58:26 -070049 if (elapsedMs < FLAGS_loopMs) {
50 fLoops *= 2;
51 } else {
52 fInnerState = kTiming_InnerState;
joshualittcb54e8e2015-10-05 13:58:26 -070053 }
joshualitte1f82962015-10-06 08:25:15 -070054 fState = kPreWarm_State;
cdalton10237252015-11-13 08:42:06 -080055 fCurrentFrame = 0;
joshualittcb54e8e2015-10-05 13:58:26 -070056 parentEvent = kReset_ParentEvents;
57 }
58 break;
59 }
60 case kTiming_InnerState: {
61 if (fCurrentFrame >= FLAGS_frames) {
cdalton10237252015-11-13 08:42:06 -080062 double now = now_ms();
63 fLastMeasurement = (now - fStartTime) / (FLAGS_frames * fLoops);
64 fCurrentFrame = 0;
joshualittcb54e8e2015-10-05 13:58:26 -070065 parentEvent = kTimingFinished_ParentEvents;
66 if (preWarmBetweenSamples) {
67 fState = kPreWarm_State;
68 } else {
cdalton10237252015-11-13 08:42:06 -080069 fStartTime = now;
joshualittcb54e8e2015-10-05 13:58:26 -070070 }
71 } else {
72 fCurrentFrame++;
73 }
74 break;
75 }
76 }
77 }
78 break;
joshualitt98d2e2f2015-10-05 07:23:30 -070079 }
joshualittcb54e8e2015-10-05 13:58:26 -070080 return parentEvent;
joshualitt98d2e2f2015-10-05 07:23:30 -070081}
82
joshualittc603c142015-10-15 07:18:29 -070083void TimingStateMachine::nextBenchmark() {
joshualittdc5db592015-10-05 13:24:55 -070084 fLoops = 1;
joshualittcb54e8e2015-10-05 13:58:26 -070085 fInnerState = kTuning_InnerState;
86 fState = kPreWarm_State;
joshualittdc5db592015-10-05 13:24:55 -070087}