cryptospore / rpms / qemu-kvm

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