本文主要是介绍Linux中一种根据外界环境温度调整CPU最大温度的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 基本思路
a) 检测环境温度;
b) 如果CPU温度升高,为了降低发热量,需要降低CPU最大频率;
c) 如果CPU温度降低,为了提高性能,可以提高CPU最大频率。
2本次测试使用的环境如下:
- 硬件 orangepi4
- 处理器rk3399 ,4×a53(最大频率1.4GHz)+ 2×a72(最大频率1.8GHz)
- 操作系统:Ubuntu18.04 server
- 使用软件:python3
3环境温度与频率对照表
期望温度与CPU温度的关系如图:
例如CPU温度从49℃提高到50℃,希望CPU最大频率变为1600MHz;
CPU温度变为从50℃降低到49℃,希望CPU频率变为1600MHz;
这样可以防止频率迅速抖动。
4python实现代码
#! /usr/bin/python3
# -*- coding: utf-8 -*-import os
import time
import sysCPUFREQ_SET = "/usr/bin/cpufreq-set"
CPUFREQ_INFO = "/usr/bin/cpufreq-info"
CPUFREQ_OPTIONS = ""
CPUFREQ_CMD = ""ENABLE = "true" # "true" or "false"
# GOVERNOR="interactive1"
MIN_SPEED = 400000 # 最低频率
MAX_SPEED = 1850000 # 最高频率
STEP_SPEED = 200000 # 频率跨度
SAMPLE_TIME = 3 # 采样周期 sec
TEMP_COUNT = 10 # 采样的温度数量
BASE_TEMP = 48 # 基础温度
GAP_TEMP = 2 # 每个频率温度跨度
HOLE_GAP = int((MAX_SPEED-MIN_SPEED)/STEP_SPEED)
INFO = 'info="/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors";'def pre_check():# if not enabled then exit gracefullyif ENABLE != "true":print("\033[31m This script have been diasbled. \033[0m")sys.exit(1)# check rootif os.getuid() != 0:print("\033[31m This script requires root privileges, trying to use sudo \033[0m")sys.exit(1)# check file existif not os.path.exists(CPUFREQ_SET):print(CPUFREQ_SET + "don't exist")sys.exit(5)if not os.path.exists(CPUFREQ_INFO):print(CPUFREQ_INFO + "don't exist")sys.exit(5)def getCPUS():"""get cpus"""res = os.popen(r"echo $(cat /proc/stat|sed -ne 's/^cpu\([[:digit:]]\+\).*/\1/p')").readline()return res.replace("\n", "").split(" ")cpu_list = getCPUS()def set_max_freq(max_freq):"""change freq"""for cpu in cpu_list:# print(CPUFREQ_SET + " --cpu " + cpu + " --max " + str(max_freq) + r" 2>&1 > /dev/null")os.system(CPUFREQ_SET + " --cpu " + cpu +" --max " + str(max_freq) + r" 2>&1 > /dev/null")def check_cpu_max_freq(set_freq_value):'''check cpu max freq'''for cpu in cpu_list:res = os.popen(CPUFREQ_INFO + r" --cpu " + cpu + " -p ").readline()_max_freq = int(res.replace("\n", "").split(" ")[1])if set_freq_value - _max_freq >= STEP_SPEED or set_freq_value - _max_freq <= -STEP_SPEED:set_max_freq(set_freq_value)return 1return 0def getCPUtemperature():''' Return CPU temperature as a int '''res = os.popen('cat /sys/class/thermal/thermal_zone0/temp').readline()temp_str = (res.replace("temp=", "").replace("'C\n", ""))return int(temp_str)/1000def get_inc_freq(average_temp_int,previous_average_temp_int,current_freq):''' get inc freq ''''''return : current_freq,change_flag'''ret_freq = MAX_SPEED - STEP_SPEED * HOLE_GAPfor num in range(HOLE_GAP + 1):if (average_temp_int - BASE_TEMP)/GAP_TEMP < num + 1:ret_freq = MAX_SPEED - STEP_SPEED * numbreakpasspassif ret_freq != current_freq:change_flag = 1else:change_flag = 0return ret_freq, change_flagdef get_dec_freq(average_temp_int,previous_average_temp_int,current_freq):''' get dec freq ''''''return : current_freq,change_flag'''ret_freq = MAX_SPEEDfor num in range(HOLE_GAP + 1):if (average_temp_int - BASE_TEMP)/GAP_TEMP > HOLE_GAP - num - 1:ret_freq = MAX_SPEED - STEP_SPEED * (HOLE_GAP - num)breakpasspassif ret_freq != current_freq:change_flag = 1else:change_flag = 0return ret_freq, change_flagdef main_loop():''' here is main func '''current_freq = MAX_SPEED# init a list count = TEMP_COUNThistory_temp = [getCPUtemperature()]*TEMP_COUNTaverage_temp = 0previous_average_temp = 0sum_history_temp = history_temp[0]*TEMP_COUNTchange_flag = 0print("start change cpu max freq")while True:check_cpu_max_freq(current_freq)for i in range(TEMP_COUNT):time.sleep(SAMPLE_TIME)cur_temp = getCPUtemperature()sum_history_temp = sum_history_temp + cur_temp - history_temp[i]history_temp[i] = cur_tempaverage_temp = sum_history_temp / TEMP_COUNT# judge temp and change maxfreqaverage_temp_int = round(average_temp)previous_average_temp_int = round(previous_average_temp)if average_temp_int > previous_average_temp_int:"""# increase temperature"""current_freq, change_flag = get_inc_freq(average_temp_int,previous_average_temp_int,current_freq)passelif average_temp_int < previous_average_temp_int:"""# decrease temperature"""current_freq, change_flag = get_dec_freq(average_temp_int,previous_average_temp_int,current_freq)passelse:passprevious_average_temp = average_temp'''change max freq'''if change_flag != 0:change_flag = 0set_max_freq(current_freq)# set_max_freq(MIN_SPEED)print("\r%6.3f \t%10d" % (average_temp, current_freq), end="")if __name__ == "__main__":main_loop()pass
这篇关于Linux中一种根据外界环境温度调整CPU最大温度的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!