Using pdh.h to get Proc Frequency...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Using pdh.h to get Proc Frequency...

  1. #1
    Join Date
    Dec 2008
    Posts
    2

    Question Using pdh.h to get Proc Frequency...

    I am writing a system monitor in the Logitec G15 SDK.

    I cannot get "\\Processor Performance(PPM_Processor_0)\\% Processor Frequency" to return a counter. It shows up exactly like that in the Performance Monitor in the Computer Management program. I have used PdhLookupPerfNameByIndex to get localized names.

    The "\\Processor(x)\\% Processor Time" works perfectly. I am stumped.

    Code:
    #include "Page0.h"
    #include "stdafx.h"
    #include "winbase.h"
    #include <tchar.h>
    
    Page0::Page0(void)
    {
    
    }
    
    Page0::~Page0(void)
    {
    }
    
    DWORD Page0::Initialize( CEzLcd *myLCD)
    {
    	DWORD				index;
    	WCHAR				text[256];
    	PDH_STATUS			pdhStatus;
    	LGProgressBarType	cpuPBType;
    
    //===============================================================================================
    //== SETUP GLOBALS
    //===============================================================================================
    	p0_ItsLCD = myLCD;
    
    	for( index=0 ; index<4 ; index++) {
    		hq_CPULoad[index] = NULL;
    		hc_CPULoad[index] = NULL;
    		hq_ProcFreq[index] = NULL;
    		hc_ProcFreq[index] = NULL;
    	}
    	dw_CPUCount = 0;
    	dw_PFreqCount = 0;
    
    #define PERF_NAME_SIZE	4
    	LSTATUS						dwStatus;
    
    	DWORD						p0_PerfIndex[PERF_NAME_SIZE] = { 238, 6 , 6332, 6334};
    	PPERF_DATA_BLOCK			p0_PerfNames[PERF_NAME_SIZE];
    	DWORD						p0_PerfSizes[PERF_NAME_SIZE];
    
    	PDH_COUNTER_PATH_ELEMENTS	pdhCpe;
    	WCHAR						pdhPath[256];
    	DWORD						pdhPathSize;
    
    	//
    	// Get the LOCALIZED names of performance counters
    	//
    	for( index=0 ; index<PERF_NAME_SIZE ; index++) {
    		PdhLookupPerfNameByIndex( NULL, p0_PerfIndex[index], NULL, &p0_PerfSizes[index]); 
    		p0_PerfNames[index] = (PPERF_DATA_BLOCK)malloc( p0_PerfSizes[index]);
    		PdhLookupPerfNameByIndex( NULL, p0_PerfIndex[index], (LPWSTR)p0_PerfNames[index], &p0_PerfSizes[index]); 
    	}
    
    	//
    	// LOAD CPU PERFORMANCE COUNTERS FOR EACH CORE
    	//
    	for( index=0 ; index<4 ; index++) {
    		swprintf( text, L"%d", index);
    		pdhCpe.szMachineName    = NULL;
    		pdhCpe.szObjectName     = (LPWSTR)p0_PerfNames[0];
    		pdhCpe.szInstanceName   = (LPWSTR)text;
    		pdhCpe.szParentInstance = NULL;
    		pdhCpe.dwInstanceIndex  = -1;
    		pdhCpe.szCounterName    = (LPWSTR)p0_PerfNames[1];
    		pdhPathSize = sizeof(pdhPath);
    		pdhStatus = PdhMakeCounterPath( &pdhCpe, pdhPath, &pdhPathSize, 0);
    		if( pdhStatus == ERROR_SUCCESS)
    		pdhStatus = PdhOpenQuery( NULL, 0, &hq_CPULoad[index]);
    		if( pdhStatus == ERROR_SUCCESS)
    		pdhStatus = PdhAddCounter( hq_CPULoad[index], pdhPath, 0, &hc_CPULoad[index]);
    		if( pdhStatus == ERROR_SUCCESS)
    		dw_CPUCount++;
    	}
    
    	//
    	// LOAD CPU FREQUENCY COUNTERS
    	//
    	for( index=0 ; index<dw_CPUCount ; index++) {
    		swprintf( text, L"PPM_Processor_%d", index);
    		pdhCpe.szMachineName    = NULL;
    		pdhCpe.szObjectName     = (LPWSTR)p0_PerfNames[2];
    		pdhCpe.szInstanceName   = (LPWSTR)text;
    		pdhCpe.szParentInstance = NULL;
    		pdhCpe.dwInstanceIndex  = -1;
    		pdhCpe.szCounterName    = (LPWSTR)p0_PerfNames[3];
    		pdhPathSize = sizeof(pdhPath);
    		pdhStatus = PdhMakeCounterPath( &pdhCpe, pdhPath, &pdhPathSize, 0);
    		if( pdhStatus == ERROR_SUCCESS)
    		pdhStatus = PdhOpenQuery( NULL, 0, &hq_ProcFreq[index]);
    		if( pdhStatus == ERROR_SUCCESS)
    		//
    		// This is where it NEVER returns a Counter...
    		//
    		// pdhPath = "\Processor Performance(PPM_Processor_0)\Processor Frequency"
    		// pdhPath = "\Processor Performance(PPM_Processor_1)\Processor Frequency"
    		// pdhPath = "\Processor Performance(PPM_Processor_2)\Processor Frequency"
    		// pdhPath = "\Processor Performance(PPM_Processor_3)\Processor Frequency"
    		//
    		pdhStatus = PdhAddCounter( hq_ProcFreq[index], pdhPath, 0, &hc_ProcFreq[index]);
    		if( pdhStatus == ERROR_SUCCESS)
    		dw_PFreqCount++;
    	}
    
    /*	//
    	// FINISH UP ANY ALLOCATED MEMORY
    	//
    	// This crashed the heap. Should this even be done?
    	//
    	for( index=0 ; index<PERF_NAME_SIZE ; index++) 
    		if( p0_PerfNames[index]) {
    			free((void*) p0_PerfNames[index]);
    			p0_PerfNames[index] = NULL;
    		}
    */
    
    	//
    	// START BUILDING THE LCD OBJECTS
    	//
    //===============================================================================================
    //== CPU LOAD FUNCTIONS
    //===============================================================================================
    	switch( dw_CPUCount) {
    		case 2:
    			cpuPBType = LG_DUEL;
    			break;
    		case 4:
    			cpuPBType = LG_QUAD;
    			break;
    		default:
    			cpuPBType = LG_FILLED;
    			break;
    	};
    	lcd_cpuProgress = myLCD->AddProgressBar( cpuPBType);
        myLCD->SetProgressBarSize( lcd_cpuProgress, 100, 10);
        myLCD->SetOrigin( lcd_cpuProgress, LGLCD_BMP_WIDTH-100, 1);
    	lcd_cpuText = myLCD->AddText( LG_SCROLLING_TEXT, LG_SMALL, DT_RIGHT, 36);
        myLCD->SetOrigin( lcd_cpuText, LGLCD_BMP_WIDTH-136, 3);
    
    //===============================================================================================
    //== MEMORY LOAD FUNCTIONS
    //===============================================================================================
        lcd_memProgress = myLCD->AddProgressBar( LG_FILLED);
        myLCD->SetProgressBarSize( lcd_memProgress, 100, 8);
        myLCD->SetOrigin( lcd_memProgress, LGLCD_BMP_WIDTH-100, 12);
    
    	lcd_pgfProgress = myLCD->AddProgressBar( LG_FILLED);
        myLCD->SetProgressBarSize( lcd_pgfProgress, 100, 3);
        myLCD->SetOrigin( lcd_pgfProgress, LGLCD_BMP_WIDTH-100, 17);
    
    	lcd_memText = myLCD->AddText( LG_STATIC_TEXT, LG_SMALL, DT_RIGHT, 36);
        myLCD->SetOrigin( lcd_memText, LGLCD_BMP_WIDTH-136, 12);
    
    	myLCD->SetText( lcd_cpuText, _T("x Ghz"));
    	myLCD->SetText( lcd_memText, _T("x Gb"));
    	myLCD->SetProgressBarPosition( lcd_cpuProgress, 100, 0);
    	myLCD->SetProgressBarPosition( lcd_memProgress, 100, 0);
    	myLCD->SetProgressBarPosition( lcd_pgfProgress, 100, 0);
    
    	return 0;
    }
    
    
    void Page0::Shutdown( void)
    {
    	DWORD		index;
    	for( index=0 ; index<dw_CPUCount ; index++) {
    		if( hq_CPULoad[index])
    			PdhCloseQuery( hq_CPULoad[index]);
    	}
    }
    
    void Page0::Idle( void)
    {
    
    }
    
    void Page0::Update( void)
    {
    	PDH_FMT_COUNTERVALUE	ctrValue;
    	PDH_STATUS				pdhStatus;
    	DWORD					index;
    	MEMORYSTATUSEX			memoryStatus;
    	TCHAR					text[256];
    
    	memoryStatus.dwLength = sizeof(MEMORYSTATUSEX);
    
    	// CPU LOAD
    	for( index=0 ; index<dw_CPUCount ; index++) {
    		pdhStatus = PdhCollectQueryData( hq_CPULoad[index]);
    		pdhStatus = PdhGetFormattedCounterValue( hc_CPULoad[index], PDH_FMT_DOUBLE, NULL, &ctrValue);
    		p0_ItsLCD->SetProgressBarPosition( lcd_cpuProgress, (FLOAT)ctrValue.doubleValue, index+1);
    	}
    
    	// MEMORY LOAD
    	if( GlobalMemoryStatusEx( &memoryStatus)) {
    		FLOAT		usedMem = (FLOAT)memoryStatus.ullTotalPhys - (FLOAT)memoryStatus.ullAvailPhys;
    		DWORDLONG	usedVM = memoryStatus.ullTotalVirtual - memoryStatus.ullAvailVirtual;
    		FLOAT		pctVM = ((FLOAT)usedVM*100) / (FLOAT)memoryStatus.ullAvailVirtual;
    
    		usedMem = usedMem / 1073741824;
    		_stprintf( text, _T("%4.2f Gb"), usedMem);
    
    		p0_ItsLCD->SetText( lcd_memText, text);
    		p0_ItsLCD->SetProgressBarPosition( lcd_memProgress, (FLOAT)memoryStatus.dwMemoryLoad, 0);
    		p0_ItsLCD->SetProgressBarPosition( lcd_pgfProgress, (FLOAT)pctVM, 0);
    	}
    }

  2. #2
    Join Date
    Dec 2008
    Posts
    2

    Exclamation [resolved]

    [RESOLVED]

    The program must be running with Admin privlegdes in Vista.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center