fix CPU usage

This commit is contained in:
Sem
2026-03-26 21:40:45 +01:00
parent e3e9890692
commit 44cacea7ae

View File

@@ -5,6 +5,7 @@ import Quickshell.Io
import QtQuick import QtQuick
import QtQuick.Layouts import QtQuick.Layouts
// qmllint disable uncreatable-type
PanelWindow { PanelWindow {
id: root id: root
@@ -19,34 +20,36 @@ PanelWindow {
Process { Process {
id: cpuProc id: cpuProc
// get cpu usage, first line of /proc/stat
command: ["cat", "/proc/stat"] command: ["cat", "/proc/stat"]
stdout: { stdout: StdioCollector {
console.log(stdout); onStreamFinished: {
console.log("test"); var line = this.text.split(/\r?\n/)[1].split(" ");
var line = stdout.split("\n")[0].split(/\s+/); console.log("the line is " + line);
var user = parseInt(line[1]); var user = parseInt(line[1]);
var nice = parseInt(line[2]); var nice = parseInt(line[2]);
var system = parseInt(line[3]); var system = parseInt(line[3]);
var idle = parseInt(line[4]); var idle = parseInt(line[4]);
var iowait = parseInt(line[5]); var iowait = parseInt(line[5]);
var irq = parseInt(line[6]); var irq = parseInt(line[6]);
var softirq = parseInt(line[7]); var softirq = parseInt(line[7]);
var idleAll = idle + iowait; var idleAll = idle + iowait;
var total = user + nice + system + idle + iowait + irq + softirq; var total = user + nice + system + idle + iowait + irq + softirq;
var diffIdle = idleAll - lastCpuIdle; var diffIdle = idleAll - root.lastCpuIdle;
var diffTotal = total - lastCpuTotal; var diffTotal = total - root.lastCpuTotal;
if (diffTotal > 0) { if (diffTotal > 0) {
cpuUsage = Math.round(100 * (1 - diffIdle / diffTotal)); root.cpuUsage = Math.round(100 * (1 - diffIdle / diffTotal));
}
console.log("diffTotal is " + diffTotal);
root.lastCpuIdle = idleAll;
root.lastCpuTotal = total;
} }
console.log("diffTotal is " + diffTotal);
lastCpuIdle = idleAll;
lastCpuTotal = total;
} }
} }