MCPcopy Create free account
hub / github.com/awawa-dev/HyperHDR / getCPU

Method getCPU

sources/performance-counters/SystemPerformanceCounters.cpp:175–279  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

173}
174
175QString SystemPerformanceCounters::getCPU()
176{
177 try
178 {
179 QJsonObject jResult;
180 QJsonArray jCores;
181
182 if (!isInitialized)
183 {
184 init();
185 return "";
186 }
187
188#ifdef _WIN32
189 DWORD size = 0, count = 0;
190
191 if (cpuPerfQuery == nullptr || PdhCollectQueryData(cpuPerfQuery) != ERROR_SUCCESS)
192 return "";
193
194 if (PdhGetFormattedCounterArray(cpuPerfTotal, PDH_FMT_LONG, &size, &count, nullptr) != PDH_MORE_DATA)
195 return "";
196
197 QByteArray myByteArray(size, 0);
198 PDH_FMT_COUNTERVALUE_ITEM* bufferData = (PDH_FMT_COUNTERVALUE_ITEM*)myByteArray.data();
199
200 if (PdhGetFormattedCounterArray(cpuPerfTotal, PDH_FMT_LONG, &size, &count, bufferData) != ERROR_SUCCESS)
201 {
202 return "";
203 }
204
205 for (DWORD i = 0; i < count; i++)
206 {
207 auto valCPU = bufferData[i].FmtValue.longValue;
208
209#ifdef UNICODE
210 QString convertedStr = QString::fromWCharArray(bufferData[i].szName);
211#else
212 QString convertedStr = QString::fromLocal8Bit(buffer[i].szName);
213#endif
214
215 if (convertedStr != "_Total")
216 {
217 jCores.append(valCPU / 100.0f);
218 }
219 else
220 {
221 jResult["total"] = (double)valCPU;
222 }
223 }
224
225#else
226
227 if (totalPerfCPU > 0)
228 {
229 memcpy(&cpuOldStat, &cpuStat, sizeof(cpuOldStat));
230
231 if (readCpuLines() == totalPerfCPU)
232 {

Callers 1

Calls 1

dataMethod · 0.80

Tested by

no test coverage detected