[Share Experiences] 编写脚本给 deepin 降降温,适用于 intel 处理器
Tofloor
poster avatar
陈陈菌
deepin
2022-10-25 03:14
Author

最近装着 DDE 的电脑烫到受不了,就想办法降下温度。众所周知在一些部分机型中 deepin 会默认把 CPU 主频拉满,所以导致高温,一眼看去很多工具只能调固定 CPU 频率,而不是根据传感器的温度阈值来动态调节。因为我自己是前端,索性就自己用 Node.js 写了个脚本来控制 CPU 主频。

其实原理也很简单,也就是对 /sys/ 下的一些文件的阈值进行读写。

对于 intel 处理器来说,

  • 最小值主频:/sys/devices/system/cpu/intel_pstate/min_perf_pct (最小阈值: 30)
  • 最大值主频:/sys/devices/system/cpu/intel_pstate/max_perf_pct (最大阈值: 100)
  • 是否关闭睿频:/sys/devices/system/cpu/intel_pstate/no_turbo (1:关闭睿频, 0:开启睿频)

以上文件读写皆需要 root 权限,当我们知道怎么控制频率后。接下来就是获取CPU的温度。(但前提是你的CPU有温度传感器)

对于我的电脑,有 WiFi 和 CPU 的温度传感器,其中涉及文件:

  • 温度阈值 位于 /sys/class/hwmon/hwmon0/temp1_input (文件具体位置每个电脑不同,可以遍历 /sys/class/hwmon/ 的每个文件夹下的 name,如果叫 "coretemp" 即属于CPU的传感器然后在计算 temp1_input 的阈值 / 1000 即可换算为摄氏度单位)
  • 频率阈值 位于 /sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq (这个文件的阈值 / 1000000 即可换算为常见的 * Ghz 频率单位)

接下来通过程序实现就很简单了:

#!/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'})
    process.stdout.write(process.platform === 'win32' ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H')
    if((temperature/1000)>=70){
        fs.writeFile(cpu.maxValueFile,'30',()=>{})
        console.log('温度>=70,将主频最大值降到 30%')
    }else if((temperature/1000)>=65){
        fs.writeFile(cpu.maxValueFile,'40',()=>{})
        console.log('温度>=65,将主频最大值降到 40%')
    }else if((temperature/1000)>=60){
        fs.writeFile(cpu.maxValueFile,'50',()=>{})
        console.log('温度>=60,将主频最大值降到 50%')
    }else if((temperature/1000)>=55){
        fs.writeFile(cpu.maxValueFile,'60',()=>{})
        console.log('温度>=55,将主频最大值降到 60%')
    }else if((temperature/1000)>=50){
        fs.writeFile(cpu.maxValueFile,'70',()=>{})
        console.log('温度>=50,将主频最大值降到 70%')
    }else if((temperature/1000)>=45){
        fs.writeFile(cpu.maxValueFile,'80',()=>{})
        console.log('温度>=45,将主频最大值降到 80%')
    }else {
        fs.writeFile(cpu.maxValueFile,'100',()=>{})
        console.log('当前温度不高,不降频率,全力拉满')
    }
    console.log(`实时CPU温度: ${temperature/1000}℃\n实时CPU频率: ${String(freq/1000000).slice(0,3)}Ghz`)
}
setInterval(_test, 300);

以上为 JS 的一些实现,当然你也可以选择用 shell 、Python 等语言来编写,本贴仅作为抛砖引玉作用。

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

Reply Favorite View the author
All Replies
black_white_bear
deepin
2022-10-25 03:21
#1

like

Reply View the author
jiong9
deepin
2022-10-25 04:00
#2

like

Reply View the author
liwl
deepin
2022-10-25 04:24
#3

like

Reply View the author
xuqi
deepin testing team
2022-10-25 04:56
#4
  • 有效果吗?like
Reply View the author
152******14
deepin
2022-10-25 04:57
#5
xuqi
  • 有效果吗?like

试一下

Reply View the author
陈陈菌
deepin
2022-10-25 05:54
#6
xuqi
  • 有效果吗?like

目前我的电脑已经把 CPU 内部温度压到 50~60 度以内了。

平时我是 70 度起步,也可以自己来微调参数优化

Reply View the author
liwl
deepin
2022-10-25 07:00
#7

采用降频的方式来降温,会不会压低性能?

Reply View the author
陈陈菌
deepin
2022-10-25 07:51
#8
liwl

采用降频的方式来降温,会不会压低性能?

会影响性能,不过更多是做均衡处理,

温度高就降频、温度低就满血运作。

Reply View the author
deepin-superuser
deepin
2022-10-25 16:58
#9
liwl

采用降频的方式来降温,会不会压低性能?

应该参考一下荣耀的 os turbo 平时降频 有高性能需求时候再动态提升

Reply View the author
BG7ZAG
deepin
2022-10-25 17:36
#10

kissing_heart

Reply View the author
晚秋(lateautumn)
Moderator
2022-10-25 17:42
#11

高手kissing_heart

Reply View the author
忆往
deepin
2022-10-25 18:19
#12

动手能力不错😁

Reply View the author
andktan
deepin
2022-10-25 22:32
#13

kissing_heart

Reply View the author
deepin_xiaoqian
deepin testing team
2022-10-26 02:00
#14

感谢分享like

Reply View the author