|
1 | | -// CPU istatistikleri |
2 | | - |
3 | 1 | #include "cpumonitor.h" |
4 | 2 |
|
| 3 | +#include <QFile> |
| 4 | +#include <QTextStream> |
| 5 | +#include <QDir> |
| 6 | + |
5 | 7 | CpuMonitor::CpuMonitor(QObject *parent) |
6 | 8 | : QObject(parent) |
7 | | -{} |
| 9 | +{ |
| 10 | + connect(&m_timer, &QTimer::timeout, this, &CpuMonitor::poll); |
| 11 | +} |
| 12 | + |
| 13 | +void CpuMonitor::start(int intervalMs) |
| 14 | +{ |
| 15 | + poll(); |
| 16 | + m_timer.start(intervalMs); |
| 17 | +} |
| 18 | + |
| 19 | +void CpuMonitor::stop() |
| 20 | +{ |
| 21 | + m_timer.stop(); |
| 22 | +} |
| 23 | + |
| 24 | +void CpuMonitor::poll() |
| 25 | +{ |
| 26 | + // ── CPU Yükü (/proc/stat) ──────────────────────────────────────────────── |
| 27 | + // /proc/stat ilk satırı: "cpu user nice system idle iowait irq softirq..." |
| 28 | + QFile statFile(QStringLiteral("/proc/stat")); |
| 29 | + if (statFile.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 30 | + QTextStream stream(&statFile); |
| 31 | + const QString line = stream.readLine(); // "cpu ..." satırı |
| 32 | + |
| 33 | + const QStringList parts = line.split(QLatin1Char(' '), Qt::SkipEmptyParts); |
| 34 | + if (parts.size() >= 5) { |
| 35 | + // idle = index 4, total = tüm değerlerin toplamı |
| 36 | + long long idle = parts[4].toLongLong(); |
| 37 | + long long total = 0; |
| 38 | + for (int i = 1; i < parts.size(); ++i) |
| 39 | + total += parts[i].toLongLong(); |
| 40 | + |
| 41 | + // Delta ile yük hesapla |
| 42 | + const long long diffIdle = idle - m_prevIdle; |
| 43 | + const long long diffTotal = total - m_prevTotal; |
| 44 | + |
| 45 | + if (diffTotal > 0) { |
| 46 | + const int load = static_cast<int>(100 * (1.0 - static_cast<double>(diffIdle) / diffTotal)); |
| 47 | + if (load != m_load) { |
| 48 | + m_load = load; |
| 49 | + emit loadChanged(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + m_prevIdle = idle; |
| 54 | + m_prevTotal = total; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // ── CPU Sıcaklığı (hwmon) ──────────────────────────────────────────────── |
| 59 | + const int temp = readCpuTemp(); |
| 60 | + if (temp > 0 && temp != m_temperature) { |
| 61 | + m_temperature = temp; |
| 62 | + emit temperatureChanged(); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +int CpuMonitor::readCpuTemp() const |
| 67 | +{ |
| 68 | + // /sys/class/hwmon/ altındaki sıcaklık sensörlerini tara |
| 69 | + const QDir hwmonDir(QStringLiteral("/sys/class/hwmon")); |
| 70 | + const QStringList hwmons = hwmonDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot); |
| 71 | + |
| 72 | + for (const QString &hwmon : hwmons) { |
| 73 | + const QString namePath = hwmonDir.filePath(hwmon + QStringLiteral("/name")); |
| 74 | + QFile nameFile(namePath); |
| 75 | + if (!nameFile.open(QIODevice::ReadOnly)) |
| 76 | + continue; |
| 77 | + |
| 78 | + const QString name = QString::fromUtf8(nameFile.readAll()).trimmed(); |
| 79 | + |
| 80 | + // k10temp (AMD) veya coretemp (Intel) sensörü |
| 81 | + if (name == QStringLiteral("k10temp") || name == QStringLiteral("coretemp")) { |
| 82 | + QFile tempFile(hwmonDir.filePath(hwmon + QStringLiteral("/temp1_input"))); |
| 83 | + if (tempFile.open(QIODevice::ReadOnly)) { |
| 84 | + const int milliCelsius = QString::fromUtf8(tempFile.readAll()).trimmed().toInt(); |
| 85 | + return milliCelsius / 1000; // milli-Celsius → Celsius |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
0 commit comments