Rise
The Vieneo Province
Toggle main menu visibility
Loading...
Searching...
No Matches
TelemetryUploadCadence.cpp
Go to the documentation of this file.
1
#include "
TelemetryUploadCadence.h
"
2
3
#include <algorithm>
4
#include <cmath>
5
6
namespace
Telemetry
7
{
8
UploadDiagnostics
gUploadDiagnostics
;
9
10
PeriodicGate::PeriodicGate
(
double
intervalSeconds) :
11
intervalSeconds_
(std::isfinite(intervalSeconds) && intervalSeconds > 0.0
12
? intervalSeconds
13
:
ClientUploadIntervalSeconds
)
14
{
15
}
16
17
bool
PeriodicGate::Advance
(
double
elapsedSeconds)
18
{
19
if
(!std::isfinite(elapsedSeconds) || elapsedSeconds <= 0.0)
20
return
false
;
21
22
accumulatedSeconds_
+= elapsedSeconds;
23
if
(
accumulatedSeconds_
<
intervalSeconds_
)
24
return
false
;
25
26
// Preserve the fractional remainder for stable average cadence while
27
// coalescing a long stall into one event. Subtracting whole intervals is
28
// more stable than fmod for values such as 2.0 % 0.05.
29
const
double
elapsedIntervals = std::floor(
accumulatedSeconds_
/
intervalSeconds_
+ 1.0e-9);
30
accumulatedSeconds_
-= elapsedIntervals *
intervalSeconds_
;
31
if
(
accumulatedSeconds_ < 0.0 || accumulatedSeconds_ >
=
intervalSeconds_
)
32
accumulatedSeconds_
= 0.0;
33
return
true
;
34
}
35
36
void
PeriodicGate::Reset
()
37
{
38
accumulatedSeconds_
= 0.0;
39
}
40
41
double
PeriodicGate::IntervalSeconds
()
const
42
{
43
return
intervalSeconds_
;
44
}
45
46
double
PeriodicGate::AccumulatedSeconds
()
const
47
{
48
return
accumulatedSeconds_
;
49
}
50
51
void
UploadDiagnostics::Reset
()
52
{
53
metrics_
= {};
54
packetWindowStartSeconds_
= 0.0;
55
lastPacketSeconds_
= 0.0;
56
packetWindowCount_
= 0;
57
}
58
59
void
UploadDiagnostics::RecordPacket
(
double
nowSeconds)
60
{
61
if
(!std::isfinite(nowSeconds) || nowSeconds < 0.0)
62
return
;
63
64
lastPacketSeconds_
= nowSeconds;
65
metrics_
.
hasPacket
=
true
;
66
++
metrics_
.
totalPackets
;
67
68
if
(
packetWindowCount_
== 0)
69
{
70
packetWindowStartSeconds_
= nowSeconds;
71
packetWindowCount_
= 1;
72
return
;
73
}
74
75
++
packetWindowCount_
;
76
const
double
elapsed = nowSeconds -
packetWindowStartSeconds_
;
77
if
(elapsed >= 0.5)
78
{
79
metrics_
.
packetsPerSecond
=
static_cast<
double
>
(
packetWindowCount_
- 1) / elapsed;
80
packetWindowStartSeconds_
= nowSeconds;
81
packetWindowCount_
= 1;
82
}
83
}
84
85
UploadMetrics
UploadDiagnostics::GetMetrics
(
double
nowSeconds)
const
86
{
87
UploadMetrics
result =
metrics_
;
88
if
(result.
hasPacket
&& std::isfinite(nowSeconds))
89
result.
ageOfLastPacketSeconds
= std::max(0.0, nowSeconds -
lastPacketSeconds_
);
90
return
result;
91
}
92
}
TelemetryUploadCadence.h
Telemetry::PeriodicGate::IntervalSeconds
double IntervalSeconds() const
Definition
TelemetryUploadCadence.cpp:41
Telemetry::PeriodicGate::PeriodicGate
PeriodicGate(double intervalSeconds)
Definition
TelemetryUploadCadence.cpp:10
Telemetry::PeriodicGate::AccumulatedSeconds
double AccumulatedSeconds() const
Definition
TelemetryUploadCadence.cpp:46
Telemetry::PeriodicGate::accumulatedSeconds_
double accumulatedSeconds_
Definition
TelemetryUploadCadence.h:24
Telemetry::PeriodicGate::intervalSeconds_
double intervalSeconds_
Definition
TelemetryUploadCadence.h:23
Telemetry::PeriodicGate::Reset
void Reset()
Definition
TelemetryUploadCadence.cpp:36
Telemetry::PeriodicGate::Advance
bool Advance(double elapsedSeconds)
Definition
TelemetryUploadCadence.cpp:17
Telemetry::UploadDiagnostics
Definition
TelemetryUploadCadence.h:37
Telemetry::UploadDiagnostics::GetMetrics
UploadMetrics GetMetrics(double nowSeconds) const
Definition
TelemetryUploadCadence.cpp:85
Telemetry::UploadDiagnostics::RecordPacket
void RecordPacket(double nowSeconds)
Definition
TelemetryUploadCadence.cpp:59
Telemetry::UploadDiagnostics::packetWindowStartSeconds_
double packetWindowStartSeconds_
Definition
TelemetryUploadCadence.h:45
Telemetry::UploadDiagnostics::Reset
void Reset()
Definition
TelemetryUploadCadence.cpp:51
Telemetry::UploadDiagnostics::lastPacketSeconds_
double lastPacketSeconds_
Definition
TelemetryUploadCadence.h:46
Telemetry::UploadDiagnostics::packetWindowCount_
std::uint32_t packetWindowCount_
Definition
TelemetryUploadCadence.h:47
Telemetry::UploadDiagnostics::metrics_
UploadMetrics metrics_
Definition
TelemetryUploadCadence.h:44
Telemetry
Definition
TelemetrySnapshot.cpp:83
Telemetry::gUploadDiagnostics
UploadDiagnostics gUploadDiagnostics
Definition
TelemetryUploadCadence.cpp:8
Telemetry::ClientUploadIntervalSeconds
constexpr double ClientUploadIntervalSeconds
Definition
TelemetryUploadCadence.h:7
Telemetry::UploadMetrics
Definition
TelemetryUploadCadence.h:28
Telemetry::UploadMetrics::packetsPerSecond
double packetsPerSecond
Definition
TelemetryUploadCadence.h:29
Telemetry::UploadMetrics::ageOfLastPacketSeconds
double ageOfLastPacketSeconds
Definition
TelemetryUploadCadence.h:31
Telemetry::UploadMetrics::totalPackets
std::uint64_t totalPackets
Definition
TelemetryUploadCadence.h:32
Telemetry::UploadMetrics::hasPacket
bool hasPacket
Definition
TelemetryUploadCadence.h:33
TelemetryUploadCadence.cpp
Generated on
for Rise by
1.18.0