static void loadUdpData(void) {
  PPERF_COUNTER_DEFINITION PerfCntr;
  DWORD c;

  if (UdpObj == NULL)
    return;
  PerfCntr = FirstCounter(UdpObj);
  for (c=0; c < UdpObj->NumCounters; c++) {
    switch (PerfCntr->CounterNameTitleIndex) {
    case DATAGRAMS_RECEIVED_PER_SEC_ID:
      udpCtrCache[DATAGRAMS_RECEIVED_PER_SEC_IDX] = *PerfCntr;
      break;
    case DATAGRAMS_NO_PORT_PER_SEC_ID:
      udpCtrCache[DATAGRAMS_NO_PORT_PER_SEC_IDX] = *PerfCntr;
      break;
    case DATAGRAMS_RECEIVED_ERRORS_ID:
      udpCtrCache[DATAGRAMS_RECEIVED_ERRORS_IDX] = *PerfCntr;
      break;
    case DATAGRAMS_SENT_PER_SEC_ID:
      udpCtrCache[DATAGRAMS_SENT_PER_SEC_IDX] = *PerfCntr;
      break;
    default:
      /* unknown counter. just skip it. */
      break;
      }
    PerfCntr = NextCounter(PerfCntr);
  }
#ifdef FLG_DEBUG
  for (c=0; c < MAX_UDP_CTRS_COLLECTED; c++) {
    if (udpCtrCache[c].CounterNameTitleIndex == 0) {
      fprintf(stderr, "DEBUG: bad udpCtr at idx=%d\n", c); fflush(stderr);
    }
  }
#endif
  }
