1N/A#!/usr/bin/ksh
1N/A#
1N/A# sampleproc - sample processes on the CPUs.
1N/A# Written using DTrace (Solaris 10 3/05).
1N/A#
1N/A# This program samples which process is on each CPU, at a particular
1N/A# configurable rate. This can be used as an estimate for which process
1N/A# is consuming the most CPU time.
1N/A#
1N/A# $Id: sampleproc 8 2007-08-06 05:55:26Z brendan $
1N/A#
1N/A# USAGE: sampleproc [hertz] # hit Ctrl-C to end sample
1N/A#
1N/A# FIELDS:
1N/A# PID Process ID
1N/A# COMMAND Command name
1N/A# COUNT Number of samples
1N/A# PERCENT Percent of CPU usage
1N/A#
1N/A# BASED ON: /usr/demo/dtrace/prof.d
1N/A#
1N/A# SEE ALSO:
2N/A# DTrace Guide "profile Provider" chapter (docs.oracle.com)
1N/A#
1N/A# PORTIONS: Copyright (c) 2005 Brendan Gregg.
1N/A#
1N/A# CDDL HEADER START
1N/A#
1N/A# The contents of this file are subject to the terms of the
1N/A# Common Development and Distribution License, Version 1.0 only
1N/A# (the "License"). You may not use this file except in compliance
1N/A# with the License.
1N/A#
1N/A# You can obtain a copy of the license at Docs/cddl1.txt
1N/A# or http://www.opensolaris.org/os/licensing.
1N/A# See the License for the specific language governing permissions
1N/A# and limitations under the License.
1N/A#
1N/A# CDDL HEADER END
1N/A#
1N/A# 09-Jun-2005 Brendan Gregg Created this.
1N/A# 09-Jul-2005 " " Last update.
1N/A
1N/A### Usage
1N/Afunction usage
1N/A{
1N/A cat <<-END >&2
1N/A USAGE: sampleproc [hertz]
1N/A eg,
1N/A sampleproc # defaults to 100 hertz
1N/A sampleproc 1000 # 1000 hertz
1N/A END
1N/A exit 1
1N/A}
1N/A
1N/A### Process arguments
1N/Aif (( $# == 0 )); then
1N/A hertz=100
1N/Aelif (( $# == 1 )); then
1N/A hertz=$1
1N/A if [[ "$hertz" = *[a-zA-Z]* ]]; then
1N/A print "ERROR2: $hertz hertz is invalid." >&2
1N/A exit 2
1N/A fi
1N/A if (( hertz > 5000 )); then
1N/A print "ERROR3: $hertz hertz is too fast (max 5000)." >&2
1N/A exit 3
1N/A fi
1N/A if (( hertz < 1 )); then
1N/A print "ERROR4: $hertz hertz is too low (min 1)." >&2
1N/A exit 4
1N/A fi
1N/Aelse
1N/A usage
1N/Afi
1N/A
1N/A### Run DTrace
1N/A/usr/sbin/dtrace -n '
1N/A #pragma D option quiet
1N/A
1N/A dtrace:::BEGIN
1N/A {
1N/A printf("Sampling at %d hertz... Hit Ctrl-C to end.\n",$1);
1N/A self->start = timestamp;
1N/A }
1N/A
1N/A profile:::profile-$1
1N/A {
1N/A @Proc[pid, execname] = count();
1N/A @BigProc[pid, execname] = sum(1000); /* dont ask */
1N/A }
1N/A
1N/A dtrace:::END
1N/A {
1N/A this->end = timestamp;
1N/A
1N/A printf("%5s %-20s %10s\n", "PID", "CMD", "COUNT");
1N/A printa("%5d %-20s %10@d\n", @Proc);
1N/A
1N/A normalize(@BigProc,
1N/A ((`ncpus_online * $1 * (this->end - self->start))/100000000));
1N/A printf("\n%5s %-20s %10s\n", "PID", "CMD", "PERCENT");
1N/A printa("%5d %-20s %10@d\n", @BigProc);
1N/A }
1N/A' $hertz