如何取得CPU使用的百分比?

richmain 2002-05-16 09:49:35
如何取得系统资源包括CPU使用百分比?
...全文
26 点赞 收藏 11
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
richmain 2002-05-25
怎样能支持95、98
回复
richmain 2002-05-22
那有什么办法都支持呢?
回复
zhuwenzheng 2002-05-21
好象不支持95,98,但是支持NT,2000
回复
richmain 2002-05-20
现在还不知道,PDH是不是可以适用于所有的Win95和NT40以上的系统?
回复
zhuwenzheng 2002-05-20
当然不同了,95,98一样,NT,2000一样
回复
richmain 2002-05-20
适用于所有的Win95和NT40以上的系统吗?
回复
kkk16 2002-05-17
给个例子给你看看吧,它可以获得很多参数,包括CPU

// PerfMon.h: interface for the CPerfMon class.
//
//////////////////////////////////////////////////////////////////////
// By Mike Ryan (mike@codexia.com)
// Copyright (c) 2000, portions (c) Allen Denver
// 07.30.2000
//
// Some of the code based on Allen Denver's article "Using the Performance Data Helper Library"
//
// Free usage granted in all applications including commercial.
// Do NOT distribute without permission from me. I can be reached
// at mike@codexia.com, http://www.codexia.com
// Please feel free to email me about this class.
//
// Compatibility:
// Windows 98, Windows NT 4.0 SP 3 (Dlls required), Windows 2000
//
// Development Environ:
// Visual C++ 6.0
//
// Libraries / DLLs:
// pdh.lib (linked in)
// pdh.dll (provided with Windows 2000, must copy in for NT 4.0)
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "perfmon.h"

CPerfMon::CPerfMon()
{
// m_nNextIndex is a unique value. It will not be decremented, even if you remove counters.
m_nNextIndex = 0;
}

CPerfMon::~CPerfMon()
{
}


// Function name : CPerfMon::Initialize
// Description : Initialize the query and memory
// Return type : BOOL ; true on success; false on fail
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
BOOL CPerfMon::Initialize()
{
if (PdhOpenQuery(NULL, 1, &m_hQuery) != ERROR_SUCCESS)
return false;

return true;
}


// Function name : CPerfMon::Uninitialize
// Description : Closes the query and fress all memory
// Return type : void
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
void CPerfMon::Uninitialize()
{
PdhCloseQuery(&m_hQuery);

// clean memory
for (int i=0;i<m_aCounters.GetSize();i++)
{
delete m_aCounters.GetAt(i);
}
}


// Function name : CPerfMon::AddCounter
// Description : Adds a counter to the query.
// Return type : int ; -1 on fail, index to counter on success.
// Argument : const char *pszCounterName
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
int CPerfMon::AddCounter(const char *pszCounterName)
{
PPDHCOUNTERSTRUCT pCounter;
pCounter = new PDHCOUNTERSTRUCT;
if (!pCounter) return -1;

// add to current query
if (PdhAddCounter(m_hQuery, pszCounterName, (DWORD)pCounter, &(pCounter->hCounter)) != ERROR_SUCCESS)
{
delete pCounter; // clean mem
return -1;
}

// insert counter into array(s)
pCounter->nIndex = m_nNextIndex++;
pCounter->lValue = 0;
pCounter->nNextIndex = 0;
pCounter->nOldestIndex = 0;
pCounter->nRawCount = 0;
m_aCounters.Add(pCounter);

return pCounter->nIndex;
}


// Function name : CPerfMon::RemoveCounter
// Description : remove a counter from the query based on the index returned from AddCounter
// Return type : BOOL ; false on fail ; true on success
// Argument : int nIndex
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
BOOL CPerfMon::RemoveCounter(int nIndex)
{
PPDHCOUNTERSTRUCT pCounter = GetCounterStruct(nIndex);
if (!pCounter) return false;

if (PdhRemoveCounter(pCounter->hCounter) != ERROR_SUCCESS)
return false;

return true;
}


// Function name : CPerfMon::CollectQueryData
// Description : Collects the data for all the counters added with AddCounter()
// Return type : BOOL ; false fail ; true success
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
BOOL CPerfMon::CollectQueryData()
{
if (PdhCollectQueryData(m_hQuery) != ERROR_SUCCESS) return false;

return true;
}


