From: dongdong tao Date: Mon, 22 Jan 2018 05:58:05 +0000 (+0800) Subject: mds: Enhance dump_tree performance by traversing the tree directly X-Git-Tag: v13.0.2~72^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=f5c0978fa399f56f14da2e49a98e6d4a425ef6c3;p=ceph.git mds: Enhance dump_tree performance by traversing the tree directly Signed-off-by: dongdong tao --- diff --git a/src/mds/MDCache.cc b/src/mds/MDCache.cc index de600c84f02b..fd53f51d6b45 100644 --- a/src/mds/MDCache.cc +++ b/src/mds/MDCache.cc @@ -11946,6 +11946,28 @@ int MDCache::cache_status(Formatter *f) return 0; } +void MDCache::dump_tree(CInode *in, const int cur_depth, const int max_depth, Formatter *f) +{ + assert(in); + if ((max_depth >= 0) && (cur_depth > max_depth)) { + return; + } + list ls; + in->get_dirfrags(ls); + for (const auto &subdir : ls) { + for (const auto &p : subdir->items) { + CDentry *dn = p.second; + CInode *in = dn->get_linkage()->get_inode(); + if (in) { + dump_tree(in, cur_depth + 1, max_depth, f); + } + } + } + f->open_object_section("inode"); + in->dump(f); + f->close_section(); +} + int MDCache::dump_cache(std::string_view file_name) { return dump_cache(file_name, NULL); @@ -11956,17 +11978,11 @@ int MDCache::dump_cache(Formatter *f) return dump_cache(std::string_view(""), f); } -int MDCache::dump_cache(std::string_view dump_root, int depth, Formatter *f) -{ - return dump_cache(std::string_view(""), f, dump_root, depth); -} - /** * Dump the metadata cache, either to a Formatter, if * provided, else to a plain text file. */ -int MDCache::dump_cache(std::string_view fn, Formatter *f, - std::string_view dump_root, int depth) +int MDCache::dump_cache(const char *fn, Formatter *f) { int r = 0; int fd = -1; @@ -11990,24 +12006,8 @@ int MDCache::dump_cache(std::string_view fn, Formatter *f, } } - auto dump_func = [fd, f, depth, &dump_root](CInode *in) { + auto dump_func = [fd, f](CInode *in) { int r; - if (!dump_root.empty()) { - string ipath; - if (in->is_root()) - ipath = "/"; - else - in->make_path_string(ipath); - - if (dump_root.length() > ipath.length() || - !equal(dump_root.begin(), dump_root.end(), ipath.begin())) - return 0; - - if (depth >= 0 && - count(ipath.begin() + dump_root.length(), ipath.end(), '/') > depth) - return 0; - } - if (f) { f->open_object_section("inode"); in->dump(f); diff --git a/src/mds/MDCache.h b/src/mds/MDCache.h index bc551a15c68e..9a8c1b9456f4 100644 --- a/src/mds/MDCache.h +++ b/src/mds/MDCache.h @@ -1172,14 +1172,12 @@ public: void discard_delayed_expire(CDir *dir); protected: - int dump_cache(std::string_view fn, Formatter *f, - std::string_view dump_root = "", - int depth = -1); + int dump_cache(const char *fn, Formatter *f); public: int dump_cache() { return dump_cache(NULL, NULL); } int dump_cache(std::string_view filename); int dump_cache(Formatter *f); - int dump_cache(std::string_view dump_root, int depth, Formatter *f); + void dump_tree(CInode *in, const int cur_depth, const int max_depth, Formatter *f); int cache_status(Formatter *f); diff --git a/src/mds/MDSRank.cc b/src/mds/MDSRank.cc index 91d4e31d3410..e8d2d9e53731 100644 --- a/src/mds/MDSRank.cc +++ b/src/mds/MDSRank.cc @@ -1971,19 +1971,7 @@ bool MDSRankDispatcher::handle_asok_command( ss << "Failed to get cache status: " << cpp_strerror(r); } } else if (command == "dump tree") { - string root; - int64_t depth; - cmd_getval(g_ceph_context, cmdmap, "root", root); - if (!cmd_getval(g_ceph_context, cmdmap, "depth", depth)) - depth = -1; - { - Mutex::Locker l(mds_lock); - int r = mdcache->dump_cache(root, depth, f); - if (r != 0) { - ss << "Failed to dump tree: " << cpp_strerror(r); - f->reset(); - } - } + command_dump_tree(cmdmap, ss, f); } else if (command == "dump loads") { Mutex::Locker l(mds_lock); int r = balancer->dump_loads(f); @@ -2359,6 +2347,24 @@ int MDSRank::_command_export_dir( return 0; } +void MDSRank::command_dump_tree(const cmdmap_t &cmdmap, std::ostream &ss, Formatter *f) +{ + std::string root; + int64_t depth; + cmd_getval(g_ceph_context, cmdmap, "root", root); + if (!cmd_getval(g_ceph_context, cmdmap, "depth", depth)) + depth = -1; + Mutex::Locker l(mds_lock); + CInode *in = mdcache->cache_traverse(filepath(root.c_str())); + if (!in) { + ss << "root inode is not in cache"; + return; + } + f->open_array_section("inodes"); + mdcache->dump_tree(in, 0, depth, f); + f->close_section(); +} + CDir *MDSRank::_command_dirfrag_get( const cmdmap_t &cmdmap, std::ostream &ss) diff --git a/src/mds/MDSRank.h b/src/mds/MDSRank.h index 39e09c7b0c0b..d29eceb986b1 100644 --- a/src/mds/MDSRank.h +++ b/src/mds/MDSRank.h @@ -441,6 +441,7 @@ class MDSRank { const cmdmap_t &cmdmap, std::ostream &ss); void command_openfiles_ls(Formatter *f); + void command_dump_tree(const cmdmap_t &cmdmap, std::ostream &ss, Formatter *f); protected: Messenger *messenger;