]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mds: move function to generic locationsrc/common/util.cc
authorIgor Golikov <igolikov@redhat.com>
Mon, 9 Feb 2026 13:14:07 +0000 (13:14 +0000)
committerIgor Golikov <igolikov@redhat.com>
Mon, 9 Feb 2026 13:15:04 +0000 (13:15 +0000)
Signed-off-by: Igor Golikov <igolikov@redhat.com>
src/common/util.cc
src/include/util.h
src/mds/MDBalancer.cc
src/mds/MDSRank.cc
src/mds/MDSRank.h
src/mds/MetricsHandler.cc

index 07cf882baee4f788ed81bd543c831ee6d98fa1fa..acd1af97f25d63e4d536dee6e81e6dc5bd01faa8 100644 (file)
 #include <sys/utsname.h>
 #endif
 
+#include <cstdlib>
 #include <fstream>
+#include <iterator>
+#include <vector>
 #include <boost/algorithm/string.hpp>
 
+#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<std::string> stat_vec((std::istream_iterator<std::string>{stat_file}),
+                                    std::istream_iterator<std::string>());
+  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
index b29a031047c51d12eca230532576f0b6d9a88016..cebdad9e8fe2229f4b7163cb521269861070ba09 100644 (file)
@@ -110,7 +110,18 @@ void dump_services(ceph::Formatter* f, const std::map<std::string,
 std::string cleanbin(ceph::buffer::list &bl, bool &b64, bool show = false);
 std::string cleanbin(std::string &str);
 
-namespace ceph::util {
+namespace ceph {
+
+enum class proc_stat_error {
+  none,
+  not_found,
+  not_resolvable
+};
+
+/// Read user+system CPU ticks for the current process from /proc/self/stat
+bool read_process_cpu_ticks(uint64_t* total, proc_stat_error* error = nullptr);
+
+namespace util {
 
 // Returns true if s matches any parameters:
 template <typename ...XS>
@@ -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 */
index c9b6b43d692ededb0b9ca07e28f6eb1d9e08b098..18c5d66338195c80e3f3e5d12151be23c3a60da8 100644 (file)
@@ -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<lvl<=5,
  * should_gather (below) will be true; so, debug_mds will be ignored even if
@@ -360,14 +361,14 @@ mds_load_t MDBalancer::get_load()
   uint64_t cpu_time = 1;
   {
     uint64_t ticks = 0;
-    ceph::mds::proc_stat_error err;
-    if (ceph::mds::read_process_cpu_ticks(&ticks, &err)) {
+    ceph::proc_stat_error err;
+    if (ceph::read_process_cpu_ticks(&ticks, &err)) {
       cpu_time = ticks;
     } else {
       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) {
         derr << "input file '" << stat_path << "' not resolvable" << dendl_impl;
-      } else if (err == ceph::mds::proc_stat_error::not_found) {
+      } else if (err == ceph::proc_stat_error::not_found) {
         derr << "input file '" << stat_path << "' not found" << dendl_impl;
       }
     }
index dafe8f0f7e837ecf652ef3e6a57a5be68ac61d60..5a5a4c33fe5c7b863ac0bdf39e3150ef9b068bbc 100644 (file)
@@ -77,38 +77,6 @@ using std::vector;
 using TOPNSPC::common::cmd_getval;
 using TOPNSPC::common::cmd_getval_or;
 
-namespace ceph::mds {
-bool read_process_cpu_ticks(uint64_t* total, proc_stat_error* error)
-{
-  ceph_assert(total != nullptr);
-  if (error) {
-    *error = 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 = proc_stat_error::not_found;
-    }
-    return false;
-  }
-
-  std::vector<std::string> stat_vec((std::istream_iterator<std::string>{stat_file}),
-                                    std::istream_iterator<std::string>());
-  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,
index 1ed37f479c2676d6ef00ce2bd7323002d1e92fe4..82657dccb901afb758466ab0cbc7fadac07cc236 100644 (file)
@@ -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;
 }
index 2ca10f5a1486036ba6f44206a433af3bb00a6b53..ffb996b3f211ffbfec4d1a67a66eb55862ff6f38 100644 (file)
@@ -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(&current_ticks, &err)) {
+  if (!ceph::read_process_cpu_ticks(&current_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;