Created
June 3, 2023 23:42
-
-
Save salihmarangoz/79aa41392f4786404ffe5c06a410e272 to your computer and use it in GitHub Desktop.
CPU/Memory Monitoring (Python)
This file contains 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 numpy as np | |
import matplotlib.pyplot as plt | |
import psutil | |
import time | |
import datetime | |
import scipy | |
##### PARAMETERS ################### | |
SAMPLE_RATE = 5 # hz | |
SMOOTHING = 0.01 | |
SAVE_AS_CSV = True | |
SAVE_AS_PNG = True | |
SAVE_AS_SVG = True | |
SHOW_FIGURE = True | |
#################################### | |
byte_to_gb = 1/1024/1024/1024 | |
total_memory = psutil.virtual_memory()[0] * byte_to_gb | |
start_time = time.time() | |
filename = "cpu_mem_monitor_" + datetime.datetime.now().strftime("%Y_%m_%d-%H_%M_%S") | |
print("Press Ctrl+C to stop monitoring...") | |
data_x = [] | |
data_mem = [] | |
data_cpu = [] | |
try: | |
while True: | |
data_x.append( time.time() - start_time ) | |
data_mem.append( psutil.virtual_memory()[3] * byte_to_gb ) | |
data_cpu.append( psutil.cpu_percent() ) | |
time.sleep(1/SAMPLE_RATE) | |
except KeyboardInterrupt: | |
print('interrupted!') | |
min_length = len(data_x) | |
if (len(data_mem) < min_length): | |
min_length = len(data_mem) | |
if (len(data_cpu) < min_length): | |
min_length = len(data_cpu) | |
data_x = data_x[:min_length] | |
data_mem = data_mem[:min_length] | |
data_cpu = data_cpu[:min_length] | |
# smooth data | |
if SMOOTHING>0: | |
sigma = len(data_x)*SMOOTHING | |
data_mem_smooth = scipy.ndimage.gaussian_filter1d(data_mem, sigma=sigma, mode="nearest") | |
data_cpu_smooth = scipy.ndimage.gaussian_filter1d(data_cpu, sigma=sigma, mode="nearest") | |
else: | |
data_mem_smooth = data_mem | |
data_cpu_smooth = data_cpu | |
fig, ax1 = plt.subplots(figsize=(12,8)) | |
color = 'tab:red' | |
ax1.set_xlabel('time (s)') | |
ax1.set_ylabel('Memory Usage (GiB)', color=color) | |
ax1.plot(data_x, data_mem, ".", color=color, alpha=0.25) | |
ax1.plot(data_x, data_mem_smooth, "-", color=color) | |
ax1.tick_params(axis='y', labelcolor=color) | |
ax2 = ax1.twinx() | |
color = 'tab:blue' | |
ax2.set_ylabel('CPU Usage (Percent)', color=color) | |
ax2.plot(data_x, data_cpu, ".", color=color, alpha=0.25) | |
ax2.plot(data_x, data_cpu_smooth, "-", color=color) | |
ax2.tick_params(axis='y', labelcolor=color) | |
fig.tight_layout() | |
if SAVE_AS_CSV: | |
combined = np.array([data_x, data_mem, data_cpu]).T | |
np.savetxt(filename + ".csv", combined, fmt="%.5f", delimiter=",", header="Time (seconds), Memory Usage (GiB), CPU Usage (Percent)") | |
if SAVE_AS_PNG: | |
plt.savefig(filename + ".png") | |
if SAVE_AS_SVG: | |
plt.savefig(filename + ".svg") | |
if SHOW_FIGURE: | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example outputs: