From 5facc441b8e47dedb494a0da08a89d81e0df99b8 Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Wed, 7 Apr 2021 09:12:33 +0200 Subject: [PATCH] shmmonitor: add --list-all --- fairmq/shmem/Monitor.cxx | 30 ++++++++++++++++++++++++++++++ fairmq/shmem/Monitor.h | 1 + fairmq/shmem/runMonitor.cxx | 9 +++++++++ 3 files changed, 40 insertions(+) diff --git a/fairmq/shmem/Monitor.cxx b/fairmq/shmem/Monitor.cxx index 72371cc7..ccc30b2e 100644 --- a/fairmq/shmem/Monitor.cxx +++ b/fairmq/shmem/Monitor.cxx @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -271,6 +272,35 @@ bool Monitor::PrintShm(const ShmId& shmId) return true; } +void Monitor::ListAll(const std::string& path) +{ + try { + if (std::filesystem::is_empty(path)) { + LOG(info) << "directory " << filesystem::path(path) << " is empty."; + return; + } + + for (const auto& entry : filesystem::directory_iterator(path)) { + string filename = entry.path().filename().string(); + // LOG(info) << filename << ", size: " << entry.file_size() << " bytes"; + if (tools::StrStartsWith(filename, "fmq_") || tools::StrStartsWith(filename, "sem.fmq_")) { + // LOG(info) << "The file '" << filename << "' belongs to FairMQ."; + if (tools::StrEndsWith(filename, "_mng")) { + string shmId = filename.substr(4, 8); + LOG(info) << "\nFound shmid '" << shmId << "':\n"; + if (!PrintShm(ShmId{shmId})) { + LOG(info) << "could not open file for shmid '" << shmId << "'"; + } + } + } else { + LOG(info) << "The file '" << filename << "' does not belong to FairMQ, skipping..."; + } + } + } catch (filesystem::filesystem_error& fse) { + LOG(error) << "error: " << fse.what(); + } +} + void Monitor::ReceiveHeartbeats() { try { diff --git a/fairmq/shmem/Monitor.h b/fairmq/shmem/Monitor.h index b6d8b163..3d571705 100644 --- a/fairmq/shmem/Monitor.h +++ b/fairmq/shmem/Monitor.h @@ -85,6 +85,7 @@ class Monitor static std::unordered_map> GetDebugInfo(const SessionId& shmId); static bool PrintShm(const ShmId& shmId); + static void ListAll(const std::string& path); static bool RemoveObject(const std::string& name); static bool RemoveFileMapping(const std::string& name); diff --git a/fairmq/shmem/runMonitor.cxx b/fairmq/shmem/runMonitor.cxx index bc197782..f221699e 100644 --- a/fairmq/shmem/runMonitor.cxx +++ b/fairmq/shmem/runMonitor.cxx @@ -86,6 +86,8 @@ int main(int argc, char** argv) bool debug = false; bool cleanOnExit = false; bool getShmId = false; + bool listAll = false; + string listAllPath; bool verbose = false; int userId = -1; @@ -104,6 +106,8 @@ int main(int argc, char** argv) ("clean-on-exit,e", value(&cleanOnExit)->implicit_value(true), "Perform cleanup on exit") ("interval" , value(&intervalInMS)->default_value(100), "Output interval for interactive mode") ("get-shmid" , value(&getShmId)->implicit_value(true), "Translate given session id and user id to a shmem id (uses current user id if none provided)") + ("list-all" , value(&listAll)->implicit_value(true), "List all sessions & segments") + ("list-all-path" , value(&listAllPath)->default_value("/dev/shm/"),"Path for the --list-all command to search segments in") ("verbose" , value(&verbose)->implicit_value(true), "Verbose mode (daemon will output to a file 'fairmq-shmmonitor_log_')") ("user-id" , value(&userId)->default_value(-1), "User id (used with --get-shmid)") ("help,h", "Print help"); @@ -150,6 +154,11 @@ int main(int argc, char** argv) return 0; } + if (listAll) { + Monitor::ListAll(listAllPath); + return 0; + } + if (!viewOnly && !interactive && !monitor) { // if neither of the run modes are selected, use view only mode. viewOnly = true;