標籤:高亮 第三方庫 核心 知識 解析 proc檔案 eal 網址 依賴關係
http://www.blogjava.net/fjzag/articles/317773.html
http://blog.csdn.net/a6225301/article/details/47092707
http://www.iplaypy.com/code/linux/l2546.html
——————————————————————————————————————————參考上面網址
感覺使用python就像是在複習liunx作業系統知識一樣。python像是一頭大蟒蛇控制著一切~python作為指令碼必須對被控制對象及其瞭解。
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/stat中。
/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使用率,結果是幾乎差不多的
#!/usr/bin/env python
# coding=utf-8
import time
import psutil
def DEAD_cpu_usage():
try :
fd = open("/proc/stat",‘r‘)
line = fd.readline()
finally:
if fd:
fd.close()
if line.startswith("cpu"):
data = line.split()
return data
return []
def caculateCUP_usage():
information = DEAD_cpu_usage()
if not information:
return 0.0
sum1 = 0.0
for each_item in information:
if each_item == ‘cpu‘:
continue
sum1 += long(each_item)
user1 = long(information[1])+long(information[2])+long(information[3])
time.sleep(0.5)
information = DEAD_cpu_usage()
if not information:
return 0.0
sum2 = 0.0
for each_item in information:
if each_item == ‘cpu‘:
continue
sum2 += long(each_item)
user2 = long(information[1])+long(information[2])+long(information[3])
cacu = (user2 - user1) / (sum2 - sum1) *100
return cacu
aa = caculateCUP_usage()
print aa
print (str)(psutil.cpu_percent(0))
print (str)(psutil.virtual_memory().percent)
網速太差了,連代碼編輯器都調不出來
用python查看樹莓的CPU使用率