Shm: Verbosity switch for Cleanup methods

This commit is contained in:
Alexey Rybalchenko 2020-08-24 13:40:49 +02:00
parent 12e6a874db
commit b126ede45a
2 changed files with 73 additions and 71 deletions

View File

@ -495,60 +495,44 @@ void Monitor::PrintHelp()
<< "[q] quit." << endl; << "[q] quit." << endl;
} }
void Monitor::RemoveObject(const string& name)
std::pair<std::string, bool> RunRemoval(std::function<bool(const std::string&)> f, std::string name, bool verbose)
{ {
if (bipc::shared_memory_object::remove(name.c_str())) { if (f(name)) {
cout << "Successfully removed '" << name << "'." << endl; if (verbose) {
cout << "Successfully removed '" << name << "'." << endl;
}
return {name, true};
} else { } else {
cout << "Did not remove '" << name << "'. Already removed?" << endl; if (verbose) {
cout << "Did not remove '" << name << "'. Already removed?" << endl;
}
return {name, false};
} }
} }
void Monitor::RemoveFileMapping(const string& name) bool Monitor::RemoveObject(const string& name) { return bipc::shared_memory_object::remove(name.c_str()); }
{ bool Monitor::RemoveFileMapping(const string& name) { return bipc::file_mapping::remove(name.c_str()); }
if (bipc::file_mapping::remove(name.c_str())) { bool Monitor::RemoveQueue(const string& name) { return bipc::message_queue::remove(name.c_str()); }
cout << "Successfully removed '" << name << "'." << endl; bool Monitor::RemoveMutex(const string& name) { return bipc::named_mutex::remove(name.c_str()); }
} else { bool Monitor::RemoveCondition(const string& name) { return bipc::named_condition::remove(name.c_str()); }
cout << "Did not remove '" << name << "'. Already removed?" << endl;
}
}
void Monitor::RemoveQueue(const string& name) std::vector<std::pair<std::string, bool>> Monitor::Cleanup(const ShmId& shmId, bool verbose /* = true */)
{ {
if (bipc::message_queue::remove(name.c_str())) { std::vector<std::pair<std::string, bool>> result;
cout << "Successfully removed '" << name << "'." << endl;
} else {
cout << "Did not remove '" << name << "'. Already removed?" << endl;
}
}
void Monitor::RemoveMutex(const string& name) if (verbose) {
{ cout << "Cleaning up for shared memory id '" << shmId.shmId << "'..." << endl;
if (bipc::named_mutex::remove(name.c_str())) {
cout << "Successfully removed '" << name << "'." << endl;
} else {
cout << "Did not remove '" << name << "'. Already removed?" << endl;
} }
}
void Monitor::RemoveCondition(const string& name)
{
if (bipc::named_condition::remove(name.c_str())) {
cout << "Successfully removed '" << name << "'." << endl;
} else {
cout << "Did not remove '" << name << "'. Already removed?" << endl;
}
}
void Monitor::Cleanup(const ShmId& shmId)
{
cout << "Cleaning up for shared memory id '" << shmId.shmId << "'..." << endl;
string managementSegmentName("fmq_" + shmId.shmId + "_mng"); string managementSegmentName("fmq_" + shmId.shmId + "_mng");
try { try {
bipc::managed_shared_memory managementSegment(bipc::open_only, managementSegmentName.c_str()); bipc::managed_shared_memory managementSegment(bipc::open_only, managementSegmentName.c_str());
RegionCounter* rc = managementSegment.find<RegionCounter>(bipc::unique_instance).first; RegionCounter* rc = managementSegment.find<RegionCounter>(bipc::unique_instance).first;
if (rc) { if (rc) {
cout << "Region counter found: " << rc->fCount << endl; if (verbose) {
cout << "Region counter found: " << rc->fCount << endl;
}
uint64_t regionCount = rc->fCount; uint64_t regionCount = rc->fCount;
Uint64RegionInfoMap* m = managementSegment.find<Uint64RegionInfoMap>(bipc::unique_instance).first; Uint64RegionInfoMap* m = managementSegment.find<Uint64RegionInfoMap>(bipc::unique_instance).first;
@ -558,55 +542,68 @@ void Monitor::Cleanup(const ShmId& shmId)
RegionInfo ri = m->at(i); RegionInfo ri = m->at(i);
string path = ri.fPath.c_str(); string path = ri.fPath.c_str();
int flags = ri.fFlags; int flags = ri.fFlags;
cout << "Found RegionInfo with path: '" << path << "', flags: " << flags << ", fDestroyed: " << ri.fDestroyed << "." << endl; if (verbose) {
cout << "Found RegionInfo with path: '" << path << "', flags: " << flags << ", fDestroyed: " << ri.fDestroyed << "." << endl;
}
if (path != "") { if (path != "") {
RemoveFileMapping(tools::ToString(path, "fmq_" + shmId.shmId + "_rg_" + to_string(i))); result.emplace_back(RunRemoval(Monitor::RemoveFileMapping, path + "fmq_" + shmId.shmId + "_rg_" + to_string(i), verbose));
} else { } else {
RemoveObject("fmq_" + shmId.shmId + "_rg_" + to_string(i)); result.emplace_back(RunRemoval(Monitor::RemoveObject, "fmq_" + shmId.shmId + "_rg_" + to_string(i), verbose));
} }
} else { } else {
RemoveObject("fmq_" + shmId.shmId + "_rg_" + to_string(i)); result.emplace_back(RunRemoval(Monitor::RemoveObject, "fmq_" + shmId.shmId + "_rg_" + to_string(i), verbose));
} }
RemoveQueue(string("fmq_" + shmId.shmId + "_rgq_" + to_string(i))); result.emplace_back(RunRemoval(Monitor::RemoveQueue, string("fmq_" + shmId.shmId + "_rgq_" + to_string(i)), verbose));
} }
} else { } else {
cout << "No region counter found. No regions to cleanup." << endl; if (verbose) {
cout << "No region counter found. No regions to cleanup." << endl;
}
} }
RemoveObject(managementSegmentName.c_str()); result.emplace_back(RunRemoval(Monitor::RemoveObject, managementSegmentName.c_str(), verbose));
} catch (bie&) { } catch (bie&) {
cout << "Did not find '" << managementSegmentName << "' shared memory segment. No regions to cleanup." << endl; if (verbose) {
cout << "Did not find '" << managementSegmentName << "' shared memory segment. No regions to cleanup." << endl;
}
} catch(out_of_range& oor) { } catch(out_of_range& oor) {
cout << "Could not locate element in the region map, out of range: " << oor.what() << endl; if (verbose) {
cout << "Could not locate element in the region map, out of range: " << oor.what() << endl;
}
} }
RemoveObject("fmq_" + shmId.shmId + "_main"); result.emplace_back(RunRemoval(Monitor::RemoveObject, "fmq_" + shmId.shmId + "_main", verbose));
RemoveMutex("fmq_" + shmId.shmId + "_mtx"); result.emplace_back(RunRemoval(Monitor::RemoveMutex, "fmq_" + shmId.shmId + "_mtx", verbose));
RemoveCondition("fmq_" + shmId.shmId + "_cv"); result.emplace_back(RunRemoval(Monitor::RemoveCondition, "fmq_" + shmId.shmId + "_cv", verbose));
cout << endl; return result;
} }
void Monitor::Cleanup(const SessionId& sessionId) std::vector<std::pair<std::string, bool>> Monitor::Cleanup(const SessionId& sessionId, bool verbose /* = true */)
{ {
ShmId shmId{buildShmIdFromSessionIdAndUserId(sessionId.sessionId)}; ShmId shmId{buildShmIdFromSessionIdAndUserId(sessionId.sessionId)};
cout << "Cleanup called with session id '" << sessionId.sessionId << "', translating to shared memory id '" << shmId.shmId << "'" << endl; if (verbose) {
Cleanup(shmId); cout << "Cleanup called with session id '" << sessionId.sessionId << "', translating to shared memory id '" << shmId.shmId << "'" << endl;
}
return Cleanup(shmId, verbose);
} }
void Monitor::CleanupFull(const ShmId& shmId) std::vector<std::pair<std::string, bool>> Monitor::CleanupFull(const ShmId& shmId, bool verbose /* = true */)
{ {
Cleanup(shmId); auto result = Cleanup(shmId, verbose);
RemoveMutex("fmq_" + shmId.shmId + "_ms"); result.emplace_back(RunRemoval(Monitor::RemoveMutex, "fmq_" + shmId.shmId + "_ms", verbose));
RemoveQueue("fmq_" + shmId.shmId + "_cq"); result.emplace_back(RunRemoval(Monitor::RemoveQueue, "fmq_" + shmId.shmId + "_cq", verbose));
return result;
} }
void Monitor::CleanupFull(const SessionId& sessionId) std::vector<std::pair<std::string, bool>> Monitor::CleanupFull(const SessionId& sessionId, bool verbose /* = true */)
{ {
ShmId shmId{buildShmIdFromSessionIdAndUserId(sessionId.sessionId)}; ShmId shmId{buildShmIdFromSessionIdAndUserId(sessionId.sessionId)};
cout << "Cleanup called with session id '" << sessionId.sessionId << "', translating to shared memory id '" << shmId.shmId << "'" << endl; if (verbose) {
CleanupFull(shmId); cout << "Cleanup called with session id '" << sessionId.sessionId << "', translating to shared memory id '" << shmId.shmId << "'" << endl;
}
return CleanupFull(shmId, verbose);
} }
Monitor::~Monitor() Monitor::~Monitor()

