rippled
LoadMonitor.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/Log.h>
21 #include <ripple/basics/UptimeClock.h>
22 #include <ripple/core/LoadMonitor.h>
23 
24 #include <date/date.h>
25 
26 namespace ripple {
27 
28 /*
29 
30 TODO
31 ----
32 
33 - Use Journal for logging
34 
35 */
36 
37 //------------------------------------------------------------------------------
38 
40  : count(0), latencyAvg(0), latencyPeak(0), isOverloaded(false)
41 {
42 }
43 
44 //------------------------------------------------------------------------------
45 
47  : mCounts(0)
48  , mLatencyEvents(0)
49  , mLatencyMSAvg(0)
50  , mLatencyMSPeak(0)
52  , mTargetLatencyPk(0)
53  , mLastUpdate(UptimeClock::now())
54  , j_(j)
55 {
56 }
57 
58 // VFALCO NOTE WHY do we need "the mutex?" This dependence on
59 // a hidden global, especially a synchronization primitive,
60 // is a flawed design.
61 // It's not clear exactly which data needs to be protected.
62 //
63 // call with the mutex
64 void
66 {
67  using namespace std::chrono_literals;
68  auto now = UptimeClock::now();
69  if (now == mLastUpdate) // current
70  return;
71 
72  // VFALCO TODO Why 8?
73  if ((now < mLastUpdate) || (now > (mLastUpdate + 8s)))
74  {
75  // way out of date
76  mCounts = 0;
77  mLatencyEvents = 0;
78  mLatencyMSAvg = 0ms;
79  mLatencyMSPeak = 0ms;
80  mLastUpdate = now;
81  return;
82  }
83 
84  // do exponential decay
85  /*
86  David:
87 
88  "Imagine if you add 10 to something every second. And you
89  also reduce it by 1/4 every second. It will "idle" at 40,
90  correponding to 10 counts per second."
91  */
92  do
93  {
94  mLastUpdate += 1s;
95  mCounts -= ((mCounts + 3) / 4);
96  mLatencyEvents -= ((mLatencyEvents + 3) / 4);
99  } while (mLastUpdate < now);
100 }
101 
102 void
104 {
105  using namespace std::chrono;
106 
107  auto const total = s.runTime() + s.waitTime();
108  // Don't include "jitter" as part of the latency
109  auto const latency = total < 2ms ? 0ms : date::round<milliseconds>(total);
110 
111  if (latency > 500ms)
112  {
113  auto mj = (latency > 1s) ? j_.warn() : j_.info();
114  JLOG(mj) << "Job: " << s.name()
115  << " run: " << date::round<milliseconds>(s.runTime()).count()
116  << "ms"
117  << " wait: " << date::round<milliseconds>(s.waitTime()).count()
118  << "ms";
119  }
120 
121  addSamples(1, latency);
122 }
123 
124 /* Add multiple samples
125  @param count The number of samples to add
126  @param latencyMS The total number of milliseconds
127 */
128 void
130 {
132 
133  update();
134  mCounts += count;
135  mLatencyEvents += count;
136  mLatencyMSAvg += latency;
137  mLatencyMSPeak += latency;
138 
139  auto const latencyPeak = mLatencyEvents * latency * 4 / count;
140 
141  if (mLatencyMSPeak < latencyPeak)
142  mLatencyMSPeak = latencyPeak;
143 }
144 
145 void
149 {
150  mTargetLatencyAvg = avg;
151  mTargetLatencyPk = pk;
152 }
153 
154 bool
158 {
159  using namespace std::chrono_literals;
160  return (mTargetLatencyPk > 0ms && (peak > mTargetLatencyPk)) ||
161  (mTargetLatencyAvg > 0ms && (avg > mTargetLatencyAvg));
162 }
163 
164 bool
166 {
168 
169  update();
170 
171  if (mLatencyEvents == 0)
172  return 0;
173 
174  return isOverTarget(
177 }
178 
181 {
182  using namespace std::chrono_literals;
183  Stats stats;
184 
186 
187  update();
188 
189  stats.count = mCounts / 4;
190 
191  if (mLatencyEvents == 0)
192  {
193  stats.latencyAvg = 0ms;
194  stats.latencyPeak = 0ms;
195  }
196  else
197  {
198  stats.latencyAvg = mLatencyMSAvg / (mLatencyEvents * 4);
199  stats.latencyPeak = mLatencyMSPeak / (mLatencyEvents * 4);
200  }
201 
202  stats.isOverloaded = isOverTarget(stats.latencyAvg, stats.latencyPeak);
203 
204  return stats;
205 }
206 
207 } // namespace ripple
ripple::LoadMonitor::isOver
bool isOver()
Definition: LoadMonitor.cpp:165
ripple::LoadMonitor::j_
const beast::Journal j_
Definition: LoadMonitor.h:85
ripple::LoadMonitor::getStats
Stats getStats()
Definition: LoadMonitor.cpp:180
std::chrono::milliseconds
ripple::LoadMonitor::Stats::count
std::uint64_t count
Definition: LoadMonitor.h:60
ripple::LoadMonitor::Stats::isOverloaded
bool isOverloaded
Definition: LoadMonitor.h:63
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
std::lock_guard
STL class.
ripple::UptimeClock
Tracks program uptime to seconds precision.
Definition: UptimeClock.h:37
ripple::LoadMonitor::addSamples
void addSamples(int count, std::chrono::milliseconds latency)
Definition: LoadMonitor.cpp:129
ripple::LoadMonitor::mTargetLatencyAvg
std::chrono::milliseconds mTargetLatencyAvg
Definition: LoadMonitor.h:82
ripple::LoadMonitor::mLastUpdate
UptimeClock::time_point mLastUpdate
Definition: LoadMonitor.h:84
ripple::LoadMonitor::isOverTarget
bool isOverTarget(std::chrono::milliseconds avg, std::chrono::milliseconds peak)
Definition: LoadMonitor.cpp:155
ripple::LoadMonitor::Stats
Definition: LoadMonitor.h:56
ripple::UptimeClock::now
static time_point now()
Definition: UptimeClock.cpp:63
ripple::LoadMonitor::mutex_
std::mutex mutex_
Definition: LoadMonitor.h:76
ripple::LoadMonitor::update
void update()
Definition: LoadMonitor.cpp:65
ripple::LoadEvent::waitTime
std::chrono::steady_clock::duration waitTime() const
Definition: LoadEvent.cpp:53
beast::Journal::info
Stream info() const
Definition: Journal.h:321
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::LoadEvent
Definition: LoadEvent.h:36
ripple::LoadEvent::name
std::string const & name() const
Definition: LoadEvent.cpp:47
ripple::LoadEvent::runTime
std::chrono::steady_clock::duration runTime() const
Definition: LoadEvent.cpp:59
ripple::LoadMonitor::LoadMonitor
LoadMonitor(beast::Journal j)
Definition: LoadMonitor.cpp:46
ripple::LoadMonitor::setTargetLatency
void setTargetLatency(std::chrono::milliseconds avg, std::chrono::milliseconds pk)
Definition: LoadMonitor.cpp:146
ripple::LoadMonitor::mLatencyMSAvg
std::chrono::milliseconds mLatencyMSAvg
Definition: LoadMonitor.h:80
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::LoadMonitor::mLatencyMSPeak
std::chrono::milliseconds mLatencyMSPeak
Definition: LoadMonitor.h:81
ripple::LoadMonitor::mLatencyEvents
int mLatencyEvents
Definition: LoadMonitor.h:79
ripple::LoadMonitor::Stats::Stats
Stats()
Definition: LoadMonitor.cpp:39
ripple::LoadMonitor::mTargetLatencyPk
std::chrono::milliseconds mTargetLatencyPk
Definition: LoadMonitor.h:83
ripple::LoadMonitor::mCounts
std::uint64_t mCounts
Definition: LoadMonitor.h:78
ripple::LoadMonitor::Stats::latencyAvg
std::chrono::milliseconds latencyAvg
Definition: LoadMonitor.h:61
ripple::LoadMonitor::Stats::latencyPeak
std::chrono::milliseconds latencyPeak
Definition: LoadMonitor.h:62
ripple::LoadMonitor::addLoadSample
void addLoadSample(LoadEvent const &sample)
Definition: LoadMonitor.cpp:103
std::chrono