Esempio n. 2
0
BOOL KProcessPerfMgr::_UpdateCounterInfo(PPERF_OBJECT_TYPE PerfObj)
{
	PPERF_COUNTER_DEFINITION PerfCntr = NULL, CurCntr = NULL;
	PPERF_COUNTER_BLOCK PtrToCntr = NULL;
	BOOL bShouldBuildMap = FALSE;
	if (m_mapProcessData2CounterID.empty())
		bShouldBuildMap = TRUE;

	memset(&m_ProcessCounters, 0, sizeof(m_ProcessCounters));

	PerfCntr = FirstCounter( PerfObj );
	for (DWORD i = 0; i < PerfObj->NumCounters; i++)
	{
		if (m_mapProcessData2CounterID.find(PerfCntr->CounterNameTitleIndex) != 
			m_mapProcessData2CounterID.end())
		{
			m_ProcessCounters[m_mapProcessData2CounterID[PerfCntr->CounterNameTitleIndex]]
			= PerfCntr;
		}
		else
		{
			if (bShouldBuildMap)
			{
				int nDataID = m_pNameMgr->GetDataIDByCounterID(PerfCntr->CounterNameTitleIndex);
				m_mapProcessData2CounterID[PerfCntr->CounterNameTitleIndex] = nDataID;
				m_ProcessCounters[nDataID] = PerfCntr;
			}
		}
		PerfCntr = NextCounter(PerfCntr);
	}
	return TRUE;
}
Esempio n. 3
0
LONGLONG CCpuUsage::CPerfCounters::GetCounterValue(PPERF_OBJECT_TYPE pPerfObj, DWORD dwCounterIndex, LPCTSTR pInstanceName)
{
	PPERF_COUNTER_DEFINITION pPerfCntr = NULL;
	PPERF_INSTANCE_DEFINITION pPerfInst = NULL;
	PPERF_COUNTER_BLOCK pCounterBlock = NULL;

	// Get the first counter.

	pPerfCntr = FirstCounter( pPerfObj );

	// Look for the index of '% Total processor time'

	for( DWORD j=0; j < pPerfObj->NumCounters; j++ )
	{
		if (pPerfCntr->CounterNameTitleIndex == dwCounterIndex)
			break;

		// Get the next counter.

		pPerfCntr = NextCounter( pPerfCntr );
	}

	if( pPerfObj->NumInstances == PERF_NO_INSTANCES )		
	{
		pCounterBlock = (PPERF_COUNTER_BLOCK) ((LPBYTE) pPerfObj + pPerfObj->DefinitionLength);
	}
	else
	{
		pPerfInst = FirstInstance( pPerfObj );
	
		// Look for instance pInstanceName
		_bstr_t bstrInstance;
		_bstr_t bstrInputInstance = pInstanceName;
		for( int k=0; k < pPerfObj->NumInstances; k++ )
		{
			bstrInstance = (wchar_t *)((PBYTE)pPerfInst + pPerfInst->NameOffset);
			if (!_stricmp((LPCTSTR)bstrInstance, (LPCTSTR)bstrInputInstance))
			{
				pCounterBlock = (PPERF_COUNTER_BLOCK) ((LPBYTE) pPerfInst + pPerfInst->ByteLength);
				break;
			}
			
			// Get the next instance.

			pPerfInst = NextInstance( pPerfInst );
		}
	}

	if (pCounterBlock)
	{
		LONGLONG *lnValue = NULL;
		lnValue = (LONGLONG*)((LPBYTE) pCounterBlock + pPerfCntr->CounterOffset);
		return *lnValue;
	}
	return -1;
}
static void loadProcessorData(void) {
  DWORD c;
  PPERF_COUNTER_DEFINITION PerfCntr;

  if (ProcessorObj == NULL)
    return;
#if 0
  if (pidCtrOffset != -1)
    return; /* hunh? */
#endif
  PerfCntr = FirstCounter(ProcessorObj);
#if 0
  fprintf(stderr, "ProcessorObj->NumCounters=%d\n", ProcessorObj->NumCounters); fflush(stderr);
#endif

  for (c=0; c < ProcessorObj->NumCounters; c++) {
#if 0
    fprintf(stderr, "PerfCntr->CounterNameTitleIndex=%d\n", 
        PerfCntr->CounterNameTitleIndex); 
    fflush(stderr);
#endif
    switch (PerfCntr->CounterNameTitleIndex) {
      case TOTAL_PROCESSORTIME_ID:
	processorCtrCache[TOTAL_PROCESSORTIME_IDX] = *PerfCntr;
	break;
      case TOTAL_USERTIME_ID:
	processorCtrCache[TOTAL_USERTIME_IDX] = *PerfCntr;
	break;
      case TOTAL_PRIVILEGEDTIME_ID:
	processorCtrCache[TOTAL_PRIVILEGEDTIME_IDX] = *PerfCntr;
	break;
      case INTERRUPTTIME_ID:
	processorCtrCache[INTERRUPTTIME_IDX] = *PerfCntr;
	break;
      case INTERRUPTS_ID:
	processorCtrCache[INTERRUPTS_IDX] = *PerfCntr;
	break;
      default:
	/* unknown counter. just skip it. */
	break;
      }
    PerfCntr = NextCounter(PerfCntr);
  }
#ifdef FLG_DEBUG
  for (c=0; c < MAX_PROCESSOR_CTRS_COLLECTED; c++) {
    if (processorCtrCache[c].CounterNameTitleIndex == 0) {
      fprintf(stderr, "DEBUG: bad processorCtr at idx=%d\n", c); fflush(stderr);
    }
  }
#endif
}
static void loadObjectData(void) {
  PPERF_COUNTER_DEFINITION PerfCntr;
  DWORD c;

  if ( ObjectsObj == NULL ) {
    return;
  }
  
  PerfCntr = FirstCounter(ObjectsObj);
  for (c=0; c < ObjectsObj->NumCounters; c++) {
    switch (PerfCntr->CounterNameTitleIndex) {
    case PROCESSES_ID:
      objectsCtrCache[PROCESSES_IDX] = *PerfCntr;
      break;
    case THREADS_ID:
      objectsCtrCache[THREADS_IDX] = *PerfCntr;
      break;
    case EVENTS_ID:
      objectsCtrCache[EVENTS_IDX] = *PerfCntr;
      break;
    case SEMAPHORES_ID:
      objectsCtrCache[SEMAPHORES_IDX] = *PerfCntr;
      break;
    case MUTEXES_ID:
      objectsCtrCache[MUTEXES_IDX] = *PerfCntr;
      break;
    case SECTIONS_ID:
      objectsCtrCache[SECTIONS_IDX] = *PerfCntr;
      break;
    default:
      /* unknown counter. just skip it. */
      break;
    }
    PerfCntr = NextCounter(PerfCntr);
  }
#ifdef FLG_DEBUG
  for (c=0; c < MAX_OBJECTS_CTRS_COLLECTED; c++) {
    if (objectsCtrCache[c].CounterNameTitleIndex == 0) {
      fprintf(stderr, "DEBUG: bad objectsCtr at idx=%d\n", c); fflush(stderr);
    }
  }
#endif
  }
