yeahuh / rpms / qemu-kvm

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