//********************************************************************* // // RefreshMemoryDlgImageList // // Refresh the image list for memory dialog. // void RefreshMemoryDlgImageList (HWND hImageList, DWORD ParentIndex, PPERF_OBJECT pImageObj) { PPERF_INSTANCE pImageInst; INT ListIndex; INT InstIndex = 0; ListIndex = SendMessage (hImageList, CB_ADDSTRING, 0, (DWORD)TEXT(" Total Commit")); SendMessage (hImageList, CB_SETITEMDATA, ListIndex, 0xFFFFFFFF); if (pImageObj) { pImageInst = FirstInstance (pImageObj); while (pImageInst && InstIndex < pImageObj->NumInstances) { if (ParentIndex == pImageInst->ParentObjectInstance) { ListIndex = SendMessage (hImageList, CB_ADDSTRING, 0, (DWORD)InstanceName(pImageInst)); SendMessage (hImageList, CB_SETITEMDATA, ListIndex, InstIndex); } pImageInst = NextInstance (pImageInst); InstIndex++; } } }
//********************************************************************* // // RefreshPviewDlgMemoryData // // Update the memory data for pview dialog. This should be done // after the ghCostlyData is collected and is not refreshing. // void RefreshPviewDlgMemoryData (HWND hPviewDlg, PPERF_INSTANCE pProcessInstance, PPERF_OBJECT pProcessObject, PPERF_OBJECT pAddressObject) { DWORD *pProcessID1; DWORD *pProcessID2; PPERF_COUNTER pCounter1; PPERF_COUNTER pCounter2; PPERF_INSTANCE pAddressInstance; INT i = 0; if ((pCounter1 = FindCounter (pProcessObject, PX_PROCESS_ID)) && (pCounter2 = FindCounter (pAddressObject, PX_PROCESS_ID))) { pProcessID1 = (DWORD *) CounterData (pProcessInstance, pCounter1); pAddressInstance = FirstInstance (pAddressObject); while (pAddressInstance && i < pAddressObject->NumInstances) { pProcessID2 = (DWORD *) CounterData (pAddressInstance, pCounter2); if (*pProcessID1 == *pProcessID2) { PaintPviewDlgMemoryData (hPviewDlg, pAddressInstance, pAddressObject); break; } pAddressInstance = NextInstance (pAddressInstance); i++; } } }
//********************************************************************* // // RefreshPviewDlgThreadPC // // Update the thread PC value. This should be done after the ghCostlyData // is collected and is no refreshing. // void RefreshPviewDlgThreadPC (HWND hPviewDlg, LPTSTR szProcessName, LPTSTR szThreadName, PPERF_OBJECT pThreadDetailsObject, PPERF_DATA pCostlyData) { PPERF_COUNTER pCounter; PPERF_INSTANCE pInstance; PPERF_INSTANCE pParent; LPTSTR szInstanceName; LPTSTR szParentName; TCHAR str[20]; DWORD *pdwData; INT i = 0; if (pCounter = FindCounter (pThreadDetailsObject, PX_THREAD_PC)) { pInstance = FirstInstance (pThreadDetailsObject); while (pInstance && i < pThreadDetailsObject->NumInstances) { if (!(szInstanceName = InstanceName (pInstance))) // can't find name ; else if (lstrcmp (szThreadName, szInstanceName)) // the thread name is different ; else if (!(pParent = FindInstanceParent (pInstance, pCostlyData))) // can't find parent ; else if (!(szParentName = InstanceName (pParent))) // can't find parent's name ; else if (!lstrcmp (szProcessName, szParentName)) { // Parent's name matches, this is the right one. // pdwData = CounterData (pInstance, pCounter); wsprintf (str, TEXT("0x%08x"), *pdwData); SetDlgItemText (hPviewDlg, PVIEW_THREAD_PC, str); return; } pInstance = NextInstance (pInstance); i++; } } // We are here only because we can't find the data to display. // SetDlgItemText (hPviewDlg, PVIEW_THREAD_PC, NODATA); }
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; }
//********************************************************************* // // RefreshThreadList // // Find all threads for a given process and update the thread list. // void RefreshThreadList (HWND hThreadList, PPERF_OBJECT pObject, DWORD ParentIndex) { PPERF_INSTANCE pInstance; TCHAR szListText[256]; INT ListIndex; PPERF_COUNTER pCounterCPU; PPERF_COUNTER pCounterPRIV; double fObjectFreq; double fObjectTime; double fTime; INT InstanceIndex = 0; INT err; if (pObject) { if ((pCounterCPU = FindCounter (pObject, PX_THREAD_CPU)) && (pCounterPRIV = FindCounter (pObject, PX_THREAD_PRIV))) { fObjectFreq = Li2Double (pObject->PerfFreq); fObjectTime = Li2Double (pObject->PerfTime); fTime = fObjectTime / fObjectFreq; pInstance = FirstInstance (pObject); while (pInstance && InstanceIndex < pObject->NumInstances) { if (ParentIndex == pInstance->ParentObjectInstance) { SetThreadListText (pInstance, pCounterCPU, pCounterPRIV, fTime, szListText); ListIndex = SendMessage (hThreadList, LB_INSERTSTRING, (WPARAM)-1, (DWORD)szListText); err = SendMessage (hThreadList, LB_SETITEMDATA, ListIndex, InstanceIndex); } pInstance = NextInstance (pInstance); InstanceIndex++; } } } }
static void loadCpuData(void) { int32 i; boolean foundIt = FALSE; PPERF_INSTANCE_DEFINITION PerfInst; lastFetchData = currentFetchData; currentFetchData.perfTimeMs = (uint32)(((__int64)PerfData->PerfTime100nSec.QuadPart) / 10000); if (ProcessorObj == NULL) { #ifdef FLG_DEBUG fprintf(stderr, "DEBUG: loadCpuData, no ProcessorObj!\n"); #endif return; } if (ProcessObj == NULL) { #ifdef FLG_DEBUG fprintf(stderr, "DEBUG: loadCpuData, no ProcessObj!\n"); #endif return; } if (ProcessObj->NumInstances <= 0) { #ifdef FLG_DEBUG fprintf(stderr, "DEBUG: loadCpuData, ProcessObj->NumInstances <= 0!\n"); #endif return; } PerfInst = FirstInstance(ProcessorObj); for (i=0; i < (int32)ProcessorObj->NumInstances; i++) { PPERF_COUNTER_BLOCK PerfCntrBlk = (PPERF_COUNTER_BLOCK)((PBYTE)PerfInst + PerfInst->ByteLength); /* starts with "_" must be "_Total" */ short* unicodePtr = (short*)((PBYTE)PerfInst + PerfInst->NameOffset); if (PerfInst->UniqueID == PERF_NO_UNIQUE_ID && unicodePtr[0] != '_') { PerfInst = NextInstance(PerfCntrBlk); continue; } currentFetchData.usertime = getLongValue(&processorCtrCache[TOTAL_USERTIME_IDX], PerfCntrBlk); currentFetchData.systime = getLongValue(&processorCtrCache[TOTAL_PRIVILEGEDTIME_IDX], PerfCntrBlk); currentFetchData.idletime = getLongValue(&processorCtrCache[TOTAL_PROCESSORTIME_IDX], PerfCntrBlk); currentFetchData.inttime = getLongValue(&processorCtrCache[INTERRUPTTIME_IDX], PerfCntrBlk); currentFetchData.interrupts = (uint32)getLongValue(&processorCtrCache[INTERRUPTS_IDX], PerfCntrBlk); foundIt = TRUE; break; } #ifdef FLG_DEBUG if (!foundIt) { fprintf(stderr, "DEBUG: did not find processor named _Total!\n"); } #endif }
//********************************************************************* // // RefreshMemoryDlg // // Refresh the memory detail dialog. // BOOL RefreshMemoryDlg (HWND hMemDlg, PPERF_INSTANCE pProcessInstance, PPERF_OBJECT pProcessObject, PPERF_OBJECT pAddressObject, PPERF_OBJECT pImageObject) { DWORD *pProcessID1; DWORD *pProcessID2; PPERF_COUNTER pCounter1; PPERF_COUNTER pCounter2; PPERF_INSTANCE pAddressInstance; HWND hImageList; TCHAR szTemp[40]; BOOL bStat = FALSE; INT InstIndex = 0; if ((pCounter1 = FindCounter (pProcessObject, PX_PROCESS_ID)) && (pCounter2 = FindCounter (pAddressObject, PX_PROCESS_ID))) { pProcessID1 = (DWORD *) CounterData (pProcessInstance, pCounter1); wsprintf (szTemp, TEXT("%s (%#x)"), InstanceName (pProcessInstance), *pProcessID1); SetDlgItemText (hMemDlg, MEMORY_PROCESS_ID, szTemp); pAddressInstance = FirstInstance (pAddressObject); while (pAddressInstance && InstIndex < pAddressObject->NumInstances) { pProcessID2 = (DWORD *) CounterData (pAddressInstance, pCounter2); if (*pProcessID1 == *pProcessID2) { PaintMemDlgAddrData (hMemDlg, pAddressInstance, pAddressObject); PaintMemDlgVMData (hMemDlg, pProcessInstance, pProcessObject); hImageList = GetDlgItem (hMemDlg, MEMORY_IMAGE); RefreshMemoryDlgImageList (hImageList, InstIndex, pImageObject); bStat = TRUE; break; } pAddressInstance = NextInstance (pAddressInstance); InstIndex++; } } return bStat; }
//********************************************************************* // // RefreshProcessList // // Find all process and update the process list. // void RefreshProcessList (HWND hProcessList, PPERF_OBJECT pObject) { PPERF_INSTANCE pInstance; TCHAR szListText[256]; INT ListIndex; PPERF_COUNTER pCounterCPU; PPERF_COUNTER pCounterPRIV; PPERF_COUNTER pCounterProcID; double fObjectFreq; double fObjectTime; double fTime; INT InstanceIndex = 0; INT err; if (pObject) { if ((pCounterCPU = FindCounter (pObject, PX_PROCESS_CPU)) && (pCounterPRIV = FindCounter (pObject, PX_PROCESS_PRIV)) && (pCounterProcID = FindCounter (pObject, PX_PROCESS_ID))) { fObjectFreq = Li2Double (pObject->PerfFreq); fObjectTime = Li2Double (pObject->PerfTime); fTime = fObjectTime / fObjectFreq; pInstance = FirstInstance (pObject); while (pInstance && InstanceIndex < pObject->NumInstances) { SetProcessListText (pInstance, pCounterCPU, pCounterPRIV, pCounterProcID, fTime, szListText); ListIndex = SendMessage (hProcessList, LB_ADDSTRING, 0, (DWORD)szListText); err = SendMessage (hProcessList, LB_SETITEMDATA, ListIndex, InstanceIndex); pInstance = NextInstance (pInstance); InstanceIndex++; } } } }
void KProcessPerfMgr::UpdatePerfData(PPERF_OBJECT_TYPE PerfObj) { if (!_UpdateCounterInfo(PerfObj)) return; PPERF_INSTANCE_DEFINITION PerfInst = NULL; map<DWORD, KProcessPerfData*> newProcessInfo; if( PerfObj->NumInstances > 0 ) { PerfInst = FirstInstance( PerfObj ); double fSumExceptIdle = 0; KProcessPerfData* pIdleProcess = NULL; for( int k=0; k < PerfObj->NumInstances; k++ ) { wchar_t* pInstName = (wchar_t*)((PBYTE)PerfInst + PerfInst->NameOffset); if ((wcsicmp(pInstName, L"_total") != 0)) { KProcessPerfData* pProcessData = _UpdateProcessData(PerfInst, PerfObj); if (pProcessData) { newProcessInfo[pProcessData->GetProcessID()] = pProcessData; if (pProcessData->GetProcessID() != 0) fSumExceptIdle += pProcessData->GetCpuUsage(); else pIdleProcess = pProcessData; } } else { _UpdateProcessTotal(PerfInst, PerfObj); } PerfInst = NextInstance( PerfInst ); } if (pIdleProcess) { double idleUsage = (double)100.0 - fSumExceptIdle; pIdleProcess->SetCpuUsage(idleUsage); } } _ClearProcessInfo(); m_ProcessInfo = newProcessInfo; newProcessInfo.clear(); }
PPERF_INSTANCE FindInstanceN (PPERF_OBJECT pObject, DWORD N) { PPERF_INSTANCE pInst; DWORD i = 0; if (!pObject) return NULL; else if (N >= (DWORD)(pObject->NumInstances)) return NULL; else { pInst = FirstInstance (pObject); while (i != N) { pInst = NextInstance (pInst); i++; } return pInst; } }
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 refreshProcess(jint pid, int32* intStorage, int64* longStorage, double* doubleStorage) { #define INT_FIELD_STORE(field, value) \ (intStorage[com_gemstone_gemfire_internal_WindowsProcessStats_##field##INT] = value) #define LONG_FIELD_STORE(field, value) \ (longStorage[com_gemstone_gemfire_internal_WindowsProcessStats_##field##LONG] = value) #define DOUBLE_FIELD_STORE(field, value) \ (doubleStorage[com_gemstone_gemfire_internal_WindowsProcessStats_##field##DOUBLE] = value) #define FLOAT_FIELD_STORE(field, value) \ DOUBLE_FIELD_STORE(field, (double)(value)) int32 i; if (ProcessObj) { PPERF_INSTANCE_DEFINITION PerfInst; if (ProcessObj->NumInstances > 0) { #if 0 static int done = 0; if (!done) { done = 1; PerfInst = FirstInstance(ProcessObj); for (i=0; i < (int32)ProcessObj->NumInstances; i++) { PPERF_COUNTER_BLOCK PerfCntrBlk; PerfCntrBlk = (PPERF_COUNTER_BLOCK)((PBYTE)PerfInst + PerfInst->ByteLength); printf("process: %s\n", getInstIdStr(PerfInst, "proc-")); PerfInst = NextInstance(PerfCntrBlk); } } #endif PerfInst = FirstInstance(ProcessObj); for (i=0; i < (int32)ProcessObj->NumInstances; i++) { PPERF_COUNTER_BLOCK PerfCntrBlk; PerfCntrBlk = (PPERF_COUNTER_BLOCK)((PBYTE)PerfInst + PerfInst->ByteLength); if (pid == getPid(pidCtrOffset, PerfCntrBlk)) { LONG_FIELD_STORE(activeTime, getLongValue(&processCtrCache[PROCESS_PROCESSORTIME_IDX], PerfCntrBlk)); LONG_FIELD_STORE(userTime, getLongValue(&processCtrCache[PROCESS_USERTIME_IDX], PerfCntrBlk)); LONG_FIELD_STORE(systemTime, getLongValue(&processCtrCache[PROCESS_PRIVILEGEDTIME_IDX], PerfCntrBlk)); LONG_FIELD_STORE(virtualSizePeak, getLongValue(&processCtrCache[VIRTUALBYTESPEAK_IDX], PerfCntrBlk)); LONG_FIELD_STORE(virtualSize, getLongValue(&processCtrCache[VIRTUALBYTES_IDX], PerfCntrBlk)); LONG_FIELD_STORE(pageFaults, getLongValue(&processCtrCache[PAGEFAULTS_IDX], PerfCntrBlk)); LONG_FIELD_STORE(workingSetSizePeak, getLongValue(&processCtrCache[WORKINGSETPEAK_IDX], PerfCntrBlk)); LONG_FIELD_STORE(workingSetSize, getLongValue(&processCtrCache[WORKINGSET_IDX], PerfCntrBlk)); LONG_FIELD_STORE(pageFileSizePeak, getLongValue(&processCtrCache[PAGEFILEBYTESPEAK_IDX], PerfCntrBlk)); LONG_FIELD_STORE(pageFileSize, getLongValue(&processCtrCache[PAGEFILEBYTES_IDX], PerfCntrBlk)); LONG_FIELD_STORE(privateSize, getLongValue(&processCtrCache[PRIVATEBYTES_IDX], PerfCntrBlk)); INT_FIELD_STORE(threads, (int32)getLongValue(&processCtrCache[THREADCOUNT_IDX], PerfCntrBlk)); INT_FIELD_STORE(priorityBase, (int32)getLongValue(&processCtrCache[PRIORITYBASE_IDX], PerfCntrBlk)); /* ELAPSEDTIME omitted? */ /* POOLPAGEDBYTES omitted? */ /* POOLNONPAGEBYTES omitted? */ INT_FIELD_STORE(handles, (int32)getLongValue(&processCtrCache[HANDLECOUNT_IDX], PerfCntrBlk)); return; } PerfInst = NextInstance(PerfCntrBlk); } } else { #ifdef FLG_DEBUG fprintf(stderr, "DEBUG: unexpected 0 instances!\n"); #endif } } #undef INT_FIELD_STORE #undef LONG_FIELD_STORE #undef FLOAT_FIELD_STORE #undef DOUBLE_FIELD_STORE }
/******************************************************************************\ * * This is a part of the Microsoft Source Code Samples. * * Copyright (C) 1993-1997 Microsoft Corporation. * * All rights reserved. \******************************************************************************/ BOOL _FindAndKillProcessNT4(LPSTR aProcessName, BOOL aKillProcess) { BOOL bRv; HKEY hKey; DWORD dwType; DWORD dwSize; DWORD dwTemp; DWORD dwTitleLastIdx; DWORD dwIndex; DWORD dwLen; DWORD pDataSize = 50 * 1024; LPSTR szCounterValueName; LPSTR szTitle; LPSTR *szTitleSz; LPSTR szTitleBuffer; PPERF_DATA pData; PPERF_OBJECT pProcessObject; bRv = FALSE; hKey = NULL; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\perflib"), 0, KEY_READ, &hKey) != ERROR_SUCCESS) return(bRv); dwSize = sizeof(dwTitleLastIdx); if(RegQueryValueEx(hKey, TEXT("Last Counter"), 0, &dwType, (LPBYTE)&dwTitleLastIdx, &dwSize) != ERROR_SUCCESS) { RegCloseKey(hKey); return(bRv); } dwSize = sizeof(dwTemp); if(RegQueryValueEx(hKey, TEXT("Version"), 0, &dwType, (LPBYTE)&dwTemp, &dwSize) != ERROR_SUCCESS) { RegCloseKey(hKey); szCounterValueName = TEXT("Counters"); if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009"), 0, KEY_READ, &hKey) != ERROR_SUCCESS) return(bRv); } else { RegCloseKey(hKey); szCounterValueName = TEXT("Counter 009"); hKey = HKEY_PERFORMANCE_DATA; } // Find out the size of the data. // dwSize = 0; if(RegQueryValueEx(hKey, szCounterValueName, 0, &dwType, 0, &dwSize) != ERROR_SUCCESS) return(bRv); // Allocate memory // szTitleBuffer = (LPTSTR)LocalAlloc(LMEM_FIXED, dwSize); if(!szTitleBuffer) { RegCloseKey(hKey); return(bRv); } szTitleSz = (LPTSTR *)LocalAlloc(LPTR, (dwTitleLastIdx + 1) * sizeof(LPTSTR)); if(!szTitleSz) { if(szTitleBuffer != NULL) { LocalFree(szTitleBuffer); szTitleBuffer = NULL; } RegCloseKey(hKey); return(bRv); } // Query the data // if(RegQueryValueEx(hKey, szCounterValueName, 0, &dwType, (BYTE *)szTitleBuffer, &dwSize) != ERROR_SUCCESS) { RegCloseKey(hKey); if(szTitleSz) LocalFree(szTitleSz); if(szTitleBuffer) LocalFree(szTitleBuffer); return(bRv); } // Setup the TitleSz array of pointers to point to beginning of each title string. // TitleBuffer is type REG_MULTI_SZ. // szTitle = szTitleBuffer; while(dwLen = lstrlen(szTitle)) { dwIndex = atoi(szTitle); szTitle = szTitle + dwLen + 1; if(dwIndex <= dwTitleLastIdx) szTitleSz[dwIndex] = szTitle; szTitle = szTitle + lstrlen(szTitle) + 1; } PX_PROCESS = GetTitleIdx (NULL, szTitleSz, dwTitleLastIdx, PN_PROCESS); PX_PROCESS_ID = GetTitleIdx (NULL, szTitleSz, dwTitleLastIdx, PN_PROCESS_ID); PX_THREAD = GetTitleIdx (NULL, szTitleSz, dwTitleLastIdx, PN_THREAD); wsprintf(INDEX_PROCTHRD_OBJ, TEXT("%ld %ld"), PX_PROCESS, PX_THREAD); pData = NULL; if(GetPerfData(HKEY_PERFORMANCE_DATA, INDEX_PROCTHRD_OBJ, &pData, &pDataSize) == ERROR_SUCCESS) { PPERF_INSTANCE pInst; DWORD i = 0; pProcessObject = FindObject(pData, PX_PROCESS); if(pProcessObject) { LPSTR szPtrStr; int iLen; char szProcessNamePruned[MAX_BUF]; char szNewProcessName[MAX_BUF]; if(sizeof(szProcessNamePruned) < (lstrlen(aProcessName) + 1)) { if(hKey) RegCloseKey(hKey); if(szTitleSz) LocalFree(szTitleSz); if(szTitleBuffer) LocalFree(szTitleBuffer); return(bRv); } /* look for .exe and remove from end of string because the Process names * under NT are returned without the extension */ lstrcpy(szProcessNamePruned, aProcessName); iLen = lstrlen(szProcessNamePruned); szPtrStr = &szProcessNamePruned[iLen - 4]; if((lstrcmpi(szPtrStr, ".exe") == 0) || (lstrcmpi(szPtrStr, ".dll") == 0)) *szPtrStr = '\0'; pInst = FirstInstance(pProcessObject); for(i = 0; i < (DWORD)(pProcessObject->NumInstances); i++) { *szNewProcessName = '\0'; if(WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)((PCHAR)pInst + pInst->NameOffset), -1, szNewProcessName, MAX_BUF, NULL, NULL) != 0) { if(lstrcmpi(szNewProcessName, szProcessNamePruned) == 0) { if(aKillProcess) KillProcess(aProcessName, NULL, PX_PROCESS_ID); bRv = TRUE; break; } } pInst = NextInstance(pInst); } } } if(hKey) RegCloseKey(hKey); if(szTitleSz) LocalFree(szTitleSz); if(szTitleBuffer) LocalFree(szTitleBuffer); return(bRv); }
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); }
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; }