换一个思路学Linux命令

关于top命令的使用手册及方法有很多,往往使用的时候我们只是看一眼,
不会就用man top查一下或者是google一下,这里对top的使用方法就不再赘述,
换一个方式,构造我们想要的top结果

虚拟机配置如下

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:    16.04
Codename:   xenial

go version go1.6.2 linux/amd64

<!-- 查看物理CPU个数 -->
➜  ~ cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
1

<!-- 查看每个物理CPU中core的个数(即核数) -->
➜  ~ cat /proc/cpuinfo| grep "cpu cores"| uniq
cpu cores   : 1


<!-- 查看逻辑CPU的个数 -->
➜  ~ cat /proc/cpuinfo| grep "processor"| wc -l
1

比如我们想构造一个比较耗CPU的场景,这里用golang程序作为示例

package main
import (
    "time"
)

func main(){
    for{
        time.Sleep(1)
    }
}

查看top结果

top - 22:01:26 up  2:15,  3 users,  load average: 0.22, 0.25, 0.12
Tasks: 106 total,   2 running, 104 sleeping,   0 stopped,   0 zombie
%Cpu(s): 99.3 us,  0.3 sy,  0.0 ni,  0.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1015976 total,   788612 free,    46544 used,   180820 buff/cache
KiB Swap:  1003516 total,   971808 free,    31708 used.   815324 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 6176 ofs       20   0    8348   5980    796 R 99.7  0.6   0:04.17 main
    1 root      20   0   37904   3672   2780 S  0.0  0.4   0:01.61 systemd
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd
    3 root      20   0       0      0      0 S  0.0  0.0   0:05.12 ksoftirqd/0

这里能看到golang程序的PID也就是进程ID为:6176,CPU占用率达99.7%之多,好可怕,这幸好是发生在虚拟机里面
这里的load average后面的三个数分别是1分钟、5分钟、15分钟的CPU的负载情况。我们的配置是单CPU单核,如果这个值是1,
那CPU就是满负荷运行了,我们这里看到的结果是top默认刷新频率是3s,load average是一个统计值。

接下来我们构造耗内存的场景

package main

import (
   "math"
)

func main(){
    for{
         go func(){
            _ =  make([]string, math.MaxInt32)
          }()
    }
}

查看top结果

➜  ~ top
op - 22:32:28 up  2:46,  3 users,  load average: 0.38, 0.24, 0.19
Tasks: 107 total,   3 running, 104 sleeping,   0 stopped,   0 zombie
%Cpu(s):  7.0 us, 66.9 sy,  0.0 ni,  0.0 id,  2.3 wa,  0.0 hi, 23.8 si,  0.0 st
KiB Mem :  1015976 total,    63904 free,   925284 used,    26788 buff/cache
KiB Swap:  1003516 total,      820 free,  1002696 used.    13208 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 9882 ofs       20   0 1861220 896372    204 R 37.1 88.2   0:02.04 main
   28 root      20   0       0      0      0 S 36.5  0.0   0:41.69 kswapd0
  128 root       0 -20       0      0      0 S  7.7  0.0   0:01.84 kworker/0:1H

内存只有1G,golang程序使用了88.2%之多,必须要考虑GC优化了

再来构造一个比较耗磁盘IO的场景,用openresty小试一把(参考openresty示例)

worker_processes 1;
error_log logs/error.log;
events {
    worker_connections 2014;
}
http {
    server {
        listen 8080;
        location /{
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
                ';
            }
        }

}

在宿主机发起并发测试

siege -T -c 500000 r10 'http://127.0.0.1:18080/'

这里在虚拟机的8080端口映射到宿主机的18080端口

扫描二维码关注公众号,回复: 5215070 查看本文章

使用iotop命令查看磁盘IO状况

sudo iotop


Total DISK READ :       0.00 B/s | Total DISK WRITE :      80.24 K/s
Actual DISK READ:       0.00 B/s | Actual DISK WRITE:       0.00 B/s
  TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND
28078 be/4 ofs         0.00 B/s   80.24 K/s  0.00 %  0.00 % nginx: worker process
    1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % init
    2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
    3 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/0]
    5 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/0:0H]
    7 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_sched]
    8 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_bh]
    9 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/0]
   10 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/0]
   11 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kdevtmpfs]
   12 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [netns]

能看到磁盘写入速度为: 80.24 K/s,可以看到这里主要是nginx程序战胜了磁盘IO,原因是因为nginx写入access.log的需要。

猜你喜欢

转载自blog.csdn.net/weixin_33953384/article/details/87424644