#!/bin/tcsh -f
#
# choose_strategy_by_heuristics <CPUNUM> [-a] <profile>
#
# Reads given profile and outputs first depth recommended
# by the evaluate_profile program for the given CPU number.
# Profile files must be in ranked format.
# The "-a" switch uses the average subtree heuristic, otherwise
# the largest possible subtree heuristic is applied.
#
# Stefan Haenssgen   05-feb-98
#
# 05-feb-98	Adapted from do_profile_evaluation script
#		Removed ranking-lookup etc (we don't have a record of
#		 all possible outcomes for real applications)
#

set evalprog="evaluate_profile"		# Name of evaluation program
set tmpfile="/tmp/doprofeval_$$"
set evaloptions="-a"			# Default analysis heuristics
set heur="average subtree"

if ($#argv < 2) then
  echo "$0 - need CPU number and profile filename to evaluate"
  exit 1
endif

if ($1 =~ [0-9]*) then
  set cpunum=$1
  shift
else
  echo "$0 - need CPU number"
  exit 2
endif

if ("$1" == "-a") then
  set evaloptions="-a"
  set heur="average subtree"
  shift
endif
if ("$1" == "-l") then
  set evaloptions=""
  set heur="largest possible subtree"
  shift
endif

echo "Analyzing profile for $cpunum CPUs, using $heur ..."
echo ""

foreach profile ($*)
  if (! -f $profile) then
    echo "Error, couldn't open profile $profile"
    exit 3
  else  
    $evalprog $evaloptions -c $cpunum < $profile > $tmpfile
    set procname=`fgrep "profiled procedure" $tmpfile | cut -d\' -f2`
    set merged=`fgrep "note - merged" $tmpfile`
    if ("$merged" != "") then
      set mergenum=`echo $merged | cut -d' ' -f4`
      echo "(profile merged from $mergenum procedures)"
    endif
    set iterations=`fgrep "iterations" $tmpfile | cut -d' ' -f3`
    if ($iterations > 1) then
      echo "(profile covers $iterations iterations)"
    endif
    set recommended=`fgrep "RECOMMENDED:" $tmpfile | head -1`
    if ("$recommended" == "") then
      echo "No depth recommended at all"
    else
      set depth=`echo $recommended | cut -d' ' -f4`
      echo "First recommended depth for procedure '$procname' is $depth"
    endif
    echo ""
    rm -f $tmpfile
  endif
end
