如何在 Linux 上检查 CPU 使用率

Linux命令 Edge插件网 1年前 (2023-09-20) 290次浏览 已收录 0个评论

无论您是运行 Web 服务器、管理数据库,还是只是确保系统的平稳运行,了解如何有效地监控 CPU 使用情况都是必不可少的技能。在本综合指南中,我们将深入研究各种 Linux 实用程序—, , , ,并为您提供知识和专业技能,以保持系统的 CPU 性能得到优化。topmpstatsariostatvmstat

如何在 Linux 上检查 CPU 使用率

了解 Linux CPU 监控工具

top:交互式 CPU 监视器

Linux 的命令行实用程序是一个多功能的交互式工具,用于实时监控 CPU 使用情况。它提供了系统进程的全面视图,包括 CPU 使用率、内存消耗等。top

用法

<span class="pln">top</span>

顶部互动

    • 观察进程列表。
    • 按 CPU 使用率对进程进行排序。Shift + P
    • 实时解释数据。

示例结果

<span class="pln"> PID USER PR NI VIRT RES SHR S </span><span class="pun">%</span><span class="pln">CPU </span><span class="pun">%</span><span class="pln">MEM TIME</span><span class="pun">+</span><span class="pln"> COMMAND
</span><span class="lit">1500</span><span class="pln"> meilana </span><span class="lit">20</span> <span class="lit">0</span> <span class="lit">1905296</span> <span class="lit">130640</span> <span class="lit">38520</span><span class="pln"> R </span><span class="lit">18.3</span> <span class="lit">6.4</span> <span class="lit">0</span><span class="pun">:</span><span class="lit">03.45</span><span class="pln"> firefox
</span><span class="lit">2550</span><span class="pln"> maria </span><span class="lit">20</span> <span class="lit">0</span> <span class="lit">1129768</span> <span class="lit">94092</span> <span class="lit">57648</span><span class="pln"> S </span><span class="lit">16.6</span> <span class="lit">4.6</span> <span class="lit">0</span><span class="pun">:</span><span class="lit">02.80</span><span class="pln"> gnome</span><span class="pun">-</span><span class="pln">terminal
</span><span class="lit">2875</span><span class="pln"> ulfa </span><span class="lit">20</span> <span class="lit">0</span> <span class="lit">680252</span> <span class="lit">52448</span> <span class="lit">32712</span><span class="pln"> S </span><span class="lit">6.6</span> <span class="lit">2.6</span> <span class="lit">0</span><span class="pun">:</span><span class="lit">00.70</span><span class="pln"> code
</span><span class="pun">...</span>

提示:按下可退出 。qtop

用于 CPU 指标mpstat

mpstatsysstat 软件包的一部分是一个强大的工具,可提供详细的 CPU 统计信息。

用法

  • 安装系统(如果尚未安装):
<span class="pln">sudo apt install sysstat </span><span class="com"># On Debian/Ubuntu</span><span class="pln">
sudo dnf install sysstat </span><span class="com"># On CentOS/RHEL</span>

运行 mpstat

<span class="pln">mpstat </span><span class="pun">-</span><span class="pln">P ALL</span>

示例结果

<span class="typ"><a href="https://www.edge66.com/edgetag/linux" title="查看更多关于Linux的文章" target="_blank">Linux</a></span> <span class="lit">5.4</span><span class="pun">.</span><span class="lit">0</span><span class="pun">-</span><span class="lit">80</span><span class="pun">-</span><span class="kwd">generic</span> <span class="pun">(</span><span class="pln">hostname</span><span class="pun">)</span>    <span class="lit">09</span><span class="pun">/</span><span class="lit">01</span><span class="pun">/</span><span class="lit">23</span><span class="pln">    _x86_64_    </span><span class="pun">(</span><span class="lit">8</span><span class="pln"> CPU</span><span class="pun">)</span>