static void loadNetworkData(void)
{
  int32 i;
  unsigned int c;
  int32 val;
  char nausea[512];
  PPERF_COUNTER_DEFINITION PerfCntr;
  PPERF_COUNTER_DEFINITION CurCntr;
  PPERF_INSTANCE_DEFINITION PerfInst;
  PPERF_COUNTER_BLOCK PerfCntrBlk;

  if (NetworkObj == NULL) {
#ifdef FLG_DEBUG
    fprintf(stderr, "DEBUG: loadNetworkData, no NetworkObj!\n");
#endif
    return;
    }
  if (NetworkObj->NumInstances <= 0) {
#ifdef FLG_DEBUG
    fprintf(stderr, "DEBUG: loadNetworkData, NetworkObj->NumInstances <= 0!\n");
#endif
    return;
    }

  /* We're collecting sums, so start by zeroing out */
  netStats.loopbackPackets = 0;
  netStats.loopbackBytes = 0;
  netStats.packetsReceived = 0;
  netStats.bytesReceived = 0;
  netStats.packetsSent = 0;
  netStats.bytesSent = 0;


  PerfCntr = FirstCounter(NetworkObj);

  // retrieve all instances
  PerfInst = FirstInstance(NetworkObj);

  for (i = 0; i < NetworkObj->NumInstances; i ++) {
    boolean loopBack;

    wchar_t* unicodePtr = (wchar_t*)((PBYTE)PerfInst + PerfInst->NameOffset);
#if 0
    printf( "\n\tInstance %S: \n", unicodePtr);
#endif

    sprintf(nausea, "%S", unicodePtr);
    loopBack = strcmp(nausea, "MS TCP Loopback interface") == 0;

    PerfCntrBlk = (PPERF_COUNTER_BLOCK)((PBYTE)PerfInst + PerfInst->ByteLength);
    CurCntr = PerfCntr;

    // retrieve all counters
    for (c = 0; c < NetworkObj->NumCounters; c ++) {

#if 0
      printf("\t\tCounter %ld: %s\n",
         CurCntr->CounterNameTitleIndex,
         lpNamesArray[CurCntr->CounterNameTitleIndex]);
#endif

      switch (CurCntr->CounterNameTitleIndex) {
      case PACKETS_RECEIVED_ID:
        val = (int32)getLongValue(CurCntr, PerfCntrBlk);
        if (loopBack)
          netStats.loopbackPackets = val;
        else
          netStats.packetsReceived += val;
	break;
      case BYTES_RECEIVED_ID:
        val = (int32)getLongValue(CurCntr, PerfCntrBlk);
        if (loopBack)
          netStats.loopbackBytes = val;
        else
          netStats.bytesReceived += val;
	break;
      case PACKETS_SENT_ID:
        if (!loopBack) {
          val = (int32)getLongValue(CurCntr, PerfCntrBlk);
          netStats.packetsSent += val;
          }
	break;
      case BYTES_SENT_ID:
        if (!loopBack) {
          val = (int32)getLongValue(CurCntr, PerfCntrBlk);
          netStats.bytesSent += val;
          }
	break;
      default:
	/* unknown counter. just skip it. */
	break;
	}

      CurCntr = NextCounter(CurCntr);
      }
    PerfInst = NextInstance(PerfCntrBlk);
    }
}
static void loadMemoryData(void) {
  PPERF_COUNTER_DEFINITION PerfCntr;
  DWORD c;

  if (MemoryObj == NULL)
    return;
  PerfCntr = FirstCounter(MemoryObj);
  for (c=0; c < MemoryObj->NumCounters; c++) {
    switch (PerfCntr->CounterNameTitleIndex) {
    case AVAILABLEBYTES_ID:
      memoryCtrCache[AVAILABLEBYTES_IDX] = *PerfCntr;
      break;
    case COMMITTEDBYTES_ID:
      memoryCtrCache[COMMITTEDBYTES_IDX] = *PerfCntr;
      break;
    case COMMITLIMIT_ID:
      memoryCtrCache[COMMITLIMIT_IDX] = *PerfCntr;
      break;
    case TOTALPAGEFAULTS_ID:
      memoryCtrCache[TOTALPAGEFAULTS_IDX] = *PerfCntr;
      break;
    case WRITECOPIES_ID:
      memoryCtrCache[WRITECOPIES_IDX] = *PerfCntr;
      break;
    case TRANSITIONFAULTS_ID:
      memoryCtrCache[TRANSITIONFAULTS_IDX] = *PerfCntr;
      break;
    case CACHEFAULTS_ID:
      memoryCtrCache[CACHEFAULTS_IDX] = *PerfCntr;
      break;
    case DEMANDZEROFAULTS_ID:
      memoryCtrCache[DEMANDZEROFAULTS_IDX] = *PerfCntr;
      break;
    case PAGES_ID:
      memoryCtrCache[PAGES_IDX] = *PerfCntr;
      break;
    case PAGESINPUT_ID:
      memoryCtrCache[PAGESINPUT_IDX] = *PerfCntr;
      break;
    case PAGEREADS_ID:
      memoryCtrCache[PAGEREADS_IDX] = *PerfCntr;
      break;
    case PAGESOUTPUT_ID:
      memoryCtrCache[PAGESOUTPUT_IDX] = *PerfCntr;
      break;
    case PAGEWRITES_ID:
      memoryCtrCache[PAGEWRITES_IDX] = *PerfCntr;
      break;
    case TOTALPOOLPAGEDBYTES_ID:
      memoryCtrCache[TOTALPOOLPAGEDBYTES_IDX] = *PerfCntr;
      break;
    case TOTALPOOLNONPAGEDBYTES_ID:
      memoryCtrCache[TOTALPOOLNONPAGEDBYTES_IDX] = *PerfCntr;
      break;
    case POOLPAGEDALLOCS_ID:
      memoryCtrCache[POOLPAGEDALLOCS_IDX] = *PerfCntr;
      break;
    case POOLNONPAGEDALLOCS_ID:
      memoryCtrCache[POOLNONPAGEDALLOCS_IDX] = *PerfCntr;
      break;
    case FREESYSTEMPAGETABLEENTRIES_ID:
      memoryCtrCache[FREESYSTEMPAGETABLEENTRIES_IDX] = *PerfCntr;
      break;
    case CACHEBYTES_ID:
      memoryCtrCache[CACHEBYTES_IDX] = *PerfCntr;
      break;
    case CACHEBYTESPEAK_ID:
      memoryCtrCache[CACHEBYTESPEAK_IDX] = *PerfCntr;
      break;
    case POOLPAGEDRESIDENTBYTES_ID:
      memoryCtrCache[POOLPAGEDRESIDENTBYTES_IDX] = *PerfCntr;
      break;
    case SYSTEMCODETOTALBYTES_ID:
      memoryCtrCache[SYSTEMCODETOTALBYTES_IDX] = *PerfCntr;
      break;
    case SYSTEMCODERESIDENTBYTES_ID:
      memoryCtrCache[SYSTEMCODERESIDENTBYTES_IDX] = *PerfCntr;
      break;
    case SYSTEMDRIVERTOTALBYTES_ID:
      memoryCtrCache[SYSTEMDRIVERTOTALBYTES_IDX] = *PerfCntr;
      break;
    case SYSTEMDRIVERRESIDENTBYTES_ID:
      memoryCtrCache[SYSTEMDRIVERRESIDENTBYTES_IDX] = *PerfCntr;
      break;
    case SYSTEMCACHERESIDENTBYTES_ID:
      memoryCtrCache[SYSTEMCACHERESIDENTBYTES_IDX] = *PerfCntr;
      break;
    case COMMITTEDBYTESINUSE_ID:
      memoryCtrCache[COMMITTEDBYTESINUSE_IDX] = *PerfCntr;
      break;
    default:
      /* unknown counter. just skip it. */
      break;
    }
    PerfCntr = NextCounter(PerfCntr);
  }
#ifdef FLG_DEBUG
  for (c=0; c < MAX_MEMORY_CTRS_COLLECTED; c++) {
    if (memoryCtrCache[c].CounterNameTitleIndex == 0) {
      fprintf(stderr, "DEBUG: bad memoryCtr at idx=%d\n", c); fflush(stderr);
    }
  }
#endif
}
static void loadSystemData(void) {
  PPERF_COUNTER_DEFINITION PerfCntr;
  DWORD c;

  if (SystemObj == NULL)
    return;
  PerfCntr = FirstCounter(SystemObj);
  for (c=0; c < SystemObj->NumCounters; c++) {
    switch (PerfCntr->CounterNameTitleIndex) {
      case TOTALFILEREADOPS_ID:
	systemCtrCache[TOTALFILEREADOPS_IDX] = *PerfCntr;
	break;
      case TOTALFILEWRITEOPS_ID:
	systemCtrCache[TOTALFILEWRITEOPS_IDX] = *PerfCntr;
	break;
      case TOTALFILECONTROLOPS_ID:
	systemCtrCache[TOTALFILECONTROLOPS_IDX] = *PerfCntr;
	break;
      case TOTALFILEREADKBYTES_ID:
	systemCtrCache[TOTALFILEREADKBYTES_IDX] = *PerfCntr;
	break;
      case TOTALFILEWRITEKBYTES_ID:
	systemCtrCache[TOTALFILEWRITEKBYTES_IDX] = *PerfCntr;
	break;
      case TOTALFILECONTROLKBYTES_ID:
	systemCtrCache[TOTALFILECONTROLKBYTES_IDX] = *PerfCntr;
	break;
      case TOTALCONTEXTSWITCHES_ID:
	systemCtrCache[TOTALCONTEXTSWITCHES_IDX] = *PerfCntr;
	break;
      case TOTALSYSTEMCALLS_ID:
	systemCtrCache[TOTALSYSTEMCALLS_IDX] = *PerfCntr;
	break;
      case TOTALFILEDATAOPS_ID:
	systemCtrCache[TOTALFILEDATAOPS_IDX] = *PerfCntr;
	break;
      case PROCESSORQUEUELENGTH_ID:
	systemCtrCache[PROCESSORQUEUELENGTH_IDX] = *PerfCntr;
	break;
      case ALIGNMENTFIXUPS_ID:
	systemCtrCache[ALIGNMENTFIXUPS_IDX] = *PerfCntr;
	break;
      case EXCEPTIONDISPATCHES_ID:
	systemCtrCache[EXCEPTIONDISPATCHES_IDX] = *PerfCntr;
	break;
      case FLOATINGEMULATIONS_ID:
	systemCtrCache[FLOATINGEMULATIONS_IDX] = *PerfCntr;
	break;
      case REGISTRYQUOTAINUSE_ID:
	systemCtrCache[REGISTRYQUOTAINUSE_IDX] = *PerfCntr;
	break;
      default:
	/* unknown counter. just skip it. */
	break;
      }
    PerfCntr = NextCounter(PerfCntr);
  }
#ifdef FLG_DEBUG
  for (c=0; c < MAX_SYSTEM_CTRS_COLLECTED; c++) {
    if (systemCtrCache[c].CounterNameTitleIndex == 0) {
      fprintf(stderr, "DEBUG: bad systemCtr at idx=%d\n", c); fflush(stderr);
    }
  }
#endif
  }
