如何在 Ubuntu 20.04 上将 Powersave 设置为默认性能模式?

如何在 Ubuntu 20.04 上将 Powersave 设置为默认性能模式?

我一直尝试让 CPU 调速器在启动时默认为“性能”而不是“省电”,但没有成功。有没有一种适用于 Ubuntu 20.04 的可靠方法?我试过了18.04 可接受的解决方案但不幸的是,当我重新启动并查看 indicator-cpufreq 时,Powersave 仍然被选中。

答案1

在Ubuntu21.10上: powerprofilesctl set performance

您可以将其添加为启动脚本。

更多信息请点击这里:适用于 Linux 的 Power-profiles-daemon 下载(deb、eopkg、rpm、xbps、xz、zst)

[编辑 - 收到一条添加其他参考资料的消息 - 请参阅包含更多信息的附加链接]

电源配置文件守护进程

不确定您是否希望我复制并粘贴 README.md 文件到power-profiles-daemon页面上。

答案2

如果您的处理器默认使用powersaveCPU 频率调节器,那么它可能正在使用 intel_pstate CPU 频率调节驱动程序。通过以下方式检查:

$ grep . /sys/devices/system/cpu/cpufreq/policy*/scaling_driver
/sys/devices/system/cpu/cpufreq/policy0/scaling_driver:intel_pstate
...
/sys/devices/system/cpu/cpufreq/policy5/scaling_driver:intel_pstate

Ubuntu 已将默认内核配置更改为默认调控器。以前是performance,现在是,如果不可用schedutil,则将改为。因此,一些较旧的答案不再适用。powersaveschedutil

方法 1 按需服务只需调用/lib/systemd/set-cpufreq,可以对其进行编辑以将调控器设置为,performance而不是其当前所做的事情。这个老答案表示在此重复和修改的方法:

doug@s18:~/config/lib/systemd$ diff -u set-cpufreq.original set-cpufreq
--- set-cpufreq.original        2021-03-10 14:07:32.036863542 -0800
+++ set-cpufreq 2021-03-10 14:10:05.313627963 -0800
@@ -10,6 +10,10 @@

 read governors < $AVAILABLE
 case $governors in
+        *performance*)
+                GOVERNOR="performance"
+                break
+                ;;
         *interactive*)
                 GOVERNOR="interactive"
                 break

修改完成后重启机器检查:

$ grep . /sys/devices/system/cpu/cpufreq/policy*/scaling_governor
/sys/devices/system/cpu/cpufreq/policy0/scaling_governor:performance
...
/sys/devices/system/cpu/cpufreq/policy5/scaling_governor:performance

并检查服务的状态,现在它应该已经停止了:

$ sudo systemctl status ondemand
[sudo] password for doug:
● ondemand.service - Set the CPU Frequency Scaling governor
     Loaded: loaded (/lib/systemd/system/ondemand.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Wed 2021-03-10 14:13:02 PST; 1min 18s ago
    Process: 667 ExecStart=/lib/systemd/set-cpufreq (code=exited, status=0/SUCCESS)
   Main PID: 667 (code=exited, status=0/SUCCESS)

Mar 10 14:12:57 s18 systemd[1]: Started Set the CPU Frequency Scaling governor.
Mar 10 14:13:02 s18 set-cpufreq[667]: Setting performance scheduler for all CPUs
Mar 10 14:13:02 s18 systemd[1]: ondemand.service: Succeeded.

如果在启动过程中稍后有什么东西覆盖了调节器设置,那么最好找出原因并删除它。但是,作为临时解决方法,请尝试在此服务中引入睡眠延迟(请注意,较旧的按需启动脚本用于延迟 1 分钟,然后更改调节器)。未经测试的示例:

doug@s18:~/config/lib/systemd$ diff -u set-cpufreq.original set-cpufreq.doug.test
--- set-cpufreq.original        2021-03-10 14:07:32.036863542 -0800
+++ set-cpufreq.doug.test       2021-03-10 16:24:13.088946203 -0800
@@ -10,6 +10,10 @@

 read governors < $AVAILABLE
 case $governors in
+        *performance*)
+                GOVERNOR="performance"
+                break
+                ;;
         *interactive*)
                 GOVERNOR="interactive"
                 break
@@ -34,6 +38,8 @@

 [ -n "${GOVERNOR:-}" ] || exit 0

+sleep 60
+
 echo "Setting $GOVERNOR scheduler for all CPUs"

 for CPUFREQ in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

方法 2 现在,如果您希望在禁用该服务的情况下运行:

$ sudo systemctl disable ondemand
Removed /etc/systemd/system/multi-user.target.wants/ondemand.service.

然后可以在 grub 命令行上设置首选项,方法是添加cpufreq.default_governor=performance已经存在的内容。/etc/default/grub在开始之前保留一份副本,以防以后想要恢复。此示例包括我的命令行中已有的其他内容。因此,就我而言,我更改了以下内容:

GRUB_CMDLINE_LINUX_DEFAULT="ipv6.disable=1 consoleblank=450 msr.allow_writes=on cpuidle.governor=teo intel_idle.states_off=4"

