#!/usr/bin/sh
#
# dcache - Dcache statistics. Reformats cpustat output. Solaris 8+, sparc.
#
# 14-Aug-2005   ver 0.98
#
# USAGE: dcache [ -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.
#
# NOTES: Reports on D$ reads.
#
# 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
	exit 1
fi
sleep=$1
loop=$2
if [ "$sleep" = "" ]; then
	sleep=1
	loop=1
elif [ "$loop" = "" ]; then
	loop=65536
fi

#
#  Determine CPU types
#
type=`cpustat -h | awk '/CPU performance counter interface/ { print $6 }'`

#
#  Main
#
case $type in
'I&II')
	### UltraSPARC I&II
	cpustat -c pic0=DC_rd,pic1=DC_rd_hit $sleep $loop | awk '
		BEGIN { pagesize = 20; lines = pagesize }
		lines >= pagesize {
		   lines = 0
		   printf("%8s %3s %5s %9s %9s %7s\n",\
		    "D$  time","cpu","event","total","hit","%hit")
		}
		$1 !~ /time/ { 
		   total = $4
		   hits = $5
		   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 B of the "UltraSPARC I&II User's Manual"
	;;
'III'|'III+')
	### UltraSPARC III, III+ & IV
	cpustat -c pic0=DC_rd,pic1=DC_rd_miss $sleep $loop | awk '
		BEGIN { pagesize = 20; lines = pagesize }
		lines >= pagesize {
		   lines = 0
		   printf("%8s %3s %5s %9s %9s %7s\n",\
		    "D$  time","cpu","event","total","miss","%hit")
		}
		$1 !~ /time/ { 
		   total = $4
		   miss = $5
		   ratio = 100 * (total - miss) / total
		   printf("%8s %3s %5s %9s %9s %7.2f\n",$1,$2,$3,$4,$5,ratio)
		   lines++
		} '
        # For documentation on the CPU counters,
        # see "SPARC V9 JPS1 Implementation Supplement: Sun UltraSPARC-III"
	;;
*)
	echo "ERROR1: Sorry, CPU type $type unsupported. Try cpustat -h"
	;;
esac

