diff --git a/XNMonitorServer/CMakeLists.txt b/XNMonitorServer/CMakeLists.txt index 095a1bb..c5dae28 100644 --- a/XNMonitorServer/CMakeLists.txt +++ b/XNMonitorServer/CMakeLists.txt @@ -35,6 +35,8 @@ add_library(XNMonitorServer SHARED XNMonitorInterface.cpp SystemInfoMonitor.h SystemInfoMonitor.cpp + ModelInfoMonitor.h + ModelInfoMonitor.cpp ${DDS_XNIDL_SOURCES_CXX} ) diff --git a/XNMonitorServer/ModelInfoMonitor.cpp b/XNMonitorServer/ModelInfoMonitor.cpp new file mode 100644 index 0000000..d32107e --- /dev/null +++ b/XNMonitorServer/ModelInfoMonitor.cpp @@ -0,0 +1,56 @@ +#include "ModelInfoMonitor.h" +#include + +using json = nlohmann::json; + +ModelInfoMonitor::~ModelInfoMonitor() +{ + //注销模型状态订阅 + TopicManager::Instance()->unregisterSubscriber("XNSim::XNSimStatus::XNModelStatus"); +} + +std::string ModelInfoMonitor::Initialize() +{ + //注册模型状态订阅 + XNDDSErrorCode ret = + TopicManager::Instance()->registerSubscriber( + "XNSim::XNSimStatus::XNModelStatus", + std::bind(&ModelInfoMonitor::ModelStatusListener, this, std::placeholders::_1)); + if (ret != XNDDSErrorCode::SUCCESS) { + return "Failed to register model status subscriber, error code: " + + std::to_string(static_cast(ret)); + } + return "Success"; +} + +std::string ModelInfoMonitor::GetAllModelInfo() +{ + std::lock_guard locker(m_ModelStatusMutex); + json jsonObj; + + // 创建模型状态JSON对象 + json modelStatusObj; + for (const auto &status : m_ModelStatus) { + json modelObj; + modelObj["modelName"] = status.second.XNModelName(); + modelObj["modelID"] = status.second.XNModelID(); + modelObj["modelStatus"] = status.second.XNModelSt(); + modelObj["modelThreadID"] = status.second.XNModelThID(); + modelObj["modelNode"] = status.second.XNModelNode(); + modelObj["modelPriority"] = status.second.XNModelPro(); + modelObj["modelRunCount"] = status.second.XNModelRunCnt(); + modelObj["modelCurrentFrequency"] = status.second.XNMdlCurFreq(); + modelObj["modelSetFrequency"] = status.second.XNMdlSetFreq(); + + modelStatusObj[std::to_string(status.first)] = modelObj; + } + jsonObj["modelStatus"] = modelStatusObj; + + return jsonObj.dump(); +} + +void ModelInfoMonitor::ModelStatusListener(const XNSim::XNSimStatus::XNModelStatus &status) +{ + std::lock_guard locker(m_ModelStatusMutex); + m_ModelStatus[status.XNModelID()] = status; +} \ No newline at end of file diff --git a/XNMonitorServer/ModelInfoMonitor.h b/XNMonitorServer/ModelInfoMonitor.h new file mode 100644 index 0000000..7ed2b09 --- /dev/null +++ b/XNMonitorServer/ModelInfoMonitor.h @@ -0,0 +1,41 @@ +#pragma once + +#include "XNMonitorServer_global.h" +#include +#include +#include "TypeDefine.h" +#include "TopicManager.h" +#include "../XNCore/XNIDL/XNSimStatusPubSubTypes.hpp" + +class XNMONITORSERVER_EXPORT ModelInfoMonitor +{ +public: + explicit ModelInfoMonitor() {} + virtual ~ModelInfoMonitor(); + +public: + std::string Initialize(); + + std::string GetAllModelInfo(); + +private: + /** + * @brief 模型状态监听 + * @param status 模型状态 + */ + void ModelStatusListener(const XNSim::XNSimStatus::XNModelStatus &status); + +private: + /** + * @brief 互斥锁 + */ + std::mutex m_ModelStatusMutex; + /** + * @brief 模型状态 + */ + std::map m_ModelStatus; + /** + * @brief 模型周期计数 + */ + std::map m_ModelCycleCount; +}; diff --git a/XNMonitorServer/TypeDefine.h b/XNMonitorServer/TypeDefine.h index b049f37..2d0a830 100755 --- a/XNMonitorServer/TypeDefine.h +++ b/XNMonitorServer/TypeDefine.h @@ -126,83 +126,6 @@ enum class XNDDSErrorCode { NOT_INITIALIZED = -8 }; -/** - * @brief 运行时数据 - */ -struct XNRuntimeData { - /** - * @brief 名称 - */ - std::string m_name; - /** - * @brief 线程ID或模型ID - */ - unsigned int m_id; - /** - * @brief 运行状态 - */ - unsigned int m_RunningState; - /** - * @brief CPU亲和性掩码 - * @note 用作线程信息时存储CPU亲和性掩码,从低位到高位依次为CPU 0~31 的亲和性掩码 - * @note 用作模型信息时存储存储线程号 - */ - unsigned int m_AffinityMask; - /** - * @brief 节点ID - * @note 用作模型信息时存储节点号 - */ - unsigned int m_NodeID; - /** - * @brief 优先级 - */ - unsigned int m_Priority; - /** - * @brief 设定频率 - */ - double m_SetFrequency; - /** - * @brief 当前频率 - */ - std::vector m_CurrentFrequency; - /** - * @brief 最大频率 - */ - double m_MaxFrequency; - /** - * @brief 最小频率 - */ - double m_MinFrequency; - /** - * @brief 平均频率 - */ - double m_AvgFrequency; - /** - * @brief 周期计数 - */ - double m_CycleCount; - /** - * @brief 设定周期 - */ - double m_SetPeriod; - /** - * @brief 当前周期 - */ - std::vector m_CurrentPeriod; - /** - * @brief 最大周期 - */ - double m_MaxPeriod; - /** - * @brief 最小周期 - */ - double m_MinPeriod; - /** - * @brief 平均周期 - */ - double m_AvgPeriod; -}; - /** * @brief 成员变量定义结构体 * @note 成员变量包含数据类型、变量名、是否为数组、数组大小、描述 diff --git a/XNMonitorServer/XNMonitorInterface.cpp b/XNMonitorServer/XNMonitorInterface.cpp index 26b82aa..6c110a3 100644 --- a/XNMonitorServer/XNMonitorInterface.cpp +++ b/XNMonitorServer/XNMonitorInterface.cpp @@ -7,22 +7,15 @@ #include #include #include "SystemInfoMonitor.h" +#include "ModelInfoMonitor.h" // 全局变量 -static std::string g_lastError; -static std::mutex g_errorMutex; - static bool g_initialized = false; SystemInfoMonitor *systemInfoMonitor = nullptr; bool g_systemInfoMonitorStarted = false; - -// 设置错误信息 -static void SetLastError(const char *error) -{ - std::lock_guard lock(g_errorMutex); - g_lastError = error; -} +ModelInfoMonitor *modelInfoMonitor = nullptr; +bool g_modelInfoMonitorStarted = false; // 初始化函数实现 int XN_Initialize(const char *domainId, int domainIdLen, char *errorMsg, int errorMsgSize) @@ -91,17 +84,27 @@ int XN_StartMonitorSystemInfo(char *errorMsg, int errorMsgSize) return -1; } + if (g_systemInfoMonitorStarted) { + if (errorMsg && errorMsgSize > 0) { + strncpy(errorMsg, "System Info Monitor Already Started", errorMsgSize - 1); + errorMsg[errorMsgSize - 1] = '\0'; + } + return -1; + } + try { systemInfoMonitor = new SystemInfoMonitor(); std::string ret = systemInfoMonitor->Initialize(); if (ret == "Success") { g_systemInfoMonitorStarted = true; if (errorMsg && errorMsgSize > 0) { - strncpy(errorMsg, ret.c_str(), errorMsgSize - 1); + strncpy(errorMsg, "System Info Monitor Started Successfully", errorMsgSize - 1); errorMsg[errorMsgSize - 1] = '\0'; } return 0; } else { + delete systemInfoMonitor; + systemInfoMonitor = nullptr; if (errorMsg && errorMsgSize > 0) { strncpy(errorMsg, ret.c_str(), errorMsgSize - 1); errorMsg[errorMsgSize - 1] = '\0'; @@ -109,6 +112,10 @@ int XN_StartMonitorSystemInfo(char *errorMsg, int errorMsgSize) return -1; } } catch (const std::exception &e) { + if (systemInfoMonitor) { + delete systemInfoMonitor; + systemInfoMonitor = nullptr; + } if (errorMsg && errorMsgSize > 0) { strncpy(errorMsg, e.what(), errorMsgSize - 1); errorMsg[errorMsgSize - 1] = '\0'; @@ -178,10 +185,104 @@ void XN_StopMonitorSystemInfo() } } +int XN_StartMonitorModelInfo(char *errorMsg, int errorMsgSize) +{ + if (!g_initialized) { + if (errorMsg && errorMsgSize > 0) { + strncpy(errorMsg, "DDSMonitor Not Initialized", errorMsgSize - 1); + errorMsg[errorMsgSize - 1] = '\0'; + } + return -1; + } + + if (g_modelInfoMonitorStarted) { + if (errorMsg && errorMsgSize > 0) { + strncpy(errorMsg, "Model Info Monitor Already Started", errorMsgSize - 1); + errorMsg[errorMsgSize - 1] = '\0'; + } + return -1; + } + + try { + modelInfoMonitor = new ModelInfoMonitor(); + std::string ret = modelInfoMonitor->Initialize(); + if (ret == "Success") { + g_modelInfoMonitorStarted = true; + if (errorMsg && errorMsgSize > 0) { + strncpy(errorMsg, "Model Info Monitor Started Successfully", errorMsgSize - 1); + errorMsg[errorMsgSize - 1] = '\0'; + } + return 0; + } else { + delete modelInfoMonitor; + modelInfoMonitor = nullptr; + if (errorMsg && errorMsgSize > 0) { + strncpy(errorMsg, ret.c_str(), errorMsgSize - 1); + errorMsg[errorMsgSize - 1] = '\0'; + } + return -1; + } + } catch (const std::exception &e) { + if (modelInfoMonitor) { + delete modelInfoMonitor; + modelInfoMonitor = nullptr; + } + if (errorMsg && errorMsgSize > 0) { + strncpy(errorMsg, e.what(), errorMsgSize - 1); + errorMsg[errorMsgSize - 1] = '\0'; + } + return -1; + } +} + +int XN_GetModelInfo(char *infoMsg, int infoMsgSize) +{ + if (!g_modelInfoMonitorStarted) { + if (infoMsg && infoMsgSize > 0) { + strncpy(infoMsg, "Model Info Monitor Not Started", infoMsgSize - 1); + infoMsg[infoMsgSize - 1] = '\0'; + } + return -1; + } + + try { + std::string info = modelInfoMonitor->GetAllModelInfo(); + if (infoMsg && infoMsgSize > 0) { + strncpy(infoMsg, info.c_str(), infoMsgSize - 1); + infoMsg[infoMsgSize - 1] = '\0'; + } + return 0; + } catch (const std::exception &e) { + if (infoMsg && infoMsgSize > 0) { + strncpy(infoMsg, e.what(), infoMsgSize - 1); + infoMsg[infoMsgSize - 1] = '\0'; + } + return -1; + } +} + +void XN_StopMonitorModelInfo() +{ + if (g_modelInfoMonitorStarted) { + delete modelInfoMonitor; + modelInfoMonitor = nullptr; + g_modelInfoMonitorStarted = false; + } +} + // 清理函数实现 void XN_Cleanup() { if (g_initialized) { + // 停止并清理系统信息监控 + if (g_systemInfoMonitorStarted) { + XN_StopMonitorSystemInfo(); + } + // 停止并清理模型信息监控 + if (g_modelInfoMonitorStarted) { + XN_StopMonitorModelInfo(); + } + // 清理DDS参与者 TopicManager::cleanupParticipant(); g_initialized = false; } diff --git a/XNMonitorServer/XNMonitorInterface.h b/XNMonitorServer/XNMonitorInterface.h index 7af9905..be2f36c 100644 --- a/XNMonitorServer/XNMonitorInterface.h +++ b/XNMonitorServer/XNMonitorInterface.h @@ -30,6 +30,16 @@ extern "C" // 停止监控系统信息 void XNMONITORSERVER_EXPORT XN_StopMonitorSystemInfo(); + + // 启动监控模型信息 + int XNMONITORSERVER_EXPORT XN_StartMonitorModelInfo(char *errorMsg, int errorMsgSize); + + // 获取模型信息 + int XNMONITORSERVER_EXPORT XN_GetModelInfo(char *infoMsg, int infoMsgSize); + + // 停止监控模型信息 + void XNMONITORSERVER_EXPORT XN_StopMonitorModelInfo(); + // // 主题管理接口 // XNDDSErrorCode XN_RegisterPublisher(const char *topicName, void **dataWriter); // XNDDSErrorCode XN_UnregisterPublisher(const char *topicName);