-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathcpu_distribution.py
executable file
·106 lines (87 loc) · 3.82 KB
/
cpu_distribution.py
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
#!/usr/bin/env python3
# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Shows CPU workload split across different CPUs.
$ python3 scripts/cpu_workload.py
CPU 0 CPU 1 CPU 2 CPU 3 CPU 4 CPU 5 CPU 6 CPU 7
19.8 20.6 18.2 15.8 6.9 17.3 5.0 20.4
gvfsd pytho kwork chrom unity kwork kwork kwork
chrom chrom indic ibus- whoop nfsd (sd-p gvfsd
ibus- cat at-sp chrom Modem nfsd4 light upsta
ibus- iprt- ibus- nacl_ cfg80 kwork nfsd bluet
chrom irqba gpg-a chrom ext4- biose nfsd dio/n
chrom acpid bamfd nvidi kwork scsi_ sshd rpc.m
upsta rsysl dbus- nfsd biose scsi_ ext4- polki
rtkit avahi upowe Netwo scsi_ biose UVM T irq/9
light rpcbi snapd cron ipv6_ biose kwork dbus-
agett kvm-i avahi kwork biose biose scsi_ syste
nfsd syste rpc.i biose biose kbloc kthro UVM g
nfsd kwork kwork biose vmsta kwork crypt kaudi
nfsd scsi_ charg biose md ksoft kwork kwork
memca biose ksmd ecryp ksoft watch migra nvme
therm biose kcomp kswap migra cpuhp watch biose
syste biose kdevt khuge watch cpuhp biose
led_w devfr kwork write cpuhp biose
rpcio oom_r ksoft kwork syste biose
kwork kwork watch migra acpi_
biose ksoft cpuhp watch watch
biose migra cpuhp kinte
biose watch rcu_s netns
biose cpuhp kthre kwork
cpuhp ksoft
watch migra
rcu_b cpuhp
kwork
"""
import collections
import os
import shutil
import sys
import time
import psutil
if not hasattr(psutil.Process, "cpu_num"):
sys.exit("platform not supported")
def clean_screen():
if psutil.POSIX:
os.system('clear')
else:
os.system('cls')
def main():
num_cpus = psutil.cpu_count()
if num_cpus > 8:
num_cpus = 8 # try to fit into screen
cpus_hidden = True
else:
cpus_hidden = False
while True:
# header
clean_screen()
cpus_percent = psutil.cpu_percent(percpu=True)
for i in range(num_cpus):
print("CPU {:<6}".format(i), end="")
if cpus_hidden:
print(" (+ hidden)", end="")
print()
for _ in range(num_cpus):
print("{:<10}".format(cpus_percent.pop(0)), end="")
print()
# processes
procs = collections.defaultdict(list)
for p in psutil.process_iter(['name', 'cpu_num']):
procs[p.info['cpu_num']].append(p.info['name'][:5])
curr_line = 3
while True:
for num in range(num_cpus):
try:
pname = procs[num].pop()
except IndexError:
pname = ""
print("{:<10}".format(pname[:10]), end="")
print()
curr_line += 1
if curr_line >= shutil.get_terminal_size()[1]:
break
time.sleep(1)
if __name__ == '__main__':
main()