]> git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/OSDPerfMetricTypes.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / mgr / OSDPerfMetricTypes.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef OSD_PERF_METRIC_H_
5 #define OSD_PERF_METRIC_H_
6
7 #include "include/denc.h"
8 #include "include/stringify.h"
9
10 #include <regex>
11
12 typedef std::vector<std::string> OSDPerfMetricSubKey; // array of regex match
13 typedef std::vector<OSDPerfMetricSubKey> OSDPerfMetricKey;
14
15 enum class OSDPerfMetricSubKeyType : uint8_t {
16 CLIENT_ID = 0,
17 CLIENT_ADDRESS = 1,
18 POOL_ID = 2,
19 NAMESPACE = 3,
20 OSD_ID = 4,
21 PG_ID = 5,
22 OBJECT_NAME = 6,
23 SNAP_ID = 7,
24 };
25
26 struct OSDPerfMetricSubKeyDescriptor {
27 OSDPerfMetricSubKeyType type = static_cast<OSDPerfMetricSubKeyType>(-1);
28 std::string regex_str;
29 std::regex regex;
30
31 bool is_supported() const {
32 switch (type) {
33 case OSDPerfMetricSubKeyType::CLIENT_ID:
34 case OSDPerfMetricSubKeyType::CLIENT_ADDRESS:
35 case OSDPerfMetricSubKeyType::POOL_ID:
36 case OSDPerfMetricSubKeyType::NAMESPACE:
37 case OSDPerfMetricSubKeyType::OSD_ID:
38 case OSDPerfMetricSubKeyType::PG_ID:
39 case OSDPerfMetricSubKeyType::OBJECT_NAME:
40 case OSDPerfMetricSubKeyType::SNAP_ID:
41 return true;
42 default:
43 return false;
44 }
45 }
46
47 OSDPerfMetricSubKeyDescriptor() {
48 }
49
50 OSDPerfMetricSubKeyDescriptor(OSDPerfMetricSubKeyType type,
51 const std::string regex)
52 : type(type), regex_str(regex) {
53 }
54
55 bool operator<(const OSDPerfMetricSubKeyDescriptor &other) const {
56 if (type < other.type) {
57 return true;
58 }
59 if (type > other.type) {
60 return false;
61 }
62 return regex_str < other.regex_str;
63 }
64
65 DENC(OSDPerfMetricSubKeyDescriptor, v, p) {
66 DENC_START(1, 1, p);
67 denc(v.type, p);
68 denc(v.regex_str, p);
69 DENC_FINISH(p);
70 }
71 };
72 WRITE_CLASS_DENC(OSDPerfMetricSubKeyDescriptor)
73
74 std::ostream& operator<<(std::ostream& os,
75 const OSDPerfMetricSubKeyDescriptor &d);
76
77 typedef std::vector<OSDPerfMetricSubKeyDescriptor> OSDPerfMetricKeyDescriptor;
78
79 template<>
80 struct denc_traits<OSDPerfMetricKeyDescriptor> {
81 static constexpr bool supported = true;
82 static constexpr bool bounded = false;
83 static constexpr bool featured = false;
84 static constexpr bool need_contiguous = true;
85 static void bound_encode(const OSDPerfMetricKeyDescriptor& v, size_t& p) {
86 p += sizeof(uint32_t);
87 const auto size = v.size();
88 if (size) {
89 size_t per = 0;
90 denc(v.front(), per);
91 p += per * size;
92 }
93 }
94 static void encode(const OSDPerfMetricKeyDescriptor& v,
95 bufferlist::contiguous_appender& p) {
96 denc_varint(v.size(), p);
97 for (auto& i : v) {
98 denc(i, p);
99 }
100 }
101 static void decode(OSDPerfMetricKeyDescriptor& v,
102 bufferptr::const_iterator& p) {
103 unsigned num;
104 denc_varint(num, p);
105 v.clear();
106 v.reserve(num);
107 for (unsigned i=0; i < num; ++i) {
108 OSDPerfMetricSubKeyDescriptor d;
109 denc(d, p);
110 if (!d.is_supported()) {
111 v.clear();
112 return;
113 }
114 try {
115 d.regex = d.regex_str.c_str();
116 } catch (const std::regex_error& e) {
117 v.clear();
118 return;
119 }
120 if (d.regex.mark_count() == 0) {
121 v.clear();
122 return;
123 }
124 v.push_back(std::move(d));
125 }
126 }
127 };
128
129 typedef std::pair<uint64_t,uint64_t> PerformanceCounter;
130 typedef std::vector<PerformanceCounter> PerformanceCounters;
131
132 enum class PerformanceCounterType : uint8_t {
133 OPS = 0,
134 WRITE_OPS = 1,
135 READ_OPS = 2,
136 BYTES = 3,
137 WRITE_BYTES = 4,
138 READ_BYTES = 5,
139 LATENCY = 6,
140 WRITE_LATENCY = 7,
141 READ_LATENCY = 8,
142 };
143
144 struct PerformanceCounterDescriptor {
145 PerformanceCounterType type = static_cast<PerformanceCounterType>(-1);
146
147 bool is_supported() const {
148 switch (type) {
149 case PerformanceCounterType::OPS:
150 case PerformanceCounterType::WRITE_OPS:
151 case PerformanceCounterType::READ_OPS:
152 case PerformanceCounterType::BYTES:
153 case PerformanceCounterType::WRITE_BYTES:
154 case PerformanceCounterType::READ_BYTES:
155 case PerformanceCounterType::LATENCY:
156 case PerformanceCounterType::WRITE_LATENCY:
157 case PerformanceCounterType::READ_LATENCY:
158 return true;
159 default:
160 return false;
161 }
162 }
163
164 PerformanceCounterDescriptor() {
165 }
166
167 PerformanceCounterDescriptor(PerformanceCounterType type) : type(type) {
168 }
169
170 bool operator<(const PerformanceCounterDescriptor &other) const {
171 return type < other.type;
172 }
173
174 bool operator==(const PerformanceCounterDescriptor &other) const {
175 return type == other.type;
176 }
177
178 bool operator!=(const PerformanceCounterDescriptor &other) const {
179 return type != other.type;
180 }
181
182 DENC(PerformanceCounterDescriptor, v, p) {
183 DENC_START(1, 1, p);
184 denc(v.type, p);
185 DENC_FINISH(p);
186 }
187
188 void pack_counter(const PerformanceCounter &c, bufferlist *bl) const;
189 void unpack_counter(bufferlist::const_iterator& bl,
190 PerformanceCounter *c) const;
191 };
192 WRITE_CLASS_DENC(PerformanceCounterDescriptor)
193
194 std::ostream& operator<<(std::ostream& os,
195 const PerformanceCounterDescriptor &d);
196
197 typedef std::vector<PerformanceCounterDescriptor> PerformanceCounterDescriptors;
198
199 template<>
200 struct denc_traits<PerformanceCounterDescriptors> {
201 static constexpr bool supported = true;
202 static constexpr bool bounded = false;
203 static constexpr bool featured = false;
204 static constexpr bool need_contiguous = true;
205 static void bound_encode(const PerformanceCounterDescriptors& v, size_t& p) {
206 p += sizeof(uint32_t);
207 const auto size = v.size();
208 if (size) {
209 size_t per = 0;
210 denc(v.front(), per);
211 p += per * size;
212 }
213 }
214 static void encode(const PerformanceCounterDescriptors& v,
215 bufferlist::contiguous_appender& p) {
216 denc_varint(v.size(), p);
217 for (auto& i : v) {
218 denc(i, p);
219 }
220 }
221 static void decode(PerformanceCounterDescriptors& v,
222 bufferptr::const_iterator& p) {
223 unsigned num;
224 denc_varint(num, p);
225 v.clear();
226 v.reserve(num);
227 for (unsigned i=0; i < num; ++i) {
228 PerformanceCounterDescriptor d;
229 denc(d, p);
230 if (d.is_supported()) {
231 v.push_back(std::move(d));
232 }
233 }
234 }
235 };
236
237 struct OSDPerfMetricLimit {
238 PerformanceCounterDescriptor order_by;
239 uint64_t max_count = 0;
240
241 OSDPerfMetricLimit() {
242 }
243
244 OSDPerfMetricLimit(const PerformanceCounterDescriptor &order_by,
245 uint64_t max_count)
246 : order_by(order_by), max_count(max_count) {
247 }
248
249 bool operator<(const OSDPerfMetricLimit &other) const {
250 if (order_by != other.order_by) {
251 return order_by < other.order_by;
252 }
253 return max_count < other.max_count;
254 }
255
256 DENC(OSDPerfMetricLimit, v, p) {
257 DENC_START(1, 1, p);
258 denc(v.order_by, p);
259 denc(v.max_count, p);
260 DENC_FINISH(p);
261 }
262 };
263 WRITE_CLASS_DENC(OSDPerfMetricLimit)
264
265 std::ostream& operator<<(std::ostream& os, const OSDPerfMetricLimit &limit);
266
267 typedef std::set<OSDPerfMetricLimit> OSDPerfMetricLimits;
268
269 typedef int OSDPerfMetricQueryID;
270
271 struct OSDPerfMetricQuery {
272 bool operator<(const OSDPerfMetricQuery &other) const {
273 if (key_descriptor < other.key_descriptor) {
274 return true;
275 }
276 if (key_descriptor > other.key_descriptor) {
277 return false;
278 }
279 return (performance_counter_descriptors <
280 other.performance_counter_descriptors);
281 }
282
283 OSDPerfMetricQuery() {
284 }
285
286 OSDPerfMetricQuery(
287 const OSDPerfMetricKeyDescriptor &key_descriptor,
288 const PerformanceCounterDescriptors &performance_counter_descriptors)
289 : key_descriptor(key_descriptor),
290 performance_counter_descriptors(performance_counter_descriptors) {
291 }
292
293 template <typename L>
294 bool get_key(L&& get_sub_key, OSDPerfMetricKey *key) const {
295 for (auto &sub_key_descriptor : key_descriptor) {
296 OSDPerfMetricSubKey sub_key;
297 if (!get_sub_key(sub_key_descriptor, &sub_key)) {
298 return false;
299 }
300 key->push_back(sub_key);
301 }
302 return true;
303 }
304
305 DENC(OSDPerfMetricQuery, v, p) {
306 DENC_START(1, 1, p);
307 denc(v.key_descriptor, p);
308 denc(v.performance_counter_descriptors, p);
309 DENC_FINISH(p);
310 }
311
312 void get_performance_counter_descriptors(
313 PerformanceCounterDescriptors *descriptors) const {
314 *descriptors = performance_counter_descriptors;
315 }
316
317 template <typename L>
318 void update_counters(L &&update_counter,
319 PerformanceCounters *counters) const {
320 auto it = counters->begin();
321 for (auto &descriptor : performance_counter_descriptors) {
322 // TODO: optimize
323 if (it == counters->end()) {
324 counters->push_back(PerformanceCounter());
325 it = std::prev(counters->end());
326 }
327 update_counter(descriptor, &(*it));
328 it++;
329 }
330 }
331
332 void pack_counters(const PerformanceCounters &counters, bufferlist *bl) const;
333
334 OSDPerfMetricKeyDescriptor key_descriptor;
335 PerformanceCounterDescriptors performance_counter_descriptors;
336 };
337 WRITE_CLASS_DENC(OSDPerfMetricQuery)
338
339 std::ostream& operator<<(std::ostream& os, const OSDPerfMetricQuery &query);
340
341 struct OSDPerfMetricReport {
342 PerformanceCounterDescriptors performance_counter_descriptors;
343 std::map<OSDPerfMetricKey, bufferlist> group_packed_performance_counters;
344
345 DENC(OSDPerfMetricReport, v, p) {
346 DENC_START(1, 1, p);
347 denc(v.performance_counter_descriptors, p);
348 denc(v.group_packed_performance_counters, p);
349 DENC_FINISH(p);
350 }
351 };
352 WRITE_CLASS_DENC(OSDPerfMetricReport)
353
354 #endif // OSD_PERF_METRIC_H_
355