2009-09-16 17:31:15 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# Copyright 2009 Red Hat, Inc. and/or its affiliates.
|
|
|
|
# Released under the GPL
|
|
|
|
#
|
|
|
|
# Author: Dan Kenigsberg <danken@redhat.com>
|
|
|
|
#
|
|
|
|
# ksmtuned - a simple script that controls whether (and with what vigor) ksm
|
|
|
|
# should search for duplicated pages.
|
|
|
|
#
|
|
|
|
# starts ksm when memory commited to qemu processes exceeds a threshold, and
|
|
|
|
# make ksm work harder and harder untill memory load falls below that
|
|
|
|
# threshold.
|
|
|
|
#
|
2009-10-06 13:26:24 +00:00
|
|
|
# send SIGUSR1 to this process right after a new qemu process is started, or
|
|
|
|
# following its death, to retune ksm accordingly
|
|
|
|
#
|
2009-09-16 17:31:15 +00:00
|
|
|
# needs testing and ironing. contact danken@redhat.com if something breaks.
|
|
|
|
|
|
|
|
if [ -f /etc/ksmtuned.conf ]; then
|
|
|
|
. /etc/ksmtuned.conf
|
|
|
|
fi
|
|
|
|
|
2010-02-04 15:58:29 +00:00
|
|
|
debug() {
|
|
|
|
if [ -n "$DEBUG" ]; then
|
|
|
|
s="`/bin/date`: $*"
|
|
|
|
[ -n "$LOGFILE" ] && echo "$s" >> "$LOGFILE" || echo "$s"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-16 17:31:15 +00:00
|
|
|
KSM_MONITOR_INTERVAL=${KSM_MONITOR_INTERVAL:-60}
|
|
|
|
KSM_NPAGES_BOOST=${KSM_NPAGES_BOOST:-300}
|
|
|
|
KSM_NPAGES_DECAY=${KSM_NPAGES_DECAY:--50}
|
|
|
|
|
|
|
|
KSM_NPAGES_MIN=${KSM_NPAGES_MIN:-64}
|
|
|
|
KSM_NPAGES_MAX=${KSM_NPAGES_MAX:-1250}
|
|
|
|
# millisecond sleep between ksm scans for 16Gb server. Smaller servers sleep
|
|
|
|
# more, bigger sleep less.
|
|
|
|
KSM_SLEEP_MSEC=${KSM_SLEEP_MSEC:-10}
|
|
|
|
|
|
|
|
KSM_THRES_COEF=${KSM_THRES_COEF:-20}
|
|
|
|
KSM_THRES_CONST=${KSM_THRES_CONST:-2048}
|
|
|
|
|
|
|
|
total=`awk '/^MemTotal:/ {print $2}' /proc/meminfo`
|
2010-02-04 15:58:29 +00:00
|
|
|
debug total $total
|
2009-09-16 17:31:15 +00:00
|
|
|
|
|
|
|
npages=0
|
|
|
|
sleep=$[KSM_SLEEP_MSEC * 16 * 1024 * 1024 / total]
|
|
|
|
[ $sleep -le 10 ] && sleep=10
|
2010-02-04 15:58:29 +00:00
|
|
|
debug sleep $sleep
|
2009-09-16 17:31:15 +00:00
|
|
|
thres=$[total * KSM_THRES_COEF / 100]
|
|
|
|
if [ $KSM_THRES_CONST -gt $thres ]; then
|
|
|
|
thres=$KSM_THRES_CONST
|
|
|
|
fi
|
2010-02-04 15:58:29 +00:00
|
|
|
debug thres $thres
|
2009-09-16 17:31:15 +00:00
|
|
|
|
|
|
|
KSMCTL () {
|
|
|
|
case x$1 in
|
|
|
|
xstop)
|
|
|
|
echo 0 > /sys/kernel/mm/ksm/run
|
|
|
|
;;
|
|
|
|
xstart)
|
|
|
|
echo $2 > /sys/kernel/mm/ksm/pages_to_scan
|
|
|
|
echo $3 > /sys/kernel/mm/ksm/sleep_millisecs
|
|
|
|
echo 1 > /sys/kernel/mm/ksm/run
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
committed_memory () {
|
|
|
|
# calculate how much memory is committed to running qemu processes
|
2013-05-15 21:00:49 +00:00
|
|
|
local pidlist
|
2013-11-06 00:47:09 +00:00
|
|
|
pidlist=$(pgrep -d ' ' -- '^qemu(-(kvm|system-.+)|:.{1,11})$')
|
2013-05-15 21:00:49 +00:00
|
|
|
if [ -n "$pidlist" ]; then
|
|
|
|
ps -p "$pidlist" -o rsz=
|
2013-11-06 00:47:09 +00:00
|
|
|
fi | awk '{ sum += $1 }; END { print 0+sum }'
|
2009-09-16 17:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free_memory () {
|
2010-07-28 04:50:43 +00:00
|
|
|
awk '/^(MemFree|Buffers|Cached):/ {free += $2}; END {print free}' \
|
2009-09-16 17:31:15 +00:00
|
|
|
/proc/meminfo
|
|
|
|
}
|
|
|
|
|
|
|
|
increase_npages() {
|
|
|
|
local delta
|
|
|
|
delta=${1:-0}
|
|
|
|
npages=$[npages + delta]
|
|
|
|
if [ $npages -lt $KSM_NPAGES_MIN ]; then
|
|
|
|
npages=$KSM_NPAGES_MIN
|
|
|
|
elif [ $npages -gt $KSM_NPAGES_MAX ]; then
|
|
|
|
npages=$KSM_NPAGES_MAX
|
|
|
|
fi
|
|
|
|
echo $npages
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
adjust () {
|
|
|
|
local free committed
|
|
|
|
free=`free_memory`
|
|
|
|
committed=`committed_memory`
|
2010-02-04 15:58:29 +00:00
|
|
|
debug committed $committed free $free
|
2009-09-16 17:31:15 +00:00
|
|
|
if [ $[committed + thres] -lt $total -a $free -gt $thres ]; then
|
|
|
|
KSMCTL stop
|
2010-02-04 15:58:29 +00:00
|
|
|
debug "$[committed + thres] < $total and free > $thres, stop ksm"
|
2009-09-16 17:31:15 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2010-02-04 15:58:29 +00:00
|
|
|
debug "$[committed + thres] > $total, start ksm"
|
2009-09-16 17:31:15 +00:00
|
|
|
if [ $free -lt $thres ]; then
|
|
|
|
npages=`increase_npages $KSM_NPAGES_BOOST`
|
2010-02-04 15:58:29 +00:00
|
|
|
debug "$free < $thres, boost"
|
2009-09-16 17:31:15 +00:00
|
|
|
else
|
|
|
|
npages=`increase_npages $KSM_NPAGES_DECAY`
|
2010-02-04 15:58:29 +00:00
|
|
|
debug "$free > $thres, decay"
|
2009-09-16 17:31:15 +00:00
|
|
|
fi
|
|
|
|
KSMCTL start $npages $sleep
|
2010-02-04 15:58:29 +00:00
|
|
|
debug "KSMCTL start $npages $sleep"
|
2009-09-16 17:31:15 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2009-10-06 13:26:24 +00:00
|
|
|
function nothing () {
|
|
|
|
:
|
|
|
|
}
|
|
|
|
|
2009-09-16 17:31:15 +00:00
|
|
|
loop () {
|
2009-10-06 13:26:24 +00:00
|
|
|
trap nothing SIGUSR1
|
2009-09-16 17:31:15 +00:00
|
|
|
while true
|
|
|
|
do
|
2009-10-06 13:26:24 +00:00
|
|
|
sleep $KSM_MONITOR_INTERVAL &
|
|
|
|
wait $!
|
2009-09-16 17:31:15 +00:00
|
|
|
adjust
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
PIDFILE=${PIDFILE-/var/run/ksmtune.pid}
|
|
|
|
if touch "$PIDFILE"; then
|
|
|
|
loop &
|
|
|
|
echo $! > "$PIDFILE"
|
|
|
|
fi
|