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:
126 Setup const& setup,
127 Application& app,
128 beast::Journal journal,
129 std::function<void()>&& signalStop);
130
131 ~PerfLogImp() override;
132
133 void
134 rpcStart(std::string const& method, std::uint64_t const requestId) override;
135
136 void
137 rpcFinish(std::string const& method, std::uint64_t const requestId) override
138 {
139 rpcEnd(method, requestId, true);
140 }
141
142 void
143 rpcError(std::string const& method, std::uint64_t const requestId) override
144 {
145 rpcEnd(method, requestId, false);
146 }
147
148 void
149 jobQueue(JobType const type) override;
150 void
151 jobStart(JobType const type, microseconds dur, steady_time_point startTime, int instance)
152 override;
153 void
154 jobFinish(JobType const type, microseconds dur, int instance) override;
155
157 countersJson() const override
158 {
159 return counters_.countersJson();
160 }
161
163 currentJson() const override
164 {
165 return counters_.currentJson();
166 }
167
168 void
169 resizeJobs(int const resize) override;
170 void
171 rotate() override;
172
173 void
174 start() override;
175
176 void
177 stop() override;
178};
179
180} // namespace perf
181} // 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:105
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:163
void rpcFinish(std::string const &method, std::uint64_t const requestId) override
Log successful finish of RPC call.
Definition PerfLogImp.h:137
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:157
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:143
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:260
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