]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/crushtool.cc
add subtree-ish sources for 12.0.3
[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 << "\n";
170 cout << "Options for the display/test stage\n";
171 cout << "\n";
172 cout << " -f --format the format of --dump, defaults to json-pretty\n";
173 cout << " can be one of json, json-pretty, xml, xml-pretty,\n";
174 cout << " table, table-kv, html, html-pretty\n";
175 cout << " --dump dump the crush map\n";
176 cout << " --tree print map summary as a tree\n";
177 cout << " --check [max_id] check if any item is referencing an unknown name/type\n";
178 cout << " -i mapfn --show-location id\n";
179 cout << " show location for given device id\n";
180 cout << " -i mapfn --test test a range of inputs on the map\n";
181 cout << " [--min-x x] [--max-x x] [--x x]\n";
182 cout << " [--min-rule r] [--max-rule r] [--rule r] [--ruleset rs]\n";
183 cout << " [--num-rep n]\n";
184 cout << " [--pool-id n] specifies pool id\n";
185 cout << " [--batches b] split the CRUSH mapping into b > 1 rounds\n";
186 cout << " [--weight|-w devno weight]\n";
187 cout << " where weight is 0 to 1.0\n";
188 cout << " [--simulate] simulate placements using a random\n";
189 cout << " number generator in place of the CRUSH\n";
190 cout << " algorithm\n";
191 cout << " --show-utilization show OSD usage\n";
192 cout << " --show-utilization-all\n";
193 cout << " include zero weight items\n";
194 cout << " --show-statistics show chi squared statistics\n";
195 cout << " --show-mappings show mappings\n";
196 cout << " --show-bad-mappings show bad mappings\n";
197 cout << " --show-choose-tries show choose tries histogram\n";
198 cout << " --output-name name\n";
199 cout << " prepend the data file(s) generated during the\n";
200 cout << " testing routine with name\n";
201 cout << " --output-csv\n";
202 cout << " export select data generated during testing routine\n";
203 cout << " to CSV files for off-line post-processing\n";
204 cout << " use --help-output for more information\n";
205 cout << "\n";
206 cout << "Options for the output stage\n";
207 cout << "\n";
208 cout << " [--outfn|-o outfile]\n";
209 cout << " specify output for modified crush map\n";
210 cout << "\n";
211 }
212
213 struct bucket_types_t {
214 const char *name;
215 int type;
216 } bucket_types[] = {
217 { "uniform", CRUSH_BUCKET_UNIFORM },
218 { "list", CRUSH_BUCKET_LIST },
219 { "straw", CRUSH_BUCKET_STRAW },
220 { "straw2", CRUSH_BUCKET_STRAW2 },
221 { "tree", CRUSH_BUCKET_TREE },
222 { 0, 0 },
223 };
224
225 struct layer_t {
226 const char *name;
227 const char *buckettype;
228 int size;
229 };
230
231 int main(int argc, const char **argv)
232 {
233 vector<const char*> args;
234 argv_to_vec(argc, argv, args);
235
236 const char *me = argv[0];
237 std::string infn, srcfn, outfn, add_name, remove_name, reweight_name;
238 bool compile = false;
239 bool decompile = false;
240 bool check = false;
241 int max_id = -1;
242 bool test = false;
243 bool display = false;
244 bool tree = false;
245 string dump_format = "json-pretty";
246 bool dump = false;
247 int full_location = -1;
248 bool write_to_file = false;
249 int verbose = 0;
250 bool unsafe_tunables = false;
251
252 bool reweight = false;
253 int add_item = -1;
254 bool update_item = false;
255 float add_weight = 0;
256 map<string,string> add_loc;
257 float reweight_weight = 0;
258
259 bool adjust = false;
260
261 int build = 0;
262 int num_osds =0;
263 vector<layer_t> layers;
264
265 int choose_local_tries = -1;
266 int choose_local_fallback_tries = -1;
267 int choose_total_tries = -1;
268 int chooseleaf_descend_once = -1;
269 int chooseleaf_vary_r = -1;
270 int chooseleaf_stable = -1;
271 int straw_calc_version = -1;
272 int allowed_bucket_algs = -1;
273
274 CrushWrapper crush;
275
276 CrushTester tester(crush, cout);
277
278 // we use -c, don't confuse the generic arg parsing
279 // only parse arguments from CEPH_ARGS, if in the environment
280 vector<const char *> env_args;
281 env_to_vec(env_args);
282 auto cct = global_init(NULL, env_args, CEPH_ENTITY_TYPE_CLIENT,
283 CODE_ENVIRONMENT_UTILITY,
284 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
285 // crushtool times out occasionally when quits. so do not
286 // release the g_ceph_context.
287 cct->get();
288 common_init_finish(g_ceph_context);
289
290 int x;
291 float y;
292 long long z;
293
294 std::string val;
295 std::ostringstream err;
296 int tmp;
297 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
298 if (ceph_argparse_double_dash(args, i)) {
299 break;
300 } else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
301 usage();
302 return EXIT_SUCCESS;
303 } else if (ceph_argparse_witharg(args, i, &val, "-d", "--decompile", (char*)NULL)) {
304 infn = val;
305 decompile = true;
306 } else if (ceph_argparse_witharg(args, i, &val, "-i", "--infn", (char*)NULL)) {
307 infn = val;
308 } else if (ceph_argparse_witharg(args, i, &val, "-o", "--outfn", (char*)NULL)) {
309 outfn = val;
310 } else if (ceph_argparse_flag(args, i, "-v", "--verbose", (char*)NULL)) {
311 verbose += 1;
312 } else if (ceph_argparse_flag(args, i, "--tree", (char*)NULL)) {
313 tree = true;
314 } else if (ceph_argparse_witharg(args, i, &val, "-f", "--format", (char*)NULL)) {
315 dump_format = val;
316 } else if (ceph_argparse_flag(args, i, "--dump", (char*)NULL)) {
317 dump = true;
318 } else if (ceph_argparse_flag(args, i, "--show_utilization", (char*)NULL)) {
319 display = true;
320 tester.set_output_utilization(true);
321 } else if (ceph_argparse_flag(args, i, "--show_utilization_all", (char*)NULL)) {
322 display = true;
323 tester.set_output_utilization_all(true);
324 } else if (ceph_argparse_flag(args, i, "--show_statistics", (char*)NULL)) {
325 display = true;
326 tester.set_output_statistics(true);
327 } else if (ceph_argparse_flag(args, i, "--show_mappings", (char*)NULL)) {
328 display = true;
329 tester.set_output_mappings(true);
330 } else if (ceph_argparse_flag(args, i, "--show_bad_mappings", (char*)NULL)) {
331 display = true;
332 tester.set_output_bad_mappings(true);
333 } else if (ceph_argparse_flag(args, i, "--show_choose_tries", (char*)NULL)) {
334 display = true;
335 tester.set_output_choose_tries(true);
336 } else if (ceph_argparse_witharg(args, i, &val, "-c", "--compile", (char*)NULL)) {
337 srcfn = val;
338 compile = true;
339 } else if (ceph_argparse_witharg(args, i, &max_id, err, "--check", (char*)NULL)) {
340 check = true;
341 } else if (ceph_argparse_flag(args, i, "-t", "--test", (char*)NULL)) {
342 test = true;
343 } else if (ceph_argparse_witharg(args, i, &full_location, err, "--show-location", (char*)NULL)) {
344 } else if (ceph_argparse_flag(args, i, "-s", "--simulate", (char*)NULL)) {
345 tester.set_random_placement();
346 } else if (ceph_argparse_flag(args, i, "--enable-unsafe-tunables", (char*)NULL)) {
347 unsafe_tunables = true;
348 } else if (ceph_argparse_witharg(args, i, &choose_local_tries, err,
349 "--set_choose_local_tries", (char*)NULL)) {
350 adjust = true;
351 } else if (ceph_argparse_witharg(args, i, &choose_local_fallback_tries, err,
352 "--set_choose_local_fallback_tries", (char*)NULL)) {
353 adjust = true;
354 } else if (ceph_argparse_witharg(args, i, &choose_total_tries, err,
355 "--set_choose_total_tries", (char*)NULL)) {
356 adjust = true;
357 } else if (ceph_argparse_witharg(args, i, &chooseleaf_descend_once, err,
358 "--set_chooseleaf_descend_once", (char*)NULL)) {
359 adjust = true;
360 } else if (ceph_argparse_witharg(args, i, &chooseleaf_vary_r, err,
361 "--set_chooseleaf_vary_r", (char*)NULL)) {
362 adjust = true;
363 } else if (ceph_argparse_witharg(args, i, &chooseleaf_stable, err,
364 "--set_chooseleaf_stable", (char*)NULL)) {
365 adjust = true;
366 } else if (ceph_argparse_witharg(args, i, &straw_calc_version, err,
367 "--set_straw_calc_version", (char*)NULL)) {
368 adjust = true;
369 } else if (ceph_argparse_witharg(args, i, &allowed_bucket_algs, err,
370 "--set_allowed_bucket_algs", (char*)NULL)) {
371 adjust = true;
372 } else if (ceph_argparse_flag(args, i, "--reweight", (char*)NULL)) {
373 reweight = true;
374 } else if (ceph_argparse_witharg(args, i, &add_item, err, "--add_item", (char*)NULL)) {
375 if (!err.str().empty()) {
376 cerr << err.str() << std::endl;
377 return EXIT_FAILURE;
378 }
379 if (i == args.end()) {
380 cerr << "expecting additional argument to --add-item" << std::endl;
381 return EXIT_FAILURE;
382 }
383 add_weight = atof(*i);
384 i = args.erase(i);
385 if (i == args.end()) {
386 cerr << "expecting additional argument to --add-item" << std::endl;
387 return EXIT_FAILURE;
388 }
389 add_name.assign(*i);
390 i = args.erase(i);
391 } else if (ceph_argparse_witharg(args, i, &add_item, err, "--update_item", (char*)NULL)) {
392 update_item = true;
393 if (!err.str().empty()) {
394 cerr << err.str() << std::endl;
395 return EXIT_FAILURE;
396 }
397 if (i == args.end()) {
398 cerr << "expecting additional argument to --update-item" << std::endl;
399 return EXIT_FAILURE;
400 }
401 add_weight = atof(*i);
402 i = args.erase(i);
403 if (i == args.end()) {
404 cerr << "expecting additional argument to --update-item" << std::endl;
405 return EXIT_FAILURE;
406 }
407 add_name.assign(*i);
408 i = args.erase(i);
409 } else if (ceph_argparse_witharg(args, i, &val, "--loc", (char*)NULL)) {
410 std::string type(val);
411 if (i == args.end()) {
412 cerr << "expecting additional argument to --loc" << std::endl;
413 return EXIT_FAILURE;
414 }
415 std::string name(*i);
416 i = args.erase(i);
417 add_loc[type] = name;
418 } else if (ceph_argparse_flag(args, i, "--output-csv", (char*)NULL)) {
419 write_to_file = true;
420 tester.set_output_data_file(true);
421 tester.set_output_csv(true);
422 } else if (ceph_argparse_flag(args, i, "--help-output", (char*)NULL)) {
423 data_analysis_usage();
424 return EXIT_SUCCESS;
425 } else if (ceph_argparse_witharg(args, i, &val, "--output-name", (char*)NULL)) {
426 std::string name(val);
427 if (i == args.end()) {
428 cerr << "expecting additional argument to --output-name" << std::endl;
429 return EXIT_FAILURE;
430 }
431 else {
432 tester.set_output_data_file_name(name + "-");
433 }
434 } else if (ceph_argparse_witharg(args, i, &val, "--remove_item", (char*)NULL)) {
435 remove_name = val;
436 } else if (ceph_argparse_witharg(args, i, &val, "--reweight_item", (char*)NULL)) {
437 reweight_name = val;
438 if (i == args.end()) {
439 cerr << "expecting additional argument to --reweight-item" << std::endl;
440 return EXIT_FAILURE;
441 }
442 reweight_weight = atof(*i);
443 i = args.erase(i);
444 } else if (ceph_argparse_flag(args, i, "--build", (char*)NULL)) {
445 build = true;
446 } else if (ceph_argparse_witharg(args, i, &num_osds, err, "--num_osds", (char*)NULL)) {
447 if (!err.str().empty()) {
448 cerr << err.str() << std::endl;
449 return EXIT_FAILURE;
450 }
451 } else if (ceph_argparse_witharg(args, i, &x, err, "--num_rep", (char*)NULL)) {
452 if (!err.str().empty()) {
453 cerr << err.str() << std::endl;
454 return EXIT_FAILURE;
455 }
456 tester.set_num_rep(x);
457 } else if (ceph_argparse_witharg(args, i, &x, err, "--max_x", (char*)NULL)) {
458 if (!err.str().empty()) {
459 cerr << err.str() << std::endl;
460 return EXIT_FAILURE;
461 }
462 tester.set_max_x(x);
463 } else if (ceph_argparse_witharg(args, i, &x, err, "--min_x", (char*)NULL)) {
464 if (!err.str().empty()) {
465 cerr << err.str() << std::endl;
466 return EXIT_FAILURE;
467 }
468 tester.set_min_x(x);
469 } else if (ceph_argparse_witharg(args, i, &z, err, "--pool_id", (char*)NULL)) {
470 if (!err.str().empty()) {
471 cerr << err.str() << std::endl;
472 return EXIT_FAILURE;
473 }
474 tester.set_pool_id(z);
475 } else if (ceph_argparse_witharg(args, i, &x, err, "--x", (char*)NULL)) {
476 if (!err.str().empty()) {
477 cerr << err.str() << std::endl;
478 return EXIT_FAILURE;
479 }
480 tester.set_x(x);
481 } else if (ceph_argparse_witharg(args, i, &x, err, "--max_rule", (char*)NULL)) {
482 if (!err.str().empty()) {
483 cerr << err.str() << std::endl;
484 return EXIT_FAILURE;
485 }
486 tester.set_max_rule(x);
487 } else if (ceph_argparse_witharg(args, i, &x, err, "--min_rule", (char*)NULL)) {
488 if (!err.str().empty()) {
489 cerr << err.str() << std::endl;
490 return EXIT_FAILURE;
491 }
492 tester.set_min_rule(x);
493 } else if (ceph_argparse_witharg(args, i, &x, err, "--rule", (char*)NULL)) {
494 if (!err.str().empty()) {
495 cerr << err.str() << std::endl;
496 return EXIT_FAILURE;
497 }
498 tester.set_rule(x);
499 } else if (ceph_argparse_witharg(args, i, &x, err, "--ruleset", (char*)NULL)) {
500 if (!err.str().empty()) {
501 cerr << err.str() << std::endl;
502 return EXIT_FAILURE;
503 }
504 tester.set_ruleset(x);
505 } else if (ceph_argparse_witharg(args, i, &x, err, "--batches", (char*)NULL)) {
506 if (!err.str().empty()) {
507 cerr << err.str() << std::endl;
508 return EXIT_FAILURE;
509 }
510 tester.set_batches(x);
511 } else if (ceph_argparse_witharg(args, i, &y, err, "--mark-down-ratio", (char*)NULL)) {
512 if (!err.str().empty()) {
513 cerr << err.str() << std::endl;
514 return EXIT_FAILURE;
515 }
516 tester.set_device_down_ratio(y);
517 } else if (ceph_argparse_witharg(args, i, &y, err, "--mark-down-bucket-ratio", (char*)NULL)) {
518 if (!err.str().empty()) {
519 cerr << err.str() << std::endl;
520 return EXIT_FAILURE;
521 }
522 tester.set_bucket_down_ratio(y);
523 } else if (ceph_argparse_witharg(args, i, &tmp, err, "--weight", (char*)NULL)) {
524 if (!err.str().empty()) {
525 cerr << err.str() << std::endl;
526 return EXIT_FAILURE;
527 }
528 int dev = tmp;
529 if (i == args.end()) {
530 cerr << "expecting additional argument to --weight" << std::endl;
531 return EXIT_FAILURE;
532 }
533 float f = atof(*i);
534 i = args.erase(i);
535 tester.set_device_weight(dev, f);
536 }
537 else {
538 ++i;
539 }
540 }
541
542 if (test && !check && !display && !write_to_file) {
543 cerr << "WARNING: no output selected; use --output-csv or --show-X" << std::endl;
544 }
545
546 if (decompile + compile + build > 1) {
547 cerr << "cannot specify more than one of compile, decompile, and build" << std::endl;
548 return EXIT_FAILURE;
549 }
550 if (!check && !compile && !decompile && !build && !test && !reweight && !adjust && !tree && !dump &&
551 add_item < 0 && full_location < 0 &&
552 remove_name.empty() && reweight_name.empty()) {
553 cerr << "no action specified; -h for help" << std::endl;
554 return EXIT_FAILURE;
555 }
556 if ((!build) && (!args.empty())) {
557 cerr << "unrecognized arguments: " << args << std::endl;
558 return EXIT_FAILURE;
559 }
560 else {
561 if ((args.size() % 3) != 0U) {
562 cerr << "remaining args: " << args << std::endl;
563 cerr << "layers must be specified with 3-tuples of (name, buckettype, size)"
564 << std::endl;
565 return EXIT_FAILURE;
566 }
567 for (size_t j = 0; j < args.size(); j += 3) {
568 layer_t l;
569 l.name = args[j];
570 l.buckettype = args[j+1];
571 l.size = atoi(args[j+2]);
572 layers.push_back(l);
573 }
574 }
575
576 /*
577 if (outfn) cout << "outfn " << outfn << std::endl;
578 if (cinfn) cout << "cinfn " << cinfn << std::endl;
579 if (dinfn) cout << "dinfn " << dinfn << std::endl;
580 */
581
582 bool modified = false;
583
584 // input ----
585
586 if (!infn.empty()) {
587 bufferlist bl;
588 std::string error;
589
590 int r = 0;
591 if (infn == "-") {
592 if (isatty(STDIN_FILENO)) {
593 cerr << "stdin must not be from a tty" << std::endl;
594 return EXIT_FAILURE;
595 }
596 r = get_fd_data(STDIN_FILENO, bl);
597 if (r < 0) {
598 cerr << "error reading data from STDIN" << std::endl;
599 return EXIT_FAILURE;
600 }
601 } else {
602 r = bl.read_file(infn.c_str(), &error);
603 if (r < 0) {
604 cerr << me << ": error reading '" << infn << "': "
605 << error << std::endl;
606 return EXIT_FAILURE;
607 }
608 }
609 bufferlist::iterator p = bl.begin();
610 try {
611 crush.decode(p);
612 } catch(...) {
613 cerr << me << ": unable to decode " << infn << std::endl;
614 return EXIT_FAILURE;
615 }
616 }
617
618 if (compile) {
619 crush.create();
620
621 // read the file
622 ifstream in(srcfn.c_str());
623 if (!in.is_open()) {
624 cerr << "input file " << srcfn << " not found" << std::endl;
625 return -ENOENT;
626 }
627
628 CrushCompiler cc(crush, cerr, verbose);
629 if (unsafe_tunables)
630 cc.enable_unsafe_tunables();
631 int r = cc.compile(in, srcfn.c_str());
632 if (r < 0)
633 return EXIT_FAILURE;
634
635 modified = true;
636 }
637
638 if (build) {
639 if (layers.empty()) {
640 cerr << me << ": must specify at least one layer" << std::endl;
641 return EXIT_FAILURE;
642 }
643
644 crush.create();
645
646 vector<int> lower_items;
647 vector<int> lower_weights;
648
649 crush.set_max_devices(num_osds);
650 for (int i=0; i<num_osds; i++) {
651 lower_items.push_back(i);
652 lower_weights.push_back(0x10000);
653 crush.set_item_name(i, "osd." + stringify(i));
654 }
655
656 crush.set_type_name(0, "osd");
657 int type = 1;
658 for (vector<layer_t>::iterator p = layers.begin(); p != layers.end(); ++p, type++) {
659 layer_t &l = *p;
660
661 dout(2) << "layer " << type
662 << " " << l.name
663 << " bucket type " << l.buckettype
664 << " " << l.size
665 << dendl;
666
667 crush.set_type_name(type, l.name);
668
669 int buckettype = -1;
670 for (int i = 0; bucket_types[i].name; i++)
671 if (l.buckettype && strcmp(l.buckettype, bucket_types[i].name) == 0) {
672 buckettype = bucket_types[i].type;
673 break;
674 }
675 if (buckettype < 0) {
676 cerr << "unknown bucket type '" << l.buckettype << "'" << std::endl;
677 return EXIT_FAILURE;
678 }
679
680 // build items
681 vector<int> cur_items;
682 vector<int> cur_weights;
683 unsigned lower_pos = 0; // lower pos
684
685 dout(2) << "lower_items " << lower_items << dendl;
686 dout(2) << "lower_weights " << lower_weights << dendl;
687
688 int i = 0;
689 while (1) {
690 if (lower_pos == lower_items.size())
691 break;
692
693 int items[num_osds];
694 int weights[num_osds];
695
696 int weight = 0;
697 int j;
698 for (j=0; j<l.size || l.size==0; j++) {
699 if (lower_pos == lower_items.size())
700 break;
701 items[j] = lower_items[lower_pos];
702 weights[j] = lower_weights[lower_pos];
703 weight += weights[j];
704 lower_pos++;
705 dout(2) << " item " << items[j] << " weight " << weights[j] << dendl;
706 }
707
708 int id;
709 int r = crush.add_bucket(0, buckettype, CRUSH_HASH_DEFAULT, type, j, items, weights, &id);
710 if (r < 0) {
711 cerr << " Couldn't add bucket: " << cpp_strerror(r) << std::endl;
712 return r;
713 }
714
715 char format[20];
716 format[sizeof(format)-1] = '\0';
717 if (l.size)
718 snprintf(format, sizeof(format)-1, "%s%%d", l.name);
719 else
720 strncpy(format, l.name, sizeof(format)-1);
721 char name[20];
722 snprintf(name, sizeof(name), format, i);
723 crush.set_item_name(id, name);
724
725 dout(2) << " in bucket " << id << " '" << name << "' size " << j << " weight " << weight << dendl;
726
727 cur_items.push_back(id);
728 cur_weights.push_back(weight);
729 i++;
730 }
731
732 lower_items.swap(cur_items);
733 lower_weights.swap(cur_weights);
734 }
735
736 string root = layers.back().size == 0 ? layers.back().name :
737 string(layers.back().name) + "0";
738
739 {
740 set<int> roots;
741 crush.find_roots(roots);
742 if (roots.size() > 1)
743 dout(1) << "The crush rulesets will use the root " << root << "\n"
744 << "and ignore the others.\n"
745 << "There are " << roots.size() << " roots, they can be\n"
746 << "grouped into a single root by appending something like:\n"
747 << " root straw 0\n"
748 << dendl;
749 }
750
751 if (OSDMap::build_simple_crush_rulesets(g_ceph_context, crush, root, &cerr))
752 return EXIT_FAILURE;
753
754 modified = true;
755 }
756
757 // mutate ----
758
759 if (choose_local_tries >= 0) {
760 crush.set_choose_local_tries(choose_local_tries);
761 modified = true;
762 }
763 if (choose_local_fallback_tries >= 0) {
764 crush.set_choose_local_fallback_tries(choose_local_fallback_tries);
765 modified = true;
766 }
767 if (choose_total_tries >= 0) {
768 crush.set_choose_total_tries(choose_total_tries);
769 modified = true;
770 }
771 if (chooseleaf_descend_once >= 0) {
772 crush.set_chooseleaf_descend_once(chooseleaf_descend_once);
773 modified = true;
774 }
775 if (chooseleaf_vary_r >= 0) {
776 crush.set_chooseleaf_vary_r(chooseleaf_vary_r);
777 modified = true;
778 }
779 if (chooseleaf_stable >= 0) {
780 crush.set_chooseleaf_stable(chooseleaf_stable);
781 modified = true;
782 }
783 if (straw_calc_version >= 0) {
784 crush.set_straw_calc_version(straw_calc_version);
785 modified = true;
786 }
787 if (allowed_bucket_algs >= 0) {
788 crush.set_allowed_bucket_algs(allowed_bucket_algs);
789 modified = true;
790 }
791
792 if (!reweight_name.empty()) {
793 cout << me << " reweighting item " << reweight_name << " to " << reweight_weight << std::endl;
794 int r;
795 if (!crush.name_exists(reweight_name)) {
796 cerr << " name " << reweight_name << " dne" << std::endl;
797 r = -ENOENT;
798 } else {
799 int item = crush.get_item_id(reweight_name);
800 r = crush.adjust_item_weightf(g_ceph_context, item, reweight_weight);
801 }
802 if (r >= 0)
803 modified = true;
804 else {
805 cerr << me << " " << cpp_strerror(r) << std::endl;
806 return r;
807 }
808 }
809
810 if (!remove_name.empty()) {
811 cout << me << " removing item " << remove_name << std::endl;
812 int r;
813 if (!crush.name_exists(remove_name)) {
814 cerr << " name " << remove_name << " dne" << std::endl;
815 r = -ENOENT;
816 } else {
817 int remove_item = crush.get_item_id(remove_name);
818 r = crush.remove_item(g_ceph_context, remove_item, false);
819 }
820 if (r == 0)
821 modified = true;
822 else {
823 cerr << me << " " << cpp_strerror(r) << std::endl;
824 return r;
825 }
826 }
827
828 if (add_item >= 0) {
829 int r;
830 if (update_item) {
831 r = crush.update_item(g_ceph_context, add_item, add_weight, add_name.c_str(), add_loc);
832 } else {
833 r = crush.insert_item(g_ceph_context, add_item, add_weight, add_name.c_str(), add_loc);
834 }
835 if (r >= 0) {
836 modified = true;
837 } else {
838 cerr << me << " " << cpp_strerror(r) << std::endl;
839 return r;
840 }
841 }
842
843 if (reweight) {
844 crush.reweight(g_ceph_context);
845 modified = true;
846 }
847
848
849 // display ---
850 if (full_location >= 0) {
851 map<string, string> loc = crush.get_full_location(full_location);
852 for (map<string,string>::iterator p = loc.begin();
853 p != loc.end();
854 ++p) {
855 cout << p->first << "\t" << p->second << std::endl;
856 }
857 }
858
859 if (tree) {
860 crush.dump_tree(&cout, NULL);
861 }
862
863 if (dump) {
864 boost::scoped_ptr<Formatter> f(Formatter::create(dump_format, "json-pretty", "json-pretty"));
865 f->open_object_section("crush_map");
866 crush.dump(f.get());
867 f->close_section();
868 f->flush(cout);
869 cout << "\n";
870 }
871
872 if (decompile) {
873 CrushCompiler cc(crush, cerr, verbose);
874 if (!outfn.empty()) {
875 ofstream o;
876 o.open(outfn.c_str(), ios::out | ios::binary | ios::trunc);
877 if (!o.is_open()) {
878 cerr << me << ": error writing '" << outfn << "'" << std::endl;
879 return EXIT_FAILURE;
880 }
881 cc.decompile(o);
882 o.close();
883 } else {
884 cc.decompile(cout);
885 }
886 }
887
888 if (check) {
889 tester.check_overlapped_rules();
890 if (max_id >= 0) {
891 if (!tester.check_name_maps(max_id)) {
892 return EXIT_FAILURE;
893 }
894 }
895 }
896
897 if (test) {
898 if (tester.get_output_utilization_all() ||
899 tester.get_output_utilization())
900 tester.set_output_statistics(true);
901
902 int r = tester.test();
903 if (r < 0)
904 return EXIT_FAILURE;
905 }
906
907 // output ---
908 if (modified) {
909 crush.finalize();
910
911 if (outfn.empty()) {
912 cout << me << " successfully built or modified map. Use '-o <file>' to write it out." << std::endl;
913 } else {
914 bufferlist bl;
915 crush.encode(bl, CEPH_FEATURES_SUPPORTED_DEFAULT);
916 int r = bl.write_file(outfn.c_str());
917 if (r < 0) {
918 cerr << me << ": error writing '" << outfn << "': " << cpp_strerror(r) << std::endl;
919 return EXIT_FAILURE;
920 }
921 if (verbose)
922 cout << "wrote crush map to " << outfn << std::endl;
923 }
924 }
925
926 return 0;
927 }
928 /*
929 * Local Variables:
930 * compile-command: "cd .. ; make crushtool && test/run-cli-tests"
931 * End:
932 */