Obtains the CPU usage trend chart of a single program.

Source: Internet
Author: User

This article positioning: CPU history data has been stored in the disk, waiting for visual analysis, there is no idea for now.
Previous ArticleArticle(Http://www.cnblogs.com/MikeZhang/archive/2012/02/01/cpuRatePythonTop.html) mentioned how to use
Python stores the result of the top command. This article describes how to save the result.

In python, we can use matplotlib to conveniently visualize data, for exampleCode:

 
1 ImportMatplotlib. pyplot as PLT
2
3List1 = [1, 2, 3]
4List2 = [4, 5, 9]
5PLT. Plot (list1, list2)
6PLT. Show ()

The execution result is as follows:

The above is just to pass two list data structures to the plot function, show will come out ...... Haha, it's very convenient!
This is used to obtain the CPU trend chart!
But the data we get is not that friendly now. For example, if I have a file (file.txt), the content is as follows:

1328503984 CPU (s): 0.0% us, 0.0% Sy, 0.0% Ni, 100.0% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% St
1328503997 CPU (s): 7.7% us, 7.7% Sy, 0.0% Ni, 76.9% ID, 0.0% wa, 0.0% hi, 7.7% Si, 0.0% St
1328504000 CPU (s): 0.0% us, 9.1% Sy, 0.0% Ni, 90.9% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% St
1328504012 CPU (s): 9.1% us, 0.0% Sy, 0.0% Ni, 90.9% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% St
1328504016 CPU (s): 8.3% us, 8.3% Sy, 0.0% Ni, 83.3% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% St
1328504019 CPU (s): 0.0% us, 0.0% Sy, 0.0% Ni, 100.0% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% St
1328504022 CPU (s): 0.0% us, 9.1% Sy, 0.0% Ni, 90.9% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% St
1328504228 CPU (s): 0.0% us, 0.0% Sy, 0.0% Ni, 100.0% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% St

The first column is the time column, and the sixth column is the idle value of the CPU.

We have to do some work to figure out the CPU usage trend from this set of data.

The following is the code. Here is an idea. You need to copy it back and modify it:

 1   # Coding: UTF-8  
2 '''
3 File: cpuusage. py
4 Author: Mike
5 E-mail: Mike_Zhang@live.com
6 '''
7 Import Matplotlib. pyplot as PLT
8 Import String
9
10 Def Getcpuinfdata (filename ):
11 Ret = {}
12 F = open (filename, " R " )
13 Linelist = f. readlines ()
14 For Line In Linelist:
15 TMP = line. Split ()
16 SZ = Len (TMP)
17 T_key = string. atoi (TMP [0]) # Get key
18 T_value = 100.001-string. atof (line. Split ( ' : ' ) [1]. Split ( ' , ' ) [3]. Split ( ' % ' ) [0]) # Get value
19 Print T_key, t_value
20 If Not Ret. has_key (t_key ):
21 RET [t_key] = []
22 RET [t_key]. append (t_value)
23 F. Close ()
24 Return RET
25
26 Retmap1 = getcpuinfdata ( " File.txt " )
27 # Generate CPU usage trend chart
28 List1 = retmap1.keys ()
29 List1.sort ()
30 List2 = []
31 For I In List1: list2.append (retmap1 [I])
32 PLT. Plot (list1, list2)
33 PLT. Show ()

Okay, that's all. I hope it will help you.

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.