1 1.1 christos #!/usr/bin/sh 2 1.1 christos # 3 1.1 christos # dappprof - profile user and library function usage. 4 1.1 christos # Written using DTrace (Solaris 10 3/05). 5 1.1 christos # 6 1.1 christos # The default output traces user functions as they are called. Options 7 1.1 christos # can be used to examine libraries and timestamps. 8 1.1 christos # 9 1.1 christos # $Id: dappprof,v 1.1.1.1 2015/09/30 22:01:09 christos Exp $ 10 1.1 christos # 11 1.1 christos # USAGE: dappprof [-acehoTU] [-u lib] { -p PID | command } 12 1.1 christos # 13 1.1 christos # -p PID # examine this PID 14 1.1 christos # -a # print all details 15 1.1 christos # -c # print call counts 16 1.1 christos # -e # print elapsed times (us) 17 1.1 christos # -o # print on cpu times (us) 18 1.1 christos # -T # print totals 19 1.1 christos # -u lib # trace this library instead 20 1.1 christos # -U # trace all libraries + user functions 21 1.1 christos # -b bufsize # dynamic variable buf size (default is "4m") 22 1.1 christos # eg, 23 1.1 christos # dappprof df -h # run and examine the "df -h" command 24 1.1 christos # dappprof -p 1871 # examine PID 1871 25 1.1 christos # 26 1.1 christos # The elapsed times are interesting, to help identify calls that take 27 1.1 christos # some time to complete (during which the process may have context 28 1.1 christos # switched off the CPU). 29 1.1 christos # 30 1.1 christos # SEE ALSO: dapptrace # DTraceToolkit 31 1.1 christos # dtruss # DTraceToolkit 32 1.1 christos # apptrace 33 1.1 christos # 34 1.1 christos # COPYRIGHT: Copyright (c) 2005 Brendan Gregg. 35 1.1 christos # 36 1.1 christos # CDDL HEADER START 37 1.1 christos # 38 1.1 christos # The contents of this file are subject to the terms of the 39 1.1 christos # Common Development and Distribution License, Version 1.0 only 40 1.1 christos # (the "License"). You may not use this file except in compliance 41 1.1 christos # with the License. 42 1.1 christos # 43 1.1 christos # You can obtain a copy of the license at Docs/cddl1.txt 44 1.1 christos # or http://www.opensolaris.org/os/licensing. 45 1.1 christos # See the License for the specific language governing permissions 46 1.1 christos # and limitations under the License. 47 1.1 christos # 48 1.1 christos # CDDL HEADER END 49 1.1 christos # 50 1.1 christos # Author: Brendan Gregg [Sydney, Australia] 51 1.1 christos # 52 1.1 christos # 16-May-2005 Brendan Gregg Created this. 53 1.1 christos # 17-Jul-2005 " " Last update. 54 1.1 christos # 55 1.1 christos 56 1.1 christos 57 1.1 christos ############################## 58 1.1 christos # --- Process Arguments --- 59 1.1 christos # 60 1.1 christos 61 1.1 christos ### Default variables 62 1.1 christos opt_totals=0; opt_pid=0; pid=0; opt_lib=0; lib="" 63 1.1 christos opt_elapsed=0; opt_cpu=0; opt_counts=0; opt_liball=0 64 1.1 christos opt_command=0; command=""; opt_buf=0; buf="4m" 65 1.1 christos 66 1.1 christos ### Process options 67 1.1 christos while getopts ab:cehop:Tu:U name 68 1.1 christos do 69 1.1 christos case $name in 70 1.1 christos a) opt_liball=1; opt_counts=1; opt_elapsed=1; opt_cpu=1 71 1.1 christos opt_totals=1 ;; 72 1.1 christos b) opt_buf=1; buf=$OPTARG ;; 73 1.1 christos p) opt_pid=1; pid=$OPTARG ;; 74 1.1 christos u) opt_lib=1; lib=$OPTARG ;; 75 1.1 christos U) opt_liball=1 ;; 76 1.1 christos c) opt_counts=1 ;; 77 1.1 christos e) opt_elapsed=1 ;; 78 1.1 christos o) opt_cpu=1 ;; 79 1.1 christos T) opt_totals=1 ;; 80 1.1 christos h|?) cat <<-END >&2 81 1.1 christos USAGE: dappprof [-cehoTU] [-u lib] { -p PID | command } 82 1.1 christos 83 1.1 christos -p PID # examine this PID 84 1.1 christos -a # print all details 85 1.1 christos -c # print syscall counts 86 1.1 christos -e # print elapsed times (us) 87 1.1 christos -o # print on cpu times 88 1.1 christos -T # print totals 89 1.1 christos -u lib # trace this library instead 90 1.1 christos -U # trace all libraries + user funcs 91 1.1 christos -b bufsize # dynamic variable buf size 92 1.1 christos eg, 93 1.1 christos dappprof df -h # run and examine "df -h" 94 1.1 christos dappprof -p 1871 # examine PID 1871 95 1.1 christos dappprof -ap 1871 # print all data 96 1.1 christos END 97 1.1 christos exit 1 98 1.1 christos esac 99 1.1 christos done 100 1.1 christos shift `expr $OPTIND - 1` 101 1.1 christos 102 1.1 christos ### Option logic 103 1.1 christos if [ $opt_pid -eq 0 ]; then 104 1.1 christos opt_command=1 105 1.1 christos if [ "$*" = "" ]; then 106 1.1 christos $0 -h 107 1.1 christos exit 108 1.1 christos fi 109 1.1 christos command="$*" 110 1.1 christos fi 111 1.1 christos if [ $opt_elapsed -eq 0 -a $opt_cpu -eq 0 -a $opt_counts -eq 0 ]; then 112 1.1 christos opt_elapsed=1; 113 1.1 christos fi 114 1.1 christos 115 1.1 christos 116 1.1 christos ### Probe logic 117 1.1 christos if [ $opt_liball -eq 1 ]; then 118 1.1 christos probe_entry='pid$target:::entry' 119 1.1 christos probe_return='pid$target:::return' 120 1.1 christos elif [ $opt_lib -eq 1 ]; then 121 1.1 christos probe_entry='pid$target:'$lib'::entry' 122 1.1 christos probe_return='pid$target:'$lib'::return' 123 1.1 christos else 124 1.1 christos probe_entry='pid$target:a.out::entry' 125 1.1 christos probe_return='pid$target:a.out::return' 126 1.1 christos fi 127 1.1 christos 128 1.1 christos ################################# 129 1.1 christos # --- Main Program, DTrace --- 130 1.1 christos # 131 1.1 christos 132 1.1 christos ### Define D Script 133 1.1 christos dtrace=' 134 1.1 christos #pragma D option quiet 135 1.1 christos 136 1.1 christos /* 137 1.1 christos * Command line arguments 138 1.1 christos */ 139 1.1 christos inline int OPT_command = '$opt_command'; 140 1.1 christos inline int OPT_liball = '$opt_liball'; 141 1.1 christos inline int OPT_elapsed = '$opt_elapsed'; 142 1.1 christos inline int OPT_cpu = '$opt_cpu'; 143 1.1 christos inline int OPT_counts = '$opt_counts'; 144 1.1 christos inline int OPT_totals = '$opt_totals'; 145 1.1 christos inline int OPT_pid = '$opt_pid'; 146 1.1 christos inline int PID = '$pid'; 147 1.1 christos inline string NAME = "'$pname'"; 148 1.1 christos 149 1.1 christos dtrace:::BEGIN 150 1.1 christos /! OPT_command/ 151 1.1 christos { 152 1.1 christos printf("Tracing... Hit Ctrl-C to end...\n"); 153 1.1 christos } 154 1.1 christos 155 1.1 christos /* 156 1.1 christos * Save syscall entry info 157 1.1 christos */ 158 1.1 christos '$probe_entry' 159 1.1 christos { 160 1.1 christos /* set function depth */ 161 1.1 christos this->fdepth = ++fdepth[probefunc]; 162 1.1 christos 163 1.1 christos /* set start details */ 164 1.1 christos self->start[probefunc,this->fdepth] = timestamp; 165 1.1 christos self->vstart[probefunc,this->fdepth] = vtimestamp; 166 1.1 christos 167 1.1 christos /* count occurances */ 168 1.1 christos OPT_counts && OPT_liball ? @Counts[probemod,probefunc] = count() : 1; 169 1.1 christos OPT_counts && ! OPT_liball ? @Counts[probefunc] = count() : 1; 170 1.1 christos OPT_counts && OPT_totals && OPT_liball ? 171 1.1 christos @Counts["TOTAL:",""] = count() : 1; 172 1.1 christos OPT_counts && OPT_totals && ! OPT_liball ? 173 1.1 christos @Counts["TOTAL:"] = count() : 1; 174 1.1 christos } 175 1.1 christos 176 1.1 christos /* 177 1.1 christos * Print return data 178 1.1 christos */ 179 1.1 christos /* print 3 arg output - default */ 180 1.1 christos '$probe_return' 181 1.1 christos /self->start[probefunc,fdepth[probefunc]]/ 182 1.1 christos { 183 1.1 christos /* fetch function depth */ 184 1.1 christos this->fdepth = fdepth[probefunc]; 185 1.1 christos 186 1.1 christos /* calculate elapsed time */ 187 1.1 christos this->elapsed = timestamp - self->start[probefunc,this->fdepth]; 188 1.1 christos self->start[probefunc,this->fdepth] = 0; 189 1.1 christos this->cpu = vtimestamp - self->vstart[probefunc,this->fdepth]; 190 1.1 christos self->vstart[probefunc,this->fdepth] = 0; 191 1.1 christos 192 1.1 christos /* save elapsed times */ 193 1.1 christos OPT_elapsed && OPT_liball ? 194 1.1 christos @Elapsed[probemod,probefunc] = sum(this->elapsed) : 1; 195 1.1 christos OPT_elapsed && ! OPT_liball ? 196 1.1 christos @Elapsed[probefunc] = sum(this->elapsed) : 1; 197 1.1 christos OPT_elapsed && OPT_totals && OPT_liball ? 198 1.1 christos @Elapsed["TOTAL:",""] = sum(this->elapsed) : 1; 199 1.1 christos OPT_elapsed && OPT_totals && ! OPT_liball ? 200 1.1 christos @Elapsed["TOTAL:"] = sum(this->elapsed) : 1; 201 1.1 christos 202 1.1 christos /* save cpu times */ 203 1.1 christos OPT_cpu && OPT_liball ? @CPU[probemod,probefunc] = sum(this->cpu) : 1; 204 1.1 christos OPT_cpu && ! OPT_liball ? @CPU[probefunc] = sum(this->cpu) : 1; 205 1.1 christos OPT_cpu && OPT_totals && OPT_liball ? 206 1.1 christos @CPU["TOTAL:",""] = sum(this->cpu) : 1; 207 1.1 christos OPT_cpu && OPT_totals && ! OPT_liball ? 208 1.1 christos @CPU["TOTAL:"] = sum(this->cpu) : 1; 209 1.1 christos 210 1.1 christos } 211 1.1 christos 212 1.1 christos /* print counts */ 213 1.1 christos dtrace:::END 214 1.1 christos { 215 1.1 christos /* print counts */ 216 1.1 christos OPT_counts ? printf("\n%-49s %16s\n","CALL","COUNT") : 1; 217 1.1 christos OPT_counts && OPT_liball ? printa("%-16s %-32s %@16d\n",@Counts) : 1; 218 1.1 christos OPT_counts && ! OPT_liball ? printa("%-49s %@16d\n",@Counts) : 1; 219 1.1 christos 220 1.1 christos /* print elapsed times */ 221 1.1 christos OPT_elapsed ? printf("\n%-49s %16s\n","CALL","ELAPSED") : 1; 222 1.1 christos OPT_elapsed && OPT_liball ? printa("%-16s %-32s %@16d\n",@Elapsed) : 1; 223 1.1 christos OPT_elapsed && ! OPT_liball ? printa("%-49s %@16d\n",@Elapsed) : 1; 224 1.1 christos 225 1.1 christos /* print cpu times */ 226 1.1 christos OPT_cpu ? printf("\n%-49s %16s\n","CALL","CPU") : 1; 227 1.1 christos OPT_cpu && OPT_liball ? printa("%-16s %-32s %@16d\n",@CPU) : 1; 228 1.1 christos OPT_cpu && ! OPT_liball ? printa("%-49s %@16d\n",@CPU) : 1; 229 1.1 christos } 230 1.1 christos ' 231 1.1 christos 232 1.1 christos ### Run DTrace 233 1.1 christos if [ $opt_command -eq 1 ]; then 234 1.1 christos /usr/sbin/dtrace -x dynvarsize=$buf -x evaltime=exec -n "$dtrace" \ 235 1.1 christos -c "$command" >&2 236 1.1 christos else 237 1.1 christos /usr/sbin/dtrace -x dynvarsize=$buf -n "$dtrace" -p "$pid" >&2 238 1.1 christos fi 239 1.1 christos 240