MCPcopy Create free account
hub / github.com/benapetr/TuxManager / readCurrentFreq

Method readCurrentFreq

src/system/cpu.cpp:247–331  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

245}
246
247void CPU::readCurrentFreq()
248{
249 const int coreCount = qMax(static_cast<int>(this->m_cores.size()), this->m_cpuLogicalCount);
250 QVector<double> coreMhz(coreCount, 0.0);
251
252 this->detectCpuFreqSource();
253
254 if (this->m_cpuFreqUseSysfs)
255 {
256 for (int i = 0; i < this->m_cpuFreqPaths.size() && i < coreMhz.size(); ++i)
257 {
258 bool ok = false;
259 const double kHz = Misc::ReadFile(this->m_cpuFreqPaths.at(i)).toDouble(&ok);
260 if (ok && kHz > 0.0)
261 coreMhz[i] = kHz / 1000.0;
262 }
263 } else
264 {
265 // Fallback: parse per-core live frequencies from /proc/cpuinfo.
266 int currentProcessor = -1;
267 QFile f("/proc/cpuinfo");
268 if (f.open(QIODevice::ReadOnly | QIODevice::Text))
269 {
270 for (;;)
271 {
272 const QByteArray line = f.readLine();
273 if (line.isNull())
274 break;
275
276 const int colon = line.indexOf(':');
277 if (colon < 0)
278 continue;
279
280 const QByteArray key = line.left(colon).trimmed();
281 const QByteArray val = line.mid(colon + 1).trimmed();
282 if (key == "processor")
283 {
284 currentProcessor = val.toInt();
285 }
286 else if (key == "cpu MHz" && currentProcessor >= 0 && currentProcessor < coreMhz.size())
287 {
288 coreMhz[currentProcessor] = val.toDouble();
289 }
290 }
291 f.close();
292 }
293 }
294
295 double sumMhz = 0.0;
296 int countMhz = 0;
297 for (double mhz : std::as_const(coreMhz))
298 {
299 if (mhz > 0.0)
300 {
301 sumMhz += mhz;
302 ++countMhz;
303 }
304 }

Callers 2

SampleMethod · 0.95
readCpuMetadataMethod · 0.95

Calls 1

detectCpuFreqSourceMethod · 0.95

Tested by

no test coverage detected