Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- LOCKFILE=/tmp/thermal_governor.lock
- # Try to acquire exclusive lock, fail if locked
- exec 200>"$LOCKFILE"
- flock -n 200 || { echo "Another instance is already running. Exiting."; exit 1; }
- # Configurable limits
- MIN_FREQ_MHZ=800
- MAX_FREQ_MHZ=3700
- STEP_SMALL=50
- STEP_MED=100
- STEP_LARGE=800
- INTERVAL=7 # seconds between checks
- # Helper: Set CPU frequency
- set_freq() {
- local freq="$1"
- sudo cpupower frequency-set -u "${freq}MHz" -d "${freq}MHz" > /dev/null
- echo "Set CPU frequency to ${freq}MHz"
- }
- # Helper: Read CPU temp in °C
- read_temp() {
- sensors 2>/dev/null | awk '/Package id 0:/ {gsub(/\+|°C/, "", $4); print int($4)}' | head -n1
- }
- # Helper: Get current CPU frequency (MHz, rounded down)
- get_current_freq() {
- awk '/cpu MHz/ {printf "%.0f\n", $4; exit}' /proc/cpuinfo
- }
- # Initialize
- echo "Starting thermal governor with range ${MIN_FREQ_MHZ}-${MAX_FREQ_MHZ} MHz"
- current_freq=$(get_current_freq)
- prev_freq=$current_freq
- set_freq "$current_freq"
- # Loop
- while true; do
- temp=$(read_temp)
- if [[ -z "$temp" ]]; then
- echo "Could not read temperature. Retrying in 1s."
- sleep 1
- continue
- fi
- echo "Temp: ${temp}°C | Current freq: ${current_freq}MHz"
- if (( temp >= 59 )); then
- (( current_freq -= STEP_LARGE ))
- echo "Temp ≥ 59°C → Dropping by ${STEP_LARGE}MHz"
- elif (( temp >= 55 )); then
- (( current_freq -= STEP_MED ))
- echo "Temp >= 55°C → Dropping by ${STEP_MED}MHz"
- elif (( temp >= 51 )); then
- (( current_freq -= STEP_SMALL ))
- echo "Temp > 51°C → Dropping by ${STEP_SMALL}MHz"
- elif (( temp < 46 )); then
- (( current_freq += STEP_SMALL ))
- echo "Temp < 46°C → Raising by ${STEP_SMALL}MHz"
- else
- echo "Temp stable (46–51°C). No change."
- fi
- # Clamp frequency to safe bounds
- if (( current_freq < MIN_FREQ_MHZ )); then current_freq=$MIN_FREQ_MHZ; fi
- if (( current_freq > MAX_FREQ_MHZ )); then current_freq=$MAX_FREQ_MHZ; fi
- # Only set frequency if it changed
- if (( current_freq != prev_freq )); then
- set_freq "$current_freq"
- prev_freq=$current_freq
- else
- echo "Frequency unchanged at ${current_freq}MHz. No command sent."
- fi
- sleep "$INTERVAL"
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement