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
);
363 rebuild_roots_with_classes();
367 int CrushWrapper::remove_root(int item
)
369 crush_bucket
*b
= get_bucket(item
);
371 // should be idempotent
372 // e.g.: we use 'crush link' to link same host into
373 // different roots, which as a result can cause different
374 // shadow trees reference same hosts too. This means
375 // we may need to destory the same buckets(hosts, racks, etc.)
376 // multiple times during rebuilding all shadow trees.
380 for (unsigned n
= 0; n
< b
->size
; n
++) {
381 if (b
->items
[n
] >= 0)
383 int r
= remove_root(b
->items
[n
]);
388 crush_remove_bucket(crush
, b
);
389 if (name_map
.count(item
) != 0) {
390 name_map
.erase(item
);
393 if (class_bucket
.count(item
) != 0)
394 class_bucket
.erase(item
);
395 class_remove_item(item
);
399 int CrushWrapper::remove_item(CephContext
*cct
, int item
, bool unlink_only
)
401 ldout(cct
, 5) << "remove_item " << item
402 << (unlink_only
? " unlink_only":"") << dendl
;
406 if (item
< 0 && !unlink_only
) {
407 crush_bucket
*t
= get_bucket(item
);
409 ldout(cct
, 1) << "remove_item bucket " << item
<< " does not exist"
415 ldout(cct
, 1) << "remove_item bucket " << item
<< " has " << t
->size
416 << " items, not empty" << dendl
;
419 if (_bucket_is_in_use(item
)) {
424 for (int i
= 0; i
< crush
->max_buckets
; i
++) {
425 if (!crush
->buckets
[i
])
427 crush_bucket
*b
= crush
->buckets
[i
];
429 for (unsigned i
=0; i
<b
->size
; ++i
) {
430 int id
= b
->items
[i
];
432 ldout(cct
, 5) << "remove_item removing item " << item
433 << " from bucket " << b
->id
<< dendl
;
434 for (auto& p
: choose_args
) {
435 // weight down each weight-set to 0 before we remove the item
436 vector
<int> weightv(get_choose_args_positions(p
.second
), 0);
437 choose_args_adjust_item_weight(cct
, p
.second
, item
, weightv
, nullptr);
439 bucket_remove_item(b
, item
);
440 adjust_item_weight(cct
, b
->id
, b
->weight
);
446 if (_maybe_remove_last_instance(cct
, item
, unlink_only
))
452 bool CrushWrapper::_search_item_exists(int item
) const
454 for (int i
= 0; i
< crush
->max_buckets
; i
++) {
455 if (!crush
->buckets
[i
])
457 crush_bucket
*b
= crush
->buckets
[i
];
458 for (unsigned j
=0; j
<b
->size
; ++j
) {
459 if (b
->items
[j
] == item
)
466 bool CrushWrapper::_bucket_is_in_use(int item
)
468 for (auto &i
: class_bucket
)
469 for (auto &j
: i
.second
)
470 if (j
.second
== item
)
472 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
473 crush_rule
*r
= crush
->rules
[i
];
476 for (unsigned j
= 0; j
< r
->len
; ++j
) {
477 if (r
->steps
[j
].op
== CRUSH_RULE_TAKE
) {
478 int step_item
= r
->steps
[j
].arg1
;
481 int res
= split_id_class(step_item
, &original_item
, &c
);
484 if (step_item
== item
|| original_item
== item
)
492 int CrushWrapper::_remove_item_under(
493 CephContext
*cct
, int item
, int ancestor
, bool unlink_only
)
495 ldout(cct
, 5) << "_remove_item_under " << item
<< " under " << ancestor
496 << (unlink_only
? " unlink_only":"") << dendl
;
502 if (!bucket_exists(ancestor
))
507 crush_bucket
*b
= get_bucket(ancestor
);
508 for (unsigned i
=0; i
<b
->size
; ++i
) {
509 int id
= b
->items
[i
];
511 ldout(cct
, 5) << "_remove_item_under removing item " << item
512 << " from bucket " << b
->id
<< dendl
;
513 for (auto& p
: choose_args
) {
514 // weight down each weight-set to 0 before we remove the item
515 vector
<int> weightv(get_choose_args_positions(p
.second
), 0);
516 _choose_args_adjust_item_weight_in_bucket(
517 cct
, p
.second
, b
->id
, item
, weightv
, nullptr);
519 bucket_remove_item(b
, item
);
520 adjust_item_weight(cct
, b
->id
, b
->weight
);
523 int r
= remove_item_under(cct
, item
, id
, unlink_only
);
531 int CrushWrapper::remove_item_under(
532 CephContext
*cct
, int item
, int ancestor
, bool unlink_only
)
534 ldout(cct
, 5) << "remove_item_under " << item
<< " under " << ancestor
535 << (unlink_only
? " unlink_only":"") << dendl
;
537 if (!unlink_only
&& _bucket_is_in_use(item
)) {
541 int ret
= _remove_item_under(cct
, item
, ancestor
, unlink_only
);
545 if (item
< 0 && !unlink_only
) {
546 crush_bucket
*t
= get_bucket(item
);
548 ldout(cct
, 1) << "remove_item_under bucket " << item
549 << " does not exist" << dendl
;
554 ldout(cct
, 1) << "remove_item_under bucket " << item
<< " has " << t
->size
555 << " items, not empty" << dendl
;
560 if (_maybe_remove_last_instance(cct
, item
, unlink_only
))
566 int CrushWrapper::get_common_ancestor_distance(CephContext
*cct
, int id
,
567 const std::multimap
<string
,string
>& loc
)
569 ldout(cct
, 5) << __func__
<< " " << id
<< " " << loc
<< dendl
;
570 if (!item_exists(id
))
572 map
<string
,string
> id_loc
= get_full_location(id
);
573 ldout(cct
, 20) << " id is at " << id_loc
<< dendl
;
575 for (map
<int,string
>::const_iterator p
= type_map
.begin();
578 map
<string
,string
>::iterator ip
= id_loc
.find(p
->second
);
579 if (ip
== id_loc
.end())
581 for (std::multimap
<string
,string
>::const_iterator q
= loc
.find(p
->second
);
584 if (q
->first
!= p
->second
)
586 if (q
->second
== ip
->second
)
593 int CrushWrapper::parse_loc_map(const std::vector
<string
>& args
,
594 std::map
<string
,string
> *ploc
)
597 for (unsigned i
= 0; i
< args
.size(); ++i
) {
598 const char *s
= args
[i
].c_str();
599 const char *pos
= strchr(s
, '=');
602 string
key(s
, 0, pos
-s
);
605 (*ploc
)[key
] = value
;
612 int CrushWrapper::parse_loc_multimap(const std::vector
<string
>& args
,
613 std::multimap
<string
,string
> *ploc
)
616 for (unsigned i
= 0; i
< args
.size(); ++i
) {
617 const char *s
= args
[i
].c_str();
618 const char *pos
= strchr(s
, '=');
621 string
key(s
, 0, pos
-s
);
624 ploc
->insert(make_pair(key
, value
));
631 bool CrushWrapper::check_item_loc(CephContext
*cct
, int item
, const map
<string
,string
>& loc
,
634 ldout(cct
, 5) << "check_item_loc item " << item
<< " loc " << loc
<< dendl
;
636 for (map
<int,string
>::const_iterator p
= type_map
.begin(); p
!= type_map
.end(); ++p
) {
641 // ignore types that aren't specified in loc
642 map
<string
,string
>::const_iterator q
= loc
.find(p
->second
);
643 if (q
== loc
.end()) {
644 ldout(cct
, 2) << "warning: did not specify location for '" << p
->second
<< "' level (levels are "
645 << type_map
<< ")" << dendl
;
649 if (!name_exists(q
->second
)) {
650 ldout(cct
, 5) << "check_item_loc bucket " << q
->second
<< " dne" << dendl
;
654 int id
= get_item_id(q
->second
);
656 ldout(cct
, 5) << "check_item_loc requested " << q
->second
<< " for type " << p
->second
657 << " is a device, not bucket" << dendl
;
661 assert(bucket_exists(id
));
662 crush_bucket
*b
= get_bucket(id
);
664 // see if item exists in this bucket
665 for (unsigned j
=0; j
<b
->size
; j
++) {
666 if (b
->items
[j
] == item
) {
667 ldout(cct
, 2) << "check_item_loc " << item
<< " exists in bucket " << b
->id
<< dendl
;
669 *weight
= crush_get_bucket_item_weight(b
, j
);
676 ldout(cct
, 1) << "check_item_loc item " << item
<< " loc " << loc
<< dendl
;
680 map
<string
, string
> CrushWrapper::get_full_location(int id
)
682 vector
<pair
<string
, string
> > full_location_ordered
;
683 map
<string
,string
> full_location
;
685 get_full_location_ordered(id
, full_location_ordered
);
687 std::copy(full_location_ordered
.begin(),
688 full_location_ordered
.end(),
689 std::inserter(full_location
, full_location
.begin()));
691 return full_location
;
694 int CrushWrapper::get_full_location_ordered(int id
, vector
<pair
<string
, string
> >& path
)
696 if (!item_exists(id
))
701 pair
<string
, string
> parent_coord
= get_immediate_parent(cur
, &ret
);
704 path
.push_back(parent_coord
);
705 cur
= get_item_id(parent_coord
.second
);
710 string
CrushWrapper::get_full_location_ordered_string(int id
)
712 vector
<pair
<string
, string
> > full_location_ordered
;
713 string full_location
;
714 get_full_location_ordered(id
, full_location_ordered
);
715 reverse(begin(full_location_ordered
), end(full_location_ordered
));
716 for(auto i
= full_location_ordered
.begin(); i
!= full_location_ordered
.end(); i
++) {
717 full_location
= full_location
+ i
->first
+ "=" + i
->second
;
718 if (i
!= full_location_ordered
.end() - 1) {
719 full_location
= full_location
+ ",";
722 return full_location
;
725 map
<int, string
> CrushWrapper::get_parent_hierarchy(int id
)
727 map
<int,string
> parent_hierarchy
;
728 pair
<string
, string
> parent_coord
= get_immediate_parent(id
);
731 // get the integer type for id and create a counter from there
732 int type_counter
= get_bucket_type(id
);
734 // if we get a negative type then we can assume that we have an OSD
735 // change behavior in get_item_type FIXME
736 if (type_counter
< 0)
739 // read the type map and get the name of the type with the largest ID
741 for (map
<int, string
>::iterator it
= type_map
.begin(); it
!= type_map
.end(); ++it
){
742 if ( (*it
).first
> high_type
)
743 high_type
= (*it
).first
;
746 parent_id
= get_item_id(parent_coord
.second
);
748 while (type_counter
< high_type
) {
750 parent_hierarchy
[ type_counter
] = parent_coord
.first
;
752 if (type_counter
< high_type
){
753 // get the coordinate information for the next parent
754 parent_coord
= get_immediate_parent(parent_id
);
755 parent_id
= get_item_id(parent_coord
.second
);
759 return parent_hierarchy
;
762 int CrushWrapper::get_children(int id
, list
<int> *children
)
769 crush_bucket
*b
= get_bucket(id
);
774 for (unsigned n
=0; n
<b
->size
; n
++) {
775 children
->push_back(b
->items
[n
]);
780 int CrushWrapper::_get_leaves(int id
, list
<int> *leaves
)
786 leaves
->push_back(id
);
790 crush_bucket
*b
= get_bucket(id
);
795 for (unsigned n
= 0; n
< b
->size
; n
++) {
796 if (b
->items
[n
] >= 0) {
797 leaves
->push_back(b
->items
[n
]);
799 // is a bucket, do recursive call
800 int r
= _get_leaves(b
->items
[n
], leaves
);
807 return 0; // all is well
810 int CrushWrapper::get_leaves(const string
&name
, set
<int> *leaves
)
815 if (!name_exists(name
)) {
819 int id
= get_item_id(name
);
827 int r
= _get_leaves(id
, &unordered
);
832 for (auto &p
: unordered
) {
839 int CrushWrapper::insert_item(
840 CephContext
*cct
, int item
, float weight
, string name
,
841 const map
<string
,string
>& loc
) // typename -> bucketname
843 ldout(cct
, 5) << "insert_item item " << item
<< " weight " << weight
844 << " name " << name
<< " loc " << loc
<< dendl
;
846 if (!is_valid_crush_name(name
))
849 if (!is_valid_crush_loc(cct
, loc
))
852 int r
= validate_weightf(weight
);
857 if (name_exists(name
)) {
858 if (get_item_id(name
) != item
) {
859 ldout(cct
, 10) << "device name '" << name
<< "' already exists as id "
860 << get_item_id(name
) << dendl
;
864 set_item_name(item
, name
);
869 // create locations if locations don't exist and add child in
870 // location with 0 weight the more detail in the insert_item method
871 // declaration in CrushWrapper.h
872 for (auto p
= type_map
.begin(); p
!= type_map
.end(); ++p
) {
873 // ignore device type
877 // skip types that are unspecified
878 map
<string
,string
>::const_iterator q
= loc
.find(p
->second
);
879 if (q
== loc
.end()) {
880 ldout(cct
, 2) << "warning: did not specify location for '"
881 << p
->second
<< "' level (levels are "
882 << type_map
<< ")" << dendl
;
886 if (!name_exists(q
->second
)) {
887 ldout(cct
, 5) << "insert_item creating bucket " << q
->second
<< dendl
;
888 int empty
= 0, newid
;
889 int r
= add_bucket(0, 0,
890 CRUSH_HASH_DEFAULT
, p
->first
, 1, &cur
, &empty
, &newid
);
892 ldout(cct
, 1) << "add_bucket failure error: " << cpp_strerror(r
)
896 set_item_name(newid
, q
->second
);
902 // add to an existing bucket
903 int id
= get_item_id(q
->second
);
904 if (!bucket_exists(id
)) {
905 ldout(cct
, 1) << "insert_item doesn't have bucket " << id
<< dendl
;
909 // check that we aren't creating a cycle.
910 if (subtree_contains(id
, cur
)) {
911 ldout(cct
, 1) << "insert_item item " << cur
<< " already exists beneath "
916 // we have done sanity check above
917 crush_bucket
*b
= get_bucket(id
);
919 if (p
->first
!= b
->type
) {
920 ldout(cct
, 1) << "insert_item existing bucket has type "
921 << "'" << type_map
[b
->type
] << "' != "
922 << "'" << type_map
[p
->first
] << "'" << dendl
;
926 // are we forming a loop?
927 if (subtree_contains(cur
, b
->id
)) {
928 ldout(cct
, 1) << "insert_item " << cur
<< " already contains " << b
->id
929 << "; cannot form loop" << dendl
;
933 ldout(cct
, 5) << "insert_item adding " << cur
<< " weight " << weight
934 << " to bucket " << id
<< dendl
;
935 int r
= bucket_add_item(b
, cur
, 0);
940 // adjust the item's weight in location
941 if (adjust_item_weightf_in_loc(cct
, item
, weight
, loc
) > 0) {
942 if (item
>= crush
->max_devices
) {
943 crush
->max_devices
= item
+ 1;
944 ldout(cct
, 5) << "insert_item max_devices now " << crush
->max_devices
947 r
= rebuild_roots_with_classes();
949 ldout(cct
, 0) << __func__
<< " unable to rebuild roots with classes: "
950 << cpp_strerror(r
) << dendl
;
956 ldout(cct
, 1) << "error: didn't find anywhere to add item " << item
957 << " in " << loc
<< dendl
;
962 int CrushWrapper::move_bucket(
963 CephContext
*cct
, int id
, const map
<string
,string
>& loc
)
965 // sorry this only works for buckets
969 if (!item_exists(id
))
972 // get the name of the bucket we are trying to move for later
973 string id_name
= get_item_name(id
);
976 int bucket_weight
= detach_bucket(cct
, id
);
978 // insert the bucket back into the hierarchy
979 return insert_item(cct
, id
, bucket_weight
/ (float)0x10000, id_name
, loc
);
982 int CrushWrapper::detach_bucket(CephContext
*cct
, int item
)
990 // check that the bucket that we want to detach exists
991 assert(bucket_exists(item
));
993 // get the bucket's weight
994 crush_bucket
*b
= get_bucket(item
);
995 unsigned bucket_weight
= b
->weight
;
997 // get where the bucket is located
998 pair
<string
, string
> bucket_location
= get_immediate_parent(item
);
1000 // get the id of the parent bucket
1001 int parent_id
= get_item_id(bucket_location
.second
);
1003 // get the parent bucket
1004 crush_bucket
*parent_bucket
= get_bucket(parent_id
);
1006 if (!IS_ERR(parent_bucket
)) {
1007 // zero out the bucket weight
1008 bucket_adjust_item_weight(cct
, parent_bucket
, item
, 0);
1009 adjust_item_weight(cct
, parent_bucket
->id
, parent_bucket
->weight
);
1010 for (auto& p
: choose_args
) {
1011 // weight down each weight-set to 0 before we remove the item
1012 vector
<int> weightv(get_choose_args_positions(p
.second
), 0);
1013 choose_args_adjust_item_weight(cct
, p
.second
, item
, weightv
, nullptr);
1016 // remove the bucket from the parent
1017 bucket_remove_item(parent_bucket
, item
);
1018 } else if (PTR_ERR(parent_bucket
) != -ENOENT
) {
1019 return PTR_ERR(parent_bucket
);
1022 // check that we're happy
1023 int test_weight
= 0;
1024 map
<string
,string
> test_location
;
1025 test_location
[ bucket_location
.first
] = (bucket_location
.second
);
1027 bool successful_detach
= !(check_item_loc(cct
, item
, test_location
,
1029 assert(successful_detach
);
1030 assert(test_weight
== 0);
1032 return bucket_weight
;
1035 int CrushWrapper::swap_bucket(CephContext
*cct
, int src
, int dst
)
1037 if (src
>= 0 || dst
>= 0)
1039 if (!item_exists(src
) || !item_exists(dst
))
1041 crush_bucket
*a
= get_bucket(src
);
1042 crush_bucket
*b
= get_bucket(dst
);
1043 unsigned aw
= a
->weight
;
1044 unsigned bw
= b
->weight
;
1047 adjust_item_weight(cct
, a
->id
, bw
);
1048 adjust_item_weight(cct
, b
->id
, aw
);
1051 map
<int,unsigned> tmp
;
1052 unsigned as
= a
->size
;
1053 unsigned bs
= b
->size
;
1054 for (unsigned i
= 0; i
< as
; ++i
) {
1055 int item
= a
->items
[0];
1056 int itemw
= crush_get_bucket_item_weight(a
, 0);
1058 bucket_remove_item(a
, item
);
1060 assert(a
->size
== 0);
1061 assert(b
->size
== bs
);
1062 for (unsigned i
= 0; i
< bs
; ++i
) {
1063 int item
= b
->items
[0];
1064 int itemw
= crush_get_bucket_item_weight(b
, 0);
1065 bucket_remove_item(b
, item
);
1066 bucket_add_item(a
, item
, itemw
);
1068 assert(a
->size
== bs
);
1069 assert(b
->size
== 0);
1070 for (auto t
: tmp
) {
1071 bucket_add_item(b
, t
.first
, t
.second
);
1073 assert(a
->size
== bs
);
1074 assert(b
->size
== as
);
1077 swap_names(src
, dst
);
1081 int CrushWrapper::link_bucket(
1082 CephContext
*cct
, int id
, const map
<string
,string
>& loc
)
1084 // sorry this only works for buckets
1088 if (!item_exists(id
))
1091 // get the name of the bucket we are trying to move for later
1092 string id_name
= get_item_name(id
);
1094 crush_bucket
*b
= get_bucket(id
);
1095 unsigned bucket_weight
= b
->weight
;
1097 return insert_item(cct
, id
, bucket_weight
/ (float)0x10000, id_name
, loc
);
1100 int CrushWrapper::create_or_move_item(
1101 CephContext
*cct
, int item
, float weight
, string name
,
1102 const map
<string
,string
>& loc
) // typename -> bucketname
1107 if (!is_valid_crush_name(name
))
1110 if (check_item_loc(cct
, item
, loc
, &old_iweight
)) {
1111 ldout(cct
, 5) << "create_or_move_item " << item
<< " already at " << loc
1114 if (_search_item_exists(item
)) {
1115 weight
= get_item_weightf(item
);
1116 ldout(cct
, 10) << "create_or_move_item " << item
1117 << " exists with weight " << weight
<< dendl
;
1118 remove_item(cct
, item
, true);
1120 ldout(cct
, 5) << "create_or_move_item adding " << item
1121 << " weight " << weight
1122 << " at " << loc
<< dendl
;
1123 ret
= insert_item(cct
, item
, weight
, name
, loc
);
1130 int CrushWrapper::update_item(
1131 CephContext
*cct
, int item
, float weight
, string name
,
1132 const map
<string
,string
>& loc
) // typename -> bucketname
1134 ldout(cct
, 5) << "update_item item " << item
<< " weight " << weight
1135 << " name " << name
<< " loc " << loc
<< dendl
;
1138 if (!is_valid_crush_name(name
))
1141 if (!is_valid_crush_loc(cct
, loc
))
1144 ret
= validate_weightf(weight
);
1149 // compare quantized (fixed-point integer) weights!
1150 int iweight
= (int)(weight
* (float)0x10000);
1152 if (check_item_loc(cct
, item
, loc
, &old_iweight
)) {
1153 ldout(cct
, 5) << "update_item " << item
<< " already at " << loc
<< dendl
;
1154 if (old_iweight
!= iweight
) {
1155 ldout(cct
, 5) << "update_item " << item
<< " adjusting weight "
1156 << ((float)old_iweight
/(float)0x10000) << " -> " << weight
1158 adjust_item_weight_in_loc(cct
, item
, iweight
, loc
);
1161 if (get_item_name(item
) != name
) {
1162 ldout(cct
, 5) << "update_item setting " << item
<< " name to " << name
1164 set_item_name(item
, name
);
1168 if (item_exists(item
)) {
1169 remove_item(cct
, item
, true);
1171 ldout(cct
, 5) << "update_item adding " << item
<< " weight " << weight
1172 << " at " << loc
<< dendl
;
1173 ret
= insert_item(cct
, item
, weight
, name
, loc
);
1180 int CrushWrapper::get_item_weight(int id
) const
1182 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1183 crush_bucket
*b
= crush
->buckets
[bidx
];
1188 for (unsigned i
= 0; i
< b
->size
; i
++)
1189 if (b
->items
[i
] == id
)
1190 return crush_get_bucket_item_weight(b
, i
);
1195 int CrushWrapper::get_item_weight_in_loc(int id
, const map
<string
,string
> &loc
)
1197 for (map
<string
,string
>::const_iterator l
= loc
.begin(); l
!= loc
.end(); ++l
) {
1199 int bid
= get_item_id(l
->second
);
1200 if (!bucket_exists(bid
))
1202 crush_bucket
*b
= get_bucket(bid
);
1203 for (unsigned int i
= 0; i
< b
->size
; i
++) {
1204 if (b
->items
[i
] == id
) {
1205 return crush_get_bucket_item_weight(b
, i
);
1212 int CrushWrapper::adjust_item_weight(CephContext
*cct
, int id
, int weight
)
1214 ldout(cct
, 5) << "adjust_item_weight " << id
<< " weight " << weight
<< dendl
;
1216 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1217 crush_bucket
*b
= crush
->buckets
[bidx
];
1220 for (unsigned i
= 0; i
< b
->size
; i
++) {
1221 if (b
->items
[i
] == id
) {
1222 int diff
= bucket_adjust_item_weight(cct
, b
, id
, weight
);
1223 ldout(cct
, 5) << "adjust_item_weight " << id
<< " diff " << diff
1224 << " in bucket " << bidx
<< dendl
;
1225 adjust_item_weight(cct
, -1 - bidx
, b
->weight
);
1235 int CrushWrapper::adjust_item_weight_in_loc(CephContext
*cct
, int id
, int weight
, const map
<string
,string
>& loc
)
1237 ldout(cct
, 5) << "adjust_item_weight_in_loc " << id
<< " weight " << weight
1238 << " in " << loc
<< dendl
;
1241 for (auto l
= loc
.begin(); l
!= loc
.end(); ++l
) {
1242 int bid
= get_item_id(l
->second
);
1243 if (!bucket_exists(bid
))
1245 crush_bucket
*b
= get_bucket(bid
);
1246 for (unsigned int i
= 0; i
< b
->size
; i
++) {
1247 if (b
->items
[i
] == id
) {
1248 int diff
= bucket_adjust_item_weight(cct
, b
, id
, weight
);
1249 ldout(cct
, 5) << "adjust_item_weight_in_loc " << id
<< " diff " << diff
1250 << " in bucket " << bid
<< dendl
;
1251 adjust_item_weight(cct
, bid
, b
->weight
);
1261 int CrushWrapper::adjust_subtree_weight(CephContext
*cct
, int id
, int weight
)
1263 ldout(cct
, 5) << __func__
<< " " << id
<< " weight " << weight
<< dendl
;
1264 crush_bucket
*b
= get_bucket(id
);
1268 list
<crush_bucket
*> q
;
1270 while (!q
.empty()) {
1273 int local_changed
= 0;
1274 for (unsigned i
=0; i
<b
->size
; ++i
) {
1275 int n
= b
->items
[i
];
1277 bucket_adjust_item_weight(cct
, b
, n
, weight
);
1281 crush_bucket
*sub
= get_bucket(n
);
1287 if (local_changed
) {
1288 adjust_item_weight(cct
, b
->id
, b
->weight
);
1294 bool CrushWrapper::check_item_present(int id
) const
1298 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1299 crush_bucket
*b
= crush
->buckets
[bidx
];
1302 for (unsigned i
= 0; i
< b
->size
; i
++)
1303 if (b
->items
[i
] == id
)
1310 pair
<string
,string
> CrushWrapper::get_immediate_parent(int id
, int *_ret
)
1313 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1314 crush_bucket
*b
= crush
->buckets
[bidx
];
1317 if (is_shadow_item(b
->id
))
1319 for (unsigned i
= 0; i
< b
->size
; i
++)
1320 if (b
->items
[i
] == id
) {
1321 string parent_id
= name_map
[b
->id
];
1322 string parent_bucket_type
= type_map
[b
->type
];
1325 return make_pair(parent_bucket_type
, parent_id
);
1332 return pair
<string
, string
>();
1335 int CrushWrapper::get_immediate_parent_id(int id
, int *parent
) const
1337 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1338 crush_bucket
*b
= crush
->buckets
[bidx
];
1341 if (is_shadow_item(b
->id
))
1343 for (unsigned i
= 0; i
< b
->size
; i
++) {
1344 if (b
->items
[i
] == id
) {
1353 int CrushWrapper::get_parent_of_type(int item
, int type
) const
1356 int r
= get_immediate_parent_id(item
, &item
);
1360 } while (get_bucket_type(item
) != type
);
1364 int CrushWrapper::rename_class(const string
& srcname
, const string
& dstname
)
1366 auto i
= class_rname
.find(srcname
);
1367 if (i
== class_rname
.end())
1369 auto j
= class_rname
.find(dstname
);
1370 if (j
!= class_rname
.end())
1373 int class_id
= i
->second
;
1374 assert(class_name
.count(class_id
));
1375 // rename any shadow buckets of old class name
1376 for (auto &it
: class_map
) {
1377 if (it
.first
< 0 && it
.second
== class_id
) {
1378 string old_name
= get_item_name(it
.first
);
1379 size_t pos
= old_name
.find("~");
1380 assert(pos
!= string::npos
);
1381 string name_no_class
= old_name
.substr(0, pos
);
1382 string old_class_name
= old_name
.substr(pos
+ 1);
1383 assert(old_class_name
== srcname
);
1384 string new_name
= name_no_class
+ "~" + dstname
;
1385 // we do not use set_item_name
1386 // because the name is intentionally invalid
1387 name_map
[it
.first
] = new_name
;
1393 class_rname
.erase(srcname
);
1394 class_name
.erase(class_id
);
1395 class_rname
[dstname
] = class_id
;
1396 class_name
[class_id
] = dstname
;
1400 int CrushWrapper::populate_classes(
1401 const std::map
<int32_t, map
<int32_t, int32_t>>& old_class_bucket
)
1403 // build set of previous used shadow ids
1404 set
<int32_t> used_ids
;
1405 for (auto& p
: old_class_bucket
) {
1406 for (auto& q
: p
.second
) {
1407 used_ids
.insert(q
.second
);
1410 // accumulate weight values for each carg and bucket as we go. because it is
1411 // depth first, we will have the nested bucket weights we need when we
1412 // finish constructing the containing buckets.
1413 map
<int,map
<int,vector
<int>>> cmap_item_weight
; // cargs -> bno -> weights
1415 find_nonshadow_roots(roots
);
1416 for (auto &r
: roots
) {
1419 for (auto &c
: class_name
) {
1421 int res
= device_class_clone(r
, c
.first
, old_class_bucket
, used_ids
,
1422 &clone
, &cmap_item_weight
);
1430 int CrushWrapper::trim_roots_with_class()
1433 find_shadow_roots(roots
);
1434 for (auto &r
: roots
) {
1437 int res
= remove_root(r
);
1441 // there is no need to reweight because we only remove from the
1446 int32_t CrushWrapper::_alloc_class_id() const {
1447 if (class_name
.empty()) {
1450 int32_t class_id
= class_name
.rbegin()->first
+ 1;
1451 if (class_id
>= 0) {
1454 // wrapped, pick a random start and do exhaustive search
1455 uint32_t upperlimit
= numeric_limits
<int32_t>::max();
1457 class_id
= rand() % upperlimit
;
1458 const auto start
= class_id
;
1460 if (!class_name
.count(class_id
)) {
1468 } while (class_id
!= start
);
1469 assert(0 == "no available class id");
1472 void CrushWrapper::reweight(CephContext
*cct
)
1476 for (set
<int>::iterator p
= roots
.begin(); p
!= roots
.end(); ++p
) {
1479 crush_bucket
*b
= get_bucket(*p
);
1480 ldout(cct
, 5) << "reweight bucket " << *p
<< dendl
;
1481 int r
= crush_reweight_bucket(crush
, b
);
1486 int CrushWrapper::add_simple_rule_at(
1487 string name
, string root_name
,
1488 string failure_domain_name
,
1489 string device_class
,
1490 string mode
, int rule_type
,
1494 if (rule_exists(name
)) {
1496 *err
<< "rule " << name
<< " exists";
1500 if (rule_exists(rno
)) {
1502 *err
<< "rule with ruleno " << rno
<< " exists";
1505 if (ruleset_exists(rno
)) {
1507 *err
<< "ruleset " << rno
<< " exists";
1511 for (rno
= 0; rno
< get_max_rules(); rno
++) {
1512 if (!rule_exists(rno
) && !ruleset_exists(rno
))
1516 if (!name_exists(root_name
)) {
1518 *err
<< "root item " << root_name
<< " does not exist";
1521 int root
= get_item_id(root_name
);
1523 if (failure_domain_name
.length()) {
1524 type
= get_type_id(failure_domain_name
);
1527 *err
<< "unknown type " << failure_domain_name
;
1531 if (device_class
.size()) {
1532 if (!class_exists(device_class
)) {
1534 *err
<< "device class " << device_class
<< " does not exist";
1537 int c
= get_class_id(device_class
);
1538 if (class_bucket
.count(root
) == 0 ||
1539 class_bucket
[root
].count(c
) == 0) {
1541 *err
<< "root " << root_name
<< " has no devices with class "
1545 root
= class_bucket
[root
][c
];
1547 if (mode
!= "firstn" && mode
!= "indep") {
1549 *err
<< "unknown mode " << mode
;
1554 if (mode
== "indep")
1556 int min_rep
= mode
== "firstn" ? 1 : 3;
1557 int max_rep
= mode
== "firstn" ? 10 : 20;
1558 //set the ruleset the same as rule_id(rno)
1559 crush_rule
*rule
= crush_make_rule(steps
, rno
, rule_type
, min_rep
, max_rep
);
1562 if (mode
== "indep") {
1563 crush_rule_set_step(rule
, step
++, CRUSH_RULE_SET_CHOOSELEAF_TRIES
, 5, 0);
1564 crush_rule_set_step(rule
, step
++, CRUSH_RULE_SET_CHOOSE_TRIES
, 100, 0);
1566 crush_rule_set_step(rule
, step
++, CRUSH_RULE_TAKE
, root
, 0);
1568 crush_rule_set_step(rule
, step
++,
1569 mode
== "firstn" ? CRUSH_RULE_CHOOSELEAF_FIRSTN
:
1570 CRUSH_RULE_CHOOSELEAF_INDEP
,
1574 crush_rule_set_step(rule
, step
++,
1575 mode
== "firstn" ? CRUSH_RULE_CHOOSE_FIRSTN
:
1576 CRUSH_RULE_CHOOSE_INDEP
,
1579 crush_rule_set_step(rule
, step
++, CRUSH_RULE_EMIT
, 0, 0);
1581 int ret
= crush_add_rule(crush
, rule
, rno
);
1583 *err
<< "failed to add rule " << rno
<< " because " << cpp_strerror(ret
);
1586 set_rule_name(rno
, name
);
1591 int CrushWrapper::add_simple_rule(
1592 string name
, string root_name
,
1593 string failure_domain_name
,
1594 string device_class
,
1595 string mode
, int rule_type
,
1598 return add_simple_rule_at(name
, root_name
, failure_domain_name
, device_class
,
1600 rule_type
, -1, err
);
1603 int CrushWrapper::get_rule_weight_osd_map(unsigned ruleno
, map
<int,float> *pmap
)
1605 if (ruleno
>= crush
->max_rules
)
1607 if (crush
->rules
[ruleno
] == NULL
)
1609 crush_rule
*rule
= crush
->rules
[ruleno
];
1611 // build a weight map for each TAKE in the rule, and then merge them
1613 // FIXME: if there are multiple takes that place a different number of
1614 // objects we do not take that into account. (Also, note that doing this
1615 // right is also a function of the pool, since the crush rule
1616 // might choose 2 + choose 2 but pool size may only be 3.)
1617 for (unsigned i
=0; i
<rule
->len
; ++i
) {
1620 if (rule
->steps
[i
].op
== CRUSH_RULE_TAKE
) {
1621 int n
= rule
->steps
[i
].arg1
;
1628 //breadth first iterate the OSD tree
1629 while (!q
.empty()) {
1630 int bno
= q
.front();
1632 crush_bucket
*b
= crush
->buckets
[-1-bno
];
1634 for (unsigned j
=0; j
<b
->size
; ++j
) {
1635 int item_id
= b
->items
[j
];
1636 if (item_id
>= 0) { //it's an OSD
1637 float w
= crush_get_bucket_item_weight(b
, j
);
1640 } else { //not an OSD, expand the child later
1641 q
.push_back(item_id
);
1647 for (map
<int,float>::iterator p
= m
.begin(); p
!= m
.end(); ++p
) {
1648 map
<int,float>::iterator q
= pmap
->find(p
->first
);
1649 if (q
== pmap
->end()) {
1650 (*pmap
)[p
->first
] = p
->second
/ sum
;
1652 q
->second
+= p
->second
/ sum
;
1660 int CrushWrapper::remove_rule(int ruleno
)
1662 if (ruleno
>= (int)crush
->max_rules
)
1664 if (crush
->rules
[ruleno
] == NULL
)
1666 crush_destroy_rule(crush
->rules
[ruleno
]);
1667 crush
->rules
[ruleno
] = NULL
;
1668 rule_name_map
.erase(ruleno
);
1673 int CrushWrapper::bucket_adjust_item_weight(CephContext
*cct
, crush_bucket
*bucket
, int item
, int weight
)
1675 if (cct
->_conf
->osd_crush_update_weight_set
) {
1677 for (position
= 0; position
< bucket
->size
; position
++)
1678 if (bucket
->items
[position
] == item
)
1680 assert(position
!= bucket
->size
);
1681 for (auto w
: choose_args
) {
1682 crush_choose_arg_map arg_map
= w
.second
;
1683 crush_choose_arg
*arg
= &arg_map
.args
[-1-bucket
->id
];
1684 for (__u32 j
= 0; j
< arg
->weight_set_size
; j
++) {
1685 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
1686 weight_set
->weights
[position
] = weight
;
1690 return crush_bucket_adjust_item_weight(crush
, bucket
, item
, weight
);
1693 int CrushWrapper::add_bucket(
1694 int bucketno
, int alg
, int hash
, int type
, int size
,
1695 int *items
, int *weights
, int *idout
)
1698 alg
= get_default_bucket_alg();
1702 crush_bucket
*b
= crush_make_bucket(crush
, alg
, hash
, type
, size
, items
,
1705 int r
= crush_add_bucket(crush
, bucketno
, b
, idout
);
1706 for (auto& p
: choose_args
) {
1707 crush_choose_arg_map
& cmap
= p
.second
;
1709 if ((int)cmap
.size
<= *idout
) {
1710 cmap
.args
= (crush_choose_arg
*)realloc(
1712 sizeof(crush_choose_arg
) * (*idout
+ 1));
1713 memset(&cmap
.args
[cmap
.size
], 0,
1714 sizeof(crush_choose_arg
) * (*idout
+ 1 - cmap
.size
));
1715 cmap
.size
= *idout
+ 1;
1718 cmap
.args
= (crush_choose_arg
*)calloc(sizeof(crush_choose_arg
),
1720 cmap
.size
= *idout
+ 1;
1723 int positions
= get_choose_args_positions(cmap
);
1724 crush_choose_arg
& carg
= cmap
.args
[*idout
];
1725 carg
.weight_set
= (crush_weight_set
*)calloc(sizeof(crush_weight_set
),
1727 carg
.weight_set_size
= positions
;
1728 for (int ppos
= 0; ppos
< positions
; ++ppos
) {
1729 carg
.weight_set
[ppos
].weights
= (__u32
*)calloc(sizeof(__u32
), size
);
1730 carg
.weight_set
[ppos
].size
= size
;
1731 for (int bpos
= 0; bpos
< size
; ++bpos
) {
1732 carg
.weight_set
[ppos
].weights
[bpos
] = weights
[bpos
];
1740 int CrushWrapper::bucket_add_item(crush_bucket
*bucket
, int item
, int weight
)
1742 __u32 new_size
= bucket
->size
+ 1;
1743 int r
= crush_bucket_add_item(crush
, bucket
, item
, weight
);
1747 for (auto w
: choose_args
) {
1748 crush_choose_arg_map arg_map
= w
.second
;
1749 crush_choose_arg
*arg
= &arg_map
.args
[-1-bucket
->id
];
1750 for (__u32 j
= 0; j
< arg
->weight_set_size
; j
++) {
1751 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
1752 weight_set
->weights
= (__u32
*)realloc(weight_set
->weights
,
1753 new_size
* sizeof(__u32
));
1754 assert(weight_set
->size
+ 1 == new_size
);
1755 weight_set
->weights
[weight_set
->size
] = weight
;
1756 weight_set
->size
= new_size
;
1758 if (arg
->ids_size
) {
1759 arg
->ids
= (__s32
*)realloc(arg
->ids
, new_size
* sizeof(__s32
));
1760 assert(arg
->ids_size
+ 1 == new_size
);
1761 arg
->ids
[arg
->ids_size
] = item
;
1762 arg
->ids_size
= new_size
;
1768 int CrushWrapper::bucket_remove_item(crush_bucket
*bucket
, int item
)
1770 __u32 new_size
= bucket
->size
- 1;
1772 for (position
= 0; position
< bucket
->size
; position
++)
1773 if (bucket
->items
[position
] == item
)
1775 assert(position
!= bucket
->size
);
1776 int r
= crush_bucket_remove_item(crush
, bucket
, item
);
1780 for (auto w
: choose_args
) {
1781 crush_choose_arg_map arg_map
= w
.second
;
1782 crush_choose_arg
*arg
= &arg_map
.args
[-1-bucket
->id
];
1783 for (__u32 j
= 0; j
< arg
->weight_set_size
; j
++) {
1784 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
1785 assert(weight_set
->size
- 1 == new_size
);
1786 for (__u32 k
= position
; k
< new_size
; k
++)
1787 weight_set
->weights
[k
] = weight_set
->weights
[k
+1];
1789 weight_set
->weights
= (__u32
*)realloc(weight_set
->weights
,
1790 new_size
* sizeof(__u32
));
1792 weight_set
->weights
= NULL
;
1794 weight_set
->size
= new_size
;
1796 if (arg
->ids_size
) {
1797 assert(arg
->ids_size
- 1 == new_size
);
1798 for (__u32 k
= position
; k
< new_size
; k
++)
1799 arg
->ids
[k
] = arg
->ids
[k
+1];
1801 arg
->ids
= (__s32
*)realloc(arg
->ids
, new_size
* sizeof(__s32
));
1805 arg
->ids_size
= new_size
;
1811 int CrushWrapper::update_device_class(int id
,
1812 const string
& class_name
,
1816 assert(item_exists(id
));
1817 auto old_class_name
= get_item_class(id
);
1818 if (old_class_name
&& old_class_name
!= class_name
) {
1819 *ss
<< "osd." << id
<< " has already bound to class '" << old_class_name
1820 << "', can not reset class to '" << class_name
<< "'; "
1821 << "use 'ceph osd crush rm-device-class <osd>' to "
1822 << "remove old class first";
1826 int class_id
= get_or_create_class_id(class_name
);
1828 *ss
<< name
<< " id " << id
<< " is negative";
1832 if (class_map
.count(id
) != 0 && class_map
[id
] == class_id
) {
1833 *ss
<< name
<< " already set to class " << class_name
;
1837 set_item_class(id
, class_id
);
1839 int r
= rebuild_roots_with_classes();
1845 int CrushWrapper::remove_device_class(CephContext
*cct
, int id
, ostream
*ss
)
1848 const char *name
= get_item_name(id
);
1850 *ss
<< "osd." << id
<< " does not have a name";
1854 const char *class_name
= get_item_class(id
);
1856 *ss
<< "osd." << id
<< " has not been bound to a specific class yet";
1859 class_remove_item(id
);
1861 int r
= rebuild_roots_with_classes();
1863 *ss
<< "unable to rebuild roots with class '" << class_name
<< "' "
1864 << "of osd." << id
<< ": " << cpp_strerror(r
);
1870 int CrushWrapper::device_class_clone(
1871 int original_id
, int device_class
,
1872 const std::map
<int32_t, map
<int32_t, int32_t>>& old_class_bucket
,
1873 const std::set
<int32_t>& used_ids
,
1875 map
<int,map
<int,vector
<int>>> *cmap_item_weight
)
1877 const char *item_name
= get_item_name(original_id
);
1878 if (item_name
== NULL
)
1880 const char *class_name
= get_class_name(device_class
);
1881 if (class_name
== NULL
)
1883 string copy_name
= item_name
+ string("~") + class_name
;
1884 if (name_exists(copy_name
)) {
1885 *clone
= get_item_id(copy_name
);
1889 crush_bucket
*original
= get_bucket(original_id
);
1890 assert(!IS_ERR(original
));
1891 crush_bucket
*copy
= crush_make_bucket(crush
,
1898 vector
<unsigned> item_orig_pos
; // new item pos -> orig item pos
1899 for (unsigned i
= 0; i
< original
->size
; i
++) {
1900 int item
= original
->items
[i
];
1901 int weight
= crush_get_bucket_item_weight(original
, i
);
1903 if (class_map
.count(item
) != 0 && class_map
[item
] == device_class
) {
1904 int res
= crush_bucket_add_item(crush
, copy
, item
, weight
);
1912 int res
= device_class_clone(item
, device_class
, old_class_bucket
,
1913 used_ids
, &child_copy_id
,
1917 crush_bucket
*child_copy
= get_bucket(child_copy_id
);
1918 assert(!IS_ERR(child_copy
));
1919 res
= crush_bucket_add_item(crush
, copy
, child_copy_id
,
1920 child_copy
->weight
);
1924 item_orig_pos
.push_back(i
);
1926 assert(item_orig_pos
.size() == copy
->size
);
1929 if (old_class_bucket
.count(original_id
) &&
1930 old_class_bucket
.at(original_id
).count(device_class
)) {
1931 bno
= old_class_bucket
.at(original_id
).at(device_class
);
1933 // pick a new shadow bucket id that is not used by the current map
1934 // *or* any previous shadow buckets.
1936 while (((-1-bno
) < crush
->max_buckets
&& crush
->buckets
[-1-bno
]) ||
1937 used_ids
.count(bno
)) {
1941 int res
= crush_add_bucket(crush
, bno
, copy
, clone
);
1944 assert(!bno
|| bno
== *clone
);
1946 res
= set_item_class(*clone
, device_class
);
1950 // we do not use set_item_name because the name is intentionally invalid
1951 name_map
[*clone
] = copy_name
;
1953 name_rmap
[copy_name
] = *clone
;
1954 class_bucket
[original_id
][device_class
] = *clone
;
1956 // set up choose_args for the new bucket.
1957 for (auto& w
: choose_args
) {
1958 crush_choose_arg_map
& cmap
= w
.second
;
1959 if (-1-bno
>= (int)cmap
.size
) {
1960 unsigned new_size
= -1-bno
+ 1;
1961 cmap
.args
= (crush_choose_arg
*)realloc(cmap
.args
,
1962 new_size
* sizeof(cmap
.args
[0]));
1963 memset(cmap
.args
+ cmap
.size
, 0,
1964 (new_size
- cmap
.size
) * sizeof(cmap
.args
[0]));
1966 auto& o
= cmap
.args
[-1-original_id
];
1967 auto& n
= cmap
.args
[-1-bno
];
1968 n
.ids_size
= 0; // FIXME: implement me someday
1969 n
.weight_set_size
= o
.weight_set_size
;
1970 n
.weight_set
= (crush_weight_set
*)calloc(
1971 n
.weight_set_size
, sizeof(crush_weight_set
));
1972 for (size_t s
= 0; s
< n
.weight_set_size
; ++s
) {
1973 n
.weight_set
[s
].size
= copy
->size
;
1974 n
.weight_set
[s
].weights
= (__u32
*)calloc(copy
->size
, sizeof(__u32
));
1976 for (size_t s
= 0; s
< n
.weight_set_size
; ++s
) {
1977 vector
<int> bucket_weights(n
.weight_set_size
);
1978 for (size_t i
= 0; i
< copy
->size
; ++i
) {
1979 int item
= copy
->items
[i
];
1981 n
.weight_set
[s
].weights
[i
] = o
.weight_set
[s
].weights
[item_orig_pos
[i
]];
1983 n
.weight_set
[s
].weights
[i
] = (*cmap_item_weight
)[w
.first
][item
][s
];
1985 bucket_weights
[s
] += n
.weight_set
[s
].weights
[i
];
1987 (*cmap_item_weight
)[w
.first
][bno
] = bucket_weights
;
1993 bool CrushWrapper::_class_is_dead(int class_id
)
1995 for (auto &p
: class_map
) {
1996 if (p
.first
>= 0 && p
.second
== class_id
) {
2000 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
2001 crush_rule
*r
= crush
->rules
[i
];
2004 for (unsigned j
= 0; j
< r
->len
; ++j
) {
2005 if (r
->steps
[j
].op
== CRUSH_RULE_TAKE
) {
2006 int root
= r
->steps
[j
].arg1
;
2007 for (auto &p
: class_bucket
) {
2009 if (q
.count(class_id
) && q
[class_id
] == root
) {
2016 // no more referenced by any devices or crush rules
2020 void CrushWrapper::cleanup_dead_classes()
2022 auto p
= class_name
.begin();
2023 while (p
!= class_name
.end()) {
2024 if (_class_is_dead(p
->first
)) {
2025 string n
= p
->second
;
2027 remove_class_name(n
);
2034 int CrushWrapper::rebuild_roots_with_classes()
2036 std::map
<int32_t, map
<int32_t, int32_t> > old_class_bucket
= class_bucket
;
2037 cleanup_dead_classes();
2038 int r
= trim_roots_with_class();
2041 class_bucket
.clear();
2042 return populate_classes(old_class_bucket
);
2045 void CrushWrapper::encode(bufferlist
& bl
, uint64_t features
) const
2049 __u32 magic
= CRUSH_MAGIC
;
2050 ::encode(magic
, bl
);
2052 ::encode(crush
->max_buckets
, bl
);
2053 ::encode(crush
->max_rules
, bl
);
2054 ::encode(crush
->max_devices
, bl
);
2056 bool encode_compat_choose_args
= false;
2057 crush_choose_arg_map arg_map
;
2058 memset(&arg_map
, '\0', sizeof(arg_map
));
2059 if (has_choose_args() &&
2060 !HAVE_FEATURE(features
, CRUSH_CHOOSE_ARGS
)) {
2061 assert(!has_incompat_choose_args());
2062 encode_compat_choose_args
= true;
2063 arg_map
= choose_args
.begin()->second
;
2067 for (int i
=0; i
<crush
->max_buckets
; i
++) {
2069 if (crush
->buckets
[i
]) alg
= crush
->buckets
[i
]->alg
;
2074 ::encode(crush
->buckets
[i
]->id
, bl
);
2075 ::encode(crush
->buckets
[i
]->type
, bl
);
2076 ::encode(crush
->buckets
[i
]->alg
, bl
);
2077 ::encode(crush
->buckets
[i
]->hash
, bl
);
2078 ::encode(crush
->buckets
[i
]->weight
, bl
);
2079 ::encode(crush
->buckets
[i
]->size
, bl
);
2080 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++)
2081 ::encode(crush
->buckets
[i
]->items
[j
], bl
);
2083 switch (crush
->buckets
[i
]->alg
) {
2084 case CRUSH_BUCKET_UNIFORM
:
2085 ::encode((reinterpret_cast<crush_bucket_uniform
*>(crush
->buckets
[i
]))->item_weight
, bl
);
2088 case CRUSH_BUCKET_LIST
:
2089 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++) {
2090 ::encode((reinterpret_cast<crush_bucket_list
*>(crush
->buckets
[i
]))->item_weights
[j
], bl
);
2091 ::encode((reinterpret_cast<crush_bucket_list
*>(crush
->buckets
[i
]))->sum_weights
[j
], bl
);
2095 case CRUSH_BUCKET_TREE
:
2096 ::encode((reinterpret_cast<crush_bucket_tree
*>(crush
->buckets
[i
]))->num_nodes
, bl
);
2097 for (unsigned j
=0; j
<(reinterpret_cast<crush_bucket_tree
*>(crush
->buckets
[i
]))->num_nodes
; j
++)
2098 ::encode((reinterpret_cast<crush_bucket_tree
*>(crush
->buckets
[i
]))->node_weights
[j
], bl
);
2101 case CRUSH_BUCKET_STRAW
:
2102 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++) {
2103 ::encode((reinterpret_cast<crush_bucket_straw
*>(crush
->buckets
[i
]))->item_weights
[j
], bl
);
2104 ::encode((reinterpret_cast<crush_bucket_straw
*>(crush
->buckets
[i
]))->straws
[j
], bl
);
2108 case CRUSH_BUCKET_STRAW2
:
2111 if (encode_compat_choose_args
&&
2112 arg_map
.args
[i
].weight_set_size
> 0) {
2113 weights
= arg_map
.args
[i
].weight_set
[0].weights
;
2115 weights
= (reinterpret_cast<crush_bucket_straw2
*>(crush
->buckets
[i
]))->item_weights
;
2117 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++) {
2118 ::encode(weights
[j
], bl
);
2130 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
2131 __u32 yes
= crush
->rules
[i
] ? 1:0;
2136 ::encode(crush
->rules
[i
]->len
, bl
);
2137 ::encode(crush
->rules
[i
]->mask
, bl
);
2138 for (unsigned j
=0; j
<crush
->rules
[i
]->len
; j
++)
2139 ::encode(crush
->rules
[i
]->steps
[j
], bl
);
2143 ::encode(type_map
, bl
);
2144 ::encode(name_map
, bl
);
2145 ::encode(rule_name_map
, bl
);
2148 ::encode(crush
->choose_local_tries
, bl
);
2149 ::encode(crush
->choose_local_fallback_tries
, bl
);
2150 ::encode(crush
->choose_total_tries
, bl
);
2151 ::encode(crush
->chooseleaf_descend_once
, bl
);
2152 ::encode(crush
->chooseleaf_vary_r
, bl
);
2153 ::encode(crush
->straw_calc_version
, bl
);
2154 ::encode(crush
->allowed_bucket_algs
, bl
);
2155 if (features
& CEPH_FEATURE_CRUSH_TUNABLES5
) {
2156 ::encode(crush
->chooseleaf_stable
, bl
);
2159 if (HAVE_FEATURE(features
, SERVER_LUMINOUS
)) {
2161 ::encode(class_map
, bl
);
2162 ::encode(class_name
, bl
);
2163 ::encode(class_bucket
, bl
);
2166 __u32 size
= (__u32
)choose_args
.size();
2168 for (auto c
: choose_args
) {
2169 ::encode(c
.first
, bl
);
2170 crush_choose_arg_map arg_map
= c
.second
;
2172 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
2173 crush_choose_arg
*arg
= &arg_map
.args
[i
];
2174 if (arg
->weight_set_size
== 0 &&
2180 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
2181 crush_choose_arg
*arg
= &arg_map
.args
[i
];
2182 if (arg
->weight_set_size
== 0 &&
2186 ::encode(arg
->weight_set_size
, bl
);
2187 for (__u32 j
= 0; j
< arg
->weight_set_size
; j
++) {
2188 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
2189 ::encode(weight_set
->size
, bl
);
2190 for (__u32 k
= 0; k
< weight_set
->size
; k
++)
2191 ::encode(weight_set
->weights
[k
], bl
);
2193 ::encode(arg
->ids_size
, bl
);
2194 for (__u32 j
= 0; j
< arg
->ids_size
; j
++)
2195 ::encode(arg
->ids
[j
], bl
);
2201 static void decode_32_or_64_string_map(map
<int32_t,string
>& m
, bufferlist::iterator
& blp
)
2211 ::decode(strlen
, blp
);
2213 // der, key was actually 64-bits!
2214 ::decode(strlen
, blp
);
2216 ::decode_nohead(strlen
, m
[key
], blp
);
2220 void CrushWrapper::decode(bufferlist::iterator
& blp
)
2225 ::decode(magic
, blp
);
2226 if (magic
!= CRUSH_MAGIC
)
2227 throw buffer::malformed_input("bad magic number");
2229 ::decode(crush
->max_buckets
, blp
);
2230 ::decode(crush
->max_rules
, blp
);
2231 ::decode(crush
->max_devices
, blp
);
2233 // legacy tunables, unless we decode something newer
2234 set_tunables_legacy();
2238 crush
->buckets
= (crush_bucket
**)calloc(1, crush
->max_buckets
* sizeof(crush_bucket
*));
2239 for (int i
=0; i
<crush
->max_buckets
; i
++) {
2240 decode_crush_bucket(&crush
->buckets
[i
], blp
);
2244 crush
->rules
= (crush_rule
**)calloc(1, crush
->max_rules
* sizeof(crush_rule
*));
2245 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
2249 crush
->rules
[i
] = NULL
;
2255 crush
->rules
[i
] = reinterpret_cast<crush_rule
*>(calloc(1, crush_rule_size(len
)));
2256 crush
->rules
[i
]->len
= len
;
2257 ::decode(crush
->rules
[i
]->mask
, blp
);
2258 for (unsigned j
=0; j
<crush
->rules
[i
]->len
; j
++)
2259 ::decode(crush
->rules
[i
]->steps
[j
], blp
);
2263 // NOTE: we had a bug where we were incoding int instead of int32, which means the
2264 // 'key' field for these maps may be either 32 or 64 bits, depending. tolerate
2265 // both by assuming the string is always non-empty.
2266 decode_32_or_64_string_map(type_map
, blp
);
2267 decode_32_or_64_string_map(name_map
, blp
);
2268 decode_32_or_64_string_map(rule_name_map
, blp
);
2272 ::decode(crush
->choose_local_tries
, blp
);
2273 ::decode(crush
->choose_local_fallback_tries
, blp
);
2274 ::decode(crush
->choose_total_tries
, blp
);
2277 ::decode(crush
->chooseleaf_descend_once
, blp
);
2280 ::decode(crush
->chooseleaf_vary_r
, blp
);
2283 ::decode(crush
->straw_calc_version
, blp
);
2286 ::decode(crush
->allowed_bucket_algs
, blp
);
2289 ::decode(crush
->chooseleaf_stable
, blp
);
2292 ::decode(class_map
, blp
);
2293 ::decode(class_name
, blp
);
2294 for (auto &c
: class_name
)
2295 class_rname
[c
.second
] = c
.first
;
2296 ::decode(class_bucket
, blp
);
2299 __u32 choose_args_size
;
2300 ::decode(choose_args_size
, blp
);
2301 for (__u32 i
= 0; i
< choose_args_size
; i
++) {
2302 uint64_t choose_args_index
;
2303 ::decode(choose_args_index
, blp
);
2304 crush_choose_arg_map arg_map
;
2305 arg_map
.size
= crush
->max_buckets
;
2306 arg_map
.args
= (crush_choose_arg
*)calloc(
2307 arg_map
.size
, sizeof(crush_choose_arg
));
2309 ::decode(size
, blp
);
2310 for (__u32 j
= 0; j
< size
; j
++) {
2312 ::decode(bucket_index
, blp
);
2313 assert(bucket_index
< arg_map
.size
);
2314 crush_choose_arg
*arg
= &arg_map
.args
[bucket_index
];
2315 ::decode(arg
->weight_set_size
, blp
);
2316 if (arg
->weight_set_size
) {
2317 arg
->weight_set
= (crush_weight_set
*)calloc(
2318 arg
->weight_set_size
, sizeof(crush_weight_set
));
2319 for (__u32 k
= 0; k
< arg
->weight_set_size
; k
++) {
2320 crush_weight_set
*weight_set
= &arg
->weight_set
[k
];
2321 ::decode(weight_set
->size
, blp
);
2322 weight_set
->weights
= (__u32
*)calloc(
2323 weight_set
->size
, sizeof(__u32
));
2324 for (__u32 l
= 0; l
< weight_set
->size
; l
++)
2325 ::decode(weight_set
->weights
[l
], blp
);
2328 ::decode(arg
->ids_size
, blp
);
2329 if (arg
->ids_size
) {
2330 assert(arg
->ids_size
== crush
->buckets
[bucket_index
]->size
);
2331 arg
->ids
= (__s32
*)calloc(arg
->ids_size
, sizeof(__s32
));
2332 for (__u32 k
= 0; k
< arg
->ids_size
; k
++)
2333 ::decode(arg
->ids
[k
], blp
);
2336 choose_args
[choose_args_index
] = arg_map
;
2342 crush_destroy(crush
);
2347 void CrushWrapper::decode_crush_bucket(crush_bucket
** bptr
, bufferlist::iterator
&blp
)
2358 case CRUSH_BUCKET_UNIFORM
:
2359 size
= sizeof(crush_bucket_uniform
);
2361 case CRUSH_BUCKET_LIST
:
2362 size
= sizeof(crush_bucket_list
);
2364 case CRUSH_BUCKET_TREE
:
2365 size
= sizeof(crush_bucket_tree
);
2367 case CRUSH_BUCKET_STRAW
:
2368 size
= sizeof(crush_bucket_straw
);
2370 case CRUSH_BUCKET_STRAW2
:
2371 size
= sizeof(crush_bucket_straw2
);
2376 snprintf(str
, sizeof(str
), "unsupported bucket algorithm: %d", alg
);
2377 throw buffer::malformed_input(str
);
2380 crush_bucket
*bucket
= reinterpret_cast<crush_bucket
*>(calloc(1, size
));
2383 ::decode(bucket
->id
, blp
);
2384 ::decode(bucket
->type
, blp
);
2385 ::decode(bucket
->alg
, blp
);
2386 ::decode(bucket
->hash
, blp
);
2387 ::decode(bucket
->weight
, blp
);
2388 ::decode(bucket
->size
, blp
);
2390 bucket
->items
= (__s32
*)calloc(1, bucket
->size
* sizeof(__s32
));
2391 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
2392 ::decode(bucket
->items
[j
], blp
);
2395 switch (bucket
->alg
) {
2396 case CRUSH_BUCKET_UNIFORM
:
2397 ::decode((reinterpret_cast<crush_bucket_uniform
*>(bucket
))->item_weight
, blp
);
2400 case CRUSH_BUCKET_LIST
: {
2401 crush_bucket_list
* cbl
= reinterpret_cast<crush_bucket_list
*>(bucket
);
2402 cbl
->item_weights
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
2403 cbl
->sum_weights
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
2405 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
2406 ::decode(cbl
->item_weights
[j
], blp
);
2407 ::decode(cbl
->sum_weights
[j
], blp
);
2412 case CRUSH_BUCKET_TREE
: {
2413 crush_bucket_tree
* cbt
= reinterpret_cast<crush_bucket_tree
*>(bucket
);
2414 ::decode(cbt
->num_nodes
, blp
);
2415 cbt
->node_weights
= (__u32
*)calloc(1, cbt
->num_nodes
* sizeof(__u32
));
2416 for (unsigned j
=0; j
<cbt
->num_nodes
; j
++) {
2417 ::decode(cbt
->node_weights
[j
], blp
);
2422 case CRUSH_BUCKET_STRAW
: {
2423 crush_bucket_straw
* cbs
= reinterpret_cast<crush_bucket_straw
*>(bucket
);
2424 cbs
->straws
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
2425 cbs
->item_weights
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
2426 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
2427 ::decode(cbs
->item_weights
[j
], blp
);
2428 ::decode(cbs
->straws
[j
], blp
);
2433 case CRUSH_BUCKET_STRAW2
: {
2434 crush_bucket_straw2
* cbs
= reinterpret_cast<crush_bucket_straw2
*>(bucket
);
2435 cbs
->item_weights
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
2436 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
2437 ::decode(cbs
->item_weights
[j
], blp
);
2443 // We should have handled this case in the first switch statement
2450 void CrushWrapper::dump(Formatter
*f
) const
2452 f
->open_array_section("devices");
2453 for (int i
=0; i
<get_max_devices(); i
++) {
2454 f
->open_object_section("device");
2455 f
->dump_int("id", i
);
2456 const char *n
= get_item_name(i
);
2458 f
->dump_string("name", n
);
2461 sprintf(name
, "device%d", i
);
2462 f
->dump_string("name", name
);
2464 const char *device_class
= get_item_class(i
);
2465 if (device_class
!= NULL
)
2466 f
->dump_string("class", device_class
);
2471 f
->open_array_section("types");
2472 int n
= get_num_type_names();
2473 for (int i
=0; n
; i
++) {
2474 const char *name
= get_type_name(i
);
2477 f
->open_object_section("type");
2478 f
->dump_int("type_id", 0);
2479 f
->dump_string("name", "device");
2485 f
->open_object_section("type");
2486 f
->dump_int("type_id", i
);
2487 f
->dump_string("name", name
);
2492 f
->open_array_section("buckets");
2493 for (int bucket
= -1; bucket
> -1-get_max_buckets(); --bucket
) {
2494 if (!bucket_exists(bucket
))
2496 f
->open_object_section("bucket");
2497 f
->dump_int("id", bucket
);
2498 if (get_item_name(bucket
))
2499 f
->dump_string("name", get_item_name(bucket
));
2500 f
->dump_int("type_id", get_bucket_type(bucket
));
2501 if (get_type_name(get_bucket_type(bucket
)))
2502 f
->dump_string("type_name", get_type_name(get_bucket_type(bucket
)));
2503 f
->dump_int("weight", get_bucket_weight(bucket
));
2504 f
->dump_string("alg", crush_bucket_alg_name(get_bucket_alg(bucket
)));
2505 f
->dump_string("hash", crush_hash_name(get_bucket_hash(bucket
)));
2506 f
->open_array_section("items");
2507 for (int j
=0; j
<get_bucket_size(bucket
); j
++) {
2508 f
->open_object_section("item");
2509 f
->dump_int("id", get_bucket_item(bucket
, j
));
2510 f
->dump_int("weight", get_bucket_item_weight(bucket
, j
));
2511 f
->dump_int("pos", j
);
2519 f
->open_array_section("rules");
2523 f
->open_object_section("tunables");
2527 dump_choose_args(f
);
2531 // depth first walker
2533 typedef CrushTreeDumper::Item Item
;
2534 const CrushWrapper
*crush
;
2535 const CrushTreeDumper::name_map_t
& weight_set_names
;
2537 explicit TreeDumper(const CrushWrapper
*crush
,
2538 const CrushTreeDumper::name_map_t
& wsnames
)
2539 : crush(crush
), weight_set_names(wsnames
) {}
2541 void dump(Formatter
*f
) {
2543 crush
->find_roots(roots
);
2544 for (set
<int>::iterator root
= roots
.begin(); root
!= roots
.end(); ++root
) {
2545 dump_item(Item(*root
, 0, 0, crush
->get_bucket_weightf(*root
)), f
);
2550 void dump_item(const Item
& qi
, Formatter
* f
) {
2551 if (qi
.is_bucket()) {
2552 f
->open_object_section("bucket");
2553 CrushTreeDumper::dump_item_fields(crush
, weight_set_names
, qi
, f
);
2554 dump_bucket_children(qi
, f
);
2557 f
->open_object_section("device");
2558 CrushTreeDumper::dump_item_fields(crush
, weight_set_names
, qi
, f
);
2563 void dump_bucket_children(const Item
& parent
, Formatter
* f
) {
2564 f
->open_array_section("items");
2565 const int max_pos
= crush
->get_bucket_size(parent
.id
);
2566 for (int pos
= 0; pos
< max_pos
; pos
++) {
2567 int id
= crush
->get_bucket_item(parent
.id
, pos
);
2568 float weight
= crush
->get_bucket_item_weightf(parent
.id
, pos
);
2569 dump_item(Item(id
, parent
.id
, parent
.depth
+ 1, weight
), f
);
2576 void CrushWrapper::dump_tree(
2578 const CrushTreeDumper::name_map_t
& weight_set_names
) const
2581 TreeDumper(this, weight_set_names
).dump(f
);
2584 void CrushWrapper::dump_tunables(Formatter
*f
) const
2586 f
->dump_int("choose_local_tries", get_choose_local_tries());
2587 f
->dump_int("choose_local_fallback_tries", get_choose_local_fallback_tries());
2588 f
->dump_int("choose_total_tries", get_choose_total_tries());
2589 f
->dump_int("chooseleaf_descend_once", get_chooseleaf_descend_once());
2590 f
->dump_int("chooseleaf_vary_r", get_chooseleaf_vary_r());
2591 f
->dump_int("chooseleaf_stable", get_chooseleaf_stable());
2592 f
->dump_int("straw_calc_version", get_straw_calc_version());
2593 f
->dump_int("allowed_bucket_algs", get_allowed_bucket_algs());
2595 // be helpful about it
2596 if (has_jewel_tunables())
2597 f
->dump_string("profile", "jewel");
2598 else if (has_hammer_tunables())
2599 f
->dump_string("profile", "hammer");
2600 else if (has_firefly_tunables())
2601 f
->dump_string("profile", "firefly");
2602 else if (has_bobtail_tunables())
2603 f
->dump_string("profile", "bobtail");
2604 else if (has_argonaut_tunables())
2605 f
->dump_string("profile", "argonaut");
2607 f
->dump_string("profile", "unknown");
2608 f
->dump_int("optimal_tunables", (int)has_optimal_tunables());
2609 f
->dump_int("legacy_tunables", (int)has_legacy_tunables());
2611 // be helpful about minimum version required
2612 f
->dump_string("minimum_required_version", get_min_required_version());
2614 f
->dump_int("require_feature_tunables", (int)has_nondefault_tunables());
2615 f
->dump_int("require_feature_tunables2", (int)has_nondefault_tunables2());
2616 f
->dump_int("has_v2_rules", (int)has_v2_rules());
2617 f
->dump_int("require_feature_tunables3", (int)has_nondefault_tunables3());
2618 f
->dump_int("has_v3_rules", (int)has_v3_rules());
2619 f
->dump_int("has_v4_buckets", (int)has_v4_buckets());
2620 f
->dump_int("require_feature_tunables5", (int)has_nondefault_tunables5());
2621 f
->dump_int("has_v5_rules", (int)has_v5_rules());
2624 void CrushWrapper::dump_choose_args(Formatter
*f
) const
2626 f
->open_object_section("choose_args");
2627 for (auto c
: choose_args
) {
2628 crush_choose_arg_map arg_map
= c
.second
;
2629 f
->open_array_section(stringify(c
.first
).c_str());
2630 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
2631 crush_choose_arg
*arg
= &arg_map
.args
[i
];
2632 if (arg
->weight_set_size
== 0 &&
2635 f
->open_object_section("choose_args");
2636 int bucket_index
= i
;
2637 f
->dump_int("bucket_id", -1-bucket_index
);
2638 if (arg
->weight_set_size
> 0) {
2639 f
->open_array_section("weight_set");
2640 for (__u32 j
= 0; j
< arg
->weight_set_size
; j
++) {
2641 f
->open_array_section("weights");
2642 __u32
*weights
= arg
->weight_set
[j
].weights
;
2643 __u32 size
= arg
->weight_set
[j
].size
;
2644 for (__u32 k
= 0; k
< size
; k
++) {
2645 f
->dump_float("weight", (float)weights
[k
]/(float)0x10000);
2651 if (arg
->ids_size
> 0) {
2652 f
->open_array_section("ids");
2653 for (__u32 j
= 0; j
< arg
->ids_size
; j
++)
2654 f
->dump_int("id", arg
->ids
[j
]);
2664 void CrushWrapper::dump_rules(Formatter
*f
) const
2666 for (int i
=0; i
<get_max_rules(); i
++) {
2667 if (!rule_exists(i
))
2673 void CrushWrapper::dump_rule(int ruleset
, Formatter
*f
) const
2675 f
->open_object_section("rule");
2676 f
->dump_int("rule_id", ruleset
);
2677 if (get_rule_name(ruleset
))
2678 f
->dump_string("rule_name", get_rule_name(ruleset
));
2679 f
->dump_int("ruleset", get_rule_mask_ruleset(ruleset
));
2680 f
->dump_int("type", get_rule_mask_type(ruleset
));
2681 f
->dump_int("min_size", get_rule_mask_min_size(ruleset
));
2682 f
->dump_int("max_size", get_rule_mask_max_size(ruleset
));
2683 f
->open_array_section("steps");
2684 for (int j
=0; j
<get_rule_len(ruleset
); j
++) {
2685 f
->open_object_section("step");
2686 switch (get_rule_op(ruleset
, j
)) {
2687 case CRUSH_RULE_NOOP
:
2688 f
->dump_string("op", "noop");
2690 case CRUSH_RULE_TAKE
:
2691 f
->dump_string("op", "take");
2693 int item
= get_rule_arg1(ruleset
, j
);
2694 f
->dump_int("item", item
);
2696 const char *name
= get_item_name(item
);
2697 f
->dump_string("item_name", name
? name
: "");
2700 case CRUSH_RULE_EMIT
:
2701 f
->dump_string("op", "emit");
2703 case CRUSH_RULE_CHOOSE_FIRSTN
:
2704 f
->dump_string("op", "choose_firstn");
2705 f
->dump_int("num", get_rule_arg1(ruleset
, j
));
2706 f
->dump_string("type", get_type_name(get_rule_arg2(ruleset
, j
)));
2708 case CRUSH_RULE_CHOOSE_INDEP
:
2709 f
->dump_string("op", "choose_indep");
2710 f
->dump_int("num", get_rule_arg1(ruleset
, j
));
2711 f
->dump_string("type", get_type_name(get_rule_arg2(ruleset
, j
)));
2713 case CRUSH_RULE_CHOOSELEAF_FIRSTN
:
2714 f
->dump_string("op", "chooseleaf_firstn");
2715 f
->dump_int("num", get_rule_arg1(ruleset
, j
));
2716 f
->dump_string("type", get_type_name(get_rule_arg2(ruleset
, j
)));
2718 case CRUSH_RULE_CHOOSELEAF_INDEP
:
2719 f
->dump_string("op", "chooseleaf_indep");
2720 f
->dump_int("num", get_rule_arg1(ruleset
, j
));
2721 f
->dump_string("type", get_type_name(get_rule_arg2(ruleset
, j
)));
2723 case CRUSH_RULE_SET_CHOOSE_TRIES
:
2724 f
->dump_string("op", "set_choose_tries");
2725 f
->dump_int("num", get_rule_arg1(ruleset
, j
));
2727 case CRUSH_RULE_SET_CHOOSELEAF_TRIES
:
2728 f
->dump_string("op", "set_chooseleaf_tries");
2729 f
->dump_int("num", get_rule_arg1(ruleset
, j
));
2732 f
->dump_int("opcode", get_rule_op(ruleset
, j
));
2733 f
->dump_int("arg1", get_rule_arg1(ruleset
, j
));
2734 f
->dump_int("arg2", get_rule_arg2(ruleset
, j
));
2742 void CrushWrapper::list_rules(Formatter
*f
) const
2744 for (int rule
= 0; rule
< get_max_rules(); rule
++) {
2745 if (!rule_exists(rule
))
2747 f
->dump_string("name", get_rule_name(rule
));
2751 void CrushWrapper::list_rules(ostream
*ss
) const
2753 for (int rule
= 0; rule
< get_max_rules(); rule
++) {
2754 if (!rule_exists(rule
))
2756 *ss
<< get_rule_name(rule
) << "\n";
2760 class CrushTreePlainDumper
: public CrushTreeDumper::Dumper
<TextTable
> {
2762 typedef CrushTreeDumper::Dumper
<TextTable
> Parent
;
2764 explicit CrushTreePlainDumper(const CrushWrapper
*crush
,
2765 const CrushTreeDumper::name_map_t
& wsnames
)
2766 : Parent(crush
, wsnames
) {}
2767 explicit CrushTreePlainDumper(const CrushWrapper
*crush
,
2768 const CrushTreeDumper::name_map_t
& wsnames
,
2770 : Parent(crush
, wsnames
, show_shadow
) {}
2773 void dump(TextTable
*tbl
) {
2774 tbl
->define_column("ID", TextTable::LEFT
, TextTable::RIGHT
);
2775 tbl
->define_column("CLASS", TextTable::LEFT
, TextTable::RIGHT
);
2776 tbl
->define_column("WEIGHT", TextTable::LEFT
, TextTable::RIGHT
);
2777 for (auto& p
: crush
->choose_args
) {
2778 if (p
.first
== CrushWrapper::DEFAULT_CHOOSE_ARGS
) {
2779 tbl
->define_column("(compat)", TextTable::LEFT
, TextTable::RIGHT
);
2782 auto q
= weight_set_names
.find(p
.first
);
2783 name
= q
!= weight_set_names
.end() ? q
->second
:
2785 tbl
->define_column(name
.c_str(), TextTable::LEFT
, TextTable::RIGHT
);
2788 tbl
->define_column("TYPE NAME", TextTable::LEFT
, TextTable::LEFT
);
2793 void dump_item(const CrushTreeDumper::Item
&qi
, TextTable
*tbl
) override
{
2794 const char *c
= crush
->get_item_class(qi
.id
);
2799 << weightf_t(qi
.weight
);
2800 for (auto& p
: crush
->choose_args
) {
2801 if (qi
.parent
< 0) {
2802 const crush_choose_arg_map cmap
= crush
->choose_args_get(p
.first
);
2803 int bidx
= -1 - qi
.parent
;
2804 const crush_bucket
*b
= crush
->get_bucket(qi
.parent
);
2806 bidx
< (int)cmap
.size
&&
2807 cmap
.args
[bidx
].weight_set
&&
2808 cmap
.args
[bidx
].weight_set_size
>= 1) {
2811 pos
< (int)cmap
.args
[bidx
].weight_set
[0].size
&&
2812 b
->items
[pos
] != qi
.id
;
2814 *tbl
<< weightf_t((float)cmap
.args
[bidx
].weight_set
[0].weights
[pos
] /
2822 for (int k
=0; k
< qi
.depth
; k
++) {
2825 if (qi
.is_bucket()) {
2826 ss
<< crush
->get_type_name(crush
->get_bucket_type(qi
.id
)) << " "
2827 << crush
->get_item_name(qi
.id
);
2829 ss
<< "osd." << qi
.id
;
2832 *tbl
<< TextTable::endrow
;
2837 class CrushTreeFormattingDumper
: public CrushTreeDumper::FormattingDumper
{
2839 typedef CrushTreeDumper::FormattingDumper Parent
;
2841 explicit CrushTreeFormattingDumper(
2842 const CrushWrapper
*crush
,
2843 const CrushTreeDumper::name_map_t
& wsnames
)
2844 : Parent(crush
, wsnames
) {}
2846 explicit CrushTreeFormattingDumper(
2847 const CrushWrapper
*crush
,
2848 const CrushTreeDumper::name_map_t
& wsnames
,
2850 : Parent(crush
, wsnames
, show_shadow
) {}
2852 void dump(Formatter
*f
) {
2853 f
->open_array_section("nodes");
2856 f
->open_array_section("stray");
2862 void CrushWrapper::dump_tree(
2865 const CrushTreeDumper::name_map_t
& weight_set_names
,
2866 bool show_shadow
) const
2870 CrushTreePlainDumper(this, weight_set_names
, show_shadow
).dump(&tbl
);
2874 CrushTreeFormattingDumper(this, weight_set_names
, show_shadow
).dump(f
);
2878 void CrushWrapper::generate_test_instances(list
<CrushWrapper
*>& o
)
2880 o
.push_back(new CrushWrapper
);
2885 * Determine the default CRUSH ruleset ID to be used with
2886 * newly created replicated pools.
2888 * @returns a ruleset ID (>=0) or -1 if no suitable ruleset found
2890 int CrushWrapper::get_osd_pool_default_crush_replicated_ruleset(CephContext
*cct
)
2892 int crush_ruleset
= cct
->_conf
->osd_pool_default_crush_rule
;
2893 if (crush_ruleset
< 0) {
2894 crush_ruleset
= find_first_ruleset(pg_pool_t::TYPE_REPLICATED
);
2895 } else if (!ruleset_exists(crush_ruleset
)) {
2896 crush_ruleset
= -1; // match find_first_ruleset() retval
2898 return crush_ruleset
;
2901 bool CrushWrapper::is_valid_crush_name(const string
& s
)
2905 for (string::const_iterator p
= s
.begin(); p
!= s
.end(); ++p
) {
2909 !(*p
>= '0' && *p
<= '9') &&
2910 !(*p
>= 'A' && *p
<= 'Z') &&
2911 !(*p
>= 'a' && *p
<= 'z'))
2917 bool CrushWrapper::is_valid_crush_loc(CephContext
*cct
,
2918 const map
<string
,string
>& loc
)
2920 for (map
<string
,string
>::const_iterator l
= loc
.begin(); l
!= loc
.end(); ++l
) {
2921 if (!is_valid_crush_name(l
->first
) ||
2922 !is_valid_crush_name(l
->second
)) {
2923 ldout(cct
, 1) << "loc["
2924 << l
->first
<< "] = '"
2925 << l
->second
<< "' not a valid crush name ([A-Za-z0-9_-.]+)"
2933 int CrushWrapper::_choose_type_stack(
2935 const vector
<pair
<int,int>>& stack
,
2936 const set
<int>& overfull
,
2937 const vector
<int>& underfull
,
2938 const vector
<int>& orig
,
2939 vector
<int>::const_iterator
& i
,
2941 vector
<int> *pw
) const
2943 vector
<int> w
= *pw
;
2946 ldout(cct
, 10) << __func__
<< " stack " << stack
2952 vector
<int> cumulative_fanout(stack
.size());
2954 for (int j
= (int)stack
.size() - 1; j
>= 0; --j
) {
2955 cumulative_fanout
[j
] = f
;
2956 f
*= stack
[j
].second
;
2958 ldout(cct
, 10) << __func__
<< " cumulative_fanout " << cumulative_fanout
2961 // identify underful targets for each intermediate level.
2962 // this serves two purposes:
2963 // 1. we can tell when we are selecting a bucket that does not have any underfull
2964 // devices beneath it. that means that if the current input includes an overfull
2965 // device, we won't be able to find an underfull device with this parent to
2967 // 2. when we decide we should reject a bucket due to the above, this list gives us
2968 // a list of peers to consider that *do* have underfull devices available.. (we
2969 // are careful to pick one that has the same parent.)
2970 vector
<set
<int>> underfull_buckets
; // level -> set of buckets with >0 underfull item(s)
2971 underfull_buckets
.resize(stack
.size() - 1);
2972 for (auto osd
: underfull
) {
2974 for (int j
= (int)stack
.size() - 2; j
>= 0; --j
) {
2975 int type
= stack
[j
].first
;
2976 item
= get_parent_of_type(item
, type
);
2977 ldout(cct
, 10) << __func__
<< " underfull " << osd
<< " type " << type
2978 << " is " << item
<< dendl
;
2979 underfull_buckets
[j
].insert(item
);
2982 ldout(cct
, 20) << __func__
<< " underfull_buckets " << underfull_buckets
<< dendl
;
2984 for (unsigned j
= 0; j
< stack
.size(); ++j
) {
2985 int type
= stack
[j
].first
;
2986 int fanout
= stack
[j
].second
;
2987 int cum_fanout
= cumulative_fanout
[j
];
2988 ldout(cct
, 10) << " level " << j
<< ": type " << type
<< " fanout " << fanout
2989 << " cumulative " << cum_fanout
2990 << " w " << w
<< dendl
;
2993 for (auto from
: w
) {
2994 ldout(cct
, 10) << " from " << from
<< dendl
;
2995 // identify leaves under each choice. we use this to check whether any of these
2996 // leaves are overfull. (if so, we need to make sure there are underfull candidates
2997 // to swap for them.)
2998 vector
<set
<int>> leaves
;
2999 leaves
.resize(fanout
);
3000 for (int pos
= 0; pos
< fanout
; ++pos
) {
3003 int item
= get_parent_of_type(*tmpi
, type
);
3006 while (n
-- && tmpi
!= orig
.end()) {
3007 leaves
[pos
].insert(*tmpi
++);
3009 ldout(cct
, 10) << __func__
<< " from " << *tmpi
<< " got " << item
3010 << " of type " << type
<< " over leaves " << leaves
[pos
] << dendl
;
3013 bool replaced
= false;
3014 if (overfull
.count(*i
)) {
3015 for (auto item
: underfull
) {
3016 ldout(cct
, 10) << __func__
<< " pos " << pos
3017 << " was " << *i
<< " considering " << item
3019 if (used
.count(item
)) {
3020 ldout(cct
, 20) << __func__
<< " in used " << used
<< dendl
;
3023 if (!subtree_contains(from
, item
)) {
3024 ldout(cct
, 20) << __func__
<< " not in subtree " << from
<< dendl
;
3027 if (std::find(orig
.begin(), orig
.end(), item
) != orig
.end()) {
3028 ldout(cct
, 20) << __func__
<< " in orig " << orig
<< dendl
;
3033 ldout(cct
, 10) << __func__
<< " pos " << pos
<< " replace "
3034 << *i
<< " -> " << item
<< dendl
;
3041 ldout(cct
, 10) << __func__
<< " pos " << pos
<< " keep " << *i
3046 if (i
== orig
.end()) {
3047 ldout(cct
, 10) << __func__
<< " end of orig, break 1" << dendl
;
3052 if (j
+ 1 < stack
.size()) {
3053 // check if any buckets have overfull leaves but no underfull candidates
3054 for (int pos
= 0; pos
< fanout
; ++pos
) {
3055 if (underfull_buckets
[j
].count(o
[pos
]) == 0) {
3056 // are any leaves overfull?
3057 bool any_overfull
= false;
3058 for (auto osd
: leaves
[pos
]) {
3059 if (overfull
.count(osd
)) {
3060 any_overfull
= true;
3064 ldout(cct
, 10) << " bucket " << o
[pos
] << " has no underfull targets and "
3065 << ">0 leaves " << leaves
[pos
] << " is overfull; alts "
3066 << underfull_buckets
[j
]
3068 for (auto alt
: underfull_buckets
[j
]) {
3069 if (std::find(o
.begin(), o
.end(), alt
) == o
.end()) {
3070 // see if alt has the same parent
3072 get_parent_of_type(o
[pos
], stack
[j
-1].first
) ==
3073 get_parent_of_type(alt
, stack
[j
-1].first
)) {
3075 ldout(cct
, 10) << " replacing " << o
[pos
]
3076 << " (which has no underfull leaves) with " << alt
3078 << get_parent_of_type(alt
, stack
[j
-1].first
) << " type "
3079 << type
<< ")" << dendl
;
3081 ldout(cct
, 10) << " replacing " << o
[pos
]
3082 << " (which has no underfull leaves) with " << alt
3083 << " (first level)" << dendl
;
3087 ldout(cct
, 30) << " alt " << alt
<< " for " << o
[pos
]
3088 << " has different parent, skipping" << dendl
;
3096 if (i
== orig
.end()) {
3097 ldout(cct
, 10) << __func__
<< " end of orig, break 2" << dendl
;
3101 ldout(cct
, 10) << __func__
<< " w <- " << o
<< " was " << w
<< dendl
;
3108 int CrushWrapper::try_remap_rule(
3112 const set
<int>& overfull
,
3113 const vector
<int>& underfull
,
3114 const vector
<int>& orig
,
3115 vector
<int> *out
) const
3117 const crush_map
*map
= crush
;
3118 const crush_rule
*rule
= get_rule(ruleno
);
3121 ldout(cct
, 10) << __func__
<< " ruleno " << ruleno
3122 << " numrep " << maxout
<< " overfull " << overfull
3123 << " underfull " << underfull
<< " orig " << orig
3125 vector
<int> w
; // working set
3128 auto i
= orig
.begin();
3131 vector
<pair
<int,int>> type_stack
; // (type, fan-out)
3133 for (unsigned step
= 0; step
< rule
->len
; ++step
) {
3134 const crush_rule_step
*curstep
= &rule
->steps
[step
];
3135 ldout(cct
, 10) << __func__
<< " step " << step
<< " w " << w
<< dendl
;
3136 switch (curstep
->op
) {
3137 case CRUSH_RULE_TAKE
:
3138 if ((curstep
->arg1
>= 0 && curstep
->arg1
< map
->max_devices
) ||
3139 (-1-curstep
->arg1
>= 0 && -1-curstep
->arg1
< map
->max_buckets
&&
3140 map
->buckets
[-1-curstep
->arg1
])) {
3142 w
.push_back(curstep
->arg1
);
3143 ldout(cct
, 10) << __func__
<< " take " << w
<< dendl
;
3145 ldout(cct
, 1) << " bad take value " << curstep
->arg1
<< dendl
;
3149 case CRUSH_RULE_CHOOSELEAF_FIRSTN
:
3150 case CRUSH_RULE_CHOOSELEAF_INDEP
:
3152 int numrep
= curstep
->arg1
;
3153 int type
= curstep
->arg2
;
3156 type_stack
.push_back(make_pair(type
, numrep
));
3157 type_stack
.push_back(make_pair(0, 1));
3158 int r
= _choose_type_stack(cct
, type_stack
, overfull
, underfull
, orig
,
3166 case CRUSH_RULE_CHOOSE_FIRSTN
:
3167 case CRUSH_RULE_CHOOSE_INDEP
:
3169 int numrep
= curstep
->arg1
;
3170 int type
= curstep
->arg2
;
3173 type_stack
.push_back(make_pair(type
, numrep
));
3177 case CRUSH_RULE_EMIT
:
3178 ldout(cct
, 10) << " emit " << w
<< dendl
;
3179 if (!type_stack
.empty()) {
3180 int r
= _choose_type_stack(cct
, type_stack
, overfull
, underfull
, orig
,
3186 for (auto item
: w
) {
3187 out
->push_back(item
);
3202 int CrushWrapper::_choose_args_adjust_item_weight_in_bucket(
3204 crush_choose_arg_map cmap
,
3207 const vector
<int>& weight
,
3211 int bidx
= -1 - bucketid
;
3212 crush_bucket
*b
= crush
->buckets
[bidx
];
3213 if (bidx
>= (int)cmap
.size
) {
3215 *ss
<< "no weight-set for bucket " << b
->id
;
3216 ldout(cct
, 10) << __func__
<< " no crush_choose_arg for bucket " << b
->id
3220 crush_choose_arg
*carg
= &cmap
.args
[bidx
];
3221 if (carg
->weight_set
== NULL
) {
3223 *ss
<< "no weight-set for bucket " << b
->id
;
3224 ldout(cct
, 10) << __func__
<< " no weight_set for bucket " << b
->id
3228 if (carg
->weight_set_size
!= weight
.size()) {
3230 *ss
<< "weight_set_size != " << weight
.size() << " for bucket " << b
->id
;
3231 ldout(cct
, 10) << __func__
<< " weight_set_size != " << weight
.size()
3232 << " for bucket " << b
->id
<< dendl
;
3235 for (unsigned i
= 0; i
< b
->size
; i
++) {
3236 if (b
->items
[i
] == id
) {
3237 for (unsigned j
= 0; j
< weight
.size(); ++j
) {
3238 carg
->weight_set
[j
].weights
[i
] = weight
[j
];
3240 ldout(cct
, 5) << __func__
<< " set " << id
<< " to " << weight
3241 << " in bucket " << b
->id
<< dendl
;
3246 vector
<int> bucket_weight(weight
.size(), 0);
3247 for (unsigned i
= 0; i
< b
->size
; i
++) {
3248 for (unsigned j
= 0; j
< weight
.size(); ++j
) {
3249 bucket_weight
[j
] += carg
->weight_set
[j
].weights
[i
];
3252 choose_args_adjust_item_weight(cct
, cmap
, b
->id
, bucket_weight
, nullptr);
3257 int CrushWrapper::choose_args_adjust_item_weight(
3259 crush_choose_arg_map cmap
,
3261 const vector
<int>& weight
,
3264 ldout(cct
, 5) << __func__
<< " " << id
<< " weight " << weight
<< dendl
;
3266 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
3267 crush_bucket
*b
= crush
->buckets
[bidx
];
3271 changed
+= _choose_args_adjust_item_weight_in_bucket(
3272 cct
, cmap
, b
->id
, id
, weight
, ss
);
3276 *ss
<< "item " << id
<< " not found in crush map";