View Raspberry CPU Usage in Python

Source: Internet
Author: User

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

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

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

—————————————————————————————————————————— refer to the above URL

Feel like using Python as if you were reviewing liunx operating system knowledge. Python is like a big python that controls everything. ~python as a script must be controlled and understood by the object.

CPU 3418 0 2624 2056421 16739 0 0 0 0
cpu0 1181 0 1075 507742 7763 0 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
ctxt 1120284
btime 1493299970
Processes 4888
procs_running 1
procs_blocked 0
SOFTIRQ 469944 169942 81135 4 10524 0 0 88585 78777 215 40762

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

The highlight is the place to be resolved, in the/proc/stat.

The/proc file system is a pseudo-file system that only exists in memory and does not occupy external memory space. It provides the interface for the kernel to communicate with the process in a file system manner. Users and applications can get system information through/PROC, and can change certain parameters of the kernel. Because the information of the system, such as the process, is dynamically changed, so when a user or application reads a file from the/proc directory, theproc file system dynamically reads the required information from the system kernel and submits it.

stat file with time information for all CPU activity, Calculate CPU occupancy by parsing time. The total CPU time of the highlighted part is divided into different parts: user  , nice  , system, Span lang= "en-US" >idle  , iowait  , IRQ  , softirq  , stealstolen, guest (there is also one that does not know: Specifically, you can refer to the first URL.

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

In a relatively short period of time to read the stat message two times, each read out to calculate total time, the user,nice,system time and calculated, respectively, to do the difference, divide. The CPU ' s usage is drawn.

But to verify the importance, I used the library psutil.

Of course, when using a third-party library, there is a problem, that is, how to install the library. After the query, pip install XXX is a relatively simple installation method, the dependency relationship is not to worry about.

first install sudo apt-get python-dav, then sudo apt-get install Pip,pip install Psutil

  Otherwise it will prompt gcc what's wrong ...

Use Psutil.cpu_percent (). You can see the CPU usage, the result is almost the same

#!/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)

The internet is too bad, even the code Editor can not be adjusted

View Raspberry CPU Usage in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.