#ifndef __GETSYSTEM_INFO_H
#define __GETSYSTEM_INFO_H
#include "psapi.h"
#include "tlhelp32.h"
typedef struct PROCESS_INFO_T
{
int memUsage; //内存使用率
int threadNum; //线程数
int TotalMemSize; //总内存
int MemSize; //内存使用量
int VirtualMem; //虚拟内存的使用
int handleCount; //句柄的数量
int SyscpuUsage; //系统的cpu利用率
float ProcpuUsage; //进程的cpu利用率
PROCESS_INFO_T()
{
memUsage = 0 ;
threadNum = 0;
MemSize = 0;
TotalMemSize = 0;
VirtualMem = 0;
handleCount = 0;
SyscpuUsage = 0;
ProcpuUsage = 0.0;
}
}PROCESS_INFO;
class DagSystemInfo:public singleton<DagSystemInfo>
{
public:
DagSystemInfo(void);
~DagSystemInfo(void);
void init(void);
void GetSysInfo(PROCESS_INFO& info);
static void S_CheckCpuUsage(void* arg);
void CheckCpuUsage(void);
private:
int GetThreadNum(void);
__int64 m_syscpu; // 系统CPU利用率
float m_Procpu; // 进程CPU利用率
};
#endif
#include "DagSystemInfo.h"
DagSystemInfo::DagSystemInfo(void)
{
}
DagSystemInfo::~DagSystemInfo(void)
{
}
int DagSystemInfo::GetThreadNum(void)
{
int iThreadNum = 0;
HANDLE hSnapshort = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if(INVALID_HANDLE_VALUE == hSnapshort)
{
return 0;
}
THREADENTRY32 stcThreadInfo;
stcThreadInfo.dwSize = sizeof(stcThreadInfo);
DWORD iCurProcessID = GetCurrentProcessId();
for(BOOL bRet = Thread32First(hSnapshort, &stcThreadInfo); bRet != FALSE; bRet = Thread32Next(hSnapshort, &stcThreadInfo))
{
if(stcThreadInfo.th32OwnerProcessID == iCurProcessID)
{
iThreadNum ++;
}
}
CloseHandle(hSnapshort);
return iThreadNum;
}
void DagSystemInfo::S_CheckCpuUsage(void* arg)
{
DagSystemInfo* pDagSysInfo = (DagSystemInfo*)arg;
pDagSysInfo->CheckCpuUsage();
}
__int64 CompareFileTime(FILETIME time1, FILETIME time2)
{
__int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime;
__int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime;
return (b - a);
}
/// 时间转换
static uint64_t file_time_2_utc(const FILETIME* ftime)
{
LARGE_INTEGER li;
li.LowPart = ftime->dwLowDateTime;
li.HighPart = ftime->dwHighDateTime;
return li.QuadPart;
}
void DagSystemInfo::CheckCpuUsage()
{
HANDLE hEvent;
BOOL res;
FILETIME preidleTime;
FILETIME prekernelTime;
FILETIME preuserTime;
FILETIME idleTime;
FILETIME kernelTime;
FILETIME userTime;
FILETIME creation_time;
FILETIME exit_time;
FILETIME kernel_time;
FILETIME user_time;
FILETIME prekernel_time;
FILETIME preuser_time;
SYSTEM_INFO info;
GetSystemInfo(&info);
int cpuCount = info.dwNumberOfProcessors;
res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime;
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, GetCurrentProcessId());
GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time);
prekernel_time = kernel_time;
preuser_time = user_time;
FILETIME now;
GetSystemTimeAsFileTime(&now);
__int64 last_system_time_ = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time)) / cpuCount;
__int64 last_time_ = file_time_2_utc(&now);
hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
while (true)
{
WaitForSingleObject(hEvent, 1000);
res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
__int64 idle = CompareFileTime(preidleTime, idleTime);
__int64 kernel = CompareFileTime(prekernelTime, kernelTime);
__int64 user = CompareFileTime(preuserTime, userTime);
m_syscpu= (kernel + user - idle) * 100 / (kernel + user);
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime;
GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time);
__int64 system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time))/ cpuCount;
GetSystemTimeAsFileTime(&now);
__int64 time = file_time_2_utc(&now);
__int64 system_time_delta = system_time - last_system_time_;
__int64 time_delta = time - last_time_;
m_Procpu = (float)system_time_delta * 100 / (float)time_delta;
last_system_time_ = system_time ;
last_time_ = time;
}
}
void DagSystemInfo::GetSysInfo(PROCESS_INFO& info)
{
info.threadNum = GetThreadNum();
HANDLE hThread = ::GetCurrentProcess();
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
info.memUsage = statex.dwMemoryLoad;
info.TotalMemSize = statex.ullTotalPhys / 1024 /1024 /1024;
DWORD dwHandleCount;
GetProcessHandleCount(hThread, &dwHandleCount);
info.handleCount = dwHandleCount;
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(hThread,&pmc,sizeof(pmc));
info.MemSize= pmc.WorkingSetSize/1024/1024;
info.VirtualMem = pmc.PagefileUsage/1024/1024;
info.SyscpuUsage = m_syscpu>100? 100:m_syscpu;
info.ProcpuUsage = m_Procpu>100? 100:m_Procpu;;
}