有谁知道windows操作系统察看cpu使用率的命令

dtc030 2003-03-13 05:03:37
有谁知道windows操作系统察看cpu使用率的命令
...全文
584 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chxinheifeng 2003-08-22
  • 打赏
  • 举报
回复
vc
1注册表的性能数据库
2toolhelp函数组查msdn
pdhOpenQuery pdhAddCounter pdhCollectQueryData
pdhGetFormattedCounterValue
alan990699 2003-08-22
  • 打赏
  • 举报
回复
treasure up,thanks
dtc030 2003-03-14
  • 打赏
  • 举报
回复
非常多谢大家!
KennyYuan 2003-03-14
  • 打赏
  • 举报
回复
呵呵,我也学习ed
child_bj 2003-03-13
  • 打赏
  • 举报
回复
以下是获取进程的信息的代码,有一个选择窗口自己选择需要显示的信息
.h文件
typedef struct _tag_PDHCounterStruct {
HCOUNTER hCounter; // Handle to the counter - given to use by PDH Library
int nNextIndex; // element to get the next raw value
int nOldestIndex; // element containing the oldes raw value
int nRawCount; // number of elements containing raw values
PDH_RAW_COUNTER a_RawValue[1024]; // Ring buffer to contain raw values
} PDHCOUNTERSTRUCT, *PPDHCOUNTERSTRUCT;

PDH_STATUS __stdcall PDH_BrowseCallback(DWORD dwParam);
BOOL PDH_AddCounter(LPTSTR szCounterName, int nItemIndex);
void InitQuery();
extern PDH_BROWSE_DLG_CONFIG gpdhBrowseDlgConfig;
extern PPDHCOUNTERSTRUCT MyCounter;
extern HQUERY hQuery;
extern LPTSTR gszReturnPath;
extern DWORD gdwReturnPathSize;
extern PACKAGE TForm1 *Form1;
extern nCounterIndex;

.cpp文件
void InitQuery()
{
PDH_STATUS pdhStatus = PdhOpenQuery (NULL, 0, &hQuery);
if (IsErrorSeverity(pdhStatus))
{
return ;
}

MyCounter = (PPDHCOUNTERSTRUCT)GlobalAlloc(GPTR, sizeof(PDHCOUNTERSTRUCT)*1024);
gdwReturnPathSize = 1024;
gszReturnPath = (LPTSTR) LocalAlloc(LPTR, gdwReturnPathSize);
nCounterIndex = 0;
}
int TForm1::GetProcessCount()
{
PDH_STATUS pdhStatus;
PDH_FMT_COUNTERVALUE fmtValue;
DWORD ctrType;
int nRetCode = 0;

pdhStatus = PdhCollectQueryData (hQuery);

// Get the current value of this counter.
for(int i=0;i<ListView2->Items->Count;i++)
{
pdhStatus = PdhGetFormattedCounterValue (MyCounter[i].hCounter,PDH_FMT_LONG,
NULL,&fmtValue);
if (pdhStatus == ERROR_SUCCESS)
{
ListView2->Items->Item[i]->SubItems->Strings[0] = FormatFloat("#,###,###,##0.",fmtValue.longValue);
}
}
/* else
{
// Print the error value.
ShowMessage("错误");;
}*/

return 0;

}

int TForm1::GetTotalCommittedMemory(int ProcessID)
{
HANDLE hProcess;
Pointer pAddr =0;
int dwTotalCommit =0;
int ret;
MEMORY_BASIC_INFORMATION mi;
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,0,ProcessID);
ret = VirtualQueryEx((char*)hProcess,(char*)pAddr,&mi,sizeof(mi));

do
{
pAddr = (char *)(mi.BaseAddress) + mi.RegionSize;
if((int)pAddr > 0x7fffffff)
break;
if(mi.State == MEM_COMMIT)
{
dwTotalCommit +=mi.RegionSize;
}
ret = VirtualQueryEx((char*)hProcess,(char*)pAddr,&mi,sizeof(mi));
}while(ret == sizeof(mi));
CloseHandle(hProcess);
return dwTotalCommit/1024;
}



