1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #ifndef CEPH_CRUSH_WRAPPER_H
5 #define CEPH_CRUSH_WRAPPER_H
14 #include "include/types.h"
23 #include "include/ceph_assert.h"
24 #include "include/err.h"
25 #include "include/encoding.h"
26 #include "include/mempool.h"
28 #include "common/Mutex.h"
34 namespace CrushTreeDumper
{
35 typedef mempool::osdmap::map
<int64_t,string
> name_map_t
;
38 WRITE_RAW_ENCODER(crush_rule_mask
) // it's all u8's
40 inline void encode(const crush_rule_step
&s
, bufferlist
&bl
)
47 inline void decode(crush_rule_step
&s
, bufferlist::const_iterator
&p
)
57 // magic value used by OSDMap for a "default" fallback choose_args, used if
58 // the choose_arg_map passed to do_rule does not exist. if this also
59 // doesn't exist, fall back to canonical weights.
61 DEFAULT_CHOOSE_ARGS
= -1
64 std::map
<int32_t, string
> type_map
; /* bucket/device type names */
65 std::map
<int32_t, string
> name_map
; /* bucket/device names */
66 std::map
<int32_t, string
> rule_name_map
;
68 std::map
<int32_t, int32_t> class_map
; /* item id -> class id */
69 std::map
<int32_t, string
> class_name
; /* class id -> class name */
70 std::map
<string
, int32_t> class_rname
; /* class name -> class id */
71 std::map
<int32_t, map
<int32_t, int32_t> > class_bucket
; /* bucket[id][class] == id */
72 std::map
<int64_t, crush_choose_arg_map
> choose_args
;
75 struct crush_map
*crush
= nullptr;
77 bool have_uniform_rules
= false;
80 mutable bool have_rmaps
= false;
81 mutable std::map
<string
, int> type_rmap
, name_rmap
, rule_name_rmap
;
82 void build_rmaps() const {
83 if (have_rmaps
) return;
84 build_rmap(type_map
, type_rmap
);
85 build_rmap(name_map
, name_rmap
);
86 build_rmap(rule_name_map
, rule_name_rmap
);
89 void build_rmap(const map
<int, string
> &f
, std::map
<string
, int> &r
) const {
91 for (std::map
<int, string
>::const_iterator p
= f
.begin(); p
!= f
.end(); ++p
)
92 r
[p
->second
] = p
->first
;
96 CrushWrapper(const CrushWrapper
& other
);
97 const CrushWrapper
& operator=(const CrushWrapper
& other
);
104 crush_destroy(crush
);
108 crush_map
*get_crush_map() { return crush
; }
113 crush_destroy(crush
);
114 crush
= crush_create();
119 set_tunables_default();
123 * true if any rule has a rule id != its position in the array
125 * These indicate "ruleset" IDs that were created by older versions
126 * of Ceph. They are cleaned up in renumber_rules so that eventually
127 * we can remove the code for handling them.
129 bool has_legacy_rule_ids() const;
132 * fix rules whose ruleid != ruleset
134 * These rules were created in older versions of Ceph. The concept
135 * of a ruleset no longer exists.
137 * Return a map of old ID -> new ID. Caller must update OSDMap
140 std::map
<int, int> renumber_rules();
142 /// true if any buckets that aren't straw2
143 bool has_non_straw2_buckets() const;
146 void set_tunables_argonaut() {
147 crush
->choose_local_tries
= 2;
148 crush
->choose_local_fallback_tries
= 5;
149 crush
->choose_total_tries
= 19;
150 crush
->chooseleaf_descend_once
= 0;
151 crush
->chooseleaf_vary_r
= 0;
152 crush
->chooseleaf_stable
= 0;
153 crush
->allowed_bucket_algs
= CRUSH_LEGACY_ALLOWED_BUCKET_ALGS
;
155 void set_tunables_bobtail() {
156 crush
->choose_local_tries
= 0;
157 crush
->choose_local_fallback_tries
= 0;
158 crush
->choose_total_tries
= 50;
159 crush
->chooseleaf_descend_once
= 1;
160 crush
->chooseleaf_vary_r
= 0;
161 crush
->chooseleaf_stable
= 0;
162 crush
->allowed_bucket_algs
= CRUSH_LEGACY_ALLOWED_BUCKET_ALGS
;
164 void set_tunables_firefly() {
165 crush
->choose_local_tries
= 0;
166 crush
->choose_local_fallback_tries
= 0;
167 crush
->choose_total_tries
= 50;
168 crush
->chooseleaf_descend_once
= 1;
169 crush
->chooseleaf_vary_r
= 1;
170 crush
->chooseleaf_stable
= 0;
171 crush
->allowed_bucket_algs
= CRUSH_LEGACY_ALLOWED_BUCKET_ALGS
;
173 void set_tunables_hammer() {
174 crush
->choose_local_tries
= 0;
175 crush
->choose_local_fallback_tries
= 0;
176 crush
->choose_total_tries
= 50;
177 crush
->chooseleaf_descend_once
= 1;
178 crush
->chooseleaf_vary_r
= 1;
179 crush
->chooseleaf_stable
= 0;
180 crush
->allowed_bucket_algs
=
181 (1 << CRUSH_BUCKET_UNIFORM
) |
182 (1 << CRUSH_BUCKET_LIST
) |
183 (1 << CRUSH_BUCKET_STRAW
) |
184 (1 << CRUSH_BUCKET_STRAW2
);
186 void set_tunables_jewel() {
187 crush
->choose_local_tries
= 0;
188 crush
->choose_local_fallback_tries
= 0;
189 crush
->choose_total_tries
= 50;
190 crush
->chooseleaf_descend_once
= 1;
191 crush
->chooseleaf_vary_r
= 1;
192 crush
->chooseleaf_stable
= 1;
193 crush
->allowed_bucket_algs
=
194 (1 << CRUSH_BUCKET_UNIFORM
) |
195 (1 << CRUSH_BUCKET_LIST
) |
196 (1 << CRUSH_BUCKET_STRAW
) |
197 (1 << CRUSH_BUCKET_STRAW2
);
200 void set_tunables_legacy() {
201 set_tunables_argonaut();
202 crush
->straw_calc_version
= 0;
204 void set_tunables_optimal() {
205 set_tunables_jewel();
206 crush
->straw_calc_version
= 1;
208 void set_tunables_default() {
209 set_tunables_jewel();
210 crush
->straw_calc_version
= 1;
213 int get_choose_local_tries() const {
214 return crush
->choose_local_tries
;
216 void set_choose_local_tries(int n
) {
217 crush
->choose_local_tries
= n
;
220 int get_choose_local_fallback_tries() const {
221 return crush
->choose_local_fallback_tries
;
223 void set_choose_local_fallback_tries(int n
) {
224 crush
->choose_local_fallback_tries
= n
;
227 int get_choose_total_tries() const {
228 return crush
->choose_total_tries
;
230 void set_choose_total_tries(int n
) {
231 crush
->choose_total_tries
= n
;
234 int get_chooseleaf_descend_once() const {
235 return crush
->chooseleaf_descend_once
;
237 void set_chooseleaf_descend_once(int n
) {
238 crush
->chooseleaf_descend_once
= !!n
;
241 int get_chooseleaf_vary_r() const {
242 return crush
->chooseleaf_vary_r
;
244 void set_chooseleaf_vary_r(int n
) {
245 crush
->chooseleaf_vary_r
= n
;
248 int get_chooseleaf_stable() const {
249 return crush
->chooseleaf_stable
;
251 void set_chooseleaf_stable(int n
) {
252 crush
->chooseleaf_stable
= n
;
255 int get_straw_calc_version() const {
256 return crush
->straw_calc_version
;
258 void set_straw_calc_version(int n
) {
259 crush
->straw_calc_version
= n
;
262 unsigned get_allowed_bucket_algs() const {
263 return crush
->allowed_bucket_algs
;
265 void set_allowed_bucket_algs(unsigned n
) {
266 crush
->allowed_bucket_algs
= n
;
269 bool has_argonaut_tunables() const {
271 crush
->choose_local_tries
== 2 &&
272 crush
->choose_local_fallback_tries
== 5 &&
273 crush
->choose_total_tries
== 19 &&
274 crush
->chooseleaf_descend_once
== 0 &&
275 crush
->chooseleaf_vary_r
== 0 &&
276 crush
->chooseleaf_stable
== 0 &&
277 crush
->allowed_bucket_algs
== CRUSH_LEGACY_ALLOWED_BUCKET_ALGS
;
279 bool has_bobtail_tunables() const {
281 crush
->choose_local_tries
== 0 &&
282 crush
->choose_local_fallback_tries
== 0 &&
283 crush
->choose_total_tries
== 50 &&
284 crush
->chooseleaf_descend_once
== 1 &&
285 crush
->chooseleaf_vary_r
== 0 &&
286 crush
->chooseleaf_stable
== 0 &&
287 crush
->allowed_bucket_algs
== CRUSH_LEGACY_ALLOWED_BUCKET_ALGS
;
289 bool has_firefly_tunables() const {
291 crush
->choose_local_tries
== 0 &&
292 crush
->choose_local_fallback_tries
== 0 &&
293 crush
->choose_total_tries
== 50 &&
294 crush
->chooseleaf_descend_once
== 1 &&
295 crush
->chooseleaf_vary_r
== 1 &&
296 crush
->chooseleaf_stable
== 0 &&
297 crush
->allowed_bucket_algs
== CRUSH_LEGACY_ALLOWED_BUCKET_ALGS
;
299 bool has_hammer_tunables() const {
301 crush
->choose_local_tries
== 0 &&
302 crush
->choose_local_fallback_tries
== 0 &&
303 crush
->choose_total_tries
== 50 &&
304 crush
->chooseleaf_descend_once
== 1 &&
305 crush
->chooseleaf_vary_r
== 1 &&
306 crush
->chooseleaf_stable
== 0 &&
307 crush
->allowed_bucket_algs
== ((1 << CRUSH_BUCKET_UNIFORM
) |
308 (1 << CRUSH_BUCKET_LIST
) |
309 (1 << CRUSH_BUCKET_STRAW
) |
310 (1 << CRUSH_BUCKET_STRAW2
));
312 bool has_jewel_tunables() const {
314 crush
->choose_local_tries
== 0 &&
315 crush
->choose_local_fallback_tries
== 0 &&
316 crush
->choose_total_tries
== 50 &&
317 crush
->chooseleaf_descend_once
== 1 &&
318 crush
->chooseleaf_vary_r
== 1 &&
319 crush
->chooseleaf_stable
== 1 &&
320 crush
->allowed_bucket_algs
== ((1 << CRUSH_BUCKET_UNIFORM
) |
321 (1 << CRUSH_BUCKET_LIST
) |
322 (1 << CRUSH_BUCKET_STRAW
) |
323 (1 << CRUSH_BUCKET_STRAW2
));
326 bool has_optimal_tunables() const {
327 return has_jewel_tunables();
329 bool has_legacy_tunables() const {
330 return has_argonaut_tunables();
333 bool has_nondefault_tunables() const {
335 (crush
->choose_local_tries
!= 2 ||
336 crush
->choose_local_fallback_tries
!= 5 ||
337 crush
->choose_total_tries
!= 19);
339 bool has_nondefault_tunables2() const {
341 crush
->chooseleaf_descend_once
!= 0;
343 bool has_nondefault_tunables3() const {
345 crush
->chooseleaf_vary_r
!= 0;
347 bool has_nondefault_tunables5() const {
349 crush
->chooseleaf_stable
!= 0;
352 bool has_v2_rules() const;
353 bool has_v3_rules() const;
354 bool has_v4_buckets() const;
355 bool has_v5_rules() const;
356 bool has_choose_args() const; // any choose_args
357 bool has_incompat_choose_args() const; // choose_args that can't be made compat
359 bool is_v2_rule(unsigned ruleid
) const;
360 bool is_v3_rule(unsigned ruleid
) const;
361 bool is_v5_rule(unsigned ruleid
) const;
363 string
get_min_required_version() const {
364 if (has_v5_rules() || has_nondefault_tunables5())
366 else if (has_v4_buckets())
368 else if (has_nondefault_tunables3())
370 else if (has_nondefault_tunables2() || has_nondefault_tunables())
376 // default bucket types
377 unsigned get_default_bucket_alg() const {
378 // in order of preference
379 if (crush
->allowed_bucket_algs
& (1 << CRUSH_BUCKET_STRAW2
))
380 return CRUSH_BUCKET_STRAW2
;
381 if (crush
->allowed_bucket_algs
& (1 << CRUSH_BUCKET_STRAW
))
382 return CRUSH_BUCKET_STRAW
;
383 if (crush
->allowed_bucket_algs
& (1 << CRUSH_BUCKET_TREE
))
384 return CRUSH_BUCKET_TREE
;
385 if (crush
->allowed_bucket_algs
& (1 << CRUSH_BUCKET_LIST
))
386 return CRUSH_BUCKET_LIST
;
387 if (crush
->allowed_bucket_algs
& (1 << CRUSH_BUCKET_UNIFORM
))
388 return CRUSH_BUCKET_UNIFORM
;
393 int get_num_type_names() const {
394 return type_map
.size();
396 int get_max_type_id() const {
397 if (type_map
.empty())
399 return type_map
.rbegin()->first
;
401 int get_type_id(const string
& name
) const {
403 if (type_rmap
.count(name
))
404 return type_rmap
[name
];
407 const char *get_type_name(int t
) const {
408 std::map
<int,string
>::const_iterator p
= type_map
.find(t
);
409 if (p
!= type_map
.end())
410 return p
->second
.c_str();
413 void set_type_name(int i
, const string
& name
) {
420 bool name_exists(const string
& name
) const {
422 return name_rmap
.count(name
);
424 bool item_exists(int i
) const {
425 return name_map
.count(i
);
427 int get_item_id(const string
& name
) const {
429 if (name_rmap
.count(name
))
430 return name_rmap
[name
];
433 const char *get_item_name(int t
) const {
434 std::map
<int,string
>::const_iterator p
= name_map
.find(t
);
435 if (p
!= name_map
.end())
436 return p
->second
.c_str();
439 int set_item_name(int i
, const string
& name
) {
440 if (!is_valid_crush_name(name
))
447 void swap_names(int a
, int b
) {
448 string an
= name_map
[a
];
449 string bn
= name_map
[b
];
457 int split_id_class(int i
, int *idout
, int *classout
) const;
459 bool class_exists(const string
& name
) const {
460 return class_rname
.count(name
);
462 const char *get_class_name(int i
) const {
463 auto p
= class_name
.find(i
);
464 if (p
!= class_name
.end())
465 return p
->second
.c_str();
468 int get_class_id(const string
& name
) const {
469 auto p
= class_rname
.find(name
);
470 if (p
!= class_rname
.end())
475 int remove_class_name(const string
& name
) {
476 auto p
= class_rname
.find(name
);
477 if (p
== class_rname
.end())
479 int class_id
= p
->second
;
480 auto q
= class_name
.find(class_id
);
481 if (q
== class_name
.end())
483 class_rname
.erase(name
);
484 class_name
.erase(class_id
);
488 int32_t _alloc_class_id() const;
490 int get_or_create_class_id(const string
& name
) {
491 int c
= get_class_id(name
);
493 int i
= _alloc_class_id();
494 class_name
[i
] = name
;
495 class_rname
[name
] = i
;
502 const char *get_item_class(int t
) const {
503 std::map
<int,int>::const_iterator p
= class_map
.find(t
);
504 if (p
== class_map
.end())
506 return get_class_name(p
->second
);
508 int get_item_class_id(int t
) const {
509 auto p
= class_map
.find(t
);
510 if (p
== class_map
.end())
514 int set_item_class(int i
, const string
& name
) {
515 if (!is_valid_crush_name(name
))
517 class_map
[i
] = get_or_create_class_id(name
);
520 int set_item_class(int i
, int c
) {
524 void get_devices_by_class(const string
&name
, set
<int> *devices
) const {
525 ceph_assert(devices
);
527 if (!class_exists(name
)) {
530 auto cid
= get_class_id(name
);
531 for (auto& p
: class_map
) {
532 if (p
.first
>= 0 && p
.second
== cid
) {
533 devices
->insert(p
.first
);
537 void class_remove_item(int i
) {
538 auto it
= class_map
.find(i
);
539 if (it
== class_map
.end()) {
544 int can_rename_item(const string
& srcname
,
545 const string
& dstname
,
547 int rename_item(const string
& srcname
,
548 const string
& dstname
,
550 int can_rename_bucket(const string
& srcname
,
551 const string
& dstname
,
553 int rename_bucket(const string
& srcname
,
554 const string
& dstname
,
558 int rename_rule(const string
& srcname
,
559 const string
& dstname
,
561 bool rule_exists(string name
) const {
563 return rule_name_rmap
.count(name
);
565 int get_rule_id(string name
) const {
567 if (rule_name_rmap
.count(name
))
568 return rule_name_rmap
[name
];
571 const char *get_rule_name(int t
) const {
572 std::map
<int,string
>::const_iterator p
= rule_name_map
.find(t
);
573 if (p
!= rule_name_map
.end())
574 return p
->second
.c_str();
577 void set_rule_name(int i
, const string
& name
) {
578 rule_name_map
[i
] = name
;
580 rule_name_rmap
[name
] = i
;
582 bool is_shadow_item(int id
) const {
583 const char *name
= get_item_name(id
);
584 return name
&& !is_valid_crush_name(name
);
589 * find tree nodes referenced by rules by a 'take' command
591 * Note that these may not be parentless roots.
593 void find_takes(set
<int> *roots
) const;
594 void find_takes_by_rule(int rule
, set
<int> *roots
) const;
599 * These are parentless nodes in the map.
601 void find_roots(set
<int> *roots
) const;
605 * find tree roots that contain shadow (device class) items only
607 void find_shadow_roots(set
<int> *roots
) const {
611 if (is_shadow_item(p
)) {
618 * find tree roots that are not shadow (device class) items
620 * These are parentless nodes in the map that are not shadow
621 * items for device classes.
623 void find_nonshadow_roots(set
<int> *roots
) const {
627 if (!is_shadow_item(p
)) {
634 * see if an item is contained within a subtree
636 * @param root haystack
638 * @return true if the item is located beneath the given node
640 bool subtree_contains(int root
, int item
) const;
644 * search for an item in any bucket
647 * @return true if present
649 bool _search_item_exists(int i
) const;
650 bool is_parent_of(int child
, int p
) const;
654 * see if item is located where we think it is
656 * This verifies that the given item is located at a particular
657 * location in the hierarchy. However, that check is imprecise; we
658 * are actually verifying that the most specific location key/value
659 * is correct. For example, if loc specifies that rack=foo and
660 * host=bar, it will verify that host=bar is correct; any placement
661 * above that level in the hierarchy is ignored. This matches the
662 * semantics for insert_item().
665 * @param item item id
666 * @param loc location to check (map of type to bucket names)
667 * @param weight optional pointer to weight of item at that location
668 * @return true if item is at specified location
670 bool check_item_loc(CephContext
*cct
, int item
, const map
<string
,string
>& loc
, int *iweight
);
671 bool check_item_loc(CephContext
*cct
, int item
, const map
<string
,string
>& loc
, float *weight
) {
673 bool ret
= check_item_loc(cct
, item
, loc
, &iweight
);
675 *weight
= (float)iweight
/ (float)0x10000;
681 * returns the (type, name) of the parent bucket of id
683 * FIXME: ambiguous for items that occur multiple times in the map
685 pair
<string
,string
> get_immediate_parent(int id
, int *ret
= NULL
) const;
687 int get_immediate_parent_id(int id
, int *parent
) const;
690 * return ancestor of the given type, or 0 if none
691 * can pass in a specific crush **rule** to return ancestor from that rule only
692 * (parent is always a bucket and thus <0)
694 int get_parent_of_type(int id
, int type
, int rule
= -1) const;
697 * get the fully qualified location of a device by successively finding
698 * parents beginning at ID and ending at highest type number specified in
699 * the CRUSH map which assumes that if device foo is under device bar, the
700 * type_id of foo < bar where type_id is the integer specified in the CRUSH map
702 * returns the location in the form of (type=foo) where type is a type of bucket
703 * specified in the CRUSH map and foo is a name specified in the CRUSH map
705 map
<string
, string
> get_full_location(int id
) const;
708 * return location map for a item, by name
710 int get_full_location(
712 std::map
<string
,string
> *ploc
);
715 * identical to get_full_location(int id) although it returns the type/name
716 * pairs in the order they occur in the hierarchy.
718 * returns -ENOENT if id is not found.
720 int get_full_location_ordered(int id
, vector
<pair
<string
, string
> >& path
) const;
723 * identical to get_full_location_ordered(int id, vector<pair<string, string> >& path),
724 * although it returns a concatenated string with the type/name pairs in descending
725 * hierarchical order with format key1=val1,key2=val2.
727 * returns the location in descending hierarchy as a string.
729 string
get_full_location_ordered_string(int id
) const;
732 * returns (type_id, type) of all parent buckets between id and
733 * default, can be used to check for anomalous CRUSH maps
735 map
<int, string
> get_parent_hierarchy(int id
) const;
738 * enumerate immediate children of given node
740 * @param id parent bucket or device id
741 * @return number of items, or error
743 int get_children(int id
, list
<int> *children
) const;
745 * enumerate all children of given node
747 * @param id parent bucket or device id
748 * @return number of items, or error
750 int get_all_children(int id
, set
<int> *children
) const;
751 void get_children_of_type(int id
,
753 vector
<int> *children
,
754 bool exclude_shadow
= true) const;
756 * enumerate all subtrees by type
758 void get_subtree_of_type(int type
, vector
<int> *subtrees
);
762 * verify upmapping results.
763 * return 0 on success or a negative errno on error.
765 int verify_upmap(CephContext
*cct
,
768 const vector
<int>& up
);
771 * enumerate leaves(devices) of given node
773 * @param name parent bucket name
774 * @return 0 on success or a negative errno on error.
776 int get_leaves(const string
&name
, set
<int> *leaves
) const;
779 int _get_leaves(int id
, list
<int> *leaves
) const; // worker
783 * insert an item into the map at a specific position
785 * Add an item as a specific location of the hierarchy.
786 * Specifically, we look for the most specific location constraint
787 * for which a bucket already exists, and then create intervening
788 * buckets beneath that in order to place the item.
790 * Note that any location specifiers *above* the most specific match
791 * are ignored. For example, if we specify that osd.12 goes in
792 * host=foo, rack=bar, and row=baz, and rack=bar is the most
793 * specific match, we will create host=foo beneath that point and
794 * put osd.12 inside it. However, we will not verify that rack=bar
795 * is beneath row=baz or move it.
797 * In short, we will build out a hierarchy, and move leaves around,
798 * but not adjust the hierarchy's internal structure. Yet.
800 * If the item is already present in the map, we will return EEXIST.
801 * If the location key/value pairs are nonsensical
802 * (rack=nameofdevice), or location specifies that do not attach us
803 * to any existing part of the hierarchy, we will return EINVAL.
807 * @param weight item weight
808 * @param name item name
809 * @param loc location (map of type to bucket names)
810 * @param init_weight_sets initialize weight-set weights to weight (vs 0)
811 * @return 0 for success, negative on error
813 int insert_item(CephContext
*cct
, int id
, float weight
, string name
,
814 const map
<string
,string
>& loc
,
815 bool init_weight_sets
=true);
818 * move a bucket in the hierarchy to the given location
820 * This has the same location and ancestor creation behavior as
821 * insert_item(), but will relocate the specified existing bucket.
824 * @param id bucket id
825 * @param loc location (map of type to bucket names)
826 * @return 0 for success, negative on error
828 int move_bucket(CephContext
*cct
, int id
, const map
<string
,string
>& loc
);
831 * swap bucket contents of two buckets without touching bucket ids
834 * @param src bucket a
835 * @param dst bucket b
836 * @return 0 for success, negative on error
838 int swap_bucket(CephContext
*cct
, int src
, int dst
);
841 * add a link to an existing bucket in the hierarchy to the new location
843 * This has the same location and ancestor creation behavior as
844 * insert_item(), but will add a new link to the specified existing
848 * @param id bucket id
849 * @param loc location (map of type to bucket names)
850 * @return 0 for success, negative on error
852 int link_bucket(CephContext
*cct
, int id
, const map
<string
,string
>& loc
);
855 * add or update an item's position in the map
857 * This is analogous to insert_item, except we will move an item if
858 * it is already present.
862 * @param weight item weight
863 * @param name item name
864 * @param loc location (map of type to bucket names)
865 * @return 0 for no change, 1 for successful change, negative on error
867 int update_item(CephContext
*cct
, int id
, float weight
, string name
, const map
<string
,string
>& loc
);
870 * create or move an item, but do not adjust its weight if it already exists
873 * @param item item id
874 * @param weight initial item weight (if we need to create it)
875 * @param name item name
876 * @param loc location (map of type to bucket names)
877 * @param init_weight_sets initialize weight-set values to weight (vs 0)
878 * @return 0 for no change, 1 for successful change, negative on error
880 int create_or_move_item(CephContext
*cct
, int item
, float weight
, string name
,
881 const map
<string
,string
>& loc
,
882 bool init_weight_sets
=true);
885 * remove all instances of an item from the map
888 * @param id item id to remove
889 * @param unlink_only unlink but do not remove bucket (useful if multiple links or not empty)
890 * @return 0 on success, negative on error
892 int remove_item(CephContext
*cct
, int id
, bool unlink_only
);
895 * recursively remove buckets starting at item and stop removing
896 * when a bucket is in use.
898 * @param item id to remove
899 * @return 0 on success, negative on error
901 int remove_root(CephContext
*cct
, int item
);
904 * remove all instances of an item nested beneath a certain point from the map
907 * @param id item id to remove
908 * @param ancestor ancestor item id under which to search for id
909 * @param unlink_only unlink but do not remove bucket (useful if bucket has multiple links or is not empty)
910 * @return 0 on success, negative on error
913 bool _maybe_remove_last_instance(CephContext
*cct
, int id
, bool unlink_only
);
914 int _remove_item_under(CephContext
*cct
, int id
, int ancestor
, bool unlink_only
);
915 bool _bucket_is_in_use(int id
);
917 int remove_item_under(CephContext
*cct
, int id
, int ancestor
, bool unlink_only
);
920 * calculate the locality/distance from a given id to a crush location map
922 * Specifically, we look for the lowest-valued type for which the
923 * location of id matches that described in loc.
926 * @param id the existing id in the map
927 * @param loc a set of key=value pairs describing a location in the hierarchy
929 int get_common_ancestor_distance(CephContext
*cct
, int id
,
930 const std::multimap
<string
,string
>& loc
) const;
933 * parse a set of key/value pairs out of a string vector
935 * These are used to describe a location in the CRUSH hierarchy.
937 * @param args list of strings (each key= or key=value)
938 * @param ploc pointer to a resulting location map or multimap
940 static int parse_loc_map(const std::vector
<string
>& args
,
941 std::map
<string
,string
> *ploc
);
942 static int parse_loc_multimap(const std::vector
<string
>& args
,
943 std::multimap
<string
,string
> *ploc
);
947 * get an item's weight
949 * Will return the weight for the first instance it finds.
951 * @param id item id to check
952 * @return weight of item
954 int get_item_weight(int id
) const;
955 float get_item_weightf(int id
) const {
956 return (float)get_item_weight(id
) / (float)0x10000;
958 int get_item_weight_in_loc(int id
, const map
<string
,string
> &loc
);
959 float get_item_weightf_in_loc(int id
, const map
<string
,string
> &loc
) {
960 return (float)get_item_weight_in_loc(id
, loc
) / (float)0x10000;
963 int validate_weightf(float weight
) {
964 uint64_t iweight
= weight
* 0x10000;
965 if (iweight
> std::numeric_limits
<int>::max()) {
970 int adjust_item_weight(CephContext
*cct
, int id
, int weight
,
971 bool update_weight_sets
=true);
972 int adjust_item_weightf(CephContext
*cct
, int id
, float weight
,
973 bool update_weight_sets
=true) {
974 int r
= validate_weightf(weight
);
978 return adjust_item_weight(cct
, id
, (int)(weight
* (float)0x10000),
981 int adjust_item_weight_in_bucket(CephContext
*cct
, int id
, int weight
,
983 bool update_weight_sets
);
984 int adjust_item_weight_in_loc(CephContext
*cct
, int id
, int weight
,
985 const map
<string
,string
>& loc
,
986 bool update_weight_sets
=true);
987 int adjust_item_weightf_in_loc(CephContext
*cct
, int id
, float weight
,
988 const map
<string
,string
>& loc
,
989 bool update_weight_sets
=true) {
990 int r
= validate_weightf(weight
);
994 return adjust_item_weight_in_loc(cct
, id
, (int)(weight
* (float)0x10000),
995 loc
, update_weight_sets
);
997 void reweight(CephContext
*cct
);
998 void reweight_bucket(crush_bucket
*b
,
999 crush_choose_arg_map
& arg_map
,
1000 vector
<uint32_t> *weightv
);
1002 int adjust_subtree_weight(CephContext
*cct
, int id
, int weight
,
1003 bool update_weight_sets
=true);
1004 int adjust_subtree_weightf(CephContext
*cct
, int id
, float weight
,
1005 bool update_weight_sets
=true) {
1006 int r
= validate_weightf(weight
);
1010 return adjust_subtree_weight(cct
, id
, (int)(weight
* (float)0x10000),
1011 update_weight_sets
);
1014 /// check if item id is present in the map hierarchy
1015 bool check_item_present(int id
) const;
1019 int get_max_devices() const {
1020 if (!crush
) return 0;
1021 return crush
->max_devices
;
1027 crush_rule
*get_rule(unsigned ruleno
) const {
1028 if (!crush
) return (crush_rule
*)(-ENOENT
);
1029 if (ruleno
>= crush
->max_rules
)
1031 return crush
->rules
[ruleno
];
1033 crush_rule_step
*get_rule_step(unsigned ruleno
, unsigned step
) const {
1034 crush_rule
*n
= get_rule(ruleno
);
1035 if (IS_ERR(n
)) return (crush_rule_step
*)(-EINVAL
);
1036 if (step
>= n
->len
) return (crush_rule_step
*)(-EINVAL
);
1037 return &n
->steps
[step
];
1042 int get_max_rules() const {
1043 if (!crush
) return 0;
1044 return crush
->max_rules
;
1046 bool rule_exists(unsigned ruleno
) const {
1047 if (!crush
) return false;
1048 if (ruleno
< crush
->max_rules
&&
1049 crush
->rules
[ruleno
] != NULL
)
1053 bool rule_has_take(unsigned ruleno
, int take
) const {
1054 if (!crush
) return false;
1055 crush_rule
*rule
= get_rule(ruleno
);
1056 for (unsigned i
= 0; i
< rule
->len
; ++i
) {
1057 if (rule
->steps
[i
].op
== CRUSH_RULE_TAKE
&&
1058 rule
->steps
[i
].arg1
== take
) {
1064 int get_rule_len(unsigned ruleno
) const {
1065 crush_rule
*r
= get_rule(ruleno
);
1066 if (IS_ERR(r
)) return PTR_ERR(r
);
1069 int get_rule_mask_ruleset(unsigned ruleno
) const {
1070 crush_rule
*r
= get_rule(ruleno
);
1071 if (IS_ERR(r
)) return -1;
1072 return r
->mask
.ruleset
;
1074 int get_rule_mask_type(unsigned ruleno
) const {
1075 crush_rule
*r
= get_rule(ruleno
);
1076 if (IS_ERR(r
)) return -1;
1077 return r
->mask
.type
;
1079 int get_rule_mask_min_size(unsigned ruleno
) const {
1080 crush_rule
*r
= get_rule(ruleno
);
1081 if (IS_ERR(r
)) return -1;
1082 return r
->mask
.min_size
;
1084 int get_rule_mask_max_size(unsigned ruleno
) const {
1085 crush_rule
*r
= get_rule(ruleno
);
1086 if (IS_ERR(r
)) return -1;
1087 return r
->mask
.max_size
;
1089 int get_rule_op(unsigned ruleno
, unsigned step
) const {
1090 crush_rule_step
*s
= get_rule_step(ruleno
, step
);
1091 if (IS_ERR(s
)) return PTR_ERR(s
);
1094 int get_rule_arg1(unsigned ruleno
, unsigned step
) const {
1095 crush_rule_step
*s
= get_rule_step(ruleno
, step
);
1096 if (IS_ERR(s
)) return PTR_ERR(s
);
1099 int get_rule_arg2(unsigned ruleno
, unsigned step
) const {
1100 crush_rule_step
*s
= get_rule_step(ruleno
, step
);
1101 if (IS_ERR(s
)) return PTR_ERR(s
);
1106 float _get_take_weight_osd_map(int root
, map
<int,float> *pmap
) const;
1107 void _normalize_weight_map(float sum
, const map
<int,float>& m
,
1108 map
<int,float> *pmap
) const;
1112 * calculate a map of osds to weights for a given rule
1114 * Generate a map of which OSDs get how much relative weight for a
1117 * @param ruleno [in] rule id
1118 * @param pmap [out] map of osd to weight
1119 * @return 0 for success, or negative error code
1121 int get_rule_weight_osd_map(unsigned ruleno
, map
<int,float> *pmap
) const;
1124 * calculate a map of osds to weights for a given starting root
1126 * Generate a map of which OSDs get how much relative weight for a
1127 * given starting root
1130 * @param pmap [out] map of osd to weight
1131 * @return 0 for success, or negative error code
1133 int get_take_weight_osd_map(int root
, map
<int,float> *pmap
) const;
1137 int add_rule(int ruleno
, int len
, int type
, int minsize
, int maxsize
) {
1138 if (!crush
) return -ENOENT
;
1139 crush_rule
*n
= crush_make_rule(len
, ruleno
, type
, minsize
, maxsize
);
1141 ruleno
= crush_add_rule(crush
, n
, ruleno
);
1144 int set_rule_mask_max_size(unsigned ruleno
, int max_size
) {
1145 crush_rule
*r
= get_rule(ruleno
);
1146 if (IS_ERR(r
)) return -1;
1147 return r
->mask
.max_size
= max_size
;
1149 int set_rule_step(unsigned ruleno
, unsigned step
, int op
, int arg1
, int arg2
) {
1150 if (!crush
) return -ENOENT
;
1151 crush_rule
*n
= get_rule(ruleno
);
1153 crush_rule_set_step(n
, step
, op
, arg1
, arg2
);
1156 int set_rule_step_take(unsigned ruleno
, unsigned step
, int val
) {
1157 return set_rule_step(ruleno
, step
, CRUSH_RULE_TAKE
, val
, 0);
1159 int set_rule_step_set_choose_tries(unsigned ruleno
, unsigned step
, int val
) {
1160 return set_rule_step(ruleno
, step
, CRUSH_RULE_SET_CHOOSE_TRIES
, val
, 0);
1162 int set_rule_step_set_choose_local_tries(unsigned ruleno
, unsigned step
, int val
) {
1163 return set_rule_step(ruleno
, step
, CRUSH_RULE_SET_CHOOSE_LOCAL_TRIES
, val
, 0);
1165 int set_rule_step_set_choose_local_fallback_tries(unsigned ruleno
, unsigned step
, int val
) {
1166 return set_rule_step(ruleno
, step
, CRUSH_RULE_SET_CHOOSE_LOCAL_FALLBACK_TRIES
, val
, 0);
1168 int set_rule_step_set_chooseleaf_tries(unsigned ruleno
, unsigned step
, int val
) {
1169 return set_rule_step(ruleno
, step
, CRUSH_RULE_SET_CHOOSELEAF_TRIES
, val
, 0);
1171 int set_rule_step_set_chooseleaf_vary_r(unsigned ruleno
, unsigned step
, int val
) {
1172 return set_rule_step(ruleno
, step
, CRUSH_RULE_SET_CHOOSELEAF_VARY_R
, val
, 0);
1174 int set_rule_step_set_chooseleaf_stable(unsigned ruleno
, unsigned step
, int val
) {
1175 return set_rule_step(ruleno
, step
, CRUSH_RULE_SET_CHOOSELEAF_STABLE
, val
, 0);
1177 int set_rule_step_choose_firstn(unsigned ruleno
, unsigned step
, int val
, int type
) {
1178 return set_rule_step(ruleno
, step
, CRUSH_RULE_CHOOSE_FIRSTN
, val
, type
);
1180 int set_rule_step_choose_indep(unsigned ruleno
, unsigned step
, int val
, int type
) {
1181 return set_rule_step(ruleno
, step
, CRUSH_RULE_CHOOSE_INDEP
, val
, type
);
1183 int set_rule_step_choose_leaf_firstn(unsigned ruleno
, unsigned step
, int val
, int type
) {
1184 return set_rule_step(ruleno
, step
, CRUSH_RULE_CHOOSELEAF_FIRSTN
, val
, type
);
1186 int set_rule_step_choose_leaf_indep(unsigned ruleno
, unsigned step
, int val
, int type
) {
1187 return set_rule_step(ruleno
, step
, CRUSH_RULE_CHOOSELEAF_INDEP
, val
, type
);
1189 int set_rule_step_emit(unsigned ruleno
, unsigned step
) {
1190 return set_rule_step(ruleno
, step
, CRUSH_RULE_EMIT
, 0, 0);
1193 int add_simple_rule(
1194 string name
, string root_name
, string failure_domain_type
,
1195 string device_class
,
1196 string mode
, int rule_type
, ostream
*err
= 0);
1199 * @param rno rule[set] id to use, -1 to pick the lowest available
1201 int add_simple_rule_at(
1202 string name
, string root_name
,
1203 string failure_domain_type
, string device_class
, string mode
,
1204 int rule_type
, int rno
, ostream
*err
= 0);
1206 int remove_rule(int ruleno
);
1210 const crush_bucket
*get_bucket(int id
) const {
1212 return (crush_bucket
*)(-EINVAL
);
1213 unsigned int pos
= (unsigned int)(-1 - id
);
1214 unsigned int max_buckets
= crush
->max_buckets
;
1215 if (pos
>= max_buckets
)
1216 return (crush_bucket
*)(-ENOENT
);
1217 crush_bucket
*ret
= crush
->buckets
[pos
];
1219 return (crush_bucket
*)(-ENOENT
);
1223 crush_bucket
*get_bucket(int id
) {
1225 return (crush_bucket
*)(-EINVAL
);
1226 unsigned int pos
= (unsigned int)(-1 - id
);
1227 unsigned int max_buckets
= crush
->max_buckets
;
1228 if (pos
>= max_buckets
)
1229 return (crush_bucket
*)(-ENOENT
);
1230 crush_bucket
*ret
= crush
->buckets
[pos
];
1232 return (crush_bucket
*)(-ENOENT
);
1236 * detach a bucket from its parent and adjust the parent weight
1238 * returns the weight of the detached bucket
1240 int detach_bucket(CephContext
*cct
, int item
);
1242 int get_new_bucket_id();
1245 int get_max_buckets() const {
1246 if (!crush
) return -EINVAL
;
1247 return crush
->max_buckets
;
1249 int get_next_bucket_id() const {
1250 if (!crush
) return -EINVAL
;
1251 return crush_get_next_bucket_id(crush
);
1253 bool bucket_exists(int id
) const {
1254 const crush_bucket
*b
= get_bucket(id
);
1259 int get_bucket_weight(int id
) const {
1260 const crush_bucket
*b
= get_bucket(id
);
1261 if (IS_ERR(b
)) return PTR_ERR(b
);
1264 float get_bucket_weightf(int id
) const {
1265 const crush_bucket
*b
= get_bucket(id
);
1266 if (IS_ERR(b
)) return 0;
1267 return b
->weight
/ (float)0x10000;
1269 int get_bucket_type(int id
) const {
1270 const crush_bucket
*b
= get_bucket(id
);
1271 if (IS_ERR(b
)) return PTR_ERR(b
);
1274 int get_bucket_alg(int id
) const {
1275 const crush_bucket
*b
= get_bucket(id
);
1276 if (IS_ERR(b
)) return PTR_ERR(b
);
1279 int get_bucket_hash(int id
) const {
1280 const crush_bucket
*b
= get_bucket(id
);
1281 if (IS_ERR(b
)) return PTR_ERR(b
);
1284 int get_bucket_size(int id
) const {
1285 const crush_bucket
*b
= get_bucket(id
);
1286 if (IS_ERR(b
)) return PTR_ERR(b
);
1289 int get_bucket_item(int id
, int pos
) const {
1290 const crush_bucket
*b
= get_bucket(id
);
1291 if (IS_ERR(b
)) return PTR_ERR(b
);
1292 if ((__u32
)pos
>= b
->size
)
1294 return b
->items
[pos
];
1296 int get_bucket_item_weight(int id
, int pos
) const {
1297 const crush_bucket
*b
= get_bucket(id
);
1298 if (IS_ERR(b
)) return PTR_ERR(b
);
1299 return crush_get_bucket_item_weight(b
, pos
);
1301 float get_bucket_item_weightf(int id
, int pos
) const {
1302 const crush_bucket
*b
= get_bucket(id
);
1303 if (IS_ERR(b
)) return 0;
1304 return (float)crush_get_bucket_item_weight(b
, pos
) / (float)0x10000;
1308 int add_bucket(int bucketno
, int alg
, int hash
, int type
, int size
,
1309 int *items
, int *weights
, int *idout
);
1310 int bucket_add_item(crush_bucket
*bucket
, int item
, int weight
);
1311 int bucket_remove_item(struct crush_bucket
*bucket
, int item
);
1312 int bucket_adjust_item_weight(
1313 CephContext
*cct
, struct crush_bucket
*bucket
, int item
, int weight
,
1314 bool adjust_weight_sets
);
1318 crush_finalize(crush
);
1319 if (!name_map
.empty() &&
1320 name_map
.rbegin()->first
>= crush
->max_devices
) {
1321 crush
->max_devices
= name_map
.rbegin()->first
+ 1;
1323 have_uniform_rules
= !has_legacy_rule_ids();
1325 int bucket_set_alg(int id
, int alg
);
1327 int update_device_class(int id
, const string
& class_name
, const string
& name
, ostream
*ss
);
1328 int remove_device_class(CephContext
*cct
, int id
, ostream
*ss
);
1329 int device_class_clone(
1330 int original
, int device_class
,
1331 const std::map
<int32_t, map
<int32_t, int32_t>>& old_class_bucket
,
1332 const std::set
<int32_t>& used_ids
,
1334 map
<int,map
<int,vector
<int>>> *cmap_item_weight
);
1335 bool class_is_in_use(int class_id
, ostream
*ss
= nullptr);
1336 int rename_class(const string
& srcname
, const string
& dstname
);
1337 int populate_classes(
1338 const std::map
<int32_t, map
<int32_t, int32_t>>& old_class_bucket
);
1339 int get_rules_by_class(const string
&class_name
, set
<int> *rules
);
1340 int get_rules_by_osd(int osd
, set
<int> *rules
);
1341 bool _class_is_dead(int class_id
);
1342 void cleanup_dead_classes();
1343 int rebuild_roots_with_classes(CephContext
*cct
);
1344 /* remove unused roots generated for class devices */
1345 int trim_roots_with_class(CephContext
*cct
);
1350 const map
<string
,string
>& classify_root
,
1351 const map
<string
,pair
<string
,string
>>& classify_bucket
1354 int set_subtree_class(const string
& name
, const string
& class_name
);
1356 void start_choose_profile() {
1357 free(crush
->choose_tries
);
1359 * the original choose_total_tries value was off by one (it
1360 * counted "retries" and not "tries"). add one to alloc.
1362 crush
->choose_tries
= (__u32
*)calloc(sizeof(*crush
->choose_tries
),
1363 (crush
->choose_total_tries
+ 1));
1364 memset(crush
->choose_tries
, 0,
1365 sizeof(*crush
->choose_tries
) * (crush
->choose_total_tries
+ 1));
1367 void stop_choose_profile() {
1368 free(crush
->choose_tries
);
1369 crush
->choose_tries
= 0;
1372 int get_choose_profile(__u32
**vec
) {
1373 if (crush
->choose_tries
) {
1374 *vec
= crush
->choose_tries
;
1375 return crush
->choose_total_tries
;
1381 void set_max_devices(int m
) {
1382 crush
->max_devices
= m
;
1385 int find_rule(int ruleset
, int type
, int size
) const {
1386 if (!crush
) return -1;
1387 if (have_uniform_rules
&&
1388 ruleset
< (int)crush
->max_rules
&&
1389 crush
->rules
[ruleset
] &&
1390 crush
->rules
[ruleset
]->mask
.type
== type
&&
1391 crush
->rules
[ruleset
]->mask
.min_size
<= size
&&
1392 crush
->rules
[ruleset
]->mask
.max_size
>= size
) {
1395 return crush_find_rule(crush
, ruleset
, type
, size
);
1398 bool ruleset_exists(const int ruleset
) const {
1399 for (size_t i
= 0; i
< crush
->max_rules
; ++i
) {
1400 if (rule_exists(i
) && crush
->rules
[i
]->mask
.ruleset
== ruleset
) {
1409 * Return the lowest numbered ruleset of type `type`
1411 * @returns a ruleset ID, or -1 if no matching rules found.
1413 int find_first_ruleset(int type
) const {
1416 for (size_t i
= 0; i
< crush
->max_rules
; ++i
) {
1418 && crush
->rules
[i
]->mask
.type
== type
1419 && (crush
->rules
[i
]->mask
.ruleset
< result
|| result
== -1)) {
1420 result
= crush
->rules
[i
]->mask
.ruleset
;
1427 bool have_choose_args(int64_t choose_args_index
) const {
1428 return choose_args
.count(choose_args_index
);
1431 crush_choose_arg_map
choose_args_get_with_fallback(
1432 int64_t choose_args_index
) const {
1433 auto i
= choose_args
.find(choose_args_index
);
1434 if (i
== choose_args
.end()) {
1435 i
= choose_args
.find(DEFAULT_CHOOSE_ARGS
);
1437 if (i
== choose_args
.end()) {
1438 crush_choose_arg_map arg_map
;
1439 arg_map
.args
= NULL
;
1446 crush_choose_arg_map
choose_args_get(int64_t choose_args_index
) const {
1447 auto i
= choose_args
.find(choose_args_index
);
1448 if (i
== choose_args
.end()) {
1449 crush_choose_arg_map arg_map
;
1450 arg_map
.args
= NULL
;
1458 void destroy_choose_args(crush_choose_arg_map arg_map
) {
1459 for (__u32 i
= 0; i
< arg_map
.size
; i
++) {
1460 crush_choose_arg
*arg
= &arg_map
.args
[i
];
1461 for (__u32 j
= 0; j
< arg
->weight_set_positions
; j
++) {
1462 crush_weight_set
*weight_set
= &arg
->weight_set
[j
];
1463 free(weight_set
->weights
);
1465 if (arg
->weight_set
)
1466 free(arg
->weight_set
);
1473 bool create_choose_args(int64_t id
, int positions
) {
1474 if (choose_args
.count(id
))
1476 ceph_assert(positions
);
1477 auto &cmap
= choose_args
[id
];
1478 cmap
.args
= static_cast<crush_choose_arg
*>(calloc(sizeof(crush_choose_arg
),
1479 crush
->max_buckets
));
1480 cmap
.size
= crush
->max_buckets
;
1481 for (int bidx
=0; bidx
< crush
->max_buckets
; ++bidx
) {
1482 crush_bucket
*b
= crush
->buckets
[bidx
];
1483 auto &carg
= cmap
.args
[bidx
];
1486 if (b
&& b
->alg
== CRUSH_BUCKET_STRAW2
) {
1487 crush_bucket_straw2
*sb
= reinterpret_cast<crush_bucket_straw2
*>(b
);
1488 carg
.weight_set_positions
= positions
;
1489 carg
.weight_set
= static_cast<crush_weight_set
*>(calloc(sizeof(crush_weight_set
),
1490 carg
.weight_set_positions
));
1491 // initialize with canonical weights
1492 for (int pos
= 0; pos
< positions
; ++pos
) {
1493 carg
.weight_set
[pos
].size
= b
->size
;
1494 carg
.weight_set
[pos
].weights
= (__u32
*)calloc(4, b
->size
);
1495 for (unsigned i
= 0; i
< b
->size
; ++i
) {
1496 carg
.weight_set
[pos
].weights
[i
] = sb
->item_weights
[i
];
1500 carg
.weight_set
= NULL
;
1501 carg
.weight_set_positions
= 0;
1507 void rm_choose_args(int64_t id
) {
1508 auto p
= choose_args
.find(id
);
1509 if (p
!= choose_args
.end()) {
1510 destroy_choose_args(p
->second
);
1511 choose_args
.erase(p
);
1515 void choose_args_clear() {
1516 for (auto w
: choose_args
)
1517 destroy_choose_args(w
.second
);
1518 choose_args
.clear();
1521 // remove choose_args for buckets that no longer exist, create them for new buckets
1522 void update_choose_args(CephContext
*cct
);
1524 // adjust choose_args_map weight, preserving the hierarchical summation
1525 // property. used by callers optimizing layouts by tweaking weights.
1526 int _choose_args_adjust_item_weight_in_bucket(
1528 crush_choose_arg_map cmap
,
1531 const vector
<int>& weight
,
1533 int choose_args_adjust_item_weight(
1535 crush_choose_arg_map cmap
,
1536 int id
, const vector
<int>& weight
,
1538 int choose_args_adjust_item_weightf(
1540 crush_choose_arg_map cmap
,
1541 int id
, const vector
<double>& weightf
,
1543 vector
<int> weight(weightf
.size());
1544 for (unsigned i
= 0; i
< weightf
.size(); ++i
) {
1545 weight
[i
] = (int)(weightf
[i
] * (double)0x10000);
1547 return choose_args_adjust_item_weight(cct
, cmap
, id
, weight
, ss
);
1550 int get_choose_args_positions(crush_choose_arg_map cmap
) {
1551 // infer positions from other buckets
1552 for (unsigned j
= 0; j
< cmap
.size
; ++j
) {
1553 if (cmap
.args
[j
].weight_set_positions
) {
1554 return cmap
.args
[j
].weight_set_positions
;
1560 template<typename WeightVector
>
1561 void do_rule(int rule
, int x
, vector
<int>& out
, int maxout
,
1562 const WeightVector
& weight
,
1563 uint64_t choose_args_index
) const {
1565 char work
[crush_work_size(crush
, maxout
)];
1566 crush_init_workspace(crush
, work
);
1567 crush_choose_arg_map arg_map
= choose_args_get_with_fallback(
1569 int numrep
= crush_do_rule(crush
, rule
, x
, rawout
, maxout
,
1570 std::data(weight
), std::size(weight
),
1571 work
, arg_map
.args
);
1575 for (int i
=0; i
<numrep
; i
++)
1579 int _choose_type_stack(
1581 const vector
<pair
<int,int>>& stack
,
1582 const set
<int>& overfull
,
1583 const vector
<int>& underfull
,
1584 const vector
<int>& more_underfull
,
1585 const vector
<int>& orig
,
1586 vector
<int>::const_iterator
& i
,
1596 const set
<int>& overfull
,
1597 const vector
<int>& underfull
,
1598 const vector
<int>& more_underfull
,
1599 const vector
<int>& orig
,
1600 vector
<int> *out
) const;
1602 bool check_crush_rule(int ruleset
, int type
, int size
, ostream
& ss
) {
1606 for (i
= 0; i
< crush
->max_rules
; i
++) {
1607 if (crush
->rules
[i
] &&
1608 crush
->rules
[i
]->mask
.ruleset
== ruleset
&&
1609 crush
->rules
[i
]->mask
.type
== type
) {
1611 if (crush
->rules
[i
]->mask
.min_size
<= size
&&
1612 crush
->rules
[i
]->mask
.max_size
>= size
) {
1614 } else if (size
< crush
->rules
[i
]->mask
.min_size
) {
1615 ss
<< "pool size is smaller than the crush rule min size";
1618 ss
<< "pool size is bigger than the crush rule max size";
1627 void encode(bufferlist
&bl
, uint64_t features
) const;
1628 void decode(bufferlist::const_iterator
&blp
);
1629 void decode_crush_bucket(crush_bucket
** bptr
, bufferlist::const_iterator
&blp
);
1630 void dump(Formatter
*f
) const;
1631 void dump_rules(Formatter
*f
) const;
1632 void dump_rule(int ruleset
, Formatter
*f
) const;
1633 void dump_tunables(Formatter
*f
) const;
1634 void dump_choose_args(Formatter
*f
) const;
1635 void list_rules(Formatter
*f
) const;
1636 void list_rules(ostream
*ss
) const;
1637 void dump_tree(ostream
*out
,
1639 const CrushTreeDumper::name_map_t
& ws
,
1640 bool show_shadow
= false) const;
1641 void dump_tree(ostream
*out
, Formatter
*f
) {
1642 dump_tree(out
, f
, CrushTreeDumper::name_map_t());
1644 void dump_tree(Formatter
*f
,
1645 const CrushTreeDumper::name_map_t
& ws
) const;
1646 static void generate_test_instances(list
<CrushWrapper
*>& o
);
1648 int get_osd_pool_default_crush_replicated_ruleset(CephContext
*cct
);
1650 static bool is_valid_crush_name(const string
& s
);
1651 static bool is_valid_crush_loc(CephContext
*cct
,
1652 const map
<string
,string
>& loc
);
1654 WRITE_CLASS_ENCODER_FEATURES(CrushWrapper
)