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  // VFALCO TODO don't return from the middle...
82  return;
83  }
84 
85  // do exponential decay
86  /*
87  David:
88 
89  "Imagine if you add 10 to something every second. And you
90  also reduce it by 1/4 every second. It will "idle" at 40,
91  correponding to 10 counts per second."
92  */
93  do
94  {
95  mLastUpdate += 1s;
96  mCounts -= ((mCounts + 3) / 4);
97  mLatencyEvents -= ((mLatencyEvents + 3) / 4);
100  } while (mLastUpdate < now);
101 }
102 
103 void
105 {
106  using namespace std::chrono;
107 
108  auto const total = s.runTime() + s.waitTime();
109  // Don't include "jitter" as part of the latency
110  auto const latency = total < 2ms ? 0ms : date::round<milliseconds>(total);
111 
112  if (latency > 500ms)
113  {
114  auto mj = (latency > 1s) ? j_.warn() : j_.info();
115  JLOG(mj) << "Job: " << s.name()
116  << " run: " << date::round<milliseconds>(s.runTime()).count()
117  << "ms"
118  << " wait: " << date::round<milliseconds>(s.waitTime()).count()
119  << "ms";
120  }
121 
122  addSamples(1, latency);
123 }
124 
125 /* Add multiple samples
126  @param count The number of samples to add
127  @param latencyMS The total number of milliseconds
128 */
129 void
131 {
133 
134  update();
135  mCounts += count;
136  mLatencyEvents += count;
137  mLatencyMSAvg += latency;
138  mLatencyMSPeak += latency;
139 
140  auto const latencyPeak = mLatencyEvents * latency * 4 / count;
141 
142  if (mLatencyMSPeak < latencyPeak)
143  mLatencyMSPeak = latencyPeak;
144 }
145 
146 void
150 {
151  mTargetLatencyAvg = avg;
152  mTargetLatencyPk = pk;
153 }
154 
155 bool
159 {
160  using namespace std::chrono_literals;
161  return (mTargetLatencyPk > 0ms && (peak > mTargetLatencyPk)) ||
162  (mTargetLatencyAvg > 0ms && (avg > mTargetLatencyAvg));
163 }
164 
165 bool
167 {
169 
170  update();
171 
172  if (mLatencyEvents == 0)
173  return 0;
174 
175  return isOverTarget(
178 }
179 
182 {
183  using namespace std::chrono_literals;
184  Stats stats;
185 
187 
188  update();
189 
190  stats.count = mCounts / 4;
191 
192  if (mLatencyEvents == 0)
193  {
194  stats.latencyAvg = 0ms;
195  stats.latencyPeak = 0ms;
196  }
197  else
198  {
199  stats.latencyAvg = mLatencyMSAvg / (mLatencyEvents * 4);
200  stats.latencyPeak = mLatencyMSPeak / (mLatencyEvents * 4);
201  }
202 
203  stats.isOverloaded = isOverTarget(stats.latencyAvg, stats.latencyPeak);
204 
205  return stats;
206 }
207 
208 } // namespace ripple
ripple::LoadMonitor::isOver
bool isOver()
Definition: LoadMonitor.cpp:166
ripple::LoadMonitor::j_
const beast::Journal j_
Definition: LoadMonitor.h:85
ripple::LoadMonitor::getStats
Stats getStats()
Definition: LoadMonitor.cpp:181
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:130
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:156
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:147
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:104
std::chrono