Skip to content

Commit 2d4be01

Browse files
committed
ADD: Memory monitoring script
1 parent 0baaf81 commit 2d4be01

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

bin/mmaxmem

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#! /bin/bash
2+
3+
echo ""
4+
echo "mmaxmem - MEGAlib's memory monitor"
5+
echo ""
6+
7+
8+
9+
help() {
10+
echo ""
11+
echo "mmaxmem - script for MEGAlib";
12+
echo "(C) by Andreas Zoglauer"
13+
echo "";
14+
echo "This script keeps track of the maximum memory used by a process."
15+
echo "";
16+
echo "Usage:";
17+
echo " mmaxmem [program name] [optional command line arguments]";
18+
echo "";
19+
echo "Example:";
20+
echo " mmaxmem mimrec";
21+
echo "";
22+
}
23+
24+
25+
# Make sure there's at least one argument
26+
if [ $# -lt 1 ]; then
27+
help
28+
exit 1
29+
fi
30+
31+
# Check for help flag
32+
if [[ "$1" == "*-h*" ]]; then
33+
help
34+
exit 0
35+
fi
36+
37+
OS=$(uname)
38+
39+
RSS_KB=0
40+
MAX_RSS_KB=0
41+
42+
# Start the command in the background
43+
"$@" &> /dev/null &
44+
APPPID=$!
45+
APPNAME=$*
46+
sleep 0.2
47+
48+
echo "Monitoring memory usage of program ${APPNAME} with ${APPPID} ..."
49+
echo ""
50+
51+
while true; do
52+
if ps -p "${APPPID}" > /dev/null 2>&1; then
53+
if [[ "${OS}" == *inux* ]]; then
54+
RSS_KB=$(awk '/VmRSS/ {print $2}' /proc/${APPPID}/status 2>/dev/null)
55+
elif [[ "${OS}" == *arwin* ]]; then
56+
RSS_KB=$(ps -o rss= -p "${APPPID}" | awk '{print $1}')
57+
else
58+
echo "Unsupported OS: \"${OS}\""
59+
exit 1
60+
fi
61+
62+
if [[ "${RSS_KB}" =~ ^[0-9]+$ ]]; then
63+
if [ "${RSS_KB}" -gt "${MAX_RSS_KB}" ]; then
64+
MAX_RSS_KB=${RSS_KB}
65+
fi
66+
CURRENT_MB=$(awk "BEGIN {printf \"%.2f\", ${RSS_KB}/1024}")
67+
MAX_MB=$(awk "BEGIN {printf \"%.2f\", ${MAX_RSS_KB}/1024}")
68+
echo -ne "Current: ${CURRENT_MB} MB\tMax: ${MAX_MB} MB\r"
69+
fi
70+
sleep 1.0
71+
else
72+
echo -e "\nProcess ${APPPID} has exited."
73+
MAX_MB=$(awk "BEGIN {printf \"%.2f\", ${MAX_RSS_KB}/1024}")
74+
echo "Maximum RSS memory used: ${MAX_MB} MB"
75+
break
76+
fi
77+
done

0 commit comments

Comments
 (0)