rippled
LoadEvent.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/core/LoadEvent.h>
21 #include <ripple/core/LoadMonitor.h>
22 #include <cassert>
23 #include <iomanip>
24 
25 namespace ripple {
26 
28  LoadMonitor& monitor,
29  std::string const& name,
30  bool shouldStart)
31  : monitor_(monitor)
32  , running_(shouldStart)
33  , name_(name)
35  , timeWaiting_{}
36  , timeRunning_{}
37 {
38 }
39 
41 {
42  if (running_)
43  stop();
44 }
45 
46 std::string const&
48 {
49  return name_;
50 }
51 
52 std::chrono::steady_clock::duration
54 {
55  return timeWaiting_;
56 }
57 
58 std::chrono::steady_clock::duration
60 {
61  return timeRunning_;
62 }
63 
64 void
66 {
67  name_ = name;
68 }
69 
70 void
72 {
73  auto const now = std::chrono::steady_clock::now();
74 
75  // If we had already called start, this call will
76  // replace the previous one. Any time accumulated will
77  // be counted as "waiting".
78  timeWaiting_ += now - mark_;
79  mark_ = now;
80  running_ = true;
81 }
82 
83 void
85 {
86  assert(running_);
87 
88  auto const now = std::chrono::steady_clock::now();
89 
90  timeRunning_ += now - mark_;
91  mark_ = now;
92  running_ = false;
93 
94  monitor_.addLoadSample(*this);
95 }
96 
97 } // namespace ripple
ripple::LoadEvent::setName
void setName(std::string const &name)
Definition: LoadEvent.cpp:65
std::string
STL class.
ripple::LoadEvent::timeRunning_
std::chrono::steady_clock::duration timeRunning_
Definition: LoadEvent.h:85
ripple::LoadEvent::stop
void stop()
Definition: LoadEvent.cpp:84
ripple::LoadEvent::running_
bool running_
Definition: LoadEvent.h:75
ripple::LoadEvent::start
void start()
Definition: LoadEvent.cpp:71
ripple::LoadEvent::waitTime
std::chrono::steady_clock::duration waitTime() const
Definition: LoadEvent.cpp:53
ripple::LoadEvent::monitor_
LoadMonitor & monitor_
Definition: LoadEvent.h:72
ripple::LoadEvent::name
std::string const & name() const
Definition: LoadEvent.cpp:47
ripple::LoadMonitor
Definition: LoadMonitor.h:36
ripple::LoadEvent::runTime
std::chrono::steady_clock::duration runTime() const
Definition: LoadEvent.cpp:59
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
iomanip
cassert
ripple::LoadEvent::timeWaiting_
std::chrono::steady_clock::duration timeWaiting_
Definition: LoadEvent.h:84
ripple::LoadEvent::~LoadEvent
~LoadEvent()
Definition: LoadEvent.cpp:40
ripple::LoadEvent::LoadEvent
LoadEvent(LoadMonitor &monitor, std::string const &name, bool shouldStart)
Definition: LoadEvent.cpp:27
ripple::LoadEvent::mark_
std::chrono::steady_clock::time_point mark_
Definition: LoadEvent.h:81
ripple::LoadEvent::name_
std::string name_
Definition: LoadEvent.h:78
ripple::LoadMonitor::addLoadSample
void addLoadSample(LoadEvent const &sample)
Definition: LoadMonitor.cpp:101
std::chrono::steady_clock::now
T now(T... args)