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>
25 #include "common/debug.h"
26 #include "common/errno.h"
27 #include "common/config.h"
28 #include "common/Formatter.h"
30 #include "common/ceph_argparse.h"
31 #include "include/stringify.h"
32 #include "global/global_context.h"
33 #include "global/global_init.h"
34 #include "osd/OSDMap.h"
35 #include "crush/CrushWrapper.h"
36 #include "crush/CrushCompiler.h"
37 #include "crush/CrushTester.h"
38 #include "include/assert.h"
40 #define dout_context g_ceph_context
41 #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 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 --reweight recalculate all bucket weights\n";
170 cout
<< "Options for the display/test stage\n";
172 cout
<< " -f --format the format of --dump, defaults to json-pretty\n";
173 cout
<< " can be one of json, json-pretty, xml, xml-pretty,\n";
174 cout
<< " table, table-kv, html, html-pretty\n";
175 cout
<< " --dump dump the crush map\n";
176 cout
<< " --tree print map summary as a tree\n";
177 cout
<< " --check [max_id] check if any item is referencing an unknown name/type\n";
178 cout
<< " -i mapfn --show-location id\n";
179 cout
<< " show location for given device id\n";
180 cout
<< " -i mapfn --test test a range of inputs on the map\n";
181 cout
<< " [--min-x x] [--max-x x] [--x x]\n";
182 cout
<< " [--min-rule r] [--max-rule r] [--rule r] [--ruleset rs]\n";
183 cout
<< " [--num-rep n]\n";
184 cout
<< " [--pool-id n] specifies pool id\n";
185 cout
<< " [--batches b] split the CRUSH mapping into b > 1 rounds\n";
186 cout
<< " [--weight|-w devno weight]\n";
187 cout
<< " where weight is 0 to 1.0\n";
188 cout
<< " [--simulate] simulate placements using a random\n";
189 cout
<< " number generator in place of the CRUSH\n";
190 cout
<< " algorithm\n";
191 cout
<< " --show-utilization show OSD usage\n";
192 cout
<< " --show-utilization-all\n";
193 cout
<< " include zero weight items\n";
194 cout
<< " --show-statistics show chi squared statistics\n";
195 cout
<< " --show-mappings show mappings\n";
196 cout
<< " --show-bad-mappings show bad mappings\n";
197 cout
<< " --show-choose-tries show choose tries histogram\n";
198 cout
<< " --output-name name\n";
199 cout
<< " prepend the data file(s) generated during the\n";
200 cout
<< " testing routine with name\n";
201 cout
<< " --output-csv\n";
202 cout
<< " export select data generated during testing routine\n";
203 cout
<< " to CSV files for off-line post-processing\n";
204 cout
<< " use --help-output for more information\n";
206 cout
<< "Options for the output stage\n";
208 cout
<< " [--outfn|-o outfile]\n";
209 cout
<< " specify output for modified crush map\n";
213 struct bucket_types_t
{
217 { "uniform", CRUSH_BUCKET_UNIFORM
},
218 { "list", CRUSH_BUCKET_LIST
},
219 { "straw", CRUSH_BUCKET_STRAW
},
220 { "straw2", CRUSH_BUCKET_STRAW2
},
221 { "tree", CRUSH_BUCKET_TREE
},
227 const char *buckettype
;
231 int main(int argc
, const char **argv
)
233 vector
<const char*> args
;
234 argv_to_vec(argc
, argv
, args
);
236 const char *me
= argv
[0];
237 std::string infn
, srcfn
, outfn
, add_name
, remove_name
, reweight_name
;
238 bool compile
= false;
239 bool decompile
= false;
243 bool display
= false;
245 string dump_format
= "json-pretty";
247 int full_location
= -1;
248 bool write_to_file
= false;
250 bool unsafe_tunables
= false;
252 bool reweight
= false;
254 bool update_item
= false;
255 float add_weight
= 0;
256 map
<string
,string
> add_loc
;
257 float reweight_weight
= 0;
263 vector
<layer_t
> layers
;
265 int choose_local_tries
= -1;
266 int choose_local_fallback_tries
= -1;
267 int choose_total_tries
= -1;
268 int chooseleaf_descend_once
= -1;
269 int chooseleaf_vary_r
= -1;
270 int chooseleaf_stable
= -1;
271 int straw_calc_version
= -1;
272 int allowed_bucket_algs
= -1;
276 CrushTester
tester(crush
, cout
);
278 // we use -c, don't confuse the generic arg parsing
279 // only parse arguments from CEPH_ARGS, if in the environment
280 vector
<const char *> env_args
;
281 env_to_vec(env_args
);
282 auto cct
= global_init(NULL
, env_args
, CEPH_ENTITY_TYPE_CLIENT
,
283 CODE_ENVIRONMENT_UTILITY
,
284 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE
);
285 // crushtool times out occasionally when quits. so do not
286 // release the g_ceph_context.
288 common_init_finish(g_ceph_context
);
295 std::ostringstream err
;
297 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ) {
298 if (ceph_argparse_double_dash(args
, i
)) {
300 } else if (ceph_argparse_flag(args
, i
, "-h", "--help", (char*)NULL
)) {
303 } else if (ceph_argparse_witharg(args
, i
, &val
, "-d", "--decompile", (char*)NULL
)) {
306 } else if (ceph_argparse_witharg(args
, i
, &val
, "-i", "--infn", (char*)NULL
)) {
308 } else if (ceph_argparse_witharg(args
, i
, &val
, "-o", "--outfn", (char*)NULL
)) {
310 } else if (ceph_argparse_flag(args
, i
, "-v", "--verbose", (char*)NULL
)) {
312 } else if (ceph_argparse_flag(args
, i
, "--tree", (char*)NULL
)) {
314 } else if (ceph_argparse_witharg(args
, i
, &val
, "-f", "--format", (char*)NULL
)) {
316 } else if (ceph_argparse_flag(args
, i
, "--dump", (char*)NULL
)) {
318 } else if (ceph_argparse_flag(args
, i
, "--show_utilization", (char*)NULL
)) {
320 tester
.set_output_utilization(true);
321 } else if (ceph_argparse_flag(args
, i
, "--show_utilization_all", (char*)NULL
)) {
323 tester
.set_output_utilization_all(true);
324 } else if (ceph_argparse_flag(args
, i
, "--show_statistics", (char*)NULL
)) {
326 tester
.set_output_statistics(true);
327 } else if (ceph_argparse_flag(args
, i
, "--show_mappings", (char*)NULL
)) {
329 tester
.set_output_mappings(true);
330 } else if (ceph_argparse_flag(args
, i
, "--show_bad_mappings", (char*)NULL
)) {
332 tester
.set_output_bad_mappings(true);
333 } else if (ceph_argparse_flag(args
, i
, "--show_choose_tries", (char*)NULL
)) {
335 tester
.set_output_choose_tries(true);
336 } else if (ceph_argparse_witharg(args
, i
, &val
, "-c", "--compile", (char*)NULL
)) {
339 } else if (ceph_argparse_witharg(args
, i
, &max_id
, err
, "--check", (char*)NULL
)) {
341 } else if (ceph_argparse_flag(args
, i
, "-t", "--test", (char*)NULL
)) {
343 } else if (ceph_argparse_witharg(args
, i
, &full_location
, err
, "--show-location", (char*)NULL
)) {
344 } else if (ceph_argparse_flag(args
, i
, "-s", "--simulate", (char*)NULL
)) {
345 tester
.set_random_placement();
346 } else if (ceph_argparse_flag(args
, i
, "--enable-unsafe-tunables", (char*)NULL
)) {
347 unsafe_tunables
= true;
348 } else if (ceph_argparse_witharg(args
, i
, &choose_local_tries
, err
,
349 "--set_choose_local_tries", (char*)NULL
)) {
351 } else if (ceph_argparse_witharg(args
, i
, &choose_local_fallback_tries
, err
,
352 "--set_choose_local_fallback_tries", (char*)NULL
)) {
354 } else if (ceph_argparse_witharg(args
, i
, &choose_total_tries
, err
,
355 "--set_choose_total_tries", (char*)NULL
)) {
357 } else if (ceph_argparse_witharg(args
, i
, &chooseleaf_descend_once
, err
,
358 "--set_chooseleaf_descend_once", (char*)NULL
)) {
360 } else if (ceph_argparse_witharg(args
, i
, &chooseleaf_vary_r
, err
,
361 "--set_chooseleaf_vary_r", (char*)NULL
)) {
363 } else if (ceph_argparse_witharg(args
, i
, &chooseleaf_stable
, err
,
364 "--set_chooseleaf_stable", (char*)NULL
)) {
366 } else if (ceph_argparse_witharg(args
, i
, &straw_calc_version
, err
,
367 "--set_straw_calc_version", (char*)NULL
)) {
369 } else if (ceph_argparse_witharg(args
, i
, &allowed_bucket_algs
, err
,
370 "--set_allowed_bucket_algs", (char*)NULL
)) {
372 } else if (ceph_argparse_flag(args
, i
, "--reweight", (char*)NULL
)) {
374 } else if (ceph_argparse_witharg(args
, i
, &add_item
, err
, "--add_item", (char*)NULL
)) {
375 if (!err
.str().empty()) {
376 cerr
<< err
.str() << std::endl
;
379 if (i
== args
.end()) {
380 cerr
<< "expecting additional argument to --add-item" << std::endl
;
383 add_weight
= atof(*i
);
385 if (i
== args
.end()) {
386 cerr
<< "expecting additional argument to --add-item" << std::endl
;
391 } else if (ceph_argparse_witharg(args
, i
, &add_item
, err
, "--update_item", (char*)NULL
)) {
393 if (!err
.str().empty()) {
394 cerr
<< err
.str() << std::endl
;
397 if (i
== args
.end()) {
398 cerr
<< "expecting additional argument to --update-item" << std::endl
;
401 add_weight
= atof(*i
);
403 if (i
== args
.end()) {
404 cerr
<< "expecting additional argument to --update-item" << std::endl
;
409 } else if (ceph_argparse_witharg(args
, i
, &val
, "--loc", (char*)NULL
)) {
410 std::string
type(val
);
411 if (i
== args
.end()) {
412 cerr
<< "expecting additional argument to --loc" << std::endl
;
415 std::string
name(*i
);
417 add_loc
[type
] = name
;
418 } else if (ceph_argparse_flag(args
, i
, "--output-csv", (char*)NULL
)) {
419 write_to_file
= true;
420 tester
.set_output_data_file(true);
421 tester
.set_output_csv(true);
422 } else if (ceph_argparse_flag(args
, i
, "--help-output", (char*)NULL
)) {
423 data_analysis_usage();
425 } else if (ceph_argparse_witharg(args
, i
, &val
, "--output-name", (char*)NULL
)) {
426 std::string
name(val
);
427 if (i
== args
.end()) {
428 cerr
<< "expecting additional argument to --output-name" << std::endl
;
432 tester
.set_output_data_file_name(name
+ "-");
434 } else if (ceph_argparse_witharg(args
, i
, &val
, "--remove_item", (char*)NULL
)) {
436 } else if (ceph_argparse_witharg(args
, i
, &val
, "--reweight_item", (char*)NULL
)) {
438 if (i
== args
.end()) {
439 cerr
<< "expecting additional argument to --reweight-item" << std::endl
;
442 reweight_weight
= atof(*i
);
444 } else if (ceph_argparse_flag(args
, i
, "--build", (char*)NULL
)) {
446 } else if (ceph_argparse_witharg(args
, i
, &num_osds
, err
, "--num_osds", (char*)NULL
)) {
447 if (!err
.str().empty()) {
448 cerr
<< err
.str() << std::endl
;
451 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--num_rep", (char*)NULL
)) {
452 if (!err
.str().empty()) {
453 cerr
<< err
.str() << std::endl
;
456 tester
.set_num_rep(x
);
457 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--max_x", (char*)NULL
)) {
458 if (!err
.str().empty()) {
459 cerr
<< err
.str() << std::endl
;
463 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--min_x", (char*)NULL
)) {
464 if (!err
.str().empty()) {
465 cerr
<< err
.str() << std::endl
;
469 } else if (ceph_argparse_witharg(args
, i
, &z
, err
, "--pool_id", (char*)NULL
)) {
470 if (!err
.str().empty()) {
471 cerr
<< err
.str() << std::endl
;
474 tester
.set_pool_id(z
);
475 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--x", (char*)NULL
)) {
476 if (!err
.str().empty()) {
477 cerr
<< err
.str() << std::endl
;
481 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--max_rule", (char*)NULL
)) {
482 if (!err
.str().empty()) {
483 cerr
<< err
.str() << std::endl
;
486 tester
.set_max_rule(x
);
487 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--min_rule", (char*)NULL
)) {
488 if (!err
.str().empty()) {
489 cerr
<< err
.str() << std::endl
;
492 tester
.set_min_rule(x
);
493 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--rule", (char*)NULL
)) {
494 if (!err
.str().empty()) {
495 cerr
<< err
.str() << std::endl
;
499 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--ruleset", (char*)NULL
)) {
500 if (!err
.str().empty()) {
501 cerr
<< err
.str() << std::endl
;
504 tester
.set_ruleset(x
);
505 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--batches", (char*)NULL
)) {
506 if (!err
.str().empty()) {
507 cerr
<< err
.str() << std::endl
;
510 tester
.set_batches(x
);
511 } else if (ceph_argparse_witharg(args
, i
, &y
, err
, "--mark-down-ratio", (char*)NULL
)) {
512 if (!err
.str().empty()) {
513 cerr
<< err
.str() << std::endl
;
516 tester
.set_device_down_ratio(y
);
517 } else if (ceph_argparse_witharg(args
, i
, &y
, err
, "--mark-down-bucket-ratio", (char*)NULL
)) {
518 if (!err
.str().empty()) {
519 cerr
<< err
.str() << std::endl
;
522 tester
.set_bucket_down_ratio(y
);
523 } else if (ceph_argparse_witharg(args
, i
, &tmp
, err
, "--weight", (char*)NULL
)) {
524 if (!err
.str().empty()) {
525 cerr
<< err
.str() << std::endl
;
529 if (i
== args
.end()) {
530 cerr
<< "expecting additional argument to --weight" << std::endl
;
535 tester
.set_device_weight(dev
, f
);
542 if (test
&& !check
&& !display
&& !write_to_file
) {
543 cerr
<< "WARNING: no output selected; use --output-csv or --show-X" << std::endl
;
546 if (decompile
+ compile
+ build
> 1) {
547 cerr
<< "cannot specify more than one of compile, decompile, and build" << std::endl
;
550 if (!check
&& !compile
&& !decompile
&& !build
&& !test
&& !reweight
&& !adjust
&& !tree
&& !dump
&&
551 add_item
< 0 && full_location
< 0 &&
552 remove_name
.empty() && reweight_name
.empty()) {
553 cerr
<< "no action specified; -h for help" << std::endl
;
556 if ((!build
) && (!args
.empty())) {
557 cerr
<< "unrecognized arguments: " << args
<< std::endl
;
561 if ((args
.size() % 3) != 0U) {
562 cerr
<< "remaining args: " << args
<< std::endl
;
563 cerr
<< "layers must be specified with 3-tuples of (name, buckettype, size)"
567 for (size_t j
= 0; j
< args
.size(); j
+= 3) {
570 l
.buckettype
= args
[j
+1];
571 l
.size
= atoi(args
[j
+2]);
577 if (outfn) cout << "outfn " << outfn << std::endl;
578 if (cinfn) cout << "cinfn " << cinfn << std::endl;
579 if (dinfn) cout << "dinfn " << dinfn << std::endl;
582 bool modified
= false;
592 if (isatty(STDIN_FILENO
)) {
593 cerr
<< "stdin must not be from a tty" << std::endl
;
596 r
= get_fd_data(STDIN_FILENO
, bl
);
598 cerr
<< "error reading data from STDIN" << std::endl
;
602 r
= bl
.read_file(infn
.c_str(), &error
);
604 cerr
<< me
<< ": error reading '" << infn
<< "': "
605 << error
<< std::endl
;
609 bufferlist::iterator p
= bl
.begin();
613 cerr
<< me
<< ": unable to decode " << infn
<< std::endl
;
622 ifstream
in(srcfn
.c_str());
624 cerr
<< "input file " << srcfn
<< " not found" << std::endl
;
628 CrushCompiler
cc(crush
, cerr
, verbose
);
630 cc
.enable_unsafe_tunables();
631 int r
= cc
.compile(in
, srcfn
.c_str());
639 if (layers
.empty()) {
640 cerr
<< me
<< ": must specify at least one layer" << std::endl
;
646 vector
<int> lower_items
;
647 vector
<int> lower_weights
;
649 crush
.set_max_devices(num_osds
);
650 for (int i
=0; i
<num_osds
; i
++) {
651 lower_items
.push_back(i
);
652 lower_weights
.push_back(0x10000);
653 crush
.set_item_name(i
, "osd." + stringify(i
));
656 crush
.set_type_name(0, "osd");
658 for (vector
<layer_t
>::iterator p
= layers
.begin(); p
!= layers
.end(); ++p
, type
++) {
661 dout(2) << "layer " << type
663 << " bucket type " << l
.buckettype
667 crush
.set_type_name(type
, l
.name
);
670 for (int i
= 0; bucket_types
[i
].name
; i
++)
671 if (l
.buckettype
&& strcmp(l
.buckettype
, bucket_types
[i
].name
) == 0) {
672 buckettype
= bucket_types
[i
].type
;
675 if (buckettype
< 0) {
676 cerr
<< "unknown bucket type '" << l
.buckettype
<< "'" << std::endl
;
681 vector
<int> cur_items
;
682 vector
<int> cur_weights
;
683 unsigned lower_pos
= 0; // lower pos
685 dout(2) << "lower_items " << lower_items
<< dendl
;
686 dout(2) << "lower_weights " << lower_weights
<< dendl
;
690 if (lower_pos
== lower_items
.size())
694 int weights
[num_osds
];
698 for (j
=0; j
<l
.size
|| l
.size
==0; j
++) {
699 if (lower_pos
== lower_items
.size())
701 items
[j
] = lower_items
[lower_pos
];
702 weights
[j
] = lower_weights
[lower_pos
];
703 weight
+= weights
[j
];
705 dout(2) << " item " << items
[j
] << " weight " << weights
[j
] << dendl
;
709 int r
= crush
.add_bucket(0, buckettype
, CRUSH_HASH_DEFAULT
, type
, j
, items
, weights
, &id
);
711 cerr
<< " Couldn't add bucket: " << cpp_strerror(r
) << std::endl
;
716 format
[sizeof(format
)-1] = '\0';
718 snprintf(format
, sizeof(format
)-1, "%s%%d", l
.name
);
720 strncpy(format
, l
.name
, sizeof(format
)-1);
722 snprintf(name
, sizeof(name
), format
, i
);
723 crush
.set_item_name(id
, name
);
725 dout(2) << " in bucket " << id
<< " '" << name
<< "' size " << j
<< " weight " << weight
<< dendl
;
727 cur_items
.push_back(id
);
728 cur_weights
.push_back(weight
);
732 lower_items
.swap(cur_items
);
733 lower_weights
.swap(cur_weights
);
736 string root
= layers
.back().size
== 0 ? layers
.back().name
:
737 string(layers
.back().name
) + "0";
741 crush
.find_roots(roots
);
742 if (roots
.size() > 1)
743 dout(1) << "The crush rulesets will use the root " << root
<< "\n"
744 << "and ignore the others.\n"
745 << "There are " << roots
.size() << " roots, they can be\n"
746 << "grouped into a single root by appending something like:\n"
751 if (OSDMap::build_simple_crush_rulesets(g_ceph_context
, crush
, root
, &cerr
))
759 if (choose_local_tries
>= 0) {
760 crush
.set_choose_local_tries(choose_local_tries
);
763 if (choose_local_fallback_tries
>= 0) {
764 crush
.set_choose_local_fallback_tries(choose_local_fallback_tries
);
767 if (choose_total_tries
>= 0) {
768 crush
.set_choose_total_tries(choose_total_tries
);
771 if (chooseleaf_descend_once
>= 0) {
772 crush
.set_chooseleaf_descend_once(chooseleaf_descend_once
);
775 if (chooseleaf_vary_r
>= 0) {
776 crush
.set_chooseleaf_vary_r(chooseleaf_vary_r
);
779 if (chooseleaf_stable
>= 0) {
780 crush
.set_chooseleaf_stable(chooseleaf_stable
);
783 if (straw_calc_version
>= 0) {
784 crush
.set_straw_calc_version(straw_calc_version
);
787 if (allowed_bucket_algs
>= 0) {
788 crush
.set_allowed_bucket_algs(allowed_bucket_algs
);
792 if (!reweight_name
.empty()) {
793 cout
<< me
<< " reweighting item " << reweight_name
<< " to " << reweight_weight
<< std::endl
;
795 if (!crush
.name_exists(reweight_name
)) {
796 cerr
<< " name " << reweight_name
<< " dne" << std::endl
;
799 int item
= crush
.get_item_id(reweight_name
);
800 r
= crush
.adjust_item_weightf(g_ceph_context
, item
, reweight_weight
);
805 cerr
<< me
<< " " << cpp_strerror(r
) << std::endl
;
810 if (!remove_name
.empty()) {
811 cout
<< me
<< " removing item " << remove_name
<< std::endl
;
813 if (!crush
.name_exists(remove_name
)) {
814 cerr
<< " name " << remove_name
<< " dne" << std::endl
;
817 int remove_item
= crush
.get_item_id(remove_name
);
818 r
= crush
.remove_item(g_ceph_context
, remove_item
, false);
823 cerr
<< me
<< " " << cpp_strerror(r
) << std::endl
;
831 r
= crush
.update_item(g_ceph_context
, add_item
, add_weight
, add_name
.c_str(), add_loc
);
833 r
= crush
.insert_item(g_ceph_context
, add_item
, add_weight
, add_name
.c_str(), add_loc
);
838 cerr
<< me
<< " " << cpp_strerror(r
) << std::endl
;
844 crush
.reweight(g_ceph_context
);
850 if (full_location
>= 0) {
851 map
<string
, string
> loc
= crush
.get_full_location(full_location
);
852 for (map
<string
,string
>::iterator p
= loc
.begin();
855 cout
<< p
->first
<< "\t" << p
->second
<< std::endl
;
860 crush
.dump_tree(&cout
, NULL
);
864 boost::scoped_ptr
<Formatter
> f(Formatter::create(dump_format
, "json-pretty", "json-pretty"));
865 f
->open_object_section("crush_map");
873 CrushCompiler
cc(crush
, cerr
, verbose
);
874 if (!outfn
.empty()) {
876 o
.open(outfn
.c_str(), ios::out
| ios::binary
| ios::trunc
);
878 cerr
<< me
<< ": error writing '" << outfn
<< "'" << std::endl
;
889 tester
.check_overlapped_rules();
891 if (!tester
.check_name_maps(max_id
)) {
898 if (tester
.get_output_utilization_all() ||
899 tester
.get_output_utilization())
900 tester
.set_output_statistics(true);
902 int r
= tester
.test();
912 cout
<< me
<< " successfully built or modified map. Use '-o <file>' to write it out." << std::endl
;
915 crush
.encode(bl
, CEPH_FEATURES_SUPPORTED_DEFAULT
);
916 int r
= bl
.write_file(outfn
.c_str());
918 cerr
<< me
<< ": error writing '" << outfn
<< "': " << cpp_strerror(r
) << std::endl
;
922 cout
<< "wrote crush map to " << outfn
<< std::endl
;
930 * compile-command: "cd .. ; make crushtool && test/run-cli-tests"