]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/crushtool.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / tools / crushtool.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 * Copyright (C) 2014 Cloudwatt <libre.licensing@cloudwatt.com>
8 *
9 * Author: Loic Dachary <loic@dachary.org>
10 *
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.
15 *
16 */
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <errno.h>
22
23 #include <fstream>
24 #include <type_traits>
25
26 #include "common/debug.h"
27 #include "common/errno.h"
28 #include "common/config.h"
29 #include "common/Formatter.h"
30
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"
40
41 #define dout_context g_ceph_context
42 #define dout_subsys ceph_subsys_crush
43
44
45 const char *infn = "stdin";
46
47 static int get_fd_data(int fd, bufferlist &bl)
48 {
49
50 uint64_t total = 0;
51 do {
52 ssize_t bytes = bl.read_fd(fd, 1024*1024);
53 if (bytes < 0) {
54 cerr << "read_fd error " << cpp_strerror(-bytes) << "\n";
55 return -1;
56 }
57
58 if (bytes == 0)
59 break;
60
61 total += bytes;
62 } while(true);
63
64 ceph_assert(bl.length() == total);
65 return 0;
66 }
67
68 ////////////////////////////////////////////////////////////////////////////
69
70 void data_analysis_usage()
71 {
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";
106 }
107
108 void usage()
109 {
110 cout << "usage: crushtool ...\n";
111 cout << "\n";
112 cout << "Display, modify and test a crush map\n";
113 cout << "\n";
114 cout << "There are five stages, running one after the other:\n";
115 cout << "\n";
116 cout << " - input/build\n";
117 cout << " - tunables adjustments\n";
118 cout << " - modifications\n";
119 cout << " - display/test\n";
120 cout << " - output\n";
121 cout << "\n";
122 cout << "Options that are not specific to a stage.\n";
123 cout << "\n";
124 cout << " [--infn|-i infile]\n";
125 cout << " read the crush map from infile\n";
126 cout << "\n";
127 cout << "Options for the input/build stage\n";
128 cout << "\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";
138 cout << "\n";
139 cout << "Options for the tunables adjustments stage\n";
140 cout << "\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";
154 cout << "\n";
155 cout << "Options for the modifications stage\n";
156 cout << "\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"
170 << " location\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";
187 cout << "\n";
188 cout << "Options for the display/test stage\n";
189 cout << "\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";
230 cout << "\n";
231 cout << "Options for the output stage\n";
232 cout << "\n";
233 cout << " [--outfn|-o outfile]\n";
234 cout << " specify output for modified crush map\n";
235 cout << "\n";
236 }
237
238 struct bucket_types_t {
239 const char *name;
240 int type;
241 } bucket_types[] = {
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 },
247 { 0, 0 },
248 };
249
250 struct layer_t {
251 const char *name;
252 const char *buckettype;
253 int size;
254 };
255
256 template<typename... Args>
257 bool argparse_withargs(std::vector<const char*> &args,
258 std::vector<const char*>::iterator& i,
259 std::ostream& oss,
260 const char* opt,
261 Args*... opts)
262 {
263 if (!ceph_argparse_flag(args, i, opt, nullptr)) {
264 return false;
265 }
266 auto parse = [&](auto& opt) {
267 if (i == args.end()) {
268 oss << "expecting additional argument to " << opt;
269 return false;
270 }
271 using opt_t = std::remove_pointer_t<decay_t<decltype(opt)>>;
272 string err;
273 if constexpr (std::is_same_v<opt_t, string>) {
274 opt->assign(*i);
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);
279 }
280 i = args.erase(i);
281 if (err.empty())
282 return true;
283 else {
284 oss << err;
285 return false;
286 }
287 };
288 (... && parse(opts));
289 return true;
290 }
291
292 int do_add_bucket(CephContext* cct,
293 const char* me,
294 CrushWrapper& crush,
295 const string& add_name,
296 const string& add_type,
297 const map<string,string>& add_loc) {
298 int bucketno;
299 if (crush.name_exists(add_name)) {
300 cerr << me << " bucket '" << add_name << "' already exists" << std::endl;
301 return -EEXIST;
302 }
303 int type = crush.get_type_id(add_type);
304 if (type <= 0) {
305 cerr << me << " bad bucket type: " << add_type << std::endl;
306 return -EINVAL;
307 }
308 if (int r = crush.add_bucket(0, 0, CRUSH_HASH_DEFAULT, type, 0, nullptr, nullptr, &bucketno);
309 r < 0) {
310 cerr << me << " unable to add bucket: " << cpp_strerror(r) << std::endl;
311 return r;
312 }
313 if (int r = crush.set_item_name(bucketno, add_name); r < 0) {
314 cerr << me << " bad bucket name: " << add_name << std::endl;
315 return r;
316 }
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;
321 return r;
322 }
323 }
324 }
325 return 0;
326 }
327
328 // return 1 for no change, 0 for successful change, negative on error
329 int do_move_item(CephContext* cct,
330 const char *me,
331 CrushWrapper& crush,
332 const string& name,
333 const map<string,string>& loc)
334 {
335 if (!crush.name_exists(name)) {
336 cerr << me << " item '" << name << "' does not exist" << std::endl;
337 return -ENOENT;
338 }
339 int id = crush.get_item_id(name);
340 if (loc.empty()) {
341 cerr << me << " expecting additional --loc argument to --move" << std::endl;
342 return -EINVAL;
343 }
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;
347 return 1;
348 }
349 if (id >= 0) {
350 switch (int r = crush.create_or_move_item(cct, id, 0, name, loc)) {
351 case 0:
352 return 1;
353 case 1:
354 return 0;
355 default:
356 return r;
357 }
358 } else {
359 return crush.move_bucket(cct, id, loc);
360 }
361 }
362
363 int main(int argc, const char **argv)
364 {
365 vector<const char*> args;
366 argv_to_vec(argc, argv, args);
367 if (args.empty()) {
368 cerr << argv[0] << ": -h or --help for usage" << std::endl;
369 exit(1);
370 }
371 if (ceph_argparse_need_usage(args)) {
372 usage();
373 exit(0);
374 }
375
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;
381 bool check = false;
382 int max_id = -1;
383 bool test = false;
384 bool display = false;
385 bool tree = false;
386 string dump_format = "json-pretty";
387 bool dump = false;
388 int full_location = -1;
389 bool write_to_file = false;
390 int verbose = 0;
391 bool unsafe_tunables = false;
392
393 bool rebuild_class_roots = false;
394
395 bool reweight = false;
396 int add_item = -1;
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;
406
407 bool adjust = false;
408
409 int build = 0;
410 int num_osds =0;
411 vector<layer_t> layers;
412
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;
421
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
426
427 string compare;
428
429 CrushWrapper crush;
430
431 CrushTester tester(crush, cout);
432
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.
441 cct->get();
442 common_init_finish(g_ceph_context);
443
444 int x;
445 float y;
446 long long z;
447
448 std::string val;
449 std::ostringstream err;
450 int tmp;
451 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
452 if (ceph_argparse_double_dash(args, i)) {
453 break;
454 } else if (ceph_argparse_witharg(args, i, &val, "-d", "--decompile", (char*)NULL)) {
455 infn = val;
456 decompile = true;
457 } else if (ceph_argparse_witharg(args, i, &val, "-i", "--infn", (char*)NULL)) {
458 infn = val;
459 } else if (ceph_argparse_witharg(args, i, &val, "-o", "--outfn", (char*)NULL)) {
460 outfn = val;
461 } else if (ceph_argparse_flag(args, i, "-v", "--verbose", (char*)NULL)) {
462 verbose += 1;
463 } else if (ceph_argparse_witharg(args, i, &val, "--compare", (char*)NULL)) {
464 compare = val;
465 } else if (ceph_argparse_flag(args, i, "--reclassify", (char*)NULL)) {
466 reclassify = true;
467 } else if (ceph_argparse_witharg(args, i, &val, "--reclassify-bucket",
468 (char*)NULL)) {
469 if (i == args.end()) {
470 cerr << "expecting additional argument" << std::endl;
471 return EXIT_FAILURE;
472 }
473 string c = *i;
474 i = args.erase(i);
475 if (i == args.end()) {
476 cerr << "expecting additional argument" << std::endl;
477 return EXIT_FAILURE;
478 }
479 reclassify_bucket[val] = make_pair(c, *i);
480 i = args.erase(i);
481 } else if (ceph_argparse_witharg(args, i, &val, "--reclassify-root",
482 (char*)NULL)) {
483 if (i == args.end()) {
484 cerr << "expecting additional argument" << std::endl;
485 return EXIT_FAILURE;
486 }
487 reclassify_root[val] = *i;
488 i = args.erase(i);
489 } else if (ceph_argparse_witharg(args, i, &val, "--set-subtree-class",
490 (char*)NULL)) {
491 if (i == args.end()) {
492 cerr << "expecting additional argument" << std::endl;
493 return EXIT_FAILURE;
494 }
495 set_subtree_class[val] = *i;
496 i = args.erase(i);
497 } else if (ceph_argparse_flag(args, i, "--tree", (char*)NULL)) {
498 tree = true;
499 } else if (ceph_argparse_witharg(args, i, &val, "-f", "--format", (char*)NULL)) {
500 dump_format = val;
501 } else if (ceph_argparse_flag(args, i, "--dump", (char*)NULL)) {
502 dump = true;
503 } else if (ceph_argparse_flag(args, i, "--show_utilization", (char*)NULL)) {
504 display = true;
505 tester.set_output_utilization(true);
506 } else if (ceph_argparse_flag(args, i, "--show_utilization_all", (char*)NULL)) {
507 display = true;
508 tester.set_output_utilization_all(true);
509 } else if (ceph_argparse_flag(args, i, "--show_statistics", (char*)NULL)) {
510 display = true;
511 tester.set_output_statistics(true);
512 } else if (ceph_argparse_flag(args, i, "--show_mappings", (char*)NULL)) {
513 display = true;
514 tester.set_output_mappings(true);
515 } else if (ceph_argparse_flag(args, i, "--show_bad_mappings", (char*)NULL)) {
516 display = true;
517 tester.set_output_bad_mappings(true);
518 } else if (ceph_argparse_flag(args, i, "--show_choose_tries", (char*)NULL)) {
519 display = true;
520 tester.set_output_choose_tries(true);
521 } else if (ceph_argparse_witharg(args, i, &val, "-c", "--compile", (char*)NULL)) {
522 srcfn = val;
523 compile = true;
524 } else if (ceph_argparse_witharg(args, i, &max_id, err, "--check", (char*)NULL)) {
525 check = true;
526 } else if (ceph_argparse_flag(args, i, "-t", "--test", (char*)NULL)) {
527 test = true;
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)) {
535 adjust = true;
536 } else if (ceph_argparse_witharg(args, i, &choose_local_fallback_tries, err,
537 "--set_choose_local_fallback_tries", (char*)NULL)) {
538 adjust = true;
539 } else if (ceph_argparse_witharg(args, i, &choose_total_tries, err,
540 "--set_choose_total_tries", (char*)NULL)) {
541 adjust = true;
542 } else if (ceph_argparse_witharg(args, i, &chooseleaf_descend_once, err,
543 "--set_chooseleaf_descend_once", (char*)NULL)) {
544 adjust = true;
545 } else if (ceph_argparse_witharg(args, i, &chooseleaf_vary_r, err,
546 "--set_chooseleaf_vary_r", (char*)NULL)) {
547 adjust = true;
548 } else if (ceph_argparse_witharg(args, i, &chooseleaf_stable, err,
549 "--set_chooseleaf_stable", (char*)NULL)) {
550 adjust = true;
551 } else if (ceph_argparse_witharg(args, i, &straw_calc_version, err,
552 "--set_straw_calc_version", (char*)NULL)) {
553 adjust = true;
554 } else if (ceph_argparse_witharg(args, i, &allowed_bucket_algs, err,
555 "--set_allowed_bucket_algs", (char*)NULL)) {
556 adjust = true;
557 } else if (ceph_argparse_flag(args, i, "--reweight", (char*)NULL)) {
558 reweight = true;
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;
564 return EXIT_FAILURE;
565 }
566 if (i == args.end()) {
567 cerr << "expecting additional argument to --add-item" << std::endl;
568 return EXIT_FAILURE;
569 }
570 add_weight = atof(*i);
571 i = args.erase(i);
572 if (i == args.end()) {
573 cerr << "expecting additional argument to --add-item" << std::endl;
574 return EXIT_FAILURE;
575 }
576 add_name.assign(*i);
577 i = args.erase(i);
578 } else if (ceph_argparse_witharg(args, i, &add_item, err, "--update_item", (char*)NULL)) {
579 update_item = true;
580 if (!err.str().empty()) {
581 cerr << err.str() << std::endl;
582 return EXIT_FAILURE;
583 }
584 if (i == args.end()) {
585 cerr << "expecting additional argument to --update-item" << std::endl;
586 return EXIT_FAILURE;
587 }
588 add_weight = atof(*i);
589 i = args.erase(i);
590 if (i == args.end()) {
591 cerr << "expecting additional argument to --update-item" << std::endl;
592 return EXIT_FAILURE;
593 }
594 add_name.assign(*i);
595 i = args.erase(i);
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;
600 return EXIT_FAILURE;
601 }
602 add_bucket = true;
603 } else if (argparse_withargs(args, i, err, "--move",
604 &move_name)) {
605 if (!err.str().empty()) {
606 cerr << err.str() << std::endl;
607 return EXIT_FAILURE;
608 }
609 move_item = true;
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;
614 return EXIT_FAILURE;
615 }
616 if (i == args.end()) {
617 cerr << "expecting additional argument to --create-simple-rule" << std::endl;
618 return EXIT_FAILURE;
619 }
620
621 rule_root.assign(*i);
622 i = args.erase(i);
623 if (i == args.end()) {
624 cerr << "expecting additional argument to --create-simple-rule" << std::endl;
625 return EXIT_FAILURE;
626 }
627
628 rule_type.assign(*i);
629 i = args.erase(i);
630 if (i == args.end()) {
631 cerr << "expecting additional argument to --create-simple-rule" << std::endl;
632 return EXIT_FAILURE;
633 }
634
635 rule_mode.assign(*i);
636 i = args.erase(i);
637
638 cout << "--create-simple-rule:"
639 << " name=" << rule_name
640 << " root=" << rule_root
641 << " type=" << rule_type
642 << " mode=" << rule_mode
643 << std::endl;
644 add_rule = true;
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;
649 return EXIT_FAILURE;
650 }
651 if (i == args.end()) {
652 cerr << "expecting additional argument to --create-replicated-rule" << std::endl;
653 return EXIT_FAILURE;
654 }
655
656 rule_root.assign(*i);
657 i = args.erase(i);
658 if (i == args.end()) {
659 cerr << "expecting additional argument to --create-replicated-rule" << std::endl;
660 return EXIT_FAILURE;
661 }
662
663 rule_type.assign(*i);
664 i = args.erase(i);
665 rule_mode = "firstn";
666
667 cout << "--create-replicated-rule:"
668 << " name=" << rule_name
669 << " root=" << rule_root
670 << " type=" << rule_type
671 << std::endl;
672 add_rule = true;
673
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;
678 return EXIT_FAILURE;
679 }
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;
684 return EXIT_FAILURE;
685 }
686 del_rule = true;
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;
691 return EXIT_FAILURE;
692 }
693 std::string name(*i);
694 i = args.erase(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();
702 return EXIT_SUCCESS;
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;
707 return EXIT_FAILURE;
708 }
709 else {
710 tester.set_output_data_file_name(name + "-");
711 }
712 } else if (ceph_argparse_witharg(args, i, &val, "--remove_item", (char*)NULL)) {
713 remove_name = val;
714 } else if (ceph_argparse_witharg(args, i, &val, "--reweight_item", (char*)NULL)) {
715 reweight_name = val;
716 if (i == args.end()) {
717 cerr << "expecting additional argument to --reweight-item" << std::endl;
718 return EXIT_FAILURE;
719 }
720 reweight_weight = atof(*i);
721 i = args.erase(i);
722 } else if (ceph_argparse_flag(args, i, "--build", (char*)NULL)) {
723 build = true;
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;
727 return EXIT_FAILURE;
728 }
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;
732 return EXIT_FAILURE;
733 }
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;
738 return EXIT_FAILURE;
739 }
740 tester.set_max_x(x);
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;
744 return EXIT_FAILURE;
745 }
746 tester.set_min_x(x);
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;
750 return EXIT_FAILURE;
751 }
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;
756 return EXIT_FAILURE;
757 }
758 tester.set_x(x);
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;
762 return EXIT_FAILURE;
763 }
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;
768 return EXIT_FAILURE;
769 }
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;
774 return EXIT_FAILURE;
775 }
776 tester.set_rule(x);
777 } else if (ceph_argparse_witharg(args, i, &x, err, "--ruleset", (char*)NULL)) {
778 if (!err.str().empty()) {
779 cerr << err.str() << std::endl;
780 return EXIT_FAILURE;
781 }
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;
786 return EXIT_FAILURE;
787 }
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;
792 return EXIT_FAILURE;
793 }
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;
798 return EXIT_FAILURE;
799 }
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;
804 return EXIT_FAILURE;
805 }
806 int dev = tmp;
807 if (i == args.end()) {
808 cerr << "expecting additional argument to --weight" << std::endl;
809 return EXIT_FAILURE;
810 }
811 float f = atof(*i);
812 i = args.erase(i);
813 tester.set_device_weight(dev, f);
814 }
815 else {
816 ++i;
817 }
818 }
819
820 if (test && !check && !display && !write_to_file && compare.empty()) {
821 cerr << "WARNING: no output selected; use --output-csv or --show-X" << std::endl;
822 }
823
824 if (decompile + compile + build > 1) {
825 cerr << "cannot specify more than one of compile, decompile, and build" << std::endl;
826 return EXIT_FAILURE;
827 }
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 &&
831 compare.empty() &&
832 remove_name.empty() && reweight_name.empty()) {
833 cerr << "no action specified; -h for help" << std::endl;
834 return EXIT_FAILURE;
835 }
836 if ((!build) && (!args.empty())) {
837 cerr << "unrecognized arguments: " << args << std::endl;
838 return EXIT_FAILURE;
839 }
840 else {
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)"
844 << std::endl;
845 return EXIT_FAILURE;
846 }
847 for (size_t j = 0; j < args.size(); j += 3) {
848 layer_t l;
849 l.name = args[j];
850 l.buckettype = args[j+1];
851 l.size = atoi(args[j+2]);
852 layers.push_back(l);
853 }
854 }
855
856 /*
857 if (outfn) cout << "outfn " << outfn << std::endl;
858 if (cinfn) cout << "cinfn " << cinfn << std::endl;
859 if (dinfn) cout << "dinfn " << dinfn << std::endl;
860 */
861
862 bool modified = false;
863
864 // input ----
865
866 if (!infn.empty()) {
867 bufferlist bl;
868 std::string error;
869
870 int r = 0;
871 if (infn == "-") {
872 if (isatty(STDIN_FILENO)) {
873 cerr << "stdin must not be from a tty" << std::endl;
874 return EXIT_FAILURE;
875 }
876 r = get_fd_data(STDIN_FILENO, bl);
877 if (r < 0) {
878 cerr << "error reading data from STDIN" << std::endl;
879 return EXIT_FAILURE;
880 }
881 } else {
882 r = bl.read_file(infn.c_str(), &error);
883 if (r < 0) {
884 cerr << me << ": error reading '" << infn << "': "
885 << error << std::endl;
886 return EXIT_FAILURE;
887 }
888 }
889 auto p = bl.cbegin();
890 try {
891 crush.decode(p);
892 } catch(...) {
893 cerr << me << ": unable to decode " << infn << std::endl;
894 return EXIT_FAILURE;
895 }
896 }
897
898 if (compile) {
899 crush.create();
900
901 // read the file
902 ifstream in(srcfn.c_str());
903 if (!in.is_open()) {
904 cerr << "input file " << srcfn << " not found" << std::endl;
905 return -ENOENT;
906 }
907
908 CrushCompiler cc(crush, cerr, verbose);
909 if (unsafe_tunables)
910 cc.enable_unsafe_tunables();
911 int r = cc.compile(in, srcfn.c_str());
912 if (r < 0)
913 return EXIT_FAILURE;
914
915 modified = true;
916 }
917
918 if (build) {
919 if (layers.empty()) {
920 cerr << me << ": must specify at least one layer" << std::endl;
921 return EXIT_FAILURE;
922 }
923
924 crush.create();
925
926 vector<int> lower_items;
927 vector<int> lower_weights;
928
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));
934 }
935
936 crush.set_type_name(0, "osd");
937 int type = 1;
938 for (vector<layer_t>::iterator p = layers.begin(); p != layers.end(); ++p, type++) {
939 layer_t &l = *p;
940
941 dout(2) << "layer " << type
942 << " " << l.name
943 << " bucket type " << l.buckettype
944 << " " << l.size
945 << dendl;
946
947 crush.set_type_name(type, l.name);
948
949 int buckettype = -1;
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;
953 break;
954 }
955 if (buckettype < 0) {
956 cerr << "unknown bucket type '" << l.buckettype << "'" << std::endl;
957 return EXIT_FAILURE;
958 }
959
960 // build items
961 vector<int> cur_items;
962 vector<int> cur_weights;
963 unsigned lower_pos = 0; // lower pos
964
965 dout(2) << "lower_items " << lower_items << dendl;
966 dout(2) << "lower_weights " << lower_weights << dendl;
967
968 int i = 0;
969 while (1) {
970 if (lower_pos == lower_items.size())
971 break;
972
973 int items[num_osds];
974 int weights[num_osds];
975
976 int weight = 0;
977 int j;
978 for (j=0; j<l.size || l.size==0; j++) {
979 if (lower_pos == lower_items.size())
980 break;
981 items[j] = lower_items[lower_pos];
982 weights[j] = lower_weights[lower_pos];
983 weight += weights[j];
984 lower_pos++;
985 dout(2) << " item " << items[j] << " weight " << weights[j] << dendl;
986 }
987
988 int id;
989 int r = crush.add_bucket(0, buckettype, CRUSH_HASH_DEFAULT, type, j, items, weights, &id);
990 if (r < 0) {
991 cerr << " Couldn't add bucket: " << cpp_strerror(r) << std::endl;
992 return r;
993 }
994
995 char format[20];
996 format[sizeof(format)-1] = '\0';
997 if (l.size)
998 snprintf(format, sizeof(format)-1, "%s%%d", l.name);
999 else
1000 strncpy(format, l.name, sizeof(format)-1);
1001 char name[20];
1002 snprintf(name, sizeof(name), format, i);
1003 crush.set_item_name(id, name);
1004
1005 dout(2) << " in bucket " << id << " '" << name << "' size " << j << " weight " << weight << dendl;
1006
1007 cur_items.push_back(id);
1008 cur_weights.push_back(weight);
1009 i++;
1010 }
1011
1012 lower_items.swap(cur_items);
1013 lower_weights.swap(cur_weights);
1014 }
1015
1016 string root = layers.back().size == 0 ? layers.back().name :
1017 string(layers.back().name) + "0";
1018
1019 {
1020 set<int> roots;
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"
1028 << std::endl;
1029 }
1030 }
1031
1032 if (OSDMap::build_simple_crush_rules(g_ceph_context, crush, root, &cerr))
1033 return EXIT_FAILURE;
1034
1035 modified = true;
1036 }
1037
1038 // mutate ----
1039
1040 if (choose_local_tries >= 0) {
1041 crush.set_choose_local_tries(choose_local_tries);
1042 modified = true;
1043 }
1044 if (choose_local_fallback_tries >= 0) {
1045 crush.set_choose_local_fallback_tries(choose_local_fallback_tries);
1046 modified = true;
1047 }
1048 if (choose_total_tries >= 0) {
1049 crush.set_choose_total_tries(choose_total_tries);
1050 modified = true;
1051 }
1052 if (chooseleaf_descend_once >= 0) {
1053 crush.set_chooseleaf_descend_once(chooseleaf_descend_once);
1054 modified = true;
1055 }
1056 if (chooseleaf_vary_r >= 0) {
1057 crush.set_chooseleaf_vary_r(chooseleaf_vary_r);
1058 modified = true;
1059 }
1060 if (chooseleaf_stable >= 0) {
1061 crush.set_chooseleaf_stable(chooseleaf_stable);
1062 modified = true;
1063 }
1064 if (straw_calc_version >= 0) {
1065 crush.set_straw_calc_version(straw_calc_version);
1066 modified = true;
1067 }
1068 if (allowed_bucket_algs >= 0) {
1069 crush.set_allowed_bucket_algs(allowed_bucket_algs);
1070 modified = true;
1071 }
1072
1073 if (!reweight_name.empty()) {
1074 cout << me << " reweighting item " << reweight_name << " to " << reweight_weight << std::endl;
1075 int r;
1076 if (!crush.name_exists(reweight_name)) {
1077 cerr << " name " << reweight_name << " dne" << std::endl;
1078 r = -ENOENT;
1079 } else {
1080 int item = crush.get_item_id(reweight_name);
1081 r = crush.adjust_item_weightf(g_ceph_context, item, reweight_weight);
1082 }
1083 if (r >= 0)
1084 modified = true;
1085 else {
1086 cerr << me << " " << cpp_strerror(r) << std::endl;
1087 return r;
1088 }
1089 }
1090
1091 if (!remove_name.empty()) {
1092 cout << me << " removing item " << remove_name << std::endl;
1093 int r;
1094 if (!crush.name_exists(remove_name)) {
1095 cerr << " name " << remove_name << " dne" << std::endl;
1096 r = -ENOENT;
1097 } else {
1098 int remove_item = crush.get_item_id(remove_name);
1099 r = crush.remove_item(g_ceph_context, remove_item, false);
1100 }
1101 if (r == 0)
1102 modified = true;
1103 else {
1104 cerr << me << " " << cpp_strerror(r) << std::endl;
1105 return r;
1106 }
1107 }
1108
1109 if (add_item >= 0) {
1110 int r;
1111 if (update_item) {
1112 r = crush.update_item(g_ceph_context, add_item, add_weight, add_name.c_str(), add_loc);
1113 } else {
1114 r = crush.insert_item(g_ceph_context, add_item, add_weight, add_name.c_str(), add_loc);
1115 }
1116 if (r >= 0) {
1117 modified = true;
1118 } else {
1119 cerr << me << " " << cpp_strerror(r) << std::endl;
1120 return r;
1121 }
1122 }
1123
1124 if (add_bucket) {
1125 if (int r = do_add_bucket(cct.get(), me, crush, add_name, add_type, add_loc); !r) {
1126 modified = true;
1127 } else {
1128 return r;
1129 }
1130 }
1131
1132 if (move_item) {
1133 if (int r = do_move_item(cct.get(), me, crush, move_name, add_loc); !r) {
1134 modified = true;
1135 } else {
1136 return r;
1137 }
1138 }
1139 if (add_rule) {
1140 if (crush.rule_exists(rule_name)) {
1141 cerr << "rule " << rule_name << " already exists" << std::endl;
1142 return EXIT_FAILURE;
1143 }
1144 int r = crush.add_simple_rule(rule_name, rule_root, rule_type,
1145 rule_device_class,
1146 rule_mode, pg_pool_t::TYPE_REPLICATED, &err);
1147 if (r < 0) {
1148 cerr << err.str() << std::endl;
1149 return EXIT_FAILURE;
1150 }
1151 modified = true;
1152 }
1153
1154 if (del_rule) {
1155 if (!crush.rule_exists(rule_name)) {
1156 cerr << "rule " << rule_name << " does not exist" << std::endl;
1157 return 0;
1158 }
1159 int ruleno = crush.get_rule_id(rule_name);
1160 ceph_assert(ruleno >= 0);
1161 int r = crush.remove_rule(ruleno);
1162 if (r < 0) {
1163 cerr << "fail to remove rule " << rule_name << std::endl;
1164 return EXIT_FAILURE;
1165 }
1166 modified = true;
1167 }
1168
1169 if (reweight) {
1170 crush.reweight(g_ceph_context);
1171 modified = true;
1172 }
1173 if (rebuild_class_roots) {
1174 int r = crush.rebuild_roots_with_classes(g_ceph_context);
1175 if (r < 0) {
1176 cerr << "failed to rebuidl roots with classes" << std::endl;
1177 return EXIT_FAILURE;
1178 }
1179 modified = true;
1180 }
1181
1182 for (auto& i : set_subtree_class) {
1183 crush.set_subtree_class(i.first, i.second);
1184 modified = true;
1185 }
1186 if (reclassify) {
1187 int r = crush.reclassify(
1188 g_ceph_context,
1189 cout,
1190 reclassify_root,
1191 reclassify_bucket);
1192 if (r < 0) {
1193 cerr << "failed to reclassify map" << std::endl;
1194 return EXIT_FAILURE;
1195 }
1196 modified = true;
1197 }
1198
1199 // display ---
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();
1203 p != loc.end();
1204 ++p) {
1205 cout << p->first << "\t" << p->second << std::endl;
1206 }
1207 }
1208
1209 if (tree) {
1210 crush.dump_tree(&cout, NULL, {}, true);
1211 }
1212
1213 if (dump) {
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());
1217 f->close_section();
1218 f->flush(cout);
1219 cout << "\n";
1220 }
1221
1222 if (decompile) {
1223 CrushCompiler cc(crush, cerr, verbose);
1224 if (!outfn.empty()) {
1225 ofstream o;
1226 o.open(outfn.c_str(), ios::out | ios::binary | ios::trunc);
1227 if (!o.is_open()) {
1228 cerr << me << ": error writing '" << outfn << "'" << std::endl;
1229 return EXIT_FAILURE;
1230 }
1231 cc.decompile(o);
1232 o.close();
1233 } else {
1234 cc.decompile(cout);
1235 }
1236 }
1237
1238 if (check) {
1239 tester.check_overlapped_rules();
1240 if (max_id >= 0) {
1241 if (!tester.check_name_maps(max_id)) {
1242 return EXIT_FAILURE;
1243 }
1244 }
1245 }
1246
1247 if (test) {
1248 if (tester.get_output_utilization_all() ||
1249 tester.get_output_utilization())
1250 tester.set_output_statistics(true);
1251
1252 int r = tester.test();
1253 if (r < 0)
1254 return EXIT_FAILURE;
1255 }
1256
1257 if (compare.size()) {
1258 CrushWrapper crush2;
1259 bufferlist in;
1260 string error;
1261 int r = in.read_file(compare.c_str(), &error);
1262 if (r < 0) {
1263 cerr << me << ": error reading '" << compare << "': "
1264 << error << std::endl;
1265 return EXIT_FAILURE;
1266 }
1267 auto p = in.cbegin();
1268 try {
1269 crush2.decode(p);
1270 } catch(...) {
1271 cerr << me << ": unable to decode " << compare << std::endl;
1272 return EXIT_FAILURE;
1273 }
1274 r = tester.compare(crush2);
1275 if (r < 0)
1276 return EXIT_FAILURE;
1277 }
1278
1279 // output ---
1280 if (modified) {
1281 crush.finalize();
1282
1283 if (outfn.empty()) {
1284 cout << me << " successfully built or modified map. Use '-o <file>' to write it out." << std::endl;
1285 } else {
1286 bufferlist bl;
1287 crush.encode(bl, CEPH_FEATURES_SUPPORTED_DEFAULT);
1288 int r = bl.write_file(outfn.c_str());
1289 if (r < 0) {
1290 cerr << me << ": error writing '" << outfn << "': " << cpp_strerror(r) << std::endl;
1291 return EXIT_FAILURE;
1292 }
1293 if (verbose)
1294 cout << "wrote crush map to " << outfn << std::endl;
1295 }
1296 }
1297
1298 return 0;
1299 }
1300 /*
1301 * Local Variables:
1302 * compile-command: "cd .. ; make crushtool && test/run-cli-tests"
1303 * End:
1304 */