近期购买了CM3588,因为使用的全被动散热,在持续30分钟的CPU满载后会出现温度超过70度的情况。为了保障CPU能应对极端负载环境,特此做了一个自动调整CPU主频的脚本。确保CPU温度持续维持在合理的温度范围内,并且尽可能释放最大性能。
脚本默认在70度会开始降频,60度会开始恢复,每10秒循环一次。
BASH脚本
- #!/bin/bash
- # 温度阈值
- TEMP_THRESHOLD_HIGH=70 # 超过此温度时降低频率
- TEMP_THRESHOLD_LOW=60 # 低于此温度时提高频率
- # 0-3核和4-7核的CPU最大频率路径
- CPU0_3_MAX_FREQ_PATH="/sys/devices/system/cpu/cpu[0-3]/cpufreq/scaling_max_freq"
- CPU4_7_MAX_FREQ_PATH="/sys/devices/system/cpu/cpu[4-7]/cpufreq/scaling_max_freq"
- # 0-3核和4-7核的频率列表
- AVAILABLE_FREQS_0_3=(408000 600000 816000 1008000 1200000 1416000 1608000 1800000)
- AVAILABLE_FREQS_4_7=(408000 600000 816000 1008000 1200000 1416000 1608000 1800000 2016000 2208000 2256000)
- # CPU温度文件路径
- TEMP_PATH="/sys/class/thermal/thermal_zone0/temp"
- # 获取当前最大频率的索引
- get_current_freq_index() {
- local freq_path=$1
- local freqs=("${!2}")
- current_freq=$(cat $freq_path)
- for i in "${!freqs[@]}"; do
- if [ "${freqs[$i]}" -eq "$current_freq" ]; then
- echo $i
- return
- fi
- done
- echo -1
- }
- # 设置CPU的最大频率
- set_max_freq() {
- local paths=$1
- local freq=$2
- for path in $paths; do
- echo "$freq" > "$path"
- done
- }
- # 主逻辑循环
- while true; do
- # 读取当前温度
- temp=$(cat "$TEMP_PATH")
- temp=$((temp / 1000)) # 转换为摄氏度
- # 调整0-3核的频率
- current_index_0_3=$(get_current_freq_index "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq" AVAILABLE_FREQS_0_3[@])
- if [ "$temp" -ge "$TEMP_THRESHOLD_HIGH" ] && [ "$current_index_0_3" -gt 0 ]; then
- new_index=$((current_index_0_3 - 1))
- set_max_freq "$CPU0_3_MAX_FREQ_PATH" "${AVAILABLE_FREQS_0_3[$new_index]}"
- echo "温度: $temp°C,高于${TEMP_THRESHOLD_HIGH}°C,0-3核降至频率: ${AVAILABLE_FREQS_0_3[$new_index]}"
- elif [ "$temp" -le "$TEMP_THRESHOLD_LOW" ] && [ "$current_index_0_3" -lt $((${#AVAILABLE_FREQS_0_3[@]} - 1)) ]; then
- new_index=$((current_index_0_3 + 1))
- set_max_freq "$CPU0_3_MAX_FREQ_PATH" "${AVAILABLE_FREQS_0_3[$new_index]}"
- echo "温度: $temp°C,低于${TEMP_THRESHOLD_LOW}°C,0-3核升至频率: ${AVAILABLE_FREQS_0_3[$new_index]}"
- else
- echo "温度: $temp°C,0-3核频率保持不变: ${AVAILABLE_FREQS_0_3[$current_index_0_3]}"
- fi
- # 调整4-7核的频率
- current_index_4_7=$(get_current_freq_index "/sys/devices/system/cpu/cpu4/cpufreq/scaling_max_freq" AVAILABLE_FREQS_4_7[@])
- if [ "$temp" -ge "$TEMP_THRESHOLD_HIGH" ] && [ "$current_index_4_7" -gt 0 ]; then
- new_index=$((current_index_4_7 - 1))
- set_max_freq "$CPU4_7_MAX_FREQ_PATH" "${AVAILABLE_FREQS_4_7[$new_index]}"
- echo "温度: $temp°C,高于${TEMP_THRESHOLD_HIGH}°C,4-7核降至频率: ${AVAILABLE_FREQS_4_7[$new_index]}"
- elif [ "$temp" -le "$TEMP_THRESHOLD_LOW" ] && [ "$current_index_4_7" -lt $((${#AVAILABLE_FREQS_4_7[@]} - 1)) ]; then
- new_index=$((current_index_4_7 + 1))
- set_max_freq "$CPU4_7_MAX_FREQ_PATH" "${AVAILABLE_FREQS_4_7[$new_index]}"
- echo "温度: $temp°C,低于${TEMP_THRESHOLD_LOW}°C,4-7核升至频率: ${AVAILABLE_FREQS_4_7[$new_index]}"
- else
- echo "温度: $temp°C,4-7核频率保持不变: ${AVAILABLE_FREQS_4_7[$current_index_4_7]}"
- fi
- sleep 10
- done