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