1#!/bin/ksh 2# #!/usr/bin/ksh 3# 4# sampleproc - sample processes on the CPUs. 5# Written using DTrace (Solaris 10 3/05). 6# 7# This program samples which process is on each CPU, at a particular 8# configurable rate. This can be used as an estimate for which process 9# is consuming the most CPU time. 10# 11# 09-Jul-2005, ver 0.70 (check for newer versions) 12# 13# USAGE: sampleproc [hertz] # hit Ctrl-C to end sample 14# 15# FIELDS: 16# PID Process ID 17# COMMAND Command name 18# COUNT Number of samples 19# PERCENT Percent of CPU usage 20# 21# BASED ON: /usr/demo/dtrace/prof.d 22# 23# SEE ALSO: 24# DTrace Guide "profile Provider" chapter (docs.sun.com) 25# 26# PORTIONS: Copyright (c) 2005 Brendan Gregg. 27# 28# CDDL HEADER START 29# 30# The contents of this file are subject to the terms of the 31# Common Development and Distribution License, Version 1.0 only 32# (the "License"). You may not use this file except in compliance 33# with the License. 34# 35# You can obtain a copy of the license at Docs/cddl1.txt 36# or http://www.opensolaris.org/os/licensing. 37# See the License for the specific language governing permissions 38# and limitations under the License. 39# 40# CDDL HEADER END 41# 42# 09-Jun-2005 Brendan Gregg Created this. 43 44### Usage 45function 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 57if (( $# == 0 )); then 58 hertz=100 59elif (( $# == 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 73else 74 usage 75fi 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 /* SOLARIS: normalize(@BigProc, 101 ((`ncpus * $1 * (this->end - self->start))/100000000)); */ 102 normalize(@BigProc, 103 ((`real_ncpus * $1 * (this->end - self->start))/100000000)); 104 printf("\n%5s %-20s %10s\n", "PID", "CMD", "PERCENT"); 105 printa("%5d %-20s %10@d\n", @BigProc); 106 } 107' $hertz 108 109