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