Linux top command to view the use of memory and multi-core CPU

View multi-core CPU commands

mpstat -P ALL and sar -P ALL 
instructions: sar -P ALL > aaa.txt redirects the output to the file aaa.txt

 

top command

It is often used to monitor the system status of Linux, such as the use of cpu and memory. Programmers basically know this command, but it is strange that very few people can use it well. For example, the meaning of the memory value in the top monitoring view is Lots of misunderstandings.
This article describes the meaning of various data in the top view through the top monitoring screenshot of a running WEB server, and also includes the sorting of the fields of each process (task) in the view.

 

top enter view

 

    The first line:
    10:08:45 — the current system time is
    10 days, 3:05 — the system has been running for 10 days, 3 hours and 5 minutes (without restarting during this period)
    1 users — there is currently 1 user logged in to the system
    load average : 0.00, 0.00, 0.00 — The three numbers after the load average are the load conditions for 1 minute, 5 minutes, and 15 minutes, respectively.

The load average data is to check the number of active processes every 5 seconds, and then calculate the value according to a specific algorithm. If this number is divided by the number of logical CPUs, a result above 5 indicates that the system is overloaded.

    The second line:
    Tasks - tasks (processes), the system now has a total of 135 processes, of which 1 is running, 134 are sleeping (sleep), 0 are in the stopped state, and 0 are in the zombie state (zombie). indivual.

    The third line: cpu status
    0.3% us - the percentage of CPU occupied by user space.
    0.0% sy — The percentage of CPU used by kernel space.
    0.0% ni — 99.7% of CPU occupied by processes whose priorities have changed
    id — 0.0% of idle CPU
    wa — 0.0% of CPU occupied by IO waiting
    hi — 0.0% of CPU occupied by hardware IRQs
    si — soft Percentage of CPU usage by Software Interrupts

The CPU usage ratio here is different from the windows concept. If you don't understand user space and kernel space, you need to charge.

    Fourth line: memory status
    3808060k total — total physical memory (4GB)
    3660048k used — total memory in use (3.6GB)
    148012k free — total free memory (148M)
    359760k buffers — cached memory (359M

    ) Five lines: swap swap partition
    4184924k total - total swap area (4G)
    0k used - total used swap area (0M)
    4184924k free - total free swap area (4G)
    2483956k cached - total buffered swap area (2483M)

The total amount of memory in use (used) in the fourth line refers to the amount of memory currently controlled by the system kernel, and the total amount of free memory (free) is the amount that the kernel has not yet brought under its control. The memory managed by the kernel is not necessarily in use, and it also includes the memory that has been used in the past and can now be reused. The kernel does not return these reusable memory to free, so free memory on linux will be Less and less, but don't worry about it.

If you calculate the number of available memory out of habit, here is an approximate calculation formula: free in the fourth line + buffers in the fourth line + cached in the fifth line, according to this formula, the available memory of this server: 148M+259M+2483M = 2990M.

For memory monitoring, in the top, we need to monitor the used of the swap partition of the fifth row at all times. If this value is constantly changing, it means that the kernel is constantly exchanging data between memory and swap, which is the real memory is not enough.

 The sixth line is blank

    第七行以下:各进程(任务)的状态监控
    PID — 进程id
    USER — 进程所有者
    PR — 进程优先级
    NI — nice值。负值表示高优先级,正值表示低优先级
    VIRT — 进程使用的虚拟内存总量,单位kb。VIRT=SWAP+RES
    RES — 进程使用的、未被换出的物理内存大小,单位kb。RES=CODE+DATA
    SHR — 共享内存大小,单位kb
    S — 进程状态。D=不可中断的睡眠状态 R=运行 S=睡眠 T=跟踪/停止 Z=僵尸进程
    %CPU — 上次更新到现在的CPU时间占用百分比
    %MEM — 进程使用的物理内存百分比
    TIME+ — 进程使用的CPU时间总计,单位1/100秒
    COMMAND — 进程名称(命令名/命令行)

 

多U多核CPU监控

 

在top基本视图中,按键盘数字“1”,可监控每个逻辑CPU的状况:

  

观察上图,服务器有4个逻辑CPU,实际上是1个物理CPU。

如果不按1,则在top视图里面显示的是所有cpu的平均值。

进程字段排序

