diff --git a/general/src/function/cpu_usage.cpp b/general/src/function/cpu_usage.cpp index 4b28730..aac7ae2 100644 --- a/general/src/function/cpu_usage.cpp +++ b/general/src/function/cpu_usage.cpp @@ -94,7 +94,7 @@ float CPUusage::GetMemoryUsage(){ #ifdef _WIN64 || _WIN32 -int CProcessMonitor::get_memory_usage(uint64_t* mem, uint64_t* vmem) +int CProcessMonitor::GetMemoryUsage(uint64_t* mem, uint64_t* vmem) { PROCESS_MEMORY_COUNTERS pmc; if(GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) @@ -104,10 +104,35 @@ int CProcessMonitor::get_memory_usage(uint64_t* mem, uint64_t* vmem) return 0; } return -1; - } -float CProcessMonitor::get_cpu_usage() +CProcessMonitor::~CProcessMonitor() +{ + clear(); +} + +HANDLE CProcessMonitor::SetPID(DWORD ProcessID) { + clear(); //如果之前监视过另一个进程,就先关闭它的句柄 + init(); + return _hProcess= OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, ProcessID); +} + +CProcessMonitor::CProcessMonitor() +{ + init(); + _processor = get_processor_number(); + this->m_log_status = STATUS_STOP; +} + +CProcessMonitor::CProcessMonitor(DWORD ProcessID) +{ + init(); + _processor = get_processor_number(); + this->SetPID(ProcessID); + this->m_log_status = STATUS_STOP; +} + +float CProcessMonitor::GetCpuUsage() { FILETIME now; FILETIME creation_time; @@ -173,5 +198,56 @@ int CProcessMonitor::get_processor_number() GetSystemInfo(&info); return info.dwNumberOfProcessors; } +int CProcessMonitor::StopLog(){ + m_mux.lock(); + m_log_status = STATUS_STOP; + m_mux.unlock(); + return 0; +} + +int CProcessMonitor::StartLog(uint64_t milisecond){ + if(m_log_status != STATUS_STOP ) + return -1; + if(m_logs.size() > 0){ + for(auto itr = m_logs.begin();itr != m_logs.end();itr++){ + delete *itr; + } + 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); + } + }); + 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 2ab9d6b..545fefa 100644 --- a/general/src/function/cpu_usage.h +++ b/general/src/function/cpu_usage.h @@ -5,34 +5,69 @@ #include #include #include +#include +#include +#include #endif #ifdef linux #include -#include #include "unistd.h" - -#include -#include -#include #include #include #include #include #include #include -#include #include using namespace std; #endif -//原理:调用GetProcessTimes(),并与上次调用得到的结果相减,即得到某段时间内CPU的使用时间 -//C++ 获取特定进程规定CPU使用率 原文:http://blog.csdn.net/liuqx97bb/article/details/52058657 +#include +#include +#include +#include +#include +#include +#include + class CProcessMonitor { + #ifdef _WIN64 || _WIN32 + +public: + typedef enum { + STATUS_LOGGING, // 正在记录 + STATUS_STARTING, // 启动中 + STATUS_STOP, // 停止记录 + }LOG_STATUS; + + typedef struct { + std::string time; + float cpu; + uint64_t phsical_memory; + uint64_t virtual_memory; + }ProcessStatus; // 某一个时刻的进程状态 + + CProcessMonitor(DWORD ProcessID); + CProcessMonitor(); + ~CProcessMonitor(); + //返回值为进程句柄,可判断OpenProcess是否成功 + HANDLE SetPID(DWORD ProcessID); + //-1 即为失败或进程已退出; 如果成功,首次调用会返回-2(中途用setpid更改了PID后首次调用也会返回-2) + float GetCpuUsage(); + int GetMemoryUsage(uint64_t* mem, uint64_t* vmem); + int StartLog(uint64_t milisecond); + int StopLog(); + + LOG_STATUS m_log_status; + std::list m_logs; + private: + std::mutex m_mux; + std::thread *m_thread; typedef long long int64_t; typedef unsigned long long uint64_t; HANDLE _hProcess; @@ -40,13 +75,10 @@ private: int64_t _last_time; //上一次的时间 int64_t _last_system_time; - // 时间转换 uint64_t FileTime2UTC(const FILETIME* ftime); - // 获得CPU的核数 int get_processor_number(); - //初始化 void init() { @@ -54,6 +86,7 @@ private: _last_time = 0; _hProcess = 0; } + // 开始记录进程使用状态日志 //关闭进程句柄 void clear() @@ -64,28 +97,9 @@ private: } } -public: - CProcessMonitor(DWORD ProcessID) { - init(); - _processor = get_processor_number(); - setpid(ProcessID); - } - CProcessMonitor() { init(); _processor = get_processor_number(); } - ~CProcessMonitor() { clear(); } - - //返回值为进程句柄,可判断OpenProcess是否成功 - HANDLE setpid(DWORD ProcessID) { - clear(); //如果之前监视过另一个进程,就先关闭它的句柄 - init(); - return _hProcess= OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, ProcessID); - } - //-1 即为失败或进程已退出; 如果成功,首次调用会返回-2(中途用setpid更改了PID后首次调用也会返回-2) - float get_cpu_usage(); - int get_memory_usage(uint64_t* mem, uint64_t* vmem); #endif #ifdef linux - public: CProcessMonitor(uint16_t ProcessID) { m_process_id = ProcessID; diff --git a/test/src/gtest_demo/gtest_demo.cpp b/test/src/gtest_demo/gtest_demo.cpp index 8222cbf..e5dafdb 100644 --- a/test/src/gtest_demo/gtest_demo.cpp +++ b/test/src/gtest_demo/gtest_demo.cpp @@ -22,7 +22,7 @@ TEST(testCase,test1){ CProcessMonitor ussage1(21132); while (1) { - std::cout<