请问VC中有能得到CPU利用率(百分之多少)的函数或办法吗?
解决方案 »
- 怎样让一个窗口一启动就带记时器?
- 关于InvalidateRect函数,在线急等
- CDHtmlDialog的问题
- 求助:关于Sleep函数的使用,仅10分了,HELP……
- http中server回应的响应消息的头域大小有没有限制?
- 这个问题我确实没办法了:视图对象中可以自己结束自己吗?即单文档多视图的程序里如何不通过主框架调用,由一个视图切换到另一视图?
- 请大家帮我看看
- 问题十:200百分请教QQ号、IP地址以及实际地理位置
- "afx"是什么意思?
- win32 console application 和win32 application有什么区别
- 非常简单:struct 怎样调用
- 如何指定CFileFind的查找顺序
//
//////////////////////////////////////////////////////////////////////
// By Mike Ryan ([email protected])
// 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 [email protected], 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;
}
//
//////////////////////////////////////////////////////////////////////
// By Mike Ryan ([email protected])
// 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 [email protected], 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)
//
//////////////////////////////////////////////////////////////////////#ifndef _PERFMON_H
#define _PERFMON_H#include <afxtempl.h>
#include <pdh.h>
#include <pdhmsg.h>#define MAX_RAW_VALUES 20//// DEFINES FOR COUNTER NAMES ////
#define CNTR_CPU "\\Processor(_Total)\\% Processor Time" // % of cpu in use
#define CNTR_MEMINUSE_BYTES "\\Memory\\Committed Bytes" // mem in use measured in bytes
#define CNTR_MEMAVAIL_BYTES "\\Memory\\Available Bytes" // mem available measured in bytes
#define CNTR_MEMAVAIL_KB "\\Memory\\Available KBytes" // mem avail in kilobytes
#define CNTR_MEMAVAIL_MB "\\Memory\\Available MBytes" // mem avail in megabytes
#define CNTR_MEMINUSE_PERCENT "\\Memory\\% Committed Bytes In Use" // % of mem in use
#define CNTR_MEMLIMIT_BYTES "\\Memory\\Commit Limit" // commit limit on memory in bytes// NOTE: Find other counters using the function PdhBrowseCounters() (lookup in MSDN).
// This function was not implemented in this class.typedef struct _tag_PDHCounterStruct {
int nIndex; // The index of this counter, returned by AddCounter()
LONG lValue; // The current value of this counter
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[MAX_RAW_VALUES]; // Ring buffer to contain raw values
} PDHCOUNTERSTRUCT, *PPDHCOUNTERSTRUCT;class CPerfMon
{
public:
CPerfMon();
~CPerfMon(); //// SETUP ////
BOOL Initialize(void);
void Uninitialize(void); //// COUNTERS ////
int AddCounter(const char *pszCounterName);
BOOL RemoveCounter(int nIndex); //// DATA ////
BOOL CollectQueryData(void);
BOOL GetStatistics(long *nMin, long *nMax, long *nMean, int nIndex);
long GetCounterValue(int nIndex);protected:
//// COUNTERS ////
PPDHCOUNTERSTRUCT GetCounterStruct(int nIndex); //// VALUES ////
BOOL UpdateValue(PPDHCOUNTERSTRUCT pCounter);
BOOL UpdateRawValue(PPDHCOUNTERSTRUCT pCounter); //// VARIABLES ////
HQUERY m_hQuery; // the query to the PDH
CArray<PPDHCOUNTERSTRUCT, PPDHCOUNTERSTRUCT> m_aCounters; // the current counters
int m_nNextIndex;
};#endif
// MemoryInfo.cpp
//
// This file contains MemoryInfo, which is basically just determines
// the system memory information and then stortes the results.
// This class COULD provide member functions that would return the
// number of bytes instead of strings. These would probably be overloaded
// functions that take pointer arguments.
//
// Copyright (c) 2001 Paul Wendt [[email protected]]
//
#include "MemoryInfo.h"
#include "SysUtils.h"#include <sstream>
using namespace std;MemoryInfo::MemoryInfo()
{
determineMemoryInfo();
}MemoryInfo::MemoryInfo(const MemoryInfo& source)
{
assign(source);
}MemoryInfo& MemoryInfo::operator=(const MemoryInfo& right)
{
if (this != &right)
{
assign(right);
} return (*this);
}MemoryInfo::~MemoryInfo()
{
// nothing to do yet
}void MemoryInfo::determineMemoryInfo(void)
{
GlobalMemoryStatus(&m_stMemStatus); m_strTotalRam = SysUtils::ByteToStr(m_stMemStatus.dwTotalPhys + 655360, 0);
m_strAvailRam = SysUtils::ByteToStr(m_stMemStatus.dwAvailPhys + 655360);
m_strTotalPageFile = SysUtils::ByteToStr(m_stMemStatus.dwTotalPageFile + 655360);
m_strAvailPageFile = SysUtils::ByteToStr(m_stMemStatus.dwAvailPageFile + 655360);
m_strTotalVirtual = SysUtils::ByteToStr(m_stMemStatus.dwTotalVirtual + 655360);
m_strAvailVirtual = SysUtils::ByteToStr(m_stMemStatus.dwAvailVirtual + 655360);
}void MemoryInfo::assign(const MemoryInfo& source)
{
m_strTotalRam = source.m_strTotalRam;
m_strAvailRam = source.m_strAvailRam;
m_strTotalPageFile = source.m_strTotalPageFile;
m_strAvailPageFile = source.m_strAvailPageFile;
m_strTotalVirtual = source.m_strTotalVirtual;
m_strAvailVirtual = source.m_strAvailVirtual;
m_stMemStatus = source.m_stMemStatus;
}
// MemoryInfo.h
//
// This file contains MemoryInfo, which is basically just determines
// the system memory information and then stortes the results.
// This class COULD provide member functions that would return the
// number of bytes instead of strings. These would probably be overloaded
// functions that take pointer arguments.
//
// Copyright (c) 2001 Paul Wendt [[email protected]]
//
#ifndef MEMORYINFO_H_
#define MEMORYINFO_H_#include "SysInfoClasses.h"
#include <windows.h>
#include <string>class DLL_CLASS MemoryInfo
{
public: // object creation/destruction
MemoryInfo();
MemoryInfo(const MemoryInfo& source);
MemoryInfo& operator=(const MemoryInfo& right);
virtual ~MemoryInfo();public:
// operations
void determineMemoryInfo(void); // attribute modification
virtual inline std::string getTotalRam(void) const;
virtual inline std::string getAvailRam(void) const;
virtual inline std::string getTotalPageFile(void) const;
virtual inline std::string getAvailPageFile(void) const;
virtual inline std::string getTotalVirtual(void) const;
virtual inline std::string getAvailVirtual(void) const;protected: // protected members
void assign(const MemoryInfo& source);private: // attributes
std::string m_strTotalRam;
std::string m_strAvailRam;
std::string m_strTotalPageFile;
std::string m_strAvailPageFile;
std::string m_strTotalVirtual;
std::string m_strAvailVirtual;
MEMORYSTATUS m_stMemStatus;
};inline std::string MemoryInfo::getTotalRam(void) const { return (m_strTotalRam); }
inline std::string MemoryInfo::getAvailRam(void) const { return (m_strAvailRam); }
inline std::string MemoryInfo::getTotalPageFile(void) const { return (m_strTotalPageFile); }
inline std::string MemoryInfo::getAvailPageFile(void) const { return (m_strAvailPageFile); }
inline std::string MemoryInfo::getTotalVirtual(void) const { return (m_strTotalVirtual); }
inline std::string MemoryInfo::getAvailVirtual(void) const { return (m_strAvailVirtual); }#endif
NtQuerySystemInformation