Skip to content

Commit 91d79c3

Browse files
Merge pull request mixxxdj#15843 from mixxxdj/sync-branch-2.6-to-main
Merge changes from `2.6` into `main`
2 parents 446b076 + 91a0755 commit 91d79c3

File tree

2 files changed

+96
-75
lines changed

2 files changed

+96
-75
lines changed

src/util/stat.cpp

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
#include <limits>
1+
#include "util/stat.h"
22

33
#include <QStringList>
4+
#include <chrono>
5+
#include <limits>
46

5-
#include "util/stat.h"
6-
#include "util/time.h"
77
#include "util/math.h"
88
#include "util/statsmanager.h"
9+
#include "util/time.h"
910

1011
Stat::Stat()
1112
: m_type(UNSPECIFIED),
@@ -20,18 +21,18 @@ Stat::Stat()
2021

2122
QString Stat::valueUnits() const {
2223
switch (m_type) {
23-
case DURATION_MSEC:
24-
return "ms";
25-
case DURATION_NANOSEC:
26-
return "ns";
27-
case DURATION_SEC:
28-
return "s";
29-
case EVENT:
30-
case EVENT_START:
31-
case EVENT_END:
32-
case UNSPECIFIED:
33-
default:
34-
return "";
24+
case DURATION_MSEC:
25+
return "ms";
26+
case DURATION_NANOSEC:
27+
return "ns";
28+
case DURATION_SEC:
29+
return "s";
30+
case EVENT:
31+
case EVENT_START:
32+
case EVENT_END:
33+
case UNSPECIFIED:
34+
default:
35+
return "";
3536
}
3637
}
3738

