Changeset View
Changeset View
Standalone View
Standalone View
libs/global/kis_latency_tracker.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2017 Bernhard Liebl <poke1024@gmx.de> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #ifndef KRITA_KIS_SCALAR_TRACKER_H | ||||
20 | #define KRITA_KIS_SCALAR_TRACKER_H | ||||
21 | | ||||
22 | #include "kis_shared.h" | ||||
23 | #include <kritaglobal_export.h> | ||||
24 | | ||||
25 | #include <QQueue> | ||||
26 | #include <QElapsedTimer> | ||||
27 | #include <QDebug> | ||||
28 | | ||||
29 | #include <boost/heap/fibonacci_heap.hpp> | ||||
30 | | ||||
31 | #include <boost/accumulators/accumulators.hpp> | ||||
32 | #include <boost/accumulators/statistics/stats.hpp> | ||||
33 | #include <boost/accumulators/statistics/rolling_mean.hpp> | ||||
34 | #include <boost/accumulators/statistics/rolling_variance.hpp> | ||||
35 | | ||||
36 | template<typename T> | ||||
37 | class KisRollingMax { | ||||
38 | public: | ||||
39 | KisRollingMax(int windowSize) : m_windowSize(windowSize) { | ||||
40 | } | ||||
41 | | ||||
42 | void push(T value) { | ||||
43 | while (m_samples.size() > m_windowSize) { | ||||
44 | m_values.erase(m_samples.dequeue()); | ||||
45 | } | ||||
46 | | ||||
47 | m_samples.enqueue(m_values.push(value)); | ||||
48 | } | ||||
49 | | ||||
50 | T max() const { | ||||
51 | if (m_values.empty()) { | ||||
52 | throw std::runtime_error("no values to get max of"); | ||||
53 | } else { | ||||
54 | return m_values.top(); | ||||
55 | } | ||||
56 | } | ||||
57 | | ||||
58 | private: | ||||
59 | const int m_windowSize; | ||||
60 | | ||||
61 | typedef boost::heap::fibonacci_heap<T> heap_type; | ||||
62 | | ||||
63 | QQueue<typename heap_type::handle_type> m_samples; | ||||
64 | heap_type m_values; | ||||
65 | }; | ||||
66 | | ||||
67 | template<typename T> | ||||
68 | class KisScalarTracker : public KisShared { | ||||
69 | public: | ||||
70 | /** | ||||
71 | * Create a tracker with the given window size. | ||||
72 | * @param window The maximum number of elements to take into account for calculation | ||||
73 | * of max, mean and variance values. | ||||
74 | */ | ||||
75 | KisScalarTracker(const QString &name, int windowSize = 500) : | ||||
76 | m_name(name), | ||||
77 | m_windowSize(windowSize), | ||||
78 | m_addCount(0), | ||||
79 | m_max(windowSize), | ||||
80 | m_acc(boost::accumulators::tag::rolling_window::window_size = windowSize) | ||||
81 | { | ||||
82 | m_printTimer.start(); | ||||
83 | } | ||||
84 | | ||||
85 | virtual ~KisScalarTracker() { | ||||
86 | } | ||||
87 | | ||||
88 | /** | ||||
89 | * Add a scalar. | ||||
90 | * @param value the scalar to be added. | ||||
91 | */ | ||||
92 | virtual void push(T value) { | ||||
93 | m_max.push(value); | ||||
94 | m_acc(value); | ||||
95 | m_addCount++; | ||||
96 | | ||||
97 | if (m_addCount >= m_windowSize || m_printTimer.elapsed() >= 1000) { | ||||
98 | m_printTimer.restart(); | ||||
99 | QString s = format(boost::accumulators::rolling_mean(m_acc), | ||||
100 | boost::accumulators::rolling_variance(m_acc), | ||||
101 | m_max.max()); | ||||
102 | print(s); | ||||
103 | m_addCount = 0; | ||||
104 | } | ||||
105 | | ||||
106 | } | ||||
107 | | ||||
108 | protected: | ||||
109 | /** | ||||
110 | * Print out a message. | ||||
111 | * @param message the message to print | ||||
112 | */ | ||||
113 | virtual void print(const QString &message) { | ||||
114 | qInfo() << qUtf8Printable(message); | ||||
115 | } | ||||
116 | | ||||
117 | /** | ||||
118 | * Formats a message for printing. | ||||
119 | * @param mean the mean scalar in the window | ||||
120 | * @param variance the variance of the scalar in the window | ||||
121 | * @param max the max scalar in the window | ||||
122 | */ | ||||
123 | virtual QString format(qint64 mean, qint64 variance, qint64 max) { | ||||
124 | return QString("%1: mean %2 ms, var %3, max %4 ms").arg(m_name).arg(mean).arg(variance).arg(max); | ||||
125 | } | ||||
126 | | ||||
127 | private: | ||||
128 | const QString m_name; | ||||
129 | const int m_windowSize; | ||||
130 | int m_addCount; | ||||
131 | | ||||
132 | QElapsedTimer m_printTimer; | ||||
133 | | ||||
134 | KisRollingMax<T> m_max; | ||||
135 | | ||||
136 | // see https://svn.boost.org/trac10/ticket/11437 | ||||
137 | typedef boost::accumulators::stats< | ||||
138 | boost::accumulators::tag::lazy_rolling_mean, | ||||
139 | boost::accumulators::tag::rolling_variance> stats; | ||||
140 | | ||||
141 | boost::accumulators::accumulator_set<T, stats> m_acc; | ||||
142 | }; | ||||
143 | | ||||
144 | /** | ||||
145 | * KisLatencyTracker tracks the time it takes events to reach a certain point in the program. | ||||
146 | */ | ||||
147 | | ||||
148 | class KRITAGLOBAL_EXPORT KisLatencyTracker : public KisScalarTracker<qint64> { | ||||
149 | public: | ||||
150 | /** | ||||
151 | * Create a tracker with the given window size. | ||||
152 | * @param window The maximum number of elements to take into account for calculation | ||||
153 | * of max, mean and variance values. | ||||
154 | */ | ||||
155 | KisLatencyTracker(int windowSize = 500); | ||||
156 | | ||||
157 | /** | ||||
158 | * Register that an event with the given timestamp has arrived just now. | ||||
159 | * @param timestamp Timestamp of the event that just arrived (the difference to the | ||||
160 | * current time is the latency). | ||||
161 | */ | ||||
162 | virtual void push(qint64 timestamp); | ||||
163 | | ||||
164 | protected: | ||||
165 | /** | ||||
166 | * @return The timestamp of "right now" in a frame that is comparable to those | ||||
167 | * timestamps given to push(). | ||||
168 | */ | ||||
169 | virtual qint64 currentTimestamp() const = 0; | ||||
170 | }; | ||||
171 | | ||||
172 | #endif //KRITA_KIS_SCALAR_TRACKER_H |