1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
7 * Copyright (C) 2017 OVH
9 * This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License version 2.1, as published by the Free Software
12 * Foundation. See file COPYING.
17 #ifndef CEPH_COMMON_PERF_COUNTERS_H
18 #define CEPH_COMMON_PERF_COUNTERS_H
26 #include "common/perf_histogram.h"
27 #include "include/utime.h"
28 #include "common/ceph_mutex.h"
29 #include "common/ceph_time.h"
32 class PerfCountersBuilder
;
35 enum perfcounter_type_d
: uint8_t
38 PERFCOUNTER_TIME
= 0x1, // float (measuring seconds)
39 PERFCOUNTER_U64
= 0x2, // integer (note: either TIME or U64 *must* be set)
40 PERFCOUNTER_LONGRUNAVG
= 0x4, // paired counter + sum (time)
41 PERFCOUNTER_COUNTER
= 0x8, // counter (vs gauge)
42 PERFCOUNTER_HISTOGRAM
= 0x10, // histogram (vector) of values
51 /* Class for constructing a PerfCounters object.
53 * This class performs some validation that the parameters we have supplied are
54 * correct in create_perf_counters().
56 * In the future, we will probably get rid of the first/last arguments, since
57 * PerfCountersBuilder can deduce them itself.
59 class PerfCountersBuilder
62 PerfCountersBuilder(CephContext
*cct
, const std::string
&name
,
64 ~PerfCountersBuilder();
66 // prio values: higher is better, and higher values get included in
67 // 'ceph daemonperf' (and similar) results.
68 // Use of priorities enables us to add large numbers of counters
69 // internally without necessarily overwhelming consumers.
72 // 'interesting' is the default threshold for `daemonperf` output
74 // `useful` is the default threshold for transmission to ceph-mgr
75 // and inclusion in prometheus/influxdb plugin output
77 PRIO_UNINTERESTING
= 2,
80 void add_u64(int key
, const char *name
,
81 const char *description
=NULL
, const char *nick
= NULL
,
82 int prio
=0, int unit
=UNIT_NONE
);
83 void add_u64_counter(int key
, const char *name
,
84 const char *description
=NULL
,
85 const char *nick
= NULL
,
86 int prio
=0, int unit
=UNIT_NONE
);
87 void add_u64_avg(int key
, const char *name
,
88 const char *description
=NULL
,
89 const char *nick
= NULL
,
90 int prio
=0, int unit
=UNIT_NONE
);
91 void add_time(int key
, const char *name
,
92 const char *description
=NULL
,
93 const char *nick
= NULL
,
95 void add_time_avg(int key
, const char *name
,
96 const char *description
=NULL
,
97 const char *nick
= NULL
,
99 void add_u64_counter_histogram(
100 int key
, const char* name
,
101 PerfHistogramCommon::axis_config_d x_axis_config
,
102 PerfHistogramCommon::axis_config_d y_axis_config
,
103 const char *description
=NULL
,
104 const char* nick
= NULL
,
105 int prio
=0, int unit
=UNIT_NONE
);
107 void set_prio_default(int prio_
)
109 prio_default
= prio_
;
112 PerfCounters
* create_perf_counters();
114 PerfCountersBuilder(const PerfCountersBuilder
&rhs
);
115 PerfCountersBuilder
& operator=(const PerfCountersBuilder
&rhs
);
116 void add_impl(int idx
, const char *name
,
117 const char *description
, const char *nick
, int prio
, int ty
, int unit
=UNIT_NONE
,
118 unique_ptr
<PerfHistogram
<>> histogram
= nullptr);
120 PerfCounters
*m_perf_counters
;
122 int prio_default
= 0;
126 * A PerfCounters object is usually associated with a single subsystem.
127 * It contains counters which we modify to track performance and throughput
130 * PerfCounters can track several different types of values:
131 * 1) integer values & counters
132 * 2) floating-point values & counters
133 * 3) floating-point averages
134 * 4) 2D histograms of quantized value pairs
136 * The difference between values, counters and histograms is in how they are initialized
137 * and accessed. For a counter, use the inc(counter, amount) function (note
138 * that amount defaults to 1 if you don't set it). For a value, use the
139 * set(index, value) function. For histogram use the hinc(value1, value2) function.
140 * (For time, use the tinc and tset variants.)
142 * If for some reason you would like to reset your counters, you can do so using
143 * the set functions even if they are counters, and you can also
144 * increment your values if for some reason you wish to.
146 * For the time average, it returns the current value and
147 * the "avgcount" member when read off. avgcount is incremented when you call
148 * tinc. Calling tset on an average is an error and will assert out.
153 /** Represents a PerfCounters data element. */
154 struct perf_counter_data_any_d
{
155 perf_counter_data_any_d()
159 type(PERFCOUNTER_NONE
),
162 perf_counter_data_any_d(const perf_counter_data_any_d
& other
)
164 description(other
.description
),
168 u64(other
.u64
.load()) {
169 pair
<uint64_t,uint64_t> a
= other
.read_avg();
172 avgcount2
= a
.second
;
173 if (other
.histogram
) {
174 histogram
.reset(new PerfHistogram
<>(*other
.histogram
));
179 const char *description
;
182 enum perfcounter_type_d type
;
184 std::atomic
<uint64_t> u64
= { 0 };
185 std::atomic
<uint64_t> avgcount
= { 0 };
186 std::atomic
<uint64_t> avgcount2
= { 0 };
187 std::unique_ptr
<PerfHistogram
<>> histogram
;
191 if (type
!= PERFCOUNTER_U64
) {
201 // read <sum, count> safely by making sure the post- and pre-count
202 // are identical; in other words the whole loop needs to be run
203 // without any intervening calls to inc, set, or tinc.
204 pair
<uint64_t,uint64_t> read_avg() const {
209 } while (avgcount
!= count
);
210 return make_pair(sum
, count
);
214 template <typename T
>
216 pair
<uint64_t, T
> last
;
217 pair
<uint64_t, T
> cur
;
218 avg_tracker() : last(0, 0), cur(0, 0) {}
219 T
current_avg() const {
220 if (cur
.first
== last
.first
)
222 return (cur
.second
- last
.second
) / (cur
.first
- last
.first
);
224 void consume_next(const pair
<uint64_t, T
> &next
) {
232 void inc(int idx
, uint64_t v
= 1);
233 void dec(int idx
, uint64_t v
= 1);
234 void set(int idx
, uint64_t v
);
235 uint64_t get(int idx
) const;
237 void tset(int idx
, utime_t v
);
238 void tinc(int idx
, utime_t v
);
239 void tinc(int idx
, ceph::timespan v
);
240 utime_t
tget(int idx
) const;
242 void hinc(int idx
, int64_t x
, int64_t y
);
245 void dump_formatted(ceph::Formatter
*f
, bool schema
,
246 const std::string
&counter
= "") const {
247 dump_formatted_generic(f
, schema
, false, counter
);
249 void dump_formatted_histograms(ceph::Formatter
*f
, bool schema
,
250 const std::string
&counter
= "") const {
251 dump_formatted_generic(f
, schema
, true, counter
);
253 pair
<uint64_t, uint64_t> get_tavg_ns(int idx
) const;
255 const std::string
& get_name() const;
256 void set_name(std::string s
) {
260 /// adjust priority values by some value
261 void set_prio_adjust(int p
) {
265 int get_adjusted_priority(int p
) const {
266 return std::max(std::min(p
+ prio_adjust
,
267 (int)PerfCountersBuilder::PRIO_CRITICAL
),
272 PerfCounters(CephContext
*cct
, const std::string
&name
,
273 int lower_bound
, int upper_bound
);
274 PerfCounters(const PerfCounters
&rhs
);
275 PerfCounters
& operator=(const PerfCounters
&rhs
);
276 void dump_formatted_generic(ceph::Formatter
*f
, bool schema
, bool histograms
,
277 const std::string
&counter
= "") const;
279 typedef std::vector
<perf_counter_data_any_d
> perf_counter_data_vec_t
;
289 const std::string m_lock_name
;
290 /** Protects m_data */
294 perf_counter_data_vec_t m_data
;
296 friend class PerfCountersBuilder
;
297 friend class PerfCountersCollectionImpl
;
300 class SortPerfCountersByName
{
302 bool operator()(const PerfCounters
* lhs
, const PerfCounters
* rhs
) const {
303 return (lhs
->get_name() < rhs
->get_name());
307 typedef std::set
<PerfCounters
*, SortPerfCountersByName
> perf_counters_set_t
;
310 * PerfCountersCollectionImp manages PerfCounters objects for a Ceph process.
312 class PerfCountersCollectionImpl
315 PerfCountersCollectionImpl();
316 ~PerfCountersCollectionImpl();
317 void add(PerfCounters
*l
);
318 void remove(PerfCounters
*l
);
320 bool reset(const std::string
&name
);
322 void dump_formatted(ceph::Formatter
*f
, bool schema
,
323 const std::string
&logger
= "",
324 const std::string
&counter
= "") const {
325 dump_formatted_generic(f
, schema
, false, logger
, counter
);
328 void dump_formatted_histograms(ceph::Formatter
*f
, bool schema
,
329 const std::string
&logger
= "",
330 const std::string
&counter
= "") const {
331 dump_formatted_generic(f
, schema
, true, logger
, counter
);
334 // A reference to a perf_counter_data_any_d, with an accompanying
335 // pointer to the enclosing PerfCounters, in order that the consumer
336 // can see the prio_adjust
340 PerfCounters::perf_counter_data_any_d
*data
;
341 PerfCounters
*perf_counters
;
343 typedef std::map
<std::string
,
344 PerfCounterRef
> CounterMap
;
346 void with_counters(std::function
<void(const CounterMap
&)>) const;
349 void dump_formatted_generic(ceph::Formatter
*f
, bool schema
, bool histograms
,
350 const std::string
&logger
= "",
351 const std::string
&counter
= "") const;
353 perf_counters_set_t m_loggers
;
360 const ceph::real_clock::time_point start
;
361 PerfCounters
* const counters
;
365 PerfGuard(PerfCounters
* const counters
,
367 : start(ceph::real_clock::now()),
373 counters
->tinc(event
, ceph::real_clock::now() - start
);