RegPerf.cpp
上传用户:haifei
上传日期:2022-07-20
资源大小:77k
文件大小:7k
- #include "stdafx.h"
- #include <winperf.h>
- #include <malloc.h>
- #include "RegPerf.h"
- /********************************************************************
- * Function: void XYGetIndex( char *, char * ) *
- * *
- * Purpose : Get the index for the given counter *
- * *
- * Comment : The index is returned in the parameter szIndex *
- * *
- ********************************************************************/
- void XYGetIndexByName( char *pszCounter, char *szIndex ,HKEY hkey)
- {
- char* pszBuffer;
- char* pszTemp;
- char szObject[256] = "";
- DWORD dwBytes;
- HKEY hKeyIndex;
- int i = 0;
- int j = 0;
- // Get the size of the counter.
- RegQueryValueEx( hkey,
- "Counters",
- NULL,
- NULL,
- NULL,
- &dwBytes );
- // Allocate memory for the buffer.
- pszBuffer = (char *) HeapAlloc( GetProcessHeap(),
- HEAP_ZERO_MEMORY,
- dwBytes );
- // Get the titles and counters.
- RegQueryValueEx( hkey,
- "Counters",
- NULL,
- NULL,
- (LPBYTE)pszBuffer,
- &dwBytes );
- //
- // Find the index value
- //
- pszTemp = pszBuffer;
- while( i != (int)dwBytes )
- {
- //
- //Get Index string
- //
- while (*(pszTemp+i) != '')
- {
- szIndex[j] = *(pszTemp+i);
- i++;
- j++;
- }
- szIndex[j] = '';
- i++;
- //
- //Get Counter's Name string
- //
- j = 0;
- while (*(pszTemp+i) != '')
- {
- szObject[j] = *(pszTemp+i);
- i++;
- j++;
- }
- szObject[j] = '';
- i++;
- //
- //Compare if find then break out while circle
- //
- if( strcmp(szObject, pszCounter) == 0 )
- break;
- //
- //Prepare for next circle
- //
- j = 0;
- if( *(pszTemp+i) == '' )
- i++;
- }
- // Deallocate the memory.
- HeapFree( GetProcessHeap(), 0, (LPVOID)pszBuffer );
- // Close the key.
- //RegCloseKey( hkey );
- }
- /********************************************************************
- * Function: void XYGetRawData( char *, char * ) *
- * *
- * Purpose : Get the raw data for the counter "%Processor Time" *
- * *
- * Comment : *
- * *
- ********************************************************************/
- void XYGetRawData(COMPUTE_DATA * pComputeData ,LPTSTR szIndex ,HKEY hkey)
- {
- PPERF_DATA_BLOCK PerfDataBlock=NULL;
- PPERF_OBJECT_TYPE PerfObjectType;
- PPERF_INSTANCE_DEFINITION PerfInstanceDefinition;
- PPERF_COUNTER_DEFINITION PerfCounterDefinition;
- PPERF_COUNTER_BLOCK PerfCounterBlock;
- DWORD BufferSize=BUFFERSIZE;
- PerfDataBlock=(PPERF_DATA_BLOCK)malloc(BufferSize);
- while(RegQueryValueEx(hkey,
- (LPTSTR)szIndex,
- NULL,
- NULL,
- (LPBYTE)PerfDataBlock,
- &BufferSize)==ERROR_MORE_DATA)
- {
- BufferSize+=INCREMENT;
- PerfDataBlock=(PPERF_DATA_BLOCK)realloc(PerfDataBlock,BufferSize);
- }
-
- pComputeData->llData100NS = *(LONGLONG UNALIGNED *)(&PerfDataBlock->PerfTime100nSec);
- //
- //Get the first object pointer
- //
- PerfObjectType=(PPERF_OBJECT_TYPE)
- ((PBYTE)PerfDataBlock+PerfDataBlock->HeaderLength);
- if(PerfObjectType->NumInstances)
- PerfCounterDefinition=(PPERF_COUNTER_DEFINITION)
- ((PBYTE)PerfObjectType+PerfObjectType->HeaderLength);
-
- PerfInstanceDefinition=(PPERF_INSTANCE_DEFINITION)
- ((PBYTE)PerfObjectType+PerfObjectType->DefinitionLength);
- //
- // Get CounterBlock Pointer
- //
- PerfCounterBlock = (PPERF_COUNTER_BLOCK)
- ((PBYTE)PerfInstanceDefinition+PerfInstanceDefinition->ByteLength);
-
- //
- // Get raw data
- //
- if (PerfCounterDefinition->CounterSize <= 4)
- {
- pComputeData->llRawData =* ((DWORD FAR *)
- ((PBYTE)PerfCounterBlock + PerfCounterDefinition->CounterOffset));
- pComputeData->llRawData &= (LONGLONG) (0x0ffffffff);
- }
- else
- {
- pComputeData->llRawData =* ((LONGLONG UNALIGNED*)
- ((PBYTE)PerfCounterBlock + PerfCounterDefinition->CounterOffset));
- }
- free(PerfDataBlock);
- }
- /********************************************************************
- * Function: FLOAT XYGetPerfData( char *, char * ) *
- * *
- * Purpose : Get the raw data for the counter "%Processor Time" *
- * *
- * Comment : Take the difference between the current and previous *
- counts, Normalize the count (counts per interval) divide by the time *
- interval (count = % of interval) *
- if (invert) *
- subtract from 1 (the normalized size of an interval) *
- multiply by 100 (convert to a percentage) this value from 100. * *
- * *
- ********************************************************************/
- FLOAT
- XYGetPerfData(IN COMPUTE_DATA Pre_CoumputeData,
- IN COMPUTE_DATA Cur_CoumputeData)
- {
- FLOAT eTimeInterval;
- FLOAT eDifference;
- FLOAT eFraction;
- FLOAT eCount ;
- LONGLONG liTimeInterval;
- LONGLONG liDifference;
- // Get the amount of time that has passed since the last sample
- liTimeInterval = Cur_CoumputeData.llData100NS-Pre_CoumputeData.llData100NS;
- eTimeInterval = (FLOAT) (liTimeInterval);
- if (eTimeInterval <= 0.0f)
- return (FLOAT) 0.0f;
- // Get the current and previous counts.
- liDifference = Cur_CoumputeData.llRawData-Pre_CoumputeData.llRawData;
- // Get the number of counts in this time interval.
- // (1, 2, 3 or any number of seconds could have gone by since
- // the last sample)
- eDifference = (FLOAT) (liDifference) ;
- eFraction = eDifference ;
- // Calculate the fraction of time used by what were measuring.
- eCount = eFraction / eTimeInterval ;
- //this is an inverted count take care of the inversion.
- eCount = (FLOAT) 1.0 - eCount ;
- // Scale the value to up to 100.
- eCount *= 100.0f ;
- if (eCount < 0.0f) eCount = 0.0f ;
- if (eCount > 100.0f )
- {
- eCount = 100.0f;
- }
- return(eCount) ;
- }