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
21 using std::ostringstream
;
27 using ceph::bufferlist
;
29 using ceph::decode_nohead
;
31 using ceph::Formatter
;
33 bool CrushWrapper::has_non_straw2_buckets() const
35 for (int i
=0; i
<crush
->max_buckets
; ++i
) {
36 crush_bucket
*b
= crush
->buckets
[i
];
39 if (b
->alg
!= CRUSH_BUCKET_STRAW2
)
45 bool CrushWrapper::has_v2_rules() const
47 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
55 bool CrushWrapper::is_v2_rule(unsigned ruleid
) const
57 // check rule for use of indep or new SET_* rule steps
58 if (ruleid
>= crush
->max_rules
)
60 crush_rule
*r
= crush
->rules
[ruleid
];
63 for (unsigned j
=0; j
<r
->len
; j
++) {
64 if (r
->steps
[j
].op
== CRUSH_RULE_CHOOSE_INDEP
||
65 r
->steps
[j
].op
== CRUSH_RULE_CHOOSELEAF_INDEP
||
66 r
->steps
[j
].op
== CRUSH_RULE_SET_CHOOSE_TRIES
||
67 r
->steps
[j
].op
== CRUSH_RULE_SET_CHOOSELEAF_TRIES
) {
74 bool CrushWrapper::has_v3_rules() const
76 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
84 bool CrushWrapper::is_v3_rule(unsigned ruleid
) const
86 // check rule for use of SET_CHOOSELEAF_VARY_R step
87 if (ruleid
>= crush
->max_rules
)
89 crush_rule
*r
= crush
->rules
[ruleid
];
92 for (unsigned j
=0; j
<r
->len
; j
++) {
93 if (r
->steps
[j
].op
== CRUSH_RULE_SET_CHOOSELEAF_VARY_R
) {
100 bool CrushWrapper::has_v4_buckets() const
102 for (int i
=0; i
<crush
->max_buckets
; ++i
) {
103 crush_bucket
*b
= crush
->buckets
[i
];
106 if (b
->alg
== CRUSH_BUCKET_STRAW2
)
112 bool CrushWrapper::has_v5_rules() const
114 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
122 bool CrushWrapper::is_v5_rule(unsigned ruleid
) const
124 // check rule for use of SET_CHOOSELEAF_STABLE step
125 if (ruleid
>= crush
->max_rules
)
127 crush_rule
*r
= crush
->rules
[ruleid
];
130 for (unsigned j
=0; j
<r
->len
; j
++) {
131 if (r
->steps
[j
].op
== CRUSH_RULE_SET_CHOOSELEAF_STABLE
) {
138 bool CrushWrapper::has_choose_args() const
140 return !choose_args
.empty();
143 bool CrushWrapper::has_incompat_choose_args() const
145 if (choose_args
.empty())
147 if (choose_args
.size() > 1)
149 if (choose_args
.begin()->first
!= DEFAULT_CHOOSE_ARGS
)
151 crush_choose_arg_map arg_map
= choose_args
.begin()->second
;
152 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
153 crush_choose_arg
*arg
= &arg_map
.args
[i
];
154 if (arg
->weight_set_positions
== 0 &&
157 if (arg
->weight_set_positions
!= 1)
159 if (arg
->ids_size
!= 0)
165 int CrushWrapper::split_id_class(int i
, int *idout
, int *classout
) const
169 string name
= get_item_name(i
);
170 size_t pos
= name
.find("~");
171 if (pos
== string::npos
) {
176 string name_no_class
= name
.substr(0, pos
);
177 if (!name_exists(name_no_class
))
179 string class_name
= name
.substr(pos
+ 1);
180 if (!class_exists(class_name
))
182 *idout
= get_item_id(name_no_class
);
183 *classout
= get_class_id(class_name
);
187 int CrushWrapper::can_rename_item(const string
& srcname
,
188 const string
& dstname
,
191 if (name_exists(srcname
)) {
192 if (name_exists(dstname
)) {
193 *ss
<< "dstname = '" << dstname
<< "' already exists";
196 if (is_valid_crush_name(dstname
)) {
199 *ss
<< "dstname = '" << dstname
<< "' does not match [-_.0-9a-zA-Z]+";
203 if (name_exists(dstname
)) {
204 *ss
<< "srcname = '" << srcname
<< "' does not exist "
205 << "and dstname = '" << dstname
<< "' already exists";
208 *ss
<< "srcname = '" << srcname
<< "' does not exist";
214 int CrushWrapper::rename_item(const string
& srcname
,
215 const string
& dstname
,
218 int ret
= can_rename_item(srcname
, dstname
, ss
);
221 int oldid
= get_item_id(srcname
);
222 return set_item_name(oldid
, dstname
);
225 int CrushWrapper::can_rename_bucket(const string
& srcname
,
226 const string
& dstname
,
229 int ret
= can_rename_item(srcname
, dstname
, ss
);
232 int srcid
= get_item_id(srcname
);
234 *ss
<< "srcname = '" << srcname
<< "' is not a bucket "
235 << "because its id = " << srcid
<< " is >= 0";
241 int CrushWrapper::rename_bucket(const string
& srcname
,
242 const string
& dstname
,
245 int ret
= can_rename_bucket(srcname
, dstname
, ss
);
248 int oldid
= get_item_id(srcname
);
249 return set_item_name(oldid
, dstname
);
252 int CrushWrapper::rename_rule(const string
& srcname
,
253 const string
& dstname
,
256 if (!rule_exists(srcname
)) {
258 *ss
<< "source rule name '" << srcname
<< "' does not exist";
262 if (rule_exists(dstname
)) {
264 *ss
<< "destination rule name '" << dstname
<< "' already exists";
268 int rule_id
= get_rule_id(srcname
);
269 auto it
= rule_name_map
.find(rule_id
);
270 ceph_assert(it
!= rule_name_map
.end());
271 it
->second
= dstname
;
273 rule_name_rmap
.erase(srcname
);
274 rule_name_rmap
[dstname
] = rule_id
;
279 void CrushWrapper::find_takes(set
<int> *roots
) const
281 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
282 crush_rule
*r
= crush
->rules
[i
];
285 for (unsigned j
=0; j
<r
->len
; j
++) {
286 if (r
->steps
[j
].op
== CRUSH_RULE_TAKE
)
287 roots
->insert(r
->steps
[j
].arg1
);
292 void CrushWrapper::find_takes_by_rule(int rule
, set
<int> *roots
) const
294 if (rule
< 0 || rule
>= (int)crush
->max_rules
)
296 crush_rule
*r
= crush
->rules
[rule
];
299 for (unsigned i
= 0; i
< r
->len
; i
++) {
300 if (r
->steps
[i
].op
== CRUSH_RULE_TAKE
)
301 roots
->insert(r
->steps
[i
].arg1
);
305 void CrushWrapper::find_roots(set
<int> *roots
) const
307 for (int i
= 0; i
< crush
->max_buckets
; i
++) {
308 if (!crush
->buckets
[i
])
310 crush_bucket
*b
= crush
->buckets
[i
];
311 if (!_search_item_exists(b
->id
))
312 roots
->insert(b
->id
);
316 bool CrushWrapper::subtree_contains(int root
, int item
) const
322 return false; // root is a leaf
324 const crush_bucket
*b
= get_bucket(root
);
328 for (unsigned j
=0; j
<b
->size
; j
++) {
329 if (subtree_contains(b
->items
[j
], item
))
335 bool CrushWrapper::_maybe_remove_last_instance(CephContext
*cct
, int item
, bool unlink_only
)
338 if (_search_item_exists(item
)) {
341 if (item
< 0 && _bucket_is_in_use(item
)) {
345 if (item
< 0 && !unlink_only
) {
346 crush_bucket
*t
= get_bucket(item
);
347 ldout(cct
, 5) << "_maybe_remove_last_instance removing bucket " << item
<< dendl
;
348 crush_remove_bucket(crush
, t
);
349 if (class_bucket
.count(item
) != 0)
350 class_bucket
.erase(item
);
351 class_remove_item(item
);
352 update_choose_args(cct
);
354 if ((item
>= 0 || !unlink_only
) && name_map
.count(item
)) {
355 ldout(cct
, 5) << "_maybe_remove_last_instance removing name for item " << item
<< dendl
;
356 name_map
.erase(item
);
358 if (item
>= 0 && !unlink_only
) {
359 class_remove_item(item
);
362 rebuild_roots_with_classes(cct
);
366 int CrushWrapper::remove_root(CephContext
*cct
, int item
)
368 crush_bucket
*b
= get_bucket(item
);
370 // should be idempotent
371 // e.g.: we use 'crush link' to link same host into
372 // different roots, which as a result can cause different
373 // shadow trees reference same hosts too. This means
374 // we may need to destory the same buckets(hosts, racks, etc.)
375 // multiple times during rebuilding all shadow trees.
379 for (unsigned n
= 0; n
< b
->size
; n
++) {
380 if (b
->items
[n
] >= 0)
382 int r
= remove_root(cct
, b
->items
[n
]);
387 crush_remove_bucket(crush
, b
);
388 if (name_map
.count(item
) != 0) {
389 name_map
.erase(item
);
392 if (class_bucket
.count(item
) != 0)
393 class_bucket
.erase(item
);
394 class_remove_item(item
);
395 update_choose_args(cct
);
399 void CrushWrapper::update_choose_args(CephContext
*cct
)
401 for (auto& i
: choose_args
) {
402 crush_choose_arg_map
&arg_map
= i
.second
;
403 assert(arg_map
.size
== (unsigned)crush
->max_buckets
);
404 unsigned positions
= get_choose_args_positions(arg_map
);
405 for (int j
= 0; j
< crush
->max_buckets
; ++j
) {
406 crush_bucket
*b
= crush
->buckets
[j
];
407 assert(j
< (int)arg_map
.size
);
408 auto& carg
= arg_map
.args
[j
];
409 // strip out choose_args for any buckets that no longer exist
410 if (!b
|| b
->alg
!= CRUSH_BUCKET_STRAW2
) {
413 ldout(cct
,10) << __func__
<< " removing " << i
.first
<< " bucket "
414 << (-1-j
) << " ids" << dendl
;
419 if (carg
.weight_set
) {
421 ldout(cct
,10) << __func__
<< " removing " << i
.first
<< " bucket "
422 << (-1-j
) << " weight_sets" << dendl
;
423 for (unsigned p
= 0; p
< carg
.weight_set_positions
; ++p
) {
424 free(carg
.weight_set
[p
].weights
);
426 free(carg
.weight_set
);
428 carg
.weight_set_positions
= 0;
432 if (carg
.weight_set_positions
== 0) {
435 if (carg
.weight_set_positions
!= positions
) {
437 lderr(cct
) << __func__
<< " " << i
.first
<< " bucket "
438 << (-1-j
) << " positions " << carg
.weight_set_positions
439 << " -> " << positions
<< dendl
;
440 continue; // wth... skip!
442 // mis-sized weight_sets? this shouldn't ever happen.
443 for (unsigned p
= 0; p
< positions
; ++p
) {
444 if (carg
.weight_set
[p
].size
!= b
->size
) {
446 lderr(cct
) << __func__
<< " fixing " << i
.first
<< " bucket "
447 << (-1-j
) << " position " << p
448 << " size " << carg
.weight_set
[p
].size
<< " -> "
450 auto old_ws
= carg
.weight_set
[p
];
451 carg
.weight_set
[p
].size
= b
->size
;
452 carg
.weight_set
[p
].weights
= (__u32
*)calloc(b
->size
, sizeof(__u32
));
453 auto max
= std::min
<unsigned>(old_ws
.size
, b
->size
);
454 for (unsigned k
= 0; k
< max
; ++k
) {
455 carg
.weight_set
[p
].weights
[k
] = old_ws
.weights
[k
];
457 free(old_ws
.weights
);
464 int CrushWrapper::remove_item(CephContext
*cct
, int item
, bool unlink_only
)
466 ldout(cct
, 5) << "remove_item " << item
467 << (unlink_only
? " unlink_only":"") << dendl
;
471 if (item
< 0 && !unlink_only
) {
472 crush_bucket
*t
= get_bucket(item
);
474 ldout(cct
, 1) << "remove_item bucket " << item
<< " does not exist"
480 ldout(cct
, 1) << "remove_item bucket " << item
<< " has " << t
->size
481 << " items, not empty" << dendl
;
484 if (_bucket_is_in_use(item
)) {
489 for (int i
= 0; i
< crush
->max_buckets
; i
++) {
490 if (!crush
->buckets
[i
])
492 crush_bucket
*b
= crush
->buckets
[i
];
494 for (unsigned i
=0; i
<b
->size
; ++i
) {
495 int id
= b
->items
[i
];
497 ldout(cct
, 5) << "remove_item removing item " << item
498 << " from bucket " << b
->id
<< dendl
;
499 adjust_item_weight_in_bucket(cct
, item
, 0, b
->id
, true);
500 bucket_remove_item(b
, item
);
506 if (_maybe_remove_last_instance(cct
, item
, unlink_only
))
512 bool CrushWrapper::_search_item_exists(int item
) const
514 for (int i
= 0; i
< crush
->max_buckets
; i
++) {
515 if (!crush
->buckets
[i
])
517 crush_bucket
*b
= crush
->buckets
[i
];
518 for (unsigned j
=0; j
<b
->size
; ++j
) {
519 if (b
->items
[j
] == item
)
526 bool CrushWrapper::_bucket_is_in_use(int item
)
528 for (auto &i
: class_bucket
)
529 for (auto &j
: i
.second
)
530 if (j
.second
== item
)
532 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
533 crush_rule
*r
= crush
->rules
[i
];
536 for (unsigned j
= 0; j
< r
->len
; ++j
) {
537 if (r
->steps
[j
].op
== CRUSH_RULE_TAKE
) {
538 int step_item
= r
->steps
[j
].arg1
;
541 int res
= split_id_class(step_item
, &original_item
, &c
);
544 if (step_item
== item
|| original_item
== item
)
552 int CrushWrapper::_remove_item_under(
553 CephContext
*cct
, int item
, int ancestor
, bool unlink_only
)
555 ldout(cct
, 5) << "_remove_item_under " << item
<< " under " << ancestor
556 << (unlink_only
? " unlink_only":"") << dendl
;
562 if (!bucket_exists(ancestor
))
567 crush_bucket
*b
= get_bucket(ancestor
);
568 for (unsigned i
=0; i
<b
->size
; ++i
) {
569 int id
= b
->items
[i
];
571 ldout(cct
, 5) << "_remove_item_under removing item " << item
572 << " from bucket " << b
->id
<< dendl
;
573 adjust_item_weight_in_bucket(cct
, item
, 0, b
->id
, true);
574 bucket_remove_item(b
, item
);
577 int r
= remove_item_under(cct
, item
, id
, unlink_only
);
585 int CrushWrapper::remove_item_under(
586 CephContext
*cct
, int item
, int ancestor
, bool unlink_only
)
588 ldout(cct
, 5) << "remove_item_under " << item
<< " under " << ancestor
589 << (unlink_only
? " unlink_only":"") << dendl
;
591 if (!unlink_only
&& _bucket_is_in_use(item
)) {
595 int ret
= _remove_item_under(cct
, item
, ancestor
, unlink_only
);
599 if (item
< 0 && !unlink_only
) {
600 crush_bucket
*t
= get_bucket(item
);
602 ldout(cct
, 1) << "remove_item_under bucket " << item
603 << " does not exist" << dendl
;
608 ldout(cct
, 1) << "remove_item_under bucket " << item
<< " has " << t
->size
609 << " items, not empty" << dendl
;
614 if (_maybe_remove_last_instance(cct
, item
, unlink_only
))
620 int CrushWrapper::get_common_ancestor_distance(CephContext
*cct
, int id
,
621 const std::multimap
<string
,string
>& loc
) const
623 ldout(cct
, 5) << __func__
<< " " << id
<< " " << loc
<< dendl
;
624 if (!item_exists(id
))
626 map
<string
,string
> id_loc
= get_full_location(id
);
627 ldout(cct
, 20) << " id is at " << id_loc
<< dendl
;
629 for (map
<int,string
>::const_iterator p
= type_map
.begin();
632 map
<string
,string
>::iterator ip
= id_loc
.find(p
->second
);
633 if (ip
== id_loc
.end())
635 for (std::multimap
<string
,string
>::const_iterator q
= loc
.find(p
->second
);
638 if (q
->first
!= p
->second
)
640 if (q
->second
== ip
->second
)
647 int CrushWrapper::parse_loc_map(const std::vector
<string
>& args
,
648 std::map
<string
,string
> *ploc
)
651 for (unsigned i
= 0; i
< args
.size(); ++i
) {
652 const char *s
= args
[i
].c_str();
653 const char *pos
= strchr(s
, '=');
656 string
key(s
, 0, pos
-s
);
659 (*ploc
)[key
] = value
;
666 int CrushWrapper::parse_loc_multimap(const std::vector
<string
>& args
,
667 std::multimap
<string
,string
> *ploc
)
670 for (unsigned i
= 0; i
< args
.size(); ++i
) {
671 const char *s
= args
[i
].c_str();
672 const char *pos
= strchr(s
, '=');
675 string
key(s
, 0, pos
-s
);
678 ploc
->insert(make_pair(key
, value
));
685 bool CrushWrapper::check_item_loc(CephContext
*cct
, int item
, const map
<string
,string
>& loc
,
688 ldout(cct
, 5) << "check_item_loc item " << item
<< " loc " << loc
<< dendl
;
690 for (map
<int,string
>::const_iterator p
= type_map
.begin(); p
!= type_map
.end(); ++p
) {
695 // ignore types that aren't specified in loc
696 map
<string
,string
>::const_iterator q
= loc
.find(p
->second
);
697 if (q
== loc
.end()) {
698 ldout(cct
, 2) << "warning: did not specify location for '" << p
->second
<< "' level (levels are "
699 << type_map
<< ")" << dendl
;
703 if (!name_exists(q
->second
)) {
704 ldout(cct
, 5) << "check_item_loc bucket " << q
->second
<< " dne" << dendl
;
708 int id
= get_item_id(q
->second
);
710 ldout(cct
, 5) << "check_item_loc requested " << q
->second
<< " for type " << p
->second
711 << " is a device, not bucket" << dendl
;
715 ceph_assert(bucket_exists(id
));
716 crush_bucket
*b
= get_bucket(id
);
718 // see if item exists in this bucket
719 for (unsigned j
=0; j
<b
->size
; j
++) {
720 if (b
->items
[j
] == item
) {
721 ldout(cct
, 2) << "check_item_loc " << item
<< " exists in bucket " << b
->id
<< dendl
;
723 *weight
= crush_get_bucket_item_weight(b
, j
);
730 ldout(cct
, 2) << __func__
<< " item " << item
<< " loc " << loc
<< dendl
;
734 map
<string
, string
> CrushWrapper::get_full_location(int id
) const
736 vector
<pair
<string
, string
> > full_location_ordered
;
737 map
<string
,string
> full_location
;
739 get_full_location_ordered(id
, full_location_ordered
);
741 std::copy(full_location_ordered
.begin(),
742 full_location_ordered
.end(),
743 std::inserter(full_location
, full_location
.begin()));
745 return full_location
;
748 int CrushWrapper::get_full_location(const string
& name
,
749 map
<string
,string
> *ploc
)
752 auto p
= name_rmap
.find(name
);
753 if (p
== name_rmap
.end()) {
756 *ploc
= get_full_location(p
->second
);
760 int CrushWrapper::get_full_location_ordered(int id
, vector
<pair
<string
, string
> >& path
) const
762 if (!item_exists(id
))
767 pair
<string
, string
> parent_coord
= get_immediate_parent(cur
, &ret
);
770 path
.push_back(parent_coord
);
771 cur
= get_item_id(parent_coord
.second
);
776 string
CrushWrapper::get_full_location_ordered_string(int id
) const
778 vector
<pair
<string
, string
> > full_location_ordered
;
779 string full_location
;
780 get_full_location_ordered(id
, full_location_ordered
);
781 reverse(begin(full_location_ordered
), end(full_location_ordered
));
782 for(auto i
= full_location_ordered
.begin(); i
!= full_location_ordered
.end(); i
++) {
783 full_location
= full_location
+ i
->first
+ "=" + i
->second
;
784 if (i
!= full_location_ordered
.end() - 1) {
785 full_location
= full_location
+ ",";
788 return full_location
;
791 map
<int, string
> CrushWrapper::get_parent_hierarchy(int id
) const
793 map
<int,string
> parent_hierarchy
;
794 pair
<string
, string
> parent_coord
= get_immediate_parent(id
);
797 // get the integer type for id and create a counter from there
798 int type_counter
= get_bucket_type(id
);
800 // if we get a negative type then we can assume that we have an OSD
801 // change behavior in get_item_type FIXME
802 if (type_counter
< 0)
805 // read the type map and get the name of the type with the largest ID
807 if (!type_map
.empty())
808 high_type
= type_map
.rbegin()->first
;
810 parent_id
= get_item_id(parent_coord
.second
);
812 while (type_counter
< high_type
) {
814 parent_hierarchy
[ type_counter
] = parent_coord
.first
;
816 if (type_counter
< high_type
){
817 // get the coordinate information for the next parent
818 parent_coord
= get_immediate_parent(parent_id
);
819 parent_id
= get_item_id(parent_coord
.second
);
823 return parent_hierarchy
;
826 int CrushWrapper::get_children(int id
, list
<int> *children
) const
833 auto *b
= get_bucket(id
);
838 for (unsigned n
=0; n
<b
->size
; n
++) {
839 children
->push_back(b
->items
[n
]);
844 int CrushWrapper::get_all_children(int id
, set
<int> *children
) const
851 auto *b
= get_bucket(id
);
857 for (unsigned n
= 0; n
< b
->size
; n
++) {
858 children
->insert(b
->items
[n
]);
860 auto r
= get_all_children(b
->items
[n
], children
);
868 void CrushWrapper::get_children_of_type(int id
,
870 vector
<int> *children
,
871 bool exclude_shadow
) const
876 children
->push_back(id
);
880 auto b
= get_bucket(id
);
884 if (b
->type
< type
) {
887 } else if (b
->type
== type
) {
888 if (!is_shadow_item(b
->id
) || !exclude_shadow
) {
889 children
->push_back(b
->id
);
893 for (unsigned n
= 0; n
< b
->size
; n
++) {
894 get_children_of_type(b
->items
[n
], type
, children
, exclude_shadow
);
898 int CrushWrapper::verify_upmap(CephContext
*cct
,
901 const vector
<int>& up
)
903 auto rule
= get_rule(rule_id
);
904 if (IS_ERR(rule
) || !rule
) {
905 lderr(cct
) << __func__
<< " rule " << rule_id
<< " does not exist"
911 std::map
<int, int> type_stack
;
912 for (unsigned step
= 0; step
< rule
->len
; ++step
) {
913 auto curstep
= &rule
->steps
[step
];
914 ldout(cct
, 10) << __func__
<< " step " << step
<< dendl
;
915 switch (curstep
->op
) {
916 case CRUSH_RULE_TAKE
:
918 root_bucket
= curstep
->arg1
;
921 case CRUSH_RULE_CHOOSELEAF_FIRSTN
:
922 case CRUSH_RULE_CHOOSELEAF_INDEP
:
924 int numrep
= curstep
->arg1
;
925 int type
= curstep
->arg2
;
928 type_stack
.emplace(type
, numrep
);
929 if (type
== 0) // osd
931 map
<int, set
<int>> osds_by_parent
; // parent_of_desired_type -> osds
932 for (auto osd
: up
) {
933 auto parent
= get_parent_of_type(osd
, type
, rule_id
);
935 osds_by_parent
[parent
].insert(osd
);
937 ldout(cct
, 1) << __func__
<< " unable to get parent of osd." << osd
938 << ", skipping for now"
942 for (auto i
: osds_by_parent
) {
943 if (i
.second
.size() > 1) {
944 lderr(cct
) << __func__
<< " multiple osds " << i
.second
945 << " come from same failure domain " << i
.first
953 case CRUSH_RULE_CHOOSE_FIRSTN
:
954 case CRUSH_RULE_CHOOSE_INDEP
:
956 int numrep
= curstep
->arg1
;
957 int type
= curstep
->arg2
;
960 type_stack
.emplace(type
, numrep
);
961 if (type
== 0) // osd
963 set
<int> parents_of_type
;
964 for (auto osd
: up
) {
965 auto parent
= get_parent_of_type(osd
, type
, rule_id
);
967 parents_of_type
.insert(parent
);
969 ldout(cct
, 1) << __func__
<< " unable to get parent of osd." << osd
970 << ", skipping for now"
974 if ((int)parents_of_type
.size() > numrep
) {
975 lderr(cct
) << __func__
<< " number of buckets "
976 << parents_of_type
.size() << " exceeds desired " << numrep
983 case CRUSH_RULE_EMIT
:
985 if (root_bucket
< 0) {
987 for (auto &item
: type_stack
) {
988 num_osds
*= item
.second
;
990 // validate the osd's in subtree
991 for (int c
= 0; cursor
< (int)up
.size() && c
< num_osds
; ++cursor
, ++c
) {
992 int osd
= up
[cursor
];
993 if (!subtree_contains(root_bucket
, osd
)) {
994 lderr(cct
) << __func__
<< " osd " << osd
<< " not in bucket " << root_bucket
<< dendl
;
1011 int CrushWrapper::_get_leaves(int id
, list
<int> *leaves
) const
1013 ceph_assert(leaves
);
1017 leaves
->push_back(id
);
1021 auto b
= get_bucket(id
);
1026 for (unsigned n
= 0; n
< b
->size
; n
++) {
1027 if (b
->items
[n
] >= 0) {
1028 leaves
->push_back(b
->items
[n
]);
1030 // is a bucket, do recursive call
1031 int r
= _get_leaves(b
->items
[n
], leaves
);
1038 return 0; // all is well
1041 int CrushWrapper::get_leaves(const string
&name
, set
<int> *leaves
) const
1043 ceph_assert(leaves
);
1046 if (!name_exists(name
)) {
1050 int id
= get_item_id(name
);
1057 list
<int> unordered
;
1058 int r
= _get_leaves(id
, &unordered
);
1063 for (auto &p
: unordered
) {
1070 int CrushWrapper::insert_item(
1071 CephContext
*cct
, int item
, float weight
, string name
,
1072 const map
<string
,string
>& loc
, // typename -> bucketname
1073 bool init_weight_sets
)
1075 ldout(cct
, 5) << "insert_item item " << item
<< " weight " << weight
1076 << " name " << name
<< " loc " << loc
<< dendl
;
1078 if (!is_valid_crush_name(name
))
1081 if (!is_valid_crush_loc(cct
, loc
))
1084 int r
= validate_weightf(weight
);
1089 if (name_exists(name
)) {
1090 if (get_item_id(name
) != item
) {
1091 ldout(cct
, 10) << "device name '" << name
<< "' already exists as id "
1092 << get_item_id(name
) << dendl
;
1096 set_item_name(item
, name
);
1101 // 1. create locations if locations don't exist
1102 // 2. add child in the location with 0 weight.
1103 // Check more detail of insert_item method declared in
1105 for (auto p
= type_map
.begin(); p
!= type_map
.end(); ++p
) {
1106 // ignore device type
1110 // skip types that are unspecified
1111 map
<string
,string
>::const_iterator q
= loc
.find(p
->second
);
1112 if (q
== loc
.end()) {
1113 ldout(cct
, 2) << "warning: did not specify location for '"
1114 << p
->second
<< "' level (levels are "
1115 << type_map
<< ")" << dendl
;
1119 if (!name_exists(q
->second
)) {
1120 ldout(cct
, 5) << "insert_item creating bucket " << q
->second
<< dendl
;
1121 int zero_weight
= 0, new_bucket_id
;
1122 int r
= add_bucket(0, 0,
1123 CRUSH_HASH_DEFAULT
, p
->first
, 1, &cur
, &zero_weight
, &new_bucket_id
);
1125 ldout(cct
, 1) << "add_bucket failure error: " << cpp_strerror(r
)
1129 set_item_name(new_bucket_id
, q
->second
);
1131 cur
= new_bucket_id
;
1135 // add to an existing bucket
1136 int id
= get_item_id(q
->second
);
1137 if (!bucket_exists(id
)) {
1138 ldout(cct
, 1) << "insert_item doesn't have bucket " << id
<< dendl
;
1142 // check that we aren't creating a cycle.
1143 if (subtree_contains(id
, cur
)) {
1144 ldout(cct
, 1) << "insert_item item " << cur
<< " already exists beneath "
1149 // we have done sanity check above
1150 crush_bucket
*b
= get_bucket(id
);
1152 if (p
->first
!= b
->type
) {
1153 ldout(cct
, 1) << "insert_item existing bucket has type "
1154 << "'" << type_map
[b
->type
] << "' != "
1155 << "'" << type_map
[p
->first
] << "'" << dendl
;
1159 // are we forming a loop?
1160 if (subtree_contains(cur
, b
->id
)) {
1161 ldout(cct
, 1) << "insert_item " << cur
<< " already contains " << b
->id
1162 << "; cannot form loop" << dendl
;
1166 ldout(cct
, 5) << "insert_item adding " << cur
<< " weight " << weight
1167 << " to bucket " << id
<< dendl
;
1168 [[maybe_unused
]] int r
= bucket_add_item(b
, cur
, 0);
1173 // adjust the item's weight in location
1174 if (adjust_item_weightf_in_loc(cct
, item
, weight
, loc
,
1175 item
>= 0 && init_weight_sets
) > 0) {
1176 if (item
>= crush
->max_devices
) {
1177 crush
->max_devices
= item
+ 1;
1178 ldout(cct
, 5) << "insert_item max_devices now " << crush
->max_devices
1181 r
= rebuild_roots_with_classes(cct
);
1183 ldout(cct
, 0) << __func__
<< " unable to rebuild roots with classes: "
1184 << cpp_strerror(r
) << dendl
;
1190 ldout(cct
, 1) << "error: didn't find anywhere to add item " << item
1191 << " in " << loc
<< dendl
;
1196 int CrushWrapper::move_bucket(
1197 CephContext
*cct
, int id
, const map
<string
,string
>& loc
)
1199 // sorry this only works for buckets
1203 if (!item_exists(id
))
1206 // get the name of the bucket we are trying to move for later
1207 string id_name
= get_item_name(id
);
1209 // detach the bucket
1210 int bucket_weight
= detach_bucket(cct
, id
);
1212 // insert the bucket back into the hierarchy
1213 return insert_item(cct
, id
, bucket_weight
/ (float)0x10000, id_name
, loc
,
1217 int CrushWrapper::detach_bucket(CephContext
*cct
, int item
)
1225 // check that the bucket that we want to detach exists
1226 ceph_assert(bucket_exists(item
));
1228 // get the bucket's weight
1229 crush_bucket
*b
= get_bucket(item
);
1230 unsigned bucket_weight
= b
->weight
;
1232 // get where the bucket is located
1233 pair
<string
, string
> bucket_location
= get_immediate_parent(item
);
1235 // get the id of the parent bucket
1236 int parent_id
= get_item_id(bucket_location
.second
);
1238 // get the parent bucket
1239 crush_bucket
*parent_bucket
= get_bucket(parent_id
);
1241 if (!IS_ERR(parent_bucket
)) {
1242 // zero out the bucket weight
1243 adjust_item_weight_in_bucket(cct
, item
, 0, parent_bucket
->id
, true);
1245 // remove the bucket from the parent
1246 bucket_remove_item(parent_bucket
, item
);
1247 } else if (PTR_ERR(parent_bucket
) != -ENOENT
) {
1248 return PTR_ERR(parent_bucket
);
1251 // check that we're happy
1252 int test_weight
= 0;
1253 map
<string
,string
> test_location
;
1254 test_location
[ bucket_location
.first
] = (bucket_location
.second
);
1256 bool successful_detach
= !(check_item_loc(cct
, item
, test_location
,
1258 ceph_assert(successful_detach
);
1259 ceph_assert(test_weight
== 0);
1261 return bucket_weight
;
1264 bool CrushWrapper::is_parent_of(int child
, int p
) const
1267 while (!get_immediate_parent_id(child
, &parent
)) {
1276 int CrushWrapper::swap_bucket(CephContext
*cct
, int src
, int dst
)
1278 if (src
>= 0 || dst
>= 0)
1280 if (!item_exists(src
) || !item_exists(dst
))
1282 crush_bucket
*a
= get_bucket(src
);
1283 crush_bucket
*b
= get_bucket(dst
);
1284 if (is_parent_of(a
->id
, b
->id
) || is_parent_of(b
->id
, a
->id
)) {
1287 unsigned aw
= a
->weight
;
1288 unsigned bw
= b
->weight
;
1291 adjust_item_weight(cct
, a
->id
, bw
);
1292 adjust_item_weight(cct
, b
->id
, aw
);
1295 map
<int,unsigned> tmp
;
1296 unsigned as
= a
->size
;
1297 unsigned bs
= b
->size
;
1298 for (unsigned i
= 0; i
< as
; ++i
) {
1299 int item
= a
->items
[0];
1300 int itemw
= crush_get_bucket_item_weight(a
, 0);
1302 bucket_remove_item(a
, item
);
1304 ceph_assert(a
->size
== 0);
1305 ceph_assert(b
->size
== bs
);
1306 for (unsigned i
= 0; i
< bs
; ++i
) {
1307 int item
= b
->items
[0];
1308 int itemw
= crush_get_bucket_item_weight(b
, 0);
1309 bucket_remove_item(b
, item
);
1310 bucket_add_item(a
, item
, itemw
);
1312 ceph_assert(a
->size
== bs
);
1313 ceph_assert(b
->size
== 0);
1314 for (auto t
: tmp
) {
1315 bucket_add_item(b
, t
.first
, t
.second
);
1317 ceph_assert(a
->size
== bs
);
1318 ceph_assert(b
->size
== as
);
1321 swap_names(src
, dst
);
1322 return rebuild_roots_with_classes(cct
);
1325 int CrushWrapper::link_bucket(
1326 CephContext
*cct
, int id
, const map
<string
,string
>& loc
)
1328 // sorry this only works for buckets
1332 if (!item_exists(id
))
1335 // get the name of the bucket we are trying to move for later
1336 string id_name
= get_item_name(id
);
1338 crush_bucket
*b
= get_bucket(id
);
1339 unsigned bucket_weight
= b
->weight
;
1341 return insert_item(cct
, id
, bucket_weight
/ (float)0x10000, id_name
, loc
);
1344 int CrushWrapper::create_or_move_item(
1345 CephContext
*cct
, int item
, float weight
, string name
,
1346 const map
<string
,string
>& loc
, // typename -> bucketname
1347 bool init_weight_sets
)
1352 if (!is_valid_crush_name(name
))
1355 if (check_item_loc(cct
, item
, loc
, &old_iweight
)) {
1356 ldout(cct
, 5) << "create_or_move_item " << item
<< " already at " << loc
1359 if (_search_item_exists(item
)) {
1360 weight
= get_item_weightf(item
);
1361 ldout(cct
, 10) << "create_or_move_item " << item
1362 << " exists with weight " << weight
<< dendl
;
1363 remove_item(cct
, item
, true);
1365 ldout(cct
, 5) << "create_or_move_item adding " << item
1366 << " weight " << weight
1367 << " at " << loc
<< dendl
;
1368 ret
= insert_item(cct
, item
, weight
, name
, loc
,
1369 item
>= 0 && init_weight_sets
);
1376 int CrushWrapper::update_item(
1377 CephContext
*cct
, int item
, float weight
, string name
,
1378 const map
<string
,string
>& loc
) // typename -> bucketname
1380 ldout(cct
, 5) << "update_item item " << item
<< " weight " << weight
1381 << " name " << name
<< " loc " << loc
<< dendl
;
1384 if (!is_valid_crush_name(name
))
1387 if (!is_valid_crush_loc(cct
, loc
))
1390 ret
= validate_weightf(weight
);
1395 // compare quantized (fixed-point integer) weights!
1396 int iweight
= (int)(weight
* (float)0x10000);
1398 if (check_item_loc(cct
, item
, loc
, &old_iweight
)) {
1399 ldout(cct
, 5) << "update_item " << item
<< " already at " << loc
<< dendl
;
1400 if (old_iweight
!= iweight
) {
1401 ldout(cct
, 5) << "update_item " << item
<< " adjusting weight "
1402 << ((float)old_iweight
/(float)0x10000) << " -> " << weight
1404 adjust_item_weight_in_loc(cct
, item
, iweight
, loc
);
1405 ret
= rebuild_roots_with_classes(cct
);
1407 ldout(cct
, 0) << __func__
<< " unable to rebuild roots with classes: "
1408 << cpp_strerror(ret
) << dendl
;
1413 if (get_item_name(item
) != name
) {
1414 ldout(cct
, 5) << "update_item setting " << item
<< " name to " << name
1416 set_item_name(item
, name
);
1420 if (item_exists(item
)) {
1421 remove_item(cct
, item
, true);
1423 ldout(cct
, 5) << "update_item adding " << item
<< " weight " << weight
1424 << " at " << loc
<< dendl
;
1425 ret
= insert_item(cct
, item
, weight
, name
, loc
);
1432 int CrushWrapper::get_item_weight(int id
) const
1434 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1435 crush_bucket
*b
= crush
->buckets
[bidx
];
1440 for (unsigned i
= 0; i
< b
->size
; i
++)
1441 if (b
->items
[i
] == id
)
1442 return crush_get_bucket_item_weight(b
, i
);
1447 int CrushWrapper::get_item_weight_in_loc(int id
, const map
<string
,string
> &loc
)
1449 for (map
<string
,string
>::const_iterator l
= loc
.begin(); l
!= loc
.end(); ++l
) {
1451 int bid
= get_item_id(l
->second
);
1452 if (!bucket_exists(bid
))
1454 crush_bucket
*b
= get_bucket(bid
);
1455 for (unsigned int i
= 0; i
< b
->size
; i
++) {
1456 if (b
->items
[i
] == id
) {
1457 return crush_get_bucket_item_weight(b
, i
);
1464 int CrushWrapper::adjust_item_weight(CephContext
*cct
, int id
, int weight
,
1465 bool update_weight_sets
)
1467 ldout(cct
, 5) << __func__
<< " " << id
<< " weight " << weight
1468 << " update_weight_sets=" << (int)update_weight_sets
1471 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1472 if (!crush
->buckets
[bidx
]) {
1475 int r
= adjust_item_weight_in_bucket(cct
, id
, weight
, -1-bidx
,
1476 update_weight_sets
);
1487 int CrushWrapper::adjust_item_weight_in_bucket(
1488 CephContext
*cct
, int id
, int weight
,
1490 bool update_weight_sets
)
1492 ldout(cct
, 5) << __func__
<< " " << id
<< " weight " << weight
1493 << " in bucket " << bucket_id
1494 << " update_weight_sets=" << (int)update_weight_sets
1497 if (!bucket_exists(bucket_id
)) {
1500 crush_bucket
*b
= get_bucket(bucket_id
);
1501 for (unsigned int i
= 0; i
< b
->size
; i
++) {
1502 if (b
->items
[i
] == id
) {
1503 int diff
= bucket_adjust_item_weight(cct
, b
, id
, weight
,
1504 update_weight_sets
);
1505 ldout(cct
, 5) << __func__
<< " " << id
<< " diff " << diff
1506 << " in bucket " << bucket_id
<< dendl
;
1507 adjust_item_weight(cct
, bucket_id
, b
->weight
, false);
1511 // update weight-sets so they continue to sum
1512 for (auto& p
: choose_args
) {
1513 auto &cmap
= p
.second
;
1517 crush_choose_arg
*arg
= &cmap
.args
[-1 - bucket_id
];
1518 if (!arg
->weight_set
) {
1521 ceph_assert(arg
->weight_set_positions
> 0);
1522 vector
<int> w(arg
->weight_set_positions
);
1523 for (unsigned i
= 0; i
< b
->size
; ++i
) {
1524 for (unsigned j
= 0; j
< arg
->weight_set_positions
; ++j
) {
1525 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
1526 w
[j
] += weight_set
->weights
[i
];
1529 ldout(cct
,5) << __func__
<< " adjusting bucket " << bucket_id
1530 << " cmap " << p
.first
<< " weights to " << w
<< dendl
;
1532 choose_args_adjust_item_weight(cct
, cmap
, bucket_id
, w
, &ss
);
1540 int CrushWrapper::adjust_item_weight_in_loc(
1541 CephContext
*cct
, int id
, int weight
,
1542 const map
<string
,string
>& loc
,
1543 bool update_weight_sets
)
1545 ldout(cct
, 5) << "adjust_item_weight_in_loc " << id
<< " weight " << weight
1547 << " update_weight_sets=" << (int)update_weight_sets
1550 for (auto l
= loc
.begin(); l
!= loc
.end(); ++l
) {
1551 int bid
= get_item_id(l
->second
);
1552 if (!bucket_exists(bid
))
1554 int r
= adjust_item_weight_in_bucket(cct
, id
, weight
, bid
,
1555 update_weight_sets
);
1566 int CrushWrapper::adjust_subtree_weight(CephContext
*cct
, int id
, int weight
,
1567 bool update_weight_sets
)
1569 ldout(cct
, 5) << __func__
<< " " << id
<< " weight " << weight
<< dendl
;
1570 crush_bucket
*b
= get_bucket(id
);
1574 list
<crush_bucket
*> q
;
1576 while (!q
.empty()) {
1579 int local_changed
= 0;
1580 for (unsigned i
=0; i
<b
->size
; ++i
) {
1581 int n
= b
->items
[i
];
1583 adjust_item_weight_in_bucket(cct
, n
, weight
, b
->id
, update_weight_sets
);
1587 crush_bucket
*sub
= get_bucket(n
);
1594 int ret
= rebuild_roots_with_classes(cct
);
1596 ldout(cct
, 0) << __func__
<< " unable to rebuild roots with classes: "
1597 << cpp_strerror(ret
) << dendl
;
1603 bool CrushWrapper::check_item_present(int id
) const
1607 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1608 crush_bucket
*b
= crush
->buckets
[bidx
];
1611 for (unsigned i
= 0; i
< b
->size
; i
++)
1612 if (b
->items
[i
] == id
)
1619 pair
<string
,string
> CrushWrapper::get_immediate_parent(int id
, int *_ret
) const
1622 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1623 crush_bucket
*b
= crush
->buckets
[bidx
];
1626 if (is_shadow_item(b
->id
))
1628 for (unsigned i
= 0; i
< b
->size
; i
++)
1629 if (b
->items
[i
] == id
) {
1630 string parent_id
= name_map
.at(b
->id
);
1631 string parent_bucket_type
= type_map
.at(b
->type
);
1634 return make_pair(parent_bucket_type
, parent_id
);
1641 return pair
<string
, string
>();
1644 int CrushWrapper::get_immediate_parent_id(int id
, int *parent
) const
1646 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
1647 crush_bucket
*b
= crush
->buckets
[bidx
];
1650 if (is_shadow_item(b
->id
))
1652 for (unsigned i
= 0; i
< b
->size
; i
++) {
1653 if (b
->items
[i
] == id
) {
1662 int CrushWrapper::get_parent_of_type(int item
, int type
, int rule
) const
1665 // no rule specified
1667 int r
= get_immediate_parent_id(item
, &item
);
1671 } while (get_bucket_type(item
) != type
);
1675 find_takes_by_rule(rule
, &roots
);
1676 for (auto root
: roots
) {
1677 vector
<int> candidates
;
1678 get_children_of_type(root
, type
, &candidates
, false);
1679 for (auto candidate
: candidates
) {
1680 if (subtree_contains(candidate
, item
)) {
1681 // note that here we assure that no two different buckets
1682 // from a single crush rule will share a same device,
1683 // which should generally be true.
1688 return 0; // not found
1691 void CrushWrapper::get_subtree_of_type(int type
, vector
<int> *subtrees
)
1695 for (auto r
: roots
) {
1696 crush_bucket
*b
= get_bucket(r
);
1699 get_children_of_type(b
->id
, type
, subtrees
);
1703 bool CrushWrapper::class_is_in_use(int class_id
, ostream
*ss
)
1705 list
<unsigned> rules
;
1706 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
1707 crush_rule
*r
= crush
->rules
[i
];
1710 for (unsigned j
= 0; j
< r
->len
; ++j
) {
1711 if (r
->steps
[j
].op
== CRUSH_RULE_TAKE
) {
1712 int root
= r
->steps
[j
].arg1
;
1713 for (auto &p
: class_bucket
) {
1715 if (q
.count(class_id
) && q
[class_id
] == root
) {
1722 if (rules
.empty()) {
1727 for (auto &p
: rules
) {
1728 os
<< "'" << get_rule_name(p
) <<"',";
1730 string
out(os
.str());
1731 out
.resize(out
.size() - 1); // drop last ','
1732 *ss
<< "still referenced by crush_rule(s): " << out
;
1737 int CrushWrapper::rename_class(const string
& srcname
, const string
& dstname
)
1739 auto i
= class_rname
.find(srcname
);
1740 if (i
== class_rname
.end())
1742 auto j
= class_rname
.find(dstname
);
1743 if (j
!= class_rname
.end())
1746 int class_id
= i
->second
;
1747 ceph_assert(class_name
.count(class_id
));
1748 // rename any shadow buckets of old class name
1749 for (auto &it
: class_map
) {
1750 if (it
.first
< 0 && it
.second
== class_id
) {
1751 string old_name
= get_item_name(it
.first
);
1752 size_t pos
= old_name
.find("~");
1753 ceph_assert(pos
!= string::npos
);
1754 string name_no_class
= old_name
.substr(0, pos
);
1755 string old_class_name
= old_name
.substr(pos
+ 1);
1756 ceph_assert(old_class_name
== srcname
);
1757 string new_name
= name_no_class
+ "~" + dstname
;
1758 // we do not use set_item_name
1759 // because the name is intentionally invalid
1760 name_map
[it
.first
] = new_name
;
1766 class_rname
.erase(srcname
);
1767 class_name
.erase(class_id
);
1768 class_rname
[dstname
] = class_id
;
1769 class_name
[class_id
] = dstname
;
1773 int CrushWrapper::populate_classes(
1774 const std::map
<int32_t, map
<int32_t, int32_t>>& old_class_bucket
)
1776 // build set of previous used shadow ids
1777 set
<int32_t> used_ids
;
1778 for (auto& p
: old_class_bucket
) {
1779 for (auto& q
: p
.second
) {
1780 used_ids
.insert(q
.second
);
1783 // accumulate weight values for each carg and bucket as we go. because it is
1784 // depth first, we will have the nested bucket weights we need when we
1785 // finish constructing the containing buckets.
1786 map
<int,map
<int,vector
<int>>> cmap_item_weight
; // cargs -> bno -> [bucket weight for each position]
1788 find_nonshadow_roots(&roots
);
1789 for (auto &r
: roots
) {
1791 for (auto &c
: class_name
) {
1793 int res
= device_class_clone(r
, c
.first
, old_class_bucket
, used_ids
,
1794 &clone
, &cmap_item_weight
);
1802 int CrushWrapper::trim_roots_with_class(CephContext
*cct
)
1805 find_shadow_roots(&roots
);
1806 for (auto &r
: roots
) {
1809 int res
= remove_root(cct
, r
);
1813 // there is no need to reweight because we only remove from the
1818 int32_t CrushWrapper::_alloc_class_id() const {
1819 if (class_name
.empty()) {
1822 int32_t class_id
= class_name
.rbegin()->first
+ 1;
1823 if (class_id
>= 0) {
1826 // wrapped, pick a random start and do exhaustive search
1827 uint32_t upperlimit
= std::numeric_limits
<int32_t>::max();
1829 class_id
= rand() % upperlimit
;
1830 const auto start
= class_id
;
1832 if (!class_name
.count(class_id
)) {
1840 } while (class_id
!= start
);
1841 ceph_abort_msg("no available class id");
1844 int CrushWrapper::set_subtree_class(
1845 const string
& subtree
,
1846 const string
& new_class
)
1848 if (!name_exists(subtree
)) {
1852 int new_class_id
= get_or_create_class_id(new_class
);
1853 int id
= get_item_id(subtree
);
1854 list
<int> q
= { id
};
1855 while (!q
.empty()) {
1858 crush_bucket
*b
= get_bucket(id
);
1862 for (unsigned i
= 0; i
< b
->size
; ++i
) {
1863 int item
= b
->items
[i
];
1865 class_map
[item
] = new_class_id
;
1874 int CrushWrapper::reclassify(
1877 const map
<string
,string
>& classify_root
,
1878 const map
<string
,pair
<string
,string
>>& classify_bucket
1881 map
<int,string
> reclassified_bucket
; // orig_id -> class
1884 for (auto& i
: classify_root
) {
1885 string root
= i
.first
;
1886 if (!name_exists(root
)) {
1887 out
<< "root " << root
<< " does not exist" << std::endl
;
1890 int root_id
= get_item_id(root
);
1891 string new_class
= i
.second
;
1892 int new_class_id
= get_or_create_class_id(new_class
);
1893 out
<< "classify_root " << root
<< " (" << root_id
1894 << ") as " << new_class
<< std::endl
;
1897 for (unsigned j
= 0; j
< crush
->max_rules
; j
++) {
1898 if (crush
->rules
[j
]) {
1899 auto rule
= crush
->rules
[j
];
1900 for (unsigned k
= 0; k
< rule
->len
; ++k
) {
1901 if (rule
->steps
[k
].op
== CRUSH_RULE_TAKE
) {
1902 int step_item
= get_rule_arg1(j
, k
);
1905 int res
= split_id_class(step_item
, &original_item
, &c
);
1909 if (original_item
== root_id
) {
1910 out
<< " rule " << j
<< " includes take on root "
1911 << root
<< " class " << c
<< std::endl
;
1920 // rebuild new buckets for root
1921 //cout << "before class_bucket: " << class_bucket << std::endl;
1922 map
<int,int> renumber
;
1924 q
.push_back(root_id
);
1925 while (!q
.empty()) {
1928 crush_bucket
*bucket
= get_bucket(id
);
1929 if (IS_ERR(bucket
)) {
1930 out
<< "cannot find bucket " << id
1931 << ": " << cpp_strerror(PTR_ERR(bucket
)) << std::endl
;
1932 return PTR_ERR(bucket
);
1936 int new_id
= get_new_bucket_id();
1937 out
<< " renumbering bucket " << id
<< " -> " << new_id
<< std::endl
;
1938 renumber
[id
] = new_id
;
1939 crush
->buckets
[-1-new_id
] = bucket
;
1940 bucket
->id
= new_id
;
1941 crush
->buckets
[-1-id
] = crush_make_bucket(crush
,
1946 crush
->buckets
[-1-id
]->id
= id
;
1947 for (auto& i
: choose_args
) {
1948 i
.second
.args
[-1-new_id
] = i
.second
.args
[-1-id
];
1949 memset(&i
.second
.args
[-1-id
], 0, sizeof(i
.second
.args
[0]));
1951 class_bucket
.erase(id
);
1952 class_bucket
[new_id
][new_class_id
] = id
;
1953 name_map
[new_id
] = string(get_item_name(id
));
1954 name_map
[id
] = string(get_item_name(id
)) + "~" + new_class
;
1956 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
1957 if (bucket
->items
[j
] < 0) {
1958 q
.push_front(bucket
->items
[j
]);
1960 // we don't reclassify the device here; if the users wants that,
1961 // they can pass --set-subtree-class separately.
1965 //cout << "mid class_bucket: " << class_bucket << std::endl;
1967 for (int i
= 0; i
< crush
->max_buckets
; ++i
) {
1968 crush_bucket
*b
= crush
->buckets
[i
];
1972 for (unsigned j
= 0; j
< b
->size
; ++j
) {
1973 if (renumber
.count(b
->items
[j
])) {
1974 b
->items
[j
] = renumber
[b
->items
[j
]];
1979 int r
= rebuild_roots_with_classes(cct
);
1981 out
<< "failed to rebuild_roots_with_classes: " << cpp_strerror(r
)
1985 //cout << "final class_bucket: " << class_bucket << std::endl;
1989 map
<int,int> send_to
; // source bucket -> dest bucket
1990 map
<int,map
<int,int>> new_class_bucket
;
1991 map
<int,string
> new_bucket_names
;
1992 map
<int,map
<string
,string
>> new_buckets
;
1993 map
<string
,int> new_bucket_by_name
;
1994 for (auto& i
: classify_bucket
) {
1995 const string
& match
= i
.first
; // prefix% or %suffix
1996 const string
& new_class
= i
.second
.first
;
1997 const string
& default_parent
= i
.second
.second
;
1998 if (!name_exists(default_parent
)) {
1999 out
<< "default parent " << default_parent
<< " does not exist"
2003 int default_parent_id
= get_item_id(default_parent
);
2004 crush_bucket
*default_parent_bucket
= get_bucket(default_parent_id
);
2005 assert(default_parent_bucket
);
2006 string default_parent_type_name
= get_type_name(default_parent_bucket
->type
);
2008 out
<< "classify_bucket " << match
<< " as " << new_class
2009 << " default bucket " << default_parent
2010 << " (" << default_parent_type_name
<< ")" << std::endl
;
2012 int new_class_id
= get_or_create_class_id(new_class
);
2013 for (int j
= 0; j
< crush
->max_buckets
; ++j
) {
2014 crush_bucket
*b
= crush
->buckets
[j
];
2015 if (!b
|| is_shadow_item(b
->id
)) {
2018 string name
= get_item_name(b
->id
);
2019 if (name
.length() < match
.length()) {
2023 if (match
[0] == '%') {
2024 if (match
.substr(1) != name
.substr(name
.size() - match
.size() + 1)) {
2027 basename
= name
.substr(0, name
.size() - match
.size() + 1);
2028 } else if (match
[match
.size() - 1] == '%') {
2029 if (match
.substr(0, match
.size() - 1) !=
2030 name
.substr(0, match
.size() - 1)) {
2033 basename
= name
.substr(match
.size() - 1);
2034 } else if (match
== name
) {
2035 basename
= default_parent
;
2039 cout
<< "match " << match
<< " to " << name
<< " basename " << basename
2041 // look up or create basename bucket
2043 if (name_exists(basename
)) {
2044 base_id
= get_item_id(basename
);
2045 cout
<< " have base " << base_id
<< std::endl
;
2046 } else if (new_bucket_by_name
.count(basename
)) {
2047 base_id
= new_bucket_by_name
[basename
];
2048 cout
<< " already creating base " << base_id
<< std::endl
;
2050 base_id
= get_new_bucket_id();
2051 crush
->buckets
[-1-base_id
] = crush_make_bucket(crush
,
2056 crush
->buckets
[-1-base_id
]->id
= base_id
;
2057 name_map
[base_id
] = basename
;
2058 new_bucket_by_name
[basename
] = base_id
;
2059 cout
<< " created base " << base_id
<< std::endl
;
2061 new_buckets
[base_id
][default_parent_type_name
] = default_parent
;
2063 send_to
[b
->id
] = base_id
;
2064 new_class_bucket
[base_id
][new_class_id
] = b
->id
;
2065 new_bucket_names
[b
->id
] = basename
+ "~" + get_class_name(new_class_id
);
2067 // make sure devices are classified
2068 for (unsigned i
= 0; i
< b
->size
; ++i
) {
2069 int item
= b
->items
[i
];
2071 class_map
[item
] = new_class_id
;
2077 // no name_exists() works below,
2080 // copy items around
2081 //cout << "send_to " << send_to << std::endl;
2084 for (auto& i
: send_to
) {
2085 crush_bucket
*from
= get_bucket(i
.first
);
2086 crush_bucket
*to
= get_bucket(i
.second
);
2087 cout
<< "moving items from " << from
->id
<< " (" << get_item_name(from
->id
)
2088 << ") to " << to
->id
<< " (" << get_item_name(to
->id
) << ")"
2090 for (unsigned j
= 0; j
< from
->size
; ++j
) {
2091 int item
= from
->items
[j
];
2093 map
<string
,string
> to_loc
;
2094 to_loc
[get_type_name(to
->type
)] = get_item_name(to
->id
);
2096 if (subtree_contains(to
->id
, item
)) {
2099 map
<string
,string
> from_loc
;
2100 from_loc
[get_type_name(from
->type
)] = get_item_name(from
->id
);
2101 auto w
= get_item_weightf_in_loc(item
, from_loc
);
2102 r
= insert_item(cct
, item
,
2104 get_item_name(item
),
2107 if (!send_to
.count(item
)) {
2108 lderr(cct
) << "item " << item
<< " in bucket " << from
->id
2109 << " is not also a reclassified bucket" << dendl
;
2112 int newitem
= send_to
[item
];
2113 if (subtree_contains(to
->id
, newitem
)) {
2116 r
= link_bucket(cct
, newitem
, to_loc
);
2119 cout
<< __func__
<< " err from insert_item: " << cpp_strerror(r
)
2126 // make sure new buckets have parents
2127 for (auto& i
: new_buckets
) {
2129 if (get_immediate_parent_id(i
.first
, &parent
) < 0) {
2130 cout
<< "new bucket " << i
.first
<< " missing parent, adding at "
2131 << i
.second
<< std::endl
;
2132 int r
= link_bucket(cct
, i
.first
, i
.second
);
2134 cout
<< __func__
<< " err from insert_item: " << cpp_strerror(r
)
2141 // set class mappings
2142 //cout << "pre class_bucket: " << class_bucket << std::endl;
2143 for (auto& i
: new_class_bucket
) {
2144 for (auto& j
: i
.second
) {
2145 class_bucket
[i
.first
][j
.first
] = j
.second
;
2149 //cout << "post class_bucket: " << class_bucket << std::endl;
2150 for (auto& i
: new_bucket_names
) {
2151 name_map
[i
.first
] = i
.second
;
2154 int r
= rebuild_roots_with_classes(cct
);
2156 out
<< "failed to rebuild_roots_with_classes: " << cpp_strerror(r
)
2160 //cout << "final class_bucket: " << class_bucket << std::endl;
2165 int CrushWrapper::get_new_bucket_id()
2168 while (crush
->buckets
[-1-id
] &&
2169 -1-id
< crush
->max_buckets
) {
2172 if (-1-id
== crush
->max_buckets
) {
2173 ++crush
->max_buckets
;
2174 crush
->buckets
= (struct crush_bucket
**)realloc(
2176 sizeof(crush
->buckets
[0]) * crush
->max_buckets
);
2177 for (auto& i
: choose_args
) {
2178 assert(i
.second
.size
== (__u32
)crush
->max_buckets
- 1);
2180 i
.second
.args
= (struct crush_choose_arg
*)realloc(
2182 sizeof(i
.second
.args
[0]) * i
.second
.size
);
2188 void CrushWrapper::reweight(CephContext
*cct
)
2191 find_nonshadow_roots(&roots
);
2192 for (auto id
: roots
) {
2195 crush_bucket
*b
= get_bucket(id
);
2196 ldout(cct
, 5) << "reweight root bucket " << id
<< dendl
;
2197 int r
= crush_reweight_bucket(crush
, b
);
2198 ceph_assert(r
== 0);
2200 for (auto& i
: choose_args
) {
2201 //cout << "carg " << i.first << std::endl;
2202 vector
<uint32_t> w
; // discard top-level weights
2203 reweight_bucket(b
, i
.second
, &w
);
2206 int r
= rebuild_roots_with_classes(cct
);
2207 ceph_assert(r
== 0);
2210 void CrushWrapper::reweight_bucket(
2212 crush_choose_arg_map
& arg_map
,
2213 vector
<uint32_t> *weightv
)
2215 int idx
= -1 - b
->id
;
2216 unsigned npos
= arg_map
.args
[idx
].weight_set_positions
;
2217 //cout << __func__ << " " << b->id << " npos " << npos << std::endl;
2218 weightv
->resize(npos
);
2219 for (unsigned i
= 0; i
< b
->size
; ++i
) {
2220 int item
= b
->items
[i
];
2222 for (unsigned pos
= 0; pos
< npos
; ++pos
) {
2223 (*weightv
)[pos
] += arg_map
.args
[idx
].weight_set
->weights
[i
];
2226 vector
<uint32_t> subw(npos
);
2227 crush_bucket
*sub
= get_bucket(item
);
2229 reweight_bucket(sub
, arg_map
, &subw
);
2230 for (unsigned pos
= 0; pos
< npos
; ++pos
) {
2231 (*weightv
)[pos
] += subw
[pos
];
2232 // strash the real bucket weight as the weights for this reference
2233 arg_map
.args
[idx
].weight_set
->weights
[i
] = subw
[pos
];
2237 //cout << __func__ << " finish " << b->id << " " << *weightv << std::endl;
2240 int CrushWrapper::add_simple_rule_at(
2241 string name
, string root_name
,
2242 string failure_domain_name
,
2243 string device_class
,
2244 string mode
, int rule_type
,
2248 if (rule_exists(name
)) {
2250 *err
<< "rule " << name
<< " exists";
2254 if (rule_exists(rno
)) {
2256 *err
<< "rule with ruleno " << rno
<< " exists";
2260 for (rno
= 0; rno
< get_max_rules(); rno
++) {
2261 if (!rule_exists(rno
))
2265 if (!name_exists(root_name
)) {
2267 *err
<< "root item " << root_name
<< " does not exist";
2270 int root
= get_item_id(root_name
);
2272 if (failure_domain_name
.length()) {
2273 type
= get_type_id(failure_domain_name
);
2276 *err
<< "unknown type " << failure_domain_name
;
2280 if (device_class
.size()) {
2281 if (!class_exists(device_class
)) {
2283 *err
<< "device class " << device_class
<< " does not exist";
2286 int c
= get_class_id(device_class
);
2287 if (class_bucket
.count(root
) == 0 ||
2288 class_bucket
[root
].count(c
) == 0) {
2290 *err
<< "root " << root_name
<< " has no devices with class "
2294 root
= class_bucket
[root
][c
];
2296 if (mode
!= "firstn" && mode
!= "indep") {
2298 *err
<< "unknown mode " << mode
;
2303 if (mode
== "indep")
2305 crush_rule
*rule
= crush_make_rule(steps
, rule_type
);
2308 if (mode
== "indep") {
2309 crush_rule_set_step(rule
, step
++, CRUSH_RULE_SET_CHOOSELEAF_TRIES
, 5, 0);
2310 crush_rule_set_step(rule
, step
++, CRUSH_RULE_SET_CHOOSE_TRIES
, 100, 0);
2312 crush_rule_set_step(rule
, step
++, CRUSH_RULE_TAKE
, root
, 0);
2314 crush_rule_set_step(rule
, step
++,
2315 mode
== "firstn" ? CRUSH_RULE_CHOOSELEAF_FIRSTN
:
2316 CRUSH_RULE_CHOOSELEAF_INDEP
,
2320 crush_rule_set_step(rule
, step
++,
2321 mode
== "firstn" ? CRUSH_RULE_CHOOSE_FIRSTN
:
2322 CRUSH_RULE_CHOOSE_INDEP
,
2325 crush_rule_set_step(rule
, step
++, CRUSH_RULE_EMIT
, 0, 0);
2327 int ret
= crush_add_rule(crush
, rule
, rno
);
2329 *err
<< "failed to add rule " << rno
<< " because " << cpp_strerror(ret
);
2332 set_rule_name(rno
, name
);
2337 int CrushWrapper::add_simple_rule(
2338 string name
, string root_name
,
2339 string failure_domain_name
,
2340 string device_class
,
2341 string mode
, int rule_type
,
2344 return add_simple_rule_at(name
, root_name
, failure_domain_name
, device_class
,
2346 rule_type
, -1, err
);
2349 float CrushWrapper::_get_take_weight_osd_map(int root
,
2350 map
<int,float> *pmap
) const
2355 //breadth first iterate the OSD tree
2356 while (!q
.empty()) {
2357 int bno
= q
.front();
2359 crush_bucket
*b
= crush
->buckets
[-1-bno
];
2361 for (unsigned j
=0; j
<b
->size
; ++j
) {
2362 int item_id
= b
->items
[j
];
2363 if (item_id
>= 0) { //it's an OSD
2364 float w
= crush_get_bucket_item_weight(b
, j
);
2365 (*pmap
)[item_id
] = w
;
2367 } else { //not an OSD, expand the child later
2368 q
.push_back(item_id
);
2375 void CrushWrapper::_normalize_weight_map(float sum
,
2376 const map
<int,float>& m
,
2377 map
<int,float> *pmap
) const
2380 map
<int,float>::iterator q
= pmap
->find(p
.first
);
2381 if (q
== pmap
->end()) {
2382 (*pmap
)[p
.first
] = p
.second
/ sum
;
2384 q
->second
+= p
.second
/ sum
;
2389 int CrushWrapper::get_take_weight_osd_map(int root
, map
<int,float> *pmap
) const
2392 float sum
= _get_take_weight_osd_map(root
, &m
);
2393 _normalize_weight_map(sum
, m
, pmap
);
2397 int CrushWrapper::get_rule_weight_osd_map(unsigned ruleno
,
2398 map
<int,float> *pmap
) const
2400 if (ruleno
>= crush
->max_rules
)
2402 if (crush
->rules
[ruleno
] == NULL
)
2404 crush_rule
*rule
= crush
->rules
[ruleno
];
2406 // build a weight map for each TAKE in the rule, and then merge them
2408 // FIXME: if there are multiple takes that place a different number of
2409 // objects we do not take that into account. (Also, note that doing this
2410 // right is also a function of the pool, since the crush rule
2411 // might choose 2 + choose 2 but pool size may only be 3.)
2412 for (unsigned i
=0; i
<rule
->len
; ++i
) {
2415 if (rule
->steps
[i
].op
== CRUSH_RULE_TAKE
) {
2416 int n
= rule
->steps
[i
].arg1
;
2421 sum
+= _get_take_weight_osd_map(n
, &m
);
2424 _normalize_weight_map(sum
, m
, pmap
);
2430 int CrushWrapper::remove_rule(int ruleno
)
2432 if (ruleno
>= (int)crush
->max_rules
)
2434 if (crush
->rules
[ruleno
] == NULL
)
2436 crush_destroy_rule(crush
->rules
[ruleno
]);
2437 crush
->rules
[ruleno
] = NULL
;
2438 rule_name_map
.erase(ruleno
);
2440 return rebuild_roots_with_classes(nullptr);
2443 int CrushWrapper::bucket_adjust_item_weight(
2444 CephContext
*cct
, crush_bucket
*bucket
, int item
, int weight
,
2445 bool adjust_weight_sets
)
2447 if (adjust_weight_sets
) {
2449 for (position
= 0; position
< bucket
->size
; position
++)
2450 if (bucket
->items
[position
] == item
)
2452 ceph_assert(position
!= bucket
->size
);
2453 for (auto &w
: choose_args
) {
2454 crush_choose_arg_map
&arg_map
= w
.second
;
2455 crush_choose_arg
*arg
= &arg_map
.args
[-1-bucket
->id
];
2456 for (__u32 j
= 0; j
< arg
->weight_set_positions
; j
++) {
2457 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
2458 weight_set
->weights
[position
] = weight
;
2462 return crush_bucket_adjust_item_weight(crush
, bucket
, item
, weight
);
2465 int CrushWrapper::add_bucket(
2466 int bucketno
, int alg
, int hash
, int type
, int size
,
2467 int *items
, int *weights
, int *idout
)
2470 alg
= get_default_bucket_alg();
2474 crush_bucket
*b
= crush_make_bucket(crush
, alg
, hash
, type
, size
, items
,
2478 int r
= crush_add_bucket(crush
, bucketno
, b
, idout
);
2479 int pos
= -1 - *idout
;
2480 for (auto& p
: choose_args
) {
2481 crush_choose_arg_map
& cmap
= p
.second
;
2482 unsigned new_size
= crush
->max_buckets
;
2484 if ((int)cmap
.size
< crush
->max_buckets
) {
2485 cmap
.args
= static_cast<crush_choose_arg
*>(realloc(
2487 sizeof(crush_choose_arg
) * new_size
));
2488 ceph_assert(cmap
.args
);
2489 memset(&cmap
.args
[cmap
.size
], 0,
2490 sizeof(crush_choose_arg
) * (new_size
- cmap
.size
));
2491 cmap
.size
= new_size
;
2494 cmap
.args
= static_cast<crush_choose_arg
*>(calloc(sizeof(crush_choose_arg
),
2496 ceph_assert(cmap
.args
);
2497 cmap
.size
= new_size
;
2500 int positions
= get_choose_args_positions(cmap
);
2501 crush_choose_arg
& carg
= cmap
.args
[pos
];
2502 carg
.weight_set
= static_cast<crush_weight_set
*>(calloc(sizeof(crush_weight_set
),
2504 carg
.weight_set_positions
= positions
;
2505 for (int ppos
= 0; ppos
< positions
; ++ppos
) {
2506 carg
.weight_set
[ppos
].weights
= (__u32
*)calloc(sizeof(__u32
), size
);
2507 carg
.weight_set
[ppos
].size
= size
;
2508 for (int bpos
= 0; bpos
< size
; ++bpos
) {
2509 carg
.weight_set
[ppos
].weights
[bpos
] = weights
[bpos
];
2513 assert(crush
->max_buckets
== (int)cmap
.size
);
2518 int CrushWrapper::bucket_add_item(crush_bucket
*bucket
, int item
, int weight
)
2520 __u32 new_size
= bucket
->size
+ 1;
2521 int r
= crush_bucket_add_item(crush
, bucket
, item
, weight
);
2525 for (auto &w
: choose_args
) {
2526 crush_choose_arg_map
&arg_map
= w
.second
;
2527 crush_choose_arg
*arg
= &arg_map
.args
[-1-bucket
->id
];
2528 for (__u32 j
= 0; j
< arg
->weight_set_positions
; j
++) {
2529 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
2530 weight_set
->weights
= (__u32
*)realloc(weight_set
->weights
,
2531 new_size
* sizeof(__u32
));
2532 ceph_assert(weight_set
->size
+ 1 == new_size
);
2533 weight_set
->weights
[weight_set
->size
] = weight
;
2534 weight_set
->size
= new_size
;
2536 if (arg
->ids_size
) {
2537 arg
->ids
= (__s32
*)realloc(arg
->ids
, new_size
* sizeof(__s32
));
2538 ceph_assert(arg
->ids_size
+ 1 == new_size
);
2539 arg
->ids
[arg
->ids_size
] = item
;
2540 arg
->ids_size
= new_size
;
2546 int CrushWrapper::bucket_remove_item(crush_bucket
*bucket
, int item
)
2548 __u32 new_size
= bucket
->size
- 1;
2550 for (position
= 0; position
< bucket
->size
; position
++)
2551 if (bucket
->items
[position
] == item
)
2553 ceph_assert(position
!= bucket
->size
);
2554 int r
= crush_bucket_remove_item(crush
, bucket
, item
);
2558 for (auto &w
: choose_args
) {
2559 crush_choose_arg_map
&arg_map
= w
.second
;
2560 crush_choose_arg
*arg
= &arg_map
.args
[-1-bucket
->id
];
2561 for (__u32 j
= 0; j
< arg
->weight_set_positions
; j
++) {
2562 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
2563 ceph_assert(weight_set
->size
- 1 == new_size
);
2564 for (__u32 k
= position
; k
< new_size
; k
++)
2565 weight_set
->weights
[k
] = weight_set
->weights
[k
+1];
2567 weight_set
->weights
= (__u32
*)realloc(weight_set
->weights
,
2568 new_size
* sizeof(__u32
));
2570 free(weight_set
->weights
);
2571 weight_set
->weights
= NULL
;
2573 weight_set
->size
= new_size
;
2575 if (arg
->ids_size
) {
2576 ceph_assert(arg
->ids_size
- 1 == new_size
);
2577 for (__u32 k
= position
; k
< new_size
; k
++)
2578 arg
->ids
[k
] = arg
->ids
[k
+1];
2580 arg
->ids
= (__s32
*)realloc(arg
->ids
, new_size
* sizeof(__s32
));
2585 arg
->ids_size
= new_size
;
2591 int CrushWrapper::bucket_set_alg(int bid
, int alg
)
2593 crush_bucket
*b
= get_bucket(bid
);
2601 int CrushWrapper::update_device_class(int id
,
2602 const string
& class_name
,
2606 ceph_assert(item_exists(id
));
2607 auto old_class_name
= get_item_class(id
);
2608 if (old_class_name
&& old_class_name
!= class_name
) {
2609 *ss
<< "osd." << id
<< " has already bound to class '" << old_class_name
2610 << "', can not reset class to '" << class_name
<< "'; "
2611 << "use 'ceph osd crush rm-device-class <id>' to "
2612 << "remove old class first";
2616 int class_id
= get_or_create_class_id(class_name
);
2618 *ss
<< name
<< " id " << id
<< " is negative";
2622 if (class_map
.count(id
) != 0 && class_map
[id
] == class_id
) {
2623 *ss
<< name
<< " already set to class " << class_name
<< ". ";
2627 set_item_class(id
, class_id
);
2629 int r
= rebuild_roots_with_classes(nullptr);
2635 int CrushWrapper::remove_device_class(CephContext
*cct
, int id
, ostream
*ss
)
2638 const char *name
= get_item_name(id
);
2640 *ss
<< "osd." << id
<< " does not have a name";
2644 const char *class_name
= get_item_class(id
);
2646 *ss
<< "osd." << id
<< " has not been bound to a specific class yet";
2649 class_remove_item(id
);
2651 int r
= rebuild_roots_with_classes(cct
);
2653 *ss
<< "unable to rebuild roots with class '" << class_name
<< "' "
2654 << "of osd." << id
<< ": " << cpp_strerror(r
);
2660 int CrushWrapper::device_class_clone(
2661 int original_id
, int device_class
,
2662 const std::map
<int32_t, map
<int32_t, int32_t>>& old_class_bucket
,
2663 const std::set
<int32_t>& used_ids
,
2665 map
<int,map
<int,vector
<int>>> *cmap_item_weight
)
2667 const char *item_name
= get_item_name(original_id
);
2668 if (item_name
== NULL
)
2670 const char *class_name
= get_class_name(device_class
);
2671 if (class_name
== NULL
)
2673 string copy_name
= item_name
+ string("~") + class_name
;
2674 if (name_exists(copy_name
)) {
2675 *clone
= get_item_id(copy_name
);
2679 crush_bucket
*original
= get_bucket(original_id
);
2680 ceph_assert(!IS_ERR(original
));
2681 crush_bucket
*copy
= crush_make_bucket(crush
,
2688 vector
<unsigned> item_orig_pos
; // new item pos -> orig item pos
2689 for (unsigned i
= 0; i
< original
->size
; i
++) {
2690 int item
= original
->items
[i
];
2691 int weight
= crush_get_bucket_item_weight(original
, i
);
2693 if (class_map
.count(item
) != 0 && class_map
[item
] == device_class
) {
2694 int res
= crush_bucket_add_item(crush
, copy
, item
, weight
);
2702 int res
= device_class_clone(item
, device_class
, old_class_bucket
,
2703 used_ids
, &child_copy_id
,
2707 crush_bucket
*child_copy
= get_bucket(child_copy_id
);
2708 ceph_assert(!IS_ERR(child_copy
));
2709 res
= crush_bucket_add_item(crush
, copy
, child_copy_id
,
2710 child_copy
->weight
);
2714 item_orig_pos
.push_back(i
);
2716 ceph_assert(item_orig_pos
.size() == copy
->size
);
2719 if (old_class_bucket
.count(original_id
) &&
2720 old_class_bucket
.at(original_id
).count(device_class
)) {
2721 bno
= old_class_bucket
.at(original_id
).at(device_class
);
2723 // pick a new shadow bucket id that is not used by the current map
2724 // *or* any previous shadow buckets.
2726 while (((-1-bno
) < crush
->max_buckets
&& crush
->buckets
[-1-bno
]) ||
2727 used_ids
.count(bno
)) {
2731 int res
= crush_add_bucket(crush
, bno
, copy
, clone
);
2734 ceph_assert(!bno
|| bno
== *clone
);
2736 res
= set_item_class(*clone
, device_class
);
2740 // we do not use set_item_name because the name is intentionally invalid
2741 name_map
[*clone
] = copy_name
;
2743 name_rmap
[copy_name
] = *clone
;
2744 class_bucket
[original_id
][device_class
] = *clone
;
2746 // set up choose_args for the new bucket.
2747 for (auto& w
: choose_args
) {
2748 crush_choose_arg_map
& cmap
= w
.second
;
2749 if (crush
->max_buckets
> (int)cmap
.size
) {
2750 unsigned new_size
= crush
->max_buckets
;
2751 cmap
.args
= static_cast<crush_choose_arg
*>(realloc(cmap
.args
,
2752 new_size
* sizeof(cmap
.args
[0])));
2753 ceph_assert(cmap
.args
);
2754 memset(cmap
.args
+ cmap
.size
, 0,
2755 (new_size
- cmap
.size
) * sizeof(cmap
.args
[0]));
2756 cmap
.size
= new_size
;
2758 auto& o
= cmap
.args
[-1-original_id
];
2759 auto& n
= cmap
.args
[-1-bno
];
2760 n
.ids_size
= 0; // FIXME: implement me someday
2761 n
.weight_set_positions
= o
.weight_set_positions
;
2762 n
.weight_set
= static_cast<crush_weight_set
*>(calloc(
2763 n
.weight_set_positions
, sizeof(crush_weight_set
)));
2764 for (size_t s
= 0; s
< n
.weight_set_positions
; ++s
) {
2765 n
.weight_set
[s
].size
= copy
->size
;
2766 n
.weight_set
[s
].weights
= (__u32
*)calloc(copy
->size
, sizeof(__u32
));
2768 for (size_t s
= 0; s
< n
.weight_set_positions
; ++s
) {
2769 vector
<int> bucket_weights(n
.weight_set_positions
);
2770 for (size_t i
= 0; i
< copy
->size
; ++i
) {
2771 int item
= copy
->items
[i
];
2773 n
.weight_set
[s
].weights
[i
] = o
.weight_set
[s
].weights
[item_orig_pos
[i
]];
2774 } else if ((*cmap_item_weight
)[w
.first
].count(item
)) {
2775 n
.weight_set
[s
].weights
[i
] = (*cmap_item_weight
)[w
.first
][item
][s
];
2777 n
.weight_set
[s
].weights
[i
] = 0;
2779 bucket_weights
[s
] += n
.weight_set
[s
].weights
[i
];
2781 (*cmap_item_weight
)[w
.first
][bno
] = bucket_weights
;
2787 int CrushWrapper::get_rules_by_class(const string
&class_name
, set
<int> *rules
)
2791 if (!class_exists(class_name
)) {
2794 int class_id
= get_class_id(class_name
);
2795 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
2796 crush_rule
*r
= crush
->rules
[i
];
2799 for (unsigned j
= 0; j
< r
->len
; ++j
) {
2800 if (r
->steps
[j
].op
== CRUSH_RULE_TAKE
) {
2801 int step_item
= r
->steps
[j
].arg1
;
2804 int res
= split_id_class(step_item
, &original_item
, &c
);
2808 if (c
!= -1 && c
== class_id
) {
2818 // return rules that might reference the given osd
2819 int CrushWrapper::get_rules_by_osd(int osd
, set
<int> *rules
)
2826 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
2827 crush_rule
*r
= crush
->rules
[i
];
2830 for (unsigned j
= 0; j
< r
->len
; ++j
) {
2831 if (r
->steps
[j
].op
== CRUSH_RULE_TAKE
) {
2832 int step_item
= r
->steps
[j
].arg1
;
2833 list
<int> unordered
;
2834 int rc
= _get_leaves(step_item
, &unordered
);
2836 return rc
; // propagate fatal errors!
2839 for (auto &o
: unordered
) {
2840 ceph_assert(o
>= 0);
2856 bool CrushWrapper::_class_is_dead(int class_id
)
2858 for (auto &p
: class_map
) {
2859 if (p
.first
>= 0 && p
.second
== class_id
) {
2863 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
2864 crush_rule
*r
= crush
->rules
[i
];
2867 for (unsigned j
= 0; j
< r
->len
; ++j
) {
2868 if (r
->steps
[j
].op
== CRUSH_RULE_TAKE
) {
2869 int root
= r
->steps
[j
].arg1
;
2870 for (auto &p
: class_bucket
) {
2872 if (q
.count(class_id
) && q
[class_id
] == root
) {
2879 // no more referenced by any devices or crush rules
2883 void CrushWrapper::cleanup_dead_classes()
2885 auto p
= class_name
.begin();
2886 while (p
!= class_name
.end()) {
2887 if (_class_is_dead(p
->first
)) {
2888 string n
= p
->second
;
2890 remove_class_name(n
);
2897 int CrushWrapper::rebuild_roots_with_classes(CephContext
*cct
)
2899 std::map
<int32_t, map
<int32_t, int32_t> > old_class_bucket
= class_bucket
;
2900 cleanup_dead_classes();
2901 int r
= trim_roots_with_class(cct
);
2904 class_bucket
.clear();
2905 return populate_classes(old_class_bucket
);
2908 void CrushWrapper::encode(bufferlist
& bl
, uint64_t features
) const
2913 __u32 magic
= CRUSH_MAGIC
;
2916 encode(crush
->max_buckets
, bl
);
2917 encode(crush
->max_rules
, bl
);
2918 encode(crush
->max_devices
, bl
);
2920 bool encode_compat_choose_args
= false;
2921 crush_choose_arg_map arg_map
;
2922 memset(&arg_map
, '\0', sizeof(arg_map
));
2923 if (has_choose_args() &&
2924 !HAVE_FEATURE(features
, CRUSH_CHOOSE_ARGS
)) {
2925 ceph_assert(!has_incompat_choose_args());
2926 encode_compat_choose_args
= true;
2927 arg_map
= choose_args
.begin()->second
;
2931 for (int i
=0; i
<crush
->max_buckets
; i
++) {
2933 if (crush
->buckets
[i
]) alg
= crush
->buckets
[i
]->alg
;
2938 encode(crush
->buckets
[i
]->id
, bl
);
2939 encode(crush
->buckets
[i
]->type
, bl
);
2940 encode(crush
->buckets
[i
]->alg
, bl
);
2941 encode(crush
->buckets
[i
]->hash
, bl
);
2942 encode(crush
->buckets
[i
]->weight
, bl
);
2943 encode(crush
->buckets
[i
]->size
, bl
);
2944 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++)
2945 encode(crush
->buckets
[i
]->items
[j
], bl
);
2947 switch (crush
->buckets
[i
]->alg
) {
2948 case CRUSH_BUCKET_UNIFORM
:
2949 encode((reinterpret_cast<crush_bucket_uniform
*>(crush
->buckets
[i
]))->item_weight
, bl
);
2952 case CRUSH_BUCKET_LIST
:
2953 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++) {
2954 encode((reinterpret_cast<crush_bucket_list
*>(crush
->buckets
[i
]))->item_weights
[j
], bl
);
2955 encode((reinterpret_cast<crush_bucket_list
*>(crush
->buckets
[i
]))->sum_weights
[j
], bl
);
2959 case CRUSH_BUCKET_TREE
:
2960 encode((reinterpret_cast<crush_bucket_tree
*>(crush
->buckets
[i
]))->num_nodes
, bl
);
2961 for (unsigned j
=0; j
<(reinterpret_cast<crush_bucket_tree
*>(crush
->buckets
[i
]))->num_nodes
; j
++)
2962 encode((reinterpret_cast<crush_bucket_tree
*>(crush
->buckets
[i
]))->node_weights
[j
], bl
);
2965 case CRUSH_BUCKET_STRAW
:
2966 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++) {
2967 encode((reinterpret_cast<crush_bucket_straw
*>(crush
->buckets
[i
]))->item_weights
[j
], bl
);
2968 encode((reinterpret_cast<crush_bucket_straw
*>(crush
->buckets
[i
]))->straws
[j
], bl
);
2972 case CRUSH_BUCKET_STRAW2
:
2975 if (encode_compat_choose_args
&&
2976 arg_map
.args
[i
].weight_set_positions
> 0) {
2977 weights
= arg_map
.args
[i
].weight_set
[0].weights
;
2979 weights
= (reinterpret_cast<crush_bucket_straw2
*>(crush
->buckets
[i
]))->item_weights
;
2981 for (unsigned j
=0; j
<crush
->buckets
[i
]->size
; j
++) {
2982 encode(weights
[j
], bl
);
2994 for (unsigned i
=0; i
<crush
->max_rules
; i
++) {
2995 __u32 yes
= crush
->rules
[i
] ? 1:0;
3000 encode(crush
->rules
[i
]->len
, bl
);
3003 * legacy crush_rule_mask was
3005 * struct crush_rule_mask {
3012 * encode ruleset=ruleid, and min/max of 1/100
3014 encode((__u8
)i
, bl
); // ruleset == ruleid
3015 encode(crush
->rules
[i
]->type
, bl
);
3016 if (HAVE_FEATURE(features
, SERVER_QUINCY
)) {
3017 encode((__u8
)1, bl
); // min_size = 1
3018 encode((__u8
)100, bl
); // max_size = 100
3020 encode(crush
->rules
[i
]->deprecated_min_size
, bl
);
3021 encode(crush
->rules
[i
]->deprecated_max_size
, bl
);
3024 for (unsigned j
=0; j
<crush
->rules
[i
]->len
; j
++)
3025 encode(crush
->rules
[i
]->steps
[j
], bl
);
3029 encode(type_map
, bl
);
3030 encode(name_map
, bl
);
3031 encode(rule_name_map
, bl
);
3034 encode(crush
->choose_local_tries
, bl
);
3035 encode(crush
->choose_local_fallback_tries
, bl
);
3036 encode(crush
->choose_total_tries
, bl
);
3037 encode(crush
->chooseleaf_descend_once
, bl
);
3038 encode(crush
->chooseleaf_vary_r
, bl
);
3039 encode(crush
->straw_calc_version
, bl
);
3040 encode(crush
->allowed_bucket_algs
, bl
);
3041 if (features
& CEPH_FEATURE_CRUSH_TUNABLES5
) {
3042 encode(crush
->chooseleaf_stable
, bl
);
3045 if (HAVE_FEATURE(features
, SERVER_LUMINOUS
)) {
3047 encode(class_map
, bl
);
3048 encode(class_name
, bl
);
3049 encode(class_bucket
, bl
);
3052 __u32 size
= (__u32
)choose_args
.size();
3054 for (auto c
: choose_args
) {
3055 encode(c
.first
, bl
);
3056 crush_choose_arg_map arg_map
= c
.second
;
3058 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
3059 crush_choose_arg
*arg
= &arg_map
.args
[i
];
3060 if (arg
->weight_set_positions
== 0 &&
3066 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
3067 crush_choose_arg
*arg
= &arg_map
.args
[i
];
3068 if (arg
->weight_set_positions
== 0 &&
3072 encode(arg
->weight_set_positions
, bl
);
3073 for (__u32 j
= 0; j
< arg
->weight_set_positions
; j
++) {
3074 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
3075 encode(weight_set
->size
, bl
);
3076 for (__u32 k
= 0; k
< weight_set
->size
; k
++)
3077 encode(weight_set
->weights
[k
], bl
);
3079 encode(arg
->ids_size
, bl
);
3080 for (__u32 j
= 0; j
< arg
->ids_size
; j
++)
3081 encode(arg
->ids
[j
], bl
);
3087 static void decode_32_or_64_string_map(map
<int32_t,string
>& m
, bufferlist::const_iterator
& blp
)
3097 decode(strlen
, blp
);
3099 // der, key was actually 64-bits!
3100 decode(strlen
, blp
);
3102 decode_nohead(strlen
, m
[key
], blp
);
3106 void CrushWrapper::decode(bufferlist::const_iterator
& blp
)
3113 if (magic
!= CRUSH_MAGIC
)
3114 throw ceph::buffer::malformed_input("bad magic number");
3116 decode(crush
->max_buckets
, blp
);
3117 decode(crush
->max_rules
, blp
);
3118 decode(crush
->max_devices
, blp
);
3120 // legacy tunables, unless we decode something newer
3121 set_tunables_legacy();
3125 crush
->buckets
= (crush_bucket
**)calloc(1, crush
->max_buckets
* sizeof(crush_bucket
*));
3126 for (int i
=0; i
<crush
->max_buckets
; i
++) {
3127 decode_crush_bucket(&crush
->buckets
[i
], blp
);
3131 crush
->rules
= (crush_rule
**)calloc(1, crush
->max_rules
* sizeof(crush_rule
*));
3132 for (unsigned i
= 0; i
< crush
->max_rules
; ++i
) {
3136 crush
->rules
[i
] = NULL
;
3142 crush
->rules
[i
] = reinterpret_cast<crush_rule
*>(calloc(1, crush_rule_size(len
)));
3143 crush
->rules
[i
]->len
= len
;
3145 __u8 ruleset
; // ignore + discard
3146 decode(ruleset
, blp
);
3148 throw ::ceph::buffer::malformed_input("crush ruleset_id != rule_id; encoding is too old");
3150 decode(crush
->rules
[i
]->type
, blp
);
3151 decode(crush
->rules
[i
]->deprecated_min_size
, blp
);
3152 decode(crush
->rules
[i
]->deprecated_max_size
, blp
);
3154 for (unsigned j
=0; j
<crush
->rules
[i
]->len
; j
++)
3155 decode(crush
->rules
[i
]->steps
[j
], blp
);
3159 // NOTE: we had a bug where we were incoding int instead of int32, which means the
3160 // 'key' field for these maps may be either 32 or 64 bits, depending. tolerate
3161 // both by assuming the string is always non-empty.
3162 decode_32_or_64_string_map(type_map
, blp
);
3163 decode_32_or_64_string_map(name_map
, blp
);
3164 decode_32_or_64_string_map(rule_name_map
, blp
);
3168 decode(crush
->choose_local_tries
, blp
);
3169 decode(crush
->choose_local_fallback_tries
, blp
);
3170 decode(crush
->choose_total_tries
, blp
);
3173 decode(crush
->chooseleaf_descend_once
, blp
);
3176 decode(crush
->chooseleaf_vary_r
, blp
);
3179 decode(crush
->straw_calc_version
, blp
);
3182 decode(crush
->allowed_bucket_algs
, blp
);
3185 decode(crush
->chooseleaf_stable
, blp
);
3188 decode(class_map
, blp
);
3189 decode(class_name
, blp
);
3190 for (auto &c
: class_name
)
3191 class_rname
[c
.second
] = c
.first
;
3192 decode(class_bucket
, blp
);
3195 __u32 choose_args_size
;
3196 decode(choose_args_size
, blp
);
3197 for (__u32 i
= 0; i
< choose_args_size
; i
++) {
3198 typename
decltype(choose_args
)::key_type choose_args_index
;
3199 decode(choose_args_index
, blp
);
3200 crush_choose_arg_map arg_map
;
3201 arg_map
.size
= crush
->max_buckets
;
3202 arg_map
.args
= static_cast<crush_choose_arg
*>(calloc(
3203 arg_map
.size
, sizeof(crush_choose_arg
)));
3206 for (__u32 j
= 0; j
< size
; j
++) {
3208 decode(bucket_index
, blp
);
3209 ceph_assert(bucket_index
< arg_map
.size
);
3210 crush_choose_arg
*arg
= &arg_map
.args
[bucket_index
];
3211 decode(arg
->weight_set_positions
, blp
);
3212 if (arg
->weight_set_positions
) {
3213 arg
->weight_set
= static_cast<crush_weight_set
*>(calloc(
3214 arg
->weight_set_positions
, sizeof(crush_weight_set
)));
3215 for (__u32 k
= 0; k
< arg
->weight_set_positions
; k
++) {
3216 crush_weight_set
*weight_set
= &arg
->weight_set
[k
];
3217 decode(weight_set
->size
, blp
);
3218 weight_set
->weights
= (__u32
*)calloc(
3219 weight_set
->size
, sizeof(__u32
));
3220 for (__u32 l
= 0; l
< weight_set
->size
; l
++)
3221 decode(weight_set
->weights
[l
], blp
);
3224 decode(arg
->ids_size
, blp
);
3225 if (arg
->ids_size
) {
3226 ceph_assert(arg
->ids_size
== crush
->buckets
[bucket_index
]->size
);
3227 arg
->ids
= (__s32
*)calloc(arg
->ids_size
, sizeof(__s32
));
3228 for (__u32 k
= 0; k
< arg
->ids_size
; k
++)
3229 decode(arg
->ids
[k
], blp
);
3232 choose_args
[choose_args_index
] = arg_map
;
3235 update_choose_args(nullptr); // in case we decode a legacy "corrupted" map
3239 crush_destroy(crush
);
3244 void CrushWrapper::decode_crush_bucket(crush_bucket
** bptr
, bufferlist::const_iterator
&blp
)
3256 case CRUSH_BUCKET_UNIFORM
:
3257 size
= sizeof(crush_bucket_uniform
);
3259 case CRUSH_BUCKET_LIST
:
3260 size
= sizeof(crush_bucket_list
);
3262 case CRUSH_BUCKET_TREE
:
3263 size
= sizeof(crush_bucket_tree
);
3265 case CRUSH_BUCKET_STRAW
:
3266 size
= sizeof(crush_bucket_straw
);
3268 case CRUSH_BUCKET_STRAW2
:
3269 size
= sizeof(crush_bucket_straw2
);
3274 snprintf(str
, sizeof(str
), "unsupported bucket algorithm: %d", alg
);
3275 throw ceph::buffer::malformed_input(str
);
3278 crush_bucket
*bucket
= reinterpret_cast<crush_bucket
*>(calloc(1, size
));
3281 decode(bucket
->id
, blp
);
3282 decode(bucket
->type
, blp
);
3283 decode(bucket
->alg
, blp
);
3284 decode(bucket
->hash
, blp
);
3285 decode(bucket
->weight
, blp
);
3286 decode(bucket
->size
, blp
);
3288 bucket
->items
= (__s32
*)calloc(1, bucket
->size
* sizeof(__s32
));
3289 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
3290 decode(bucket
->items
[j
], blp
);
3293 switch (bucket
->alg
) {
3294 case CRUSH_BUCKET_UNIFORM
:
3295 decode((reinterpret_cast<crush_bucket_uniform
*>(bucket
))->item_weight
, blp
);
3298 case CRUSH_BUCKET_LIST
: {
3299 crush_bucket_list
* cbl
= reinterpret_cast<crush_bucket_list
*>(bucket
);
3300 cbl
->item_weights
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
3301 cbl
->sum_weights
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
3303 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
3304 decode(cbl
->item_weights
[j
], blp
);
3305 decode(cbl
->sum_weights
[j
], blp
);
3310 case CRUSH_BUCKET_TREE
: {
3311 crush_bucket_tree
* cbt
= reinterpret_cast<crush_bucket_tree
*>(bucket
);
3312 decode(cbt
->num_nodes
, blp
);
3313 cbt
->node_weights
= (__u32
*)calloc(1, cbt
->num_nodes
* sizeof(__u32
));
3314 for (unsigned j
=0; j
<cbt
->num_nodes
; j
++) {
3315 decode(cbt
->node_weights
[j
], blp
);
3320 case CRUSH_BUCKET_STRAW
: {
3321 crush_bucket_straw
* cbs
= reinterpret_cast<crush_bucket_straw
*>(bucket
);
3322 cbs
->straws
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
3323 cbs
->item_weights
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
3324 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
3325 decode(cbs
->item_weights
[j
], blp
);
3326 decode(cbs
->straws
[j
], blp
);
3331 case CRUSH_BUCKET_STRAW2
: {
3332 crush_bucket_straw2
* cbs
= reinterpret_cast<crush_bucket_straw2
*>(bucket
);
3333 cbs
->item_weights
= (__u32
*)calloc(1, bucket
->size
* sizeof(__u32
));
3334 for (unsigned j
= 0; j
< bucket
->size
; ++j
) {
3335 decode(cbs
->item_weights
[j
], blp
);
3341 // We should have handled this case in the first switch statement
3348 void CrushWrapper::dump(Formatter
*f
) const
3350 f
->open_array_section("devices");
3351 for (int i
=0; i
<get_max_devices(); i
++) {
3352 f
->open_object_section("device");
3353 f
->dump_int("id", i
);
3354 const char *n
= get_item_name(i
);
3356 f
->dump_string("name", n
);
3359 sprintf(name
, "device%d", i
);
3360 f
->dump_string("name", name
);
3362 const char *device_class
= get_item_class(i
);
3363 if (device_class
!= NULL
)
3364 f
->dump_string("class", device_class
);
3369 f
->open_array_section("types");
3370 int n
= get_num_type_names();
3371 for (int i
=0; n
; i
++) {
3372 const char *name
= get_type_name(i
);
3375 f
->open_object_section("type");
3376 f
->dump_int("type_id", 0);
3377 f
->dump_string("name", "device");
3383 f
->open_object_section("type");
3384 f
->dump_int("type_id", i
);
3385 f
->dump_string("name", name
);
3390 f
->open_array_section("buckets");
3391 for (int bucket
= -1; bucket
> -1-get_max_buckets(); --bucket
) {
3392 if (!bucket_exists(bucket
))
3394 f
->open_object_section("bucket");
3395 f
->dump_int("id", bucket
);
3396 if (get_item_name(bucket
))
3397 f
->dump_string("name", get_item_name(bucket
));
3398 f
->dump_int("type_id", get_bucket_type(bucket
));
3399 if (get_type_name(get_bucket_type(bucket
)))
3400 f
->dump_string("type_name", get_type_name(get_bucket_type(bucket
)));
3401 f
->dump_int("weight", get_bucket_weight(bucket
));
3402 f
->dump_string("alg", crush_bucket_alg_name(get_bucket_alg(bucket
)));
3403 f
->dump_string("hash", crush_hash_name(get_bucket_hash(bucket
)));
3404 f
->open_array_section("items");
3405 for (int j
=0; j
<get_bucket_size(bucket
); j
++) {
3406 f
->open_object_section("item");
3407 f
->dump_int("id", get_bucket_item(bucket
, j
));
3408 f
->dump_int("weight", get_bucket_item_weight(bucket
, j
));
3409 f
->dump_int("pos", j
);
3417 f
->open_array_section("rules");
3421 f
->open_object_section("tunables");
3425 dump_choose_args(f
);
3429 // depth first walker
3431 typedef CrushTreeDumper::Item Item
;
3432 const CrushWrapper
*crush
;
3433 const CrushTreeDumper::name_map_t
& weight_set_names
;
3435 explicit TreeDumper(const CrushWrapper
*crush
,
3436 const CrushTreeDumper::name_map_t
& wsnames
)
3437 : crush(crush
), weight_set_names(wsnames
) {}
3439 void dump(Formatter
*f
) {
3441 crush
->find_roots(&roots
);
3442 for (set
<int>::iterator root
= roots
.begin(); root
!= roots
.end(); ++root
) {
3443 dump_item(Item(*root
, 0, 0, crush
->get_bucket_weightf(*root
)), f
);
3448 void dump_item(const Item
& qi
, Formatter
* f
) {
3449 if (qi
.is_bucket()) {
3450 f
->open_object_section("bucket");
3451 CrushTreeDumper::dump_item_fields(crush
, weight_set_names
, qi
, f
);
3452 dump_bucket_children(qi
, f
);
3455 f
->open_object_section("device");
3456 CrushTreeDumper::dump_item_fields(crush
, weight_set_names
, qi
, f
);
3461 void dump_bucket_children(const Item
& parent
, Formatter
* f
) {
3462 f
->open_array_section("items");
3463 const int max_pos
= crush
->get_bucket_size(parent
.id
);
3464 for (int pos
= 0; pos
< max_pos
; pos
++) {
3465 int id
= crush
->get_bucket_item(parent
.id
, pos
);
3466 float weight
= crush
->get_bucket_item_weightf(parent
.id
, pos
);
3467 dump_item(Item(id
, parent
.id
, parent
.depth
+ 1, weight
), f
);
3474 void CrushWrapper::dump_tree(
3476 const CrushTreeDumper::name_map_t
& weight_set_names
) const
3479 TreeDumper(this, weight_set_names
).dump(f
);
3482 void CrushWrapper::dump_tunables(Formatter
*f
) const
3484 f
->dump_int("choose_local_tries", get_choose_local_tries());
3485 f
->dump_int("choose_local_fallback_tries", get_choose_local_fallback_tries());
3486 f
->dump_int("choose_total_tries", get_choose_total_tries());
3487 f
->dump_int("chooseleaf_descend_once", get_chooseleaf_descend_once());
3488 f
->dump_int("chooseleaf_vary_r", get_chooseleaf_vary_r());
3489 f
->dump_int("chooseleaf_stable", get_chooseleaf_stable());
3490 f
->dump_int("straw_calc_version", get_straw_calc_version());
3491 f
->dump_int("allowed_bucket_algs", get_allowed_bucket_algs());
3493 // be helpful about it
3494 if (has_jewel_tunables())
3495 f
->dump_string("profile", "jewel");
3496 else if (has_hammer_tunables())
3497 f
->dump_string("profile", "hammer");
3498 else if (has_firefly_tunables())
3499 f
->dump_string("profile", "firefly");
3500 else if (has_bobtail_tunables())
3501 f
->dump_string("profile", "bobtail");
3502 else if (has_argonaut_tunables())
3503 f
->dump_string("profile", "argonaut");
3505 f
->dump_string("profile", "unknown");
3506 f
->dump_int("optimal_tunables", (int)has_optimal_tunables());
3507 f
->dump_int("legacy_tunables", (int)has_legacy_tunables());
3509 // be helpful about minimum version required
3510 f
->dump_string("minimum_required_version", get_min_required_version());
3512 f
->dump_int("require_feature_tunables", (int)has_nondefault_tunables());
3513 f
->dump_int("require_feature_tunables2", (int)has_nondefault_tunables2());
3514 f
->dump_int("has_v2_rules", (int)has_v2_rules());
3515 f
->dump_int("require_feature_tunables3", (int)has_nondefault_tunables3());
3516 f
->dump_int("has_v3_rules", (int)has_v3_rules());
3517 f
->dump_int("has_v4_buckets", (int)has_v4_buckets());
3518 f
->dump_int("require_feature_tunables5", (int)has_nondefault_tunables5());
3519 f
->dump_int("has_v5_rules", (int)has_v5_rules());
3522 void CrushWrapper::dump_choose_args(Formatter
*f
) const
3524 f
->open_object_section("choose_args");
3525 for (auto c
: choose_args
) {
3526 crush_choose_arg_map arg_map
= c
.second
;
3527 f
->open_array_section(stringify(c
.first
).c_str());
3528 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
3529 crush_choose_arg
*arg
= &arg_map
.args
[i
];
3530 if (arg
->weight_set_positions
== 0 &&
3533 f
->open_object_section("choose_args");
3534 int bucket_index
= i
;
3535 f
->dump_int("bucket_id", -1-bucket_index
);
3536 if (arg
->weight_set_positions
> 0) {
3537 f
->open_array_section("weight_set");
3538 for (__u32 j
= 0; j
< arg
->weight_set_positions
; j
++) {
3539 f
->open_array_section("weights");
3540 __u32
*weights
= arg
->weight_set
[j
].weights
;
3541 __u32 size
= arg
->weight_set
[j
].size
;
3542 for (__u32 k
= 0; k
< size
; k
++) {
3543 f
->dump_float("weight", (float)weights
[k
]/(float)0x10000);
3549 if (arg
->ids_size
> 0) {
3550 f
->open_array_section("ids");
3551 for (__u32 j
= 0; j
< arg
->ids_size
; j
++)
3552 f
->dump_int("id", arg
->ids
[j
]);
3562 void CrushWrapper::dump_rules(Formatter
*f
) const
3564 for (int i
=0; i
<get_max_rules(); i
++) {
3565 if (!rule_exists(i
))
3571 void CrushWrapper::dump_rule(int rule_id
, Formatter
*f
) const
3573 f
->open_object_section("rule");
3574 f
->dump_int("rule_id", rule_id
);
3575 if (get_rule_name(rule_id
))
3576 f
->dump_string("rule_name", get_rule_name(rule_id
));
3577 f
->dump_int("type", get_rule_type(rule_id
));
3578 f
->open_array_section("steps");
3579 for (int j
=0; j
<get_rule_len(rule_id
); j
++) {
3580 f
->open_object_section("step");
3581 switch (get_rule_op(rule_id
, j
)) {
3582 case CRUSH_RULE_NOOP
:
3583 f
->dump_string("op", "noop");
3585 case CRUSH_RULE_TAKE
:
3586 f
->dump_string("op", "take");
3588 int item
= get_rule_arg1(rule_id
, j
);
3589 f
->dump_int("item", item
);
3591 const char *name
= get_item_name(item
);
3592 f
->dump_string("item_name", name
? name
: "");
3595 case CRUSH_RULE_EMIT
:
3596 f
->dump_string("op", "emit");
3598 case CRUSH_RULE_CHOOSE_FIRSTN
:
3599 f
->dump_string("op", "choose_firstn");
3600 f
->dump_int("num", get_rule_arg1(rule_id
, j
));
3601 f
->dump_string("type", get_type_name(get_rule_arg2(rule_id
, j
)));
3603 case CRUSH_RULE_CHOOSE_INDEP
:
3604 f
->dump_string("op", "choose_indep");
3605 f
->dump_int("num", get_rule_arg1(rule_id
, j
));
3606 f
->dump_string("type", get_type_name(get_rule_arg2(rule_id
, j
)));
3608 case CRUSH_RULE_CHOOSELEAF_FIRSTN
:
3609 f
->dump_string("op", "chooseleaf_firstn");
3610 f
->dump_int("num", get_rule_arg1(rule_id
, j
));
3611 f
->dump_string("type", get_type_name(get_rule_arg2(rule_id
, j
)));
3613 case CRUSH_RULE_CHOOSELEAF_INDEP
:
3614 f
->dump_string("op", "chooseleaf_indep");
3615 f
->dump_int("num", get_rule_arg1(rule_id
, j
));
3616 f
->dump_string("type", get_type_name(get_rule_arg2(rule_id
, j
)));
3618 case CRUSH_RULE_SET_CHOOSE_TRIES
:
3619 f
->dump_string("op", "set_choose_tries");
3620 f
->dump_int("num", get_rule_arg1(rule_id
, j
));
3622 case CRUSH_RULE_SET_CHOOSELEAF_TRIES
:
3623 f
->dump_string("op", "set_chooseleaf_tries");
3624 f
->dump_int("num", get_rule_arg1(rule_id
, j
));
3627 f
->dump_int("opcode", get_rule_op(rule_id
, j
));
3628 f
->dump_int("arg1", get_rule_arg1(rule_id
, j
));
3629 f
->dump_int("arg2", get_rule_arg2(rule_id
, j
));
3637 void CrushWrapper::list_rules(Formatter
*f
) const
3639 for (int rule
= 0; rule
< get_max_rules(); rule
++) {
3640 if (!rule_exists(rule
))
3642 f
->dump_string("name", get_rule_name(rule
));
3646 void CrushWrapper::list_rules(ostream
*ss
) const
3648 for (int rule
= 0; rule
< get_max_rules(); rule
++) {
3649 if (!rule_exists(rule
))
3651 *ss
<< get_rule_name(rule
) << "\n";
3655 class CrushTreePlainDumper
: public CrushTreeDumper::Dumper
<TextTable
> {
3657 typedef CrushTreeDumper::Dumper
<TextTable
> Parent
;
3659 explicit CrushTreePlainDumper(const CrushWrapper
*crush
,
3660 const CrushTreeDumper::name_map_t
& wsnames
)
3661 : Parent(crush
, wsnames
) {}
3662 explicit CrushTreePlainDumper(const CrushWrapper
*crush
,
3663 const CrushTreeDumper::name_map_t
& wsnames
,
3665 : Parent(crush
, wsnames
, show_shadow
) {}
3668 void dump(TextTable
*tbl
) {
3669 tbl
->define_column("ID", TextTable::LEFT
, TextTable::RIGHT
);
3670 tbl
->define_column("CLASS", TextTable::LEFT
, TextTable::RIGHT
);
3671 tbl
->define_column("WEIGHT", TextTable::LEFT
, TextTable::RIGHT
);
3672 for (auto& p
: crush
->choose_args
) {
3673 if (p
.first
== CrushWrapper::DEFAULT_CHOOSE_ARGS
) {
3674 tbl
->define_column("(compat)", TextTable::LEFT
, TextTable::RIGHT
);
3677 auto q
= weight_set_names
.find(p
.first
);
3678 name
= q
!= weight_set_names
.end() ? q
->second
:
3680 tbl
->define_column(name
.c_str(), TextTable::LEFT
, TextTable::RIGHT
);
3683 tbl
->define_column("TYPE NAME", TextTable::LEFT
, TextTable::LEFT
);
3688 void dump_item(const CrushTreeDumper::Item
&qi
, TextTable
*tbl
) override
{
3689 const char *c
= crush
->get_item_class(qi
.id
);
3694 << weightf_t(qi
.weight
);
3695 for (auto& p
: crush
->choose_args
) {
3696 if (qi
.parent
< 0) {
3697 const crush_choose_arg_map cmap
= crush
->choose_args_get(p
.first
);
3698 int bidx
= -1 - qi
.parent
;
3699 const crush_bucket
*b
= crush
->get_bucket(qi
.parent
);
3701 bidx
< (int)cmap
.size
&&
3702 cmap
.args
[bidx
].weight_set
&&
3703 cmap
.args
[bidx
].weight_set_positions
>= 1) {
3706 pos
< (int)cmap
.args
[bidx
].weight_set
[0].size
&&
3707 b
->items
[pos
] != qi
.id
;
3709 *tbl
<< weightf_t((float)cmap
.args
[bidx
].weight_set
[0].weights
[pos
] /
3717 for (int k
=0; k
< qi
.depth
; k
++) {
3720 if (qi
.is_bucket()) {
3721 ss
<< crush
->get_type_name(crush
->get_bucket_type(qi
.id
)) << " "
3722 << crush
->get_item_name(qi
.id
);
3724 ss
<< "osd." << qi
.id
;
3727 *tbl
<< TextTable::endrow
;
3732 class CrushTreeFormattingDumper
: public CrushTreeDumper::FormattingDumper
{
3734 typedef CrushTreeDumper::FormattingDumper Parent
;
3736 explicit CrushTreeFormattingDumper(
3737 const CrushWrapper
*crush
,
3738 const CrushTreeDumper::name_map_t
& wsnames
)
3739 : Parent(crush
, wsnames
) {}
3741 explicit CrushTreeFormattingDumper(
3742 const CrushWrapper
*crush
,
3743 const CrushTreeDumper::name_map_t
& wsnames
,
3745 : Parent(crush
, wsnames
, show_shadow
) {}
3747 void dump(Formatter
*f
) {
3748 f
->open_array_section("nodes");
3752 // There is no stray bucket whose id is a negative number, so just get
3753 // the max_id and iterate from 0 to max_id to dump stray osds.
3754 f
->open_array_section("stray");
3755 int32_t max_id
= -1;
3756 if (!crush
->name_map
.empty()) {
3757 max_id
= crush
->name_map
.rbegin()->first
;
3759 for (int32_t i
= 0; i
<= max_id
; i
++) {
3760 if (crush
->item_exists(i
) && !is_touched(i
) && should_dump(i
)) {
3761 dump_item(CrushTreeDumper::Item(i
, 0, 0, 0), f
);
3769 void CrushWrapper::dump_tree(
3772 const CrushTreeDumper::name_map_t
& weight_set_names
,
3773 bool show_shadow
) const
3777 CrushTreePlainDumper(this, weight_set_names
, show_shadow
).dump(&tbl
);
3781 CrushTreeFormattingDumper(this, weight_set_names
, show_shadow
).dump(f
);
3785 void CrushWrapper::generate_test_instances(list
<CrushWrapper
*>& o
)
3787 o
.push_back(new CrushWrapper
);
3792 * Determine the default CRUSH rule ID to be used with
3793 * newly created replicated pools.
3795 * @returns a rule ID (>=0) or -1 if no suitable rule found
3797 int CrushWrapper::get_osd_pool_default_crush_replicated_rule(
3800 int crush_rule
= cct
->_conf
.get_val
<int64_t>("osd_pool_default_crush_rule");
3801 if (crush_rule
< 0) {
3802 crush_rule
= find_first_rule(pg_pool_t::TYPE_REPLICATED
);
3803 } else if (!rule_exists(crush_rule
)) {
3804 crush_rule
= -1; // match find_first_rule() retval
3809 bool CrushWrapper::is_valid_crush_name(const string
& s
)
3813 for (string::const_iterator p
= s
.begin(); p
!= s
.end(); ++p
) {
3817 !(*p
>= '0' && *p
<= '9') &&
3818 !(*p
>= 'A' && *p
<= 'Z') &&
3819 !(*p
>= 'a' && *p
<= 'z'))
3825 bool CrushWrapper::is_valid_crush_loc(CephContext
*cct
,
3826 const map
<string
,string
>& loc
)
3828 for (map
<string
,string
>::const_iterator l
= loc
.begin(); l
!= loc
.end(); ++l
) {
3829 if (!is_valid_crush_name(l
->first
) ||
3830 !is_valid_crush_name(l
->second
)) {
3831 ldout(cct
, 1) << "loc["
3832 << l
->first
<< "] = '"
3833 << l
->second
<< "' not a valid crush name ([A-Za-z0-9_-.]+)"
3841 int CrushWrapper::_choose_type_stack(
3843 const vector
<pair
<int,int>>& stack
,
3844 const set
<int>& overfull
,
3845 const vector
<int>& underfull
,
3846 const vector
<int>& more_underfull
,
3847 const vector
<int>& orig
,
3848 vector
<int>::const_iterator
& i
,
3854 vector
<int> w
= *pw
;
3857 ldout(cct
, 10) << __func__
<< " stack " << stack
3862 ceph_assert(root_bucket
< 0);
3863 vector
<int> cumulative_fanout(stack
.size());
3865 for (int j
= (int)stack
.size() - 1; j
>= 0; --j
) {
3866 cumulative_fanout
[j
] = f
;
3867 f
*= stack
[j
].second
;
3869 ldout(cct
, 10) << __func__
<< " cumulative_fanout " << cumulative_fanout
3872 // identify underfull targets for each intermediate level.
3873 // this serves two purposes:
3874 // 1. we can tell when we are selecting a bucket that does not have any underfull
3875 // devices beneath it. that means that if the current input includes an overfull
3876 // device, we won't be able to find an underfull device with this parent to
3878 // 2. when we decide we should reject a bucket due to the above, this list gives us
3879 // a list of peers to consider that *do* have underfull devices available.. (we
3880 // are careful to pick one that has the same parent.)
3881 vector
<set
<int>> underfull_buckets
; // level -> set of buckets with >0 underfull item(s)
3882 underfull_buckets
.resize(stack
.size() - 1);
3883 for (auto osd
: underfull
) {
3885 for (int j
= (int)stack
.size() - 2; j
>= 0; --j
) {
3886 int type
= stack
[j
].first
;
3887 item
= get_parent_of_type(item
, type
, rule
);
3888 ldout(cct
, 10) << __func__
<< " underfull " << osd
<< " type " << type
3889 << " is " << item
<< dendl
;
3890 if (!subtree_contains(root_bucket
, item
)) {
3891 ldout(cct
, 20) << __func__
<< " not in root subtree " << root_bucket
<< dendl
;
3894 underfull_buckets
[j
].insert(item
);
3897 ldout(cct
, 20) << __func__
<< " underfull_buckets " << underfull_buckets
<< dendl
;
3899 for (unsigned j
= 0; j
< stack
.size(); ++j
) {
3900 int type
= stack
[j
].first
;
3901 int fanout
= stack
[j
].second
;
3902 int cum_fanout
= cumulative_fanout
[j
];
3903 ldout(cct
, 10) << " level " << j
<< ": type " << type
<< " fanout " << fanout
3904 << " cumulative " << cum_fanout
3905 << " w " << w
<< dendl
;
3908 if (i
== orig
.end()) {
3909 ldout(cct
, 10) << __func__
<< " end of orig, break 0" << dendl
;
3912 for (auto from
: w
) {
3913 ldout(cct
, 10) << " from " << from
<< dendl
;
3914 // identify leaves under each choice. we use this to check whether any of these
3915 // leaves are overfull. (if so, we need to make sure there are underfull candidates
3916 // to swap for them.)
3917 vector
<set
<int>> leaves
;
3918 leaves
.resize(fanout
);
3919 for (int pos
= 0; pos
< fanout
; ++pos
) {
3922 int item
= get_parent_of_type(*tmpi
, type
, rule
);
3925 while (n
-- && tmpi
!= orig
.end()) {
3926 leaves
[pos
].insert(*tmpi
++);
3928 ldout(cct
, 10) << __func__
<< " from " << *tmpi
<< " got " << item
3929 << " of type " << type
<< " over leaves " << leaves
[pos
] << dendl
;
3932 bool replaced
= false;
3933 if (overfull
.count(*i
)) {
3934 for (auto item
: underfull
) {
3935 ldout(cct
, 10) << __func__
<< " pos " << pos
3936 << " was " << *i
<< " considering " << item
3938 if (used
.count(item
)) {
3939 ldout(cct
, 20) << __func__
<< " in used " << used
<< dendl
;
3942 if (!subtree_contains(from
, item
)) {
3943 ldout(cct
, 20) << __func__
<< " not in subtree " << from
<< dendl
;
3946 if (std::find(orig
.begin(), orig
.end(), item
) != orig
.end()) {
3947 ldout(cct
, 20) << __func__
<< " in orig " << orig
<< dendl
;
3952 ldout(cct
, 10) << __func__
<< " pos " << pos
<< " replace "
3953 << *i
<< " -> " << item
<< dendl
;
3955 ceph_assert(i
!= orig
.end());
3960 for (auto item
: more_underfull
) {
3961 ldout(cct
, 10) << __func__
<< " more underfull pos " << pos
3962 << " was " << *i
<< " considering " << item
3964 if (used
.count(item
)) {
3965 ldout(cct
, 20) << __func__
<< " in used " << used
<< dendl
;
3968 if (!subtree_contains(from
, item
)) {
3969 ldout(cct
, 20) << __func__
<< " not in subtree " << from
<< dendl
;
3972 if (std::find(orig
.begin(), orig
.end(), item
) != orig
.end()) {
3973 ldout(cct
, 20) << __func__
<< " in orig " << orig
<< dendl
;
3978 ldout(cct
, 10) << __func__
<< " pos " << pos
<< " replace "
3979 << *i
<< " -> " << item
<< dendl
;
3981 assert(i
!= orig
.end());
3988 ldout(cct
, 10) << __func__
<< " pos " << pos
<< " keep " << *i
3990 ceph_assert(i
!= orig
.end());
3994 if (i
== orig
.end()) {
3995 ldout(cct
, 10) << __func__
<< " end of orig, break 1" << dendl
;
4000 if (j
+ 1 < stack
.size()) {
4001 // check if any buckets have overfull leaves but no underfull candidates
4002 for (int pos
= 0; pos
< fanout
; ++pos
) {
4003 if (underfull_buckets
[j
].count(o
[pos
]) == 0) {
4004 // are any leaves overfull?
4005 bool any_overfull
= false;
4006 for (auto osd
: leaves
[pos
]) {
4007 if (overfull
.count(osd
)) {
4008 any_overfull
= true;
4013 ldout(cct
, 10) << " bucket " << o
[pos
] << " has no underfull targets and "
4014 << ">0 leaves " << leaves
[pos
] << " is overfull; alts "
4015 << underfull_buckets
[j
]
4017 for (auto alt
: underfull_buckets
[j
]) {
4018 if (std::find(o
.begin(), o
.end(), alt
) == o
.end()) {
4019 // see if alt has the same parent
4021 get_parent_of_type(o
[pos
], stack
[j
-1].first
, rule
) ==
4022 get_parent_of_type(alt
, stack
[j
-1].first
, rule
)) {
4024 ldout(cct
, 10) << " replacing " << o
[pos
]
4025 << " (which has no underfull leaves) with " << alt
4027 << get_parent_of_type(alt
, stack
[j
-1].first
, rule
) << " type "
4028 << type
<< ")" << dendl
;
4030 ldout(cct
, 10) << " replacing " << o
[pos
]
4031 << " (which has no underfull leaves) with " << alt
4032 << " (first level)" << dendl
;
4036 ldout(cct
, 30) << " alt " << alt
<< " for " << o
[pos
]
4037 << " has different parent, skipping" << dendl
;
4045 if (i
== orig
.end()) {
4046 ldout(cct
, 10) << __func__
<< " end of orig, break 2" << dendl
;
4050 ldout(cct
, 10) << __func__
<< " w <- " << o
<< " was " << w
<< dendl
;
4057 int CrushWrapper::try_remap_rule(
4061 const set
<int>& overfull
,
4062 const vector
<int>& underfull
,
4063 const vector
<int>& more_underfull
,
4064 const vector
<int>& orig
,
4065 vector
<int> *out
) const
4067 const crush_map
*map
= crush
;
4068 const crush_rule
*rule
= get_rule(ruleno
);
4071 ldout(cct
, 10) << __func__
<< " ruleno " << ruleno
4072 << " numrep " << maxout
<< " overfull " << overfull
4073 << " underfull " << underfull
4074 << " more_underfull " << more_underfull
4077 vector
<int> w
; // working set
4080 auto i
= orig
.begin();
4083 vector
<pair
<int,int>> type_stack
; // (type, fan-out)
4084 int root_bucket
= 0;
4085 for (unsigned step
= 0; step
< rule
->len
; ++step
) {
4086 const crush_rule_step
*curstep
= &rule
->steps
[step
];
4087 ldout(cct
, 10) << __func__
<< " step " << step
<< " w " << w
<< dendl
;
4088 switch (curstep
->op
) {
4089 case CRUSH_RULE_TAKE
:
4090 if ((curstep
->arg1
>= 0 && curstep
->arg1
< map
->max_devices
) ||
4091 (-1-curstep
->arg1
>= 0 && -1-curstep
->arg1
< map
->max_buckets
&&
4092 map
->buckets
[-1-curstep
->arg1
])) {
4094 w
.push_back(curstep
->arg1
);
4095 root_bucket
= curstep
->arg1
;
4096 ldout(cct
, 10) << __func__
<< " take " << w
<< dendl
;
4098 ldout(cct
, 1) << " bad take value " << curstep
->arg1
<< dendl
;
4102 case CRUSH_RULE_CHOOSELEAF_FIRSTN
:
4103 case CRUSH_RULE_CHOOSELEAF_INDEP
:
4105 int numrep
= curstep
->arg1
;
4106 int type
= curstep
->arg2
;
4109 type_stack
.push_back(make_pair(type
, numrep
));
4111 type_stack
.push_back(make_pair(0, 1));
4112 int r
= _choose_type_stack(cct
, type_stack
, overfull
, underfull
, more_underfull
, orig
,
4113 i
, used
, &w
, root_bucket
, ruleno
);
4120 case CRUSH_RULE_CHOOSE_FIRSTN
:
4121 case CRUSH_RULE_CHOOSE_INDEP
:
4123 int numrep
= curstep
->arg1
;
4124 int type
= curstep
->arg2
;
4127 type_stack
.push_back(make_pair(type
, numrep
));
4131 case CRUSH_RULE_EMIT
:
4132 ldout(cct
, 10) << " emit " << w
<< dendl
;
4133 if (!type_stack
.empty()) {
4134 int r
= _choose_type_stack(cct
, type_stack
, overfull
, underfull
, more_underfull
, orig
,
4135 i
, used
, &w
, root_bucket
, ruleno
);
4140 for (auto item
: w
) {
4141 out
->push_back(item
);
4156 int CrushWrapper::_choose_args_adjust_item_weight_in_bucket(
4158 crush_choose_arg_map cmap
,
4161 const vector
<int>& weight
,
4165 int bidx
= -1 - bucketid
;
4166 crush_bucket
*b
= crush
->buckets
[bidx
];
4167 if (bidx
>= (int)cmap
.size
) {
4169 *ss
<< "no weight-set for bucket " << b
->id
;
4170 ldout(cct
, 10) << __func__
<< " no crush_choose_arg for bucket " << b
->id
4174 crush_choose_arg
*carg
= &cmap
.args
[bidx
];
4175 if (carg
->weight_set
== NULL
) {
4176 // create a weight-set for this bucket and populate it with the
4178 unsigned positions
= get_choose_args_positions(cmap
);
4179 carg
->weight_set_positions
= positions
;
4180 carg
->weight_set
= static_cast<crush_weight_set
*>(
4181 calloc(sizeof(crush_weight_set
), positions
));
4182 for (unsigned p
= 0; p
< positions
; ++p
) {
4183 carg
->weight_set
[p
].size
= b
->size
;
4184 carg
->weight_set
[p
].weights
= (__u32
*)calloc(b
->size
, sizeof(__u32
));
4185 for (unsigned i
= 0; i
< b
->size
; ++i
) {
4186 carg
->weight_set
[p
].weights
[i
] = crush_get_bucket_item_weight(b
, i
);
4191 if (carg
->weight_set_positions
!= weight
.size()) {
4193 *ss
<< "weight_set_positions != " << weight
.size() << " for bucket " << b
->id
;
4194 ldout(cct
, 10) << __func__
<< " weight_set_positions != " << weight
.size()
4195 << " for bucket " << b
->id
<< dendl
;
4198 for (unsigned i
= 0; i
< b
->size
; i
++) {
4199 if (b
->items
[i
] == id
) {
4200 for (unsigned j
= 0; j
< weight
.size(); ++j
) {
4201 carg
->weight_set
[j
].weights
[i
] = weight
[j
];
4203 ldout(cct
, 5) << __func__
<< " set " << id
<< " to " << weight
4204 << " in bucket " << b
->id
<< dendl
;
4209 vector
<int> bucket_weight(weight
.size(), 0);
4210 for (unsigned i
= 0; i
< b
->size
; i
++) {
4211 for (unsigned j
= 0; j
< weight
.size(); ++j
) {
4212 bucket_weight
[j
] += carg
->weight_set
[j
].weights
[i
];
4215 choose_args_adjust_item_weight(cct
, cmap
, b
->id
, bucket_weight
, nullptr);
4220 int CrushWrapper::choose_args_adjust_item_weight(
4222 crush_choose_arg_map cmap
,
4224 const vector
<int>& weight
,
4227 ldout(cct
, 5) << __func__
<< " " << id
<< " weight " << weight
<< dendl
;
4229 for (int bidx
= 0; bidx
< crush
->max_buckets
; bidx
++) {
4230 crush_bucket
*b
= crush
->buckets
[bidx
];
4234 changed
+= _choose_args_adjust_item_weight_in_bucket(
4235 cct
, cmap
, b
->id
, id
, weight
, ss
);
4239 *ss
<< "item " << id
<< " not found in crush map";