Merge pull request #5497 from ictlyh/master

Translated tech/20170410 Cpustat – Monitors CPU Utilization by Runnin…
This commit is contained in:
Yuanhao Luo 2017-04-22 19:37:07 +08:00 committed by GitHub
commit 64798389bd
2 changed files with 168 additions and 168 deletions

View File

@ -1,168 +0,0 @@
ictlyh Translating
Cpustat Monitors CPU Utilization by Running Processes in Linux
============================================================
Cpustat is a powerful system performance measure program for Linux, written using [Go programming language][3]. It attempts to reveal CPU utilization and saturation in an effective way, using The Utilization Saturation and Errors (USE) Method (a methodology for analyzing the performance of any system).
It extracts higher frequency samples of every process being executed on the system and then summarizes these samples at a lower frequency. For instance, it can measure every process every 200ms and summarize these samples every 5 seconds, including min/average/max values for certain metrics.
**Suggested Read:** [20 Command Line Tools to Monitor Linux Performance][4]
Cpustat outputs data in two possible ways: a pure text list of the summary interval and a colorful scrolling dashboard of each sample.
### How to Install Cpustat in Linux
You must have Go (GoLang) installed on your Linux system in order to use cpustat, click on the link below to follow the GoLang installation steps that is if you do not have it installed:
1. [Install GoLang (Go Programming Language) in Linux][1]
Once you have installed Go, type the go get command below to install it, this command will install the cpustat binary in your GOBIN variable:
```
# go get github.com/uber-common/cpustat
```
### How to Use Cpustat in Linux
When the installation process completes, run cpustat as follows with root privileges using the sudo command that is if your controlling the system as a non-root user, otherwise youll get the error as shown:
```
$ $GOBIN/cpustat
This program uses the netlink taskstats interface, so it must be run as root.
```
Note: To run cpustat as well as all other Go programs you have installed on your system like any other commands, include GOBIN variable in your PATH environment variable. Open the link below to learn how to set the PATH variable in Linux.
1. [Learn How to Set Your $PATH Variables Permanently in Linux][2]
This is how cpustat works; the `/proc` directory is queried to get the current [list of process IDs][5] for every interval, and:
* for each PID, read /proc/pid/stat, then compute difference from previous sample.
* in case its a new PID, read /proc/pid/cmdline.
* for each PID, send a netlink message to fetch the taskstats, compute difference from previous sample.
* fetch /proc/stat to get the overall system stats.
Again, each sleep interval is adjusted to account for the amount of time consumed fetching all of these stats. Furthermore, each sample also records the time it took to scale each measurement by the actual elapsed time between samples. This attempts to account for delays in cpustat itself.
When run without any arguments, cpustat will display the following by default: sampling interval: 200ms, summary interval: 2s (10 samples), [showing top 10 procs][6], user filter: all, pid filter: all as shown in the screenshot below:
```
$ sudo $GOBIN/cpustat
```
[
![Cpustat - Monitor Linux CPU Usage](http://www.tecmint.com/wp-content/uploads/2017/03/Cpustat-Monitor-Linux-CPU-Usage.png)
][7]
Cpustat Monitor Linux CPU Usage
From the output above, the following are the meanings of the system-wide summary metrics displayed before the fields:
* usr  min/avg/max user mode run time as a percentage of a CPU.
* sys  min/avg/max system mode run time as a percentage of a CPU.
* nice  min/avg/max user mode low priority run time as a percentage of a CPU.
* idle  min/avg/max user mode run time as a percentage of a CPU.
* iowait  min/avg/max delay time waiting for disk IO.
* prun  min/avg/max count of processes in a runnable state (same as load average).
* pblock  min/avg/max count of processes blocked on disk IO.
* pstart  number of processes/threads started in this summary interval.
Still from the output above, for a given process, the different columns mean:
* name  common process name from /proc/pid/stat or /proc/pid/cmdline.
* pid  process id, also referred to as “tgid”.
* min  lowest sample of user+system time for the pid, measured from /proc/pid/stat. Scale is a percentage of a CPU.
* max  highest sample of user+system time for this pid, also measured from /proc/pid/stat.
* usr  average user time for the pid over the summary period, measured from /proc/pid/stat.
* sys  average system time for the pid over the summary period, measured from /proc/pid/stat.
* nice  indicates current “nice” value for the process, measured from /proc/pid/stat. Higher means “nicer”.
* runq  time the process and all of its threads spent runnable but waiting to run, measured from taskstats via netlink. Scale is a percentage of a CPU.
* iow  time the process and all of its threads spent blocked by disk IO, measured from taskstats via netlink. Scale is a percentage of a CPU, averaged over the summary interval.
* swap  time the process and all of its threads spent waiting to be swapped in, measured from taskstats via netlink. Scale is a percentage of a CPU, averaged over the summary interval.
* vcx and icx  total number of voluntary context switches by the process and all of its threads over the summary interval, measured from taskstats via netlink.
* rss  current RSS value fetched from /proc/pid/stat. It is the amount of memory this process is using.
* ctime  sum of user+sys CPU time consumed by waited for children that exited during this summary interval, measured from /proc/pid/stat.
Note that long running child processes can often confuse this measurement, because the time is reported only when the child process exits. However, this is useful for measuring the impact of frequent cron jobs and health checks where the CPU time is often consumed by many child processes.
* thrd  number of threads at the end of the summary interval, measured from /proc/pid/stat.
* sam  number of samples for this process included in the summary interval. Processes that have recently started or exited may have been visible for fewer samples than the summary interval.
The following command displays the top 10 root user processes running on the system:
```
$ sudo $GOBIN/cpustat -u root
```
[
![Find Root User Running Processes](http://www.tecmint.com/wp-content/uploads/2017/03/show-root-user-processes.png)
][8]
Find Root User Running Processes
To display output in a fancy terminal mode, use the `-t` flag as follows:
```
$ sudo $GOBIN/cpustat -u roo -t
```
[
![Running Process Usage of Root User](http://www.tecmint.com/wp-content/uploads/2017/03/Root-User-Runnng-Processes.png)
][9]
Running Process Usage of Root User
To view the [top x number of processes][10] (the default is 10), you can use the `-n` flag, the following command shows the [top 20 Linux processes running][11] on the system:
```
$ sudo $GOBIN/cpustat -n 20
```
You can also write CPU profile to a file using the `-cpuprofile` option as follows and then use the [cat command][12] to view the file:
```
$ sudo $GOBIN/cpustat -cpuprofile cpuprof.txt
$ cat cpuprof.txt
```
To display help info, use the `-h` flag as follows:
```
$ sudo $GOBIN/cpustat -h
```
Find additional info from the cpustat Github Repository: [https://github.com/uber-common/cpustat][13]
Thats all! In this article, we showed you how to install and use cpustat, a useful system performance measure tool for Linux. Share your thoughts with us via the comment section below.
--------------------------------------------------------------------------------
作者简介:
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/cpustat-monitors-cpu-utilization-by-processes-in-linux/
作者:[Aaron Kili][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/aaronkili/
[1]:http://www.tecmint.com/install-go-in-linux/
[2]:http://www.tecmint.com/set-path-variable-linux-permanently/
[3]:http://www.tecmint.com/install-go-in-linux/
[4]:http://www.tecmint.com/command-line-tools-to-monitor-linux-performance/
[5]:http://www.tecmint.com/find-process-name-pid-number-linux/
[6]:http://www.tecmint.com/find-linux-processes-memory-ram-cpu-usage/
[7]:http://www.tecmint.com/wp-content/uploads/2017/03/Cpustat-Monitor-Linux-CPU-Usage.png
[8]:http://www.tecmint.com/wp-content/uploads/2017/03/show-root-user-processes.png
[9]:http://www.tecmint.com/wp-content/uploads/2017/03/Root-User-Runnng-Processes.png
[10]:http://www.tecmint.com/find-processes-by-memory-usage-top-batch-mode/
[11]:http://www.tecmint.com/install-htop-linux-process-monitoring-for-rhel-centos-fedora/
[12]:http://www.tecmint.com/13-basic-cat-command-examples-in-linux/
[13]:https://github.com/uber-common/cpustat
[14]:http://www.tecmint.com/author/aaronkili/
[15]:http://www.tecmint.com/10-useful-free-linux-ebooks-for-newbies-and-administrators/
[16]:http://www.tecmint.com/free-linux-shell-scripting-books/

View File

@ -0,0 +1,168 @@
Cpustat - 在 Linux 下通过运行程序监控 CPU 使用率
============================================================
Cpustat 是 Linux 下一个强大的系统性能测量程序,它用 [Go 编程语言][3] 编写。它尝试使用 [The Utilization Saturation and ErrorsUSEMethod](http://www.brendangregg.com/usemethod.html "The Utilization Saturation and ErrorsUSEMethod")(一个用于分析任意系统性能的方法),以有效的方式显示 CPU 利用率和饱和度。
它高频率提取系统中运行的每个进程的样本,然后用较低频率汇总这些样本。例如,它能够每 200ms 测量一次每个进程,然后每 5 秒汇总这些样本,包括某些度量的最小/平均/最大值min/avg/max
**推荐阅读:** [监控 Linux 性能的 20 个命令行工具][4]
Cpustat 能用两种方式输出数据:汇总间隔的纯文本列表和每个样本的有色滚动面板。
### 如何在 Linux 中安装 Cpustat
为了使用 cpustat你的 Linux 系统中必须安装有 GoGoLang如果你还没有安装它点击下面的链接逐步安装 GoLang
1. [在 Linux 下安装 GoLangGo 编程语言)][1]
安装完 Go 以后,输入下面的 `go get` 命令安装 Cpustat这个命令会将 cpustat 二进制文件安装到你的 `GOBIN` 变量(该变量所指的路径):
# go get github.com/uber-common/cpustat
### 如何在 Linux 中使用 Cpustat
安装过程完成后,如果你不是以 root 用户控制系统,像下面这样使用 sudo 命令获取 root 权限运行 cpustat否则会出现下面显示的错误信息
$ $GOBIN/cpustat
This program uses the netlink taskstats interface, so it must be run as root.
注意:想要像你系统中已经安装的其它 Go 程序那样运行 cpustat你需要把 `GOBIN` 变量添加到 `PATH` 环境变量。打开下面的链接学习如何在 Linux 中设置 `PATH` 变量。
1. [学习如何在 Linux 中永久设置你的 $PATH 变量][2]
cpustat 是这样工作的:每个间隔会查询 `/proc` 目录获取当前[进程 ID 列表][5],然后:
* 对于每个 PID读取 /proc/pid/stat然后计算和前一个样本的差别。
* 如果是一个新的 PID读取 /proc/pid/cmdline。
* 对于每个 PID发送 netlink 消息获取 taskstat计算和前一个样本的差别。
* 读取 /proc/stat 获取总的系统统计信息。
根据获取所有这些统计信息所花费的时间,会调整每个睡眠间隔。另外,通过样本之间实际经过的时间,每个样本也会记录它用于测量的时间。这可用于计算 cpustat 自身的延迟。
当不带任何参数运行时cpustat 默认会显示以下信息样本间隔200ms汇总间隔2s10 个样本),[显示前 10 个进程][6]用户过滤器allpid 过滤器all正如下面截图所示
$ sudo $GOBIN/cpustat
[
![Cpustat - 监控 Linux CPU 使用](http://www.tecmint.com/wp-content/uploads/2017/03/Cpustat-Monitor-Linux-CPU-Usage.png)
][7]
Cpustat 监控 Linux CPU 使用
在上面的输出中,之前显示的系统范围的度量字段意义如下:
* usr - 用户模式运行时间占 CPU 百分比的 min/avg/max 值。
* sys - 系统模式运行时间占 CPU 百分比 的min/avg/max 值。
* nice - 用户模式低优先级运行时间占 CPU 百分比的 min/avg/max 值。
* idle - 用户模式空闲时间占 CPU 百分比的 min/avg/max 值。
* iowait - 等待磁盘 IO 的 min/avg/max 延迟时间。
* prun - 可运行状态的 min/avg/max 进程数量load average 也是如此)。
* pblock - 被磁盘 IO 阻塞的 min/avg/max 进程数量。
* pstat - 这次汇总间隔启动的进程/线程数目。
同样还是上面的输出,对于一个进程,不同列的意思分别是:
* name - 从 /proc/pid/stat 或 /proc/pid/cmdline 获取的进程名称。
* pid - 进程 ID也称为 “tgid”。
* min - 该 pid user+system 时间的最小样本,从 /proc/pid/stat 计算。比率是 CPU 的百分比。
* max - 该 pid user+system 时间的最大样本,从 /proc/pid/stat 计算。
* usr - 该汇总期间该 pid 的平均 user 时间,从 /proc/pid/stat 计算。
* sys - 该汇总期间该 pid 的平均 system 时间,从 /proc/pid/stat 计算。
* nice - 表示该进程的当前 “nice” 值,从 /proc/pid/stat 计算。值越高表示越好nicer
* runq - 进程和它所有线程可运行但等待运行的时间,通过 netlink 从 taskstats 计算。比率是 CPU 的百分比。
* iow - 进程和它所有线程被磁盘 IO 阻塞的时间,通过 netlink 从 taskstats 计算。比率是 CPU 的百分比,对整个汇总间隔平均。
* swap - 进程和它所有线程等待被换入swap in的时间过 netlink 从 taskstats 计算。Scale 是 CPU 的百分比,对整个汇总间隔平均。
* vcx 和 icx - 在汇总间隔期间进程和它的所有线程自动上下文切换总的次数,过 netlink 从 taskstats 计算。
* rss - 从 /proc/pid/stat 获取的当前 RSS 值。它是指该进程正在使用的内存数量。
* ctime - 在汇总间隔期间等待子进程退出的 user+sys CPU 时间总和,从 /proc/pid/stat 计算。
注意长时间运行的子进程可能导致混淆这个值,因为只有在子进程退出后才会报告时间。但是,这对于计算高频 cron 任务以及 CPU 时间经常被多个子进程使用的健康检查非常有帮助。
* thrd - 汇总间隔最后线程的数目,从 /proc/pid/stat 计算。
* sam - 在这个汇总间隔期间该进程的样本数目。最近启动会退出的进程可能比汇总间隔的样本数目少。
下面的命令显示了系统中运行的前 10 个 root 用户进程:
$ sudo $GOBIN/cpustat -u root
[
![查找 root 用户正在运行的进程](http://www.tecmint.com/wp-content/uploads/2017/03/show-root-user-processes.png)
][8]
查找 root 用户正在运行的进程
要想用更好看的终端模式显示输出,像下面这样用 `-t` 选项:
$ sudo $GOBIN/cpustat -u roo -t
[
![root 用户正在运行的进程](http://www.tecmint.com/wp-content/uploads/2017/03/Root-User-Runnng-Processes.png)
][9]
root 用户正在运行的进程
要查看前 [x 个进程][10](默认是 10你可以使用 `-n` 选项,下面的命令显示了系统中 [正在运行的前 20 个进程][11]
$ sudo $GOBIN/cpustat -n 20
你也可以像下面这样使用 `-cpuprofile` 选项将 CPU 信息写到文件,然后用 [cat 命令][12]查看文件:
$ sudo $GOBIN/cpustat -cpuprofile cpuprof.txt
$ cat cpuprof.txt
要显示帮助信息,像下面这样使用 `-h` 选项:
$ sudo $GOBIN/cpustat -h
从 cpustat Github 参仓库:[https://github.com/uber-common/cpustat][13] 查阅其它资料。
就是这些!在这篇文章中,我们向你展示了如何安装和使用 cpustatLinux 下一个有用的系统性能测量工具。通过下面的评论框和我们分享你的想法吧。
--------------------------------------------------------------------------------
作者简介:
Aaron Kili 是一个 Linux 和 F.O.S.SFree and Open-Source Software 爱好者,一个 Linux 系统管理员、web 开发员,现在也是 TecMint 的内容创建者,他喜欢和电脑一起工作,他相信知识共享。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/cpustat-monitors-cpu-utilization-by-processes-in-linux/
作者:[Aaron Kili][a]
译者:[ictlyh](https://github.com/ictlyh)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/aaronkili/
[1]:http://www.tecmint.com/install-go-in-linux/
[2]:http://www.tecmint.com/set-path-variable-linux-permanently/
[3]:http://www.tecmint.com/install-go-in-linux/
[4]:http://www.tecmint.com/command-line-tools-to-monitor-linux-performance/
[5]:http://www.tecmint.com/find-process-name-pid-number-linux/
[6]:http://www.tecmint.com/find-linux-processes-memory-ram-cpu-usage/
[7]:http://www.tecmint.com/wp-content/uploads/2017/03/Cpustat-Monitor-Linux-CPU-Usage.png
[8]:http://www.tecmint.com/wp-content/uploads/2017/03/show-root-user-processes.png
[9]:http://www.tecmint.com/wp-content/uploads/2017/03/Root-User-Runnng-Processes.png
[10]:http://www.tecmint.com/find-processes-by-memory-usage-top-batch-mode/
[11]:http://www.tecmint.com/install-htop-linux-process-monitoring-for-rhel-centos-fedora/
[12]:http://www.tecmint.com/13-basic-cat-command-examples-in-linux/
[13]:https://github.com/uber-common/cpustat
[14]:http://www.tecmint.com/author/aaronkili/
[15]:http://www.tecmint.com/10-useful-free-linux-ebooks-for-newbies-and-administrators/
[16]:http://www.tecmint.com/free-linux-shell-scripting-books/