]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/crushtool.cc
b73a1d8c2adc7ece38f31e60b9eb5c5f8be6cff1
[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
25 #include "common/debug.h"
26 #include "common/errno.h"
27 #include "common/config.h"
28 #include "common/Formatter.h"
29
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"
39
40 #define dout_context g_ceph_context
41 #define dout_subsys ceph_subsys_crush
42
43 using namespace std;
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 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 --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";
175 cout << "\n";
176 cout << "Options for the display/test stage\n";
177 cout << "\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";
211 cout << "\n";
212 cout << "Options for the output stage\n";
213 cout << "\n";
214 cout << " [--outfn|-o outfile]\n";
215 cout << " specify output for modified crush map\n";
216 cout << "\n";
217 }
218
219 struct bucket_types_t {
220 const char *name;
221 int type;
222 } bucket_types[] = {
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 },
228 { 0, 0 },
229 };
230
231 struct layer_t {
232 const char *name;
233 const char *buckettype;
234 int size;
235 };
236
237 int main(int argc, const char **argv)
238 {
239 vector<const char*> args;
240 argv_to_vec(argc, argv, args);
241
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;
246 bool check = false;
247 int max_id = -1;
248 bool test = false;
249 bool display = false;
250 bool tree = false;
251 string dump_format = "json-pretty";
252 bool dump = false;
253 int full_location = -1;
254 bool write_to_file = false;
255 int verbose = 0;
256 bool unsafe_tunables = false;
257
258 bool reweight = false;
259 int add_item = -1;
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;
267
268 bool adjust = false;
269
270 int build = 0;
271 int num_osds =0;
272 vector<layer_t> layers;
273
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;
282
283 CrushWrapper crush;
284
285 CrushTester tester(crush, cout);
286
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.
296 cct->get();
297 common_init_finish(g_ceph_context);
298
299 int x;
300 float y;
301 long long z;
302
303 std::string val;
304 std::ostringstream err;
305 int tmp;
306 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
307 if (ceph_argparse_double_dash(args, i)) {
308 break;
309 } else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
310 usage();
311 return EXIT_SUCCESS;
312 } else if (ceph_argparse_witharg(args, i, &val, "-d", "--decompile", (char*)NULL)) {
313 infn = val;
314 decompile = true;
315 } else if (ceph_argparse_witharg(args, i, &val, "-i", "--infn", (char*)NULL)) {
316 infn = val;
317 } else if (ceph_argparse_witharg(args, i, &val, "-o", "--outfn", (char*)NULL)) {
318 outfn = val;
319 } else if (ceph_argparse_flag(args, i, "-v", "--verbose", (char*)NULL)) {
320 verbose += 1;
321 } else if (ceph_argparse_flag(args, i, "--tree", (char*)NULL)) {
322 tree = true;
323 } else if (ceph_argparse_witharg(args, i, &val, "-f", "--format", (char*)NULL)) {
324 dump_format = val;
325 } else if (ceph_argparse_flag(args, i, "--dump", (char*)NULL)) {
326 dump = true;
327 } else if (ceph_argparse_flag(args, i, "--show_utilization", (char*)NULL)) {
328 display = true;
329 tester.set_output_utilization(true);
330 } else if (ceph_argparse_flag(args, i, "--show_utilization_all", (char*)NULL)) {
331 display = true;
332 tester.set_output_utilization_all(true);
333 } else if (ceph_argparse_flag(args, i, "--show_statistics", (char*)NULL)) {
334 display = true;
335 tester.set_output_statistics(true);
336 } else if (ceph_argparse_flag(args, i, "--show_mappings", (char*)NULL)) {
337 display = true;
338 tester.set_output_mappings(true);
339 } else if (ceph_argparse_flag(args, i, "--show_bad_mappings", (char*)NULL)) {
340 display = true;
341 tester.set_output_bad_mappings(true);
342 } else if (ceph_argparse_flag(args, i, "--show_choose_tries", (char*)NULL)) {
343 display = true;
344 tester.set_output_choose_tries(true);
345 } else if (ceph_argparse_witharg(args, i, &val, "-c", "--compile", (char*)NULL)) {
346 srcfn = val;
347 compile = true;
348 } else if (ceph_argparse_witharg(args, i, &max_id, err, "--check", (char*)NULL)) {
349 check = true;
350 } else if (ceph_argparse_flag(args, i, "-t", "--test", (char*)NULL)) {
351 test = true;
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)) {
359 adjust = true;
360 } else if (ceph_argparse_witharg(args, i, &choose_local_fallback_tries, err,
361 "--set_choose_local_fallback_tries", (char*)NULL)) {
362 adjust = true;
363 } else if (ceph_argparse_witharg(args, i, &choose_total_tries, err,
364 "--set_choose_total_tries", (char*)NULL)) {
365 adjust = true;
366 } else if (ceph_argparse_witharg(args, i, &chooseleaf_descend_once, err,
367 "--set_chooseleaf_descend_once", (char*)NULL)) {
368 adjust = true;
369 } else if (ceph_argparse_witharg(args, i, &chooseleaf_vary_r, err,
370 "--set_chooseleaf_vary_r", (char*)NULL)) {
371 adjust = true;
372 } else if (ceph_argparse_witharg(args, i, &chooseleaf_stable, err,
373 "--set_chooseleaf_stable", (char*)NULL)) {
374 adjust = true;
375 } else if (ceph_argparse_witharg(args, i, &straw_calc_version, err,
376 "--set_straw_calc_version", (char*)NULL)) {
377 adjust = true;
378 } else if (ceph_argparse_witharg(args, i, &allowed_bucket_algs, err,
379 "--set_allowed_bucket_algs", (char*)NULL)) {
380 adjust = true;
381 } else if (ceph_argparse_flag(args, i, "--reweight", (char*)NULL)) {
382 reweight = true;
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;
386 return EXIT_FAILURE;
387 }
388 if (i == args.end()) {
389 cerr << "expecting additional argument to --add-item" << std::endl;
390 return EXIT_FAILURE;
391 }
392 add_weight = atof(*i);
393 i = args.erase(i);
394 if (i == args.end()) {
395 cerr << "expecting additional argument to --add-item" << std::endl;
396 return EXIT_FAILURE;
397 }
398 add_name.assign(*i);
399 i = args.erase(i);
400 } else if (ceph_argparse_witharg(args, i, &add_item, err, "--update_item", (char*)NULL)) {
401 update_item = true;
402 if (!err.str().empty()) {
403 cerr << err.str() << std::endl;
404 return EXIT_FAILURE;
405 }
406 if (i == args.end()) {
407 cerr << "expecting additional argument to --update-item" << std::endl;
408 return EXIT_FAILURE;
409 }
410 add_weight = atof(*i);
411 i = args.erase(i);
412 if (i == args.end()) {
413 cerr << "expecting additional argument to --update-item" << std::endl;
414 return EXIT_FAILURE;
415 }
416 add_name.assign(*i);
417 i = args.erase(i);
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;
422 return EXIT_FAILURE;
423 }
424 if (i == args.end()) {
425 cerr << "expecting additional argument to --create-simple-rule" << std::endl;
426 return EXIT_FAILURE;
427 }
428
429 rule_root.assign(*i);
430 i = args.erase(i);
431 if (i == args.end()) {
432 cerr << "expecting additional argument to --create-simple-rule" << std::endl;
433 return EXIT_FAILURE;
434 }
435
436 rule_type.assign(*i);
437 i = args.erase(i);
438 if (i == args.end()) {
439 cerr << "expecting additional argument to --create-simple-rule" << std::endl;
440 return EXIT_FAILURE;
441 }
442
443 rule_mode.assign(*i);
444 i = args.erase(i);
445
446 cout << "--create-simple-rule:"
447 << " name=" << rule_name
448 << " root=" << rule_root
449 << " type=" << rule_type
450 << " mode=" << rule_mode
451 << std::endl;
452 add_rule = true;
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;
457 return EXIT_FAILURE;
458 }
459 del_rule = true;
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;
464 return EXIT_FAILURE;
465 }
466 std::string name(*i);
467 i = args.erase(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();
475 return EXIT_SUCCESS;
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;
480 return EXIT_FAILURE;
481 }
482 else {
483 tester.set_output_data_file_name(name + "-");
484 }
485 } else if (ceph_argparse_witharg(args, i, &val, "--remove_item", (char*)NULL)) {
486 remove_name = val;
487 } else if (ceph_argparse_witharg(args, i, &val, "--reweight_item", (char*)NULL)) {
488 reweight_name = val;
489 if (i == args.end()) {
490 cerr << "expecting additional argument to --reweight-item" << std::endl;
491 return EXIT_FAILURE;
492 }
493 reweight_weight = atof(*i);
494 i = args.erase(i);
495 } else if (ceph_argparse_flag(args, i, "--build", (char*)NULL)) {
496 build = true;
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;
500 return EXIT_FAILURE;
501 }
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;
505 return EXIT_FAILURE;
506 }
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;
511 return EXIT_FAILURE;
512 }
513 tester.set_max_x(x);
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;
517 return EXIT_FAILURE;
518 }
519 tester.set_min_x(x);
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;
523 return EXIT_FAILURE;
524 }
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;
529 return EXIT_FAILURE;
530 }
531 tester.set_x(x);
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;
535 return EXIT_FAILURE;
536 }
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;
541 return EXIT_FAILURE;
542 }
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;
547 return EXIT_FAILURE;
548 }
549 tester.set_rule(x);
550 } else if (ceph_argparse_witharg(args, i, &x, err, "--ruleset", (char*)NULL)) {
551 if (!err.str().empty()) {
552 cerr << err.str() << std::endl;
553 return EXIT_FAILURE;
554 }
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;
559 return EXIT_FAILURE;
560 }
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;
565 return EXIT_FAILURE;
566 }
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;
571 return EXIT_FAILURE;
572 }
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;
577 return EXIT_FAILURE;
578 }
579 int dev = tmp;
580 if (i == args.end()) {
581 cerr << "expecting additional argument to --weight" << std::endl;
582 return EXIT_FAILURE;
583 }
584 float f = atof(*i);
585 i = args.erase(i);
586 tester.set_device_weight(dev, f);
587 }
588 else {
589 ++i;
590 }
591 }
592
593 if (test && !check && !display && !write_to_file) {
594 cerr << "WARNING: no output selected; use --output-csv or --show-X" << std::endl;
595 }
596
597 if (decompile + compile + build > 1) {
598 cerr << "cannot specify more than one of compile, decompile, and build" << std::endl;
599 return EXIT_FAILURE;
600 }
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;
605 return EXIT_FAILURE;
606 }
607 if ((!build) && (!args.empty())) {
608 cerr << "unrecognized arguments: " << args << std::endl;
609 return EXIT_FAILURE;
610 }
611 else {
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)"
615 << std::endl;
616 return EXIT_FAILURE;
617 }
618 for (size_t j = 0; j < args.size(); j += 3) {
619 layer_t l;
620 l.name = args[j];
621 l.buckettype = args[j+1];
622 l.size = atoi(args[j+2]);
623 layers.push_back(l);
624 }
625 }
626
627 /*
628 if (outfn) cout << "outfn " << outfn << std::endl;
629 if (cinfn) cout << "cinfn " << cinfn << std::endl;
630 if (dinfn) cout << "dinfn " << dinfn << std::endl;
631 */
632
633 bool modified = false;
634
635 // input ----
636
637 if (!infn.empty()) {
638 bufferlist bl;
639 std::string error;
640
641 int r = 0;
642 if (infn == "-") {
643 if (isatty(STDIN_FILENO)) {
644 cerr << "stdin must not be from a tty" << std::endl;
645 return EXIT_FAILURE;
646 }
647 r = get_fd_data(STDIN_FILENO, bl);
648 if (r < 0) {
649 cerr << "error reading data from STDIN" << std::endl;
650 return EXIT_FAILURE;
651 }
652 } else {
653 r = bl.read_file(infn.c_str(), &error);
654 if (r < 0) {
655 cerr << me << ": error reading '" << infn << "': "
656 << error << std::endl;
657 return EXIT_FAILURE;
658 }
659 }
660 bufferlist::iterator p = bl.begin();
661 try {
662 crush.decode(p);
663 } catch(...) {
664 cerr << me << ": unable to decode " << infn << std::endl;
665 return EXIT_FAILURE;
666 }
667 }
668
669 if (compile) {
670 crush.create();
671
672 // read the file
673 ifstream in(srcfn.c_str());
674 if (!in.is_open()) {
675 cerr << "input file " << srcfn << " not found" << std::endl;
676 return -ENOENT;
677 }
678
679 CrushCompiler cc(crush, cerr, verbose);
680 if (unsafe_tunables)
681 cc.enable_unsafe_tunables();
682 int r = cc.compile(in, srcfn.c_str());
683 if (r < 0)
684 return EXIT_FAILURE;
685
686 modified = true;
687 }
688
689 if (build) {
690 if (layers.empty()) {
691 cerr << me << ": must specify at least one layer" << std::endl;
692 return EXIT_FAILURE;
693 }
694
695 crush.create();
696
697 vector<int> lower_items;
698 vector<int> lower_weights;
699
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));
705 }
706
707 crush.set_type_name(0, "osd");
708 int type = 1;
709 for (vector<layer_t>::iterator p = layers.begin(); p != layers.end(); ++p, type++) {
710 layer_t &l = *p;
711
712 dout(2) << "layer " << type
713 << " " << l.name
714 << " bucket type " << l.buckettype
715 << " " << l.size
716 << dendl;
717
718 crush.set_type_name(type, l.name);
719
720 int buckettype = -1;
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;
724 break;
725 }
726 if (buckettype < 0) {
727 cerr << "unknown bucket type '" << l.buckettype << "'" << std::endl;
728 return EXIT_FAILURE;
729 }
730
731 // build items
732 vector<int> cur_items;
733 vector<int> cur_weights;
734 unsigned lower_pos = 0; // lower pos
735
736 dout(2) << "lower_items " << lower_items << dendl;
737 dout(2) << "lower_weights " << lower_weights << dendl;
738
739 int i = 0;
740 while (1) {
741 if (lower_pos == lower_items.size())
742 break;
743
744 int items[num_osds];
745 int weights[num_osds];
746
747 int weight = 0;
748 int j;
749 for (j=0; j<l.size || l.size==0; j++) {
750 if (lower_pos == lower_items.size())
751 break;
752 items[j] = lower_items[lower_pos];
753 weights[j] = lower_weights[lower_pos];
754 weight += weights[j];
755 lower_pos++;
756 dout(2) << " item " << items[j] << " weight " << weights[j] << dendl;
757 }
758
759 int id;
760 int r = crush.add_bucket(0, buckettype, CRUSH_HASH_DEFAULT, type, j, items, weights, &id);
761 if (r < 0) {
762 cerr << " Couldn't add bucket: " << cpp_strerror(r) << std::endl;
763 return r;
764 }
765
766 char format[20];
767 format[sizeof(format)-1] = '\0';
768 if (l.size)
769 snprintf(format, sizeof(format)-1, "%s%%d", l.name);
770 else
771 strncpy(format, l.name, sizeof(format)-1);
772 char name[20];
773 snprintf(name, sizeof(name), format, i);
774 crush.set_item_name(id, name);
775
776 dout(2) << " in bucket " << id << " '" << name << "' size " << j << " weight " << weight << dendl;
777
778 cur_items.push_back(id);
779 cur_weights.push_back(weight);
780 i++;
781 }
782
783 lower_items.swap(cur_items);
784 lower_weights.swap(cur_weights);
785 }
786
787 string root = layers.back().size == 0 ? layers.back().name :
788 string(layers.back().name) + "0";
789
790 {
791 set<int> roots;
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"
798 << " root straw 0\n"
799 << dendl;
800 }
801
802 if (OSDMap::build_simple_crush_rules(g_ceph_context, crush, root, &cerr))
803 return EXIT_FAILURE;
804
805 modified = true;
806 }
807
808 // mutate ----
809
810 if (choose_local_tries >= 0) {
811 crush.set_choose_local_tries(choose_local_tries);
812 modified = true;
813 }
814 if (choose_local_fallback_tries >= 0) {
815 crush.set_choose_local_fallback_tries(choose_local_fallback_tries);
816 modified = true;
817 }
818 if (choose_total_tries >= 0) {
819 crush.set_choose_total_tries(choose_total_tries);
820 modified = true;
821 }
822 if (chooseleaf_descend_once >= 0) {
823 crush.set_chooseleaf_descend_once(chooseleaf_descend_once);
824 modified = true;
825 }
826 if (chooseleaf_vary_r >= 0) {
827 crush.set_chooseleaf_vary_r(chooseleaf_vary_r);
828 modified = true;
829 }
830 if (chooseleaf_stable >= 0) {
831 crush.set_chooseleaf_stable(chooseleaf_stable);
832 modified = true;
833 }
834 if (straw_calc_version >= 0) {
835 crush.set_straw_calc_version(straw_calc_version);
836 modified = true;
837 }
838 if (allowed_bucket_algs >= 0) {
839 crush.set_allowed_bucket_algs(allowed_bucket_algs);
840 modified = true;
841 }
842
843 if (!reweight_name.empty()) {
844 cout << me << " reweighting item " << reweight_name << " to " << reweight_weight << std::endl;
845 int r;
846 if (!crush.name_exists(reweight_name)) {
847 cerr << " name " << reweight_name << " dne" << std::endl;
848 r = -ENOENT;
849 } else {
850 int item = crush.get_item_id(reweight_name);
851 r = crush.adjust_item_weightf(g_ceph_context, item, reweight_weight);
852 }
853 if (r >= 0)
854 modified = true;
855 else {
856 cerr << me << " " << cpp_strerror(r) << std::endl;
857 return r;
858 }
859 }
860
861 if (!remove_name.empty()) {
862 cout << me << " removing item " << remove_name << std::endl;
863 int r;
864 if (!crush.name_exists(remove_name)) {
865 cerr << " name " << remove_name << " dne" << std::endl;
866 r = -ENOENT;
867 } else {
868 int remove_item = crush.get_item_id(remove_name);
869 r = crush.remove_item(g_ceph_context, remove_item, false);
870 }
871 if (r == 0)
872 modified = true;
873 else {
874 cerr << me << " " << cpp_strerror(r) << std::endl;
875 return r;
876 }
877 }
878
879 if (add_item >= 0) {
880 int r;
881 if (update_item) {
882 r = crush.update_item(g_ceph_context, add_item, add_weight, add_name.c_str(), add_loc);
883 } else {
884 r = crush.insert_item(g_ceph_context, add_item, add_weight, add_name.c_str(), add_loc);
885 }
886 if (r >= 0) {
887 modified = true;
888 } else {
889 cerr << me << " " << cpp_strerror(r) << std::endl;
890 return r;
891 }
892 }
893
894 if (add_rule) {
895 if (crush.rule_exists(rule_name)) {
896 cerr << "rule " << rule_name << " already exists" << std::endl;
897 return EXIT_FAILURE;
898 }
899 int r = crush.add_simple_rule(rule_name, rule_root, rule_type, rule_mode,
900 pg_pool_t::TYPE_REPLICATED, &err);
901 if (r < 0) {
902 cerr << err.str() << std::endl;
903 return EXIT_FAILURE;
904 }
905 modified = true;
906 }
907
908 if (del_rule) {
909 if (!crush.rule_exists(rule_name)) {
910 cerr << "rule " << rule_name << " does not exist" << std::endl;
911 return 0;
912 }
913 int ruleno = crush.get_rule_id(rule_name);
914 assert(ruleno >= 0);
915 int r = crush.remove_rule(ruleno);
916 if (r < 0) {
917 cerr << "fail to remove rule " << rule_name << std::endl;
918 return EXIT_FAILURE;
919 }
920 modified = true;
921 }
922
923 if (reweight) {
924 crush.reweight(g_ceph_context);
925 modified = true;
926 }
927
928
929 // display ---
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();
933 p != loc.end();
934 ++p) {
935 cout << p->first << "\t" << p->second << std::endl;
936 }
937 }
938
939 if (tree) {
940 crush.dump_tree(&cout, NULL);
941 }
942
943 if (dump) {
944 boost::scoped_ptr<Formatter> f(Formatter::create(dump_format, "json-pretty", "json-pretty"));
945 f->open_object_section("crush_map");
946 crush.dump(f.get());
947 f->close_section();
948 f->flush(cout);
949 cout << "\n";
950 }
951
952 if (decompile) {
953 CrushCompiler cc(crush, cerr, verbose);
954 if (!outfn.empty()) {
955 ofstream o;
956 o.open(outfn.c_str(), ios::out | ios::binary | ios::trunc);
957 if (!o.is_open()) {
958 cerr << me << ": error writing '" << outfn << "'" << std::endl;
959 return EXIT_FAILURE;
960 }
961 cc.decompile(o);
962 o.close();
963 } else {
964 cc.decompile(cout);
965 }
966 }
967
968 if (check) {
969 tester.check_overlapped_rules();
970 if (max_id >= 0) {
971 if (!tester.check_name_maps(max_id)) {
972 return EXIT_FAILURE;
973 }
974 }
975 }
976
977 if (test) {
978 if (tester.get_output_utilization_all() ||
979 tester.get_output_utilization())
980 tester.set_output_statistics(true);
981
982 int r = tester.test();
983 if (r < 0)
984 return EXIT_FAILURE;
985 }
986
987 // output ---
988 if (modified) {
989 crush.finalize();
990
991 if (outfn.empty()) {
992 cout << me << " successfully built or modified map. Use '-o <file>' to write it out." << std::endl;
993 } else {
994 bufferlist bl;
995 crush.encode(bl, CEPH_FEATURES_SUPPORTED_DEFAULT);
996 int r = bl.write_file(outfn.c_str());
997 if (r < 0) {
998 cerr << me << ": error writing '" << outfn << "': " << cpp_strerror(r) << std::endl;
999 return EXIT_FAILURE;
1000 }
1001 if (verbose)
1002 cout << "wrote crush map to " << outfn << std::endl;
1003 }
1004 }
1005
1006 return 0;
1007 }
1008 /*
1009 * Local Variables:
1010 * compile-command: "cd .. ; make crushtool && test/run-cli-tests"
1011 * End:
1012 */