cryptospore / rpms / qemu-kvm

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