shmmonitor: add output with -v (non-interactive)

This commit is contained in:
Alexey Rybalchenko 2020-08-06 10:08:11 +02:00
parent b83655d5da
commit b0f73017e2
4 changed files with 42 additions and 12 deletions

View File

@ -25,13 +25,15 @@
#include <fairmq/tools/CppSTL.h> #include <fairmq/tools/CppSTL.h>
#include <fairmq/tools/Strings.h> #include <fairmq/tools/Strings.h>
#include <boost/process.hpp>
#include <boost/filesystem.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem.hpp>
#include <boost/interprocess/indexes/null_index.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/named_condition.hpp> #include <boost/interprocess/sync/named_condition.hpp>
#include <boost/interprocess/sync/named_mutex.hpp> #include <boost/interprocess/sync/named_mutex.hpp>
#include <boost/interprocess/ipc/message_queue.hpp> #include <boost/interprocess/mem_algo/simple_seq_fit.hpp>
#include <boost/process.hpp>
#include <cstdlib> // getenv #include <cstdlib> // getenv
#include <condition_variable> #include <condition_variable>
@ -55,6 +57,13 @@ namespace shmem
struct SharedMemoryError : std::runtime_error { using std::runtime_error::runtime_error; }; struct SharedMemoryError : std::runtime_error { using std::runtime_error::runtime_error; };
using SimpleSeqFitSegment = boost::interprocess::basic_managed_shared_memory<char,
boost::interprocess::simple_seq_fit<boost::interprocess::mutex_family>,
boost::interprocess::iset_index>;
using RBTreeBestFitSegment = boost::interprocess::basic_managed_shared_memory<char,
boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>,
boost::interprocess::iset_index>;
class Manager class Manager
{ {
public: public:
@ -133,7 +142,7 @@ class Manager
Manager(const Manager&) = delete; Manager(const Manager&) = delete;
Manager operator=(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; } boost::interprocess::managed_shared_memory& ManagementSegment() { return fManagementSegment; }
static void StartMonitor(const std::string& id) static void StartMonitor(const std::string& id)
@ -446,7 +455,8 @@ class Manager
private: private:
std::string fShmId; std::string fShmId;
std::string fDeviceId; std::string fDeviceId;
boost::interprocess::managed_shared_memory fSegment; // boost::interprocess::managed_shared_memory fSegment;
RBTreeBestFitSegment fSegment;
boost::interprocess::managed_shared_memory fManagementSegment; boost::interprocess::managed_shared_memory fManagementSegment;
VoidAlloc fShmVoidAlloc; VoidAlloc fShmVoidAlloc;
boost::interprocess::named_mutex fShmMtx; boost::interprocess::named_mutex fShmMtx;

View File

@ -22,6 +22,10 @@
#include <csignal> #include <csignal>
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <chrono>
#include <ctime>
#include <time.h>
#include <iomanip>
#include <termios.h> #include <termios.h>
#include <poll.h> #include <poll.h>
@ -48,7 +52,7 @@ void signalHandler(int signal)
gSignalStatus = 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) : fSelfDestruct(selfDestruct)
, fInteractive(interactive) , fInteractive(interactive)
, fViewOnly(viewOnly) , fViewOnly(viewOnly)
@ -56,6 +60,7 @@ Monitor::Monitor(const string& shmId, bool selfDestruct, bool interactive, bool
, fSeenOnce(false) , fSeenOnce(false)
, fCleanOnExit(cleanOnExit) , fCleanOnExit(cleanOnExit)
, fTimeoutInMS(timeoutInMS) , fTimeoutInMS(timeoutInMS)
, fIntervalInMS(intervalInMS)
, fShmId(shmId) , fShmId(shmId)
, fSegmentName("fmq_" + fShmId + "_main") , fSegmentName("fmq_" + fShmId + "_main")
, fManagementSegmentName("fmq_" + fShmId + "_mng") , fManagementSegmentName("fmq_" + fShmId + "_mng")
@ -110,7 +115,7 @@ void Monitor::Run()
Interactive(); Interactive();
} else { } else {
while (!fTerminating) { while (!fTerminating) {
this_thread::sleep_for(chrono::milliseconds(100)); this_thread::sleep_for(chrono::milliseconds(fIntervalInMS));
CheckSegment(); CheckSegment();
} }
} }
@ -183,7 +188,7 @@ void Monitor::Interactive()
PrintHeader(); PrintHeader();
while (!fTerminating) { while (!fTerminating) {
if (poll(cinfd, 1, 100)) { if (poll(cinfd, 1, fIntervalInMS)) {
if (fTerminating || gSignalStatus != 0) { if (fTerminating || gSignalStatus != 0) {
break; break;
} }
@ -275,7 +280,7 @@ void Monitor::CheckSegment()
unsigned int numDevices = 0; unsigned int numDevices = 0;
if (fInteractive) { if (fInteractive || fViewOnly) {
DeviceCounter* dc = managementSegment.find<DeviceCounter>(bipc::unique_instance).first; DeviceCounter* dc = managementSegment.find<DeviceCounter>(bipc::unique_instance).first;
if (dc) { if (dc) {
numDevices = dc->fCount; numDevices = dc->fCount;
@ -303,6 +308,18 @@ void Monitor::CheckSegment()
<< setw(8) << numDevices << " | " << setw(8) << numDevices << " | "
<< setw(10) << (fViewOnly ? "view only" : to_string(duration)) << " |" << setw(10) << (fViewOnly ? "view only" : to_string(duration)) << " |"
<< c << flush; << c << flush;
} else if (fViewOnly) {
time_t current = chrono::system_clock::to_time_t(now);
chrono::milliseconds ms = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch());
struct tm local;
localtime_r(&current, &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&) { } catch (bie&) {
fHeartbeatTriggered = false; fHeartbeatTriggered = false;
@ -473,7 +490,7 @@ void Monitor::Cleanup(const ShmId& shmId)
RemoveObject(managementSegmentName.c_str()); RemoveObject(managementSegmentName.c_str());
} catch (bie&) { } catch (bie&) {
cout << "Did not find '" << managementSegmentName << "' shared memory segment. No regions to cleanup." << endl; 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; cout << "Could not locate element in the region map, out of range: " << oor.what() << endl;
} }

View File

@ -37,7 +37,7 @@ struct ShmId
class Monitor class Monitor
{ {
public: 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(const Monitor&) = delete;
Monitor operator=(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 fSeenOnce; // true is segment has been opened successfully at least once
bool fCleanOnExit; bool fCleanOnExit;
unsigned int fTimeoutInMS; unsigned int fTimeoutInMS;
unsigned int fIntervalInMS;
std::string fShmId; std::string fShmId;
std::string fSegmentName; std::string fSegmentName;
std::string fManagementSegmentName; std::string fManagementSegmentName;

View File

@ -76,6 +76,7 @@ int main(int argc, char** argv)
bool interactive = false; bool interactive = false;
bool viewOnly = false; bool viewOnly = false;
unsigned int timeoutInMS = 5000; unsigned int timeoutInMS = 5000;
unsigned int intervalInMS = 100;
bool runAsDaemon = false; bool runAsDaemon = false;
bool cleanOnExit = false; bool cleanOnExit = false;
@ -90,6 +91,7 @@ int main(int argc, char** argv)
("timeout,t" , value<unsigned int>(&timeoutInMS)->default_value(5000), "Heartbeat timeout in milliseconds") ("timeout,t" , value<unsigned int>(&timeoutInMS)->default_value(5000), "Heartbeat timeout in milliseconds")
("daemonize,d" , value<bool>(&runAsDaemon)->implicit_value(true), "Daemonize the monitor") ("daemonize,d" , value<bool>(&runAsDaemon)->implicit_value(true), "Daemonize the monitor")
("clean-on-exit,e", value<bool>(&cleanOnExit)->implicit_value(true), "Perform cleanup on exit") ("clean-on-exit,e", value<bool>(&cleanOnExit)->implicit_value(true), "Perform cleanup on exit")
("interval" , value<unsigned int>(&intervalInMS)->default_value(100), "Output interval for interactive/view-only mode")
("help,h", "Print help"); ("help,h", "Print help");
variables_map vm; variables_map vm;
@ -117,7 +119,7 @@ int main(int argc, char** argv)
cout << "Starting shared memory monitor for session: \"" << sessionName << "\" (shmId: " << shmId << ")..." << endl; 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.CatchSignals();
monitor.Run(); monitor.Run();