rippled
Loading...
Searching...
No Matches
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 <xrpld/perflog/PerfLog.h>
24#include <xrpld/rpc/detail/Handler.h>
25#include <xrpl/beast/utility/Journal.h>
26
27#include <boost/asio/ip/host_name.hpp>
28
29#include <condition_variable>
30#include <cstdint>
31#include <fstream>
32#include <memory>
33#include <string>
34#include <thread>
35#include <unordered_map>
36#include <vector>
37
38namespace ripple {
39namespace perf {
40
42template <typename T>
43struct Locked
44{
47
48 Locked() = default;
49 Locked(T const& value) : value(value)
50 {
51 }
52 Locked(T&& value) : value(std::move(value))
53 {
54 }
55 Locked(Locked const& rhs) : value(rhs.value)
56 {
57 }
58 Locked(Locked&& rhs) : value(std::move(rhs.value))
59 {
60 }
61};
62
66class PerfLogImp : public PerfLog
67{
71 struct Counters
72 {
73 public:
78 struct Rpc
79 {
80 // Counters for each time a method starts and then either
81 // finishes successfully or with an exception.
85 // Cumulative duration of all finished and errored method calls.
87 };
88
92 struct Jq
93 {
94 // Counters for each time a job is enqueued, begins to run,
95 // finishes.
99 // Cumulative duration of all jobs' queued and running times.
102 };
103
104 // rpc_ and jq_ do not need mutex protection because all
105 // keys and values are created before more threads are started.
112
113 Counters(std::set<char const*> const& labels, JobTypes const& jobTypes);
115 countersJson() const;
117 currentJson() const;
118 };
119
130 std::string const hostname_{boost::asio::ip::host_name()};
131 bool stop_{false};
132 bool rotate_{false};
133
134 void
135 openLog();
136 void
137 run();
138 void
139 report();
140 void
141 rpcEnd(
142 std::string const& method,
143 std::uint64_t const requestId,
144 bool finish);
145
146public:
148 Setup const& setup,
149 Application& app,
150 beast::Journal journal,
151 std::function<void()>&& signalStop);
152
153 ~PerfLogImp() override;
154
155 void
156 rpcStart(std::string const& method, std::uint64_t const requestId) override;
157
158 void
159 rpcFinish(std::string const& method, std::uint64_t const requestId) override
160 {
161 rpcEnd(method, requestId, true);
162 }
163
164 void
165 rpcError(std::string const& method, std::uint64_t const requestId) override
166 {
167 rpcEnd(method, requestId, false);
168 }
169
170 void
171 jobQueue(JobType const type) override;
172 void
173 jobStart(
174 JobType const type,
175 microseconds dur,
176 steady_time_point startTime,
177 int instance) override;
178 void
179 jobFinish(JobType const type, microseconds dur, int instance) override;
180
182 countersJson() const override
183 {
184 return counters_.countersJson();
185 }
186
188 currentJson() const override
189 {
190 return counters_.currentJson();
191 }
192
193 void
194 resizeJobs(int const resize) override;
195 void
196 rotate() override;
197
198 void
199 start() override;
200
201 void
202 stop() override;
203};
204
205} // namespace perf
206} // namespace ripple
207
208#endif // RIPPLE_BASICS_PERFLOGIMP_H
Represents a JSON value.
Definition: json_value.h:148
A generic endpoint for log messages.
Definition: Journal.h:60
static JobTypes const & instance()
Definition: JobTypes.h:128
Implementation class for PerfLog.
Definition: PerfLogImp.h:67
std::string const hostname_
Definition: PerfLogImp.h:130
void rpcEnd(std::string const &method, std::uint64_t const requestId, bool finish)
Definition: PerfLogImp.cpp:345
void resizeJobs(int const resize) override
Ensure enough room to store each currently executing job.
Definition: PerfLogImp.cpp:439
beast::Journal const j_
Definition: PerfLogImp.h:122
void rpcError(std::string const &method, std::uint64_t const requestId) override
Log errored RPC call.
Definition: PerfLogImp.h:165
std::function< void()> const signalStop_
Definition: PerfLogImp.h:123
Json::Value countersJson() const override
Render performance counters in Json.
Definition: PerfLogImp.h:182
void jobQueue(JobType const type) override
Log queued job.
Definition: PerfLogImp.cpp:381
void jobStart(JobType const type, microseconds dur, steady_time_point startTime, int instance) override
Log job executing.
Definition: PerfLogImp.cpp:395
void rpcStart(std::string const &method, std::uint64_t const requestId) override
Log start of RPC call.
Definition: PerfLogImp.cpp:326
void rotate() override
Rotate perf log file.
Definition: PerfLogImp.cpp:447
std::condition_variable cond_
Definition: PerfLogImp.h:128
void stop() override
Definition: PerfLogImp.cpp:465
void rpcFinish(std::string const &method, std::uint64_t const requestId) override
Log successful finish of RPC call.
Definition: PerfLogImp.h:159
void start() override
Definition: PerfLogImp.cpp:458
std::ofstream logFile_
Definition: PerfLogImp.h:125
void jobFinish(JobType const type, microseconds dur, int instance) override
Log job finishing.
Definition: PerfLogImp.cpp:419
system_time_point lastLog_
Definition: PerfLogImp.h:129
Json::Value currentJson() const override
Render currently executing jobs and RPC calls and durations in Json.
Definition: PerfLogImp.h:188
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
Definition: PerfLog.h:51
std::chrono::time_point< steady_clock > steady_time_point
Definition: PerfLog.h:55
std::chrono::microseconds microseconds
Definition: PerfLog.h:59
std::set< char const * > getHandlerNames()
Return names of all methods.
Definition: Handler.cpp:316
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
JobType
Definition: Job.h:34
STL namespace.
A box coupling data with a mutex for locking access to it.
Definition: PerfLogImp.h:44
Locked(Locked const &rhs)
Definition: PerfLogImp.h:55
Locked(T &&value)
Definition: PerfLogImp.h:52
Locked(T const &value)
Definition: PerfLogImp.h:49
Locked(Locked &&rhs)
Definition: PerfLogImp.h:58
std::mutex mutex
Definition: PerfLogImp.h:46
Job Queue task performance counters.
Definition: PerfLogImp.h:93
RPC performance counters.
Definition: PerfLogImp.h:79
Track performance counters and currently executing tasks.
Definition: PerfLogImp.h:72
std::unordered_map< std::string, Locked< Rpc > > rpc_
Definition: PerfLogImp.h:106
std::vector< std::pair< JobType, steady_time_point > > jobs_
Definition: PerfLogImp.h:108
std::unordered_map< std::uint64_t, MethodStart > methods_
Definition: PerfLogImp.h:110
Json::Value countersJson() const
Definition: PerfLogImp.cpp:79
std::unordered_map< JobType, Locked< Jq > > jq_
Definition: PerfLogImp.h:107
Configuration from [perf] section of rippled.cfg.
Definition: PerfLog.h:65