三 文章首页 实时留言 网络邻居 开往 虫洞
返回

在 Linux 控制 intel CPU 调度的方法

2022-08-22 08:56:27
分类: Linux 标签: 系统运维,node.js

在硬件驱动成功的情况下,拿到 root 权限,然后编辑以下文件。

参数阈值: 10-100,最大主频

vim /sys/devices/system/cpu/intel_pstate/max_perf_pct

参数阈值:10-100,最小主频

vim /sys/devices/system/cpu/intel_pstate/min_perf_pct

是否关闭CPU睿频,1 为不开启 0 则开启睿频

vim /sys/devices/system/cpu/intel_pstate/no_turbo

如果你动手能力强的话,可以用脚本来实现自动化。比如检查某硬件温度多少时调整多少对应的CPU频率阈值。

对于我之前的老电脑(Surface pro 7 intel i5-1035G4版),WiFi 和 CPU 的温度传感器其中涉及的接口文件参考:

温度阈值 位于 /sys/class/hwmon/hwmon0/temp1_input (文件具体位置每个电脑不同,可以遍历 /sys/class/hwmon/ 的每个文件夹下的 name,如果叫 "coretemp" 即属于CPU的温度传感器然后在计算 temp1_input 的阈值 / 1024 即可换算为摄氏度单位)

频率阈值 位于 /sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq (这个文件的阈值 / 1024000 即可换算为常见的 * Ghz 频率单位)

相关案例参考代码

我这里写了一个类似的场景,以下为 JS 的一些实现,当然你也可以选择用 shell 、Python 等语言来编写,本贴仅作为抛砖引玉作用。

#!/usr/bin/env node

const fs = require("fs")
const process = require("process")

function getTemperatureFileURL(){
    return new Promise((res)=>{
        let findUrl = "/sys/class/hwmon/"
        let hwmons = fs.readdirSync(findUrl,{encoding:"utf-8"})
        hwmons.forEach(dir=>{
            let name = fs.readFileSync(`${findUrl}${dir}/name`,{encoding:"utf-8"})
            let re = /coretemp/
            if(re.test(String(name))){
                res(`${findUrl}${dir}/temp1_input`)
            }
        })
    })
}
const cpu = {
    minValueFile: "/sys/devices/system/cpu/intel_pstate/min_perf_pct",
    maxValueFile: "/sys/devices/system/cpu/intel_pstate/max_perf_pct",
    noTurboFile: "/sys/devices/system/cpu/intel_pstate/no_turbo",
    freq: "/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq"
}
async function _test(){
    let temperature = fs.readFileSync(await getTemperatureFileURL(),{encoding:"utf-8"}) 
    let freq = fs.readFileSync(cpu.freq,{encoding:"utf-8"})
    if((temperature/1000)>=70){
        fs.writeFileSync(cpu.maxValueFile,"30")
        console.log("温度>=70,将主频最大值降到 30%")
    }else if((temperature/1000)>=65){
        fs.writeFileSync(cpu.maxValueFile,"40")
        console.log("温度>=65,将主频最大值降到 40%")
    }else if((temperature/1000)>=60){
        fs.writeFileSync(cpu.maxValueFile,"50")
        console.log("温度>=60,将主频最大值降到 50%")
    }else if((temperature/1000)>=55){
        fs.writeFileSync(cpu.maxValueFile,"60")
        console.log("温度>=55,将主频最大值降到 60%")
    }else if((temperature/1000)>=50){
        fs.writeFileSync(cpu.maxValueFile,"70")
        console.log("温度>=50,将主频最大值降到 70%")
    }else if((temperature/1000)>=45){
        fs.writeFileSync(cpu.maxValueFile,"80")
        console.log("温度>=45,将主频最大值降到 80%")
    }else {
        fs.writeFileSync(cpu.maxValueFile,"100",()=>{})
        console.log("当前温度不高,不降频率,全力拉满")
    }
    console.log(`实时CPU温度: ${temperature/1024}℃
实时CPU频率: ${String(freq/1024000).slice(0,3)}Ghz`)
}
setInterval(_test, 300);

对于守护进程,可以通过 PM2 来管理这个不停运作的 JS 进程。