From: Igor Golikov Date: Mon, 9 Feb 2026 13:14:07 +0000 (+0000) Subject: mds: move function to generic locationsrc/common/util.cc X-Git-Tag: testing/wip-vshankar-testing-20260222.101816~3^2~6 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=31942c42950dc8ce3a7befa6db65495144165db9;p=ceph-ci.git mds: move function to generic locationsrc/common/util.cc Signed-off-by: Igor Golikov --- diff --git a/src/common/util.cc b/src/common/util.cc index 07cf882baee..acd1af97f25 100644 --- a/src/common/util.cc +++ b/src/common/util.cc @@ -17,9 +17,13 @@ #include #endif +#include #include +#include +#include #include +#include "acconfig.h" #include "include/compat.h" #include "include/util.h" #include "common/debug.h" @@ -459,3 +463,43 @@ std::string bytes2str(uint64_t count) { snprintf(str, sizeof str, "%" PRIu64 "%sB", count, s[i]); return std::string(str); } + +#ifndef _WIN32 +bool ceph::read_process_cpu_ticks(uint64_t* total, ceph::proc_stat_error* error) +{ + ceph_assert(total != nullptr); + if (error) { + *error = ceph::proc_stat_error::none; + } + const char* stat_path = PROCPREFIX "/proc/self/stat"; + std::ifstream stat_file(stat_path); + if (!stat_file.is_open()) { + if (error) { + *error = ceph::proc_stat_error::not_found; + } + return false; + } + + std::vector stat_vec((std::istream_iterator{stat_file}), + std::istream_iterator()); + if (stat_vec.size() < 15) { + if (error) { + *error = ceph::proc_stat_error::not_resolvable; + } + return false; + } + + uint64_t utime = std::strtoull(stat_vec[13].c_str(), nullptr, 10); + uint64_t stime = std::strtoull(stat_vec[14].c_str(), nullptr, 10); + *total = utime + stime; + return true; +} +#else +bool ceph::read_process_cpu_ticks(uint64_t* total, ceph::proc_stat_error* error) +{ + if (error) { + *error = ceph::proc_stat_error::not_found; + } + return false; +} +#endif diff --git a/src/include/util.h b/src/include/util.h index b29a031047c..cebdad9e8fe 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -110,7 +110,18 @@ void dump_services(ceph::Formatter* f, const std::map @@ -119,5 +130,6 @@ bool match_str(const std::string& s, const XS& ...xs) return ((s == xs) || ...); } -} // namespace ceph::util +} // namespace util +} // namespace ceph #endif /* CEPH_UTIL_H */ diff --git a/src/mds/MDBalancer.cc b/src/mds/MDBalancer.cc index c9b6b43d692..18c5d663381 100644 --- a/src/mds/MDBalancer.cc +++ b/src/mds/MDBalancer.cc @@ -40,6 +40,7 @@ using namespace std; #include "common/config.h" #include "common/debug.h" #include "common/errno.h" +#include "include/util.h" /* Note, by default debug_mds_balancer is 1/5. For debug messages 1 stat_vec((std::istream_iterator{stat_file}), - std::istream_iterator()); - if (stat_vec.size() < 15) { - if (error) { - *error = proc_stat_error::not_resolvable; - } - return false; - } - - uint64_t utime = std::strtoull(stat_vec[13].c_str(), nullptr, 10); - uint64_t stime = std::strtoull(stat_vec[14].c_str(), nullptr, 10); - *total = utime + stime; - return true; -} -} - class C_Flush_Journal : public MDSInternalContext { public: C_Flush_Journal(MDCache *mdcache, MDLog *mdlog, MDSRank *mds, diff --git a/src/mds/MDSRank.h b/src/mds/MDSRank.h index 1ed37f479c2..82657dccb90 100644 --- a/src/mds/MDSRank.h +++ b/src/mds/MDSRank.h @@ -127,15 +127,6 @@ enum { l_mdm_last, }; -namespace ceph::mds { -enum class proc_stat_error { - none, - not_found, - not_resolvable -}; -bool read_process_cpu_ticks(uint64_t* total, proc_stat_error* error = nullptr); -} - namespace ceph { struct heartbeat_handle_d; } diff --git a/src/mds/MetricsHandler.cc b/src/mds/MetricsHandler.cc index 2ca10f5a148..ffb996b3f21 100644 --- a/src/mds/MetricsHandler.cc +++ b/src/mds/MetricsHandler.cc @@ -15,6 +15,7 @@ #include "common/errno.h" #include "common/perf_counters.h" #include "common/perf_counters_key.h" +#include "include/util.h" #include "include/fs_types.h" #include "include/stringify.h" @@ -671,7 +672,7 @@ void MetricsHandler::maybe_update_subvolume_quota(inodeno_t subvol_id, uint64_t void MetricsHandler::sample_cpu_usage() { uint64_t current_ticks = 0; - ceph::mds::proc_stat_error err; + ceph::proc_stat_error err; if (clk_tck <= 0) { rank_telemetry.metrics.cpu_usage_percent = 0; @@ -681,15 +682,15 @@ void MetricsHandler::sample_cpu_usage() { return; } - if (!ceph::mds::read_process_cpu_ticks(¤t_ticks, &err)) { + if (!ceph::read_process_cpu_ticks(¤t_ticks, &err)) { rank_telemetry.metrics.cpu_usage_percent = 0; if (rank_perf_counters) { rank_perf_counters->set(l_mds_rank_perf_cpu_usage, 0); } constexpr const char* stat_path = PROCPREFIX "/proc/self/stat"; - if (err == ceph::mds::proc_stat_error::not_resolvable) { + if (err == ceph::proc_stat_error::not_resolvable) { dout(5) << "input file '" << stat_path << "' not resolvable" << dendl; - } else if (err == ceph::mds::proc_stat_error::not_found) { + } else if (err == ceph::proc_stat_error::not_found) { dout(5) << "input file '" << stat_path << "' not found" << dendl; } return;