xrpld
Loading...
Searching...
No Matches
LoadMonitor.cpp
1#include <xrpl/core/LoadMonitor.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/UptimeClock.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/core/LoadEvent.h>
7
8#include <chrono>
9#include <mutex>
10
11namespace xrpl {
12
13/*
14
15TODO
16----
17
18- Use Journal for logging
19
20*/
21
22//------------------------------------------------------------------------------
23
27
28//------------------------------------------------------------------------------
29
39
40// VFALCO NOTE WHY do we need "the mutex?" This dependence on
41// a hidden global, especially a synchronization primitive,
42// is a flawed design.
43// It's not clear exactly which data needs to be protected.
44//
45// call with the mutex
46void
48{
49 using namespace std::chrono_literals;
50 auto now = UptimeClock::now();
51 if (now == lastUpdate_) // current
52 return;
53
54 // VFALCO TODO Why 8?
55 if ((now < lastUpdate_) || (now > (lastUpdate_ + 8s)))
56 {
57 // way out of date
58 counts_ = 0;
60 latencyMSAvg_ = 0ms;
61 latencyMSPeak_ = 0ms;
62 lastUpdate_ = now;
63 return;
64 }
65
66 // do exponential decay
67 /*
68 David:
69
70 "Imagine if you add 10 to something every second. And you
71 also reduce it by 1/4 every second. It will "idle" at 40,
72 corresponding to 10 counts per second."
73 */
74 do
75 {
76 lastUpdate_ += 1s;
77 counts_ -= ((counts_ + 3) / 4);
78 latencyEvents_ -= ((latencyEvents_ + 3) / 4);
81 } while (lastUpdate_ < now);
82}
83
84void
86{
87 using namespace std::chrono;
88
89 auto const total = s.runTime() + s.waitTime();
90 // Don't include "jitter" as part of the latency
91 auto const latency = total < 2ms ? 0ms : round<milliseconds>(total);
92
93 if (latency > 500ms)
94 {
95 auto mj = (latency > 1s) ? j_.warn() : j_.info();
96 JLOG(mj) << "Job: " << s.name() << " run: " << round<milliseconds>(s.runTime()).count()
97 << "ms"
98 << " wait: " << round<milliseconds>(s.waitTime()).count() << "ms";
99 }
100
101 addSamples(1, latency);
102}
103
104/* Add multiple samples
105 @param count The number of samples to add
106 @param latencyMS The total number of milliseconds
107*/
108void
110{
111 std::scoped_lock const sl(mutex_);
112
113 update();
114 counts_ += count;
116 latencyMSAvg_ += latency;
117 latencyMSPeak_ += latency;
118
119 auto const latencyPeak = latencyEvents_ * latency * 4 / count;
120
121 if (latencyMSPeak_ < latencyPeak)
122 latencyMSPeak_ = latencyPeak;
123}
124
125void
131
132bool
134{
135 using namespace std::chrono_literals;
136 return (targetLatencyPk_ > 0ms && (peak > targetLatencyPk_)) ||
137 (targetLatencyAvg_ > 0ms && (avg > targetLatencyAvg_));
138}
139
140bool
142{
143 std::scoped_lock const sl(mutex_);
144
145 update();
146
147 if (latencyEvents_ == 0)
148 return false;
149
150 return isOverTarget(
152}
153
156{
157 using namespace std::chrono_literals;
158 Stats stats;
159
160 std::scoped_lock const sl(mutex_);
161
162 update();
163
164 stats.count = counts_ / 4;
165
166 if (latencyEvents_ == 0)
167 {
168 stats.latencyAvg = 0ms;
169 stats.latencyPeak = 0ms;
170 }
171 else
172 {
175 }
176
177 stats.isOverloaded = isOverTarget(stats.latencyAvg, stats.latencyPeak);
178
179 return stats;
180}
181
182} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
std::chrono::steady_clock::duration waitTime() const
Definition LoadEvent.cpp:35
std::chrono::steady_clock::duration runTime() const
Definition LoadEvent.cpp:41
std::string const & name() const
Definition LoadEvent.cpp:29
std::chrono::milliseconds latencyMSPeak_
Definition LoadMonitor.h:60
void addLoadSample(LoadEvent const &sample)
std::uint64_t counts_
Definition LoadMonitor.h:57
void setTargetLatency(std::chrono::milliseconds avg, std::chrono::milliseconds pk)
std::chrono::milliseconds targetLatencyAvg_
Definition LoadMonitor.h:61
bool isOverTarget(std::chrono::milliseconds avg, std::chrono::milliseconds peak)
void addSamples(int count, std::chrono::milliseconds latency)
std::mutex mutex_
Definition LoadMonitor.h:55
LoadMonitor(beast::Journal j)
std::chrono::milliseconds latencyMSAvg_
Definition LoadMonitor.h:59
UptimeClock::time_point lastUpdate_
Definition LoadMonitor.h:63
beast::Journal const j_
Definition LoadMonitor.h:64
std::chrono::milliseconds targetLatencyPk_
Definition LoadMonitor.h:62
Tracks program uptime to seconds precision.
Definition UptimeClock.h:18
static time_point now()
T count(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T round(T... args)
std::chrono::milliseconds latencyPeak
Definition LoadMonitor.h:41
std::chrono::milliseconds latencyAvg
Definition LoadMonitor.h:40