#! /bin/sh
#
# Written by Mark Goodwin (markgw@sgi.com), December 2000.
#
# Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
#
# This simple script captures a PCP archive for replay with any
# PCP monitoring tool (including locktop).
#

[ $# -ne 1 ] && echo "Usage: capture_archive archivename" && exit 1
archivename="$1"

if [ ! -f /etc/pcp.env ]
then
    echo "Error: pcp is not installed."
    echo "Get it from http://oss.sgi.com/projects/pcp"
fi

# source the PCP environment
. /etc/pcp.env

# check the pmda_lockstat is installed and available
if pminfo -f lockstat.detail.time.wait >/dev/null 2>&1
then
	:
else
    echo "Error: you need to enable lockmetering in the kernel"
    echo "with \"../lockstat on\" and then configure the PCP"
    echo "lockstat agent with \"cd ../pmda_lockstat; ./Install\"."
    exit 1
fi

[ -f $archivename.0 ] && echo "Error: archive \"$archivename\" already exists" && exit 1

echo -n "Starting pmlogger to create \"$archivename\" ..."

cat <<EOFEOF | pmlogger "$archivename" &
log mandatory on once { hinv }
log mandatory on 5 seconds {
    lockstat
    kernel.all.cpu
    kernel.percpu.cpu
}
EOFEOF
pid=$!

sleep 2
if fgrep "Log finished" pmlogger.log >/dev/null 2>&1
then
    echo "Error ... tail of pmlogger.log follows:"
    tail pmlogger.log
    exit 1
else
    echo " OK."
fi

echo "When your workload has finished, use this command to kill pmlogger:"
echo "# kill -TERM $pid"

exit 0
