summaryrefslogtreecommitdiffstats
path: root/src/util/timer.cpp
blob: e9160652282ca7809eaccdbf4e3aea273ed7838f (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "util/timer.h"

#include "util/experiment.h"
#include "util/time.h"
#include "waveform/guitick.h"

Timer::Timer(const QString& key, Stat::ComputeFlags compute)
        : m_key(key),
          m_compute(Stat::experimentFlags(compute)),
          m_running(false) {
}

void Timer::start() {
    m_running = true;
    m_time.start();
}

mixxx::Duration Timer::restart(bool report) {
    if (m_running) {
        mixxx::Duration elapsed = m_time.restart();
        if (report) {
            // Ignore the report if it crosses the experiment boundary.
            Experiment::Mode oldMode = Stat::modeFromFlags(m_compute);
            if (oldMode == Experiment::mode()) {
                Stat::track(m_key, Stat::DURATION_NANOSEC, m_compute,
                            elapsed.toIntegerNanos());
            }
        }
        return elapsed;
    } else {
        start();
        return mixxx::Duration::fromNanos(0);
    }
}

mixxx::Duration Timer::elapsed(bool report) {
    mixxx::Duration elapsedTime = m_time.elapsed();
    if (report) {
        // Ignore the report if it crosses the experiment boundary.
        Experiment::Mode oldMode = Stat::modeFromFlags(m_compute);
        if (oldMode == Experiment::mode()) {
            Stat::track(m_key, Stat::DURATION_NANOSEC, m_compute,
                        elapsedTime.toIntegerNanos());
        }
    }
    return elapsedTime;
}


SuspendableTimer::SuspendableTimer(const QString& key,
                                   Stat::ComputeFlags compute)
        : Timer(key, compute) {
}

void SuspendableTimer::start() {
    m_leapTime = mixxx::Duration::fromSeconds(0);
    Timer::start();
}

mixxx::Duration SuspendableTimer::suspend() {
    m_leapTime += m_time.elapsed();
    m_running = false;
    return m_leapTime;
}

void SuspendableTimer::go() {
    Timer::start();
}

mixxx::Duration SuspendableTimer::elapsed(bool report) {
    m_leapTime += m_time.elapsed();
    if (report) {
        // Ignore the report if it crosses the experiment boundary.
        Experiment::Mode oldMode = Stat::modeFromFlags(m_compute);
        if (oldMode == Experiment::mode()) {
            Stat::track(m_key, Stat::DURATION_NANOSEC, m_compute,
                        m_leapTime.toIntegerNanos());
        }
    }
    return m_leapTime;
}

GuiTickTimer::GuiTickTimer(QObject* pParent)
        : QObject(pParent),
          m_pGuiTick(make_parented<ControlProxy>(
              "[Master]", "guiTickTime", this)),
          m_bActive(false) {
}

void GuiTickTimer::start(mixxx::Duration duration) {
    m_pGuiTick->connectValueChanged(SLOT(slotGuiTick(double)));
    m_interval = duration;
    m_lastUpdate = mixxx::Duration::fromSeconds(0);
    m_bActive = true;
}

void GuiTickTimer::stop() {
    m_pGuiTick->disconnect();
    m_bActive = false;
    m_interval = mixxx::Duration::fromSeconds(0);
    m_lastUpdate = mixxx::Duration::fromSeconds(0);
}

void GuiTickTimer::slotGuiTick(double) {
    if (m_bActive) {
        auto time = mixxx::Time::elapsed();
        if (time - m_lastUpdate >= m_interval) {
            m_lastUpdate = time;
            emit(timeout());
        }
    }
}