// Function name : CPerfMon::UpdateValue
// Description : Updates the counter value for the counter in pCounter
// Return type : BOOL ; false fail ; true success
// Argument : PPDHCOUNTERSTRUCT pCounter
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
BOOL CPerfMon::UpdateValue(PPDHCOUNTERSTRUCT pCounter)
{
PDH_FMT_COUNTERVALUE pdhFormattedValue;

// get the value from the PDH
if (PdhGetFormattedCounterValue(pCounter->hCounter, PDH_FMT_LONG, NULL, &pdhFormattedValue) != ERROR_SUCCESS)
return false;

// test the value for validity
if (pdhFormattedValue.CStatus != ERROR_SUCCESS)
return false;

// set value
pCounter->lValue = pdhFormattedValue.longValue;

return true;
}


// Function name : CPerfMon::UpdateRawValue
// Description : Update the raw values for the counter in pCounter
// Return type : BOOL ; false fail ; true success
// Argument : PPDHCOUNTERSTRUCT pCounter
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
BOOL CPerfMon::UpdateRawValue(PPDHCOUNTERSTRUCT pCounter)
{
PPDH_RAW_COUNTER ppdhRawCounter;

// Assign the next value into the array
ppdhRawCounter = &(pCounter->a_RawValue[pCounter->nNextIndex]);

if (PdhGetRawCounterValue(pCounter->hCounter, NULL, ppdhRawCounter) != ERROR_SUCCESS)
return false;

// update raw counter - up to MAX_RAW_VALUES
pCounter->nRawCount = min(pCounter->nRawCount + 1, MAX_RAW_VALUES);

// Update next index - rolls back to zero upon reaching MAX_RAW_VALUES
pCounter->nNextIndex = (pCounter->nNextIndex + 1) % MAX_RAW_VALUES;

// The Oldest index remains zero until the array is filled.
// It will now be the same as the 'next' index since it was previously assigned.
if (pCounter->nRawCount >= MAX_RAW_VALUES)
pCounter->nOldestIndex = pCounter->nNextIndex;

return true;
}

BOOL CPerfMon::GetStatistics(long *nMin, long *nMax, long *nMean, int nIndex)
{
PDH_STATISTICS pdhStats;
PPDHCOUNTERSTRUCT pCounter = GetCounterStruct(nIndex);
if (!pCounter) return false;

if (PdhComputeCounterStatistics(pCounter->hCounter, PDH_FMT_LONG, pCounter->nOldestIndex, pCounter->nRawCount, pCounter->a_RawValue, &pdhStats) != ERROR_SUCCESS)
return false;

// set values
if (pdhStats.min.CStatus != ERROR_SUCCESS)
*nMin = 0;
else
*nMin = pdhStats.min.longValue;

if (pdhStats.max.CStatus != ERROR_SUCCESS)
*nMax = 0;
else
*nMax = pdhStats.max.longValue;

if (pdhStats.mean.CStatus != ERROR_SUCCESS)
*nMean = 0;
else
*nMean = pdhStats.mean.longValue;

return true;
}


// Function name : CPerfMon::GetCounterValue
// Description : return the value of the counter
// Return type : long ; -999 on failed ; value on success
// Argument : int nIndex
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
long CPerfMon::GetCounterValue(int nIndex)
{
PPDHCOUNTERSTRUCT pCounter = GetCounterStruct(nIndex);
if (!pCounter) return -999L;

// update the value(s)
if (!UpdateValue(pCounter)) return -999L;
if (!UpdateRawValue(pCounter)) return -999L;

// return the value
return pCounter->lValue;
}


// Function name : CPerfMon::GetCounterStruct
// Description : Lookup a counterstruct based on the index
// Return type : PPDHCOUNTERSTRUCT ; null on failed ; pointer to counter struct on success
// Argument : int nIndex
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
PPDHCOUNTERSTRUCT CPerfMon::GetCounterStruct(int nIndex)
{
for (int i=0;i<m_aCounters.GetSize();i++)
{
if (m_aCounters.GetAt(i)->nIndex == nIndex)
return m_aCounters.GetAt(i);
}

return NULL;
}
回复
masterz 2002-05-16
study the sample provided by MSDN(Using the PDH Interface)
StatList Sample: PDH Counter Statistics Demo Application
回复
zhuwenzheng 2002-05-16
http://www.vckbase.com/bbs/prime/viewprime.asp?id=351
http://www.vckbase.com/bbs/prime/viewprime.asp?id=352
回复
richmain 2002-05-16
To zhuwenzheng:

352中的例程是不是只适用于NT呢?对与Win95和Win98等系统适用吗?

请大峡多多指教
回复
phoenix_oyh 2002-05-16
多谢 zhuwenzheng
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2002-05-16 09:49
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……