From b0f73017e2fc0563331e76573df1ce08571c1a80 Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Thu, 6 Aug 2020 10:08:11 +0200 Subject: [PATCH] shmmonitor: add output with -v (non-interactive) --- fairmq/shmem/Manager.h | 20 +++++++++++++++----- fairmq/shmem/Monitor.cxx | 27 ++++++++++++++++++++++----- fairmq/shmem/Monitor.h | 3 ++- fairmq/shmem/runMonitor.cxx | 4 +++- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/fairmq/shmem/Manager.h b/fairmq/shmem/Manager.h index 0ab5f442..b923b11a 100644 --- a/fairmq/shmem/Manager.h +++ b/fairmq/shmem/Manager.h @@ -25,13 +25,15 @@ #include #include -#include -#include #include +#include +#include +#include #include #include #include -#include +#include +#include #include // getenv #include @@ -55,6 +57,13 @@ namespace shmem struct SharedMemoryError : std::runtime_error { using std::runtime_error::runtime_error; }; +using SimpleSeqFitSegment = boost::interprocess::basic_managed_shared_memory, + boost::interprocess::iset_index>; +using RBTreeBestFitSegment = boost::interprocess::basic_managed_shared_memory, + boost::interprocess::iset_index>; + class Manager { public: @@ -133,7 +142,7 @@ class Manager Manager(const Manager&) = delete; Manager operator=(const Manager&) = delete; - boost::interprocess::managed_shared_memory& Segment() { return fSegment; } + RBTreeBestFitSegment& Segment() { return fSegment; } boost::interprocess::managed_shared_memory& ManagementSegment() { return fManagementSegment; } static void StartMonitor(const std::string& id) @@ -446,7 +455,8 @@ class Manager private: std::string fShmId; std::string fDeviceId; - boost::interprocess::managed_shared_memory fSegment; + // boost::interprocess::managed_shared_memory fSegment; + RBTreeBestFitSegment fSegment; boost::interprocess::managed_shared_memory fManagementSegment; VoidAlloc fShmVoidAlloc; boost::interprocess::named_mutex fShmMtx; diff --git a/fairmq/shmem/Monitor.cxx b/fairmq/shmem/Monitor.cxx index b3a58b05..2a12bc53 100644 --- a/fairmq/shmem/Monitor.cxx +++ b/fairmq/shmem/Monitor.cxx @@ -22,6 +22,10 @@ #include #include #include +#include +#include +#include +#include #include #include @@ -48,7 +52,7 @@ void signalHandler(int signal) gSignalStatus = signal; } -Monitor::Monitor(const string& shmId, bool selfDestruct, bool interactive, bool viewOnly, unsigned int timeoutInMS, bool runAsDaemon, bool cleanOnExit) +Monitor::Monitor(const string& shmId, bool selfDestruct, bool interactive, bool viewOnly, unsigned int timeoutInMS, unsigned int intervalInMS, bool runAsDaemon, bool cleanOnExit) : fSelfDestruct(selfDestruct) , fInteractive(interactive) , fViewOnly(viewOnly) @@ -56,6 +60,7 @@ Monitor::Monitor(const string& shmId, bool selfDestruct, bool interactive, bool , fSeenOnce(false) , fCleanOnExit(cleanOnExit) , fTimeoutInMS(timeoutInMS) + , fIntervalInMS(intervalInMS) , fShmId(shmId) , fSegmentName("fmq_" + fShmId + "_main") , fManagementSegmentName("fmq_" + fShmId + "_mng") @@ -110,7 +115,7 @@ void Monitor::Run() Interactive(); } else { while (!fTerminating) { - this_thread::sleep_for(chrono::milliseconds(100)); + this_thread::sleep_for(chrono::milliseconds(fIntervalInMS)); CheckSegment(); } } @@ -183,7 +188,7 @@ void Monitor::Interactive() PrintHeader(); while (!fTerminating) { - if (poll(cinfd, 1, 100)) { + if (poll(cinfd, 1, fIntervalInMS)) { if (fTerminating || gSignalStatus != 0) { break; } @@ -275,7 +280,7 @@ void Monitor::CheckSegment() unsigned int numDevices = 0; - if (fInteractive) { + if (fInteractive || fViewOnly) { DeviceCounter* dc = managementSegment.find(bipc::unique_instance).first; if (dc) { numDevices = dc->fCount; @@ -303,6 +308,18 @@ void Monitor::CheckSegment() << setw(8) << numDevices << " | " << setw(10) << (fViewOnly ? "view only" : to_string(duration)) << " |" << c << flush; + } else if (fViewOnly) { + time_t current = chrono::system_clock::to_time_t(now); + chrono::milliseconds ms = chrono::duration_cast(now.time_since_epoch()); + struct tm local; + localtime_r(¤t, &local); + char timeBuffer[80]; + size_t count = strftime(timeBuffer, 80, "%F %T", &local); + cout << (count != 0 ? timeBuffer : "") << "." << setfill('0') << setw(6) << ms.count() % 1000000 + << ", name: " << fSegmentName + << ", size: " << segment.get_size() + << ", free: " << segment.get_free_memory() + << ", numDevices: " << numDevices << endl; } } catch (bie&) { fHeartbeatTriggered = false; @@ -473,7 +490,7 @@ void Monitor::Cleanup(const ShmId& shmId) RemoveObject(managementSegmentName.c_str()); } catch (bie&) { cout << "Did not find '" << managementSegmentName << "' shared memory segment. No regions to cleanup." << endl; - } catch(std::out_of_range& oor) { + } catch(out_of_range& oor) { cout << "Could not locate element in the region map, out of range: " << oor.what() << endl; } diff --git a/fairmq/shmem/Monitor.h b/fairmq/shmem/Monitor.h index d0ee9add..22f1fd8a 100644 --- a/fairmq/shmem/Monitor.h +++ b/fairmq/shmem/Monitor.h @@ -37,7 +37,7 @@ struct ShmId class Monitor { public: - Monitor(const std::string& shmId, bool selfDestruct, bool interactive, bool viewOnly, unsigned int timeoutInMS, bool runAsDaemon, bool cleanOnExit); + Monitor(const std::string& shmId, bool selfDestruct, bool interactive, bool viewOnly, unsigned int timeoutInMS, unsigned int intervalInMS, bool runAsDaemon, bool cleanOnExit); Monitor(const Monitor&) = delete; Monitor operator=(const Monitor&) = delete; @@ -84,6 +84,7 @@ class Monitor bool fSeenOnce; // true is segment has been opened successfully at least once bool fCleanOnExit; unsigned int fTimeoutInMS; + unsigned int fIntervalInMS; std::string fShmId; std::string fSegmentName; std::string fManagementSegmentName; diff --git a/fairmq/shmem/runMonitor.cxx b/fairmq/shmem/runMonitor.cxx index ac8b3dfa..fb5fe8e5 100644 --- a/fairmq/shmem/runMonitor.cxx +++ b/fairmq/shmem/runMonitor.cxx @@ -76,6 +76,7 @@ int main(int argc, char** argv) bool interactive = false; bool viewOnly = false; unsigned int timeoutInMS = 5000; + unsigned int intervalInMS = 100; bool runAsDaemon = false; bool cleanOnExit = false; @@ -90,6 +91,7 @@ int main(int argc, char** argv) ("timeout,t" , value(&timeoutInMS)->default_value(5000), "Heartbeat timeout in milliseconds") ("daemonize,d" , value(&runAsDaemon)->implicit_value(true), "Daemonize the monitor") ("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") ("help,h", "Print help"); variables_map vm; @@ -117,7 +119,7 @@ int main(int argc, char** argv) cout << "Starting shared memory monitor for session: \"" << sessionName << "\" (shmId: " << shmId << ")..." << endl; - Monitor monitor(shmId, selfDestruct, interactive, viewOnly, timeoutInMS, runAsDaemon, cleanOnExit); + Monitor monitor(shmId, selfDestruct, interactive, viewOnly, timeoutInMS, intervalInMS, runAsDaemon, cleanOnExit); monitor.CatchSignals(); monitor.Run();