From 75cd6d4e20eb733d5054e0130eb2d4763da2ed64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abdurrahman=20Kaan=20Dike=C3=A7?= Date: Sat, 31 Oct 2026 01:09:51 +0300 Subject: [PATCH] mu300-cpu lowlatency: all or nothing, and a partly-off CPU0 is shown as such From kanoqwq cc0ecce7 (cpu-idle.uc). A state of CPU0 that does not take the value (written, then read back) puts every state back as it was, and a setting that cannot be saved is undone the same way: nothing is left half switched or switched but unsaved. status --json reports "mixed" (some of CPU0's states off, some on) next to "active". Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VVN2zDgf76kMDVSuWhHPUE --- rootfs/overlay/opt/mu300/bin/mu300-cpu | 34 +++++++++++++++++------ tests/test_cpu.py | 38 ++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/rootfs/overlay/opt/mu300/bin/mu300-cpu b/rootfs/overlay/opt/mu300/bin/mu300-cpu index 2be9848b..30e43754 100755 --- a/rootfs/overlay/opt/mu300/bin/mu300-cpu +++ b/rootfs/overlay/opt/mu300/bin/mu300-cpu @@ -170,13 +170,25 @@ cores_json() { # on average (100 ms at worst); with them off, 0.6 ms (2 ms); the stock firmware the other way round. The power cost # was not measured, so it stays off unless the owner switches it on. lowlat_nodes() { for n in "$CPUDIR"/cpu0/cpuidle/state*/disable; do [ -f "$n" ] && echo "$n"; done; } +# All or nothing (kanoqwq's cpu-idle, cc0ecce7): when a state does not take the value (written, then read back), every +# state gets back what it had, so a failed switch never leaves CPU0 half in one setting and half in the other; and a +# setting that cannot be saved is undone the same way (what runs is what the next boot applies). +lowlat_snapshot() { _lw_was=; for n in $(lowlat_nodes); do _lw_was="$_lw_was $n=$(rd "$n")"; done; } +lowlat_restore() { # back to the last lowlat_snapshot + for _nv in $_lw_was; do + [ "$(rd "${_nv%=*}")" = "${_nv##*=}" ] || { echo "${_nv##*=}" > "${_nv%=*}"; } 2>/dev/null + done + return 0 +} lowlat_write() { # lowlat_write 0|1: 1 disables every state of CPU0 - _st=0 + lowlat_snapshot for n in $(lowlat_nodes); do [ "$(rd "$n")" = "$1" ] && continue - echo "$1" > "$n" 2>/dev/null || _st=1 + { echo "$1" > "$n"; } 2>/dev/null && [ "$(rd "$n")" = "$1" ] && continue + lowlat_restore + return 1 done - return $_st + return 0 } lowlat_supported() { [ -n "$(lowlat_nodes)" ]; } lowlat_active() { # every state of CPU0 disabled @@ -184,11 +196,17 @@ lowlat_active() { # every state of CPU0 disabled for n in $(lowlat_nodes); do _any=1; [ "$(rd "$n")" = 1 ] || return 1; done [ -n "$_any" ] } +lowlat_mixed() { # some of CPU0's states disabled and some not (by hand, or by something else) + _on= _off= + for n in $(lowlat_nodes); do if [ "$(rd "$n")" = 1 ]; then _off=1; else _on=1; fi; done + [ -n "$_on" ] && [ -n "$_off" ] +} lowlat_set() { case ${1:-} in on) _v=1 ;; off) _v=0 ;; *) echo "usage: mu300-cpu lowlatency [on|off]" >&2; return 2 ;; esac lowlat_supported || { echo "mu300-cpu: this kernel has no cpuidle states for CPU0" >&2; return 1; } - lowlat_write "$_v" || { echo "mu300-cpu: CPU0's idle states could not be written" >&2; return 1; } - if [ "$_v" = 1 ]; then conf_set LOWLATENCY 1; else conf_del LOWLATENCY; fi + lowlat_write "$_v" || { echo "mu300-cpu: CPU0's idle states could not be written; they are as they were, nothing saved" >&2; return 1; } + if [ "$_v" = 1 ]; then conf_set LOWLATENCY 1; else conf_del LOWLATENCY; fi || + { lowlat_restore; echo "mu300-cpu: the setting could not be saved; CPU0's idle states are as they were" >&2; return 1; } say "low latency $1 (CPU0 idle states $([ "$_v" = 1 ] && echo off || echo on)), saved" } lowlat_apply() { # at boot: only an "on" is applied; off leaves the kernel's own settings alone @@ -283,9 +301,9 @@ status_json() { _saved=$(conf_get PROFILE) printf '{"ok":1,"profile":%s,"profiles":["saving","balanced","performance"],"throttling":%s,"thermal":%s,' \ "$(jstr "$_saved")" "$(throttling)" "$(thermal_json)" - printf '"cores":%s,"cores_off":%s,"lowlatency":{"saved":%s,"active":%s,"supported":%s},"policies":[' \ + printf '"cores":%s,"cores_off":%s,"lowlatency":{"saved":%s,"active":%s,"mixed":%s,"supported":%s},"policies":[' \ "$(cores_json)" "$(jstr "$(conf_get CORES_OFF)")" "$(conf_get LOWLATENCY)" \ - "$(lowlat_active && echo 1 || echo 0)" "$(lowlat_supported && echo 1 || echo 0)" + "$(lowlat_active && echo 1 || echo 0)" "$(lowlat_mixed && echo 1 || echo 0)" "$(lowlat_supported && echo 1 || echo 0)" _sep= for pol in $(policies); do _rel=$(rd "$pol/related_cpus") @@ -313,7 +331,7 @@ status_human() { _offl=; for _c in $(cpus); do [ "$(rd "$CPUDIR/cpu$_c/online")" = 0 ] && _offl="$_offl cpu$_c"; done echo "offline cores:${_offl:- none} (kept offline: $(conf_get CORES_OFF | sed 's/^$/none/'))" if lowlat_supported; then - echo "low latency: $([ "$(conf_get LOWLATENCY)" = 1 ] && echo on || echo off) (CPU0 idle states $(lowlat_active && echo off || echo on))" + echo "low latency: $([ "$(conf_get LOWLATENCY)" = 1 ] && echo on || echo off) (CPU0 idle states $(lowlat_active && echo off || { lowlat_mixed && echo 'partly off' || echo on; }))" fi } diff --git a/tests/test_cpu.py b/tests/test_cpu.py index 89e15d80..5ba71c55 100644 --- a/tests/test_cpu.py +++ b/tests/test_cpu.py @@ -4,6 +4,7 @@ A profile is a governor and a per-cluster scaling_min/max inside the hardware fr changes voltage and never touches the kernel thermal trips. These tests check the bounds (nothing is set outside cpuinfo_min_freq..cpuinfo_max_freq), the governor fallback, the ceiling file thermal-guard reads, and the JSON.""" import json +import os import unittest from helpers import ShellTest, BIN @@ -45,8 +46,8 @@ class CpuTest(ShellTest): self.policy('policy7', '7', [614400, 768000, 936000, 1105000, 1228800, 1378000, 1536000, 1703000, 1872000, 2041000, 2210000]) def cpu(self, shell, args, **env): - return self.sh(shell, f'"{CPU}" {args}', MU300_SYSROOT=self.root, MU300_RUN=self.run_dir, - MU300_CPU_CONF=self.conf, **env) + env = dict(dict(MU300_SYSROOT=self.root, MU300_RUN=self.run_dir, MU300_CPU_CONF=self.conf), **env) + return self.sh(shell, f'"{CPU}" {args}', **env) def read(self, policy, leaf): return (self.cpufreq / policy / leaf).read_text().strip() @@ -291,7 +292,7 @@ class CoresAndLatency(CpuTest): self.assertEqual(self.cpu(shell, 'lowlatency on').returncode, 0) self.assertEqual({n.read_text().strip() for n in nodes}, {'1'}) st = json.loads(self.cpu(shell, 'status --json').stdout) - self.assertEqual(st['lowlatency'], {'saved': 1, 'active': 1, 'supported': 1}) + self.assertEqual(st['lowlatency'], {'saved': 1, 'active': 1, 'mixed': 0, 'supported': 1}) for n in nodes: n.write_text('0\n') # a boot self.cpu(shell, 'apply') @@ -301,6 +302,37 @@ class CoresAndLatency(CpuTest): self.assertEqual(self.cpu(shell, 'lowlatency').stdout.strip(), 'off') self.assertNotEqual(self.cpu(shell, 'lowlatency maybe').returncode, 0) + @unittest.skipIf(os.geteuid() == 0, 'root writes a read-only file') + def test_a_state_the_kernel_does_not_take_puts_every_state_back(self): + # kanoqwq's cpu-idle (cc0ecce7): all or nothing, and nothing saved + for shell in self.each_shell(): + self.conf.unlink(missing_ok=True) + base = self.cores() + nodes = [base / f'cpu0/cpuidle/state{s}/disable' for s in range(3)] + nodes[1].write_text('1\n') # mixed before: state1 off by hand + st = json.loads(self.cpu(shell, 'status --json').stdout) + self.assertEqual((st['lowlatency']['active'], st['lowlatency']['mixed']), (0, 1)) + self.assertIn('partly off', self.cpu(shell, 'status').stdout) + nodes[2].chmod(0o444) + try: + r = self.cpu(shell, 'lowlatency on') + finally: + nodes[2].chmod(0o644) + self.assertNotEqual(r.returncode, 0) + self.assertIn('as they were', r.stderr) + self.assertEqual([n.read_text().strip() for n in nodes], ['0', '1', '0']) + self.assertFalse(self.conf.exists() and 'LOWLATENCY' in self.conf.read_text()) + + def test_a_setting_that_cannot_be_saved_is_undone(self): + for shell in self.each_shell(): + base = self.cores() + nodes = [base / f'cpu0/cpuidle/state{s}/disable' for s in range(3)] + blocker = self.tmp / 'not-a-dir' + blocker.write_text('') + r = self.cpu(shell, 'lowlatency on', MU300_CPU_CONF=blocker / 'cpu.conf') + self.assertNotEqual(r.returncode, 0) + self.assertEqual({n.read_text().strip() for n in nodes}, {'0'}) + def test_low_latency_without_cpuidle_is_refused(self): for shell in self.each_shell(): (self.root / 'sys/devices/system/cpu').mkdir(parents=True, exist_ok=True)