1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 * Copyright (C) 2014 Cloudwatt <libre.licensing@cloudwatt.com>
9 * Author: Loic Dachary <loic@dachary.org>
11 * This is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License version 2.1, as published by the Free Software
14 * Foundation. See file COPYING.
18 #include <sys/types.h>
24 #include <type_traits>
26 #include "common/debug.h"
27 #include "common/errno.h"
28 #include "common/config.h"
29 #include "common/Formatter.h"
31 #include "common/ceph_argparse.h"
32 #include "include/stringify.h"
33 #include "global/global_context.h"
34 #include "global/global_init.h"
35 #include "osd/OSDMap.h"
36 #include "crush/CrushWrapper.h"
37 #include "crush/CrushCompiler.h"
38 #include "crush/CrushTester.h"
39 #include "include/ceph_assert.h"
41 #define dout_context g_ceph_context
42 #define dout_subsys ceph_subsys_crush
45 const char *infn
= "stdin";
47 static int get_fd_data(int fd
, bufferlist
&bl
)
52 ssize_t bytes
= bl
.read_fd(fd
, 1024*1024);
54 cerr
<< "read_fd error " << cpp_strerror(-bytes
) << "\n";
64 ceph_assert(bl
.length() == total
);
68 ////////////////////////////////////////////////////////////////////////////
70 void data_analysis_usage()
72 cout
<< "data output from testing routine ...\n";
73 cout
<< " absolute_weights\n";
74 cout
<< " the decimal weight of each OSD\n";
75 cout
<< " data layout: ROW MAJOR\n";
76 cout
<< " OSD id (int), weight (int)\n";
77 cout
<< " batch_device_expected_utilization_all\n";
78 cout
<< " the expected number of objects each OSD should receive per placement batch\n";
79 cout
<< " which may be a decimal value\n";
80 cout
<< " data layout: COLUMN MAJOR\n";
81 cout
<< " round (int), objects expected on OSD 0...OSD n (float)\n";
82 cout
<< " batch_device_utilization_all\n";
83 cout
<< " the number of objects stored on each OSD during each placement round\n";
84 cout
<< " data layout: COLUMN MAJOR\n";
85 cout
<< " round (int), objects stored on OSD 0...OSD n (int)\n";
86 cout
<< " device_utilization_all\n";
87 cout
<< " the number of objects stored on each OSD at the end of placements\n";
88 cout
<< " data_layout: ROW MAJOR\n";
89 cout
<< " OSD id (int), objects stored (int), objects expected (float)\n";
90 cout
<< " device_utilization\n";
91 cout
<< " the number of objects stored on each OSD marked 'up' at the end of placements\n";
92 cout
<< " data_layout: ROW MAJOR\n";
93 cout
<< " OSD id (int), objects stored (int), objects expected (float)\n";
94 cout
<< " placement_information\n";
95 cout
<< " the map of input -> OSD\n";
96 cout
<< " data_layout: ROW MAJOR\n";
97 cout
<< " input (int), OSD's mapped (int)\n";
98 cout
<< " proportional_weights_all\n";
99 cout
<< " the proportional weight of each OSD specified in the CRUSH map\n";
100 cout
<< " data_layout: ROW MAJOR\n";
101 cout
<< " OSD id (int), proportional weight (float)\n";
102 cout
<< " proportional_weights\n";
103 cout
<< " the proportional weight of each 'up' OSD specified in the CRUSH map\n";
104 cout
<< " data_layout: ROW MAJOR\n";
105 cout
<< " OSD id (int), proportional weight (float)\n";
110 cout
<< "usage: crushtool ...\n";
112 cout
<< "Display, modify and test a crush map\n";
114 cout
<< "There are five stages, running one after the other:\n";
116 cout
<< " - input/build\n";
117 cout
<< " - tunables adjustments\n";
118 cout
<< " - modifications\n";
119 cout
<< " - display/test\n";
120 cout
<< " - output\n";
122 cout
<< "Options that are not specific to a stage.\n";
124 cout
<< " [--infn|-i infile]\n";
125 cout
<< " read the crush map from infile\n";
127 cout
<< "Options for the input/build stage\n";
129 cout
<< " --decompile|-d map decompile a crush map to source\n";
130 cout
<< " [--outfn|-o outfile]\n";
131 cout
<< " specify output for for (de)compilation\n";
132 cout
<< " --compile|-c map.txt compile a map from source\n";
133 cout
<< " --enable-unsafe-tunables\n";
134 cout
<< " compile with unsafe tunables\n";
135 cout
<< " --build --num_osds N layer1 ...\n";
136 cout
<< " build a new map, where each 'layer' is\n";
137 cout
<< " 'name (uniform|straw2|straw|list|tree) size'\n";
139 cout
<< "Options for the tunables adjustments stage\n";
141 cout
<< " --set-choose-local-tries N\n";
142 cout
<< " set choose local retries before re-descent\n";
143 cout
<< " --set-choose-local-fallback-tries N\n";
144 cout
<< " set choose local retries using fallback\n";
145 cout
<< " permutation before re-descent\n";
146 cout
<< " --set-choose-total-tries N\n";
147 cout
<< " set choose total descent attempts\n";
148 cout
<< " --set-chooseleaf-descend-once <0|1>\n";
149 cout
<< " set chooseleaf to (not) retry the recursive descent\n";
150 cout
<< " --set-chooseleaf-vary-r <0|1>\n";
151 cout
<< " set chooseleaf to (not) vary r based on parent\n";
152 cout
<< " --set-chooseleaf-stable <0|1>\n";
153 cout
<< " set chooseleaf firstn to (not) return stable results\n";
155 cout
<< "Options for the modifications stage\n";
157 cout
<< " -i mapfn --add-item id weight name [--loc type name ...]\n";
158 cout
<< " insert an item into the hierarchy at the\n";
159 cout
<< " given location\n";
160 cout
<< " -i mapfn --update-item id weight name [--loc type name ...]\n";
161 cout
<< " insert or move an item into the hierarchy at the\n";
162 cout
<< " given location\n";
163 cout
<< " -i mapfn --remove-item name\n"
164 << " remove the given item\n";
165 cout
<< " -i mapfn --reweight-item name weight\n";
166 cout
<< " reweight a given item (and adjust ancestor\n"
167 << " weights as needed)\n";
168 cout
<< " -i mapfn --add-bucket name type [--loc type name ...]\n"
169 << " insert a bucket into the hierarchy at the given\n"
171 cout
<< " -i mapfn --move name --loc type name ...\n"
172 << " move the given item to specified location\n";
173 cout
<< " -i mapfn --reweight recalculate all bucket weights\n";
174 cout
<< " -i mapfn --rebuild-class-roots\n";
175 cout
<< " rebuild the per-class shadow trees (normally a no-op)\n";
176 cout
<< " -i mapfn --create-simple-rule name root type mode\n"
177 << " create crush rule <name> to start from <root>,\n"
178 << " replicate across buckets of type <type>, using\n"
179 << " a choose mode of <firstn|indep>\n";
180 cout
<< " -i mapfn --create-replicated-rule name root type\n"
181 << " create crush rule <name> to start from <root>,\n"
182 << " replicate across buckets of type <type>\n";
183 cout
<< " --device-class <class>\n";
184 cout
<< " use device class <class> for new rule\n";
185 cout
<< " -i mapfn --remove-rule name\n"
186 << " remove the specified crush rule\n";
188 cout
<< "Options for the display/test stage\n";
190 cout
<< " -f --format the format of --dump, defaults to json-pretty\n";
191 cout
<< " can be one of json, json-pretty, xml, xml-pretty,\n";
192 cout
<< " table, table-kv, html, html-pretty\n";
193 cout
<< " --dump dump the crush map\n";
194 cout
<< " --tree print map summary as a tree\n";
195 cout
<< " --check [max_id] check if any item is referencing an unknown name/type\n";
196 cout
<< " -i mapfn --show-location id\n";
197 cout
<< " show location for given device id\n";
198 cout
<< " -i mapfn --test test a range of inputs on the map\n";
199 cout
<< " [--min-x x] [--max-x x] [--x x]\n";
200 cout
<< " [--min-rule r] [--max-rule r] [--rule r] [--ruleset rs]\n";
201 cout
<< " [--num-rep n]\n";
202 cout
<< " [--pool-id n] specifies pool id\n";
203 cout
<< " [--batches b] split the CRUSH mapping into b > 1 rounds\n";
204 cout
<< " [--weight|-w devno weight]\n";
205 cout
<< " where weight is 0 to 1.0\n";
206 cout
<< " [--simulate] simulate placements using a random\n";
207 cout
<< " number generator in place of the CRUSH\n";
208 cout
<< " algorithm\n";
209 cout
<< " --show-utilization show OSD usage\n";
210 cout
<< " --show-utilization-all\n";
211 cout
<< " include zero weight items\n";
212 cout
<< " --show-statistics show chi squared statistics\n";
213 cout
<< " --show-mappings show mappings\n";
214 cout
<< " --show-bad-mappings show bad mappings\n";
215 cout
<< " --show-choose-tries show choose tries histogram\n";
216 cout
<< " --output-name name\n";
217 cout
<< " prepend the data file(s) generated during the\n";
218 cout
<< " testing routine with name\n";
219 cout
<< " --output-csv\n";
220 cout
<< " export select data generated during testing routine\n";
221 cout
<< " to CSV files for off-line post-processing\n";
222 cout
<< " use --help-output for more information\n";
223 cout
<< " --reclassify transform legacy CRUSH map buckets and rules\n";
224 cout
<< " by adding classes\n";
225 cout
<< " --reclassify-bucket <bucket-match> <class> <default-parent>\n";
226 cout
<< " --reclassify-root <bucket-name> <class>\n";
227 cout
<< " --set-subtree-class <bucket-name> <class>\n";
228 cout
<< " set class for all items beneath bucket-name\n";
229 cout
<< " --compare <otherfile> compare two maps using --test parameters\n";
231 cout
<< "Options for the output stage\n";
233 cout
<< " [--outfn|-o outfile]\n";
234 cout
<< " specify output for modified crush map\n";
238 struct bucket_types_t
{
242 { "uniform", CRUSH_BUCKET_UNIFORM
},
243 { "list", CRUSH_BUCKET_LIST
},
244 { "straw", CRUSH_BUCKET_STRAW
},
245 { "straw2", CRUSH_BUCKET_STRAW2
},
246 { "tree", CRUSH_BUCKET_TREE
},
252 const char *buckettype
;
256 template<typename
... Args
>
257 bool argparse_withargs(std::vector
<const char*> &args
,
258 std::vector
<const char*>::iterator
& i
,
263 if (!ceph_argparse_flag(args
, i
, opt
, nullptr)) {
266 auto parse
= [&](auto& opt
) {
267 if (i
== args
.end()) {
268 oss
<< "expecting additional argument to " << opt
;
271 using opt_t
= std::remove_pointer_t
<decay_t
<decltype(opt
)>>;
273 if constexpr (std::is_same_v
<opt_t
, string
>) {
275 } else if constexpr (is_same_v
<opt_t
, int>) {
276 *opt
= strict_strtol(*i
, 10, &err
);
277 } else if constexpr (is_same_v
<opt_t
, float>) {
278 *opt
= strict_strtof(*i
, &err
);
288 (... && parse(opts
));
292 int do_add_bucket(CephContext
* cct
,
295 const string
& add_name
,
296 const string
& add_type
,
297 const map
<string
,string
>& add_loc
) {
299 if (crush
.name_exists(add_name
)) {
300 cerr
<< me
<< " bucket '" << add_name
<< "' already exists" << std::endl
;
303 int type
= crush
.get_type_id(add_type
);
305 cerr
<< me
<< " bad bucket type: " << add_type
<< std::endl
;
308 if (int r
= crush
.add_bucket(0, 0, CRUSH_HASH_DEFAULT
, type
, 0, nullptr, nullptr, &bucketno
);
310 cerr
<< me
<< " unable to add bucket: " << cpp_strerror(r
) << std::endl
;
313 if (int r
= crush
.set_item_name(bucketno
, add_name
); r
< 0) {
314 cerr
<< me
<< " bad bucket name: " << add_name
<< std::endl
;
317 if (!add_loc
.empty()) {
318 if (!crush
.check_item_loc(cct
, bucketno
, add_loc
, (int*)nullptr)) {
319 if (int r
= crush
.move_bucket(cct
, bucketno
, add_loc
); r
< 0) {
320 cerr
<< me
<< " error moving bucket '" << add_name
<< "' to " << add_loc
<< std::endl
;
328 // return 1 for no change, 0 for successful change, negative on error
329 int do_move_item(CephContext
* cct
,
333 const map
<string
,string
>& loc
)
335 if (!crush
.name_exists(name
)) {
336 cerr
<< me
<< " item '" << name
<< "' does not exist" << std::endl
;
339 int id
= crush
.get_item_id(name
);
341 cerr
<< me
<< " expecting additional --loc argument to --move" << std::endl
;
344 if (crush
.check_item_loc(cct
, id
, loc
, (int*)nullptr)) {
345 // it's already there
346 cerr
<< me
<< " item '" << name
<< "' already at " << loc
<< std::endl
;
350 switch (int r
= crush
.create_or_move_item(cct
, id
, 0, name
, loc
)) {
359 return crush
.move_bucket(cct
, id
, loc
);
363 int main(int argc
, const char **argv
)
365 vector
<const char*> args
;
366 argv_to_vec(argc
, argv
, args
);
368 cerr
<< argv
[0] << ": -h or --help for usage" << std::endl
;
371 if (ceph_argparse_need_usage(args
)) {
376 const char *me
= argv
[0];
377 std::string infn
, srcfn
, outfn
, add_name
, add_type
, remove_name
, reweight_name
;
378 std::string move_name
;
379 bool compile
= false;
380 bool decompile
= false;
384 bool display
= false;
386 string dump_format
= "json-pretty";
388 int full_location
= -1;
389 bool write_to_file
= false;
391 bool unsafe_tunables
= false;
393 bool rebuild_class_roots
= false;
395 bool reweight
= false;
397 bool add_bucket
= false;
398 bool update_item
= false;
399 bool move_item
= false;
400 bool add_rule
= false;
401 std::string rule_name
, rule_root
, rule_type
, rule_mode
, rule_device_class
;
402 bool del_rule
= false;
403 float add_weight
= 0;
404 map
<string
,string
> add_loc
;
405 float reweight_weight
= 0;
411 vector
<layer_t
> layers
;
413 int choose_local_tries
= -1;
414 int choose_local_fallback_tries
= -1;
415 int choose_total_tries
= -1;
416 int chooseleaf_descend_once
= -1;
417 int chooseleaf_vary_r
= -1;
418 int chooseleaf_stable
= -1;
419 int straw_calc_version
= -1;
420 int allowed_bucket_algs
= -1;
422 bool reclassify
= false;
423 map
<string
,pair
<string
,string
>> reclassify_bucket
; // %suffix or prefix% -> class, default_root
424 map
<string
,string
> reclassify_root
; // bucket -> class
425 map
<string
,string
> set_subtree_class
; // bucket -> class
431 CrushTester
tester(crush
, cout
);
433 // we use -c, don't confuse the generic arg parsing
434 // only parse arguments from CEPH_ARGS, if in the environment
435 vector
<const char *> empty_args
;
436 auto cct
= global_init(NULL
, empty_args
, CEPH_ENTITY_TYPE_CLIENT
,
437 CODE_ENVIRONMENT_UTILITY
,
438 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE
);
439 // crushtool times out occasionally when quits. so do not
440 // release the g_ceph_context.
442 common_init_finish(g_ceph_context
);
449 std::ostringstream err
;
451 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ) {
452 if (ceph_argparse_double_dash(args
, i
)) {
454 } else if (ceph_argparse_witharg(args
, i
, &val
, "-d", "--decompile", (char*)NULL
)) {
457 } else if (ceph_argparse_witharg(args
, i
, &val
, "-i", "--infn", (char*)NULL
)) {
459 } else if (ceph_argparse_witharg(args
, i
, &val
, "-o", "--outfn", (char*)NULL
)) {
461 } else if (ceph_argparse_flag(args
, i
, "-v", "--verbose", (char*)NULL
)) {
463 } else if (ceph_argparse_witharg(args
, i
, &val
, "--compare", (char*)NULL
)) {
465 } else if (ceph_argparse_flag(args
, i
, "--reclassify", (char*)NULL
)) {
467 } else if (ceph_argparse_witharg(args
, i
, &val
, "--reclassify-bucket",
469 if (i
== args
.end()) {
470 cerr
<< "expecting additional argument" << std::endl
;
475 if (i
== args
.end()) {
476 cerr
<< "expecting additional argument" << std::endl
;
479 reclassify_bucket
[val
] = make_pair(c
, *i
);
481 } else if (ceph_argparse_witharg(args
, i
, &val
, "--reclassify-root",
483 if (i
== args
.end()) {
484 cerr
<< "expecting additional argument" << std::endl
;
487 reclassify_root
[val
] = *i
;
489 } else if (ceph_argparse_witharg(args
, i
, &val
, "--set-subtree-class",
491 if (i
== args
.end()) {
492 cerr
<< "expecting additional argument" << std::endl
;
495 set_subtree_class
[val
] = *i
;
497 } else if (ceph_argparse_flag(args
, i
, "--tree", (char*)NULL
)) {
499 } else if (ceph_argparse_witharg(args
, i
, &val
, "-f", "--format", (char*)NULL
)) {
501 } else if (ceph_argparse_flag(args
, i
, "--dump", (char*)NULL
)) {
503 } else if (ceph_argparse_flag(args
, i
, "--show_utilization", (char*)NULL
)) {
505 tester
.set_output_utilization(true);
506 } else if (ceph_argparse_flag(args
, i
, "--show_utilization_all", (char*)NULL
)) {
508 tester
.set_output_utilization_all(true);
509 } else if (ceph_argparse_flag(args
, i
, "--show_statistics", (char*)NULL
)) {
511 tester
.set_output_statistics(true);
512 } else if (ceph_argparse_flag(args
, i
, "--show_mappings", (char*)NULL
)) {
514 tester
.set_output_mappings(true);
515 } else if (ceph_argparse_flag(args
, i
, "--show_bad_mappings", (char*)NULL
)) {
517 tester
.set_output_bad_mappings(true);
518 } else if (ceph_argparse_flag(args
, i
, "--show_choose_tries", (char*)NULL
)) {
520 tester
.set_output_choose_tries(true);
521 } else if (ceph_argparse_witharg(args
, i
, &val
, "-c", "--compile", (char*)NULL
)) {
524 } else if (ceph_argparse_witharg(args
, i
, &max_id
, err
, "--check", (char*)NULL
)) {
526 } else if (ceph_argparse_flag(args
, i
, "-t", "--test", (char*)NULL
)) {
528 } else if (ceph_argparse_witharg(args
, i
, &full_location
, err
, "--show-location", (char*)NULL
)) {
529 } else if (ceph_argparse_flag(args
, i
, "-s", "--simulate", (char*)NULL
)) {
530 tester
.set_random_placement();
531 } else if (ceph_argparse_flag(args
, i
, "--enable-unsafe-tunables", (char*)NULL
)) {
532 unsafe_tunables
= true;
533 } else if (ceph_argparse_witharg(args
, i
, &choose_local_tries
, err
,
534 "--set_choose_local_tries", (char*)NULL
)) {
536 } else if (ceph_argparse_witharg(args
, i
, &choose_local_fallback_tries
, err
,
537 "--set_choose_local_fallback_tries", (char*)NULL
)) {
539 } else if (ceph_argparse_witharg(args
, i
, &choose_total_tries
, err
,
540 "--set_choose_total_tries", (char*)NULL
)) {
542 } else if (ceph_argparse_witharg(args
, i
, &chooseleaf_descend_once
, err
,
543 "--set_chooseleaf_descend_once", (char*)NULL
)) {
545 } else if (ceph_argparse_witharg(args
, i
, &chooseleaf_vary_r
, err
,
546 "--set_chooseleaf_vary_r", (char*)NULL
)) {
548 } else if (ceph_argparse_witharg(args
, i
, &chooseleaf_stable
, err
,
549 "--set_chooseleaf_stable", (char*)NULL
)) {
551 } else if (ceph_argparse_witharg(args
, i
, &straw_calc_version
, err
,
552 "--set_straw_calc_version", (char*)NULL
)) {
554 } else if (ceph_argparse_witharg(args
, i
, &allowed_bucket_algs
, err
,
555 "--set_allowed_bucket_algs", (char*)NULL
)) {
557 } else if (ceph_argparse_flag(args
, i
, "--reweight", (char*)NULL
)) {
559 } else if (ceph_argparse_flag(args
, i
, "--rebuild-class-roots", (char*)NULL
)) {
560 rebuild_class_roots
= true;
561 } else if (ceph_argparse_witharg(args
, i
, &add_item
, err
, "--add_item", (char*)NULL
)) {
562 if (!err
.str().empty()) {
563 cerr
<< err
.str() << std::endl
;
566 if (i
== args
.end()) {
567 cerr
<< "expecting additional argument to --add-item" << std::endl
;
570 add_weight
= atof(*i
);
572 if (i
== args
.end()) {
573 cerr
<< "expecting additional argument to --add-item" << std::endl
;
578 } else if (ceph_argparse_witharg(args
, i
, &add_item
, err
, "--update_item", (char*)NULL
)) {
580 if (!err
.str().empty()) {
581 cerr
<< err
.str() << std::endl
;
584 if (i
== args
.end()) {
585 cerr
<< "expecting additional argument to --update-item" << std::endl
;
588 add_weight
= atof(*i
);
590 if (i
== args
.end()) {
591 cerr
<< "expecting additional argument to --update-item" << std::endl
;
596 } else if (argparse_withargs(args
, i
, err
, "--add-bucket",
597 &add_name
, &add_type
)) {
598 if (!err
.str().empty()) {
599 cerr
<< err
.str() << std::endl
;
603 } else if (argparse_withargs(args
, i
, err
, "--move",
605 if (!err
.str().empty()) {
606 cerr
<< err
.str() << std::endl
;
610 } else if (ceph_argparse_witharg(args
, i
, &val
, err
, "--create-simple-rule", (char*)NULL
)) {
611 rule_name
.assign(val
);
612 if (!err
.str().empty()) {
613 cerr
<< err
.str() << std::endl
;
616 if (i
== args
.end()) {
617 cerr
<< "expecting additional argument to --create-simple-rule" << std::endl
;
621 rule_root
.assign(*i
);
623 if (i
== args
.end()) {
624 cerr
<< "expecting additional argument to --create-simple-rule" << std::endl
;
628 rule_type
.assign(*i
);
630 if (i
== args
.end()) {
631 cerr
<< "expecting additional argument to --create-simple-rule" << std::endl
;
635 rule_mode
.assign(*i
);
638 cout
<< "--create-simple-rule:"
639 << " name=" << rule_name
640 << " root=" << rule_root
641 << " type=" << rule_type
642 << " mode=" << rule_mode
645 } else if (ceph_argparse_witharg(args
, i
, &val
, err
, "--create-replicated-rule", (char*)NULL
)) {
646 rule_name
.assign(val
);
647 if (!err
.str().empty()) {
648 cerr
<< err
.str() << std::endl
;
651 if (i
== args
.end()) {
652 cerr
<< "expecting additional argument to --create-replicated-rule" << std::endl
;
656 rule_root
.assign(*i
);
658 if (i
== args
.end()) {
659 cerr
<< "expecting additional argument to --create-replicated-rule" << std::endl
;
663 rule_type
.assign(*i
);
665 rule_mode
= "firstn";
667 cout
<< "--create-replicated-rule:"
668 << " name=" << rule_name
669 << " root=" << rule_root
670 << " type=" << rule_type
674 } else if (ceph_argparse_witharg(args
, i
, &val
, "--device-class", (char*)NULL
)) {
675 rule_device_class
.assign(val
);
676 if (!err
.str().empty()) {
677 cerr
<< err
.str() << std::endl
;
680 } else if (ceph_argparse_witharg(args
, i
, &val
, "--remove-rule", (char*)NULL
)) {
681 rule_name
.assign(val
);
682 if (!err
.str().empty()) {
683 cerr
<< err
.str() << std::endl
;
687 } else if (ceph_argparse_witharg(args
, i
, &val
, "--loc", (char*)NULL
)) {
688 std::string
type(val
);
689 if (i
== args
.end()) {
690 cerr
<< "expecting additional argument to --loc" << std::endl
;
693 std::string
name(*i
);
695 add_loc
[type
] = name
;
696 } else if (ceph_argparse_flag(args
, i
, "--output-csv", (char*)NULL
)) {
697 write_to_file
= true;
698 tester
.set_output_data_file(true);
699 tester
.set_output_csv(true);
700 } else if (ceph_argparse_flag(args
, i
, "--help-output", (char*)NULL
)) {
701 data_analysis_usage();
703 } else if (ceph_argparse_witharg(args
, i
, &val
, "--output-name", (char*)NULL
)) {
704 std::string
name(val
);
705 if (i
== args
.end()) {
706 cerr
<< "expecting additional argument to --output-name" << std::endl
;
710 tester
.set_output_data_file_name(name
+ "-");
712 } else if (ceph_argparse_witharg(args
, i
, &val
, "--remove_item", (char*)NULL
)) {
714 } else if (ceph_argparse_witharg(args
, i
, &val
, "--reweight_item", (char*)NULL
)) {
716 if (i
== args
.end()) {
717 cerr
<< "expecting additional argument to --reweight-item" << std::endl
;
720 reweight_weight
= atof(*i
);
722 } else if (ceph_argparse_flag(args
, i
, "--build", (char*)NULL
)) {
724 } else if (ceph_argparse_witharg(args
, i
, &num_osds
, err
, "--num_osds", (char*)NULL
)) {
725 if (!err
.str().empty()) {
726 cerr
<< err
.str() << std::endl
;
729 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--num_rep", (char*)NULL
)) {
730 if (!err
.str().empty()) {
731 cerr
<< err
.str() << std::endl
;
734 tester
.set_num_rep(x
);
735 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--max_x", (char*)NULL
)) {
736 if (!err
.str().empty()) {
737 cerr
<< err
.str() << std::endl
;
741 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--min_x", (char*)NULL
)) {
742 if (!err
.str().empty()) {
743 cerr
<< err
.str() << std::endl
;
747 } else if (ceph_argparse_witharg(args
, i
, &z
, err
, "--pool_id", (char*)NULL
)) {
748 if (!err
.str().empty()) {
749 cerr
<< err
.str() << std::endl
;
752 tester
.set_pool_id(z
);
753 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--x", (char*)NULL
)) {
754 if (!err
.str().empty()) {
755 cerr
<< err
.str() << std::endl
;
759 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--max_rule", (char*)NULL
)) {
760 if (!err
.str().empty()) {
761 cerr
<< err
.str() << std::endl
;
764 tester
.set_max_rule(x
);
765 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--min_rule", (char*)NULL
)) {
766 if (!err
.str().empty()) {
767 cerr
<< err
.str() << std::endl
;
770 tester
.set_min_rule(x
);
771 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--rule", (char*)NULL
)) {
772 if (!err
.str().empty()) {
773 cerr
<< err
.str() << std::endl
;
777 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--ruleset", (char*)NULL
)) {
778 if (!err
.str().empty()) {
779 cerr
<< err
.str() << std::endl
;
782 tester
.set_ruleset(x
);
783 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--batches", (char*)NULL
)) {
784 if (!err
.str().empty()) {
785 cerr
<< err
.str() << std::endl
;
788 tester
.set_batches(x
);
789 } else if (ceph_argparse_witharg(args
, i
, &y
, err
, "--mark-down-ratio", (char*)NULL
)) {
790 if (!err
.str().empty()) {
791 cerr
<< err
.str() << std::endl
;
794 tester
.set_device_down_ratio(y
);
795 } else if (ceph_argparse_witharg(args
, i
, &y
, err
, "--mark-down-bucket-ratio", (char*)NULL
)) {
796 if (!err
.str().empty()) {
797 cerr
<< err
.str() << std::endl
;
800 tester
.set_bucket_down_ratio(y
);
801 } else if (ceph_argparse_witharg(args
, i
, &tmp
, err
, "--weight", (char*)NULL
)) {
802 if (!err
.str().empty()) {
803 cerr
<< err
.str() << std::endl
;
807 if (i
== args
.end()) {
808 cerr
<< "expecting additional argument to --weight" << std::endl
;
813 tester
.set_device_weight(dev
, f
);
820 if (test
&& !check
&& !display
&& !write_to_file
&& compare
.empty()) {
821 cerr
<< "WARNING: no output selected; use --output-csv or --show-X" << std::endl
;
824 if (decompile
+ compile
+ build
> 1) {
825 cerr
<< "cannot specify more than one of compile, decompile, and build" << std::endl
;
828 if (!check
&& !compile
&& !decompile
&& !build
&& !test
&& !reweight
&& !adjust
&& !tree
&& !dump
&&
829 add_item
< 0 && !add_bucket
&& !move_item
&& !add_rule
&& !del_rule
&& full_location
< 0 &&
830 !reclassify
&& !rebuild_class_roots
&&
832 remove_name
.empty() && reweight_name
.empty()) {
833 cerr
<< "no action specified; -h for help" << std::endl
;
836 if ((!build
) && (!args
.empty())) {
837 cerr
<< "unrecognized arguments: " << args
<< std::endl
;
841 if ((args
.size() % 3) != 0U) {
842 cerr
<< "remaining args: " << args
<< std::endl
;
843 cerr
<< "layers must be specified with 3-tuples of (name, buckettype, size)"
847 for (size_t j
= 0; j
< args
.size(); j
+= 3) {
850 l
.buckettype
= args
[j
+1];
851 l
.size
= atoi(args
[j
+2]);
857 if (outfn) cout << "outfn " << outfn << std::endl;
858 if (cinfn) cout << "cinfn " << cinfn << std::endl;
859 if (dinfn) cout << "dinfn " << dinfn << std::endl;
862 bool modified
= false;
872 if (isatty(STDIN_FILENO
)) {
873 cerr
<< "stdin must not be from a tty" << std::endl
;
876 r
= get_fd_data(STDIN_FILENO
, bl
);
878 cerr
<< "error reading data from STDIN" << std::endl
;
882 r
= bl
.read_file(infn
.c_str(), &error
);
884 cerr
<< me
<< ": error reading '" << infn
<< "': "
885 << error
<< std::endl
;
889 auto p
= bl
.cbegin();
893 cerr
<< me
<< ": unable to decode " << infn
<< std::endl
;
902 ifstream
in(srcfn
.c_str());
904 cerr
<< "input file " << srcfn
<< " not found" << std::endl
;
908 CrushCompiler
cc(crush
, cerr
, verbose
);
910 cc
.enable_unsafe_tunables();
911 int r
= cc
.compile(in
, srcfn
.c_str());
919 if (layers
.empty()) {
920 cerr
<< me
<< ": must specify at least one layer" << std::endl
;
926 vector
<int> lower_items
;
927 vector
<int> lower_weights
;
929 crush
.set_max_devices(num_osds
);
930 for (int i
=0; i
<num_osds
; i
++) {
931 lower_items
.push_back(i
);
932 lower_weights
.push_back(0x10000);
933 crush
.set_item_name(i
, "osd." + stringify(i
));
936 crush
.set_type_name(0, "osd");
938 for (vector
<layer_t
>::iterator p
= layers
.begin(); p
!= layers
.end(); ++p
, type
++) {
941 dout(2) << "layer " << type
943 << " bucket type " << l
.buckettype
947 crush
.set_type_name(type
, l
.name
);
950 for (int i
= 0; bucket_types
[i
].name
; i
++)
951 if (l
.buckettype
&& strcmp(l
.buckettype
, bucket_types
[i
].name
) == 0) {
952 buckettype
= bucket_types
[i
].type
;
955 if (buckettype
< 0) {
956 cerr
<< "unknown bucket type '" << l
.buckettype
<< "'" << std::endl
;
961 vector
<int> cur_items
;
962 vector
<int> cur_weights
;
963 unsigned lower_pos
= 0; // lower pos
965 dout(2) << "lower_items " << lower_items
<< dendl
;
966 dout(2) << "lower_weights " << lower_weights
<< dendl
;
970 if (lower_pos
== lower_items
.size())
974 int weights
[num_osds
];
978 for (j
=0; j
<l
.size
|| l
.size
==0; j
++) {
979 if (lower_pos
== lower_items
.size())
981 items
[j
] = lower_items
[lower_pos
];
982 weights
[j
] = lower_weights
[lower_pos
];
983 weight
+= weights
[j
];
985 dout(2) << " item " << items
[j
] << " weight " << weights
[j
] << dendl
;
989 int r
= crush
.add_bucket(0, buckettype
, CRUSH_HASH_DEFAULT
, type
, j
, items
, weights
, &id
);
991 cerr
<< " Couldn't add bucket: " << cpp_strerror(r
) << std::endl
;
996 format
[sizeof(format
)-1] = '\0';
998 snprintf(format
, sizeof(format
)-1, "%s%%d", l
.name
);
1000 strncpy(format
, l
.name
, sizeof(format
)-1);
1002 snprintf(name
, sizeof(name
), format
, i
);
1003 crush
.set_item_name(id
, name
);
1005 dout(2) << " in bucket " << id
<< " '" << name
<< "' size " << j
<< " weight " << weight
<< dendl
;
1007 cur_items
.push_back(id
);
1008 cur_weights
.push_back(weight
);
1012 lower_items
.swap(cur_items
);
1013 lower_weights
.swap(cur_weights
);
1016 string root
= layers
.back().size
== 0 ? layers
.back().name
:
1017 string(layers
.back().name
) + "0";
1021 crush
.find_roots(&roots
);
1022 if (roots
.size() > 1) {
1023 cerr
<< "The crush rulesets will use the root " << root
<< "\n"
1024 << "and ignore the others.\n"
1025 << "There are " << roots
.size() << " roots, they can be\n"
1026 << "grouped into a single root by appending something like:\n"
1027 << " root straw 0\n"
1032 if (OSDMap::build_simple_crush_rules(g_ceph_context
, crush
, root
, &cerr
))
1033 return EXIT_FAILURE
;
1040 if (choose_local_tries
>= 0) {
1041 crush
.set_choose_local_tries(choose_local_tries
);
1044 if (choose_local_fallback_tries
>= 0) {
1045 crush
.set_choose_local_fallback_tries(choose_local_fallback_tries
);
1048 if (choose_total_tries
>= 0) {
1049 crush
.set_choose_total_tries(choose_total_tries
);
1052 if (chooseleaf_descend_once
>= 0) {
1053 crush
.set_chooseleaf_descend_once(chooseleaf_descend_once
);
1056 if (chooseleaf_vary_r
>= 0) {
1057 crush
.set_chooseleaf_vary_r(chooseleaf_vary_r
);
1060 if (chooseleaf_stable
>= 0) {
1061 crush
.set_chooseleaf_stable(chooseleaf_stable
);
1064 if (straw_calc_version
>= 0) {
1065 crush
.set_straw_calc_version(straw_calc_version
);
1068 if (allowed_bucket_algs
>= 0) {
1069 crush
.set_allowed_bucket_algs(allowed_bucket_algs
);
1073 if (!reweight_name
.empty()) {
1074 cout
<< me
<< " reweighting item " << reweight_name
<< " to " << reweight_weight
<< std::endl
;
1076 if (!crush
.name_exists(reweight_name
)) {
1077 cerr
<< " name " << reweight_name
<< " dne" << std::endl
;
1080 int item
= crush
.get_item_id(reweight_name
);
1081 r
= crush
.adjust_item_weightf(g_ceph_context
, item
, reweight_weight
);
1086 cerr
<< me
<< " " << cpp_strerror(r
) << std::endl
;
1091 if (!remove_name
.empty()) {
1092 cout
<< me
<< " removing item " << remove_name
<< std::endl
;
1094 if (!crush
.name_exists(remove_name
)) {
1095 cerr
<< " name " << remove_name
<< " dne" << std::endl
;
1098 int remove_item
= crush
.get_item_id(remove_name
);
1099 r
= crush
.remove_item(g_ceph_context
, remove_item
, false);
1104 cerr
<< me
<< " " << cpp_strerror(r
) << std::endl
;
1109 if (add_item
>= 0) {
1112 r
= crush
.update_item(g_ceph_context
, add_item
, add_weight
, add_name
.c_str(), add_loc
);
1114 r
= crush
.insert_item(g_ceph_context
, add_item
, add_weight
, add_name
.c_str(), add_loc
);
1119 cerr
<< me
<< " " << cpp_strerror(r
) << std::endl
;
1125 if (int r
= do_add_bucket(cct
.get(), me
, crush
, add_name
, add_type
, add_loc
); !r
) {
1133 if (int r
= do_move_item(cct
.get(), me
, crush
, move_name
, add_loc
); !r
) {
1140 if (crush
.rule_exists(rule_name
)) {
1141 cerr
<< "rule " << rule_name
<< " already exists" << std::endl
;
1142 return EXIT_FAILURE
;
1144 int r
= crush
.add_simple_rule(rule_name
, rule_root
, rule_type
,
1146 rule_mode
, pg_pool_t::TYPE_REPLICATED
, &err
);
1148 cerr
<< err
.str() << std::endl
;
1149 return EXIT_FAILURE
;
1155 if (!crush
.rule_exists(rule_name
)) {
1156 cerr
<< "rule " << rule_name
<< " does not exist" << std::endl
;
1159 int ruleno
= crush
.get_rule_id(rule_name
);
1160 ceph_assert(ruleno
>= 0);
1161 int r
= crush
.remove_rule(ruleno
);
1163 cerr
<< "fail to remove rule " << rule_name
<< std::endl
;
1164 return EXIT_FAILURE
;
1170 crush
.reweight(g_ceph_context
);
1173 if (rebuild_class_roots
) {
1174 int r
= crush
.rebuild_roots_with_classes(g_ceph_context
);
1176 cerr
<< "failed to rebuidl roots with classes" << std::endl
;
1177 return EXIT_FAILURE
;
1182 for (auto& i
: set_subtree_class
) {
1183 crush
.set_subtree_class(i
.first
, i
.second
);
1187 int r
= crush
.reclassify(
1193 cerr
<< "failed to reclassify map" << std::endl
;
1194 return EXIT_FAILURE
;
1200 if (full_location
>= 0) {
1201 map
<string
, string
> loc
= crush
.get_full_location(full_location
);
1202 for (map
<string
,string
>::iterator p
= loc
.begin();
1205 cout
<< p
->first
<< "\t" << p
->second
<< std::endl
;
1210 crush
.dump_tree(&cout
, NULL
, {}, true);
1214 boost::scoped_ptr
<Formatter
> f(Formatter::create(dump_format
, "json-pretty", "json-pretty"));
1215 f
->open_object_section("crush_map");
1216 crush
.dump(f
.get());
1223 CrushCompiler
cc(crush
, cerr
, verbose
);
1224 if (!outfn
.empty()) {
1226 o
.open(outfn
.c_str(), ios::out
| ios::binary
| ios::trunc
);
1228 cerr
<< me
<< ": error writing '" << outfn
<< "'" << std::endl
;
1229 return EXIT_FAILURE
;
1239 tester
.check_overlapped_rules();
1241 if (!tester
.check_name_maps(max_id
)) {
1242 return EXIT_FAILURE
;
1248 if (tester
.get_output_utilization_all() ||
1249 tester
.get_output_utilization())
1250 tester
.set_output_statistics(true);
1252 int r
= tester
.test();
1254 return EXIT_FAILURE
;
1257 if (compare
.size()) {
1258 CrushWrapper crush2
;
1261 int r
= in
.read_file(compare
.c_str(), &error
);
1263 cerr
<< me
<< ": error reading '" << compare
<< "': "
1264 << error
<< std::endl
;
1265 return EXIT_FAILURE
;
1267 auto p
= in
.cbegin();
1271 cerr
<< me
<< ": unable to decode " << compare
<< std::endl
;
1272 return EXIT_FAILURE
;
1274 r
= tester
.compare(crush2
);
1276 return EXIT_FAILURE
;
1283 if (outfn
.empty()) {
1284 cout
<< me
<< " successfully built or modified map. Use '-o <file>' to write it out." << std::endl
;
1287 crush
.encode(bl
, CEPH_FEATURES_SUPPORTED_DEFAULT
);
1288 int r
= bl
.write_file(outfn
.c_str());
1290 cerr
<< me
<< ": error writing '" << outfn
<< "': " << cpp_strerror(r
) << std::endl
;
1291 return EXIT_FAILURE
;
1294 cout
<< "wrote crush map to " << outfn
<< std::endl
;
1302 * compile-command: "cd .. ; make crushtool && test/run-cli-tests"