From 4c90cee20473aa51c04731f4ef2d286b2a2abf44 Mon Sep 17 00:00:00 2001 From: xie xingguo Date: Tue, 18 Jun 2019 16:06:52 +0800 Subject: [PATCH] osd/OSDMap: pass filter directly to the OSDUtilizationDumper This makes the code easy to maintain and especially to extend, e.g., adding per pool filters. Signed-off-by: xie xingguo --- src/mgr/DaemonServer.cc | 8 +------ src/osd/OSDMap.cc | 47 ++++++++++++++++++----------------------- src/osd/OSDMap.h | 3 +-- 3 files changed, 22 insertions(+), 36 deletions(-) diff --git a/src/mgr/DaemonServer.cc b/src/mgr/DaemonServer.cc index 6853a62d70c..6a63aca1424 100644 --- a/src/mgr/DaemonServer.cc +++ b/src/mgr/DaemonServer.cc @@ -1228,27 +1228,21 @@ bool DaemonServer::_handle_command( } stringstream rs; r = cluster_state.with_osdmap_and_pgmap([&](const OSDMap& osdmap, const PGMap& pgmap) { - string class_name; - string item_name; // sanity check filter(s) if (filter_by == "class") { if (!osdmap.crush->class_exists(filter)) { rs << "specified class '" << filter << "' does not exist"; return -EINVAL; } - class_name = filter; } if (filter_by == "name") { if (!osdmap.crush->name_exists(filter)) { rs << "specified name '" << filter << "' does not exist"; return -EINVAL; } - item_name = filter; } print_osd_utilization(osdmap, pgmap, ss, - f.get(), method == "tree", - class_name, item_name); - + f.get(), method == "tree", filter); cmdctx->odata.append(ss); return 0; }); diff --git a/src/osd/OSDMap.cc b/src/osd/OSDMap.cc index e2d839df291..e276534708f 100644 --- a/src/osd/OSDMap.cc +++ b/src/osd/OSDMap.cc @@ -4974,23 +4974,23 @@ public: OSDUtilizationDumper(const CrushWrapper *crush, const OSDMap *osdmap_, const PGMap& pgmap_, bool tree_, - const string& class_name_, - const string& item_name_) : + const string& filter) : Parent(crush, osdmap_->get_pool_names()), osdmap(osdmap_), pgmap(pgmap_), tree(tree_), - class_name(class_name_), - item_name(item_name_), min_var(-1), max_var(-1), stddev(0), sum(0) { - if (osdmap->crush->name_exists(item_name)) { - // filter out items we are allowed to dump - auto item_id = osdmap->crush->get_item_id(item_name); + if (osdmap->crush->name_exists(filter)) { + // filter by crush node + auto item_id = osdmap->crush->get_item_id(filter); allowed.insert(item_id); osdmap->crush->get_all_children(item_id, &allowed); + } else if (osdmap->crush->class_exists(filter)) { + // filter by device class + class_id = osdmap->crush->get_class_id(filter); } average_util = average_utilization(); } @@ -5000,18 +5000,17 @@ protected: bool should_dump(int id) const { if (!allowed.empty() && !allowed.count(id)) // filter by name return false; - if (id >= 0 && !class_name.empty()) { - const char* item_class_name = osdmap->crush->get_item_class(id); - if (!item_class_name || // not bound to a class yet - item_class_name != class_name) // or already bound to - // a different class + if (id >= 0 && class_id >= 0) { + auto item_class_id = osdmap->crush->get_item_class_id(id); + if (item_class_id < 0 || // not bound to a class yet + item_class_id != class_id) // or already bound to a different class return false; } return true; } set get_dumped_osds() { - if (class_name.empty() && item_name.empty()) { + if (allowed.empty() && class_id < 0) { // old way, all return {}; } @@ -5165,13 +5164,12 @@ protected: const OSDMap *osdmap; const PGMap& pgmap; bool tree; - const string class_name; - const string item_name; double average_util; double min_var; double max_var; double stddev; double sum; + int class_id = -1; set allowed; set dumped_osds; }; @@ -5183,9 +5181,8 @@ public: OSDUtilizationPlainDumper(const CrushWrapper *crush, const OSDMap *osdmap, const PGMap& pgmap, bool tree, - const string& class_name, - const string& item_name) : - Parent(crush, osdmap, pgmap, tree, class_name, item_name) {} + const string& filter) : + Parent(crush, osdmap, pgmap, tree, filter) {} void dump(TextTable *tbl) { tbl->define_column("ID", TextTable::LEFT, TextTable::RIGHT); @@ -5320,9 +5317,8 @@ public: OSDUtilizationFormatDumper(const CrushWrapper *crush, const OSDMap *osdmap, const PGMap& pgmap, bool tree, - const string& class_name, - const string& item_name) : - Parent(crush, osdmap, pgmap, tree, class_name, item_name) {} + const string& filter) : + Parent(crush, osdmap, pgmap, tree, filter) {} void dump(Formatter *f) { f->open_array_section("nodes"); @@ -5398,21 +5394,18 @@ void print_osd_utilization(const OSDMap& osdmap, ostream& out, Formatter *f, bool tree, - const string& class_name, - const string& item_name) + const string& filter) { const CrushWrapper *crush = osdmap.crush.get(); if (f) { f->open_object_section("df"); - OSDUtilizationFormatDumper d(crush, &osdmap, pgmap, tree, - class_name, item_name); + OSDUtilizationFormatDumper d(crush, &osdmap, pgmap, tree, filter); d.dump(f); d.summary(f); f->close_section(); f->flush(out); } else { - OSDUtilizationPlainDumper d(crush, &osdmap, pgmap, tree, - class_name, item_name); + OSDUtilizationPlainDumper d(crush, &osdmap, pgmap, tree, filter); TextTable tbl; d.dump(&tbl); out << tbl << d.summary() << "\n"; diff --git a/src/osd/OSDMap.h b/src/osd/OSDMap.h index de8f87353ea..fd227a99a0c 100644 --- a/src/osd/OSDMap.h +++ b/src/osd/OSDMap.h @@ -1544,7 +1544,6 @@ void print_osd_utilization(const OSDMap& osdmap, std::ostream& out, ceph::Formatter *f, bool tree, - const std::string& class_name, - const std::string& item_name); + const std::string& filter); #endif -- 2.39.5