分享

Linux环境获取(cpu、内存、网卡流量等)系统性能数据

yuwenge 发表于 2015-6-22 16:45:47 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 2 45326
本帖最后由 yuwenge 于 2015-6-22 16:46 编辑
问题导读
1.如何查看CPU利用率?
2.进程CPU利用率如何查看?
3.内存利用率需要查看哪个文件?




CPU利用率
/proc/stat文件里包含系统cpu使用情况,2-8列对应的CPU消耗含义是用户态消耗(user)、用户态nice消耗(nice)、内核态消耗(sys)、空闲时间(idle)、等待IO消耗(iowait)、硬件中断消耗(hardirq)、软中断消耗(softirq)。(man proc)
[mw_shl_code=bash,true]$ cat /proc/stat
cpu  2339504870 2641995 716723851 70316975284 7219566 328971 304057999 0 0
cpu0 211214357 275710 57674236 2778331292 3688175 10421 12279511 0 0
cpu1 140100049 278788 47142039 2857686940 180452 32028 14960093 0 0
...[/mw_shl_code]
要计算t1~t2时间内系统的cpu利用率,需要在t1、t2时刻分别采集cpu使用信息
[mw_shl_code=bash,true]t1_all = t1(user + nice + sys + idle + iowait + hardirq + sortirq)
t1_used = t1(user + nice + sys + iowait + hardirq + sortirq)
t2_all = t2(user + nice + sys + idle + iowait + hardirq + sortirq
t2_used = t2(user + nice + sys + iowait + hardirq + sortirq)

cpu_usage = (t2_used - t1_used) / (t2_all - t1_all)[/mw_shl_code]

进程CPU利用率
/proc/$pid/stat文件里包含某个进程的cpu使用信息,14、15、16、17列对应进程用户态消耗(user)、内核态消耗(sys)、用户态等待子进程的消耗(user_child)、内核态等待子进程的消耗(sys_child)。
[mw_shl_code=bash,true]$cat /proc/24076/stat
24076 (redis-server) S 1 24076 24076 0 -1 4202560 5755 147 0 0 1864 3298 0 0 20 0 3 0 70238536 159555584 5145 18446744073709551615 1 1 0 0 0 0 0 4097 17610 18446744073709551615 0 0 17 4 0 0 0 0 0[/mw_shl_code]
t1、t2时刻分别采集/proc/stat、/proc/$pid/stat,获取总的cpu消耗,以及进程的cpu消耗
[mw_shl_code=bash,true]t1_all = t1(user + nice + sys + idle + iowait + hardirq + sortirq)
t1_pid = t1(user + sys + user_child + sys_child)
t2_all = t2(user + nice + sys + idle + iowait + hardirq + sortirq)
t2_pid = t2(user + sys + user_child + sys_child)

pid_cpu_usage = (t2_pid - t1_pid) / (t2_all - t1_all)[/mw_shl_code]

内存利用率
/proc/meminfo文件里包含系统内存使用信息
[mw_shl_code=bash,true]$ cat /proc/meminfo
MemTotal:       198450624 kB
MemFree:        184950332 kB

mem_usage = 1 - MemFree / MemTotal[/mw_shl_code]

系统load
/proc/uptime的前3列包含了最近1min、5min、15min系统的平均load(正在运行或等待IO的进程数,进程state为R或D)
[mw_shl_code=bash,true]$cat /proc/loadavg
0.02 0.09 0.08 1/2362 855[/mw_shl_code]

网卡流量信息
/proc/net/dev包含所有网卡的流量信息,第1、2、10、11列分别代表网卡当前接受字节数(recv_bytes)、接受包个数(recv_packets)、发送字节数(send_bytes)、发送个数(send_packets)。
[mw_shl_code=bash,true]$cat /proc/net/dev
Inter-|   Receive                                                |  Transmit
face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls     carrier compressed
lo:25047448261 37947893    0    0    0     0          0         0 25047448261 37947893    0    0    0     0       0          0
eth0:2566932970457 7620547348    0    0    2     0          0     62732 728662220296 6145797287    0    0    0     0       0          0
eth1:2023304550582 7268848253    0    0    0     0          0     77924 742443231982 6129090809    0    0    0     0       0          0[/mw_shl_code]
t1~t2时间内的平均流量为
[mw_shl_code=bash,true]avg_recv_bytes = (t2_recv_bytes - t1_recv_bytes) / (t2 - t1)
avg_send_bytes = (t2_send_bytes - t1_send_bytes) / (t2 - t1)[/mw_shl_code]

磁盘(文件系统)空间信息
通过statfs系统调用可获取当前文件系统的空间占用情况
[mw_shl_code=bash,true]struct statfs {
    long    f_type;     /* type of file system (see below) */
    long    f_bsize;    /* optimal transfer block size */
    long    f_blocks;   /* total data blocks in file system */
    long    f_bfree;    /* free blocks in fs */
    long    f_bavail;   /* free blocks avail to non-superuser */
    ...
};

disk_usage = 1 - f_bfree / f_blocks[/mw_shl_code]

磁盘读写信息
/proc/diskstats包含磁盘的IO信息,第4、6、8、10、13列分别为当前磁盘读次数(read),磁盘读扇区数(read_sec)、磁盘写次数(write)、磁盘写扇区数(write_sec)、磁盘IO毫秒数(io_ms)。
[mw_shl_code=bash,true]$cat /proc/diskstats
8       4 sda4 3 0 12 8 0 0 0 0 0 8 8
8       5 sda5 87928 55284 2842138 17349 1230958 7932106 73380432 4931382 0 503343 4947504
8      16 sdb 393 35 3418 37 17 2 152 0 0 37 37
8      32 sdc 788 114 10690 10132 14550036 226568635 1929026928 623148463 0 4917530 623154797[/mw_shl_code]
t1~t2时间内磁盘平均读写次数、扇区数、IO利用率计算如下
[mw_shl_code=bash,true]avg_read = (t2_read - t1_read) / (t2 - t1)
avg_read_sec = (t2_read_sec - t1_read_sec) / (t2 - t1)
avg_write = (t2_write - t1_write) / (t2 - t1)
avg_write_sec = (t2_write_sec - t1_write_sec) / (t2 - t1)
io_util = (t2_io_ms - t1_io_ms) / (t2 - t1) (t1~t2时间段内IO的时间百分比)[/mw_shl_code]




已有(2)人评论

跳转到指定楼层
sqlFocus 发表于 2015-6-23 08:34:52
基础性的好东东,谢谢分享
回复

使用道具 举报

levycui 发表于 2015-6-23 11:53:05
查看linux性能可以使用top、free -m、vmstat、iostat、sar等工具
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条