]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include "osd/osd_types.h" | |
5 | #include "common/debug.h" | |
6 | #include "common/Formatter.h" | |
7 | #include "common/errno.h" | |
c07f9fc5 | 8 | #include "common/TextTable.h" |
7c673cae FG |
9 | #include "include/stringify.h" |
10 | ||
11 | #include "CrushWrapper.h" | |
12 | #include "CrushTreeDumper.h" | |
13 | ||
14 | #define dout_subsys ceph_subsys_crush | |
15 | ||
3efd9988 | 16 | bool CrushWrapper::has_legacy_rule_ids() const |
31f18b77 FG |
17 | { |
18 | for (unsigned i=0; i<crush->max_rules; i++) { | |
19 | crush_rule *r = crush->rules[i]; | |
20 | if (r && | |
21 | r->mask.ruleset != i) { | |
22 | return true; | |
23 | } | |
24 | } | |
25 | return false; | |
26 | } | |
27 | ||
3efd9988 | 28 | std::map<int, int> CrushWrapper::renumber_rules() |
31f18b77 | 29 | { |
3efd9988 | 30 | std::map<int, int> result; |
31f18b77 FG |
31 | for (unsigned i=0; i<crush->max_rules; i++) { |
32 | crush_rule *r = crush->rules[i]; | |
3efd9988 FG |
33 | if (r && r->mask.ruleset != i) { |
34 | result[r->mask.ruleset] = i; | |
35 | r->mask.ruleset = i; | |
31f18b77 FG |
36 | } |
37 | } | |
3efd9988 | 38 | return result; |
31f18b77 FG |
39 | } |
40 | ||
c07f9fc5 FG |
41 | bool CrushWrapper::has_non_straw2_buckets() const |
42 | { | |
43 | for (int i=0; i<crush->max_buckets; ++i) { | |
44 | crush_bucket *b = crush->buckets[i]; | |
45 | if (!b) | |
46 | continue; | |
47 | if (b->alg != CRUSH_BUCKET_STRAW2) | |
48 | return true; | |
49 | } | |
50 | return false; | |
51 | } | |
52 | ||
7c673cae FG |
53 | bool CrushWrapper::has_v2_rules() const |
54 | { | |
55 | for (unsigned i=0; i<crush->max_rules; i++) { | |
56 | if (is_v2_rule(i)) { | |
57 | return true; | |
58 | } | |
59 | } | |
60 | return false; | |
61 | } | |
62 | ||
63 | bool CrushWrapper::is_v2_rule(unsigned ruleid) const | |
64 | { | |
65 | // check rule for use of indep or new SET_* rule steps | |
66 | if (ruleid >= crush->max_rules) | |
67 | return false; | |
68 | crush_rule *r = crush->rules[ruleid]; | |
69 | if (!r) | |
70 | return false; | |
71 | for (unsigned j=0; j<r->len; j++) { | |
72 | if (r->steps[j].op == CRUSH_RULE_CHOOSE_INDEP || | |
73 | r->steps[j].op == CRUSH_RULE_CHOOSELEAF_INDEP || | |
74 | r->steps[j].op == CRUSH_RULE_SET_CHOOSE_TRIES || | |
75 | r->steps[j].op == CRUSH_RULE_SET_CHOOSELEAF_TRIES) { | |
76 | return true; | |
77 | } | |
78 | } | |
79 | return false; | |
80 | } | |
81 | ||
82 | bool CrushWrapper::has_v3_rules() const | |
83 | { | |
84 | for (unsigned i=0; i<crush->max_rules; i++) { | |
85 | if (is_v3_rule(i)) { | |
86 | return true; | |
87 | } | |
88 | } | |
89 | return false; | |
90 | } | |
91 | ||
92 | bool CrushWrapper::is_v3_rule(unsigned ruleid) const | |
93 | { | |
94 | // check rule for use of SET_CHOOSELEAF_VARY_R step | |
95 | if (ruleid >= crush->max_rules) | |
96 | return false; | |
97 | crush_rule *r = crush->rules[ruleid]; | |
98 | if (!r) | |
99 | return false; | |
100 | for (unsigned j=0; j<r->len; j++) { | |
101 | if (r->steps[j].op == CRUSH_RULE_SET_CHOOSELEAF_VARY_R) { | |
102 | return true; | |
103 | } | |
104 | } | |
105 | return false; | |
106 | } | |
107 | ||
108 | bool CrushWrapper::has_v4_buckets() const | |
109 | { | |
110 | for (int i=0; i<crush->max_buckets; ++i) { | |
111 | crush_bucket *b = crush->buckets[i]; | |
112 | if (!b) | |
113 | continue; | |
114 | if (b->alg == CRUSH_BUCKET_STRAW2) | |
115 | return true; | |
116 | } | |
117 | return false; | |
118 | } | |
119 | ||
120 | bool CrushWrapper::has_v5_rules() const | |
121 | { | |
122 | for (unsigned i=0; i<crush->max_rules; i++) { | |
123 | if (is_v5_rule(i)) { | |
124 | return true; | |
125 | } | |
126 | } | |
127 | return false; | |
128 | } | |
129 | ||
130 | bool CrushWrapper::is_v5_rule(unsigned ruleid) const | |
131 | { | |
132 | // check rule for use of SET_CHOOSELEAF_STABLE step | |
133 | if (ruleid >= crush->max_rules) | |
134 | return false; | |
135 | crush_rule *r = crush->rules[ruleid]; | |
136 | if (!r) | |
137 | return false; | |
138 | for (unsigned j=0; j<r->len; j++) { | |
139 | if (r->steps[j].op == CRUSH_RULE_SET_CHOOSELEAF_STABLE) { | |
140 | return true; | |
141 | } | |
142 | } | |
143 | return false; | |
144 | } | |
145 | ||
31f18b77 | 146 | bool CrushWrapper::has_choose_args() const |
7c673cae FG |
147 | { |
148 | return !choose_args.empty(); | |
149 | } | |
150 | ||
31f18b77 | 151 | bool CrushWrapper::has_incompat_choose_args() const |
7c673cae | 152 | { |
31f18b77 FG |
153 | if (choose_args.empty()) |
154 | return false; | |
155 | if (choose_args.size() > 1) | |
156 | return true; | |
c07f9fc5 FG |
157 | if (choose_args.begin()->first != DEFAULT_CHOOSE_ARGS) |
158 | return true; | |
31f18b77 FG |
159 | crush_choose_arg_map arg_map = choose_args.begin()->second; |
160 | for (__u32 i = 0; i < arg_map.size; i++) { | |
161 | crush_choose_arg *arg = &arg_map.args[i]; | |
28e407b8 | 162 | if (arg->weight_set_positions == 0 && |
31f18b77 FG |
163 | arg->ids_size == 0) |
164 | continue; | |
28e407b8 | 165 | if (arg->weight_set_positions != 1) |
31f18b77 FG |
166 | return true; |
167 | if (arg->ids_size != 0) | |
168 | return true; | |
169 | } | |
170 | return false; | |
7c673cae FG |
171 | } |
172 | ||
173 | int CrushWrapper::split_id_class(int i, int *idout, int *classout) const | |
174 | { | |
175 | if (!item_exists(i)) | |
176 | return -EINVAL; | |
177 | string name = get_item_name(i); | |
178 | size_t pos = name.find("~"); | |
179 | if (pos == string::npos) { | |
180 | *idout = i; | |
181 | *classout = -1; | |
182 | return 0; | |
183 | } | |
184 | string name_no_class = name.substr(0, pos); | |
185 | if (!name_exists(name_no_class)) | |
186 | return -ENOENT; | |
187 | string class_name = name.substr(pos + 1); | |
188 | if (!class_exists(class_name)) | |
189 | return -ENOENT; | |
190 | *idout = get_item_id(name_no_class); | |
191 | *classout = get_class_id(class_name); | |
192 | return 0; | |
193 | } | |
194 | ||
195 | int CrushWrapper::can_rename_item(const string& srcname, | |
196 | const string& dstname, | |
197 | ostream *ss) const | |
198 | { | |
199 | if (name_exists(srcname)) { | |
200 | if (name_exists(dstname)) { | |
201 | *ss << "dstname = '" << dstname << "' already exists"; | |
202 | return -EEXIST; | |
203 | } | |
204 | if (is_valid_crush_name(dstname)) { | |
205 | return 0; | |
206 | } else { | |
207 | *ss << "dstname = '" << dstname << "' does not match [-_.0-9a-zA-Z]+"; | |
208 | return -EINVAL; | |
209 | } | |
210 | } else { | |
211 | if (name_exists(dstname)) { | |
212 | *ss << "srcname = '" << srcname << "' does not exist " | |
213 | << "and dstname = '" << dstname << "' already exists"; | |
214 | return -EALREADY; | |
215 | } else { | |
216 | *ss << "srcname = '" << srcname << "' does not exist"; | |
217 | return -ENOENT; | |
218 | } | |
219 | } | |
220 | } | |
221 | ||
222 | int CrushWrapper::rename_item(const string& srcname, | |
223 | const string& dstname, | |
224 | ostream *ss) | |
225 | { | |
226 | int ret = can_rename_item(srcname, dstname, ss); | |
227 | if (ret < 0) | |
228 | return ret; | |
229 | int oldid = get_item_id(srcname); | |
230 | return set_item_name(oldid, dstname); | |
231 | } | |
232 | ||
233 | int CrushWrapper::can_rename_bucket(const string& srcname, | |
234 | const string& dstname, | |
235 | ostream *ss) const | |
236 | { | |
237 | int ret = can_rename_item(srcname, dstname, ss); | |
238 | if (ret) | |
239 | return ret; | |
240 | int srcid = get_item_id(srcname); | |
241 | if (srcid >= 0) { | |
242 | *ss << "srcname = '" << srcname << "' is not a bucket " | |
243 | << "because its id = " << srcid << " is >= 0"; | |
244 | return -ENOTDIR; | |
245 | } | |
246 | return 0; | |
247 | } | |
248 | ||
249 | int CrushWrapper::rename_bucket(const string& srcname, | |
250 | const string& dstname, | |
251 | ostream *ss) | |
252 | { | |
253 | int ret = can_rename_bucket(srcname, dstname, ss); | |
254 | if (ret < 0) | |
255 | return ret; | |
256 | int oldid = get_item_id(srcname); | |
257 | return set_item_name(oldid, dstname); | |
258 | } | |
259 | ||
b5b8bbf5 FG |
260 | int CrushWrapper::rename_rule(const string& srcname, |
261 | const string& dstname, | |
262 | ostream *ss) | |
263 | { | |
264 | if (!rule_exists(srcname)) { | |
265 | if (ss) { | |
266 | *ss << "source rule name '" << srcname << "' does not exist"; | |
267 | } | |
268 | return -ENOENT; | |
269 | } | |
270 | if (rule_exists(dstname)) { | |
271 | if (ss) { | |
272 | *ss << "destination rule name '" << dstname << "' already exists"; | |
273 | } | |
274 | return -EEXIST; | |
275 | } | |
276 | int rule_id = get_rule_id(srcname); | |
277 | auto it = rule_name_map.find(rule_id); | |
278 | assert(it != rule_name_map.end()); | |
279 | it->second = dstname; | |
280 | if (have_rmaps) { | |
281 | rule_name_rmap.erase(srcname); | |
282 | rule_name_rmap[dstname] = rule_id; | |
283 | } | |
284 | return 0; | |
285 | } | |
286 | ||
3efd9988 | 287 | void CrushWrapper::find_takes(set<int> *roots) const |
7c673cae FG |
288 | { |
289 | for (unsigned i=0; i<crush->max_rules; i++) { | |
290 | crush_rule *r = crush->rules[i]; | |
291 | if (!r) | |
292 | continue; | |
293 | for (unsigned j=0; j<r->len; j++) { | |
294 | if (r->steps[j].op == CRUSH_RULE_TAKE) | |
3efd9988 | 295 | roots->insert(r->steps[j].arg1); |
7c673cae FG |
296 | } |
297 | } | |
298 | } | |
299 | ||
28e407b8 AA |
300 | void CrushWrapper::find_takes_by_rule(int rule, set<int> *roots) const |
301 | { | |
302 | if (rule < 0 || rule >= (int)crush->max_rules) | |
303 | return; | |
304 | crush_rule *r = crush->rules[rule]; | |
305 | if (!r) | |
306 | return; | |
307 | for (unsigned i = 0; i < r->len; i++) { | |
308 | if (r->steps[i].op == CRUSH_RULE_TAKE) | |
309 | roots->insert(r->steps[i].arg1); | |
310 | } | |
311 | } | |
312 | ||
3efd9988 | 313 | void CrushWrapper::find_roots(set<int> *roots) const |
7c673cae FG |
314 | { |
315 | for (int i = 0; i < crush->max_buckets; i++) { | |
316 | if (!crush->buckets[i]) | |
317 | continue; | |
318 | crush_bucket *b = crush->buckets[i]; | |
319 | if (!_search_item_exists(b->id)) | |
3efd9988 | 320 | roots->insert(b->id); |
7c673cae FG |
321 | } |
322 | } | |
323 | ||
324 | bool CrushWrapper::subtree_contains(int root, int item) const | |
325 | { | |
326 | if (root == item) | |
327 | return true; | |
328 | ||
329 | if (root >= 0) | |
330 | return false; // root is a leaf | |
331 | ||
332 | const crush_bucket *b = get_bucket(root); | |
333 | if (IS_ERR(b)) | |
334 | return false; | |
335 | ||
336 | for (unsigned j=0; j<b->size; j++) { | |
337 | if (subtree_contains(b->items[j], item)) | |
338 | return true; | |
339 | } | |
340 | return false; | |
341 | } | |
342 | ||
343 | bool CrushWrapper::_maybe_remove_last_instance(CephContext *cct, int item, bool unlink_only) | |
344 | { | |
345 | // last instance? | |
346 | if (_search_item_exists(item)) { | |
347 | return false; | |
348 | } | |
349 | if (item < 0 && _bucket_is_in_use(item)) { | |
350 | return false; | |
351 | } | |
352 | ||
353 | if (item < 0 && !unlink_only) { | |
354 | crush_bucket *t = get_bucket(item); | |
355 | ldout(cct, 5) << "_maybe_remove_last_instance removing bucket " << item << dendl; | |
356 | crush_remove_bucket(crush, t); | |
357 | if (class_bucket.count(item) != 0) | |
358 | class_bucket.erase(item); | |
224ce89b | 359 | class_remove_item(item); |
28e407b8 | 360 | update_choose_args(cct); |
7c673cae FG |
361 | } |
362 | if ((item >= 0 || !unlink_only) && name_map.count(item)) { | |
363 | ldout(cct, 5) << "_maybe_remove_last_instance removing name for item " << item << dendl; | |
364 | name_map.erase(item); | |
365 | have_rmaps = false; | |
c07f9fc5 FG |
366 | if (item >= 0 && !unlink_only) { |
367 | class_remove_item(item); | |
368 | } | |
7c673cae | 369 | } |
d2e6a577 | 370 | rebuild_roots_with_classes(); |
7c673cae FG |
371 | return true; |
372 | } | |
373 | ||
35e4c445 | 374 | int CrushWrapper::remove_root(int item) |
7c673cae | 375 | { |
7c673cae | 376 | crush_bucket *b = get_bucket(item); |
d2e6a577 FG |
377 | if (IS_ERR(b)) { |
378 | // should be idempotent | |
379 | // e.g.: we use 'crush link' to link same host into | |
380 | // different roots, which as a result can cause different | |
381 | // shadow trees reference same hosts too. This means | |
382 | // we may need to destory the same buckets(hosts, racks, etc.) | |
383 | // multiple times during rebuilding all shadow trees. | |
384 | return 0; | |
385 | } | |
7c673cae FG |
386 | |
387 | for (unsigned n = 0; n < b->size; n++) { | |
388 | if (b->items[n] >= 0) | |
389 | continue; | |
35e4c445 | 390 | int r = remove_root(b->items[n]); |
7c673cae FG |
391 | if (r < 0) |
392 | return r; | |
393 | } | |
394 | ||
395 | crush_remove_bucket(crush, b); | |
396 | if (name_map.count(item) != 0) { | |
397 | name_map.erase(item); | |
398 | have_rmaps = false; | |
399 | } | |
400 | if (class_bucket.count(item) != 0) | |
401 | class_bucket.erase(item); | |
c07f9fc5 | 402 | class_remove_item(item); |
28e407b8 | 403 | update_choose_args(nullptr); |
7c673cae FG |
404 | return 0; |
405 | } | |
406 | ||
28e407b8 AA |
407 | void CrushWrapper::update_choose_args(CephContext *cct) |
408 | { | |
409 | for (auto& i : choose_args) { | |
410 | crush_choose_arg_map &arg_map = i.second; | |
411 | unsigned positions = get_choose_args_positions(arg_map); | |
412 | for (int j = 0; j < crush->max_buckets; ++j) { | |
413 | crush_bucket *b = crush->buckets[j]; | |
414 | auto& carg = arg_map.args[j]; | |
415 | // strip out choose_args for any buckets that no longer exist | |
416 | if (!b || b->alg != CRUSH_BUCKET_STRAW2) { | |
417 | if (carg.ids) { | |
418 | if (cct) | |
419 | ldout(cct,0) << __func__ << " removing " << i.first << " bucket " | |
420 | << (-1-j) << " ids" << dendl; | |
421 | free(carg.ids); | |
422 | carg.ids = 0; | |
423 | carg.ids_size = 0; | |
424 | } | |
425 | if (carg.weight_set) { | |
426 | if (cct) | |
427 | ldout(cct,0) << __func__ << " removing " << i.first << " bucket " | |
428 | << (-1-j) << " weight_sets" << dendl; | |
429 | for (unsigned p = 0; p < carg.weight_set_positions; ++p) { | |
430 | free(carg.weight_set[p].weights); | |
431 | } | |
432 | free(carg.weight_set); | |
433 | carg.weight_set = 0; | |
434 | carg.weight_set_positions = 0; | |
435 | } | |
436 | continue; | |
437 | } | |
438 | if (carg.weight_set_positions == 0) { | |
439 | continue; // skip it | |
440 | } | |
441 | if (carg.weight_set_positions != positions) { | |
442 | if (cct) | |
443 | lderr(cct) << __func__ << " " << i.first << " bucket " | |
444 | << (-1-j) << " positions " << carg.weight_set_positions | |
445 | << " -> " << positions << dendl; | |
446 | continue; // wth... skip! | |
447 | } | |
448 | // mis-sized weight_sets? this shouldn't ever happen. | |
449 | for (unsigned p = 0; p < positions; ++p) { | |
450 | if (carg.weight_set[p].size != b->size) { | |
451 | if (cct) | |
452 | lderr(cct) << __func__ << " fixing " << i.first << " bucket " | |
453 | << (-1-j) << " position " << p | |
454 | << " size " << carg.weight_set[p].size << " -> " | |
455 | << b->size << dendl; | |
456 | auto old_ws = carg.weight_set[p]; | |
457 | carg.weight_set[p].size = b->size; | |
458 | carg.weight_set[p].weights = (__u32*)calloc(b->size, sizeof(__u32)); | |
459 | auto max = std::min<unsigned>(old_ws.size, b->size); | |
460 | for (unsigned k = 0; k < max; ++k) { | |
461 | carg.weight_set[p].weights[k] = old_ws.weights[k]; | |
462 | } | |
463 | free(old_ws.weights); | |
464 | } | |
465 | } | |
466 | } | |
467 | } | |
468 | } | |
469 | ||
7c673cae FG |
470 | int CrushWrapper::remove_item(CephContext *cct, int item, bool unlink_only) |
471 | { | |
c07f9fc5 FG |
472 | ldout(cct, 5) << "remove_item " << item |
473 | << (unlink_only ? " unlink_only":"") << dendl; | |
7c673cae FG |
474 | |
475 | int ret = -ENOENT; | |
476 | ||
477 | if (item < 0 && !unlink_only) { | |
478 | crush_bucket *t = get_bucket(item); | |
479 | if (IS_ERR(t)) { | |
c07f9fc5 FG |
480 | ldout(cct, 1) << "remove_item bucket " << item << " does not exist" |
481 | << dendl; | |
7c673cae FG |
482 | return -ENOENT; |
483 | } | |
484 | ||
485 | if (t->size) { | |
486 | ldout(cct, 1) << "remove_item bucket " << item << " has " << t->size | |
487 | << " items, not empty" << dendl; | |
488 | return -ENOTEMPTY; | |
489 | } | |
490 | if (_bucket_is_in_use(item)) { | |
491 | return -EBUSY; | |
492 | } | |
493 | } | |
494 | ||
495 | for (int i = 0; i < crush->max_buckets; i++) { | |
496 | if (!crush->buckets[i]) | |
497 | continue; | |
498 | crush_bucket *b = crush->buckets[i]; | |
499 | ||
500 | for (unsigned i=0; i<b->size; ++i) { | |
501 | int id = b->items[i]; | |
502 | if (id == item) { | |
503 | ldout(cct, 5) << "remove_item removing item " << item | |
504 | << " from bucket " << b->id << dendl; | |
c07f9fc5 FG |
505 | for (auto& p : choose_args) { |
506 | // weight down each weight-set to 0 before we remove the item | |
507 | vector<int> weightv(get_choose_args_positions(p.second), 0); | |
508 | choose_args_adjust_item_weight(cct, p.second, item, weightv, nullptr); | |
509 | } | |
31f18b77 | 510 | bucket_remove_item(b, item); |
7c673cae FG |
511 | adjust_item_weight(cct, b->id, b->weight); |
512 | ret = 0; | |
513 | } | |
514 | } | |
515 | } | |
516 | ||
517 | if (_maybe_remove_last_instance(cct, item, unlink_only)) | |
518 | ret = 0; | |
519 | ||
520 | return ret; | |
521 | } | |
522 | ||
523 | bool CrushWrapper::_search_item_exists(int item) const | |
524 | { | |
525 | for (int i = 0; i < crush->max_buckets; i++) { | |
526 | if (!crush->buckets[i]) | |
527 | continue; | |
528 | crush_bucket *b = crush->buckets[i]; | |
529 | for (unsigned j=0; j<b->size; ++j) { | |
530 | if (b->items[j] == item) | |
531 | return true; | |
532 | } | |
533 | } | |
534 | return false; | |
535 | } | |
536 | ||
537 | bool CrushWrapper::_bucket_is_in_use(int item) | |
538 | { | |
539 | for (auto &i : class_bucket) | |
540 | for (auto &j : i.second) | |
541 | if (j.second == item) | |
542 | return true; | |
543 | for (unsigned i = 0; i < crush->max_rules; ++i) { | |
544 | crush_rule *r = crush->rules[i]; | |
545 | if (!r) | |
546 | continue; | |
547 | for (unsigned j = 0; j < r->len; ++j) { | |
548 | if (r->steps[j].op == CRUSH_RULE_TAKE) { | |
549 | int step_item = r->steps[j].arg1; | |
550 | int original_item; | |
551 | int c; | |
552 | int res = split_id_class(step_item, &original_item, &c); | |
553 | if (res < 0) | |
554 | return false; | |
555 | if (step_item == item || original_item == item) | |
556 | return true; | |
557 | } | |
558 | } | |
559 | } | |
560 | return false; | |
561 | } | |
562 | ||
c07f9fc5 FG |
563 | int CrushWrapper::_remove_item_under( |
564 | CephContext *cct, int item, int ancestor, bool unlink_only) | |
7c673cae FG |
565 | { |
566 | ldout(cct, 5) << "_remove_item_under " << item << " under " << ancestor | |
567 | << (unlink_only ? " unlink_only":"") << dendl; | |
568 | ||
569 | if (ancestor >= 0) { | |
570 | return -EINVAL; | |
571 | } | |
572 | ||
573 | if (!bucket_exists(ancestor)) | |
574 | return -EINVAL; | |
575 | ||
576 | int ret = -ENOENT; | |
577 | ||
578 | crush_bucket *b = get_bucket(ancestor); | |
579 | for (unsigned i=0; i<b->size; ++i) { | |
580 | int id = b->items[i]; | |
581 | if (id == item) { | |
c07f9fc5 FG |
582 | ldout(cct, 5) << "_remove_item_under removing item " << item |
583 | << " from bucket " << b->id << dendl; | |
c07f9fc5 FG |
584 | for (auto& p : choose_args) { |
585 | // weight down each weight-set to 0 before we remove the item | |
586 | vector<int> weightv(get_choose_args_positions(p.second), 0); | |
587 | _choose_args_adjust_item_weight_in_bucket( | |
588 | cct, p.second, b->id, item, weightv, nullptr); | |
589 | } | |
35e4c445 | 590 | bucket_remove_item(b, item); |
7c673cae FG |
591 | adjust_item_weight(cct, b->id, b->weight); |
592 | ret = 0; | |
593 | } else if (id < 0) { | |
594 | int r = remove_item_under(cct, item, id, unlink_only); | |
595 | if (r == 0) | |
596 | ret = 0; | |
597 | } | |
598 | } | |
599 | return ret; | |
600 | } | |
601 | ||
c07f9fc5 FG |
602 | int CrushWrapper::remove_item_under( |
603 | CephContext *cct, int item, int ancestor, bool unlink_only) | |
7c673cae FG |
604 | { |
605 | ldout(cct, 5) << "remove_item_under " << item << " under " << ancestor | |
606 | << (unlink_only ? " unlink_only":"") << dendl; | |
607 | ||
608 | if (!unlink_only && _bucket_is_in_use(item)) { | |
609 | return -EBUSY; | |
610 | } | |
611 | ||
612 | int ret = _remove_item_under(cct, item, ancestor, unlink_only); | |
613 | if (ret < 0) | |
614 | return ret; | |
615 | ||
616 | if (item < 0 && !unlink_only) { | |
617 | crush_bucket *t = get_bucket(item); | |
618 | if (IS_ERR(t)) { | |
619 | ldout(cct, 1) << "remove_item_under bucket " << item | |
620 | << " does not exist" << dendl; | |
621 | return -ENOENT; | |
622 | } | |
623 | ||
624 | if (t->size) { | |
625 | ldout(cct, 1) << "remove_item_under bucket " << item << " has " << t->size | |
626 | << " items, not empty" << dendl; | |
627 | return -ENOTEMPTY; | |
628 | } | |
629 | } | |
630 | ||
631 | if (_maybe_remove_last_instance(cct, item, unlink_only)) | |
632 | ret = 0; | |
633 | ||
634 | return ret; | |
635 | } | |
636 | ||
637 | int CrushWrapper::get_common_ancestor_distance(CephContext *cct, int id, | |
638 | const std::multimap<string,string>& loc) | |
639 | { | |
640 | ldout(cct, 5) << __func__ << " " << id << " " << loc << dendl; | |
641 | if (!item_exists(id)) | |
642 | return -ENOENT; | |
643 | map<string,string> id_loc = get_full_location(id); | |
644 | ldout(cct, 20) << " id is at " << id_loc << dendl; | |
645 | ||
646 | for (map<int,string>::const_iterator p = type_map.begin(); | |
647 | p != type_map.end(); | |
648 | ++p) { | |
649 | map<string,string>::iterator ip = id_loc.find(p->second); | |
650 | if (ip == id_loc.end()) | |
651 | continue; | |
652 | for (std::multimap<string,string>::const_iterator q = loc.find(p->second); | |
653 | q != loc.end(); | |
654 | ++q) { | |
655 | if (q->first != p->second) | |
656 | break; | |
657 | if (q->second == ip->second) | |
658 | return p->first; | |
659 | } | |
660 | } | |
661 | return -ERANGE; | |
662 | } | |
663 | ||
664 | int CrushWrapper::parse_loc_map(const std::vector<string>& args, | |
665 | std::map<string,string> *ploc) | |
666 | { | |
667 | ploc->clear(); | |
668 | for (unsigned i = 0; i < args.size(); ++i) { | |
669 | const char *s = args[i].c_str(); | |
670 | const char *pos = strchr(s, '='); | |
671 | if (!pos) | |
672 | return -EINVAL; | |
673 | string key(s, 0, pos-s); | |
674 | string value(pos+1); | |
675 | if (value.length()) | |
676 | (*ploc)[key] = value; | |
677 | else | |
678 | return -EINVAL; | |
679 | } | |
680 | return 0; | |
681 | } | |
682 | ||
683 | int CrushWrapper::parse_loc_multimap(const std::vector<string>& args, | |
684 | std::multimap<string,string> *ploc) | |
685 | { | |
686 | ploc->clear(); | |
687 | for (unsigned i = 0; i < args.size(); ++i) { | |
688 | const char *s = args[i].c_str(); | |
689 | const char *pos = strchr(s, '='); | |
690 | if (!pos) | |
691 | return -EINVAL; | |
692 | string key(s, 0, pos-s); | |
693 | string value(pos+1); | |
694 | if (value.length()) | |
695 | ploc->insert(make_pair(key, value)); | |
696 | else | |
697 | return -EINVAL; | |
698 | } | |
699 | return 0; | |
700 | } | |
701 | ||
702 | bool CrushWrapper::check_item_loc(CephContext *cct, int item, const map<string,string>& loc, | |
703 | int *weight) | |
704 | { | |
705 | ldout(cct, 5) << "check_item_loc item " << item << " loc " << loc << dendl; | |
706 | ||
707 | for (map<int,string>::const_iterator p = type_map.begin(); p != type_map.end(); ++p) { | |
708 | // ignore device | |
709 | if (p->first == 0) | |
710 | continue; | |
711 | ||
712 | // ignore types that aren't specified in loc | |
713 | map<string,string>::const_iterator q = loc.find(p->second); | |
714 | if (q == loc.end()) { | |
715 | ldout(cct, 2) << "warning: did not specify location for '" << p->second << "' level (levels are " | |
716 | << type_map << ")" << dendl; | |
717 | continue; | |
718 | } | |
719 | ||
720 | if (!name_exists(q->second)) { | |
721 | ldout(cct, 5) << "check_item_loc bucket " << q->second << " dne" << dendl; | |
722 | return false; | |
723 | } | |
724 | ||
725 | int id = get_item_id(q->second); | |
726 | if (id >= 0) { | |
727 | ldout(cct, 5) << "check_item_loc requested " << q->second << " for type " << p->second | |
728 | << " is a device, not bucket" << dendl; | |
729 | return false; | |
730 | } | |
731 | ||
732 | assert(bucket_exists(id)); | |
733 | crush_bucket *b = get_bucket(id); | |
734 | ||
735 | // see if item exists in this bucket | |
736 | for (unsigned j=0; j<b->size; j++) { | |
737 | if (b->items[j] == item) { | |
738 | ldout(cct, 2) << "check_item_loc " << item << " exists in bucket " << b->id << dendl; | |
739 | if (weight) | |
740 | *weight = crush_get_bucket_item_weight(b, j); | |
741 | return true; | |
742 | } | |
743 | } | |
744 | return false; | |
745 | } | |
746 | ||
94b18763 | 747 | ldout(cct, 2) << __func__ << " item " << item << " loc " << loc << dendl; |
7c673cae FG |
748 | return false; |
749 | } | |
750 | ||
751 | map<string, string> CrushWrapper::get_full_location(int id) | |
752 | { | |
753 | vector<pair<string, string> > full_location_ordered; | |
754 | map<string,string> full_location; | |
755 | ||
756 | get_full_location_ordered(id, full_location_ordered); | |
757 | ||
758 | std::copy(full_location_ordered.begin(), | |
759 | full_location_ordered.end(), | |
760 | std::inserter(full_location, full_location.begin())); | |
761 | ||
762 | return full_location; | |
763 | } | |
764 | ||
765 | int CrushWrapper::get_full_location_ordered(int id, vector<pair<string, string> >& path) | |
766 | { | |
767 | if (!item_exists(id)) | |
768 | return -ENOENT; | |
769 | int cur = id; | |
770 | int ret; | |
771 | while (true) { | |
772 | pair<string, string> parent_coord = get_immediate_parent(cur, &ret); | |
773 | if (ret != 0) | |
774 | break; | |
775 | path.push_back(parent_coord); | |
776 | cur = get_item_id(parent_coord.second); | |
777 | } | |
778 | return 0; | |
779 | } | |
780 | ||
31f18b77 FG |
781 | string CrushWrapper::get_full_location_ordered_string(int id) |
782 | { | |
783 | vector<pair<string, string> > full_location_ordered; | |
784 | string full_location; | |
785 | get_full_location_ordered(id, full_location_ordered); | |
786 | reverse(begin(full_location_ordered), end(full_location_ordered)); | |
787 | for(auto i = full_location_ordered.begin(); i != full_location_ordered.end(); i++) { | |
788 | full_location = full_location + i->first + "=" + i->second; | |
789 | if (i != full_location_ordered.end() - 1) { | |
790 | full_location = full_location + ","; | |
791 | } | |
792 | } | |
793 | return full_location; | |
794 | } | |
7c673cae FG |
795 | |
796 | map<int, string> CrushWrapper::get_parent_hierarchy(int id) | |
797 | { | |
798 | map<int,string> parent_hierarchy; | |
799 | pair<string, string> parent_coord = get_immediate_parent(id); | |
800 | int parent_id; | |
801 | ||
802 | // get the integer type for id and create a counter from there | |
803 | int type_counter = get_bucket_type(id); | |
804 | ||
805 | // if we get a negative type then we can assume that we have an OSD | |
806 | // change behavior in get_item_type FIXME | |
807 | if (type_counter < 0) | |
808 | type_counter = 0; | |
809 | ||
810 | // read the type map and get the name of the type with the largest ID | |
811 | int high_type = 0; | |
812 | for (map<int, string>::iterator it = type_map.begin(); it != type_map.end(); ++it){ | |
813 | if ( (*it).first > high_type ) | |
814 | high_type = (*it).first; | |
815 | } | |
816 | ||
817 | parent_id = get_item_id(parent_coord.second); | |
818 | ||
819 | while (type_counter < high_type) { | |
820 | type_counter++; | |
821 | parent_hierarchy[ type_counter ] = parent_coord.first; | |
822 | ||
823 | if (type_counter < high_type){ | |
824 | // get the coordinate information for the next parent | |
825 | parent_coord = get_immediate_parent(parent_id); | |
826 | parent_id = get_item_id(parent_coord.second); | |
827 | } | |
828 | } | |
829 | ||
830 | return parent_hierarchy; | |
831 | } | |
832 | ||
833 | int CrushWrapper::get_children(int id, list<int> *children) | |
834 | { | |
835 | // leaf? | |
836 | if (id >= 0) { | |
837 | return 0; | |
838 | } | |
839 | ||
840 | crush_bucket *b = get_bucket(id); | |
841 | if (IS_ERR(b)) { | |
842 | return -ENOENT; | |
843 | } | |
844 | ||
845 | for (unsigned n=0; n<b->size; n++) { | |
846 | children->push_back(b->items[n]); | |
847 | } | |
848 | return b->size; | |
849 | } | |
850 | ||
28e407b8 AA |
851 | void CrushWrapper::get_children_of_type(int id, |
852 | int type, | |
853 | set<int> *children, | |
854 | bool exclude_shadow) const | |
855 | { | |
856 | if (id >= 0) { | |
857 | if (type == 0) { | |
858 | // want leaf? | |
859 | children->insert(id); | |
860 | } | |
861 | return; | |
862 | } | |
863 | auto b = get_bucket(id); | |
864 | if (IS_ERR(b)) { | |
865 | return; | |
866 | } | |
867 | if (b->type < type) { | |
868 | // give up | |
869 | return; | |
870 | } else if (b->type == type) { | |
871 | if (!is_shadow_item(b->id) || !exclude_shadow) { | |
872 | children->insert(b->id); | |
873 | } | |
874 | return; | |
875 | } | |
876 | for (unsigned n = 0; n < b->size; n++) { | |
877 | get_children_of_type(b->items[n], type, children, exclude_shadow); | |
878 | } | |
879 | } | |
880 | ||
94b18763 FG |
881 | int CrushWrapper::get_rule_failure_domain(int rule_id) |
882 | { | |
883 | crush_rule *rule = get_rule(rule_id); | |
884 | if (IS_ERR(rule)) { | |
885 | return -ENOENT; | |
886 | } | |
887 | int type = 0; // default to osd-level | |
888 | for (unsigned s = 0; s < rule->len; ++s) { | |
889 | if ((rule->steps[s].op == CRUSH_RULE_CHOOSE_FIRSTN || | |
890 | rule->steps[s].op == CRUSH_RULE_CHOOSE_INDEP || | |
891 | rule->steps[s].op == CRUSH_RULE_CHOOSELEAF_FIRSTN || | |
892 | rule->steps[s].op == CRUSH_RULE_CHOOSELEAF_INDEP) && | |
893 | rule->steps[s].arg2 > type) { | |
894 | type = rule->steps[s].arg2; | |
895 | } | |
896 | } | |
897 | return type; | |
898 | } | |
899 | ||
31f18b77 FG |
900 | int CrushWrapper::_get_leaves(int id, list<int> *leaves) |
901 | { | |
902 | assert(leaves); | |
7c673cae | 903 | |
31f18b77 FG |
904 | // Already leaf? |
905 | if (id >= 0) { | |
906 | leaves->push_back(id); | |
907 | return 0; | |
908 | } | |
909 | ||
910 | crush_bucket *b = get_bucket(id); | |
911 | if (IS_ERR(b)) { | |
912 | return -ENOENT; | |
913 | } | |
914 | ||
915 | for (unsigned n = 0; n < b->size; n++) { | |
916 | if (b->items[n] >= 0) { | |
917 | leaves->push_back(b->items[n]); | |
918 | } else { | |
919 | // is a bucket, do recursive call | |
920 | int r = _get_leaves(b->items[n], leaves); | |
921 | if (r < 0) { | |
922 | return r; | |
923 | } | |
924 | } | |
925 | } | |
926 | ||
927 | return 0; // all is well | |
928 | } | |
929 | ||
930 | int CrushWrapper::get_leaves(const string &name, set<int> *leaves) | |
7c673cae | 931 | { |
31f18b77 FG |
932 | assert(leaves); |
933 | leaves->clear(); | |
934 | ||
935 | if (!name_exists(name)) { | |
936 | return -ENOENT; | |
937 | } | |
938 | ||
939 | int id = get_item_id(name); | |
940 | if (id >= 0) { | |
941 | // already leaf | |
942 | leaves->insert(id); | |
943 | return 0; | |
7c673cae FG |
944 | } |
945 | ||
31f18b77 FG |
946 | list<int> unordered; |
947 | int r = _get_leaves(id, &unordered); | |
948 | if (r < 0) { | |
949 | return r; | |
950 | } | |
951 | ||
952 | for (auto &p : unordered) { | |
953 | leaves->insert(p); | |
954 | } | |
955 | ||
956 | return 0; | |
957 | } | |
958 | ||
c07f9fc5 FG |
959 | int CrushWrapper::insert_item( |
960 | CephContext *cct, int item, float weight, string name, | |
961 | const map<string,string>& loc) // typename -> bucketname | |
31f18b77 | 962 | { |
7c673cae FG |
963 | ldout(cct, 5) << "insert_item item " << item << " weight " << weight |
964 | << " name " << name << " loc " << loc << dendl; | |
965 | ||
966 | if (!is_valid_crush_name(name)) | |
967 | return -EINVAL; | |
968 | ||
969 | if (!is_valid_crush_loc(cct, loc)) | |
970 | return -EINVAL; | |
971 | ||
224ce89b WB |
972 | int r = validate_weightf(weight); |
973 | if (r < 0) { | |
974 | return r; | |
975 | } | |
976 | ||
7c673cae FG |
977 | if (name_exists(name)) { |
978 | if (get_item_id(name) != item) { | |
979 | ldout(cct, 10) << "device name '" << name << "' already exists as id " | |
980 | << get_item_id(name) << dendl; | |
981 | return -EEXIST; | |
982 | } | |
983 | } else { | |
984 | set_item_name(item, name); | |
985 | } | |
986 | ||
987 | int cur = item; | |
988 | ||
c07f9fc5 FG |
989 | // create locations if locations don't exist and add child in |
990 | // location with 0 weight the more detail in the insert_item method | |
991 | // declaration in CrushWrapper.h | |
992 | for (auto p = type_map.begin(); p != type_map.end(); ++p) { | |
7c673cae FG |
993 | // ignore device type |
994 | if (p->first == 0) | |
995 | continue; | |
996 | ||
997 | // skip types that are unspecified | |
998 | map<string,string>::const_iterator q = loc.find(p->second); | |
999 | if (q == loc.end()) { | |
c07f9fc5 FG |
1000 | ldout(cct, 2) << "warning: did not specify location for '" |
1001 | << p->second << "' level (levels are " | |
7c673cae FG |
1002 | << type_map << ")" << dendl; |
1003 | continue; | |
1004 | } | |
1005 | ||
1006 | if (!name_exists(q->second)) { | |
1007 | ldout(cct, 5) << "insert_item creating bucket " << q->second << dendl; | |
1008 | int empty = 0, newid; | |
1009 | int r = add_bucket(0, 0, | |
1010 | CRUSH_HASH_DEFAULT, p->first, 1, &cur, &empty, &newid); | |
1011 | if (r < 0) { | |
c07f9fc5 FG |
1012 | ldout(cct, 1) << "add_bucket failure error: " << cpp_strerror(r) |
1013 | << dendl; | |
7c673cae FG |
1014 | return r; |
1015 | } | |
1016 | set_item_name(newid, q->second); | |
1017 | ||
1018 | cur = newid; | |
1019 | continue; | |
1020 | } | |
1021 | ||
1022 | // add to an existing bucket | |
1023 | int id = get_item_id(q->second); | |
1024 | if (!bucket_exists(id)) { | |
1025 | ldout(cct, 1) << "insert_item doesn't have bucket " << id << dendl; | |
1026 | return -EINVAL; | |
1027 | } | |
1028 | ||
1029 | // check that we aren't creating a cycle. | |
1030 | if (subtree_contains(id, cur)) { | |
c07f9fc5 FG |
1031 | ldout(cct, 1) << "insert_item item " << cur << " already exists beneath " |
1032 | << id << dendl; | |
7c673cae FG |
1033 | return -EINVAL; |
1034 | } | |
1035 | ||
1036 | // we have done sanity check above | |
1037 | crush_bucket *b = get_bucket(id); | |
1038 | ||
1039 | if (p->first != b->type) { | |
1040 | ldout(cct, 1) << "insert_item existing bucket has type " | |
1041 | << "'" << type_map[b->type] << "' != " | |
1042 | << "'" << type_map[p->first] << "'" << dendl; | |
1043 | return -EINVAL; | |
1044 | } | |
1045 | ||
1046 | // are we forming a loop? | |
1047 | if (subtree_contains(cur, b->id)) { | |
1048 | ldout(cct, 1) << "insert_item " << cur << " already contains " << b->id | |
1049 | << "; cannot form loop" << dendl; | |
1050 | return -ELOOP; | |
1051 | } | |
1052 | ||
1053 | ldout(cct, 5) << "insert_item adding " << cur << " weight " << weight | |
1054 | << " to bucket " << id << dendl; | |
31f18b77 | 1055 | int r = bucket_add_item(b, cur, 0); |
7c673cae FG |
1056 | assert (!r); |
1057 | break; | |
1058 | } | |
1059 | ||
1060 | // adjust the item's weight in location | |
c07f9fc5 | 1061 | if (adjust_item_weightf_in_loc(cct, item, weight, loc) > 0) { |
7c673cae FG |
1062 | if (item >= crush->max_devices) { |
1063 | crush->max_devices = item + 1; | |
c07f9fc5 FG |
1064 | ldout(cct, 5) << "insert_item max_devices now " << crush->max_devices |
1065 | << dendl; | |
1066 | } | |
1067 | r = rebuild_roots_with_classes(); | |
1068 | if (r < 0) { | |
1069 | ldout(cct, 0) << __func__ << " unable to rebuild roots with classes: " | |
1070 | << cpp_strerror(r) << dendl; | |
1071 | return r; | |
7c673cae FG |
1072 | } |
1073 | return 0; | |
1074 | } | |
1075 | ||
c07f9fc5 FG |
1076 | ldout(cct, 1) << "error: didn't find anywhere to add item " << item |
1077 | << " in " << loc << dendl; | |
7c673cae FG |
1078 | return -EINVAL; |
1079 | } | |
1080 | ||
c07f9fc5 FG |
1081 | |
1082 | int CrushWrapper::move_bucket( | |
1083 | CephContext *cct, int id, const map<string,string>& loc) | |
7c673cae | 1084 | { |
7c673cae FG |
1085 | // sorry this only works for buckets |
1086 | if (id >= 0) | |
1087 | return -EINVAL; | |
1088 | ||
1089 | if (!item_exists(id)) | |
1090 | return -ENOENT; | |
1091 | ||
1092 | // get the name of the bucket we are trying to move for later | |
1093 | string id_name = get_item_name(id); | |
1094 | ||
1095 | // detach the bucket | |
1096 | int bucket_weight = detach_bucket(cct, id); | |
1097 | ||
1098 | // insert the bucket back into the hierarchy | |
1099 | return insert_item(cct, id, bucket_weight / (float)0x10000, id_name, loc); | |
1100 | } | |
1101 | ||
c07f9fc5 FG |
1102 | int CrushWrapper::detach_bucket(CephContext *cct, int item) |
1103 | { | |
1104 | if (!crush) | |
1105 | return (-EINVAL); | |
1106 | ||
1107 | if (item >= 0) | |
1108 | return (-EINVAL); | |
1109 | ||
1110 | // check that the bucket that we want to detach exists | |
1111 | assert(bucket_exists(item)); | |
1112 | ||
1113 | // get the bucket's weight | |
1114 | crush_bucket *b = get_bucket(item); | |
1115 | unsigned bucket_weight = b->weight; | |
1116 | ||
1117 | // get where the bucket is located | |
1118 | pair<string, string> bucket_location = get_immediate_parent(item); | |
1119 | ||
1120 | // get the id of the parent bucket | |
1121 | int parent_id = get_item_id(bucket_location.second); | |
1122 | ||
1123 | // get the parent bucket | |
1124 | crush_bucket *parent_bucket = get_bucket(parent_id); | |
1125 | ||
1126 | if (!IS_ERR(parent_bucket)) { | |
1127 | // zero out the bucket weight | |
1128 | bucket_adjust_item_weight(cct, parent_bucket, item, 0); | |
1129 | adjust_item_weight(cct, parent_bucket->id, parent_bucket->weight); | |
1130 | for (auto& p : choose_args) { | |
1131 | // weight down each weight-set to 0 before we remove the item | |
1132 | vector<int> weightv(get_choose_args_positions(p.second), 0); | |
1133 | choose_args_adjust_item_weight(cct, p.second, item, weightv, nullptr); | |
1134 | } | |
1135 | ||
1136 | // remove the bucket from the parent | |
1137 | bucket_remove_item(parent_bucket, item); | |
1138 | } else if (PTR_ERR(parent_bucket) != -ENOENT) { | |
1139 | return PTR_ERR(parent_bucket); | |
1140 | } | |
1141 | ||
1142 | // check that we're happy | |
1143 | int test_weight = 0; | |
1144 | map<string,string> test_location; | |
1145 | test_location[ bucket_location.first ] = (bucket_location.second); | |
1146 | ||
1147 | bool successful_detach = !(check_item_loc(cct, item, test_location, | |
1148 | &test_weight)); | |
1149 | assert(successful_detach); | |
1150 | assert(test_weight == 0); | |
1151 | ||
1152 | return bucket_weight; | |
1153 | } | |
1154 | ||
31f18b77 | 1155 | int CrushWrapper::swap_bucket(CephContext *cct, int src, int dst) |
7c673cae | 1156 | { |
31f18b77 FG |
1157 | if (src >= 0 || dst >= 0) |
1158 | return -EINVAL; | |
1159 | if (!item_exists(src) || !item_exists(dst)) | |
1160 | return -EINVAL; | |
1161 | crush_bucket *a = get_bucket(src); | |
1162 | crush_bucket *b = get_bucket(dst); | |
1163 | unsigned aw = a->weight; | |
1164 | unsigned bw = b->weight; | |
1165 | ||
1166 | // swap weights | |
1167 | adjust_item_weight(cct, a->id, bw); | |
1168 | adjust_item_weight(cct, b->id, aw); | |
1169 | ||
1170 | // swap items | |
1171 | map<int,unsigned> tmp; | |
1172 | unsigned as = a->size; | |
1173 | unsigned bs = b->size; | |
1174 | for (unsigned i = 0; i < as; ++i) { | |
1175 | int item = a->items[0]; | |
1176 | int itemw = crush_get_bucket_item_weight(a, 0); | |
1177 | tmp[item] = itemw; | |
1178 | bucket_remove_item(a, item); | |
1179 | } | |
1180 | assert(a->size == 0); | |
1181 | assert(b->size == bs); | |
1182 | for (unsigned i = 0; i < bs; ++i) { | |
1183 | int item = b->items[0]; | |
1184 | int itemw = crush_get_bucket_item_weight(b, 0); | |
1185 | bucket_remove_item(b, item); | |
1186 | bucket_add_item(a, item, itemw); | |
1187 | } | |
1188 | assert(a->size == bs); | |
1189 | assert(b->size == 0); | |
1190 | for (auto t : tmp) { | |
1191 | bucket_add_item(b, t.first, t.second); | |
1192 | } | |
1193 | assert(a->size == bs); | |
1194 | assert(b->size == as); | |
1195 | ||
1196 | // swap names | |
1197 | swap_names(src, dst); | |
b5b8bbf5 | 1198 | return rebuild_roots_with_classes(); |
31f18b77 | 1199 | } |
7c673cae | 1200 | |
c07f9fc5 FG |
1201 | int CrushWrapper::link_bucket( |
1202 | CephContext *cct, int id, const map<string,string>& loc) | |
31f18b77 | 1203 | { |
7c673cae FG |
1204 | // sorry this only works for buckets |
1205 | if (id >= 0) | |
1206 | return -EINVAL; | |
1207 | ||
1208 | if (!item_exists(id)) | |
1209 | return -ENOENT; | |
1210 | ||
1211 | // get the name of the bucket we are trying to move for later | |
1212 | string id_name = get_item_name(id); | |
1213 | ||
1214 | crush_bucket *b = get_bucket(id); | |
1215 | unsigned bucket_weight = b->weight; | |
1216 | ||
1217 | return insert_item(cct, id, bucket_weight / (float)0x10000, id_name, loc); | |
1218 | } | |
1219 | ||
c07f9fc5 FG |
1220 | int CrushWrapper::create_or_move_item( |
1221 | CephContext *cct, int item, float weight, string name, | |
1222 | const map<string,string>& loc) // typename -> bucketname | |
7c673cae | 1223 | { |
7c673cae FG |
1224 | int ret = 0; |
1225 | int old_iweight; | |
1226 | ||
1227 | if (!is_valid_crush_name(name)) | |
1228 | return -EINVAL; | |
1229 | ||
1230 | if (check_item_loc(cct, item, loc, &old_iweight)) { | |
c07f9fc5 FG |
1231 | ldout(cct, 5) << "create_or_move_item " << item << " already at " << loc |
1232 | << dendl; | |
7c673cae FG |
1233 | } else { |
1234 | if (_search_item_exists(item)) { | |
1235 | weight = get_item_weightf(item); | |
c07f9fc5 FG |
1236 | ldout(cct, 10) << "create_or_move_item " << item |
1237 | << " exists with weight " << weight << dendl; | |
7c673cae FG |
1238 | remove_item(cct, item, true); |
1239 | } | |
c07f9fc5 FG |
1240 | ldout(cct, 5) << "create_or_move_item adding " << item |
1241 | << " weight " << weight | |
7c673cae FG |
1242 | << " at " << loc << dendl; |
1243 | ret = insert_item(cct, item, weight, name, loc); | |
1244 | if (ret == 0) | |
1245 | ret = 1; // changed | |
1246 | } | |
1247 | return ret; | |
1248 | } | |
1249 | ||
c07f9fc5 FG |
1250 | int CrushWrapper::update_item( |
1251 | CephContext *cct, int item, float weight, string name, | |
1252 | const map<string,string>& loc) // typename -> bucketname | |
7c673cae | 1253 | { |
7c673cae FG |
1254 | ldout(cct, 5) << "update_item item " << item << " weight " << weight |
1255 | << " name " << name << " loc " << loc << dendl; | |
1256 | int ret = 0; | |
1257 | ||
1258 | if (!is_valid_crush_name(name)) | |
1259 | return -EINVAL; | |
1260 | ||
1261 | if (!is_valid_crush_loc(cct, loc)) | |
1262 | return -EINVAL; | |
1263 | ||
224ce89b WB |
1264 | ret = validate_weightf(weight); |
1265 | if (ret < 0) { | |
1266 | return ret; | |
1267 | } | |
1268 | ||
7c673cae FG |
1269 | // compare quantized (fixed-point integer) weights! |
1270 | int iweight = (int)(weight * (float)0x10000); | |
1271 | int old_iweight; | |
1272 | if (check_item_loc(cct, item, loc, &old_iweight)) { | |
1273 | ldout(cct, 5) << "update_item " << item << " already at " << loc << dendl; | |
1274 | if (old_iweight != iweight) { | |
1275 | ldout(cct, 5) << "update_item " << item << " adjusting weight " | |
c07f9fc5 FG |
1276 | << ((float)old_iweight/(float)0x10000) << " -> " << weight |
1277 | << dendl; | |
7c673cae FG |
1278 | adjust_item_weight_in_loc(cct, item, iweight, loc); |
1279 | ret = 1; | |
1280 | } | |
1281 | if (get_item_name(item) != name) { | |
c07f9fc5 FG |
1282 | ldout(cct, 5) << "update_item setting " << item << " name to " << name |
1283 | << dendl; | |
7c673cae FG |
1284 | set_item_name(item, name); |
1285 | ret = 1; | |
1286 | } | |
1287 | } else { | |
1288 | if (item_exists(item)) { | |
1289 | remove_item(cct, item, true); | |
1290 | } | |
1291 | ldout(cct, 5) << "update_item adding " << item << " weight " << weight | |
1292 | << " at " << loc << dendl; | |
1293 | ret = insert_item(cct, item, weight, name, loc); | |
1294 | if (ret == 0) | |
1295 | ret = 1; // changed | |
1296 | } | |
1297 | return ret; | |
1298 | } | |
1299 | ||
1300 | int CrushWrapper::get_item_weight(int id) const | |
1301 | { | |
1302 | for (int bidx = 0; bidx < crush->max_buckets; bidx++) { | |
1303 | crush_bucket *b = crush->buckets[bidx]; | |
1304 | if (b == NULL) | |
1305 | continue; | |
1306 | if (b->id == id) | |
1307 | return b->weight; | |
1308 | for (unsigned i = 0; i < b->size; i++) | |
1309 | if (b->items[i] == id) | |
1310 | return crush_get_bucket_item_weight(b, i); | |
1311 | } | |
1312 | return -ENOENT; | |
1313 | } | |
1314 | ||
1315 | int CrushWrapper::get_item_weight_in_loc(int id, const map<string,string> &loc) | |
1316 | { | |
1317 | for (map<string,string>::const_iterator l = loc.begin(); l != loc.end(); ++l) { | |
1318 | ||
1319 | int bid = get_item_id(l->second); | |
1320 | if (!bucket_exists(bid)) | |
1321 | continue; | |
1322 | crush_bucket *b = get_bucket(bid); | |
1323 | for (unsigned int i = 0; i < b->size; i++) { | |
1324 | if (b->items[i] == id) { | |
1325 | return crush_get_bucket_item_weight(b, i); | |
1326 | } | |
1327 | } | |
1328 | } | |
1329 | return -ENOENT; | |
1330 | } | |
1331 | ||
1332 | int CrushWrapper::adjust_item_weight(CephContext *cct, int id, int weight) | |
1333 | { | |
1334 | ldout(cct, 5) << "adjust_item_weight " << id << " weight " << weight << dendl; | |
1335 | int changed = 0; | |
1336 | for (int bidx = 0; bidx < crush->max_buckets; bidx++) { | |
1337 | crush_bucket *b = crush->buckets[bidx]; | |
1338 | if (b == 0) | |
1339 | continue; | |
1340 | for (unsigned i = 0; i < b->size; i++) { | |
1341 | if (b->items[i] == id) { | |
31f18b77 | 1342 | int diff = bucket_adjust_item_weight(cct, b, id, weight); |
c07f9fc5 FG |
1343 | ldout(cct, 5) << "adjust_item_weight " << id << " diff " << diff |
1344 | << " in bucket " << bidx << dendl; | |
7c673cae FG |
1345 | adjust_item_weight(cct, -1 - bidx, b->weight); |
1346 | changed++; | |
1347 | } | |
1348 | } | |
1349 | } | |
1350 | if (!changed) | |
1351 | return -ENOENT; | |
1352 | return changed; | |
1353 | } | |
1354 | ||
1355 | int CrushWrapper::adjust_item_weight_in_loc(CephContext *cct, int id, int weight, const map<string,string>& loc) | |
1356 | { | |
c07f9fc5 FG |
1357 | ldout(cct, 5) << "adjust_item_weight_in_loc " << id << " weight " << weight |
1358 | << " in " << loc << dendl; | |
7c673cae FG |
1359 | int changed = 0; |
1360 | ||
c07f9fc5 | 1361 | for (auto l = loc.begin(); l != loc.end(); ++l) { |
7c673cae FG |
1362 | int bid = get_item_id(l->second); |
1363 | if (!bucket_exists(bid)) | |
1364 | continue; | |
1365 | crush_bucket *b = get_bucket(bid); | |
1366 | for (unsigned int i = 0; i < b->size; i++) { | |
1367 | if (b->items[i] == id) { | |
31f18b77 | 1368 | int diff = bucket_adjust_item_weight(cct, b, id, weight); |
c07f9fc5 FG |
1369 | ldout(cct, 5) << "adjust_item_weight_in_loc " << id << " diff " << diff |
1370 | << " in bucket " << bid << dendl; | |
7c673cae FG |
1371 | adjust_item_weight(cct, bid, b->weight); |
1372 | changed++; | |
1373 | } | |
1374 | } | |
1375 | } | |
1376 | if (!changed) | |
1377 | return -ENOENT; | |
1378 | return changed; | |
1379 | } | |
1380 | ||
1381 | int CrushWrapper::adjust_subtree_weight(CephContext *cct, int id, int weight) | |
1382 | { | |
1383 | ldout(cct, 5) << __func__ << " " << id << " weight " << weight << dendl; | |
1384 | crush_bucket *b = get_bucket(id); | |
1385 | if (IS_ERR(b)) | |
1386 | return PTR_ERR(b); | |
1387 | int changed = 0; | |
1388 | list<crush_bucket*> q; | |
1389 | q.push_back(b); | |
1390 | while (!q.empty()) { | |
1391 | b = q.front(); | |
1392 | q.pop_front(); | |
1393 | int local_changed = 0; | |
1394 | for (unsigned i=0; i<b->size; ++i) { | |
1395 | int n = b->items[i]; | |
1396 | if (n >= 0) { | |
31f18b77 | 1397 | bucket_adjust_item_weight(cct, b, n, weight); |
7c673cae FG |
1398 | ++changed; |
1399 | ++local_changed; | |
1400 | } else { | |
1401 | crush_bucket *sub = get_bucket(n); | |
1402 | if (IS_ERR(sub)) | |
1403 | continue; | |
1404 | q.push_back(sub); | |
1405 | } | |
1406 | } | |
1407 | if (local_changed) { | |
1408 | adjust_item_weight(cct, b->id, b->weight); | |
1409 | } | |
1410 | } | |
1411 | return changed; | |
1412 | } | |
1413 | ||
1414 | bool CrushWrapper::check_item_present(int id) const | |
1415 | { | |
1416 | bool found = false; | |
1417 | ||
1418 | for (int bidx = 0; bidx < crush->max_buckets; bidx++) { | |
1419 | crush_bucket *b = crush->buckets[bidx]; | |
1420 | if (b == 0) | |
1421 | continue; | |
1422 | for (unsigned i = 0; i < b->size; i++) | |
1423 | if (b->items[i] == id) | |
1424 | found = true; | |
1425 | } | |
1426 | return found; | |
1427 | } | |
1428 | ||
1429 | ||
1430 | pair<string,string> CrushWrapper::get_immediate_parent(int id, int *_ret) | |
1431 | { | |
1432 | ||
1433 | for (int bidx = 0; bidx < crush->max_buckets; bidx++) { | |
1434 | crush_bucket *b = crush->buckets[bidx]; | |
1435 | if (b == 0) | |
1436 | continue; | |
c07f9fc5 | 1437 | if (is_shadow_item(b->id)) |
224ce89b | 1438 | continue; |
7c673cae FG |
1439 | for (unsigned i = 0; i < b->size; i++) |
1440 | if (b->items[i] == id) { | |
1441 | string parent_id = name_map[b->id]; | |
1442 | string parent_bucket_type = type_map[b->type]; | |
1443 | if (_ret) | |
1444 | *_ret = 0; | |
1445 | return make_pair(parent_bucket_type, parent_id); | |
1446 | } | |
1447 | } | |
1448 | ||
1449 | if (_ret) | |
1450 | *_ret = -ENOENT; | |
1451 | ||
1452 | return pair<string, string>(); | |
1453 | } | |
1454 | ||
1455 | int CrushWrapper::get_immediate_parent_id(int id, int *parent) const | |
1456 | { | |
1457 | for (int bidx = 0; bidx < crush->max_buckets; bidx++) { | |
1458 | crush_bucket *b = crush->buckets[bidx]; | |
1459 | if (b == 0) | |
1460 | continue; | |
c07f9fc5 | 1461 | if (is_shadow_item(b->id)) |
224ce89b | 1462 | continue; |
7c673cae FG |
1463 | for (unsigned i = 0; i < b->size; i++) { |
1464 | if (b->items[i] == id) { | |
1465 | *parent = b->id; | |
1466 | return 0; | |
1467 | } | |
1468 | } | |
1469 | } | |
1470 | return -ENOENT; | |
1471 | } | |
1472 | ||
28e407b8 | 1473 | int CrushWrapper::get_parent_of_type(int item, int type, int rule) const |
31f18b77 | 1474 | { |
28e407b8 AA |
1475 | if (rule < 0) { |
1476 | // no rule specified | |
1477 | do { | |
1478 | int r = get_immediate_parent_id(item, &item); | |
1479 | if (r < 0) { | |
1480 | return 0; | |
1481 | } | |
1482 | } while (get_bucket_type(item) != type); | |
1483 | return item; | |
1484 | } | |
1485 | set<int> roots; | |
1486 | find_takes_by_rule(rule, &roots); | |
1487 | for (auto root : roots) { | |
1488 | set<int> candidates; | |
1489 | get_children_of_type(root, type, &candidates, false); | |
1490 | for (auto candidate : candidates) { | |
1491 | if (subtree_contains(candidate, item)) { | |
1492 | // note that here we assure that no two different buckets | |
1493 | // from a single crush rule will share a same device, | |
1494 | // which should generally be true. | |
1495 | return candidate; | |
1496 | } | |
31f18b77 | 1497 | } |
28e407b8 AA |
1498 | } |
1499 | return 0; // not found | |
31f18b77 FG |
1500 | } |
1501 | ||
35e4c445 FG |
1502 | int CrushWrapper::rename_class(const string& srcname, const string& dstname) |
1503 | { | |
1504 | auto i = class_rname.find(srcname); | |
1505 | if (i == class_rname.end()) | |
1506 | return -ENOENT; | |
1507 | auto j = class_rname.find(dstname); | |
1508 | if (j != class_rname.end()) | |
1509 | return -EEXIST; | |
1510 | ||
1511 | int class_id = i->second; | |
1512 | assert(class_name.count(class_id)); | |
1513 | // rename any shadow buckets of old class name | |
1514 | for (auto &it: class_map) { | |
1515 | if (it.first < 0 && it.second == class_id) { | |
1516 | string old_name = get_item_name(it.first); | |
1517 | size_t pos = old_name.find("~"); | |
1518 | assert(pos != string::npos); | |
1519 | string name_no_class = old_name.substr(0, pos); | |
1520 | string old_class_name = old_name.substr(pos + 1); | |
1521 | assert(old_class_name == srcname); | |
1522 | string new_name = name_no_class + "~" + dstname; | |
1523 | // we do not use set_item_name | |
1524 | // because the name is intentionally invalid | |
1525 | name_map[it.first] = new_name; | |
1526 | have_rmaps = false; | |
1527 | } | |
1528 | } | |
1529 | ||
1530 | // rename class | |
1531 | class_rname.erase(srcname); | |
1532 | class_name.erase(class_id); | |
1533 | class_rname[dstname] = class_id; | |
1534 | class_name[class_id] = dstname; | |
1535 | return 0; | |
1536 | } | |
1537 | ||
d2e6a577 FG |
1538 | int CrushWrapper::populate_classes( |
1539 | const std::map<int32_t, map<int32_t, int32_t>>& old_class_bucket) | |
c07f9fc5 | 1540 | { |
d2e6a577 FG |
1541 | // build set of previous used shadow ids |
1542 | set<int32_t> used_ids; | |
1543 | for (auto& p : old_class_bucket) { | |
1544 | for (auto& q : p.second) { | |
1545 | used_ids.insert(q.second); | |
c07f9fc5 | 1546 | } |
c07f9fc5 | 1547 | } |
35e4c445 FG |
1548 | // accumulate weight values for each carg and bucket as we go. because it is |
1549 | // depth first, we will have the nested bucket weights we need when we | |
1550 | // finish constructing the containing buckets. | |
28e407b8 | 1551 | map<int,map<int,vector<int>>> cmap_item_weight; // cargs -> bno -> [bucket weight for each position] |
7c673cae | 1552 | set<int> roots; |
3efd9988 | 1553 | find_nonshadow_roots(&roots); |
7c673cae FG |
1554 | for (auto &r : roots) { |
1555 | if (r >= 0) | |
1556 | continue; | |
7c673cae FG |
1557 | for (auto &c : class_name) { |
1558 | int clone; | |
d2e6a577 | 1559 | int res = device_class_clone(r, c.first, old_class_bucket, used_ids, |
35e4c445 | 1560 | &clone, &cmap_item_weight); |
7c673cae FG |
1561 | if (res < 0) |
1562 | return res; | |
1563 | } | |
1564 | } | |
1565 | return 0; | |
1566 | } | |
1567 | ||
35e4c445 | 1568 | int CrushWrapper::trim_roots_with_class() |
7c673cae FG |
1569 | { |
1570 | set<int> roots; | |
3efd9988 | 1571 | find_shadow_roots(&roots); |
7c673cae FG |
1572 | for (auto &r : roots) { |
1573 | if (r >= 0) | |
1574 | continue; | |
35e4c445 | 1575 | int res = remove_root(r); |
7c673cae FG |
1576 | if (res) |
1577 | return res; | |
1578 | } | |
1579 | // there is no need to reweight because we only remove from the | |
1580 | // root and down | |
1581 | return 0; | |
1582 | } | |
1583 | ||
224ce89b WB |
1584 | int32_t CrushWrapper::_alloc_class_id() const { |
1585 | if (class_name.empty()) { | |
1586 | return 0; | |
1587 | } | |
1588 | int32_t class_id = class_name.rbegin()->first + 1; | |
1589 | if (class_id >= 0) { | |
1590 | return class_id; | |
1591 | } | |
1592 | // wrapped, pick a random start and do exhaustive search | |
1593 | uint32_t upperlimit = numeric_limits<int32_t>::max(); | |
1594 | upperlimit++; | |
1595 | class_id = rand() % upperlimit; | |
1596 | const auto start = class_id; | |
1597 | do { | |
1598 | if (!class_name.count(class_id)) { | |
1599 | return class_id; | |
1600 | } else { | |
1601 | class_id++; | |
1602 | if (class_id < 0) { | |
1603 | class_id = 0; | |
1604 | } | |
1605 | } | |
1606 | } while (class_id != start); | |
1607 | assert(0 == "no available class id"); | |
1608 | } | |
1609 | ||
7c673cae FG |
1610 | void CrushWrapper::reweight(CephContext *cct) |
1611 | { | |
1612 | set<int> roots; | |
3efd9988 | 1613 | find_roots(&roots); |
7c673cae FG |
1614 | for (set<int>::iterator p = roots.begin(); p != roots.end(); ++p) { |
1615 | if (*p >= 0) | |
1616 | continue; | |
1617 | crush_bucket *b = get_bucket(*p); | |
1618 | ldout(cct, 5) << "reweight bucket " << *p << dendl; | |
1619 | int r = crush_reweight_bucket(crush, b); | |
1620 | assert(r == 0); | |
1621 | } | |
1622 | } | |
1623 | ||
31f18b77 FG |
1624 | int CrushWrapper::add_simple_rule_at( |
1625 | string name, string root_name, | |
1626 | string failure_domain_name, | |
224ce89b | 1627 | string device_class, |
31f18b77 | 1628 | string mode, int rule_type, |
224ce89b WB |
1629 | int rno, |
1630 | ostream *err) | |
7c673cae FG |
1631 | { |
1632 | if (rule_exists(name)) { | |
1633 | if (err) | |
1634 | *err << "rule " << name << " exists"; | |
1635 | return -EEXIST; | |
1636 | } | |
1637 | if (rno >= 0) { | |
1638 | if (rule_exists(rno)) { | |
1639 | if (err) | |
1640 | *err << "rule with ruleno " << rno << " exists"; | |
1641 | return -EEXIST; | |
1642 | } | |
1643 | if (ruleset_exists(rno)) { | |
1644 | if (err) | |
1645 | *err << "ruleset " << rno << " exists"; | |
1646 | return -EEXIST; | |
1647 | } | |
1648 | } else { | |
1649 | for (rno = 0; rno < get_max_rules(); rno++) { | |
1650 | if (!rule_exists(rno) && !ruleset_exists(rno)) | |
1651 | break; | |
1652 | } | |
1653 | } | |
1654 | if (!name_exists(root_name)) { | |
1655 | if (err) | |
1656 | *err << "root item " << root_name << " does not exist"; | |
1657 | return -ENOENT; | |
1658 | } | |
1659 | int root = get_item_id(root_name); | |
1660 | int type = 0; | |
1661 | if (failure_domain_name.length()) { | |
1662 | type = get_type_id(failure_domain_name); | |
1663 | if (type < 0) { | |
1664 | if (err) | |
1665 | *err << "unknown type " << failure_domain_name; | |
1666 | return -EINVAL; | |
1667 | } | |
1668 | } | |
224ce89b WB |
1669 | if (device_class.size()) { |
1670 | if (!class_exists(device_class)) { | |
1671 | if (err) | |
1672 | *err << "device class " << device_class << " does not exist"; | |
1673 | return -EINVAL; | |
1674 | } | |
1675 | int c = get_class_id(device_class); | |
1676 | if (class_bucket.count(root) == 0 || | |
1677 | class_bucket[root].count(c) == 0) { | |
1678 | if (err) | |
1679 | *err << "root " << root_name << " has no devices with class " | |
1680 | << device_class; | |
1681 | return -EINVAL; | |
1682 | } | |
1683 | root = class_bucket[root][c]; | |
1684 | } | |
7c673cae FG |
1685 | if (mode != "firstn" && mode != "indep") { |
1686 | if (err) | |
1687 | *err << "unknown mode " << mode; | |
1688 | return -EINVAL; | |
1689 | } | |
1690 | ||
1691 | int steps = 3; | |
1692 | if (mode == "indep") | |
1693 | steps = 5; | |
1694 | int min_rep = mode == "firstn" ? 1 : 3; | |
1695 | int max_rep = mode == "firstn" ? 10 : 20; | |
1696 | //set the ruleset the same as rule_id(rno) | |
1697 | crush_rule *rule = crush_make_rule(steps, rno, rule_type, min_rep, max_rep); | |
1698 | assert(rule); | |
1699 | int step = 0; | |
1700 | if (mode == "indep") { | |
1701 | crush_rule_set_step(rule, step++, CRUSH_RULE_SET_CHOOSELEAF_TRIES, 5, 0); | |
1702 | crush_rule_set_step(rule, step++, CRUSH_RULE_SET_CHOOSE_TRIES, 100, 0); | |
1703 | } | |
1704 | crush_rule_set_step(rule, step++, CRUSH_RULE_TAKE, root, 0); | |
1705 | if (type) | |
1706 | crush_rule_set_step(rule, step++, | |
1707 | mode == "firstn" ? CRUSH_RULE_CHOOSELEAF_FIRSTN : | |
1708 | CRUSH_RULE_CHOOSELEAF_INDEP, | |
1709 | CRUSH_CHOOSE_N, | |
1710 | type); | |
1711 | else | |
1712 | crush_rule_set_step(rule, step++, | |
1713 | mode == "firstn" ? CRUSH_RULE_CHOOSE_FIRSTN : | |
1714 | CRUSH_RULE_CHOOSE_INDEP, | |
1715 | CRUSH_CHOOSE_N, | |
1716 | 0); | |
1717 | crush_rule_set_step(rule, step++, CRUSH_RULE_EMIT, 0, 0); | |
1718 | ||
1719 | int ret = crush_add_rule(crush, rule, rno); | |
1720 | if(ret < 0) { | |
1721 | *err << "failed to add rule " << rno << " because " << cpp_strerror(ret); | |
1722 | return ret; | |
1723 | } | |
1724 | set_rule_name(rno, name); | |
1725 | have_rmaps = false; | |
1726 | return rno; | |
1727 | } | |
1728 | ||
31f18b77 FG |
1729 | int CrushWrapper::add_simple_rule( |
1730 | string name, string root_name, | |
1731 | string failure_domain_name, | |
224ce89b | 1732 | string device_class, |
31f18b77 FG |
1733 | string mode, int rule_type, |
1734 | ostream *err) | |
7c673cae | 1735 | { |
224ce89b WB |
1736 | return add_simple_rule_at(name, root_name, failure_domain_name, device_class, |
1737 | mode, | |
31f18b77 | 1738 | rule_type, -1, err); |
7c673cae FG |
1739 | } |
1740 | ||
3efd9988 FG |
1741 | float CrushWrapper::_get_take_weight_osd_map(int root, |
1742 | map<int,float> *pmap) const | |
1743 | { | |
1744 | float sum = 0.0; | |
1745 | list<int> q; | |
1746 | q.push_back(root); | |
1747 | //breadth first iterate the OSD tree | |
1748 | while (!q.empty()) { | |
1749 | int bno = q.front(); | |
1750 | q.pop_front(); | |
1751 | crush_bucket *b = crush->buckets[-1-bno]; | |
1752 | assert(b); | |
1753 | for (unsigned j=0; j<b->size; ++j) { | |
1754 | int item_id = b->items[j]; | |
1755 | if (item_id >= 0) { //it's an OSD | |
1756 | float w = crush_get_bucket_item_weight(b, j); | |
1757 | (*pmap)[item_id] = w; | |
1758 | sum += w; | |
1759 | } else { //not an OSD, expand the child later | |
1760 | q.push_back(item_id); | |
1761 | } | |
1762 | } | |
1763 | } | |
1764 | return sum; | |
1765 | } | |
1766 | ||
1767 | void CrushWrapper::_normalize_weight_map(float sum, | |
1768 | const map<int,float>& m, | |
1769 | map<int,float> *pmap) const | |
1770 | { | |
1771 | for (auto& p : m) { | |
1772 | map<int,float>::iterator q = pmap->find(p.first); | |
1773 | if (q == pmap->end()) { | |
1774 | (*pmap)[p.first] = p.second / sum; | |
1775 | } else { | |
1776 | q->second += p.second / sum; | |
1777 | } | |
1778 | } | |
1779 | } | |
1780 | ||
1781 | int CrushWrapper::get_take_weight_osd_map(int root, map<int,float> *pmap) const | |
1782 | { | |
1783 | map<int,float> m; | |
1784 | float sum = _get_take_weight_osd_map(root, &m); | |
1785 | _normalize_weight_map(sum, m, pmap); | |
1786 | return 0; | |
1787 | } | |
1788 | ||
1789 | int CrushWrapper::get_rule_weight_osd_map(unsigned ruleno, | |
1790 | map<int,float> *pmap) const | |
7c673cae FG |
1791 | { |
1792 | if (ruleno >= crush->max_rules) | |
1793 | return -ENOENT; | |
1794 | if (crush->rules[ruleno] == NULL) | |
1795 | return -ENOENT; | |
1796 | crush_rule *rule = crush->rules[ruleno]; | |
1797 | ||
1798 | // build a weight map for each TAKE in the rule, and then merge them | |
31f18b77 FG |
1799 | |
1800 | // FIXME: if there are multiple takes that place a different number of | |
1801 | // objects we do not take that into account. (Also, note that doing this | |
1802 | // right is also a function of the pool, since the crush rule | |
1803 | // might choose 2 + choose 2 but pool size may only be 3.) | |
7c673cae FG |
1804 | for (unsigned i=0; i<rule->len; ++i) { |
1805 | map<int,float> m; | |
1806 | float sum = 0; | |
1807 | if (rule->steps[i].op == CRUSH_RULE_TAKE) { | |
1808 | int n = rule->steps[i].arg1; | |
1809 | if (n >= 0) { | |
1810 | m[n] = 1.0; | |
1811 | sum = 1.0; | |
1812 | } else { | |
3efd9988 | 1813 | sum += _get_take_weight_osd_map(n, &m); |
7c673cae FG |
1814 | } |
1815 | } | |
3efd9988 | 1816 | _normalize_weight_map(sum, m, pmap); |
7c673cae FG |
1817 | } |
1818 | ||
1819 | return 0; | |
1820 | } | |
1821 | ||
1822 | int CrushWrapper::remove_rule(int ruleno) | |
1823 | { | |
1824 | if (ruleno >= (int)crush->max_rules) | |
1825 | return -ENOENT; | |
1826 | if (crush->rules[ruleno] == NULL) | |
1827 | return -ENOENT; | |
1828 | crush_destroy_rule(crush->rules[ruleno]); | |
1829 | crush->rules[ruleno] = NULL; | |
1830 | rule_name_map.erase(ruleno); | |
1831 | have_rmaps = false; | |
b5b8bbf5 | 1832 | return rebuild_roots_with_classes(); |
7c673cae FG |
1833 | } |
1834 | ||
31f18b77 FG |
1835 | int CrushWrapper::bucket_adjust_item_weight(CephContext *cct, crush_bucket *bucket, int item, int weight) |
1836 | { | |
1837 | if (cct->_conf->osd_crush_update_weight_set) { | |
1838 | unsigned position; | |
1839 | for (position = 0; position < bucket->size; position++) | |
1840 | if (bucket->items[position] == item) | |
1841 | break; | |
1842 | assert(position != bucket->size); | |
b5b8bbf5 FG |
1843 | for (auto &w : choose_args) { |
1844 | crush_choose_arg_map &arg_map = w.second; | |
31f18b77 | 1845 | crush_choose_arg *arg = &arg_map.args[-1-bucket->id]; |
28e407b8 | 1846 | for (__u32 j = 0; j < arg->weight_set_positions; j++) { |
31f18b77 FG |
1847 | crush_weight_set *weight_set = &arg->weight_set[j]; |
1848 | weight_set->weights[position] = weight; | |
1849 | } | |
1850 | } | |
1851 | } | |
1852 | return crush_bucket_adjust_item_weight(crush, bucket, item, weight); | |
1853 | } | |
1854 | ||
c07f9fc5 FG |
1855 | int CrushWrapper::add_bucket( |
1856 | int bucketno, int alg, int hash, int type, int size, | |
1857 | int *items, int *weights, int *idout) | |
1858 | { | |
1859 | if (alg == 0) { | |
1860 | alg = get_default_bucket_alg(); | |
1861 | if (alg == 0) | |
1862 | return -EINVAL; | |
1863 | } | |
1864 | crush_bucket *b = crush_make_bucket(crush, alg, hash, type, size, items, | |
1865 | weights); | |
1866 | assert(b); | |
b5b8bbf5 | 1867 | assert(idout); |
c07f9fc5 | 1868 | int r = crush_add_bucket(crush, bucketno, b, idout); |
b5b8bbf5 | 1869 | int pos = -1 - *idout; |
c07f9fc5 FG |
1870 | for (auto& p : choose_args) { |
1871 | crush_choose_arg_map& cmap = p.second; | |
1872 | if (cmap.args) { | |
b5b8bbf5 | 1873 | if ((int)cmap.size <= pos) { |
c07f9fc5 FG |
1874 | cmap.args = (crush_choose_arg*)realloc( |
1875 | cmap.args, | |
b5b8bbf5 FG |
1876 | sizeof(crush_choose_arg) * (pos + 1)); |
1877 | assert(cmap.args); | |
c07f9fc5 | 1878 | memset(&cmap.args[cmap.size], 0, |
b5b8bbf5 FG |
1879 | sizeof(crush_choose_arg) * (pos + 1 - cmap.size)); |
1880 | cmap.size = pos + 1; | |
c07f9fc5 FG |
1881 | } |
1882 | } else { | |
1883 | cmap.args = (crush_choose_arg*)calloc(sizeof(crush_choose_arg), | |
b5b8bbf5 FG |
1884 | pos + 1); |
1885 | assert(cmap.args); | |
1886 | cmap.size = pos + 1; | |
c07f9fc5 FG |
1887 | } |
1888 | if (size > 0) { | |
1889 | int positions = get_choose_args_positions(cmap); | |
b5b8bbf5 | 1890 | crush_choose_arg& carg = cmap.args[pos]; |
c07f9fc5 FG |
1891 | carg.weight_set = (crush_weight_set*)calloc(sizeof(crush_weight_set), |
1892 | size); | |
28e407b8 | 1893 | carg.weight_set_positions = positions; |
c07f9fc5 FG |
1894 | for (int ppos = 0; ppos < positions; ++ppos) { |
1895 | carg.weight_set[ppos].weights = (__u32*)calloc(sizeof(__u32), size); | |
1896 | carg.weight_set[ppos].size = size; | |
1897 | for (int bpos = 0; bpos < size; ++bpos) { | |
1898 | carg.weight_set[ppos].weights[bpos] = weights[bpos]; | |
1899 | } | |
1900 | } | |
1901 | } | |
1902 | } | |
1903 | return r; | |
1904 | } | |
1905 | ||
31f18b77 FG |
1906 | int CrushWrapper::bucket_add_item(crush_bucket *bucket, int item, int weight) |
1907 | { | |
1908 | __u32 new_size = bucket->size + 1; | |
35e4c445 FG |
1909 | int r = crush_bucket_add_item(crush, bucket, item, weight); |
1910 | if (r < 0) { | |
1911 | return r; | |
1912 | } | |
b5b8bbf5 FG |
1913 | for (auto &w : choose_args) { |
1914 | crush_choose_arg_map &arg_map = w.second; | |
31f18b77 | 1915 | crush_choose_arg *arg = &arg_map.args[-1-bucket->id]; |
28e407b8 | 1916 | for (__u32 j = 0; j < arg->weight_set_positions; j++) { |
31f18b77 | 1917 | crush_weight_set *weight_set = &arg->weight_set[j]; |
c07f9fc5 FG |
1918 | weight_set->weights = (__u32*)realloc(weight_set->weights, |
1919 | new_size * sizeof(__u32)); | |
31f18b77 FG |
1920 | assert(weight_set->size + 1 == new_size); |
1921 | weight_set->weights[weight_set->size] = weight; | |
1922 | weight_set->size = new_size; | |
1923 | } | |
1924 | if (arg->ids_size) { | |
224ce89b | 1925 | arg->ids = (__s32 *)realloc(arg->ids, new_size * sizeof(__s32)); |
31f18b77 FG |
1926 | assert(arg->ids_size + 1 == new_size); |
1927 | arg->ids[arg->ids_size] = item; | |
1928 | arg->ids_size = new_size; | |
1929 | } | |
1930 | } | |
35e4c445 | 1931 | return 0; |
31f18b77 FG |
1932 | } |
1933 | ||
1934 | int CrushWrapper::bucket_remove_item(crush_bucket *bucket, int item) | |
1935 | { | |
1936 | __u32 new_size = bucket->size - 1; | |
1937 | unsigned position; | |
1938 | for (position = 0; position < bucket->size; position++) | |
1939 | if (bucket->items[position] == item) | |
1940 | break; | |
1941 | assert(position != bucket->size); | |
35e4c445 FG |
1942 | int r = crush_bucket_remove_item(crush, bucket, item); |
1943 | if (r < 0) { | |
1944 | return r; | |
1945 | } | |
b5b8bbf5 FG |
1946 | for (auto &w : choose_args) { |
1947 | crush_choose_arg_map &arg_map = w.second; | |
31f18b77 | 1948 | crush_choose_arg *arg = &arg_map.args[-1-bucket->id]; |
28e407b8 | 1949 | for (__u32 j = 0; j < arg->weight_set_positions; j++) { |
31f18b77 FG |
1950 | crush_weight_set *weight_set = &arg->weight_set[j]; |
1951 | assert(weight_set->size - 1 == new_size); | |
1952 | for (__u32 k = position; k < new_size; k++) | |
1953 | weight_set->weights[k] = weight_set->weights[k+1]; | |
35e4c445 FG |
1954 | if (new_size) { |
1955 | weight_set->weights = (__u32*)realloc(weight_set->weights, | |
1956 | new_size * sizeof(__u32)); | |
1957 | } else { | |
1958 | weight_set->weights = NULL; | |
1959 | } | |
31f18b77 FG |
1960 | weight_set->size = new_size; |
1961 | } | |
1962 | if (arg->ids_size) { | |
1963 | assert(arg->ids_size - 1 == new_size); | |
1964 | for (__u32 k = position; k < new_size; k++) | |
1965 | arg->ids[k] = arg->ids[k+1]; | |
35e4c445 FG |
1966 | if (new_size) { |
1967 | arg->ids = (__s32 *)realloc(arg->ids, new_size * sizeof(__s32)); | |
1968 | } else { | |
1969 | arg->ids = NULL; | |
1970 | } | |
31f18b77 FG |
1971 | arg->ids_size = new_size; |
1972 | } | |
1973 | } | |
35e4c445 | 1974 | return 0; |
31f18b77 FG |
1975 | } |
1976 | ||
3efd9988 FG |
1977 | int CrushWrapper::bucket_set_alg(int bid, int alg) |
1978 | { | |
1979 | crush_bucket *b = get_bucket(bid); | |
1980 | if (!b) { | |
1981 | return -ENOENT; | |
1982 | } | |
1983 | b->alg = alg; | |
1984 | return 0; | |
1985 | } | |
1986 | ||
224ce89b WB |
1987 | int CrushWrapper::update_device_class(int id, |
1988 | const string& class_name, | |
1989 | const string& name, | |
1990 | ostream *ss) | |
7c673cae | 1991 | { |
c07f9fc5 FG |
1992 | assert(item_exists(id)); |
1993 | auto old_class_name = get_item_class(id); | |
1994 | if (old_class_name && old_class_name != class_name) { | |
1995 | *ss << "osd." << id << " has already bound to class '" << old_class_name | |
1996 | << "', can not reset class to '" << class_name << "'; " | |
1997 | << "use 'ceph osd crush rm-device-class <osd>' to " | |
1998 | << "remove old class first"; | |
1999 | return -EBUSY; | |
2000 | } | |
2001 | ||
224ce89b | 2002 | int class_id = get_or_create_class_id(class_name); |
7c673cae | 2003 | if (id < 0) { |
224ce89b | 2004 | *ss << name << " id " << id << " is negative"; |
7c673cae FG |
2005 | return -EINVAL; |
2006 | } | |
7c673cae FG |
2007 | |
2008 | if (class_map.count(id) != 0 && class_map[id] == class_id) { | |
224ce89b | 2009 | *ss << name << " already set to class " << class_name; |
7c673cae FG |
2010 | return 0; |
2011 | } | |
2012 | ||
2013 | set_item_class(id, class_id); | |
2014 | ||
2015 | int r = rebuild_roots_with_classes(); | |
2016 | if (r < 0) | |
2017 | return r; | |
2018 | return 1; | |
2019 | } | |
2020 | ||
c07f9fc5 FG |
2021 | int CrushWrapper::remove_device_class(CephContext *cct, int id, ostream *ss) |
2022 | { | |
2023 | assert(ss); | |
2024 | const char *name = get_item_name(id); | |
2025 | if (!name) { | |
2026 | *ss << "osd." << id << " does not have a name"; | |
2027 | return -ENOENT; | |
2028 | } | |
2029 | ||
2030 | const char *class_name = get_item_class(id); | |
2031 | if (!class_name) { | |
2032 | *ss << "osd." << id << " has not been bound to a specific class yet"; | |
2033 | return 0; | |
2034 | } | |
2035 | class_remove_item(id); | |
2036 | ||
c07f9fc5 FG |
2037 | int r = rebuild_roots_with_classes(); |
2038 | if (r < 0) { | |
2039 | *ss << "unable to rebuild roots with class '" << class_name << "' " | |
2040 | << "of osd." << id << ": " << cpp_strerror(r); | |
2041 | return r; | |
2042 | } | |
2043 | return 0; | |
2044 | } | |
2045 | ||
d2e6a577 FG |
2046 | int CrushWrapper::device_class_clone( |
2047 | int original_id, int device_class, | |
2048 | const std::map<int32_t, map<int32_t, int32_t>>& old_class_bucket, | |
2049 | const std::set<int32_t>& used_ids, | |
35e4c445 FG |
2050 | int *clone, |
2051 | map<int,map<int,vector<int>>> *cmap_item_weight) | |
7c673cae FG |
2052 | { |
2053 | const char *item_name = get_item_name(original_id); | |
2054 | if (item_name == NULL) | |
2055 | return -ECHILD; | |
2056 | const char *class_name = get_class_name(device_class); | |
2057 | if (class_name == NULL) | |
2058 | return -EBADF; | |
2059 | string copy_name = item_name + string("~") + class_name; | |
2060 | if (name_exists(copy_name)) { | |
2061 | *clone = get_item_id(copy_name); | |
2062 | return 0; | |
2063 | } | |
35e4c445 | 2064 | |
7c673cae | 2065 | crush_bucket *original = get_bucket(original_id); |
d2e6a577 | 2066 | assert(!IS_ERR(original)); |
7c673cae FG |
2067 | crush_bucket *copy = crush_make_bucket(crush, |
2068 | original->alg, | |
2069 | original->hash, | |
2070 | original->type, | |
2071 | 0, NULL, NULL); | |
d2e6a577 | 2072 | assert(copy); |
35e4c445 FG |
2073 | |
2074 | vector<unsigned> item_orig_pos; // new item pos -> orig item pos | |
7c673cae FG |
2075 | for (unsigned i = 0; i < original->size; i++) { |
2076 | int item = original->items[i]; | |
2077 | int weight = crush_get_bucket_item_weight(original, i); | |
2078 | if (item >= 0) { | |
2079 | if (class_map.count(item) != 0 && class_map[item] == device_class) { | |
35e4c445 | 2080 | int res = crush_bucket_add_item(crush, copy, item, weight); |
7c673cae FG |
2081 | if (res) |
2082 | return res; | |
35e4c445 FG |
2083 | } else { |
2084 | continue; | |
7c673cae FG |
2085 | } |
2086 | } else { | |
2087 | int child_copy_id; | |
d2e6a577 | 2088 | int res = device_class_clone(item, device_class, old_class_bucket, |
35e4c445 FG |
2089 | used_ids, &child_copy_id, |
2090 | cmap_item_weight); | |
7c673cae FG |
2091 | if (res < 0) |
2092 | return res; | |
2093 | crush_bucket *child_copy = get_bucket(child_copy_id); | |
d2e6a577 | 2094 | assert(!IS_ERR(child_copy)); |
35e4c445 FG |
2095 | res = crush_bucket_add_item(crush, copy, child_copy_id, |
2096 | child_copy->weight); | |
7c673cae FG |
2097 | if (res) |
2098 | return res; | |
2099 | } | |
35e4c445 | 2100 | item_orig_pos.push_back(i); |
7c673cae | 2101 | } |
35e4c445 FG |
2102 | assert(item_orig_pos.size() == copy->size); |
2103 | ||
d2e6a577 FG |
2104 | int bno = 0; |
2105 | if (old_class_bucket.count(original_id) && | |
2106 | old_class_bucket.at(original_id).count(device_class)) { | |
2107 | bno = old_class_bucket.at(original_id).at(device_class); | |
2108 | } else { | |
2109 | // pick a new shadow bucket id that is not used by the current map | |
2110 | // *or* any previous shadow buckets. | |
2111 | bno = -1; | |
2112 | while (((-1-bno) < crush->max_buckets && crush->buckets[-1-bno]) || | |
2113 | used_ids.count(bno)) { | |
2114 | --bno; | |
2115 | } | |
2116 | } | |
2117 | int res = crush_add_bucket(crush, bno, copy, clone); | |
7c673cae FG |
2118 | if (res) |
2119 | return res; | |
d2e6a577 | 2120 | assert(!bno || bno == *clone); |
35e4c445 | 2121 | |
7c673cae FG |
2122 | res = set_item_class(*clone, device_class); |
2123 | if (res < 0) | |
2124 | return res; | |
35e4c445 | 2125 | |
7c673cae FG |
2126 | // we do not use set_item_name because the name is intentionally invalid |
2127 | name_map[*clone] = copy_name; | |
2128 | if (have_rmaps) | |
2129 | name_rmap[copy_name] = *clone; | |
2130 | class_bucket[original_id][device_class] = *clone; | |
35e4c445 FG |
2131 | |
2132 | // set up choose_args for the new bucket. | |
2133 | for (auto& w : choose_args) { | |
2134 | crush_choose_arg_map& cmap = w.second; | |
2135 | if (-1-bno >= (int)cmap.size) { | |
2136 | unsigned new_size = -1-bno + 1; | |
2137 | cmap.args = (crush_choose_arg*)realloc(cmap.args, | |
2138 | new_size * sizeof(cmap.args[0])); | |
b5b8bbf5 | 2139 | assert(cmap.args); |
35e4c445 FG |
2140 | memset(cmap.args + cmap.size, 0, |
2141 | (new_size - cmap.size) * sizeof(cmap.args[0])); | |
b5b8bbf5 | 2142 | cmap.size = new_size; |
35e4c445 FG |
2143 | } |
2144 | auto& o = cmap.args[-1-original_id]; | |
2145 | auto& n = cmap.args[-1-bno]; | |
2146 | n.ids_size = 0; // FIXME: implement me someday | |
28e407b8 | 2147 | n.weight_set_positions = o.weight_set_positions; |
35e4c445 | 2148 | n.weight_set = (crush_weight_set*)calloc( |
28e407b8 AA |
2149 | n.weight_set_positions, sizeof(crush_weight_set)); |
2150 | for (size_t s = 0; s < n.weight_set_positions; ++s) { | |
35e4c445 FG |
2151 | n.weight_set[s].size = copy->size; |
2152 | n.weight_set[s].weights = (__u32*)calloc(copy->size, sizeof(__u32)); | |
2153 | } | |
28e407b8 AA |
2154 | for (size_t s = 0; s < n.weight_set_positions; ++s) { |
2155 | vector<int> bucket_weights(n.weight_set_positions); | |
35e4c445 FG |
2156 | for (size_t i = 0; i < copy->size; ++i) { |
2157 | int item = copy->items[i]; | |
2158 | if (item >= 0) { | |
2159 | n.weight_set[s].weights[i] = o.weight_set[s].weights[item_orig_pos[i]]; | |
28e407b8 | 2160 | } else if ((*cmap_item_weight)[w.first].count(item)) { |
35e4c445 | 2161 | n.weight_set[s].weights[i] = (*cmap_item_weight)[w.first][item][s]; |
28e407b8 AA |
2162 | } else { |
2163 | n.weight_set[s].weights[i] = 0; | |
35e4c445 FG |
2164 | } |
2165 | bucket_weights[s] += n.weight_set[s].weights[i]; | |
2166 | } | |
2167 | (*cmap_item_weight)[w.first][bno] = bucket_weights; | |
2168 | } | |
2169 | } | |
7c673cae FG |
2170 | return 0; |
2171 | } | |
2172 | ||
b5b8bbf5 FG |
2173 | int CrushWrapper::get_rules_by_class(const string &class_name, set<int> *rules) |
2174 | { | |
2175 | assert(rules); | |
2176 | rules->clear(); | |
2177 | if (!class_exists(class_name)) { | |
2178 | return -ENOENT; | |
2179 | } | |
2180 | int class_id = get_class_id(class_name); | |
2181 | for (unsigned i = 0; i < crush->max_rules; ++i) { | |
2182 | crush_rule *r = crush->rules[i]; | |
2183 | if (!r) | |
2184 | continue; | |
2185 | for (unsigned j = 0; j < r->len; ++j) { | |
2186 | if (r->steps[j].op == CRUSH_RULE_TAKE) { | |
2187 | int step_item = r->steps[j].arg1; | |
2188 | int original_item; | |
2189 | int c; | |
2190 | int res = split_id_class(step_item, &original_item, &c); | |
2191 | if (res < 0) { | |
2192 | return res; | |
2193 | } | |
2194 | if (c != -1 && c == class_id) { | |
2195 | rules->insert(i); | |
2196 | break; | |
2197 | } | |
2198 | } | |
2199 | } | |
2200 | } | |
2201 | return 0; | |
2202 | } | |
2203 | ||
3efd9988 FG |
2204 | // return rules that might reference the given osd |
2205 | int CrushWrapper::get_rules_by_osd(int osd, set<int> *rules) | |
2206 | { | |
2207 | assert(rules); | |
2208 | rules->clear(); | |
2209 | if (osd < 0) { | |
2210 | return -EINVAL; | |
2211 | } | |
2212 | for (unsigned i = 0; i < crush->max_rules; ++i) { | |
2213 | crush_rule *r = crush->rules[i]; | |
2214 | if (!r) | |
2215 | continue; | |
2216 | for (unsigned j = 0; j < r->len; ++j) { | |
2217 | if (r->steps[j].op == CRUSH_RULE_TAKE) { | |
2218 | int step_item = r->steps[j].arg1; | |
2219 | list<int> unordered; | |
2220 | int rc = _get_leaves(step_item, &unordered); | |
2221 | if (rc < 0) { | |
2222 | return rc; // propagate fatal errors! | |
2223 | } | |
2224 | bool match = false; | |
2225 | for (auto &o: unordered) { | |
2226 | assert(o >= 0); | |
2227 | if (o == osd) { | |
2228 | match = true; | |
2229 | break; | |
2230 | } | |
2231 | } | |
2232 | if (match) { | |
2233 | rules->insert(i); | |
2234 | break; | |
2235 | } | |
2236 | } | |
2237 | } | |
2238 | } | |
2239 | return 0; | |
2240 | } | |
2241 | ||
d2e6a577 FG |
2242 | bool CrushWrapper::_class_is_dead(int class_id) |
2243 | { | |
2244 | for (auto &p: class_map) { | |
2245 | if (p.first >= 0 && p.second == class_id) { | |
2246 | return false; | |
2247 | } | |
2248 | } | |
2249 | for (unsigned i = 0; i < crush->max_rules; ++i) { | |
2250 | crush_rule *r = crush->rules[i]; | |
2251 | if (!r) | |
2252 | continue; | |
2253 | for (unsigned j = 0; j < r->len; ++j) { | |
2254 | if (r->steps[j].op == CRUSH_RULE_TAKE) { | |
2255 | int root = r->steps[j].arg1; | |
2256 | for (auto &p : class_bucket) { | |
2257 | auto& q = p.second; | |
2258 | if (q.count(class_id) && q[class_id] == root) { | |
2259 | return false; | |
2260 | } | |
2261 | } | |
2262 | } | |
2263 | } | |
2264 | } | |
2265 | // no more referenced by any devices or crush rules | |
2266 | return true; | |
2267 | } | |
2268 | ||
2269 | void CrushWrapper::cleanup_dead_classes() | |
2270 | { | |
35e4c445 FG |
2271 | auto p = class_name.begin(); |
2272 | while (p != class_name.end()) { | |
2273 | if (_class_is_dead(p->first)) { | |
2274 | string n = p->second; | |
2275 | ++p; | |
2276 | remove_class_name(n); | |
2277 | } else { | |
2278 | ++p; | |
2279 | } | |
d2e6a577 FG |
2280 | } |
2281 | } | |
2282 | ||
7c673cae FG |
2283 | int CrushWrapper::rebuild_roots_with_classes() |
2284 | { | |
d2e6a577 FG |
2285 | std::map<int32_t, map<int32_t, int32_t> > old_class_bucket = class_bucket; |
2286 | cleanup_dead_classes(); | |
35e4c445 | 2287 | int r = trim_roots_with_class(); |
7c673cae FG |
2288 | if (r < 0) |
2289 | return r; | |
d2e6a577 FG |
2290 | class_bucket.clear(); |
2291 | return populate_classes(old_class_bucket); | |
7c673cae FG |
2292 | } |
2293 | ||
2294 | void CrushWrapper::encode(bufferlist& bl, uint64_t features) const | |
2295 | { | |
2296 | assert(crush); | |
2297 | ||
2298 | __u32 magic = CRUSH_MAGIC; | |
2299 | ::encode(magic, bl); | |
2300 | ||
2301 | ::encode(crush->max_buckets, bl); | |
2302 | ::encode(crush->max_rules, bl); | |
2303 | ::encode(crush->max_devices, bl); | |
2304 | ||
31f18b77 FG |
2305 | bool encode_compat_choose_args = false; |
2306 | crush_choose_arg_map arg_map; | |
2307 | memset(&arg_map, '\0', sizeof(arg_map)); | |
2308 | if (has_choose_args() && | |
2309 | !HAVE_FEATURE(features, CRUSH_CHOOSE_ARGS)) { | |
2310 | assert(!has_incompat_choose_args()); | |
2311 | encode_compat_choose_args = true; | |
2312 | arg_map = choose_args.begin()->second; | |
2313 | } | |
2314 | ||
7c673cae FG |
2315 | // buckets |
2316 | for (int i=0; i<crush->max_buckets; i++) { | |
2317 | __u32 alg = 0; | |
2318 | if (crush->buckets[i]) alg = crush->buckets[i]->alg; | |
2319 | ::encode(alg, bl); | |
2320 | if (!alg) | |
2321 | continue; | |
2322 | ||
2323 | ::encode(crush->buckets[i]->id, bl); | |
2324 | ::encode(crush->buckets[i]->type, bl); | |
2325 | ::encode(crush->buckets[i]->alg, bl); | |
2326 | ::encode(crush->buckets[i]->hash, bl); | |
2327 | ::encode(crush->buckets[i]->weight, bl); | |
2328 | ::encode(crush->buckets[i]->size, bl); | |
2329 | for (unsigned j=0; j<crush->buckets[i]->size; j++) | |
2330 | ::encode(crush->buckets[i]->items[j], bl); | |
2331 | ||
2332 | switch (crush->buckets[i]->alg) { | |
2333 | case CRUSH_BUCKET_UNIFORM: | |
2334 | ::encode((reinterpret_cast<crush_bucket_uniform*>(crush->buckets[i]))->item_weight, bl); | |
2335 | break; | |
2336 | ||
2337 | case CRUSH_BUCKET_LIST: | |
2338 | for (unsigned j=0; j<crush->buckets[i]->size; j++) { | |
2339 | ::encode((reinterpret_cast<crush_bucket_list*>(crush->buckets[i]))->item_weights[j], bl); | |
2340 | ::encode((reinterpret_cast<crush_bucket_list*>(crush->buckets[i]))->sum_weights[j], bl); | |
2341 | } | |
2342 | break; | |
2343 | ||
2344 | case CRUSH_BUCKET_TREE: | |
2345 | ::encode((reinterpret_cast<crush_bucket_tree*>(crush->buckets[i]))->num_nodes, bl); | |
2346 | for (unsigned j=0; j<(reinterpret_cast<crush_bucket_tree*>(crush->buckets[i]))->num_nodes; j++) | |
2347 | ::encode((reinterpret_cast<crush_bucket_tree*>(crush->buckets[i]))->node_weights[j], bl); | |
2348 | break; | |
2349 | ||
2350 | case CRUSH_BUCKET_STRAW: | |
2351 | for (unsigned j=0; j<crush->buckets[i]->size; j++) { | |
2352 | ::encode((reinterpret_cast<crush_bucket_straw*>(crush->buckets[i]))->item_weights[j], bl); | |
2353 | ::encode((reinterpret_cast<crush_bucket_straw*>(crush->buckets[i]))->straws[j], bl); | |
2354 | } | |
2355 | break; | |
2356 | ||
2357 | case CRUSH_BUCKET_STRAW2: | |
31f18b77 FG |
2358 | { |
2359 | __u32 *weights; | |
2360 | if (encode_compat_choose_args && | |
28e407b8 | 2361 | arg_map.args[i].weight_set_positions > 0) { |
31f18b77 FG |
2362 | weights = arg_map.args[i].weight_set[0].weights; |
2363 | } else { | |
2364 | weights = (reinterpret_cast<crush_bucket_straw2*>(crush->buckets[i]))->item_weights; | |
2365 | } | |
2366 | for (unsigned j=0; j<crush->buckets[i]->size; j++) { | |
2367 | ::encode(weights[j], bl); | |
2368 | } | |
7c673cae FG |
2369 | } |
2370 | break; | |
2371 | ||
2372 | default: | |
2373 | ceph_abort(); | |
2374 | break; | |
2375 | } | |
2376 | } | |
2377 | ||
2378 | // rules | |
2379 | for (unsigned i=0; i<crush->max_rules; i++) { | |
2380 | __u32 yes = crush->rules[i] ? 1:0; | |
2381 | ::encode(yes, bl); | |
2382 | if (!yes) | |
2383 | continue; | |
2384 | ||
2385 | ::encode(crush->rules[i]->len, bl); | |
2386 | ::encode(crush->rules[i]->mask, bl); | |
2387 | for (unsigned j=0; j<crush->rules[i]->len; j++) | |
2388 | ::encode(crush->rules[i]->steps[j], bl); | |
2389 | } | |
2390 | ||
2391 | // name info | |
2392 | ::encode(type_map, bl); | |
2393 | ::encode(name_map, bl); | |
2394 | ::encode(rule_name_map, bl); | |
2395 | ||
2396 | // tunables | |
2397 | ::encode(crush->choose_local_tries, bl); | |
2398 | ::encode(crush->choose_local_fallback_tries, bl); | |
2399 | ::encode(crush->choose_total_tries, bl); | |
2400 | ::encode(crush->chooseleaf_descend_once, bl); | |
2401 | ::encode(crush->chooseleaf_vary_r, bl); | |
2402 | ::encode(crush->straw_calc_version, bl); | |
2403 | ::encode(crush->allowed_bucket_algs, bl); | |
2404 | if (features & CEPH_FEATURE_CRUSH_TUNABLES5) { | |
2405 | ::encode(crush->chooseleaf_stable, bl); | |
2406 | } | |
2407 | ||
2408 | if (HAVE_FEATURE(features, SERVER_LUMINOUS)) { | |
2409 | // device classes | |
2410 | ::encode(class_map, bl); | |
2411 | ::encode(class_name, bl); | |
2412 | ::encode(class_bucket, bl); | |
2413 | ||
c07f9fc5 | 2414 | // choose args |
224ce89b WB |
2415 | __u32 size = (__u32)choose_args.size(); |
2416 | ::encode(size, bl); | |
7c673cae FG |
2417 | for (auto c : choose_args) { |
2418 | ::encode(c.first, bl); | |
2419 | crush_choose_arg_map arg_map = c.second; | |
224ce89b | 2420 | size = 0; |
7c673cae FG |
2421 | for (__u32 i = 0; i < arg_map.size; i++) { |
2422 | crush_choose_arg *arg = &arg_map.args[i]; | |
28e407b8 | 2423 | if (arg->weight_set_positions == 0 && |
7c673cae FG |
2424 | arg->ids_size == 0) |
2425 | continue; | |
2426 | size++; | |
2427 | } | |
2428 | ::encode(size, bl); | |
2429 | for (__u32 i = 0; i < arg_map.size; i++) { | |
2430 | crush_choose_arg *arg = &arg_map.args[i]; | |
28e407b8 | 2431 | if (arg->weight_set_positions == 0 && |
7c673cae FG |
2432 | arg->ids_size == 0) |
2433 | continue; | |
2434 | ::encode(i, bl); | |
28e407b8 AA |
2435 | ::encode(arg->weight_set_positions, bl); |
2436 | for (__u32 j = 0; j < arg->weight_set_positions; j++) { | |
7c673cae FG |
2437 | crush_weight_set *weight_set = &arg->weight_set[j]; |
2438 | ::encode(weight_set->size, bl); | |
2439 | for (__u32 k = 0; k < weight_set->size; k++) | |
2440 | ::encode(weight_set->weights[k], bl); | |
2441 | } | |
2442 | ::encode(arg->ids_size, bl); | |
2443 | for (__u32 j = 0; j < arg->ids_size; j++) | |
2444 | ::encode(arg->ids[j], bl); | |
2445 | } | |
2446 | } | |
2447 | } | |
2448 | } | |
2449 | ||
2450 | static void decode_32_or_64_string_map(map<int32_t,string>& m, bufferlist::iterator& blp) | |
2451 | { | |
2452 | m.clear(); | |
2453 | __u32 n; | |
2454 | ::decode(n, blp); | |
2455 | while (n--) { | |
2456 | __s32 key; | |
2457 | ::decode(key, blp); | |
2458 | ||
2459 | __u32 strlen; | |
2460 | ::decode(strlen, blp); | |
2461 | if (strlen == 0) { | |
2462 | // der, key was actually 64-bits! | |
2463 | ::decode(strlen, blp); | |
2464 | } | |
2465 | ::decode_nohead(strlen, m[key], blp); | |
2466 | } | |
2467 | } | |
2468 | ||
2469 | void CrushWrapper::decode(bufferlist::iterator& blp) | |
2470 | { | |
2471 | create(); | |
2472 | ||
2473 | __u32 magic; | |
2474 | ::decode(magic, blp); | |
2475 | if (magic != CRUSH_MAGIC) | |
2476 | throw buffer::malformed_input("bad magic number"); | |
2477 | ||
2478 | ::decode(crush->max_buckets, blp); | |
2479 | ::decode(crush->max_rules, blp); | |
2480 | ::decode(crush->max_devices, blp); | |
2481 | ||
2482 | // legacy tunables, unless we decode something newer | |
2483 | set_tunables_legacy(); | |
2484 | ||
2485 | try { | |
2486 | // buckets | |
2487 | crush->buckets = (crush_bucket**)calloc(1, crush->max_buckets * sizeof(crush_bucket*)); | |
2488 | for (int i=0; i<crush->max_buckets; i++) { | |
2489 | decode_crush_bucket(&crush->buckets[i], blp); | |
2490 | } | |
2491 | ||
2492 | // rules | |
2493 | crush->rules = (crush_rule**)calloc(1, crush->max_rules * sizeof(crush_rule*)); | |
2494 | for (unsigned i = 0; i < crush->max_rules; ++i) { | |
2495 | __u32 yes; | |
2496 | ::decode(yes, blp); | |
2497 | if (!yes) { | |
2498 | crush->rules[i] = NULL; | |
2499 | continue; | |
2500 | } | |
2501 | ||
2502 | __u32 len; | |
2503 | ::decode(len, blp); | |
2504 | crush->rules[i] = reinterpret_cast<crush_rule*>(calloc(1, crush_rule_size(len))); | |
2505 | crush->rules[i]->len = len; | |
2506 | ::decode(crush->rules[i]->mask, blp); | |
2507 | for (unsigned j=0; j<crush->rules[i]->len; j++) | |
2508 | ::decode(crush->rules[i]->steps[j], blp); | |
2509 | } | |
2510 | ||
2511 | // name info | |
2512 | // NOTE: we had a bug where we were incoding int instead of int32, which means the | |
2513 | // 'key' field for these maps may be either 32 or 64 bits, depending. tolerate | |
2514 | // both by assuming the string is always non-empty. | |
2515 | decode_32_or_64_string_map(type_map, blp); | |
2516 | decode_32_or_64_string_map(name_map, blp); | |
2517 | decode_32_or_64_string_map(rule_name_map, blp); | |
2518 | ||
2519 | // tunables | |
2520 | if (!blp.end()) { | |
2521 | ::decode(crush->choose_local_tries, blp); | |
2522 | ::decode(crush->choose_local_fallback_tries, blp); | |
2523 | ::decode(crush->choose_total_tries, blp); | |
2524 | } | |
2525 | if (!blp.end()) { | |
2526 | ::decode(crush->chooseleaf_descend_once, blp); | |
2527 | } | |
2528 | if (!blp.end()) { | |
2529 | ::decode(crush->chooseleaf_vary_r, blp); | |
2530 | } | |
2531 | if (!blp.end()) { | |
2532 | ::decode(crush->straw_calc_version, blp); | |
2533 | } | |
2534 | if (!blp.end()) { | |
2535 | ::decode(crush->allowed_bucket_algs, blp); | |
2536 | } | |
2537 | if (!blp.end()) { | |
2538 | ::decode(crush->chooseleaf_stable, blp); | |
2539 | } | |
2540 | if (!blp.end()) { | |
2541 | ::decode(class_map, blp); | |
2542 | ::decode(class_name, blp); | |
2543 | for (auto &c : class_name) | |
2544 | class_rname[c.second] = c.first; | |
2545 | ::decode(class_bucket, blp); | |
7c673cae FG |
2546 | } |
2547 | if (!blp.end()) { | |
224ce89b | 2548 | __u32 choose_args_size; |
7c673cae | 2549 | ::decode(choose_args_size, blp); |
224ce89b | 2550 | for (__u32 i = 0; i < choose_args_size; i++) { |
b5b8bbf5 | 2551 | typename decltype(choose_args)::key_type choose_args_index; |
7c673cae FG |
2552 | ::decode(choose_args_index, blp); |
2553 | crush_choose_arg_map arg_map; | |
2554 | arg_map.size = crush->max_buckets; | |
c07f9fc5 FG |
2555 | arg_map.args = (crush_choose_arg*)calloc( |
2556 | arg_map.size, sizeof(crush_choose_arg)); | |
7c673cae FG |
2557 | __u32 size; |
2558 | ::decode(size, blp); | |
2559 | for (__u32 j = 0; j < size; j++) { | |
2560 | __u32 bucket_index; | |
2561 | ::decode(bucket_index, blp); | |
2562 | assert(bucket_index < arg_map.size); | |
2563 | crush_choose_arg *arg = &arg_map.args[bucket_index]; | |
28e407b8 AA |
2564 | ::decode(arg->weight_set_positions, blp); |
2565 | if (arg->weight_set_positions) { | |
c07f9fc5 | 2566 | arg->weight_set = (crush_weight_set*)calloc( |
28e407b8 AA |
2567 | arg->weight_set_positions, sizeof(crush_weight_set)); |
2568 | for (__u32 k = 0; k < arg->weight_set_positions; k++) { | |
c07f9fc5 FG |
2569 | crush_weight_set *weight_set = &arg->weight_set[k]; |
2570 | ::decode(weight_set->size, blp); | |
2571 | weight_set->weights = (__u32*)calloc( | |
2572 | weight_set->size, sizeof(__u32)); | |
2573 | for (__u32 l = 0; l < weight_set->size; l++) | |
2574 | ::decode(weight_set->weights[l], blp); | |
2575 | } | |
7c673cae FG |
2576 | } |
2577 | ::decode(arg->ids_size, blp); | |
c07f9fc5 FG |
2578 | if (arg->ids_size) { |
2579 | assert(arg->ids_size == crush->buckets[bucket_index]->size); | |
2580 | arg->ids = (__s32 *)calloc(arg->ids_size, sizeof(__s32)); | |
2581 | for (__u32 k = 0; k < arg->ids_size; k++) | |
2582 | ::decode(arg->ids[k], blp); | |
2583 | } | |
7c673cae FG |
2584 | } |
2585 | choose_args[choose_args_index] = arg_map; | |
2586 | } | |
2587 | } | |
28e407b8 | 2588 | update_choose_args(nullptr); // in case we decode a legacy "corrupted" map |
7c673cae FG |
2589 | finalize(); |
2590 | } | |
2591 | catch (...) { | |
2592 | crush_destroy(crush); | |
2593 | throw; | |
2594 | } | |
2595 | } | |
2596 | ||
2597 | void CrushWrapper::decode_crush_bucket(crush_bucket** bptr, bufferlist::iterator &blp) | |
2598 | { | |
2599 | __u32 alg; | |
2600 | ::decode(alg, blp); | |
2601 | if (!alg) { | |
2602 | *bptr = NULL; | |
2603 | return; | |
2604 | } | |
2605 | ||
2606 | int size = 0; | |
2607 | switch (alg) { | |
2608 | case CRUSH_BUCKET_UNIFORM: | |
2609 | size = sizeof(crush_bucket_uniform); | |
2610 | break; | |
2611 | case CRUSH_BUCKET_LIST: | |
2612 | size = sizeof(crush_bucket_list); | |
2613 | break; | |
2614 | case CRUSH_BUCKET_TREE: | |
2615 | size = sizeof(crush_bucket_tree); | |
2616 | break; | |
2617 | case CRUSH_BUCKET_STRAW: | |
2618 | size = sizeof(crush_bucket_straw); | |
2619 | break; | |
2620 | case CRUSH_BUCKET_STRAW2: | |
2621 | size = sizeof(crush_bucket_straw2); | |
2622 | break; | |
2623 | default: | |
2624 | { | |
2625 | char str[128]; | |
2626 | snprintf(str, sizeof(str), "unsupported bucket algorithm: %d", alg); | |
2627 | throw buffer::malformed_input(str); | |
2628 | } | |
2629 | } | |
2630 | crush_bucket *bucket = reinterpret_cast<crush_bucket*>(calloc(1, size)); | |
2631 | *bptr = bucket; | |
2632 | ||
2633 | ::decode(bucket->id, blp); | |
2634 | ::decode(bucket->type, blp); | |
2635 | ::decode(bucket->alg, blp); | |
2636 | ::decode(bucket->hash, blp); | |
2637 | ::decode(bucket->weight, blp); | |
2638 | ::decode(bucket->size, blp); | |
2639 | ||
2640 | bucket->items = (__s32*)calloc(1, bucket->size * sizeof(__s32)); | |
2641 | for (unsigned j = 0; j < bucket->size; ++j) { | |
2642 | ::decode(bucket->items[j], blp); | |
2643 | } | |
2644 | ||
2645 | switch (bucket->alg) { | |
2646 | case CRUSH_BUCKET_UNIFORM: | |
2647 | ::decode((reinterpret_cast<crush_bucket_uniform*>(bucket))->item_weight, blp); | |
2648 | break; | |
2649 | ||
2650 | case CRUSH_BUCKET_LIST: { | |
2651 | crush_bucket_list* cbl = reinterpret_cast<crush_bucket_list*>(bucket); | |
2652 | cbl->item_weights = (__u32*)calloc(1, bucket->size * sizeof(__u32)); | |
2653 | cbl->sum_weights = (__u32*)calloc(1, bucket->size * sizeof(__u32)); | |
2654 | ||
2655 | for (unsigned j = 0; j < bucket->size; ++j) { | |
2656 | ::decode(cbl->item_weights[j], blp); | |
2657 | ::decode(cbl->sum_weights[j], blp); | |
2658 | } | |
2659 | break; | |
2660 | } | |
2661 | ||
2662 | case CRUSH_BUCKET_TREE: { | |
2663 | crush_bucket_tree* cbt = reinterpret_cast<crush_bucket_tree*>(bucket); | |
2664 | ::decode(cbt->num_nodes, blp); | |
2665 | cbt->node_weights = (__u32*)calloc(1, cbt->num_nodes * sizeof(__u32)); | |
2666 | for (unsigned j=0; j<cbt->num_nodes; j++) { | |
2667 | ::decode(cbt->node_weights[j], blp); | |
2668 | } | |
2669 | break; | |
2670 | } | |
2671 | ||
2672 | case CRUSH_BUCKET_STRAW: { | |
2673 | crush_bucket_straw* cbs = reinterpret_cast<crush_bucket_straw*>(bucket); | |
2674 | cbs->straws = (__u32*)calloc(1, bucket->size * sizeof(__u32)); | |
2675 | cbs->item_weights = (__u32*)calloc(1, bucket->size * sizeof(__u32)); | |
2676 | for (unsigned j = 0; j < bucket->size; ++j) { | |
2677 | ::decode(cbs->item_weights[j], blp); | |
2678 | ::decode(cbs->straws[j], blp); | |
2679 | } | |
2680 | break; | |
2681 | } | |
2682 | ||
2683 | case CRUSH_BUCKET_STRAW2: { | |
2684 | crush_bucket_straw2* cbs = reinterpret_cast<crush_bucket_straw2*>(bucket); | |
2685 | cbs->item_weights = (__u32*)calloc(1, bucket->size * sizeof(__u32)); | |
2686 | for (unsigned j = 0; j < bucket->size; ++j) { | |
2687 | ::decode(cbs->item_weights[j], blp); | |
2688 | } | |
2689 | break; | |
2690 | } | |
2691 | ||
2692 | default: | |
2693 | // We should have handled this case in the first switch statement | |
2694 | ceph_abort(); | |
2695 | break; | |
2696 | } | |
2697 | } | |
2698 | ||
2699 | ||
2700 | void CrushWrapper::dump(Formatter *f) const | |
2701 | { | |
2702 | f->open_array_section("devices"); | |
2703 | for (int i=0; i<get_max_devices(); i++) { | |
2704 | f->open_object_section("device"); | |
2705 | f->dump_int("id", i); | |
2706 | const char *n = get_item_name(i); | |
2707 | if (n) { | |
2708 | f->dump_string("name", n); | |
2709 | } else { | |
2710 | char name[20]; | |
2711 | sprintf(name, "device%d", i); | |
2712 | f->dump_string("name", name); | |
2713 | } | |
2714 | const char *device_class = get_item_class(i); | |
2715 | if (device_class != NULL) | |
2716 | f->dump_string("class", device_class); | |
2717 | f->close_section(); | |
2718 | } | |
2719 | f->close_section(); | |
2720 | ||
2721 | f->open_array_section("types"); | |
2722 | int n = get_num_type_names(); | |
2723 | for (int i=0; n; i++) { | |
2724 | const char *name = get_type_name(i); | |
2725 | if (!name) { | |
2726 | if (i == 0) { | |
2727 | f->open_object_section("type"); | |
2728 | f->dump_int("type_id", 0); | |
2729 | f->dump_string("name", "device"); | |
2730 | f->close_section(); | |
2731 | } | |
2732 | continue; | |
2733 | } | |
2734 | n--; | |
2735 | f->open_object_section("type"); | |
2736 | f->dump_int("type_id", i); | |
2737 | f->dump_string("name", name); | |
2738 | f->close_section(); | |
2739 | } | |
2740 | f->close_section(); | |
2741 | ||
2742 | f->open_array_section("buckets"); | |
2743 | for (int bucket = -1; bucket > -1-get_max_buckets(); --bucket) { | |
2744 | if (!bucket_exists(bucket)) | |
2745 | continue; | |
2746 | f->open_object_section("bucket"); | |
2747 | f->dump_int("id", bucket); | |
2748 | if (get_item_name(bucket)) | |
2749 | f->dump_string("name", get_item_name(bucket)); | |
2750 | f->dump_int("type_id", get_bucket_type(bucket)); | |
2751 | if (get_type_name(get_bucket_type(bucket))) | |
2752 | f->dump_string("type_name", get_type_name(get_bucket_type(bucket))); | |
2753 | f->dump_int("weight", get_bucket_weight(bucket)); | |
2754 | f->dump_string("alg", crush_bucket_alg_name(get_bucket_alg(bucket))); | |
2755 | f->dump_string("hash", crush_hash_name(get_bucket_hash(bucket))); | |
2756 | f->open_array_section("items"); | |
2757 | for (int j=0; j<get_bucket_size(bucket); j++) { | |
2758 | f->open_object_section("item"); | |
2759 | f->dump_int("id", get_bucket_item(bucket, j)); | |
2760 | f->dump_int("weight", get_bucket_item_weight(bucket, j)); | |
2761 | f->dump_int("pos", j); | |
2762 | f->close_section(); | |
2763 | } | |
2764 | f->close_section(); | |
2765 | f->close_section(); | |
2766 | } | |
2767 | f->close_section(); | |
2768 | ||
2769 | f->open_array_section("rules"); | |
2770 | dump_rules(f); | |
2771 | f->close_section(); | |
2772 | ||
2773 | f->open_object_section("tunables"); | |
2774 | dump_tunables(f); | |
2775 | f->close_section(); | |
2776 | ||
2777 | dump_choose_args(f); | |
2778 | } | |
2779 | ||
2780 | namespace { | |
2781 | // depth first walker | |
2782 | class TreeDumper { | |
2783 | typedef CrushTreeDumper::Item Item; | |
2784 | const CrushWrapper *crush; | |
c07f9fc5 | 2785 | const CrushTreeDumper::name_map_t& weight_set_names; |
7c673cae | 2786 | public: |
c07f9fc5 FG |
2787 | explicit TreeDumper(const CrushWrapper *crush, |
2788 | const CrushTreeDumper::name_map_t& wsnames) | |
2789 | : crush(crush), weight_set_names(wsnames) {} | |
7c673cae FG |
2790 | |
2791 | void dump(Formatter *f) { | |
2792 | set<int> roots; | |
3efd9988 | 2793 | crush->find_roots(&roots); |
7c673cae | 2794 | for (set<int>::iterator root = roots.begin(); root != roots.end(); ++root) { |
c07f9fc5 | 2795 | dump_item(Item(*root, 0, 0, crush->get_bucket_weightf(*root)), f); |
7c673cae FG |
2796 | } |
2797 | } | |
2798 | ||
2799 | private: | |
2800 | void dump_item(const Item& qi, Formatter* f) { | |
2801 | if (qi.is_bucket()) { | |
2802 | f->open_object_section("bucket"); | |
c07f9fc5 | 2803 | CrushTreeDumper::dump_item_fields(crush, weight_set_names, qi, f); |
7c673cae FG |
2804 | dump_bucket_children(qi, f); |
2805 | f->close_section(); | |
2806 | } else { | |
2807 | f->open_object_section("device"); | |
c07f9fc5 | 2808 | CrushTreeDumper::dump_item_fields(crush, weight_set_names, qi, f); |
7c673cae FG |
2809 | f->close_section(); |
2810 | } | |
2811 | } | |
2812 | ||
2813 | void dump_bucket_children(const Item& parent, Formatter* f) { | |
2814 | f->open_array_section("items"); | |
2815 | const int max_pos = crush->get_bucket_size(parent.id); | |
2816 | for (int pos = 0; pos < max_pos; pos++) { | |
2817 | int id = crush->get_bucket_item(parent.id, pos); | |
2818 | float weight = crush->get_bucket_item_weightf(parent.id, pos); | |
c07f9fc5 | 2819 | dump_item(Item(id, parent.id, parent.depth + 1, weight), f); |
7c673cae FG |
2820 | } |
2821 | f->close_section(); | |
2822 | } | |
2823 | }; | |
2824 | } | |
2825 | ||
c07f9fc5 FG |
2826 | void CrushWrapper::dump_tree( |
2827 | Formatter *f, | |
2828 | const CrushTreeDumper::name_map_t& weight_set_names) const | |
7c673cae FG |
2829 | { |
2830 | assert(f); | |
c07f9fc5 | 2831 | TreeDumper(this, weight_set_names).dump(f); |
7c673cae FG |
2832 | } |
2833 | ||
2834 | void CrushWrapper::dump_tunables(Formatter *f) const | |
2835 | { | |
2836 | f->dump_int("choose_local_tries", get_choose_local_tries()); | |
2837 | f->dump_int("choose_local_fallback_tries", get_choose_local_fallback_tries()); | |
2838 | f->dump_int("choose_total_tries", get_choose_total_tries()); | |
2839 | f->dump_int("chooseleaf_descend_once", get_chooseleaf_descend_once()); | |
2840 | f->dump_int("chooseleaf_vary_r", get_chooseleaf_vary_r()); | |
2841 | f->dump_int("chooseleaf_stable", get_chooseleaf_stable()); | |
2842 | f->dump_int("straw_calc_version", get_straw_calc_version()); | |
2843 | f->dump_int("allowed_bucket_algs", get_allowed_bucket_algs()); | |
2844 | ||
2845 | // be helpful about it | |
2846 | if (has_jewel_tunables()) | |
2847 | f->dump_string("profile", "jewel"); | |
2848 | else if (has_hammer_tunables()) | |
2849 | f->dump_string("profile", "hammer"); | |
2850 | else if (has_firefly_tunables()) | |
2851 | f->dump_string("profile", "firefly"); | |
2852 | else if (has_bobtail_tunables()) | |
2853 | f->dump_string("profile", "bobtail"); | |
2854 | else if (has_argonaut_tunables()) | |
2855 | f->dump_string("profile", "argonaut"); | |
2856 | else | |
2857 | f->dump_string("profile", "unknown"); | |
2858 | f->dump_int("optimal_tunables", (int)has_optimal_tunables()); | |
2859 | f->dump_int("legacy_tunables", (int)has_legacy_tunables()); | |
2860 | ||
2861 | // be helpful about minimum version required | |
2862 | f->dump_string("minimum_required_version", get_min_required_version()); | |
2863 | ||
2864 | f->dump_int("require_feature_tunables", (int)has_nondefault_tunables()); | |
2865 | f->dump_int("require_feature_tunables2", (int)has_nondefault_tunables2()); | |
2866 | f->dump_int("has_v2_rules", (int)has_v2_rules()); | |
2867 | f->dump_int("require_feature_tunables3", (int)has_nondefault_tunables3()); | |
2868 | f->dump_int("has_v3_rules", (int)has_v3_rules()); | |
2869 | f->dump_int("has_v4_buckets", (int)has_v4_buckets()); | |
2870 | f->dump_int("require_feature_tunables5", (int)has_nondefault_tunables5()); | |
2871 | f->dump_int("has_v5_rules", (int)has_v5_rules()); | |
2872 | } | |
2873 | ||
2874 | void CrushWrapper::dump_choose_args(Formatter *f) const | |
2875 | { | |
2876 | f->open_object_section("choose_args"); | |
2877 | for (auto c : choose_args) { | |
2878 | crush_choose_arg_map arg_map = c.second; | |
2879 | f->open_array_section(stringify(c.first).c_str()); | |
2880 | for (__u32 i = 0; i < arg_map.size; i++) { | |
2881 | crush_choose_arg *arg = &arg_map.args[i]; | |
28e407b8 | 2882 | if (arg->weight_set_positions == 0 && |
7c673cae FG |
2883 | arg->ids_size == 0) |
2884 | continue; | |
2885 | f->open_object_section("choose_args"); | |
2886 | int bucket_index = i; | |
2887 | f->dump_int("bucket_id", -1-bucket_index); | |
28e407b8 | 2888 | if (arg->weight_set_positions > 0) { |
7c673cae | 2889 | f->open_array_section("weight_set"); |
28e407b8 | 2890 | for (__u32 j = 0; j < arg->weight_set_positions; j++) { |
7c673cae FG |
2891 | f->open_array_section("weights"); |
2892 | __u32 *weights = arg->weight_set[j].weights; | |
2893 | __u32 size = arg->weight_set[j].size; | |
2894 | for (__u32 k = 0; k < size; k++) { | |
2895 | f->dump_float("weight", (float)weights[k]/(float)0x10000); | |
2896 | } | |
2897 | f->close_section(); | |
2898 | } | |
2899 | f->close_section(); | |
2900 | } | |
2901 | if (arg->ids_size > 0) { | |
2902 | f->open_array_section("ids"); | |
2903 | for (__u32 j = 0; j < arg->ids_size; j++) | |
2904 | f->dump_int("id", arg->ids[j]); | |
2905 | f->close_section(); | |
2906 | } | |
2907 | f->close_section(); | |
2908 | } | |
2909 | f->close_section(); | |
2910 | } | |
2911 | f->close_section(); | |
2912 | } | |
2913 | ||
2914 | void CrushWrapper::dump_rules(Formatter *f) const | |
2915 | { | |
2916 | for (int i=0; i<get_max_rules(); i++) { | |
2917 | if (!rule_exists(i)) | |
2918 | continue; | |
2919 | dump_rule(i, f); | |
2920 | } | |
2921 | } | |
2922 | ||
2923 | void CrushWrapper::dump_rule(int ruleset, Formatter *f) const | |
2924 | { | |
2925 | f->open_object_section("rule"); | |
2926 | f->dump_int("rule_id", ruleset); | |
2927 | if (get_rule_name(ruleset)) | |
2928 | f->dump_string("rule_name", get_rule_name(ruleset)); | |
2929 | f->dump_int("ruleset", get_rule_mask_ruleset(ruleset)); | |
2930 | f->dump_int("type", get_rule_mask_type(ruleset)); | |
2931 | f->dump_int("min_size", get_rule_mask_min_size(ruleset)); | |
2932 | f->dump_int("max_size", get_rule_mask_max_size(ruleset)); | |
2933 | f->open_array_section("steps"); | |
2934 | for (int j=0; j<get_rule_len(ruleset); j++) { | |
2935 | f->open_object_section("step"); | |
2936 | switch (get_rule_op(ruleset, j)) { | |
2937 | case CRUSH_RULE_NOOP: | |
2938 | f->dump_string("op", "noop"); | |
2939 | break; | |
2940 | case CRUSH_RULE_TAKE: | |
2941 | f->dump_string("op", "take"); | |
2942 | { | |
2943 | int item = get_rule_arg1(ruleset, j); | |
2944 | f->dump_int("item", item); | |
2945 | ||
2946 | const char *name = get_item_name(item); | |
2947 | f->dump_string("item_name", name ? name : ""); | |
2948 | } | |
2949 | break; | |
2950 | case CRUSH_RULE_EMIT: | |
2951 | f->dump_string("op", "emit"); | |
2952 | break; | |
2953 | case CRUSH_RULE_CHOOSE_FIRSTN: | |
2954 | f->dump_string("op", "choose_firstn"); | |
2955 | f->dump_int("num", get_rule_arg1(ruleset, j)); | |
2956 | f->dump_string("type", get_type_name(get_rule_arg2(ruleset, j))); | |
2957 | break; | |
2958 | case CRUSH_RULE_CHOOSE_INDEP: | |
2959 | f->dump_string("op", "choose_indep"); | |
2960 | f->dump_int("num", get_rule_arg1(ruleset, j)); | |
2961 | f->dump_string("type", get_type_name(get_rule_arg2(ruleset, j))); | |
2962 | break; | |
2963 | case CRUSH_RULE_CHOOSELEAF_FIRSTN: | |
2964 | f->dump_string("op", "chooseleaf_firstn"); | |
2965 | f->dump_int("num", get_rule_arg1(ruleset, j)); | |
2966 | f->dump_string("type", get_type_name(get_rule_arg2(ruleset, j))); | |
2967 | break; | |
2968 | case CRUSH_RULE_CHOOSELEAF_INDEP: | |
2969 | f->dump_string("op", "chooseleaf_indep"); | |
2970 | f->dump_int("num", get_rule_arg1(ruleset, j)); | |
2971 | f->dump_string("type", get_type_name(get_rule_arg2(ruleset, j))); | |
2972 | break; | |
2973 | case CRUSH_RULE_SET_CHOOSE_TRIES: | |
2974 | f->dump_string("op", "set_choose_tries"); | |
2975 | f->dump_int("num", get_rule_arg1(ruleset, j)); | |
2976 | break; | |
2977 | case CRUSH_RULE_SET_CHOOSELEAF_TRIES: | |
2978 | f->dump_string("op", "set_chooseleaf_tries"); | |
2979 | f->dump_int("num", get_rule_arg1(ruleset, j)); | |
2980 | break; | |
2981 | default: | |
2982 | f->dump_int("opcode", get_rule_op(ruleset, j)); | |
2983 | f->dump_int("arg1", get_rule_arg1(ruleset, j)); | |
2984 | f->dump_int("arg2", get_rule_arg2(ruleset, j)); | |
2985 | } | |
2986 | f->close_section(); | |
2987 | } | |
2988 | f->close_section(); | |
2989 | f->close_section(); | |
2990 | } | |
2991 | ||
2992 | void CrushWrapper::list_rules(Formatter *f) const | |
2993 | { | |
2994 | for (int rule = 0; rule < get_max_rules(); rule++) { | |
2995 | if (!rule_exists(rule)) | |
2996 | continue; | |
2997 | f->dump_string("name", get_rule_name(rule)); | |
2998 | } | |
2999 | } | |
3000 | ||
c07f9fc5 FG |
3001 | void CrushWrapper::list_rules(ostream *ss) const |
3002 | { | |
3003 | for (int rule = 0; rule < get_max_rules(); rule++) { | |
3004 | if (!rule_exists(rule)) | |
3005 | continue; | |
3006 | *ss << get_rule_name(rule) << "\n"; | |
3007 | } | |
3008 | } | |
7c673cae | 3009 | |
c07f9fc5 FG |
3010 | class CrushTreePlainDumper : public CrushTreeDumper::Dumper<TextTable> { |
3011 | public: | |
3012 | typedef CrushTreeDumper::Dumper<TextTable> Parent; | |
3013 | ||
3014 | explicit CrushTreePlainDumper(const CrushWrapper *crush, | |
3015 | const CrushTreeDumper::name_map_t& wsnames) | |
3016 | : Parent(crush, wsnames) {} | |
3017 | explicit CrushTreePlainDumper(const CrushWrapper *crush, | |
3018 | const CrushTreeDumper::name_map_t& wsnames, | |
3019 | bool show_shadow) | |
3020 | : Parent(crush, wsnames, show_shadow) {} | |
3021 | ||
3022 | ||
3023 | void dump(TextTable *tbl) { | |
3024 | tbl->define_column("ID", TextTable::LEFT, TextTable::RIGHT); | |
d2e6a577 | 3025 | tbl->define_column("CLASS", TextTable::LEFT, TextTable::RIGHT); |
c07f9fc5 FG |
3026 | tbl->define_column("WEIGHT", TextTable::LEFT, TextTable::RIGHT); |
3027 | for (auto& p : crush->choose_args) { | |
3028 | if (p.first == CrushWrapper::DEFAULT_CHOOSE_ARGS) { | |
3029 | tbl->define_column("(compat)", TextTable::LEFT, TextTable::RIGHT); | |
3030 | } else { | |
3031 | string name; | |
3032 | auto q = weight_set_names.find(p.first); | |
3033 | name = q != weight_set_names.end() ? q->second : | |
3034 | stringify(p.first); | |
3035 | tbl->define_column(name.c_str(), TextTable::LEFT, TextTable::RIGHT); | |
3036 | } | |
3037 | } | |
3038 | tbl->define_column("TYPE NAME", TextTable::LEFT, TextTable::LEFT); | |
3039 | Parent::dump(tbl); | |
7c673cae FG |
3040 | } |
3041 | ||
3042 | protected: | |
c07f9fc5 | 3043 | void dump_item(const CrushTreeDumper::Item &qi, TextTable *tbl) override { |
d2e6a577 FG |
3044 | const char *c = crush->get_item_class(qi.id); |
3045 | if (!c) | |
3046 | c = ""; | |
c07f9fc5 | 3047 | *tbl << qi.id |
d2e6a577 | 3048 | << c |
c07f9fc5 FG |
3049 | << weightf_t(qi.weight); |
3050 | for (auto& p : crush->choose_args) { | |
3051 | if (qi.parent < 0) { | |
3052 | const crush_choose_arg_map cmap = crush->choose_args_get(p.first); | |
3053 | int bidx = -1 - qi.parent; | |
3054 | const crush_bucket *b = crush->get_bucket(qi.parent); | |
3055 | if (b && | |
3056 | bidx < (int)cmap.size && | |
3057 | cmap.args[bidx].weight_set && | |
28e407b8 | 3058 | cmap.args[bidx].weight_set_positions >= 1) { |
c07f9fc5 FG |
3059 | int pos; |
3060 | for (pos = 0; | |
3061 | pos < (int)cmap.args[bidx].weight_set[0].size && | |
3062 | b->items[pos] != qi.id; | |
3063 | ++pos) ; | |
3064 | *tbl << weightf_t((float)cmap.args[bidx].weight_set[0].weights[pos] / | |
3065 | (float)0x10000); | |
3066 | continue; | |
3067 | } | |
3068 | } | |
3069 | *tbl << ""; | |
7c673cae | 3070 | } |
c07f9fc5 FG |
3071 | ostringstream ss; |
3072 | for (int k=0; k < qi.depth; k++) { | |
3073 | ss << " "; | |
7c673cae | 3074 | } |
c07f9fc5 FG |
3075 | if (qi.is_bucket()) { |
3076 | ss << crush->get_type_name(crush->get_bucket_type(qi.id)) << " " | |
3077 | << crush->get_item_name(qi.id); | |
3078 | } else { | |
3079 | ss << "osd." << qi.id; | |
3080 | } | |
3081 | *tbl << ss.str(); | |
3082 | *tbl << TextTable::endrow; | |
7c673cae FG |
3083 | } |
3084 | }; | |
3085 | ||
3086 | ||
3087 | class CrushTreeFormattingDumper : public CrushTreeDumper::FormattingDumper { | |
3088 | public: | |
3089 | typedef CrushTreeDumper::FormattingDumper Parent; | |
3090 | ||
c07f9fc5 FG |
3091 | explicit CrushTreeFormattingDumper( |
3092 | const CrushWrapper *crush, | |
3093 | const CrushTreeDumper::name_map_t& wsnames) | |
3094 | : Parent(crush, wsnames) {} | |
3095 | ||
3096 | explicit CrushTreeFormattingDumper( | |
3097 | const CrushWrapper *crush, | |
3098 | const CrushTreeDumper::name_map_t& wsnames, | |
3099 | bool show_shadow) | |
3100 | : Parent(crush, wsnames, show_shadow) {} | |
7c673cae FG |
3101 | |
3102 | void dump(Formatter *f) { | |
3103 | f->open_array_section("nodes"); | |
3104 | Parent::dump(f); | |
3105 | f->close_section(); | |
3106 | f->open_array_section("stray"); | |
3107 | f->close_section(); | |
3108 | } | |
3109 | }; | |
3110 | ||
3111 | ||
c07f9fc5 FG |
3112 | void CrushWrapper::dump_tree( |
3113 | ostream *out, | |
3114 | Formatter *f, | |
3115 | const CrushTreeDumper::name_map_t& weight_set_names, | |
3116 | bool show_shadow) const | |
7c673cae | 3117 | { |
c07f9fc5 FG |
3118 | if (out) { |
3119 | TextTable tbl; | |
3120 | CrushTreePlainDumper(this, weight_set_names, show_shadow).dump(&tbl); | |
3121 | *out << tbl; | |
3122 | } | |
3123 | if (f) { | |
3124 | CrushTreeFormattingDumper(this, weight_set_names, show_shadow).dump(f); | |
3125 | } | |
7c673cae FG |
3126 | } |
3127 | ||
3128 | void CrushWrapper::generate_test_instances(list<CrushWrapper*>& o) | |
3129 | { | |
3130 | o.push_back(new CrushWrapper); | |
3131 | // fixme | |
3132 | } | |
3133 | ||
7c673cae FG |
3134 | /** |
3135 | * Determine the default CRUSH ruleset ID to be used with | |
3136 | * newly created replicated pools. | |
3137 | * | |
3138 | * @returns a ruleset ID (>=0) or -1 if no suitable ruleset found | |
3139 | */ | |
3140 | int CrushWrapper::get_osd_pool_default_crush_replicated_ruleset(CephContext *cct) | |
3141 | { | |
31f18b77 FG |
3142 | int crush_ruleset = cct->_conf->osd_pool_default_crush_rule; |
3143 | if (crush_ruleset < 0) { | |
7c673cae FG |
3144 | crush_ruleset = find_first_ruleset(pg_pool_t::TYPE_REPLICATED); |
3145 | } else if (!ruleset_exists(crush_ruleset)) { | |
3146 | crush_ruleset = -1; // match find_first_ruleset() retval | |
3147 | } | |
7c673cae FG |
3148 | return crush_ruleset; |
3149 | } | |
3150 | ||
3151 | bool CrushWrapper::is_valid_crush_name(const string& s) | |
3152 | { | |
3153 | if (s.empty()) | |
3154 | return false; | |
3155 | for (string::const_iterator p = s.begin(); p != s.end(); ++p) { | |
3156 | if (!(*p == '-') && | |
3157 | !(*p == '_') && | |
3158 | !(*p == '.') && | |
3159 | !(*p >= '0' && *p <= '9') && | |
3160 | !(*p >= 'A' && *p <= 'Z') && | |
3161 | !(*p >= 'a' && *p <= 'z')) | |
3162 | return false; | |
3163 | } | |
3164 | return true; | |
3165 | } | |
3166 | ||
3167 | bool CrushWrapper::is_valid_crush_loc(CephContext *cct, | |
3168 | const map<string,string>& loc) | |
3169 | { | |
3170 | for (map<string,string>::const_iterator l = loc.begin(); l != loc.end(); ++l) { | |
3171 | if (!is_valid_crush_name(l->first) || | |
3172 | !is_valid_crush_name(l->second)) { | |
3173 | ldout(cct, 1) << "loc[" | |
3174 | << l->first << "] = '" | |
3175 | << l->second << "' not a valid crush name ([A-Za-z0-9_-.]+)" | |
3176 | << dendl; | |
3177 | return false; | |
3178 | } | |
3179 | } | |
3180 | return true; | |
3181 | } | |
3182 | ||
3183 | int CrushWrapper::_choose_type_stack( | |
3184 | CephContext *cct, | |
3185 | const vector<pair<int,int>>& stack, | |
3186 | const set<int>& overfull, | |
3187 | const vector<int>& underfull, | |
3188 | const vector<int>& orig, | |
3189 | vector<int>::const_iterator& i, | |
3190 | set<int>& used, | |
3191 | vector<int> *pw) const | |
3192 | { | |
3193 | vector<int> w = *pw; | |
3194 | vector<int> o; | |
3195 | ||
3196 | ldout(cct, 10) << __func__ << " stack " << stack | |
3197 | << " orig " << orig | |
3198 | << " at " << *i | |
3199 | << " pw " << *pw | |
3200 | << dendl; | |
3201 | ||
3202 | vector<int> cumulative_fanout(stack.size()); | |
3203 | int f = 1; | |
3204 | for (int j = (int)stack.size() - 1; j >= 0; --j) { | |
3205 | cumulative_fanout[j] = f; | |
3206 | f *= stack[j].second; | |
3207 | } | |
3208 | ldout(cct, 10) << __func__ << " cumulative_fanout " << cumulative_fanout | |
3209 | << dendl; | |
3210 | ||
31f18b77 FG |
3211 | // identify underful targets for each intermediate level. |
3212 | // this serves two purposes: | |
3213 | // 1. we can tell when we are selecting a bucket that does not have any underfull | |
3214 | // devices beneath it. that means that if the current input includes an overfull | |
3215 | // device, we won't be able to find an underfull device with this parent to | |
3216 | // swap for it. | |
3217 | // 2. when we decide we should reject a bucket due to the above, this list gives us | |
3218 | // a list of peers to consider that *do* have underfull devices available.. (we | |
3219 | // are careful to pick one that has the same parent.) | |
3220 | vector<set<int>> underfull_buckets; // level -> set of buckets with >0 underfull item(s) | |
3221 | underfull_buckets.resize(stack.size() - 1); | |
3222 | for (auto osd : underfull) { | |
3223 | int item = osd; | |
3224 | for (int j = (int)stack.size() - 2; j >= 0; --j) { | |
3225 | int type = stack[j].first; | |
3226 | item = get_parent_of_type(item, type); | |
3227 | ldout(cct, 10) << __func__ << " underfull " << osd << " type " << type | |
3228 | << " is " << item << dendl; | |
3229 | underfull_buckets[j].insert(item); | |
3230 | } | |
3231 | } | |
3232 | ldout(cct, 20) << __func__ << " underfull_buckets " << underfull_buckets << dendl; | |
3233 | ||
7c673cae FG |
3234 | for (unsigned j = 0; j < stack.size(); ++j) { |
3235 | int type = stack[j].first; | |
3236 | int fanout = stack[j].second; | |
3237 | int cum_fanout = cumulative_fanout[j]; | |
3238 | ldout(cct, 10) << " level " << j << ": type " << type << " fanout " << fanout | |
3239 | << " cumulative " << cum_fanout | |
3240 | << " w " << w << dendl; | |
3241 | vector<int> o; | |
3242 | auto tmpi = i; | |
28e407b8 AA |
3243 | if (i == orig.end()) { |
3244 | ldout(cct, 10) << __func__ << " end of orig, break 0" << dendl; | |
3245 | break; | |
3246 | } | |
7c673cae FG |
3247 | for (auto from : w) { |
3248 | ldout(cct, 10) << " from " << from << dendl; | |
31f18b77 FG |
3249 | // identify leaves under each choice. we use this to check whether any of these |
3250 | // leaves are overfull. (if so, we need to make sure there are underfull candidates | |
3251 | // to swap for them.) | |
3252 | vector<set<int>> leaves; | |
3253 | leaves.resize(fanout); | |
7c673cae FG |
3254 | for (int pos = 0; pos < fanout; ++pos) { |
3255 | if (type > 0) { | |
3256 | // non-leaf | |
31f18b77 | 3257 | int item = get_parent_of_type(*tmpi, type); |
7c673cae | 3258 | o.push_back(item); |
7c673cae | 3259 | int n = cum_fanout; |
31f18b77 FG |
3260 | while (n-- && tmpi != orig.end()) { |
3261 | leaves[pos].insert(*tmpi++); | |
3262 | } | |
3263 | ldout(cct, 10) << __func__ << " from " << *tmpi << " got " << item | |
3264 | << " of type " << type << " over leaves " << leaves[pos] << dendl; | |
7c673cae FG |
3265 | } else { |
3266 | // leaf | |
3267 | bool replaced = false; | |
3268 | if (overfull.count(*i)) { | |
3269 | for (auto item : underfull) { | |
3270 | ldout(cct, 10) << __func__ << " pos " << pos | |
3271 | << " was " << *i << " considering " << item | |
3272 | << dendl; | |
3273 | if (used.count(item)) { | |
3274 | ldout(cct, 20) << __func__ << " in used " << used << dendl; | |
3275 | continue; | |
3276 | } | |
3277 | if (!subtree_contains(from, item)) { | |
3278 | ldout(cct, 20) << __func__ << " not in subtree " << from << dendl; | |
3279 | continue; | |
3280 | } | |
3281 | if (std::find(orig.begin(), orig.end(), item) != orig.end()) { | |
3282 | ldout(cct, 20) << __func__ << " in orig " << orig << dendl; | |
3283 | continue; | |
3284 | } | |
3285 | o.push_back(item); | |
3286 | used.insert(item); | |
3287 | ldout(cct, 10) << __func__ << " pos " << pos << " replace " | |
3288 | << *i << " -> " << item << dendl; | |
3289 | replaced = true; | |
28e407b8 | 3290 | assert(i != orig.end()); |
7c673cae FG |
3291 | ++i; |
3292 | break; | |
3293 | } | |
3294 | } | |
3295 | if (!replaced) { | |
3296 | ldout(cct, 10) << __func__ << " pos " << pos << " keep " << *i | |
3297 | << dendl; | |
28e407b8 | 3298 | assert(i != orig.end()); |
7c673cae FG |
3299 | o.push_back(*i); |
3300 | ++i; | |
3301 | } | |
3302 | if (i == orig.end()) { | |
3303 | ldout(cct, 10) << __func__ << " end of orig, break 1" << dendl; | |
3304 | break; | |
3305 | } | |
3306 | } | |
3307 | } | |
31f18b77 FG |
3308 | if (j + 1 < stack.size()) { |
3309 | // check if any buckets have overfull leaves but no underfull candidates | |
3310 | for (int pos = 0; pos < fanout; ++pos) { | |
3311 | if (underfull_buckets[j].count(o[pos]) == 0) { | |
3312 | // are any leaves overfull? | |
3313 | bool any_overfull = false; | |
3314 | for (auto osd : leaves[pos]) { | |
3315 | if (overfull.count(osd)) { | |
3316 | any_overfull = true; | |
3317 | } | |
3318 | } | |
3319 | if (any_overfull) { | |
3320 | ldout(cct, 10) << " bucket " << o[pos] << " has no underfull targets and " | |
3321 | << ">0 leaves " << leaves[pos] << " is overfull; alts " | |
3322 | << underfull_buckets[j] | |
3323 | << dendl; | |
3324 | for (auto alt : underfull_buckets[j]) { | |
3325 | if (std::find(o.begin(), o.end(), alt) == o.end()) { | |
3326 | // see if alt has the same parent | |
3327 | if (j == 0 || | |
3328 | get_parent_of_type(o[pos], stack[j-1].first) == | |
3329 | get_parent_of_type(alt, stack[j-1].first)) { | |
3330 | if (j) | |
3331 | ldout(cct, 10) << " replacing " << o[pos] | |
3332 | << " (which has no underfull leaves) with " << alt | |
3333 | << " (same parent " | |
3334 | << get_parent_of_type(alt, stack[j-1].first) << " type " | |
3335 | << type << ")" << dendl; | |
3336 | else | |
3337 | ldout(cct, 10) << " replacing " << o[pos] | |
3338 | << " (which has no underfull leaves) with " << alt | |
3339 | << " (first level)" << dendl; | |
3340 | o[pos] = alt; | |
3341 | break; | |
3342 | } else { | |
3343 | ldout(cct, 30) << " alt " << alt << " for " << o[pos] | |
3344 | << " has different parent, skipping" << dendl; | |
3345 | } | |
3346 | } | |
3347 | } | |
3348 | } | |
3349 | } | |
3350 | } | |
3351 | } | |
7c673cae FG |
3352 | if (i == orig.end()) { |
3353 | ldout(cct, 10) << __func__ << " end of orig, break 2" << dendl; | |
3354 | break; | |
3355 | } | |
3356 | } | |
3357 | ldout(cct, 10) << __func__ << " w <- " << o << " was " << w << dendl; | |
3358 | w.swap(o); | |
3359 | } | |
3360 | *pw = w; | |
3361 | return 0; | |
3362 | } | |
3363 | ||
3364 | int CrushWrapper::try_remap_rule( | |
3365 | CephContext *cct, | |
3366 | int ruleno, | |
3367 | int maxout, | |
3368 | const set<int>& overfull, | |
3369 | const vector<int>& underfull, | |
3370 | const vector<int>& orig, | |
3371 | vector<int> *out) const | |
3372 | { | |
3373 | const crush_map *map = crush; | |
3374 | const crush_rule *rule = get_rule(ruleno); | |
3375 | assert(rule); | |
3376 | ||
3377 | ldout(cct, 10) << __func__ << " ruleno " << ruleno | |
3378 | << " numrep " << maxout << " overfull " << overfull | |
3379 | << " underfull " << underfull << " orig " << orig | |
3380 | << dendl; | |
3381 | vector<int> w; // working set | |
3382 | out->clear(); | |
3383 | ||
3384 | auto i = orig.begin(); | |
3385 | set<int> used; | |
3386 | ||
3387 | vector<pair<int,int>> type_stack; // (type, fan-out) | |
3388 | ||
3389 | for (unsigned step = 0; step < rule->len; ++step) { | |
3390 | const crush_rule_step *curstep = &rule->steps[step]; | |
3391 | ldout(cct, 10) << __func__ << " step " << step << " w " << w << dendl; | |
3392 | switch (curstep->op) { | |
3393 | case CRUSH_RULE_TAKE: | |
3394 | if ((curstep->arg1 >= 0 && curstep->arg1 < map->max_devices) || | |
3395 | (-1-curstep->arg1 >= 0 && -1-curstep->arg1 < map->max_buckets && | |
3396 | map->buckets[-1-curstep->arg1])) { | |
3397 | w.clear(); | |
3398 | w.push_back(curstep->arg1); | |
3399 | ldout(cct, 10) << __func__ << " take " << w << dendl; | |
3400 | } else { | |
3401 | ldout(cct, 1) << " bad take value " << curstep->arg1 << dendl; | |
3402 | } | |
3403 | break; | |
3404 | ||
3405 | case CRUSH_RULE_CHOOSELEAF_FIRSTN: | |
3406 | case CRUSH_RULE_CHOOSELEAF_INDEP: | |
3407 | { | |
3408 | int numrep = curstep->arg1; | |
3409 | int type = curstep->arg2; | |
3410 | if (numrep <= 0) | |
3411 | numrep += maxout; | |
3412 | type_stack.push_back(make_pair(type, numrep)); | |
28e407b8 AA |
3413 | if (type > 0) |
3414 | type_stack.push_back(make_pair(0, 1)); | |
7c673cae FG |
3415 | int r = _choose_type_stack(cct, type_stack, overfull, underfull, orig, |
3416 | i, used, &w); | |
3417 | if (r < 0) | |
3418 | return r; | |
3419 | type_stack.clear(); | |
3420 | } | |
3421 | break; | |
3422 | ||
3423 | case CRUSH_RULE_CHOOSE_FIRSTN: | |
3424 | case CRUSH_RULE_CHOOSE_INDEP: | |
3425 | { | |
3426 | int numrep = curstep->arg1; | |
3427 | int type = curstep->arg2; | |
3428 | if (numrep <= 0) | |
3429 | numrep += maxout; | |
3430 | type_stack.push_back(make_pair(type, numrep)); | |
3431 | } | |
3432 | break; | |
3433 | ||
3434 | case CRUSH_RULE_EMIT: | |
3435 | ldout(cct, 10) << " emit " << w << dendl; | |
3436 | if (!type_stack.empty()) { | |
3437 | int r = _choose_type_stack(cct, type_stack, overfull, underfull, orig, | |
3438 | i, used, &w); | |
3439 | if (r < 0) | |
3440 | return r; | |
3441 | type_stack.clear(); | |
3442 | } | |
3443 | for (auto item : w) { | |
3444 | out->push_back(item); | |
3445 | } | |
3446 | w.clear(); | |
3447 | break; | |
3448 | ||
3449 | default: | |
3450 | // ignore | |
3451 | break; | |
3452 | } | |
3453 | } | |
3454 | ||
3455 | return 0; | |
3456 | } | |
c07f9fc5 FG |
3457 | |
3458 | ||
3459 | int CrushWrapper::_choose_args_adjust_item_weight_in_bucket( | |
3460 | CephContext *cct, | |
3461 | crush_choose_arg_map cmap, | |
3462 | int bucketid, | |
3463 | int id, | |
3464 | const vector<int>& weight, | |
3465 | ostream *ss) | |
3466 | { | |
3467 | int changed = 0; | |
3468 | int bidx = -1 - bucketid; | |
3469 | crush_bucket *b = crush->buckets[bidx]; | |
3470 | if (bidx >= (int)cmap.size) { | |
3471 | if (ss) | |
3472 | *ss << "no weight-set for bucket " << b->id; | |
3473 | ldout(cct, 10) << __func__ << " no crush_choose_arg for bucket " << b->id | |
3474 | << dendl; | |
3475 | return 0; | |
3476 | } | |
3477 | crush_choose_arg *carg = &cmap.args[bidx]; | |
3478 | if (carg->weight_set == NULL) { | |
28e407b8 AA |
3479 | // create a weight-set for this bucket and populate it with the |
3480 | // bucket weights | |
3481 | unsigned positions = get_choose_args_positions(cmap); | |
3482 | carg->weight_set_positions = positions; | |
3483 | carg->weight_set = static_cast<crush_weight_set*>( | |
3484 | calloc(sizeof(crush_weight_set), positions)); | |
3485 | for (unsigned p = 0; p < positions; ++p) { | |
3486 | carg->weight_set[p].size = b->size; | |
3487 | carg->weight_set[p].weights = (__u32*)calloc(b->size, sizeof(__u32)); | |
3488 | for (unsigned i = 0; i < b->size; ++i) { | |
3489 | carg->weight_set[p].weights[i] = crush_get_bucket_item_weight(b, i); | |
3490 | } | |
3491 | } | |
3492 | changed++; | |
c07f9fc5 | 3493 | } |
28e407b8 | 3494 | if (carg->weight_set_positions != weight.size()) { |
c07f9fc5 | 3495 | if (ss) |
28e407b8 AA |
3496 | *ss << "weight_set_positions != " << weight.size() << " for bucket " << b->id; |
3497 | ldout(cct, 10) << __func__ << " weight_set_positions != " << weight.size() | |
c07f9fc5 FG |
3498 | << " for bucket " << b->id << dendl; |
3499 | return 0; | |
3500 | } | |
3501 | for (unsigned i = 0; i < b->size; i++) { | |
3502 | if (b->items[i] == id) { | |
3503 | for (unsigned j = 0; j < weight.size(); ++j) { | |
3504 | carg->weight_set[j].weights[i] = weight[j]; | |
3505 | } | |
3506 | ldout(cct, 5) << __func__ << " set " << id << " to " << weight | |
3507 | << " in bucket " << b->id << dendl; | |
3508 | changed++; | |
3509 | } | |
3510 | } | |
3511 | if (changed) { | |
3512 | vector<int> bucket_weight(weight.size(), 0); | |
3513 | for (unsigned i = 0; i < b->size; i++) { | |
3514 | for (unsigned j = 0; j < weight.size(); ++j) { | |
3515 | bucket_weight[j] += carg->weight_set[j].weights[i]; | |
3516 | } | |
3517 | } | |
3518 | choose_args_adjust_item_weight(cct, cmap, b->id, bucket_weight, nullptr); | |
3519 | } | |
3520 | return changed; | |
3521 | } | |
3522 | ||
3523 | int CrushWrapper::choose_args_adjust_item_weight( | |
3524 | CephContext *cct, | |
3525 | crush_choose_arg_map cmap, | |
3526 | int id, | |
3527 | const vector<int>& weight, | |
3528 | ostream *ss) | |
3529 | { | |
3530 | ldout(cct, 5) << __func__ << " " << id << " weight " << weight << dendl; | |
3531 | int changed = 0; | |
3532 | for (int bidx = 0; bidx < crush->max_buckets; bidx++) { | |
3533 | crush_bucket *b = crush->buckets[bidx]; | |
3534 | if (b == nullptr) { | |
3535 | continue; | |
3536 | } | |
3537 | changed += _choose_args_adjust_item_weight_in_bucket( | |
3538 | cct, cmap, b->id, id, weight, ss); | |
3539 | } | |
3540 | if (!changed) { | |
3541 | if (ss) | |
3542 | *ss << "item " << id << " not found in crush map"; | |
3543 | return -ENOENT; | |
3544 | } | |
3545 | return changed; | |
3546 | } |