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