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