默认进入top时,各进程是按照CPU的占用量来排序的,在【top视图 01】中进程ID为14210的java进程排在第一(cpu占用100%),进程ID为14183的java进程排在第二(cpu占用12%)。可通过键盘指令来改变排序字段,比如想监控哪个进程占用MEM最多,我一般的使用方法如下:

1. 敲击键盘“b”(打开/关闭加亮效果),top的视图变化如下:

    

我们发现进程id为12363的“top”进程被加亮了,top进程就是视图第二行显示的唯一的运行态(runing)的那个进程,可以通过敲击“y”键关闭或打开运行态进程的加亮效果。
2. 敲击键盘“x”(打开/关闭排序列的加亮效果),top的视图变化如下:
可以看到,top默认的排序列是“%CPU”。
3. 通过”shift + >”或”shift + <”可以向右或左改变排序列,下图是按一次”shift + >”的效果图:

 

视图现在已经按照%MEM来排序了。

改变进程显示字段

1. 敲击“f”键,top进入另一个视图,在这里可以编排基本视图中的显示字段:

 

 

这里列出了所有可在top基本视图中显示的进程字段,有”*”并且标注为大写字母的字段是可显示的,没有”*”并且是小写字母的字段是不显示的。如果要在基本视图中显示“CODE”和“DATA”两个字段,可以通过敲击“r”和“s”键:

 

 2. “回车”返回基本视图,可以看到多了“CODE”和“DATA”两个字段:

 

 

 

top命令的补充

top命令是Linux上进行系统监控的首选命令,但有时候却达不到我们的要求,比如当前这台服务器,top监控有很大的局限性。这台服务器运行着websphere集群,有两个节点服务,就是【top视图 01】中的老大、老二两个java进程,top命令的监控最小单位是进程,所以看不到我关心的java线程数和客户连接数,而这两个指标是java的web服务非常重要的指标,通常我用ps和netstate两个命令来补充top的不足。

 监控java线程数:

ps -eLf | grep java | wc -l

监控网络客户连接数:

netstat -n | grep tcp | grep 侦听端口 | wc -l

上面两个命令,可改动grep的参数,来达到更细致的监控要求。

在Linux系统“一切都是文件”的思想贯彻指导下,所有进程的运行状态都可以用文件来获取。系统根目录/proc中,每一个数字子目录的名字都是运行中的进程的PID,进入任一个进程目录,可通过其中文件或目录来观察进程的各项运行指标,例如task目录就是用来描述进程中线程的,因此也可以通过下面的方法获取某进程中运行中的线程数量(PID指的是进程ID):

ls /proc/PID/task | wc -l
在linux中还有一个命令pmap,来输出进程内存的状况,可以用来分析线程堆栈:

 pmap PID

大家都熟悉Linux下可以通过top命令来查看所有进程的内存,CPU等信息。除此之外,还有其他一些命令,可以得到更详细的信息,例如进程相关

cat /proc/your_PID/status  

通过top或ps -ef | grep '进程名' 得到进程的PID。该命令可以提供进程状态、文件句柄数、内存使用情况等信息。
内存相关
    vmstat -s -S M  
该可以查看包含内存每个项目的报告,通过-S M或-S k可以指定查看的单位,默认为kb。结合watch命令就可以看到动态变化的报告了。

也可用  cat /proc/meminfo  

要看cpu的配置信息可用

cat /proc/cpuinfo  

它能显示诸如CPU核心数,时钟频率、CPU型号等信息。

要查看cpu波动情况的,尤其是多核机器上,可使用

mpstat -P ALL 10 

该命令可间隔10秒钟采样一次CPU的使用情况,每个核的情况都会显示出来,例如,每个核的idle情况等。
只需查看均值的,可用
    iostat -c 
IO相关
    iostat -P ALL  
该命令可查看所有设备使用率、读写字节数等信息。

另外,htop ,有时间可以用一下。

 

 

Linux查看物理CPU个数、核数、逻辑CPU个数

# 总核数 = 物理CPU个数 X 每颗物理CPU的核数

# 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数

 

# 查看物理CPU个数

cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l

 

# 查看每个物理CPU中core的个数(即核数)

cat /proc/cpuinfo| grep "cpu cores"| uniq

 

# 查看逻辑CPU的个数

cat /proc/cpuinfo| grep "processor"| wc -l

 

 查看CPU信息(型号)
cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326500626&siteId=291194637