#!/usr/bin/sh
#
# icache_x86 - Icache statistics. Reformats cpustat output. Solaris 8+, x86.
#
# NOTE: The values are not perfect - this is the best data we can read from
#  a generic P6 family processor. Vendor specific processors can provide 
#  extra and more accurate performance counters (see end of script).
#  (The Icache hit ratios appear higher than they should be).
#
# 14-Aug-2005   ver 0.70
#
# USAGE: icache_x86 [ -h | [interval [count]] ]
#
# SEE ALSO: cpustat -h
#           man cpustat
#
# WARNING: Only one cpustat/cputrack can be run at one time. 
#  Must be run as root.
#
# COPYRIGHT: Copyright (c) 2003, 2004 Brendan Gregg.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#  (http://www.gnu.org/copyleft/gpl.html)
#
# Author: Brendan Gregg  [Sydney, Australia]
#
# 11-Mar-2004	Brendan Gregg	Created this.

PATH=/usr/bin:/usr/sbin


#
#  Command line arguments
#
if [ x$1 = "x-h" -o x$1 = "x--help" ]; then
	echo "USAGE: $0 [ -h | [interval [count]] ]" >&2
	echo "   eg, $0		# run a 1 second sample" >&2
	echo "       $0 1 5	# 5 x 1 second samples" >&2
	echo "NOTE: values are not perfect, read this script for details." >&2
	exit 1
fi
sleep=$1
loop=$2
if [ "$sleep" = "" ]; then
	sleep=1
	loop=1
elif [ "$loop" = "" ]; then
	loop=65536
fi


#
#  Main
#
cpustat -c pic0=ifu_ifetch,pic1=ifu_ifetch_miss $sleep $loop | awk '
	BEGIN { pagesize = 20; lines = pagesize }
	lines >= pagesize {
	   lines = 0
	   printf("%8s %3s %5s %9s %9s %7s\n",\
	    "I$  time","cpu","event","total","miss","%hit")
	}
	$1 !~ /time/ { 
	   total = $4
	   misses = $5
	   hits = total - misses
	   ratio = 100 * hits / total
	   printf("%8s %3s %5s %9s %9s %7.2f\n",$1,$2,$3,$4,$5,ratio)
	   lines++
	}
'
# For documentation on the CPU counters, 
# see Appendix A.1 of the "Intel Architecture Software Developer's Manual"

