16,471
社区成员
发帖
与我相关
我的任务
分享
#include "StdAfx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <pdh.h>
#include "Performance.h"
#include "Log.h"
#include <vector>
using namespace std;
#pragma comment ( lib , "Pdh.lib" )
bool CPerCounter::m_bIsInitPerCounter = false;
bool CWebSiteFlow::m_bIsInitWebSiteFlow = false;
bool CWebSiteConnect::m_bIsInitWebSiteConnect = false;
CPerformance::CPerformance(void)
{
m_hQuery = NULL;
m_CounterHandle = NULL;
OpenQuery();
};
void CPerformance::OpenQuery()
{
PDH_STATUS pdhStatus = PdhOpenQuery (0, 0, &m_hQuery);
#ifdef DEBUG_PERFORMANCE
if( ERROR_SUCCESS != pdhStatus)
{
CString strDebug;
strDebug.Format("open query failed with :%d",pdhStatus);
IBLOG(strDebug);
}
#endif
}
CPerformance::~CPerformance(void)
{
if( m_hQuery )
{
PDH_STATUS pdhStatus = PdhCloseQuery (m_hQuery);
}
};
void CPerformance::Init(LPTSTR countPath)
{
#ifdef DEBUG_PERFORMANCE
CString strTest;
strTest.Format("CPerformance::Init %s", countPath);
IBLOG(strTest);
#endif
CHAR szPathBuffer[256] = {'\0'};
strcat(szPathBuffer,countPath);
PDH_STATUS pdhStatus;
pdhStatus = PdhAddCounter (m_hQuery,
szPathBuffer,
0,
&m_CounterHandle);
#ifdef DEBUG_PERFORMANCE
if( ERROR_SUCCESS != pdhStatus)
{
CString strDebug;
strDebug.Format("PdhAddCounter %s failed with :%d",countPath,pdhStatus);
IBLOG(strDebug);
}
else
{
CString strDebug;
strDebug.Format("PdhAddCounter %s succeed",countPath);
IBLOG(strDebug);
}
#endif
}
int CPerformance::GetObjValue()
{
int nRetCode;
if( m_hQuery )
{
PDH_FMT_COUNTERVALUE fmtValue;
DWORD ctrType;
PDH_STATUS pdhStatus;
pdhStatus = PdhCollectQueryData (m_hQuery);
#ifdef DEBUG_PERFORMANCE
if( ERROR_SUCCESS != pdhStatus)
{
CString strDebug;
strDebug.Format("PdhCollectQueryData failed with :%d",pdhStatus);
IBLOG(strDebug);
return 0;
}
#endif
pdhStatus = PdhGetFormattedCounterValue (m_CounterHandle,
PDH_FMT_DOUBLE,
&ctrType,
&fmtValue);
#ifdef DEBUG_PERFORMANCE
if( ERROR_SUCCESS != pdhStatus)
{
CString strDebug;
strDebug.Format("PdhGetFormattedCounterValue failed with :%d",pdhStatus);
IBLOG(strDebug);
}
#endif
return nRetCode=(int)fmtValue.doubleValue;
}
else
{
#ifdef DEBUG_PERFORMANCE
IBLOG("get objvalue with empty query handle");
#endif
}
return 0;
}
vector<string> CPerformance::GetCountInstance(LPTSTR countObj)
{
vector<string> vecResult;
PDH_STATUS pdhStatus ;
LPTSTR szCounterListBuffer = NULL ;
DWORD dwCounterListSize = 0;
LPTSTR szInstanceListBuffer = NULL ;
DWORD dwInstanceListSize = 0;
LPTSTR szThisInstance = NULL ;
LPTSTR szThisCounter = NULL ;
// 首先调用该函数获得接受计数器列表的缓冲区大小
pdhStatus = PdhEnumObjectItems (
NULL , // [IN]数据源,NT4.0必须为NULL
NULL , // [IN]机器名。本地机器为NULL
countObj , // [IN]计数器名
szCounterListBuffer , // [OUT]接收计数器列表的缓冲区,如果计数器列表长度为0,则该项为空
& dwCounterListSize , // [IN/OUT]设置或接收计数器列表长度
szInstanceListBuffer , // [OUT]接收实例列表的缓冲区,如果计数器列表长度为0,则该项为空
& dwInstanceListSize , // [IN/OUT]设置或接收实例列表长度
PERF_DETAIL_WIZARD , // 获取信息的级别
// PERF_DETAIL_NOVICE 初级级别
// PERF_DETAIL_ADVANCE 高级级别(包含初级)
// PERF_DETAIL_EXPERT 专家级别(包含初级和高级)
// PERF_DETAIL_WIZARD 系统级别(包含所有级别)
0 ) ; // 最后一个参数系统保留为0
// 根据得到的缓冲区大小分配计数器列表缓冲区内存
szCounterListBuffer = ( LPTSTR ) malloc ( dwCounterListSize * sizeof ( TCHAR ) ) ;
// 根据得到的缓冲区大小分配实例名列表缓冲区内存
szInstanceListBuffer = ( LPTSTR ) malloc ( dwInstanceListSize * sizeof ( TCHAR ) ) ;
#ifdef DEBUG_PERFORMANCE
CString strTest;
strTest.Format("dwCounterListSize=%d dwInstanceListSize=%d",dwCounterListSize,dwInstanceListSize);
IBLOG(strTest);
#endif
if( szCounterListBuffer && szInstanceListBuffer)
{
// 获取指定计数器对象的所有计数器和实例
pdhStatus = PdhEnumObjectItems (
NULL , // [IN]数据源,NT4.0必须为NULL
NULL , // [IN]机器名。本地机器为NULL
countObj , // [IN]计数器名
szCounterListBuffer , // [OUT]接收计数器列表的缓冲区,如果计数器列表长度为0,则该项为空
& dwCounterListSize , // [IN/OUT]设置或接收计数器列表长度
szInstanceListBuffer , // [OUT]接收实例列表的缓冲区,如果计数器列表长度为0,则该项为空
& dwInstanceListSize , // [IN/OUT]设置或接收实例列表长度
PERF_DETAIL_WIZARD , // 获取信息的级别
0 ) ; // 最后一个参数系统保留为0
if (pdhStatus == ERROR_SUCCESS)
{
szThisInstance = szInstanceListBuffer ;
//szThisCounter = szCounterListBuffer ;
for ( ; * szThisInstance != 0 ; szThisInstance += ( lstrlen ( szThisInstance ) + 1 ) )
{
// 每循环一次 szThisInstance 就是杖举到的计数器对象
//printf(szThisInstance);
string strInstance = szThisInstance;
vecResult.push_back(strInstance);
}
}
else
{
#ifdef DEBUG_PERFORMANCE
CString strTest;
strTest.Format("PdhEnumObjectItems failed %d",(unsigned int)pdhStatus );
IBLOG(strTest);
#endif
}
}
if(szInstanceListBuffer)
{
free(szInstanceListBuffer);
szInstanceListBuffer=NULL;
}
if( szCounterListBuffer )
{
free(szCounterListBuffer);
szCounterListBuffer=NULL;
}
return vecResult;
}
// 获得主机总物理内存
int CPerformance::GetTotalPhysicalMemory(void)
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
if (GlobalMemoryStatusEx (&statex))
{
return statex.ullTotalPhys/(1024*1024);
}
return -1;
}