更改为:

GRUB_CMDLINE_LINUX_DEFAULT="ipv6.disable=1 consoleblank=450 cpufreq.default_governor=performance msr.allow_writes=on cpuidle.governor=teo intel_idle.states_off=4"

然后运行sudo update-grub并重新启动。然后检查:

doug@s18:~$ grep . /sys/devices/system/cpu/cpufreq/policy*/scaling_governor
/sys/devices/system/cpu/cpufreq/policy0/scaling_governor:performance
...
/sys/devices/system/cpu/cpufreq/policy5/scaling_governor:performance

注意:确保您的计算机可以在性能模式下运行而不会产生太多热量,因为无论使用什么热节流方法,在启动过程中都可能无法运行。

答案3

在我的 Ubuntu 20.04 上,我已完成以下操作,以便能够完全灵活地使用频率缩放调节器。默认的 CPU 频率缩放驱动程序是英特尔 pstate 驱动程序,但这仅允许按需和性能调节器(至少在我较旧的英特尔 CPU 上)。如果禁用英特尔 pstate 驱动程序,则默认的 CPU 频率缩放驱动程序将更改为支持以下调节器的 acpi 驱动程序:cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors conservative ondemand userspace powersave performance schedutil

要禁用英特尔 pstate 驱动程序,请将“intel_pstate=disable”添加到以下行:

GRUB_CMDLINE_LINUX_DEFAULT= 在 /etc/default/grub 中并运行“sudo update-grub”以使其在下次重启时生效。

重新启动机器。

完成后,通过发出命令禁用现有服务“ondemand.service”:sudo systemctl disable ondemand.service sudo systemctl stop ondemand.service

现在,创建一个由 root 拥有的新服务文件 /etc/systemd/system/mygovernor.service,其内容如下:

[Unit]
Description=Set CPU Frequency Scaling governor
ConditionVirtualization=no
ConditionPathExists=/sys/devices/system/cpu/online

[Service]
Type=idle
EnvironmentFile=/etc/default/mygovernor
ExecCondition=/bin/bash -xc '/bin/systemctl is-enabled --quiet ondemand.service && exit 1 || exit 0'
ExecStart=/etc/systemd/set-mygovernor ${GOVERNOR}
 
[Install]
WantedBy=multi-user.target

还创建一个文件 /etc/systemd/set-mygovernor,内容如下:


#! /bin/bash
# This script is called by script /etc/systemd/system/mygovernor.service
# It will set the CPU Frequency Scaling governor to the value passed in
# the first command line argument "$1"

set -eu

FIRSTCPU=$(cut -f1 -d- /sys/devices/system/cpu/online)
AVAILABLE=$(/bin/cat /sys/devices/system/cpu/cpu${FIRSTCPU}/cpufreq /scaling_available_governors)

# Check if the specified commandline governor ID is supported on this PC

GOVERNOR=""

for gov in ${AVAILABLE}; do
    if [[ "${gov}" == "${1}" ]]; then
        GOVERNOR="${gov}"
        break
    fi
done

if [ -z ${GOVERNOR} ]; then
       echo "Unknown governor =" \"${1}\"
       exit 1
fi

echo "Setting CPUFreq Scaling governor = \"$GOVERNOR\" for all CPUs"

for CPUFREQ in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
do
        [ -f "${CPUFREQ}" ] || continue
        echo -n "${GOVERNOR}" > ${CPUFREQ}
done

确保它由 root 拥有并可执行:sudo chown root:root /etc/systemd/set-mygovernor sudo chmod +x /etc/systemd/set-mygovernor

创建文件 /etc/default/mygovernor,内容如下:

# Environment file for systemd service /etc/systemd/system/mygovernor.service
# which set the cpufreq governor to be used by the system.  A list of supported
# governors may be found by the following command:
# "cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors"
# conservative ondemand userspace powersave performance schedutil

GOVERNOR=schedutil

现在您必须告诉 systemd 您已经添加了一个新服务:

sudo systemctl daemon-reload

并启用和启动新服务:

sudo systemctl enable mygovernor.service
sudo systemctl start mygovernor.service

要查看结果,请发出以下命令:

cpupower frequency-info

您可以轻松更换州长:

  1. 编辑 /etc/default/mygovernor 以选择其中一个受支持的调控器
  2. sudo systemctl 重启 mygovernor.service

答案4

关于此问题的许多答案都需要安装新应用程序cpufreq,例如cpupowerpowerprofilesctl。新应用程序包括不是必要的。

就像这样GitHub 帖子状态:

由于某种原因,Ubuntu 20.04 中的默认 CPU 调速器(即使在固定 PC 上)也是powersave

您可以performance使用以下命令启用 CPU 调节器:

$ sudo systemctl disable ondemand.service
$ echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

重新启动计算机,性能调节器应为默认调节器。

要测试默认调节器是否已更改为所需性能,请输入以下内容并观察调节器是否设置正确

$ cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 
performance

相关内容