yeahuh / rpms / qemu-kvm

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