overlay: add resource monitor widget

This commit is contained in:
end-4
2025-11-07 12:56:44 +01:00
parent e5e85db75d
commit a27a6deddf
8 changed files with 272 additions and 12 deletions
@@ -10,17 +10,55 @@ import Quickshell.Io
* Simple polled resource usage service with RAM, Swap, and CPU usage.
*/
Singleton {
property double memoryTotal: 1
property double memoryFree: 1
property double memoryUsed: memoryTotal - memoryFree
property double memoryUsedPercentage: memoryUsed / memoryTotal
property double swapTotal: 1
property double swapFree: 1
property double swapUsed: swapTotal - swapFree
property double swapUsedPercentage: swapTotal > 0 ? (swapUsed / swapTotal) : 0
property double cpuUsage: 0
id: root
property real memoryTotal: 1
property real memoryFree: 0
property real memoryUsed: memoryTotal - memoryFree
property real memoryUsedPercentage: memoryUsed / memoryTotal
property real swapTotal: 1
property real swapFree: 0
property real swapUsed: swapTotal - swapFree
property real swapUsedPercentage: swapTotal > 0 ? (swapUsed / swapTotal) : 0
property real cpuUsage: 0
property var previousCpuStats
property string maxAvailableMemoryString: kbToGbString(ResourceUsage.memoryTotal)
property string maxAvailableSwapString: kbToGbString(ResourceUsage.swapTotal)
property string maxAvailableCpuString: "--"
readonly property int historyLength: Config?.options.resources.historyLength ?? 60
property list<real> cpuUsageHistory: []
property list<real> memoryUsageHistory: []
property list<real> swapUsageHistory: []
function kbToGbString(kb) {
return (kb / (1024 * 1024)).toFixed(1) + " GB";
}
function updateMemoryUsageHistory() {
memoryUsageHistory = [...memoryUsageHistory, memoryUsedPercentage]
if (memoryUsageHistory.length > historyLength) {
memoryUsageHistory.shift()
}
}
function updateSwapUsageHistory() {
swapUsageHistory = [...swapUsageHistory, swapUsedPercentage]
if (swapUsageHistory.length > historyLength) {
swapUsageHistory.shift()
}
}
function updateCpuUsageHistory() {
cpuUsageHistory = [...cpuUsageHistory, cpuUsage]
if (cpuUsageHistory.length > historyLength) {
cpuUsageHistory.shift()
}
}
function updateHistories() {
updateMemoryUsageHistory()
updateSwapUsageHistory()
updateCpuUsageHistory()
}
Timer {
interval: 1
running: true
@@ -29,6 +67,7 @@ Singleton {
// Reload files
fileMeminfo.reload()
fileStat.reload()
fileCpuinfo.reload()
// Parse memory and swap usage
const textMeminfo = fileMeminfo.text()
@@ -53,10 +92,36 @@ Singleton {
previousCpuStats = { total, idle }
}
// Parse max CPU frequency
const textCpuinfo = fileCpuinfo.text()
// Try to find 'cpu max MHz', fallback to highest 'cpu MHz'
let maxMHz = 0
let match
// Try cpu max MHz (modern kernels)
match = textCpuinfo.match(/cpu max MHz\s*:\s*([\d.]+)/)
if (match) {
maxMHz = Number(match[1])
} else {
// Fallback: find all cpu MHz lines and take the max
let mhzRegex = /cpu MHz\s*:\s*([\d.]+)/g
let mhzMatch
let mhzList = []
while ((mhzMatch = mhzRegex.exec(textCpuinfo)) !== null) {
mhzList.push(Number(mhzMatch[1]))
}
if (mhzList.length > 0) {
maxMHz = Math.max.apply(null, mhzList)
}
}
root.maxAvailableCpuString = maxMHz > 0 ? (maxMHz / 1000).toFixed(1) + "GHz" : "--"
root.updateHistories()
interval = Config.options?.resources?.updateInterval ?? 3000
}
}
FileView { id: fileMeminfo; path: "/proc/meminfo" }
FileView { id: fileStat; path: "/proc/stat" }
FileView { id: fileCpuinfo; path: "/proc/cpuinfo" }
}