监控后端增加模型信息监控
This commit is contained in:
parent
7e85f4e23a
commit
051a2562be
@ -35,6 +35,8 @@ add_library(XNMonitorServer SHARED
|
|||||||
XNMonitorInterface.cpp
|
XNMonitorInterface.cpp
|
||||||
SystemInfoMonitor.h
|
SystemInfoMonitor.h
|
||||||
SystemInfoMonitor.cpp
|
SystemInfoMonitor.cpp
|
||||||
|
ModelInfoMonitor.h
|
||||||
|
ModelInfoMonitor.cpp
|
||||||
${DDS_XNIDL_SOURCES_CXX}
|
${DDS_XNIDL_SOURCES_CXX}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
56
XNMonitorServer/ModelInfoMonitor.cpp
Normal file
56
XNMonitorServer/ModelInfoMonitor.cpp
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#include "ModelInfoMonitor.h"
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
ModelInfoMonitor::~ModelInfoMonitor()
|
||||||
|
{
|
||||||
|
//注销模型状态订阅
|
||||||
|
TopicManager::Instance()->unregisterSubscriber("XNSim::XNSimStatus::XNModelStatus");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ModelInfoMonitor::Initialize()
|
||||||
|
{
|
||||||
|
//注册模型状态订阅
|
||||||
|
XNDDSErrorCode ret =
|
||||||
|
TopicManager::Instance()->registerSubscriber<XNSim::XNSimStatus::XNModelStatusPubSubType>(
|
||||||
|
"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<int>(ret));
|
||||||
|
}
|
||||||
|
return "Success";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ModelInfoMonitor::GetAllModelInfo()
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> 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<std::mutex> locker(m_ModelStatusMutex);
|
||||||
|
m_ModelStatus[status.XNModelID()] = status;
|
||||||
|
}
|
41
XNMonitorServer/ModelInfoMonitor.h
Normal file
41
XNMonitorServer/ModelInfoMonitor.h
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "XNMonitorServer_global.h"
|
||||||
|
#include <map>
|
||||||
|
#include <mutex>
|
||||||
|
#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<uint32_t, XNSim::XNSimStatus::XNModelStatus> m_ModelStatus;
|
||||||
|
/**
|
||||||
|
* @brief 模型周期计数
|
||||||
|
*/
|
||||||
|
std::map<uint32_t, uint64_t> m_ModelCycleCount;
|
||||||
|
};
|
@ -126,83 +126,6 @@ enum class XNDDSErrorCode {
|
|||||||
NOT_INITIALIZED = -8
|
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<double> 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<double> m_CurrentPeriod;
|
|
||||||
/**
|
|
||||||
* @brief 最大周期
|
|
||||||
*/
|
|
||||||
double m_MaxPeriod;
|
|
||||||
/**
|
|
||||||
* @brief 最小周期
|
|
||||||
*/
|
|
||||||
double m_MinPeriod;
|
|
||||||
/**
|
|
||||||
* @brief 平均周期
|
|
||||||
*/
|
|
||||||
double m_AvgPeriod;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 成员变量定义结构体
|
* @brief 成员变量定义结构体
|
||||||
* @note 成员变量包含数据类型、变量名、是否为数组、数组大小、描述
|
* @note 成员变量包含数据类型、变量名、是否为数组、数组大小、描述
|
||||||
|
@ -7,22 +7,15 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "SystemInfoMonitor.h"
|
#include "SystemInfoMonitor.h"
|
||||||
|
#include "ModelInfoMonitor.h"
|
||||||
|
|
||||||
// 全局变量
|
// 全局变量
|
||||||
static std::string g_lastError;
|
|
||||||
static std::mutex g_errorMutex;
|
|
||||||
|
|
||||||
static bool g_initialized = false;
|
static bool g_initialized = false;
|
||||||
|
|
||||||
SystemInfoMonitor *systemInfoMonitor = nullptr;
|
SystemInfoMonitor *systemInfoMonitor = nullptr;
|
||||||
bool g_systemInfoMonitorStarted = false;
|
bool g_systemInfoMonitorStarted = false;
|
||||||
|
ModelInfoMonitor *modelInfoMonitor = nullptr;
|
||||||
// 设置错误信息
|
bool g_modelInfoMonitorStarted = false;
|
||||||
static void SetLastError(const char *error)
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(g_errorMutex);
|
|
||||||
g_lastError = error;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化函数实现
|
// 初始化函数实现
|
||||||
int XN_Initialize(const char *domainId, int domainIdLen, char *errorMsg, int errorMsgSize)
|
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;
|
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 {
|
try {
|
||||||
systemInfoMonitor = new SystemInfoMonitor();
|
systemInfoMonitor = new SystemInfoMonitor();
|
||||||
std::string ret = systemInfoMonitor->Initialize();
|
std::string ret = systemInfoMonitor->Initialize();
|
||||||
if (ret == "Success") {
|
if (ret == "Success") {
|
||||||
g_systemInfoMonitorStarted = true;
|
g_systemInfoMonitorStarted = true;
|
||||||
if (errorMsg && errorMsgSize > 0) {
|
if (errorMsg && errorMsgSize > 0) {
|
||||||
strncpy(errorMsg, ret.c_str(), errorMsgSize - 1);
|
strncpy(errorMsg, "System Info Monitor Started Successfully", errorMsgSize - 1);
|
||||||
errorMsg[errorMsgSize - 1] = '\0';
|
errorMsg[errorMsgSize - 1] = '\0';
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
|
delete systemInfoMonitor;
|
||||||
|
systemInfoMonitor = nullptr;
|
||||||
if (errorMsg && errorMsgSize > 0) {
|
if (errorMsg && errorMsgSize > 0) {
|
||||||
strncpy(errorMsg, ret.c_str(), errorMsgSize - 1);
|
strncpy(errorMsg, ret.c_str(), errorMsgSize - 1);
|
||||||
errorMsg[errorMsgSize - 1] = '\0';
|
errorMsg[errorMsgSize - 1] = '\0';
|
||||||
@ -109,6 +112,10 @@ int XN_StartMonitorSystemInfo(char *errorMsg, int errorMsgSize)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
|
if (systemInfoMonitor) {
|
||||||
|
delete systemInfoMonitor;
|
||||||
|
systemInfoMonitor = nullptr;
|
||||||
|
}
|
||||||
if (errorMsg && errorMsgSize > 0) {
|
if (errorMsg && errorMsgSize > 0) {
|
||||||
strncpy(errorMsg, e.what(), errorMsgSize - 1);
|
strncpy(errorMsg, e.what(), errorMsgSize - 1);
|
||||||
errorMsg[errorMsgSize - 1] = '\0';
|
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()
|
void XN_Cleanup()
|
||||||
{
|
{
|
||||||
if (g_initialized) {
|
if (g_initialized) {
|
||||||
|
// 停止并清理系统信息监控
|
||||||
|
if (g_systemInfoMonitorStarted) {
|
||||||
|
XN_StopMonitorSystemInfo();
|
||||||
|
}
|
||||||
|
// 停止并清理模型信息监控
|
||||||
|
if (g_modelInfoMonitorStarted) {
|
||||||
|
XN_StopMonitorModelInfo();
|
||||||
|
}
|
||||||
|
// 清理DDS参与者
|
||||||
TopicManager::cleanupParticipant();
|
TopicManager::cleanupParticipant();
|
||||||
g_initialized = false;
|
g_initialized = false;
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,16 @@ extern "C"
|
|||||||
|
|
||||||
// 停止监控系统信息
|
// 停止监控系统信息
|
||||||
void XNMONITORSERVER_EXPORT XN_StopMonitorSystemInfo();
|
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_RegisterPublisher(const char *topicName, void **dataWriter);
|
||||||
// XNDDSErrorCode XN_UnregisterPublisher(const char *topicName);
|
// XNDDSErrorCode XN_UnregisterPublisher(const char *topicName);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user