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_rulesets() const
18 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
19 crush_rule
*r
= crush
->rules
[i
];
21 r
->mask
.ruleset
!= i
) {
28 int CrushWrapper::renumber_rules_by_ruleset()
31 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
32 crush_rule
*r
= crush
->rules
[i
];
33 if (r
&& r
->mask
.ruleset
>= max_ruleset
) {
34 max_ruleset
= r
->mask
.ruleset
+ 1;
37 struct crush_rule
**newrules
=
38 (crush_rule
**)calloc(1, max_ruleset
* sizeof(crush_rule
*));
39 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
40 crush_rule
*r
= crush
->rules
[i
];
43 if (newrules
[r
->mask
.ruleset
]) {
44 // collision, we can't do it.
48 newrules
[r
->mask
.ruleset
] = r
;
53 crush
->rules
= newrules
;
54 crush
->max_rules
= max_ruleset
;
58 bool CrushWrapper::has_multirule_rulesets() const
60 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
61 crush_rule
*r
= crush
->rules
[i
];
64 for (unsigned j
=i
+1; j
<crush
->max_rules
; j
++) {
65 crush_rule
*s
= crush
->rules
[j
];
68 if (r
->mask
.ruleset
== s
->mask
.ruleset
)
75 bool CrushWrapper::has_non_straw2_buckets() const
77 for (int i
=0; i
<crush
->max_buckets
; ++i
) {
78 crush_bucket
*b
= crush
->buckets
[i
];
81 if (b
->alg
!= CRUSH_BUCKET_STRAW2
)
87 bool CrushWrapper::has_v2_rules() const
89 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
97 bool CrushWrapper::is_v2_rule(unsigned ruleid
) const
99 // check rule for use of indep or new SET_* rule steps
100 if (ruleid
>= crush
->max_rules
)
102 crush_rule
*r
= crush
->rules
[ruleid
];
105 for (unsigned j
=0; j
<r
->len
; j
++) {
106 if (r
->steps
[j
].op
== CRUSH_RULE_CHOOSE_INDEP
||
107 r
->steps
[j
].op
== CRUSH_RULE_CHOOSELEAF_INDEP
||
108 r
->steps
[j
].op
== CRUSH_RULE_SET_CHOOSE_TRIES
||
109 r
->steps
[j
].op
== CRUSH_RULE_SET_CHOOSELEAF_TRIES
) {
116 bool CrushWrapper::has_v3_rules() const
118 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
126 bool CrushWrapper::is_v3_rule(unsigned ruleid
) const
128 // check rule for use of SET_CHOOSELEAF_VARY_R step
129 if (ruleid
>= crush
->max_rules
)
131 crush_rule
*r
= crush
->rules
[ruleid
];
134 for (unsigned j
=0; j
<r
->len
; j
++) {
135 if (r
->steps
[j
].op
== CRUSH_RULE_SET_CHOOSELEAF_VARY_R
) {
142 bool CrushWrapper::has_v4_buckets() const
144 for (int i
=0; i
<crush
->max_buckets
; ++i
) {
145 crush_bucket
*b
= crush
->buckets
[i
];
148 if (b
->alg
== CRUSH_BUCKET_STRAW2
)
154 bool CrushWrapper::has_v5_rules() const
156 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
164 bool CrushWrapper::is_v5_rule(unsigned ruleid
) const
166 // check rule for use of SET_CHOOSELEAF_STABLE step
167 if (ruleid
>= crush
->max_rules
)
169 crush_rule
*r
= crush
->rules
[ruleid
];
172 for (unsigned j
=0; j
<r
->len
; j
++) {
173 if (r
->steps
[j
].op
== CRUSH_RULE_SET_CHOOSELEAF_STABLE
) {
180 bool CrushWrapper::has_choose_args() const
182 return !choose_args
.empty();
185 bool CrushWrapper::has_incompat_choose_args() const
187 if (choose_args
.empty())
189 if (choose_args
.size() > 1)
191 if (choose_args
.begin()->first
!= DEFAULT_CHOOSE_ARGS
)
193 crush_choose_arg_map arg_map
= choose_args
.begin()->second
;
194 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
195 crush_choose_arg
*arg
= &arg_map
.args
[i
];
196 if (arg
->weight_set_size
== 0 &&
199 if (arg
->weight_set_size
!= 1)
201 if (arg
->ids_size
!= 0)
207 int CrushWrapper::split_id_class(int i
, int *idout
, int *classout
) const
211 string name
= get_item_name(i
);
212 size_t pos
= name
.find("~");
213 if (pos
== string::npos
) {
218 string name_no_class
= name
.substr(0, pos
);
219 if (!name_exists(name_no_class
))
221 string class_name
= name
.substr(pos
+ 1);
222 if (!class_exists(class_name
))
224 *idout
= get_item_id(name_no_class
);
225 *classout
= get_class_id(class_name
);
229 int CrushWrapper::can_rename_item(const string
& srcname
,
230 const string
& dstname
,
233 if (name_exists(srcname
)) {
234 if (name_exists(dstname
)) {
235 *ss
<< "dstname = '" << dstname
<< "' already exists";
238 if (is_valid_crush_name(dstname
)) {
241 *ss
<< "dstname = '" << dstname
<< "' does not match [-_.0-9a-zA-Z]+";
245 if (name_exists(dstname
)) {
246 *ss
<< "srcname = '" << srcname
<< "' does not exist "
247 << "and dstname = '" << dstname
<< "' already exists";
250 *ss
<< "srcname = '" << srcname
<< "' does not exist";
256 int CrushWrapper::rename_item(const string
& srcname
,
257 const string
& dstname
,
260 int ret
= can_rename_item(srcname
, dstname
, ss
);
263 int oldid
= get_item_id(srcname
);
264 return set_item_name(oldid
, dstname
);
267 int CrushWrapper::can_rename_bucket(const string
& srcname
,
268 const string
& dstname
,
271 int ret
= can_rename_item(srcname
, dstname
, ss
);
274 int srcid
= get_item_id(srcname
);
276 *ss
<< "srcname = '" << srcname
<< "' is not a bucket "
277 << "because its id = " << srcid
<< " is >= 0";
283 int CrushWrapper::rename_bucket(const string
& srcname
,
284 const string
& dstname
,
287 int ret
= can_rename_bucket(srcname
, dstname
, ss
);
290 int oldid
= get_item_id(srcname
);
291 return set_item_name(oldid
, dstname
);
294 void CrushWrapper::find_takes(set
<int>& roots
) const
296 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
297 crush_rule
*r
= crush
->rules
[i
];
300 for (unsigned j
=0; j
<r
->len
; j
++) {
301 if (r
->steps
[j
].op
== CRUSH_RULE_TAKE
)
302 roots
.insert(r
->steps
[j
].arg1
);
307 void CrushWrapper::find_roots(set
<int>& roots
) const
309 for (int i
= 0; i
< crush
->max_buckets
; i
++) {
310 if (!crush
->buckets
[i
])
312 crush_bucket
*b
= crush
->buckets
[i
];
313 if (!_search_item_exists(b
->id
))
318 bool CrushWrapper::subtree_contains(int root
, int item
) const
324 return false; // root is a leaf
326 const crush_bucket
*b
= get_bucket(root
);
330 for (unsigned j
=0; j
<b
->size
; j
++) {
331 if (subtree_contains(b
->items
[j
], item
))
337 bool CrushWrapper::_maybe_remove_last_instance(CephContext
*cct
, int item
, bool unlink_only
)
340 if (_search_item_exists(item
)) {
343 if (item
< 0 && _bucket_is_in_use(item
)) {
347 if (item
< 0 && !unlink_only
) {
348 crush_bucket
*t
= get_bucket(item
);
349 ldout(cct
, 5) << "_maybe_remove_last_instance removing bucket " << item
<< dendl
;
350 crush_remove_bucket(crush
, t
);
351 if (class_bucket
.count(item
) != 0)
352 class_bucket
.erase(item
);
353 class_remove_item(item
);
355 if ((item
>= 0 || !unlink_only
) && name_map
.count(item
)) {
356 ldout(cct
, 5) << "_maybe_remove_last_instance removing name for item " << item
<< dendl
;
357 name_map
.erase(item
);
359 if (item
>= 0 && !unlink_only
) {
360 class_remove_item(item
);
366 int CrushWrapper::remove_root(int item
, bool unused
)
368 if (unused
&& _bucket_is_in_use(item
))
371 crush_bucket
*b
= get_bucket(item
);
375 for (unsigned n
= 0; n
< b
->size
; n
++) {
376 if (b
->items
[n
] >= 0)
378 int r
= remove_root(b
->items
[n
], unused
);
383 crush_remove_bucket(crush
, b
);
384 if (name_map
.count(item
) != 0) {
385 name_map
.erase(item
);
388 if (class_bucket
.count(item
) != 0)
389 class_bucket
.erase(item
);
390 class_remove_item(item
);
394 int CrushWrapper::remove_item(CephContext
*cct
, int item
, bool unlink_only
)
396 ldout(cct
, 5) << "remove_item " << item
397 << (unlink_only
? " unlink_only":"") << dendl
;
401 if (item
< 0 && !unlink_only
) {
402 crush_bucket
*t
= get_bucket(item
);
404 ldout(cct
, 1) << "remove_item bucket " << item
<< " does not exist"
410 ldout(cct
, 1) << "remove_item bucket " << item
<< " has " << t
->size
411 << " items, not empty" << dendl
;
414 if (_bucket_is_in_use(item
)) {
419 for (int i
= 0; i
< crush
->max_buckets
; i
++) {
420 if (!crush
->buckets
[i
])
422 crush_bucket
*b
= crush
->buckets
[i
];
424 for (unsigned i
=0; i
<b
->size
; ++i
) {
425 int id
= b
->items
[i
];
427 ldout(cct
, 5) << "remove_item removing item " << item
428 << " from bucket " << b
->id
<< dendl
;
429 for (auto& p
: choose_args
) {
430 // weight down each weight-set to 0 before we remove the item
431 vector
<int> weightv(get_choose_args_positions(p
.second
), 0);
432 choose_args_adjust_item_weight(cct
, p
.second
, item
, weightv
, nullptr);
434 bucket_remove_item(b
, item
);
435 adjust_item_weight(cct
, b
->id
, b
->weight
);
441 if (_maybe_remove_last_instance(cct
, item
, unlink_only
))
447 bool CrushWrapper::_search_item_exists(int item
) const
449 for (int i
= 0; i
< crush
->max_buckets
; i
++) {
450 if (!crush
->buckets
[i
])
452 crush_bucket
*b
= crush
->buckets
[i
];
453 for (unsigned j
=0; j
<b
->size
; ++j
) {
454 if (b
->items
[j
] == item
)
461 bool CrushWrapper::_bucket_is_in_use(int item
)
463 for (auto &i
: class_bucket
)
464 for (auto &j
: i
.second
)
465 if (j
.second
== item
)
467 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
468 crush_rule
*r
= crush
->rules
[i
];
471 for (unsigned j
= 0; j
< r
->len
; ++j
) {
472 if (r
->steps
[j
].op
== CRUSH_RULE_TAKE
) {
473 int step_item
= r
->steps
[j
].arg1
;
476 int res
= split_id_class(step_item
, &original_item
, &c
);
479 if (step_item
== item
|| original_item
== item
)
487 int CrushWrapper::_remove_item_under(
488 CephContext
*cct
, int item
, int ancestor
, bool unlink_only
)
490 ldout(cct
, 5) << "_remove_item_under " << item
<< " under " << ancestor
491 << (unlink_only
? " unlink_only":"") << dendl
;
497 if (!bucket_exists(ancestor
))
502 crush_bucket
*b
= get_bucket(ancestor
);
503 for (unsigned i
=0; i
<b
->size
; ++i
) {
504 int id
= b
->items
[i
];
506 ldout(cct
, 5) << "_remove_item_under removing item " << item
507 << " from bucket " << b
->id
<< dendl
;
508 bucket_remove_item(b
, item
);
509 for (auto& p
: choose_args
) {
510 // weight down each weight-set to 0 before we remove the item
511 vector
<int> weightv(get_choose_args_positions(p
.second
), 0);
512 _choose_args_adjust_item_weight_in_bucket(
513 cct
, p
.second
, b
->id
, item
, weightv
, nullptr);
515 adjust_item_weight(cct
, b
->id
, b
->weight
);
518 int r
= remove_item_under(cct
, item
, id
, unlink_only
);
526 int CrushWrapper::remove_item_under(
527 CephContext
*cct
, int item
, int ancestor
, bool unlink_only
)
529 ldout(cct
, 5) << "remove_item_under " << item
<< " under " << ancestor
530 << (unlink_only
? " unlink_only":"") << dendl
;
532 if (!unlink_only
&& _bucket_is_in_use(item
)) {
536 int ret
= _remove_item_under(cct
, item
, ancestor
, unlink_only
);
540 if (item
< 0 && !unlink_only
) {
541 crush_bucket
*t
= get_bucket(item
);
543 ldout(cct
, 1) << "remove_item_under bucket " << item
544 << " does not exist" << dendl
;
549 ldout(cct
, 1) << "remove_item_under bucket " << item
<< " has " << t
->size
550 << " items, not empty" << dendl
;
555 if (_maybe_remove_last_instance(cct
, item
, unlink_only
))
561 int CrushWrapper::get_common_ancestor_distance(CephContext
*cct
, int id
,
562 const std::multimap
<string
,string
>& loc
)
564 ldout(cct
, 5) << __func__
<< " " << id
<< " " << loc
<< dendl
;
565 if (!item_exists(id
))
567 map
<string
,string
> id_loc
= get_full_location(id
);
568 ldout(cct
, 20) << " id is at " << id_loc
<< dendl
;
570 for (map
<int,string
>::const_iterator p
= type_map
.begin();
573 map
<string
,string
>::iterator ip
= id_loc
.find(p
->second
);
574 if (ip
== id_loc
.end())
576 for (std::multimap
<string
,string
>::const_iterator q
= loc
.find(p
->second
);
579 if (q
->first
!= p
->second
)
581 if (q
->second
== ip
->second
)
588 int CrushWrapper::parse_loc_map(const std::vector
<string
>& args
,
589 std::map
<string
,string
> *ploc
)
592 for (unsigned i
= 0; i
< args
.size(); ++i
) {
593 const char *s
= args
[i
].c_str();
594 const char *pos
= strchr(s
, '=');
597 string
key(s
, 0, pos
-s
);
600 (*ploc
)[key
] = value
;
607 int CrushWrapper::parse_loc_multimap(const std::vector
<string
>& args
,
608 std::multimap
<string
,string
> *ploc
)
611 for (unsigned i
= 0; i
< args
.size(); ++i
) {
612 const char *s
= args
[i
].c_str();
613 const char *pos
= strchr(s
, '=');
616 string
key(s
, 0, pos
-s
);
619 ploc
->insert(make_pair(key
, value
));
626 bool CrushWrapper::check_item_loc(CephContext
*cct
, int item
, const map
<string
,string
>& loc
,
629 ldout(cct
, 5) << "check_item_loc item " << item
<< " loc " << loc
<< dendl
;
631 for (map
<int,string
>::const_iterator p
= type_map
.begin(); p
!= type_map
.end(); ++p
) {
636 // ignore types that aren't specified in loc
637 map
<string
,string
>::const_iterator q
= loc
.find(p
->second
);
638 if (q
== loc
.end()) {
639 ldout(cct
, 2) << "warning: did not specify location for '" << p
->second
<< "' level (levels are "
640 << type_map
<< ")" << dendl
;
644 if (!name_exists(q
->second
)) {
645 ldout(cct
, 5) << "check_item_loc bucket " << q
->second
<< " dne" << dendl
;
649 int id
= get_item_id(q
->second
);
651 ldout(cct
, 5) << "check_item_loc requested " << q
->second
<< " for type " << p
->second
652 << " is a device, not bucket" << dendl
;
656 assert(bucket_exists(id
));
657 crush_bucket
*b
= get_bucket(id
);
659 // see if item exists in this bucket
660 for (unsigned j
=0; j
<b
->size
; j
++) {
661 if (b
->items
[j
] == item
) {
662 ldout(cct
, 2) << "check_item_loc " << item
<< " exists in bucket " << b
->id
<< dendl
;
664 *weight
= crush_get_bucket_item_weight(b
, j
);
671 ldout(cct
, 1) << "check_item_loc item " << item
<< " loc " << loc
<< dendl
;
675 map
<string
, string
> CrushWrapper::get_full_location(int id
)
677 vector
<pair
<string
, string
> > full_location_ordered
;
678 map
<string
,string
> full_location
;
680 get_full_location_ordered(id
, full_location_ordered
);
682 std::copy(full_location_ordered
.begin(),
683 full_location_ordered
.end(),
684 std::inserter(full_location
, full_location
.begin()));
686 return full_location
;
689 int CrushWrapper::get_full_location_ordered(int id
, vector
<pair
<string
, string
> >& path
)
691 if (!item_exists(id
))
696 pair
<string
, string
> parent_coord
= get_immediate_parent(cur
, &ret
);
699 path
.push_back(parent_coord
);
700 cur
= get_item_id(parent_coord
.second
);
705 string
CrushWrapper::get_full_location_ordered_string(int id
)
707 vector
<pair
<string
, string
> > full_location_ordered
;
708 string full_location
;
709 get_full_location_ordered(id
, full_location_ordered
);
710 reverse(begin(full_location_ordered
), end(full_location_ordered
));
711 for(auto i
= full_location_ordered
.begin(); i
!= full_location_ordered
.end(); i
++) {
712 full_location
= full_location
+ i
->first
+ "=" + i
->second
;
713 if (i
!= full_location_ordered
.end() - 1) {
714 full_location
= full_location
+ ",";
717 return full_location
;
720 map
<int, string
> CrushWrapper::get_parent_hierarchy(int id
)
722 map
<int,string
> parent_hierarchy
;
723 pair
<string
, string
> parent_coord
= get_immediate_parent(id
);
726 // get the integer type for id and create a counter from there
727 int type_counter
= get_bucket_type(id
);
729 // if we get a negative type then we can assume that we have an OSD
730 // change behavior in get_item_type FIXME
731 if (type_counter
< 0)
734 // read the type map and get the name of the type with the largest ID
736 for (map
<int, string
>::iterator it
= type_map
.begin(); it
!= type_map
.end(); ++it
){
737 if ( (*it
).first
> high_type
)
738 high_type
= (*it
).first
;
741 parent_id
= get_item_id(parent_coord
.second
);
743 while (type_counter
< high_type
) {
745 parent_hierarchy
[ type_counter
] = parent_coord
.first
;
747 if (type_counter
< high_type
){
748 // get the coordinate information for the next parent
749 parent_coord
= get_immediate_parent(parent_id
);
750 parent_id
= get_item_id(parent_coord
.second
);
754 return parent_hierarchy
;
757 int CrushWrapper::get_children(int id
, list
<int> *children
)
764 crush_bucket
*b
= get_bucket(id
);
769 for (unsigned n
=0; n
<b
->size
; n
++) {
770 children
->push_back(b
->items
[n
]);
775 int CrushWrapper::_get_leaves(int id
, list
<int> *leaves
)
781 leaves
->push_back(id
);
785 crush_bucket
*b
= get_bucket(id
);
790 for (unsigned n
= 0; n
< b
->size
; n
++) {
791 if (b
->items
[n
] >= 0) {
792 leaves
->push_back(b
->items
[n
]);
794 // is a bucket, do recursive call
795 int r
= _get_leaves(b
->items
[n
], leaves
);
802 return 0; // all is well
805 int CrushWrapper::get_leaves(const string
&name
, set
<int> *leaves
)
810 if (!name_exists(name
)) {
814 int id
= get_item_id(name
);
822 int r
= _get_leaves(id
, &unordered
);
827 for (auto &p
: unordered
) {
834 int CrushWrapper::insert_item(
835 CephContext
*cct
, int item
, float weight
, string name
,
836 const map
<string
,string
>& loc
) // typename -> bucketname
838 ldout(cct
, 5) << "insert_item item " << item
<< " weight " << weight
839 << " name " << name
<< " loc " << loc
<< dendl
;
841 if (!is_valid_crush_name(name
))
844 if (!is_valid_crush_loc(cct
, loc
))
847 int r
= validate_weightf(weight
);
852 if (name_exists(name
)) {
853 if (get_item_id(name
) != item
) {
854 ldout(cct
, 10) << "device name '" << name
<< "' already exists as id "
855 << get_item_id(name
) << dendl
;
859 set_item_name(item
, name
);
864 // create locations if locations don't exist and add child in
865 // location with 0 weight the more detail in the insert_item method
866 // declaration in CrushWrapper.h
867 for (auto p
= type_map
.begin(); p
!= type_map
.end(); ++p
) {
868 // ignore device type
872 // skip types that are unspecified
873 map
<string
,string
>::const_iterator q
= loc
.find(p
->second
);
874 if (q
== loc
.end()) {
875 ldout(cct
, 2) << "warning: did not specify location for '"
876 << p
->second
<< "' level (levels are "
877 << type_map
<< ")" << dendl
;
881 if (!name_exists(q
->second
)) {
882 ldout(cct
, 5) << "insert_item creating bucket " << q
->second
<< dendl
;
883 int empty
= 0, newid
;
884 int r
= add_bucket(0, 0,
885 CRUSH_HASH_DEFAULT
, p
->first
, 1, &cur
, &empty
, &newid
);
887 ldout(cct
, 1) << "add_bucket failure error: " << cpp_strerror(r
)
891 set_item_name(newid
, q
->second
);
897 // add to an existing bucket
898 int id
= get_item_id(q
->second
);
899 if (!bucket_exists(id
)) {
900 ldout(cct
, 1) << "insert_item doesn't have bucket " << id
<< dendl
;
904 // check that we aren't creating a cycle.
905 if (subtree_contains(id
, cur
)) {
906 ldout(cct
, 1) << "insert_item item " << cur
<< " already exists beneath "
911 // we have done sanity check above
912 crush_bucket
*b
= get_bucket(id
);
914 if (p
->first
!= b
->type
) {
915 ldout(cct
, 1) << "insert_item existing bucket has type "
916 << "'" << type_map
[b
->type
] << "' != "
917 << "'" << type_map
[p
->first
] << "'" << dendl
;
921 // are we forming a loop?
922 if (subtree_contains(cur
, b
->id
)) {
923 ldout(cct
, 1) << "insert_item " << cur
<< " already contains " << b
->id
924 << "; cannot form loop" << dendl
;
928 ldout(cct
, 5) << "insert_item adding " << cur
<< " weight " << weight
929 << " to bucket " << id
<< dendl
;
930 int r
= bucket_add_item(b
, cur
, 0);
935 // adjust the item's weight in location
936 if (adjust_item_weightf_in_loc(cct
, item
, weight
, loc
) > 0) {
937 if (item
>= crush
->max_devices
) {
938 crush
->max_devices
= item
+ 1;
939 ldout(cct
, 5) << "insert_item max_devices now " << crush
->max_devices
942 r
= rebuild_roots_with_classes();
944 ldout(cct
, 0) << __func__
<< " unable to rebuild roots with classes: "
945 << cpp_strerror(r
) << dendl
;
951 ldout(cct
, 1) << "error: didn't find anywhere to add item " << item
952 << " in " << loc
<< dendl
;
957 int CrushWrapper::move_bucket(
958 CephContext
*cct
, int id
, const map
<string
,string
>& loc
)
960 // sorry this only works for buckets
964 if (!item_exists(id
))
967 // get the name of the bucket we are trying to move for later
968 string id_name
= get_item_name(id
);
971 int bucket_weight
= detach_bucket(cct
, id
);
973 // insert the bucket back into the hierarchy
974 return insert_item(cct
, id
, bucket_weight
/ (float)0x10000, id_name
, loc
);
977 int CrushWrapper::detach_bucket(CephContext
*cct
, int item
)
985 // check that the bucket that we want to detach exists
986 assert(bucket_exists(item
));
988 // get the bucket's weight
989 crush_bucket
*b
= get_bucket(item
);
990 unsigned bucket_weight
= b
->weight
;
992 // get where the bucket is located
993 pair
<string
, string
> bucket_location
= get_immediate_parent(item
);
995 // get the id of the parent bucket
996 int parent_id
= get_item_id(bucket_location
.second
);
998 // get the parent bucket
999 crush_bucket
*parent_bucket
= get_bucket(parent_id
);
1001 if (!IS_ERR(parent_bucket
)) {
1002 // zero out the bucket weight
1003 bucket_adjust_item_weight(cct
, parent_bucket
, item
, 0);
1004 adjust_item_weight(cct
, parent_bucket
->id
, parent_bucket
->weight
);
1005 for (auto& p
: choose_args
) {
1006 // weight down each weight-set to 0 before we remove the item
1007 vector
<int> weightv(get_choose_args_positions(p
.second
), 0);
1008 choose_args_adjust_item_weight(cct
, p
.second
, item
, weightv
, nullptr);
1011 // remove the bucket from the parent
1012 bucket_remove_item(parent_bucket
, item
);
1013 } else if (PTR_ERR(parent_bucket
) != -ENOENT
) {
1014 return PTR_ERR(parent_bucket
);
1017 // check that we're happy
1018 int test_weight
= 0;
1019 map
<string
,string
> test_location
;
1020 test_location
[ bucket_location
.first
] = (bucket_location
.second
);
1022 bool successful_detach
= !(check_item_loc(cct
, item
, test_location
,
1024 assert(successful_detach
);
1025 assert(test_weight
== 0);
1027 return bucket_weight
;
1030 int CrushWrapper::swap_bucket(CephContext
*cct
, int src
, int dst
)
1032 if (src
>= 0 || dst
>= 0)
1034 if (!item_exists(src
) || !item_exists(dst
))
1036 crush_bucket
*a
= get_bucket(src
);
1037 crush_bucket
*b
= get_bucket(dst
);
1038 unsigned aw
= a
->weight
;
1039 unsigned bw
= b
->weight
;
1042 adjust_item_weight(cct
, a
->id
, bw
);
1043 adjust_item_weight(cct
, b
->id
, aw
);
1046 map
<int,unsigned> tmp
;
1047 unsigned as
= a
->size
;
1048 unsigned bs
= b
->size
;
1049 for (unsigned i
= 0; i
< as
; ++i
) {
1050 int item
= a
->items
[0];
1051 int itemw
= crush_get_bucket_item_weight(a
, 0);
1053 bucket_remove_item(a
, item
);
1055 assert(a
->size
== 0);
1056 assert(b
->size
== bs
);
1057 for (unsigned i
= 0; i
< bs
; ++i
) {
1058 int item
= b
->items
[0];
1059 int itemw
= crush_get_bucket_item_weight(b
, 0);
1060 bucket_remove_item(b
, item
);
1061 bucket_add_item(a
, item
, itemw
);
1063 assert(a
->size
== bs
);
1064 assert(b
->size
== 0);
1065 for (auto t
: tmp
) {
1066 bucket_add_item(b
, t
.first
, t
.second
);
1068 assert(a
->size
== bs
);
1069 assert(b
->size
== as
);
1072 swap_names(src
, dst
);
1076 int CrushWrapper::link_bucket(
1077 CephContext
*cct
, int id
, const map
<string
,string
>& loc
)
1079 // sorry this only works for buckets
1083 if (!item_exists(id
))
1086 // get the name of the bucket we are trying to move for later
1087 string id_name
= get_item_name(id
);
1089 crush_bucket
*b
= get_bucket(id
);
1090 unsigned bucket_weight
= b
->weight
;
1092 return insert_item(cct
, id
, bucket_weight
/ (float)0x10000, id_name
, loc
);
1095 int CrushWrapper::create_or_move_item(
1096 CephContext
*cct
, int item
, float weight
, string name
,
1097 const map
<string
,string
>& loc
) // typename -> bucketname
1102 if (!is_valid_crush_name(name
))
1105 if (check_item_loc(cct
, item
, loc
, &old_iweight
)) {
1106 ldout(cct
, 5) << "create_or_move_item " << item
<< " already at " << loc
1109 if (_search_item_exists(item
)) {
1110 weight
= get_item_weightf(item
);
1111 ldout(cct
, 10) << "create_or_move_item " << item
1112 << " exists with weight " << weight
<< dendl
;
1113 remove_item(cct
, item
, true);
1115 ldout(cct
, 5) << "create_or_move_item adding " << item
1116 << " weight " << weight
1117 << " at " << loc
<< dendl
;
1118 ret
= insert_item(cct
, item
, weight
, name
, loc
);
1125 int CrushWrapper::update_item(
1126 CephContext
*cct
, int item
, float weight
, string name
,
1127 const map
<string
,string
>& loc
) // typename -> bucketname
1129 ldout(cct
, 5) << "update_item item " << item
<< " weight " << weight
1130 << " name " << name
<< " loc " << loc
<< dendl
;
1133 if (!is_valid_crush_name(name
))
1136 if (!is_valid_crush_loc(cct
, loc
))
1139 ret
= validate_weightf(weight
);
1144 // compare quantized (fixed-point integer) weights!
1145 int iweight
= (int)(weight
* (float)0x10000);
1147 if (check_item_loc(cct
, item
, loc
, &old_iweight
)) {
1148 ldout(cct
, 5) << "update_item " << item
<< " already at " << loc
<< dendl
;
1149 if (old_iweight
!= iweight
) {
1150 ldout(cct
, 5) << "update_item " << item
<< " adjusting weight "
1151 << ((float)old_iweight
/(float)0x10000) << " -> " << weight
1153 adjust_item_weight_in_loc(cct
, item
, iweight
, loc
);
1156 if (get_item_name(item
) != name
) {
1157 ldout(cct
, 5) << "update_item setting " << item
<< " name to " << name
1159 set_item_name(item
, name
);
1163 if (item_exists(item
)) {
1164 remove_item(cct
, item
, true);
1166 ldout(cct
, 5) << "update_item adding " << item
<< " weight " << weight
1167 << " at " << loc
<< dendl
;
1168 ret
= insert_item(cct
, item
, weight
, name
, loc
);
1175 int CrushWrapper::get_item_weight(int id
) const
1177 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1178 crush_bucket
*b
= crush
->buckets
[bidx
];
1183 for (unsigned i
= 0; i
< b
->size
; i
++)
1184 if (b
->items
[i
] == id
)
1185 return crush_get_bucket_item_weight(b
, i
);
1190 int CrushWrapper::get_item_weight_in_loc(int id
, const map
<string
,string
> &loc
)
1192 for (map
<string
,string
>::const_iterator l
= loc
.begin(); l
!= loc
.end(); ++l
) {
1194 int bid
= get_item_id(l
->second
);
1195 if (!bucket_exists(bid
))
1197 crush_bucket
*b
= get_bucket(bid
);
1198 for (unsigned int i
= 0; i
< b
->size
; i
++) {
1199 if (b
->items
[i
] == id
) {
1200 return crush_get_bucket_item_weight(b
, i
);
1207 int CrushWrapper::adjust_item_weight(CephContext
*cct
, int id
, int weight
)
1209 ldout(cct
, 5) << "adjust_item_weight " << id
<< " weight " << weight
<< dendl
;
1211 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1212 crush_bucket
*b
= crush
->buckets
[bidx
];
1215 for (unsigned i
= 0; i
< b
->size
; i
++) {
1216 if (b
->items
[i
] == id
) {
1217 int diff
= bucket_adjust_item_weight(cct
, b
, id
, weight
);
1218 ldout(cct
, 5) << "adjust_item_weight " << id
<< " diff " << diff
1219 << " in bucket " << bidx
<< dendl
;
1220 adjust_item_weight(cct
, -1 - bidx
, b
->weight
);
1230 int CrushWrapper::adjust_item_weight_in_loc(CephContext
*cct
, int id
, int weight
, const map
<string
,string
>& loc
)
1232 ldout(cct
, 5) << "adjust_item_weight_in_loc " << id
<< " weight " << weight
1233 << " in " << loc
<< dendl
;
1236 for (auto l
= loc
.begin(); l
!= loc
.end(); ++l
) {
1237 int bid
= get_item_id(l
->second
);
1238 if (!bucket_exists(bid
))
1240 crush_bucket
*b
= get_bucket(bid
);
1241 for (unsigned int i
= 0; i
< b
->size
; i
++) {
1242 if (b
->items
[i
] == id
) {
1243 int diff
= bucket_adjust_item_weight(cct
, b
, id
, weight
);
1244 ldout(cct
, 5) << "adjust_item_weight_in_loc " << id
<< " diff " << diff
1245 << " in bucket " << bid
<< dendl
;
1246 adjust_item_weight(cct
, bid
, b
->weight
);
1256 int CrushWrapper::adjust_subtree_weight(CephContext
*cct
, int id
, int weight
)
1258 ldout(cct
, 5) << __func__
<< " " << id
<< " weight " << weight
<< dendl
;
1259 crush_bucket
*b
= get_bucket(id
);
1263 list
<crush_bucket
*> q
;
1265 while (!q
.empty()) {
1268 int local_changed
= 0;
1269 for (unsigned i
=0; i
<b
->size
; ++i
) {
1270 int n
= b
->items
[i
];
1272 bucket_adjust_item_weight(cct
, b
, n
, weight
);
1276 crush_bucket
*sub
= get_bucket(n
);
1282 if (local_changed
) {
1283 adjust_item_weight(cct
, b
->id
, b
->weight
);
1289 bool CrushWrapper::check_item_present(int id
) const
1293 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1294 crush_bucket
*b
= crush
->buckets
[bidx
];
1297 for (unsigned i
= 0; i
< b
->size
; i
++)
1298 if (b
->items
[i
] == id
)
1305 pair
<string
,string
> CrushWrapper::get_immediate_parent(int id
, int *_ret
)
1308 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1309 crush_bucket
*b
= crush
->buckets
[bidx
];
1312 if (is_shadow_item(b
->id
))
1314 for (unsigned i
= 0; i
< b
->size
; i
++)
1315 if (b
->items
[i
] == id
) {
1316 string parent_id
= name_map
[b
->id
];
1317 string parent_bucket_type
= type_map
[b
->type
];
1320 return make_pair(parent_bucket_type
, parent_id
);
1327 return pair
<string
, string
>();
1330 int CrushWrapper::get_immediate_parent_id(int id
, int *parent
) const
1332 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1333 crush_bucket
*b
= crush
->buckets
[bidx
];
1336 if (is_shadow_item(b
->id
))
1338 for (unsigned i
= 0; i
< b
->size
; i
++) {
1339 if (b
->items
[i
] == id
) {
1348 int CrushWrapper::get_parent_of_type(int item
, int type
) const
1351 int r
= get_immediate_parent_id(item
, &item
);
1355 } while (get_bucket_type(item
) != type
);
1360 bool CrushWrapper::class_is_in_use(int class_id
, ostream
*ss
)
1362 list
<unsigned> rules
;
1363 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
1364 crush_rule
*r
= crush
->rules
[i
];
1367 for (unsigned j
= 0; j
< r
->len
; ++j
) {
1368 if (r
->steps
[j
].op
== CRUSH_RULE_TAKE
) {
1369 int root
= r
->steps
[j
].arg1
;
1370 for (auto &p
: class_bucket
) {
1372 if (q
.count(class_id
) && q
[class_id
] == root
) {
1379 if (rules
.empty()) {
1384 for (auto &p
: rules
) {
1385 os
<< "'" << get_rule_name(p
) <<"',";
1387 string
out(os
.str());
1388 out
.resize(out
.size() - 1); // drop last ','
1389 *ss
<< "still referenced by crush_rule(s): " << out
;
1394 int CrushWrapper::populate_classes()
1397 find_nonshadow_roots(roots
);
1398 for (auto &r
: roots
) {
1401 for (auto &c
: class_name
) {
1403 int res
= device_class_clone(r
, c
.first
, &clone
);
1411 int CrushWrapper::cleanup_classes()
1413 return trim_roots_with_class(true);
1416 int CrushWrapper::trim_roots_with_class(bool unused
)
1419 find_shadow_roots(roots
);
1420 for (auto &r
: roots
) {
1423 int res
= remove_root(r
, unused
);
1427 // there is no need to reweight because we only remove from the
1432 int32_t CrushWrapper::_alloc_class_id() const {
1433 if (class_name
.empty()) {
1436 int32_t class_id
= class_name
.rbegin()->first
+ 1;
1437 if (class_id
>= 0) {
1440 // wrapped, pick a random start and do exhaustive search
1441 uint32_t upperlimit
= numeric_limits
<int32_t>::max();
1443 class_id
= rand() % upperlimit
;
1444 const auto start
= class_id
;
1446 if (!class_name
.count(class_id
)) {
1454 } while (class_id
!= start
);
1455 assert(0 == "no available class id");
1458 void CrushWrapper::reweight(CephContext
*cct
)
1462 for (set
<int>::iterator p
= roots
.begin(); p
!= roots
.end(); ++p
) {
1465 crush_bucket
*b
= get_bucket(*p
);
1466 ldout(cct
, 5) << "reweight bucket " << *p
<< dendl
;
1467 int r
= crush_reweight_bucket(crush
, b
);
1472 int CrushWrapper::add_simple_rule_at(
1473 string name
, string root_name
,
1474 string failure_domain_name
,
1475 string device_class
,
1476 string mode
, int rule_type
,
1480 if (rule_exists(name
)) {
1482 *err
<< "rule " << name
<< " exists";
1486 if (rule_exists(rno
)) {
1488 *err
<< "rule with ruleno " << rno
<< " exists";
1491 if (ruleset_exists(rno
)) {
1493 *err
<< "ruleset " << rno
<< " exists";
1497 for (rno
= 0; rno
< get_max_rules(); rno
++) {
1498 if (!rule_exists(rno
) && !ruleset_exists(rno
))
1502 if (!name_exists(root_name
)) {
1504 *err
<< "root item " << root_name
<< " does not exist";
1507 int root
= get_item_id(root_name
);
1509 if (failure_domain_name
.length()) {
1510 type
= get_type_id(failure_domain_name
);
1513 *err
<< "unknown type " << failure_domain_name
;
1517 if (device_class
.size()) {
1518 if (!class_exists(device_class
)) {
1520 *err
<< "device class " << device_class
<< " does not exist";
1523 int c
= get_class_id(device_class
);
1524 if (class_bucket
.count(root
) == 0 ||
1525 class_bucket
[root
].count(c
) == 0) {
1527 *err
<< "root " << root_name
<< " has no devices with class "
1531 root
= class_bucket
[root
][c
];
1533 if (mode
!= "firstn" && mode
!= "indep") {
1535 *err
<< "unknown mode " << mode
;
1540 if (mode
== "indep")
1542 int min_rep
= mode
== "firstn" ? 1 : 3;
1543 int max_rep
= mode
== "firstn" ? 10 : 20;
1544 //set the ruleset the same as rule_id(rno)
1545 crush_rule
*rule
= crush_make_rule(steps
, rno
, rule_type
, min_rep
, max_rep
);
1548 if (mode
== "indep") {
1549 crush_rule_set_step(rule
, step
++, CRUSH_RULE_SET_CHOOSELEAF_TRIES
, 5, 0);
1550 crush_rule_set_step(rule
, step
++, CRUSH_RULE_SET_CHOOSE_TRIES
, 100, 0);
1552 crush_rule_set_step(rule
, step
++, CRUSH_RULE_TAKE
, root
, 0);
1554 crush_rule_set_step(rule
, step
++,
1555 mode
== "firstn" ? CRUSH_RULE_CHOOSELEAF_FIRSTN
:
1556 CRUSH_RULE_CHOOSELEAF_INDEP
,
1560 crush_rule_set_step(rule
, step
++,
1561 mode
== "firstn" ? CRUSH_RULE_CHOOSE_FIRSTN
:
1562 CRUSH_RULE_CHOOSE_INDEP
,
1565 crush_rule_set_step(rule
, step
++, CRUSH_RULE_EMIT
, 0, 0);
1567 int ret
= crush_add_rule(crush
, rule
, rno
);
1569 *err
<< "failed to add rule " << rno
<< " because " << cpp_strerror(ret
);
1572 set_rule_name(rno
, name
);
1577 int CrushWrapper::add_simple_rule(
1578 string name
, string root_name
,
1579 string failure_domain_name
,
1580 string device_class
,
1581 string mode
, int rule_type
,
1584 return add_simple_rule_at(name
, root_name
, failure_domain_name
, device_class
,
1586 rule_type
, -1, err
);
1589 int CrushWrapper::get_rule_weight_osd_map(unsigned ruleno
, map
<int,float> *pmap
)
1591 if (ruleno
>= crush
->max_rules
)
1593 if (crush
->rules
[ruleno
] == NULL
)
1595 crush_rule
*rule
= crush
->rules
[ruleno
];
1597 // build a weight map for each TAKE in the rule, and then merge them
1599 // FIXME: if there are multiple takes that place a different number of
1600 // objects we do not take that into account. (Also, note that doing this
1601 // right is also a function of the pool, since the crush rule
1602 // might choose 2 + choose 2 but pool size may only be 3.)
1603 for (unsigned i
=0; i
<rule
->len
; ++i
) {
1606 if (rule
->steps
[i
].op
== CRUSH_RULE_TAKE
) {
1607 int n
= rule
->steps
[i
].arg1
;
1614 //breadth first iterate the OSD tree
1615 while (!q
.empty()) {
1616 int bno
= q
.front();
1618 crush_bucket
*b
= crush
->buckets
[-1-bno
];
1620 for (unsigned j
=0; j
<b
->size
; ++j
) {
1621 int item_id
= b
->items
[j
];
1622 if (item_id
>= 0) { //it's an OSD
1623 float w
= crush_get_bucket_item_weight(b
, j
);
1626 } else { //not an OSD, expand the child later
1627 q
.push_back(item_id
);
1633 for (map
<int,float>::iterator p
= m
.begin(); p
!= m
.end(); ++p
) {
1634 map
<int,float>::iterator q
= pmap
->find(p
->first
);
1635 if (q
== pmap
->end()) {
1636 (*pmap
)[p
->first
] = p
->second
/ sum
;
1638 q
->second
+= p
->second
/ sum
;
1646 int CrushWrapper::remove_rule(int ruleno
)
1648 if (ruleno
>= (int)crush
->max_rules
)
1650 if (crush
->rules
[ruleno
] == NULL
)
1652 crush_destroy_rule(crush
->rules
[ruleno
]);
1653 crush
->rules
[ruleno
] = NULL
;
1654 rule_name_map
.erase(ruleno
);
1659 int CrushWrapper::bucket_adjust_item_weight(CephContext
*cct
, crush_bucket
*bucket
, int item
, int weight
)
1661 if (cct
->_conf
->osd_crush_update_weight_set
) {
1663 for (position
= 0; position
< bucket
->size
; position
++)
1664 if (bucket
->items
[position
] == item
)
1666 assert(position
!= bucket
->size
);
1667 for (auto w
: choose_args
) {
1668 crush_choose_arg_map arg_map
= w
.second
;
1669 crush_choose_arg
*arg
= &arg_map
.args
[-1-bucket
->id
];
1670 for (__u32 j
= 0; j
< arg
->weight_set_size
; j
++) {
1671 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
1672 weight_set
->weights
[position
] = weight
;
1676 return crush_bucket_adjust_item_weight(crush
, bucket
, item
, weight
);
1679 int CrushWrapper::add_bucket(
1680 int bucketno
, int alg
, int hash
, int type
, int size
,
1681 int *items
, int *weights
, int *idout
)
1684 alg
= get_default_bucket_alg();
1688 crush_bucket
*b
= crush_make_bucket(crush
, alg
, hash
, type
, size
, items
,
1691 int r
= crush_add_bucket(crush
, bucketno
, b
, idout
);
1692 for (auto& p
: choose_args
) {
1693 crush_choose_arg_map
& cmap
= p
.second
;
1695 if ((int)cmap
.size
<= *idout
) {
1696 cmap
.args
= (crush_choose_arg
*)realloc(
1698 sizeof(crush_choose_arg
) * (*idout
+ 1));
1699 memset(&cmap
.args
[cmap
.size
], 0,
1700 sizeof(crush_choose_arg
) * (*idout
+ 1 - cmap
.size
));
1701 cmap
.size
= *idout
+ 1;
1704 cmap
.args
= (crush_choose_arg
*)calloc(sizeof(crush_choose_arg
),
1706 cmap
.size
= *idout
+ 1;
1709 int positions
= get_choose_args_positions(cmap
);
1710 crush_choose_arg
& carg
= cmap
.args
[*idout
];
1711 carg
.weight_set
= (crush_weight_set
*)calloc(sizeof(crush_weight_set
),
1713 carg
.weight_set_size
= positions
;
1714 for (int ppos
= 0; ppos
< positions
; ++ppos
) {
1715 carg
.weight_set
[ppos
].weights
= (__u32
*)calloc(sizeof(__u32
), size
);
1716 carg
.weight_set
[ppos
].size
= size
;
1717 for (int bpos
= 0; bpos
< size
; ++bpos
) {
1718 carg
.weight_set
[ppos
].weights
[bpos
] = weights
[bpos
];
1726 int CrushWrapper::bucket_add_item(crush_bucket
*bucket
, int item
, int weight
)
1728 __u32 new_size
= bucket
->size
+ 1;
1729 for (auto w
: choose_args
) {
1730 crush_choose_arg_map arg_map
= w
.second
;
1731 crush_choose_arg
*arg
= &arg_map
.args
[-1-bucket
->id
];
1732 for (__u32 j
= 0; j
< arg
->weight_set_size
; j
++) {
1733 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
1734 weight_set
->weights
= (__u32
*)realloc(weight_set
->weights
,
1735 new_size
* sizeof(__u32
));
1736 assert(weight_set
->size
+ 1 == new_size
);
1737 weight_set
->weights
[weight_set
->size
] = weight
;
1738 weight_set
->size
= new_size
;
1740 if (arg
->ids_size
) {
1741 arg
->ids
= (__s32
*)realloc(arg
->ids
, new_size
* sizeof(__s32
));
1742 assert(arg
->ids_size
+ 1 == new_size
);
1743 arg
->ids
[arg
->ids_size
] = item
;
1744 arg
->ids_size
= new_size
;
1747 return crush_bucket_add_item(crush
, bucket
, item
, weight
);
1750 int CrushWrapper::bucket_remove_item(crush_bucket
*bucket
, int item
)
1752 __u32 new_size
= bucket
->size
- 1;
1754 for (position
= 0; position
< bucket
->size
; position
++)
1755 if (bucket
->items
[position
] == item
)
1757 assert(position
!= bucket
->size
);
1758 for (auto w
: choose_args
) {
1759 crush_choose_arg_map arg_map
= w
.second
;
1760 crush_choose_arg
*arg
= &arg_map
.args
[-1-bucket
->id
];
1761 for (__u32 j
= 0; j
< arg
->weight_set_size
; j
++) {
1762 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
1763 assert(weight_set
->size
- 1 == new_size
);
1764 for (__u32 k
= position
; k
< new_size
; k
++)
1765 weight_set
->weights
[k
] = weight_set
->weights
[k
+1];
1766 weight_set
->weights
= (__u32
*)realloc(weight_set
->weights
,
1767 new_size
* sizeof(__u32
));
1768 weight_set
->size
= new_size
;
1770 if (arg
->ids_size
) {
1771 assert(arg
->ids_size
- 1 == new_size
);
1772 for (__u32 k
= position
; k
< new_size
; k
++)
1773 arg
->ids
[k
] = arg
->ids
[k
+1];
1774 arg
->ids
= (__s32
*)realloc(arg
->ids
, new_size
* sizeof(__s32
));
1775 arg
->ids_size
= new_size
;
1778 return crush_bucket_remove_item(crush
, bucket
, item
);
1781 int CrushWrapper::update_device_class(int id
,
1782 const string
& class_name
,
1786 assert(item_exists(id
));
1787 auto old_class_name
= get_item_class(id
);
1788 if (old_class_name
&& old_class_name
!= class_name
) {
1789 *ss
<< "osd." << id
<< " has already bound to class '" << old_class_name
1790 << "', can not reset class to '" << class_name
<< "'; "
1791 << "use 'ceph osd crush rm-device-class <osd>' to "
1792 << "remove old class first";
1796 int class_id
= get_or_create_class_id(class_name
);
1798 *ss
<< name
<< " id " << id
<< " is negative";
1802 if (class_map
.count(id
) != 0 && class_map
[id
] == class_id
) {
1803 *ss
<< name
<< " already set to class " << class_name
;
1807 set_item_class(id
, class_id
);
1809 int r
= rebuild_roots_with_classes();
1815 int CrushWrapper::remove_device_class(CephContext
*cct
, int id
, ostream
*ss
)
1818 const char *name
= get_item_name(id
);
1820 *ss
<< "osd." << id
<< " does not have a name";
1824 const char *class_name
= get_item_class(id
);
1826 *ss
<< "osd." << id
<< " has not been bound to a specific class yet";
1829 class_remove_item(id
);
1831 // note that there is no need to remove ourselves from shadow parent
1832 // and reweight because we are going to destroy all shadow trees
1833 // rebuild them all (if necessary) later.
1835 // see if there is any osds that still reference this class
1837 get_devices_by_class(class_name
, &devices
);
1838 if (devices
.empty()) {
1839 // class has no more devices
1840 remove_class_name(class_name
);
1843 int r
= rebuild_roots_with_classes();
1845 *ss
<< "unable to rebuild roots with class '" << class_name
<< "' "
1846 << "of osd." << id
<< ": " << cpp_strerror(r
);
1852 int CrushWrapper::device_class_clone(int original_id
, int device_class
, int *clone
)
1854 const char *item_name
= get_item_name(original_id
);
1855 if (item_name
== NULL
)
1857 const char *class_name
= get_class_name(device_class
);
1858 if (class_name
== NULL
)
1860 string copy_name
= item_name
+ string("~") + class_name
;
1861 if (name_exists(copy_name
)) {
1862 *clone
= get_item_id(copy_name
);
1865 crush_bucket
*original
= get_bucket(original_id
);
1866 if (IS_ERR(original
))
1868 crush_bucket
*copy
= crush_make_bucket(crush
,
1875 for (unsigned i
= 0; i
< original
->size
; i
++) {
1876 int item
= original
->items
[i
];
1877 int weight
= crush_get_bucket_item_weight(original
, i
);
1879 if (class_map
.count(item
) != 0 && class_map
[item
] == device_class
) {
1880 int res
= bucket_add_item(copy
, item
, weight
);
1886 int res
= device_class_clone(item
, device_class
, &child_copy_id
);
1889 crush_bucket
*child_copy
= get_bucket(child_copy_id
);
1890 if (IS_ERR(child_copy
))
1892 res
= bucket_add_item(copy
, child_copy_id
, child_copy
->weight
);
1897 int res
= crush_add_bucket(crush
, 0, copy
, clone
);
1900 res
= set_item_class(*clone
, device_class
);
1903 // we do not use set_item_name because the name is intentionally invalid
1904 name_map
[*clone
] = copy_name
;
1906 name_rmap
[copy_name
] = *clone
;
1907 class_bucket
[original_id
][device_class
] = *clone
;
1911 int CrushWrapper::rebuild_roots_with_classes()
1913 int r
= trim_roots_with_class(false);
1916 r
= populate_classes();
1919 return trim_roots_with_class(true);
1922 void CrushWrapper::encode(bufferlist
& bl
, uint64_t features
) const
1926 __u32 magic
= CRUSH_MAGIC
;
1927 ::encode(magic
, bl
);
1929 ::encode(crush
->max_buckets
, bl
);
1930 ::encode(crush
->max_rules
, bl
);
1931 ::encode(crush
->max_devices
, bl
);
1933 bool encode_compat_choose_args
= false;
1934 crush_choose_arg_map arg_map
;
1935 memset(&arg_map
, '\0', sizeof(arg_map
));
1936 if (has_choose_args() &&
1937 !HAVE_FEATURE(features
, CRUSH_CHOOSE_ARGS
)) {
1938 assert(!has_incompat_choose_args());
1939 encode_compat_choose_args
= true;
1940 arg_map
= choose_args
.begin()->second
;
1944 for (int i
=0; i
<crush
->max_buckets
; i
++) {
1946 if (crush
->buckets
[i
]) alg
= crush
->buckets
[i
]->alg
;
1951 ::encode(crush
->buckets
[i
]->id
, bl
);
1952 ::encode(crush
->buckets
[i
]->type
, bl
);
1953 ::encode(crush
->buckets
[i
]->alg
, bl
);
1954 ::encode(crush
->buckets
[i
]->hash
, bl
);
1955 ::encode(crush
->buckets
[i
]->weight
, bl
);
1956 ::encode(crush
->buckets
[i
]->size
, bl
);
1957 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++)
1958 ::encode(crush
->buckets
[i
]->items
[j
], bl
);
1960 switch (crush
->buckets
[i
]->alg
) {
1961 case CRUSH_BUCKET_UNIFORM
:
1962 ::encode((reinterpret_cast<crush_bucket_uniform
*>(crush
->buckets
[i
]))->item_weight
, bl
);
1965 case CRUSH_BUCKET_LIST
:
1966 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++) {
1967 ::encode((reinterpret_cast<crush_bucket_list
*>(crush
->buckets
[i
]))->item_weights
[j
], bl
);
1968 ::encode((reinterpret_cast<crush_bucket_list
*>(crush
->buckets
[i
]))->sum_weights
[j
], bl
);
1972 case CRUSH_BUCKET_TREE
:
1973 ::encode((reinterpret_cast<crush_bucket_tree
*>(crush
->buckets
[i
]))->num_nodes
, bl
);
1974 for (unsigned j
=0; j
<(reinterpret_cast<crush_bucket_tree
*>(crush
->buckets
[i
]))->num_nodes
; j
++)
1975 ::encode((reinterpret_cast<crush_bucket_tree
*>(crush
->buckets
[i
]))->node_weights
[j
], bl
);
1978 case CRUSH_BUCKET_STRAW
:
1979 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++) {
1980 ::encode((reinterpret_cast<crush_bucket_straw
*>(crush
->buckets
[i
]))->item_weights
[j
], bl
);
1981 ::encode((reinterpret_cast<crush_bucket_straw
*>(crush
->buckets
[i
]))->straws
[j
], bl
);
1985 case CRUSH_BUCKET_STRAW2
:
1988 if (encode_compat_choose_args
&&
1989 arg_map
.args
[i
].weight_set_size
> 0) {
1990 weights
= arg_map
.args
[i
].weight_set
[0].weights
;
1992 weights
= (reinterpret_cast<crush_bucket_straw2
*>(crush
->buckets
[i
]))->item_weights
;
1994 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++) {
1995 ::encode(weights
[j
], bl
);
2007 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
2008 __u32 yes
= crush
->rules
[i
] ? 1:0;
2013 ::encode(crush
->rules
[i
]->len
, bl
);
2014 ::encode(crush
->rules
[i
]->mask
, bl
);
2015 for (unsigned j
=0; j
<crush
->rules
[i
]->len
; j
++)
2016 ::encode(crush
->rules
[i
]->steps
[j
], bl
);
2020 ::encode(type_map
, bl
);
2021 ::encode(name_map
, bl
);
2022 ::encode(rule_name_map
, bl
);
2025 ::encode(crush
->choose_local_tries
, bl
);
2026 ::encode(crush
->choose_local_fallback_tries
, bl
);
2027 ::encode(crush
->choose_total_tries
, bl
);
2028 ::encode(crush
->chooseleaf_descend_once
, bl
);
2029 ::encode(crush
->chooseleaf_vary_r
, bl
);
2030 ::encode(crush
->straw_calc_version
, bl
);
2031 ::encode(crush
->allowed_bucket_algs
, bl
);
2032 if (features
& CEPH_FEATURE_CRUSH_TUNABLES5
) {
2033 ::encode(crush
->chooseleaf_stable
, bl
);
2036 if (HAVE_FEATURE(features
, SERVER_LUMINOUS
)) {
2038 ::encode(class_map
, bl
);
2039 ::encode(class_name
, bl
);
2040 ::encode(class_bucket
, bl
);
2043 __u32 size
= (__u32
)choose_args
.size();
2045 for (auto c
: choose_args
) {
2046 ::encode(c
.first
, bl
);
2047 crush_choose_arg_map arg_map
= c
.second
;
2049 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
2050 crush_choose_arg
*arg
= &arg_map
.args
[i
];
2051 if (arg
->weight_set_size
== 0 &&
2057 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
2058 crush_choose_arg
*arg
= &arg_map
.args
[i
];
2059 if (arg
->weight_set_size
== 0 &&
2063 ::encode(arg
->weight_set_size
, bl
);
2064 for (__u32 j
= 0; j
< arg
->weight_set_size
; j
++) {
2065 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
2066 ::encode(weight_set
->size
, bl
);
2067 for (__u32 k
= 0; k
< weight_set
->size
; k
++)
2068 ::encode(weight_set
->weights
[k
], bl
);
2070 ::encode(arg
->ids_size
, bl
);
2071 for (__u32 j
= 0; j
< arg
->ids_size
; j
++)
2072 ::encode(arg
->ids
[j
], bl
);
2078 static void decode_32_or_64_string_map(map
<int32_t,string
>& m
, bufferlist::iterator
& blp
)
2088 ::decode(strlen
, blp
);
2090 // der, key was actually 64-bits!
2091 ::decode(strlen
, blp
);
2093 ::decode_nohead(strlen
, m
[key
], blp
);
2097 void CrushWrapper::decode(bufferlist::iterator
& blp
)
2102 ::decode(magic
, blp
);
2103 if (magic
!= CRUSH_MAGIC
)
2104 throw buffer::malformed_input("bad magic number");
2106 ::decode(crush
->max_buckets
, blp
);
2107 ::decode(crush
->max_rules
, blp
);
2108 ::decode(crush
->max_devices
, blp
);
2110 // legacy tunables, unless we decode something newer
2111 set_tunables_legacy();
2115 crush
->buckets
= (crush_bucket
**)calloc(1, crush
->max_buckets
* sizeof(crush_bucket
*));
2116 for (int i
=0; i
<crush
->max_buckets
; i
++) {
2117 decode_crush_bucket(&crush
->buckets
[i
], blp
);
2121 crush
->rules
= (crush_rule
**)calloc(1, crush
->max_rules
* sizeof(crush_rule
*));
2122 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
2126 crush
->rules
[i
] = NULL
;
2132 crush
->rules
[i
] = reinterpret_cast<crush_rule
*>(calloc(1, crush_rule_size(len
)));
2133 crush
->rules
[i
]->len
= len
;
2134 ::decode(crush
->rules
[i
]->mask
, blp
);
2135 for (unsigned j
=0; j
<crush
->rules
[i
]->len
; j
++)
2136 ::decode(crush
->rules
[i
]->steps
[j
], blp
);
2140 // NOTE: we had a bug where we were incoding int instead of int32, which means the
2141 // 'key' field for these maps may be either 32 or 64 bits, depending. tolerate
2142 // both by assuming the string is always non-empty.
2143 decode_32_or_64_string_map(type_map
, blp
);
2144 decode_32_or_64_string_map(name_map
, blp
);
2145 decode_32_or_64_string_map(rule_name_map
, blp
);
2149 ::decode(crush
->choose_local_tries
, blp
);
2150 ::decode(crush
->choose_local_fallback_tries
, blp
);
2151 ::decode(crush
->choose_total_tries
, blp
);
2154 ::decode(crush
->chooseleaf_descend_once
, blp
);
2157 ::decode(crush
->chooseleaf_vary_r
, blp
);
2160 ::decode(crush
->straw_calc_version
, blp
);
2163 ::decode(crush
->allowed_bucket_algs
, blp
);
2166 ::decode(crush
->chooseleaf_stable
, blp
);
2169 ::decode(class_map
, blp
);
2170 ::decode(class_name
, blp
);
2171 for (auto &c
: class_name
)
2172 class_rname
[c
.second
] = c
.first
;
2173 ::decode(class_bucket
, blp
);
2177 __u32 choose_args_size
;
2178 ::decode(choose_args_size
, blp
);
2179 for (__u32 i
= 0; i
< choose_args_size
; i
++) {
2180 uint64_t choose_args_index
;
2181 ::decode(choose_args_index
, blp
);
2182 crush_choose_arg_map arg_map
;
2183 arg_map
.size
= crush
->max_buckets
;
2184 arg_map
.args
= (crush_choose_arg
*)calloc(
2185 arg_map
.size
, sizeof(crush_choose_arg
));
2187 ::decode(size
, blp
);
2188 for (__u32 j
= 0; j
< size
; j
++) {
2190 ::decode(bucket_index
, blp
);
2191 assert(bucket_index
< arg_map
.size
);
2192 crush_choose_arg
*arg
= &arg_map
.args
[bucket_index
];
2193 ::decode(arg
->weight_set_size
, blp
);
2194 if (arg
->weight_set_size
) {
2195 arg
->weight_set
= (crush_weight_set
*)calloc(
2196 arg
->weight_set_size
, sizeof(crush_weight_set
));
2197 for (__u32 k
= 0; k
< arg
->weight_set_size
; k
++) {
2198 crush_weight_set
*weight_set
= &arg
->weight_set
[k
];
2199 ::decode(weight_set
->size
, blp
);
2200 weight_set
->weights
= (__u32
*)calloc(
2201 weight_set
->size
, sizeof(__u32
));
2202 for (__u32 l
= 0; l
< weight_set
->size
; l
++)
2203 ::decode(weight_set
->weights
[l
], blp
);
2206 ::decode(arg
->ids_size
, blp
);
2207 if (arg
->ids_size
) {
2208 assert(arg
->ids_size
== crush
->buckets
[bucket_index
]->size
);
2209 arg
->ids
= (__s32
*)calloc(arg
->ids_size
, sizeof(__s32
));
2210 for (__u32 k
= 0; k
< arg
->ids_size
; k
++)
2211 ::decode(arg
->ids
[k
], blp
);
2214 choose_args
[choose_args_index
] = arg_map
;
2220 crush_destroy(crush
);
2225 void CrushWrapper::decode_crush_bucket(crush_bucket
** bptr
, bufferlist::iterator
&blp
)
2236 case CRUSH_BUCKET_UNIFORM
:
2237 size
= sizeof(crush_bucket_uniform
);
2239 case CRUSH_BUCKET_LIST
:
2240 size
= sizeof(crush_bucket_list
);
2242 case CRUSH_BUCKET_TREE
:
2243 size
= sizeof(crush_bucket_tree
);
2245 case CRUSH_BUCKET_STRAW
:
2246 size
= sizeof(crush_bucket_straw
);
2248 case CRUSH_BUCKET_STRAW2
:
2249 size
= sizeof(crush_bucket_straw2
);
2254 snprintf(str
, sizeof(str
), "unsupported bucket algorithm: %d", alg
);
2255 throw buffer::malformed_input(str
);
2258 crush_bucket
*bucket
= reinterpret_cast<crush_bucket
*>(calloc(1, size
));
2261 ::decode(bucket
->id
, blp
);
2262 ::decode(bucket
->type
, blp
);
2263 ::decode(bucket
->alg
, blp
);
2264 ::decode(bucket
->hash
, blp
);
2265 ::decode(bucket
->weight
, blp
);
2266 ::decode(bucket
->size
, blp
);
2268 bucket
->items
= (__s32
*)calloc(1, bucket
->size
* sizeof(__s32
));
2269 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
2270 ::decode(bucket
->items
[j
], blp
);
2273 switch (bucket
->alg
) {
2274 case CRUSH_BUCKET_UNIFORM
:
2275 ::decode((reinterpret_cast<crush_bucket_uniform
*>(bucket
))->item_weight
, blp
);
2278 case CRUSH_BUCKET_LIST
: {
2279 crush_bucket_list
* cbl
= reinterpret_cast<crush_bucket_list
*>(bucket
);
2280 cbl
->item_weights
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
2281 cbl
->sum_weights
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
2283 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
2284 ::decode(cbl
->item_weights
[j
], blp
);
2285 ::decode(cbl
->sum_weights
[j
], blp
);
2290 case CRUSH_BUCKET_TREE
: {
2291 crush_bucket_tree
* cbt
= reinterpret_cast<crush_bucket_tree
*>(bucket
);
2292 ::decode(cbt
->num_nodes
, blp
);
2293 cbt
->node_weights
= (__u32
*)calloc(1, cbt
->num_nodes
* sizeof(__u32
));
2294 for (unsigned j
=0; j
<cbt
->num_nodes
; j
++) {
2295 ::decode(cbt
->node_weights
[j
], blp
);
2300 case CRUSH_BUCKET_STRAW
: {
2301 crush_bucket_straw
* cbs
= reinterpret_cast<crush_bucket_straw
*>(bucket
);
2302 cbs
->straws
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
2303 cbs
->item_weights
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
2304 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
2305 ::decode(cbs
->item_weights
[j
], blp
);
2306 ::decode(cbs
->straws
[j
], blp
);
2311 case CRUSH_BUCKET_STRAW2
: {
2312 crush_bucket_straw2
* cbs
= reinterpret_cast<crush_bucket_straw2
*>(bucket
);
2313 cbs
->item_weights
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
2314 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
2315 ::decode(cbs
->item_weights
[j
], blp
);
2321 // We should have handled this case in the first switch statement
2328 void CrushWrapper::dump(Formatter
*f
) const
2330 f
->open_array_section("devices");
2331 for (int i
=0; i
<get_max_devices(); i
++) {
2332 f
->open_object_section("device");
2333 f
->dump_int("id", i
);
2334 const char *n
= get_item_name(i
);
2336 f
->dump_string("name", n
);
2339 sprintf(name
, "device%d", i
);
2340 f
->dump_string("name", name
);
2342 const char *device_class
= get_item_class(i
);
2343 if (device_class
!= NULL
)
2344 f
->dump_string("class", device_class
);
2349 f
->open_array_section("types");
2350 int n
= get_num_type_names();
2351 for (int i
=0; n
; i
++) {
2352 const char *name
= get_type_name(i
);
2355 f
->open_object_section("type");
2356 f
->dump_int("type_id", 0);
2357 f
->dump_string("name", "device");
2363 f
->open_object_section("type");
2364 f
->dump_int("type_id", i
);
2365 f
->dump_string("name", name
);
2370 f
->open_array_section("buckets");
2371 for (int bucket
= -1; bucket
> -1-get_max_buckets(); --bucket
) {
2372 if (!bucket_exists(bucket
))
2374 f
->open_object_section("bucket");
2375 f
->dump_int("id", bucket
);
2376 if (get_item_name(bucket
))
2377 f
->dump_string("name", get_item_name(bucket
));
2378 f
->dump_int("type_id", get_bucket_type(bucket
));
2379 if (get_type_name(get_bucket_type(bucket
)))
2380 f
->dump_string("type_name", get_type_name(get_bucket_type(bucket
)));
2381 f
->dump_int("weight", get_bucket_weight(bucket
));
2382 f
->dump_string("alg", crush_bucket_alg_name(get_bucket_alg(bucket
)));
2383 f
->dump_string("hash", crush_hash_name(get_bucket_hash(bucket
)));
2384 f
->open_array_section("items");
2385 for (int j
=0; j
<get_bucket_size(bucket
); j
++) {
2386 f
->open_object_section("item");
2387 f
->dump_int("id", get_bucket_item(bucket
, j
));
2388 f
->dump_int("weight", get_bucket_item_weight(bucket
, j
));
2389 f
->dump_int("pos", j
);
2397 f
->open_array_section("rules");
2401 f
->open_object_section("tunables");
2405 dump_choose_args(f
);
2409 // depth first walker
2411 typedef CrushTreeDumper::Item Item
;
2412 const CrushWrapper
*crush
;
2413 const CrushTreeDumper::name_map_t
& weight_set_names
;
2415 explicit TreeDumper(const CrushWrapper
*crush
,
2416 const CrushTreeDumper::name_map_t
& wsnames
)
2417 : crush(crush
), weight_set_names(wsnames
) {}
2419 void dump(Formatter
*f
) {
2421 crush
->find_roots(roots
);
2422 for (set
<int>::iterator root
= roots
.begin(); root
!= roots
.end(); ++root
) {
2423 dump_item(Item(*root
, 0, 0, crush
->get_bucket_weightf(*root
)), f
);
2428 void dump_item(const Item
& qi
, Formatter
* f
) {
2429 if (qi
.is_bucket()) {
2430 f
->open_object_section("bucket");
2431 CrushTreeDumper::dump_item_fields(crush
, weight_set_names
, qi
, f
);
2432 dump_bucket_children(qi
, f
);
2435 f
->open_object_section("device");
2436 CrushTreeDumper::dump_item_fields(crush
, weight_set_names
, qi
, f
);
2441 void dump_bucket_children(const Item
& parent
, Formatter
* f
) {
2442 f
->open_array_section("items");
2443 const int max_pos
= crush
->get_bucket_size(parent
.id
);
2444 for (int pos
= 0; pos
< max_pos
; pos
++) {
2445 int id
= crush
->get_bucket_item(parent
.id
, pos
);
2446 float weight
= crush
->get_bucket_item_weightf(parent
.id
, pos
);
2447 dump_item(Item(id
, parent
.id
, parent
.depth
+ 1, weight
), f
);
2454 void CrushWrapper::dump_tree(
2456 const CrushTreeDumper::name_map_t
& weight_set_names
) const
2459 TreeDumper(this, weight_set_names
).dump(f
);
2462 void CrushWrapper::dump_tunables(Formatter
*f
) const
2464 f
->dump_int("choose_local_tries", get_choose_local_tries());
2465 f
->dump_int("choose_local_fallback_tries", get_choose_local_fallback_tries());
2466 f
->dump_int("choose_total_tries", get_choose_total_tries());
2467 f
->dump_int("chooseleaf_descend_once", get_chooseleaf_descend_once());
2468 f
->dump_int("chooseleaf_vary_r", get_chooseleaf_vary_r());
2469 f
->dump_int("chooseleaf_stable", get_chooseleaf_stable());
2470 f
->dump_int("straw_calc_version", get_straw_calc_version());
2471 f
->dump_int("allowed_bucket_algs", get_allowed_bucket_algs());
2473 // be helpful about it
2474 if (has_jewel_tunables())
2475 f
->dump_string("profile", "jewel");
2476 else if (has_hammer_tunables())
2477 f
->dump_string("profile", "hammer");
2478 else if (has_firefly_tunables())
2479 f
->dump_string("profile", "firefly");
2480 else if (has_bobtail_tunables())
2481 f
->dump_string("profile", "bobtail");
2482 else if (has_argonaut_tunables())
2483 f
->dump_string("profile", "argonaut");
2485 f
->dump_string("profile", "unknown");
2486 f
->dump_int("optimal_tunables", (int)has_optimal_tunables());
2487 f
->dump_int("legacy_tunables", (int)has_legacy_tunables());
2489 // be helpful about minimum version required
2490 f
->dump_string("minimum_required_version", get_min_required_version());
2492 f
->dump_int("require_feature_tunables", (int)has_nondefault_tunables());
2493 f
->dump_int("require_feature_tunables2", (int)has_nondefault_tunables2());
2494 f
->dump_int("has_v2_rules", (int)has_v2_rules());
2495 f
->dump_int("require_feature_tunables3", (int)has_nondefault_tunables3());
2496 f
->dump_int("has_v3_rules", (int)has_v3_rules());
2497 f
->dump_int("has_v4_buckets", (int)has_v4_buckets());
2498 f
->dump_int("require_feature_tunables5", (int)has_nondefault_tunables5());
2499 f
->dump_int("has_v5_rules", (int)has_v5_rules());
2502 void CrushWrapper::dump_choose_args(Formatter
*f
) const
2504 f
->open_object_section("choose_args");
2505 for (auto c
: choose_args
) {
2506 crush_choose_arg_map arg_map
= c
.second
;
2507 f
->open_array_section(stringify(c
.first
).c_str());
2508 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
2509 crush_choose_arg
*arg
= &arg_map
.args
[i
];
2510 if (arg
->weight_set_size
== 0 &&
2513 f
->open_object_section("choose_args");
2514 int bucket_index
= i
;
2515 f
->dump_int("bucket_id", -1-bucket_index
);
2516 if (arg
->weight_set_size
> 0) {
2517 f
->open_array_section("weight_set");
2518 for (__u32 j
= 0; j
< arg
->weight_set_size
; j
++) {
2519 f
->open_array_section("weights");
2520 __u32
*weights
= arg
->weight_set
[j
].weights
;
2521 __u32 size
= arg
->weight_set
[j
].size
;
2522 for (__u32 k
= 0; k
< size
; k
++) {
2523 f
->dump_float("weight", (float)weights
[k
]/(float)0x10000);
2529 if (arg
->ids_size
> 0) {
2530 f
->open_array_section("ids");
2531 for (__u32 j
= 0; j
< arg
->ids_size
; j
++)
2532 f
->dump_int("id", arg
->ids
[j
]);
2542 void CrushWrapper::dump_rules(Formatter
*f
) const
2544 for (int i
=0; i
<get_max_rules(); i
++) {
2545 if (!rule_exists(i
))
2551 void CrushWrapper::dump_rule(int ruleset
, Formatter
*f
) const
2553 f
->open_object_section("rule");
2554 f
->dump_int("rule_id", ruleset
);
2555 if (get_rule_name(ruleset
))
2556 f
->dump_string("rule_name", get_rule_name(ruleset
));
2557 f
->dump_int("ruleset", get_rule_mask_ruleset(ruleset
));
2558 f
->dump_int("type", get_rule_mask_type(ruleset
));
2559 f
->dump_int("min_size", get_rule_mask_min_size(ruleset
));
2560 f
->dump_int("max_size", get_rule_mask_max_size(ruleset
));
2561 f
->open_array_section("steps");
2562 for (int j
=0; j
<get_rule_len(ruleset
); j
++) {
2563 f
->open_object_section("step");
2564 switch (get_rule_op(ruleset
, j
)) {
2565 case CRUSH_RULE_NOOP
:
2566 f
->dump_string("op", "noop");
2568 case CRUSH_RULE_TAKE
:
2569 f
->dump_string("op", "take");
2571 int item
= get_rule_arg1(ruleset
, j
);
2572 f
->dump_int("item", item
);
2574 const char *name
= get_item_name(item
);
2575 f
->dump_string("item_name", name
? name
: "");
2578 case CRUSH_RULE_EMIT
:
2579 f
->dump_string("op", "emit");
2581 case CRUSH_RULE_CHOOSE_FIRSTN
:
2582 f
->dump_string("op", "choose_firstn");
2583 f
->dump_int("num", get_rule_arg1(ruleset
, j
));
2584 f
->dump_string("type", get_type_name(get_rule_arg2(ruleset
, j
)));
2586 case CRUSH_RULE_CHOOSE_INDEP
:
2587 f
->dump_string("op", "choose_indep");
2588 f
->dump_int("num", get_rule_arg1(ruleset
, j
));
2589 f
->dump_string("type", get_type_name(get_rule_arg2(ruleset
, j
)));
2591 case CRUSH_RULE_CHOOSELEAF_FIRSTN
:
2592 f
->dump_string("op", "chooseleaf_firstn");
2593 f
->dump_int("num", get_rule_arg1(ruleset
, j
));
2594 f
->dump_string("type", get_type_name(get_rule_arg2(ruleset
, j
)));
2596 case CRUSH_RULE_CHOOSELEAF_INDEP
:
2597 f
->dump_string("op", "chooseleaf_indep");
2598 f
->dump_int("num", get_rule_arg1(ruleset
, j
));
2599 f
->dump_string("type", get_type_name(get_rule_arg2(ruleset
, j
)));
2601 case CRUSH_RULE_SET_CHOOSE_TRIES
:
2602 f
->dump_string("op", "set_choose_tries");
2603 f
->dump_int("num", get_rule_arg1(ruleset
, j
));
2605 case CRUSH_RULE_SET_CHOOSELEAF_TRIES
:
2606 f
->dump_string("op", "set_chooseleaf_tries");
2607 f
->dump_int("num", get_rule_arg1(ruleset
, j
));
2610 f
->dump_int("opcode", get_rule_op(ruleset
, j
));
2611 f
->dump_int("arg1", get_rule_arg1(ruleset
, j
));
2612 f
->dump_int("arg2", get_rule_arg2(ruleset
, j
));
2620 void CrushWrapper::list_rules(Formatter
*f
) const
2622 for (int rule
= 0; rule
< get_max_rules(); rule
++) {
2623 if (!rule_exists(rule
))
2625 f
->dump_string("name", get_rule_name(rule
));
2629 void CrushWrapper::list_rules(ostream
*ss
) const
2631 for (int rule
= 0; rule
< get_max_rules(); rule
++) {
2632 if (!rule_exists(rule
))
2634 *ss
<< get_rule_name(rule
) << "\n";
2638 class CrushTreePlainDumper
: public CrushTreeDumper::Dumper
<TextTable
> {
2640 typedef CrushTreeDumper::Dumper
<TextTable
> Parent
;
2642 explicit CrushTreePlainDumper(const CrushWrapper
*crush
,
2643 const CrushTreeDumper::name_map_t
& wsnames
)
2644 : Parent(crush
, wsnames
) {}
2645 explicit CrushTreePlainDumper(const CrushWrapper
*crush
,
2646 const CrushTreeDumper::name_map_t
& wsnames
,
2648 : Parent(crush
, wsnames
, show_shadow
) {}
2651 void dump(TextTable
*tbl
) {
2652 tbl
->define_column("ID", TextTable::LEFT
, TextTable::RIGHT
);
2653 tbl
->define_column("WEIGHT", TextTable::LEFT
, TextTable::RIGHT
);
2654 for (auto& p
: crush
->choose_args
) {
2655 if (p
.first
== CrushWrapper::DEFAULT_CHOOSE_ARGS
) {
2656 tbl
->define_column("(compat)", TextTable::LEFT
, TextTable::RIGHT
);
2659 auto q
= weight_set_names
.find(p
.first
);
2660 name
= q
!= weight_set_names
.end() ? q
->second
:
2662 tbl
->define_column(name
.c_str(), TextTable::LEFT
, TextTable::RIGHT
);
2665 tbl
->define_column("TYPE NAME", TextTable::LEFT
, TextTable::LEFT
);
2670 void dump_item(const CrushTreeDumper::Item
&qi
, TextTable
*tbl
) override
{
2672 << weightf_t(qi
.weight
);
2673 for (auto& p
: crush
->choose_args
) {
2674 if (qi
.parent
< 0) {
2675 const crush_choose_arg_map cmap
= crush
->choose_args_get(p
.first
);
2676 int bidx
= -1 - qi
.parent
;
2677 const crush_bucket
*b
= crush
->get_bucket(qi
.parent
);
2679 bidx
< (int)cmap
.size
&&
2680 cmap
.args
[bidx
].weight_set
&&
2681 cmap
.args
[bidx
].weight_set_size
>= 1) {
2684 pos
< (int)cmap
.args
[bidx
].weight_set
[0].size
&&
2685 b
->items
[pos
] != qi
.id
;
2687 *tbl
<< weightf_t((float)cmap
.args
[bidx
].weight_set
[0].weights
[pos
] /
2695 for (int k
=0; k
< qi
.depth
; k
++) {
2698 if (qi
.is_bucket()) {
2699 ss
<< crush
->get_type_name(crush
->get_bucket_type(qi
.id
)) << " "
2700 << crush
->get_item_name(qi
.id
);
2702 ss
<< "osd." << qi
.id
;
2705 *tbl
<< TextTable::endrow
;
2710 class CrushTreeFormattingDumper
: public CrushTreeDumper::FormattingDumper
{
2712 typedef CrushTreeDumper::FormattingDumper Parent
;
2714 explicit CrushTreeFormattingDumper(
2715 const CrushWrapper
*crush
,
2716 const CrushTreeDumper::name_map_t
& wsnames
)
2717 : Parent(crush
, wsnames
) {}
2719 explicit CrushTreeFormattingDumper(
2720 const CrushWrapper
*crush
,
2721 const CrushTreeDumper::name_map_t
& wsnames
,
2723 : Parent(crush
, wsnames
, show_shadow
) {}
2725 void dump(Formatter
*f
) {
2726 f
->open_array_section("nodes");
2729 f
->open_array_section("stray");
2735 void CrushWrapper::dump_tree(
2738 const CrushTreeDumper::name_map_t
& weight_set_names
,
2739 bool show_shadow
) const
2743 CrushTreePlainDumper(this, weight_set_names
, show_shadow
).dump(&tbl
);
2747 CrushTreeFormattingDumper(this, weight_set_names
, show_shadow
).dump(f
);
2751 void CrushWrapper::generate_test_instances(list
<CrushWrapper
*>& o
)
2753 o
.push_back(new CrushWrapper
);
2758 * Determine the default CRUSH ruleset ID to be used with
2759 * newly created replicated pools.
2761 * @returns a ruleset ID (>=0) or -1 if no suitable ruleset found
2763 int CrushWrapper::get_osd_pool_default_crush_replicated_ruleset(CephContext
*cct
)
2765 int crush_ruleset
= cct
->_conf
->osd_pool_default_crush_rule
;
2766 if (crush_ruleset
< 0) {
2767 crush_ruleset
= find_first_ruleset(pg_pool_t::TYPE_REPLICATED
);
2768 } else if (!ruleset_exists(crush_ruleset
)) {
2769 crush_ruleset
= -1; // match find_first_ruleset() retval
2771 return crush_ruleset
;
2774 bool CrushWrapper::is_valid_crush_name(const string
& s
)
2778 for (string::const_iterator p
= s
.begin(); p
!= s
.end(); ++p
) {
2782 !(*p
>= '0' && *p
<= '9') &&
2783 !(*p
>= 'A' && *p
<= 'Z') &&
2784 !(*p
>= 'a' && *p
<= 'z'))
2790 bool CrushWrapper::is_valid_crush_loc(CephContext
*cct
,
2791 const map
<string
,string
>& loc
)
2793 for (map
<string
,string
>::const_iterator l
= loc
.begin(); l
!= loc
.end(); ++l
) {
2794 if (!is_valid_crush_name(l
->first
) ||
2795 !is_valid_crush_name(l
->second
)) {
2796 ldout(cct
, 1) << "loc["
2797 << l
->first
<< "] = '"
2798 << l
->second
<< "' not a valid crush name ([A-Za-z0-9_-.]+)"
2806 int CrushWrapper::_choose_type_stack(
2808 const vector
<pair
<int,int>>& stack
,
2809 const set
<int>& overfull
,
2810 const vector
<int>& underfull
,
2811 const vector
<int>& orig
,
2812 vector
<int>::const_iterator
& i
,
2814 vector
<int> *pw
) const
2816 vector
<int> w
= *pw
;
2819 ldout(cct
, 10) << __func__
<< " stack " << stack
2825 vector
<int> cumulative_fanout(stack
.size());
2827 for (int j
= (int)stack
.size() - 1; j
>= 0; --j
) {
2828 cumulative_fanout
[j
] = f
;
2829 f
*= stack
[j
].second
;
2831 ldout(cct
, 10) << __func__
<< " cumulative_fanout " << cumulative_fanout
2834 // identify underful targets for each intermediate level.
2835 // this serves two purposes:
2836 // 1. we can tell when we are selecting a bucket that does not have any underfull
2837 // devices beneath it. that means that if the current input includes an overfull
2838 // device, we won't be able to find an underfull device with this parent to
2840 // 2. when we decide we should reject a bucket due to the above, this list gives us
2841 // a list of peers to consider that *do* have underfull devices available.. (we
2842 // are careful to pick one that has the same parent.)
2843 vector
<set
<int>> underfull_buckets
; // level -> set of buckets with >0 underfull item(s)
2844 underfull_buckets
.resize(stack
.size() - 1);
2845 for (auto osd
: underfull
) {
2847 for (int j
= (int)stack
.size() - 2; j
>= 0; --j
) {
2848 int type
= stack
[j
].first
;
2849 item
= get_parent_of_type(item
, type
);
2850 ldout(cct
, 10) << __func__
<< " underfull " << osd
<< " type " << type
2851 << " is " << item
<< dendl
;
2852 underfull_buckets
[j
].insert(item
);
2855 ldout(cct
, 20) << __func__
<< " underfull_buckets " << underfull_buckets
<< dendl
;
2857 for (unsigned j
= 0; j
< stack
.size(); ++j
) {
2858 int type
= stack
[j
].first
;
2859 int fanout
= stack
[j
].second
;
2860 int cum_fanout
= cumulative_fanout
[j
];
2861 ldout(cct
, 10) << " level " << j
<< ": type " << type
<< " fanout " << fanout
2862 << " cumulative " << cum_fanout
2863 << " w " << w
<< dendl
;
2866 for (auto from
: w
) {
2867 ldout(cct
, 10) << " from " << from
<< dendl
;
2868 // identify leaves under each choice. we use this to check whether any of these
2869 // leaves are overfull. (if so, we need to make sure there are underfull candidates
2870 // to swap for them.)
2871 vector
<set
<int>> leaves
;
2872 leaves
.resize(fanout
);
2873 for (int pos
= 0; pos
< fanout
; ++pos
) {
2876 int item
= get_parent_of_type(*tmpi
, type
);
2879 while (n
-- && tmpi
!= orig
.end()) {
2880 leaves
[pos
].insert(*tmpi
++);
2882 ldout(cct
, 10) << __func__
<< " from " << *tmpi
<< " got " << item
2883 << " of type " << type
<< " over leaves " << leaves
[pos
] << dendl
;
2886 bool replaced
= false;
2887 if (overfull
.count(*i
)) {
2888 for (auto item
: underfull
) {
2889 ldout(cct
, 10) << __func__
<< " pos " << pos
2890 << " was " << *i
<< " considering " << item
2892 if (used
.count(item
)) {
2893 ldout(cct
, 20) << __func__
<< " in used " << used
<< dendl
;
2896 if (!subtree_contains(from
, item
)) {
2897 ldout(cct
, 20) << __func__
<< " not in subtree " << from
<< dendl
;
2900 if (std::find(orig
.begin(), orig
.end(), item
) != orig
.end()) {
2901 ldout(cct
, 20) << __func__
<< " in orig " << orig
<< dendl
;
2906 ldout(cct
, 10) << __func__
<< " pos " << pos
<< " replace "
2907 << *i
<< " -> " << item
<< dendl
;
2914 ldout(cct
, 10) << __func__
<< " pos " << pos
<< " keep " << *i
2919 if (i
== orig
.end()) {
2920 ldout(cct
, 10) << __func__
<< " end of orig, break 1" << dendl
;
2925 if (j
+ 1 < stack
.size()) {
2926 // check if any buckets have overfull leaves but no underfull candidates
2927 for (int pos
= 0; pos
< fanout
; ++pos
) {
2928 if (underfull_buckets
[j
].count(o
[pos
]) == 0) {
2929 // are any leaves overfull?
2930 bool any_overfull
= false;
2931 for (auto osd
: leaves
[pos
]) {
2932 if (overfull
.count(osd
)) {
2933 any_overfull
= true;
2937 ldout(cct
, 10) << " bucket " << o
[pos
] << " has no underfull targets and "
2938 << ">0 leaves " << leaves
[pos
] << " is overfull; alts "
2939 << underfull_buckets
[j
]
2941 for (auto alt
: underfull_buckets
[j
]) {
2942 if (std::find(o
.begin(), o
.end(), alt
) == o
.end()) {
2943 // see if alt has the same parent
2945 get_parent_of_type(o
[pos
], stack
[j
-1].first
) ==
2946 get_parent_of_type(alt
, stack
[j
-1].first
)) {
2948 ldout(cct
, 10) << " replacing " << o
[pos
]
2949 << " (which has no underfull leaves) with " << alt
2951 << get_parent_of_type(alt
, stack
[j
-1].first
) << " type "
2952 << type
<< ")" << dendl
;
2954 ldout(cct
, 10) << " replacing " << o
[pos
]
2955 << " (which has no underfull leaves) with " << alt
2956 << " (first level)" << dendl
;
2960 ldout(cct
, 30) << " alt " << alt
<< " for " << o
[pos
]
2961 << " has different parent, skipping" << dendl
;
2969 if (i
== orig
.end()) {
2970 ldout(cct
, 10) << __func__
<< " end of orig, break 2" << dendl
;
2974 ldout(cct
, 10) << __func__
<< " w <- " << o
<< " was " << w
<< dendl
;
2981 int CrushWrapper::try_remap_rule(
2985 const set
<int>& overfull
,
2986 const vector
<int>& underfull
,
2987 const vector
<int>& orig
,
2988 vector
<int> *out
) const
2990 const crush_map
*map
= crush
;
2991 const crush_rule
*rule
= get_rule(ruleno
);
2994 ldout(cct
, 10) << __func__
<< " ruleno " << ruleno
2995 << " numrep " << maxout
<< " overfull " << overfull
2996 << " underfull " << underfull
<< " orig " << orig
2998 vector
<int> w
; // working set
3001 auto i
= orig
.begin();
3004 vector
<pair
<int,int>> type_stack
; // (type, fan-out)
3006 for (unsigned step
= 0; step
< rule
->len
; ++step
) {
3007 const crush_rule_step
*curstep
= &rule
->steps
[step
];
3008 ldout(cct
, 10) << __func__
<< " step " << step
<< " w " << w
<< dendl
;
3009 switch (curstep
->op
) {
3010 case CRUSH_RULE_TAKE
:
3011 if ((curstep
->arg1
>= 0 && curstep
->arg1
< map
->max_devices
) ||
3012 (-1-curstep
->arg1
>= 0 && -1-curstep
->arg1
< map
->max_buckets
&&
3013 map
->buckets
[-1-curstep
->arg1
])) {
3015 w
.push_back(curstep
->arg1
);
3016 ldout(cct
, 10) << __func__
<< " take " << w
<< dendl
;
3018 ldout(cct
, 1) << " bad take value " << curstep
->arg1
<< dendl
;
3022 case CRUSH_RULE_CHOOSELEAF_FIRSTN
:
3023 case CRUSH_RULE_CHOOSELEAF_INDEP
:
3025 int numrep
= curstep
->arg1
;
3026 int type
= curstep
->arg2
;
3029 type_stack
.push_back(make_pair(type
, numrep
));
3030 type_stack
.push_back(make_pair(0, 1));
3031 int r
= _choose_type_stack(cct
, type_stack
, overfull
, underfull
, orig
,
3039 case CRUSH_RULE_CHOOSE_FIRSTN
:
3040 case CRUSH_RULE_CHOOSE_INDEP
:
3042 int numrep
= curstep
->arg1
;
3043 int type
= curstep
->arg2
;
3046 type_stack
.push_back(make_pair(type
, numrep
));
3050 case CRUSH_RULE_EMIT
:
3051 ldout(cct
, 10) << " emit " << w
<< dendl
;
3052 if (!type_stack
.empty()) {
3053 int r
= _choose_type_stack(cct
, type_stack
, overfull
, underfull
, orig
,
3059 for (auto item
: w
) {
3060 out
->push_back(item
);
3075 int CrushWrapper::_choose_args_adjust_item_weight_in_bucket(
3077 crush_choose_arg_map cmap
,
3080 const vector
<int>& weight
,
3084 int bidx
= -1 - bucketid
;
3085 crush_bucket
*b
= crush
->buckets
[bidx
];
3086 if (bidx
>= (int)cmap
.size
) {
3088 *ss
<< "no weight-set for bucket " << b
->id
;
3089 ldout(cct
, 10) << __func__
<< " no crush_choose_arg for bucket " << b
->id
3093 crush_choose_arg
*carg
= &cmap
.args
[bidx
];
3094 if (carg
->weight_set
== NULL
) {
3096 *ss
<< "no weight-set for bucket " << b
->id
;
3097 ldout(cct
, 10) << __func__
<< " no weight_set for bucket " << b
->id
3101 if (carg
->weight_set_size
!= weight
.size()) {
3103 *ss
<< "weight_set_size != " << weight
.size() << " for bucket " << b
->id
;
3104 ldout(cct
, 10) << __func__
<< " weight_set_size != " << weight
.size()
3105 << " for bucket " << b
->id
<< dendl
;
3108 for (unsigned i
= 0; i
< b
->size
; i
++) {
3109 if (b
->items
[i
] == id
) {
3110 for (unsigned j
= 0; j
< weight
.size(); ++j
) {
3111 carg
->weight_set
[j
].weights
[i
] = weight
[j
];
3113 ldout(cct
, 5) << __func__
<< " set " << id
<< " to " << weight
3114 << " in bucket " << b
->id
<< dendl
;
3119 vector
<int> bucket_weight(weight
.size(), 0);
3120 for (unsigned i
= 0; i
< b
->size
; i
++) {
3121 for (unsigned j
= 0; j
< weight
.size(); ++j
) {
3122 bucket_weight
[j
] += carg
->weight_set
[j
].weights
[i
];
3125 choose_args_adjust_item_weight(cct
, cmap
, b
->id
, bucket_weight
, nullptr);
3130 int CrushWrapper::choose_args_adjust_item_weight(
3132 crush_choose_arg_map cmap
,
3134 const vector
<int>& weight
,
3137 ldout(cct
, 5) << __func__
<< " " << id
<< " weight " << weight
<< dendl
;
3139 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
3140 crush_bucket
*b
= crush
->buckets
[bidx
];
3144 changed
+= _choose_args_adjust_item_weight_in_bucket(
3145 cct
, cmap
, b
->id
, id
, weight
, ss
);
3149 *ss
<< "item " << id
<< " not found in crush map";