fix CPU usage
This commit is contained in:
@@ -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,11 +20,12 @@ 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]);
|
||||||
@@ -36,17 +38,18 @@ PanelWindow {
|
|||||||
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);
|
console.log("diffTotal is " + diffTotal);
|
||||||
|
|
||||||
lastCpuIdle = idleAll;
|
root.lastCpuIdle = idleAll;
|
||||||
lastCpuTotal = total;
|
root.lastCpuTotal = total;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user