From 30e81d58f82a9f0e9f8a9746812ed1130268fbdf Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Sat, 27 Mar 2021 13:38:54 +0100 Subject: [PATCH] shmmonitor: allow getting shmids based on session/userid --- fairmq/shmem/Common.h | 9 +++++++-- fairmq/shmem/runMonitor.cxx | 19 +++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/fairmq/shmem/Common.h b/fairmq/shmem/Common.h index 6f0109ac..a8a49b58 100644 --- a/fairmq/shmem/Common.h +++ b/fairmq/shmem/Common.h @@ -195,9 +195,9 @@ struct RegionBlock // find id for unique shmem name: // a hash of user id + session id, truncated to 8 characters (to accommodate for name size limit on some systems (MacOS)). -inline std::string makeShmIdStr(const std::string& sessionId) +inline std::string makeShmIdStr(const std::string& sessionId, const std::string& userId) { - std::string seed((std::to_string(geteuid()) + sessionId)); + std::string seed(userId + sessionId); // generate a 8-digit hex value out of sha256 hash std::vector hash(4); picosha2::hash256(seed.begin(), seed.end(), hash.begin(), hash.end()); @@ -205,6 +205,11 @@ inline std::string makeShmIdStr(const std::string& sessionId) return picosha2::bytes_to_hex_string(hash.begin(), hash.end()); } +inline std::string makeShmIdStr(const std::string& sessionId) +{ + return makeShmIdStr(sessionId, std::to_string(geteuid())); +} + inline uint64_t makeShmIdUint64(const std::string& sessionId) { std::string shmId = makeShmIdStr(sessionId); diff --git a/fairmq/shmem/runMonitor.cxx b/fairmq/shmem/runMonitor.cxx index a128becd..f3cfdd8d 100644 --- a/fairmq/shmem/runMonitor.cxx +++ b/fairmq/shmem/runMonitor.cxx @@ -80,6 +80,8 @@ int main(int argc, char** argv) bool runAsDaemon = false; bool debug = false; bool cleanOnExit = false; + bool getShmId = false; + int userId = -1; options_description desc("Options"); desc.add_options() @@ -91,9 +93,11 @@ int main(int argc, char** argv) ("view,v" , value(&viewOnly)->implicit_value(true), "Run in view only mode") ("timeout,t" , value(&timeoutInMS)->default_value(5000), "Heartbeat timeout in milliseconds") ("daemonize,d" , value(&runAsDaemon)->implicit_value(true), "Daemonize the monitor") - ("debug,b" , value(&debug)->implicit_value(true), "Debug - Print a list of messages)") + ("debug,b" , value(&debug)->implicit_value(true), "Debug - Print a list of messages)") ("clean-on-exit,e", value(&cleanOnExit)->implicit_value(true), "Perform cleanup on exit") ("interval" , value(&intervalInMS)->default_value(100), "Output interval for interactive/view-only 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)") + ("user-id" , value(&userId)->default_value(-1), "User id") ("help,h", "Print help"); variables_map vm; @@ -110,7 +114,18 @@ int main(int argc, char** argv) daemonize(); } - if (shmId == "") { + if (getShmId) { + if (userId == -1) { + cout << "shmem id for session '" << sessionName << "' and current user id " << geteuid() + << " is: " << makeShmIdStr(sessionName) << endl; + } else { + cout << "shmem id for session '" << sessionName << "' and user id " << userId + << " is: " << makeShmIdStr(sessionName, to_string(userId)) << endl; + } + return 0; + } + + if (shmId.empty()) { shmId = makeShmIdStr(sessionName); }