【Ubuntu】在Ubuntu中实现酣畅淋漓的性能释放:调整CPU频率

一、问题描述

在机器人开发中,经常需要运行诸如 SLAM 和 Planning 等 CPU 密集型程序,这些程序需要充分发挥计算机的性能,以确保算法的高效运行。然而,默认情况下,Ubuntu 通常将 CPU 设置为节能模式,导致 CPU 在低频率下运行,从而可能影响算法的执行效率。因此,为了最大化 CPU 性能,需要将所有核心的工作模式设置为高性能。

查看各 CPU 核心的工作模式可以通过下面这条命令:

bash 复制代码
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

终端打印以下内容:

复制代码
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave

这说明CPU处于节点模式,正在养生

二、软件安装与设置

执行以下命令安装 indicator-cpufreq:

bash 复制代码
sudo apt-get install indicator-cpufreq

安装完成后,重新启动计算机。重新启动后,在界面右上角会出现如下图标。

点击该图标,并选择"性能"模式。

  • performance:高性能模式,将 CPU 频率设为最高值,以最大化 CPU 性能。
  • powersave:节能模式,降低 CPU频率,以最大化节能。

三、查看各 CPU 状态

执行以下命令,查看各 CPU 核心的工作模式:

bash 复制代码
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

终端将会打印以下内容:

bash 复制代码
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance

这说明所有 CPU 都处于 "performance" (性能) 模式。

四、开机默认高性能设置

上述步骤可以方便快捷地设置当前 CPU 状态,但重启计算机后,系统会恢复默认设置。为了确保系统在每次启动时都以高性能模式运行,可以按照以下步骤进行设置:

4.1 安装 cpufrequtils

执行以下命令安装 cpufrequtils 软件:

bash 复制代码
sudo apt-get install cpufrequtils

该软件包含一些常用命令,如:

  • cpufreq-info:查看 CPU 状态;
  • sudo cpufreq-set -c 0 -g performance:设置指定 CPU 的状态。其中,-c 0 指定要设置的 CPU 核心编号
  • performance 表示将 CPU 状态设置为高性能模式;
  • sudo cpufreq-set -c 1 -d 900MHz:设置指定 CPU 的最低频率为 900MHz;
  • sudo cpufreq-set -c 2 -u 2.6GHz:设置指定 CPU 的最高频率为 2.6GHz。

4.2 编写脚本

创建 set_cpu_performance.sh 文件,并赋予可执行权限:

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

cpu_mode=performance  # CPU operating mode, defaulting to performance mode

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

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

# Validate the current CPU frequency scaling governor state
cpufreq-info --policy | grep "current policy"

echo "All $cpu_cores CPU cores are now in $cpu_mode mode."

在脚本中,可以通过修改 cpu_mode 变量来切换工作模式。

4.3 设为默认开机脚本

(1)将你的脚本放置在一个合适的位置,比如 /usr/local/bin。

(2)创建一个 .service 文件,这将告诉 systemd 如何启动你的脚本。

bash 复制代码
sudo nano /etc/systemd/system/set_cpu_performance.service

(3)在编辑器中输入以下内容:

bash 复制代码
[Unit]
Description=Set CPU performance mode
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/set_cpu_performance.sh

[Install]
WantedBy=multi-user.target

请确保 ExecStart 的路径与你的脚本的实际路径相匹配,然后保存并退出编辑器。

(4)刷新 systemd 以加载新的服务单元

bash 复制代码
sudo systemctl daemon-reload

(5)启用服务,使其在启动时自动运行。

bash 复制代码
sudo systemctl enable set_cpu_performance.service

以上操作能够确保系统在每次启动时都以高性能模式运行。

相关推荐
蒋星熠5 分钟前
WebSocket网络编程深度实践:从协议原理到生产级应用
网络·数据库·redis·python·websocket·网络协议·微服务
被遗忘的旋律.18 分钟前
Linux驱动开发笔记(十)——中断
linux·驱动开发·笔记
凡间客44 分钟前
Linux防火墙-Firewalld
linux·运维·服务器
nnerddboy1 小时前
Linux嵌入式自学笔记(基于野火EBF6ULL):1.配置环境
linux·笔记·单片机·嵌入式硬件
时序数据说1 小时前
物联网时序数据管理的利器:为何IoTDB备受青睐?
大数据·数据库·物联网·时序数据库·iotdb
Justin_191 小时前
Linux防火墙firewalld
大数据·linux·运维
GBASE2 小时前
GBASE南大通用技术分享:GBase 8a集群内存管理之堆内存
数据库
城管不管2 小时前
搭建分片集群
大数据·数据库
Mr_戴先森3 小时前
50条常用的MySQL命令汇总
数据库·mysql·oracle