# bootchart - boot sequence auditing
#
# bootchart allows you to audit the boot sequence of your computer and
# generate a pretty chart of the processes run, including how long they
# took and how much CPU and I/O they used.
description   "boot sequence auditing"
start on virtual-filesystems
stop on stopped rc
script
    grep -q "profile" /proc/cmdline && { stop; exit 0; }
    grep -q "bootchart=disable" /proc/cmdline && { stop; exit 0; }
    if grep -q "bootchart=[0-9]*hz" /proc/cmdline
    then
   HZ=$(sed -e 's/.*bootchart=//;s/hz.*//' /proc/cmdline)
    else
   HZ=25
    fi
    mkdir -p /var/run/bootchart
    exec /lib/bootchart/collector $HZ /var/run/bootchart 2>/dev/null
end script
pre-stop script
    # Sleep for an extra 45s to allow enough time to chart the desktop
    # login
    [ "$UPSTART_STOP_EVENTS" = "stopped" ] && sleep 45
end script
post-stop script
    if [ -d /dev/.bootchart ]
    then
   LOGS=/dev/.bootchart/log
    else
   LOGS=/var/run/bootchart
    fi
    # Figure out name for the chart
    base="$(hostname -s)-$(lsb_release -sc)-$(date +%Y%m%d)"
    count=1
    while [ -e "/var/log/bootchart/$base-$count.tgz" -o -e "/var/log/bootchart/$base-$count.png" -o -e "/var/log/bootchart/$base-$count.svg" ]
    do
   count=$(( $count + 1 ))
    done
    BASE="/var/log/bootchart/$base-$count"
    TARBALL="$BASE.tgz"
    # Gather the output into the tarball
    /lib/bootchart/gather "$TARBALL" "$LOGS"
    # Generate SVG and optionally PNG if pybootchartgui is installed
    if [ -x /usr/bin/bootchart ]
    then
   if grep -q "bootchart=svg" /proc/cmdline
   then
       format=svg
   else
       format=png
   fi
   bootchart --format=$format \
      --crop-after=compiz,metacity,mutter,kwin,xfwm4 \
      --annotate=ureadahead,mountall,hostname,hwclock \
      --annotate=Xorg \
      --annotate=gdm-session-worker \
      --output="/var/log/bootchart" "$TARBALL"
    fi
    # Clean up
    rm -rf $LOGS
    if [ -d /dev/.bootchart ]
    then
   umount /dev/.bootchart/proc
   rm -rf /dev/.bootchart
    fi
end script