@@ -40,7 +41,7 @@ void Stat::processReport(const StatReport& report) {
4041
if (m_compute & (Stat::SUM | Stat::AVERAGE)) {
4142
m_sum += report.value;
4243
}
43-
if ((m_compute & Stat::MAX)&& report.value > m_max) {
44+
if ((m_compute & Stat::MAX) && report.value > m_max) {
4445
m_max = report.value;
4546
}
4647
if ((m_compute & Stat::MIN) && report.value < m_min) {
@@ -69,20 +70,41 @@ void Stat::processReport(const StatReport& report) {
6970
}
7071
}
7172

72-
QDebug operator<<(QDebug dbg, const Stat &stat) {
73+
QDebug operator<<(QDebug dbg, const Stat& stat) {
7374
QStringList stats;
75+
76+
auto formatTime = [](double ns) -> QString {
77+
using namespace std::chrono;
78+
79+
// Converting input to integral nanoseconds for safe duration_cast
80+
auto nanosecondsVal = duration_cast<nanoseconds>(duration<double, std::nano>(ns));
81+
82+
if (nanosecondsVal >= seconds(1)) {
83+
double sec = ns / 1e9;
84+
return QString::number(sec, 'f', 2) + " s";
85+
} else if (nanosecondsVal >= milliseconds(1)) {
86+
double ms = ns / 1e6;
87+
return QString::number(ms, 'f', 2) + " ms";
88+
} else if (nanosecondsVal >= microseconds(1)) {
89+
double us = ns / 1e3;
90+
return QString::number(us, 'f', 2) + " µs";
91+
} else {
92+
return QString::number(ns, 'f', 2) + " ns";
93+
}
94+
};
95+
7496
if (stat.m_compute & Stat::COUNT) {
7597
stats << "count=" + QString::number(stat.m_report_count);
7698
}
7799

78100
if (stat.m_compute & Stat::SUM) {
79-
stats << "sum=" + QString::number(stat.m_sum) + stat.valueUnits();
101+
stats << "sum=" + formatTime(stat.m_sum);
80102
}
81103

82104
if (stat.m_compute & Stat::AVERAGE) {
83105
QString value = "average=";
84106
if (stat.m_report_count > 0) {
85-
value += QString::number(stat.m_sum / stat.m_report_count) + stat.valueUnits();
107+
value += formatTime(stat.m_sum / stat.m_report_count);
86108
} else {
87109
value += "XXX";
88110
}
@@ -92,7 +114,7 @@ QDebug operator<<(QDebug dbg, const Stat &stat) {
92114
if (stat.m_compute & Stat::MIN) {
93115
QString value = "min=";
94116
if (stat.m_report_count > 0) {
95-
value += QString::number(stat.m_min) + stat.valueUnits();
117+
value += formatTime(stat.m_min);
96118
} else {
97119
value += "XXX";
98120
}
@@ -102,7 +124,7 @@ QDebug operator<<(QDebug dbg, const Stat &stat) {
102124
if (stat.m_compute & Stat::MAX) {
103125
QString value = "max=";
104126
if (stat.m_report_count > 0) {
105-
value += QString::number(stat.m_max) + stat.valueUnits();
127+
value += formatTime(stat.m_max);
106128
} else {
107129
value += "XXX";
108130
}
@@ -111,9 +133,9 @@ QDebug operator<<(QDebug dbg, const Stat &stat) {
111133

112134
if (stat.m_compute & Stat::SAMPLE_VARIANCE) {
113135
double variance = stat.variance();
114-
stats << "variance=" + QString::number(variance) + stat.valueUnits() + "^2";
136+
stats << "variance=" + formatTime(variance) + "^2";
115137
if (variance >= 0.0) {
116-
stats << "stddev=" + QString::number(sqrt(variance)) + stat.valueUnits();
138+
stats << "stddev=" + formatTime(sqrt(variance));
117139
}
118140
}
119141

@@ -123,10 +145,9 @@ QDebug operator<<(QDebug dbg, const Stat &stat) {
123145

124146
if (stat.m_compute & Stat::HISTOGRAM) {
125147
QStringList histogram;
126-
for (auto it = stat.m_histogram.constBegin();
127-
it != stat.m_histogram.constEnd(); ++it) {
128-
histogram << QString::number(it.key()) + stat.valueUnits() + ":" +
129-
QString::number(it.value());
148+
for (auto it = stat.m_histogram.constBegin(); it != stat.m_histogram.constEnd(); ++it) {
149+
histogram << formatTime(static_cast<double>(it.key())) + ":" +
150+
QString::number(it.value());
130151
}
131152
stats << "histogram=" + histogram.join(",");
132153
}
@@ -137,9 +158,9 @@ QDebug operator<<(QDebug dbg, const Stat &stat) {
137158

138159
// static
139160
bool Stat::track(QString tag,
140-
Stat::StatType type,
141-
Stat::ComputeFlags compute,
142-
double value) {
161+
Stat::StatType type,
162+
Stat::ComputeFlags compute,
163+
double value) {
143164
if (!StatsManager::s_bStatsManagerEnabled) {
144165
return false;
145166
}

src/util/stat.h

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

33
#include <QMap>
4-
#include <QVector>
54
#include <QString>
5+
#include <QVector>
66

77
#include "util/experiment.h"
88

@@ -23,58 +23,58 @@ class Stat {
2323

2424
static QString statTypeToString(StatType type) {
2525
switch (type) {
26-
case UNSPECIFIED:
27-
return "UNSPECIFIED";
28-
case COUNTER:
29-
return "COUNTER";
30-
case DURATION_MSEC:
31-
return "DURATION_MSEC";
32-
case DURATION_NANOSEC:
33-
return "DURATION_NANOSEC";
34-
case DURATION_SEC:
35-
return "DURATION_SEC";
36-
case EVENT:
37-
return "EVENT";
38-
case EVENT_START:
39-
return "START";
40-
case EVENT_END:
41-
return "END";
42-
default:
43-
return "UNKNOWN";
26+
case UNSPECIFIED:
27+
return "UNSPECIFIED";
28+
case COUNTER:
29+
return "COUNTER";
30+
case DURATION_MSEC:
31+
return "DURATION_MSEC";
32+
case DURATION_NANOSEC:
33+
return "DURATION_NANOSEC";
34+
case DURATION_SEC:
35+
return "DURATION_SEC";
36+
case EVENT:
37+
return "EVENT";
38+
case EVENT_START:
39+
return "START";
40+
case EVENT_END:
41+
return "END";
42+
default:
43+
return "UNKNOWN";
4444
}
4545
}
4646

4747
// TODO make this an enum class for improved typesafety and issues with
4848
// ambiguous overloads.
4949
enum ComputeTypes {
50-
NONE = 0x0000,
50+
NONE = 0x0000,
5151
// O(1) in time and space.
52-
COUNT = 0x0001,
52+
COUNT = 0x0001,
5353
// O(1) in time and space
54-
SUM = 0x0002,
54+
SUM = 0x0002,
5555
// O(1) in time and space.
56-
AVERAGE = 0x0004,
56+
AVERAGE = 0x0004,
5757
// O(1) in time and space.
5858
SAMPLE_VARIANCE = 0x0008,
59-
SAMPLE_MEDIAN = 0x0010,
59+
SAMPLE_MEDIAN = 0x0010,
6060
// O(1) in time and space.
61-
MIN = 0x0020,
61+
MIN = 0x0020,
6262
// O(1) in time and space.
63-
MAX = 0x0040,
63+
MAX = 0x0040,
6464
// O(1) in time, O(k) in space where k is # of distinct values.
6565
// Use carefully!
66-
HISTOGRAM = 0x0080,
66+
HISTOGRAM = 0x0080,
6767
// O(1) in time, O(n) in space where n is the # of reports.
6868
// Use carefully!
69-
VALUES = 0x0100,
69+
VALUES = 0x0100,
7070
// TODO, track average reports per second
7171
REPORTS_PER_SECOND = 0x0200,
7272
// TODO, track the time in between received reports.
7373
REPORT_TIME_DELTA = 0x0400,
7474
// Used for marking stats recorded in EXPERIMENT mode.
75-
STATS_EXPERIMENT = 0x0800,
75+
STATS_EXPERIMENT = 0x0800,
7676
// Used for marking stats recorded in BASE mode.
77-
STATS_BASE = 0x1000,
77+
STATS_BASE = 0x1000,
7878
};
7979
Q_DECLARE_FLAGS(ComputeFlags, ComputeTypes);
8080

@@ -89,13 +89,13 @@ class Stat {
8989

9090
static ComputeFlags experimentFlags(ComputeFlags flags) {
9191
switch (Experiment::mode()) {
92-
case Experiment::EXPERIMENT:
93-
return flags | STATS_EXPERIMENT;
94-
case Experiment::BASE:
95-
return flags | STATS_BASE;
96-
default:
97-
case Experiment::OFF:
98-
return flags;
92+
case Experiment::EXPERIMENT:
93+
return flags | STATS_EXPERIMENT;
94+
case Experiment::BASE:
95+
return flags | STATS_BASE;
96+
default:
97+
case Experiment::OFF:
98+
return flags;
9999
}
100100
}
101101

@@ -120,23 +120,23 @@ class Stat {
120120
QMap<double, double> m_histogram;
121121

122122
static bool track(QString tag,
123-
Stat::StatType type,
124-
Stat::ComputeFlags compute,
125-
double value);
123+
Stat::StatType type,
124+
Stat::ComputeFlags compute,
125+
double value);
126126

127127
// Disallow to use this class with implicit converted char strings.
128128
// This should not be uses to avoid unicode encoding and memory
129129
// allocation at every call. Use a static tag like this:
130130
// static const QString tag("TAG TEXT");
131-
static bool track(const char *,
132-
Stat::StatType,
133-
Stat::ComputeFlags,
134-
double) = delete;
131+
static bool track(const char*,
132+
Stat::StatType,
133+
Stat::ComputeFlags,
134+
double) = delete;
135135
};
136136

137137
Q_DECLARE_OPERATORS_FOR_FLAGS(Stat::ComputeFlags);
138138

139-
QDebug operator<<(QDebug dbg, const Stat &stat);
139+
QDebug operator<<(QDebug dbg, const Stat& stat);
140140

141141
struct StatReport {
142142
QString tag;

0 commit comments

Comments
 (0)