#!/bin/bash 

# $Id: psrun-raw,v 1.8 2006/01/21 22:46:27 rkufrin Exp $

############################################################################
#
#                    University of Illinois/NCSA
#                        Open Source License
# 
#          Copyright(C) 2003-2006, The Board of Trustees of the
#              University of Illinois. All rights reserved.
#
#                            Developed by:             
# 
#                        The PerfSuite Project
#            National Center for Supercomputing Applications
#              University of Illinois at Urbana-Champaign
# 
#                   http://perfsuite.ncsa.uiuc.edu/
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal with the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, 
# and/or sell copies of the Software, and to permit persons to whom the 
# Software is furnished to do so, subject to the following conditions:
# 
# + Redistributions of source code must retain the above copyright notice, 
#   this list of conditions and the following disclaimers.
# + Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimers in
#   the documentation and/or other materials provided with the distribution.
# + Neither the names of The PerfSuite Project, NCSA/University of Illinois
#   at Urbana-Champaign, nor the names of its contributors may be used to
#   endorse or promote products derived from this Software without specific
#   prior written permission.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS WITH THE SOFTWARE.
############################################################################

# psrun-raw: run a command using psrun but filter the output
#            so that only the raw performance event counts are
#            reported.
#
# Author: Rick Kufrin
# Date:   12/2003
#
# This file is part of PerfSuite.

#set -vx

if test $# -lt 1 ; then
    echo "Usage: `basename $0` [ psrun-options ] command [ args ]"
    exit 1
fi

PREFIX=/tmp/`basename $0`.$$
TMPFILE=$PREFIX.xml
export PS_HWPC_NOPID=1
export PS_HWPC_NOHOSTNAME=1

trap "/bin/rm -f $TMPFILE" 0

psrun -o $PREFIX $* 

if [ ! -f $TMPFILE ]; then
    echo ""
    echo "Error: the expected output file ($TMPFILE) does not exist."
    echo ""
    echo "Possible causes:"
    echo "  - psrun program is not in your PATH"
    echo "  - PerfSuite and PAPI \"lib\" directories are not in your LD_LIBRARY_PATH"
    echo "  - an error occurred within psrun (for example, invalid configuration file)"
    echo ""
    echo "Current settings:"
    echo ""
    echo "  PATH: $PATH"
    echo ""
    echo "  LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
    exit 1
fi

grep 'hwpcevent ' $TMPFILE  | \
	             sed -e 's/<hwpcevent type=\".*\" name=\"//' \
	                 -e 's/\" derived=\".*\">/ /' \
                         -e 's/<\/hwpcevent>//'
