#ifndef __MAS_PFM_MONITOR_H #define __MAS_PFM_MONITOR_H #include "DBMgr.h" #include "FileHelper.h" #include "Locks.h" #include "Thread.h" #include "SysUtils.h" #include "nvml.h" NAMESPACE_MAS_BEGIN #define MASPFMMONITOR PerformanceMonitor::GetInstance() typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLINIT)(void); typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLSHUTDOWN)(void); typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLDEVICEGETCOUNT)(TZ_Uint32 *); typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLDEVICEGETHANDLEBYINDEX)(TZ_Uint32 index, nvmlDevice_t *device); typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLDEVICEGETMEMORYINFO)(nvmlDevice_t device, nvmlMemory_t *memory); typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLDEVICEGETUTILIZATIONRATE)(nvmlDevice_t device, nvmlUtilization_t *utilization); typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLDEVICEGETTEMPERATURE)(nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp); // 系统GPU情况 struct SysGPUPerf { TZ_Uint32 gpuIndex; TZ_Uint32 gpuUseRate; TZ_Uint64 memTotal; TZ_Uint64 memFree; TZ_Uint64 memUsed; }; class PerformanceMonitor : tzc::OSThread { public: static PerformanceMonitor * GetInstance(); static void DestoryInstance(); TZ_INT Initialize(); TZ_INT Dispose(); /* 设置采集时间间隔 */ void SetIntervalTime(TZ_INT time); private: PerformanceMonitor(); ~PerformanceMonitor(); void initData(); /* 功能:按设定间隔采集系统性能数据,并写入数据库 */ virtual void Entry(); /* CPU占用率 */ void collectCPUData(); /* 内存占用率 */ void collectMemData(); /* 磁盘IO */ void collectDiskIOData(); /* 网络带宽占用率 */ void collectBandWidthRate(); /* CPU信息 */ void collectCPUInfo(); /* 网络连接信息 */ void collectNetworkInfo(); /* 磁盘使用率 */ void collectDiskInfo(); /* GPU信息 */ void collectGPUInfo(); void collectGPUData(); void clearGPUperf(); template TZ_INT loadNvmlFunc(T & funcPoint, const std::string & funcName); private: TZ_BOOL m_inited; TZ_INT m_timeCnt; TZ_INT m_intervalTime; /* 自定义采集频率 */ GPUPERF_NVMLINIT m_fNvmlInit; GPUPERF_NVMLSHUTDOWN m_fNvmlShutdown; GPUPERF_NVMLDEVICEGETCOUNT m_fNvmlDeviceGetCount; GPUPERF_NVMLDEVICEGETHANDLEBYINDEX m_fNvmlDeviceGetHandleByIndex; GPUPERF_NVMLDEVICEGETMEMORYINFO m_fNvmlDeviceGetMemoryInfo; GPUPERF_NVMLDEVICEGETUTILIZATIONRATE m_fNvmlDeviceGetUtilizationRate; GPUPERF_NVMLDEVICEGETTEMPERATURE m_fNvmlDeviceGetTemperature; TZ_DLLHD m_hdLib; TblPerformanceRecordItem m_tbl; /* CPU占用率参数 */ SysStatInfo m_cpuDataInfo; TZ_DOUBLE m_cpuUse; /* 内存占用率参数 */ SysMemInfo m_memDataInfo; /* 磁盘IO参数 */ std::list m_diskDataList; TZ_DOUBLE m_readSpeed; TZ_DOUBLE m_writeSpeed; /* 网络带宽占用率参数 */ std::list m_netDataList; std::map m_bandWidthInfoMap; /* 系统信息参数 */ SystemInfo m_sysInfo; std::list m_interFaces; /* CPU信息参数 */ CPUInfo m_cpuInfo; /* 网络接口信息参数(端口使用情况) */ std::list m_netInfos; /* 磁盘信息参数 */ std::list m_diskInfos; /* GPU信息参数 */ std::string m_GPUName; std::vector m_vGPUPerf; tzc::Mutex m_lockGPUPerf; static PerformanceMonitor * _ins; static tzc::Mutex _insLock; }; template TZ_INT PerformanceMonitor::loadNvmlFunc(T & funcPoint, const std::string & funcName) { if (m_hdLib == nullptr) { TZLogWarn("The hd Lib is nullptr"); return MEC_FAILED; } TZ_DLLHD hdFunc = tzc::SysUtils::GetSymbol(m_hdLib, funcName.c_str()); if (hdFunc == nullptr) { TZLogWarn("The handle of function is nullptr:%s", funcName.c_str()); return MEC_FAILED; } funcPoint = (T)(hdFunc); TZLogInfo("load function success!!!:%s", funcName.c_str()); return MEC_OK; } NAMESPACE_MAS_END #endif