Bash-Funk “memory” module
The following commands are available when this module is loaded:
License
SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com)
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-alloc-mem
Usage: -alloc-mem [OPTION]... MEMORY_IN_MB
Allocates the given amount of RAM.
Requirements:
+ Command 'python' must be available.
Parameters:
MEMORY_IN_MB (required, integer: 1-?)
Amount of RAM in MB to allocate.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
echo -n "Allocating ${_MEMORY_IN_MB} MB of memory (may take a moment)..."
python -c "
a='1'*1024*1024*${_MEMORY_IN_MB}
print('DONE')
raw_input('Press enter to exit...')
"
-memfree
Usage: -memfree [OPTION]... [MEMORY_UNIT]
Prints the free memory (in KB by default).
Parameters:
MEMORY_UNIT (default: 'KB', one of: [KB,MB,GB])
The memory unit of the printed value.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -memfree
1036560
$ -memfree MB
1012
$ -memfree GB
1
Implementation:
local totalMem=$(awk '/MemFree/ {print $2}' /proc/meminfo)
local totalMemUnit=$(awk '/MemFree/ {print $3}' /proc/meminfo)
case ${totalMemUnit} in
[Kk][Bb]) local memTotalKB=$totalMem ;;
[Mm][Bb]) local memTotalKB=$(( totalMem * 1024 )) ;;
[Gg][Bb]) local memTotalKB=$(( totalMem * 1024 * 1024 )) ;;
*) echo "-memfree: Error: Unsupported memory unit ${totalMemUnit} encountered."
return 1
;;
esac
case $_MEMORY_UNIT in
KB) echo $memTotalKB ;;
MB) echo $(( memTotalKB / 1024 )) ;;
GB) echo $(( memTotalKB / 1024 / 1024 )) ;;
esac
-meminfo
Usage: -meminfo [OPTION]...
Prints memory information from /proc/meminfo.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -meminfo
MemTotal: 24689452 kB
MemFree: 13713796 kB
MemAvailable: 16143004 kB
...
Implementation:
cat /proc/meminfo
-memtotal
Usage: -memtotal [OPTION]... [MEMORY_UNIT]
Prints the total memory (in KB by default).
Parameters:
MEMORY_UNIT (default: 'KB', one of: [KB,MB,GB])
The memory unit of the printed value.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -memtotal
1036560
$ -memtotal MB
1012
$ -memtotal GB
1
Implementation:
local totalMem=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
local totalMemUnit=$(awk '/MemTotal/ {print $3}' /proc/meminfo)
case ${totalMemUnit} in
[Kk][Bb]) local memTotalKB=$totalMem ;;
[Mm][Bb]) local memTotalKB=$(( totalMem * 1024 )) ;;
[Gg][Bb]) local memTotalKB=$(( totalMem * 1024 * 1024 )) ;;
*) echo "-memtotal: Error: Unsupported memory unit ${totalMemUnit} encountered."
return 1
;;
esac
case $_MEMORY_UNIT in
KB) echo $memTotalKB ;;
MB) echo $(( memTotalKB / 1024 )) ;;
GB) echo $(( memTotalKB / 1024 / 1024 )) ;;
esac
-procmem
Usage: -procmem [OPTION]...
Prints memory consumption information of all running processes.
Options:
--color [WHEN] (default: 'auto', one of: [always,auto,never])
Indicates when to colorize the output.
-g, --group
Group memory usage of same processes.
-----------------------------
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
if [[ $_group ]]; then
echo " PHYS. MEM VIRT. MEM USER # PROCESS"
local mem1 mem2 usr cmd prev_mem1 prev_mem2 prev_usr prev_cmd prev_count
(ps -eww -o rss,vsize,user,args --sort=+user,+args | tail -n +1 | while read mem1 mem2 usr cmd; do
if [[ $prev_usr == $usr && $prev_cmd == $cmd ]]; then
prev_mem1=$((prev_mem1 + mem1))
prev_mem2=$((prev_mem1 + mem2))
prev_count=$(( prev_count + 1 ))
else
if [[ -n $prev_cmd ]]; then
printf "%5d.%02d MB " $((prev_mem1/1024 )) $(( (prev_mem1*100/1024) - (prev_mem1/1024*100) ))
printf "%5d.%02d MB " $((prev_mem2/1024 )) $(( (prev_mem2*100/1024) - (prev_mem2/1024*100) ))
printf "%-8s " $prev_usr
printf "%2sx " $prev_count
echo "$prev_cmd"
fi
prev_usr=$usr
prev_mem1=$mem1
prev_mem2=$mem2
prev_cmd=$cmd
prev_count=1
fi
done && (
printf "%5d.%02d MB " $((prev_mem1/1024 )) $(( (prev_mem1*100/1024) - (prev_mem1/1024*100) ))
printf "%5d.%02d MB " $((prev_mem2/1024 )) $(( (prev_mem2*100/1024) - (prev_mem2/1024*100) ))
printf " %-8s " $prev_usr
printf "%2sx $prev_cmd\n" $prev_count
)) | grep -v "0.00 MB"| sort -h | -ansi-alternate --color ${_color:-auto}
else
echo " PHYS. MEM VIRT. MEM PID USER PROCESS"
local mem1 mem2 pid usr cmd
ps -eww -o rss,vsize,pid,user,args --sort=+rss,+args | tail -n +1 | while read mem1 mem2 pid usr cmd; do
if [[ mem2 -gt 0 ]]; then
printf "%5d.%02d MB " $((mem1/1024 )) $(( (mem1*100/1024) - (mem1/1024*100) ))
printf "%5d.%02d MB " $((mem2/1024 )) $(( (mem2*100/1024) - (mem2/1024*100) ))
printf "%6d " $pid
printf "%-8s " $usr
echo "$cmd"
fi
done | -ansi-alternate --color ${_color:-auto}
fi
-test-all-memory
Usage: -test-all-memory [OPTION]...
Performs a selftest of all functions of this module by executing each function with option '--selftest'.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
-alloc-mem --selftest && echo || return 1
if [ -e /proc/meminfo ]; then -memfree --selftest && echo || return 1; fi
if [ -e /proc/meminfo ]; then -meminfo --selftest && echo || return 1; fi
if [ -e /proc/meminfo ]; then -memtotal --selftest && echo || return 1; fi
-procmem --selftest && echo || return 1