|
3 | 3 |
|
4 | 4 | #include "util/mallocHelper.h"
|
5 | 5 |
|
6 |
| -#if !FF_ENABLE_CPUUSAGE_PERFLIB |
7 | 6 | #include <ntstatus.h>
|
8 | 7 | #include <winternl.h>
|
| 8 | +#include <windows.h> |
| 9 | +#include <wchar.h> |
| 10 | +#include "util/windows/perflib_.h" |
9 | 11 |
|
10 |
| -const char* ffGetCpuUsageInfo(FFlist* cpuTimes) |
| 12 | +static const char* getInfoByNqsi(FFlist* cpuTimes) |
11 | 13 | {
|
12 | 14 | ULONG size = 0;
|
13 | 15 | if(NtQuerySystemInformation(SystemProcessorPerformanceInformation, NULL, 0, &size) != STATUS_INFO_LENGTH_MISMATCH)
|
@@ -36,49 +38,53 @@ const char* ffGetCpuUsageInfo(FFlist* cpuTimes)
|
36 | 38 |
|
37 | 39 | return NULL;
|
38 | 40 | }
|
39 |
| -#else |
40 |
| -#include <windows.h> |
41 |
| -#include "util/windows/perflib_.h" |
42 |
| -#include <wchar.h> |
43 | 41 |
|
44 |
| -static inline void ffPerfCloseQueryHandle(HANDLE* phQuery) |
| 42 | +static const char* getInfoByPerflib(FFlist* cpuTimes) |
45 | 43 | {
|
46 |
| - if (*phQuery != NULL) |
| 44 | + static HANDLE hQuery = NULL; |
| 45 | + |
| 46 | + if (hQuery == NULL) |
47 | 47 | {
|
48 |
| - PerfCloseQueryHandle(*phQuery); |
49 |
| - *phQuery = NULL; |
| 48 | + struct FFPerfQuerySpec |
| 49 | + { |
| 50 | + PERF_COUNTER_IDENTIFIER Identifier; |
| 51 | + WCHAR Name[16]; |
| 52 | + } querySpec = { |
| 53 | + .Identifier = { |
| 54 | + // Processor Information GUID |
| 55 | + // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\_V2Providers\{383487a6-3676-4870-a4e7-d45b30c35629}\{b4fc721a-0378-476f-89ba-a5a79f810b36} |
| 56 | + .CounterSetGuid = { 0xb4fc721a, 0x0378, 0x476f, {0x89, 0xba, 0xa5, 0xa7, 0x9f, 0x81, 0x0b, 0x36} }, |
| 57 | + .Size = sizeof(querySpec), |
| 58 | + .CounterId = PERF_WILDCARD_COUNTER, // https://learn.microsoft.com/en-us/windows/win32/perfctrs/using-the-perflib-functions-to-consume-counter-data |
| 59 | + .InstanceId = PERF_WILDCARD_COUNTER, |
| 60 | + }, |
| 61 | + .Name = PERF_WILDCARD_INSTANCE, |
| 62 | + }; |
| 63 | + |
| 64 | + if (PerfOpenQueryHandle(NULL, &hQuery) != ERROR_SUCCESS) |
| 65 | + { |
| 66 | + PerfCloseQueryHandle(hQuery); |
| 67 | + hQuery = INVALID_HANDLE_VALUE; |
| 68 | + return "PerfOpenQueryHandle() failed"; |
| 69 | + } |
| 70 | + |
| 71 | + if (PerfAddCounters(hQuery, &querySpec.Identifier, sizeof(querySpec)) != ERROR_SUCCESS) |
| 72 | + { |
| 73 | + PerfCloseQueryHandle(hQuery); |
| 74 | + hQuery = INVALID_HANDLE_VALUE; |
| 75 | + return "PerfAddCounters() failed"; |
| 76 | + } |
| 77 | + |
| 78 | + if (querySpec.Identifier.Status != ERROR_SUCCESS) |
| 79 | + { |
| 80 | + PerfCloseQueryHandle(hQuery); |
| 81 | + hQuery = INVALID_HANDLE_VALUE; |
| 82 | + return "PerfAddCounters() reports invalid identifier"; |
| 83 | + } |
50 | 84 | }
|
51 |
| -} |
52 | 85 |
|
53 |
| -const char* ffGetCpuUsageInfo(FFlist* cpuTimes) |
54 |
| -{ |
55 |
| - struct FFPerfQuerySpec |
56 |
| - { |
57 |
| - PERF_COUNTER_IDENTIFIER Identifier; |
58 |
| - WCHAR Name[16]; |
59 |
| - } querySpec = { |
60 |
| - .Identifier = { |
61 |
| - // Processor Information GUID |
62 |
| - // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\_V2Providers\{383487a6-3676-4870-a4e7-d45b30c35629}\{b4fc721a-0378-476f-89ba-a5a79f810b36} |
63 |
| - .CounterSetGuid = { 0xb4fc721a, 0x0378, 0x476f, {0x89, 0xba, 0xa5, 0xa7, 0x9f, 0x81, 0x0b, 0x36} }, |
64 |
| - .Size = sizeof(querySpec), |
65 |
| - .CounterId = PERF_WILDCARD_COUNTER, // https://learn.microsoft.com/en-us/windows/win32/perfctrs/using-the-perflib-functions-to-consume-counter-data |
66 |
| - .InstanceId = PERF_WILDCARD_COUNTER, |
67 |
| - }, |
68 |
| - .Name = PERF_WILDCARD_INSTANCE, |
69 |
| - }; |
70 |
| - |
71 |
| - __attribute__((__cleanup__(ffPerfCloseQueryHandle))) |
72 |
| - HANDLE hQuery = NULL; |
73 |
| - |
74 |
| - if (PerfOpenQueryHandle(NULL, &hQuery) != ERROR_SUCCESS) |
75 |
| - return "PerfOpenQueryHandle() failed"; |
76 |
| - |
77 |
| - if (PerfAddCounters(hQuery, &querySpec.Identifier, sizeof(querySpec)) != ERROR_SUCCESS) |
78 |
| - return "PerfAddCounters() failed"; |
79 |
| - |
80 |
| - if (querySpec.Identifier.Status != ERROR_SUCCESS) |
81 |
| - return "PerfAddCounters() reports invalid identifier"; |
| 86 | + if (hQuery == INVALID_HANDLE_VALUE) |
| 87 | + return "Init hQuery failed"; |
82 | 88 |
|
83 | 89 | DWORD dataSize = 0;
|
84 | 90 | if (PerfQueryCounterData(hQuery, NULL, 0, &dataSize) != ERROR_NOT_ENOUGH_MEMORY)
|
@@ -146,4 +152,23 @@ const char* ffGetCpuUsageInfo(FFlist* cpuTimes)
|
146 | 152 |
|
147 | 153 | return NULL;
|
148 | 154 | }
|
149 |
| -#endif |
| 155 | + |
| 156 | +const char* ffGetCpuUsageInfo(FFlist* cpuTimes) |
| 157 | +{ |
| 158 | + #if __aarch64__ |
| 159 | + static uint8_t winver = 10; // Assume Windows 10 or later for WoA |
| 160 | + #else |
| 161 | + static uint8_t winver = 0; |
| 162 | + if (winver == 0) |
| 163 | + winver = (uint8_t) ffStrbufToUInt(&instance.state.platform.sysinfo.release, 1); |
| 164 | + #endif |
| 165 | + |
| 166 | + if (winver >= 10) |
| 167 | + { |
| 168 | + if (getInfoByPerflib(cpuTimes) == NULL) return NULL; |
| 169 | + ffListClear(cpuTimes); |
| 170 | + winver = 1; // Fall back to NQSI |
| 171 | + } |
| 172 | + |
| 173 | + return getInfoByNqsi(cpuTimes); |
| 174 | +} |
0 commit comments