static void loadProcessData(void) {
  DWORD c;
  PPERF_COUNTER_DEFINITION PerfCntr;

  if (pidCtrOffset != -1)
    return; /* hunh? */
  if (ProcessObj == NULL)
    return;
  PerfCntr = FirstCounter(ProcessObj);
  for (c=0; c < ProcessObj->NumCounters; c++) {
    switch (PerfCntr->CounterNameTitleIndex) {
      case PID_ID:
	pidCtrOffset = PerfCntr->CounterOffset;
	break;
      case PROCESS_PROCESSORTIME_ID:
	processCtrCache[PROCESS_PROCESSORTIME_IDX] = *PerfCntr;
	break;
      case PROCESS_USERTIME_ID:
	processCtrCache[PROCESS_USERTIME_IDX] = *PerfCntr;
	break;
      case PROCESS_PRIVILEGEDTIME_ID:
	processCtrCache[PROCESS_PRIVILEGEDTIME_IDX] = *PerfCntr;
	break;
      case VIRTUALBYTESPEAK_ID:
	processCtrCache[VIRTUALBYTESPEAK_IDX] = *PerfCntr;
	break;
      case VIRTUALBYTES_ID:
	processCtrCache[VIRTUALBYTES_IDX] = *PerfCntr;
	break;
      case PAGEFAULTS_ID:
	processCtrCache[PAGEFAULTS_IDX] = *PerfCntr;
	break;
      case WORKINGSETPEAK_ID:
	processCtrCache[WORKINGSETPEAK_IDX] = *PerfCntr;
	break;
      case WORKINGSET_ID:
	processCtrCache[WORKINGSET_IDX] = *PerfCntr;
	break;
      case PAGEFILEBYTESPEAK_ID:
	processCtrCache[PAGEFILEBYTESPEAK_IDX] = *PerfCntr;
	break;
      case PAGEFILEBYTES_ID:
	processCtrCache[PAGEFILEBYTES_IDX] = *PerfCntr;
	break;
      case PRIVATEBYTES_ID:
	processCtrCache[PRIVATEBYTES_IDX] = *PerfCntr;
	break;
      case THREADCOUNT_ID:
	processCtrCache[THREADCOUNT_IDX] = *PerfCntr;
	break;
      case PRIORITYBASE_ID:
	processCtrCache[PRIORITYBASE_IDX] = *PerfCntr;
	break;
      case POOLPAGEDBYTES_ID:
	processCtrCache[POOLPAGEDBYTES_IDX] = *PerfCntr;
	break;
      case POOLNONPAGEDBYTES_ID:
	processCtrCache[POOLNONPAGEDBYTES_IDX] = *PerfCntr;
	break;
      case HANDLECOUNT_ID:
	processCtrCache[HANDLECOUNT_IDX] = *PerfCntr;
	break;
      default:
	/* unknown counter. just skip it. */
	break;
      }
    PerfCntr = NextCounter(PerfCntr);
    } /* for */
#ifdef FLG_DEBUG
  for (c=0; c < MAX_PROCESS_CTRS_COLLECTED; c++) {
    if (processCtrCache[c].CounterNameTitleIndex == 0) {
      fprintf(stderr, "DEBUG: bad processCtr at idx=%d\n", c); fflush(stderr);
      }
    }
#endif
  }
