diff --git a/package/src/countmon.cpp b/package/src/countmon.cpp index 599144b..6f4cb9e 100644 --- a/package/src/countmon.cpp +++ b/package/src/countmon.cpp @@ -47,6 +47,7 @@ void countmon::update_stats(const std::vector& pids, count_stat_update["nprocs"] += 1L; count_stat_update["nthreads"] += std::stol(stat_entries[prmon::num_threads]); + count_stat_update["nfds"] += prmon::count_fds(pid, read_path); } stat_entries.clear(); } diff --git a/package/src/countmon.h b/package/src/countmon.h index 709b5bf..7cec9ac 100644 --- a/package/src/countmon.h +++ b/package/src/countmon.h @@ -21,7 +21,8 @@ class countmon final : public Imonitor, public MessageBase { private: // Setup the parameters to monitor here const prmon::parameter_list params = {{"nprocs", "1", "1"}, - {"nthreads", "1", "1"}}; + {"nthreads", "1", "1"}, + {"nfds", "1", "1"}}; // Dynamic monitoring container for value measurements // This will be filled at initialisation, taking the names diff --git a/package/src/utils.cpp b/package/src/utils.cpp index 50d6065..c2644fa 100644 --- a/package/src/utils.cpp +++ b/package/src/utils.cpp @@ -9,9 +9,11 @@ #include #include #include +#include #include #include +#include const std::pair> prmon::cmd_pipe_output( const std::vector cmdargs) { @@ -115,3 +117,19 @@ unsigned int prmon::parse_uint_field(const std::string& s) { if (s == "-" || s.empty()) return 0; return std::stoul(s); } + +int prmon::count_fds(pid_t pid, const std::string& read_path) { + std::stringstream fd_path; + fd_path << read_path << "/proc/" << pid << "/fd"; + DIR* dir = opendir(fd_path.str().c_str()); + if (!dir) return 0; + int count = 0; + struct dirent* entry; + while ((entry = readdir(dir)) != nullptr) { + if (entry->d_name[0] != '.') { + count++; + } + } + closedir(dir); + return count; +} diff --git a/package/src/utils.h b/package/src/utils.h index 0b08869..03d3a6d 100644 --- a/package/src/utils.h +++ b/package/src/utils.h @@ -58,6 +58,9 @@ const bool smaps_rollup_exists(); // Utility function to parse a string to uint unsigned int parse_uint_field(const std::string& s); + +// Utility function to count the number of file descriptors for a PID +int count_fds(pid_t pid, const std::string& read_path); } // namespace prmon #endif // PRMON_UTILS_H