1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "tools/rbd/ArgumentTypes.h"
5 #include "tools/rbd/Shell.h"
6 #include "tools/rbd/Utils.h"
7 #include "include/stringify.h"
8 #include "common/ceph_context.h"
9 #include "common/ceph_json.h"
10 #include "common/errno.h"
11 #include "common/Formatter.h"
12 #include "common/TextTable.h"
13 #include "global/global_context.h"
20 #include <sys/types.h>
23 #include <boost/algorithm/string.hpp>
24 #include <boost/assign.hpp>
25 #include <boost/bimap.hpp>
26 #include <boost/program_options.hpp>
27 #include "json_spirit/json_spirit.h"
33 namespace at
= argument_types
;
34 namespace po
= boost::program_options
;
38 enum class StatDescriptor
{
47 typedef boost::bimap
<StatDescriptor
, std::string
> StatDescriptors
;
49 static const StatDescriptors STAT_DESCRIPTORS
=
50 boost::assign::list_of
<StatDescriptors::relation
>
51 (StatDescriptor::WRITE_OPS
, "write_ops")
52 (StatDescriptor::READ_OPS
, "read_ops")
53 (StatDescriptor::WRITE_BYTES
, "write_bytes")
54 (StatDescriptor::READ_BYTES
, "read_bytes")
55 (StatDescriptor::WRITE_LATENCY
, "write_latency")
56 (StatDescriptor::READ_LATENCY
, "read_latency");
58 std::ostream
& operator<<(std::ostream
& os
, const StatDescriptor
& val
) {
59 auto it
= STAT_DESCRIPTORS
.left
.find(val
);
60 if (it
== STAT_DESCRIPTORS
.left
.end()) {
61 os
<< "unknown (" << static_cast<int>(val
) << ")";
68 void validate(boost::any
& v
, const std::vector
<std::string
>& values
,
69 StatDescriptor
*target_type
, int) {
70 po::validators::check_first_occurrence(v
);
71 std::string s
= po::validators::get_single_string(values
);
72 boost::replace_all(s
, "_", " ");
73 boost::replace_all(s
, "-", "_");
75 auto it
= STAT_DESCRIPTORS
.right
.find(s
);
76 if (it
== STAT_DESCRIPTORS
.right
.end()) {
77 throw po::validation_error(po::validation_error::invalid_option_value
);
79 v
= boost::any(it
->second
);
83 ImageStat(const std::string
& pool_name
, const std::string
& pool_namespace
,
84 const std::string
& image_name
)
85 : pool_name(pool_name
), pool_namespace(pool_namespace
),
86 image_name(image_name
) {
87 stats
.resize(STAT_DESCRIPTORS
.size());
90 std::string pool_name
;
91 std::string pool_namespace
;
92 std::string image_name
;
93 std::vector
<double> stats
;
96 typedef std::vector
<ImageStat
> ImageStats
;
98 typedef std::pair
<std::string
, std::string
> SpecPair
;
100 std::string
format_pool_spec(const std::string
& pool
,
101 const std::string
& pool_namespace
) {
102 std::string pool_spec
{pool
};
103 if (!pool_namespace
.empty()) {
104 pool_spec
+= "/" + pool_namespace
;
109 int query_iostats(librados::Rados
& rados
, const std::string
& pool_spec
,
110 StatDescriptor sort_by
, ImageStats
* image_stats
,
111 std::ostream
& err_os
) {
112 auto sort_by_str
= STAT_DESCRIPTORS
.left
.find(sort_by
)->second
;
114 std::string cmd
= R
"(
116 "prefix
": "rbd perf image stats
",
117 "pool_spec
": ")" + pool_spec + R"(",
118 "sort_by
": ")" + sort_by_str + R"(",
125 int r = rados.mgr_command(cmd, in_bl, &out_bl, &outs);
126 if (r == -EOPNOTSUPP) {
127 err_os << "rbd
: 'rbd_support' mgr module is
not enabled
."
128 << std::endl << std::endl
129 << "Use
'ceph mgr module enable rbd_support' to enable
."
133 err_os << "rbd
: mgr command failed
: " << cpp_strerror(r);
135 err_os << ": " << outs;
141 json_spirit::mValue json_root;
142 if (!json_spirit::read(out_bl.to_str(), json_root)) {
143 err_os << "rbd
: error parsing perf stats
" << std::endl;
147 image_stats->clear();
149 auto& root = json_root.get_obj();
151 // map JSON stat descriptor order to our internal order
152 std::map<uint32_t, uint32_t> json_to_internal_stats;
153 auto& json_stat_descriptors = root["stat_descriptors
"].get_array();
154 for (size_t idx = 0; idx < json_stat_descriptors.size(); ++idx) {
155 auto it = STAT_DESCRIPTORS.right.find(
156 json_stat_descriptors[idx].get_str());
157 if (it == STAT_DESCRIPTORS.right.end()) {
160 json_to_internal_stats[idx] = static_cast<uint32_t>(it->second);
163 // cache a mapping from pool descriptors back to pool-specs
164 std::map<std::string, SpecPair> json_to_internal_pools;
165 auto& pool_descriptors = root["pool_descriptors
"].get_obj();
166 for (auto& pool : pool_descriptors) {
167 auto& pool_spec = pool.second.get_str();
168 auto pos = pool_spec.rfind("/");
170 SpecPair pair{pool_spec.substr(0, pos), ""};
171 if (pos != std::string::npos) {
172 pair.second = pool_spec.substr(pos + 1);
175 json_to_internal_pools[pool.first] = pair;
178 auto& stats = root["stats
"].get_array();
179 for (auto& stat : stats) {
180 auto& stat_obj = stat.get_obj();
181 if (!stat_obj.empty()) {
182 auto& image_spec = stat_obj.begin()->first;
184 auto pos = image_spec.find("/");
185 SpecPair pair{image_spec.substr(0, pos), ""};
186 if (pos != std::string::npos) {
187 pair.second = image_spec.substr(pos + 1);
190 const auto pool_it = json_to_internal_pools.find(pair.first);
191 if (pool_it == json_to_internal_pools.end()) {
195 image_stats->emplace_back(
196 pool_it->second.first, pool_it->second.second, pair.second);
198 auto& image_stat = image_stats->back();
199 auto& data = stat_obj.begin()->second.get_array();
200 for (auto& indexes : json_to_internal_stats) {
201 image_stat.stats[indexes.second] = data[indexes.first].get_real();
205 } catch (std::runtime_error &e) {
206 err_os << "rbd
: error parsing perf stats
: " << e.what() << std::endl;
213 void format_stat(StatDescriptor stat_descriptor, double stat,
215 switch (stat_descriptor) {
216 case StatDescriptor::WRITE_OPS:
217 case StatDescriptor::READ_OPS:
218 os << si_u_t(stat) << "/s
";
220 case StatDescriptor::WRITE_BYTES:
221 case StatDescriptor::READ_BYTES:
222 os << byte_u_t(stat) << "/s
";
224 case StatDescriptor::WRITE_LATENCY:
225 case StatDescriptor::READ_LATENCY:
226 os << std::fixed << std::setprecision(2);
227 if (stat >= 1000000000) {
228 os << (stat / 1000000000) << " s
";
229 } else if (stat >= 1000000) {
230 os << (stat / 1000000) << " ms
";
231 } else if (stat >= 1000) {
232 os << (stat / 1000) << " us
";
243 } // anonymous namespace
247 struct Iterations {};
249 void validate(boost::any& v, const std::vector<std::string>& values,
250 Iterations *target_type, int) {
251 po::validators::check_first_occurrence(v);
252 auto& s = po::validators::get_single_string(values);
255 auto iterations = boost::lexical_cast<uint32_t>(s);
256 if (iterations > 0) {
257 v = boost::any(iterations);
260 } catch (const boost::bad_lexical_cast &) {
262 throw po::validation_error(po::validation_error::invalid_option_value);
265 void format(const ImageStats& image_stats, Formatter* f, bool global_search) {
268 f->open_array_section("images
");
270 tbl.define_column("NAME
", TextTable::LEFT, TextTable::LEFT);
271 for (auto& stat : STAT_DESCRIPTORS.left) {
273 switch (stat.first) {
274 case StatDescriptor::WRITE_OPS:
277 case StatDescriptor::READ_OPS:
280 case StatDescriptor::WRITE_BYTES:
283 case StatDescriptor::READ_BYTES:
286 case StatDescriptor::WRITE_LATENCY:
289 case StatDescriptor::READ_LATENCY:
296 tbl.define_column(title, TextTable::RIGHT, TextTable::RIGHT);
300 for (auto& image_stat : image_stats) {
302 f->open_object_section("image
");
303 f->dump_string("pool
", image_stat.pool_name);
304 f->dump_string("pool_namespace
", image_stat.pool_namespace);
305 f->dump_string("image
", image_stat.image_name);
306 for (auto& pair : STAT_DESCRIPTORS.left) {
307 f->dump_float(pair.second.c_str(),
308 image_stat.stats[static_cast<size_t>(pair.first)]);
314 name += image_stat.pool_name + "/";
315 if (!image_stat.pool_namespace.empty()) {
316 name += image_stat.pool_namespace + "/";
319 name += image_stat.image_name;
322 for (auto& pair : STAT_DESCRIPTORS.left) {
323 std::stringstream str;
324 format_stat(pair.first,
325 image_stat.stats[static_cast<size_t>(pair.first)], str);
329 tbl << TextTable::endrow;
337 std::cout << tbl << std::endl;
341 } // namespace iostat
348 MainWindow(librados::Rados& rados, const std::string& pool_spec)
349 : m_rados(rados), m_pool_spec(pool_spec) {
354 keypad(stdscr, TRUE);
355 nodelay(stdscr, TRUE);
364 std::stringstream err_str;
366 r = query_iostats(m_rados, m_pool_spec, m_sort_by, &m_image_stats,
374 wait_for_key_or_delay();
377 if (ch == 'q' || ch == 'Q') {
379 } else if (ch == '<' || ch == KEY_LEFT) {
380 auto it = STAT_DESCRIPTORS.left.find(m_sort_by);
381 if (it != STAT_DESCRIPTORS.left.begin()) {
382 m_sort_by = (--it)->first;
384 } else if (ch == '>' || ch == KEY_RIGHT) {
385 auto it = STAT_DESCRIPTORS.left.find(m_sort_by);
386 if (it != STAT_DESCRIPTORS.left.end() &&
387 ++it != STAT_DESCRIPTORS.left.end()) {
388 m_sort_by = it->first;
396 std::cerr << err_str.str() << std::endl;
402 static const size_t STAT_COLUMN_WIDTH = 12;
404 librados::Rados& m_rados;
405 std::string m_pool_spec;
407 ImageStats m_image_stats;
408 StatDescriptor m_sort_by = StatDescriptor::WRITE_OPS;
410 bool m_pending_win_opened = false;
411 WINDOW* m_pending_win = nullptr;
416 std::map<StatDescriptor, std::string> m_columns;
418 void init_columns() {
420 for (auto& pair : STAT_DESCRIPTORS.left) {
422 switch (pair.first) {
423 case StatDescriptor::WRITE_OPS:
424 title = "WRITES OPS
";
426 case StatDescriptor::READ_OPS:
429 case StatDescriptor::WRITE_BYTES:
430 title = "WRITE BYTES
";
432 case StatDescriptor::READ_BYTES:
433 title = "READ BYTES
";
435 case StatDescriptor::WRITE_LATENCY:
438 case StatDescriptor::READ_LATENCY:
445 m_columns[pair.first] = (title);
450 getmaxyx(stdscr, m_height, m_width);
452 redraw_main_window();
453 redraw_pending_window();
458 void redraw_main_window() {
460 mvhline(0, 0, ' ' | A_REVERSE, m_width);
462 // print header for all metrics
463 int remaining_cols = m_width;
464 std::stringstream str;
465 for (auto& pair : m_columns) {
466 int attr = A_REVERSE;
468 if (pair.first == m_sort_by) {
474 title += pair.second;
477 str << std::right << std::setfill(' ')
478 << std::setw(STAT_COLUMN_WIDTH)
482 addstr(str.str().c_str());
483 remaining_cols -= title.size();
490 // print each image (one per line)
492 int remaining_lines = m_height - 1;
493 for (auto& image_stat : m_image_stats) {
494 if (remaining_lines <= 0) {
500 for (auto& pair : m_columns) {
502 format_stat(pair.first,
503 image_stat.stats[static_cast<size_t>(pair.first)], str);
504 auto value = str.str().substr(0, STAT_COLUMN_WIDTH);
507 str << std::right << std::setfill(' ')
508 << std::setw(STAT_COLUMN_WIDTH)
510 addstr(str.str().c_str());
514 if (m_pool_spec.empty()) {
515 image = format_pool_spec(image_stat.pool_name,
516 image_stat.pool_namespace) + "/";
518 image += image_stat.image_name;
519 addstr(image.substr(0, remaining_cols).c_str());
522 wnoutrefresh(stdscr);
525 void redraw_pending_window() {
526 // draw a "please by patient
" window while waiting
527 const char* msg = "Waiting
for initial stats
";
529 int width = strlen(msg) + 4;;
530 int starty = (m_height - height) / 2;
531 int startx = (m_width - width) / 2;
533 if (m_image_stats.empty() && !m_pending_win_opened) {
534 m_pending_win_opened = true;
535 m_pending_win = newwin(height, width, starty, startx);
538 if (m_pending_win != nullptr) {
539 if (m_image_stats.empty()) {
540 box(m_pending_win, 0 , 0);
541 mvwaddstr(m_pending_win, 2, 2, msg);
542 wnoutrefresh(m_pending_win);
544 delwin(m_pending_win);
545 m_pending_win = nullptr;
550 void wait_for_key_or_delay() {
553 FD_SET(STDIN_FILENO, &fds);
555 // no point to refreshing faster than the stats period
557 tval.tv_sec = std::min<uint32_t>(
558 10, g_conf().get_val<int64_t>("mgr_stats_period
"));
561 select(STDIN_FILENO + 1, &fds, NULL, NULL, &tval);
566 #endif // HAVE_CURSES
569 void get_arguments_iostat(po::options_description *positional,
570 po::options_description *options) {
571 at::add_pool_options(positional, options, true);
572 options->add_options()
573 ("iterations
", po::value<iostat::Iterations>(),
574 "iterations of metric collection
[> 0]")
575 ("sort
-by
", po::value<StatDescriptor>()->default_value(StatDescriptor::WRITE_OPS),
577 "(write
-ops
, read
-ops
, write
-bytes
, read
-bytes
, write
-latency
, read
-latency
) "
578 "[default: write
-ops
]");
579 at::add_format_options(options);
582 int execute_iostat(const po::variables_map &vm,
583 const std::vector<std::string> &ceph_global_init_args) {
585 std::string pool_namespace;
586 size_t arg_index = 0;
587 int r = utils::get_pool_and_namespace_names(vm, false, &pool,
588 &pool_namespace, &arg_index);
593 uint32_t iterations = 0;
594 if (vm.count("iterations
")) {
595 iterations = vm["iterations
"].as<uint32_t>();
597 auto sort_by = vm["sort
-by
"].as<StatDescriptor>();
599 at::Format::Formatter formatter;
600 r = utils::get_formatter(vm, &formatter);
605 auto f = formatter.get();
606 if (iterations > 1 && f != nullptr) {
607 std::cerr << "rbd
: specifing iterations is
not valid with formatted output
"
612 librados::Rados rados;
613 r = utils::init_rados(&rados);
618 r = rados.wait_for_latest_osdmap();
620 std::cerr << "rbd
: failed to retrieve OSD map
" << std::endl;
624 if (!pool_namespace.empty()) {
625 // default empty pool name only if namespace is specified to allow
626 // for an empty pool_spec (-> GLOBAL_POOL_KEY)
627 utils::normalize_pool_name(&pool);
629 std::string pool_spec = format_pool_spec(pool, pool_namespace);
631 // no point to refreshing faster than the stats period
632 auto delay = std::min<uint32_t>(10, g_conf().get_val<int64_t>("mgr_stats_period
"));
634 ImageStats image_stats;
636 bool printed_notice = false;
637 while (count++ < iterations || iterations == 0) {
638 r = query_iostats(rados, pool_spec, sort_by, &image_stats, std::cerr);
643 if (count == 1 && image_stats.empty()) {
645 if (!printed_notice) {
646 std::cerr << "rbd
: waiting
for initial image stats
"
647 << std::endl << std::endl;;
648 printed_notice = true;
651 iostat::format(image_stats, f, pool_spec.empty());
664 void get_arguments_iotop(po::options_description *positional,
665 po::options_description *options) {
666 at::add_pool_options(positional, options, true);
669 int execute_iotop(const po::variables_map &vm,
670 const std::vector<std::string> &ceph_global_init_args) {
672 std::string pool_namespace;
673 size_t arg_index = 0;
674 int r = utils::get_pool_and_namespace_names(vm, false, &pool,
675 &pool_namespace, &arg_index);
680 librados::Rados rados;
681 r = utils::init_rados(&rados);
686 r = rados.wait_for_latest_osdmap();
688 std::cerr << "rbd
: failed to retrieve OSD map
" << std::endl;
692 if (!pool_namespace.empty()) {
693 // default empty pool name only if namespace is specified to allow
694 // for an empty pool_spec (-> GLOBAL_POOL_KEY)
695 utils::normalize_pool_name(&pool);
697 iotop::MainWindow mainWindow(rados, format_pool_spec(pool, pool_namespace));
698 r = mainWindow.run();
706 Shell::Action top_action(
707 {"perf
", "image
", "iotop
"}, {}, "Display a top
-like IO monitor
.", "",
708 &get_arguments_iotop, &execute_iotop);
710 #endif // HAVE_CURSES
712 Shell::Action stat_action(
713 {"perf
", "image
", "iostat
"}, {}, "Display image IO statistics
.", "",
714 &get_arguments_iostat, &execute_iostat);
716 } // namespace action