generated from Start9Labs/hello-world-startos
-
Notifications
You must be signed in to change notification settings - Fork 3
/
check-mem.py
executable file
·46 lines (38 loc) · 1.19 KB
/
check-mem.py
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
#!/usr/bin/env python3
from pathlib import Path
import sys
MINIMUM_MEM = 16
MINIMUM_AVAIL = 1
BYTES_IN_GB = 1024**3 # should be GiB, but matches system info display
def get_available_bytes():
meminfo = Path('/proc/meminfo').read_text()
active = None
total = None
for line in meminfo.split('\n'):
if ':' not in line:
continue
name, rest = line.split(':')
num = int(rest.strip().split(' ')[0]) * 1024
if name == 'Active(anon)':
active = num
if name == 'MemTotal':
total = num
return total - active
def get_used_bytes():
stat = Path('/sys/fs/cgroup/memory.stat').read_text()
for line in stat.split('\n'):
name, rest = line.split(' ')
if name == 'anon':
return int(rest.strip()[0])
try:
used_gb = get_used_bytes() / BYTES_IN_GB
available_gb = get_available_bytes() / BYTES_IN_GB
except Exception as e:
print(e, file=sys.stderr)
exit(60)
if used_gb + available_gb < MINIMUM_MEM:
print(f'Not enough usable memory ({used_gb + available_gb:.2f}GB). Try stopping other services.', file=sys.stderr)
exit(1)
if used_gb > MINIMUM_MEM and available_gb < MINIMUM_AVAIL:
print(f'Using too much memory ({used_gb:.2f}GB). Try a smaller model or lower settings.', file=sys.stderr)
exit(2)