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