rippled
PerfLogImp.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2018 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 #ifndef RIPPLE_BASICS_PERFLOGIMP_H
21 #define RIPPLE_BASICS_PERFLOGIMP_H
22 
23 #include <ripple/basics/PerfLog.h>
24 #include <ripple/basics/chrono.h>
25 #include <ripple/beast/utility/Journal.h>
26 #include <ripple/core/Stoppable.h>
27 #include <ripple/protocol/jss.h>
28 #include <ripple/rpc/impl/Handler.h>
29 #include <boost/asio/ip/host_name.hpp>
30 #include <condition_variable>
31 #include <cstdint>
32 #include <fstream>
33 #include <memory>
34 #include <mutex>
35 #include <string>
36 #include <thread>
37 #include <type_traits>
38 #include <unordered_map>
39 #include <utility>
40 #include <vector>
41 
42 namespace ripple {
43 namespace perf {
44 
46 template <typename T>
47 struct Locked
48 {
49  T value;
50  mutable std::mutex mutex;
51 
52  Locked() = default;
53  Locked(T const& value) : value(value)
54  {
55  }
56  Locked(T&& value) : value(std::move(value))
57  {
58  }
59  Locked(Locked const& rhs) : value(rhs.value)
60  {
61  }
62  Locked(Locked&& rhs) : value(std::move(rhs.value))
63  {
64  }
65 };
66 
70 class PerfLogImp : public PerfLog, Stoppable
71 {
75  struct Counters
76  {
77  public:
82  struct Rpc
83  {
84  // Counters for each time a method starts and then either
85  // finishes successfully or with an exception.
89  // Cumulative duration of all finished and errored method calls.
91  };
92 
96  struct Jq
97  {
98  // Counters for each time a job is enqueued, begins to run,
99  // finishes.
103  // Cumulative duration of all jobs' queued and running times.
106  };
107 
108  // rpc_ and jq_ do not need mutex protection because all
109  // keys and values are created before more threads are started.
116 
117  Counters(
118  std::vector<char const*> const& labels,
119  JobTypes const& jobTypes);
121  countersJson() const;
123  currentJson() const;
124  };
125 
126  Setup const setup_;
128  std::function<void()> const signalStop_;
135  std::string const hostname_{boost::asio::ip::host_name()};
136  bool stop_{false};
137  bool rotate_{false};
138 
139  void
140  openLog();
141  void
142  run();
143  void
144  report();
145  void
146  rpcEnd(
147  std::string const& method,
148  std::uint64_t const requestId,
149  bool finish);
150 
151 public:
152  PerfLogImp(
153  Setup const& setup,
154  Stoppable& parent,
155  beast::Journal journal,
156  std::function<void()>&& signalStop);
157 
158  ~PerfLogImp() override;
159 
160  void
161  rpcStart(std::string const& method, std::uint64_t const requestId) override;
162 
163  void
164  rpcFinish(std::string const& method, std::uint64_t const requestId) override
165  {
166  rpcEnd(method, requestId, true);
167  }
168 
169  void
170  rpcError(std::string const& method, std::uint64_t const requestId) override
171  {
172  rpcEnd(method, requestId, false);
173  }
174 
175  void
176  jobQueue(JobType const type) override;
177  void
178  jobStart(
179  JobType const type,
180  microseconds dur,
181  steady_time_point startTime,
182  int instance) override;
183  void
184  jobFinish(JobType const type, microseconds dur, int instance) override;
185 
187  countersJson() const override
188  {
189  return counters_.countersJson();
190  }
191 
193  currentJson() const override
194  {
195  return counters_.currentJson();
196  }
197 
198  void
199  resizeJobs(int const resize) override;
200  void
201  rotate() override;
202 
203  // Called when application is ready to start threads.
204  void
205  onStart() override;
206  // Called when the application begins shutdown.
207  void
208  onStop() override;
209  // Called when all child Stoppable objects have stopped.
210  void
211  onChildrenStopped() override;
212 };
213 
214 } // namespace perf
215 } // namespace ripple
216 
217 #endif // RIPPLE_BASICS_PERFLOGIMP_H
ripple::perf::PerfLog::microseconds
std::chrono::microseconds microseconds
Definition: PerfLog.h:55
ripple::perf::PerfLogImp::Counters::Jq::runningDuration
microseconds runningDuration
Definition: PerfLogImp.h:105
ripple::perf::PerfLogImp::Counters::Rpc::started
std::uint64_t started
Definition: PerfLogImp.h:86
fstream
std::string
STL class.
ripple::perf::PerfLogImp::Counters::Jq::finished
std::uint64_t finished
Definition: PerfLogImp.h:102
ripple::perf::PerfLogImp::lastLog_
system_time_point lastLog_
Definition: PerfLogImp.h:134
utility
ripple::JobTypes
Definition: JobTypes.h:32
ripple::perf::PerfLogImp::Counters::Jq::queued
std::uint64_t queued
Definition: PerfLogImp.h:100
ripple::perf::PerfLogImp::openLog
void openLog()
Definition: PerfLogImp.cpp:220
ripple::perf::PerfLogImp::Counters::jobsMutex_
std::mutex jobsMutex_
Definition: PerfLogImp.h:113
ripple::perf::PerfLogImp::countersJson
Json::Value countersJson() const override
Render performance counters in Json.
Definition: PerfLogImp.h:187
std::pair
ripple::perf::PerfLogImp::Counters::methodsMutex_
std::mutex methodsMutex_
Definition: PerfLogImp.h:115
vector
ripple::perf::PerfLogImp::jobFinish
void jobFinish(JobType const type, microseconds dur, int instance) override
Log job finishing.
Definition: PerfLogImp.cpp:413
ripple::perf::PerfLogImp::j_
const beast::Journal j_
Definition: PerfLogImp.h:127
ripple::perf::PerfLogImp::rotate
void rotate() override
Rotate perf log file.
Definition: PerfLogImp.cpp:440
ripple::perf::PerfLogImp::cond_
std::condition_variable cond_
Definition: PerfLogImp.h:133
ripple::perf::PerfLogImp::PerfLogImp
PerfLogImp(Setup const &setup, Stoppable &parent, beast::Journal journal, std::function< void()> &&signalStop)
Definition: PerfLogImp.cpp:304
ripple::perf::PerfLogImp::Counters::Rpc::finished
std::uint64_t finished
Definition: PerfLogImp.h:87
ripple::perf::PerfLogImp::Counters::Jq::started
std::uint64_t started
Definition: PerfLogImp.h:101
ripple::perf::PerfLog
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
Definition: PerfLog.h:46
ripple::perf::PerfLogImp
Implementation class for PerfLog.
Definition: PerfLogImp.h:70
ripple::perf::PerfLogImp::Counters::methods_
std::unordered_map< std::uint64_t, MethodStart > methods_
Definition: PerfLogImp.h:114
std::function
ripple::perf::PerfLogImp::Counters::Rpc::errored
std::uint64_t errored
Definition: PerfLogImp.h:88
ripple::perf::PerfLogImp::hostname_
const std::string hostname_
Definition: PerfLogImp.h:135
ripple::perf::PerfLog::Setup
Configuration from [perf] section of rippled.cfg.
Definition: PerfLog.h:60
ripple::perf::Locked::Locked
Locked(T const &value)
Definition: PerfLogImp.h:53
ripple::perf::PerfLogImp::jobStart
void jobStart(JobType const type, microseconds dur, steady_time_point startTime, int instance) override
Log job executing.
Definition: PerfLogImp.cpp:390
ripple::perf::PerfLogImp::rotate_
bool rotate_
Definition: PerfLogImp.h:137
ripple::perf::PerfLogImp::currentJson
Json::Value currentJson() const override
Render currently executing jobs and RPC calls and durations in Json.
Definition: PerfLogImp.h:193
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
thread
ripple::perf::Locked::Locked
Locked(T &&value)
Definition: PerfLogImp.h:56
ripple::perf::PerfLogImp::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: PerfLogImp.cpp:458
ripple::perf::Locked
A box coupling data with a mutex for locking access to it.
Definition: PerfLogImp.h:47
ripple::perf::PerfLogImp::mutex_
std::mutex mutex_
Definition: PerfLogImp.h:132
std::ofstream
STL class.
ripple::perf::PerfLogImp::Counters::jobs_
std::vector< std::pair< JobType, steady_time_point > > jobs_
Definition: PerfLogImp.h:112
ripple::perf::PerfLogImp::Counters::jq_
std::unordered_map< JobType, Locked< Jq > > jq_
Definition: PerfLogImp.h:111
ripple::JobTypes::instance
static JobTypes const & instance()
Definition: JobTypes.h:103
std::chrono::time_point
cstdint
ripple::perf::Locked::value
T value
Definition: PerfLogImp.h:49
ripple::perf::PerfLogImp::thread_
std::thread thread_
Definition: PerfLogImp.h:131
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint64_t
ripple::perf::PerfLogImp::signalStop_
const std::function< void()> signalStop_
Definition: PerfLogImp.h:128
ripple::perf::PerfLogImp::Counters::rpc_
std::unordered_map< std::string, Locked< Rpc > > rpc_
Definition: PerfLogImp.h:110
memory
ripple::perf::PerfLogImp::Counters::Jq
Job Queue task performance counters.
Definition: PerfLogImp.h:96
ripple::perf::PerfLogImp::~PerfLogImp
~PerfLogImp() override
Definition: PerfLogImp.cpp:317
ripple::perf::PerfLogImp::Counters::Jq::queuedDuration
microseconds queuedDuration
Definition: PerfLogImp.h:104
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::perf::Locked::Locked
Locked(Locked &&rhs)
Definition: PerfLogImp.h:62
ripple::perf::PerfLog::steady_time_point
std::chrono::time_point< steady_clock > steady_time_point
Definition: PerfLog.h:51
ripple::perf::PerfLogImp::stop_
bool stop_
Definition: PerfLogImp.h:136
ripple::perf::PerfLogImp::Counters::Rpc
RPC performance counters.
Definition: PerfLogImp.h:82
std
STL namespace.
ripple::perf::PerfLogImp::rpcEnd
void rpcEnd(std::string const &method, std::uint64_t const requestId, bool finish)
Definition: PerfLogImp.cpp:342
ripple::perf::PerfLogImp::rpcStart
void rpcStart(std::string const &method, std::uint64_t const requestId) override
Log start of RPC call.
Definition: PerfLogImp.cpp:323
ripple::perf::PerfLogImp::Counters::currentJson
Json::Value currentJson() const
Definition: PerfLogImp.cpp:171
condition_variable
ripple::perf::PerfLogImp::Counters
Track performance counters and currently executing tasks.
Definition: PerfLogImp.h:75
ripple::perf::PerfLogImp::run
void run()
Definition: PerfLogImp.cpp:254
ripple::perf::Locked::mutex
std::mutex mutex
Definition: PerfLogImp.h:50
mutex
ripple::JobType
JobType
Definition: Job.h:33
ripple::perf::Locked::Locked
Locked(Locked const &rhs)
Definition: PerfLogImp.h:59
ripple::perf::PerfLogImp::setup_
const Setup setup_
Definition: PerfLogImp.h:126
ripple::perf::PerfLogImp::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: PerfLogImp.cpp:472
ripple::perf::PerfLogImp::rpcFinish
void rpcFinish(std::string const &method, std::uint64_t const requestId) override
Log successful finish of RPC call.
Definition: PerfLogImp.h:164
ripple::perf::PerfLogImp::Counters::Counters
Counters(std::vector< char const * > const &labels, JobTypes const &jobTypes)
Definition: PerfLogImp.cpp:43
ripple::perf::PerfLogImp::logFile_
std::ofstream logFile_
Definition: PerfLogImp.h:130
ripple::perf::PerfLogImp::counters_
Counters counters_
Definition: PerfLogImp.h:129
ripple::RPC::getHandlerNames
std::vector< char const * > getHandlerNames()
Return names of all methods.
Definition: Handler.cpp:260
ripple::perf::PerfLogImp::Counters::Rpc::duration
microseconds duration
Definition: PerfLogImp.h:90
ripple::perf::PerfLogImp::jobQueue
void jobQueue(JobType const type) override
Log queued job.
Definition: PerfLogImp.cpp:377
unordered_map
type_traits
ripple::perf::PerfLogImp::onStart
void onStart() override
Override called during start.
Definition: PerfLogImp.cpp:451
ripple::perf::PerfLogImp::resizeJobs
void resizeJobs(int const resize) override
Ensure enough room to store each currently executing job.
Definition: PerfLogImp.cpp:432
ripple::perf::PerfLogImp::Counters::countersJson
Json::Value countersJson() const
Definition: PerfLogImp.cpp:76
ripple::perf::PerfLogImp::report
void report()
Definition: PerfLogImp.cpp:279
ripple::perf::PerfLogImp::rpcError
void rpcError(std::string const &method, std::uint64_t const requestId) override
Log errored RPC call.
Definition: PerfLogImp.h:170
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::perf::Locked::Locked
Locked()=default
string