#!/bin/bash
#
# OProfile reporting script for hybrid application use.
#
# Use the oprofileh.sh script to gather performance data on an application, 
# then this script can be used to create the report(s).
#
# -----------------------------------------------------------------------------
# Licensed Materials - Property of IBM
# 5724-S84
# (C) Copyright IBM Corp. 2007,2007 All Rights Reserved
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with
# IBM Corp.
#

if [ `uname -m` != "x86_64" ] ; then
	echo "This program must run on an Opteron node of a hybrid system."
	exit 1
fi

RUNID=""
DELETE=""
PRINT=""
HOST_REPORT=""
CELL_REPORT=""

TEMP=`getopt -o dhp --long help,delete,print,cell,host,cell-options:,host-options:,runid: -n '$SCRIPT' -- "$@"`

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

eval set -- "$TEMP"

while true ; do
	case "$1" in
		-h|--help)
			printf "%s\n" "usage: oprofilerpt --runid=ID [options] opreport|opannotate|opgprof"
			printf "%s\n" "  -h,--help		print this help"
			printf "%s\n" "  -d,--delete		delete OProfile session data after report"
			printf "%s\n" "  -p,--print		print a copy of the OProfile report(s)"
			printf "%s\n" "  --cell		create cell report(s)"
			printf "%s\n" "  --host		create host report"
			printf "%s\n" "  --cell-options=OPTS	options for cell report"
			printf "%s\n" "  --host-options=OPTS	options for host report"
			exit 0 ;;
		-d|--delete)
			DELETE="true"
			shift 1 ;;
		-p|--print)
			PRINT="true"
			shift 1 ;;
		--cell)
			CELL_REPORT="true"
			shift 1 ;;
		--host)
			HOST_REPORT="true"
			shift 1 ;;
		--cell-options)
			CELL_OPTIONS=$2
			CELL_REPORT="true"
			shift 2 ;;
		--host-options)
			HOST_OPTIONS=$2
			HOST_REPORT="true"
			shift 2 ;;
		--runid)
			RUNID=$2
			shift 2 ;;
		--)
			shift ; break ;;
		*) 
			echo "Internal error" ; exit 1 ;;
	esac
done
echo "Remaining arguments:"
for arg do
	echo '-->'"\`$arg'"
	PROGRAM="$arg" 
done

if [ "$PROGRAM" = "" ] ;	then 
	echo "Must specify a program to run on the command line."
	exit 1
fi

if [ "$RUNID" = "" ] ; 		then 
	echo "Must specify --runid option on the command line."
	exit 1
fi

# Application Environment Variables
if [ ! "$HYBRID_PERF_TOOLS_DIR" ] ; then
  export HYBRID_PERF_TOOLS_DIR=$SDK_PROTOTYPE_ROOT/usr/bin
fi
source $HYBRID_PERF_TOOLS_DIR/perfToolHostSetup

if [ "$SCRIPT_DEBUG" ] ; then
	echo "oprofilerpt environment:"
	echo "PROGRAM     = $PROGRAM"
	echo "RUNID       = $RUNID"
	echo "CELL_OPTIONS= $CELL_OPTIONS"
	echo "HOST_OPTIONS= $HOST_OPTIONS"
fi

if [ "$HOST_REPORT" = "true" ] ; then

        HOST_DATA_DIR=$PERF_DATA_DIR/oprofile/$RUNID/x86_64/`$hostname`
	echo "OProfile x86_64 data directory: $HOST_DATA_DIR"

	sudo mkdir -p "$HOST_DATA_DIR"

	if [ "$PROGRAM" = "opannotate" ] ; then
	    OUTPUT="$HOST_DATA_DIR/annotate"
	else
	    OUTPUT="$HOST_DATA_DIR/$PROGRAM.out"
	fi
	echo "Output will be stored in: $OUTPUT"

#	$PROGRAM $HOST_OPTIONS -o $OUTPUT --session-dir=$HOST_DATA_DIR
	sudo $PROGRAM $HOST_OPTIONS -o $OUTPUT

	if [ "$PROGRAM" != "opannotate" ] && [ "$PRINT" = "true" ] ; then
	    cat "$HOST_DATA_DIR/$PROGRAM.out"
	fi

	if [ "$DELETE" = "true" ] ; then
	    echo "--session-dir not supported yet."
#	    sudo opcontrol --reset --session-dir=$HOST_DATA_DIR
	fi
fi

if [ "$CELL_REPORT" = "true" ] ; then

    for CELLNAME in `ls $PERF_DATA_DIR/oprofile/$RUNID/cbe`; do

	echo "Running report for CBE node: $CELLNAME"

        CELL_DATA_DIR=$PERF_DATA_DIR/oprofile/$RUNID/cbe/$CELLNAME
	echo "OProfile CBE data directory: $CELL_DATA_DIR"

	if [ "$PROGRAM" = "opannotate" ] ; then
	    OUTPUT="$CELL_DATA_DIR/annotate"
	else
	    OUTPUT="$CELL_DATA_DIR/$PROGRAM.out"
	fi
	echo "Output will be stored in: $OUTPUT"

	ssh $CELLNAME sudo $PROGRAM $CELL_OPTIONS -o $OUTPUT --session-dir=$CELL_DATA_DIR

	if [ "$PROGRAM" != "opannotate" ] && [ "$PRINT" = "true" ] ; then
	    cat "$CELL_DATA_DIR/$PROGRAM.out"
	fi

	if [ "$DELETE" = "true" ] ; then
	    ssh $CELLNAME sudo opcontrol --reset --session-dir=$CELL_DATA_DIR
	fi

    done
fi

echo "$result"
exit $result

