home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / sbin / bootchartd < prev    next >
Text File  |  2010-01-14  |  6KB  |  251 lines

  1. #!/bin/bash
  2. #
  3. # Bootchart logger script
  4. # Ziga Mahkovec  <ziga.mahkovec@klika.si>
  5. #
  6. # This script is used for data collection for the bootchart
  7. # boot performance visualization tool (http://www.bootchart.org).
  8. #
  9. # To profile the boot process, bootchartd should be called instead of
  10. # /sbin/init.  Modify the kernel command line to include:
  11. # init=/sbin/bootchartd
  12. #
  13. # bootchartd will then start itself in background and exec /sbin/init
  14. # (or an alternative init process if specified using bootchart_init=)
  15. #
  16. # To profile a running system, run:
  17. # $ /sbin/bootchartd start; sleep 30; /sbin/bootchartd stop
  18. #
  19.  
  20. PATH="/sbin:/bin:/usr/sbin:/usr/bin:$PATH"
  21.  
  22. # Read configuration.
  23. CONF="/etc/bootchartd.conf"
  24. if [ -f $PWD/bootchartd.conf ]; then
  25.     . $PWD/bootchartd.conf
  26. elif [ -f $CONF ]; then
  27.         . $CONF
  28. else
  29.         echo "$CONF missing"
  30.         exit 1
  31. fi
  32.  
  33. if test -z "`which accton >& /dev/null`"; then
  34.     PROCESS_ACCOUNTING="no"
  35. fi
  36.  
  37. # Start the boot logger.
  38. start()
  39. {
  40.     # Mount the temporary file system for log file storage.  If possible,
  41.     # a temporary directory is created.  In most cases though (i.e. during
  42.     # boot), a tmpfs is mounted in /mnt.  The mount point is immediately
  43.     # released using a lazy umount, so the script must never leave that
  44.     # directory.
  45.     LOG_DIR="$( mktemp -d /tmp/bootchart.XXXXXX 2>/dev/null )"
  46.     if [ -z "$LOG_DIR" ]; then
  47.         LOG_DIR="/lib/bootchart/mnt"
  48.         LAZY_UMOUNT="yes"
  49.         mount -n -t tmpfs -o size=$TMPFS_SIZE none "$LOG_DIR" >/dev/null 2>&1
  50.     fi
  51.     cd "$LOG_DIR"
  52.     [ -n "$LAZY_UMOUNT" ] && umount -nfl "$LOG_DIR"
  53.  
  54.     # use dmsg for debugging
  55.     mknod kmsg c 1 11
  56.     echo "bootchartd started" >> kmsg
  57.  
  58.     # Mount a copy of /proc just for us ...
  59.     mkdir proc
  60.     mount -n -t proc none proc
  61.  
  62.     # Enable process accounting if configured
  63.     if [ "$PROCESS_ACCOUNTING" = "yes" ]; then
  64.         > kernel_pacct
  65.         accton kernel_pacct
  66.         echo "bootchartd pacct started" >> kmsg
  67.     fi
  68.  
  69.     #
  70.     # Run loggers in background
  71.     #
  72.     /lib/bootchart/bootchart-collector -p proc $SAMPLE_HZ &
  73.  
  74.     if [ -n "$IN_INIT" ]; then
  75.         # If we were called during init, wait for the boot process to end
  76.         wait_boot &
  77.     elif [ "$#" -gt 0 ]; then
  78.         # If a command was passed, run it
  79.         # (used for profiling specific applications)
  80.         echo "profile.process = $( basename $1 )" >> header
  81.         $@
  82.         stop
  83.     fi
  84. }
  85.  
  86. # Wait for the boot process to end.
  87. wait_boot()
  88. {
  89.     local runlevel=$( sed -n 's/.*:\(.*\):initdefault:.*/\1/gp' /etc/inittab )
  90.  
  91.     # The processes we have to wait for
  92.     local exit_proc="kdm_greet xterm konsole stopinitrd metacity mutter compiz"
  93.      # Don't exit until *all* the mandatory procs are running. Only used if exit_proc
  94.      # is unset.
  95.      local mandatory_procs="nautilus mutter"
  96.  
  97.     # early_login in FC4 starts gdm early, so fall back to mingetty
  98.     local early_login="no"
  99.     grep -q early_login /proc/cmdline && early_login="yes"
  100.     if [ "x$runlevel" = "x2" -o "x$runlevel" = "x3" -o "$early_login" = "yes" ]; then
  101.         exit_proc="mingetty agetty rungetty getty"
  102.     fi
  103.     while true; do
  104.         if [ -n "$exit_proc" -a -n "$( pidof $exit_proc )" ]; then
  105.             # Give the exit process some time to start
  106.             stopinitrd=$( pidof stopinitrd )
  107.             if [ -n "$stopinitrd" ] ; then
  108.                 # move initrd logs somewhere safe (?)
  109.                 pkill -f /lib/bootchart/bootchart-collector
  110.                 sleep 2 # the even 
  111.                 mv $LOG_DIR/*.log /mnt/dev/shm/
  112.                 wait
  113.                 kill $stopinitrd
  114.                 exit 0
  115.             else # give an unambiguous settle afterwards
  116.                 sleep 20
  117.             fi
  118.  
  119.             # Flush the log files
  120.             stop
  121.             return
  122.                 else
  123.              for proc in $mandatory_procs; do
  124.              if ! pidof $proc >/dev/null; then break; fi
  125.                         sleep 30
  126.                         stop
  127.                         return
  128.                     done
  129.         fi
  130.         usleep 200000
  131.     done;
  132. }
  133.  
  134.  
  135. # Stop the boot logger.
  136. # Some final log files are created and then all log files
  137. # from the tmpfs are packaged and stored in $BOOTLOG_DEST.
  138. stop()
  139. {
  140.     pkill -f /lib/bootchart/bootchart-collector
  141.     sleep 2 # the even 
  142.  
  143.     # Stop process accounting if configured
  144.     local pacct=
  145.     if [ "$PROCESS_ACCOUNTING" = "yes" ]; then
  146.         accton
  147.         pacct=kernel_pacct
  148.     fi
  149.  
  150.     # Write system information
  151.     log_header
  152.  
  153.     # Package log files
  154.  
  155.     # FIXME: do we -really- need any of this ?
  156.         for i in *.log; do
  157.         if test -f /dev/shm/$i; then
  158.             cat /dev/shm/$i $i > $i.new && mv $i.new $i
  159.                       rm /dev/shm/$i
  160.         fi
  161.     done
  162.     dmesg > dmesg
  163.  
  164.     # if it hung - clean it up at least
  165.     pkill -9 -f /lib/bootchart/bootchart-collector
  166.  
  167.     # cleanup
  168.     rm -f klog
  169.     umount proc
  170.     rmdir proc
  171.  
  172.     tar -zcf "$BOOTLOG_DEST" header dmesg $pacct *.log
  173.     if [ -z "$LAZY_UMOUNT" ]; then
  174.         rm "$LOG_DIR"/*
  175.         rmdir "$LOG_DIR"
  176.     fi
  177.  
  178.     # Render the chart if configured (and the renderer is installed)
  179.     if [ "$AUTO_RENDER" = "yes" -a -x /usr/bin/pybootchartgui ]; then
  180.                 cd $AUTO_RENDER_DIR
  181.         /usr/bin/pybootchartgui -o "$AUTO_RENDER_DIR"/bootchart.$AUTO_RENDER_FORMAT -f $AUTO_RENDER_FORMAT "$BOOTLOG_DEST"
  182.         fi
  183. }
  184.  
  185.  
  186. # Log some basic information about the system.
  187. log_header()
  188. {
  189.     if [ -x "/usr/bin/dpkg-query" ]; then
  190.         version="$(dpkg-query -f'${Version}' -W bootchart)"
  191.     else
  192.         version="$(rpm -q bootchart --queryformat '%{VERSION}')"
  193.     fi
  194.     (    echo "version = $version"
  195.     echo "title = Boot chart for $(hostname) ($(date))"
  196.     echo "system.uname = $(uname -srvm)"
  197.     echo "system.release = $(lsb_release -sd)"
  198.     echo "system.cpu = $(grep '^model name' /proc/cpuinfo)"\
  199.          "($(grep -c '^model name' /proc/cpuinfo))"
  200.     echo "system.kernel.options = $(sed q /proc/cmdline)"
  201.     ) > header
  202. }
  203.  
  204. if [ $$ -eq 1 ]; then
  205.     # Started by the kernel.  Start the logger in background and exec
  206.     # init(1).
  207.     IN_INIT="yes"
  208.     echo "Starting bootchart logging"
  209.     start &
  210.     
  211.     # Optionally, an alternative init(1) process may be specified using
  212.     # the kernel command line (e.g. "bootchart_init=/sbin/initng")
  213.     init="/sbin/init"
  214.     for i in $@; do
  215.         if [ "${i%%=*}" = "bootchart_init" ]; then
  216.             init="${i#*=}"
  217.             break
  218.         fi
  219.                 if [ "${i%%=*}" = "init" ]; then
  220.             _init=${i#*=}
  221.             if test "$_init" != "/sbin/bootchartd"; then
  222.                            init="$_init"
  223.                         fi
  224.                         break
  225.                 fi
  226.     done
  227.     exec $init $*
  228. fi
  229.  
  230. case "$1" in
  231.     "init")
  232.         # Started by the init script
  233.         IN_INIT="yes"
  234.         echo "Starting bootchart logging"
  235.         start &
  236.         ;;
  237.     "start")
  238.         # Started by the user
  239.         shift
  240.         start $@
  241.         ;;
  242.     "stop")
  243.         # Signal all background processes to stop logging
  244.         killall -USR1 bootchartd
  245.         ;;
  246.     *)
  247.         echo $"Usage: $0 {init|start|stop}"
  248.         ;;
  249. esac
  250.  
  251.