testsAndMisc/linux_configuration/fixes/lib/arch_cpu.sh
Krzysztof kuhy Rudnicki 4f2b0ccdf7 Dissolve linux_configuration/scripts/ and single_use/
Two levels of pure taxonomy carrying no information, and a 2-directory
cap leaves no room for them. Categories now sit directly under
linux_configuration/: features, fixes, utils, misc, fresh-install,
installers, diagnostics, gaming, boot_recovery, periodic_background, lib,
meta, tests. A script's own lib/ and tests/ are exempt from the cap, so
those stay put.

Depth violations 248 -> 139; roughly 40 of the remainder are inside
digital_wellbeing and hosts, deferred until the guard-lib repoint.

The moves are the easy half. What breaks is every path that counted
levels, and three classes of it had to be found by resolving paths rather
than reading diffs:

  - 52 relative `source` lines for lib/common.sh and lib/android.sh. The
    correct prefix differs per file: 40 were ../../ and 12 were ../../../,
    so a blanket replace would have fixed one set and broken the other.
  - REPO_ROOT in 7 fixes/lib/tests harnesses and setup_syncyomi_guard.sh,
    each walking up four levels from a directory now two levels shallower.
    This caused a real regression -- the fixes suite went red with
    "log_warn: command not found" -- caught by running it and comparing
    against the pre-move baseline worktree.
  - conftest.py's sys.path entries, which took the pytest gate down with
    14 collection errors, and the `# shellcheck source=` directives, which
    took shellcheck down with SC1091.

16 symlinks in $HOME broke, including the entire i3blocks bar. All
repointed, and cpu_monitor/memory/disk/time/volume were run to confirm
they emit real output rather than merely resolving.

Verified: 1798 tests pass at 100% coverage, 8 of 9 lib/tests suites pass
(the ninth needs the coverage jail, identically at baseline), pre-commit
is clean, and the two untracked secret configs are still ignored after
their gitignore paths changed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJrqKERAo5QUeEHWS5Asgp
2026-08-23 18:53:30 +02:00

129 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
# CPU governor and IO scheduler tweaks.
#
# Sourced by optimize_arch_desktop.sh; split out to keep it under the 250-line cap.
# Sourced rather than run, so it inherits the caller's strict mode and
# the helper functions and variables defined above the source line.
# ===================================================================
# 1. CPU Governor → performance
# ===================================================================
tweak_cpu_governor() {
local gov_files
# CPU_SYSFS_ROOT and the two write targets below default to the real
# locations and are overridden only by the test harness, which runs
# un-jailed in ci_mirror.sh and in CI.
gov_files=$(find "${CPU_SYSFS_ROOT:-/sys/devices/system/cpu}" -maxdepth 3 -name scaling_governor 2>/dev/null || true)
if [[ -z $gov_files ]]; then
log_warn "No CPU governor sysfs files found — skipping."
return 0
fi
# Check current state
local all_performance=true
local f
for f in $gov_files; do
if [[ $(cat "$f") != "performance" ]]; then
all_performance=false
break
fi
done
if [[ $all_performance == "true" ]]; then
log_ok "All CPU cores already on 'performance' governor — skipping."
return 0
fi
for f in $gov_files; do
echo "performance" >"$f"
done
# Make it persistent via a sysctl-style drop-in using udev rule
local udev_rule="${UDEV_RULES_DIR:-/etc/udev/rules.d}/60-cpu-governor-performance.rules"
if [[ ! -f $udev_rule ]]; then
cat >"$udev_rule" <<'UDEVEOF'
# Set CPU governor to performance on all cores at boot
SUBSYSTEM=="module", DEVPATH=="*/cpu/*", ATTR{scaling_governor}=="*", ATTR{scaling_governor}="performance"
UDEVEOF
fi
# Also install cpupower hook as a more reliable persistence method
local cpupower_conf="${CPUPOWER_CONF_FILE:-/etc/default/cpupower}"
if has_cmd cpupower; then
if [[ ! -f $cpupower_conf ]] || ! grep -q "^governor='performance'" "$cpupower_conf" 2>/dev/null; then
mkdir -p "$(dirname "$cpupower_conf")"
cat >"$cpupower_conf" <<'CPUEOF'
# /etc/default/cpupower — managed by optimize_arch_desktop.sh
governor='performance'
CPUEOF
systemctl enable cpupower.service 2>/dev/null || true
fi
fi
return 0
}
# ===================================================================
# 2. I/O Scheduler per drive type
# ===================================================================
tweak_io_scheduler() {
local changed=false
local block_dev
local block_root="${BLOCK_SYSFS_ROOT:-/sys/block}"
for block_dev in "$block_root"/sd* "$block_root"/nvme* "$block_root"/vd*; do
[[ -d $block_dev ]] || continue
local sched_file="$block_dev/queue/scheduler"
[[ -f $sched_file ]] || continue
local dev_name
dev_name=$(basename "$block_dev")
local rotational
rotational=$(cat "$block_dev/queue/rotational" 2>/dev/null || echo 1)
local current
current=$(sed 's/.*\[\(.*\)\].*/\1/' "$sched_file" 2>/dev/null || true)
local target
if [[ $dev_name == nvme* ]]; then
target="none"
elif [[ $rotational -eq 0 ]]; then
target="mq-deadline"
else
target="bfq"
fi
if [[ $current == "$target" ]]; then
log_ok "$dev_name: already using '$target' scheduler."
continue
fi
echo "$target" >"$sched_file" 2>/dev/null || true
log_info "$dev_name: scheduler changed from '$current' to '$target'."
changed=true
done
# Persist via udev rule
local udev_rule="${UDEV_RULES_DIR:-/etc/udev/rules.d}/60-io-scheduler.rules"
if [[ ! -f $udev_rule ]]; then
cat >"$udev_rule" <<'IOEOF'
# NVMe: no scheduler (multi-queue hardware handles it)
ACTION=="add|change", KERNEL=="nvme[0-9]*", ATTR{queue/scheduler}="none"
# SATA SSD: mq-deadline (low latency)
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="mq-deadline"
# HDD: BFQ (fair bandwidth allocation)
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="bfq"
IOEOF
fi
if [[ $changed == "false" ]]; then
log_ok "All I/O schedulers already optimal."
fi
return 0
}
# ===================================================================
# Apply all tweaks
# ===================================================================