]> git.pld-linux.org Git - packages/qemu.git/blame - ksmtuned
- updated some dependencies, added conditions for -module-block-*
[packages/qemu.git] / ksmtuned
CommitLineData
9fdf235e
JR
1#!/bin/sh
2#
3# Copyright 2009 Red Hat, Inc. and/or its affiliates.
4# Released under the GPL
5#
6# Author: Dan Kenigsberg <danken@redhat.com>
7# un-bash-ed: Jan Rękorajski <baggins@pld-linux.org>
8#
9# ksmtuned - a simple script that controls whether (and with what vigor) ksm
10# should search for duplicated pages.
11#
12# starts ksm when memory commited to qemu processes exceeds a threshold, and
13# make ksm work harder and harder untill memory load falls below that
14# threshold.
15#
16# send SIGUSR1 to this process right after a new qemu process is started, or
17# following its death, to retune ksm accordingly
18
19[ -f /etc/ksmtuned.conf ] && . /etc/ksmtuned.conf
20
21debug() {
22 if [ -n "$DEBUG" ]; then
23 s="`/bin/date`: $*"
24 [ -n "$LOGFILE" ] && echo "$s" >> "$LOGFILE" || echo "$s"
25 fi
26}
27
28KSM_MONITOR_INTERVAL=${KSM_MONITOR_INTERVAL:-60}
29KSM_NPAGES_BOOST=${KSM_NPAGES_BOOST:-300}
30KSM_NPAGES_DECAY=${KSM_NPAGES_DECAY:--50}
31
32KSM_NPAGES_MIN=${KSM_NPAGES_MIN:-64}
33KSM_NPAGES_MAX=${KSM_NPAGES_MAX:-1250}
34# millisecond sleep between ksm scans for 16Gb server. Smaller servers sleep
35# more, bigger sleep less.
36KSM_SLEEP_MSEC=${KSM_SLEEP_MSEC:-10}
37
38KSM_THRES_COEF=${KSM_THRES_COEF:-20}
39KSM_THRES_CONST=${KSM_THRES_CONST:-2048}
40
41total=`awk '/^MemTotal:/ {print $2}' /proc/meminfo`
42debug total $total
43
44npages=0
45sleep=$(( $KSM_SLEEP_MSEC * 16 * 1024 * 1024 / $total ))
46[ $sleep -le 10 ] && sleep=10
47debug sleep $sleep
48thres=$(( $total * $KSM_THRES_COEF / 100 ))
49if [ $KSM_THRES_CONST -gt $thres ]; then
50 thres=$KSM_THRES_CONST
51fi
52debug thres $thres
53
54KSMCTL () {
55 case x$1 in
56 xstop)
57 echo 0 > /sys/kernel/mm/ksm/run
58 ;;
59 xstart)
60 echo $2 > /sys/kernel/mm/ksm/pages_to_scan
61 echo $3 > /sys/kernel/mm/ksm/sleep_millisecs
62 echo 1 > /sys/kernel/mm/ksm/run
63 ;;
64 esac
65}
66
67committed_memory () {
68 # calculate how much memory is committed to running qemu processes
69 local progname
70 progname=${1:-qemu-kvm}
71 ps -C "$progname" -o rsz | awk '{ sum += $1 }; END { print sum }'
72}
73
74free_memory () {
75 awk '/^(MemFree|Buffers|Cached):/ {free += $2}; END {print free}' \
76 /proc/meminfo
77}
78
79increase_npages() {
80 local delta
81 delta=${1:-0}
82 npages=$(( $npages + $delta ))
83 if [ $npages -lt $KSM_NPAGES_MIN ]; then
84 npages=$KSM_NPAGES_MIN
85 elif [ $npages -gt $KSM_NPAGES_MAX ]; then
86 npages=$KSM_NPAGES_MAX
87 fi
88 echo $npages
89}
90
91
92adjust () {
93 local free committed
94 free=`free_memory`
95 committed=`committed_memory`
96 debug committed $committed free $free
97 if [ $(( $committed + $thres )) -lt $total -a $free -gt $thres ]; then
98 KSMCTL stop
99 debug "$(( $committed + $thres )) < $total and free > $thres, stop ksm"
100 return 1
101 fi
102 debug "$(( $committed + $thres )) > $total, start ksm"
103 if [ $free -lt $thres ]; then
104 npages=`increase_npages $KSM_NPAGES_BOOST`
105 debug "$free < $thres, boost"
106 else
107 npages=`increase_npages $KSM_NPAGES_DECAY`
108 debug "$free > $thres, decay"
109 fi
110 KSMCTL start $npages $sleep
111 debug "KSMCTL start $npages $sleep"
112 return 0
113}
114
115nothing () {
116 :
117}
118
119loop () {
120 trap nothing USR1
121 while true
122 do
123 sleep $KSM_MONITOR_INTERVAL &
124 wait $!
125 adjust
126 done
127}
128
129PIDFILE=${PIDFILE-/var/run/ksmtune.pid}
130if touch "$PIDFILE"; then
131 loop &
132 echo $! > "$PIDFILE"
133fi
This page took 0.058699 seconds and 4 git commands to generate.