rippled
Loading...
Searching...
No Matches
PerfLogImp.h
1#ifndef XRPL_BASICS_PERFLOGIMP_H
2#define XRPL_BASICS_PERFLOGIMP_H
3
4#include <xrpld/rpc/detail/Handler.h>
5
6#include <xrpl/beast/utility/Journal.h>
7#include <xrpl/core/PerfLog.h>
8
9#include <boost/asio/ip/host_name.hpp>
10
11#include <condition_variable>
12#include <cstdint>
13#include <fstream>
14#include <memory>
15#include <string>
16#include <thread>
17#include <unordered_map>
18#include <vector>
19
20namespace xrpl {
21namespace perf {
22
24template <typename T>
25struct Locked
26{
29
30 Locked() = default;
31 Locked(T const& value) : value(value)
32 {
33 }
34 Locked(T&& value) : value(std::move(value))
35 {
36 }
37 Locked(Locked const& rhs) : value(rhs.value)
38 {
39 }
40 Locked(Locked&& rhs) : value(std::move(rhs.value))
41 {
42 }
43};
44
48class PerfLogImp : public PerfLog
49{
53 struct Counters
54 {
55 public:
60 struct Rpc
61 {
62 // Counters for each time a method starts and then either
63 // finishes successfully or with an exception.
67 // Cumulative duration of all finished and errored method calls.
69 };
70
74 struct Jq
75 {
76 // Counters for each time a job is enqueued, begins to run,
77 // finishes.
81 // Cumulative duration of all jobs' queued and running times.
84 };
85
86 // rpc_ and jq_ do not need mutex protection because all
87 // keys and values are created before more threads are started.
94
95 Counters(std::set<char const*> const& labels, JobTypes const& jobTypes);
97 countersJson() const;
99 currentJson() const;
100 };
101
112 std::string const hostname_{boost::asio::ip::host_name()};
113 bool stop_{false};
114 bool rotate_{false};
115
116 void
117 openLog();
118 void
119 run();
120 void
121 report();
122 void
123 rpcEnd(
124 std::string const& method,
125 std::uint64_t const requestId,
126 bool finish);
127
128public:
130 Setup const& setup,
131 Application& app,
132 beast::Journal journal,
133 std::function<void()>&& signalStop);
134
135 ~PerfLogImp() override;
136
137 void
138 rpcStart(std::string const& method, std::uint64_t const requestId) override;
139
140 void
141 rpcFinish(std::string const& method, std::uint64_t const requestId) override
142 {
143 rpcEnd(method, requestId, true);
144 }
145
146 void
147 rpcError(std::string const& method, std::uint64_t const requestId) override
148 {
149 rpcEnd(method, requestId, false);
150 }
151
152 void
153 jobQueue(JobType const type) override;
154 void
155 jobStart(
156 JobType const type,
157 microseconds dur,
158 steady_time_point startTime,
159 int instance) override;
160 void
161 jobFinish(JobType const type, microseconds dur, int instance) override;
162
164 countersJson() const override
165 {
166 return counters_.countersJson();
167 }
168
170 currentJson() const override
171 {
172 return counters_.currentJson();
173 }
174
175 void
176 resizeJobs(int const resize) override;
177 void
178 rotate() override;
179
180 void
181 start() override;
182
183 void
184 stop() override;
185};
186
187} // namespace perf
188} // namespace xrpl
189
190#endif // XRPL_BASICS_PERFLOGIMP_H
Represents a JSON value.
Definition json_value.h:131
A generic endpoint for log messages.
Definition Journal.h:41
static JobTypes const & instance()
Definition JobTypes.h:109
Implementation class for PerfLog.
Definition PerfLogImp.h:49
std::condition_variable cond_
Definition PerfLogImp.h:110
void jobFinish(JobType const type, microseconds dur, int instance) override
Log job finishing.
void start() override
std::ofstream logFile_
Definition PerfLogImp.h:107
void rpcStart(std::string const &method, std::uint64_t const requestId) override
Log start of RPC call.
void jobQueue(JobType const type) override
Log queued job.
void resizeJobs(int const resize) override
Ensure enough room to store each currently executing job.
system_time_point lastLog_
Definition PerfLogImp.h:111
beast::Journal const j_
Definition PerfLogImp.h:104
Json::Value currentJson() const override
Render currently executing jobs and RPC calls and durations in Json.
Definition PerfLogImp.h:170
void rpcFinish(std::string const &method, std::uint64_t const requestId) override
Log successful finish of RPC call.
Definition PerfLogImp.h:141
void rpcEnd(std::string const &method, std::uint64_t const requestId, bool finish)
void stop() override
void rotate() override
Rotate perf log file.
std::function< void()> const signalStop_
Definition PerfLogImp.h:105
void jobStart(JobType const type, microseconds dur, steady_time_point startTime, int instance) override
Log job executing.
Json::Value countersJson() const override
Render performance counters in Json.
Definition PerfLogImp.h:164
std::string const hostname_
Definition PerfLogImp.h:112
void rpcError(std::string const &method, std::uint64_t const requestId) override
Log errored RPC call.
Definition PerfLogImp.h:147
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
Definition PerfLog.h:32
std::chrono::microseconds microseconds
Definition PerfLog.h:40
std::chrono::time_point< steady_clock > steady_time_point
Definition PerfLog.h:36
STL namespace.
std::set< char const * > getHandlerNames()
Return names of all methods.
Definition Handler.cpp:301
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
JobType
Definition Job.h:15
A box coupling data with a mutex for locking access to it.
Definition PerfLogImp.h:26
Locked(T const &value)
Definition PerfLogImp.h:31
Locked(Locked const &rhs)
Definition PerfLogImp.h:37
Locked(Locked &&rhs)
Definition PerfLogImp.h:40
std::mutex mutex
Definition PerfLogImp.h:28
Locked(T &&value)
Definition PerfLogImp.h:34
Job Queue task performance counters.
Definition PerfLogImp.h:75
RPC performance counters.
Definition PerfLogImp.h:61
Track performance counters and currently executing tasks.
Definition PerfLogImp.h:54
std::unordered_map< JobType, Locked< Jq > > jq_
Definition PerfLogImp.h:89
std::vector< std::pair< JobType, steady_time_point > > jobs_
Definition PerfLogImp.h:90
Json::Value currentJson() const
std::unordered_map< std::uint64_t, MethodStart > methods_
Definition PerfLogImp.h:92
std::unordered_map< std::string, Locked< Rpc > > rpc_
Definition PerfLogImp.h:88
Json::Value countersJson() const
Configuration from [perf] section of xrpld.cfg.
Definition PerfLog.h:46