xrpld
Loading...
Searching...
No Matches
PerfLogImp.h
1#pragma once
2
3#include <xrpl/basics/StringUtilities.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/core/Job.h>
6#include <xrpl/core/JobTypes.h>
7#include <xrpl/core/PerfLog.h>
8#include <xrpl/json/json_value.h>
9
10#include <boost/asio/ip/host_name.hpp>
11
12#include <condition_variable>
13#include <cstdint>
14#include <fstream>
15#include <functional>
16#include <mutex>
17#include <span>
18#include <string>
19#include <string_view>
20#include <thread>
21#include <unordered_map>
22#include <utility>
23#include <vector>
24
25namespace xrpl::perf {
26
30template <typename T>
31struct Locked
32{
35
36 Locked() = default;
37 Locked(T const& value) : value(value)
38 {
39 }
40 Locked(T&& value) : value(std::move(value))
41 {
42 }
43 Locked(Locked const& rhs) : value(rhs.value)
44 {
45 }
46 Locked(Locked&& rhs) : value(std::move(rhs.value))
47 {
48 }
49};
50
54class PerfLogImp : public PerfLog
55{
59 struct Counters
60 {
61 public:
63
67 struct Rpc
68 {
69 // Counters for each time a method starts and then either
70 // finishes successfully or with an exception.
74 // Cumulative duration of all finished and errored method calls.
76 };
77
81 struct Jq
82 {
83 // Counters for each time a job is enqueued, begins to run,
84 // finishes.
88 // Cumulative duration of all jobs' queued and running times.
91 };
92
93 // rpc and jq do not need mutex protection because all
94 // keys and values are created before more threads are started.
95 //
96 // Every key views the characters of a name from the methodNames constructor
97 // parameter below, which the caller guarantees outlive this object, so the
98 // map copies no name to store one and needs no string to look one up.
100
101 // The same names, in the order the caller gave them, and still carrying the
102 // proof that each reaches its terminating null. countersJson() walks these
103 // rather than rpc, so that it can report a key as a C string. Held by value,
104 // so that a caller may build the range it passes on the fly: only the names
105 // have to outlive this object, not the container that carried them.
110 // Each view is a key of rpc above, not the argument rpcStart() received, so
111 // currentJson() may read it as a C string.
114
115 Counters(std::span<NullTerminatedView const> methodNames, JobTypes const& jobTypes);
117 countersJson() const;
119 currentJson() const;
120 };
121
132 std::string const hostname_{boost::asio::ip::host_name()};
133 bool stop_{false};
134 bool rotate_{false};
135
136 void
137 openLog();
138 void
139 run();
140 void
141 report();
142 void
143 rpcEnd(std::string_view method, std::uint64_t const requestId, bool finish);
144
145public:
153 Setup setup,
154 Application& app,
156 beast::Journal journal,
157 std::function<void()>&& signalStop);
158
159 ~PerfLogImp() override;
160
161 void
162 rpcStart(std::string_view method, std::uint64_t const requestId) override;
163
164 void
165 rpcFinish(std::string_view method, std::uint64_t const requestId) override
166 {
167 rpcEnd(method, requestId, true);
168 }
169
170 void
171 rpcError(std::string_view method, std::uint64_t const requestId) override
172 {
173 rpcEnd(method, requestId, false);
174 }
175
176 void
177 jobQueue(JobType const type) override;
178 void
179 jobStart(JobType const type, Microseconds dur, SteadyTimePoint startTime, int instance)
180 override;
181 void
182 jobFinish(JobType const type, Microseconds dur, int instance) override;
183
185 countersJson() const override
186 {
187 return counters_.countersJson();
188 }
189
191 currentJson() const override
192 {
193 return counters_.currentJson();
194 }
195
196 void
197 resizeJobs(int const resize) override;
198 void
199 rotate() override;
200
201 void
202 start() override;
203
204 void
205 stop() override;
206};
207
208} // namespace xrpl::perf
A generic endpoint for log messages.
Definition Journal.h:44
Represents a JSON value.
Definition json_value.h:117
PerfLogImp(Setup setup, Application &app, std::span< NullTerminatedView const > methodNames, beast::Journal journal, std::function< void()> &&signalStop)
std::condition_variable cond_
Definition PerfLogImp.h:130
void start() override
std::ofstream logFile_
Definition PerfLogImp.h:127
void rpcEnd(std::string_view method, std::uint64_t const requestId, bool finish)
void jobQueue(JobType const type) override
Log queued job.
void resizeJobs(int const resize) override
Ensure enough room to store each currently executing job.
void jobStart(JobType const type, Microseconds dur, SteadyTimePoint startTime, int instance) override
Log job executing.
void rpcError(std::string_view method, std::uint64_t const requestId) override
Log errored RPC call.
Definition PerfLogImp.h:171
void rpcFinish(std::string_view method, std::uint64_t const requestId) override
Log successful finish of RPC call.
Definition PerfLogImp.h:165
beast::Journal const j_
Definition PerfLogImp.h:124
SystemTimePoint lastLog_
Definition PerfLogImp.h:131
json::Value currentJson() const override
Render currently executing jobs and RPC calls and durations in Json.
Definition PerfLogImp.h:191
void stop() override
void rotate() override
Rotate perf log file.
std::function< void()> const signalStop_
Definition PerfLogImp.h:125
void rpcStart(std::string_view method, std::uint64_t const requestId) override
Log start of RPC call.
void jobFinish(JobType const type, Microseconds dur, int instance) override
Log job finishing.
json::Value countersJson() const override
Render performance counters in Json.
Definition PerfLogImp.h:185
std::string const hostname_
Definition PerfLogImp.h:132
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
Definition PerfLog.h:33
std::chrono::time_point< SystemClock > SystemTimePoint
Definition PerfLog.h:38
std::chrono::microseconds Microseconds
Definition PerfLog.h:41
std::chrono::time_point< SteadyClock > SteadyTimePoint
Definition PerfLog.h:37
STL namespace.
Dummy class for unit tests.
Definition Workers.h:14
JobType
Definition Job.h:21
Locked(T const &value)
Definition PerfLogImp.h:37
Locked(Locked const &rhs)
Definition PerfLogImp.h:43
Locked(Locked &&rhs)
Definition PerfLogImp.h:46
std::mutex mutex
Definition PerfLogImp.h:34
Locked(T &&value)
Definition PerfLogImp.h:40
Job Queue task performance counters.
Definition PerfLogImp.h:82
RPC performance counters.
Definition PerfLogImp.h:68
Track performance counters and currently executing tasks.
Definition PerfLogImp.h:60
std::vector< NullTerminatedView > labels
Definition PerfLogImp.h:106
std::unordered_map< std::string_view, Locked< Rpc > > rpc
Definition PerfLogImp.h:99
std::pair< std::string_view, SteadyTimePoint > MethodStart
Definition PerfLogImp.h:62
json::Value countersJson() const
json::Value currentJson() const
std::vector< std::pair< JobType, SteadyTimePoint > > jobs
Definition PerfLogImp.h:108
std::unordered_map< JobType, Locked< Jq > > jq
Definition PerfLogImp.h:107
std::unordered_map< std::uint64_t, MethodStart > methods
Definition PerfLogImp.h:112
Counters(std::span< NullTerminatedView const > methodNames, JobTypes const &jobTypes)
Configuration from [perf] section of xrpld.cfg.
Definition PerfLog.h:47