xref: /netbsd-src/external/cddl/dtracetoolkit/dist/Proc/sampleproc (revision c29d51755812ace2e87aeefdb06cb2b4dac7087a)
1*c29d5175Schristos#!/usr/bin/ksh
2*c29d5175Schristos#
3*c29d5175Schristos# sampleproc - sample processes on the CPUs.
4*c29d5175Schristos#              Written using DTrace (Solaris 10 3/05).
5*c29d5175Schristos#
6*c29d5175Schristos# This program samples which process is on each CPU, at a particular
7*c29d5175Schristos# configurable rate. This can be used as an estimate for which process
8*c29d5175Schristos# is consuming the most CPU time.
9*c29d5175Schristos#
10*c29d5175Schristos# $Id: sampleproc,v 1.1.1.1 2015/09/30 22:01:09 christos Exp $
11*c29d5175Schristos#
12*c29d5175Schristos# USAGE:	sampleproc [hertz]	# hit Ctrl-C to end sample
13*c29d5175Schristos#
14*c29d5175Schristos# FIELDS:
15*c29d5175Schristos#		PID        Process ID
16*c29d5175Schristos#		COMMAND    Command name
17*c29d5175Schristos#		COUNT      Number of samples
18*c29d5175Schristos#		PERCENT    Percent of CPU usage
19*c29d5175Schristos#
20*c29d5175Schristos# BASED ON: /usr/demo/dtrace/prof.d
21*c29d5175Schristos#
22*c29d5175Schristos# SEE ALSO:
23*c29d5175Schristos#           DTrace Guide "profile Provider" chapter (docs.sun.com)
24*c29d5175Schristos#
25*c29d5175Schristos# PORTIONS: Copyright (c) 2005 Brendan Gregg.
26*c29d5175Schristos#
27*c29d5175Schristos# CDDL HEADER START
28*c29d5175Schristos#
29*c29d5175Schristos#  The contents of this file are subject to the terms of the
30*c29d5175Schristos#  Common Development and Distribution License, Version 1.0 only
31*c29d5175Schristos#  (the "License").  You may not use this file except in compliance
32*c29d5175Schristos#  with the License.
33*c29d5175Schristos#
34*c29d5175Schristos#  You can obtain a copy of the license at Docs/cddl1.txt
35*c29d5175Schristos#  or http://www.opensolaris.org/os/licensing.
36*c29d5175Schristos#  See the License for the specific language governing permissions
37*c29d5175Schristos#  and limitations under the License.
38*c29d5175Schristos#
39*c29d5175Schristos# CDDL HEADER END
40*c29d5175Schristos#
41*c29d5175Schristos# 09-Jun-2005   Brendan Gregg   Created this.
42*c29d5175Schristos# 09-Jul-2005	   "      "	Last update.
43*c29d5175Schristos
44*c29d5175Schristos### Usage
45*c29d5175Schristosfunction usage
46*c29d5175Schristos{
47*c29d5175Schristos        cat <<-END >&2
48*c29d5175Schristos	USAGE: sampleproc [hertz]
49*c29d5175Schristos	   eg,
50*c29d5175Schristos	       sampleproc               # defaults to 100 hertz
51*c29d5175Schristos	       sampleproc 1000          # 1000 hertz
52*c29d5175Schristos	END
53*c29d5175Schristos	exit 1
54*c29d5175Schristos}
55*c29d5175Schristos
56*c29d5175Schristos### Process arguments
57*c29d5175Schristosif (( $# == 0 )); then
58*c29d5175Schristos        hertz=100
59*c29d5175Schristoselif (( $# == 1 )); then
60*c29d5175Schristos	hertz=$1
61*c29d5175Schristos	if [[ "$hertz" = *[a-zA-Z]* ]]; then
62*c29d5175Schristos		print "ERROR2: $hertz hertz is invalid." >&2
63*c29d5175Schristos		exit 2
64*c29d5175Schristos	fi
65*c29d5175Schristos	if (( hertz > 5000 )); then
66*c29d5175Schristos		print "ERROR3: $hertz hertz is too fast (max 5000)." >&2
67*c29d5175Schristos		exit 3
68*c29d5175Schristos	fi
69*c29d5175Schristos	if (( hertz < 1 )); then
70*c29d5175Schristos		print "ERROR4: $hertz hertz is too low (min 1)." >&2
71*c29d5175Schristos		exit 4
72*c29d5175Schristos	fi
73*c29d5175Schristoselse
74*c29d5175Schristos	usage
75*c29d5175Schristosfi
76*c29d5175Schristos
77*c29d5175Schristos### Run DTrace
78*c29d5175Schristos/usr/sbin/dtrace -n '
79*c29d5175Schristos #pragma D option quiet
80*c29d5175Schristos
81*c29d5175Schristos dtrace:::BEGIN
82*c29d5175Schristos {
83*c29d5175Schristos	printf("Sampling at %d hertz... Hit Ctrl-C to end.\n",$1);
84*c29d5175Schristos	self->start = timestamp;
85*c29d5175Schristos }
86*c29d5175Schristos
87*c29d5175Schristos profile:::profile-$1
88*c29d5175Schristos {
89*c29d5175Schristos	@Proc[pid, execname] = count();
90*c29d5175Schristos	@BigProc[pid, execname] = sum(1000); /* dont ask */
91*c29d5175Schristos }
92*c29d5175Schristos
93*c29d5175Schristos dtrace:::END
94*c29d5175Schristos {
95*c29d5175Schristos	this->end = timestamp;
96*c29d5175Schristos
97*c29d5175Schristos	printf("%5s %-20s %10s\n", "PID", "CMD", "COUNT");
98*c29d5175Schristos	printa("%5d %-20s %10@d\n", @Proc);
99*c29d5175Schristos
100*c29d5175Schristos	normalize(@BigProc,
101*c29d5175Schristos	    ((`ncpus_online * $1 * (this->end - self->start))/100000000));
102*c29d5175Schristos	printf("\n%5s %-20s %10s\n", "PID", "CMD", "PERCENT");
103*c29d5175Schristos	printa("%5d %-20s %10@d\n", @BigProc);
104*c29d5175Schristos }
105*c29d5175Schristos' $hertz
106