内存与 CPU 性能工具
使用 `vmstat`、`sar` 和 `mpstat` 等工具诊断与 CPU 和内存相关的性能问题。
内存与 CPU 性能工具 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Diagnose Performance Issues
Understanding system performance is key to troubleshooting slow applications or an unresponsive server. CPU and memory are often the first resources to check.
In this lesson, we'll explore powerful command-line tools to monitor and diagnose issues related to your system's CPU and memory usage.
Meet `vmstat`
The vmstat (virtual memory statistics) command provides a quick, real-time snapshot of your system's activity. It reports on processes, memory, paging, block I/O, traps, and CPU activity.
It's excellent for observing current system behavior and identifying immediate bottlenecks.
`vmstat` Memory Overview
Let's run vmstat 1 1 to see a single report, then we'll break down the memory-related output. This command shows a report every 1 second, for 1 iteration.
- swpd: amount of virtual memory used.
- free: amount of idle memory.
- buff: memory used as buffers.
- cache: memory used as cache.
vmstat 1 1`vmstat` CPU Details
Now, let's look at the CPU section of vmstat. Run the command again if you like, and focus on the 'cpu' columns:
- us (user): time spent running non-kernel code.
- sy (system): time spent running kernel code.
- id (idle): time spent idle.
- wa (wait): time spent waiting for I/O.
High wa often indicates disk bottlenecks.
vmstat 1 1Introducing `sar`
The sar (System Activity Reporter) command is part of the sysstat package. Unlike vmstat, sar can collect and report historical system activity data.
This makes it invaluable for long-term analysis, identifying trends, and diagnosing intermittent problems that might not be visible with real-time tools.
`sar` for CPU Usage
To view CPU utilization with sar, we use the -u option. Run sar -u 1 1 to get a single report.
- %user: CPU usage by user-level applications.
- %system: CPU usage by kernel-level tasks.
- %iowait: Time CPU spent waiting for I/O.
- %idle: Time CPU was idle.
sar -u 1 1`sar` for Memory Usage
To get detailed memory utilization reports with sar, use the -r option. Try sar -r 1 1.
- kbmemfree: Amount of free memory.
- kbmemused: Amount of used memory.
- %memused: Percentage of used memory.
- kbbuffers: Memory used by kernel buffers.
- kbcached: Memory used by page cache.
sar -r 1 1Understanding `mpstat`
The mpstat (multi-processor statistics) command is also part of the sysstat package. Its main advantage is providing per-processor or per-core CPU activity.
This is crucial for systems with multiple CPU cores, as it helps identify if a specific core is overloaded while others are idle.
`mpstat` Per-Core Activity
To see statistics for all individual CPU cores, use mpstat -P ALL 1 1. You'll see a separate line for each core (CPU 0, CPU 1, etc.) and an 'all' line for the average.
This helps pinpoint if a single-threaded application is maxing out one core, even if overall CPU usage seems low.
mpstat -P ALL 1 1Performance Tool Check
Which of the following statements about Linux performance tools are TRUE?
Recap: CPU & Memory Tools
In this lesson, you learned about three essential tools for monitoring CPU and memory performance:
vmstat: For real-time system activity snapshots.sar: For collecting and reporting historical system activity data.mpstat: For detailed per-processor/per-core CPU usage.
Mastering these commands will significantly boost your ability to diagnose and troubleshoot Linux system performance issues.
常见问题解答
「内存与 CPU 性能工具」课时是免费的吗?
是的 — 「内存与 CPU 性能工具」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。
「内存与 CPU 性能工具」这节课中我会学到什么?
使用 `vmstat`、`sar` 和 `mpstat` 等工具诊断与 CPU 和内存相关的性能问题。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Command Line Mastery 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「内存与 CPU 性能工具」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?
能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。