?? cpuusage.cpp
字號:
/////////////////////////////////////////////////////////////////////////////
// 類名:CCpuUsage
// 功能:CPU利用率接口聲明部分(服務器端)
// 說明:提供三個接口涵數:
// IsShellSystemType()用來檢測系統版本;
// CpuUsage2000()來完成NT/2000下CPU利用率;
// CpuUsage9x()來完成9x下CPU利用率;
// 作者:徐景周(jingzhou_xu@163.net)
// 組織:未來工作室(Future Studio)
// 日期:2003.1.1
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CPUuse.h"
#include "CpuUsage.h"
/////////////////////////////////////////////////////////////////////////////
// CCpuUsage
STDMETHODIMP CCpuUsage::InterfaceSupportsErrorInfo(REFIID riid)
{
static const IID* arr[] =
{
&IID_ICpuUsage
};
for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
if (InlineIsEqualGUID(*arr[i],riid))
return S_OK;
}
return S_FALSE;
}
// ------------------------------------------------------------------------------------------------
// 判斷操作系統涵數及變量,jingzhou xu
typedef enum tagWin32SysType{
Windows32s,
WindowsNT3,
Windows95,
Windows98,
WindowsME,
WindowsNT4,
Windows2000,
WindowsXP
}Win32SysType;
STDMETHODIMP CCpuUsage::IsShellSystemType(BSTR *pbstrSysType)
{
Win32SysType ShellType;
DWORD winVer;
OSVERSIONINFO *osvi;
winVer=GetVersion();
if(winVer<0x80000000){ /*NT */
ShellType=WindowsNT3;
osvi= (OSVERSIONINFO *)malloc(sizeof(OSVERSIONINFO));
if (osvi!=NULL){
memset(osvi,0,sizeof(OSVERSIONINFO));
osvi->dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
GetVersionEx(osvi);
if(osvi->dwMajorVersion==4L)ShellType=WindowsNT4;
else if(osvi->dwMajorVersion==5L&&osvi->dwMinorVersion==0L)ShellType=Windows2000;
else if(osvi->dwMajorVersion==5L&&osvi->dwMinorVersion==1L)ShellType=WindowsXP;
free(osvi);
}
}
else if (LOBYTE(LOWORD(winVer))<4)
ShellType=Windows32s;
else{
ShellType=Windows95;
osvi= (OSVERSIONINFO *)malloc(sizeof(OSVERSIONINFO));
if (osvi!=NULL){
memset(osvi,0,sizeof(OSVERSIONINFO));
osvi->dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
GetVersionEx(osvi);
if(osvi->dwMajorVersion==4L&&osvi->dwMinorVersion==10L)ShellType=Windows98;
else if(osvi->dwMajorVersion==4L&&osvi->dwMinorVersion==90L)ShellType=WindowsME;
free(osvi);
}
}
if(pbstrSysType)
{
USES_CONVERSION;
switch(ShellType)
{
case Windows32s:
*pbstrSysType = SysAllocString(A2W("Windows32s"));
break;
case WindowsNT3:
*pbstrSysType = SysAllocString(A2W("WindowsNT3"));
break;
case Windows95:
*pbstrSysType = SysAllocString(A2W("Windows95"));
break;
case Windows98:
*pbstrSysType = SysAllocString(A2W("Windows98"));
break;
case WindowsME:
*pbstrSysType = SysAllocString(A2W("WindowsME"));
break;
case WindowsNT4:
*pbstrSysType = SysAllocString(A2W("WindowsNT4"));
break;
case Windows2000:
*pbstrSysType = SysAllocString(A2W("Windows2000"));
break;
case WindowsXP:
*pbstrSysType = SysAllocString(A2W("WindowsXP"));
break;
default:
*pbstrSysType = SysAllocString(A2W("Unknown"));
}
}
return S_OK;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// Windows9x下CPU使用率
STDMETHODIMP CCpuUsage::CpuUsage9x(long *plResult)
{
HKEY hkey;
DWORD dwDataSize;
DWORD dwType;
DWORD dwCpuUsage;
// 啟動計數
if ( RegOpenKeyEx( HKEY_DYN_DATA,
"PerfStats\\StartStat",
0,KEY_ALL_ACCESS,
&hkey ) != ERROR_SUCCESS)
return S_FALSE;
dwDataSize=sizeof(DWORD);
RegQueryValueEx( hkey,
"KERNEL\\CPUUsage",
NULL,&dwType,
(LPBYTE)&dwCpuUsage,
&dwDataSize );
RegCloseKey(hkey);
// 得到當前計數值
if ( RegOpenKeyEx( HKEY_DYN_DATA,
"PerfStats\\StatData",
0,KEY_READ,
&hkey ) != ERROR_SUCCESS)
return S_FALSE;
dwDataSize=sizeof(DWORD);
RegQueryValueEx( hkey,
"KERNEL\\CPUUsage",
NULL,&dwType,
(LPBYTE)&dwCpuUsage,
&dwDataSize );
*plResult = dwCpuUsage;
RegCloseKey(hkey);
/*
// 停止計數
if ( RegOpenKeyEx( HKEY_DYN_DATA,
"PerfStats\\StopStat",
0,KEY_ALL_ACCESS,
&hkey ) != ERROR_SUCCESS)
return S_FALSE;
dwDataSize=sizeof(DWORD);
RegQueryValueEx( hkey,
"KERNEL\\CPUUsage",
NULL,&dwType,
(LPBYTE)&dwCpuUsage,
&dwDataSize );
RegCloseKey(hkey);
*/
return S_OK;
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// WindowsNT/2000下CPU使用率
#define SystemBasicInformation 0
#define SystemPerformanceInformation 2
#define SystemTimeInformation 3
#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))
typedef struct
{
DWORD dwUnknown1;
ULONG uKeMaximumIncrement;
ULONG uPageSize;
ULONG uMmNumberOfPhysicalPages;
ULONG uMmLowestPhysicalPage;
ULONG uMmHighestPhysicalPage;
ULONG uAllocationGranularity;
PVOID pLowestUserAddress;
PVOID pMmHighestUserAddress;
ULONG uKeActiveProcessors;
BYTE bKeNumberProcessors;
BYTE bUnknown2;
WORD wUnknown3;
} SYSTEM_BASIC_INFORMATION;
typedef struct
{
LARGE_INTEGER liIdleTime;
DWORD dwSpare[76];
} SYSTEM_PERFORMANCE_INFORMATION;
typedef struct
{
LARGE_INTEGER liKeBootTime;
LARGE_INTEGER liKeSystemTime;
LARGE_INTEGER liExpTimeZoneBias;
ULONG uCurrentTimeZoneId;
DWORD dwReserved;
} SYSTEM_TIME_INFORMATION;
// ntdll!NtQuerySystemInformation (NT specific!)
//
// The function copies the system information of the
// specified type into a buffer
//
// NTSYSAPI
// NTSTATUS
// NTAPI
// NtQuerySystemInformation(
// IN UINT SystemInformationClass, // information type
// OUT PVOID SystemInformation, // pointer to buffer
// IN ULONG SystemInformationLength, // buffer size in bytes
// OUT PULONG ReturnLength OPTIONAL // pointer to a 32-bit
// // variable that receives
// // the number of bytes
// // written to the buffer
// );
typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);
PROCNTQSI NtQuerySystemInformation;
STDMETHODIMP CCpuUsage::CpuUsage2000(long *plResult)
{
SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
SYSTEM_TIME_INFORMATION SysTimeInfo;
SYSTEM_BASIC_INFORMATION SysBaseInfo;
double dbIdleTime;
double dbSystemTime;
LONG status;
static LARGE_INTEGER liOldIdleTime = {0,0};
static LARGE_INTEGER liOldSystemTime = {0,0};
NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(
GetModuleHandle("ntdll"),
"NtQuerySystemInformation"
);
if (!NtQuerySystemInformation)
return S_FALSE;
// 得到系統中處理器數
status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
if (status != NO_ERROR)
return S_FALSE;
// 得到新的系統時間
status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);
if (status!=NO_ERROR)
return S_FALSE;
// 得到新的CPU空閑時間
status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
if (status != NO_ERROR)
return S_FALSE;
// 如果第一次調用跳過
if (liOldIdleTime.QuadPart != 0)
{
// 當前值 = 最新值 - 舊值
dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);
// 當前CPU空閑時間 = 空閑時間 / 系統時間
dbIdleTime = dbIdleTime / dbSystemTime;
// 當前CPU利用率% = 100 - (當前CPU空閑時間 * 100) / 處理器數
dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;
*plResult = (UINT)dbIdleTime;
}
// 存儲新的CPU空閑及系統時間
liOldIdleTime = SysPerfInfo.liIdleTime;
liOldSystemTime = SysTimeInfo.liKeSystemTime;
return S_OK;
}
// ------------------------------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -