Linux中一种根据外界环境温度调整CPU最大温度的方法

2023-11-21 21:40

本文主要是介绍Linux中一种根据外界环境温度调整CPU最大温度的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 基本思路

a) 检测环境温度;
b) 如果CPU温度升高,为了降低发热量,需要降低CPU最大频率;
c) 如果CPU温度降低,为了提高性能,可以提高CPU最大频率。

2本次测试使用的环境如下:

  1. 硬件 orangepi4
  2. 处理器rk3399 ,4×a53(最大频率1.4GHz)+ 2×a72(最大频率1.8GHz)
  3. 操作系统:Ubuntu18.04 server
  4. 使用软件: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最大温度的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/405283

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

linux hostname设置全过程

《linuxhostname设置全过程》:本文主要介绍linuxhostname设置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录查询hostname设置步骤其它相关点hostid/etc/hostsEDChina编程A工具license破解注意事项总结以RHE

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方