Ubuntu uniformly sets high-performance mode for multiple CPUs


reference link

1. Problem description

  The CPU settings found on the Internet in high-performance mode can only be set for CPU0a single CPU. The following is a unified setting of the working mode for multi-core CPUs.

2. Software installation and setting

  Execute the following command sudo apt-get install indicator-cpufreqand restart the computer. At this time, the following image will appear in the upper right corner of the interface icon. Click the icon and set it to performance mode.

  • performance : High-performance mode, maximizes CPU performance and sets the CPU frequency to the highest value
  • powersave : Energy saving mode, maximize energy saving, set the CPU frequency to the lowest value
  • ondemand: Dynamically adjust frequency according to CPU usage, suitable for general desktop applications
  • conservative: More conservative than ondemand, suitable for mobile devices in power saving mode

3. Check the status of each CPU

  Executing the following command cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor, you can see that each CPU is in performance mode.
Insert image description here

4. The default high performance when booting

  The graphical operation of the above steps is convenient and fast, but it can only guarantee the current CPU status. After restarting the computer, the default settings will be restored. The following are the steps to set the default high performance at startup:

4.1 Install cpufrequtils

  Execute sudo apt-get install cpufrequtilsto install the software.
Commonly used commands of this software

  • cpufreq-info:View CPU status;
  • sudo cpufreq-set -c 0 -g performance: Set the status of the specified CPU. -c 1Specify the CPU core number to be set. 0 represents the 1st CPU core; -g performanceset the CPU status to performance;
  • sudo cpufreq-set -c 1 -d 900MHz: Set the status of the specified CPU and -d 900MHzspecify the minimum frequency of the CPU;
  • sudo cpufreq-set -c 2 -u 2.6GHz: Set the status of the specified CPU and -u 2.6GHzspecify the maximum frequency of the CPU;

4.2 Writing scripts

  Write set_cpu_performance.shthe file and sudo chmod +x set_cpu_performance.shgive it executable permissions

#!/bin/bash
# Check if cpufrequtils package is installed
if ! [ -x "$(command -v cpufreq-set)" ]; then
  echo "Error: cpufrequtils package is not installed. Please install it first."
  exit 1
fi

cpu_mode=performance
#cpu_mode=powersave

# Get the number of CPU cores
cpu_cores=$(nproc)

# Set performance mode for each CPU core
for ((cpu=0; cpu<$cpu_cores; cpu++));
do
  sudo cpufreq-set -c $cpu -g ${cpu_mode}
done

# Verify the current CPU frequency governor
cpufreq-info --policy | grep "current policy"

echo "Curent CPU(${cpu_cores}) are ${cpu_mode}."
# cat /proc/cpuinfo | grep processor | wc -l

in,

  • cpu_mode:Can be switched between powersave,performance
  • cat /proc/cpuinfo | grep processor | wc -l: Get the current total number of CPUs, or use lscputo view

4.3 Set as default boot script

sudo bash set_cpu_performance.shConfirm that the script works    by executing it . As for how to set the default execution script at startup, please refer to other blogs;

Guess you like

Origin blog.csdn.net/qq_38429958/article/details/132425857