Source · fairmq/shmem/
Monitor.hpublic header
Defines class fair::mq::shmem::Monitor, struct fair::mq::shmem::BufferDebugInfo, struct fair::mq::shmem::Monitor::DaemonPresent, struct fair::mq::shmem::Monitor::MonitorError, struct fair::mq::shmem::SegmentConfig, struct fair::mq::shmem::SessionId, struct fair::mq::shmem::ShmId.
Described in Monitor and clean up, fairmq-shmmonitor.
fairmq/shmem/Monitor.h186 lines · C++API: MonitorAPI: BufferDebugInfoAPI: DaemonPresentAPI: MonitorErrorAPI: SegmentConfigAPI: SessionIdAPI: ShmIdGitHub ↗
1/********************************************************************************2 * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *3 * *4 * This software is distributed under the terms of the *5 * GNU Lesser General Public Licence (LGPL) version 3, *6 * copied verbatim in the file "LICENSE" *7 ********************************************************************************/8#ifndef FAIR_MQ_SHMEM_MONITOR_H_9#define FAIR_MQ_SHMEM_MONITOR_H_10 11#include <fairmq/UnmanagedRegion.h>12 13#include <fairlogger/Logger.h>14 15#include <thread>16#include <chrono>17#include <atomic>18#include <string>19#include <stdexcept>20#include <unordered_map>21#include <utility> // pair22#include <vector>23 24namespace fair::mq::shmem25{26 27struct SessionId28{29 std::string sessionId;30 explicit operator std::string() const { return sessionId; }31};32 33struct ShmId34{35 std::string shmId;36 explicit operator std::string() const { return shmId; }37};38 39struct BufferDebugInfo40{41 BufferDebugInfo(size_t offset, pid_t pid, size_t size, uint64_t creationTime)42 : fOffset(offset)43 , fPid(pid)44 , fSize(size)45 , fCreationTime(creationTime)46 {}47 48 size_t fOffset;49 pid_t fPid;50 size_t fSize;51 uint64_t fCreationTime;52};53 54struct SegmentConfig55{56 uint16_t id;57 uint64_t size;58 std::string allocationAlgorithm;59};60 61class Monitor62{63 public:64 Monitor(std::string shmId, bool selfDestruct, bool interactive, bool viewOnly, unsigned int timeoutInMS, unsigned int intervalInMS, bool runAsDaemon, bool cleanOnExit);65 Monitor(const Monitor&) = delete;66 Monitor(Monitor&&) = delete;67 Monitor& operator=(const Monitor&) = delete;68 Monitor& operator=(Monitor&&) = delete;69 70 virtual ~Monitor();71 72 void CatchSignals();73 void Run();74 75 /// @brief Cleanup all shared memory artifacts created by devices76 /// @param shmId shared memory id77 /// @param verbose output cleanup results to stdout78 static std::vector<std::pair<std::string, bool>> Cleanup(const ShmId& shmId, bool verbose = true);79 /// @brief Cleanup all shared memory artifacts created by devices80 /// @param sessionId session id81 /// @param verbose output cleanup results to stdout82 static std::vector<std::pair<std::string, bool>> Cleanup(const SessionId& sessionId, bool verbose = true);83 /// @brief Cleanup all shared memory artifacts created by devices and monitors84 /// @param shmId shared memory id85 /// @param verbose output cleanup results to stdout86 static std::vector<std::pair<std::string, bool>> CleanupFull(const ShmId& shmId, bool verbose = true);87 /// @brief Cleanup all shared memory artifacts created by devices and monitors88 /// @param sessionId session id89 /// @param verbose output cleanup results to stdout90 static std::vector<std::pair<std::string, bool>> CleanupFull(const SessionId& sessionId, bool verbose = true);91 92 /// @brief [EXPERIMENTAL] cleanup the content of the shem segment, without recreating it93 /// @param shmId shared memory id94 /// Only call this when segment is not in use95 static void ResetContent(const ShmId& shmId, bool verbose = true);96 /// @brief [EXPERIMENTAL] cleanup the content of the shem segment, without recreating it97 /// @param sessionId session id98 /// Only call this when segment is not in use99 static void ResetContent(const SessionId& sessionId, bool verbose = true);100 /// @brief [EXPERIMENTAL] cleanup the content of the shem segment, without recreating it101 /// @param shmId shared memory id102 /// Only call this when segment is not in use103 static void ResetContent(const ShmId& shmId, const std::vector<SegmentConfig>& segmentCfgs, const std::vector<RegionConfig>& regionCfgs, bool verbose = true);104 /// @brief [EXPERIMENTAL] cleanup the content of the shem segment, without recreating it105 /// @param sessionId session id106 /// Only call this when segment is not in use107 static void ResetContent(const SessionId& sessionId, const std::vector<SegmentConfig>& segmentCfgs, const std::vector<RegionConfig>& regionCfgs, bool verbose = true);108 109 /// @brief Outputs list of messages in shmem (if compiled with FAIRMQ_DEBUG_MODE=ON)110 /// @param shmId shmem id111 static void PrintDebugInfo(const ShmId& shmId);112 /// @brief Outputs list of messages in shmem (if compiled with FAIRMQ_DEBUG_MODE=ON)113 /// @param sessionId session id114 static void PrintDebugInfo(const SessionId& sessionId);115 /// @brief Returns a list of messages in shmem (if compiled with FAIRMQ_DEBUG_MODE=ON)116 /// @param shmId shmem id117 static std::unordered_map<uint16_t, std::vector<BufferDebugInfo>> GetDebugInfo(const ShmId& shmId);118 /// @brief Returns a list of messages in shmem (if compiled with FAIRMQ_DEBUG_MODE=ON)119 /// @param sessionId session id120 static std::unordered_map<uint16_t, std::vector<BufferDebugInfo>> GetDebugInfo(const SessionId& sessionId);121 /// @brief Returns the amount of free memory in the specified segment122 /// @param shmId shmem id123 /// @param segmentId segment id124 /// @throws MonitorError125 static unsigned long GetFreeMemory(const ShmId& shmId, uint16_t segmentId);126 /// @brief Returns the amount of free memory in the specified segment127 /// @param sessionId session id128 /// @param segmentId segment id129 /// @throws MonitorError130 static unsigned long GetFreeMemory(const SessionId& sessionId, uint16_t segmentId);131 /// @brief Checks if a given segment can be opened132 /// @param shmId shmem id133 /// @param segmentId segment id134 static bool SegmentIsPresent(const ShmId& shmId, uint16_t segmentId);135 /// @brief Checks if a given segment can be opened136 /// @param sessionId session id137 /// @param segmentId segment id138 static bool SegmentIsPresent(const SessionId& sessionId, uint16_t segmentId);139 /// @brief Checks if a given region can be opened140 /// @param shmId shmem id141 /// @param regionId region id142 static bool RegionIsPresent(const ShmId& shmId, uint16_t regionId);143 /// @brief Checks if a given region can be opened144 /// @param sessionId session id145 /// @param regionId region id146 static bool RegionIsPresent(const SessionId& sessionId, uint16_t regionId);147 148 149 static bool PrintShm(const ShmId& shmId);150 static void ListAll(const std::string& path);151 152 static bool RemoveObject(const std::string& name);153 static bool RemoveFileMapping(const std::string& name);154 static bool RemoveQueue(const std::string& name);155 static bool RemoveMutex(const std::string& name);156 static bool RemoveCondition(const std::string& name);157 158 struct DaemonPresent : std::runtime_error { using std::runtime_error::runtime_error; };159 struct MonitorError : std::runtime_error { using std::runtime_error::runtime_error; };160 161 private:162 void PrintHelp();163 void Watch();164 void CheckHeartbeats();165 void CheckSegment();166 void Interactive();167 void SignalMonitor();168 169 bool fSelfDestruct; // will self-destruct after the memory has been closed170 bool fInteractive; // running in interactive mode171 bool fViewOnly; // view only mode172 bool fMonitor;173 bool fSeenOnce{false}; // true is segment has been opened successfully at least once174 bool fCleanOnExit;175 unsigned int fTimeoutInMS;176 unsigned int fIntervalInMS;177 std::string fShmId;178 std::atomic<bool> fTerminating;179 std::atomic<bool> fHeartbeatTriggered;180 std::atomic<std::chrono::high_resolution_clock::time_point> fLastHeartbeat;181 std::thread fSignalThread;182};183 184} // namespace fair::mq::shmem185 186#endif /* FAIR_MQ_SHMEM_MONITOR_H_ */