View File

@ -14,6 +14,7 @@
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include <unordered_map> #include <unordered_map>
#include <utility> // pair
#include <vector> #include <vector>
namespace fair namespace fair
@ -65,27 +66,31 @@ class Monitor
/// @brief Cleanup all shared memory artifacts created by devices /// @brief Cleanup all shared memory artifacts created by devices
/// @param shmId shared memory id /// @param shmId shared memory id
static void Cleanup(const ShmId& shmId); /// @param verbose output cleanup results to stdout
static std::vector<std::pair<std::string, bool>> Cleanup(const ShmId& shmId, bool verbose = true);
/// @brief Cleanup all shared memory artifacts created by devices /// @brief Cleanup all shared memory artifacts created by devices
/// @param sessionId session id /// @param sessionId session id
static void Cleanup(const SessionId& sessionId); /// @param verbose output cleanup results to stdout
static std::vector<std::pair<std::string, bool>> Cleanup(const SessionId& sessionId, bool verbose = true);
/// @brief Cleanup all shared memory artifacts created by devices and monitors /// @brief Cleanup all shared memory artifacts created by devices and monitors
/// @param shmId shared memory id /// @param shmId shared memory id
static void CleanupFull(const ShmId& shmId); /// @param verbose output cleanup results to stdout
static std::vector<std::pair<std::string, bool>> CleanupFull(const ShmId& shmId, bool verbose = true);
/// @brief Cleanup all shared memory artifacts created by devices and monitors /// @brief Cleanup all shared memory artifacts created by devices and monitors
/// @param sessionId session id /// @param sessionId session id
static void CleanupFull(const SessionId& sessionId); /// @param verbose output cleanup results to stdout
static std::vector<std::pair<std::string, bool>> CleanupFull(const SessionId& sessionId, bool verbose = true);
static void PrintDebugInfo(const ShmId& shmId); static void PrintDebugInfo(const ShmId& shmId);
static void PrintDebugInfo(const SessionId& shmId); static void PrintDebugInfo(const SessionId& shmId);
static std::vector<BufferDebugInfo> GetDebugInfo(const ShmId& shmId); static std::vector<BufferDebugInfo> GetDebugInfo(const ShmId& shmId);
static std::vector<BufferDebugInfo> GetDebugInfo(const SessionId& shmId); static std::vector<BufferDebugInfo> GetDebugInfo(const SessionId& shmId);
static void RemoveObject(const std::string&); static bool RemoveObject(const std::string& name);
static void RemoveFileMapping(const std::string&); static bool RemoveFileMapping(const std::string& name);
static void RemoveQueue(const std::string&); static bool RemoveQueue(const std::string& name);
static void RemoveMutex(const std::string&); static bool RemoveMutex(const std::string& name);
static void RemoveCondition(const std::string&); static bool RemoveCondition(const std::string& name);
struct DaemonPresent : std::runtime_error { using std::runtime_error::runtime_error; }; struct DaemonPresent : std::runtime_error { using std::runtime_error::runtime_error; };