From d47c777a26a6fda581e5510a4b2163a47439864b Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Thu, 7 Apr 2022 23:18:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=9B=91=E6=8E=A7=E7=9A=84?= =?UTF-8?q?=E6=8A=A5=E8=AD=A6=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- general/src/function/cpu_usage.cpp | 139 ++++++++++++++++----- general/src/function/cpu_usage.h | 25 +++- test/src/test_cpu_usage/test_cpu_usage.cpp | 30 +++-- 3 files changed, 145 insertions(+), 49 deletions(-) diff --git a/general/src/function/cpu_usage.cpp b/general/src/function/cpu_usage.cpp index 96173cf..ea0018b 100644 --- a/general/src/function/cpu_usage.cpp +++ b/general/src/function/cpu_usage.cpp @@ -108,6 +108,7 @@ int CProcessMonitor::GetMemoryUsage(uint64_t* mem, uint64_t* vmem) CProcessMonitor::~CProcessMonitor() { + clear(); } @@ -122,6 +123,14 @@ CProcessMonitor::CProcessMonitor() init(); _processor = get_processor_number(); this->m_log_status = STATUS_STOP; + this->m_alert = false; + this->m_cpu_threshold = 30; + this->m_virtual_memory_threshold = uint64_t(2048) * 1024 * 1024; + this->m_physical_memory_threshold = uint64_t(2048) * 1024 * 1024; +} + +std::list & CProcessMonitor::Logs(){ + return this->m_logs; } CProcessMonitor::CProcessMonitor(DWORD ProcessID) @@ -130,6 +139,10 @@ CProcessMonitor::CProcessMonitor(DWORD ProcessID) _processor = get_processor_number(); this->SetPID(ProcessID); this->m_log_status = STATUS_STOP; + this->m_alert = false; + this->m_cpu_threshold = 30; + this->m_virtual_memory_threshold = uint64_t(2048) * 1024 * 1024; + this->m_physical_memory_threshold = uint64_t(2048) * 1024 * 1024; } float CProcessMonitor::GetCpuUsage() @@ -205,6 +218,98 @@ int CProcessMonitor::StopLog(){ return 0; } +int CProcessMonitor::SetAlert(bool){ + m_mux.lock(); + this->m_alert = true; + m_mux.unlock(); + + return 0; +} + +int CProcessMonitor::SetAlertThreshold(int cpu, uint64_t mem,uint64_t mem2){ + m_mux.lock(); + m_cpu_threshold = cpu; + m_virtual_memory_threshold = mem; + m_physical_memory_threshold = mem2; + m_mux.unlock(); + + return 0; +} + +int CProcessMonitor::SetMemoryAlertNotify(OnMemoryAlert alert){ + m_mux.lock(); + this->m_on_memory_alert = alert; + m_mux.unlock(); + + return 0; +} + +int CProcessMonitor::SetCpuAlertNotify(OnCpuAlert alert){ + m_mux.lock(); + this->m_on_cpu_alert = alert; + m_mux.unlock(); + + return 0; +} + + +void MonitorProcess(CProcessMonitor *pr,uint64_t milisecond) { + + if(pr->m_log_status == CProcessMonitor::STATUS_STARTING){ + pr->m_mux.lock(); + pr->m_log_status = CProcessMonitor::STATUS_LOGGING; + pr->m_mux.unlock(); + } + while(pr->m_log_status == CProcessMonitor::STATUS_LOGGING){ + Sleep(milisecond); + + SYSTEMTIME stTime; + GetLocalTime(&stTime); + WORD wYear = stTime.wYear; + WORD wMonth = stTime.wMonth; + WORD wDay = stTime.wDay; + WORD wHour = stTime.wHour; + WORD wMinute = stTime.wMinute; + WORD wSecond = stTime.wSecond; + CString m_date; + //m_date字符串即为当前时间。如:2010年4月23日 11:12:45 + m_date.Format("%4d-%2d-%2d %2d:%2d:%2d", wYear, wMonth, wDay, + wHour, wMinute, wSecond); + USES_CONVERSION; + std::string strS(m_date); //CString-->std::string + CProcessMonitor::ProcessStatus *p = new CProcessMonitor::ProcessStatus; + p->time = strS; + uint64_t vmem,pmem; + pr->GetMemoryUsage(&pmem,&vmem); + p->phsical_memory = pmem; + p->virtual_memory = vmem; + p->cpu = pr->GetCpuUsage(); + pr->m_logs.push_back(p); + + if(pr->m_alert){ + bool flag = false; + // 物理内存大于阀值 + if(p->phsical_memory > pr->m_physical_memory_threshold){ + if(pr->m_on_memory_alert){ + pr->m_on_memory_alert(p->phsical_memory, p->virtual_memory); + } + } + // 虚拟内存大于监控阀值 + if(p->virtual_memory > pr->m_virtual_memory_threshold) { + if(pr->m_on_memory_alert) + pr->m_on_memory_alert(p->phsical_memory, p->virtual_memory); + } + + // cpu大于被监控cpu + if(p->cpu > pr->m_cpu_threshold){ + if(pr->m_on_cpu_alert){ + pr->m_on_cpu_alert(p->cpu); + } + } + } + } +} + int CProcessMonitor::StartLog(uint64_t milisecond){ if(m_log_status != STATUS_STOP ) return -1; @@ -215,39 +320,9 @@ int CProcessMonitor::StartLog(uint64_t milisecond){ m_logs.clear(); } this->m_log_status = STATUS_STARTING; - m_thread = new std::thread([this,milisecond](){ - if(m_log_status == CProcessMonitor::STATUS_STARTING){ - m_mux.lock(); - m_log_status = CProcessMonitor::STATUS_LOGGING; - m_mux.unlock(); - } - while(m_log_status == CProcessMonitor::STATUS_LOGGING){ - Sleep(milisecond); - SYSTEMTIME stTime; - GetLocalTime(&stTime); - WORD wYear = stTime.wYear; - WORD wMonth = stTime.wMonth; - WORD wDay = stTime.wDay; - WORD wHour = stTime.wHour; - WORD wMinute = stTime.wMinute; - WORD wSecond = stTime.wSecond; - CString m_date; - //m_date字符串即为当前时间。如:2010年4月23日 11:12:45 - m_date.Format("%4d-%2d-%2d %2d:%2d:%2d", wYear, wMonth, wDay, wHour, wMinute, wSecond); - USES_CONVERSION; - std::string strS(m_date); //CString-->std::string - ProcessStatus *p = new ProcessStatus; - p->time = strS; - uint64_t vmem,pmem; - this->GetMemoryUsage(&pmem,&vmem); - p->phsical_memory = pmem; - p->virtual_memory = vmem; - p->cpu = this->GetCpuUsage(); - this->m_logs.push_back(p); - } - }); + + m_thread = new std::thread(MonitorProcess,this,milisecond); return 0; } - #endif \ No newline at end of file diff --git a/general/src/function/cpu_usage.h b/general/src/function/cpu_usage.h index 545fefa..dcb3a13 100644 --- a/general/src/function/cpu_usage.h +++ b/general/src/function/cpu_usage.h @@ -34,7 +34,6 @@ using namespace std; #include class CProcessMonitor { - #ifdef _WIN64 || _WIN32 public: @@ -44,13 +43,16 @@ public: STATUS_STOP, // 停止记录 }LOG_STATUS; + typedef std::function OnCpuAlert; // cpu告警 + typedef std::function OnMemoryAlert; // 内存告警 + typedef struct { std::string time; float cpu; uint64_t phsical_memory; uint64_t virtual_memory; }ProcessStatus; // 某一个时刻的进程状态 - + std::list &Logs(); CProcessMonitor(DWORD ProcessID); CProcessMonitor(); ~CProcessMonitor(); @@ -61,11 +63,22 @@ public: int GetMemoryUsage(uint64_t* mem, uint64_t* vmem); int StartLog(uint64_t milisecond); int StopLog(); - - LOG_STATUS m_log_status; - std::list m_logs; - + int SetAlert(bool); + int SetAlertThreshold(int cpu, uint64_t mem,uint64_t mem2); + // 设置内存报警的回调函数 + int SetMemoryAlertNotify(OnMemoryAlert ); + // 设置cpu报警的回调函数 + int SetCpuAlertNotify(OnCpuAlert ); + friend void MonitorProcess(CProcessMonitor *,uint64_t ); private: + std::list m_logs; + LOG_STATUS m_log_status; + uint64_t m_virtual_memory_threshold; + uint64_t m_physical_memory_threshold; + int m_cpu_threshold; + OnMemoryAlert m_on_memory_alert; + OnCpuAlert m_on_cpu_alert; + bool m_alert; std::mutex m_mux; std::thread *m_thread; typedef long long int64_t; diff --git a/test/src/test_cpu_usage/test_cpu_usage.cpp b/test/src/test_cpu_usage/test_cpu_usage.cpp index a63141c..0bf14cc 100644 --- a/test/src/test_cpu_usage/test_cpu_usage.cpp +++ b/test/src/test_cpu_usage/test_cpu_usage.cpp @@ -2,24 +2,32 @@ #include "function/cpu_usage.h" #include #include - +#include using namespace std; -int main(int argc,char **argv){ - CProcessMonitor usage1(19728); +int main(int argc, char **argv) +{ + CProcessMonitor usage1(26644); usage1.StartLog(1000); + usage1.SetAlert(true); + + usage1.SetCpuAlertNotify([](float cpu){ + std::cout<<"cpu alert detected "<time<<" "<<(*itr)->cpu; - } + // for (auto itr = usage1.m_logs.begin(); itr != usage1.m_logs.end(); itr++) + // { + // std::cout<<(*itr)->time<<" cpu: "<<(*itr)->cpu + // << " physical mem: " << (*itr)->phsical_memory + // << " virtual mem: " << (*itr)->virtual_memory + // <