aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/VisualBench/TimingStateMachine.cpp
blob: 00e0b618e740d10c2cf94d768de90e57d9c8a059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "TimingStateMachine.h"

#include "SkCanvas.h"
#include "SkCommandLineFlags.h"

DEFINE_int32(gpuFrameLag, 5, "Overestimate of maximum number of frames GPU is allowed to lag.");
DEFINE_int32(frames, 5, "Number of frames of each skp to render per sample.");
DEFINE_double(loopMs, 5, "Each benchmark will be tuned until it takes loopsMs millseconds.");

static double now_ms() { return SkTime::GetNSecs() * 1e-6; }

TimingStateMachine::TimingStateMachine()
    : fCurrentFrame(0)
    , fLoops(1)
    , fLastMeasurement(0.)
    , fState(kPreWarm_State)
    , fInnerState(kTuning_InnerState) {
}

TimingStateMachine::ParentEvents TimingStateMachine::nextFrame(bool preWarmBetweenSamples) {
    ParentEvents parentEvent = kTiming_ParentEvents;
    switch (fState) {
        case kPreWarm_State: {
            if (fCurrentFrame >= FLAGS_gpuFrameLag) {
                fCurrentFrame = 0;
                fStartTime = now_ms();
                fState = kTiming_State;
            } else {
                fCurrentFrame++;
            }
            break;
        }
        case kTiming_State: {
            switch (fInnerState) {
                case kTuning_InnerState: {
                    if (1 << 30 == fLoops) {
                        // We're about to wrap.  Something's wrong with the bench.
                        SkDebugf("InnerLoops wrapped\n");
                        fLoops = 1;
                    } else {
                        double elapsedMs = now_ms() - fStartTime;
                        if (elapsedMs < FLAGS_loopMs) {
                            fLoops *= 2;
                        } else {
                            fInnerState = kTiming_InnerState;
                        }
                        fState = kPreWarm_State;
                        fCurrentFrame = 0;
                        parentEvent = kReset_ParentEvents;
                    }
                    break;
                }
                case kTiming_InnerState: {
                    if (fCurrentFrame >= FLAGS_frames) {
                        double now = now_ms();
                        fLastMeasurement = (now - fStartTime) / (FLAGS_frames * fLoops);
                        fCurrentFrame = 0;
                        parentEvent = kTimingFinished_ParentEvents;
                        if (preWarmBetweenSamples) {
                            fState = kPreWarm_State;
                        } else {
                            fStartTime = now;
                        }
                    } else {
                        fCurrentFrame++;
                    }
                    break;
                }
            }
        }
        break;
    }
    return parentEvent;
}

void TimingStateMachine::nextBenchmark() {
    fLoops = 1;
    fInnerState = kTuning_InnerState;
    fState = kPreWarm_State;
}