-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathps.py
More file actions
executable file
·66 lines (50 loc) · 1.49 KB
/
ps.py
File metadata and controls
executable file
·66 lines (50 loc) · 1.49 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
#!/usr/bin/env python
import subprocess
import time
import array
import itertools
import copy
symbols='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
orig_graphline = array.array('u', itertools.repeat(' ', 100))
graphfree = array.array('u', itertools.repeat('/', 100))
def scale(num):
mul = 1
while num * mul < 50:
mul *= 2
return mul
while True:
start = time.clock_gettime(time.CLOCK_MONOTONIC_RAW)
psproc = subprocess.run(("ps", "--no-headers", "-A", "-L", "-o", "psr,pcpu"),
stdout=subprocess.PIPE)
lines = psproc.stdout.splitlines()
cores = dict()
totalpercent = 0.0
for line in lines:
items = line.split(b' ')
core = -1
percent = -1.0
for item in items:
if item != b'':
if core < 0:
core = int(item)
else:
percent = float(item)
totalpercent += percent
break
if core not in cores:
cores[core] = 0.0
cores[core] += percent
maxcpu = len(cores)
totalpercent /= maxcpu
for item in cores:
cores[item] /= maxcpu
order = sorted(cores, key=lambda x: cores[x])
graphline = copy.copy(orig_graphline)
diff = 0
for item in range(max(order)):
mul = scale(totalpercent)
diff += int(cores[order[item]] * mul)
graphline[diff] = symbols[order[item]]
print(graphline[:diff+1].tounicode(), end='')
print("{}| {percent}%".format(graphfree[:100-(diff + 1)].tounicode(), percent=100/mul));
time.sleep(1 - (time.clock_gettime(time.CLOCK_MONOTONIC_RAW) - start))