void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
GetProcessCount();
}
//---------------------------------------------------------------------------
#include <pdhmsg.h>
PDH_BROWSE_DLG_CONFIG gpdhBrowseDlgConfig;
LPTSTR gszReturnPath;
DWORD gdwReturnPathSize;
PPDHCOUNTERSTRUCT MyCounter;
HQUERY hQuery;
int nCounterIndex;

PDH_STATUS __stdcall PDH_BrowseCallback(DWORD dwParam)
{
PDH_STATUS returnValue;
LPTSTR szCurrentPath;
int nLen;

if (gpdhBrowseDlgConfig.CallBackStatus == PDH_MORE_DATA) {
// Buffer too small for the counters selected

// Free original buffer
if (NULL != LocalFree(gszReturnPath)) {
return PDH_MEMORY_ALLOCATION_FAILURE;
}

// Alloc new larger buffer
gdwReturnPathSize += 1024;
gszReturnPath = (LPTSTR) LocalAlloc(LPTR, gdwReturnPathSize);
if (gszReturnPath == NULL) {
return PDH_MEMORY_ALLOCATION_FAILURE;
}

// Set the config structure members for the new buffer
gpdhBrowseDlgConfig.szReturnPathBuffer = gszReturnPath;
gpdhBrowseDlgConfig.cchReturnPathLength = gdwReturnPathSize;

// retry the counter browse selection
return PDH_RETRY;
}

returnValue = ERROR_SUCCESS;
szCurrentPath = gszReturnPath;
// This string manipulation code is in a try/except block
// to gracefully handle any problems
// with pointer values, null termination, boundary conditions, etc.
__try {
while (TRUE) {
nLen = lstrlen(szCurrentPath);

if (nLen == 0)
break;

PDH_AddCounter(szCurrentPath, nCounterIndex);
nCounterIndex++;
szCurrentPath += (nLen + 1);
}
}
__except(EXCEPTION_EXECUTE_HANDLER) {
returnValue = PDH_INVALID_BUFFER;
}

return returnValue ;
}


void __fastcall TForm1::Button9Click(TObject *Sender)
{

ZeroMemory(&gpdhBrowseDlgConfig, sizeof(gpdhBrowseDlgConfig));


gpdhBrowseDlgConfig.hWndOwner = this->Handle;

gpdhBrowseDlgConfig.bIncludeInstanceIndex = false;
gpdhBrowseDlgConfig.bSingleCounterPerAdd = false;
gpdhBrowseDlgConfig.bSingleCounterPerDialog = false;
gpdhBrowseDlgConfig.bLocalCountersOnly = true;
gpdhBrowseDlgConfig.bWildCardInstances = true;
gpdhBrowseDlgConfig.bHideDetailBox = true;
gpdhBrowseDlgConfig.bInitializePath = true;
gpdhBrowseDlgConfig.szReturnPathBuffer = gszReturnPath;
gpdhBrowseDlgConfig.cchReturnPathLength = gdwReturnPathSize;

gpdhBrowseDlgConfig.pCallBack = PDH_BrowseCallback;

__try {
PdhBrowseCounters(&gpdhBrowseDlgConfig);
}
__except(EXCEPTION_EXECUTE_HANDLER) {
// UpdateStatus(TEXT("Exception in PdhBrowseCounters. (Handled)"));
// MessageBeep(0);
}


}
//---------------------------------------------------------------------------
BOOL PDH_AddCounter(LPTSTR szCounterName, int nItemIndex)
{
BOOL fRes = TRUE;

// Allocate a PDHCOUNTERSTRUCT for this new counter

// Add the counter to the list view control


__try {
// Add the counter to the current query
if (ERROR_SUCCESS == PdhAddCounter(hQuery, szCounterName, (DWORD)(&MyCounter[nItemIndex]), &(MyCounter[nItemIndex].hCounter)))
{
TListItem * Item = Form1->ListView2->Items->Add();
Item->Caption = szCounterName;
Item->SubItems->Add("");
fRes = FALSE;
}
}
__except(EXCEPTION_EXECUTE_HANDLER) {
fRes = FALSE;
}

// If the add failed, then clean up the list view and the counter struct

return fRes;
}

child_bj 2003-03-13
  • 打赏
  • 举报
回复
转一篇BCB版里讨论的结果

====================================================================

一句话:在一个特定时间段内计算特定进程的总时间和所有进程的总时间,它们的比值就是那个特定进程的确CPU占有率,这里我采用1秒种。

