Guest User

Untitled

a guest
Apr 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.46 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Copyright 2009 Red Hat, Inc. and/or its affiliates.
  4. # Released under the GPL
  5. #
  6. # ksmd         Kernel Samepage Merging Daemon
  7. #
  8. # chkconfig: - 85 15
  9. # description: The Kernel Samepage Merging control Daemon is a simple script  \
  10. #          that controls whether (and with what vigor) should ksm search  \
  11. #              duplicated pages.
  12. # processname: ksmd
  13. # config: /etc/ksmd.conf
  14. # pidfile: /var/run/ksmd.pid
  15. #
  16. ### BEGIN INIT INFO
  17. # Provides: ksmd
  18. # Required-Start:
  19. # Required-Stop:
  20. # Should-Start:
  21. # Short-Description: tune the speed of ksm
  22. # Description: The Kernel Samepage Merging control Daemon is a simple script
  23. #   that controls whether (and with what vigor) should ksm search duplicated
  24. #   memory pages.
  25. #   needs testing and ironing. contact danken redhat com if something breaks.
  26. ### END INIT INFO
  27.  
  28. ###########################
  29. if [ -f /etc/ksmd.conf ]; then
  30.     . /etc/ksmd.conf
  31. fi
  32.  
  33. KSM_MONITOR_INTERVAL=${KSM_MONITOR_INTERVAL:-60}
  34. KSM_NPAGES_BOOST=${KSM_NPAGES_BOOST:-300}
  35. KSM_NPAGES_DECAY=${KSM_NPAGES_DECAY:--50}
  36.  
  37. KSM_NPAGES_MIN=${KSM_NPAGES_MIN:-64}
  38. KSM_NPAGES_MAX=${KSM_NPAGES_MAX:-1250}
  39. # microsecond sleep between ksm scans for 16Gb server. Smaller servers sleep
  40. # more, bigger sleep less.
  41. KSM_SLEEP=${KSM_SLEEP:-10000}
  42.  
  43. KSM_THRES_COEF=${KSM_THRES_COEF:-20}
  44. KSM_THRES_CONST=${KSM_THRES_CONST:-2048}
  45.  
  46. total=`awk '/^MemTotal:/ {print $2}' /proc/meminfo`
  47. [ -n "$DEBUG" ] && echo total $total
  48.  
  49. npages=0
  50. sleep=$[KSM_SLEEP * 16 * 1024 * 1024 / total]
  51. [ -n "$DEBUG" ] && echo sleep $sleep
  52. thres=$[total * KSM_THRES_COEF / 100]
  53. if [ $KSM_THRES_CONST -gt $thres ]; then
  54.     thres=$KSM_THRES_CONST
  55. fi
  56. [ -n "$DEBUG" ] && echo thres $thres
  57.  
  58. KSMCTL () {
  59.     if [ -x /usr/bin/ksmctl ]; then
  60.         /usr/bin/ksmctl $*
  61.     else
  62.         case x$1 in
  63.             xstop)
  64.                 echo 0 > /sys/kernel/mm/ksm/run
  65.                 ;;
  66.             xstart)
  67.                 echo $2 > /sys/kernel/mm/ksm/pages_to_scan
  68.                 echo $3 > /sys/kernel/mm/ksm/sleep
  69.                 echo 1 > /sys/kernel/mm/ksm/run
  70.                 ;;
  71.         esac
  72.     fi
  73. }
  74.  
  75. committed_memory () {
  76.     # calculate how much memory is committed to running qemu processes
  77.     local progname
  78.     progname=${1:-qemu}
  79.     ps -o vsz `pgrep $progname` | awk '{ sum += $1 }; END { print sum }'
  80. }
  81.  
  82. increase_napges() {
  83.     local delta
  84.     delta=${1:-0}
  85.     npages=$[npages + delta]
  86.     if [ $npages -lt $KSM_NPAGES_MIN ]; then
  87.         npages=$KSM_NPAGES_MIN
  88.     elif [ $npages -gt $KSM_NPAGES_MAX ]; then
  89.         npages=$KSM_NPAGES_MAX
  90.     fi
  91.     echo $npages
  92. }
  93.  
  94. adjust () {
  95.     local free committed
  96.     free=`awk '/^MemFree:/ { free += $2}; /^Buffers:/ {free += $2}; /^MemCached:  / {free += $2}; END {print free}' /proc/meminfo`
  97.     committed=`committed_memory`
  98.     [ -n "$DEBUG" ] && echo committed $committed free $free
  99.     if [ $[committed + thres] -lt $total -a $free -gt $thres ]; then
  100.         KSMCTL stop
  101.         [ -n "$DEBUG" ] && echo "$[committed + thres] < $total and free > $thres  , stop ksm"
  102.         return 1
  103.     fi
  104.     [ -n "$DEBUG" ] && echo "$[committed + thres] > $total, start ksm"
  105.     if [ $free -lt $thres ]; then
  106.         npages=`increase_napges $KSM_NPAGES_BOOST`
  107.         [ -n "$DEBUG" ] && echo "$free < $thres, boost"
  108.     else
  109.         npages=`increase_napges $KSM_NPAGES_DECAY`
  110.         [ -n "$DEBUG" ] && echo "$free > $thres, decay"
  111.     fi
  112.     KSMCTL start $npages $sleep
  113.     [ -n "$DEBUG" ] && echo "KSMCTL start $npages $sleep"
  114.     return 0
  115. }
  116.  
  117. loop () {
  118.     while true
  119.     do
  120.         sleep $KSM_MONITOR_INTERVAL
  121.         adjust
  122.     done
  123. }
  124.  
  125. ###########################
  126. . /etc/rc.d/init.d/functions
  127.  
  128. prog=ksmd
  129. pidfile=${PIDFILE-/var/run/ksmd.pid}
  130. RETVAL=0
  131.  
  132. start() {
  133.     echo -n $"Starting $prog: "
  134.     daemon --pidfile=${pidfile} $0 loop
  135.     RETVAL=$?
  136.     echo
  137.     return $RETVAL
  138. }
  139.  
  140. stop() {
  141.     echo -n $"Stopping $prog: "
  142.     killproc -p ${pidfile}
  143.     RETVAL=$?
  144.     echo
  145. }
  146.  
  147. signal () {
  148.     pkill -P `cat ${pidfile}` sleep
  149. }
  150.  
  151. case "$1" in
  152.   start)
  153.     start
  154.     ;;
  155.   stop)
  156.     stop
  157.     ;;
  158.   status)
  159.         status -p ${pidfile} $prog
  160.     RETVAL=$?
  161.     ;;
  162.   restart)
  163.     stop
  164.     start
  165.     ;;
  166.   signal)
  167.     signal
  168.     ;;
  169.   loop)
  170.         RETVAL=1
  171.     if [ -w `dirname ${pidfile}` ]; then
  172.             loop &
  173.             echo $! > ${pidfile}
  174.             RETVAL=$?
  175.         fi
  176.     ;;
  177.   *)
  178.     echo $"Usage: $prog {start|stop|status|signal|help}"
  179.     RETVAL=3
  180. esac
  181.  
  182. exit $RETVAL
Add Comment
Please, Sign In to add comment