-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathram-use-plot
executable file
·89 lines (77 loc) · 2.14 KB
/
ram-use-plot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
# Author: Daniel Rode
# Dependencies:
# Python 3.11+
# plotext
# pandas
# Created: 21 Jun 2024
# Updated: 27 Sep 2024
import sys
from sys import exit
from pathlib import Path
import pandas as pd
import plotext as plt
# Functions
def grep(query, array):
for i in array:
if query in i:
yield i
# Parse command line arguments
args = sys.argv[1:]
try:
csv_path = args[0]
except IndexError:
print("Usage: this.py LOG_PATH")
exit(1)
# Get system ram size
mem_info = Path('/proc/meminfo').read_text().split('\n')
total_ram = next(grep("MemTotal: ", mem_info))
total_ram = total_ram.split(':')[1].strip().split(' ')[0]
total_ram = int(total_ram)
# Import and shape data
df = pd.read_csv(
csv_path,
delimiter='\tMemAvailable: ',
skiprows=1,
engine='python',
)
df['time'] = pd.to_datetime(
df[df.columns[0]], #.apply(lambda x: x[:28]),
# format='%a %b %d %H:%M:%S MDT %Y',
format='%Y-%m-%d %H:%M:%S',
)
# Plotext assumes times are in UTC
# It also seems to mistakenly assume that MST = MDT, resulting in an x-axis
# that is off by 1 hour, so by using -0700 (instead of -0600) as the offset,
# this works around the bug
df['time_utc_str'] = df['time'] \
.dt.tz_localize("-0700") \
.dt.tz_convert('UTC') \
.dt.strftime("%m/%d-%H:%M") \
;
df['GB'] = df[df.columns[1]].apply(
lambda x: (total_ram - int(x.strip().split(' ')[0])) / 1000000
)
# Limit plot height so terminal prompt does not cut off top of graph and to
# make room for printing stats
plt.plot_size(height=plt.terminal_height()-6)
# Plot data
plt.title("RAM Usage")
plt.xlabel("Time")
plt.ylabel("Ram Usage (GB)")
plt.date_form("m/d-H:M")
x = df['time_utc_str']
y = df['GB']
plt.plot(x, y)
plt.show()
# Print stats
min_row = df[df['GB']==df['GB'].min()]
max_row = df[df['GB']==df['GB'].max()]
print(f"Min: {min_row['GB'].values[0]} @ {min_row['time'].values[0]}")
print(f"Max: {max_row['GB'].values[0]} @ {max_row['time'].values[0]}")
print(f"Total RAM: {total_ram / 1000000} GB")
# Plot data with seaborn
# import seaborn as sns
# filename = "ram-usage-plot.png"
# plt = sns.lineplot(x=x, y=y)
# plt.get_figure().savefig(filename, bbox_inches='tight', dpi=300)