Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package/src/countmon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void countmon::update_stats(const std::vector<pid_t>& 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();
}
Expand Down
3 changes: 2 additions & 1 deletion package/src/countmon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions package/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <dirent.h>

#include <iostream>
#include <string>
#include <sstream>

const std::pair<int, std::vector<std::string>> prmon::cmd_pipe_output(
const std::vector<std::string> cmdargs) {
Expand Down Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions package/src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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