perf: merge cpuCoreUsage/cpuCoreFreq/cpuCoreHistory into single cpuCores array

This commit is contained in:
Damocles 2026-04-15 18:58:24 +02:00
parent 8fdd9692e6
commit c8d71bd871
3 changed files with 25 additions and 34 deletions

View file

@ -22,9 +22,7 @@ M.BarSection {
}
}
readonly property var _coreUsage: M.SystemStats.cpuCoreUsage
readonly property var _coreFreq: M.SystemStats.cpuCoreFreq
readonly property var _coreHistory: M.SystemStats.cpuCoreHistory
readonly property var _cores: M.SystemStats.cpuCores
readonly property var _coreMaxFreq: M.SystemStats.cpuCoreMaxFreq
readonly property var _coreTypes: M.SystemStats.cpuCoreTypes
@ -114,14 +112,14 @@ M.BarSection {
// Per-core rows
Repeater {
model: root._coreUsage.length
model: root._cores.length
delegate: Item {
required property int index
width: hoverPanel.contentWidth
readonly property int _u: root._coreUsage[index] ?? 0
readonly property real _f: root._coreFreq[index] ?? 0
readonly property int _u: root._cores[index]?.usage ?? 0
readonly property real _f: root._cores[index]?.freq_ghz ?? 0
readonly property color _barColor: root._loadColor(_u)
readonly property bool _throttled: {
const maxF = root._coreMaxFreq[index] ?? 0;
@ -204,7 +202,7 @@ M.BarSection {
width: 32
height: 10
property var _hist: root._coreHistory[parent.parent.index] || []
property var _hist: root._cores[parent.parent.index]?.history ?? []
property color _col: parent.parent._barColor
on_HistChanged: if (root._showPanel)

View file

@ -10,9 +10,7 @@ QtObject {
// CPU
property int cpuUsage: 0
property real cpuFreqGhz: 0
property var cpuCoreUsage: []
property var cpuCoreFreq: []
property var cpuCoreHistory: []
property var cpuCores: [] // [{usage, freq_ghz, history:[]}]
property var cpuCoreMaxFreq: []
property var cpuCoreTypes: []
@ -40,17 +38,17 @@ QtObject {
if (ev.type === "cpu") {
root.cpuUsage = ev.usage;
root.cpuFreqGhz = ev.freq_ghz;
root.cpuCoreUsage = ev.core_usage;
root.cpuCoreFreq = ev.core_freq_ghz;
const histLen = 16;
const oldH = root.cpuCoreHistory;
const newH = [];
for (let i = 0; i < ev.core_usage.length; i++) {
const prev = i < oldH.length ? oldH[i] : [];
const next = prev.concat([ev.core_usage[i]]);
newH.push(next.length > histLen ? next.slice(next.length - histLen) : next);
}
root.cpuCoreHistory = newH;
const prev = root.cpuCores;
root.cpuCores = ev.cores.map((c, i) => {
const oldHist = prev[i]?.history ?? [];
const hist = oldHist.concat([c.usage]);
return {
usage: c.usage,
freq_ghz: c.freq_ghz,
history: hist.length > histLen ? hist.slice(hist.length - histLen) : hist
};
});
} else if (ev.type === "mem") {
root.memPercent = ev.percent;
root.memUsedGb = ev.used_gb;

View file

@ -132,22 +132,18 @@ fn emit_cpu(out: &mut impl Write, prev: &[Sample], curr: &[Sample], freqs: &[f64
freqs.iter().sum::<f64>() / freqs.len() as f64
};
let n = core_usage.len().max(freqs.len());
let _ = write!(
out,
"{{\"type\":\"cpu\",\"usage\":{usage},\"freq_ghz\":{avg_freq:.3},\"core_usage\":["
"{{\"type\":\"cpu\",\"usage\":{usage},\"freq_ghz\":{avg_freq:.3},\"cores\":["
);
for (i, u) in core_usage.iter().enumerate() {
for i in 0..n {
if i > 0 {
let _ = write!(out, ",");
}
let _ = write!(out, "{u}");
}
let _ = write!(out, "],\"core_freq_ghz\":[");
for (i, f) in freqs.iter().enumerate() {
if i > 0 {
let _ = write!(out, ",");
}
let _ = write!(out, "{f:.3}");
let u = core_usage.get(i).copied().unwrap_or(0);
let f = freqs.get(i).copied().unwrap_or(0.0);
let _ = write!(out, "{{\"usage\":{u},\"freq_ghz\":{f:.3}}}");
}
let _ = writeln!(out, "]}}");
}
@ -360,8 +356,7 @@ SwapFree: 8192000 kB";
assert!(s.contains("\"type\":\"cpu\""));
assert!(s.contains("\"usage\":"));
assert!(s.contains("\"freq_ghz\":"));
assert!(s.contains("\"core_usage\":"));
assert!(s.contains("\"core_freq_ghz\":"));
assert!(s.contains("\"cores\":"));
assert!(s.trim().ends_with('}'));
}
@ -399,8 +394,8 @@ SwapFree: 8192000 kB";
let mut buf = Vec::new();
emit_cpu(&mut buf, &curr, &curr, &freqs);
let s = String::from_utf8(buf).unwrap();
assert!(s.contains("3.200"), "got: {s}");
assert!(s.contains("2.900"), "got: {s}");
assert!(s.contains("\"freq_ghz\":3.200"), "got: {s}");
assert!(s.contains("\"freq_ghz\":2.900"), "got: {s}");
}
// ── emit_mem (via parse_meminfo) ─────────────────────────────────────