#!/usr/bin/env bash # fan-state — one tab-separated line describing fans/thermals/throttle-guard, # for the daemon-sec.fans Omarchy bar widget + panel. Everything here is # readable without root; polled every few seconds so it must stay cheap. # # TCTL \t GPU_C \t FAN1_RPM \t FAN1_PCT \t FAN2_RPM \t FAN2_PCT \t DRIVER \t # CAP_MHZ \t HWMAX_MHZ \t PERSIST \t BOOST \t PROFILE \t AVG_MHZ \t MAX_MHZ \t # BUSY_PCT \t THROTTLE \t CURVE \t PKG_W # # TCTL = CPU Tctl °C (k10temp) # GPU_C = dGPU °C from the uniwill EC driver (empty if not loaded) # FAN1/2 = CPU/GPU fan rpm + commanded duty % (uniwill; GPU fan is dead) # DRIVER = 1 when the uniwill hwmon is present (fanfix fans loads it) # CAP/HWMAX = cpu0 scaling_max_freq / cpuinfo_max_freq in MHz # PERSIST = 1 when the fanfix tmpfiles cap survives reboots # BOOST = global cpufreq boost flag (must stay 1 for power-profiles-daemon) # AVG/MAX = instantaneous core clocks from /proc/cpuinfo # BUSY_PCT = CPU utilisation since the PREVIOUS poll (stat snapshot in # $XDG_RUNTIME_DIR — no sleep in the poll path) # THROTTLE = 1 when the EC clamp is active right now (fanfix's rule: # busy ≥ 25 % yet no core above 600 MHz) # CURVE = 1 when the fanfix-fan.service curve daemon is running # PKG_W = SoC package power in W (amdgpu) set -o pipefail export LC_ALL=C TRIP_MHZ=600 BUSY_MIN=25 CONF=/etc/tmpfiles.d/99-cpu-freq-cap.conf STATE_FILE=${XDG_RUNTIME_DIR:-/tmp}/daemon-sec.fans.stat hwmon_by_name() { local h; for h in /sys/class/hwmon/hwmon*; do [ "$(cat "$h/name" 2>/dev/null)" = "$1" ] && { echo "$h"; return 0; }; done; return 1; } rd() { cat "$1" 2>/dev/null || echo 0; } tctl=0 h=$(hwmon_by_name k10temp) && tctl=$(( $(rd "$h/temp1_input") / 1000 )) driver=0 gpu="" f1r="" f1p="" f2r="" f2p="" if h=$(hwmon_by_name uniwill); then driver=1 gpu=$(( $(rd "$h/temp2_input") / 1000 )) f1r=$(rd "$h/fan1_input"); f1p=$(( $(rd "$h/pwm1") * 100 / 255 )) f2r=$(rd "$h/fan2_input"); f2p=$(( $(rd "$h/pwm2") * 100 / 255 )) fi pkg="" h=$(hwmon_by_name amdgpu) && pkg=$(awk "BEGIN{printf \"%.1f\", $(rd "$h/power1_input")/1e6}") cap=$(( $(rd /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq) / 1000 )) hwmax=$(( $(rd /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq) / 1000 )) persist=0; [ -e "$CONF" ] && persist=1 boost=$(rd /sys/devices/system/cpu/cpufreq/boost) profile=$(powerprofilesctl get 2>/dev/null || echo "") read -r avg mx <<<"$(grep MHz /proc/cpuinfo \ | awk '{s+=$4; if($4>m)m=$4} END{if(NR)printf "%d %d", s/NR, m; else print "0 0"}')" busy=0 cur=$(head -1 /proc/stat) if [ -r "$STATE_FILE" ]; then busy=$(awk -v a="$(cat "$STATE_FILE")" -v b="$cur" 'BEGIN { na = split(a, pa, " "); nb = split(b, pb, " ") for (i = 2; i <= 9 && i <= na && i <= nb; i++) { ta += pa[i]; tb += pb[i] } ia = pa[5] + pa[6]; ib = pb[5] + pb[6] # idle + iowait dt = tb - ta printf "%d", (dt > 0) ? 100 - (ib - ia) * 100 / dt : 0 }') fi printf '%s\n' "$cur" > "$STATE_FILE" throttle=0 [ "$busy" -ge "$BUSY_MIN" ] && [ "$mx" -gt 0 ] && [ "$mx" -lt "$TRIP_MHZ" ] && throttle=1 curve=0 systemctl is-active --quiet fanfix-fan.service 2>/dev/null && curve=1 printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' \ "$tctl" "$gpu" "$f1r" "$f1p" "$f2r" "$f2p" "$driver" \ "$cap" "$hwmax" "$persist" "$boost" "$profile" "$avg" "$mx" \ "$busy" "$throttle" "$curve" "$pkg"