Repository navigation
Expand file tree
/
Copy pathFl_gnuplotlib_demo.py
More file actions
executable file
·116 lines (89 loc) · 2.94 KB
/
Copy pathFl_gnuplotlib_demo.py
File metadata and controls
executable file
·116 lines (89 loc) · 2.94 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python3
import sys
import os
import re
import numpy as np
import numpysane as nps
from fltk import *
from Fl_gnuplotlib import *
W_WINDOW = 1200
H_WINDOW = 600
# Layout: top row has conchoids plot + controls, bottom row has cpu
W_HALF = W_WINDOW // 2
H_HALF = H_WINDOW // 2
H_CONTROLS = 30
H_CONTROLS_MARGIN = 10
N_HISTORY = 60 # seconds of history
def plot_conchoids(*args):
N = int(Nconchoids.value())
rho = np.linspace(0, 2*np.pi, 100)
a = nps.dummy(np.arange(-N//2,N//2), -1)
conchoids.plot( rho,
1./np.cos(rho) + a*np.cos(rho),
_with = f'lines lw {int(thickness.value())}',
set = 'polar',
square = True,
xrange = [-5,5],
yrange = [-5,5] )
cpu_usage_0 = None
def read_cpu():
'''Read per-CPU usage from /proc/stat. Returns fractional usage [0,1] per CPU.'''
global cpu_usage_0
cpu_usage = []
with open('/proc/stat') as f:
for line in f:
if re.match(r'cpu[0-9]',line) is None: continue
fields = [int(x) for x in line.split()[1:]]
# fields: user nice system idle iowait irq softirq ...
idle = fields[3] + fields[4] # idle + iowait
total = sum(fields)
cpu_usage.append((idle, total))
cpu_usage = np.array(cpu_usage)
if cpu_usage_0 is None:
ret = None
else:
ret = cpu_usage - cpu_usage_0
cpu_usage_0 = cpu_usage
return ret
read_cpu() # Read the initial state, to base off of
cpu_history = np.zeros((N_HISTORY, len(cpu_usage_0))) * np.nan
def update_cpu(*args):
global cpu_usage_0
(d_idle, d_total) = read_cpu().T
usage = 1.0 - d_idle / d_total
cpu_history[:-1] = cpu_history[1:]
cpu_history[-1] = usage
cpu_plot.plot( cpu_history.T,
_with = 'lines lw 2',
legend = np.array([f"cpu{i}" for i in range(cpu_history.shape[-1])]),
yrange = (-0.1, 1.1),
ylabel = 'Usage',
title = 'CPU Usage' )
Fl.repeat_timeout(1.0, update_cpu)
window = Fl_Window(W_WINDOW, H_WINDOW, "plot demo")
conchoids = Fl_Gnuplotlib_Window(0, 0, W_HALF, H_HALF)
y = 0
Nconchoids = Fl_Hor_Value_Slider(W_HALF, y, W_HALF, H_CONTROLS,
"Number")
Nconchoids.precision(0)
Nconchoids.step(2)
Nconchoids.bounds(2,10)
Nconchoids.type(FL_HORIZONTAL)
Nconchoids.value(6)
Nconchoids.callback(plot_conchoids)
y += H_CONTROLS + H_CONTROLS_MARGIN
thickness = Fl_Hor_Value_Slider(W_HALF, y, W_HALF, H_CONTROLS,
"Thickness")
thickness.precision(0)
thickness.step(1)
thickness.bounds(1,4)
thickness.type(FL_HORIZONTAL)
thickness.value(1)
thickness.callback(plot_conchoids)
cpu_plot = Fl_Gnuplotlib_Window(0, H_HALF, W_WINDOW, H_HALF)
window.resizable(window)
window.end()
window.show()
plot_conchoids()
Fl.add_timeout(1.0, update_cpu)
Fl.run()