在 18.04 中将 CPU 调控器设置为性能

Fal*_*alc 47 cpu governor 18.04

我试图在 18.04 中将 CPU 调控器设置为性能,问题是它永远不会在重新启动后幸存下来。

我已经尝试在这个问题的答案中 遵循这些步骤如何将“性能”而不是“Powersave”设置为默认值?但是当我重新启动计算机时,CPU 调节器会切换回节电模式。

我也试过创建一个/etc/rc.local用以下行调用的文件:

cpupower frequency-set --governor performance
Run Code Online (Sandbox Code Playgroud)

这也无法在重新启动后幸存下来。

我怎样才能做到这一点?

n1g*_*g3r 36

sudo apt-get install cpufrequtils
echo 'GOVERNOR="performance"' | sudo tee /etc/default/cpufrequtils
sudo systemctl disable ondemand
Run Code Online (Sandbox Code Playgroud)

  • 实际上,只需最后一个命令就足够了。在 `sudo systemctl disable on demand` 并重启后:`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor` 显示 `performance`。 (9认同)

Win*_*nix 8

简答

输入/etc/rc.local这些命令:

sleep 120
cpupower frequency-set --governor performance
Run Code Online (Sandbox Code Playgroud)

开机1分钟后自动切换到Powersave

无论出于何种原因,我的 Skylake Intel CPU 总是以性能模式启动,然后在 1 分钟标记处自动切换到节能模式。

如果您在启动时将模式设置为性能,它将在大约 1 分钟的正常运行时间标记处被覆盖到节能模式。

在下面的 GIF 中,启动时 3000+ MHz 的 CPU 速度出现在顶部附近。运行时间出现在底部附近。当正常运行时间达到大约 1 分钟时,您会看到 CPU MHz 下降。:

CPU 省电 1 分钟 2.gif


监视调用的确切第二个 Powersave 的程序

/usr/local/bin/watch-gov.sh以下位置创建此脚本:

#! /bin/bash

# NAME: watch-gov.sh
# PATH: /usr/local/bin
# DESC: Set governnor to performance and watch for change
#       Ask Ubuntu question: https://askubuntu.com/questions/1021748/set-cpu-governor-to-performance-in-18-04/1084727#1084727
# CALL: called from `/etc/rc.local`
# DATE: Created Oct 18, 2018.

echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
last_gov=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
Uptime=$(uptime)
echo "watch-gov.sh: Set to performance at $Uptime " > /tmp/watch-gov.log

for ((i=0; i<300; i++)) ; do
    curr_gov=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
    if [ $last_gov != $curr_gov ] ; then
        last_gov=$curr_gov
        Uptime=$(uptime)
        echo "watch-gov.sh: Current governor: $last_gov Uptime: $Uptime" >> \
            /tmp/watch-gov.log
    fi
    sleep 1
done
Run Code Online (Sandbox Code Playgroud)

/etc/rc.localexit 0命令之前调用脚本(在下面详细解释)。

登录后一分钟查看输出:

$ cat /tmp/watch-gov.log
watch-gov.sh: Set to performance at  17:50:09 up 0 min,  0 users,  load average: 0.00, 0.00, 0.00 
watch-gov.sh: Current governor: powersave Uptime:  17:51:09 up 1 min,  1 user,  load average: 1.89, 0.62, 0.22
Run Code Online (Sandbox Code Playgroud)

从这个答案中确认,这1 分钟的powersave调控力是由 控制的/etc/init.d/ondemand


设置性能模式前休眠 120 秒

保持性能模式的最简单方法是/etc/rc.local在包含以下内容的最后一行之前编辑和插入这些行exit 0

sleep 120 # Give CPU startup routines time to settle.
echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
Run Code Online (Sandbox Code Playgroud)

保存文件并重新启动。

如果在exit 0它之后插入新行,它将永远不会被执行。

/etc/rc.local在 18.04 中进行设置,请参阅:如何/etc/rc.local使用 Systemd启用


注意事项

您的机器运行温度可能会升高 10 到 15 摄氏度。

您可能需要删除其他更改 CPU 频率的程序,如果它们覆盖了您的性能设置 /etc/rc.local


Dou*_*ies 6

默认的 Ubuntu 内核配置是这样的,即在启动期间将使用性能 CPU 频率缩放调节器。内核配置文件(/boot/config-4.15.0-36-generic在本例中为 )的相关部分是:

#
# CPU Frequency scaling
#
CONFIG_CPU_FREQ=y
CONFIG_CPU_FREQ_GOV_ATTR_SET=y
CONFIG_CPU_FREQ_GOV_COMMON=y
CONFIG_CPU_FREQ_STAT=y
CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE=y
# CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE is not set
# CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE is not set
# CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND is not set
# CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE is not set
# CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL is not set
CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
CONFIG_CPU_FREQ_GOV_POWERSAVE=y
CONFIG_CPU_FREQ_GOV_USERSPACE=y
CONFIG_CPU_FREQ_GOV_ONDEMAND=y
CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
CONFIG_CPU_FREQ_GOV_SCHEDUTIL=y

