RegPerf.cpp
上传用户:haifei
上传日期:2022-07-20
资源大小:77k
文件大小:7k
源码类别:

网络编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include <winperf.h>
  3. #include <malloc.h>
  4. #include "RegPerf.h"
  5. /********************************************************************
  6. * Function: void XYGetIndex( char *, char * )                        *
  7. *                                                                    *
  8. * Purpose : Get the index for the given counter                      *
  9. *                                                                    *
  10. * Comment : The index is returned in the parameter szIndex           *
  11. *                                                                    *
  12. ********************************************************************/
  13. void XYGetIndexByName( char *pszCounter, char *szIndex ,HKEY hkey)
  14. {
  15.      char*  pszBuffer;
  16.      char*  pszTemp;
  17.      char   szObject[256] = "";
  18.      DWORD  dwBytes;
  19.      HKEY   hKeyIndex;
  20.      int    i = 0;
  21.      int    j = 0;
  22.       // Get the size of the counter.
  23.       RegQueryValueEx( hkey,
  24.                        "Counters",
  25.                        NULL, 
  26.    NULL, 
  27.    NULL,
  28.                        &dwBytes );
  29.       // Allocate memory for the buffer.
  30.       pszBuffer = (char *) HeapAlloc( GetProcessHeap(),
  31.                                       HEAP_ZERO_MEMORY,
  32.                                       dwBytes );
  33.       // Get the titles and counters.
  34.       RegQueryValueEx( hkey,
  35.                        "Counters",
  36.                        NULL, 
  37.    NULL,
  38.                        (LPBYTE)pszBuffer,
  39.                        &dwBytes );
  40.   //
  41.       // Find the index value 
  42.   //
  43.       pszTemp = pszBuffer;
  44.       while( i != (int)dwBytes )
  45.       {
  46.   //
  47.   //Get Index string
  48.           //
  49.           while (*(pszTemp+i) != '')
  50.           {
  51.               szIndex[j] = *(pszTemp+i);
  52.               i++;
  53.               j++;
  54.           }
  55.           szIndex[j] = '';
  56.           i++;
  57.   //
  58.   //Get Counter's Name string
  59.   //
  60.           j = 0;
  61.           while (*(pszTemp+i) != '')
  62.           {
  63.               szObject[j] = *(pszTemp+i);
  64.               i++;
  65.               j++;
  66.           }
  67.           szObject[j] = '';
  68.           i++;
  69.   //
  70.   //Compare if find then break out while circle 
  71.   //
  72.           if( strcmp(szObject, pszCounter) == 0 )
  73.              break;
  74.   //
  75.   //Prepare for next circle
  76.   //
  77.           j = 0;
  78.           if( *(pszTemp+i) == '' )
  79.              i++;
  80.       }
  81.       // Deallocate the memory.
  82.       HeapFree( GetProcessHeap(), 0, (LPVOID)pszBuffer );
  83.       // Close the key.
  84.       //RegCloseKey( hkey );
  85. }
  86. /********************************************************************
  87. * Function: void XYGetRawData( char *, char * )                      *
  88. *                                                                    *
  89. * Purpose : Get the raw data for the counter "%Processor Time"       *
  90. *                                                                    *
  91. * Comment :                                                          *
  92. *                                                                    *
  93. ********************************************************************/
  94. void XYGetRawData(COMPUTE_DATA * pComputeData ,LPTSTR  szIndex ,HKEY hkey)
  95. {
  96. PPERF_DATA_BLOCK PerfDataBlock=NULL;
  97. PPERF_OBJECT_TYPE PerfObjectType;
  98. PPERF_INSTANCE_DEFINITION   PerfInstanceDefinition;
  99. PPERF_COUNTER_DEFINITION    PerfCounterDefinition;
  100. PPERF_COUNTER_BLOCK         PerfCounterBlock;
  101. DWORD BufferSize=BUFFERSIZE;
  102. PerfDataBlock=(PPERF_DATA_BLOCK)malloc(BufferSize);
  103. while(RegQueryValueEx(hkey,
  104.                   (LPTSTR)szIndex,
  105.   NULL,
  106.   NULL,
  107.   (LPBYTE)PerfDataBlock,
  108.                   &BufferSize)==ERROR_MORE_DATA)
  109. {
  110. BufferSize+=INCREMENT;
  111. PerfDataBlock=(PPERF_DATA_BLOCK)realloc(PerfDataBlock,BufferSize);
  112. }
  113.         
  114. pComputeData->llData100NS = *(LONGLONG UNALIGNED *)(&PerfDataBlock->PerfTime100nSec);
  115. //
  116. //Get the first object pointer
  117. //
  118. PerfObjectType=(PPERF_OBJECT_TYPE)
  119.   ((PBYTE)PerfDataBlock+PerfDataBlock->HeaderLength);
  120. if(PerfObjectType->NumInstances)
  121.     PerfCounterDefinition=(PPERF_COUNTER_DEFINITION)
  122. ((PBYTE)PerfObjectType+PerfObjectType->HeaderLength);
  123.     
  124.     PerfInstanceDefinition=(PPERF_INSTANCE_DEFINITION)
  125. ((PBYTE)PerfObjectType+PerfObjectType->DefinitionLength);
  126. //
  127. // Get CounterBlock Pointer
  128. //
  129.     PerfCounterBlock = (PPERF_COUNTER_BLOCK)
  130. ((PBYTE)PerfInstanceDefinition+PerfInstanceDefinition->ByteLength);
  131. //
  132. // Get raw data
  133. //
  134.     if (PerfCounterDefinition->CounterSize <= 4)
  135.     {
  136.         pComputeData->llRawData =* ((DWORD FAR *) 
  137. ((PBYTE)PerfCounterBlock + PerfCounterDefinition->CounterOffset));
  138.         pComputeData->llRawData &= (LONGLONG) (0x0ffffffff);
  139.     }
  140.     else
  141.     {
  142.         pComputeData->llRawData =* ((LONGLONG UNALIGNED*)
  143. ((PBYTE)PerfCounterBlock + PerfCounterDefinition->CounterOffset));
  144.     }
  145. free(PerfDataBlock);
  146. }
  147. /********************************************************************
  148. * Function: FLOAT XYGetPerfData( char *, char * )                    *
  149. *                                                                    *
  150. * Purpose : Get the raw data for the counter "%Processor Time"       *
  151. *                                                                    *
  152. * Comment : Take the difference between the current and previous     *
  153. counts, Normalize the count (counts per interval) divide by the time *
  154. interval (count = % of interval)                                     *
  155.         if (invert)                                                  *
  156.             subtract from 1 (the normalized size of an interval)     *
  157. multiply by 100 (convert to a percentage) this value from 100.       *                                            *
  158. *                                                                    *
  159. ********************************************************************/
  160. FLOAT
  161. XYGetPerfData(IN COMPUTE_DATA Pre_CoumputeData,
  162.               IN COMPUTE_DATA Cur_CoumputeData)
  163. {
  164.     FLOAT   eTimeInterval;
  165.     FLOAT   eDifference;
  166.     FLOAT   eFraction;
  167.     FLOAT   eCount ;
  168.     LONGLONG   liTimeInterval;
  169.     LONGLONG   liDifference;
  170.     // Get the amount of time that has passed since the last sample
  171.     liTimeInterval = Cur_CoumputeData.llData100NS-Pre_CoumputeData.llData100NS;
  172.     eTimeInterval = (FLOAT) (liTimeInterval);
  173.     if (eTimeInterval <= 0.0f)
  174.        return (FLOAT) 0.0f;
  175.     // Get the current and previous counts.
  176.     liDifference = Cur_CoumputeData.llRawData-Pre_CoumputeData.llRawData;
  177.     // Get the number of counts in this time interval.
  178.     // (1, 2, 3 or any number of seconds could have gone by since
  179.     // the last sample)
  180.     eDifference = (FLOAT) (liDifference) ;
  181.     eFraction = eDifference ;
  182.     // Calculate the fraction of time used by what were measuring.
  183.     eCount = eFraction / eTimeInterval ;
  184.     //this is an inverted count take care of the inversion.
  185.     eCount = (FLOAT) 1.0 - eCount ;
  186.     // Scale the value to up to 100.
  187.     eCount *= 100.0f ;
  188.     if (eCount < 0.0f) eCount = 0.0f ;
  189.     if (eCount > 100.0f )
  190. {
  191.         eCount = 100.0f;
  192.     }
  193.     return(eCount) ;