<span class="lit">12</span><span class="pun">:</span><span class="lit">32</span><span class="pun">:</span><span class="lit">25</span><span class="pln"> PM  CPU   </span><span class="pun">%</span><span class="pln">usr  </span><span class="pun">%</span><span class="pln">nice   </span><span class="pun">%</span><span class="pln">sys </span><span class="pun">%</span><span class="pln">iowait   </span><span class="pun">%</span><span class="pln">irq  </span><span class="pun">%</span><span class="pln">soft  </span><span class="pun">%</span><span class="pln">steal  </span><span class="pun">%</span><span class="pln">guest  </span><span class="pun">%</span><span class="pln">gnice  </span><span class="pun">%</span><span class="pln">idle
</span><span class="lit">12</span><span class="pun">:</span><span class="lit">32</span><span class="pun">:</span><span class="lit">25</span><span class="pln"> PM  all   </span><span class="lit">1.63</span>   <span class="lit">0.05</span>   <span class="lit">0.26</span>    <span class="lit">0.12</span>   <span class="lit">0.00</span>   <span class="lit">0.02</span>    <span class="lit">0.00</span>    <span class="lit">0.00</span>    <span class="lit">0.00</span>  <span class="lit">97.91</span>
<span class="lit">12</span><span class="pun">:</span><span class="lit">32</span><span class="pun">:</span><span class="lit">25</span><span class="pln"> PM    </span><span class="lit">0</span>   <span class="lit">0.48</span>   <span class="lit">0.05</span>   <span class="lit">0.26</span>    <span class="lit">0.11</span>   <span class="lit">0.00</span>   <span class="lit">0.02</span>    <span class="lit">0.00</span>    <span class="lit">0.00</span>    <span class="lit">0.00</span>  <span class="lit">99.08</span>
<span class="pun">...</span>

故障排除提示:如果未找到,请确保正确安装和配置。mpstatsysstat

使用 分析 CPU 性能sar

(系统活动报告器)实用程序提供系统性能的历史视图,包括 CPU 使用率。这是识别趋势和潜在问题的好工具。sar

用法

<span class="pln">sar </span><span class="pun">-</span><span class="pln">u</span>

示例结果

<span class="lit">12</span><span class="pun">:</span><span class="lit">00</span><span class="pun">:</span><span class="lit">01</span><span class="pln"> AM     CPU     </span><span class="pun">%</span><span class="pln">user     </span><span class="pun">%</span><span class="pln">nice   </span><span class="pun">%</span><span class="pln">system   </span><span class="pun">%</span><span class="pln">iowait    </span><span class="pun">%</span><span class="pln">steal     </span><span class="pun">%</span><span class="pln">idle
</span><span class="lit">12</span><span class="pun">:</span><span class="lit">10</span><span class="pun">:</span><span class="lit">01</span><span class="pln"> AM     all      </span><span class="lit">2.84</span>      <span class="lit">0.02</span>      <span class="lit">0.31</span>      <span class="lit">0.05</span>      <span class="lit">0.00</span>     <span class="lit">96.77</span>
<span class="lit">12</span><span class="pun">:</span><span class="lit">20</span><span class="pun">:</span><span class="lit">01</span><span class="pln"> AM     all      </span><span class="lit">3.24</span>      <span class="lit">0.01</span>      <span class="lit">0.37</span>      <span class="lit">0.05</span>      <span class="lit">0.00</span>     <span class="lit">96.34</span>
<span class="pun">...</span>

提示:用于查看特定日期的历史数据。sar --/var/log/sa/sadd

分析 磁盘活动iostat

虽然主要是一种磁盘监控工具,但iostat对于诊断由I / O操作引起的CPU瓶颈非常宝贵。

用法

<span class="pln">iostat</span>

示例结果

