Obtain system performance data in Linux
Obtain the CPU usage of system performance data in Linux
The/proc/stat file contains the system cpu usage. The CPU consumption in the 2-8 columns indicates user consumption and nice consumption) kernel consumption (sys), idle, iowait, hardirq, and softirq ). (Man proc)
$ cat /proc/statcpu 2339504870 2641995 716723851 70316975284 7219566 328971 304057999 0 0cpu0 211214357 275710 57674236 2778331292 3688175 10421 12279511 0 0cpu1 140100049 278788 47142039 2857686940 180452 32028 14960093 0 0...
To calculate t1 ~ Cpu usage of the system during t2. cpu usage information must be collected at T1.
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 + sortirqt2_used = t2(user + nice + sys + iowait + hardirq + sortirq)cpu_usage = (t2_used - t1_used) / (t2_all - t1_all)
Process CPU utilization
The/proc/$ pid/stat file contains the cpu usage information of a process. columns 14, 15, 16, and 17 correspond to the user consumption (user) and kernel consumption (sys) of the process) user-state waiting sub-process consumption (user_child), kernel-state waiting sub-process consumption (sys_child ).
$cat /proc/24076/stat24076 (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
Collect/proc/stat,/proc/$ pid/stat at t1 and t2 respectively to obtain the total cpu consumption and cpu consumption of processes.
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)
Memory usage
The/proc/meminfo file contains the system memory usage information.
$ cat /proc/meminfoMemTotal: 198450624 kBMemFree: 184950332 kBmem_usage = 1 - MemFree / MemTotal
System load
The first three columns of/proc/uptime contain the average load of the last 1 min, 5 min, and 15 min systems (number of processes running or waiting for IO, process state is R or D)
$cat /proc/loadavg0.02 0.09 0.08 1/2362 855
Nic Traffic Information
/Proc/net/dev contains the traffic information of all NICs. The 1st, 2, 10, and 11 columns indicate the number of currently accepted bytes (recv_bytes) and number of accepted packets (recv_packets) of the NICS) the number of sent bytes (send_bytes) and the number of sent bytes (send_packets ).
$cat /proc/net/devInter-| 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 ~ The average traffic during t2 is
avg_recv_bytes = (t2_recv_bytes - t1_recv_bytes) / (t2 - t1)avg_send_bytes = (t2_send_bytes - t1_send_bytes) / (t2 - t1)
Disk (File System) Space Information
You can call the statfs system to obtain the space usage of the current file system.
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
Disk read/write information
/Proc/diskstats contains the disk IO information. Column 4-9 shows the current disk read count (read), disk read slice (read_sec), and disk read Millisecond (read_ms) disk write count (write), number of disk write sectors (write_sec), and number of disk write milliseconds (write_ms ).
$cat /proc/diskstats8 4 sda4 3 0 12 8 0 0 0 0 0 8 88 5 sda5 87928 55284 2842138 17349 1230958 7932106 73380432 4931382 0 503343 49475048 16 sdb 393 35 3418 37 17 2 152 0 0 37 378 32 sdc 788 114 10690 10132 14550036 226568635 1929026928 623148463 0 4917530 623154797
T1 ~ The average disk reads/writes, slice count, and I/O utilization are calculated as follows:
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 ~ Percentage of I/O time in T2)
This article permanently updates the link address: