Last active
October 23, 2022 20:37
-
-
Save oleksiiBobko/02570c80e0740573441a5380baa162cd to your computer and use it in GitHub Desktop.
Capture CPU utilization per core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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