<span class="pln">avg</span><span class="pun">-</span><span class="pln">cpu</span><span class="pun">:</span>  <span class="pun">%</span><span class="pln">user   </span><span class="pun">%</span><span class="pln">nice </span><span class="pun">%</span><span class="pln">system </span><span class="pun">%</span><span class="pln">iowait  </span><span class="pun">%</span><span class="pln">steal   </span><span class="pun">%</span><span class="pln">idle
          </span><span class="lit">15.23</span>    <span class="lit">0.00</span>    <span class="lit">0.80</span>    <span class="lit">3.19</span>    <span class="lit">0.00</span>   <span class="lit">80.78</span>

<span class="typ">Device</span><span class="pln">             tps    kB_read</span><span class="pun">/</span><span class="pln">s    kB_wrtn</span><span class="pun">/</span><span class="pln">s    kB_read    kB_wrtn
sda               </span><span class="lit">3.22</span>        <span class="lit">43.60</span>         <span class="lit">0.17</span>     <span class="lit">840219</span>       <span class="lit">3152</span><span class="pln">
sdb               </span><span class="lit">0.00</span>         <span class="lit">0.00</span>         <span class="lit">0.00</span>         <span class="lit">44</span>          <span class="lit">0</span>
<span class="pun">...</span>

故障排除提示:如果未找到,请确保正确安装和配置。iostatsysstat

了解系统行为vmstat

vmstat提供对各种系统参数的见解,包括 CPU、内存和 I/O 统计信息。

用法

<span class="pln">vmstat </span><span class="lit">1</span>

示例结果

<span class="pln">procs </span><span class="pun">-----------</span><span class="pln">memory</span><span class="pun">----------</span> <span class="pun">---</span><span class="pln">swap</span><span class="pun">--</span> <span class="pun">-----</span><span class="pln">io</span><span class="pun">----</span> <span class="pun">-</span><span class="pln">system</span><span class="pun">--</span> <span class="pun">------</span><span class="pln">cpu</span><span class="pun">-----</span><span class="pln">
 r  b   swpd   free   buff  cache   si   so    bi    bo   </span><span class="kwd">in</span><span class="pln">   cs us sy id wa st
 </span><span class="lit">1</span>  <span class="lit">0</span>    <span class="lit">336</span>  <span class="lit">85348</span>  <span class="lit">11804</span> <span class="lit">667628</span>    <span class="lit">0</span>    <span class="lit">0</span>    <span class="lit">54</span>    <span class="lit">33</span>   <span class="lit">56</span>   <span class="lit">62</span>  <span class="lit">9</span>  <span class="lit">2</span> <span class="lit">89</span>  <span class="lit">0</span>  <span class="lit">0</span>
 <span class="pun">...</span>

提示:按下可停止。Ctrl + Cvmstat

比较和选择合适的工具

这些工具中的每一个都有其优势和特定的用例。选择正确的工具时,请考虑以下事项:

  • 对于实时监控:提供交互式视图。top
  • 对于历史数据分析:是您的最佳选择。sar
  • 用于诊断 CPU-I/O 问题:非常宝贵。iostat
  • 用于全面的系统分析:涵盖各种参数。vmstat

比较和选择合适的工具

选择正确的工具取决于您的特定监控需求。这是一个快速比较:

工具 实时 安装 互动 其他指标
top 是的 预装 是的 内存, 进程
mpstat 系统统计 每个处理器
sar 是的 系统统计 磁盘、内存
iostat 系统统计 磁盘、网络
vmstat 预装 内存, 进程

监控 Linux 上的 CPU 使用率是维护系统性能和稳定性的一项基本任务。通过掌握本指南中讨论的工具—, , , , 和 – 您可以主动识别和解决与 CPU 相关的问题,确保您的 Linux 系统平稳运行。topmpstatsariostatvmstat

请记住,有效的监控是一个持续的过程。定期查看 CPU 使用率和系统统计信息将帮助您优化 Linux 环境并为用户提供无缝体验。


Edge插件网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何在 Linux 上检查 CPU 使用率
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址