From 3c7e60a4d336720751a21696683d299d8cd191a8 Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Thu, 21 Aug 2025 17:21:48 +0530 Subject: [PATCH] mds/CDentry: shorten path before logging Printing full path in logs not only is unuseful but also reduces the readability of logs (image path with 2000 components). Therefore, print only 10 final components of the path. Signed-off-by: Rishabh Dave --- src/include/filepath.cc | 8 ++++++++ src/include/filepath.h | 4 ++++ src/mds/CDentry.cc | 22 +++++++++++++++++++--- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/include/filepath.cc b/src/include/filepath.cc index 3844945ea47..ce2c29ea674 100644 --- a/src/include/filepath.cc +++ b/src/include/filepath.cc @@ -41,6 +41,14 @@ void filepath::parse_bits() const { } } +void filepath::set_trimmed() { + if (trimmed) + return; + // indicates that the path has been shortened. + path += "..."; + trimmed = true; +} + void filepath::set_path(std::string_view s) { if (!s.empty() && s[0] == '/') { path = s.substr(1); diff --git a/src/include/filepath.h b/src/include/filepath.h index 67be4a08759..920ffcb95cb 100644 --- a/src/include/filepath.h +++ b/src/include/filepath.h @@ -38,6 +38,9 @@ namespace ceph { class Formatter; } class filepath { inodeno_t ino = 0; // base inode. ino=0 implies pure relative path. std::string path; // relative path. + // tells get_path() whether it should prefix path with "..." to indicate that + // it was shortened. + bool trimmed = false; /** bits - path segments * this is ['a', 'b', 'c'] for both the aboslute and relative case. @@ -82,6 +85,7 @@ class filepath { // accessors inodeno_t get_ino() const { return ino; } const std::string& get_path() const { return path; } + void set_trimmed(); const char *c_str() const { return path.c_str(); } int length() const { return path.length(); } diff --git a/src/mds/CDentry.cc b/src/mds/CDentry.cc index d373f82aa22..cc519fdcc50 100644 --- a/src/mds/CDentry.cc +++ b/src/mds/CDentry.cc @@ -313,9 +313,25 @@ void CDentry::make_path_string(string& s, bool projected) const void CDentry::make_path(filepath& fp, bool projected) const { - ceph_assert(dir); - dir->inode->make_path(fp, projected); - fp.push_dentry(get_name()); + /* path_comp_count = path components count + * + * Printing more than 10 components of a path not only is not useful but also it + * makes reading logs harder (imagine path with 2000 components). Therefore, + * shorten path. + */ + static int path_comp_count = 1; + fp.set_trimmed(); + + if (path_comp_count <= 10) { + ceph_assert(dir); + ++path_comp_count; + dir->inode->make_path(fp, projected); + fp.push_dentry(get_name()); + } else { + // resetting the counter + path_comp_count = 1; + return; + } } /* -- 2.39.5