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