Skip to content

Instantly share code, notes, and snippets.

@salihmarangoz
Created June 3, 2023 23:42
Show Gist options
  • Save salihmarangoz/79aa41392f4786404ffe5c06a410e272 to your computer and use it in GitHub Desktop.
Save salihmarangoz/79aa41392f4786404ffe5c06a410e272 to your computer and use it in GitHub Desktop.
CPU/Memory Monitoring (Python)
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()
@salihmarangoz
Copy link
Author

Example outputs:

cpu_mem_monitor_2023_06_04-01_38_22

# Time (seconds), Memory Usage (GiB), CPU Usage (Percent)
0.00007,23.47302,0.00000
0.20067,23.47383,3.20000
0.40144,23.47383,3.80000
0.60216,23.47361,4.10000
0.80268,23.47361,3.80000
1.00343,23.47361,5.00000
1.20413,23.52803,4.30000
1.40470,23.47929,5.10000
1.60540,23.47963,4.40000
1.80616,23.47963,2.50000
2.00689,23.47963,13.10000
2.20744,23.47984,9.40000
2.40813,23.48064,9.40000
2.60877,23.48132,8.90000
2.80951,23.48119,4.40000
3.01029,23.48119,2.20000
3.21113,23.48503,3.80000
3.41195,23.49002,4.10000
3.61278,23.48085,3.80000
3.81354,23.47957,6.60000
4.01435,23.47982,3.50000
4.21502,23.48079,2.80000
4.41578,23.48079,3.80000
4.61643,23.47924,2.80000
4.81721,23.47500,3.50000
5.01813,23.48590,7.20000
5.21900,23.47237,3.20000
5.41976,23.47059,3.50000
5.62044,23.47130,3.20000
5.82102,23.47382,2.80000
6.02174,23.47417,1.90000
6.22254,23.47438,2.50000
6.42317,23.47438,1.90000
6.62388,23.47509,2.50000
6.82462,23.47789,3.20000
7.02546,23.47789,2.80000
7.22625,23.47813,3.80000
7.42709,23.47521,6.80000
7.62765,23.47275,4.70000
7.82843,23.47309,3.20000
8.02920,23.47357,1.60000
8.23002,23.47380,1.90000
8.43077,23.47380,3.10000
8.63154,23.47404,3.10000
8.83229,23.47453,1.90000
9.03304,23.47453,2.80000
9.23388,23.47453,1.60000
9.43468,23.50336,4.10000
9.63547,23.47446,3.20000
9.83625,23.47446,2.80000
10.03708,23.47086,7.60000
10.23793,23.48008,7.50000
10.43876,23.48008,1.90000
10.63951,23.48013,2.80000
10.84028,23.48035,4.10000
11.04104,23.48035,2.50000
11.24179,23.48035,1.90000
11.44254,23.48035,2.80000
11.64327,23.47947,3.50000
11.84405,23.47947,2.20000
12.04482,23.47968,2.20000
12.24553,23.48063,3.10000
12.44622,23.48063,2.60000
12.64699,23.48351,4.40000
12.84777,23.48351,1.60000
13.04857,23.48375,2.80000
13.24932,23.49106,3.80000
13.45008,23.48935,2.50000
13.65102,23.48690,3.80000
13.85177,23.48690,2.50000
14.05254,23.48711,2.20000
14.25346,23.48711,2.80000
14.45423,23.48711,1.60000
14.65499,23.48711,2.80000
14.85571,23.48734,2.20000
15.05653,23.48758,1.60000
15.25734,23.49190,5.00000
15.45792,23.48738,5.70000
15.65872,23.48623,3.40000
15.85947,23.48590,3.10000
16.06024,23.48590,1.00000
16.26138,23.48615,2.20000
16.46212,23.48494,3.50000
16.66319,23.48494,1.60000
16.86396,23.48515,2.80000
17.06478,23.48536,2.50000
17.26561,23.48548,4.70000
17.46637,23.48548,2.20000
17.66715,23.53412,4.10000
17.86775,23.47694,2.50000
18.06856,23.47694,3.10000
18.26935,23.47694,1.90000
18.47025,23.47694,1.60000
18.67107,23.47755,3.80000
18.87183,23.47755,2.90000
19.07255,23.47755,2.20000
19.27336,23.47779,1.90000
19.47408,23.48040,4.10000
19.67472,23.48040,2.50000
19.87546,23.47977,3.50000
20.07627,23.48000,2.60000
20.27701,23.48024,1.30000
20.47784,23.48544,7.20000
20.67839,23.48625,3.90000
20.87914,23.48648,2.30000
21.07983,23.48648,1.90000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment