]> git.proxmox.com Git - ceph.git/blob - ceph/src/crush/CrushTreeDumper.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / crush / CrushTreeDumper.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph distributed storage system
5 *
6 * Copyright (C) 2015 Mirantis Inc
7 *
8 * Author: Mykola Golub <mgolub@mirantis.com>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 */
16
17 #ifndef CRUSH_TREE_DUMPER_H
18 #define CRUSH_TREE_DUMPER_H
19
20 #include "CrushWrapper.h"
21
22 /**
23 * CrushTreeDumper:
24 * A helper class and functions to dump a crush tree.
25 *
26 * Example:
27 *
28 * class SimpleDumper : public CrushTreeDumper::Dumper<ostream> {
29 * public:
30 * SimpleDumper(const CrushWrapper *crush) :
31 * CrushTreeDumper::Dumper<ostream>(crush) {}
32 * protected:
33 * virtual void dump_item(const CrushTreeDumper::Item &qi, ostream *out) {
34 * *out << qi.id;
35 * for (int k = 0; k < qi.depth; k++)
36 * *out << "-";
37 * if (qi.is_bucket())
38 * *out << crush->get_item_name(qi.id)
39 * else
40 * *out << "osd." << qi.id;
41 * *out << "\n";
42 * }
43 * };
44 *
45 * SimpleDumper(crush).dump(out);
46 *
47 */
48
49 namespace CrushTreeDumper {
50
51 struct Item {
52 int id;
53 int depth;
54 float weight;
55 list<int> children;
56
57 Item() : id(0), depth(0), weight(0) {}
58 Item(int i, int d, float w) : id(i), depth(d), weight(w) {}
59
60 bool is_bucket() const { return id < 0; }
61 };
62
63 template <typename F>
64 class Dumper : public list<Item> {
65 public:
66 explicit Dumper(const CrushWrapper *crush_) : crush(crush_) {
67 crush->find_roots(roots);
68 root = roots.begin();
69 }
70
71 virtual ~Dumper() {}
72
73 virtual void reset() {
74 root = roots.begin();
75 touched.clear();
76 clear();
77 }
78
79 bool next(Item &qi) {
80 if (empty()) {
81 if (root == roots.end())
82 return false;
83 push_back(Item(*root, 0, crush->get_bucket_weightf(*root)));
84 ++root;
85 }
86
87 qi = front();
88 pop_front();
89 touched.insert(qi.id);
90
91 if (qi.is_bucket()) {
92 // queue bucket contents...
93 int s = crush->get_bucket_size(qi.id);
94 for (int k = s - 1; k >= 0; k--) {
95 int id = crush->get_bucket_item(qi.id, k);
96 qi.children.push_back(id);
97 push_front(Item(id, qi.depth + 1,
98 crush->get_bucket_item_weightf(qi.id, k)));
99 }
100 }
101 return true;
102 }
103
104 void dump(F *f) {
105 reset();
106 Item qi;
107 while (next(qi))
108 dump_item(qi, f);
109 }
110
111 bool is_touched(int id) const { return touched.count(id) > 0; }
112
113 protected:
114 virtual void dump_item(const Item &qi, F *f) = 0;
115
116 protected:
117 const CrushWrapper *crush;
118
119 private:
120 set<int> touched;
121 set<int> roots;
122 set<int>::iterator root;
123 };
124
125 inline void dump_item_fields(const CrushWrapper *crush,
126 const Item &qi, Formatter *f) {
127 f->dump_int("id", qi.id);
128 if (qi.is_bucket()) {
129 int type = crush->get_bucket_type(qi.id);
130 f->dump_string("name", crush->get_item_name(qi.id));
131 f->dump_string("type", crush->get_type_name(type));
132 f->dump_int("type_id", type);
133 } else {
134 f->dump_stream("name") << "osd." << qi.id;
135 f->dump_string("type", crush->get_type_name(0));
136 f->dump_int("type_id", 0);
137 f->dump_float("crush_weight", qi.weight);
138 f->dump_unsigned("depth", qi.depth);
139 }
140 }
141
142 inline void dump_bucket_children(const CrushWrapper *crush,
143 const Item &qi, Formatter *f) {
144 if (!qi.is_bucket())
145 return;
146
147 f->open_array_section("children");
148 for (list<int>::const_iterator i = qi.children.begin();
149 i != qi.children.end();
150 ++i) {
151 f->dump_int("child", *i);
152 }
153 f->close_section();
154 }
155
156 class FormattingDumper : public Dumper<Formatter> {
157 public:
158 explicit FormattingDumper(const CrushWrapper *crush) : Dumper<Formatter>(crush) {}
159
160 protected:
161 void dump_item(const Item &qi, Formatter *f) override {
162 f->open_object_section("item");
163 dump_item_fields(qi, f);
164 dump_bucket_children(qi, f);
165 f->close_section();
166 }
167
168 virtual void dump_item_fields(const Item &qi, Formatter *f) {
169 CrushTreeDumper::dump_item_fields(crush, qi, f);
170 }
171
172 virtual void dump_bucket_children(const Item &qi, Formatter *f) {
173 CrushTreeDumper::dump_bucket_children(crush, qi, f);
174 }
175 };
176
177 }
178
179 #endif