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