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 
48 class PerfLogImp : public PerfLog, Stoppable
49 {
53  struct Counters
54  {
55  public:
60  struct Rpc
61  {
62  // Keep all items that need to be synchronized in one place
63  // to minimize copy overhead while locked.
64  struct Sync
65  {
66  // Counters for each time a method starts and then either
67  // finishes successfully or with an exception.
71  // Cumulative duration of all finished and errored method calls.
73  };
74 
76  mutable std::mutex mut;
77 
78  Rpc() = default;
79 
80  Rpc(Rpc const& orig) : sync(orig.sync)
81  {
82  }
83  };
84 
88  struct Jq
89  {
90  // Keep all items that need to be synchronized in one place
91  // to minimize copy overhead while locked.
92  struct Sync
93  {
94  // Counters for each time a job is enqueued, begins to run,
95  // finishes.
99  // Cumulative duration of all jobs' queued and running times.
102  };
103 
106  mutable std::mutex mut;
107 
108  Jq(std::string const& labelArg) : label(labelArg)
109  {
110  }
111 
112  Jq(Jq const& orig) : sync(orig.sync), label(orig.label)
113  {
114  }
115  };
116 
117  // rpc_ and jq_ do not need mutex protection because all
118  // keys and values are created before more threads are started.
122  int workers_{0};
126 
127  Counters(
128  std::vector<char const*> const& labels,
129  JobTypes const& jobTypes);
131  countersJson() const;
133  currentJson() const;
134  };
135 
136  Setup const setup_;
138  std::function<void()> const signalStop_;
145  std::string const hostname_{boost::asio::ip::host_name()};
146  bool stop_{false};
147  bool rotate_{false};
148 
149  void
150  openLog();
151  void
152  run();
153  void
154  report();
155  void
156  rpcEnd(
157  std::string const& method,
158  std::uint64_t const requestId,
159  bool finish);
160 
161 public:
162  PerfLogImp(
163  Setup const& setup,
164  Stoppable& parent,
165  beast::Journal journal,
166  std::function<void()>&& signalStop);
167 
168  ~PerfLogImp() override;
169 
170  void
171  rpcStart(std::string const& method, std::uint64_t const requestId) override;
172 
173  void
174  rpcFinish(std::string const& method, std::uint64_t const requestId) override
175  {
176  rpcEnd(method, requestId, true);
177  }
178 
179  void
180  rpcError(std::string const& method, std::uint64_t const requestId) override
181  {
182  rpcEnd(method, requestId, false);
183  }
184 
185  void
186  jobQueue(JobType const type) override;
187  void
188  jobStart(
189  JobType const type,
190  microseconds dur,
191  steady_time_point startTime,
192  int instance) override;
193  void
194  jobFinish(JobType const type, microseconds dur, int instance) override;
195 
197  countersJson() const override
198  {
199  return counters_.countersJson();
200  }
201 
203  currentJson() const override
204  {
205  return counters_.currentJson();
206  }
207 
208  void
209  resizeJobs(int const resize) override;
210  void
211  rotate() override;
212 
213  // Stoppable
214  void
215  onPrepare() override
216  {
217  }
218 
219  // Called when application is ready to start threads.
220  void
221  onStart() override;
222  // Called when the application begins shutdown.
223  void
224  onStop() override;
225  // Called when all child Stoppable objects have stopped.
226  void
227  onChildrenStopped() override;
228 };
229 
230 } // namespace perf
231 } // namespace ripple
232 
233 #endif // RIPPLE_BASICS_PERFLOGIMP_H
ripple::perf::PerfLog::microseconds
std::chrono::microseconds microseconds
Definition: PerfLog.h:55
fstream
std::string
STL class.
ripple::perf::PerfLogImp::lastLog_
system_time_point lastLog_
Definition: PerfLogImp.h:144
utility
ripple::perf::PerfLogImp::Counters::Rpc::Sync
Definition: PerfLogImp.h:64
ripple::JobTypes
Definition: JobTypes.h:32
ripple::perf::PerfLogImp::openLog
void openLog()
Definition: PerfLogImp.cpp:233
ripple::perf::PerfLogImp::Counters::Jq::label
const std::string label
Definition: PerfLogImp.h:105
ripple::perf::PerfLogImp::Counters::jobsMutex_
std::mutex jobsMutex_
Definition: PerfLogImp.h:123
ripple::perf::PerfLogImp::countersJson
Json::Value countersJson() const override
Render performance counters in Json.
Definition: PerfLogImp.h:197
std::pair
ripple::perf::PerfLogImp::Counters::methodsMutex_
std::mutex methodsMutex_
Definition: PerfLogImp.h:125
vector
ripple::perf::PerfLogImp::jobFinish
void jobFinish(JobType const type, microseconds dur, int instance) override
Log job finishing.
Definition: PerfLogImp.cpp:423
ripple::perf::PerfLogImp::j_
const beast::Journal j_
Definition: PerfLogImp.h:137
ripple::perf::PerfLogImp::Counters::Jq::mut
std::mutex mut
Definition: PerfLogImp.h:106
ripple::perf::PerfLogImp::rotate
void rotate() override
Rotate perf log file.
Definition: PerfLogImp.cpp:451
std::chrono::microseconds
ripple::perf::PerfLogImp::cond_
std::condition_variable cond_
Definition: PerfLogImp.h:143
ripple::perf::PerfLogImp::PerfLogImp
PerfLogImp(Setup const &setup, Stoppable &parent, beast::Journal journal, std::function< void()> &&signalStop)
Definition: PerfLogImp.cpp:314
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:48
ripple::perf::PerfLogImp::Counters::methods_
std::unordered_map< std::uint64_t, MethodStart > methods_
Definition: PerfLogImp.h:124
std::function
ripple::perf::PerfLogImp::Counters::Jq::Jq
Jq(Jq const &orig)
Definition: PerfLogImp.h:112
ripple::perf::PerfLogImp::hostname_
const std::string hostname_
Definition: PerfLogImp.h:145
ripple::perf::PerfLog::Setup
Configuration from [perf] section of rippled.cfg.
Definition: PerfLog.h:60
ripple::perf::PerfLogImp::Counters::Jq::Sync::queued
std::uint64_t queued
Definition: PerfLogImp.h:96
ripple::perf::PerfLogImp::Counters::Rpc::Sync::errored
std::uint64_t errored
Definition: PerfLogImp.h:70
ripple::perf::PerfLogImp::jobStart
void jobStart(JobType const type, microseconds dur, steady_time_point startTime, int instance) override
Log job executing.
Definition: PerfLogImp.cpp:400
ripple::perf::PerfLogImp::Counters::Jq::Sync::started
std::uint64_t started
Definition: PerfLogImp.h:97
ripple::perf::PerfLogImp::rotate_
bool rotate_
Definition: PerfLogImp.h:147
ripple::perf::PerfLogImp::currentJson
Json::Value currentJson() const override
Render currently executing jobs and RPC calls and durations in Json.
Definition: PerfLogImp.h:203
ripple::perf::PerfLogImp::Counters::rpc_
std::unordered_map< std::string, Rpc > rpc_
Definition: PerfLogImp.h:119
ripple::perf::PerfLogImp::onPrepare
void onPrepare() override
Override called during preparation.
Definition: PerfLogImp.h:215
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
thread
ripple::perf::PerfLogImp::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: PerfLogImp.cpp:469
ripple::perf::PerfLogImp::mutex_
std::mutex mutex_
Definition: PerfLogImp.h:142
std::ofstream
STL class.
ripple::perf::PerfLogImp::Counters::jobs_
std::vector< std::pair< JobType, steady_time_point > > jobs_
Definition: PerfLogImp.h:121
ripple::perf::PerfLogImp::Counters::Rpc::Sync::duration
microseconds duration
Definition: PerfLogImp.h:72
ripple::JobTypes::instance
static JobTypes const & instance()
Definition: JobTypes.h:101
ripple::perf::PerfLogImp::Counters::Jq::Sync::finished
std::uint64_t finished
Definition: PerfLogImp.h:98
ripple::perf::PerfLogImp::Counters::jq_
std::unordered_map< std::underlying_type_t< JobType >, Jq > jq_
Definition: PerfLogImp.h:120
std::chrono::time_point
ripple::perf::PerfLogImp::Counters::Rpc::Sync::finished
std::uint64_t finished
Definition: PerfLogImp.h:69
cstdint
ripple::perf::PerfLogImp::thread_
std::thread thread_
Definition: PerfLogImp.h:141
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:138
memory
ripple::perf::PerfLogImp::Counters::Jq
Job Queue task performance counters.
Definition: PerfLogImp.h:88
ripple::perf::PerfLogImp::Counters::Rpc::Rpc
Rpc()=default
ripple::perf::PerfLogImp::~PerfLogImp
~PerfLogImp() override
Definition: PerfLogImp.cpp:327
ripple::perf::PerfLogImp::Counters::Jq::Sync::runningDuration
microseconds runningDuration
Definition: PerfLogImp.h:101
ripple::perf::PerfLogImp::Counters::Rpc::Rpc
Rpc(Rpc const &orig)
Definition: PerfLogImp.h:80
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::perf::PerfLog::steady_time_point
std::chrono::time_point< steady_clock > steady_time_point
Definition: PerfLog.h:51
ripple::perf::PerfLogImp::Counters::Rpc::Sync::started
std::uint64_t started
Definition: PerfLogImp.h:68
ripple::perf::PerfLogImp::stop_
bool stop_
Definition: PerfLogImp.h:146
ripple::perf::PerfLogImp::Counters::Rpc
RPC performance counters.
Definition: PerfLogImp.h:60
ripple::perf::PerfLogImp::Counters::Rpc::sync
Sync sync
Definition: PerfLogImp.h:75
ripple::perf::PerfLogImp::rpcEnd
void rpcEnd(std::string const &method, std::uint64_t const requestId, bool finish)
Definition: PerfLogImp.cpp:352
ripple::perf::PerfLogImp::Counters::Jq::Jq
Jq(std::string const &labelArg)
Definition: PerfLogImp.h:108
ripple::perf::PerfLogImp::rpcStart
void rpcStart(std::string const &method, std::uint64_t const requestId) override
Log start of RPC call.
Definition: PerfLogImp.cpp:333
ripple::perf::PerfLogImp::Counters::currentJson
Json::Value currentJson() const
Definition: PerfLogImp.cpp:177
condition_variable
ripple::perf::PerfLogImp::Counters
Track performance counters and currently executing tasks.
Definition: PerfLogImp.h:53
ripple::perf::PerfLogImp::run
void run()
Definition: PerfLogImp.cpp:267
mutex
ripple::JobType
JobType
Definition: Job.h:33
ripple::perf::PerfLogImp::setup_
const Setup setup_
Definition: PerfLogImp.h:136
ripple::perf::PerfLogImp::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: PerfLogImp.cpp:485
ripple::perf::PerfLogImp::Counters::Jq::sync
Sync sync
Definition: PerfLogImp.h:104
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:174
ripple::perf::PerfLogImp::Counters::Counters
Counters(std::vector< char const * > const &labels, JobTypes const &jobTypes)
Definition: PerfLogImp.cpp:41
ripple::perf::PerfLogImp::logFile_
std::ofstream logFile_
Definition: PerfLogImp.h:140
ripple::perf::PerfLogImp::counters_
Counters counters_
Definition: PerfLogImp.h:139
ripple::RPC::getHandlerNames
std::vector< char const * > getHandlerNames()
Return names of all methods.
Definition: Handler.cpp:261
ripple::perf::PerfLogImp::jobQueue
void jobQueue(JobType const type) override
Log queued job.
Definition: PerfLogImp.cpp:387
unordered_map
ripple::perf::PerfLogImp::Counters::Jq::Sync
Definition: PerfLogImp.h:92
ripple::perf::PerfLogImp::Counters::Rpc::mut
std::mutex mut
Definition: PerfLogImp.h:76
ripple::perf::PerfLogImp::Counters::workers_
int workers_
Definition: PerfLogImp.h:122
type_traits
ripple::perf::PerfLogImp::onStart
void onStart() override
Override called during start.
Definition: PerfLogImp.cpp:462
ripple::perf::PerfLogImp::resizeJobs
void resizeJobs(int const resize) override
Ensure enough room to store each currently executing job.
Definition: PerfLogImp.cpp:442
ripple::perf::PerfLogImp::Counters::countersJson
Json::Value countersJson() const
Definition: PerfLogImp.cpp:75
ripple::perf::PerfLogImp::report
void report()
Definition: PerfLogImp.cpp:292
ripple::perf::PerfLogImp::rpcError
void rpcError(std::string const &method, std::uint64_t const requestId) override
Log errored RPC call.
Definition: PerfLogImp.h:180
ripple::perf::PerfLogImp::Counters::Jq::Sync::queuedDuration
microseconds queuedDuration
Definition: PerfLogImp.h:100
Json::Value
Represents a JSON value.
Definition: json_value.h:145
string