1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "osd/osd_types.h"
5 #include "common/debug.h"
6 #include "common/Formatter.h"
7 #include "common/errno.h"
8 #include "common/TextTable.h"
9 #include "include/stringify.h"
11 #include "CrushWrapper.h"
12 #include "CrushTreeDumper.h"
14 #define dout_subsys ceph_subsys_crush
16 bool CrushWrapper::has_legacy_rule_ids() const
18 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
19 crush_rule
*r
= crush
->rules
[i
];
21 r
->mask
.ruleset
!= i
) {
28 std::map
<int, int> CrushWrapper::renumber_rules()
30 std::map
<int, int> result
;
31 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
32 crush_rule
*r
= crush
->rules
[i
];
33 if (r
&& r
->mask
.ruleset
!= i
) {
34 result
[r
->mask
.ruleset
] = i
;
41 bool CrushWrapper::has_non_straw2_buckets() const
43 for (int i
=0; i
<crush
->max_buckets
; ++i
) {
44 crush_bucket
*b
= crush
->buckets
[i
];
47 if (b
->alg
!= CRUSH_BUCKET_STRAW2
)
53 bool CrushWrapper::has_v2_rules() const
55 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
63 bool CrushWrapper::is_v2_rule(unsigned ruleid
) const
65 // check rule for use of indep or new SET_* rule steps
66 if (ruleid
>= crush
->max_rules
)
68 crush_rule
*r
= crush
->rules
[ruleid
];
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
) {
82 bool CrushWrapper::has_v3_rules() const
84 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
92 bool CrushWrapper::is_v3_rule(unsigned ruleid
) const
94 // check rule for use of SET_CHOOSELEAF_VARY_R step
95 if (ruleid
>= crush
->max_rules
)
97 crush_rule
*r
= crush
->rules
[ruleid
];
100 for (unsigned j
=0; j
<r
->len
; j
++) {
101 if (r
->steps
[j
].op
== CRUSH_RULE_SET_CHOOSELEAF_VARY_R
) {
108 bool CrushWrapper::has_v4_buckets() const
110 for (int i
=0; i
<crush
->max_buckets
; ++i
) {
111 crush_bucket
*b
= crush
->buckets
[i
];
114 if (b
->alg
== CRUSH_BUCKET_STRAW2
)
120 bool CrushWrapper::has_v5_rules() const
122 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
130 bool CrushWrapper::is_v5_rule(unsigned ruleid
) const
132 // check rule for use of SET_CHOOSELEAF_STABLE step
133 if (ruleid
>= crush
->max_rules
)
135 crush_rule
*r
= crush
->rules
[ruleid
];
138 for (unsigned j
=0; j
<r
->len
; j
++) {
139 if (r
->steps
[j
].op
== CRUSH_RULE_SET_CHOOSELEAF_STABLE
) {
146 bool CrushWrapper::has_choose_args() const
148 return !choose_args
.empty();
151 bool CrushWrapper::has_incompat_choose_args() const
153 if (choose_args
.empty())
155 if (choose_args
.size() > 1)
157 if (choose_args
.begin()->first
!= DEFAULT_CHOOSE_ARGS
)
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
];
162 if (arg
->weight_set_positions
== 0 &&
165 if (arg
->weight_set_positions
!= 1)
167 if (arg
->ids_size
!= 0)
173 int CrushWrapper::split_id_class(int i
, int *idout
, int *classout
) const
177 string name
= get_item_name(i
);
178 size_t pos
= name
.find("~");
179 if (pos
== string::npos
) {
184 string name_no_class
= name
.substr(0, pos
);
185 if (!name_exists(name_no_class
))
187 string class_name
= name
.substr(pos
+ 1);
188 if (!class_exists(class_name
))
190 *idout
= get_item_id(name_no_class
);
191 *classout
= get_class_id(class_name
);
195 int CrushWrapper::can_rename_item(const string
& srcname
,
196 const string
& dstname
,
199 if (name_exists(srcname
)) {
200 if (name_exists(dstname
)) {
201 *ss
<< "dstname = '" << dstname
<< "' already exists";
204 if (is_valid_crush_name(dstname
)) {
207 *ss
<< "dstname = '" << dstname
<< "' does not match [-_.0-9a-zA-Z]+";
211 if (name_exists(dstname
)) {
212 *ss
<< "srcname = '" << srcname
<< "' does not exist "
213 << "and dstname = '" << dstname
<< "' already exists";
216 *ss
<< "srcname = '" << srcname
<< "' does not exist";
222 int CrushWrapper::rename_item(const string
& srcname
,
223 const string
& dstname
,
226 int ret
= can_rename_item(srcname
, dstname
, ss
);
229 int oldid
= get_item_id(srcname
);
230 return set_item_name(oldid
, dstname
);
233 int CrushWrapper::can_rename_bucket(const string
& srcname
,
234 const string
& dstname
,
237 int ret
= can_rename_item(srcname
, dstname
, ss
);
240 int srcid
= get_item_id(srcname
);
242 *ss
<< "srcname = '" << srcname
<< "' is not a bucket "
243 << "because its id = " << srcid
<< " is >= 0";
249 int CrushWrapper::rename_bucket(const string
& srcname
,
250 const string
& dstname
,
253 int ret
= can_rename_bucket(srcname
, dstname
, ss
);
256 int oldid
= get_item_id(srcname
);
257 return set_item_name(oldid
, dstname
);
260 int CrushWrapper::rename_rule(const string
& srcname
,
261 const string
& dstname
,
264 if (!rule_exists(srcname
)) {
266 *ss
<< "source rule name '" << srcname
<< "' does not exist";
270 if (rule_exists(dstname
)) {
272 *ss
<< "destination rule name '" << dstname
<< "' already exists";
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
;
281 rule_name_rmap
.erase(srcname
);
282 rule_name_rmap
[dstname
] = rule_id
;
287 void CrushWrapper::find_takes(set
<int> *roots
) const
289 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
290 crush_rule
*r
= crush
->rules
[i
];
293 for (unsigned j
=0; j
<r
->len
; j
++) {
294 if (r
->steps
[j
].op
== CRUSH_RULE_TAKE
)
295 roots
->insert(r
->steps
[j
].arg1
);
300 void CrushWrapper::find_takes_by_rule(int rule
, set
<int> *roots
) const
302 if (rule
< 0 || rule
>= (int)crush
->max_rules
)
304 crush_rule
*r
= crush
->rules
[rule
];
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
);
313 void CrushWrapper::find_roots(set
<int> *roots
) const
315 for (int i
= 0; i
< crush
->max_buckets
; i
++) {
316 if (!crush
->buckets
[i
])
318 crush_bucket
*b
= crush
->buckets
[i
];
319 if (!_search_item_exists(b
->id
))
320 roots
->insert(b
->id
);
324 bool CrushWrapper::subtree_contains(int root
, int item
) const
330 return false; // root is a leaf
332 const crush_bucket
*b
= get_bucket(root
);
336 for (unsigned j
=0; j
<b
->size
; j
++) {
337 if (subtree_contains(b
->items
[j
], item
))
343 bool CrushWrapper::_maybe_remove_last_instance(CephContext
*cct
, int item
, bool unlink_only
)
346 if (_search_item_exists(item
)) {
349 if (item
< 0 && _bucket_is_in_use(item
)) {
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
);
359 class_remove_item(item
);
360 update_choose_args(cct
);
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
);
366 if (item
>= 0 && !unlink_only
) {
367 class_remove_item(item
);
370 rebuild_roots_with_classes();
374 int CrushWrapper::remove_root(int item
)
376 crush_bucket
*b
= get_bucket(item
);
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.
387 for (unsigned n
= 0; n
< b
->size
; n
++) {
388 if (b
->items
[n
] >= 0)
390 int r
= remove_root(b
->items
[n
]);
395 crush_remove_bucket(crush
, b
);
396 if (name_map
.count(item
) != 0) {
397 name_map
.erase(item
);
400 if (class_bucket
.count(item
) != 0)
401 class_bucket
.erase(item
);
402 class_remove_item(item
);
403 update_choose_args(nullptr);
407 void CrushWrapper::update_choose_args(CephContext
*cct
)
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
) {
419 ldout(cct
,0) << __func__
<< " removing " << i
.first
<< " bucket "
420 << (-1-j
) << " ids" << dendl
;
425 if (carg
.weight_set
) {
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
);
432 free(carg
.weight_set
);
434 carg
.weight_set_positions
= 0;
438 if (carg
.weight_set_positions
== 0) {
441 if (carg
.weight_set_positions
!= positions
) {
443 lderr(cct
) << __func__
<< " " << i
.first
<< " bucket "
444 << (-1-j
) << " positions " << carg
.weight_set_positions
445 << " -> " << positions
<< dendl
;
446 continue; // wth... skip!
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
) {
452 lderr(cct
) << __func__
<< " fixing " << i
.first
<< " bucket "
453 << (-1-j
) << " position " << p
454 << " size " << carg
.weight_set
[p
].size
<< " -> "
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
];
463 free(old_ws
.weights
);
470 int CrushWrapper::remove_item(CephContext
*cct
, int item
, bool unlink_only
)
472 ldout(cct
, 5) << "remove_item " << item
473 << (unlink_only
? " unlink_only":"") << dendl
;
477 if (item
< 0 && !unlink_only
) {
478 crush_bucket
*t
= get_bucket(item
);
480 ldout(cct
, 1) << "remove_item bucket " << item
<< " does not exist"
486 ldout(cct
, 1) << "remove_item bucket " << item
<< " has " << t
->size
487 << " items, not empty" << dendl
;
490 if (_bucket_is_in_use(item
)) {
495 for (int i
= 0; i
< crush
->max_buckets
; i
++) {
496 if (!crush
->buckets
[i
])
498 crush_bucket
*b
= crush
->buckets
[i
];
500 for (unsigned i
=0; i
<b
->size
; ++i
) {
501 int id
= b
->items
[i
];
503 ldout(cct
, 5) << "remove_item removing item " << item
504 << " from bucket " << b
->id
<< dendl
;
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);
510 bucket_remove_item(b
, item
);
511 adjust_item_weight(cct
, b
->id
, b
->weight
);
517 if (_maybe_remove_last_instance(cct
, item
, unlink_only
))
523 bool CrushWrapper::_search_item_exists(int item
) const
525 for (int i
= 0; i
< crush
->max_buckets
; i
++) {
526 if (!crush
->buckets
[i
])
528 crush_bucket
*b
= crush
->buckets
[i
];
529 for (unsigned j
=0; j
<b
->size
; ++j
) {
530 if (b
->items
[j
] == item
)
537 bool CrushWrapper::_bucket_is_in_use(int item
)
539 for (auto &i
: class_bucket
)
540 for (auto &j
: i
.second
)
541 if (j
.second
== item
)
543 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
544 crush_rule
*r
= crush
->rules
[i
];
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
;
552 int res
= split_id_class(step_item
, &original_item
, &c
);
555 if (step_item
== item
|| original_item
== item
)
563 int CrushWrapper::_remove_item_under(
564 CephContext
*cct
, int item
, int ancestor
, bool unlink_only
)
566 ldout(cct
, 5) << "_remove_item_under " << item
<< " under " << ancestor
567 << (unlink_only
? " unlink_only":"") << dendl
;
573 if (!bucket_exists(ancestor
))
578 crush_bucket
*b
= get_bucket(ancestor
);
579 for (unsigned i
=0; i
<b
->size
; ++i
) {
580 int id
= b
->items
[i
];
582 ldout(cct
, 5) << "_remove_item_under removing item " << item
583 << " from bucket " << b
->id
<< dendl
;
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);
590 bucket_remove_item(b
, item
);
591 adjust_item_weight(cct
, b
->id
, b
->weight
);
594 int r
= remove_item_under(cct
, item
, id
, unlink_only
);
602 int CrushWrapper::remove_item_under(
603 CephContext
*cct
, int item
, int ancestor
, bool unlink_only
)
605 ldout(cct
, 5) << "remove_item_under " << item
<< " under " << ancestor
606 << (unlink_only
? " unlink_only":"") << dendl
;
608 if (!unlink_only
&& _bucket_is_in_use(item
)) {
612 int ret
= _remove_item_under(cct
, item
, ancestor
, unlink_only
);
616 if (item
< 0 && !unlink_only
) {
617 crush_bucket
*t
= get_bucket(item
);
619 ldout(cct
, 1) << "remove_item_under bucket " << item
620 << " does not exist" << dendl
;
625 ldout(cct
, 1) << "remove_item_under bucket " << item
<< " has " << t
->size
626 << " items, not empty" << dendl
;
631 if (_maybe_remove_last_instance(cct
, item
, unlink_only
))
637 int CrushWrapper::get_common_ancestor_distance(CephContext
*cct
, int id
,
638 const std::multimap
<string
,string
>& loc
)
640 ldout(cct
, 5) << __func__
<< " " << id
<< " " << loc
<< dendl
;
641 if (!item_exists(id
))
643 map
<string
,string
> id_loc
= get_full_location(id
);
644 ldout(cct
, 20) << " id is at " << id_loc
<< dendl
;
646 for (map
<int,string
>::const_iterator p
= type_map
.begin();
649 map
<string
,string
>::iterator ip
= id_loc
.find(p
->second
);
650 if (ip
== id_loc
.end())
652 for (std::multimap
<string
,string
>::const_iterator q
= loc
.find(p
->second
);
655 if (q
->first
!= p
->second
)
657 if (q
->second
== ip
->second
)
664 int CrushWrapper::parse_loc_map(const std::vector
<string
>& args
,
665 std::map
<string
,string
> *ploc
)
668 for (unsigned i
= 0; i
< args
.size(); ++i
) {
669 const char *s
= args
[i
].c_str();
670 const char *pos
= strchr(s
, '=');
673 string
key(s
, 0, pos
-s
);
676 (*ploc
)[key
] = value
;
683 int CrushWrapper::parse_loc_multimap(const std::vector
<string
>& args
,
684 std::multimap
<string
,string
> *ploc
)
687 for (unsigned i
= 0; i
< args
.size(); ++i
) {
688 const char *s
= args
[i
].c_str();
689 const char *pos
= strchr(s
, '=');
692 string
key(s
, 0, pos
-s
);
695 ploc
->insert(make_pair(key
, value
));
702 bool CrushWrapper::check_item_loc(CephContext
*cct
, int item
, const map
<string
,string
>& loc
,
705 ldout(cct
, 5) << "check_item_loc item " << item
<< " loc " << loc
<< dendl
;
707 for (map
<int,string
>::const_iterator p
= type_map
.begin(); p
!= type_map
.end(); ++p
) {
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
;
720 if (!name_exists(q
->second
)) {
721 ldout(cct
, 5) << "check_item_loc bucket " << q
->second
<< " dne" << dendl
;
725 int id
= get_item_id(q
->second
);
727 ldout(cct
, 5) << "check_item_loc requested " << q
->second
<< " for type " << p
->second
728 << " is a device, not bucket" << dendl
;
732 assert(bucket_exists(id
));
733 crush_bucket
*b
= get_bucket(id
);
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
;
740 *weight
= crush_get_bucket_item_weight(b
, j
);
747 ldout(cct
, 2) << __func__
<< " item " << item
<< " loc " << loc
<< dendl
;
751 map
<string
, string
> CrushWrapper::get_full_location(int id
)
753 vector
<pair
<string
, string
> > full_location_ordered
;
754 map
<string
,string
> full_location
;
756 get_full_location_ordered(id
, full_location_ordered
);
758 std::copy(full_location_ordered
.begin(),
759 full_location_ordered
.end(),
760 std::inserter(full_location
, full_location
.begin()));
762 return full_location
;
765 int CrushWrapper::get_full_location_ordered(int id
, vector
<pair
<string
, string
> >& path
)
767 if (!item_exists(id
))
772 pair
<string
, string
> parent_coord
= get_immediate_parent(cur
, &ret
);
775 path
.push_back(parent_coord
);
776 cur
= get_item_id(parent_coord
.second
);
781 string
CrushWrapper::get_full_location_ordered_string(int id
)
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
+ ",";
793 return full_location
;
796 map
<int, string
> CrushWrapper::get_parent_hierarchy(int id
)
798 map
<int,string
> parent_hierarchy
;
799 pair
<string
, string
> parent_coord
= get_immediate_parent(id
);
802 // get the integer type for id and create a counter from there
803 int type_counter
= get_bucket_type(id
);
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)
810 // read the type map and get the name of the type with the largest ID
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
;
817 parent_id
= get_item_id(parent_coord
.second
);
819 while (type_counter
< high_type
) {
821 parent_hierarchy
[ type_counter
] = parent_coord
.first
;
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
);
830 return parent_hierarchy
;
833 int CrushWrapper::get_children(int id
, list
<int> *children
)
840 crush_bucket
*b
= get_bucket(id
);
845 for (unsigned n
=0; n
<b
->size
; n
++) {
846 children
->push_back(b
->items
[n
]);
851 void CrushWrapper::get_children_of_type(int id
,
854 bool exclude_shadow
) const
859 children
->insert(id
);
863 auto b
= get_bucket(id
);
867 if (b
->type
< type
) {
870 } else if (b
->type
== type
) {
871 if (!is_shadow_item(b
->id
) || !exclude_shadow
) {
872 children
->insert(b
->id
);
876 for (unsigned n
= 0; n
< b
->size
; n
++) {
877 get_children_of_type(b
->items
[n
], type
, children
, exclude_shadow
);
881 int CrushWrapper::get_rule_failure_domain(int rule_id
)
883 crush_rule
*rule
= get_rule(rule_id
);
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
;
900 int CrushWrapper::_get_leaves(int id
, list
<int> *leaves
)
906 leaves
->push_back(id
);
910 crush_bucket
*b
= get_bucket(id
);
915 for (unsigned n
= 0; n
< b
->size
; n
++) {
916 if (b
->items
[n
] >= 0) {
917 leaves
->push_back(b
->items
[n
]);
919 // is a bucket, do recursive call
920 int r
= _get_leaves(b
->items
[n
], leaves
);
927 return 0; // all is well
930 int CrushWrapper::get_leaves(const string
&name
, set
<int> *leaves
)
935 if (!name_exists(name
)) {
939 int id
= get_item_id(name
);
947 int r
= _get_leaves(id
, &unordered
);
952 for (auto &p
: unordered
) {
959 int CrushWrapper::insert_item(
960 CephContext
*cct
, int item
, float weight
, string name
,
961 const map
<string
,string
>& loc
) // typename -> bucketname
963 ldout(cct
, 5) << "insert_item item " << item
<< " weight " << weight
964 << " name " << name
<< " loc " << loc
<< dendl
;
966 if (!is_valid_crush_name(name
))
969 if (!is_valid_crush_loc(cct
, loc
))
972 int r
= validate_weightf(weight
);
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
;
984 set_item_name(item
, name
);
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
) {
993 // ignore device type
997 // skip types that are unspecified
998 map
<string
,string
>::const_iterator q
= loc
.find(p
->second
);
999 if (q
== loc
.end()) {
1000 ldout(cct
, 2) << "warning: did not specify location for '"
1001 << p
->second
<< "' level (levels are "
1002 << type_map
<< ")" << dendl
;
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
);
1012 ldout(cct
, 1) << "add_bucket failure error: " << cpp_strerror(r
)
1016 set_item_name(newid
, q
->second
);
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
;
1029 // check that we aren't creating a cycle.
1030 if (subtree_contains(id
, cur
)) {
1031 ldout(cct
, 1) << "insert_item item " << cur
<< " already exists beneath "
1036 // we have done sanity check above
1037 crush_bucket
*b
= get_bucket(id
);
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
;
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
;
1053 ldout(cct
, 5) << "insert_item adding " << cur
<< " weight " << weight
1054 << " to bucket " << id
<< dendl
;
1055 int r
= bucket_add_item(b
, cur
, 0);
1060 // adjust the item's weight in location
1061 if (adjust_item_weightf_in_loc(cct
, item
, weight
, loc
) > 0) {
1062 if (item
>= crush
->max_devices
) {
1063 crush
->max_devices
= item
+ 1;
1064 ldout(cct
, 5) << "insert_item max_devices now " << crush
->max_devices
1067 r
= rebuild_roots_with_classes();
1069 ldout(cct
, 0) << __func__
<< " unable to rebuild roots with classes: "
1070 << cpp_strerror(r
) << dendl
;
1076 ldout(cct
, 1) << "error: didn't find anywhere to add item " << item
1077 << " in " << loc
<< dendl
;
1082 int CrushWrapper::move_bucket(
1083 CephContext
*cct
, int id
, const map
<string
,string
>& loc
)
1085 // sorry this only works for buckets
1089 if (!item_exists(id
))
1092 // get the name of the bucket we are trying to move for later
1093 string id_name
= get_item_name(id
);
1095 // detach the bucket
1096 int bucket_weight
= detach_bucket(cct
, id
);
1098 // insert the bucket back into the hierarchy
1099 return insert_item(cct
, id
, bucket_weight
/ (float)0x10000, id_name
, loc
);
1102 int CrushWrapper::detach_bucket(CephContext
*cct
, int item
)
1110 // check that the bucket that we want to detach exists
1111 assert(bucket_exists(item
));
1113 // get the bucket's weight
1114 crush_bucket
*b
= get_bucket(item
);
1115 unsigned bucket_weight
= b
->weight
;
1117 // get where the bucket is located
1118 pair
<string
, string
> bucket_location
= get_immediate_parent(item
);
1120 // get the id of the parent bucket
1121 int parent_id
= get_item_id(bucket_location
.second
);
1123 // get the parent bucket
1124 crush_bucket
*parent_bucket
= get_bucket(parent_id
);
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);
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
);
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
);
1147 bool successful_detach
= !(check_item_loc(cct
, item
, test_location
,
1149 assert(successful_detach
);
1150 assert(test_weight
== 0);
1152 return bucket_weight
;
1155 int CrushWrapper::swap_bucket(CephContext
*cct
, int src
, int dst
)
1157 if (src
>= 0 || dst
>= 0)
1159 if (!item_exists(src
) || !item_exists(dst
))
1161 crush_bucket
*a
= get_bucket(src
);
1162 crush_bucket
*b
= get_bucket(dst
);
1163 unsigned aw
= a
->weight
;
1164 unsigned bw
= b
->weight
;
1167 adjust_item_weight(cct
, a
->id
, bw
);
1168 adjust_item_weight(cct
, b
->id
, aw
);
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);
1178 bucket_remove_item(a
, item
);
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
);
1188 assert(a
->size
== bs
);
1189 assert(b
->size
== 0);
1190 for (auto t
: tmp
) {
1191 bucket_add_item(b
, t
.first
, t
.second
);
1193 assert(a
->size
== bs
);
1194 assert(b
->size
== as
);
1197 swap_names(src
, dst
);
1198 return rebuild_roots_with_classes();
1201 int CrushWrapper::link_bucket(
1202 CephContext
*cct
, int id
, const map
<string
,string
>& loc
)
1204 // sorry this only works for buckets
1208 if (!item_exists(id
))
1211 // get the name of the bucket we are trying to move for later
1212 string id_name
= get_item_name(id
);
1214 crush_bucket
*b
= get_bucket(id
);
1215 unsigned bucket_weight
= b
->weight
;
1217 return insert_item(cct
, id
, bucket_weight
/ (float)0x10000, id_name
, loc
);
1220 int CrushWrapper::create_or_move_item(
1221 CephContext
*cct
, int item
, float weight
, string name
,
1222 const map
<string
,string
>& loc
) // typename -> bucketname
1227 if (!is_valid_crush_name(name
))
1230 if (check_item_loc(cct
, item
, loc
, &old_iweight
)) {
1231 ldout(cct
, 5) << "create_or_move_item " << item
<< " already at " << loc
1234 if (_search_item_exists(item
)) {
1235 weight
= get_item_weightf(item
);
1236 ldout(cct
, 10) << "create_or_move_item " << item
1237 << " exists with weight " << weight
<< dendl
;
1238 remove_item(cct
, item
, true);
1240 ldout(cct
, 5) << "create_or_move_item adding " << item
1241 << " weight " << weight
1242 << " at " << loc
<< dendl
;
1243 ret
= insert_item(cct
, item
, weight
, name
, loc
);
1250 int CrushWrapper::update_item(
1251 CephContext
*cct
, int item
, float weight
, string name
,
1252 const map
<string
,string
>& loc
) // typename -> bucketname
1254 ldout(cct
, 5) << "update_item item " << item
<< " weight " << weight
1255 << " name " << name
<< " loc " << loc
<< dendl
;
1258 if (!is_valid_crush_name(name
))
1261 if (!is_valid_crush_loc(cct
, loc
))
1264 ret
= validate_weightf(weight
);
1269 // compare quantized (fixed-point integer) weights!
1270 int iweight
= (int)(weight
* (float)0x10000);
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 "
1276 << ((float)old_iweight
/(float)0x10000) << " -> " << weight
1278 adjust_item_weight_in_loc(cct
, item
, iweight
, loc
);
1281 if (get_item_name(item
) != name
) {
1282 ldout(cct
, 5) << "update_item setting " << item
<< " name to " << name
1284 set_item_name(item
, name
);
1288 if (item_exists(item
)) {
1289 remove_item(cct
, item
, true);
1291 ldout(cct
, 5) << "update_item adding " << item
<< " weight " << weight
1292 << " at " << loc
<< dendl
;
1293 ret
= insert_item(cct
, item
, weight
, name
, loc
);
1300 int CrushWrapper::get_item_weight(int id
) const
1302 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1303 crush_bucket
*b
= crush
->buckets
[bidx
];
1308 for (unsigned i
= 0; i
< b
->size
; i
++)
1309 if (b
->items
[i
] == id
)
1310 return crush_get_bucket_item_weight(b
, i
);
1315 int CrushWrapper::get_item_weight_in_loc(int id
, const map
<string
,string
> &loc
)
1317 for (map
<string
,string
>::const_iterator l
= loc
.begin(); l
!= loc
.end(); ++l
) {
1319 int bid
= get_item_id(l
->second
);
1320 if (!bucket_exists(bid
))
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
);
1332 int CrushWrapper::adjust_item_weight(CephContext
*cct
, int id
, int weight
)
1334 ldout(cct
, 5) << "adjust_item_weight " << id
<< " weight " << weight
<< dendl
;
1336 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1337 crush_bucket
*b
= crush
->buckets
[bidx
];
1340 for (unsigned i
= 0; i
< b
->size
; i
++) {
1341 if (b
->items
[i
] == id
) {
1342 int diff
= bucket_adjust_item_weight(cct
, b
, id
, weight
);
1343 ldout(cct
, 5) << "adjust_item_weight " << id
<< " diff " << diff
1344 << " in bucket " << bidx
<< dendl
;
1345 adjust_item_weight(cct
, -1 - bidx
, b
->weight
);
1355 int CrushWrapper::adjust_item_weight_in_loc(CephContext
*cct
, int id
, int weight
, const map
<string
,string
>& loc
)
1357 ldout(cct
, 5) << "adjust_item_weight_in_loc " << id
<< " weight " << weight
1358 << " in " << loc
<< dendl
;
1361 for (auto l
= loc
.begin(); l
!= loc
.end(); ++l
) {
1362 int bid
= get_item_id(l
->second
);
1363 if (!bucket_exists(bid
))
1365 crush_bucket
*b
= get_bucket(bid
);
1366 for (unsigned int i
= 0; i
< b
->size
; i
++) {
1367 if (b
->items
[i
] == id
) {
1368 int diff
= bucket_adjust_item_weight(cct
, b
, id
, weight
);
1369 ldout(cct
, 5) << "adjust_item_weight_in_loc " << id
<< " diff " << diff
1370 << " in bucket " << bid
<< dendl
;
1371 adjust_item_weight(cct
, bid
, b
->weight
);
1381 int CrushWrapper::adjust_subtree_weight(CephContext
*cct
, int id
, int weight
)
1383 ldout(cct
, 5) << __func__
<< " " << id
<< " weight " << weight
<< dendl
;
1384 crush_bucket
*b
= get_bucket(id
);
1388 list
<crush_bucket
*> q
;
1390 while (!q
.empty()) {
1393 int local_changed
= 0;
1394 for (unsigned i
=0; i
<b
->size
; ++i
) {
1395 int n
= b
->items
[i
];
1397 bucket_adjust_item_weight(cct
, b
, n
, weight
);
1401 crush_bucket
*sub
= get_bucket(n
);
1407 if (local_changed
) {
1408 adjust_item_weight(cct
, b
->id
, b
->weight
);
1414 bool CrushWrapper::check_item_present(int id
) const
1418 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1419 crush_bucket
*b
= crush
->buckets
[bidx
];
1422 for (unsigned i
= 0; i
< b
->size
; i
++)
1423 if (b
->items
[i
] == id
)
1430 pair
<string
,string
> CrushWrapper::get_immediate_parent(int id
, int *_ret
)
1433 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1434 crush_bucket
*b
= crush
->buckets
[bidx
];
1437 if (is_shadow_item(b
->id
))
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
];
1445 return make_pair(parent_bucket_type
, parent_id
);
1452 return pair
<string
, string
>();
1455 int CrushWrapper::get_immediate_parent_id(int id
, int *parent
) const
1457 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1458 crush_bucket
*b
= crush
->buckets
[bidx
];
1461 if (is_shadow_item(b
->id
))
1463 for (unsigned i
= 0; i
< b
->size
; i
++) {
1464 if (b
->items
[i
] == id
) {
1473 int CrushWrapper::get_parent_of_type(int item
, int type
, int rule
) const
1476 // no rule specified
1478 int r
= get_immediate_parent_id(item
, &item
);
1482 } while (get_bucket_type(item
) != type
);
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.
1499 return 0; // not found
1502 int CrushWrapper::rename_class(const string
& srcname
, const string
& dstname
)
1504 auto i
= class_rname
.find(srcname
);
1505 if (i
== class_rname
.end())
1507 auto j
= class_rname
.find(dstname
);
1508 if (j
!= class_rname
.end())
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
;
1531 class_rname
.erase(srcname
);
1532 class_name
.erase(class_id
);
1533 class_rname
[dstname
] = class_id
;
1534 class_name
[class_id
] = dstname
;
1538 int CrushWrapper::populate_classes(
1539 const std::map
<int32_t, map
<int32_t, int32_t>>& old_class_bucket
)
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
);
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.
1551 map
<int,map
<int,vector
<int>>> cmap_item_weight
; // cargs -> bno -> [bucket weight for each position]
1553 find_nonshadow_roots(&roots
);
1554 for (auto &r
: roots
) {
1557 for (auto &c
: class_name
) {
1559 int res
= device_class_clone(r
, c
.first
, old_class_bucket
, used_ids
,
1560 &clone
, &cmap_item_weight
);
1568 int CrushWrapper::trim_roots_with_class()
1571 find_shadow_roots(&roots
);
1572 for (auto &r
: roots
) {
1575 int res
= remove_root(r
);
1579 // there is no need to reweight because we only remove from the
1584 int32_t CrushWrapper::_alloc_class_id() const {
1585 if (class_name
.empty()) {
1588 int32_t class_id
= class_name
.rbegin()->first
+ 1;
1589 if (class_id
>= 0) {
1592 // wrapped, pick a random start and do exhaustive search
1593 uint32_t upperlimit
= numeric_limits
<int32_t>::max();
1595 class_id
= rand() % upperlimit
;
1596 const auto start
= class_id
;
1598 if (!class_name
.count(class_id
)) {
1606 } while (class_id
!= start
);
1607 assert(0 == "no available class id");
1610 void CrushWrapper::reweight(CephContext
*cct
)
1614 for (set
<int>::iterator p
= roots
.begin(); p
!= roots
.end(); ++p
) {
1617 crush_bucket
*b
= get_bucket(*p
);
1618 ldout(cct
, 5) << "reweight bucket " << *p
<< dendl
;
1619 int r
= crush_reweight_bucket(crush
, b
);
1624 int CrushWrapper::add_simple_rule_at(
1625 string name
, string root_name
,
1626 string failure_domain_name
,
1627 string device_class
,
1628 string mode
, int rule_type
,
1632 if (rule_exists(name
)) {
1634 *err
<< "rule " << name
<< " exists";
1638 if (rule_exists(rno
)) {
1640 *err
<< "rule with ruleno " << rno
<< " exists";
1643 if (ruleset_exists(rno
)) {
1645 *err
<< "ruleset " << rno
<< " exists";
1649 for (rno
= 0; rno
< get_max_rules(); rno
++) {
1650 if (!rule_exists(rno
) && !ruleset_exists(rno
))
1654 if (!name_exists(root_name
)) {
1656 *err
<< "root item " << root_name
<< " does not exist";
1659 int root
= get_item_id(root_name
);
1661 if (failure_domain_name
.length()) {
1662 type
= get_type_id(failure_domain_name
);
1665 *err
<< "unknown type " << failure_domain_name
;
1669 if (device_class
.size()) {
1670 if (!class_exists(device_class
)) {
1672 *err
<< "device class " << device_class
<< " does not exist";
1675 int c
= get_class_id(device_class
);
1676 if (class_bucket
.count(root
) == 0 ||
1677 class_bucket
[root
].count(c
) == 0) {
1679 *err
<< "root " << root_name
<< " has no devices with class "
1683 root
= class_bucket
[root
][c
];
1685 if (mode
!= "firstn" && mode
!= "indep") {
1687 *err
<< "unknown mode " << mode
;
1692 if (mode
== "indep")
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
);
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);
1704 crush_rule_set_step(rule
, step
++, CRUSH_RULE_TAKE
, root
, 0);
1706 crush_rule_set_step(rule
, step
++,
1707 mode
== "firstn" ? CRUSH_RULE_CHOOSELEAF_FIRSTN
:
1708 CRUSH_RULE_CHOOSELEAF_INDEP
,
1712 crush_rule_set_step(rule
, step
++,
1713 mode
== "firstn" ? CRUSH_RULE_CHOOSE_FIRSTN
:
1714 CRUSH_RULE_CHOOSE_INDEP
,
1717 crush_rule_set_step(rule
, step
++, CRUSH_RULE_EMIT
, 0, 0);
1719 int ret
= crush_add_rule(crush
, rule
, rno
);
1721 *err
<< "failed to add rule " << rno
<< " because " << cpp_strerror(ret
);
1724 set_rule_name(rno
, name
);
1729 int CrushWrapper::add_simple_rule(
1730 string name
, string root_name
,
1731 string failure_domain_name
,
1732 string device_class
,
1733 string mode
, int rule_type
,
1736 return add_simple_rule_at(name
, root_name
, failure_domain_name
, device_class
,
1738 rule_type
, -1, err
);
1741 float CrushWrapper::_get_take_weight_osd_map(int root
,
1742 map
<int,float> *pmap
) const
1747 //breadth first iterate the OSD tree
1748 while (!q
.empty()) {
1749 int bno
= q
.front();
1751 crush_bucket
*b
= crush
->buckets
[-1-bno
];
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
;
1759 } else { //not an OSD, expand the child later
1760 q
.push_back(item_id
);
1767 void CrushWrapper::_normalize_weight_map(float sum
,
1768 const map
<int,float>& m
,
1769 map
<int,float> *pmap
) const
1772 map
<int,float>::iterator q
= pmap
->find(p
.first
);
1773 if (q
== pmap
->end()) {
1774 (*pmap
)[p
.first
] = p
.second
/ sum
;
1776 q
->second
+= p
.second
/ sum
;
1781 int CrushWrapper::get_take_weight_osd_map(int root
, map
<int,float> *pmap
) const
1784 float sum
= _get_take_weight_osd_map(root
, &m
);
1785 _normalize_weight_map(sum
, m
, pmap
);
1789 int CrushWrapper::get_rule_weight_osd_map(unsigned ruleno
,
1790 map
<int,float> *pmap
) const
1792 if (ruleno
>= crush
->max_rules
)
1794 if (crush
->rules
[ruleno
] == NULL
)
1796 crush_rule
*rule
= crush
->rules
[ruleno
];
1798 // build a weight map for each TAKE in the rule, and then merge them
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.)
1804 for (unsigned i
=0; i
<rule
->len
; ++i
) {
1807 if (rule
->steps
[i
].op
== CRUSH_RULE_TAKE
) {
1808 int n
= rule
->steps
[i
].arg1
;
1813 sum
+= _get_take_weight_osd_map(n
, &m
);
1816 _normalize_weight_map(sum
, m
, pmap
);
1822 int CrushWrapper::remove_rule(int ruleno
)
1824 if (ruleno
>= (int)crush
->max_rules
)
1826 if (crush
->rules
[ruleno
] == NULL
)
1828 crush_destroy_rule(crush
->rules
[ruleno
]);
1829 crush
->rules
[ruleno
] = NULL
;
1830 rule_name_map
.erase(ruleno
);
1832 return rebuild_roots_with_classes();
1835 int CrushWrapper::bucket_adjust_item_weight(CephContext
*cct
, crush_bucket
*bucket
, int item
, int weight
)
1837 if (cct
->_conf
->osd_crush_update_weight_set
) {
1839 for (position
= 0; position
< bucket
->size
; position
++)
1840 if (bucket
->items
[position
] == item
)
1842 assert(position
!= bucket
->size
);
1843 for (auto &w
: choose_args
) {
1844 crush_choose_arg_map
&arg_map
= w
.second
;
1845 crush_choose_arg
*arg
= &arg_map
.args
[-1-bucket
->id
];
1846 for (__u32 j
= 0; j
< arg
->weight_set_positions
; j
++) {
1847 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
1848 weight_set
->weights
[position
] = weight
;
1852 return crush_bucket_adjust_item_weight(crush
, bucket
, item
, weight
);
1855 int CrushWrapper::add_bucket(
1856 int bucketno
, int alg
, int hash
, int type
, int size
,
1857 int *items
, int *weights
, int *idout
)
1860 alg
= get_default_bucket_alg();
1864 crush_bucket
*b
= crush_make_bucket(crush
, alg
, hash
, type
, size
, items
,
1868 int r
= crush_add_bucket(crush
, bucketno
, b
, idout
);
1869 int pos
= -1 - *idout
;
1870 for (auto& p
: choose_args
) {
1871 crush_choose_arg_map
& cmap
= p
.second
;
1873 if ((int)cmap
.size
<= pos
) {
1874 cmap
.args
= (crush_choose_arg
*)realloc(
1876 sizeof(crush_choose_arg
) * (pos
+ 1));
1878 memset(&cmap
.args
[cmap
.size
], 0,
1879 sizeof(crush_choose_arg
) * (pos
+ 1 - cmap
.size
));
1880 cmap
.size
= pos
+ 1;
1883 cmap
.args
= (crush_choose_arg
*)calloc(sizeof(crush_choose_arg
),
1886 cmap
.size
= pos
+ 1;
1889 int positions
= get_choose_args_positions(cmap
);
1890 crush_choose_arg
& carg
= cmap
.args
[pos
];
1891 carg
.weight_set
= (crush_weight_set
*)calloc(sizeof(crush_weight_set
),
1893 carg
.weight_set_positions
= positions
;
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
];
1906 int CrushWrapper::bucket_add_item(crush_bucket
*bucket
, int item
, int weight
)
1908 __u32 new_size
= bucket
->size
+ 1;
1909 int r
= crush_bucket_add_item(crush
, bucket
, item
, weight
);
1913 for (auto &w
: choose_args
) {
1914 crush_choose_arg_map
&arg_map
= w
.second
;
1915 crush_choose_arg
*arg
= &arg_map
.args
[-1-bucket
->id
];
1916 for (__u32 j
= 0; j
< arg
->weight_set_positions
; j
++) {
1917 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
1918 weight_set
->weights
= (__u32
*)realloc(weight_set
->weights
,
1919 new_size
* sizeof(__u32
));
1920 assert(weight_set
->size
+ 1 == new_size
);
1921 weight_set
->weights
[weight_set
->size
] = weight
;
1922 weight_set
->size
= new_size
;
1924 if (arg
->ids_size
) {
1925 arg
->ids
= (__s32
*)realloc(arg
->ids
, new_size
* sizeof(__s32
));
1926 assert(arg
->ids_size
+ 1 == new_size
);
1927 arg
->ids
[arg
->ids_size
] = item
;
1928 arg
->ids_size
= new_size
;
1934 int CrushWrapper::bucket_remove_item(crush_bucket
*bucket
, int item
)
1936 __u32 new_size
= bucket
->size
- 1;
1938 for (position
= 0; position
< bucket
->size
; position
++)
1939 if (bucket
->items
[position
] == item
)
1941 assert(position
!= bucket
->size
);
1942 int r
= crush_bucket_remove_item(crush
, bucket
, item
);
1946 for (auto &w
: choose_args
) {
1947 crush_choose_arg_map
&arg_map
= w
.second
;
1948 crush_choose_arg
*arg
= &arg_map
.args
[-1-bucket
->id
];
1949 for (__u32 j
= 0; j
< arg
->weight_set_positions
; j
++) {
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];
1955 weight_set
->weights
= (__u32
*)realloc(weight_set
->weights
,
1956 new_size
* sizeof(__u32
));
1958 weight_set
->weights
= NULL
;
1960 weight_set
->size
= new_size
;
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];
1967 arg
->ids
= (__s32
*)realloc(arg
->ids
, new_size
* sizeof(__s32
));
1971 arg
->ids_size
= new_size
;
1977 int CrushWrapper::bucket_set_alg(int bid
, int alg
)
1979 crush_bucket
*b
= get_bucket(bid
);
1987 int CrushWrapper::update_device_class(int id
,
1988 const string
& class_name
,
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";
2002 int class_id
= get_or_create_class_id(class_name
);
2004 *ss
<< name
<< " id " << id
<< " is negative";
2008 if (class_map
.count(id
) != 0 && class_map
[id
] == class_id
) {
2009 *ss
<< name
<< " already set to class " << class_name
;
2013 set_item_class(id
, class_id
);
2015 int r
= rebuild_roots_with_classes();
2021 int CrushWrapper::remove_device_class(CephContext
*cct
, int id
, ostream
*ss
)
2024 const char *name
= get_item_name(id
);
2026 *ss
<< "osd." << id
<< " does not have a name";
2030 const char *class_name
= get_item_class(id
);
2032 *ss
<< "osd." << id
<< " has not been bound to a specific class yet";
2035 class_remove_item(id
);
2037 int r
= rebuild_roots_with_classes();
2039 *ss
<< "unable to rebuild roots with class '" << class_name
<< "' "
2040 << "of osd." << id
<< ": " << cpp_strerror(r
);
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
,
2051 map
<int,map
<int,vector
<int>>> *cmap_item_weight
)
2053 const char *item_name
= get_item_name(original_id
);
2054 if (item_name
== NULL
)
2056 const char *class_name
= get_class_name(device_class
);
2057 if (class_name
== NULL
)
2059 string copy_name
= item_name
+ string("~") + class_name
;
2060 if (name_exists(copy_name
)) {
2061 *clone
= get_item_id(copy_name
);
2065 crush_bucket
*original
= get_bucket(original_id
);
2066 assert(!IS_ERR(original
));
2067 crush_bucket
*copy
= crush_make_bucket(crush
,
2074 vector
<unsigned> item_orig_pos
; // new item pos -> orig item pos
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
);
2079 if (class_map
.count(item
) != 0 && class_map
[item
] == device_class
) {
2080 int res
= crush_bucket_add_item(crush
, copy
, item
, weight
);
2088 int res
= device_class_clone(item
, device_class
, old_class_bucket
,
2089 used_ids
, &child_copy_id
,
2093 crush_bucket
*child_copy
= get_bucket(child_copy_id
);
2094 assert(!IS_ERR(child_copy
));
2095 res
= crush_bucket_add_item(crush
, copy
, child_copy_id
,
2096 child_copy
->weight
);
2100 item_orig_pos
.push_back(i
);
2102 assert(item_orig_pos
.size() == copy
->size
);
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
);
2109 // pick a new shadow bucket id that is not used by the current map
2110 // *or* any previous shadow buckets.
2112 while (((-1-bno
) < crush
->max_buckets
&& crush
->buckets
[-1-bno
]) ||
2113 used_ids
.count(bno
)) {
2117 int res
= crush_add_bucket(crush
, bno
, copy
, clone
);
2120 assert(!bno
|| bno
== *clone
);
2122 res
= set_item_class(*clone
, device_class
);
2126 // we do not use set_item_name because the name is intentionally invalid
2127 name_map
[*clone
] = copy_name
;
2129 name_rmap
[copy_name
] = *clone
;
2130 class_bucket
[original_id
][device_class
] = *clone
;
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]));
2140 memset(cmap
.args
+ cmap
.size
, 0,
2141 (new_size
- cmap
.size
) * sizeof(cmap
.args
[0]));
2142 cmap
.size
= new_size
;
2144 auto& o
= cmap
.args
[-1-original_id
];
2145 auto& n
= cmap
.args
[-1-bno
];
2146 n
.ids_size
= 0; // FIXME: implement me someday
2147 n
.weight_set_positions
= o
.weight_set_positions
;
2148 n
.weight_set
= (crush_weight_set
*)calloc(
2149 n
.weight_set_positions
, sizeof(crush_weight_set
));
2150 for (size_t s
= 0; s
< n
.weight_set_positions
; ++s
) {
2151 n
.weight_set
[s
].size
= copy
->size
;
2152 n
.weight_set
[s
].weights
= (__u32
*)calloc(copy
->size
, sizeof(__u32
));
2154 for (size_t s
= 0; s
< n
.weight_set_positions
; ++s
) {
2155 vector
<int> bucket_weights(n
.weight_set_positions
);
2156 for (size_t i
= 0; i
< copy
->size
; ++i
) {
2157 int item
= copy
->items
[i
];
2159 n
.weight_set
[s
].weights
[i
] = o
.weight_set
[s
].weights
[item_orig_pos
[i
]];
2160 } else if ((*cmap_item_weight
)[w
.first
].count(item
)) {
2161 n
.weight_set
[s
].weights
[i
] = (*cmap_item_weight
)[w
.first
][item
][s
];
2163 n
.weight_set
[s
].weights
[i
] = 0;
2165 bucket_weights
[s
] += n
.weight_set
[s
].weights
[i
];
2167 (*cmap_item_weight
)[w
.first
][bno
] = bucket_weights
;
2173 int CrushWrapper::get_rules_by_class(const string
&class_name
, set
<int> *rules
)
2177 if (!class_exists(class_name
)) {
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
];
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
;
2190 int res
= split_id_class(step_item
, &original_item
, &c
);
2194 if (c
!= -1 && c
== class_id
) {
2204 // return rules that might reference the given osd
2205 int CrushWrapper::get_rules_by_osd(int osd
, set
<int> *rules
)
2212 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
2213 crush_rule
*r
= crush
->rules
[i
];
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
);
2222 return rc
; // propagate fatal errors!
2225 for (auto &o
: unordered
) {
2242 bool CrushWrapper::_class_is_dead(int class_id
)
2244 for (auto &p
: class_map
) {
2245 if (p
.first
>= 0 && p
.second
== class_id
) {
2249 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
2250 crush_rule
*r
= crush
->rules
[i
];
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
) {
2258 if (q
.count(class_id
) && q
[class_id
] == root
) {
2265 // no more referenced by any devices or crush rules
2269 void CrushWrapper::cleanup_dead_classes()
2271 auto p
= class_name
.begin();
2272 while (p
!= class_name
.end()) {
2273 if (_class_is_dead(p
->first
)) {
2274 string n
= p
->second
;
2276 remove_class_name(n
);
2283 int CrushWrapper::rebuild_roots_with_classes()
2285 std::map
<int32_t, map
<int32_t, int32_t> > old_class_bucket
= class_bucket
;
2286 cleanup_dead_classes();
2287 int r
= trim_roots_with_class();
2290 class_bucket
.clear();
2291 return populate_classes(old_class_bucket
);
2294 void CrushWrapper::encode(bufferlist
& bl
, uint64_t features
) const
2298 __u32 magic
= CRUSH_MAGIC
;
2299 ::encode(magic
, bl
);
2301 ::encode(crush
->max_buckets
, bl
);
2302 ::encode(crush
->max_rules
, bl
);
2303 ::encode(crush
->max_devices
, bl
);
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
;
2316 for (int i
=0; i
<crush
->max_buckets
; i
++) {
2318 if (crush
->buckets
[i
]) alg
= crush
->buckets
[i
]->alg
;
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
);
2332 switch (crush
->buckets
[i
]->alg
) {
2333 case CRUSH_BUCKET_UNIFORM
:
2334 ::encode((reinterpret_cast<crush_bucket_uniform
*>(crush
->buckets
[i
]))->item_weight
, bl
);
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
);
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
);
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
);
2357 case CRUSH_BUCKET_STRAW2
:
2360 if (encode_compat_choose_args
&&
2361 arg_map
.args
[i
].weight_set_positions
> 0) {
2362 weights
= arg_map
.args
[i
].weight_set
[0].weights
;
2364 weights
= (reinterpret_cast<crush_bucket_straw2
*>(crush
->buckets
[i
]))->item_weights
;
2366 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++) {
2367 ::encode(weights
[j
], bl
);
2379 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
2380 __u32 yes
= crush
->rules
[i
] ? 1:0;
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
);
2392 ::encode(type_map
, bl
);
2393 ::encode(name_map
, bl
);
2394 ::encode(rule_name_map
, bl
);
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
);
2408 if (HAVE_FEATURE(features
, SERVER_LUMINOUS
)) {
2410 ::encode(class_map
, bl
);
2411 ::encode(class_name
, bl
);
2412 ::encode(class_bucket
, bl
);
2415 __u32 size
= (__u32
)choose_args
.size();
2417 for (auto c
: choose_args
) {
2418 ::encode(c
.first
, bl
);
2419 crush_choose_arg_map arg_map
= c
.second
;
2421 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
2422 crush_choose_arg
*arg
= &arg_map
.args
[i
];
2423 if (arg
->weight_set_positions
== 0 &&
2429 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
2430 crush_choose_arg
*arg
= &arg_map
.args
[i
];
2431 if (arg
->weight_set_positions
== 0 &&
2435 ::encode(arg
->weight_set_positions
, bl
);
2436 for (__u32 j
= 0; j
< arg
->weight_set_positions
; j
++) {
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
);
2442 ::encode(arg
->ids_size
, bl
);
2443 for (__u32 j
= 0; j
< arg
->ids_size
; j
++)
2444 ::encode(arg
->ids
[j
], bl
);
2450 static void decode_32_or_64_string_map(map
<int32_t,string
>& m
, bufferlist::iterator
& blp
)
2460 ::decode(strlen
, blp
);
2462 // der, key was actually 64-bits!
2463 ::decode(strlen
, blp
);
2465 ::decode_nohead(strlen
, m
[key
], blp
);
2469 void CrushWrapper::decode(bufferlist::iterator
& blp
)
2474 ::decode(magic
, blp
);
2475 if (magic
!= CRUSH_MAGIC
)
2476 throw buffer::malformed_input("bad magic number");
2478 ::decode(crush
->max_buckets
, blp
);
2479 ::decode(crush
->max_rules
, blp
);
2480 ::decode(crush
->max_devices
, blp
);
2482 // legacy tunables, unless we decode something newer
2483 set_tunables_legacy();
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
);
2493 crush
->rules
= (crush_rule
**)calloc(1, crush
->max_rules
* sizeof(crush_rule
*));
2494 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
2498 crush
->rules
[i
] = NULL
;
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
);
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
);
2521 ::decode(crush
->choose_local_tries
, blp
);
2522 ::decode(crush
->choose_local_fallback_tries
, blp
);
2523 ::decode(crush
->choose_total_tries
, blp
);
2526 ::decode(crush
->chooseleaf_descend_once
, blp
);
2529 ::decode(crush
->chooseleaf_vary_r
, blp
);
2532 ::decode(crush
->straw_calc_version
, blp
);
2535 ::decode(crush
->allowed_bucket_algs
, blp
);
2538 ::decode(crush
->chooseleaf_stable
, blp
);
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
);
2548 __u32 choose_args_size
;
2549 ::decode(choose_args_size
, blp
);
2550 for (__u32 i
= 0; i
< choose_args_size
; i
++) {
2551 typename
decltype(choose_args
)::key_type choose_args_index
;
2552 ::decode(choose_args_index
, blp
);
2553 crush_choose_arg_map arg_map
;
2554 arg_map
.size
= crush
->max_buckets
;
2555 arg_map
.args
= (crush_choose_arg
*)calloc(
2556 arg_map
.size
, sizeof(crush_choose_arg
));
2558 ::decode(size
, blp
);
2559 for (__u32 j
= 0; j
< size
; j
++) {
2561 ::decode(bucket_index
, blp
);
2562 assert(bucket_index
< arg_map
.size
);
2563 crush_choose_arg
*arg
= &arg_map
.args
[bucket_index
];
2564 ::decode(arg
->weight_set_positions
, blp
);
2565 if (arg
->weight_set_positions
) {
2566 arg
->weight_set
= (crush_weight_set
*)calloc(
2567 arg
->weight_set_positions
, sizeof(crush_weight_set
));
2568 for (__u32 k
= 0; k
< arg
->weight_set_positions
; k
++) {
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
);
2577 ::decode(arg
->ids_size
, blp
);
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
);
2585 choose_args
[choose_args_index
] = arg_map
;
2588 update_choose_args(nullptr); // in case we decode a legacy "corrupted" map
2592 crush_destroy(crush
);
2597 void CrushWrapper::decode_crush_bucket(crush_bucket
** bptr
, bufferlist::iterator
&blp
)
2608 case CRUSH_BUCKET_UNIFORM
:
2609 size
= sizeof(crush_bucket_uniform
);
2611 case CRUSH_BUCKET_LIST
:
2612 size
= sizeof(crush_bucket_list
);
2614 case CRUSH_BUCKET_TREE
:
2615 size
= sizeof(crush_bucket_tree
);
2617 case CRUSH_BUCKET_STRAW
:
2618 size
= sizeof(crush_bucket_straw
);
2620 case CRUSH_BUCKET_STRAW2
:
2621 size
= sizeof(crush_bucket_straw2
);
2626 snprintf(str
, sizeof(str
), "unsupported bucket algorithm: %d", alg
);
2627 throw buffer::malformed_input(str
);
2630 crush_bucket
*bucket
= reinterpret_cast<crush_bucket
*>(calloc(1, size
));
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
);
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
);
2645 switch (bucket
->alg
) {
2646 case CRUSH_BUCKET_UNIFORM
:
2647 ::decode((reinterpret_cast<crush_bucket_uniform
*>(bucket
))->item_weight
, blp
);
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
));
2655 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
2656 ::decode(cbl
->item_weights
[j
], blp
);
2657 ::decode(cbl
->sum_weights
[j
], blp
);
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
);
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
);
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
);
2693 // We should have handled this case in the first switch statement
2700 void CrushWrapper::dump(Formatter
*f
) const
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
);
2708 f
->dump_string("name", n
);
2711 sprintf(name
, "device%d", i
);
2712 f
->dump_string("name", name
);
2714 const char *device_class
= get_item_class(i
);
2715 if (device_class
!= NULL
)
2716 f
->dump_string("class", device_class
);
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
);
2727 f
->open_object_section("type");
2728 f
->dump_int("type_id", 0);
2729 f
->dump_string("name", "device");
2735 f
->open_object_section("type");
2736 f
->dump_int("type_id", i
);
2737 f
->dump_string("name", name
);
2742 f
->open_array_section("buckets");
2743 for (int bucket
= -1; bucket
> -1-get_max_buckets(); --bucket
) {
2744 if (!bucket_exists(bucket
))
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
);
2769 f
->open_array_section("rules");
2773 f
->open_object_section("tunables");
2777 dump_choose_args(f
);
2781 // depth first walker
2783 typedef CrushTreeDumper::Item Item
;
2784 const CrushWrapper
*crush
;
2785 const CrushTreeDumper::name_map_t
& weight_set_names
;
2787 explicit TreeDumper(const CrushWrapper
*crush
,
2788 const CrushTreeDumper::name_map_t
& wsnames
)
2789 : crush(crush
), weight_set_names(wsnames
) {}
2791 void dump(Formatter
*f
) {
2793 crush
->find_roots(&roots
);
2794 for (set
<int>::iterator root
= roots
.begin(); root
!= roots
.end(); ++root
) {
2795 dump_item(Item(*root
, 0, 0, crush
->get_bucket_weightf(*root
)), f
);
2800 void dump_item(const Item
& qi
, Formatter
* f
) {
2801 if (qi
.is_bucket()) {
2802 f
->open_object_section("bucket");
2803 CrushTreeDumper::dump_item_fields(crush
, weight_set_names
, qi
, f
);
2804 dump_bucket_children(qi
, f
);
2807 f
->open_object_section("device");
2808 CrushTreeDumper::dump_item_fields(crush
, weight_set_names
, qi
, f
);
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
);
2819 dump_item(Item(id
, parent
.id
, parent
.depth
+ 1, weight
), f
);
2826 void CrushWrapper::dump_tree(
2828 const CrushTreeDumper::name_map_t
& weight_set_names
) const
2831 TreeDumper(this, weight_set_names
).dump(f
);
2834 void CrushWrapper::dump_tunables(Formatter
*f
) const
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());
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");
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());
2861 // be helpful about minimum version required
2862 f
->dump_string("minimum_required_version", get_min_required_version());
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());
2874 void CrushWrapper::dump_choose_args(Formatter
*f
) const
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
];
2882 if (arg
->weight_set_positions
== 0 &&
2885 f
->open_object_section("choose_args");
2886 int bucket_index
= i
;
2887 f
->dump_int("bucket_id", -1-bucket_index
);
2888 if (arg
->weight_set_positions
> 0) {
2889 f
->open_array_section("weight_set");
2890 for (__u32 j
= 0; j
< arg
->weight_set_positions
; j
++) {
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);
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
]);
2914 void CrushWrapper::dump_rules(Formatter
*f
) const
2916 for (int i
=0; i
<get_max_rules(); i
++) {
2917 if (!rule_exists(i
))
2923 void CrushWrapper::dump_rule(int ruleset
, Formatter
*f
) const
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");
2940 case CRUSH_RULE_TAKE
:
2941 f
->dump_string("op", "take");
2943 int item
= get_rule_arg1(ruleset
, j
);
2944 f
->dump_int("item", item
);
2946 const char *name
= get_item_name(item
);
2947 f
->dump_string("item_name", name
? name
: "");
2950 case CRUSH_RULE_EMIT
:
2951 f
->dump_string("op", "emit");
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
)));
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
)));
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
)));
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
)));
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
));
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
));
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
));
2992 void CrushWrapper::list_rules(Formatter
*f
) const
2994 for (int rule
= 0; rule
< get_max_rules(); rule
++) {
2995 if (!rule_exists(rule
))
2997 f
->dump_string("name", get_rule_name(rule
));
3001 void CrushWrapper::list_rules(ostream
*ss
) const
3003 for (int rule
= 0; rule
< get_max_rules(); rule
++) {
3004 if (!rule_exists(rule
))
3006 *ss
<< get_rule_name(rule
) << "\n";
3010 class CrushTreePlainDumper
: public CrushTreeDumper::Dumper
<TextTable
> {
3012 typedef CrushTreeDumper::Dumper
<TextTable
> Parent
;
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
,
3020 : Parent(crush
, wsnames
, show_shadow
) {}
3023 void dump(TextTable
*tbl
) {
3024 tbl
->define_column("ID", TextTable::LEFT
, TextTable::RIGHT
);
3025 tbl
->define_column("CLASS", TextTable::LEFT
, TextTable::RIGHT
);
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
);
3032 auto q
= weight_set_names
.find(p
.first
);
3033 name
= q
!= weight_set_names
.end() ? q
->second
:
3035 tbl
->define_column(name
.c_str(), TextTable::LEFT
, TextTable::RIGHT
);
3038 tbl
->define_column("TYPE NAME", TextTable::LEFT
, TextTable::LEFT
);
3043 void dump_item(const CrushTreeDumper::Item
&qi
, TextTable
*tbl
) override
{
3044 const char *c
= crush
->get_item_class(qi
.id
);
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
);
3056 bidx
< (int)cmap
.size
&&
3057 cmap
.args
[bidx
].weight_set
&&
3058 cmap
.args
[bidx
].weight_set_positions
>= 1) {
3061 pos
< (int)cmap
.args
[bidx
].weight_set
[0].size
&&
3062 b
->items
[pos
] != qi
.id
;
3064 *tbl
<< weightf_t((float)cmap
.args
[bidx
].weight_set
[0].weights
[pos
] /
3072 for (int k
=0; k
< qi
.depth
; k
++) {
3075 if (qi
.is_bucket()) {
3076 ss
<< crush
->get_type_name(crush
->get_bucket_type(qi
.id
)) << " "
3077 << crush
->get_item_name(qi
.id
);
3079 ss
<< "osd." << qi
.id
;
3082 *tbl
<< TextTable::endrow
;
3087 class CrushTreeFormattingDumper
: public CrushTreeDumper::FormattingDumper
{
3089 typedef CrushTreeDumper::FormattingDumper Parent
;
3091 explicit CrushTreeFormattingDumper(
3092 const CrushWrapper
*crush
,
3093 const CrushTreeDumper::name_map_t
& wsnames
)
3094 : Parent(crush
, wsnames
) {}
3096 explicit CrushTreeFormattingDumper(
3097 const CrushWrapper
*crush
,
3098 const CrushTreeDumper::name_map_t
& wsnames
,
3100 : Parent(crush
, wsnames
, show_shadow
) {}
3102 void dump(Formatter
*f
) {
3103 f
->open_array_section("nodes");
3106 f
->open_array_section("stray");
3112 void CrushWrapper::dump_tree(
3115 const CrushTreeDumper::name_map_t
& weight_set_names
,
3116 bool show_shadow
) const
3120 CrushTreePlainDumper(this, weight_set_names
, show_shadow
).dump(&tbl
);
3124 CrushTreeFormattingDumper(this, weight_set_names
, show_shadow
).dump(f
);
3128 void CrushWrapper::generate_test_instances(list
<CrushWrapper
*>& o
)
3130 o
.push_back(new CrushWrapper
);
3135 * Determine the default CRUSH ruleset ID to be used with
3136 * newly created replicated pools.
3138 * @returns a ruleset ID (>=0) or -1 if no suitable ruleset found
3140 int CrushWrapper::get_osd_pool_default_crush_replicated_ruleset(CephContext
*cct
)
3142 int crush_ruleset
= cct
->_conf
->osd_pool_default_crush_rule
;
3143 if (crush_ruleset
< 0) {
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
3148 return crush_ruleset
;
3151 bool CrushWrapper::is_valid_crush_name(const string
& s
)
3155 for (string::const_iterator p
= s
.begin(); p
!= s
.end(); ++p
) {
3159 !(*p
>= '0' && *p
<= '9') &&
3160 !(*p
>= 'A' && *p
<= 'Z') &&
3161 !(*p
>= 'a' && *p
<= 'z'))
3167 bool CrushWrapper::is_valid_crush_loc(CephContext
*cct
,
3168 const map
<string
,string
>& loc
)
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_-.]+)"
3183 int CrushWrapper::_choose_type_stack(
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
,
3191 vector
<int> *pw
) const
3193 vector
<int> w
= *pw
;
3196 ldout(cct
, 10) << __func__
<< " stack " << stack
3202 vector
<int> cumulative_fanout(stack
.size());
3204 for (int j
= (int)stack
.size() - 1; j
>= 0; --j
) {
3205 cumulative_fanout
[j
] = f
;
3206 f
*= stack
[j
].second
;
3208 ldout(cct
, 10) << __func__
<< " cumulative_fanout " << cumulative_fanout
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
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
) {
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
);
3232 ldout(cct
, 20) << __func__
<< " underfull_buckets " << underfull_buckets
<< dendl
;
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
;
3243 if (i
== orig
.end()) {
3244 ldout(cct
, 10) << __func__
<< " end of orig, break 0" << dendl
;
3247 for (auto from
: w
) {
3248 ldout(cct
, 10) << " from " << from
<< dendl
;
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
);
3254 for (int pos
= 0; pos
< fanout
; ++pos
) {
3257 int item
= get_parent_of_type(*tmpi
, type
);
3260 while (n
-- && tmpi
!= orig
.end()) {
3261 leaves
[pos
].insert(*tmpi
++);
3263 ldout(cct
, 10) << __func__
<< " from " << *tmpi
<< " got " << item
3264 << " of type " << type
<< " over leaves " << leaves
[pos
] << dendl
;
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
3273 if (used
.count(item
)) {
3274 ldout(cct
, 20) << __func__
<< " in used " << used
<< dendl
;
3277 if (!subtree_contains(from
, item
)) {
3278 ldout(cct
, 20) << __func__
<< " not in subtree " << from
<< dendl
;
3281 if (std::find(orig
.begin(), orig
.end(), item
) != orig
.end()) {
3282 ldout(cct
, 20) << __func__
<< " in orig " << orig
<< dendl
;
3287 ldout(cct
, 10) << __func__
<< " pos " << pos
<< " replace "
3288 << *i
<< " -> " << item
<< dendl
;
3290 assert(i
!= orig
.end());
3296 ldout(cct
, 10) << __func__
<< " pos " << pos
<< " keep " << *i
3298 assert(i
!= orig
.end());
3302 if (i
== orig
.end()) {
3303 ldout(cct
, 10) << __func__
<< " end of orig, break 1" << dendl
;
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;
3320 ldout(cct
, 10) << " bucket " << o
[pos
] << " has no underfull targets and "
3321 << ">0 leaves " << leaves
[pos
] << " is overfull; alts "
3322 << underfull_buckets
[j
]
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
3328 get_parent_of_type(o
[pos
], stack
[j
-1].first
) ==
3329 get_parent_of_type(alt
, stack
[j
-1].first
)) {
3331 ldout(cct
, 10) << " replacing " << o
[pos
]
3332 << " (which has no underfull leaves) with " << alt
3334 << get_parent_of_type(alt
, stack
[j
-1].first
) << " type "
3335 << type
<< ")" << dendl
;
3337 ldout(cct
, 10) << " replacing " << o
[pos
]
3338 << " (which has no underfull leaves) with " << alt
3339 << " (first level)" << dendl
;
3343 ldout(cct
, 30) << " alt " << alt
<< " for " << o
[pos
]
3344 << " has different parent, skipping" << dendl
;
3352 if (i
== orig
.end()) {
3353 ldout(cct
, 10) << __func__
<< " end of orig, break 2" << dendl
;
3357 ldout(cct
, 10) << __func__
<< " w <- " << o
<< " was " << w
<< dendl
;
3364 int CrushWrapper::try_remap_rule(
3368 const set
<int>& overfull
,
3369 const vector
<int>& underfull
,
3370 const vector
<int>& orig
,
3371 vector
<int> *out
) const
3373 const crush_map
*map
= crush
;
3374 const crush_rule
*rule
= get_rule(ruleno
);
3377 ldout(cct
, 10) << __func__
<< " ruleno " << ruleno
3378 << " numrep " << maxout
<< " overfull " << overfull
3379 << " underfull " << underfull
<< " orig " << orig
3381 vector
<int> w
; // working set
3384 auto i
= orig
.begin();
3387 vector
<pair
<int,int>> type_stack
; // (type, fan-out)
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
])) {
3398 w
.push_back(curstep
->arg1
);
3399 ldout(cct
, 10) << __func__
<< " take " << w
<< dendl
;
3401 ldout(cct
, 1) << " bad take value " << curstep
->arg1
<< dendl
;
3405 case CRUSH_RULE_CHOOSELEAF_FIRSTN
:
3406 case CRUSH_RULE_CHOOSELEAF_INDEP
:
3408 int numrep
= curstep
->arg1
;
3409 int type
= curstep
->arg2
;
3412 type_stack
.push_back(make_pair(type
, numrep
));
3414 type_stack
.push_back(make_pair(0, 1));
3415 int r
= _choose_type_stack(cct
, type_stack
, overfull
, underfull
, orig
,
3423 case CRUSH_RULE_CHOOSE_FIRSTN
:
3424 case CRUSH_RULE_CHOOSE_INDEP
:
3426 int numrep
= curstep
->arg1
;
3427 int type
= curstep
->arg2
;
3430 type_stack
.push_back(make_pair(type
, numrep
));
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
,
3443 for (auto item
: w
) {
3444 out
->push_back(item
);
3459 int CrushWrapper::_choose_args_adjust_item_weight_in_bucket(
3461 crush_choose_arg_map cmap
,
3464 const vector
<int>& weight
,
3468 int bidx
= -1 - bucketid
;
3469 crush_bucket
*b
= crush
->buckets
[bidx
];
3470 if (bidx
>= (int)cmap
.size
) {
3472 *ss
<< "no weight-set for bucket " << b
->id
;
3473 ldout(cct
, 10) << __func__
<< " no crush_choose_arg for bucket " << b
->id
3477 crush_choose_arg
*carg
= &cmap
.args
[bidx
];
3478 if (carg
->weight_set
== NULL
) {
3479 // create a weight-set for this bucket and populate it with the
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
);
3494 if (carg
->weight_set_positions
!= weight
.size()) {
3496 *ss
<< "weight_set_positions != " << weight
.size() << " for bucket " << b
->id
;
3497 ldout(cct
, 10) << __func__
<< " weight_set_positions != " << weight
.size()
3498 << " for bucket " << b
->id
<< dendl
;
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
];
3506 ldout(cct
, 5) << __func__
<< " set " << id
<< " to " << weight
3507 << " in bucket " << b
->id
<< dendl
;
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
];
3518 choose_args_adjust_item_weight(cct
, cmap
, b
->id
, bucket_weight
, nullptr);
3523 int CrushWrapper::choose_args_adjust_item_weight(
3525 crush_choose_arg_map cmap
,
3527 const vector
<int>& weight
,
3530 ldout(cct
, 5) << __func__
<< " " << id
<< " weight " << weight
<< dendl
;
3532 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
3533 crush_bucket
*b
= crush
->buckets
[bidx
];
3537 changed
+= _choose_args_adjust_item_weight_in_bucket(
3538 cct
, cmap
, b
->id
, id
, weight
, ss
);
3542 *ss
<< "item " << id
<< " not found in crush map";