]> git.proxmox.com Git - ceph.git/blob - ceph/src/mon/PGMap.cc
import quincy 17.2.0
[ceph.git] / ceph / src / mon / PGMap.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <boost/algorithm/string.hpp>
5
6 #include "include/rados.h"
7 #include "PGMap.h"
8
9 #define dout_subsys ceph_subsys_mon
10 #include "common/debug.h"
11 #include "common/Clock.h"
12 #include "common/Formatter.h"
13 #include "global/global_context.h"
14 #include "include/ceph_features.h"
15 #include "include/stringify.h"
16
17 #include "osd/osd_types.h"
18 #include "osd/OSDMap.h"
19 #include <boost/range/adaptor/reversed.hpp>
20
21 #define dout_context g_ceph_context
22
23 using std::list;
24 using std::make_pair;
25 using std::map;
26 using std::pair;
27 using std::ostream;
28 using std::ostringstream;
29 using std::set;
30 using std::string;
31 using std::stringstream;
32 using std::vector;
33
34 using ceph::bufferlist;
35 using ceph::fixed_u_to_string;
36
37 using TOPNSPC::common::cmd_getval;
38 using TOPNSPC::common::cmd_getval_or;
39
40 MEMPOOL_DEFINE_OBJECT_FACTORY(PGMapDigest, pgmap_digest, pgmap);
41 MEMPOOL_DEFINE_OBJECT_FACTORY(PGMap, pgmap, pgmap);
42 MEMPOOL_DEFINE_OBJECT_FACTORY(PGMap::Incremental, pgmap_inc, pgmap);
43
44
45 // ---------------------
46 // PGMapDigest
47
48 void PGMapDigest::encode(bufferlist& bl, uint64_t features) const
49 {
50 // NOTE: see PGMap::encode_digest
51 uint8_t v = 4;
52 assert(HAVE_FEATURE(features, SERVER_NAUTILUS));
53 ENCODE_START(v, 1, bl);
54 encode(num_pg, bl);
55 encode(num_pg_active, bl);
56 encode(num_pg_unknown, bl);
57 encode(num_osd, bl);
58 encode(pg_pool_sum, bl, features);
59 encode(pg_sum, bl, features);
60 encode(osd_sum, bl, features);
61 encode(num_pg_by_state, bl);
62 encode(num_pg_by_osd, bl);
63 encode(num_pg_by_pool, bl);
64 encode(osd_last_seq, bl);
65 encode(per_pool_sum_delta, bl, features);
66 encode(per_pool_sum_deltas_stamps, bl);
67 encode(pg_sum_delta, bl, features);
68 encode(stamp_delta, bl);
69 encode(avail_space_by_rule, bl);
70 encode(purged_snaps, bl);
71 encode(osd_sum_by_class, bl, features);
72 ENCODE_FINISH(bl);
73 }
74
75 void PGMapDigest::decode(bufferlist::const_iterator& p)
76 {
77 DECODE_START(4, p);
78 assert(struct_v >= 4);
79 decode(num_pg, p);
80 decode(num_pg_active, p);
81 decode(num_pg_unknown, p);
82 decode(num_osd, p);
83 decode(pg_pool_sum, p);
84 decode(pg_sum, p);
85 decode(osd_sum, p);
86 decode(num_pg_by_state, p);
87 decode(num_pg_by_osd, p);
88 decode(num_pg_by_pool, p);
89 decode(osd_last_seq, p);
90 decode(per_pool_sum_delta, p);
91 decode(per_pool_sum_deltas_stamps, p);
92 decode(pg_sum_delta, p);
93 decode(stamp_delta, p);
94 decode(avail_space_by_rule, p);
95 decode(purged_snaps, p);
96 decode(osd_sum_by_class, p);
97 DECODE_FINISH(p);
98 }
99
100 void PGMapDigest::dump(ceph::Formatter *f) const
101 {
102 f->dump_unsigned("num_pg", num_pg);
103 f->dump_unsigned("num_pg_active", num_pg_active);
104 f->dump_unsigned("num_pg_unknown", num_pg_unknown);
105 f->dump_unsigned("num_osd", num_osd);
106 f->dump_object("pool_sum", pg_sum);
107 f->dump_object("osd_sum", osd_sum);
108
109 f->open_object_section("osd_sum_by_class");
110 for (auto& i : osd_sum_by_class) {
111 f->dump_object(i.first.c_str(), i.second);
112 }
113 f->close_section();
114
115 f->open_array_section("pool_stats");
116 for (auto& p : pg_pool_sum) {
117 f->open_object_section("pool_stat");
118 f->dump_int("poolid", p.first);
119 auto q = num_pg_by_pool.find(p.first);
120 if (q != num_pg_by_pool.end())
121 f->dump_unsigned("num_pg", q->second);
122 p.second.dump(f);
123 f->close_section();
124 }
125 f->close_section();
126 f->open_array_section("osd_stats");
127 int i = 0;
128 // TODO: this isn't really correct since we can dump non-existent OSDs
129 // I dunno what osd_last_seq is set to in that case...
130 for (auto& p : osd_last_seq) {
131 f->open_object_section("osd_stat");
132 f->dump_int("osd", i);
133 f->dump_unsigned("seq", p);
134 f->close_section();
135 ++i;
136 }
137 f->close_section();
138 f->open_array_section("num_pg_by_state");
139 for (auto& p : num_pg_by_state) {
140 f->open_object_section("count");
141 f->dump_string("state", pg_state_string(p.first));
142 f->dump_unsigned("num", p.second);
143 f->close_section();
144 }
145 f->close_section();
146 f->open_array_section("num_pg_by_osd");
147 for (auto& p : num_pg_by_osd) {
148 f->open_object_section("count");
149 f->dump_unsigned("osd", p.first);
150 f->dump_unsigned("num_primary_pg", p.second.primary);
151 f->dump_unsigned("num_acting_pg", p.second.acting);
152 f->dump_unsigned("num_up_not_acting_pg", p.second.up_not_acting);
153 f->close_section();
154 }
155 f->close_section();
156 f->open_array_section("purged_snaps");
157 for (auto& j : purged_snaps) {
158 f->open_object_section("pool");
159 f->dump_int("pool", j.first);
160 f->open_object_section("purged_snaps");
161 for (auto i = j.second.begin(); i != j.second.end(); ++i) {
162 f->open_object_section("interval");
163 f->dump_stream("start") << i.get_start();
164 f->dump_stream("length") << i.get_len();
165 f->close_section();
166 }
167 f->close_section();
168 f->close_section();
169 }
170 f->close_section();
171 }
172
173 void PGMapDigest::generate_test_instances(list<PGMapDigest*>& ls)
174 {
175 ls.push_back(new PGMapDigest);
176 }
177
178 inline std::string percentify(const float& a) {
179 std::stringstream ss;
180 if (a < 0.01)
181 ss << "0";
182 else
183 ss << std::fixed << std::setprecision(2) << a;
184 return ss.str();
185 }
186
187 void PGMapDigest::print_summary(ceph::Formatter *f, ostream *out) const
188 {
189 if (f)
190 f->open_array_section("pgs_by_state");
191
192 // list is descending numeric order (by count)
193 std::multimap<int,uint64_t> state_by_count; // count -> state
194 for (auto p = num_pg_by_state.begin();
195 p != num_pg_by_state.end();
196 ++p) {
197 state_by_count.insert(make_pair(p->second, p->first));
198 }
199 if (f) {
200 for (auto p = state_by_count.rbegin();
201 p != state_by_count.rend();
202 ++p)
203 {
204 f->open_object_section("pgs_by_state_element");
205 f->dump_string("state_name", pg_state_string(p->second));
206 f->dump_unsigned("count", p->first);
207 f->close_section();
208 }
209 }
210 if (f)
211 f->close_section();
212
213 if (f) {
214 f->dump_unsigned("num_pgs", num_pg);
215 f->dump_unsigned("num_pools", pg_pool_sum.size());
216 f->dump_unsigned("num_objects", pg_sum.stats.sum.num_objects);
217 f->dump_unsigned("data_bytes", pg_sum.stats.sum.num_bytes);
218 f->dump_unsigned("bytes_used", osd_sum.statfs.get_used_raw());
219 f->dump_unsigned("bytes_avail", osd_sum.statfs.available);
220 f->dump_unsigned("bytes_total", osd_sum.statfs.total);
221 } else {
222 *out << " pools: " << pg_pool_sum.size() << " pools, "
223 << num_pg << " pgs\n";
224 *out << " objects: " << si_u_t(pg_sum.stats.sum.num_objects) << " objects, "
225 << byte_u_t(pg_sum.stats.sum.num_bytes) << "\n";
226 *out << " usage: "
227 << byte_u_t(osd_sum.statfs.get_used_raw()) << " used, "
228 << byte_u_t(osd_sum.statfs.available) << " / "
229 << byte_u_t(osd_sum.statfs.total) << " avail\n";
230 *out << " pgs: ";
231 }
232
233 bool pad = false;
234
235 if (num_pg_unknown > 0) {
236 float p = (float)num_pg_unknown / (float)num_pg;
237 if (f) {
238 f->dump_float("unknown_pgs_ratio", p);
239 } else {
240 char b[20];
241 snprintf(b, sizeof(b), "%.3lf", p * 100.0);
242 *out << b << "% pgs unknown\n";
243 pad = true;
244 }
245 }
246
247 int num_pg_inactive = num_pg - num_pg_active - num_pg_unknown;
248 if (num_pg_inactive > 0) {
249 float p = (float)num_pg_inactive / (float)num_pg;
250 if (f) {
251 f->dump_float("inactive_pgs_ratio", p);
252 } else {
253 if (pad) {
254 *out << " ";
255 }
256 char b[20];
257 snprintf(b, sizeof(b), "%.3f", p * 100.0);
258 *out << b << "% pgs not active\n";
259 pad = true;
260 }
261 }
262
263 list<string> sl;
264 overall_recovery_summary(f, &sl);
265 if (!f && !sl.empty()) {
266 for (auto p = sl.begin(); p != sl.end(); ++p) {
267 if (pad) {
268 *out << " ";
269 }
270 *out << *p << "\n";
271 pad = true;
272 }
273 }
274 sl.clear();
275
276 if (!f) {
277 unsigned max_width = 1;
278 for (auto p = state_by_count.rbegin(); p != state_by_count.rend(); ++p)
279 {
280 std::stringstream ss;
281 ss << p->first;
282 max_width = std::max<size_t>(ss.str().size(), max_width);
283 }
284
285 for (auto p = state_by_count.rbegin(); p != state_by_count.rend(); ++p)
286 {
287 if (pad) {
288 *out << " ";
289 }
290 pad = true;
291 out->setf(std::ios::left);
292 *out << std::setw(max_width) << p->first
293 << " " << pg_state_string(p->second) << "\n";
294 out->unsetf(std::ios::left);
295 }
296 }
297
298 ostringstream ss_rec_io;
299 overall_recovery_rate_summary(f, &ss_rec_io);
300 ostringstream ss_client_io;
301 overall_client_io_rate_summary(f, &ss_client_io);
302 ostringstream ss_cache_io;
303 overall_cache_io_rate_summary(f, &ss_cache_io);
304
305 if (!f && (ss_client_io.str().length() || ss_rec_io.str().length()
306 || ss_cache_io.str().length())) {
307 *out << "\n \n";
308 *out << " io:\n";
309 }
310
311 if (!f && ss_client_io.str().length())
312 *out << " client: " << ss_client_io.str() << "\n";
313 if (!f && ss_rec_io.str().length())
314 *out << " recovery: " << ss_rec_io.str() << "\n";
315 if (!f && ss_cache_io.str().length())
316 *out << " cache: " << ss_cache_io.str() << "\n";
317 }
318
319 void PGMapDigest::print_oneline_summary(ceph::Formatter *f, ostream *out) const
320 {
321 std::stringstream ss;
322
323 if (f)
324 f->open_array_section("num_pg_by_state");
325 for (auto p = num_pg_by_state.begin();
326 p != num_pg_by_state.end();
327 ++p) {
328 if (f) {
329 f->open_object_section("state");
330 f->dump_string("name", pg_state_string(p->first));
331 f->dump_unsigned("num", p->second);
332 f->close_section();
333 }
334 if (p != num_pg_by_state.begin())
335 ss << ", ";
336 ss << p->second << " " << pg_state_string(p->first);
337 }
338 if (f)
339 f->close_section();
340
341 string states = ss.str();
342 if (out)
343 *out << num_pg << " pgs: "
344 << states << "; "
345 << byte_u_t(pg_sum.stats.sum.num_bytes) << " data, "
346 << byte_u_t(osd_sum.statfs.get_used()) << " used, "
347 << byte_u_t(osd_sum.statfs.available) << " / "
348 << byte_u_t(osd_sum.statfs.total) << " avail";
349 if (f) {
350 f->dump_unsigned("num_pgs", num_pg);
351 f->dump_unsigned("num_bytes", pg_sum.stats.sum.num_bytes);
352 f->dump_int("total_bytes", osd_sum.statfs.total);
353 f->dump_int("total_avail_bytes", osd_sum.statfs.available);
354 f->dump_int("total_used_bytes", osd_sum.statfs.get_used());
355 f->dump_int("total_used_raw_bytes", osd_sum.statfs.get_used_raw());
356 }
357
358 // make non-negative; we can get negative values if osds send
359 // uncommitted stats and then "go backward" or if they are just
360 // buggy/wrong.
361 pool_stat_t pos_delta = pg_sum_delta;
362 pos_delta.floor(0);
363 if (pos_delta.stats.sum.num_rd ||
364 pos_delta.stats.sum.num_wr) {
365 if (out)
366 *out << "; ";
367 if (pos_delta.stats.sum.num_rd) {
368 int64_t rd = (pos_delta.stats.sum.num_rd_kb << 10) / (double)stamp_delta;
369 if (out)
370 *out << byte_u_t(rd) << "/s rd, ";
371 if (f)
372 f->dump_unsigned("read_bytes_sec", rd);
373 }
374 if (pos_delta.stats.sum.num_wr) {
375 int64_t wr = (pos_delta.stats.sum.num_wr_kb << 10) / (double)stamp_delta;
376 if (out)
377 *out << byte_u_t(wr) << "/s wr, ";
378 if (f)
379 f->dump_unsigned("write_bytes_sec", wr);
380 }
381 int64_t iops = (pos_delta.stats.sum.num_rd + pos_delta.stats.sum.num_wr) / (double)stamp_delta;
382 if (out)
383 *out << si_u_t(iops) << " op/s";
384 if (f)
385 f->dump_unsigned("io_sec", iops);
386 }
387
388 list<string> sl;
389 overall_recovery_summary(f, &sl);
390 if (out)
391 for (auto p = sl.begin(); p != sl.end(); ++p)
392 *out << "; " << *p;
393 std::stringstream ssr;
394 overall_recovery_rate_summary(f, &ssr);
395 if (out && ssr.str().length())
396 *out << "; " << ssr.str() << " recovering";
397 }
398
399 void PGMapDigest::get_recovery_stats(
400 double *misplaced_ratio,
401 double *degraded_ratio,
402 double *inactive_pgs_ratio,
403 double *unknown_pgs_ratio) const
404 {
405 if (pg_sum.stats.sum.num_objects_degraded &&
406 pg_sum.stats.sum.num_object_copies > 0) {
407 *degraded_ratio = (double)pg_sum.stats.sum.num_objects_degraded /
408 (double)pg_sum.stats.sum.num_object_copies;
409 } else {
410 *degraded_ratio = 0;
411 }
412 if (pg_sum.stats.sum.num_objects_misplaced &&
413 pg_sum.stats.sum.num_object_copies > 0) {
414 *misplaced_ratio = (double)pg_sum.stats.sum.num_objects_misplaced /
415 (double)pg_sum.stats.sum.num_object_copies;
416 } else {
417 *misplaced_ratio = 0;
418 }
419 if (num_pg > 0) {
420 int num_pg_inactive = num_pg - num_pg_active - num_pg_unknown;
421 *inactive_pgs_ratio = (double)num_pg_inactive / (double)num_pg;
422 *unknown_pgs_ratio = (double)num_pg_unknown / (double)num_pg;
423 } else {
424 *inactive_pgs_ratio = 0;
425 *unknown_pgs_ratio = 0;
426 }
427 }
428
429 void PGMapDigest::recovery_summary(ceph::Formatter *f, list<string> *psl,
430 const pool_stat_t& pool_sum) const
431 {
432 if (pool_sum.stats.sum.num_objects_degraded && pool_sum.stats.sum.num_object_copies > 0) {
433 double pc = (double)pool_sum.stats.sum.num_objects_degraded /
434 (double)pool_sum.stats.sum.num_object_copies * (double)100.0;
435 char b[20];
436 snprintf(b, sizeof(b), "%.3lf", pc);
437 if (f) {
438 f->dump_unsigned("degraded_objects", pool_sum.stats.sum.num_objects_degraded);
439 f->dump_unsigned("degraded_total", pool_sum.stats.sum.num_object_copies);
440 f->dump_float("degraded_ratio", pc / 100.0);
441 } else {
442 ostringstream ss;
443 ss << pool_sum.stats.sum.num_objects_degraded
444 << "/" << pool_sum.stats.sum.num_object_copies << " objects degraded (" << b << "%)";
445 psl->push_back(ss.str());
446 }
447 }
448 if (pool_sum.stats.sum.num_objects_misplaced && pool_sum.stats.sum.num_object_copies > 0) {
449 double pc = (double)pool_sum.stats.sum.num_objects_misplaced /
450 (double)pool_sum.stats.sum.num_object_copies * (double)100.0;
451 char b[20];
452 snprintf(b, sizeof(b), "%.3lf", pc);
453 if (f) {
454 f->dump_unsigned("misplaced_objects", pool_sum.stats.sum.num_objects_misplaced);
455 f->dump_unsigned("misplaced_total", pool_sum.stats.sum.num_object_copies);
456 f->dump_float("misplaced_ratio", pc / 100.0);
457 } else {
458 ostringstream ss;
459 ss << pool_sum.stats.sum.num_objects_misplaced
460 << "/" << pool_sum.stats.sum.num_object_copies << " objects misplaced (" << b << "%)";
461 psl->push_back(ss.str());
462 }
463 }
464 if (pool_sum.stats.sum.num_objects_unfound && pool_sum.stats.sum.num_objects) {
465 double pc = (double)pool_sum.stats.sum.num_objects_unfound /
466 (double)pool_sum.stats.sum.num_objects * (double)100.0;
467 char b[20];
468 snprintf(b, sizeof(b), "%.3lf", pc);
469 if (f) {
470 f->dump_unsigned("unfound_objects", pool_sum.stats.sum.num_objects_unfound);
471 f->dump_unsigned("unfound_total", pool_sum.stats.sum.num_objects);
472 f->dump_float("unfound_ratio", pc / 100.0);
473 } else {
474 ostringstream ss;
475 ss << pool_sum.stats.sum.num_objects_unfound
476 << "/" << pool_sum.stats.sum.num_objects << " objects unfound (" << b << "%)";
477 psl->push_back(ss.str());
478 }
479 }
480 }
481
482 void PGMapDigest::recovery_rate_summary(ceph::Formatter *f, ostream *out,
483 const pool_stat_t& delta_sum,
484 utime_t delta_stamp) const
485 {
486 // make non-negative; we can get negative values if osds send
487 // uncommitted stats and then "go backward" or if they are just
488 // buggy/wrong.
489 pool_stat_t pos_delta = delta_sum;
490 pos_delta.floor(0);
491 if (pos_delta.stats.sum.num_objects_recovered ||
492 pos_delta.stats.sum.num_bytes_recovered ||
493 pos_delta.stats.sum.num_keys_recovered) {
494 int64_t objps = pos_delta.stats.sum.num_objects_recovered / (double)delta_stamp;
495 int64_t bps = pos_delta.stats.sum.num_bytes_recovered / (double)delta_stamp;
496 int64_t kps = pos_delta.stats.sum.num_keys_recovered / (double)delta_stamp;
497 if (f) {
498 f->dump_int("recovering_objects_per_sec", objps);
499 f->dump_int("recovering_bytes_per_sec", bps);
500 f->dump_int("recovering_keys_per_sec", kps);
501 f->dump_int("num_objects_recovered", pos_delta.stats.sum.num_objects_recovered);
502 f->dump_int("num_bytes_recovered", pos_delta.stats.sum.num_bytes_recovered);
503 f->dump_int("num_keys_recovered", pos_delta.stats.sum.num_keys_recovered);
504 } else {
505 *out << byte_u_t(bps) << "/s";
506 if (pos_delta.stats.sum.num_keys_recovered)
507 *out << ", " << si_u_t(kps) << " keys/s";
508 *out << ", " << si_u_t(objps) << " objects/s";
509 }
510 }
511 }
512
513 void PGMapDigest::overall_recovery_rate_summary(ceph::Formatter *f, ostream *out) const
514 {
515 recovery_rate_summary(f, out, pg_sum_delta, stamp_delta);
516 }
517
518 void PGMapDigest::overall_recovery_summary(ceph::Formatter *f, list<string> *psl) const
519 {
520 recovery_summary(f, psl, pg_sum);
521 }
522
523 void PGMapDigest::pool_recovery_rate_summary(ceph::Formatter *f, ostream *out,
524 uint64_t poolid) const
525 {
526 auto p = per_pool_sum_delta.find(poolid);
527 if (p == per_pool_sum_delta.end())
528 return;
529
530 auto ts = per_pool_sum_deltas_stamps.find(p->first);
531 ceph_assert(ts != per_pool_sum_deltas_stamps.end());
532 recovery_rate_summary(f, out, p->second.first, ts->second);
533 }
534
535 void PGMapDigest::pool_recovery_summary(ceph::Formatter *f, list<string> *psl,
536 uint64_t poolid) const
537 {
538 auto p = pg_pool_sum.find(poolid);
539 if (p == pg_pool_sum.end())
540 return;
541
542 recovery_summary(f, psl, p->second);
543 }
544
545 void PGMapDigest::client_io_rate_summary(ceph::Formatter *f, ostream *out,
546 const pool_stat_t& delta_sum,
547 utime_t delta_stamp) const
548 {
549 pool_stat_t pos_delta = delta_sum;
550 pos_delta.floor(0);
551 if (pos_delta.stats.sum.num_rd ||
552 pos_delta.stats.sum.num_wr) {
553 if (pos_delta.stats.sum.num_rd) {
554 int64_t rd = (pos_delta.stats.sum.num_rd_kb << 10) / (double)delta_stamp;
555 if (f) {
556 f->dump_int("read_bytes_sec", rd);
557 } else {
558 *out << byte_u_t(rd) << "/s rd, ";
559 }
560 }
561 if (pos_delta.stats.sum.num_wr) {
562 int64_t wr = (pos_delta.stats.sum.num_wr_kb << 10) / (double)delta_stamp;
563 if (f) {
564 f->dump_int("write_bytes_sec", wr);
565 } else {
566 *out << byte_u_t(wr) << "/s wr, ";
567 }
568 }
569 int64_t iops_rd = pos_delta.stats.sum.num_rd / (double)delta_stamp;
570 int64_t iops_wr = pos_delta.stats.sum.num_wr / (double)delta_stamp;
571 if (f) {
572 f->dump_int("read_op_per_sec", iops_rd);
573 f->dump_int("write_op_per_sec", iops_wr);
574 } else {
575 *out << si_u_t(iops_rd) << " op/s rd, " << si_u_t(iops_wr) << " op/s wr";
576 }
577 }
578 }
579
580 void PGMapDigest::overall_client_io_rate_summary(ceph::Formatter *f, ostream *out) const
581 {
582 client_io_rate_summary(f, out, pg_sum_delta, stamp_delta);
583 }
584
585 void PGMapDigest::pool_client_io_rate_summary(ceph::Formatter *f, ostream *out,
586 uint64_t poolid) const
587 {
588 auto p = per_pool_sum_delta.find(poolid);
589 if (p == per_pool_sum_delta.end())
590 return;
591
592 auto ts = per_pool_sum_deltas_stamps.find(p->first);
593 ceph_assert(ts != per_pool_sum_deltas_stamps.end());
594 client_io_rate_summary(f, out, p->second.first, ts->second);
595 }
596
597 void PGMapDigest::cache_io_rate_summary(ceph::Formatter *f, ostream *out,
598 const pool_stat_t& delta_sum,
599 utime_t delta_stamp) const
600 {
601 pool_stat_t pos_delta = delta_sum;
602 pos_delta.floor(0);
603 bool have_output = false;
604
605 if (pos_delta.stats.sum.num_flush) {
606 int64_t flush = (pos_delta.stats.sum.num_flush_kb << 10) / (double)delta_stamp;
607 if (f) {
608 f->dump_int("flush_bytes_sec", flush);
609 } else {
610 *out << byte_u_t(flush) << "/s flush";
611 have_output = true;
612 }
613 }
614 if (pos_delta.stats.sum.num_evict) {
615 int64_t evict = (pos_delta.stats.sum.num_evict_kb << 10) / (double)delta_stamp;
616 if (f) {
617 f->dump_int("evict_bytes_sec", evict);
618 } else {
619 if (have_output)
620 *out << ", ";
621 *out << byte_u_t(evict) << "/s evict";
622 have_output = true;
623 }
624 }
625 if (pos_delta.stats.sum.num_promote) {
626 int64_t promote = pos_delta.stats.sum.num_promote / (double)delta_stamp;
627 if (f) {
628 f->dump_int("promote_op_per_sec", promote);
629 } else {
630 if (have_output)
631 *out << ", ";
632 *out << si_u_t(promote) << " op/s promote";
633 have_output = true;
634 }
635 }
636 if (pos_delta.stats.sum.num_flush_mode_low) {
637 if (f) {
638 f->dump_int("num_flush_mode_low", pos_delta.stats.sum.num_flush_mode_low);
639 } else {
640 if (have_output)
641 *out << ", ";
642 *out << si_u_t(pos_delta.stats.sum.num_flush_mode_low) << " PGs flushing";
643 have_output = true;
644 }
645 }
646 if (pos_delta.stats.sum.num_flush_mode_high) {
647 if (f) {
648 f->dump_int("num_flush_mode_high", pos_delta.stats.sum.num_flush_mode_high);
649 } else {
650 if (have_output)
651 *out << ", ";
652 *out << si_u_t(pos_delta.stats.sum.num_flush_mode_high) << " PGs flushing (high)";
653 have_output = true;
654 }
655 }
656 if (pos_delta.stats.sum.num_evict_mode_some) {
657 if (f) {
658 f->dump_int("num_evict_mode_some", pos_delta.stats.sum.num_evict_mode_some);
659 } else {
660 if (have_output)
661 *out << ", ";
662 *out << si_u_t(pos_delta.stats.sum.num_evict_mode_some) << " PGs evicting";
663 have_output = true;
664 }
665 }
666 if (pos_delta.stats.sum.num_evict_mode_full) {
667 if (f) {
668 f->dump_int("num_evict_mode_full", pos_delta.stats.sum.num_evict_mode_full);
669 } else {
670 if (have_output)
671 *out << ", ";
672 *out << si_u_t(pos_delta.stats.sum.num_evict_mode_full) << " PGs evicting (full)";
673 }
674 }
675 }
676
677 void PGMapDigest::overall_cache_io_rate_summary(ceph::Formatter *f, ostream *out) const
678 {
679 cache_io_rate_summary(f, out, pg_sum_delta, stamp_delta);
680 }
681
682 void PGMapDigest::pool_cache_io_rate_summary(ceph::Formatter *f, ostream *out,
683 uint64_t poolid) const
684 {
685 auto p = per_pool_sum_delta.find(poolid);
686 if (p == per_pool_sum_delta.end())
687 return;
688
689 auto ts = per_pool_sum_deltas_stamps.find(p->first);
690 ceph_assert(ts != per_pool_sum_deltas_stamps.end());
691 cache_io_rate_summary(f, out, p->second.first, ts->second);
692 }
693
694 ceph_statfs PGMapDigest::get_statfs(OSDMap &osdmap,
695 std::optional<int64_t> data_pool) const
696 {
697 ceph_statfs statfs;
698 bool filter = false;
699 object_stat_sum_t sum;
700
701 if (data_pool) {
702 auto i = pg_pool_sum.find(*data_pool);
703 if (i != pg_pool_sum.end()) {
704 sum = i->second.stats.sum;
705 filter = true;
706 }
707 }
708
709 if (filter) {
710 statfs.kb_used = (sum.num_bytes >> 10);
711 statfs.kb_avail = get_pool_free_space(osdmap, *data_pool) >> 10;
712 statfs.num_objects = sum.num_objects;
713 statfs.kb = statfs.kb_used + statfs.kb_avail;
714 } else {
715 // these are in KB.
716 statfs.kb = osd_sum.statfs.kb();
717 statfs.kb_used = osd_sum.statfs.kb_used_raw();
718 statfs.kb_avail = osd_sum.statfs.kb_avail();
719 statfs.num_objects = pg_sum.stats.sum.num_objects;
720 }
721
722 return statfs;
723 }
724
725 void PGMapDigest::dump_pool_stats_full(
726 const OSDMap &osd_map,
727 stringstream *ss,
728 ceph::Formatter *f,
729 bool verbose) const
730 {
731 TextTable tbl;
732
733 if (f) {
734 f->open_array_section("pools");
735 } else {
736 tbl.define_column("POOL", TextTable::LEFT, TextTable::LEFT);
737 tbl.define_column("ID", TextTable::RIGHT, TextTable::RIGHT);
738 tbl.define_column("PGS", TextTable::RIGHT, TextTable::RIGHT);
739 tbl.define_column("STORED", TextTable::RIGHT, TextTable::RIGHT);
740 if (verbose) {
741 tbl.define_column("(DATA)", TextTable::RIGHT, TextTable::RIGHT);
742 tbl.define_column("(OMAP)", TextTable::RIGHT, TextTable::RIGHT);
743 }
744 tbl.define_column("OBJECTS", TextTable::RIGHT, TextTable::RIGHT);
745 tbl.define_column("USED", TextTable::RIGHT, TextTable::RIGHT);
746 if (verbose) {
747 tbl.define_column("(DATA)", TextTable::RIGHT, TextTable::RIGHT);
748 tbl.define_column("(OMAP)", TextTable::RIGHT, TextTable::RIGHT);
749 }
750 tbl.define_column("%USED", TextTable::RIGHT, TextTable::RIGHT);
751 tbl.define_column("MAX AVAIL", TextTable::RIGHT, TextTable::RIGHT);
752
753 if (verbose) {
754 tbl.define_column("QUOTA OBJECTS", TextTable::RIGHT, TextTable::RIGHT);
755 tbl.define_column("QUOTA BYTES", TextTable::RIGHT, TextTable::RIGHT);
756 tbl.define_column("DIRTY", TextTable::RIGHT, TextTable::RIGHT);
757 tbl.define_column("USED COMPR", TextTable::RIGHT, TextTable::RIGHT);
758 tbl.define_column("UNDER COMPR", TextTable::RIGHT, TextTable::RIGHT);
759 }
760 }
761
762 map<int,uint64_t> avail_by_rule;
763 for (auto p = osd_map.get_pools().begin();
764 p != osd_map.get_pools().end(); ++p) {
765 int64_t pool_id = p->first;
766 if ((pool_id < 0) || (pg_pool_sum.count(pool_id) == 0))
767 continue;
768
769 const string& pool_name = osd_map.get_pool_name(pool_id);
770 auto pool_pg_num = osd_map.get_pg_num(pool_id);
771 const pool_stat_t &stat = pg_pool_sum.at(pool_id);
772
773 const pg_pool_t *pool = osd_map.get_pg_pool(pool_id);
774 int ruleno = pool->get_crush_rule();
775 int64_t avail;
776 if (avail_by_rule.count(ruleno) == 0) {
777 // FIXME: we don't guarantee avail_space_by_rule is up-to-date before this function is invoked
778 avail = get_rule_avail(ruleno);
779 if (avail < 0)
780 avail = 0;
781 avail_by_rule[ruleno] = avail;
782 } else {
783 avail = avail_by_rule[ruleno];
784 }
785 if (f) {
786 f->open_object_section("pool");
787 f->dump_string("name", pool_name);
788 f->dump_int("id", pool_id);
789 f->open_object_section("stats");
790 } else {
791 tbl << pool_name
792 << pool_id
793 << pool_pg_num;
794 }
795 float raw_used_rate = osd_map.pool_raw_used_rate(pool_id);
796 bool per_pool = use_per_pool_stats();
797 bool per_pool_omap = use_per_pool_omap_stats();
798 dump_object_stat_sum(tbl, f, stat, avail, raw_used_rate, verbose, per_pool,
799 per_pool_omap, pool);
800 if (f) {
801 f->close_section(); // stats
802 f->close_section(); // pool
803 } else {
804 tbl << TextTable::endrow;
805 }
806 }
807 if (f)
808 f->close_section();
809 else {
810 ceph_assert(ss != nullptr);
811 *ss << "--- POOLS ---\n";
812 *ss << tbl;
813 }
814 }
815
816 void PGMapDigest::dump_cluster_stats(stringstream *ss,
817 ceph::Formatter *f,
818 bool verbose) const
819 {
820 if (f) {
821 f->open_object_section("stats");
822 f->dump_int("total_bytes", osd_sum.statfs.total);
823 f->dump_int("total_avail_bytes", osd_sum.statfs.available);
824 f->dump_int("total_used_bytes", osd_sum.statfs.get_used());
825 f->dump_int("total_used_raw_bytes", osd_sum.statfs.get_used_raw());
826 f->dump_float("total_used_raw_ratio", osd_sum.statfs.get_used_raw_ratio());
827 f->dump_unsigned("num_osds", osd_sum.num_osds);
828 f->dump_unsigned("num_per_pool_osds", osd_sum.num_per_pool_osds);
829 f->dump_unsigned("num_per_pool_omap_osds", osd_sum.num_per_pool_omap_osds);
830 f->close_section();
831 f->open_object_section("stats_by_class");
832 for (auto& i : osd_sum_by_class) {
833 f->open_object_section(i.first.c_str());
834 f->dump_int("total_bytes", i.second.statfs.total);
835 f->dump_int("total_avail_bytes", i.second.statfs.available);
836 f->dump_int("total_used_bytes", i.second.statfs.get_used());
837 f->dump_int("total_used_raw_bytes", i.second.statfs.get_used_raw());
838 f->dump_float("total_used_raw_ratio",
839 i.second.statfs.get_used_raw_ratio());
840 f->close_section();
841 }
842 f->close_section();
843 } else {
844 ceph_assert(ss != nullptr);
845 TextTable tbl;
846 tbl.define_column("CLASS", TextTable::LEFT, TextTable::LEFT);
847 tbl.define_column("SIZE", TextTable::RIGHT, TextTable::RIGHT);
848 tbl.define_column("AVAIL", TextTable::RIGHT, TextTable::RIGHT);
849 tbl.define_column("USED", TextTable::RIGHT, TextTable::RIGHT);
850 tbl.define_column("RAW USED", TextTable::RIGHT, TextTable::RIGHT);
851 tbl.define_column("%RAW USED", TextTable::RIGHT, TextTable::RIGHT);
852
853
854 for (auto& i : osd_sum_by_class) {
855 tbl << i.first;
856 tbl << stringify(byte_u_t(i.second.statfs.total))
857 << stringify(byte_u_t(i.second.statfs.available))
858 << stringify(byte_u_t(i.second.statfs.get_used()))
859 << stringify(byte_u_t(i.second.statfs.get_used_raw()))
860 << percentify(i.second.statfs.get_used_raw_ratio()*100.0)
861 << TextTable::endrow;
862 }
863 tbl << "TOTAL";
864 tbl << stringify(byte_u_t(osd_sum.statfs.total))
865 << stringify(byte_u_t(osd_sum.statfs.available))
866 << stringify(byte_u_t(osd_sum.statfs.get_used()))
867 << stringify(byte_u_t(osd_sum.statfs.get_used_raw()))
868 << percentify(osd_sum.statfs.get_used_raw_ratio()*100.0)
869 << TextTable::endrow;
870
871 *ss << "--- RAW STORAGE ---\n";
872 *ss << tbl;
873 }
874 }
875
876 void PGMapDigest::dump_object_stat_sum(
877 TextTable &tbl, ceph::Formatter *f,
878 const pool_stat_t &pool_stat, uint64_t avail,
879 float raw_used_rate, bool verbose, bool per_pool, bool per_pool_omap,
880 const pg_pool_t *pool)
881 {
882 const object_stat_sum_t &sum = pool_stat.stats.sum;
883 const store_statfs_t statfs = pool_stat.store_stats;
884
885 if (sum.num_object_copies > 0) {
886 raw_used_rate *= (float)(sum.num_object_copies - sum.num_objects_degraded) / sum.num_object_copies;
887 }
888
889 uint64_t used_data_bytes = pool_stat.get_allocated_data_bytes(per_pool);
890 uint64_t used_omap_bytes = pool_stat.get_allocated_omap_bytes(per_pool_omap);
891 uint64_t used_bytes = used_data_bytes + used_omap_bytes;
892
893 float used = 0.0;
894 // note avail passed in is raw_avail, calc raw_used here.
895 if (avail) {
896 used = used_bytes;
897 used /= used + avail;
898 } else if (used_bytes) {
899 used = 1.0;
900 }
901 auto avail_res = raw_used_rate ? avail / raw_used_rate : 0;
902 // an approximation for actually stored user data
903 auto stored_data_normalized = pool_stat.get_user_data_bytes(
904 raw_used_rate, per_pool);
905 auto stored_omap_normalized = pool_stat.get_user_omap_bytes(
906 raw_used_rate, per_pool_omap);
907 auto stored_normalized = stored_data_normalized + stored_omap_normalized;
908 // same, amplied by replication or EC
909 auto stored_raw = stored_normalized * raw_used_rate;
910 if (f) {
911 f->dump_int("stored", stored_normalized);
912 if (verbose) {
913 f->dump_int("stored_data", stored_data_normalized);
914 f->dump_int("stored_omap", stored_omap_normalized);
915 }
916 f->dump_int("objects", sum.num_objects);
917 f->dump_int("kb_used", shift_round_up(used_bytes, 10));
918 f->dump_int("bytes_used", used_bytes);
919 if (verbose) {
920 f->dump_int("data_bytes_used", used_data_bytes);
921 f->dump_int("omap_bytes_used", used_omap_bytes);
922 }
923 f->dump_float("percent_used", used);
924 f->dump_unsigned("max_avail", avail_res);
925 if (verbose) {
926 f->dump_int("quota_objects", pool->quota_max_objects);
927 f->dump_int("quota_bytes", pool->quota_max_bytes);
928 if (pool->is_tier()) {
929 f->dump_int("dirty", sum.num_objects_dirty);
930 } else {
931 f->dump_int("dirty", 0);
932 }
933 f->dump_int("rd", sum.num_rd);
934 f->dump_int("rd_bytes", sum.num_rd_kb * 1024ull);
935 f->dump_int("wr", sum.num_wr);
936 f->dump_int("wr_bytes", sum.num_wr_kb * 1024ull);
937 f->dump_int("compress_bytes_used", statfs.data_compressed_allocated);
938 f->dump_int("compress_under_bytes", statfs.data_compressed_original);
939 // Stored by user amplified by replication
940 f->dump_int("stored_raw", stored_raw);
941 f->dump_unsigned("avail_raw", avail);
942 }
943 } else {
944 tbl << stringify(byte_u_t(stored_normalized));
945 if (verbose) {
946 tbl << stringify(byte_u_t(stored_data_normalized));
947 tbl << stringify(byte_u_t(stored_omap_normalized));
948 }
949 tbl << stringify(si_u_t(sum.num_objects));
950 tbl << stringify(byte_u_t(used_bytes));
951 if (verbose) {
952 tbl << stringify(byte_u_t(used_data_bytes));
953 tbl << stringify(byte_u_t(used_omap_bytes));
954 }
955 tbl << percentify(used*100);
956 tbl << stringify(byte_u_t(avail_res));
957 if (verbose) {
958 if (pool->quota_max_objects == 0)
959 tbl << "N/A";
960 else
961 tbl << stringify(si_u_t(pool->quota_max_objects));
962 if (pool->quota_max_bytes == 0)
963 tbl << "N/A";
964 else
965 tbl << stringify(byte_u_t(pool->quota_max_bytes));
966 if (pool->is_tier()) {
967 tbl << stringify(si_u_t(sum.num_objects_dirty));
968 } else {
969 tbl << "N/A";
970 }
971 tbl << stringify(byte_u_t(statfs.data_compressed_allocated));
972 tbl << stringify(byte_u_t(statfs.data_compressed_original));
973 }
974 }
975 }
976
977 int64_t PGMapDigest::get_pool_free_space(const OSDMap &osd_map,
978 int64_t poolid) const
979 {
980 const pg_pool_t *pool = osd_map.get_pg_pool(poolid);
981 int ruleno = pool->get_crush_rule();
982 int64_t avail;
983 avail = get_rule_avail(ruleno);
984 if (avail < 0)
985 avail = 0;
986
987 return avail / osd_map.pool_raw_used_rate(poolid);
988 }
989
990 int64_t PGMap::get_rule_avail(const OSDMap& osdmap, int ruleno) const
991 {
992 map<int,float> wm;
993 int r = osdmap.crush->get_rule_weight_osd_map(ruleno, &wm);
994 if (r < 0) {
995 return r;
996 }
997 if (wm.empty()) {
998 return 0;
999 }
1000
1001 float fratio = osdmap.get_full_ratio();
1002
1003 int64_t min = -1;
1004 for (auto p = wm.begin(); p != wm.end(); ++p) {
1005 auto osd_info = osd_stat.find(p->first);
1006 if (osd_info != osd_stat.end()) {
1007 if (osd_info->second.statfs.total == 0 || p->second == 0) {
1008 // osd must be out, hence its stats have been zeroed
1009 // (unless we somehow managed to have a disk with size 0...)
1010 //
1011 // (p->second == 0), if osd weight is 0, no need to
1012 // calculate proj below.
1013 continue;
1014 }
1015 double unusable = (double)osd_info->second.statfs.kb() *
1016 (1.0 - fratio);
1017 double avail = std::max(0.0, (double)osd_info->second.statfs.kb_avail() - unusable);
1018 avail *= 1024.0;
1019 int64_t proj = (int64_t)(avail / (double)p->second);
1020 if (min < 0 || proj < min) {
1021 min = proj;
1022 }
1023 } else {
1024 if (osdmap.is_up(p->first)) {
1025 // This is a level 4 rather than an error, because we might have
1026 // only just started, and not received the first stats message yet.
1027 dout(4) << "OSD " << p->first << " is up, but has no stats" << dendl;
1028 }
1029 }
1030 }
1031 return min;
1032 }
1033
1034 void PGMap::get_rules_avail(const OSDMap& osdmap,
1035 std::map<int,int64_t> *avail_map) const
1036 {
1037 avail_map->clear();
1038 for (auto p : osdmap.get_pools()) {
1039 int64_t pool_id = p.first;
1040 if ((pool_id < 0) || (pg_pool_sum.count(pool_id) == 0))
1041 continue;
1042 const pg_pool_t *pool = osdmap.get_pg_pool(pool_id);
1043 int ruleno = pool->get_crush_rule();
1044 if (avail_map->count(ruleno) == 0)
1045 (*avail_map)[ruleno] = get_rule_avail(osdmap, ruleno);
1046 }
1047 }
1048
1049 // ---------------------
1050 // PGMap
1051
1052 void PGMap::Incremental::dump(ceph::Formatter *f) const
1053 {
1054 f->dump_unsigned("version", version);
1055 f->dump_stream("stamp") << stamp;
1056 f->dump_unsigned("osdmap_epoch", osdmap_epoch);
1057 f->dump_unsigned("pg_scan_epoch", pg_scan);
1058
1059 f->open_array_section("pg_stat_updates");
1060 for (auto p = pg_stat_updates.begin(); p != pg_stat_updates.end(); ++p) {
1061 f->open_object_section("pg_stat");
1062 f->dump_stream("pgid") << p->first;
1063 p->second.dump(f);
1064 f->close_section();
1065 }
1066 f->close_section();
1067
1068 f->open_array_section("osd_stat_updates");
1069 for (auto p = osd_stat_updates.begin(); p != osd_stat_updates.end(); ++p) {
1070 f->open_object_section("osd_stat");
1071 f->dump_int("osd", p->first);
1072 p->second.dump(f);
1073 f->close_section();
1074 }
1075 f->close_section();
1076 f->open_array_section("pool_statfs_updates");
1077 for (auto p = pool_statfs_updates.begin(); p != pool_statfs_updates.end(); ++p) {
1078 f->open_object_section("pool_statfs");
1079 f->dump_stream("poolid/osd") << p->first;
1080 p->second.dump(f);
1081 f->close_section();
1082 }
1083 f->close_section();
1084
1085 f->open_array_section("osd_stat_removals");
1086 for (auto p = osd_stat_rm.begin(); p != osd_stat_rm.end(); ++p)
1087 f->dump_int("osd", *p);
1088 f->close_section();
1089
1090 f->open_array_section("pg_removals");
1091 for (auto p = pg_remove.begin(); p != pg_remove.end(); ++p)
1092 f->dump_stream("pgid") << *p;
1093 f->close_section();
1094 }
1095
1096 void PGMap::Incremental::generate_test_instances(list<PGMap::Incremental*>& o)
1097 {
1098 o.push_back(new Incremental);
1099 o.push_back(new Incremental);
1100 o.back()->version = 1;
1101 o.back()->stamp = utime_t(123,345);
1102 o.push_back(new Incremental);
1103 o.back()->version = 2;
1104 o.back()->pg_stat_updates[pg_t(1,2)] = pg_stat_t();
1105 o.back()->osd_stat_updates[5] = osd_stat_t();
1106 o.push_back(new Incremental);
1107 o.back()->version = 3;
1108 o.back()->osdmap_epoch = 1;
1109 o.back()->pg_scan = 2;
1110 o.back()->pg_stat_updates[pg_t(4,5)] = pg_stat_t();
1111 o.back()->osd_stat_updates[6] = osd_stat_t();
1112 o.back()->pg_remove.insert(pg_t(1,2));
1113 o.back()->osd_stat_rm.insert(5);
1114 o.back()->pool_statfs_updates[std::make_pair(1234,4)] = store_statfs_t();
1115 }
1116
1117 // --
1118
1119 void PGMap::apply_incremental(CephContext *cct, const Incremental& inc)
1120 {
1121 ceph_assert(inc.version == version+1);
1122 version++;
1123
1124 pool_stat_t pg_sum_old = pg_sum;
1125 mempool::pgmap::unordered_map<int32_t, pool_stat_t> pg_pool_sum_old;
1126 pg_pool_sum_old = pg_pool_sum;
1127
1128 for (auto p = inc.pg_stat_updates.begin();
1129 p != inc.pg_stat_updates.end();
1130 ++p) {
1131 const pg_t &update_pg(p->first);
1132 auto update_pool = update_pg.pool();
1133 const pg_stat_t &update_stat(p->second);
1134
1135 auto pg_stat_iter = pg_stat.find(update_pg);
1136 pool_stat_t &pool_sum_ref = pg_pool_sum[update_pool];
1137 if (pg_stat_iter == pg_stat.end()) {
1138 pg_stat.insert(make_pair(update_pg, update_stat));
1139 } else {
1140 stat_pg_sub(update_pg, pg_stat_iter->second);
1141 pool_sum_ref.sub(pg_stat_iter->second);
1142 pg_stat_iter->second = update_stat;
1143 }
1144 stat_pg_add(update_pg, update_stat);
1145 pool_sum_ref.add(update_stat);
1146 }
1147
1148 for (auto p = inc.pool_statfs_updates.begin();
1149 p != inc.pool_statfs_updates.end();
1150 ++p) {
1151 auto update_pool = p->first.first;
1152 auto update_osd = p->first.second;
1153 auto& statfs_inc = p->second;
1154
1155 auto pool_statfs_iter =
1156 pool_statfs.find(std::make_pair(update_pool, update_osd));
1157 if (pg_pool_sum.count(update_pool)) {
1158 pool_stat_t &pool_sum_ref = pg_pool_sum[update_pool];
1159 if (pool_statfs_iter == pool_statfs.end()) {
1160 pool_statfs.emplace(std::make_pair(update_pool, update_osd), statfs_inc);
1161 } else {
1162 pool_sum_ref.sub(pool_statfs_iter->second);
1163 pool_statfs_iter->second = statfs_inc;
1164 }
1165 pool_sum_ref.add(statfs_inc);
1166 }
1167 }
1168
1169 for (auto p = inc.get_osd_stat_updates().begin();
1170 p != inc.get_osd_stat_updates().end();
1171 ++p) {
1172 int osd = p->first;
1173 const osd_stat_t &new_stats(p->second);
1174
1175 auto t = osd_stat.find(osd);
1176 if (t == osd_stat.end()) {
1177 osd_stat.insert(make_pair(osd, new_stats));
1178 } else {
1179 stat_osd_sub(t->first, t->second);
1180 t->second = new_stats;
1181 }
1182 stat_osd_add(osd, new_stats);
1183 }
1184 set<int64_t> deleted_pools;
1185 for (auto p = inc.pg_remove.begin();
1186 p != inc.pg_remove.end();
1187 ++p) {
1188 const pg_t &removed_pg(*p);
1189 auto s = pg_stat.find(removed_pg);
1190 bool pool_erased = false;
1191 if (s != pg_stat.end()) {
1192 pool_erased = stat_pg_sub(removed_pg, s->second);
1193
1194 // decrease pool stats if pg was removed
1195 auto pool_stats_it = pg_pool_sum.find(removed_pg.pool());
1196 if (pool_stats_it != pg_pool_sum.end()) {
1197 pool_stats_it->second.sub(s->second);
1198 }
1199
1200 pg_stat.erase(s);
1201 if (pool_erased) {
1202 deleted_pools.insert(removed_pg.pool());
1203 }
1204 }
1205 }
1206
1207 for (auto p = inc.get_osd_stat_rm().begin();
1208 p != inc.get_osd_stat_rm().end();
1209 ++p) {
1210 auto t = osd_stat.find(*p);
1211 if (t != osd_stat.end()) {
1212 stat_osd_sub(t->first, t->second);
1213 osd_stat.erase(t);
1214 }
1215 for (auto i = pool_statfs.begin(); i != pool_statfs.end(); ++i) {
1216 if (i->first.second == *p) {
1217 pg_pool_sum[i->first.first].sub(i->second);
1218 pool_statfs.erase(i);
1219 }
1220 }
1221 }
1222
1223 // skip calculating delta while sum was not synchronized
1224 if (!stamp.is_zero() && !pg_sum_old.stats.sum.is_zero()) {
1225 utime_t delta_t;
1226 delta_t = inc.stamp;
1227 delta_t -= stamp;
1228 // calculate a delta, and average over the last 2 deltas.
1229 pool_stat_t d = pg_sum;
1230 d.stats.sub(pg_sum_old.stats);
1231 pg_sum_deltas.push_back(make_pair(d, delta_t));
1232 stamp_delta += delta_t;
1233 pg_sum_delta.stats.add(d.stats);
1234 auto smooth_intervals =
1235 cct ? cct->_conf.get_val<uint64_t>("mon_stat_smooth_intervals") : 1;
1236 while (pg_sum_deltas.size() > smooth_intervals) {
1237 pg_sum_delta.stats.sub(pg_sum_deltas.front().first.stats);
1238 stamp_delta -= pg_sum_deltas.front().second;
1239 pg_sum_deltas.pop_front();
1240 }
1241 }
1242 stamp = inc.stamp;
1243
1244 update_pool_deltas(cct, inc.stamp, pg_pool_sum_old);
1245
1246 for (auto p : deleted_pools) {
1247 if (cct)
1248 dout(20) << " deleted pool " << p << dendl;
1249 deleted_pool(p);
1250 }
1251
1252 if (inc.osdmap_epoch)
1253 last_osdmap_epoch = inc.osdmap_epoch;
1254 if (inc.pg_scan)
1255 last_pg_scan = inc.pg_scan;
1256 }
1257
1258 void PGMap::calc_stats()
1259 {
1260 num_pg = 0;
1261 num_pg_active = 0;
1262 num_pg_unknown = 0;
1263 num_osd = 0;
1264 pg_pool_sum.clear();
1265 num_pg_by_pool.clear();
1266 pg_by_osd.clear();
1267 pg_sum = pool_stat_t();
1268 osd_sum = osd_stat_t();
1269 osd_sum_by_class.clear();
1270 num_pg_by_state.clear();
1271 num_pg_by_pool_state.clear();
1272 num_pg_by_osd.clear();
1273
1274 for (auto p = pg_stat.begin();
1275 p != pg_stat.end();
1276 ++p) {
1277 auto pg = p->first;
1278 stat_pg_add(pg, p->second);
1279 pg_pool_sum[pg.pool()].add(p->second);
1280 }
1281 for (auto p = pool_statfs.begin();
1282 p != pool_statfs.end();
1283 ++p) {
1284 auto pool = p->first.first;
1285 pg_pool_sum[pool].add(p->second);
1286 }
1287 for (auto p = osd_stat.begin();
1288 p != osd_stat.end();
1289 ++p)
1290 stat_osd_add(p->first, p->second);
1291 }
1292
1293 void PGMap::stat_pg_add(const pg_t &pgid, const pg_stat_t &s,
1294 bool sameosds)
1295 {
1296 auto pool = pgid.pool();
1297 pg_sum.add(s);
1298
1299 num_pg++;
1300 num_pg_by_state[s.state]++;
1301 num_pg_by_pool_state[pgid.pool()][s.state]++;
1302 num_pg_by_pool[pool]++;
1303
1304 if ((s.state & PG_STATE_CREATING) &&
1305 s.parent_split_bits == 0) {
1306 creating_pgs.insert(pgid);
1307 if (s.acting_primary >= 0) {
1308 creating_pgs_by_osd_epoch[s.acting_primary][s.mapping_epoch].insert(pgid);
1309 }
1310 }
1311
1312 if (s.state & PG_STATE_ACTIVE) {
1313 ++num_pg_active;
1314 }
1315 if (s.state == 0) {
1316 ++num_pg_unknown;
1317 }
1318
1319 if (sameosds)
1320 return;
1321
1322 for (auto p = s.blocked_by.begin();
1323 p != s.blocked_by.end();
1324 ++p) {
1325 ++blocked_by_sum[*p];
1326 }
1327
1328 for (auto p = s.acting.begin(); p != s.acting.end(); ++p) {
1329 pg_by_osd[*p].insert(pgid);
1330 num_pg_by_osd[*p].acting++;
1331 }
1332 for (auto p = s.up.begin(); p != s.up.end(); ++p) {
1333 auto& t = pg_by_osd[*p];
1334 if (t.find(pgid) == t.end()) {
1335 t.insert(pgid);
1336 num_pg_by_osd[*p].up_not_acting++;
1337 }
1338 }
1339
1340 if (s.up_primary >= 0) {
1341 num_pg_by_osd[s.up_primary].primary++;
1342 }
1343 }
1344
1345 bool PGMap::stat_pg_sub(const pg_t &pgid, const pg_stat_t &s,
1346 bool sameosds)
1347 {
1348 bool pool_erased = false;
1349 pg_sum.sub(s);
1350
1351 num_pg--;
1352 int end = --num_pg_by_state[s.state];
1353 ceph_assert(end >= 0);
1354 if (end == 0)
1355 num_pg_by_state.erase(s.state);
1356 if (--num_pg_by_pool_state[pgid.pool()][s.state] == 0) {
1357 num_pg_by_pool_state[pgid.pool()].erase(s.state);
1358 }
1359 end = --num_pg_by_pool[pgid.pool()];
1360 if (end == 0) {
1361 pool_erased = true;
1362 }
1363
1364 if ((s.state & PG_STATE_CREATING) &&
1365 s.parent_split_bits == 0) {
1366 creating_pgs.erase(pgid);
1367 if (s.acting_primary >= 0) {
1368 map<epoch_t,set<pg_t> >& r = creating_pgs_by_osd_epoch[s.acting_primary];
1369 r[s.mapping_epoch].erase(pgid);
1370 if (r[s.mapping_epoch].empty())
1371 r.erase(s.mapping_epoch);
1372 if (r.empty())
1373 creating_pgs_by_osd_epoch.erase(s.acting_primary);
1374 }
1375 }
1376
1377 if (s.state & PG_STATE_ACTIVE) {
1378 --num_pg_active;
1379 }
1380 if (s.state == 0) {
1381 --num_pg_unknown;
1382 }
1383
1384 if (sameosds)
1385 return pool_erased;
1386
1387 for (auto p = s.blocked_by.begin();
1388 p != s.blocked_by.end();
1389 ++p) {
1390 auto q = blocked_by_sum.find(*p);
1391 ceph_assert(q != blocked_by_sum.end());
1392 --q->second;
1393 if (q->second == 0)
1394 blocked_by_sum.erase(q);
1395 }
1396
1397 set<int32_t> actingset;
1398 for (auto p = s.acting.begin(); p != s.acting.end(); ++p) {
1399 actingset.insert(*p);
1400 auto& oset = pg_by_osd[*p];
1401 oset.erase(pgid);
1402 if (oset.empty())
1403 pg_by_osd.erase(*p);
1404 auto it = num_pg_by_osd.find(*p);
1405 if (it != num_pg_by_osd.end() && it->second.acting > 0)
1406 it->second.acting--;
1407 }
1408 for (auto p = s.up.begin(); p != s.up.end(); ++p) {
1409 auto& oset = pg_by_osd[*p];
1410 oset.erase(pgid);
1411 if (oset.empty())
1412 pg_by_osd.erase(*p);
1413 if (actingset.count(*p))
1414 continue;
1415 auto it = num_pg_by_osd.find(*p);
1416 if (it != num_pg_by_osd.end() && it->second.up_not_acting > 0)
1417 it->second.up_not_acting--;
1418 }
1419
1420 if (s.up_primary >= 0) {
1421 auto it = num_pg_by_osd.find(s.up_primary);
1422 if (it != num_pg_by_osd.end() && it->second.primary > 0)
1423 it->second.primary--;
1424 }
1425 return pool_erased;
1426 }
1427
1428 void PGMap::calc_purged_snaps()
1429 {
1430 purged_snaps.clear();
1431 set<int64_t> unknown;
1432 for (auto& i : pg_stat) {
1433 if (i.second.state == 0) {
1434 unknown.insert(i.first.pool());
1435 purged_snaps.erase(i.first.pool());
1436 continue;
1437 } else if (unknown.count(i.first.pool())) {
1438 continue;
1439 }
1440 auto j = purged_snaps.find(i.first.pool());
1441 if (j == purged_snaps.end()) {
1442 // base case
1443 purged_snaps[i.first.pool()] = i.second.purged_snaps;
1444 } else {
1445 j->second.intersection_of(i.second.purged_snaps);
1446 }
1447 }
1448 }
1449
1450 void PGMap::calc_osd_sum_by_class(const OSDMap& osdmap)
1451 {
1452 osd_sum_by_class.clear();
1453 for (auto& i : osd_stat) {
1454 const char *class_name = osdmap.crush->get_item_class(i.first);
1455 if (class_name) {
1456 osd_sum_by_class[class_name].add(i.second);
1457 }
1458 }
1459 }
1460
1461 void PGMap::stat_osd_add(int osd, const osd_stat_t &s)
1462 {
1463 num_osd++;
1464 osd_sum.add(s);
1465 if (osd >= (int)osd_last_seq.size()) {
1466 osd_last_seq.resize(osd + 1);
1467 }
1468 osd_last_seq[osd] = s.seq;
1469 }
1470
1471 void PGMap::stat_osd_sub(int osd, const osd_stat_t &s)
1472 {
1473 num_osd--;
1474 osd_sum.sub(s);
1475 ceph_assert(osd < (int)osd_last_seq.size());
1476 osd_last_seq[osd] = 0;
1477 }
1478
1479 void PGMap::encode_digest(const OSDMap& osdmap,
1480 bufferlist& bl, uint64_t features)
1481 {
1482 get_rules_avail(osdmap, &avail_space_by_rule);
1483 calc_osd_sum_by_class(osdmap);
1484 calc_purged_snaps();
1485 PGMapDigest::encode(bl, features);
1486 }
1487
1488 void PGMap::encode(bufferlist &bl, uint64_t features) const
1489 {
1490 ENCODE_START(8, 8, bl);
1491 encode(version, bl);
1492 encode(pg_stat, bl);
1493 encode(osd_stat, bl, features);
1494 encode(last_osdmap_epoch, bl);
1495 encode(last_pg_scan, bl);
1496 encode(stamp, bl);
1497 encode(pool_statfs, bl, features);
1498 ENCODE_FINISH(bl);
1499 }
1500
1501 void PGMap::decode(bufferlist::const_iterator &bl)
1502 {
1503 DECODE_START(8, bl);
1504 decode(version, bl);
1505 decode(pg_stat, bl);
1506 decode(osd_stat, bl);
1507 decode(last_osdmap_epoch, bl);
1508 decode(last_pg_scan, bl);
1509 decode(stamp, bl);
1510 decode(pool_statfs, bl);
1511 DECODE_FINISH(bl);
1512
1513 calc_stats();
1514 }
1515
1516 void PGMap::dump(ceph::Formatter *f, bool with_net) const
1517 {
1518 dump_basic(f);
1519 dump_pg_stats(f, false);
1520 dump_pool_stats(f);
1521 dump_osd_stats(f, with_net);
1522 }
1523
1524 void PGMap::dump_basic(ceph::Formatter *f) const
1525 {
1526 f->dump_unsigned("version", version);
1527 f->dump_stream("stamp") << stamp;
1528 f->dump_unsigned("last_osdmap_epoch", last_osdmap_epoch);
1529 f->dump_unsigned("last_pg_scan", last_pg_scan);
1530
1531 f->open_object_section("pg_stats_sum");
1532 pg_sum.dump(f);
1533 f->close_section();
1534
1535 f->open_object_section("osd_stats_sum");
1536 osd_sum.dump(f);
1537 f->close_section();
1538
1539 dump_delta(f);
1540 }
1541
1542 void PGMap::dump_delta(ceph::Formatter *f) const
1543 {
1544 f->open_object_section("pg_stats_delta");
1545 pg_sum_delta.dump(f);
1546 f->dump_stream("stamp_delta") << stamp_delta;
1547 f->close_section();
1548 }
1549
1550 void PGMap::dump_pg_stats(ceph::Formatter *f, bool brief) const
1551 {
1552 f->open_array_section("pg_stats");
1553 for (auto i = pg_stat.begin();
1554 i != pg_stat.end();
1555 ++i) {
1556 f->open_object_section("pg_stat");
1557 f->dump_stream("pgid") << i->first;
1558 if (brief)
1559 i->second.dump_brief(f);
1560 else
1561 i->second.dump(f);
1562 f->close_section();
1563 }
1564 f->close_section();
1565 }
1566
1567 void PGMap::dump_pg_progress(ceph::Formatter *f) const
1568 {
1569 f->open_object_section("pgs");
1570 for (auto& i : pg_stat) {
1571 std::string n = stringify(i.first);
1572 f->open_object_section(n.c_str());
1573 f->dump_int("num_bytes_recovered", i.second.stats.sum.num_bytes_recovered);
1574 f->dump_int("num_bytes", i.second.stats.sum.num_bytes);
1575 f->dump_unsigned("reported_epoch", i.second.reported_epoch);
1576 f->dump_string("state", pg_state_string(i.second.state));
1577 f->close_section();
1578 }
1579 f->close_section();
1580 }
1581
1582 void PGMap::dump_pool_stats(ceph::Formatter *f) const
1583 {
1584 f->open_array_section("pool_stats");
1585 for (auto p = pg_pool_sum.begin();
1586 p != pg_pool_sum.end();
1587 ++p) {
1588 f->open_object_section("pool_stat");
1589 f->dump_int("poolid", p->first);
1590 auto q = num_pg_by_pool.find(p->first);
1591 if (q != num_pg_by_pool.end())
1592 f->dump_unsigned("num_pg", q->second);
1593 p->second.dump(f);
1594 f->close_section();
1595 }
1596 f->close_section();
1597 }
1598
1599 void PGMap::dump_osd_stats(ceph::Formatter *f, bool with_net) const
1600 {
1601 f->open_array_section("osd_stats");
1602 for (auto q = osd_stat.begin();
1603 q != osd_stat.end();
1604 ++q) {
1605 f->open_object_section("osd_stat");
1606 f->dump_int("osd", q->first);
1607 q->second.dump(f, with_net);
1608 f->close_section();
1609 }
1610 f->close_section();
1611
1612 f->open_array_section("pool_statfs");
1613 for (auto& p : pool_statfs) {
1614 f->open_object_section("item");
1615 f->dump_int("poolid", p.first.first);
1616 f->dump_int("osd", p.first.second);
1617 p.second.dump(f);
1618 f->close_section();
1619 }
1620 f->close_section();
1621 }
1622
1623 void PGMap::dump_osd_ping_times(ceph::Formatter *f) const
1624 {
1625 f->open_array_section("osd_ping_times");
1626 for (const auto& [osd, stat] : osd_stat) {
1627 f->open_object_section("osd_ping_time");
1628 f->dump_int("osd", osd);
1629 stat.dump_ping_time(f);
1630 f->close_section();
1631 }
1632 f->close_section();
1633 }
1634
1635 // note: dump_pg_stats_plain() is static
1636 void PGMap::dump_pg_stats_plain(
1637 ostream& ss,
1638 const mempool::pgmap::unordered_map<pg_t, pg_stat_t>& pg_stats,
1639 bool brief)
1640 {
1641 TextTable tab;
1642
1643 if (brief){
1644 tab.define_column("PG_STAT", TextTable::LEFT, TextTable::LEFT);
1645 tab.define_column("STATE", TextTable::LEFT, TextTable::RIGHT);
1646 tab.define_column("UP", TextTable::LEFT, TextTable::RIGHT);
1647 tab.define_column("UP_PRIMARY", TextTable::LEFT, TextTable::RIGHT);
1648 tab.define_column("ACTING", TextTable::LEFT, TextTable::RIGHT);
1649 tab.define_column("ACTING_PRIMARY", TextTable::LEFT, TextTable::RIGHT);
1650 }
1651 else {
1652 tab.define_column("PG_STAT", TextTable::LEFT, TextTable::LEFT);
1653 tab.define_column("OBJECTS", TextTable::LEFT, TextTable::RIGHT);
1654 tab.define_column("MISSING_ON_PRIMARY", TextTable::LEFT, TextTable::RIGHT);
1655 tab.define_column("DEGRADED", TextTable::LEFT, TextTable::RIGHT);
1656 tab.define_column("MISPLACED", TextTable::LEFT, TextTable::RIGHT);
1657 tab.define_column("UNFOUND", TextTable::LEFT, TextTable::RIGHT);
1658 tab.define_column("BYTES", TextTable::LEFT, TextTable::RIGHT);
1659 tab.define_column("OMAP_BYTES*", TextTable::LEFT, TextTable::RIGHT);
1660 tab.define_column("OMAP_KEYS*", TextTable::LEFT, TextTable::RIGHT);
1661 tab.define_column("LOG", TextTable::LEFT, TextTable::RIGHT);
1662 tab.define_column("DISK_LOG", TextTable::LEFT, TextTable::RIGHT);
1663 tab.define_column("STATE", TextTable::LEFT, TextTable::RIGHT);
1664 tab.define_column("STATE_STAMP", TextTable::LEFT, TextTable::RIGHT);
1665 tab.define_column("VERSION", TextTable::LEFT, TextTable::RIGHT);
1666 tab.define_column("REPORTED", TextTable::LEFT, TextTable::RIGHT);
1667 tab.define_column("UP", TextTable::LEFT, TextTable::RIGHT);
1668 tab.define_column("UP_PRIMARY", TextTable::LEFT, TextTable::RIGHT);
1669 tab.define_column("ACTING", TextTable::LEFT, TextTable::RIGHT);
1670 tab.define_column("ACTING_PRIMARY", TextTable::LEFT, TextTable::RIGHT);
1671 tab.define_column("LAST_SCRUB", TextTable::LEFT, TextTable::RIGHT);
1672 tab.define_column("SCRUB_STAMP", TextTable::LEFT, TextTable::RIGHT);
1673 tab.define_column("LAST_DEEP_SCRUB", TextTable::LEFT, TextTable::RIGHT);
1674 tab.define_column("DEEP_SCRUB_STAMP", TextTable::LEFT, TextTable::RIGHT);
1675 tab.define_column("SNAPTRIMQ_LEN", TextTable::LEFT, TextTable::RIGHT);
1676 tab.define_column("LAST_SCRUB_DURATION", TextTable::LEFT, TextTable::RIGHT);
1677 tab.define_column("SCRUB_SCHEDULING", TextTable::LEFT, TextTable::LEFT);
1678 tab.define_column("OBJECTS_SCRUBBED", TextTable::LEFT, TextTable::RIGHT);
1679 tab.define_column("OBJECTS_TRIMMED", TextTable::LEFT, TextTable::RIGHT);
1680 }
1681
1682 for (const auto& [pg, st] : pg_stats) {
1683 if (brief) {
1684 tab << pg
1685 << pg_state_string(st.state)
1686 << st.up
1687 << st.up_primary
1688 << st.acting
1689 << st.acting_primary
1690 << TextTable::endrow;
1691 } else {
1692 ostringstream reported;
1693 reported << st.reported_epoch << ":" << st.reported_seq;
1694
1695 tab << pg
1696 << st.stats.sum.num_objects
1697 << st.stats.sum.num_objects_missing_on_primary
1698 << st.stats.sum.num_objects_degraded
1699 << st.stats.sum.num_objects_misplaced
1700 << st.stats.sum.num_objects_unfound
1701 << st.stats.sum.num_bytes
1702 << st.stats.sum.num_omap_bytes
1703 << st.stats.sum.num_omap_keys
1704 << st.log_size
1705 << st.ondisk_log_size
1706 << pg_state_string(st.state)
1707 << st.last_change
1708 << st.version
1709 << reported.str()
1710 << pg_vector_string(st.up)
1711 << st.up_primary
1712 << pg_vector_string(st.acting)
1713 << st.acting_primary
1714 << st.last_scrub
1715 << st.last_scrub_stamp
1716 << st.last_deep_scrub
1717 << st.last_deep_scrub_stamp
1718 << st.snaptrimq_len
1719 << st.last_scrub_duration
1720 << st.dump_scrub_schedule()
1721 << st.objects_scrubbed
1722 << st.objects_trimmed
1723 << TextTable::endrow;
1724 }
1725 }
1726
1727 ss << tab;
1728 }
1729
1730 void PGMap::dump(ostream& ss) const
1731 {
1732 dump_basic(ss);
1733 dump_pg_stats(ss, false);
1734 dump_pool_stats(ss, false);
1735 dump_pg_sum_stats(ss, false);
1736 dump_osd_stats(ss);
1737 }
1738
1739 void PGMap::dump_basic(ostream& ss) const
1740 {
1741 ss << "version " << version << std::endl;
1742 ss << "stamp " << stamp << std::endl;
1743 ss << "last_osdmap_epoch " << last_osdmap_epoch << std::endl;
1744 ss << "last_pg_scan " << last_pg_scan << std::endl;
1745 }
1746
1747 void PGMap::dump_pg_stats(ostream& ss, bool brief) const
1748 {
1749 dump_pg_stats_plain(ss, pg_stat, brief);
1750 }
1751
1752 void PGMap::dump_pool_stats(ostream& ss, bool header) const
1753 {
1754 TextTable tab;
1755
1756 if (header) {
1757 tab.define_column("POOLID", TextTable::LEFT, TextTable::LEFT);
1758 tab.define_column("OBJECTS", TextTable::LEFT, TextTable::RIGHT);
1759 tab.define_column("MISSING_ON_PRIMARY", TextTable::LEFT, TextTable::RIGHT);
1760 tab.define_column("DEGRADED", TextTable::LEFT, TextTable::RIGHT);
1761 tab.define_column("MISPLACED", TextTable::LEFT, TextTable::RIGHT);
1762 tab.define_column("UNFOUND", TextTable::LEFT, TextTable::RIGHT);
1763 tab.define_column("BYTES", TextTable::LEFT, TextTable::RIGHT);
1764 tab.define_column("OMAP_BYTES*", TextTable::LEFT, TextTable::RIGHT);
1765 tab.define_column("OMAP_KEYS*", TextTable::LEFT, TextTable::RIGHT);
1766 tab.define_column("LOG", TextTable::LEFT, TextTable::RIGHT);
1767 tab.define_column("DISK_LOG", TextTable::LEFT, TextTable::RIGHT);
1768 } else {
1769 tab.define_column("", TextTable::LEFT, TextTable::LEFT);
1770 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1771 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1772 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1773 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1774 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1775 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1776 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1777 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1778 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1779 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1780 }
1781
1782 for (auto p = pg_pool_sum.begin();
1783 p != pg_pool_sum.end();
1784 ++p) {
1785 tab << p->first
1786 << p->second.stats.sum.num_objects
1787 << p->second.stats.sum.num_objects_missing_on_primary
1788 << p->second.stats.sum.num_objects_degraded
1789 << p->second.stats.sum.num_objects_misplaced
1790 << p->second.stats.sum.num_objects_unfound
1791 << p->second.stats.sum.num_bytes
1792 << p->second.stats.sum.num_omap_bytes
1793 << p->second.stats.sum.num_omap_keys
1794 << p->second.log_size
1795 << p->second.ondisk_log_size
1796 << TextTable::endrow;
1797 }
1798
1799 ss << tab;
1800 }
1801
1802 void PGMap::dump_pg_sum_stats(ostream& ss, bool header) const
1803 {
1804 TextTable tab;
1805
1806 if (header) {
1807 tab.define_column("PG_STAT", TextTable::LEFT, TextTable::LEFT);
1808 tab.define_column("OBJECTS", TextTable::LEFT, TextTable::RIGHT);
1809 tab.define_column("MISSING_ON_PRIMARY", TextTable::LEFT, TextTable::RIGHT);
1810 tab.define_column("DEGRADED", TextTable::LEFT, TextTable::RIGHT);
1811 tab.define_column("MISPLACED", TextTable::LEFT, TextTable::RIGHT);
1812 tab.define_column("UNFOUND", TextTable::LEFT, TextTable::RIGHT);
1813 tab.define_column("BYTES", TextTable::LEFT, TextTable::RIGHT);
1814 tab.define_column("OMAP_BYTES*", TextTable::LEFT, TextTable::RIGHT);
1815 tab.define_column("OMAP_KEYS*", TextTable::LEFT, TextTable::RIGHT);
1816 tab.define_column("LOG", TextTable::LEFT, TextTable::RIGHT);
1817 tab.define_column("DISK_LOG", TextTable::LEFT, TextTable::RIGHT);
1818 } else {
1819 tab.define_column("", TextTable::LEFT, TextTable::LEFT);
1820 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1821 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1822 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1823 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1824 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1825 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1826 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1827 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1828 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1829 tab.define_column("", TextTable::LEFT, TextTable::RIGHT);
1830 };
1831
1832 tab << "sum"
1833 << pg_sum.stats.sum.num_objects
1834 << pg_sum.stats.sum.num_objects_missing_on_primary
1835 << pg_sum.stats.sum.num_objects_degraded
1836 << pg_sum.stats.sum.num_objects_misplaced
1837 << pg_sum.stats.sum.num_objects_unfound
1838 << pg_sum.stats.sum.num_bytes
1839 << pg_sum.stats.sum.num_omap_bytes
1840 << pg_sum.stats.sum.num_omap_keys
1841 << pg_sum.log_size
1842 << pg_sum.ondisk_log_size
1843 << TextTable::endrow;
1844
1845 ss << tab;
1846 }
1847
1848 void PGMap::dump_osd_stats(ostream& ss) const
1849 {
1850 TextTable tab;
1851
1852 tab.define_column("OSD_STAT", TextTable::LEFT, TextTable::LEFT);
1853 tab.define_column("USED", TextTable::LEFT, TextTable::RIGHT);
1854 tab.define_column("AVAIL", TextTable::LEFT, TextTable::RIGHT);
1855 tab.define_column("USED_RAW", TextTable::LEFT, TextTable::RIGHT);
1856 tab.define_column("TOTAL", TextTable::LEFT, TextTable::RIGHT);
1857 tab.define_column("HB_PEERS", TextTable::LEFT, TextTable::RIGHT);
1858 tab.define_column("PG_SUM", TextTable::LEFT, TextTable::RIGHT);
1859 tab.define_column("PRIMARY_PG_SUM", TextTable::LEFT, TextTable::RIGHT);
1860
1861 for (auto p = osd_stat.begin();
1862 p != osd_stat.end();
1863 ++p) {
1864 tab << p->first
1865 << byte_u_t(p->second.statfs.get_used())
1866 << byte_u_t(p->second.statfs.available)
1867 << byte_u_t(p->second.statfs.get_used_raw())
1868 << byte_u_t(p->second.statfs.total)
1869 << p->second.hb_peers
1870 << get_num_pg_by_osd(p->first)
1871 << get_num_primary_pg_by_osd(p->first)
1872 << TextTable::endrow;
1873 }
1874
1875 tab << "sum"
1876 << byte_u_t(osd_sum.statfs.get_used())
1877 << byte_u_t(osd_sum.statfs.available)
1878 << byte_u_t(osd_sum.statfs.get_used_raw())
1879 << byte_u_t(osd_sum.statfs.total)
1880 << TextTable::endrow;
1881
1882 ss << tab;
1883 }
1884
1885 void PGMap::dump_osd_sum_stats(ostream& ss) const
1886 {
1887 TextTable tab;
1888
1889 tab.define_column("OSD_STAT", TextTable::LEFT, TextTable::LEFT);
1890 tab.define_column("USED", TextTable::LEFT, TextTable::RIGHT);
1891 tab.define_column("AVAIL", TextTable::LEFT, TextTable::RIGHT);
1892 tab.define_column("USED_RAW", TextTable::LEFT, TextTable::RIGHT);
1893 tab.define_column("TOTAL", TextTable::LEFT, TextTable::RIGHT);
1894
1895 tab << "sum"
1896 << byte_u_t(osd_sum.statfs.get_used())
1897 << byte_u_t(osd_sum.statfs.available)
1898 << byte_u_t(osd_sum.statfs.get_used_raw())
1899 << byte_u_t(osd_sum.statfs.total)
1900 << TextTable::endrow;
1901
1902 ss << tab;
1903 }
1904
1905 void PGMap::get_stuck_stats(
1906 int types, const utime_t cutoff,
1907 mempool::pgmap::unordered_map<pg_t, pg_stat_t>& stuck_pgs) const
1908 {
1909 ceph_assert(types != 0);
1910 for (auto i = pg_stat.begin();
1911 i != pg_stat.end();
1912 ++i) {
1913 utime_t val = cutoff; // don't care about >= cutoff so that is infinity
1914
1915 if ((types & STUCK_INACTIVE) && !(i->second.state & PG_STATE_ACTIVE)) {
1916 if (i->second.last_active < val)
1917 val = i->second.last_active;
1918 }
1919
1920 if ((types & STUCK_UNCLEAN) && !(i->second.state & PG_STATE_CLEAN)) {
1921 if (i->second.last_clean < val)
1922 val = i->second.last_clean;
1923 }
1924
1925 if ((types & STUCK_DEGRADED) && (i->second.state & PG_STATE_DEGRADED)) {
1926 if (i->second.last_undegraded < val)
1927 val = i->second.last_undegraded;
1928 }
1929
1930 if ((types & STUCK_UNDERSIZED) && (i->second.state & PG_STATE_UNDERSIZED)) {
1931 if (i->second.last_fullsized < val)
1932 val = i->second.last_fullsized;
1933 }
1934
1935 if ((types & STUCK_STALE) && (i->second.state & PG_STATE_STALE)) {
1936 if (i->second.last_unstale < val)
1937 val = i->second.last_unstale;
1938 }
1939
1940 // val is now the earliest any of the requested stuck states began
1941 if (val < cutoff) {
1942 stuck_pgs[i->first] = i->second;
1943 }
1944 }
1945 }
1946
1947 void PGMap::dump_stuck(ceph::Formatter *f, int types, utime_t cutoff) const
1948 {
1949 mempool::pgmap::unordered_map<pg_t, pg_stat_t> stuck_pg_stats;
1950 get_stuck_stats(types, cutoff, stuck_pg_stats);
1951 f->open_array_section("stuck_pg_stats");
1952 for (auto i = stuck_pg_stats.begin();
1953 i != stuck_pg_stats.end();
1954 ++i) {
1955 f->open_object_section("pg_stat");
1956 f->dump_stream("pgid") << i->first;
1957 i->second.dump(f);
1958 f->close_section();
1959 }
1960 f->close_section();
1961 }
1962
1963 void PGMap::dump_stuck_plain(ostream& ss, int types, utime_t cutoff) const
1964 {
1965 mempool::pgmap::unordered_map<pg_t, pg_stat_t> stuck_pg_stats;
1966 get_stuck_stats(types, cutoff, stuck_pg_stats);
1967 if (!stuck_pg_stats.empty())
1968 dump_pg_stats_plain(ss, stuck_pg_stats, true);
1969 }
1970
1971 int PGMap::dump_stuck_pg_stats(
1972 stringstream &ds,
1973 ceph::Formatter *f,
1974 int threshold,
1975 vector<string>& args) const
1976 {
1977 int stuck_types = 0;
1978
1979 for (auto i = args.begin(); i != args.end(); ++i) {
1980 if (*i == "inactive")
1981 stuck_types |= PGMap::STUCK_INACTIVE;
1982 else if (*i == "unclean")
1983 stuck_types |= PGMap::STUCK_UNCLEAN;
1984 else if (*i == "undersized")
1985 stuck_types |= PGMap::STUCK_UNDERSIZED;
1986 else if (*i == "degraded")
1987 stuck_types |= PGMap::STUCK_DEGRADED;
1988 else if (*i == "stale")
1989 stuck_types |= PGMap::STUCK_STALE;
1990 else {
1991 ds << "Unknown type: " << *i << std::endl;
1992 return -EINVAL;
1993 }
1994 }
1995
1996 utime_t now(ceph_clock_now());
1997 utime_t cutoff = now - utime_t(threshold, 0);
1998
1999 if (!f) {
2000 dump_stuck_plain(ds, stuck_types, cutoff);
2001 } else {
2002 dump_stuck(f, stuck_types, cutoff);
2003 f->flush(ds);
2004 }
2005
2006 return 0;
2007 }
2008
2009 void PGMap::dump_osd_perf_stats(ceph::Formatter *f) const
2010 {
2011 f->open_array_section("osd_perf_infos");
2012 for (auto i = osd_stat.begin();
2013 i != osd_stat.end();
2014 ++i) {
2015 f->open_object_section("osd");
2016 f->dump_int("id", i->first);
2017 {
2018 f->open_object_section("perf_stats");
2019 i->second.os_perf_stat.dump(f);
2020 f->close_section();
2021 }
2022 f->close_section();
2023 }
2024 f->close_section();
2025 }
2026 void PGMap::print_osd_perf_stats(std::ostream *ss) const
2027 {
2028 TextTable tab;
2029 tab.define_column("osd", TextTable::LEFT, TextTable::RIGHT);
2030 tab.define_column("commit_latency(ms)", TextTable::LEFT, TextTable::RIGHT);
2031 tab.define_column("apply_latency(ms)", TextTable::LEFT, TextTable::RIGHT);
2032 for (auto i = osd_stat.begin();
2033 i != osd_stat.end();
2034 ++i) {
2035 tab << i->first;
2036 tab << i->second.os_perf_stat.os_commit_latency_ns / 1000000ull;
2037 tab << i->second.os_perf_stat.os_apply_latency_ns / 1000000ull;
2038 tab << TextTable::endrow;
2039 }
2040 (*ss) << tab;
2041 }
2042
2043 void PGMap::dump_osd_blocked_by_stats(ceph::Formatter *f) const
2044 {
2045 f->open_array_section("osd_blocked_by_infos");
2046 for (auto i = blocked_by_sum.begin();
2047 i != blocked_by_sum.end();
2048 ++i) {
2049 f->open_object_section("osd");
2050 f->dump_int("id", i->first);
2051 f->dump_int("num_blocked", i->second);
2052 f->close_section();
2053 }
2054 f->close_section();
2055 }
2056 void PGMap::print_osd_blocked_by_stats(std::ostream *ss) const
2057 {
2058 TextTable tab;
2059 tab.define_column("osd", TextTable::LEFT, TextTable::RIGHT);
2060 tab.define_column("num_blocked", TextTable::LEFT, TextTable::RIGHT);
2061 for (auto i = blocked_by_sum.begin();
2062 i != blocked_by_sum.end();
2063 ++i) {
2064 tab << i->first;
2065 tab << i->second;
2066 tab << TextTable::endrow;
2067 }
2068 (*ss) << tab;
2069 }
2070
2071
2072 /**
2073 * update aggregated delta
2074 *
2075 * @param cct ceph context
2076 * @param ts Timestamp for the stats being delta'ed
2077 * @param old_pool_sum Previous stats sum
2078 * @param last_ts Last timestamp for pool
2079 * @param result_pool_sum Resulting stats
2080 * @param result_pool_delta Resulting pool delta
2081 * @param result_ts_delta Resulting timestamp delta
2082 * @param delta_avg_list List of last N computed deltas, used to average
2083 */
2084 void PGMap::update_delta(
2085 CephContext *cct,
2086 const utime_t ts,
2087 const pool_stat_t& old_pool_sum,
2088 utime_t *last_ts,
2089 const pool_stat_t& current_pool_sum,
2090 pool_stat_t *result_pool_delta,
2091 utime_t *result_ts_delta,
2092 mempool::pgmap::list<pair<pool_stat_t,utime_t> > *delta_avg_list)
2093 {
2094 /* @p ts is the timestamp we want to associate with the data
2095 * in @p old_pool_sum, and on which we will base ourselves to
2096 * calculate the delta, stored in 'delta_t'.
2097 */
2098 utime_t delta_t;
2099 delta_t = ts; // start with the provided timestamp
2100 delta_t -= *last_ts; // take the last timestamp we saw
2101 *last_ts = ts; // @p ts becomes the last timestamp we saw
2102
2103 // adjust delta_t, quick start if there is no update in a long period
2104 delta_t = std::min(delta_t,
2105 utime_t(2 * (cct ? cct->_conf->mon_delta_reset_interval : 10), 0));
2106
2107 // calculate a delta, and average over the last 6 deltas by default.
2108 /* start by taking a copy of our current @p result_pool_sum, and by
2109 * taking out the stats from @p old_pool_sum. This generates a stats
2110 * delta. Stash this stats delta in @p delta_avg_list, along with the
2111 * timestamp delta for these results.
2112 */
2113 pool_stat_t d = current_pool_sum;
2114 d.stats.sub(old_pool_sum.stats);
2115
2116 /* Aggregate current delta, and take out the last seen delta (if any) to
2117 * average it out.
2118 * Skip calculating delta while sum was not synchronized.
2119 */
2120 if(!old_pool_sum.stats.sum.is_zero()) {
2121 delta_avg_list->push_back(make_pair(d,delta_t));
2122 *result_ts_delta += delta_t;
2123 result_pool_delta->stats.add(d.stats);
2124 }
2125 size_t s = cct ? cct->_conf.get_val<uint64_t>("mon_stat_smooth_intervals") : 1;
2126 while (delta_avg_list->size() > s) {
2127 result_pool_delta->stats.sub(delta_avg_list->front().first.stats);
2128 *result_ts_delta -= delta_avg_list->front().second;
2129 delta_avg_list->pop_front();
2130 }
2131 }
2132
2133 /**
2134 * Update a given pool's deltas
2135 *
2136 * @param cct Ceph Context
2137 * @param ts Timestamp for the stats being delta'ed
2138 * @param pool Pool's id
2139 * @param old_pool_sum Previous stats sum
2140 */
2141 void PGMap::update_one_pool_delta(
2142 CephContext *cct,
2143 const utime_t ts,
2144 const int64_t pool,
2145 const pool_stat_t& old_pool_sum)
2146 {
2147 if (per_pool_sum_deltas.count(pool) == 0) {
2148 ceph_assert(per_pool_sum_deltas_stamps.count(pool) == 0);
2149 ceph_assert(per_pool_sum_delta.count(pool) == 0);
2150 }
2151
2152 auto& sum_delta = per_pool_sum_delta[pool];
2153
2154 update_delta(cct, ts, old_pool_sum, &sum_delta.second, pg_pool_sum[pool],
2155 &sum_delta.first, &per_pool_sum_deltas_stamps[pool],
2156 &per_pool_sum_deltas[pool]);
2157 }
2158
2159 /**
2160 * Update pools' deltas
2161 *
2162 * @param cct CephContext
2163 * @param ts Timestamp for the stats being delta'ed
2164 * @param pg_pool_sum_old Map of pool stats for delta calcs.
2165 */
2166 void PGMap::update_pool_deltas(
2167 CephContext *cct, const utime_t ts,
2168 const mempool::pgmap::unordered_map<int32_t,pool_stat_t>& pg_pool_sum_old)
2169 {
2170 for (auto it = pg_pool_sum_old.begin();
2171 it != pg_pool_sum_old.end(); ++it) {
2172 update_one_pool_delta(cct, ts, it->first, it->second);
2173 }
2174 }
2175
2176 void PGMap::clear_delta()
2177 {
2178 pg_sum_delta = pool_stat_t();
2179 pg_sum_deltas.clear();
2180 stamp_delta = utime_t();
2181 }
2182
2183 void PGMap::generate_test_instances(list<PGMap*>& o)
2184 {
2185 o.push_back(new PGMap);
2186 list<Incremental*> inc;
2187 Incremental::generate_test_instances(inc);
2188 delete inc.front();
2189 inc.pop_front();
2190 while (!inc.empty()) {
2191 PGMap *pmp = new PGMap();
2192 *pmp = *o.back();
2193 o.push_back(pmp);
2194 o.back()->apply_incremental(NULL, *inc.front());
2195 delete inc.front();
2196 inc.pop_front();
2197 }
2198 }
2199
2200 void PGMap::get_filtered_pg_stats(uint64_t state, int64_t poolid, int64_t osdid,
2201 bool primary, set<pg_t>& pgs) const
2202 {
2203 for (auto i = pg_stat.begin();
2204 i != pg_stat.end();
2205 ++i) {
2206 if ((poolid >= 0) && (poolid != i->first.pool()))
2207 continue;
2208 if ((osdid >= 0) && !(i->second.is_acting_osd(osdid,primary)))
2209 continue;
2210 if (state == (uint64_t)-1 || // "all"
2211 (i->second.state & state) || // matches a state bit
2212 (state == 0 && i->second.state == 0)) { // matches "unknown" (== 0)
2213 pgs.insert(i->first);
2214 }
2215 }
2216 }
2217
2218 void PGMap::dump_filtered_pg_stats(ceph::Formatter *f, set<pg_t>& pgs) const
2219 {
2220 f->open_array_section("pg_stats");
2221 for (auto i = pgs.begin(); i != pgs.end(); ++i) {
2222 const pg_stat_t& st = pg_stat.at(*i);
2223 f->open_object_section("pg_stat");
2224 f->dump_stream("pgid") << *i;
2225 st.dump(f);
2226 f->close_section();
2227 }
2228 f->close_section();
2229 }
2230
2231 void PGMap::dump_filtered_pg_stats(ostream& ss, set<pg_t>& pgs) const
2232 {
2233 TextTable tab;
2234 utime_t now = ceph_clock_now();
2235
2236 tab.define_column("PG", TextTable::LEFT, TextTable::LEFT);
2237 tab.define_column("OBJECTS", TextTable::LEFT, TextTable::RIGHT);
2238 tab.define_column("DEGRADED", TextTable::LEFT, TextTable::RIGHT);
2239 tab.define_column("MISPLACED", TextTable::LEFT, TextTable::RIGHT);
2240 tab.define_column("UNFOUND", TextTable::LEFT, TextTable::RIGHT);
2241 tab.define_column("BYTES", TextTable::LEFT, TextTable::RIGHT);
2242 tab.define_column("OMAP_BYTES*", TextTable::LEFT, TextTable::RIGHT);
2243 tab.define_column("OMAP_KEYS*", TextTable::LEFT, TextTable::RIGHT);
2244 tab.define_column("LOG", TextTable::LEFT, TextTable::RIGHT);
2245 tab.define_column("STATE", TextTable::LEFT, TextTable::RIGHT);
2246 tab.define_column("SINCE", TextTable::LEFT, TextTable::RIGHT);
2247 tab.define_column("VERSION", TextTable::LEFT, TextTable::RIGHT);
2248 tab.define_column("REPORTED", TextTable::LEFT, TextTable::RIGHT);
2249 tab.define_column("UP", TextTable::LEFT, TextTable::RIGHT);
2250 tab.define_column("ACTING", TextTable::LEFT, TextTable::RIGHT);
2251 tab.define_column("SCRUB_STAMP", TextTable::LEFT, TextTable::RIGHT);
2252 tab.define_column("DEEP_SCRUB_STAMP", TextTable::LEFT, TextTable::RIGHT);
2253 tab.define_column("LAST_SCRUB_DURATION", TextTable::LEFT, TextTable::RIGHT);
2254 tab.define_column("SCRUB_SCHEDULING", TextTable::LEFT, TextTable::LEFT);
2255
2256 for (auto i = pgs.begin(); i != pgs.end(); ++i) {
2257 const pg_stat_t& st = pg_stat.at(*i);
2258
2259 ostringstream reported;
2260 reported << st.reported_epoch << ":" << st.reported_seq;
2261
2262 ostringstream upstr, actingstr;
2263 upstr << pg_vector_string(st.up) << 'p' << st.up_primary;
2264 actingstr << pg_vector_string(st.acting) << 'p' << st.acting_primary;
2265 tab << *i
2266 << st.stats.sum.num_objects
2267 << st.stats.sum.num_objects_degraded
2268 << st.stats.sum.num_objects_misplaced
2269 << st.stats.sum.num_objects_unfound
2270 << st.stats.sum.num_bytes
2271 << st.stats.sum.num_omap_bytes
2272 << st.stats.sum.num_omap_keys
2273 << st.log_size
2274 << pg_state_string(st.state)
2275 << utimespan_str(now - st.last_change)
2276 << st.version
2277 << reported.str()
2278 << upstr.str()
2279 << actingstr.str()
2280 << st.last_scrub_stamp
2281 << st.last_deep_scrub_stamp
2282 << st.last_scrub_duration
2283 << st.dump_scrub_schedule()
2284 << TextTable::endrow;
2285 }
2286
2287 ss << tab;
2288 }
2289
2290 void PGMap::dump_pool_stats_and_io_rate(int64_t poolid, const OSDMap &osd_map,
2291 ceph::Formatter *f,
2292 stringstream *rs) const {
2293 const string& pool_name = osd_map.get_pool_name(poolid);
2294 if (f) {
2295 f->open_object_section("pool");
2296 f->dump_string("pool_name", pool_name.c_str());
2297 f->dump_int("pool_id", poolid);
2298 f->open_object_section("recovery");
2299 }
2300 list<string> sl;
2301 stringstream tss;
2302 pool_recovery_summary(f, &sl, poolid);
2303 if (!f && !sl.empty()) {
2304 for (auto &p : sl)
2305 tss << " " << p << "\n";
2306 }
2307 if (f) {
2308 f->close_section(); // object section recovery
2309 f->open_object_section("recovery_rate");
2310 }
2311 ostringstream rss;
2312 pool_recovery_rate_summary(f, &rss, poolid);
2313 if (!f && !rss.str().empty())
2314 tss << " recovery io " << rss.str() << "\n";
2315 if (f) {
2316 f->close_section(); // object section recovery_rate
2317 f->open_object_section("client_io_rate");
2318 }
2319 rss.clear();
2320 rss.str("");
2321 pool_client_io_rate_summary(f, &rss, poolid);
2322 if (!f && !rss.str().empty())
2323 tss << " client io " << rss.str() << "\n";
2324 // dump cache tier IO rate for cache pool
2325 const pg_pool_t *pool = osd_map.get_pg_pool(poolid);
2326 if (pool->is_tier()) {
2327 if (f) {
2328 f->close_section(); // object section client_io_rate
2329 f->open_object_section("cache_io_rate");
2330 }
2331 rss.clear();
2332 rss.str("");
2333 pool_cache_io_rate_summary(f, &rss, poolid);
2334 if (!f && !rss.str().empty())
2335 tss << " cache tier io " << rss.str() << "\n";
2336 }
2337 if (f) {
2338 f->close_section(); // object section cache_io_rate
2339 f->close_section(); // object section pool
2340 } else {
2341 *rs << "pool " << pool_name << " id " << poolid << "\n";
2342 if (!tss.str().empty())
2343 *rs << tss.str() << "\n";
2344 else
2345 *rs << " nothing is going on\n\n";
2346 }
2347 }
2348
2349 // Get crush parentage for an osd (skip root)
2350 set<std::string> PGMap::osd_parentage(const OSDMap& osdmap, int id) const
2351 {
2352 set<std::string> reporters_by_subtree;
2353 auto reporter_subtree_level = g_conf().get_val<string>("mon_osd_reporter_subtree_level");
2354
2355 auto loc = osdmap.crush->get_full_location(id);
2356 for (auto& [parent_bucket_type, parent_id] : loc) {
2357 // Should we show the root? Might not be too informative like "default"
2358 if (parent_bucket_type != "root" &&
2359 parent_bucket_type != reporter_subtree_level) {
2360 reporters_by_subtree.insert(parent_id);
2361 }
2362 }
2363 return reporters_by_subtree;
2364 }
2365
2366 void PGMap::get_health_checks(
2367 CephContext *cct,
2368 const OSDMap& osdmap,
2369 health_check_map_t *checks) const
2370 {
2371 utime_t now = ceph_clock_now();
2372 const auto max = cct->_conf.get_val<uint64_t>("mon_health_max_detail");
2373 const auto& pools = osdmap.get_pools();
2374
2375 typedef enum pg_consequence_t {
2376 UNAVAILABLE = 1, // Client IO to the pool may block
2377 DEGRADED = 2, // Fewer than the requested number of replicas are present
2378 BACKFILL_FULL = 3, // Backfill is blocked for space considerations
2379 // This may or may not be a deadlock condition.
2380 DAMAGED = 4, // The data may be missing or inconsistent on disk and
2381 // requires repair
2382 RECOVERY_FULL = 5 // Recovery is blocked because OSDs are full
2383 } pg_consequence_t;
2384
2385 // For a given PG state, how should it be reported at the pool level?
2386 class PgStateResponse {
2387 public:
2388 pg_consequence_t consequence;
2389 typedef std::function< utime_t(const pg_stat_t&) > stuck_cb;
2390 stuck_cb stuck_since;
2391 bool invert;
2392
2393 PgStateResponse(const pg_consequence_t& c, stuck_cb&& s)
2394 : consequence(c), stuck_since(std::move(s)), invert(false)
2395 {
2396 }
2397
2398 PgStateResponse(const pg_consequence_t& c, stuck_cb&& s, bool i)
2399 : consequence(c), stuck_since(std::move(s)), invert(i)
2400 {
2401 }
2402 };
2403
2404 // Record the PG state counts that contributed to a reported pool state
2405 class PgCauses {
2406 public:
2407 // Map of PG_STATE_* to number of pgs in that state.
2408 std::map<unsigned, unsigned> states;
2409
2410 // List of all PG IDs that had a state contributing
2411 // to this health condition.
2412 std::set<pg_t> pgs;
2413
2414 std::map<pg_t, std::string> pg_messages;
2415 };
2416
2417 // Map of PG state to how to respond to it
2418 std::map<unsigned, PgStateResponse> state_to_response = {
2419 // Immediate reports
2420 { PG_STATE_INCONSISTENT, {DAMAGED, {}} },
2421 { PG_STATE_INCOMPLETE, {UNAVAILABLE, {}} },
2422 { PG_STATE_SNAPTRIM_ERROR, {DAMAGED, {}} },
2423 { PG_STATE_RECOVERY_UNFOUND, {DAMAGED, {}} },
2424 { PG_STATE_BACKFILL_UNFOUND, {DAMAGED, {}} },
2425 { PG_STATE_BACKFILL_TOOFULL, {BACKFILL_FULL, {}} },
2426 { PG_STATE_RECOVERY_TOOFULL, {RECOVERY_FULL, {}} },
2427 { PG_STATE_DEGRADED, {DEGRADED, {}} },
2428 { PG_STATE_DOWN, {UNAVAILABLE, {}} },
2429 // Delayed (wait until stuck) reports
2430 { PG_STATE_PEERING, {UNAVAILABLE, [](const pg_stat_t &p){return p.last_peered;} } },
2431 { PG_STATE_UNDERSIZED, {DEGRADED, [](const pg_stat_t &p){return p.last_fullsized;} } },
2432 { PG_STATE_STALE, {UNAVAILABLE, [](const pg_stat_t &p){return p.last_unstale;} } },
2433 // Delayed and inverted reports
2434 { PG_STATE_ACTIVE, {UNAVAILABLE, [](const pg_stat_t &p){return p.last_active;}, true} }
2435 };
2436
2437 // Specialized state printer that takes account of inversion of
2438 // ACTIVE, CLEAN checks.
2439 auto state_name = [](const uint64_t &state) {
2440 // Special cases for the states that are inverted checks
2441 if (state == PG_STATE_CLEAN) {
2442 return std::string("unclean");
2443 } else if (state == PG_STATE_ACTIVE) {
2444 return std::string("inactive");
2445 } else {
2446 return pg_state_string(state);
2447 }
2448 };
2449
2450 // Map of what is wrong to information about why, implicitly also stores
2451 // the list of what is wrong.
2452 std::map<pg_consequence_t, PgCauses> detected;
2453
2454 // Optimisation: trim down the number of checks to apply based on
2455 // the summary counters
2456 std::map<unsigned, PgStateResponse> possible_responses;
2457 for (const auto &i : num_pg_by_state) {
2458 for (const auto &j : state_to_response) {
2459 if (!j.second.invert) {
2460 // Check for normal tests by seeing if any pgs have the flag
2461 if (i.first & j.first) {
2462 possible_responses.insert(j);
2463 }
2464 }
2465 }
2466 }
2467
2468 for (const auto &j : state_to_response) {
2469 if (j.second.invert) {
2470 // Check for inverted tests by seeing if not-all pgs have the flag
2471 const auto &found = num_pg_by_state.find(j.first);
2472 if (found == num_pg_by_state.end() || found->second != num_pg) {
2473 possible_responses.insert(j);
2474 }
2475 }
2476 }
2477
2478 utime_t cutoff = now - utime_t(cct->_conf.get_val<int64_t>("mon_pg_stuck_threshold"), 0);
2479 // Loop over all PGs, if there are any possibly-unhealthy states in there
2480 if (!possible_responses.empty()) {
2481 for (const auto& i : pg_stat) {
2482 const auto &pg_id = i.first;
2483 const auto &pg_info = i.second;
2484
2485 for (const auto &j : state_to_response) {
2486 const auto &pg_response_state = j.first;
2487 const auto &pg_response = j.second;
2488
2489 // Apply the state test
2490 if (!(bool(pg_info.state & pg_response_state) != pg_response.invert)) {
2491 continue;
2492 }
2493
2494 // Apply stuckness test if needed
2495 if (pg_response.stuck_since) {
2496 // Delayed response, check for stuckness
2497 utime_t last_whatever = pg_response.stuck_since(pg_info);
2498 if (last_whatever.is_zero() &&
2499 pg_info.last_change >= cutoff) {
2500 // still moving, ignore
2501 continue;
2502 } else if (last_whatever >= cutoff) {
2503 // Not stuck enough, ignore.
2504 continue;
2505 } else {
2506
2507 }
2508 }
2509
2510 auto &causes = detected[pg_response.consequence];
2511 causes.states[pg_response_state]++;
2512 causes.pgs.insert(pg_id);
2513
2514 // Don't bother composing detail string if we have already recorded
2515 // too many
2516 if (causes.pg_messages.size() > max) {
2517 continue;
2518 }
2519
2520 std::ostringstream ss;
2521 if (pg_response.stuck_since) {
2522 utime_t since = pg_response.stuck_since(pg_info);
2523 ss << "pg " << pg_id << " is stuck " << state_name(pg_response_state);
2524 if (since == utime_t()) {
2525 ss << " since forever";
2526 } else {
2527 utime_t dur = now - since;
2528 ss << " for " << utimespan_str(dur);
2529 }
2530 ss << ", current state " << pg_state_string(pg_info.state)
2531 << ", last acting " << pg_vector_string(pg_info.acting);
2532 } else {
2533 ss << "pg " << pg_id << " is "
2534 << pg_state_string(pg_info.state);
2535 ss << ", acting " << pg_vector_string(pg_info.acting);
2536 if (pg_info.stats.sum.num_objects_unfound) {
2537 ss << ", " << pg_info.stats.sum.num_objects_unfound
2538 << " unfound";
2539 }
2540 }
2541
2542 if (pg_info.state & PG_STATE_INCOMPLETE) {
2543 const pg_pool_t *pi = osdmap.get_pg_pool(pg_id.pool());
2544 if (pi && pi->min_size > 1) {
2545 ss << " (reducing pool "
2546 << osdmap.get_pool_name(pg_id.pool())
2547 << " min_size from " << (int)pi->min_size
2548 << " may help; search ceph.com/docs for 'incomplete')";
2549 }
2550 }
2551
2552 causes.pg_messages[pg_id] = ss.str();
2553 }
2554 }
2555 } else {
2556 dout(10) << __func__ << " skipping loop over PGs: counters look OK" << dendl;
2557 }
2558
2559 for (const auto &i : detected) {
2560 std::string health_code;
2561 health_status_t sev;
2562 std::string summary;
2563 switch(i.first) {
2564 case UNAVAILABLE:
2565 health_code = "PG_AVAILABILITY";
2566 sev = HEALTH_WARN;
2567 summary = "Reduced data availability: ";
2568 break;
2569 case DEGRADED:
2570 health_code = "PG_DEGRADED";
2571 summary = "Degraded data redundancy: ";
2572 sev = HEALTH_WARN;
2573 break;
2574 case BACKFILL_FULL:
2575 health_code = "PG_BACKFILL_FULL";
2576 summary = "Low space hindering backfill (add storage if this doesn't resolve itself): ";
2577 sev = HEALTH_WARN;
2578 break;
2579 case DAMAGED:
2580 health_code = "PG_DAMAGED";
2581 summary = "Possible data damage: ";
2582 sev = HEALTH_ERR;
2583 break;
2584 case RECOVERY_FULL:
2585 health_code = "PG_RECOVERY_FULL";
2586 summary = "Full OSDs blocking recovery: ";
2587 sev = HEALTH_ERR;
2588 break;
2589 default:
2590 ceph_abort();
2591 }
2592
2593 if (i.first == DEGRADED) {
2594 if (pg_sum.stats.sum.num_objects_degraded &&
2595 pg_sum.stats.sum.num_object_copies > 0) {
2596 double pc = (double)pg_sum.stats.sum.num_objects_degraded /
2597 (double)pg_sum.stats.sum.num_object_copies * (double)100.0;
2598 char b[20];
2599 snprintf(b, sizeof(b), "%.3lf", pc);
2600 ostringstream ss;
2601 ss << pg_sum.stats.sum.num_objects_degraded
2602 << "/" << pg_sum.stats.sum.num_object_copies << " objects degraded ("
2603 << b << "%)";
2604
2605 // Throw in a comma for the benefit of the following PG counts
2606 summary += ss.str() + ", ";
2607 }
2608 }
2609
2610 // Compose summary message saying how many PGs in what states led
2611 // to this health check failing
2612 std::vector<std::string> pg_msgs;
2613 int64_t count = 0;
2614 for (const auto &j : i.second.states) {
2615 std::ostringstream msg;
2616 msg << j.second << (j.second > 1 ? " pgs " : " pg ") << state_name(j.first);
2617 pg_msgs.push_back(msg.str());
2618 count += j.second;
2619 }
2620 summary += joinify(pg_msgs.begin(), pg_msgs.end(), std::string(", "));
2621
2622 health_check_t *check = &checks->add(
2623 health_code,
2624 sev,
2625 summary,
2626 count);
2627
2628 // Compose list of PGs contributing to this health check failing
2629 for (const auto &j : i.second.pg_messages) {
2630 check->detail.push_back(j.second);
2631 }
2632 }
2633
2634 // OSD_SCRUB_ERRORS
2635 if (pg_sum.stats.sum.num_scrub_errors) {
2636 ostringstream ss;
2637 ss << pg_sum.stats.sum.num_scrub_errors << " scrub errors";
2638 checks->add("OSD_SCRUB_ERRORS", HEALTH_ERR, ss.str(),
2639 pg_sum.stats.sum.num_scrub_errors);
2640 }
2641
2642 // LARGE_OMAP_OBJECTS
2643 if (pg_sum.stats.sum.num_large_omap_objects) {
2644 list<string> detail;
2645 for (auto &pool : pools) {
2646 const string& pool_name = osdmap.get_pool_name(pool.first);
2647 auto it2 = pg_pool_sum.find(pool.first);
2648 if (it2 == pg_pool_sum.end()) {
2649 continue;
2650 }
2651 const pool_stat_t *pstat = &it2->second;
2652 if (pstat == nullptr) {
2653 continue;
2654 }
2655 const object_stat_sum_t& sum = pstat->stats.sum;
2656 if (sum.num_large_omap_objects) {
2657 stringstream ss;
2658 ss << sum.num_large_omap_objects << " large objects found in pool "
2659 << "'" << pool_name << "'";
2660 detail.push_back(ss.str());
2661 }
2662 }
2663 if (!detail.empty()) {
2664 ostringstream ss;
2665 ss << pg_sum.stats.sum.num_large_omap_objects << " large omap objects";
2666 auto& d = checks->add("LARGE_OMAP_OBJECTS", HEALTH_WARN, ss.str(),
2667 pg_sum.stats.sum.num_large_omap_objects);
2668 stringstream tip;
2669 tip << "Search the cluster log for 'Large omap object found' for more "
2670 << "details.";
2671 detail.push_back(tip.str());
2672 d.detail.swap(detail);
2673 }
2674 }
2675
2676 // CACHE_POOL_NEAR_FULL
2677 {
2678 list<string> detail;
2679 unsigned num_pools = 0;
2680 for (auto& p : pools) {
2681 if ((!p.second.target_max_objects && !p.second.target_max_bytes) ||
2682 !pg_pool_sum.count(p.first)) {
2683 continue;
2684 }
2685 bool nearfull = false;
2686 const string& name = osdmap.get_pool_name(p.first);
2687 const pool_stat_t& st = get_pg_pool_sum_stat(p.first);
2688 uint64_t ratio = p.second.cache_target_full_ratio_micro +
2689 ((1000000 - p.second.cache_target_full_ratio_micro) *
2690 cct->_conf->mon_cache_target_full_warn_ratio);
2691 if (p.second.target_max_objects &&
2692 (uint64_t)(st.stats.sum.num_objects -
2693 st.stats.sum.num_objects_hit_set_archive) >
2694 p.second.target_max_objects * (ratio / 1000000.0)) {
2695 ostringstream ss;
2696 ss << "cache pool '" << name << "' with "
2697 << si_u_t(st.stats.sum.num_objects)
2698 << " objects at/near target max "
2699 << si_u_t(p.second.target_max_objects) << " objects";
2700 detail.push_back(ss.str());
2701 nearfull = true;
2702 }
2703 if (p.second.target_max_bytes &&
2704 (uint64_t)(st.stats.sum.num_bytes -
2705 st.stats.sum.num_bytes_hit_set_archive) >
2706 p.second.target_max_bytes * (ratio / 1000000.0)) {
2707 ostringstream ss;
2708 ss << "cache pool '" << name
2709 << "' with " << byte_u_t(st.stats.sum.num_bytes)
2710 << " at/near target max "
2711 << byte_u_t(p.second.target_max_bytes);
2712 detail.push_back(ss.str());
2713 nearfull = true;
2714 }
2715 if (nearfull) {
2716 ++num_pools;
2717 }
2718 }
2719 if (!detail.empty()) {
2720 ostringstream ss;
2721 ss << num_pools << " cache pools at or near target size";
2722 auto& d = checks->add("CACHE_POOL_NEAR_FULL", HEALTH_WARN, ss.str(),
2723 num_pools);
2724 d.detail.swap(detail);
2725 }
2726 }
2727
2728 // TOO_FEW_PGS
2729 unsigned num_in = osdmap.get_num_in_osds();
2730 auto sum_pg_up = std::max(static_cast<size_t>(pg_sum.up), pg_stat.size());
2731 const auto min_pg_per_osd =
2732 cct->_conf.get_val<uint64_t>("mon_pg_warn_min_per_osd");
2733 if (num_in && min_pg_per_osd > 0 && osdmap.get_pools().size() > 0) {
2734 auto per = sum_pg_up / num_in;
2735 if (per < min_pg_per_osd && per) {
2736 ostringstream ss;
2737 ss << "too few PGs per OSD (" << per
2738 << " < min " << min_pg_per_osd << ")";
2739 checks->add("TOO_FEW_PGS", HEALTH_WARN, ss.str(),
2740 min_pg_per_osd - per);
2741 }
2742 }
2743
2744 // TOO_MANY_PGS
2745 auto max_pg_per_osd = cct->_conf.get_val<uint64_t>("mon_max_pg_per_osd");
2746 if (num_in && max_pg_per_osd > 0) {
2747 auto per = sum_pg_up / num_in;
2748 if (per > max_pg_per_osd) {
2749 ostringstream ss;
2750 ss << "too many PGs per OSD (" << per
2751 << " > max " << max_pg_per_osd << ")";
2752 checks->add("TOO_MANY_PGS", HEALTH_WARN, ss.str(),
2753 per - max_pg_per_osd);
2754 }
2755 }
2756
2757 // TOO_FEW_OSDS
2758 auto warn_too_few_osds = cct->_conf.get_val<bool>("mon_warn_on_too_few_osds");
2759 auto osd_pool_default_size = cct->_conf.get_val<uint64_t>("osd_pool_default_size");
2760 if (warn_too_few_osds && osdmap.get_num_osds() < osd_pool_default_size) {
2761 ostringstream ss;
2762 ss << "OSD count " << osdmap.get_num_osds()
2763 << " < osd_pool_default_size " << osd_pool_default_size;
2764 checks->add("TOO_FEW_OSDS", HEALTH_WARN, ss.str(),
2765 osd_pool_default_size - osdmap.get_num_osds());
2766 }
2767
2768 // SLOW_PING_TIME
2769 // Convert milliseconds to microseconds
2770 auto warn_slow_ping_time = cct->_conf.get_val<double>("mon_warn_on_slow_ping_time") * 1000;
2771 auto grace = cct->_conf.get_val<int64_t>("osd_heartbeat_grace");
2772 if (warn_slow_ping_time == 0) {
2773 double ratio = cct->_conf.get_val<double>("mon_warn_on_slow_ping_ratio");
2774 warn_slow_ping_time = grace;
2775 warn_slow_ping_time *= 1000000 * ratio; // Seconds of grace to microseconds at ratio
2776 }
2777 if (warn_slow_ping_time > 0) {
2778
2779 struct mon_ping_item_t {
2780 uint32_t pingtime;
2781 int from;
2782 int to;
2783 bool improving;
2784
2785 bool operator<(const mon_ping_item_t& rhs) const {
2786 if (pingtime < rhs.pingtime)
2787 return true;
2788 if (pingtime > rhs.pingtime)
2789 return false;
2790 if (from < rhs.from)
2791 return true;
2792 if (from > rhs.from)
2793 return false;
2794 return to < rhs.to;
2795 }
2796 };
2797
2798 list<string> detail_back;
2799 list<string> detail_front;
2800 list<string> detail;
2801 set<mon_ping_item_t> back_sorted, front_sorted;
2802 for (auto i : osd_stat) {
2803 for (auto j : i.second.hb_pingtime) {
2804
2805 // Maybe source info is old
2806 if (now.sec() - j.second.last_update > grace * 60)
2807 continue;
2808
2809 mon_ping_item_t back;
2810 back.pingtime = std::max(j.second.back_pingtime[0], j.second.back_pingtime[1]);
2811 back.pingtime = std::max(back.pingtime, j.second.back_pingtime[2]);
2812 back.from = i.first;
2813 back.to = j.first;
2814 if (back.pingtime > warn_slow_ping_time) {
2815 back.improving = (j.second.back_pingtime[0] < j.second.back_pingtime[1]
2816 && j.second.back_pingtime[1] < j.second.back_pingtime[2]);
2817 back_sorted.emplace(back);
2818 }
2819
2820 mon_ping_item_t front;
2821 front.pingtime = std::max(j.second.front_pingtime[0], j.second.front_pingtime[1]);
2822 front.pingtime = std::max(front.pingtime, j.second.front_pingtime[2]);
2823 front.from = i.first;
2824 front.to = j.first;
2825 if (front.pingtime > warn_slow_ping_time) {
2826 front.improving = (j.second.front_pingtime[0] < j.second.front_pingtime[1]
2827 && j.second.front_pingtime[1] < j.second.back_pingtime[2]);
2828 front_sorted.emplace(front);
2829 }
2830 }
2831 if (i.second.num_shards_repaired >
2832 cct->_conf.get_val<uint64_t>("mon_osd_warn_num_repaired")) {
2833 ostringstream ss;
2834 ss << "osd." << i.first << " had " << i.second.num_shards_repaired << " reads repaired";
2835 detail.push_back(ss.str());
2836 }
2837 }
2838 if (!detail.empty()) {
2839 ostringstream ss;
2840 ss << "Too many repaired reads on " << detail.size() << " OSDs";
2841 auto& d = checks->add("OSD_TOO_MANY_REPAIRS", HEALTH_WARN, ss.str(),
2842 detail.size());
2843 d.detail.swap(detail);
2844 }
2845 int max_detail = 10;
2846 for (auto &sback : boost::adaptors::reverse(back_sorted)) {
2847 ostringstream ss;
2848 if (max_detail == 0) {
2849 ss << "Truncated long network list. Use ceph daemon mgr.# dump_osd_network for more information";
2850 detail_back.push_back(ss.str());
2851 break;
2852 }
2853 max_detail--;
2854 ss << "Slow OSD heartbeats on back from osd." << sback.from
2855 << " [" << osd_parentage(osdmap, sback.from) << "]"
2856 << (osdmap.is_down(sback.from) ? " (down)" : "")
2857 << " to osd." << sback.to
2858 << " [" << osd_parentage(osdmap, sback.to) << "]"
2859 << (osdmap.is_down(sback.to) ? " (down)" : "")
2860 << " " << fixed_u_to_string(sback.pingtime, 3) << " msec"
2861 << (sback.improving ? " possibly improving" : "");
2862 detail_back.push_back(ss.str());
2863 }
2864 max_detail = 10;
2865 for (auto &sfront : boost::adaptors::reverse(front_sorted)) {
2866 ostringstream ss;
2867 if (max_detail == 0) {
2868 ss << "Truncated long network list. Use ceph daemon mgr.# dump_osd_network for more information";
2869 detail_front.push_back(ss.str());
2870 break;
2871 }
2872 max_detail--;
2873 // Get crush parentage for each osd
2874 ss << "Slow OSD heartbeats on front from osd." << sfront.from
2875 << " [" << osd_parentage(osdmap, sfront.from) << "]"
2876 << (osdmap.is_down(sfront.from) ? " (down)" : "")
2877 << " to osd." << sfront.to
2878 << " [" << osd_parentage(osdmap, sfront.to) << "]"
2879 << (osdmap.is_down(sfront.to) ? " (down)" : "")
2880 << " " << fixed_u_to_string(sfront.pingtime, 3) << " msec"
2881 << (sfront.improving ? " possibly improving" : "");
2882 detail_front.push_back(ss.str());
2883 }
2884 if (detail_back.size() != 0) {
2885 ostringstream ss;
2886 ss << "Slow OSD heartbeats on back (longest "
2887 << fixed_u_to_string(back_sorted.rbegin()->pingtime, 3) << "ms)";
2888 auto& d = checks->add("OSD_SLOW_PING_TIME_BACK", HEALTH_WARN, ss.str(),
2889 back_sorted.size());
2890 d.detail.swap(detail_back);
2891 }
2892 if (detail_front.size() != 0) {
2893 ostringstream ss;
2894 ss << "Slow OSD heartbeats on front (longest "
2895 << fixed_u_to_string(front_sorted.rbegin()->pingtime, 3) << "ms)";
2896 auto& d = checks->add("OSD_SLOW_PING_TIME_FRONT", HEALTH_WARN, ss.str(),
2897 front_sorted.size());
2898 d.detail.swap(detail_front);
2899 }
2900 }
2901
2902 // SMALLER_PGP_NUM
2903 // MANY_OBJECTS_PER_PG
2904 if (!pg_stat.empty()) {
2905 list<string> pgp_detail, many_detail;
2906 const auto mon_pg_warn_min_objects =
2907 cct->_conf.get_val<int64_t>("mon_pg_warn_min_objects");
2908 const auto mon_pg_warn_min_pool_objects =
2909 cct->_conf.get_val<int64_t>("mon_pg_warn_min_pool_objects");
2910 const auto mon_pg_warn_max_object_skew =
2911 cct->_conf.get_val<double>("mon_pg_warn_max_object_skew");
2912 for (auto p = pg_pool_sum.begin();
2913 p != pg_pool_sum.end();
2914 ++p) {
2915 const pg_pool_t *pi = osdmap.get_pg_pool(p->first);
2916 if (!pi)
2917 continue; // in case osdmap changes haven't propagated to PGMap yet
2918 const string& name = osdmap.get_pool_name(p->first);
2919 // NOTE: we use pg_num_target and pgp_num_target for the purposes of
2920 // the warnings. If the cluster is failing to converge on the target
2921 // values that is a separate issue!
2922 if (pi->get_pg_num_target() > pi->get_pgp_num_target() &&
2923 !(name.find(".DELETED") != string::npos &&
2924 cct->_conf->mon_fake_pool_delete)) {
2925 ostringstream ss;
2926 ss << "pool " << name << " pg_num "
2927 << pi->get_pg_num_target()
2928 << " > pgp_num " << pi->get_pgp_num_target();
2929 pgp_detail.push_back(ss.str());
2930 }
2931 int average_objects_per_pg = pg_sum.stats.sum.num_objects / pg_stat.size();
2932 if (average_objects_per_pg > 0 &&
2933 pg_sum.stats.sum.num_objects >= mon_pg_warn_min_objects &&
2934 p->second.stats.sum.num_objects >= mon_pg_warn_min_pool_objects) {
2935 int objects_per_pg = p->second.stats.sum.num_objects /
2936 pi->get_pg_num_target();
2937 float ratio = (float)objects_per_pg / (float)average_objects_per_pg;
2938 if (mon_pg_warn_max_object_skew > 0 &&
2939 ratio > mon_pg_warn_max_object_skew) {
2940 ostringstream ss;
2941 if (pi->pg_autoscale_mode != pg_pool_t::pg_autoscale_mode_t::ON) {
2942 ss << "pool " << name << " objects per pg ("
2943 << objects_per_pg << ") is more than " << ratio
2944 << " times cluster average ("
2945 << average_objects_per_pg << ")";
2946 many_detail.push_back(ss.str());
2947 }
2948 }
2949 }
2950 }
2951 if (!pgp_detail.empty()) {
2952 ostringstream ss;
2953 ss << pgp_detail.size() << " pools have pg_num > pgp_num";
2954 auto& d = checks->add("SMALLER_PGP_NUM", HEALTH_WARN, ss.str(),
2955 pgp_detail.size());
2956 d.detail.swap(pgp_detail);
2957 }
2958 if (!many_detail.empty()) {
2959 ostringstream ss;
2960 ss << many_detail.size() << " pools have many more objects per pg than"
2961 << " average";
2962 auto& d = checks->add("MANY_OBJECTS_PER_PG", HEALTH_WARN, ss.str(),
2963 many_detail.size());
2964 d.detail.swap(many_detail);
2965 }
2966 }
2967
2968 // POOL_FULL
2969 // POOL_NEAR_FULL
2970 {
2971 float warn_threshold = (float)g_conf().get_val<int64_t>("mon_pool_quota_warn_threshold")/100;
2972 float crit_threshold = (float)g_conf().get_val<int64_t>("mon_pool_quota_crit_threshold")/100;
2973 list<string> full_detail, nearfull_detail;
2974 unsigned full_pools = 0, nearfull_pools = 0;
2975 for (auto it : pools) {
2976 auto it2 = pg_pool_sum.find(it.first);
2977 if (it2 == pg_pool_sum.end()) {
2978 continue;
2979 }
2980 const pool_stat_t *pstat = &it2->second;
2981 const object_stat_sum_t& sum = pstat->stats.sum;
2982 const string& pool_name = osdmap.get_pool_name(it.first);
2983 const pg_pool_t &pool = it.second;
2984 bool full = false, nearfull = false;
2985 if (pool.quota_max_objects > 0) {
2986 stringstream ss;
2987 if ((uint64_t)sum.num_objects >= pool.quota_max_objects) {
2988 } else if (crit_threshold > 0 &&
2989 sum.num_objects >= pool.quota_max_objects*crit_threshold) {
2990 ss << "pool '" << pool_name
2991 << "' has " << sum.num_objects << " objects"
2992 << " (max " << pool.quota_max_objects << ")";
2993 full_detail.push_back(ss.str());
2994 full = true;
2995 } else if (warn_threshold > 0 &&
2996 sum.num_objects >= pool.quota_max_objects*warn_threshold) {
2997 ss << "pool '" << pool_name
2998 << "' has " << sum.num_objects << " objects"
2999 << " (max " << pool.quota_max_objects << ")";
3000 nearfull_detail.push_back(ss.str());
3001 nearfull = true;
3002 }
3003 }
3004 if (pool.quota_max_bytes > 0) {
3005 stringstream ss;
3006 if ((uint64_t)sum.num_bytes >= pool.quota_max_bytes) {
3007 } else if (crit_threshold > 0 &&
3008 sum.num_bytes >= pool.quota_max_bytes*crit_threshold) {
3009 ss << "pool '" << pool_name
3010 << "' has " << byte_u_t(sum.num_bytes)
3011 << " (max " << byte_u_t(pool.quota_max_bytes) << ")";
3012 full_detail.push_back(ss.str());
3013 full = true;
3014 } else if (warn_threshold > 0 &&
3015 sum.num_bytes >= pool.quota_max_bytes*warn_threshold) {
3016 ss << "pool '" << pool_name
3017 << "' has " << byte_u_t(sum.num_bytes)
3018 << " (max " << byte_u_t(pool.quota_max_bytes) << ")";
3019 nearfull_detail.push_back(ss.str());
3020 nearfull = true;
3021 }
3022 }
3023 if (full) {
3024 ++full_pools;
3025 }
3026 if (nearfull) {
3027 ++nearfull_pools;
3028 }
3029 }
3030 if (full_pools) {
3031 ostringstream ss;
3032 ss << full_pools << " pools full";
3033 auto& d = checks->add("POOL_FULL", HEALTH_ERR, ss.str(), full_pools);
3034 d.detail.swap(full_detail);
3035 }
3036 if (nearfull_pools) {
3037 ostringstream ss;
3038 ss << nearfull_pools << " pools nearfull";
3039 auto& d = checks->add("POOL_NEAR_FULL", HEALTH_WARN, ss.str(), nearfull_pools);
3040 d.detail.swap(nearfull_detail);
3041 }
3042 }
3043
3044 // OBJECT_MISPLACED
3045 if (pg_sum.stats.sum.num_objects_misplaced &&
3046 pg_sum.stats.sum.num_object_copies > 0 &&
3047 cct->_conf->mon_warn_on_misplaced) {
3048 double pc = (double)pg_sum.stats.sum.num_objects_misplaced /
3049 (double)pg_sum.stats.sum.num_object_copies * (double)100.0;
3050 char b[20];
3051 snprintf(b, sizeof(b), "%.3lf", pc);
3052 ostringstream ss;
3053 ss << pg_sum.stats.sum.num_objects_misplaced
3054 << "/" << pg_sum.stats.sum.num_object_copies << " objects misplaced ("
3055 << b << "%)";
3056 checks->add("OBJECT_MISPLACED", HEALTH_WARN, ss.str(),
3057 pg_sum.stats.sum.num_objects_misplaced);
3058 }
3059
3060 // OBJECT_UNFOUND
3061 if (pg_sum.stats.sum.num_objects_unfound &&
3062 pg_sum.stats.sum.num_objects) {
3063 double pc = (double)pg_sum.stats.sum.num_objects_unfound /
3064 (double)pg_sum.stats.sum.num_objects * (double)100.0;
3065 char b[20];
3066 snprintf(b, sizeof(b), "%.3lf", pc);
3067 ostringstream ss;
3068 ss << pg_sum.stats.sum.num_objects_unfound
3069 << "/" << pg_sum.stats.sum.num_objects << " objects unfound (" << b << "%)";
3070 auto& d = checks->add("OBJECT_UNFOUND", HEALTH_WARN, ss.str(),
3071 pg_sum.stats.sum.num_objects_unfound);
3072
3073 for (auto& p : pg_stat) {
3074 if (p.second.stats.sum.num_objects_unfound) {
3075 ostringstream ss;
3076 ss << "pg " << p.first
3077 << " has " << p.second.stats.sum.num_objects_unfound
3078 << " unfound objects";
3079 d.detail.push_back(ss.str());
3080 if (d.detail.size() > max) {
3081 d.detail.push_back("(additional pgs left out for brevity)");
3082 break;
3083 }
3084 }
3085 }
3086 }
3087
3088 // REQUEST_SLOW
3089 // REQUEST_STUCK
3090 // SLOW_OPS unifies them in mimic.
3091 if (osdmap.require_osd_release < ceph_release_t::mimic &&
3092 cct->_conf->mon_osd_warn_op_age > 0 &&
3093 !osd_sum.op_queue_age_hist.h.empty() &&
3094 osd_sum.op_queue_age_hist.upper_bound() / 1000.0 >
3095 cct->_conf->mon_osd_warn_op_age) {
3096 list<string> warn_detail, error_detail;
3097 unsigned warn = 0, error = 0;
3098 float err_age =
3099 cct->_conf->mon_osd_warn_op_age * cct->_conf->mon_osd_err_op_age_ratio;
3100 const pow2_hist_t& h = osd_sum.op_queue_age_hist;
3101 for (unsigned i = h.h.size() - 1; i > 0; --i) {
3102 float ub = (float)(1 << i) / 1000.0;
3103 if (ub < cct->_conf->mon_osd_warn_op_age)
3104 break;
3105 if (h.h[i]) {
3106 ostringstream ss;
3107 ss << h.h[i] << " ops are blocked > " << ub << " sec";
3108 if (ub > err_age) {
3109 error += h.h[i];
3110 error_detail.push_back(ss.str());
3111 } else {
3112 warn += h.h[i];
3113 warn_detail.push_back(ss.str());
3114 }
3115 }
3116 }
3117
3118 map<float,set<int>> warn_osd_by_max; // max -> osds
3119 map<float,set<int>> error_osd_by_max; // max -> osds
3120 if (!warn_detail.empty() || !error_detail.empty()) {
3121 for (auto& p : osd_stat) {
3122 const pow2_hist_t& h = p.second.op_queue_age_hist;
3123 for (unsigned i = h.h.size() - 1; i > 0; --i) {
3124 float ub = (float)(1 << i) / 1000.0;
3125 if (ub < cct->_conf->mon_osd_warn_op_age)
3126 break;
3127 if (h.h[i]) {
3128 if (ub > err_age) {
3129 error_osd_by_max[ub].insert(p.first);
3130 } else {
3131 warn_osd_by_max[ub].insert(p.first);
3132 }
3133 break;
3134 }
3135 }
3136 }
3137 }
3138
3139 if (!warn_detail.empty()) {
3140 ostringstream ss;
3141 ss << warn << " slow requests are blocked > "
3142 << cct->_conf->mon_osd_warn_op_age << " sec";
3143 auto& d = checks->add("REQUEST_SLOW", HEALTH_WARN, ss.str(), warn);
3144 d.detail.swap(warn_detail);
3145 int left = max;
3146 for (auto& p : warn_osd_by_max) {
3147 ostringstream ss;
3148 if (p.second.size() > 1) {
3149 ss << "osds " << p.second
3150 << " have blocked requests > " << p.first << " sec";
3151 } else {
3152 ss << "osd." << *p.second.begin()
3153 << " has blocked requests > " << p.first << " sec";
3154 }
3155 d.detail.push_back(ss.str());
3156 if (--left == 0) {
3157 break;
3158 }
3159 }
3160 }
3161 if (!error_detail.empty()) {
3162 ostringstream ss;
3163 ss << error << " stuck requests are blocked > "
3164 << err_age << " sec";
3165 auto& d = checks->add("REQUEST_STUCK", HEALTH_ERR, ss.str(), error);
3166 d.detail.swap(error_detail);
3167 int left = max;
3168 for (auto& p : error_osd_by_max) {
3169 ostringstream ss;
3170 if (p.second.size() > 1) {
3171 ss << "osds " << p.second
3172 << " have stuck requests > " << p.first << " sec";
3173 } else {
3174 ss << "osd." << *p.second.begin()
3175 << " has stuck requests > " << p.first << " sec";
3176 }
3177 d.detail.push_back(ss.str());
3178 if (--left == 0) {
3179 break;
3180 }
3181 }
3182 }
3183 }
3184
3185 // OBJECT_STORE_WARN
3186 if (osd_sum.os_alerts.size()) {
3187 map<string, pair<size_t, list<string>>> os_alerts_sum;
3188
3189 for (auto& a : osd_sum.os_alerts) {
3190 int left = max;
3191 string s0 = " osd.";
3192 s0 += stringify(a.first);
3193 for (auto& aa : a.second) {
3194 string s(s0);
3195 s += " ";
3196 s += aa.second;
3197 auto it = os_alerts_sum.find(aa.first);
3198 if (it == os_alerts_sum.end()) {
3199 list<string> d;
3200 d.emplace_back(s);
3201 os_alerts_sum.emplace(aa.first, std::make_pair(1, d));
3202 } else {
3203 auto& p = it->second;
3204 ++p.first;
3205 p.second.emplace_back(s);
3206 }
3207 if (--left == 0) {
3208 break;
3209 }
3210 }
3211 }
3212
3213 for (auto& asum : os_alerts_sum) {
3214 string summary = stringify(asum.second.first) + " OSD(s)";
3215 if (asum.first == "BLUEFS_SPILLOVER") {
3216 summary += " experiencing BlueFS spillover";
3217 } else if (asum.first == "BLUESTORE_NO_COMPRESSION") {
3218 summary += " have broken BlueStore compression";
3219 } else if (asum.first == "BLUESTORE_LEGACY_STATFS") {
3220 summary += " reporting legacy (not per-pool) BlueStore stats";
3221 } else if (asum.first == "BLUESTORE_DISK_SIZE_MISMATCH") {
3222 summary += " have dangerous mismatch between BlueStore block device and free list sizes";
3223 } else if (asum.first == "BLUESTORE_NO_PER_PG_OMAP") {
3224 summary += " reporting legacy (not per-pg) BlueStore omap";
3225 } else if (asum.first == "BLUESTORE_NO_PER_POOL_OMAP") {
3226 summary += " reporting legacy (not per-pool) BlueStore omap usage stats";
3227 } else if (asum.first == "BLUESTORE_SPURIOUS_READ_ERRORS") {
3228 summary += " have spurious read errors";
3229 }
3230
3231 auto& d = checks->add(asum.first, HEALTH_WARN, summary, asum.second.first);
3232 for (auto& s : asum.second.second) {
3233 d.detail.push_back(s);
3234 }
3235 }
3236 }
3237 // PG_NOT_SCRUBBED
3238 // PG_NOT_DEEP_SCRUBBED
3239 if (cct->_conf->mon_warn_pg_not_scrubbed_ratio ||
3240 cct->_conf->mon_warn_pg_not_deep_scrubbed_ratio) {
3241 list<string> detail, deep_detail;
3242 int detail_max = max, deep_detail_max = max;
3243 int detail_more = 0, deep_detail_more = 0;
3244 int detail_total = 0, deep_detail_total = 0;
3245 for (auto& p : pg_stat) {
3246 int64_t pnum = p.first.pool();
3247 auto pool = osdmap.get_pg_pool(pnum);
3248 if (!pool)
3249 continue;
3250 if (cct->_conf->mon_warn_pg_not_scrubbed_ratio) {
3251 double scrub_max_interval = 0;
3252 pool->opts.get(pool_opts_t::SCRUB_MAX_INTERVAL, &scrub_max_interval);
3253 if (scrub_max_interval <= 0) {
3254 scrub_max_interval = cct->_conf->osd_scrub_max_interval;
3255 }
3256 const double age = (cct->_conf->mon_warn_pg_not_scrubbed_ratio * scrub_max_interval) +
3257 scrub_max_interval;
3258 utime_t cutoff = now;
3259 cutoff -= age;
3260 if (p.second.last_scrub_stamp < cutoff) {
3261 if (detail_max > 0) {
3262 ostringstream ss;
3263 ss << "pg " << p.first << " not scrubbed since "
3264 << p.second.last_scrub_stamp;
3265 detail.push_back(ss.str());
3266 --detail_max;
3267 } else {
3268 ++detail_more;
3269 }
3270 ++detail_total;
3271 }
3272 }
3273 if (cct->_conf->mon_warn_pg_not_deep_scrubbed_ratio) {
3274 double deep_scrub_interval = 0;
3275 pool->opts.get(pool_opts_t::DEEP_SCRUB_INTERVAL, &deep_scrub_interval);
3276 if (deep_scrub_interval <= 0) {
3277 deep_scrub_interval = cct->_conf->osd_deep_scrub_interval;
3278 }
3279 double deep_age = (cct->_conf->mon_warn_pg_not_deep_scrubbed_ratio * deep_scrub_interval) +
3280 deep_scrub_interval;
3281 utime_t deep_cutoff = now;
3282 deep_cutoff -= deep_age;
3283 if (p.second.last_deep_scrub_stamp < deep_cutoff) {
3284 if (deep_detail_max > 0) {
3285 ostringstream ss;
3286 ss << "pg " << p.first << " not deep-scrubbed since "
3287 << p.second.last_deep_scrub_stamp;
3288 deep_detail.push_back(ss.str());
3289 --deep_detail_max;
3290 } else {
3291 ++deep_detail_more;
3292 }
3293 ++deep_detail_total;
3294 }
3295 }
3296 }
3297 if (detail_total) {
3298 ostringstream ss;
3299 ss << detail_total << " pgs not scrubbed in time";
3300 auto& d = checks->add("PG_NOT_SCRUBBED", HEALTH_WARN, ss.str(), detail_total);
3301
3302 if (!detail.empty()) {
3303 d.detail.swap(detail);
3304
3305 if (detail_more) {
3306 ostringstream ss;
3307 ss << detail_more << " more pgs... ";
3308 d.detail.push_back(ss.str());
3309 }
3310 }
3311 }
3312 if (deep_detail_total) {
3313 ostringstream ss;
3314 ss << deep_detail_total << " pgs not deep-scrubbed in time";
3315 auto& d = checks->add("PG_NOT_DEEP_SCRUBBED", HEALTH_WARN, ss.str(),
3316 deep_detail_total);
3317
3318 if (!deep_detail.empty()) {
3319 d.detail.swap(deep_detail);
3320
3321 if (deep_detail_more) {
3322 ostringstream ss;
3323 ss << deep_detail_more << " more pgs... ";
3324 d.detail.push_back(ss.str());
3325 }
3326 }
3327 }
3328 }
3329
3330 // POOL_APP
3331 if (g_conf().get_val<bool>("mon_warn_on_pool_no_app")) {
3332 list<string> detail;
3333 for (auto &it : pools) {
3334 const pg_pool_t &pool = it.second;
3335 const string& pool_name = osdmap.get_pool_name(it.first);
3336 auto it2 = pg_pool_sum.find(it.first);
3337 if (it2 == pg_pool_sum.end()) {
3338 continue;
3339 }
3340 const pool_stat_t *pstat = &it2->second;
3341 if (pstat == nullptr) {
3342 continue;
3343 }
3344 const object_stat_sum_t& sum = pstat->stats.sum;
3345 // application metadata is not encoded until luminous is minimum
3346 // required release
3347 if (sum.num_objects > 0 && pool.application_metadata.empty() &&
3348 !pool.is_tier()) {
3349 stringstream ss;
3350 ss << "application not enabled on pool '" << pool_name << "'";
3351 detail.push_back(ss.str());
3352 }
3353 }
3354 if (!detail.empty()) {
3355 ostringstream ss;
3356 ss << detail.size() << " pool(s) do not have an application enabled";
3357 auto& d = checks->add("POOL_APP_NOT_ENABLED", HEALTH_WARN, ss.str(),
3358 detail.size());
3359 stringstream tip;
3360 tip << "use 'ceph osd pool application enable <pool-name> "
3361 << "<app-name>', where <app-name> is 'cephfs', 'rbd', 'rgw', "
3362 << "or freeform for custom applications.";
3363 detail.push_back(tip.str());
3364 d.detail.swap(detail);
3365 }
3366 }
3367
3368 // PG_SLOW_SNAP_TRIMMING
3369 if (!pg_stat.empty() && cct->_conf->mon_osd_snap_trim_queue_warn_on > 0) {
3370 uint32_t snapthreshold = cct->_conf->mon_osd_snap_trim_queue_warn_on;
3371 uint64_t snaptrimq_exceeded = 0;
3372 uint32_t longest_queue = 0;
3373 const pg_t* longest_q_pg = nullptr;
3374 list<string> detail;
3375
3376 for (auto& i: pg_stat) {
3377 uint32_t current_len = i.second.snaptrimq_len;
3378 if (current_len >= snapthreshold) {
3379 snaptrimq_exceeded++;
3380 if (longest_queue <= current_len) {
3381 longest_q_pg = &i.first;
3382 longest_queue = current_len;
3383 }
3384 if (detail.size() < max - 1) {
3385 stringstream ss;
3386 ss << "snap trim queue for pg " << i.first << " at " << current_len;
3387 detail.push_back(ss.str());
3388 continue;
3389 }
3390 if (detail.size() < max) {
3391 detail.push_back("...more pgs affected");
3392 continue;
3393 }
3394 }
3395 }
3396
3397 if (snaptrimq_exceeded) {
3398 {
3399 ostringstream ss;
3400 ss << "longest queue on pg " << *longest_q_pg << " at " << longest_queue;
3401 detail.push_back(ss.str());
3402 }
3403
3404 stringstream ss;
3405 ss << "snap trim queue for " << snaptrimq_exceeded << " pg(s) >= " << snapthreshold << " (mon_osd_snap_trim_queue_warn_on)";
3406 auto& d = checks->add("PG_SLOW_SNAP_TRIMMING", HEALTH_WARN, ss.str(),
3407 snaptrimq_exceeded);
3408 detail.push_back("try decreasing \"osd snap trim sleep\" and/or increasing \"osd pg max concurrent snap trims\".");
3409 d.detail.swap(detail);
3410 }
3411 }
3412 }
3413
3414 void PGMap::print_summary(ceph::Formatter *f, ostream *out) const
3415 {
3416 if (f) {
3417 f->open_array_section("pgs_by_pool_state");
3418 for (auto& i: num_pg_by_pool_state) {
3419 f->open_object_section("per_pool_pgs_by_state");
3420 f->dump_int("pool_id", i.first);
3421 f->open_array_section("pg_state_counts");
3422 for (auto& j : i.second) {
3423 f->open_object_section("pg_state_count");
3424 f->dump_string("state_name", pg_state_string(j.first));
3425 f->dump_int("count", j.second);
3426 f->close_section();
3427 }
3428 f->close_section();
3429 f->close_section();
3430 }
3431 f->close_section();
3432 }
3433 PGMapDigest::print_summary(f, out);
3434 }
3435
3436 int process_pg_map_command(
3437 const string& orig_prefix,
3438 const cmdmap_t& orig_cmdmap,
3439 const PGMap& pg_map,
3440 const OSDMap& osdmap,
3441 ceph::Formatter *f,
3442 stringstream *ss,
3443 bufferlist *odata)
3444 {
3445 string prefix = orig_prefix;
3446 auto cmdmap = orig_cmdmap;
3447
3448 string omap_stats_note =
3449 "\n* NOTE: Omap statistics are gathered during deep scrub and "
3450 "may be inaccurate soon afterwards depending on utilization. See "
3451 "http://docs.ceph.com/en/latest/dev/placement-group/#omap-statistics "
3452 "for further details.\n";
3453 bool omap_stats_note_required = false;
3454
3455 // perhaps these would be better in the parsing, but it's weird
3456 bool primary = false;
3457 if (prefix == "pg dump_json") {
3458 vector<string> v;
3459 v.push_back(string("all"));
3460 cmd_putval(g_ceph_context, cmdmap, "dumpcontents", v);
3461 prefix = "pg dump";
3462 } else if (prefix == "pg dump_pools_json") {
3463 vector<string> v;
3464 v.push_back(string("pools"));
3465 cmd_putval(g_ceph_context, cmdmap, "dumpcontents", v);
3466 prefix = "pg dump";
3467 } else if (prefix == "pg ls-by-primary") {
3468 primary = true;
3469 prefix = "pg ls";
3470 } else if (prefix == "pg ls-by-osd") {
3471 prefix = "pg ls";
3472 } else if (prefix == "pg ls-by-pool") {
3473 prefix = "pg ls";
3474 string poolstr;
3475 cmd_getval(cmdmap, "poolstr", poolstr);
3476 int64_t pool = osdmap.lookup_pg_pool_name(poolstr.c_str());
3477 if (pool < 0) {
3478 *ss << "pool " << poolstr << " does not exist";
3479 return -ENOENT;
3480 }
3481 cmd_putval(g_ceph_context, cmdmap, "pool", pool);
3482 }
3483
3484 stringstream ds;
3485 if (prefix == "pg stat") {
3486 if (f) {
3487 f->open_object_section("pg_summary");
3488 pg_map.print_oneline_summary(f, NULL);
3489 f->close_section();
3490 f->flush(ds);
3491 } else {
3492 ds << pg_map;
3493 }
3494 odata->append(ds);
3495 return 0;
3496 }
3497
3498 if (prefix == "pg getmap") {
3499 pg_map.encode(*odata);
3500 *ss << "got pgmap version " << pg_map.version;
3501 return 0;
3502 }
3503
3504 if (prefix == "pg dump") {
3505 string val;
3506 vector<string> dumpcontents;
3507 set<string> what;
3508 if (cmd_getval(cmdmap, "dumpcontents", dumpcontents)) {
3509 copy(dumpcontents.begin(), dumpcontents.end(),
3510 inserter(what, what.end()));
3511 }
3512 if (what.empty())
3513 what.insert("all");
3514 if (f) {
3515 if (what.count("all")) {
3516 f->open_object_section("pg_map");
3517 pg_map.dump(f);
3518 f->close_section();
3519 } else if (what.count("summary") || what.count("sum")) {
3520 f->open_object_section("pg_map");
3521 pg_map.dump_basic(f);
3522 f->close_section();
3523 } else {
3524 if (what.count("pools")) {
3525 pg_map.dump_pool_stats(f);
3526 }
3527 if (what.count("osds")) {
3528 pg_map.dump_osd_stats(f);
3529 }
3530 if (what.count("pgs")) {
3531 pg_map.dump_pg_stats(f, false);
3532 }
3533 if (what.count("pgs_brief")) {
3534 pg_map.dump_pg_stats(f, true);
3535 }
3536 if (what.count("delta")) {
3537 f->open_object_section("delta");
3538 pg_map.dump_delta(f);
3539 f->close_section();
3540 }
3541 }
3542 f->flush(*odata);
3543 } else {
3544 if (what.count("all")) {
3545 pg_map.dump(ds);
3546 omap_stats_note_required = true;
3547 } else if (what.count("summary") || what.count("sum")) {
3548 pg_map.dump_basic(ds);
3549 pg_map.dump_pg_sum_stats(ds, true);
3550 pg_map.dump_osd_sum_stats(ds);
3551 omap_stats_note_required = true;
3552 } else {
3553 if (what.count("pgs_brief")) {
3554 pg_map.dump_pg_stats(ds, true);
3555 }
3556 bool header = true;
3557 if (what.count("pgs")) {
3558 pg_map.dump_pg_stats(ds, false);
3559 header = false;
3560 omap_stats_note_required = true;
3561 }
3562 if (what.count("pools")) {
3563 pg_map.dump_pool_stats(ds, header);
3564 omap_stats_note_required = true;
3565 }
3566 if (what.count("osds")) {
3567 pg_map.dump_osd_stats(ds);
3568 }
3569 }
3570 odata->append(ds);
3571 if (omap_stats_note_required) {
3572 odata->append(omap_stats_note);
3573 }
3574 }
3575 *ss << "dumped " << what;
3576 return 0;
3577 }
3578
3579 if (prefix == "pg ls") {
3580 int64_t osd = -1;
3581 int64_t pool = -1;
3582 vector<string>states;
3583 set<pg_t> pgs;
3584 cmd_getval(cmdmap, "pool", pool);
3585 cmd_getval(cmdmap, "osd", osd);
3586 cmd_getval(cmdmap, "states", states);
3587 if (pool >= 0 && !osdmap.have_pg_pool(pool)) {
3588 *ss << "pool " << pool << " does not exist";
3589 return -ENOENT;
3590 }
3591 if (osd >= 0 && !osdmap.is_up(osd)) {
3592 *ss << "osd " << osd << " is not up";
3593 return -EAGAIN;
3594 }
3595 if (states.empty())
3596 states.push_back("all");
3597
3598 uint64_t state = 0;
3599
3600 while (!states.empty()) {
3601 string state_str = states.back();
3602
3603 if (state_str == "all") {
3604 state = -1;
3605 break;
3606 } else {
3607 auto filter = pg_string_state(state_str);
3608 if (!filter) {
3609 *ss << "'" << state_str << "' is not a valid pg state,"
3610 << " available choices: " << pg_state_string(0xFFFFFFFF);
3611 return -EINVAL;
3612 }
3613 state |= *filter;
3614 }
3615
3616 states.pop_back();
3617 }
3618
3619 pg_map.get_filtered_pg_stats(state, pool, osd, primary, pgs);
3620
3621 if (f && !pgs.empty()) {
3622 pg_map.dump_filtered_pg_stats(f, pgs);
3623 f->flush(*odata);
3624 } else if (!pgs.empty()) {
3625 pg_map.dump_filtered_pg_stats(ds, pgs);
3626 odata->append(ds);
3627 odata->append(omap_stats_note);
3628 }
3629 return 0;
3630 }
3631
3632 if (prefix == "pg dump_stuck") {
3633 vector<string> stuckop_vec;
3634 cmd_getval(cmdmap, "stuckops", stuckop_vec);
3635 if (stuckop_vec.empty())
3636 stuckop_vec.push_back("unclean");
3637 const int64_t threshold = cmd_getval_or<int64_t>(
3638 cmdmap, "threshold",
3639 g_conf().get_val<int64_t>("mon_pg_stuck_threshold"));
3640
3641 if (pg_map.dump_stuck_pg_stats(ds, f, (int)threshold, stuckop_vec) < 0) {
3642 *ss << "failed";
3643 } else {
3644 *ss << "ok";
3645 }
3646 odata->append(ds);
3647 return 0;
3648 }
3649
3650 if (prefix == "pg debug") {
3651 const string debugop = cmd_getval_or<string>(
3652 cmdmap, "debugop",
3653 "unfound_objects_exist");
3654 if (debugop == "unfound_objects_exist") {
3655 bool unfound_objects_exist = false;
3656 for (const auto& p : pg_map.pg_stat) {
3657 if (p.second.stats.sum.num_objects_unfound > 0) {
3658 unfound_objects_exist = true;
3659 break;
3660 }
3661 }
3662 if (unfound_objects_exist)
3663 ds << "TRUE";
3664 else
3665 ds << "FALSE";
3666 odata->append(ds);
3667 return 0;
3668 }
3669 if (debugop == "degraded_pgs_exist") {
3670 bool degraded_pgs_exist = false;
3671 for (const auto& p : pg_map.pg_stat) {
3672 if (p.second.stats.sum.num_objects_degraded > 0) {
3673 degraded_pgs_exist = true;
3674 break;
3675 }
3676 }
3677 if (degraded_pgs_exist)
3678 ds << "TRUE";
3679 else
3680 ds << "FALSE";
3681 odata->append(ds);
3682 return 0;
3683 }
3684 }
3685
3686 if (prefix == "osd perf") {
3687 if (f) {
3688 f->open_object_section("osdstats");
3689 pg_map.dump_osd_perf_stats(f);
3690 f->close_section();
3691 f->flush(ds);
3692 } else {
3693 pg_map.print_osd_perf_stats(&ds);
3694 }
3695 odata->append(ds);
3696 return 0;
3697 }
3698
3699 if (prefix == "osd blocked-by") {
3700 if (f) {
3701 f->open_object_section("osd_blocked_by");
3702 pg_map.dump_osd_blocked_by_stats(f);
3703 f->close_section();
3704 f->flush(ds);
3705 } else {
3706 pg_map.print_osd_blocked_by_stats(&ds);
3707 }
3708 odata->append(ds);
3709 return 0;
3710 }
3711
3712 return -EOPNOTSUPP;
3713 }
3714
3715 void PGMapUpdater::check_osd_map(
3716 CephContext *cct,
3717 const OSDMap& osdmap,
3718 const PGMap& pgmap,
3719 PGMap::Incremental *pending_inc)
3720 {
3721 for (auto& p : pgmap.osd_stat) {
3722 if (!osdmap.exists(p.first)) {
3723 // remove osd_stat
3724 pending_inc->rm_stat(p.first);
3725 } else if (osdmap.is_out(p.first)) {
3726 // zero osd_stat
3727 if (p.second.statfs.total != 0) {
3728 pending_inc->stat_osd_out(p.first);
3729 }
3730 } else if (!osdmap.is_up(p.first)) {
3731 // zero the op_queue_age_hist
3732 if (!p.second.op_queue_age_hist.empty()) {
3733 pending_inc->stat_osd_down_up(p.first, pgmap);
3734 }
3735 }
3736 }
3737
3738 // deleted pgs (pools)?
3739 for (auto& p : pgmap.pg_pool_sum) {
3740 if (!osdmap.have_pg_pool(p.first)) {
3741 ldout(cct, 10) << __func__ << " pool " << p.first << " gone, removing pgs"
3742 << dendl;
3743 for (auto& q : pgmap.pg_stat) {
3744 if (q.first.pool() == p.first) {
3745 pending_inc->pg_remove.insert(q.first);
3746 }
3747 }
3748 auto q = pending_inc->pg_stat_updates.begin();
3749 while (q != pending_inc->pg_stat_updates.end()) {
3750 if (q->first.pool() == p.first) {
3751 q = pending_inc->pg_stat_updates.erase(q);
3752 } else {
3753 ++q;
3754 }
3755 }
3756 }
3757 }
3758
3759 // new (split or new pool) or merged pgs?
3760 map<int64_t,unsigned> new_pg_num;
3761 for (auto& p : osdmap.get_pools()) {
3762 int64_t poolid = p.first;
3763 const pg_pool_t& pi = p.second;
3764 auto q = pgmap.num_pg_by_pool.find(poolid);
3765 unsigned my_pg_num = 0;
3766 if (q != pgmap.num_pg_by_pool.end())
3767 my_pg_num = q->second;
3768 unsigned pg_num = pi.get_pg_num();
3769 new_pg_num[poolid] = pg_num;
3770 if (my_pg_num < pg_num) {
3771 ldout(cct,10) << __func__ << " pool " << poolid << " pg_num " << pg_num
3772 << " > my pg_num " << my_pg_num << dendl;
3773 for (unsigned ps = my_pg_num; ps < pg_num; ++ps) {
3774 pg_t pgid(ps, poolid);
3775 if (pending_inc->pg_stat_updates.count(pgid) == 0) {
3776 ldout(cct,20) << __func__ << " adding " << pgid << dendl;
3777 pg_stat_t &stats = pending_inc->pg_stat_updates[pgid];
3778 stats.last_fresh = osdmap.get_modified();
3779 stats.last_active = osdmap.get_modified();
3780 stats.last_change = osdmap.get_modified();
3781 stats.last_peered = osdmap.get_modified();
3782 stats.last_clean = osdmap.get_modified();
3783 stats.last_unstale = osdmap.get_modified();
3784 stats.last_undegraded = osdmap.get_modified();
3785 stats.last_fullsized = osdmap.get_modified();
3786 stats.last_scrub_stamp = osdmap.get_modified();
3787 stats.last_deep_scrub_stamp = osdmap.get_modified();
3788 stats.last_clean_scrub_stamp = osdmap.get_modified();
3789 }
3790 }
3791 } else if (my_pg_num > pg_num) {
3792 ldout(cct,10) << __func__ << " pool " << poolid << " pg_num " << pg_num
3793 << " < my pg_num " << my_pg_num << dendl;
3794 for (unsigned i = pg_num; i < my_pg_num; ++i) {
3795 pg_t pgid(i, poolid);
3796 ldout(cct,20) << __func__ << " removing merged " << pgid << dendl;
3797 if (pgmap.pg_stat.count(pgid)) {
3798 pending_inc->pg_remove.insert(pgid);
3799 }
3800 pending_inc->pg_stat_updates.erase(pgid);
3801 }
3802 }
3803 }
3804 auto i = pending_inc->pg_stat_updates.begin();
3805 while (i != pending_inc->pg_stat_updates.end()) {
3806 auto j = new_pg_num.find(i->first.pool());
3807 if (j == new_pg_num.end() ||
3808 i->first.ps() >= j->second) {
3809 ldout(cct,20) << __func__ << " removing pending update to old "
3810 << i->first << dendl;
3811 i = pending_inc->pg_stat_updates.erase(i);
3812 } else {
3813 ++i;
3814 }
3815 }
3816 }
3817
3818 static void _try_mark_pg_stale(
3819 const OSDMap& osdmap,
3820 pg_t pgid,
3821 const pg_stat_t& cur,
3822 PGMap::Incremental *pending_inc)
3823 {
3824 if ((cur.state & PG_STATE_STALE) == 0 &&
3825 cur.acting_primary != -1 &&
3826 osdmap.is_down(cur.acting_primary)) {
3827 pg_stat_t *newstat;
3828 auto q = pending_inc->pg_stat_updates.find(pgid);
3829 if (q != pending_inc->pg_stat_updates.end()) {
3830 if ((q->second.acting_primary == cur.acting_primary) ||
3831 ((q->second.state & PG_STATE_STALE) == 0 &&
3832 q->second.acting_primary != -1 &&
3833 osdmap.is_down(q->second.acting_primary))) {
3834 newstat = &q->second;
3835 } else {
3836 // pending update is no longer down or already stale
3837 return;
3838 }
3839 } else {
3840 newstat = &pending_inc->pg_stat_updates[pgid];
3841 *newstat = cur;
3842 }
3843 dout(10) << __func__ << " marking pg " << pgid
3844 << " stale (acting_primary " << newstat->acting_primary
3845 << ")" << dendl;
3846 newstat->state |= PG_STATE_STALE;
3847 newstat->last_unstale = ceph_clock_now();
3848 }
3849 }
3850
3851 void PGMapUpdater::check_down_pgs(
3852 const OSDMap &osdmap,
3853 const PGMap &pg_map,
3854 bool check_all,
3855 const set<int>& need_check_down_pg_osds,
3856 PGMap::Incremental *pending_inc)
3857 {
3858 // if a large number of osds changed state, just iterate over the whole
3859 // pg map.
3860 if (need_check_down_pg_osds.size() > (unsigned)osdmap.get_num_osds() *
3861 g_conf().get_val<double>("mon_pg_check_down_all_threshold")) {
3862 check_all = true;
3863 }
3864
3865 if (check_all) {
3866 for (const auto& p : pg_map.pg_stat) {
3867 _try_mark_pg_stale(osdmap, p.first, p.second, pending_inc);
3868 }
3869 } else {
3870 for (auto osd : need_check_down_pg_osds) {
3871 if (osdmap.is_down(osd)) {
3872 auto p = pg_map.pg_by_osd.find(osd);
3873 if (p == pg_map.pg_by_osd.end()) {
3874 continue;
3875 }
3876 for (auto pgid : p->second) {
3877 const pg_stat_t &stat = pg_map.pg_stat.at(pgid);
3878 ceph_assert(stat.acting_primary == osd);
3879 _try_mark_pg_stale(osdmap, pgid, stat, pending_inc);
3880 }
3881 }
3882 }
3883 }
3884 }
3885
3886 int reweight::by_utilization(
3887 const OSDMap &osdmap,
3888 const PGMap &pgm,
3889 int oload,
3890 double max_changef,
3891 int max_osds,
3892 bool by_pg, const set<int64_t> *pools,
3893 bool no_increasing,
3894 mempool::osdmap::map<int32_t, uint32_t>* new_weights,
3895 std::stringstream *ss,
3896 std::string *out_str,
3897 ceph::Formatter *f)
3898 {
3899 if (oload <= 100) {
3900 *ss << "You must give a percentage higher than 100. "
3901 "The reweighting threshold will be calculated as <average-utilization> "
3902 "times <input-percentage>. For example, an argument of 200 would "
3903 "reweight OSDs which are twice as utilized as the average OSD.\n";
3904 return -EINVAL;
3905 }
3906
3907 vector<int> pgs_by_osd(osdmap.get_max_osd());
3908
3909 // Avoid putting a small number (or 0) in the denominator when calculating
3910 // average_util
3911 double average_util;
3912 if (by_pg) {
3913 // by pg mapping
3914 double weight_sum = 0.0; // sum up the crush weights
3915 unsigned num_pg_copies = 0;
3916 int num_osds = 0;
3917 for (const auto& pg : pgm.pg_stat) {
3918 if (pools && pools->count(pg.first.pool()) == 0)
3919 continue;
3920 for (const auto acting : pg.second.acting) {
3921 if (!osdmap.exists(acting)) {
3922 continue;
3923 }
3924 if (acting >= (int)pgs_by_osd.size())
3925 pgs_by_osd.resize(acting);
3926 if (pgs_by_osd[acting] == 0) {
3927 if (osdmap.crush->get_item_weightf(acting) <= 0) {
3928 //skip if we currently can not identify item
3929 continue;
3930 }
3931 weight_sum += osdmap.crush->get_item_weightf(acting);
3932 ++num_osds;
3933 }
3934 ++pgs_by_osd[acting];
3935 ++num_pg_copies;
3936 }
3937 }
3938
3939 if (!num_osds || (num_pg_copies / num_osds < g_conf()->mon_reweight_min_pgs_per_osd)) {
3940 *ss << "Refusing to reweight: we only have " << num_pg_copies
3941 << " PGs across " << num_osds << " osds!\n";
3942 return -EDOM;
3943 }
3944
3945 average_util = (double)num_pg_copies / weight_sum;
3946 } else {
3947 // by osd utilization
3948 int num_osd = std::max<size_t>(1, pgm.osd_stat.size());
3949 if ((uint64_t)pgm.osd_sum.statfs.total / num_osd
3950 < g_conf()->mon_reweight_min_bytes_per_osd) {
3951 *ss << "Refusing to reweight: we only have " << pgm.osd_sum.statfs.kb()
3952 << " kb across all osds!\n";
3953 return -EDOM;
3954 }
3955 if ((uint64_t)pgm.osd_sum.statfs.get_used_raw() / num_osd
3956 < g_conf()->mon_reweight_min_bytes_per_osd) {
3957 *ss << "Refusing to reweight: we only have "
3958 << pgm.osd_sum.statfs.kb_used_raw()
3959 << " kb used across all osds!\n";
3960 return -EDOM;
3961 }
3962
3963 average_util = (double)pgm.osd_sum.statfs.get_used_raw() /
3964 (double)pgm.osd_sum.statfs.total;
3965 }
3966
3967 // adjust down only if we are above the threshold
3968 const double overload_util = average_util * (double)oload / 100.0;
3969
3970 // but aggressively adjust weights up whenever possible.
3971 const double underload_util = average_util;
3972
3973 const unsigned max_change = (unsigned)(max_changef * (double)CEPH_OSD_IN);
3974
3975 ostringstream oss;
3976 if (f) {
3977 f->open_object_section("reweight_by_utilization");
3978 f->dump_int("overload_min", oload);
3979 f->dump_float("max_change", max_changef);
3980 f->dump_int("max_change_osds", max_osds);
3981 f->dump_float("average_utilization", average_util);
3982 f->dump_float("overload_utilization", overload_util);
3983 } else {
3984 oss << "oload " << oload << "\n";
3985 oss << "max_change " << max_changef << "\n";
3986 oss << "max_change_osds " << max_osds << "\n";
3987 oss.precision(4);
3988 oss << "average_utilization " << std::fixed << average_util << "\n";
3989 oss << "overload_utilization " << overload_util << "\n";
3990 }
3991 int num_changed = 0;
3992
3993 // precompute util for each OSD
3994 std::vector<std::pair<int, float> > util_by_osd;
3995 for (const auto& p : pgm.osd_stat) {
3996 std::pair<int, float> osd_util;
3997 osd_util.first = p.first;
3998 if (by_pg) {
3999 if (p.first >= (int)pgs_by_osd.size() ||
4000 pgs_by_osd[p.first] == 0) {
4001 // skip if this OSD does not contain any pg
4002 // belonging to the specified pool(s).
4003 continue;
4004 }
4005
4006 if (osdmap.crush->get_item_weightf(p.first) <= 0) {
4007 // skip if we are unable to locate item.
4008 continue;
4009 }
4010
4011 osd_util.second =
4012 pgs_by_osd[p.first] / osdmap.crush->get_item_weightf(p.first);
4013 } else {
4014 osd_util.second =
4015 (double)p.second.statfs.get_used_raw() / (double)p.second.statfs.total;
4016 }
4017 util_by_osd.push_back(osd_util);
4018 }
4019
4020 // sort by absolute deviation from the mean utilization,
4021 // in descending order.
4022 std::sort(util_by_osd.begin(), util_by_osd.end(),
4023 [average_util](std::pair<int, float> l, std::pair<int, float> r) {
4024 return abs(l.second - average_util) > abs(r.second - average_util);
4025 }
4026 );
4027
4028 if (f)
4029 f->open_array_section("reweights");
4030
4031 for (const auto& p : util_by_osd) {
4032 unsigned weight = osdmap.get_weight(p.first);
4033 if (weight == 0) {
4034 // skip if OSD is currently out
4035 continue;
4036 }
4037 float util = p.second;
4038
4039 if (util >= overload_util) {
4040 // Assign a lower weight to overloaded OSDs. The current weight
4041 // is a factor to take into account the original weights,
4042 // to represent e.g. differing storage capacities
4043 unsigned new_weight = (unsigned)((average_util / util) * (float)weight);
4044 if (weight > max_change)
4045 new_weight = std::max(new_weight, weight - max_change);
4046 new_weights->insert({p.first, new_weight});
4047 if (f) {
4048 f->open_object_section("osd");
4049 f->dump_int("osd", p.first);
4050 f->dump_float("weight", (float)weight / (float)CEPH_OSD_IN);
4051 f->dump_float("new_weight", (float)new_weight / (float)CEPH_OSD_IN);
4052 f->close_section();
4053 } else {
4054 oss << "osd." << p.first << " weight "
4055 << (float)weight / (float)CEPH_OSD_IN << " -> "
4056 << (float)new_weight / (float)CEPH_OSD_IN << "\n";
4057 }
4058 if (++num_changed >= max_osds)
4059 break;
4060 }
4061 if (!no_increasing && util <= underload_util) {
4062 // assign a higher weight.. if we can.
4063 unsigned new_weight = (unsigned)((average_util / util) * (float)weight);
4064 new_weight = std::min(new_weight, weight + max_change);
4065 if (new_weight > CEPH_OSD_IN)
4066 new_weight = CEPH_OSD_IN;
4067 if (new_weight > weight) {
4068 new_weights->insert({p.first, new_weight});
4069 oss << "osd." << p.first << " weight "
4070 << (float)weight / (float)CEPH_OSD_IN << " -> "
4071 << (float)new_weight / (float)CEPH_OSD_IN << "\n";
4072 if (++num_changed >= max_osds)
4073 break;
4074 }
4075 }
4076 }
4077 if (f) {
4078 f->close_section();
4079 }
4080
4081 OSDMap newmap;
4082 newmap.deepish_copy_from(osdmap);
4083 OSDMap::Incremental newinc;
4084 newinc.fsid = newmap.get_fsid();
4085 newinc.epoch = newmap.get_epoch() + 1;
4086 newinc.new_weight = *new_weights;
4087 newmap.apply_incremental(newinc);
4088
4089 osdmap.summarize_mapping_stats(&newmap, pools, out_str, f);
4090
4091 if (f) {
4092 f->close_section();
4093 } else {
4094 *out_str += "\n";
4095 *out_str += oss.str();
4096 }
4097 return num_changed;
4098 }