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)
41  , latencyAvg (0)
42  , latencyPeak (0)
43  , isOverloaded (false)
44 {
45 }
46 
47 //------------------------------------------------------------------------------
48 
50  : mCounts (0)
51  , mLatencyEvents (0)
52  , mLatencyMSAvg (0)
53  , mLatencyMSPeak (0)
54  , mTargetLatencyAvg (0)
55  , mTargetLatencyPk (0)
56  , mLastUpdate (UptimeClock::now())
57  , j_ (j)
58 {
59 }
60 
61 // VFALCO NOTE WHY do we need "the mutex?" This dependence on
62 // a hidden global, especially a synchronization primitive,
63 // is a flawed design.
64 // It's not clear exactly which data needs to be protected.
65 //
66 // call with the mutex
68 {
69  using namespace std::chrono_literals;
70  auto now = UptimeClock::now();
71  if (now == mLastUpdate) // current
72  return;
73 
74  // VFALCO TODO Why 8?
75  if ((now < mLastUpdate) || (now > (mLastUpdate + 8s)))
76  {
77  // way out of date
78  mCounts = 0;
79  mLatencyEvents = 0;
80  mLatencyMSAvg = 0ms;
81  mLatencyMSPeak = 0ms;
82  mLastUpdate = now;
83  // VFALCO TODO don't return from the middle...
84  return;
85  }
86 
87  // do exponential decay
88  /*
89  David:
90 
91  "Imagine if you add 10 to something every second. And you
92  also reduce it by 1/4 every second. It will "idle" at 40,
93  correponding to 10 counts per second."
94  */
95  do
96  {
97  mLastUpdate += 1s;
98  mCounts -= ((mCounts + 3) / 4);
99  mLatencyEvents -= ((mLatencyEvents + 3) / 4);
100  mLatencyMSAvg -= (mLatencyMSAvg / 4);
102  }
103  while (mLastUpdate < now);
104 }
105 
107 {
108  using namespace std::chrono;
109 
110  auto const total = s.runTime() + s.waitTime();
111  // Don't include "jitter" as part of the latency
112  auto const latency = total < 2ms ? 0ms : date::round<milliseconds>(total);
113 
114  if (latency > 500ms)
115  {
116  auto mj = (latency > 1s) ? j_.warn() : j_.info();
117  JLOG (mj) << "Job: " << s.name() <<
118  " run: " << date::round<milliseconds>(s.runTime()).count() <<
119  "ms" << " wait: " <<
120  date::round<milliseconds>(s.waitTime()).count() << "ms";
121  }
122 
123  addSamples (1, latency);
124 }
125 
126 /* Add multiple samples
127  @param count The number of samples to add
128  @param latencyMS The total number of milliseconds
129 */
131 {
132  std::lock_guard sl (mutex_);
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 
148 {
149  mTargetLatencyAvg = avg;
150  mTargetLatencyPk = pk;
151 }
152 
155 {
156  using namespace std::chrono_literals;
157  return (mTargetLatencyPk > 0ms && (peak > mTargetLatencyPk)) ||
158  (mTargetLatencyAvg > 0ms && (avg > mTargetLatencyAvg));
159 }
160 
162 {
163  std::lock_guard sl (mutex_);
164 
165  update ();
166 
167  if (mLatencyEvents == 0)
168  return 0;
169 
171 }
172 
174 {
175  using namespace std::chrono_literals;
176  Stats stats;
177 
178  std::lock_guard sl (mutex_);
179 
180  update ();
181 
182  stats.count = mCounts / 4;
183 
184  if (mLatencyEvents == 0)
185  {
186  stats.latencyAvg = 0ms;
187  stats.latencyPeak = 0ms;
188  }
189  else
190  {
191  stats.latencyAvg = mLatencyMSAvg / (mLatencyEvents * 4);
192  stats.latencyPeak = mLatencyMSPeak / (mLatencyEvents * 4);
193  }
194 
195  stats.isOverloaded = isOverTarget (stats.latencyAvg, stats.latencyPeak);
196 
197  return stats;
198 }
199 
200 } // ripple
ripple::LoadMonitor::isOver
bool isOver()
Definition: LoadMonitor.cpp:161
ripple::LoadMonitor::j_
const beast::Journal j_
Definition: LoadMonitor.h:78
ripple::LoadMonitor::getStats
Stats getStats()
Definition: LoadMonitor.cpp:173
std::chrono::milliseconds
ripple::LoadMonitor::Stats::count
std::uint64_t count
Definition: LoadMonitor.h:56
ripple::LoadMonitor::Stats::isOverloaded
bool isOverloaded
Definition: LoadMonitor.h:59
beast::Journal::warn
Stream warn() const
Definition: Journal.h:302
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:75
ripple::LoadMonitor::mLastUpdate
UptimeClock::time_point mLastUpdate
Definition: LoadMonitor.h:77
ripple::LoadMonitor::isOverTarget
bool isOverTarget(std::chrono::milliseconds avg, std::chrono::milliseconds peak)
Definition: LoadMonitor.cpp:153
ripple::LoadMonitor::Stats
Definition: LoadMonitor.h:52
ripple::UptimeClock::now
static time_point now()
Definition: UptimeClock.cpp:64
ripple::LoadMonitor::mutex_
std::mutex mutex_
Definition: LoadMonitor.h:69
ripple::LoadMonitor::update
void update()
Definition: LoadMonitor.cpp:67
ripple::LoadEvent::waitTime
std::chrono::steady_clock::duration waitTime() const
Definition: LoadEvent.cpp:52
beast::Journal::info
Stream info() const
Definition: Journal.h:297
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
ripple::LoadEvent
Definition: LoadEvent.h:36
ripple::LoadEvent::name
std::string const & name() const
Definition: LoadEvent.cpp:46
ripple::LoadEvent::runTime
std::chrono::steady_clock::duration runTime() const
Definition: LoadEvent.cpp:58
ripple::LoadMonitor::LoadMonitor
LoadMonitor(beast::Journal j)
Definition: LoadMonitor.cpp:49
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:73
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:74
ripple::LoadMonitor::mLatencyEvents
int mLatencyEvents
Definition: LoadMonitor.h:72
ripple::LoadMonitor::Stats::Stats
Stats()
Definition: LoadMonitor.cpp:39
ripple::LoadMonitor::mTargetLatencyPk
std::chrono::milliseconds mTargetLatencyPk
Definition: LoadMonitor.h:76
ripple::LoadMonitor::mCounts
std::uint64_t mCounts
Definition: LoadMonitor.h:71
ripple::LoadMonitor::Stats::latencyAvg
std::chrono::milliseconds latencyAvg
Definition: LoadMonitor.h:57
ripple::LoadMonitor::Stats::latencyPeak
std::chrono::milliseconds latencyPeak
Definition: LoadMonitor.h:58
ripple::LoadMonitor::addLoadSample
void addLoadSample(LoadEvent const &sample)
Definition: LoadMonitor.cpp:106
std::chrono