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";
169 cout
<< " -i mapfn --create-simple-rule name root type mode\n"
170 << " create crush rule <name> to start from <root>,\n"
171 << " replicate across buckets of type <type>, using\n"
172 << " a choose mode of <firstn|indep>\n";
173 cout
<< " -i mapfn --remove-rule name\n"
174 << " remove the specified crush rule\n";
176 cout
<< "Options for the display/test stage\n";
178 cout
<< " -f --format the format of --dump, defaults to json-pretty\n";
179 cout
<< " can be one of json, json-pretty, xml, xml-pretty,\n";
180 cout
<< " table, table-kv, html, html-pretty\n";
181 cout
<< " --dump dump the crush map\n";
182 cout
<< " --tree print map summary as a tree\n";
183 cout
<< " --check [max_id] check if any item is referencing an unknown name/type\n";
184 cout
<< " -i mapfn --show-location id\n";
185 cout
<< " show location for given device id\n";
186 cout
<< " -i mapfn --test test a range of inputs on the map\n";
187 cout
<< " [--min-x x] [--max-x x] [--x x]\n";
188 cout
<< " [--min-rule r] [--max-rule r] [--rule r] [--ruleset rs]\n";
189 cout
<< " [--num-rep n]\n";
190 cout
<< " [--pool-id n] specifies pool id\n";
191 cout
<< " [--batches b] split the CRUSH mapping into b > 1 rounds\n";
192 cout
<< " [--weight|-w devno weight]\n";
193 cout
<< " where weight is 0 to 1.0\n";
194 cout
<< " [--simulate] simulate placements using a random\n";
195 cout
<< " number generator in place of the CRUSH\n";
196 cout
<< " algorithm\n";
197 cout
<< " --show-utilization show OSD usage\n";
198 cout
<< " --show-utilization-all\n";
199 cout
<< " include zero weight items\n";
200 cout
<< " --show-statistics show chi squared statistics\n";
201 cout
<< " --show-mappings show mappings\n";
202 cout
<< " --show-bad-mappings show bad mappings\n";
203 cout
<< " --show-choose-tries show choose tries histogram\n";
204 cout
<< " --output-name name\n";
205 cout
<< " prepend the data file(s) generated during the\n";
206 cout
<< " testing routine with name\n";
207 cout
<< " --output-csv\n";
208 cout
<< " export select data generated during testing routine\n";
209 cout
<< " to CSV files for off-line post-processing\n";
210 cout
<< " use --help-output for more information\n";
212 cout
<< "Options for the output stage\n";
214 cout
<< " [--outfn|-o outfile]\n";
215 cout
<< " specify output for modified crush map\n";
219 struct bucket_types_t
{
223 { "uniform", CRUSH_BUCKET_UNIFORM
},
224 { "list", CRUSH_BUCKET_LIST
},
225 { "straw", CRUSH_BUCKET_STRAW
},
226 { "straw2", CRUSH_BUCKET_STRAW2
},
227 { "tree", CRUSH_BUCKET_TREE
},
233 const char *buckettype
;
237 int main(int argc
, const char **argv
)
239 vector
<const char*> args
;
240 argv_to_vec(argc
, argv
, args
);
242 const char *me
= argv
[0];
243 std::string infn
, srcfn
, outfn
, add_name
, remove_name
, reweight_name
;
244 bool compile
= false;
245 bool decompile
= false;
249 bool display
= false;
251 string dump_format
= "json-pretty";
253 int full_location
= -1;
254 bool write_to_file
= false;
256 bool unsafe_tunables
= false;
258 bool reweight
= false;
260 bool update_item
= false;
261 bool add_rule
= false;
262 std::string rule_name
, rule_root
, rule_type
, rule_mode
;
263 bool del_rule
= false;
264 float add_weight
= 0;
265 map
<string
,string
> add_loc
;
266 float reweight_weight
= 0;
272 vector
<layer_t
> layers
;
274 int choose_local_tries
= -1;
275 int choose_local_fallback_tries
= -1;
276 int choose_total_tries
= -1;
277 int chooseleaf_descend_once
= -1;
278 int chooseleaf_vary_r
= -1;
279 int chooseleaf_stable
= -1;
280 int straw_calc_version
= -1;
281 int allowed_bucket_algs
= -1;
285 CrushTester
tester(crush
, cout
);
287 // we use -c, don't confuse the generic arg parsing
288 // only parse arguments from CEPH_ARGS, if in the environment
289 vector
<const char *> env_args
;
290 env_to_vec(env_args
);
291 auto cct
= global_init(NULL
, env_args
, CEPH_ENTITY_TYPE_CLIENT
,
292 CODE_ENVIRONMENT_UTILITY
,
293 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE
);
294 // crushtool times out occasionally when quits. so do not
295 // release the g_ceph_context.
297 common_init_finish(g_ceph_context
);
304 std::ostringstream err
;
306 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ) {
307 if (ceph_argparse_double_dash(args
, i
)) {
309 } else if (ceph_argparse_flag(args
, i
, "-h", "--help", (char*)NULL
)) {
312 } else if (ceph_argparse_witharg(args
, i
, &val
, "-d", "--decompile", (char*)NULL
)) {
315 } else if (ceph_argparse_witharg(args
, i
, &val
, "-i", "--infn", (char*)NULL
)) {
317 } else if (ceph_argparse_witharg(args
, i
, &val
, "-o", "--outfn", (char*)NULL
)) {
319 } else if (ceph_argparse_flag(args
, i
, "-v", "--verbose", (char*)NULL
)) {
321 } else if (ceph_argparse_flag(args
, i
, "--tree", (char*)NULL
)) {
323 } else if (ceph_argparse_witharg(args
, i
, &val
, "-f", "--format", (char*)NULL
)) {
325 } else if (ceph_argparse_flag(args
, i
, "--dump", (char*)NULL
)) {
327 } else if (ceph_argparse_flag(args
, i
, "--show_utilization", (char*)NULL
)) {
329 tester
.set_output_utilization(true);
330 } else if (ceph_argparse_flag(args
, i
, "--show_utilization_all", (char*)NULL
)) {
332 tester
.set_output_utilization_all(true);
333 } else if (ceph_argparse_flag(args
, i
, "--show_statistics", (char*)NULL
)) {
335 tester
.set_output_statistics(true);
336 } else if (ceph_argparse_flag(args
, i
, "--show_mappings", (char*)NULL
)) {
338 tester
.set_output_mappings(true);
339 } else if (ceph_argparse_flag(args
, i
, "--show_bad_mappings", (char*)NULL
)) {
341 tester
.set_output_bad_mappings(true);
342 } else if (ceph_argparse_flag(args
, i
, "--show_choose_tries", (char*)NULL
)) {
344 tester
.set_output_choose_tries(true);
345 } else if (ceph_argparse_witharg(args
, i
, &val
, "-c", "--compile", (char*)NULL
)) {
348 } else if (ceph_argparse_witharg(args
, i
, &max_id
, err
, "--check", (char*)NULL
)) {
350 } else if (ceph_argparse_flag(args
, i
, "-t", "--test", (char*)NULL
)) {
352 } else if (ceph_argparse_witharg(args
, i
, &full_location
, err
, "--show-location", (char*)NULL
)) {
353 } else if (ceph_argparse_flag(args
, i
, "-s", "--simulate", (char*)NULL
)) {
354 tester
.set_random_placement();
355 } else if (ceph_argparse_flag(args
, i
, "--enable-unsafe-tunables", (char*)NULL
)) {
356 unsafe_tunables
= true;
357 } else if (ceph_argparse_witharg(args
, i
, &choose_local_tries
, err
,
358 "--set_choose_local_tries", (char*)NULL
)) {
360 } else if (ceph_argparse_witharg(args
, i
, &choose_local_fallback_tries
, err
,
361 "--set_choose_local_fallback_tries", (char*)NULL
)) {
363 } else if (ceph_argparse_witharg(args
, i
, &choose_total_tries
, err
,
364 "--set_choose_total_tries", (char*)NULL
)) {
366 } else if (ceph_argparse_witharg(args
, i
, &chooseleaf_descend_once
, err
,
367 "--set_chooseleaf_descend_once", (char*)NULL
)) {
369 } else if (ceph_argparse_witharg(args
, i
, &chooseleaf_vary_r
, err
,
370 "--set_chooseleaf_vary_r", (char*)NULL
)) {
372 } else if (ceph_argparse_witharg(args
, i
, &chooseleaf_stable
, err
,
373 "--set_chooseleaf_stable", (char*)NULL
)) {
375 } else if (ceph_argparse_witharg(args
, i
, &straw_calc_version
, err
,
376 "--set_straw_calc_version", (char*)NULL
)) {
378 } else if (ceph_argparse_witharg(args
, i
, &allowed_bucket_algs
, err
,
379 "--set_allowed_bucket_algs", (char*)NULL
)) {
381 } else if (ceph_argparse_flag(args
, i
, "--reweight", (char*)NULL
)) {
383 } else if (ceph_argparse_witharg(args
, i
, &add_item
, err
, "--add_item", (char*)NULL
)) {
384 if (!err
.str().empty()) {
385 cerr
<< err
.str() << std::endl
;
388 if (i
== args
.end()) {
389 cerr
<< "expecting additional argument to --add-item" << std::endl
;
392 add_weight
= atof(*i
);
394 if (i
== args
.end()) {
395 cerr
<< "expecting additional argument to --add-item" << std::endl
;
400 } else if (ceph_argparse_witharg(args
, i
, &add_item
, err
, "--update_item", (char*)NULL
)) {
402 if (!err
.str().empty()) {
403 cerr
<< err
.str() << std::endl
;
406 if (i
== args
.end()) {
407 cerr
<< "expecting additional argument to --update-item" << std::endl
;
410 add_weight
= atof(*i
);
412 if (i
== args
.end()) {
413 cerr
<< "expecting additional argument to --update-item" << std::endl
;
418 } else if (ceph_argparse_witharg(args
, i
, &val
, err
, "--create-simple-rule", (char*)NULL
)) {
419 rule_name
.assign(val
);
420 if (!err
.str().empty()) {
421 cerr
<< err
.str() << std::endl
;
424 if (i
== args
.end()) {
425 cerr
<< "expecting additional argument to --create-simple-rule" << std::endl
;
429 rule_root
.assign(*i
);
431 if (i
== args
.end()) {
432 cerr
<< "expecting additional argument to --create-simple-rule" << std::endl
;
436 rule_type
.assign(*i
);
438 if (i
== args
.end()) {
439 cerr
<< "expecting additional argument to --create-simple-rule" << std::endl
;
443 rule_mode
.assign(*i
);
446 cout
<< "--create-simple-rule:"
447 << " name=" << rule_name
448 << " root=" << rule_root
449 << " type=" << rule_type
450 << " mode=" << rule_mode
453 } else if (ceph_argparse_witharg(args
, i
, &val
, "--remove-rule", (char*)NULL
)) {
454 rule_name
.assign(val
);
455 if (!err
.str().empty()) {
456 cerr
<< err
.str() << std::endl
;
460 } else if (ceph_argparse_witharg(args
, i
, &val
, "--loc", (char*)NULL
)) {
461 std::string
type(val
);
462 if (i
== args
.end()) {
463 cerr
<< "expecting additional argument to --loc" << std::endl
;
466 std::string
name(*i
);
468 add_loc
[type
] = name
;
469 } else if (ceph_argparse_flag(args
, i
, "--output-csv", (char*)NULL
)) {
470 write_to_file
= true;
471 tester
.set_output_data_file(true);
472 tester
.set_output_csv(true);
473 } else if (ceph_argparse_flag(args
, i
, "--help-output", (char*)NULL
)) {
474 data_analysis_usage();
476 } else if (ceph_argparse_witharg(args
, i
, &val
, "--output-name", (char*)NULL
)) {
477 std::string
name(val
);
478 if (i
== args
.end()) {
479 cerr
<< "expecting additional argument to --output-name" << std::endl
;
483 tester
.set_output_data_file_name(name
+ "-");
485 } else if (ceph_argparse_witharg(args
, i
, &val
, "--remove_item", (char*)NULL
)) {
487 } else if (ceph_argparse_witharg(args
, i
, &val
, "--reweight_item", (char*)NULL
)) {
489 if (i
== args
.end()) {
490 cerr
<< "expecting additional argument to --reweight-item" << std::endl
;
493 reweight_weight
= atof(*i
);
495 } else if (ceph_argparse_flag(args
, i
, "--build", (char*)NULL
)) {
497 } else if (ceph_argparse_witharg(args
, i
, &num_osds
, err
, "--num_osds", (char*)NULL
)) {
498 if (!err
.str().empty()) {
499 cerr
<< err
.str() << std::endl
;
502 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--num_rep", (char*)NULL
)) {
503 if (!err
.str().empty()) {
504 cerr
<< err
.str() << std::endl
;
507 tester
.set_num_rep(x
);
508 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--max_x", (char*)NULL
)) {
509 if (!err
.str().empty()) {
510 cerr
<< err
.str() << std::endl
;
514 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--min_x", (char*)NULL
)) {
515 if (!err
.str().empty()) {
516 cerr
<< err
.str() << std::endl
;
520 } else if (ceph_argparse_witharg(args
, i
, &z
, err
, "--pool_id", (char*)NULL
)) {
521 if (!err
.str().empty()) {
522 cerr
<< err
.str() << std::endl
;
525 tester
.set_pool_id(z
);
526 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--x", (char*)NULL
)) {
527 if (!err
.str().empty()) {
528 cerr
<< err
.str() << std::endl
;
532 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--max_rule", (char*)NULL
)) {
533 if (!err
.str().empty()) {
534 cerr
<< err
.str() << std::endl
;
537 tester
.set_max_rule(x
);
538 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--min_rule", (char*)NULL
)) {
539 if (!err
.str().empty()) {
540 cerr
<< err
.str() << std::endl
;
543 tester
.set_min_rule(x
);
544 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--rule", (char*)NULL
)) {
545 if (!err
.str().empty()) {
546 cerr
<< err
.str() << std::endl
;
550 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--ruleset", (char*)NULL
)) {
551 if (!err
.str().empty()) {
552 cerr
<< err
.str() << std::endl
;
555 tester
.set_ruleset(x
);
556 } else if (ceph_argparse_witharg(args
, i
, &x
, err
, "--batches", (char*)NULL
)) {
557 if (!err
.str().empty()) {
558 cerr
<< err
.str() << std::endl
;
561 tester
.set_batches(x
);
562 } else if (ceph_argparse_witharg(args
, i
, &y
, err
, "--mark-down-ratio", (char*)NULL
)) {
563 if (!err
.str().empty()) {
564 cerr
<< err
.str() << std::endl
;
567 tester
.set_device_down_ratio(y
);
568 } else if (ceph_argparse_witharg(args
, i
, &y
, err
, "--mark-down-bucket-ratio", (char*)NULL
)) {
569 if (!err
.str().empty()) {
570 cerr
<< err
.str() << std::endl
;
573 tester
.set_bucket_down_ratio(y
);
574 } else if (ceph_argparse_witharg(args
, i
, &tmp
, err
, "--weight", (char*)NULL
)) {
575 if (!err
.str().empty()) {
576 cerr
<< err
.str() << std::endl
;
580 if (i
== args
.end()) {
581 cerr
<< "expecting additional argument to --weight" << std::endl
;
586 tester
.set_device_weight(dev
, f
);
593 if (test
&& !check
&& !display
&& !write_to_file
) {
594 cerr
<< "WARNING: no output selected; use --output-csv or --show-X" << std::endl
;
597 if (decompile
+ compile
+ build
> 1) {
598 cerr
<< "cannot specify more than one of compile, decompile, and build" << std::endl
;
601 if (!check
&& !compile
&& !decompile
&& !build
&& !test
&& !reweight
&& !adjust
&& !tree
&& !dump
&&
602 add_item
< 0 && !add_rule
&& !del_rule
&& full_location
< 0 &&
603 remove_name
.empty() && reweight_name
.empty()) {
604 cerr
<< "no action specified; -h for help" << std::endl
;
607 if ((!build
) && (!args
.empty())) {
608 cerr
<< "unrecognized arguments: " << args
<< std::endl
;
612 if ((args
.size() % 3) != 0U) {
613 cerr
<< "remaining args: " << args
<< std::endl
;
614 cerr
<< "layers must be specified with 3-tuples of (name, buckettype, size)"
618 for (size_t j
= 0; j
< args
.size(); j
+= 3) {
621 l
.buckettype
= args
[j
+1];
622 l
.size
= atoi(args
[j
+2]);
628 if (outfn) cout << "outfn " << outfn << std::endl;
629 if (cinfn) cout << "cinfn " << cinfn << std::endl;
630 if (dinfn) cout << "dinfn " << dinfn << std::endl;
633 bool modified
= false;
643 if (isatty(STDIN_FILENO
)) {
644 cerr
<< "stdin must not be from a tty" << std::endl
;
647 r
= get_fd_data(STDIN_FILENO
, bl
);
649 cerr
<< "error reading data from STDIN" << std::endl
;
653 r
= bl
.read_file(infn
.c_str(), &error
);
655 cerr
<< me
<< ": error reading '" << infn
<< "': "
656 << error
<< std::endl
;
660 bufferlist::iterator p
= bl
.begin();
664 cerr
<< me
<< ": unable to decode " << infn
<< std::endl
;
673 ifstream
in(srcfn
.c_str());
675 cerr
<< "input file " << srcfn
<< " not found" << std::endl
;
679 CrushCompiler
cc(crush
, cerr
, verbose
);
681 cc
.enable_unsafe_tunables();
682 int r
= cc
.compile(in
, srcfn
.c_str());
690 if (layers
.empty()) {
691 cerr
<< me
<< ": must specify at least one layer" << std::endl
;
697 vector
<int> lower_items
;
698 vector
<int> lower_weights
;
700 crush
.set_max_devices(num_osds
);
701 for (int i
=0; i
<num_osds
; i
++) {
702 lower_items
.push_back(i
);
703 lower_weights
.push_back(0x10000);
704 crush
.set_item_name(i
, "osd." + stringify(i
));
707 crush
.set_type_name(0, "osd");
709 for (vector
<layer_t
>::iterator p
= layers
.begin(); p
!= layers
.end(); ++p
, type
++) {
712 dout(2) << "layer " << type
714 << " bucket type " << l
.buckettype
718 crush
.set_type_name(type
, l
.name
);
721 for (int i
= 0; bucket_types
[i
].name
; i
++)
722 if (l
.buckettype
&& strcmp(l
.buckettype
, bucket_types
[i
].name
) == 0) {
723 buckettype
= bucket_types
[i
].type
;
726 if (buckettype
< 0) {
727 cerr
<< "unknown bucket type '" << l
.buckettype
<< "'" << std::endl
;
732 vector
<int> cur_items
;
733 vector
<int> cur_weights
;
734 unsigned lower_pos
= 0; // lower pos
736 dout(2) << "lower_items " << lower_items
<< dendl
;
737 dout(2) << "lower_weights " << lower_weights
<< dendl
;
741 if (lower_pos
== lower_items
.size())
745 int weights
[num_osds
];
749 for (j
=0; j
<l
.size
|| l
.size
==0; j
++) {
750 if (lower_pos
== lower_items
.size())
752 items
[j
] = lower_items
[lower_pos
];
753 weights
[j
] = lower_weights
[lower_pos
];
754 weight
+= weights
[j
];
756 dout(2) << " item " << items
[j
] << " weight " << weights
[j
] << dendl
;
760 int r
= crush
.add_bucket(0, buckettype
, CRUSH_HASH_DEFAULT
, type
, j
, items
, weights
, &id
);
762 cerr
<< " Couldn't add bucket: " << cpp_strerror(r
) << std::endl
;
767 format
[sizeof(format
)-1] = '\0';
769 snprintf(format
, sizeof(format
)-1, "%s%%d", l
.name
);
771 strncpy(format
, l
.name
, sizeof(format
)-1);
773 snprintf(name
, sizeof(name
), format
, i
);
774 crush
.set_item_name(id
, name
);
776 dout(2) << " in bucket " << id
<< " '" << name
<< "' size " << j
<< " weight " << weight
<< dendl
;
778 cur_items
.push_back(id
);
779 cur_weights
.push_back(weight
);
783 lower_items
.swap(cur_items
);
784 lower_weights
.swap(cur_weights
);
787 string root
= layers
.back().size
== 0 ? layers
.back().name
:
788 string(layers
.back().name
) + "0";
792 crush
.find_roots(roots
);
793 if (roots
.size() > 1)
794 dout(1) << "The crush rulesets will use the root " << root
<< "\n"
795 << "and ignore the others.\n"
796 << "There are " << roots
.size() << " roots, they can be\n"
797 << "grouped into a single root by appending something like:\n"
802 if (OSDMap::build_simple_crush_rules(g_ceph_context
, crush
, root
, &cerr
))
810 if (choose_local_tries
>= 0) {
811 crush
.set_choose_local_tries(choose_local_tries
);
814 if (choose_local_fallback_tries
>= 0) {
815 crush
.set_choose_local_fallback_tries(choose_local_fallback_tries
);
818 if (choose_total_tries
>= 0) {
819 crush
.set_choose_total_tries(choose_total_tries
);
822 if (chooseleaf_descend_once
>= 0) {
823 crush
.set_chooseleaf_descend_once(chooseleaf_descend_once
);
826 if (chooseleaf_vary_r
>= 0) {
827 crush
.set_chooseleaf_vary_r(chooseleaf_vary_r
);
830 if (chooseleaf_stable
>= 0) {
831 crush
.set_chooseleaf_stable(chooseleaf_stable
);
834 if (straw_calc_version
>= 0) {
835 crush
.set_straw_calc_version(straw_calc_version
);
838 if (allowed_bucket_algs
>= 0) {
839 crush
.set_allowed_bucket_algs(allowed_bucket_algs
);
843 if (!reweight_name
.empty()) {
844 cout
<< me
<< " reweighting item " << reweight_name
<< " to " << reweight_weight
<< std::endl
;
846 if (!crush
.name_exists(reweight_name
)) {
847 cerr
<< " name " << reweight_name
<< " dne" << std::endl
;
850 int item
= crush
.get_item_id(reweight_name
);
851 r
= crush
.adjust_item_weightf(g_ceph_context
, item
, reweight_weight
);
856 cerr
<< me
<< " " << cpp_strerror(r
) << std::endl
;
861 if (!remove_name
.empty()) {
862 cout
<< me
<< " removing item " << remove_name
<< std::endl
;
864 if (!crush
.name_exists(remove_name
)) {
865 cerr
<< " name " << remove_name
<< " dne" << std::endl
;
868 int remove_item
= crush
.get_item_id(remove_name
);
869 r
= crush
.remove_item(g_ceph_context
, remove_item
, false);
874 cerr
<< me
<< " " << cpp_strerror(r
) << std::endl
;
882 r
= crush
.update_item(g_ceph_context
, add_item
, add_weight
, add_name
.c_str(), add_loc
);
884 r
= crush
.insert_item(g_ceph_context
, add_item
, add_weight
, add_name
.c_str(), add_loc
);
889 cerr
<< me
<< " " << cpp_strerror(r
) << std::endl
;
895 if (crush
.rule_exists(rule_name
)) {
896 cerr
<< "rule " << rule_name
<< " already exists" << std::endl
;
899 int r
= crush
.add_simple_rule(rule_name
, rule_root
, rule_type
, rule_mode
,
900 pg_pool_t::TYPE_REPLICATED
, &err
);
902 cerr
<< err
.str() << std::endl
;
909 if (!crush
.rule_exists(rule_name
)) {
910 cerr
<< "rule " << rule_name
<< " does not exist" << std::endl
;
913 int ruleno
= crush
.get_rule_id(rule_name
);
915 int r
= crush
.remove_rule(ruleno
);
917 cerr
<< "fail to remove rule " << rule_name
<< std::endl
;
924 crush
.reweight(g_ceph_context
);
930 if (full_location
>= 0) {
931 map
<string
, string
> loc
= crush
.get_full_location(full_location
);
932 for (map
<string
,string
>::iterator p
= loc
.begin();
935 cout
<< p
->first
<< "\t" << p
->second
<< std::endl
;
940 crush
.dump_tree(&cout
, NULL
);
944 boost::scoped_ptr
<Formatter
> f(Formatter::create(dump_format
, "json-pretty", "json-pretty"));
945 f
->open_object_section("crush_map");
953 CrushCompiler
cc(crush
, cerr
, verbose
);
954 if (!outfn
.empty()) {
956 o
.open(outfn
.c_str(), ios::out
| ios::binary
| ios::trunc
);
958 cerr
<< me
<< ": error writing '" << outfn
<< "'" << std::endl
;
969 tester
.check_overlapped_rules();
971 if (!tester
.check_name_maps(max_id
)) {
978 if (tester
.get_output_utilization_all() ||
979 tester
.get_output_utilization())
980 tester
.set_output_statistics(true);
982 int r
= tester
.test();
992 cout
<< me
<< " successfully built or modified map. Use '-o <file>' to write it out." << std::endl
;
995 crush
.encode(bl
, CEPH_FEATURES_SUPPORTED_DEFAULT
);
996 int r
= bl
.write_file(outfn
.c_str());
998 cerr
<< me
<< ": error writing '" << outfn
<< "': " << cpp_strerror(r
) << std::endl
;
1002 cout
<< "wrote crush map to " << outfn
<< std::endl
;
1010 * compile-command: "cd .. ; make crushtool && test/run-cli-tests"