51becbde
Peter M. Groen
Committed the ent...
|
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/* Copyright (C) 2019
*
* This file is part of the osdev components suite
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifndef OSDEV_COMPONENTS_MQTT_MEASUREMENT_HISTOGRAM_H
#define OSDEV_COMPONENTS_MQTT_MEASUREMENT_HISTOGRAM_H
#include <atomic>
#include <cmath>
#include <limits>
#include <mutex>
#include <string>
#include <vector>
#include "utils.h"
#include "ihistogram.h"
namespace osdev {
namespace components {
namespace mqtt {
namespace measurement {
template <typename T>
double fabs(T val)
{
return std::fabs(val);
}
template <typename Rep, typename Dur>
double fabs(const std::chrono::duration<Rep, Dur>& dur)
{
return std::fabs(dur.count());
}
template <typename T>
double to_double(T val)
{
return static_cast<double>(val);
}
template <typename Rep, typename Dur>
double to_double(const std::chrono::duration<Rep, Dur>& dur)
{
return static_cast<double>(dur.count());
}
template <typename T>
std::string to_string(T val)
{
return std::to_string(val);
}
template <typename Rep, typename Dur>
std::string to_string(const std::chrono::duration<Rep, Dur>& dur)
{
return std::to_string(dur.count());
}
template <typename T>
T min(T)
{
return std::numeric_limits<T>::min();
}
template <typename Rep, typename Dur>
std::chrono::duration<Rep, Dur> min(const std::chrono::duration<Rep, Dur>&)
{
return std::chrono::duration<Rep, Dur>::min();
}
template <typename T>
T max(T)
{
return std::numeric_limits<T>::max();
}
template <typename Rep, typename Dur>
std::chrono::duration<Rep, Dur> max(const std::chrono::duration<Rep, Dur>&)
{
return std::chrono::duration<Rep, Dur>::max();
}
/**
* @brief This class implements a histogram for typed values.
* Outlier values to the left are counted on the first bin and to the right on the last bin.
* @tparam T The value type. T must be default constructable.
* @tparam Buckets The number of buckets to use for this histogram. Default is 10 buckets.
*/
template <typename T, std::size_t Buckets = 10>
class Histogram : public IHistogram
{
public:
/**
* @brief Construct a histogram.
* @param id The histogram identification
* @param minValue The minimum value of the histogram.
* @param maxValue The maxmim value of the histogram.
* @param unit The unit of the values that are collected in this histogram.
*/
Histogram(const std::string& id, T minValue, T maxValue, const char* unit);
/**
* @brief Set a value in the histogram.
*/
void setValue(T value);
/**
* @return true when values are added since the last time a snapshot was taken of the histogram or a reset was performed, false otherwise.
*/
bool isUpdated() const;
/**
* @brief Reset the histogram.
* All counts are made zero and the smallest and largest recorded values are set to default.
*/
void reset();
// IHistogram implementation
virtual const std::string& id() const override;
virtual std::size_t numBuckets() const override;
virtual double bucketWidth() const override;
virtual std::string minValueString() const override;
virtual std::string maxValueString() const override;
virtual const std::string& unit() const override;
virtual HistogramData histogramData() const override;
virtual std::size_t numberOfValuesSinceLastClear() const override;
virtual std::string smallestValueSinceLastClear() const override;
virtual std::string largestValueSinceLastClear() const override;
virtual std::string averageValueSinceLastClear() const override;
virtual void clearRunningValues() override;
virtual std::string toString() const override;
private:
/**
* @brief Get the index on which to count the given value.
* The outliers to the left are counted on index 0 and to the right on index size() - 1.
* @param value The value to get the index for.
* @return the index in the histogram on which to count the given value.
*/
inline std::size_t index(T value)
{
if (value > m_maxValue) {
return m_histogram.size() - 1;
}
if (value < m_minValue) {
return 0;
}
return 1 + static_cast<std::size_t>(to_double((value - m_minValue) / m_bucketWidth));
}
const std::string m_id;
const std::string m_unit;
const T m_minValue;
const T m_maxValue;
const double m_bucketWidth;
T m_smallestValue;
T m_largestValue;
T m_smallestValueSinceLastClear;
T m_largestValueSinceLastClear;
T m_summedValueSinceLastClear;
std::size_t m_numberOfValuesSinceLastClear;
mutable std::mutex m_mutex;
std::vector<std::size_t> m_histogram;
mutable std::atomic_bool m_isUpdated;
};
template <typename T, std::size_t Buckets>
Histogram<T, Buckets>::Histogram(const std::string& _id, T minValue, T maxValue, const char* _unit)
: m_id(_id)
, m_unit(_unit)
, m_minValue(minValue)
, m_maxValue(maxValue)
, m_bucketWidth(fabs((m_maxValue - m_minValue)) / Buckets)
, m_smallestValue{ max(maxValue) }
, m_largestValue{ min(maxValue) }
, m_smallestValueSinceLastClear{ max(maxValue) }
, m_largestValueSinceLastClear{ min(maxValue) }
, m_summedValueSinceLastClear{}
, m_numberOfValuesSinceLastClear{}
, m_mutex()
, m_histogram(Buckets + 2) // + 2 for the out of bound buckets
, m_isUpdated(false)
{
}
template <typename T, std::size_t Buckets>
void Histogram<T, Buckets>::setValue(T value)
{
std::lock_guard<std::mutex> lg(m_mutex);
apply_unused_parameters(lg);
if (value > m_largestValueSinceLastClear) {
m_largestValueSinceLastClear = value;
}
if (value < m_smallestValueSinceLastClear) {
m_smallestValueSinceLastClear = value;
}
if (value > m_largestValue) {
m_largestValue = value;
}
if (value < m_smallestValue) {
m_smallestValue = value;
}
m_summedValueSinceLastClear += value;
++m_numberOfValuesSinceLastClear;
m_histogram[this->index(value)]++;
m_isUpdated = true;
}
template <typename T, std::size_t Buckets>
bool Histogram<T, Buckets>::isUpdated() const
{
return m_isUpdated;
}
template <typename T, std::size_t Buckets>
void Histogram<T, Buckets>::reset()
{
std::lock_guard<std::mutex> lg(m_mutex);
apply_unused_parameters(lg);
m_smallestValue = T{ max(m_maxValue) };
m_largestValue = T{ min(m_maxValue) };
m_smallestValueSinceLastClear = T{ max(m_maxValue) };
m_largestValueSinceLastClear = T{ min(m_maxValue) };
m_summedValueSinceLastClear = T{};
m_numberOfValuesSinceLastClear = 0;
std::fill(m_histogram.begin(), m_histogram.end(), 0);
m_isUpdated = false;
}
template <typename T, std::size_t Buckets>
const std::string& Histogram<T, Buckets>::id() const
{
return m_id;
}
template <typename T, std::size_t Buckets>
std::size_t Histogram<T, Buckets>::numBuckets() const
{
return Buckets;
}
template <typename T, std::size_t Buckets>
double Histogram<T, Buckets>::bucketWidth() const
{
return m_bucketWidth;
}
template <typename T, std::size_t Buckets>
std::string Histogram<T, Buckets>::minValueString() const
{
return to_string(m_minValue);
}
template <typename T, std::size_t Buckets>
std::string Histogram<T, Buckets>::maxValueString() const
{
return to_string(m_maxValue);
}
template <typename T, std::size_t Buckets>
const std::string& Histogram<T, Buckets>::unit() const
{
return m_unit;
}
template <typename T, std::size_t Buckets>
HistogramData Histogram<T, Buckets>::histogramData() const
{
std::lock_guard<std::mutex> lg(m_mutex);
apply_unused_parameters(lg);
m_isUpdated = false;
return HistogramData(to_string(m_smallestValue), to_string(m_largestValue),
to_string(m_smallestValueSinceLastClear), to_string(m_largestValueSinceLastClear), averageValueSinceLastClear(),
m_numberOfValuesSinceLastClear, m_histogram);
}
template <typename T, std::size_t Buckets>
std::size_t Histogram<T, Buckets>::numberOfValuesSinceLastClear() const
{
return m_numberOfValuesSinceLastClear;
}
template <typename T, std::size_t Buckets>
std::string Histogram<T, Buckets>::smallestValueSinceLastClear() const
{
return to_string(m_smallestValueSinceLastClear);
}
template <typename T, std::size_t Buckets>
std::string Histogram<T, Buckets>::largestValueSinceLastClear() const
{
return to_string(m_largestValueSinceLastClear);
}
template <typename T, std::size_t Buckets>
std::string Histogram<T, Buckets>::averageValueSinceLastClear() const
{
if (0 == m_numberOfValuesSinceLastClear) {
return "undefined";
}
return to_string(to_double(m_summedValueSinceLastClear) / m_numberOfValuesSinceLastClear);
}
template <typename T, std::size_t Buckets>
void Histogram<T, Buckets>::clearRunningValues()
{
std::lock_guard<std::mutex> lg(m_mutex);
apply_unused_parameters(lg);
m_smallestValueSinceLastClear = T{ max(m_maxValue) };
m_largestValueSinceLastClear = T{ min(m_maxValue) };
m_summedValueSinceLastClear = T{};
m_numberOfValuesSinceLastClear = 0;
}
template <typename T, std::size_t Buckets>
std::string Histogram<T, Buckets>::toString() const
{
return visualizeHistogram(*this);
}
} // End namespace measurement
} // End namespace mqtt
} // End namespace components
} // End namespace osdev
#endif // OSDEV_COMPONENTS_MQTT_MEASUREMENT_HISTOGRAM_H
|