进程监视器添加记录功能
parent
36a2f64c17
commit
77c1dbefe4
|
@ -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
|
|
@ -5,34 +5,69 @@
|
|||
#include <windows.h>
|
||||
#include <psapi.h>
|
||||
#include <assert.h>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <atlstr.h>
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef linux
|
||||
#include<iostream>
|
||||
#include<stdio.h>
|
||||
#include "unistd.h"
|
||||
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
#include <string>
|
||||
#include<unistd.h>
|
||||
#include<fcntl.h>
|
||||
#include<ctype.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <vector>
|
||||
#include <assert.h>
|
||||
|
||||
using namespace std;
|
||||
#endif
|
||||
|
||||
//原理:调用GetProcessTimes(),并与上次调用得到的结果相减,即得到某段时间内CPU的使用时间
|
||||
//C++ 获取特定进程规定CPU使用率 原文:http://blog.csdn.net/liuqx97bb/article/details/52058657
|
||||
#include <list>
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include<stdio.h>
|
||||
#include <mutex>
|
||||
|
||||
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<ProcessStatus*> 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;
|
||||
|
|
|
@ -22,7 +22,7 @@ TEST(testCase,test1){
|
|||
CProcessMonitor ussage1(21132);
|
||||
while (1)
|
||||
{
|
||||
std::cout<<ussage1.get_cpu_usage()<<std::endl;
|
||||
std::cout<<ussage1.GetCpuUsage()<<std::endl;
|
||||
Sleep(1000);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,8 +9,8 @@ int main(int argc,char **argv){
|
|||
CProcessMonitor usage1 = CProcessMonitor(14316);
|
||||
while(true){
|
||||
uint64_t vmem,pmem;
|
||||
usage1.get_memory_usage(&pmem,&vmem);
|
||||
std::cout<<"cpu usage is "<< usage1.get_cpu_usage() << " "<<pmem << "byte "<< vmem <<"byte" << std::endl;
|
||||
usage1.GetMemoryUsage(&pmem,&vmem);
|
||||
std::cout<<"cpu usage is "<< usage1.GetCpuUsage() << " "<<pmem << "byte "<< vmem <<"byte" << std::endl;
|
||||
Sleep(1000);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue