#!/bin/sh
#
# Bootchart logger script
# Ziga Mahkovec  <ziga.mahkovec@klika.si>
#
# This script is used for data collection for the bootchart
# boot performance visualization tool (http://www.bootchart.org).
#
# Simplified and adapted for OpenBricks by
# Davide Cavalca <davide@geexbox.org>

VERSION="0.8gx"
BOOTLOG_LOCK=".lock"
SAMPLE_PERIOD=0.2
EXIT_PROC="enna"
BOOTLOG_DEST="/var/log/bootchart.tgz"

# Start the boot logger.
start()
{
  # Make sure only a single instance is running
  [ -f "$BOOTLOG_LOCK" ] && return

  # Create logging directory
  LOG_DIR="/tmp/bootchart.$$"
  mkdir -p $LOG_DIR
  cd "$LOG_DIR"
  > "$BOOTLOG_LOCK"
  
  # Enable process accounting if configured
  > kernel_pacct
  /usr/bin/accton kernel_pacct

  log_output "cat /proc/stat" proc_stat.log &
  log_output "cat /proc/diskstats" proc_diskstats.log &
  log_output "cat /proc/*/stat" proc_ps.log &
  wait_boot &
}


# Run the command ($1) every $SAMPLE_PERIOD seconds and append output to
# file ($2).  Mostly pure shell, so we avoid creating an excessive number of
# processes (thus polluting the process tree).
log_output()
{
  # Set the stop() trap.
  trap stop USR1

  local cmd="$1"
  local logfile="$2"
  while [ -f "$BOOTLOG_LOCK" ]; do
    # Write the time (in jiffies).
    read uptime < /proc/uptime
    uptime=${uptime%% [0-9]*}
    uptime=${uptime/./}
    echo $uptime

    # Log the command output
    eval $cmd 2>/dev/null
    echo
    sleep $SAMPLE_PERIOD
  done  >> "$logfile" || stop
}

# Wait for the boot process to end.
wait_boot()
{
  while [ -f "$BOOTLOG_LOCK" ]; do
    if [ -n "$( pidof $EXIT_PROC )" ]; then
      # Give the exit process some time to start
      sleep 5
      # Flush the log files
      stop
      return
    fi
    sleep 2
  done;
}


# Stop the boot logger.  The lock file is removed to force the loggers in
# background to exit.  Some final log files are created and then all log files
# from the tmpfs are packaged and stored in $BOOTLOG_DEST.
stop()
{
  if [ ! -f "$BOOTLOG_LOCK" ]; then
    echo "${0##*/} not running"
    return
  fi
  # Prevent concurrent stop() calls
  rm -f "$BOOTLOG_LOCK" || return
  sleep $SAMPLE_PERIOD
  sleep $SAMPLE_PERIOD

  # Stop process accounting if configured
  local pacct=
  /usr/bin/accton
  pacct=kernel_pacct

  # Write system information
  log_header

  # Package log files
  tar -zcf "$BOOTLOG_DEST" header $pacct *.log
}


# Log some basic information about the system.
log_header()
{
  (
    echo "version = $VERSION"
    echo "title = Boot chart for $(cat /proc/sys/kernel/hostname) ($( date ))"
    echo "system.uname = $( uname -srvm | sed q )"
    echo "system.release = $( cat /etc/issue|sed 's/Welcome to //'|sed 's/ (C).*//' )"

    # Get CPU count
    local cpucount=$(grep -c '^processor' /proc/cpuinfo)
    if [ $cpucount -gt 1 -a -n "$(grep 'sibling.*2' /proc/cpuinfo)" ]; then
      # Hyper-Threading enabled
      cpucount=$(( $cpucount / 2 ))
    fi
    if grep -q '^model name' /proc/cpuinfo; then
      echo "system.cpu = $( grep '^model name' /proc/cpuinfo | sed q )" "($cpucount)"
    else
      echo "system.cpu = $( grep '^cpu' /proc/cpuinfo | sed q )" "($cpucount)" 
    fi
    echo "system.kernel.options = $( sed q /proc/cmdline )"
  ) >> header
}

echo "Starting bootchart logging"
start &