====================================================================

typedef struct _THREAD_INFO
{
LARGE_INTEGER CreateTime;
DWORD dwUnknown1;
DWORD dwStartAddress;
DWORD StartEIP;
DWORD dwOwnerPID;
DWORD dwThreadId;
DWORD dwCurrentPriority;
DWORD dwBasePriority;
DWORD dwContextSwitches;
DWORD Unknown;
DWORD WaitReason;

}THREADINFO, *PTHREADINFO;

typedef struct _UNICODE_STRING
{
USHORT Length;
USHORT MaxLength;
PWSTR Buffer;

} UNICODE_STRING;

typedef struct _PROCESS_INFO
{
DWORD dwOffset;
DWORD dwThreadsCount;
DWORD dwUnused1[6];
LARGE_INTEGER CreateTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER KernelTime;
UNICODE_STRING ProcessName;

DWORD dwBasePriority;
DWORD dwProcessID;
DWORD dwParentProcessId;
DWORD dwHandleCount;
DWORD dwUnused3[2];

DWORD dwVirtualBytesPeak;
DWORD dwVirtualBytes;
ULONG dwPageFaults;
DWORD dwWorkingSetPeak;
DWORD dwWorkingSet;
DWORD dwQuotaPeakPagedPoolUsage;
DWORD dwQuotaPagedPoolUsage;
DWORD dwQuotaPeakNonPagedPoolUsage;
DWORD dwQuotaNonPagedPoolUsage;
DWORD dwPageFileUsage;
DWORD dwPageFileUsagePeak;

DWORD dCommitCharge;
THREADINFO ThreadSysInfo[1];

} PROCESSINFO, *PPROCESSINFO;


//每秒钟查询一次
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Button2Click(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
PVOID pProcInfo = NULL;
DWORD dwInfoSize = 0x20000;
PPROCESSINFO pProcessInfo;
DWORD dwWorkingSet;
long ( __stdcall *NtQuerySystemInformation )( DWORD, PVOID, DWORD, DWORD );


static __int64 LastTotalProcessCPUUsage = 0;
static __int64 LastCurrentProcessCPUUsage = 0;

int CurrentDelta;
int TotalDelta;

__int64 TotalProcessCPUUsage = 0;
__int64 CurrentProcessCPUUsage = 0;

/////////////////////////////////

pProcInfo = (PVOID)(new byte[dwInfoSize]);

NtQuerySystemInformation = (long(__stdcall*)(DWORD,PVOID,DWORD,DWORD))
GetProcAddress( GetModuleHandle( "ntdll.dll" ),"NtQuerySystemInformation" );

NtQuerySystemInformation(5,pProcInfo,dwInfoSize,0);

pProcessInfo = (PPROCESSINFO)pProcInfo;

do
{
TotalProcessCPUUsage += (__int64)pProcessInfo->KernelTime.QuadPart + (__int64)pProcessInfo->UserTime.QuadPart;

if(pProcessInfo->dwProcessID == GetCurrentProcessId())
{
dwWorkingSet = pProcessInfo->dwWorkingSet;
CurrentProcessCPUUsage += (__int64)pProcessInfo->KernelTime.QuadPart + (__int64)pProcessInfo->UserTime.QuadPart;
}

/////////
if(pProcessInfo->dwOffset == 0)
{
break;
}

pProcessInfo = (PPROCESSINFO)((byte*)pProcessInfo + pProcessInfo->dwOffset);
}
while(true);

TotalDelta = TotalProcessCPUUsage - LastTotalProcessCPUUsage;
CurrentDelta = CurrentProcessCPUUsage - LastCurrentProcessCPUUsage;

if(TotalDelta != 0)
this->Caption = "CPU = " + IntToStr(100 * CurrentDelta / TotalDelta) +
"Memory = "+ IntToStr(dwWorkingSet / 1024) " K";

LastTotalProcessCPUUsage = TotalProcessCPUUsage;
LastCurrentProcessCPUUsage = CurrentProcessCPUUsage;

delete[] pProcInfo;
}




smzh8 2003-03-13
  • 打赏
  • 举报
回复
获得CPU的使用率可以通过注册表,读(HKEY_DYN_DATA\PerfStats\(StatData或StartStat)\KERNELCPUUsage值

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