进程监视器添加记录功能

master
zcy 2022-03-28 17:01:45 +08:00
parent 36a2f64c17
commit 77c1dbefe4
4 changed files with 126 additions and 36 deletions

View File

@ -94,7 +94,7 @@ float CPUusage::GetMemoryUsage(){
#ifdef _WIN64 || _WIN32 #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; PROCESS_MEMORY_COUNTERS pmc;
if(GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(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 0;
} }
return -1; 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 now;
FILETIME creation_time; FILETIME creation_time;
@ -173,5 +198,56 @@ int CProcessMonitor::get_processor_number()
GetSystemInfo(&info); GetSystemInfo(&info);
return info.dwNumberOfProcessors; 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 #endif

View File

@ -5,34 +5,69 @@
#include <windows.h> #include <windows.h>
#include <psapi.h> #include <psapi.h>
#include <assert.h> #include <assert.h>
#include <thread>
#include <functional>
#include <atlstr.h>
#endif #endif
#ifdef linux #ifdef linux
#include<iostream> #include<iostream>
#include<stdio.h>
#include "unistd.h" #include "unistd.h"
#include<stdlib.h>
#include<string.h>
#include <string>
#include<unistd.h> #include<unistd.h>
#include<fcntl.h> #include<fcntl.h>
#include<ctype.h> #include<ctype.h>
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <vector>
#include <assert.h> #include <assert.h>
using namespace std; using namespace std;
#endif #endif
//原理调用GetProcessTimes()并与上次调用得到的结果相减即得到某段时间内CPU的使用时间 #include <list>
//C++ 获取特定进程规定CPU使用率 原文http://blog.csdn.net/liuqx97bb/article/details/52058657 #include<stdlib.h>
#include<string.h>
#include <string>
#include <vector>
#include<stdio.h>
#include <mutex>
class CProcessMonitor { class CProcessMonitor {
#ifdef _WIN64 || _WIN32 #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<ProcessStatus*> m_logs;
private: private:
std::mutex m_mux;
std::thread *m_thread;
typedef long long int64_t; typedef long long int64_t;
typedef unsigned long long uint64_t; typedef unsigned long long uint64_t;
HANDLE _hProcess; HANDLE _hProcess;
@ -40,13 +75,10 @@ private:
int64_t _last_time; //上一次的时间 int64_t _last_time; //上一次的时间
int64_t _last_system_time; int64_t _last_system_time;
// 时间转换 // 时间转换
uint64_t FileTime2UTC(const FILETIME* ftime); uint64_t FileTime2UTC(const FILETIME* ftime);
// 获得CPU的核数 // 获得CPU的核数
int get_processor_number(); int get_processor_number();
//初始化 //初始化
void init() void init()
{ {
@ -54,6 +86,7 @@ private:
_last_time = 0; _last_time = 0;
_hProcess = 0; _hProcess = 0;
} }
// 开始记录进程使用状态日志
//关闭进程句柄 //关闭进程句柄
void clear() 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 #endif
#ifdef linux #ifdef linux
public: public:
CProcessMonitor(uint16_t ProcessID) { CProcessMonitor(uint16_t ProcessID) {
m_process_id = ProcessID; m_process_id = ProcessID;

View File

@ -22,7 +22,7 @@ TEST(testCase,test1){
CProcessMonitor ussage1(21132); CProcessMonitor ussage1(21132);
while (1) while (1)
{ {
std::cout<<ussage1.get_cpu_usage()<<std::endl; std::cout<<ussage1.GetCpuUsage()<<std::endl;
Sleep(1000); Sleep(1000);
} }
} }

View File

@ -9,8 +9,8 @@ int main(int argc,char **argv){
CProcessMonitor usage1 = CProcessMonitor(14316); CProcessMonitor usage1 = CProcessMonitor(14316);
while(true){ while(true){
uint64_t vmem,pmem; uint64_t vmem,pmem;
usage1.get_memory_usage(&pmem,&vmem); usage1.GetMemoryUsage(&pmem,&vmem);
std::cout<<"cpu usage is "<< usage1.get_cpu_usage() << " "<<pmem << "byte "<< vmem <<"byte" << std::endl; std::cout<<"cpu usage is "<< usage1.GetCpuUsage() << " "<<pmem << "byte "<< vmem <<"byte" << std::endl;
Sleep(1000); Sleep(1000);
} }
} }