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/basics/chrono.h>
26#include <xrpl/beast/utility/Journal.h>
27#include <xrpl/protocol/jss.h>
28#include <boost/asio/ip/host_name.hpp>
29#include <condition_variable>
30#include <cstdint>
31#include <fstream>
32#include <memory>
33#include <mutex>
34#include <string>
35#include <thread>
36#include <type_traits>
37#include <unordered_map>
38#include <utility>
39#include <vector>
40
41namespace ripple {
42namespace perf {
43
45template <typename T>
46struct Locked
47{
50
51 Locked() = default;
52 Locked(T const& value) : value(value)
53 {
54 }
55 Locked(T&& value) : value(std::move(value))
56 {
57 }
58 Locked(Locked const& rhs) : value(rhs.value)
59 {
60 }
61 Locked(Locked&& rhs) : value(std::move(rhs.value))
62 {
63 }
64};
65
69class PerfLogImp : public PerfLog
70{
74 struct Counters
75 {
76 public:
81 struct Rpc
82 {
83 // Counters for each time a method starts and then either
84 // finishes successfully or with an exception.
88 // Cumulative duration of all finished and errored method calls.
90 };
91
95 struct Jq
96 {
97 // Counters for each time a job is enqueued, begins to run,
98 // finishes.
102 // Cumulative duration of all jobs' queued and running times.
105 };
106
107 // rpc_ and jq_ do not need mutex protection because all
108 // keys and values are created before more threads are started.
115
116 Counters(std::set<char const*> const& labels, JobTypes const& jobTypes);
118 countersJson() const;
120 currentJson() const;
121 };
122
133 std::string const hostname_{boost::asio::ip::host_name()};
134 bool stop_{false};
135 bool rotate_{false};
136
137 void
138 openLog();
139 void
140 run();
141 void
142 report();
143 void
144 rpcEnd(
145 std::string const& method,
146 std::uint64_t const requestId,
147 bool finish);
148
149public:
151 Setup const& setup,
152 Application& app,
153 beast::Journal journal,
154 std::function<void()>&& signalStop);
155
156 ~PerfLogImp() override;
157
158 void
159 rpcStart(std::string const& method, std::uint64_t const requestId) override;
160
161 void
162 rpcFinish(std::string const& method, std::uint64_t const requestId) override
163 {
164 rpcEnd(method, requestId, true);
165 }
166
167 void
168 rpcError(std::string const& method, std::uint64_t const requestId) override
169 {
170 rpcEnd(method, requestId, false);
171 }
172
173 void
174 jobQueue(JobType const type) override;
175 void
176 jobStart(
177 JobType const type,
178 microseconds dur,
179 steady_time_point startTime,
180 int instance) 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 perf
209} // namespace ripple
210
211#endif // RIPPLE_BASICS_PERFLOGIMP_H
Represents a JSON value.
Definition: json_value.h:147
A generic endpoint for log messages.
Definition: Journal.h:59
static JobTypes const & instance()
Definition: JobTypes.h:129
Implementation class for PerfLog.
Definition: PerfLogImp.h:70
std::string const hostname_
Definition: PerfLogImp.h:133
void rpcEnd(std::string const &method, std::uint64_t const requestId, bool finish)
Definition: PerfLogImp.cpp:347
void resizeJobs(int const resize) override
Ensure enough room to store each currently executing job.
Definition: PerfLogImp.cpp:441
beast::Journal const j_
Definition: PerfLogImp.h:125
void rpcError(std::string const &method, std::uint64_t const requestId) override
Log errored RPC call.
Definition: PerfLogImp.h:168
std::function< void()> const signalStop_
Definition: PerfLogImp.h:126
Json::Value countersJson() const override
Render performance counters in Json.
Definition: PerfLogImp.h:185
void jobQueue(JobType const type) override
Log queued job.
Definition: PerfLogImp.cpp:383
void jobStart(JobType const type, microseconds dur, steady_time_point startTime, int instance) override
Log job executing.
Definition: PerfLogImp.cpp:397
void rpcStart(std::string const &method, std::uint64_t const requestId) override
Log start of RPC call.
Definition: PerfLogImp.cpp:328
void rotate() override
Rotate perf log file.
Definition: PerfLogImp.cpp:449
std::condition_variable cond_
Definition: PerfLogImp.h:131
void stop() override
Definition: PerfLogImp.cpp:467
void rpcFinish(std::string const &method, std::uint64_t const requestId) override
Log successful finish of RPC call.
Definition: PerfLogImp.h:162
void start() override
Definition: PerfLogImp.cpp:460
std::ofstream logFile_
Definition: PerfLogImp.h:128
void jobFinish(JobType const type, microseconds dur, int instance) override
Log job finishing.
Definition: PerfLogImp.cpp:421
system_time_point lastLog_
Definition: PerfLogImp.h:132
Json::Value currentJson() const override
Render currently executing jobs and RPC calls and durations in Json.
Definition: PerfLogImp.h:191
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
Definition: PerfLog.h:49
std::chrono::time_point< steady_clock > steady_time_point
Definition: PerfLog.h:53
std::chrono::microseconds microseconds
Definition: PerfLog.h:57
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:35
STL namespace.
A box coupling data with a mutex for locking access to it.
Definition: PerfLogImp.h:47
Locked(Locked const &rhs)
Definition: PerfLogImp.h:58
Locked(T &&value)
Definition: PerfLogImp.h:55
Locked(T const &value)
Definition: PerfLogImp.h:52
Locked(Locked &&rhs)
Definition: PerfLogImp.h:61
std::mutex mutex
Definition: PerfLogImp.h:49
Job Queue task performance counters.
Definition: PerfLogImp.h:96
RPC performance counters.
Definition: PerfLogImp.h:82
Track performance counters and currently executing tasks.
Definition: PerfLogImp.h:75
std::unordered_map< std::string, Locked< Rpc > > rpc_
Definition: PerfLogImp.h:109
std::vector< std::pair< JobType, steady_time_point > > jobs_
Definition: PerfLogImp.h:111
std::unordered_map< std::uint64_t, MethodStart > methods_
Definition: PerfLogImp.h:113
Json::Value countersJson() const
Definition: PerfLogImp.cpp:81
std::unordered_map< JobType, Locked< Jq > > jq_
Definition: PerfLogImp.h:110
Configuration from [perf] section of rippled.cfg.
Definition: PerfLog.h:63