live_cpu_and_temp.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python3
  2. import argparse
  3. import capnp
  4. from collections import defaultdict
  5. from cereal.messaging import SubMaster
  6. from common.numpy_fast import mean
  7. from typing import Optional, Dict
  8. def cputime_total(ct):
  9. return ct.user + ct.nice + ct.system + ct.idle + ct.iowait + ct.irq + ct.softirq
  10. def cputime_busy(ct):
  11. return ct.user + ct.nice + ct.system + ct.irq + ct.softirq
  12. def proc_cputime_total(ct):
  13. return ct.cpuUser + ct.cpuSystem + ct.cpuChildrenUser + ct.cpuChildrenSystem
  14. def proc_name(proc):
  15. name = proc.name
  16. if len(proc.cmdline):
  17. name = proc.cmdline[0]
  18. if len(proc.exe):
  19. name = proc.exe + " - " + name
  20. return name
  21. if __name__ == "__main__":
  22. parser = argparse.ArgumentParser()
  23. parser.add_argument('--mem', action='store_true')
  24. parser.add_argument('--cpu', action='store_true')
  25. args = parser.parse_args()
  26. sm = SubMaster(['deviceState', 'procLog'])
  27. last_temp = 0.0
  28. last_mem = 0.0
  29. total_times = [0.]*8
  30. busy_times = [0.]*8
  31. prev_proclog: Optional[capnp._DynamicStructReader] = None
  32. prev_proclog_t: Optional[int] = None
  33. while True:
  34. sm.update()
  35. if sm.updated['deviceState']:
  36. t = sm['deviceState']
  37. last_temp = mean(t.cpuTempC)
  38. last_mem = t.memoryUsagePercent
  39. if sm.updated['procLog']:
  40. m = sm['procLog']
  41. cores = [0.]*8
  42. total_times_new = [0.]*8
  43. busy_times_new = [0.]*8
  44. for c in m.cpuTimes:
  45. n = c.cpuNum
  46. total_times_new[n] = cputime_total(c)
  47. busy_times_new[n] = cputime_busy(c)
  48. for n in range(8):
  49. t_busy = busy_times_new[n] - busy_times[n]
  50. t_total = total_times_new[n] - total_times[n]
  51. cores[n] = t_busy / t_total
  52. total_times = total_times_new[:]
  53. busy_times = busy_times_new[:]
  54. print(f"CPU {100.0 * mean(cores):.2f}% - RAM: {last_mem:.2f}% - Temp {last_temp:.2f}C")
  55. if args.cpu and prev_proclog is not None and prev_proclog_t is not None:
  56. procs: Dict[str, float] = defaultdict(float)
  57. dt = (sm.logMonoTime['procLog'] - prev_proclog_t) / 1e9
  58. for proc in m.procs:
  59. try:
  60. name = proc_name(proc)
  61. prev_proc = [p for p in prev_proclog.procs if proc.pid == p.pid][0]
  62. cpu_time = proc_cputime_total(proc) - proc_cputime_total(prev_proc)
  63. cpu_usage = cpu_time / dt * 100.
  64. procs[name] += cpu_usage
  65. except IndexError:
  66. pass
  67. print("Top CPU usage:")
  68. for k, v in sorted(procs.items(), key=lambda item: item[1], reverse=True)[:10]:
  69. print(f"{k.rjust(70)} {v:.2f} %")
  70. print()
  71. if args.mem:
  72. mems = {}
  73. for proc in m.procs:
  74. name = proc_name(proc)
  75. mems[name] = float(proc.memRss) / 1e6
  76. print("Top memory usage:")
  77. for k, v in sorted(mems.items(), key=lambda item: item[1], reverse=True)[:10]:
  78. print(f"{k.rjust(70)} {v:.2f} MB")
  79. print()
  80. prev_proclog = m
  81. prev_proclog_t = sm.logMonoTime['procLog']