]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/osdmaptool.cc
ab6346f80f301aa80cbfb413dcc5020c80aac574
[ceph.git] / ceph / src / tools / osdmaptool.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 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include <string>
16 #include <sys/stat.h>
17
18 #include "common/ceph_argparse.h"
19 #include "common/errno.h"
20 #include "common/safe_io.h"
21
22 #include "global/global_init.h"
23 #include "osd/OSDMap.h"
24
25 using namespace std;
26
27 void usage()
28 {
29 cout << " usage: [--print] [--createsimple <numosd> [--clobber] [--pg_bits <bitsperosd>]] <mapfilename>" << std::endl;
30 cout << " --export-crush <file> write osdmap's crush map to <file>" << std::endl;
31 cout << " --import-crush <file> replace osdmap's crush map with <file>" << std::endl;
32 cout << " --test-map-pgs [--pool <poolid>] [--pg_num <pg_num>] map all pgs" << std::endl;
33 cout << " --test-map-pgs-dump [--pool <poolid>] map all pgs" << std::endl;
34 cout << " --test-map-pgs-dump-all [--pool <poolid>] map all pgs to osds" << std::endl;
35 cout << " --mark-up-in mark osds up and in (but do not persist)" << std::endl;
36 cout << " --clear-temp clear pg_temp and primary_temp" << std::endl;
37 cout << " --test-random do random placements" << std::endl;
38 cout << " --test-map-pg <pgid> map a pgid to osds" << std::endl;
39 cout << " --test-map-object <objectname> [--pool <poolid>] map an object to osds"
40 << std::endl;
41 cout << " --upmap-cleanup <file> clean up pg_upmap[_items] entries, writing" << std::endl;
42 cout << " commands to <file> [default: - for stdout]" << std::endl;
43 cout << " --upmap <file> calculate pg upmap entries to balance pg layout" << std::endl;
44 cout << " writing commands to <file> [default: - for stdout]" << std::endl;
45 cout << " --upmap-max <max-count> set max upmap entries to calculate [default: 100]" << std::endl;
46 cout << " --upmap-deviation <max-deviation>" << std::endl;
47 cout << " max deviation from target [default: .01]" << std::endl;
48 cout << " --upmap-pool <poolname> restrict upmap balancing to 1 or more pools" << std::endl;
49 cout << " --upmap-save write modified OSDMap with upmap changes" << std::endl;
50 exit(1);
51 }
52
53 void print_inc_upmaps(const OSDMap::Incremental& pending_inc, int fd)
54 {
55 ostringstream ss;
56 for (auto& i : pending_inc.old_pg_upmap) {
57 ss << "ceph osd rm-pg-upmap " << i << std::endl;
58 }
59 for (auto& i : pending_inc.new_pg_upmap) {
60 ss << "ceph osd pg-upmap " << i.first;
61 for (auto osd : i.second) {
62 ss << " " << osd;
63 }
64 ss << std::endl;
65 }
66 for (auto& i : pending_inc.old_pg_upmap_items) {
67 ss << "ceph osd rm-pg-upmap-items " << i << std::endl;
68 }
69 for (auto& i : pending_inc.new_pg_upmap_items) {
70 ss << "ceph osd pg-upmap-items " << i.first;
71 for (auto p : i.second) {
72 ss << " " << p.first << " " << p.second;
73 }
74 ss << std::endl;
75 }
76 string s = ss.str();
77 int r = safe_write(fd, s.c_str(), s.size());
78 if (r < 0) {
79 cerr << "error writing output: " << cpp_strerror(r) << std::endl;
80 exit(1);
81 }
82 }
83
84 int main(int argc, const char **argv)
85 {
86 vector<const char*> args;
87 argv_to_vec(argc, argv, args);
88 env_to_vec(args);
89
90 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
91 CODE_ENVIRONMENT_UTILITY,
92 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
93 common_init_finish(g_ceph_context);
94
95 const char *me = argv[0];
96
97 std::string fn;
98 bool print = false;
99 boost::scoped_ptr<Formatter> print_formatter;
100 bool tree = false;
101 boost::scoped_ptr<Formatter> tree_formatter;
102 bool createsimple = false;
103 bool create_from_conf = false;
104 int num_osd = 0;
105 int pg_bits = g_conf->osd_pg_bits;
106 int pgp_bits = g_conf->osd_pgp_bits;
107 bool clobber = false;
108 bool modified = false;
109 std::string export_crush, import_crush, test_map_pg, test_map_object;
110 bool test_crush = false;
111 int range_first = -1;
112 int range_last = -1;
113 int pool = -1;
114 bool mark_up_in = false;
115 bool clear_temp = false;
116 bool test_map_pgs = false;
117 bool test_map_pgs_dump = false;
118 bool test_random = false;
119 bool upmap_cleanup = false;
120 bool upmap = false;
121 bool upmap_save = false;
122 std::string upmap_file = "-";
123 int upmap_max = 100;
124 float upmap_deviation = .01;
125 std::set<std::string> upmap_pools;
126 int64_t pg_num = -1;
127 bool test_map_pgs_dump_all = false;
128
129 std::string val;
130 std::ostringstream err;
131 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
132 if (ceph_argparse_double_dash(args, i)) {
133 break;
134 } else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
135 usage();
136 } else if (ceph_argparse_flag(args, i, "-p", "--print", (char*)NULL)) {
137 print = true;
138 } else if (ceph_argparse_witharg(args, i, &val, err, "--dump", (char*)NULL)) {
139 print = true;
140 if (!val.empty() && val != "plain") {
141 print_formatter.reset(Formatter::create(val, "", "json"));
142 }
143 } else if (ceph_argparse_witharg(args, i, &val, err, "--tree", (char*)NULL)) {
144 tree = true;
145 if (!val.empty() && val != "plain") {
146 tree_formatter.reset(Formatter::create(val, "", "json"));
147 }
148 } else if (ceph_argparse_witharg(args, i, &upmap_file, "--upmap-cleanup", (char*)NULL)) {
149 upmap_cleanup = true;
150 } else if (ceph_argparse_witharg(args, i, &upmap_file, "--upmap-save", (char*)NULL)) {
151 upmap_save = true;
152 } else if (ceph_argparse_witharg(args, i, &upmap_file, "--upmap", (char*)NULL)) {
153 upmap_cleanup = true;
154 upmap = true;
155 } else if (ceph_argparse_witharg(args, i, &upmap_max, err, "--upmap-max", (char*)NULL)) {
156 } else if (ceph_argparse_witharg(args, i, &upmap_deviation, err, "--upmap-deviation", (char*)NULL)) {
157 } else if (ceph_argparse_witharg(args, i, &val, "--upmap-pool", (char*)NULL)) {
158 upmap_pools.insert(val);
159 } else if (ceph_argparse_witharg(args, i, &num_osd, err, "--createsimple", (char*)NULL)) {
160 if (!err.str().empty()) {
161 cerr << err.str() << std::endl;
162 exit(EXIT_FAILURE);
163 }
164 createsimple = true;
165 } else if (ceph_argparse_flag(args, i, "--create-from-conf", (char*)NULL)) {
166 create_from_conf = true;
167 } else if (ceph_argparse_flag(args, i, "--mark-up-in", (char*)NULL)) {
168 mark_up_in = true;
169 } else if (ceph_argparse_flag(args, i, "--clear-temp", (char*)NULL)) {
170 clear_temp = true;
171 } else if (ceph_argparse_flag(args, i, "--test-map-pgs", (char*)NULL)) {
172 test_map_pgs = true;
173 } else if (ceph_argparse_flag(args, i, "--test-map-pgs-dump", (char*)NULL)) {
174 test_map_pgs_dump = true;
175 } else if (ceph_argparse_flag(args, i, "--test-map-pgs-dump-all", (char*)NULL)) {
176 test_map_pgs_dump_all = true;
177 } else if (ceph_argparse_flag(args, i, "--test-random", (char*)NULL)) {
178 test_random = true;
179 } else if (ceph_argparse_flag(args, i, "--clobber", (char*)NULL)) {
180 clobber = true;
181 } else if (ceph_argparse_witharg(args, i, &pg_bits, err, "--pg_bits", (char*)NULL)) {
182 if (!err.str().empty()) {
183 cerr << err.str() << std::endl;
184 exit(EXIT_FAILURE);
185 }
186 } else if (ceph_argparse_witharg(args, i, &pgp_bits, err, "--pgp_bits", (char*)NULL)) {
187 if (!err.str().empty()) {
188 cerr << err.str() << std::endl;
189 exit(EXIT_FAILURE);
190 }
191 } else if (ceph_argparse_witharg(args, i, &val, "--export_crush", (char*)NULL)) {
192 export_crush = val;
193 } else if (ceph_argparse_witharg(args, i, &val, "--import_crush", (char*)NULL)) {
194 import_crush = val;
195 } else if (ceph_argparse_witharg(args, i, &val, "--test_map_pg", (char*)NULL)) {
196 test_map_pg = val;
197 } else if (ceph_argparse_witharg(args, i, &val, "--test_map_object", (char*)NULL)) {
198 test_map_object = val;
199 } else if (ceph_argparse_flag(args, i, "--test_crush", (char*)NULL)) {
200 test_crush = true;
201 } else if (ceph_argparse_witharg(args, i, &val, err, "--pg_num", (char*)NULL)) {
202 string interr;
203 pg_num = strict_strtoll(val.c_str(), 10, &interr);
204 if (interr.length() > 0) {
205 cerr << "error parsing integer value " << interr << std::endl;
206 exit(EXIT_FAILURE);
207 }
208 } else if (ceph_argparse_witharg(args, i, &range_first, err, "--range_first", (char*)NULL)) {
209 } else if (ceph_argparse_witharg(args, i, &range_last, err, "--range_last", (char*)NULL)) {
210 } else if (ceph_argparse_witharg(args, i, &pool, err, "--pool", (char*)NULL)) {
211 if (!err.str().empty()) {
212 cerr << err.str() << std::endl;
213 exit(EXIT_FAILURE);
214 }
215 } else {
216 ++i;
217 }
218 }
219 if (args.empty()) {
220 cerr << me << ": must specify osdmap filename" << std::endl;
221 usage();
222 }
223 else if (args.size() > 1) {
224 cerr << me << ": too many arguments" << std::endl;
225 usage();
226 }
227 fn = args[0];
228
229 if (range_first >= 0 && range_last >= 0) {
230 set<OSDMap*> maps;
231 OSDMap *prev = NULL;
232 for (int i=range_first; i <= range_last; i++) {
233 ostringstream f;
234 f << fn << "/" << i;
235 bufferlist bl;
236 string error, s = f.str();
237 int r = bl.read_file(s.c_str(), &error);
238 if (r < 0) {
239 cerr << "unable to read " << s << ": " << cpp_strerror(r) << std::endl;
240 exit(1);
241 }
242 cout << s << " got " << bl.length() << " bytes" << std::endl;
243 OSDMap *o = new OSDMap;
244 o->decode(bl);
245 maps.insert(o);
246 if (prev)
247 OSDMap::dedup(prev, o);
248 prev = o;
249 }
250 exit(0);
251 }
252
253 OSDMap osdmap;
254 bufferlist bl;
255
256 cerr << me << ": osdmap file '" << fn << "'" << std::endl;
257
258 int r = 0;
259 struct stat st;
260 if (!createsimple && !create_from_conf && !clobber) {
261 std::string error;
262 r = bl.read_file(fn.c_str(), &error);
263 if (r == 0) {
264 try {
265 osdmap.decode(bl);
266 }
267 catch (const buffer::error &e) {
268 cerr << me << ": error decoding osdmap '" << fn << "'" << std::endl;
269 return -1;
270 }
271 }
272 else {
273 cerr << me << ": couldn't open " << fn << ": " << error << std::endl;
274 return -1;
275 }
276 }
277 else if ((createsimple || create_from_conf) && !clobber && ::stat(fn.c_str(), &st) == 0) {
278 cerr << me << ": " << fn << " exists, --clobber to overwrite" << std::endl;
279 return -1;
280 }
281
282 if (createsimple || create_from_conf) {
283 if (createsimple) {
284 if (num_osd < 1) {
285 cerr << me << ": osd count must be > 0" << std::endl;
286 exit(1);
287 }
288 } else {
289 num_osd = -1;
290 }
291 uuid_d fsid;
292 memset(&fsid, 0, sizeof(uuid_d));
293 osdmap.build_simple(g_ceph_context, 0, fsid, num_osd, pg_bits, pgp_bits);
294 modified = true;
295 }
296
297 if (mark_up_in) {
298 cout << "marking all OSDs up and in" << std::endl;
299 int n = osdmap.get_max_osd();
300 for (int i=0; i<n; i++) {
301 osdmap.set_state(i, osdmap.get_state(i) | CEPH_OSD_UP);
302 osdmap.set_weight(i, CEPH_OSD_IN);
303 osdmap.crush->adjust_item_weightf(g_ceph_context, i, 1.0);
304 }
305 }
306 if (clear_temp) {
307 cout << "clearing pg/primary temp" << std::endl;
308 osdmap.clear_temp();
309 }
310 int upmap_fd = STDOUT_FILENO;
311 if (upmap || upmap_cleanup) {
312 if (upmap_file != "-") {
313 upmap_fd = ::open(upmap_file.c_str(), O_CREAT|O_WRONLY, 0644);
314 if (upmap_fd < 0) {
315 cerr << "error opening " << upmap_file << ": " << cpp_strerror(errno)
316 << std::endl;
317 exit(1);
318 }
319 cout << "writing upmap command output to: " << upmap_file << std::endl;
320 }
321 }
322 if (upmap_cleanup) {
323 cout << "checking for upmap cleanups" << std::endl;
324 OSDMap::Incremental pending_inc(osdmap.get_epoch()+1);
325 pending_inc.fsid = osdmap.get_fsid();
326 int r = osdmap.clean_pg_upmaps(g_ceph_context, &pending_inc);
327 if (r > 0) {
328 print_inc_upmaps(pending_inc, upmap_fd);
329 r = osdmap.apply_incremental(pending_inc);
330 assert(r == 0);
331 }
332 }
333 if (upmap) {
334 cout << "upmap, max-count " << upmap_max
335 << ", max deviation " << upmap_deviation
336 << std::endl;
337 OSDMap::Incremental pending_inc(osdmap.get_epoch()+1);
338 pending_inc.fsid = osdmap.get_fsid();
339 set<int64_t> pools;
340 for (auto& s : upmap_pools) {
341 int64_t p = osdmap.lookup_pg_pool_name(s);
342 if (p < 0) {
343 cerr << " pool '" << s << "' does not exist" << std::endl;
344 exit(1);
345 }
346 pools.insert(p);
347 }
348 if (!pools.empty())
349 cout << " limiting to pools " << upmap_pools << " (" << pools << ")"
350 << std::endl;
351 int changed = osdmap.calc_pg_upmaps(
352 g_ceph_context, upmap_deviation,
353 upmap_max, pools,
354 &pending_inc);
355 if (changed) {
356 print_inc_upmaps(pending_inc, upmap_fd);
357 if (upmap_save) {
358 int r = osdmap.apply_incremental(pending_inc);
359 assert(r == 0);
360 modified = true;
361 }
362 } else {
363 cout << "no upmaps proposed" << std::endl;
364 }
365 }
366 if (upmap_file != "-") {
367 ::close(upmap_fd);
368 }
369
370 if (!import_crush.empty()) {
371 bufferlist cbl;
372 std::string error;
373 r = cbl.read_file(import_crush.c_str(), &error);
374 if (r) {
375 cerr << me << ": error reading crush map from " << import_crush
376 << ": " << error << std::endl;
377 exit(1);
378 }
379
380 // validate
381 CrushWrapper cw;
382 bufferlist::iterator p = cbl.begin();
383 cw.decode(p);
384
385 if (cw.get_max_devices() > osdmap.get_max_osd()) {
386 cerr << me << ": crushmap max_devices " << cw.get_max_devices()
387 << " > osdmap max_osd " << osdmap.get_max_osd() << std::endl;
388 exit(1);
389 }
390
391 // apply
392 OSDMap::Incremental inc;
393 inc.fsid = osdmap.get_fsid();
394 inc.epoch = osdmap.get_epoch()+1;
395 inc.crush = cbl;
396 osdmap.apply_incremental(inc);
397 cout << me << ": imported " << cbl.length() << " byte crush map from " << import_crush << std::endl;
398 modified = true;
399 }
400
401 if (!export_crush.empty()) {
402 bufferlist cbl;
403 osdmap.crush->encode(cbl, CEPH_FEATURES_SUPPORTED_DEFAULT);
404 r = cbl.write_file(export_crush.c_str());
405 if (r < 0) {
406 cerr << me << ": error writing crush map to " << import_crush << std::endl;
407 exit(1);
408 }
409 cout << me << ": exported crush map to " << export_crush << std::endl;
410 }
411
412 if (!test_map_object.empty()) {
413 object_t oid(test_map_object);
414 if (pool == -1) {
415 cout << me << ": assuming pool 0 (use --pool to override)" << std::endl;
416 pool = 0;
417 }
418 if (!osdmap.have_pg_pool(pool)) {
419 cerr << "There is no pool " << pool << std::endl;
420 exit(1);
421 }
422 object_locator_t loc(pool);
423 pg_t raw_pgid = osdmap.object_locator_to_pg(oid, loc);
424 pg_t pgid = osdmap.raw_pg_to_pg(raw_pgid);
425
426 vector<int> acting;
427 osdmap.pg_to_acting_osds(pgid, acting);
428 cout << " object '" << oid
429 << "' -> " << pgid
430 << " -> " << acting
431 << std::endl;
432 }
433 if (!test_map_pg.empty()) {
434 pg_t pgid;
435 if (!pgid.parse(test_map_pg.c_str())) {
436 cerr << me << ": failed to parse pg '" << test_map_pg << std::endl;
437 usage();
438 }
439 cout << " parsed '" << test_map_pg << "' -> " << pgid << std::endl;
440
441 vector<int> raw, up, acting;
442 int raw_primary, up_primary, acting_primary;
443 osdmap.pg_to_raw_osds(pgid, &raw, &raw_primary);
444 osdmap.pg_to_up_acting_osds(pgid, &up, &up_primary,
445 &acting, &acting_primary);
446 cout << pgid << " raw (" << raw << ", p" << raw_primary
447 << ") up (" << up << ", p" << up_primary
448 << ") acting (" << acting << ", p" << acting_primary << ")"
449 << std::endl;
450 }
451 if (test_map_pgs || test_map_pgs_dump || test_map_pgs_dump_all) {
452 if (pool != -1 && !osdmap.have_pg_pool(pool)) {
453 cerr << "There is no pool " << pool << std::endl;
454 exit(1);
455 }
456 int n = osdmap.get_max_osd();
457 vector<int> count(n, 0);
458 vector<int> first_count(n, 0);
459 vector<int> primary_count(n, 0);
460 vector<int> size(30, 0);
461 if (test_random)
462 srand(getpid());
463 auto& pools = osdmap.get_pools();
464 for (auto p = pools.begin(); p != pools.end(); ++p) {
465 if (pool != -1 && p->first != pool)
466 continue;
467 if (pg_num > 0)
468 p->second.set_pg_num(pg_num);
469
470 cout << "pool " << p->first
471 << " pg_num " << p->second.get_pg_num() << std::endl;
472 for (unsigned i = 0; i < p->second.get_pg_num(); ++i) {
473 pg_t pgid = pg_t(i, p->first);
474
475 vector<int> osds, raw, up, acting;
476 int primary, calced_primary, up_primary, acting_primary;
477 if (test_random) {
478 osds.resize(p->second.size);
479 for (unsigned i=0; i<osds.size(); ++i) {
480 osds[i] = rand() % osdmap.get_max_osd();
481 }
482 primary = osds[0];
483 } else if (test_map_pgs_dump_all) {
484 osdmap.pg_to_raw_osds(pgid, &raw, &calced_primary);
485 osdmap.pg_to_up_acting_osds(pgid, &up, &up_primary,
486 &acting, &acting_primary);
487 } else {
488 osdmap.pg_to_acting_osds(pgid, &osds, &primary);
489 }
490 size[osds.size()]++;
491
492 if (test_map_pgs_dump) {
493 cout << pgid << "\t" << osds << "\t" << primary << std::endl;
494 } else if (test_map_pgs_dump_all) {
495 cout << pgid << " raw (" << raw << ", p" << calced_primary
496 << ") up (" << up << ", p" << up_primary
497 << ") acting (" << acting << ", p" << acting_primary << ")"
498 << std::endl;
499 }
500
501 for (unsigned i=0; i<osds.size(); i++) {
502 //cout << " rep " << i << " on " << osds[i] << std::endl;
503 count[osds[i]]++;
504 }
505 if (osds.size())
506 first_count[osds[0]]++;
507 if (primary >= 0)
508 primary_count[primary]++;
509 }
510 }
511
512 uint64_t total = 0;
513 int in = 0;
514 int min_osd = -1;
515 int max_osd = -1;
516 cout << "#osd\tcount\tfirst\tprimary\tc wt\twt\n";
517 for (int i=0; i<n; i++) {
518 if (!osdmap.is_in(i))
519 continue;
520 if (osdmap.crush->get_item_weight(i) <= 0)
521 continue;
522 in++;
523 cout << "osd." << i
524 << "\t" << count[i]
525 << "\t" << first_count[i]
526 << "\t" << primary_count[i]
527 << "\t" << osdmap.crush->get_item_weightf(i)
528 << "\t" << osdmap.get_weightf(i)
529 << std::endl;
530 total += count[i];
531 if (count[i] &&
532 (min_osd < 0 ||
533 count[i] < count[min_osd]))
534 min_osd = i;
535 if (count[i] &&
536 (max_osd < 0 ||
537 count[i] > count[max_osd]))
538 max_osd = i;
539
540 }
541 uint64_t avg = in ? (total / in) : 0;
542 double dev = 0;
543 for (int i=0; i<n; i++) {
544 if (!osdmap.is_in(i))
545 continue;
546 if (osdmap.crush->get_item_weight(i) <= 0)
547 continue;
548 dev += (avg - count[i]) * (avg - count[i]);
549 }
550 dev /= in;
551 dev = sqrt(dev);
552
553 //double edev = sqrt(pgavg) * (double)avg / pgavg;
554 double edev = sqrt((double)total / (double)in * (1.0 - (1.0 / (double)in)));
555 cout << " in " << in << std::endl;
556 cout << " avg " << avg
557 << " stddev " << dev
558 << " (" << (dev/avg) << "x)"
559 << " (expected " << edev << " " << (edev/avg) << "x))"
560 << std::endl;
561
562 if (min_osd >= 0)
563 cout << " min osd." << min_osd << " " << count[min_osd] << std::endl;
564 if (max_osd >= 0)
565 cout << " max osd." << max_osd << " " << count[max_osd] << std::endl;
566
567 for (int i=0; i<4; i++) {
568 cout << "size " << i << "\t" << size[i] << std::endl;
569 }
570 }
571 if (test_crush) {
572 int pass = 0;
573 while (1) {
574 cout << "pass " << ++pass << std::endl;
575
576 ceph::unordered_map<pg_t,vector<int> > m;
577 for (map<int64_t,pg_pool_t>::const_iterator p = osdmap.get_pools().begin();
578 p != osdmap.get_pools().end();
579 ++p) {
580 const pg_pool_t *pool = osdmap.get_pg_pool(p->first);
581 for (ps_t ps = 0; ps < pool->get_pg_num(); ps++) {
582 pg_t pgid(ps, p->first, -1);
583 for (int i=0; i<100; i++) {
584 cout << pgid << " attempt " << i << std::endl;
585
586 vector<int> r;
587 osdmap.pg_to_acting_osds(pgid, r);
588 //cout << pgid << " " << r << std::endl;
589 if (m.count(pgid)) {
590 if (m[pgid] != r) {
591 cout << pgid << " had " << m[pgid] << " now " << r << std::endl;
592 ceph_abort();
593 }
594 } else
595 m[pgid] = r;
596 }
597 }
598 }
599 }
600 }
601
602 if (!print && !tree && !modified &&
603 export_crush.empty() && import_crush.empty() &&
604 test_map_pg.empty() && test_map_object.empty() &&
605 !test_map_pgs && !test_map_pgs_dump && !test_map_pgs_dump_all &&
606 !upmap && !upmap_cleanup) {
607 cerr << me << ": no action specified?" << std::endl;
608 usage();
609 }
610
611 if (modified)
612 osdmap.inc_epoch();
613
614 if (print) {
615 if (print_formatter) {
616 print_formatter->open_object_section("osdmap");
617 osdmap.dump(print_formatter.get());
618 print_formatter->close_section();
619 print_formatter->flush(cout);
620 } else {
621 osdmap.print(cout);
622 }
623 }
624
625 if (tree) {
626 if (tree_formatter) {
627 tree_formatter->open_object_section("tree");
628 osdmap.print_tree(tree_formatter.get(), NULL);
629 tree_formatter->close_section();
630 tree_formatter->flush(cout);
631 cout << std::endl;
632 } else {
633 osdmap.print_tree(NULL, &cout);
634 }
635 }
636 if (modified) {
637 bl.clear();
638 osdmap.encode(bl, CEPH_FEATURES_SUPPORTED_DEFAULT | CEPH_FEATURE_RESERVED);
639
640 // write it out
641 cout << me << ": writing epoch " << osdmap.get_epoch()
642 << " to " << fn
643 << std::endl;
644 int r = bl.write_file(fn.c_str());
645 if (r) {
646 cerr << "osdmaptool: error writing to '" << fn << "': "
647 << cpp_strerror(r) << std::endl;
648 return 1;
649 }
650 }
651
652
653 return 0;
654 }