-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphcpu.py
More file actions
85 lines (62 loc) · 1.58 KB
/
graphcpu.py
File metadata and controls
85 lines (62 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python
import gtk
import gobject
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtkagg \
import FigureCanvasGTKAgg as FigureCanvas
import time
import psutil as p
def prepare_cpu_usage():
t = p.cpu_times()
if hasattr(t, "nice"):
return[t.user, t.nice, t.system, t.idle]
else:
return [t.user, 0, t.system, t.idle]
def get_cpu_usage():
global before
now = prepare_cpu_usage()
delta = [now[i]-before[i] for i in range(len(now))]
total = sum(delta)
before = now
return [(100.0*dt)/total for dt in delta]
def update_draw(*args):
global i
result = get_cpu_usage()
user.append(result[0])
nice.append(result[1])
sys.append(result[2])
idle.append(result[3])
l_user.set_data(range(len(user)), user)
l_nice.set_data(range(len(nice)), nice)
l_sys.set_data(range(len(sys)), sys)
l_idle.set_data(range(len(idle)), idle)
fig.canvas.draw()
i += 1
if i > 30:
return False
else:
time.sleep(1)
return True
i = 0
before = prepare_cpu_usage()
win = gtk.Window()
win.connect("destroy", gtk.main_quit)
win.set_default_size(600, 400)
win.set_title("30 Seconds of CPU Usage Updated in real-time")
fig = Figure()
ax = fig.add_subplot(111)
ax.set_xlim(0, 30)
ax.set_ylim([0, 100])
ax.set_autoscale_on(False)
user, nice, sys, idle, = [], [], [], []
l_user, = ax.plot ([], user, label="User %")
l_nice, = ax.plot ([], nice, label="Nice %")
l_sys, = ax.plot([], sys, label="Sys %")
l_idle, = ax.plot([], idle, label="Idle %")
ax.legend()
canvas = FigureCanvas(fig)
win.add(canvas)
update_draw()
gobject.idle_add(update_draw)
win.show_all()
gtk.main()