歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux環境獲取系統性能數據

Linux環境獲取系統性能數據

日期:2017/2/28 14:01:42   编辑:Linux教程

Linux環境獲取系統性能數據

CPU利用率

/proc/stat文件裡包含系統cpu使用情況,2-8列對應的CPU消耗含義是用戶態消耗(user)、用戶態nice消耗(nice)、內核態消耗(sys)、空閒時間(idle)、等待IO消耗(iowait)、硬件中斷消耗(hardirq)、軟中斷消耗(softirq)。(man proc)

$ 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
...

要計算t1~t2時間內系統的cpu利用率,需要在t1、t2時刻分別采集cpu使用信息

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)

進程CPU利用率

/proc/$pid/stat文件裡包含某個進程的cpu使用信息,14、15、16、17列對應進程用戶態消耗(user)、內核態消耗(sys)、用戶態等待子進程的消耗(user_child)、內核態等待子進程的消耗(sys_child)。

$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

t1、t2時刻分別采集/proc/stat、/proc/$pid/stat,獲取總的cpu消耗,以及進程的cpu消耗

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)

內存利用率

/proc/meminfo文件裡包含系統內存使用信息

$ cat /proc/meminfo
MemTotal:       198450624 kB
MemFree:        184950332 kB

mem_usage = 1 - MemFree / MemTotal

系統load

/proc/uptime的前3列包含了最近1min、5min、15min系統的平均load(正在運行或等待IO的進程數,進程state為R或D)

$cat /proc/loadavg
0.02 0.09 0.08 1/2362 855

網卡流量信息

/proc/net/dev包含所有網卡的流量信息,第1、2、10、11列分別代表網卡當前接受字節數(recv_bytes)、接受包個數(recv_packets)、發送字節數(send_bytes)、發送個數(send_packets)。

$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

t1~t2時間內的平均流量為

avg_recv_bytes = (t2_recv_bytes - t1_recv_bytes) / (t2 - t1)
avg_send_bytes = (t2_send_bytes - t1_send_bytes) / (t2 - t1)

磁盤(文件系統)空間信息

通過statfs系統調用可獲取當前文件系統的空間占用情況

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

磁盤讀寫信息

/proc/diskstats包含磁盤的IO信息,第4-9列分別為當前磁盤讀次數(read),磁盤讀扇區數(read_sec)、磁盤讀毫秒數(read_ms)、磁盤寫次數(write)、磁盤寫扇區數(write_sec)、磁盤寫毫秒數(write_ms)。

$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

t1~t2時間內磁盤平均讀寫次數、扇區數、IO利用率計算如下

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_read_ms + t2_write_ms - t1_read_ms - t1_write_ms) / (t2 - t1) (t1~t2時間段內IO的時間百分比)
Copyright © Linux教程網 All Rights Reserved