PDH - Performance Counters Functions

Veröffentliche deine funktionierenden Skripte und Funktionen

Moderator: jNizM

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

PDH - Performance Counters Functions

Post by jNizM » 20 May 2022, 03:35

Moin,

ich wollte schon immer mit PDH (https://docs.microsoft.com/en-us/windows/win32/perfctrs/performance-counters-functions) und AutoHotkey spielen.

Hat ein paar Versuche (und Jahre) gedauert (da immer wieder in Vergessenheit geraten). Die CPU Auslastung scheint soweit zu funktionieren. Wie genau das im Vergleich zum Taskmanager ist, kann ich aber nicht sagen.

Falls jemand Interesse hat und damit weiter spielen möchte, natürlich der code dazu:

Code: Select all

Gui, Add, Edit, xm ym vED
Gui, Show, w200 h200

ERROR_SUCCESS  := 0
PDH_FMT_LONG   := 0x00000100
PDH_FMT_DOUBLE := 0x00000200
PDH_FMT_LARGE  := 0x00000400
try
{
	if !(hPDH := DllCall("LoadLibrary", "str", "pdh.dll", "ptr"))
		throw Exception("PDH could not be loaded", -1)

	if (PDH_STATUS := DllCall("pdh\PdhOpenQuery", "Ptr", 0, "UPtr", 0, "Ptr*", hQuery, "UInt") != ERROR_SUCCESS)
		throw Exception("PdhOpenQuery failed with " PDH_STATUS, -1)

	if (PDH_STATUS := DllCall("pdh\PdhAddEnglishCounter", "Ptr", hQuery, "Str", "\Processor(_Total)\% Processor Time", "UPtr", 0, "Ptr*", hCounter, "UInt") != ERROR_SUCCESS)
		throw Exception("PdhAddEnglishCounter failed with " PDH_STATUS, -1)

	if (PDH_STATUS := DllCall("pdh\PdhCollectQueryData", "Ptr", hQuery, "UInt") != ERROR_SUCCESS)
		throw Exception("PdhCollectQueryData failed with " PDH_STATUS, -1)

	while (ERROR_SUCCESS = PDH_STATUS)
	{
		PDH_STATUS := DllCall("pdh\PdhCollectQueryData", "Ptr", hQuery, "UInt")

		if (ERROR_SUCCESS = PDH_STATUS)
		{
			VarSetCapacity(Value, 16, 0)
			if (PDH_STATUS := DllCall("pdh\PdhGetFormattedCounterValue", "Ptr", hCounter, "UInt", PDH_FMT_DOUBLE, "Ptr", 0, "Ptr", &value, "UInt") != ERROR_SUCCESS)
				throw Exception("PdhGetFormattedCounterValue failed with " PDH_STATUS, -1)
			GuiControl,,ED, % NumGet(value, 8, "Double") " %"
			;GuiControl,,ED, % NumGet(value, 8, "Int64") " %"
		}
		sleep 1000
	}
}
catch exception
{
	throw Exception
}
finally
{
	if (hQuery)
		DllCall("pdh\PdhCloseQuery", "Ptr", hQuery)
	if (hPDH)
		DllCall("FreeLibrary", "Ptr", hPDH)
}
return

GuiClose:
GuiEscape:
	if (hQuery)
		DllCall("pdh\PdhCloseQuery", "Ptr", hQuery)
	if (hPDH)
		DllCall("FreeLibrary", "Ptr", hPDH)
ExitApp
Weitere Beispiele:

Code: Select all

"\Process(_Total)\Handle Count"						; PDH_FMT_LARGE + "Int64" bevorzugt
"\Process(_Total)\Thread Count"						; PDH_FMT_LARGE + "Int64" bevorzugt
"\Process(notepad)\% Processor Time"
"\Process(outlook)\Working Set"						; PDH_FMT_LARGE + "Int64" bevorzugt
"\Process(firefox)\Private Bytes"					; PDH_FMT_LARGE + "Int64" bevorzugt
"\Memory\Available Bytes"							; PDH_FMT_LARGE + "Int64" bevorzugt
"\Memory\Committed Bytes"							; PDH_FMT_LARGE + "Int64" bevorzugt
"\Network Interface(*)\Bytes Sent/sec"
"\Network Interface(*)\Bytes Received/sec"
"\Thermal Zone Information(*)\Temperature"			; PDH_FMT_DOUBLE + "Double" bevorzugt | Ergebnis - 273.15 (da Angabe in Kelvin)
Unter dem Registrierungseintrag Counter in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009 findet man alle vom System unterstützten Counters

Mit Powershell kann man sich auch eine Übersicht geben
Get-Counter -ListSet * | Sort-Object -Property CounterSetName | Select CounterSetName

Aufgebaut ist der String folgendermaßen -> https://docs.microsoft.com/en-us/windows/win32/perfctrs/specifying-a-counter-path

grüße
jNizM
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Skripte und Funktionen”