Linux 系统设置cpu频率

source_code: https://github.com/emagii/cpufrequtils

cpufreq-set - A small tool which allows to modify cpufreq settings.(修改内存频率的工具)

cpufreq-set allows you to modify cpufreq settings without having to type e.g. “/sys/devices/system/cpu/cpu0/cpufreq/scaling_set_speed” all the time.(不需要直接操作sysfs相关节点即可调整cpufreq).

OPTIONS
-c --cpu
number of CPU where cpufreq settings shall be modified.(cpu核心号)

   -d --min <FREQ>
          new minimum CPU frequency the governor may select.(调控器可以选择新的最低 CPU 频率。)

   -u --max <FREQ>
          new maximum CPU frequency the governor may select.

   -g --governor <GOV>
          new cpufreq governor.(设定调控器类型。)

   -f --freq <FREQ>
          specific frequency to be set. Requires userspace governor to be available and loaded.(要设置的特定频率。要求用户空间调控器可用并已加载。)

   -r --related
          modify all hardware-related CPUs at the same time(同时修改所有与硬件相关的 CPU。)

   -h --help
          Prints out the help screen.

REMARKS(备注/特别说明)
Omitting the -c or --cpu argument is equivalent to setting it to zero.(省略 -c 或 --cpu 参数等同于将其设置为零。)

   The -f FREQ, --freq FREQ parameter cannot be combined with any other parameter except the -c CPU, --cpu CPU parameter.(-f 只能用在存在-c参数的场景。)

   FREQuencies  can be passed in Hz, kHz (default), MHz, GHz, or THz by postfixing the value with the wanted unit name, without any space (frequency in
   kHz =^ Hz * 0.001 =^ MHz * 1000 =^ GHz * 1000000).
   (FREQuencies 可以以 Hz、kHz(默认)、MHz、GHz 或 THz 为单位传递,方法是在值后加上所需的单位名称,没有任何空格(频率 kHz =^ Hz * 0.001 =^ MHz * 1000 =^ GHz * 1000000)。)

FILES(依赖文件)
/sys/devices/system/cpu/cpu*/cpufreq/
/proc/cpufreq (deprecated - 废弃)
/proc/sys/cpu/ (deprecated - 废弃)

// cpufreq-info -g
|- cpufreq_get_available_governors
	|- sysfs_get_available_governors(cpu)
		|- sysfs_read_file(cpu, "scaling_available_governors", linebuf, sizeof(linebuf))

由上述源码来看,cpuutils的本质是修改cpu sysfs导出的相关节点来调整cpu的频率。
可是cpu支持哪些governors是在哪里做限定的,scaling_available_governors看到仅支持performance模式。

因为sysfs是内核导出的节点,那就找一找内核源码??看了下v6.6的内核源码,发现仅支持两种模式。

// drivers/cpufreq/cpufreq.c
static unsigned int cpufreq_parse_policy(char *str_governor)
{
        if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN))
                return CPUFREQ_POLICY_PERFORMANCE;

        if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN))
                return CPUFREQ_POLICY_POWERSAVE;

        return CPUFREQ_POLICY_UNKNOWN;
}

除了performancepowersave两种模式,可是为什么其他文章说支持以下所有模式??

命令:sudo cpufreq-set -g 《模式》

powersave,是无论如何都只会保持最低频率的所谓”省电”模式;
userspace,是自定义频率时的模式,这个是当你设定特定频率时自动转变的;
ondemand,默认模式。一有cpu计算量的任务,就会立即达到最大频率运行,等执行完 毕就立即回到最低频率;
conservative,翻译成保守(中庸)模 式,会自动在频率上下限调整,ondemand的区别在于它会按需分配频率,而不是一味追求最高频率;
performance,顾名思义只注重效率,无论如何一直保持以最大频率运行。

// cpufreq-set -g
|- cpufreq_modify_policy_governor()
	|- verify_gov(new_gov, governor) // 对输入的governor字段做合法性判断
	|- sysfs_write_one_value
		|- sysfs_write_file // 将值写入到cpu WRITE_SCALING_GOVERNOR节点。
/* write access */

enum {
        WRITE_SCALING_MIN_FREQ,
        WRITE_SCALING_MAX_FREQ,
        WRITE_SCALING_GOVERNOR,
        WRITE_SCALING_SET_SPEED,
        MAX_WRITE_FILES
};

static const char *write_files[MAX_VALUE_FILES] = {
        [WRITE_SCALING_MIN_FREQ] = "scaling_min_freq",
        [WRITE_SCALING_MAX_FREQ] = "scaling_max_freq",
        [WRITE_SCALING_GOVERNOR] = "scaling_governor",
        [WRITE_SCALING_SET_SPEED] = "scaling_setspeed",
};

从代码来看,cpufreq-set仅仅是将输入的governor简单校验之后写入对应节点,真正的governor合法性判断由内核来做。

// cpufreq_conservative.c
// cpufreq_ondemand.c
// cpufreq_powersave.c
// cpufreq_userspace.c
// cpufreq_performance.c

这些代码都被编译成了单独的模块,当向scaling_governor节点写governor名时,对应的驱动会被加载,同时scaling_available_frequencies也会添加对应的governer

$ lsmod | grep cpu
cpufreq_ondemand       28672  0
cpufreq_conservative    20480  1
cpufreq_powersave      16384  0
acpi_cpufreq           45056  0
  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值