#
# CPU frequency scaling drivers
#
CONFIG_X86_INTEL_PSTATE=y
CONFIG_X86_PCC_CPUFREQ=y
CONFIG_X86_ACPI_CPUFREQ=y
CONFIG_X86_ACPI_CPUFREQ_CPB=y
CONFIG_X86_POWERNOW_K8=y
CONFIG_X86_AMD_FREQ_SENSITIVITY=m
CONFIG_X86_SPEEDSTEP_CENTRINO=y
CONFIG_X86_P4_CLOCKMOD=m
Run Code Online (Sandbox Code Playgroud)

但默认情况下,在启动过程中ondemand也会执行该服务。它休眠 1 分钟,然后根据可用性将缩放调节器更改为interactiveondemandpowersave。反过来,可用性取决于您使用的 CPU 频率缩放驱动程序。代码是(在多个位置,搜索ondemand):

#! /bin/sh
### BEGIN INIT INFO
# Provides:          ondemand
# Required-Start:    $remote_fs $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Set the CPU Frequency Scaling governor to "ondemand"
### END INIT INFO

# Don't run if we're going to start an Android LXC container:
[ ! -f /etc/init/lxc-android-config.conf ] || exit 0

PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

AVAILABLE="/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors"
DOWN_FACTOR="/sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor"

case "$1" in
    start)
        start-stop-daemon --start --background --exec /etc/init.d/ondemand -- background
        ;;
    background)
        sleep 60 # probably enough time for desktop login

        [ -f $AVAILABLE ] || exit 0
        read governors < $AVAILABLE
        case $governors in
                *interactive*)
                        GOVERNOR="interactive"
                        break
                        ;;
                *ondemand*)
                        GOVERNOR="ondemand"
                        case $(uname -m) in
                                ppc64*)
                                        SAMPLING=100
                                ;;
                        esac
                        break
                        ;;
                *powersave*)
                        GOVERNOR="powersave"
                        break
                        ;;
                *)
                        exit 0
                        ;;
        esac

        for CPUFREQ in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
        do
                [ -f $CPUFREQ ] || continue
                echo -n $GOVERNOR > $CPUFREQ
        done
        if [ -n "$SAMPLING" ] && [ -f $DOWN_FACTOR ]; then
                echo -n $SAMPLING > $DOWN_FACTOR
        fi
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop)
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac
Run Code Online (Sandbox Code Playgroud)

为什么它被称为“按需”,但它设置了其他调控器(例如,使用 intel_pstate 驱动程序它会设置 powersave 调控器)?因为这个工具早于 intel_pstate 驱动程序,回到了那个时代,到目前为止,主要的频率缩放驱动程序是 acpi-cpufreq 驱动程序,而“ondemand”是首选的 Ubuntu 默认调控器。

因此,启动并保持使用性能 CPU 频率调节调节器的一种方法是禁用远离它的服务(这也在另一个答案中提到):

前:

~$ systemctl list-units --all --type=service | grep ondemand
  ondemand.service                                      loaded    inactive dead    Set the CPU Frequency Scaling governor
~$ cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
ondemand
ondemand
Run Code Online (Sandbox Code Playgroud)

禁用服务:

~$ sudo systemctl disable ondemand
Removed /etc/systemd/system/multi-user.target.wants/ondemand.service.
Run Code Online (Sandbox Code Playgroud)

重新启动,然后再次检查(确保在重新启动后等待一分钟):

doug@s17:~$ systemctl list-units --all --type=service | grep ondemand
doug@s17:~$
doug@s17:~$ cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
performance
performance
Run Code Online (Sandbox Code Playgroud)

注意:此答案中的示例来自使用 acpi-cpufreq CPU 频率缩放驱动程序的计算机。如果您使用的是 intel_pstate 驱动程序,没有按需调节器,则默认情况下将使用节能调节器。

预期问题:为什么我的 CPU 频率会缩放,即使在使用性能调控器时也是如此?

答:现代处理器会缩放 CPU 频率,即使在性能模式下也是如此,并且作为它们进入的空闲状态深度的函数。如果您真的想锁定 CPU 频率,则禁用所有大于 0 的空闲状态。但是,请注意,这将消耗大量的电量。

就我个人而言,正如在另一个答案中提到的,我使用性能调节器或节能调节器作为我正在做的任何工作的功能。我的脚本有点不同:

$ cat set_cpu_performance
#! /bin/bash
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

for file in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo "performance" > $file; done

cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
Run Code Online (Sandbox Code Playgroud)

和:

$ cat set_cpu_powersave
#! /bin/bash
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

for file in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo "powersave" > $file; done

cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
Run Code Online (Sandbox Code Playgroud)

示例用法(在使用 intel_pstate 驱动程序的计算机上):

$ sudo ./set_cpu_performance
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
performance
performance
performance
performance
performance
performance
performance
performance
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

126493 次

最近记录:

5 年 前