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