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