#!/bin/bash
count=-1; interval=1; opt_persec=0; opt_clear=1
while getopts Chs name
do
case $name in
C) opt_clear=0 ;;
s) opt_persec=1 ;;
h|?) cat <<-END >&2
USAGE: topsyscall [-s] [interval [count]]
-C -s eg,
topsyscall topsyscall 5 END
exit 1
esac
done
shift $(( $OPTIND - 1 ))
if [[ "$1" > 0 ]]; then
interval=$1; shift
fi
if [[ "$1" > 0 ]]; then
count=$1; shift
fi
if (( opt_clear )); then
clearstr=`clear`
else
clearstr=.
fi
/usr/sbin/dtrace -n '
/* constants */
inline int OPT_clear = '$opt_clear';
inline int OPT_persec = '$opt_persec';
inline int INTERVAL = '$interval';
inline int COUNTER = '$count';
inline int SCREEN = 20;
inline string CLEAR = "'$clearstr'";
/* variables */
dtrace:::BEGIN
{
secs = INTERVAL;
counts = COUNTER;
printf("Tracing... Please wait.\n");
}
/* record syscall event */
syscall:::entry
{
@Name[probefunc] = count();
@Total = count();
}
/* timer */
profile:::tick-1sec
{
secs--;
}
/* update screen */
profile:::tick-1sec
/secs == 0/
{
/* fetch load averages */
/* SOLARIS:
this->load1a = `hp_avenrun[0] / 65536;
this->load5a = `hp_avenrun[1] / 65536;
this->load15a = `hp_avenrun[2] / 65536;
this->load1b = ((`hp_avenrun[0] % 65536) * 100) / 65536;
this->load5b = ((`hp_avenrun[1] % 65536) * 100) / 65536;
this->load15b = ((`hp_avenrun[2] % 65536) * 100) / 65536;
*/
this->fscale = `averunnable.fscale;
this->load1a = `averunnable.ldavg[0] / this->fscale;
this->load5a = `averunnable.ldavg[1] / this->fscale;
this->load15a = `averunnable.ldavg[2] / this->fscale;
this->load1b = ((`averunnable.ldavg[0] % this->fscale) * 100) / this->fscale;
this->load5b = ((`averunnable.ldavg[1] % this->fscale) * 100) / this->fscale;
this->load15b = ((`averunnable.ldavg[2] % this->fscale) * 100) / this->fscale;
/* clear screen */
OPT_clear ? printf("%s", CLEAR) : 1;
/* print load average */
printf("%Y, load average: %d.%02d, %d.%02d, %d.%02d",
walltimestamp, this->load1a, this->load1b, this->load5a,
this->load5b, this->load15a, this->load15b);
/* calculate per second values if needed */
OPT_persec ? normalize(@Total, INTERVAL) : 1;
OPT_persec ? normalize(@Name, INTERVAL) : 1;
/* print syscall count */
printf(" %s: ", OPT_persec ? "syscalls/s" : "syscalls");
printa("%@d\n",@Total);
/* print report */
trunc(@Name, SCREEN);
printf("\n %-25s %12s\n", "SYSCALL",
OPT_persec ? "COUNT/s" : "COUNT");
printa(" %-25s %@12d\n", @Name);
printf("\n");
/* reset variables */
trunc(@Name);
clear(@Total);
secs = INTERVAL;
counts--;
}
/*
* End of program
*/
profile:::tick-1sec
/counts == 0/
{
exit(0);
}
/*
* Cleanup for Ctrl-C
*/
dtrace:::END
{
trunc(@Name);
trunc(@Total);
}
'