在Linux中,计算机的所有数据的计算都通过读取分析 /proc 文件系统来实现,Linux内核会实时把硬件的状况更新到 /proc 这个内存文件系统中。我们来基于读取/proc目录里的各种文件来分析系统性能情况。
cat /proc/stat
得到的结果,从左至右依次是user, nice, system, idle, iowait, irq, softirq, steal, guest, guestnice
cpu 193 0 519 206557 57 0 1 0 0
cpu0 193 0 519 206557 57 0 1 0 0
intr 24347 157 425 0 0 0 0 0 0 1 0 0 0 474 0 0 118 0 2884 3533 0 ...
ctxt 34465
btime 1587177917
processes 1151
procs_running 1
procs_blocked 0
softirq 43254 0 16131 413 3533 2943 0 408 0 16 19810
计算CPU占有率的代码如下 -- 主要是字符串的分隔,拼接,提取等
#!/usr/bin/python
# coding=utf8
import time
def get_cpu_info():
"""cpu的时间"""
f = open('/proc/stat')
info = f.readlines()[0]
f.close()
cpu_list = info.split(' ')
work_time = int(cpu_list[2]) + int(cpu_list[3]) + int(cpu_list[4])
total_time = int(cpu_list[2]) + int(cpu_list[3]) + int(cpu_list[4]) + int(cpu_list[5]) + int(cpu_list[6]) + int(cpu_list[7]) + int(cpu_list[8]) + int(cpu_list[9])
return work_time, total_time
def get_cpu_percent():
"""cpu的占有率"""
prev_work_time = get_cpu_info()[0]
prev_total_time = get_cpu_info()[1]
time.sleep(2)
current_work_time = get_cpu_info()[0]
current_total_time = get_cpu_info()[1]
cpu_percent = (current_work_time - prev_work_time) * 100 / (current_total_time - prev_total_time)
return cpu_percent
2. 内存: 内存的信息在/proc/meminfo文件中
cat /proc/meminfo
得到的信息如下
MemTotal: 1004412 kB
MemFree: 846368 kB
Buffers: 8164 kB
Cached: 41912 kB
SwapCached: 0 kB
Active: 36604 kB
Inactive: 37920 kB
Active(anon): 24456 kB
Inactive(anon): 148 kB
Active(file): 12148 kB
Inactive(file): 37772 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 2031608 kB
SwapFree: 2031608 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 24476 kB
Mapped: 8572 kB
Shmem: 160 kB
Slab: 61388 kB
SReclaimable: 10024 kB
SUnreclaim: 51364 kB
KernelStack: 608 kB
PageTables: 1592 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 2533812 kB
Committed_AS: 74748 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 153444 kB
VmallocChunk: 34359572340 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 6144 kB
DirectMap2M: 1042432 kB
DirectMap1G: 0 kB
计算内存的占有
#!/usr/bin/python
# coding=utf8
def get_mem_percent():
f = open('/proc/meminfo')
mem_info = f.readlines()
f.close()
mem_total = int(mem_info[0].strip().split(':')[1].strip().split(' ')[0])
mem_availble = int(mem_info[2].strip().split(':')[1].strip().split(' ')[0])
mem_percent = (mem_total - mem_availble) * 100 / mem_total
return mem_percent
def get_swap_percent():
f = open('/proc/meminfo')
mem_info = f.readlines()
for i in mem_info:
if i.startswith('SwapTotal'):
swap_total = int(i.strip().split(':')[1].strip().split(' ')[0])
elif i.startswith('SwapFree'):
swap_free = int(i.strip().split(':')[1].strip().split(' ')[0])
swap_percent = (swap_total - swap_free) * 100 / swap_total
return swap_percent
需要注意的是,当前使用的内存是用总量减可用内存,即 total-available 得到的
因为 used 的值不包括一些被内核占用并且永不释放的缓存内存
3. 网络:网络数据保存在/proc/net/dev文件中
cat /proc/net/dev
得到如下的信息
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
enp0s25: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
lo: 65691844 116033 0 0 0 0 0 0 65691844 116033 0 0 0 0 0 0
wlp4s0: 1249471414 907278 0 0 0 0 0 0 88564991 428056 0 0 0 0 0 0
我们要计算下载和上传的速度,需要用到第一个值和第九个值,分别是下载的总byte数、上传的总byte数
这里我计算了两秒内的平均速度。
#!/usr/bin/python
# coding=utf8
import time
def get_net_bytes():
f = open('/proc/net/dev')
net_info = f.readlines()
f.close()
download_bytes = 0
upload_bytes = 0
for i in net_info[2:]:
if not i.strip().startswith('lo'):
num_list = []
for j in i.strip().split(' '):
if j.isdigit():
num_list.append(j)
download_bytes += int(num_list[0])
upload_bytes += int(num_list[8])
return download_bytes, upload_bytes
def get_net_io():
prev_download_bytes = get_net_bytes()[0]
prev_upload_bytes = get_net_bytes()[1]
time.sleep(2)
current_download_bytes = get_net_bytes()[0]
current_upload_bytes = get_net_bytes()[1]
download_speed = (current_download_bytes - prev_download_bytes) / 1024 / 2
upload_speed = (current_upload_bytes - prev_upload_bytes) / 1024 / 2
return download_speed, upload_speed