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