#!/bin/bash
# This is the script to start and stop collection of lockmeter statisitcs
#
# reqparam value start|stop|report|postprocess|install Which action you'd like to perform
# optparam value Suffix - an optional string to append to the name of the log file 
#

. /etc/autobench.conf || . functions

if [ $# -lt 1 ]; then
  echo "You must specify start, stop, report, postprocess, or install"
  exit 1
fi

CMD="`echo $1 | tr -t A-Z a-z`"

if [ $# -eq 2 ]; then
  SUFFIX=".$2"
fi

function install_lockmeter() {
  # Install lockstat if it isn't already...
  if [ ! \( -d $AUTODIR/sources/lockstat -a -x $AUTODIR/sources/lockstat/lockstat \) ]; then
    $AUTODIR/scripts/profilers/install/lockmeter
  fi
}

case $CMD in
  install)
    if [ -f /proc/lockmeter ]; then
      install_lockmeter
    fi
    ;;
  start)
    install_lockmeter

    #start lockmeter
    if [ -f /proc/lockmeter ]; then
      pushd $AUTODIR/sources/lockstat > /dev/null
        ./lockstat off
        ./lockstat reset
        ./lockstat on
      popd > /dev/null
    else
      echo "Lockmeter not built into this kernel"
    fi
    ;;

  print-start)
    echo 'if [ -f /proc/lockmeter ]; then'
    echo '   $AUTODIR/sources/lockstat/lockstat off'
    echo '   $AUTODIR/sources/lockstat/lockstat reset'
    echo '   $AUTODIR/sources/lockstat/lockstat on'
    echo 'fi'
    ;;

  stop)
    #stop lockmeter
    if [ -f /proc/lockmeter ]; then
      pushd $AUTODIR/sources/lockstat > /dev/null
        ./lockstat off
      popd > /dev/null
    fi
    ;;

  print-stop)
    echo 'if [ -f /proc/lockmeter ]; then'
    echo '   $AUTODIR/sources/lockstat/lockstat off'
    echo 'fi'
    ;;

  report)
    #process the results
    if [ -f /proc/lockmeter ]; then
      pushd $AUTODIR/sources/lockstat > /dev/null
        find_system_map
        lockstat -m "$SYSTEM.MAP" print > $LOGDIR/analysis/lockstat.$UN_NUMBER$SUFFIX
      popd > /dev/null
    fi
    ;;

  postprocess)
    ;;

  *)
    echo "The first argument must be start, stop, prepare, postproces, or install"
    ;;
esac

