Skip to content

Commit

Permalink
Less verbose memory usage info.
Browse files Browse the repository at this point in the history
Signed-off-by: Tim 'mithro' Ansell <me@mith.ro>
  • Loading branch information
mithro committed Feb 24, 2019
1 parent 41fe60c commit a100135
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions fuzzers/run_fuzzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,70 @@ def get(cls, pid):
return "{}\n{}".format(stdout, stderr)


def get_memory():
return subprocess.check_output('free -mh', shell=True).decode("utf-8")
def mem_convert(s):
"""
>>> mem_convert('62Gi')
62000000000.0
>>> mem_convert('62Mi')
62000000.0
>>> mem_convert('62B')
62.0
"""
units = {
'Gi': 1e9,
'Mi': 1e6,
'Ki': 1e3,
'B': 1,
}
u = '',
m = 1
for u, m in units.items():
if s.endswith(u):
break

v = float(s[:-len(u)])
v = v * m
return v


def get_memory(memstr=None):
r"""
>>> import pprint
>>> pprint.pprint(get_memory('''\
... total used free shared buff/cache available
... Mem: 62Gi 19Gi 4.8Gi 661Mi 38Gi 42Gi
... Swap: 0B 0B 0B
... '''))
{'mem': {'available': 42000000000.0,
'buff/cache': 38000000000.0,
'free': 4800000000.0,
'shared': 661000000.0,
'total': 62000000000.0,
'used': 19000000000.0},
'swap': {'free': 0.0, 'total': 0.0, 'used': 0.0}}
"""
if memstr is None:
memstr = subprocess.check_output('free -mh', shell=True).decode("utf-8")

lines = [x.split() for x in memstr.strip().splitlines()]
lines[0].insert(0, 'type:')

for l in lines:
l[0] = l[0][:-1].lower()

headers = lines[0][1:]
lines = lines[1:]

memory = {}
for l in lines:
t, l = l[0], l[1:]

d = {}
for k, v in zip(headers, l):
d[k] = mem_convert(v)
memory[t] = d

return memory


def should_run_submake(make_flags):
Expand Down Expand Up @@ -477,10 +539,12 @@ def log(msg, *a, **k):

if retcode is not None:
break
mem = get_memory()['mem']
log(
"Still running (1m:{:0.2f}%, 5m:{:0.2f}%, 15m:{:0.2f}%).\n{}\n{}",
"Still running (1m:{:0.2f}%, 5m:{:0.2f}%, 15m:{:0.2f}% Mem:{:0.1f}Gi used, {:0.1f}Gi free).\n{}",
*get_load(),
get_memory(),
mem['used']/1e9,
mem['available']/1e9, # Using available so the numbers add up.
PsTree.get(p.pid),
)
except (Exception, KeyboardInterrupt, SystemExit):
Expand Down

0 comments on commit a100135

Please sign in to comment.