用python查看树莓的CPU使用率

http://www.blogjava.net/fjzag/articles/317773.html

http://blog.csdn.net/a6225301/article/details/47092707

http://www.iplaypy.com/code/linux/l2546.html

——————————————————————————————————————————参考上面网址

查看系统cpu状态的文件,高亮的地方是这次需要解析的地方,在/proc/stat中。

cpu 3418 0 2624 2056421 16739 0 86 0 0 0
cpu0 1181 0 1075 507742 7763 0 83 0 0 0
cpu1 592 0 501 516431 2959 0 0 0 0 0
cpu2 879 0 547 517329 1667 0 2 0 0 0
cpu3 766 0 501 514919 4350 0 1 0 0 0
intr 2251555 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7112 14 0 0 1 0 0 0 0 87 0 0 1700303 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 108 2 0 0 0 0 0 0 0 0 1 0 4160 0 0 0 0 5137 4961 2349 0 0 0 0 0 0 0 0 0 0 0 0 353286 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 1120284
btime 1493299970
processes 4888
procs_running 1
procs_blocked 0
softirq 469944 169942 81135 4 10524 0 0 88585 78777 215 40762

——————————————————————————————————————————————————

/proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。

它以文件系统的方式为内核与进程提供通信的接口。

用户和应用程序可以通过/proc得到系统的信息,并可以改变内核的某些参数。

由于系统的信息,如进程,是动态改变的,所以用户或应用程序读取/proc目录中的文件时,proc文件系统是动态从系统内核读出所需信息并提交的。

stat文件中有CPU所有活动的时间信息,通过解析时间来计算得出CPU占有率。

高亮部分的和是整个cpu时间,分为不同的几部分:

user ,nice ,system,idle ,iowait ,irq ,softirq ,stealstolen,guest(还有一个不知道。。)具体的可以参考第一个网址。

 cpu usage=[(user_2 +sys_2+nice_2) - (user_1 + sys_1+nice_1)]/(total_2 - total_1)*100

 在一个比较短的时间里读出stat消息两次,每次读出计算total时间,把user,nice,system时间和计算出,分别对应做差,相除。就得出了CPU‘s usage。

  但是为了验证重要性,我使用了库psutil  。

  当然,在使用第三方库的时候遇到了一点问题,就是怎么安装库。进过查询了之后,pip install XXX是比较简单的安装方式,依赖关系都是不用操心的。

  首先安装sudo apt-get python-dav,然后sudo apt-get install pip,pip install psutil

  不然会提示gcc什么的错误。。。

  使用psutil.cpu_percent()。就可以查看cpu使用率,结果是几乎差不多的

 

 1 #!/usr/bin/env python
 2 # coding=utf-8
 3 import time
 4 import psutil
 5 def DEAD_cpu_usage():
 6     try :
 7         fd = open("/proc/stat",'r')
 8         line = fd.readline()
 9     finally:
10         if fd:
11         fd.close()
12 
13 if line.startswith("cpu"):
14     data = line.split()
15     return data
16 return []
17 
18 def caculateCUP_usage():
19      information = DEAD_cpu_usage()
20 
21      if not information:
22      return 0.0
23 
24      sum1 = 0.0
25      for each_item in information:
26          if each_item == 'cpu':
27          continue
28      sum1 += long(each_item)
29      user1 = long(information[1])+long(information[2])+long(information[3])
30 
31      time.sleep(0.5)
32      information = DEAD_cpu_usage()
33 
34      if not information:
35           return 0.0
36 
37     sum2 = 0.0
38     for each_item in information:
39          if each_item == 'cpu':
40               continue
41     sum2 += long(each_item)
42     user2 = long(information[1])+long(information[2])+long(information[3]) 
43 
44     cacu = (user2 - user1) / (sum2 - sum1) *100
45 
46 return cacu
47 
48 aa = caculateCUP_usage()
49 print aa
50 print (str)(psutil.cpu_percent(0))
51 print (str)(psutil.virtual_memory().percent)
posted @ 2017-04-27 23:37  AAAron  阅读(526)  评论(0编辑  收藏  举报