2020-01-17 12:23:21 -07:00
|
|
|
#!/usr/bin/env python3
|
2020-02-13 16:07:03 -07:00
|
|
|
import argparse
|
|
|
|
|
2020-01-17 12:23:21 -07:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
from cereal.messaging import SubMaster
|
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
|
2020-01-17 12:23:21 -07:00
|
|
|
def cputime_total(ct):
|
2020-02-13 16:07:03 -07:00
|
|
|
return ct.user + ct.nice + ct.system + ct.idle + ct.iowait + ct.irq + ct.softirq
|
2020-01-17 12:23:21 -07:00
|
|
|
|
|
|
|
|
|
|
|
def cputime_busy(ct):
|
2020-02-13 16:07:03 -07:00
|
|
|
return ct.user + ct.nice + ct.system + ct.irq + ct.softirq
|
2020-01-17 12:23:21 -07:00
|
|
|
|
|
|
|
|
2020-03-12 17:27:52 -06:00
|
|
|
def proc_cputime_total(ct):
|
|
|
|
return ct.cpuUser + ct.cpuSystem + ct.cpuChildrenUser + ct.cpuChildrenSystem
|
|
|
|
|
|
|
|
|
|
|
|
def proc_name(proc):
|
|
|
|
name = proc.name
|
|
|
|
if len(proc.cmdline):
|
|
|
|
name = proc.cmdline[0]
|
|
|
|
if len(proc.exe):
|
|
|
|
name = proc.exe + " - " + name
|
|
|
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--mem', action='store_true')
|
2020-03-12 17:27:52 -06:00
|
|
|
parser.add_argument('--cpu', action='store_true')
|
2020-02-13 16:07:03 -07:00
|
|
|
args = parser.parse_args()
|
2020-01-17 12:23:21 -07:00
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
sm = SubMaster(['thermal', 'procLog'])
|
2020-01-17 12:23:21 -07:00
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
last_temp = 0.0
|
|
|
|
last_mem = 0.0
|
|
|
|
total_times = [0., 0., 0., 0.]
|
|
|
|
busy_times = [0., 0., 0.0, 0.]
|
2020-01-17 12:23:21 -07:00
|
|
|
|
2020-03-12 17:27:52 -06:00
|
|
|
prev_proclog = None
|
|
|
|
prev_proclog_t = None
|
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
while True:
|
|
|
|
sm.update()
|
2020-01-17 12:23:21 -07:00
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
if sm.updated['thermal']:
|
|
|
|
t = sm['thermal']
|
2020-10-19 05:44:36 -06:00
|
|
|
last_temp = np.mean(t.cpu)
|
2020-02-13 16:07:03 -07:00
|
|
|
last_mem = t.memUsedPercent
|
2020-01-17 12:23:21 -07:00
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
if sm.updated['procLog']:
|
|
|
|
m = sm['procLog']
|
2020-01-17 12:23:21 -07:00
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
cores = [0., 0., 0., 0.]
|
|
|
|
total_times_new = [0., 0., 0., 0.]
|
|
|
|
busy_times_new = [0., 0., 0.0, 0.]
|
2020-01-30 15:17:09 -07:00
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
for c in m.cpuTimes:
|
|
|
|
n = c.cpuNum
|
|
|
|
total_times_new[n] = cputime_total(c)
|
|
|
|
busy_times_new[n] = cputime_busy(c)
|
2020-01-17 12:23:21 -07:00
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
for n in range(4):
|
|
|
|
t_busy = busy_times_new[n] - busy_times[n]
|
|
|
|
t_total = total_times_new[n] - total_times[n]
|
|
|
|
cores[n] = t_busy / t_total
|
2020-01-17 12:23:21 -07:00
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
total_times = total_times_new[:]
|
|
|
|
busy_times = busy_times_new[:]
|
2020-01-17 12:23:21 -07:00
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
print("CPU %.2f%% - RAM: %.2f - Temp %.2f" % (100. * np.mean(cores), last_mem, last_temp))
|
2020-01-17 12:23:21 -07:00
|
|
|
|
2020-03-12 17:27:52 -06:00
|
|
|
if args.cpu and prev_proclog is not None:
|
|
|
|
procs = {}
|
|
|
|
dt = (sm.logMonoTime['procLog'] - prev_proclog_t) / 1e9
|
|
|
|
for proc in m.procs:
|
|
|
|
try:
|
|
|
|
name = proc_name(proc)
|
|
|
|
prev_proc = [p for p in prev_proclog.procs if proc.pid == p.pid][0]
|
|
|
|
cpu_time = proc_cputime_total(proc) - proc_cputime_total(prev_proc)
|
|
|
|
cpu_usage = cpu_time / dt * 100.
|
|
|
|
procs[name] = cpu_usage
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
print("Top CPU usage:")
|
|
|
|
for k, v in sorted(procs.items(), key=lambda item: item[1], reverse=True)[:10]:
|
|
|
|
print(f"{k.rjust(70)} {v:.2f} %")
|
|
|
|
print()
|
|
|
|
|
2020-02-13 16:07:03 -07:00
|
|
|
if args.mem:
|
|
|
|
mems = {}
|
|
|
|
for proc in m.procs:
|
2020-03-12 17:27:52 -06:00
|
|
|
name = proc_name(proc)
|
2020-02-13 16:07:03 -07:00
|
|
|
mems[name] = float(proc.memRss) / 1e6
|
|
|
|
print("Top memory usage:")
|
|
|
|
for k, v in sorted(mems.items(), key=lambda item: item[1], reverse=True)[:10]:
|
|
|
|
print(f"{k.rjust(70)} {v:.2f} MB")
|
|
|
|
print()
|
2020-03-12 17:27:52 -06:00
|
|
|
|
|
|
|
prev_proclog = m
|
|
|
|
prev_proclog_t = sm.logMonoTime['procLog']
|