Blame SOURCES/ksmtuned

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