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