devstack/tools/memory_tracker.sh
Clark Boylan a40f9cb91f Add option to install everything in global venvs
Since we are python3 only for openstack we create a single python3
virtualenv to install all the packages into. This gives us the benefits
of installing into a virtualenv while still ensuring coinstallability.
This is a major change and will likely break many things.

There are several reasons for this. The change that started this effort
was pip stopped uninstalling packages which used distutils to generate
their package installation. Many distro packages do this which meant
that pip installed packages and distro packages could not coexist in the
global install space. More recently git has made pip installing repos as
root more difficult due to file ownership concerns.

Currently the switch to the global venv is optional, but if we go down
this path we should very quickly remove the old global installation
method as it has only caused us problems.

Major hurdles we have to get over are convincing rootwrap to trust
binaries in the virtualenvs (so you'll notice we update rootwrap
configs).

Some distros still have issues, keep them using the old setup for now.

Depends-On: https://review.opendev.org/c/openstack/grenade/+/880266
Co-Authored-By: Dr. Jens Harbott <frickler@offenerstapel.de>
Change-Id: If9bc7ba45522189d03f19b86cb681bb150ee2f25
2023-08-02 07:07:25 +02:00

126 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
#
# 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.
set -o errexit
# TODO(frickler): make this use stackrc variables
if [ -x /opt/stack/data/venv/bin/python ]; then
PYTHON=/opt/stack/data/venv/bin/python
else
PYTHON=${PYTHON:-python3}
fi
# time to sleep between checks
SLEEP_TIME=20
# MemAvailable is the best estimation and has built-in heuristics
# around reclaimable memory. However, it is not available until 3.14
# kernel (i.e. Ubuntu LTS Trusty misses it). In that case, we fall
# back to free+buffers+cache as the available memory.
USE_MEM_AVAILABLE=0
if grep -q '^MemAvailable:' /proc/meminfo; then
USE_MEM_AVAILABLE=1
fi
function get_mem_unevictable {
awk '/^Unevictable:/ {print $2}' /proc/meminfo
}
function get_mem_available {
if [[ $USE_MEM_AVAILABLE -eq 1 ]]; then
awk '/^MemAvailable:/ {print $2}' /proc/meminfo
else
awk '/^MemFree:/ {free=$2}
/^Buffers:/ {buffers=$2}
/^Cached:/ {cached=$2}
END { print free+buffers+cached }' /proc/meminfo
fi
}
function tracker {
local low_point
local unevictable_point
low_point=$(get_mem_available)
# log mlocked memory at least on first iteration
unevictable_point=0
while [ 1 ]; do
local mem_available
mem_available=$(get_mem_available)
local unevictable
unevictable=$(get_mem_unevictable)
if [ $mem_available -lt $low_point -o $unevictable -ne $unevictable_point ]; then
echo "[[["
date
# whenever we see less memory available than last time, dump the
# snapshot of current usage; i.e. checking the latest entry in the file
# will give the peak-memory usage
if [[ $mem_available -lt $low_point ]]; then
low_point=$mem_available
echo "---"
# always available greppable output; given difference in
# meminfo output as described above...
echo "memory_tracker low_point: $mem_available"
echo "---"
cat /proc/meminfo
echo "---"
# would hierarchial view be more useful (-H)? output is
# not sorted by usage then, however, and the first
# question is "what's using up the memory"
#
# there are a lot of kernel threads, especially on a 8-cpu
# system. do a best-effort removal to improve
# signal/noise ratio of output.
ps --sort=-pmem -eo pid:10,pmem:6,rss:15,ppid:10,cputime:10,nlwp:8,wchan:25,args:100 |
grep -v ']$'
fi
echo "---"
# list processes that lock memory from swap
if [[ $unevictable -ne $unevictable_point ]]; then
unevictable_point=$unevictable
${PYTHON} $(dirname $0)/mlock_report.py
fi
echo "]]]"
fi
sleep $SLEEP_TIME
done
}
function usage {
echo "Usage: $0 [-x] [-s N]" 1>&2
exit 1
}
while getopts ":s:x" opt; do
case $opt in
s)
SLEEP_TIME=$OPTARG
;;
x)
set -o xtrace
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
tracker