Esempio n. 10
0
void GetProcessID(LPCTSTR pProcessName, std::vector<DWORD>& SetOfPID)
{
    PPERF_DATA_BLOCK pPerfData = NULL;
    PPERF_OBJECT_TYPE pPerfObj;
    PPERF_INSTANCE_DEFINITION pPerfInst;
    PPERF_COUNTER_DEFINITION pPerfCntr, pCurCntr;
    PPERF_COUNTER_BLOCK PtrToCntr;
    DWORD BufferSize = TOTALBYTES;
    DWORD i, j;
    LONG k;

    // Allocate the buffer for the performance data.

    pPerfData = (PPERF_DATA_BLOCK)malloc(BufferSize);

    wchar_t szKey[32];
    swprintf_s(szKey, L"%d %d", PROCESS_OBJECT_INDEX, PROC_ID_COUNTER);
    LONG lRes;
    while ((lRes = RegQueryValueEx(HKEY_PERFORMANCE_DATA,
                                   szKey,
                                   NULL,
                                   NULL,
                                   (LPBYTE)pPerfData,
                                   &BufferSize)) == ERROR_MORE_DATA)
    {
        // Get a buffer that is big enough.

        BufferSize += BYTEINCREMENT;
        pPerfData = (PPERF_DATA_BLOCK)realloc(pPerfData, BufferSize);
    }

    // Get the first object type.

    pPerfObj = FirstObject(pPerfData);

    // Process all objects.

    for (i = 0; i < pPerfData->NumObjectTypes; i++)
    {

        if (pPerfObj->ObjectNameTitleIndex != PROCESS_OBJECT_INDEX)
        {
            pPerfObj = NextObject(pPerfObj);
            continue;
        }

        SetOfPID.clear();

        // Get the first counter.

        pPerfCntr = FirstCounter(pPerfObj);

        // Get the first instance.

        pPerfInst = FirstInstance(pPerfObj);

        _bstr_t bstrProcessName, bstrInput;

        // Retrieve all instances.
        for (k = 0; k < pPerfObj->NumInstances; k++)
        {
            pCurCntr = pPerfCntr;
            bstrInput = pProcessName;
            bstrProcessName = (wchar_t *)((PBYTE)pPerfInst + pPerfInst->NameOffset);
            if (!_wcsicmp((LPCTSTR)bstrProcessName, (LPCTSTR)bstrInput))
            {
                // Retrieve all counters.

                for (j = 0; j < pPerfObj->NumCounters; j++)
                {
                    if (pCurCntr->CounterNameTitleIndex == PROC_ID_COUNTER)
                    {
                        PtrToCntr = CounterBlock(pPerfInst);
                        DWORD *pdwValue = (DWORD*)((LPBYTE)PtrToCntr + pCurCntr->CounterOffset);
                        SetOfPID.push_back(*pdwValue);
                        break;
                    }


                    // Get the next counter.

                    pCurCntr = NextCounter(pCurCntr);
                }
            }

            // Get the next instance.

            pPerfInst = NextInstance(pPerfInst);
        }
    }
    free(pPerfData);
    RegCloseKey(HKEY_PERFORMANCE_DATA);
}
Esempio n. 11
0
static
PDH_STATUS
PdhiExpandCounterPath (
    IN      LPCWSTR szWildCardPath,
    IN      LPVOID  pExpandedPathList,
    IN      LPDWORD pcchPathListLength,
    IN      BOOL    bUnicode
)
{
    PPERF_MACHINE       pMachine;
    PPDHI_COUNTER_PATH  pWildCounterPath = NULL;
    WCHAR               szWorkBuffer[MAX_PATH];
    WCHAR               szCounterName[MAX_PATH];
    WCHAR               szInstanceName[MAX_PATH];
    LPWSTR              szEndOfObjectString;
    LPWSTR              szInstanceString;
    LPWSTR              szCounterString;
    LPVOID              szNextUserString;
    PERF_OBJECT_TYPE    *pObjectDef;
    PERF_OBJECT_TYPE    *pParentObjectDef;
    PERF_COUNTER_DEFINITION *pCounterDef;
    PERF_INSTANCE_DEFINITION *pInstanceDef;
    PERF_INSTANCE_DEFINITION *pParentInstanceDef;

    DWORD               dwBufferRemaining;
    DWORD               dwSize;
    DWORD               dwSizeReturned = 0;
    PDH_STATUS          pdhStatus = ERROR_SUCCESS;

    DWORD               dwCtrNdx, dwInstNdx;
    BOOL                bMoreData = FALSE;

    // allocate buffers
    pWildCounterPath = G_ALLOC (GPTR, PDHI_COUNTER_PATH_BUFFER_SIZE);

    if (pWildCounterPath == NULL) {
      // unable to allocate memory so bail out
      pdhStatus = PDH_MEMORY_ALLOCATION_FAILURE;
    } else {
      __try {
        dwBufferRemaining = *pcchPathListLength;
        szNextUserString = pExpandedPathList;
      } __except (EXCEPTION_EXECUTE_HANDLER) {
        pdhStatus = PDH_INVALID_ARGUMENT;
      }
    }

    if (pdhStatus == ERROR_SUCCESS) {
      // Parse wild card Path 
                  
      dwSize = G_SIZE (pWildCounterPath);
      if (ParseFullPathNameW (szWildCardPath, &dwSize, pWildCounterPath)) {
        // get the machine referenced in the path
        pMachine = GetMachine (pWildCounterPath->szMachineName, 0);
        if (pMachine != NULL) {
          if (wcsncmp (cszDoubleBackSlash, szWildCardPath, 2) == 0) {
            // the caller wants the machine name in the path so
            // copy it to the work buffer
            lstrcpyW (szWorkBuffer, pWildCounterPath->szMachineName);
          } else {
            *szWorkBuffer = 0;
          }
          // append the object name (since wild card objects are not
          // currently supported.

          lstrcatW (szWorkBuffer, cszBackSlash);
          lstrcatW (szWorkBuffer, pWildCounterPath->szObjectName);
          szEndOfObjectString = &szWorkBuffer[lstrlenW(szWorkBuffer)];
          
          // get object pointer (since objects are not wild)
          pObjectDef = GetObjectDefByName (
            pMachine->pSystemPerfData,
            pMachine->dwLastPerfString,
            pMachine->szPerfStrings,
            pWildCounterPath->szObjectName);

          if (pObjectDef != NULL) {
            // for each counters and identify matches
            pCounterDef = FirstCounter (pObjectDef);
            for (dwCtrNdx = 0; dwCtrNdx < pObjectDef->NumCounters; dwCtrNdx++) {
              // for each counter check instances (if supported) 
              //  and keep matches
              if ((pCounterDef->CounterNameTitleIndex > 0) &&
                  (pCounterDef->CounterNameTitleIndex < pMachine->dwLastPerfString ) &&
                  (!((pCounterDef->CounterType & PERF_DISPLAY_NOSHOW) &&
                     // this is a hack because this type is not defined correctly
                    (pCounterDef->CounterType != PERF_AVERAGE_BULK)))) {
                // look up name of each object & store size
                lstrcpyW (szCounterName,
                  pMachine->szPerfStrings[pCounterDef->CounterNameTitleIndex]);
                if (WildStringMatchW(pWildCounterPath->szCounterName, szCounterName)) {
                  // if this object has instances, then walk down
                  // the instance list and save any matches
                  if (pObjectDef->NumInstances != PERF_NO_INSTANCES) {
                    // then walk instances to find matches
                    pInstanceDef = FirstInstance (pObjectDef);
                    if (pObjectDef->NumInstances > 0) {
                      for (dwInstNdx = 0;
                        dwInstNdx < (DWORD)pObjectDef->NumInstances;
                        dwInstNdx++) {
                        szInstanceString = szEndOfObjectString;
                        if (pInstanceDef->ParentObjectTitleIndex > 0) {
                          // then add in parent instance name
                          pParentObjectDef = GetObjectDefByTitleIndex (
                            pMachine->pSystemPerfData,
                            pInstanceDef->ParentObjectTitleIndex);
                          if (pParentObjectDef != NULL) {
                            pParentInstanceDef = GetInstance (
                              pParentObjectDef,
                              pInstanceDef->ParentObjectInstance);
                            if (pParentInstanceDef != NULL) {
                              GetInstanceNameStr (pParentInstanceDef,
                                szInstanceName,
                                pObjectDef->CodePage);
                              if (WildStringMatchW (pWildCounterPath->szParentName, szInstanceName)) {
                                // add this string
                                szInstanceString = szEndOfObjectString;
                                lstrcpyW (szInstanceString, cszLeftParen);
                                lstrcatW (szInstanceString, szInstanceName);
                                lstrcatW (szInstanceString, cszSlash);
                              } else {
                                // get next instance and continue 
                                pInstanceDef = NextInstance(pInstanceDef);
                                continue;
                              }
                            } else {
                              // unable to locate parent instance
                              // so cancel this one, then 
                              // get next instance and continue 
                              pInstanceDef = NextInstance(pInstanceDef);
                              continue;
                            }
                          } else {
                            // unable to locate parent object
                            // so cancel this one, then 
                            // get next instance and continue 
                            pInstanceDef = NextInstance(pInstanceDef);
                            continue;
                          }
                        } else {
                          // no parent name so continue
                          szInstanceString = szEndOfObjectString;
                          lstrcpyW (szInstanceString, cszSlash);
                        }
                        GetInstanceNameStr (pInstanceDef,
                          szInstanceName,
                          pObjectDef->CodePage);

                        //
                        //  BUGBUG: need to do something with indexes.
                        //
                        // if this instance name matches, then keep it
                        if (WildStringMatchW (pWildCounterPath->szInstanceName, szInstanceName)) {
                          lstrcatW (szInstanceString, szInstanceName);
                          lstrcatW (szInstanceString, cszRightParenBackSlash);
                          // now append this counter name
                          lstrcatW (szInstanceString, szCounterName);

                          // add it to the user's buffer if there's room
                          dwSize = lstrlenW(szWorkBuffer) + 1;
                          if (!bMoreData && (dwSize  < dwBufferRemaining)) {
                            if (bUnicode) {
                                lstrcpyW ((LPWSTR)szNextUserString, szWorkBuffer);
                                (LPBYTE)szNextUserString += dwSize * sizeof(WCHAR);
                            } else {
                                wcstombs ((LPSTR)szNextUserString, szWorkBuffer, dwSize);
                                (LPBYTE)szNextUserString += dwSize * sizeof(CHAR);
                            }
                            dwSizeReturned += dwSize;
                            dwBufferRemaining -= dwSize;
                          } else {
                            // they've run out of buffer so just update the size required
                            bMoreData = TRUE;
                            dwSizeReturned += dwSize;
                          }
                        } else {
                          // they don't want this instance so skip it
                        }
                        pInstanceDef = NextInstance (pInstanceDef);
                      } // end for each instance in object
                    } else {
                      // this object supports instances, 
                      // but doesn't currently have any
                      // so do nothing.
                    }
                  } else {
                    // this object does not use instances so copy this
                    // counter to the caller's buffer.
                    szCounterString = szEndOfObjectString;
                    lstrcpyW (szCounterString, cszBackSlash);
                    lstrcatW (szCounterString, szCounterName);
                    dwSize = lstrlenW(szWorkBuffer) + 1;
                    if (!bMoreData && (dwSize  < dwBufferRemaining)) {
                      if (bUnicode) {
                        lstrcpyW ((LPWSTR)szNextUserString, szWorkBuffer);
                        (LPBYTE)szNextUserString += dwSize * sizeof(WCHAR);
                      } else {
                        wcstombs ((LPSTR)szNextUserString, szWorkBuffer, dwSize);
                        (LPBYTE)szNextUserString += dwSize * sizeof(CHAR);
                      }
                      dwSizeReturned += dwSize;
                      dwBufferRemaining -= dwSize;
                    } else {
                      // they've run out of buffer so bail out of this loop
                      bMoreData = TRUE;
                      dwSizeReturned += dwSize;
                    }
                  }
                } else {
                  // this counter doesn't match so skip it
                }
              } else {
                // this counter string is not available
              }
              pCounterDef = NextCounter(pCounterDef);
            } // end for each counter in this object
            if (bUnicode) {
                *(LPWSTR)szNextUserString = 0;  // MSZ terminator
            } else {
                *(LPSTR)szNextUserString = 0;  // MSZ terminator
            }
            *pcchPathListLength = dwSizeReturned;
            if (bMoreData) {
              pdhStatus = PDH_MORE_DATA;
            } else {
              pdhStatus = ERROR_SUCCESS;
            }
          } else {
            // unable to find object
            pdhStatus = PDH_INVALID_PATH;
          }
        } else {
          // unable to connect to machine.
          pdhStatus = PDH_CANNOT_CONNECT_MACHINE;
        }
      } else {
        // unable to read input path string
          pdhStatus = PDH_INVALID_PATH;
      }
    }

    if (pWildCounterPath != NULL) G_FREE (pWildCounterPath);

    return pdhStatus;
}