-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththrash
executable file
·48 lines (40 loc) · 1.32 KB
/
thrash
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
#!/usr/bin/env python
# put "thrash" before your command and the command will be monitored
# for thrashing
# Dependencies:
# ansicolors (https://pypi.org/project/ansicolors)
# Author: David McClosky
# Homepage: http://zorglish.org
# Code: http://github.com/dmcc
import os
import sys
import time
import colors
period = int(os.getenv('THRASH_PERIOD', '15'))
command = sys.argv[1:]
print("Command:", ' '.join(command))
pid = os.fork()
if pid == 0: # child
os.execvp(command[0], command)
else: # parent
maxes = {}
while 1:
print()
print(colors.color(f"[Time: {time.asctime()}", fg='red'), end=' ')
stats = []
for line in open('/proc/%s/status' % pid):
if line.startswith('Vm'):
# example line: "VmRSS: 1036 kB"
attr, val, unit = line.split()
attr = attr[:-1]
val = int(val)
stats.append(f"{attr}: {val}")
maxes.setdefault(attr, 0)
maxes[attr] = max(maxes[attr], val)
if stats:
print(colors.color("Cur: " + ", ".join(stats), fg='red'))
else:
raise SystemExit # XXX bad hack
print(colors.color("Max " + ', '.join(["%s: %s" % pair
for pair in maxes.items()]) + "]", fg='red'))
time.sleep(period)