科技 > 人工智能 > 智能机器人

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

98人参与 2024-08-06 智能机器人

一、问题描述

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

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

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:

sudo apt-get install indicator-cpufreq

安装完成后,重新启动计算机。重新启动后,在界面右上角会出现如下图标。
在这里插入图片描述

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

三、查看各 cpu 状态

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

cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

终端将会打印以下内容:

performance
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance
performance

这说明所有 cpu 都处于 “performance” (性能) 模式。

四、开机默认高性能设置

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

4.1 安装 cpufrequtils

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

sudo apt-get install cpufrequtils

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

4.2 编写脚本

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

#!/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 如何启动你的脚本。

sudo nano /etc/systemd/system/set_cpu_performance.service

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

[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 以加载新的服务单元

sudo systemctl daemon-reload

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

sudo systemctl enable set_cpu_performance.service

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

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

【Ubuntu20.04 CUDA11.1+Torch1.10+Anaconda 保姆级安装教程】

08-06

【超详细含图】Ubuntu系统忘记root密码的解决方法_ubuntu忘记root密码

08-06

优必选、吉利、天奇股份达成战略合作,极氪迎来首位人形机器人“员工”

08-06

179 元起,天猫精灵 IN 糖 6 智能音箱发布:首发千岛式互动屏“会跳科目三”

08-06

中兴星云研发大模型通过备案:支持需求、设计、编程、测试等不同阶段 30 多种场景

08-06

大型 AI 行为艺术:「人类止步」Reddit,17 个大模型疯狂灌水|开源

08-06

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论