Skip to content

Instantly share code, notes, and snippets.

@oleksiiBobko
Last active October 23, 2022 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleksiiBobko/02570c80e0740573441a5380baa162cd to your computer and use it in GitHub Desktop.
Save oleksiiBobko/02570c80e0740573441a5380baa162cd to your computer and use it in GitHub Desktop.
Capture CPU utilization per core
import psutil
import time
import matplotlib.pyplot as plt
def start():
cpu_cores = psutil.cpu_count()
time_range = [i for i in range(60)]
cores = []
for c in range(cpu_cores):
cores.append([])
for i in range(60):
cpus_percent = psutil.cpu_percent(percpu=True)
print(cpus_percent)
for c in range(cpu_cores):
cores[c].append(cpus_percent.pop(0))
time.sleep(1)
for c in range(cpu_cores):
core_name = 'Core' + str(c)
plt.plot(time_range, cores[c], label = core_name)
plt.xlabel('Time - sec')
plt.ylabel('Utilization - %')
plt.title('CPU Utilization')
plt.legend()
plt.show()
if __name__ == '__main__':
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment