blob: 1767a61ee1c61b2e6772240174ba1ab1e0e0d602 [file] [log] [blame]
#include <filesystem> // NOLINT
#include <iostream>
#include <string>
#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "tlbmc/hal/shared_mem/metrics.h"
#include "tlbmc/hal/shared_mem/segment_manager.h"
#include "tlbmc/hal/shared_mem/server.h"
ABSL_FLAG(
std::string, command, "",
"Command to execute: 'get_metrics', 'check_segment', 'remove_segment'");
int main(int argc, char** argv) {
absl::ParseCommandLine(argc, argv);
using ::milotic_tlbmc::SharedMemoryServer;
using ::milotic_tlbmc::TlbmcMetrics;
std::string command = absl::GetFlag(FLAGS_command);
if (command == "check_segment") {
bool file_exists = std::filesystem::exists(
std::string{milotic_tlbmc::kShareMemInitializedFile});
bool segment_exists = false;
try {
boost::interprocess::managed_shared_memory sensors_memory(
boost::interprocess::open_only, "TlbmcSharedMemory");
segment_exists = true;
} catch (const boost::interprocess::interprocess_exception& e) {
segment_exists = false;
}
std::cerr << "file_exists: " << file_exists << '\n';
std::cerr << "segment_exists: " << segment_exists << '\n';
return 0;
}
if (command == "get_metrics") {
const TlbmcMetrics* metrics =
SharedMemoryServer::GetInstance().GetMetrics();
std::cerr << "Metrics pointer is " << metrics << '\n';
if (metrics != nullptr) {
std::cerr << "Metrics is " << metrics->ToJson().dump(2) << '\n';
}
return 0;
}
if (command == "remove_segment") {
boost::interprocess::shared_memory_object::remove("TlbmcSharedMemory");
std::filesystem::remove(
std::string{milotic_tlbmc::kShareMemInitializedFile});
return 0;
}
return 0;
}