PerformanceMonitor.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef __MAS_PFM_MONITOR_H
  2. #define __MAS_PFM_MONITOR_H
  3. #include "DBMgr.h"
  4. #include "FileHelper.h"
  5. #include "Locks.h"
  6. #include "Thread.h"
  7. #include "SysUtils.h"
  8. #include "nvml.h"
  9. NAMESPACE_MAS_BEGIN
  10. #define MASPFMMONITOR PerformanceMonitor::GetInstance()
  11. typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLINIT)(void);
  12. typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLSHUTDOWN)(void);
  13. typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLDEVICEGETCOUNT)(TZ_Uint32 *);
  14. typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLDEVICEGETHANDLEBYINDEX)(TZ_Uint32 index,
  15. nvmlDevice_t *device);
  16. typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLDEVICEGETMEMORYINFO)(nvmlDevice_t device,
  17. nvmlMemory_t *memory);
  18. typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLDEVICEGETUTILIZATIONRATE)(nvmlDevice_t device,
  19. nvmlUtilization_t *utilization);
  20. typedef nvmlReturn_t (DECLDIR * GPUPERF_NVMLDEVICEGETTEMPERATURE)(nvmlDevice_t device,
  21. nvmlTemperatureSensors_t sensorType, unsigned int *temp);
  22. // 系统GPU情况
  23. struct SysGPUPerf
  24. {
  25. TZ_Uint32 gpuIndex;
  26. TZ_Uint32 gpuUseRate;
  27. TZ_Uint64 memTotal;
  28. TZ_Uint64 memFree;
  29. TZ_Uint64 memUsed;
  30. };
  31. class PerformanceMonitor : tzc::OSThread {
  32. public:
  33. static PerformanceMonitor * GetInstance();
  34. static void DestoryInstance();
  35. TZ_INT Initialize();
  36. TZ_INT Dispose();
  37. /* 设置采集时间间隔 */
  38. void SetIntervalTime(TZ_INT time);
  39. private:
  40. PerformanceMonitor();
  41. ~PerformanceMonitor();
  42. void initData();
  43. /* 功能:按设定间隔采集系统性能数据,并写入数据库 */
  44. virtual void Entry();
  45. /* CPU占用率 */
  46. void collectCPUData();
  47. /* 内存占用率 */
  48. void collectMemData();
  49. /* 磁盘IO */
  50. void collectDiskIOData();
  51. /* 网络带宽占用率 */
  52. void collectBandWidthRate();
  53. /* CPU信息 */
  54. void collectCPUInfo();
  55. /* 网络连接信息 */
  56. void collectNetworkInfo();
  57. /* 磁盘使用率 */
  58. void collectDiskInfo();
  59. /* GPU信息 */
  60. void collectGPUInfo();
  61. void collectGPUData();
  62. void clearGPUperf();
  63. template<typename T>
  64. TZ_INT loadNvmlFunc(T & funcPoint, const std::string & funcName);
  65. private:
  66. TZ_BOOL m_inited;
  67. TZ_INT m_timeCnt;
  68. TZ_INT m_intervalTime; /* 自定义采集频率 */
  69. GPUPERF_NVMLINIT m_fNvmlInit;
  70. GPUPERF_NVMLSHUTDOWN m_fNvmlShutdown;
  71. GPUPERF_NVMLDEVICEGETCOUNT m_fNvmlDeviceGetCount;
  72. GPUPERF_NVMLDEVICEGETHANDLEBYINDEX m_fNvmlDeviceGetHandleByIndex;
  73. GPUPERF_NVMLDEVICEGETMEMORYINFO m_fNvmlDeviceGetMemoryInfo;
  74. GPUPERF_NVMLDEVICEGETUTILIZATIONRATE m_fNvmlDeviceGetUtilizationRate;
  75. GPUPERF_NVMLDEVICEGETTEMPERATURE m_fNvmlDeviceGetTemperature;
  76. TZ_DLLHD m_hdLib;
  77. TblPerformanceRecordItem m_tbl;
  78. /* CPU占用率参数 */
  79. SysStatInfo m_cpuDataInfo;
  80. TZ_DOUBLE m_cpuUse;
  81. /* 内存占用率参数 */
  82. SysMemInfo m_memDataInfo;
  83. /* 磁盘IO参数 */
  84. std::list<SysDiskStatInfo> m_diskDataList;
  85. TZ_DOUBLE m_readSpeed;
  86. TZ_DOUBLE m_writeSpeed;
  87. /* 网络带宽占用率参数 */
  88. std::list<SysNetBandInfo> m_netDataList;
  89. std::map<std::string, TZ_DOUBLE> m_bandWidthInfoMap;
  90. /* 系统信息参数 */
  91. SystemInfo m_sysInfo;
  92. std::list<InterfaceInfo> m_interFaces;
  93. /* CPU信息参数 */
  94. CPUInfo m_cpuInfo;
  95. /* 网络接口信息参数(端口使用情况) */
  96. std::list<NetstatInfo> m_netInfos;
  97. /* 磁盘信息参数 */
  98. std::list<DiskstatInfo> m_diskInfos;
  99. /* GPU信息参数 */
  100. std::string m_GPUName;
  101. std::vector<SysGPUPerf> m_vGPUPerf;
  102. tzc::Mutex m_lockGPUPerf;
  103. static PerformanceMonitor * _ins;
  104. static tzc::Mutex _insLock;
  105. };
  106. template<typename T>
  107. TZ_INT PerformanceMonitor::loadNvmlFunc(T & funcPoint, const std::string & funcName)
  108. {
  109. if (m_hdLib == nullptr)
  110. {
  111. TZLogWarn("The hd Lib is nullptr");
  112. return MEC_FAILED;
  113. }
  114. TZ_DLLHD hdFunc = tzc::SysUtils::GetSymbol(m_hdLib, funcName.c_str());
  115. if (hdFunc == nullptr)
  116. {
  117. TZLogWarn("The handle of function is nullptr:%s", funcName.c_str());
  118. return MEC_FAILED;
  119. }
  120. funcPoint = (T)(hdFunc);
  121. TZLogInfo("load function success!!!:%s", funcName.c_str());
  122. return MEC_OK;
  123. }
  124. NAMESPACE_MAS_END
  125. #endif