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;
这样可以防止频率迅速抖动。
Linux中一种根据外界环境温度调整CPU最大温度的方法_第1张图片

4python实现代码

#! /usr/bin/python3
# -*- coding: utf-8 -*-

import os
import time
import sys

CPUFREQ_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 gracefully
    if ENABLE != "true":
        print("\033[31m This script have been diasbled. \033[0m")
        sys.exit(1)

    # check root
    if os.getuid() != 0:
        print(
            "\033[31m This script requires root privileges, trying to use sudo \033[0m")
        sys.exit(1)

    # check file exist
    if 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 1
    return 0


def 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)/1000


def 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_GAP
    for num in range(HOLE_GAP + 1):
        if (average_temp_int - BASE_TEMP)/GAP_TEMP < num + 1:
            ret_freq = MAX_SPEED - STEP_SPEED * num
            break
            pass
        pass

    if ret_freq != current_freq:
        change_flag = 1
    else:
        change_flag = 0
    return ret_freq, change_flag


def get_dec_freq(
        average_temp_int,
        previous_average_temp_int,
        current_freq):
    ''' get dec freq '''
    '''return : current_freq,change_flag'''
    ret_freq = MAX_SPEED
    for 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)
            break
            pass
        pass

    if ret_freq != current_freq:
        change_flag = 1
    else:
        change_flag = 0
    return ret_freq, change_flag


def main_loop():
    ''' here is main func '''
    current_freq = MAX_SPEED
    # init a list count = TEMP_COUNT
    history_temp = [getCPUtemperature()]*TEMP_COUNT
    average_temp = 0
    previous_average_temp = 0
    sum_history_temp = history_temp[0]*TEMP_COUNT
    change_flag = 0
    print("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_temp
            average_temp = sum_history_temp / TEMP_COUNT
            # judge temp and change maxfreq
            average_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)
                pass
            elif 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)
                pass
            else:
                pass
            previous_average_temp = average_temp
            '''change max freq'''
            if change_flag != 0:
                change_flag = 0
                set_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)