1 1.1 christos #!/usr/bin/sh 2 1.1 christos # 3 1.1 christos # cputimes - print CPU time consumed by Kernel/Idle/Processes. 4 1.1 christos # Written using DTrace (Solaris 10 3/05). 5 1.1 christos # 6 1.1 christos # $Id: cputimes,v 1.1.1.1 2015/09/30 22:01:09 christos Exp $ 7 1.1 christos # 8 1.1 christos # This program accurately measures time consumed by the kernel, but in 9 1.1 christos # doing so creates extra kernel load of it's own. The extra kernel 10 1.1 christos # activity can be measured by running one cputimes and then another, and 11 1.1 christos # comparing the difference in kernel consumed time. This method can be 12 1.1 christos # used to estimate the load created by other DTrace scripts. 13 1.1 christos # 14 1.1 christos # USAGE: cputimes [-ahTV] [-t top] [interval [count]] 15 1.1 christos # 16 1.1 christos # -a # print all processes 17 1.1 christos # -T # print totals 18 1.1 christos # -V # don't print timestamps 19 1.1 christos # -t num # print top num lines only 20 1.1 christos # eg, 21 1.1 christos # cputimes 1 # print every 1 second 22 1.1 christos # cputimes -a 10 # print all processes every 10 secs 23 1.1 christos # cputimes -at 8 5 # print top 8 lines every 5 secs 24 1.1 christos # 25 1.1 christos # 26 1.1 christos # FIELDS: 27 1.1 christos # THREADS The following or the process name, 28 1.1 christos # IDLE Idle time - CPU running idle thread 29 1.1 christos # KERNEL Kernel time - Kernel servicing interrupts, ... 30 1.1 christos # PROCESS Process time - PIDs running on the system 31 1.1 christos # TIME (ns) Sum of the CPU time, ns (nanoseconds) 32 1.1 christos # 33 1.1 christos # NOTES: 34 1.1 christos # * This takes into account multiple CPU servers, the total 35 1.1 christos # seconds consumed will be a multiple of the CPU count and interval. 36 1.1 christos # 37 1.1 christos # SEE ALSO: cpudists 38 1.1 christos # Heisenberg's uncertainty principle. 39 1.1 christos # 40 1.1 christos # COPYRIGHT: Copyright (c) 2005 Brendan Gregg. 41 1.1 christos # 42 1.1 christos # CDDL HEADER START 43 1.1 christos # 44 1.1 christos # The contents of this file are subject to the terms of the 45 1.1 christos # Common Development and Distribution License, Version 1.0 only 46 1.1 christos # (the "License"). You may not use this file except in compliance 47 1.1 christos # with the License. 48 1.1 christos # 49 1.1 christos # You can obtain a copy of the license at Docs/cddl1.txt 50 1.1 christos # or http://www.opensolaris.org/os/licensing. 51 1.1 christos # See the License for the specific language governing permissions 52 1.1 christos # and limitations under the License. 53 1.1 christos # 54 1.1 christos # CDDL HEADER END 55 1.1 christos # 56 1.1 christos # Author: Brendan Gregg [Sydney, Australia] 57 1.1 christos # 58 1.1 christos # 27-Apr-2005 Brendan Gregg Created this. 59 1.1 christos # 22-Sep-2005 " " Fixed a key corruption bug. 60 1.1 christos # 22-Sep-2005 " " Last update. 61 1.1 christos # 62 1.1 christos 63 1.1 christos 64 1.1 christos ############################## 65 1.1 christos # --- Process Arguments --- 66 1.1 christos # 67 1.1 christos opt_all=0; opt_time=1; opt_top=0; opt_totals=0 68 1.1 christos top=0; interval=1; count=1 69 1.1 christos 70 1.1 christos while getopts aht:TV name 71 1.1 christos do 72 1.1 christos case $name in 73 1.1 christos a) opt_all=1 ;; 74 1.1 christos T) opt_totals=1 ;; 75 1.1 christos V) opt_time=0 ;; 76 1.1 christos t) opt_top=1; top=$OPTARG ;; 77 1.1 christos h|?) cat <<-END >&2 78 1.1 christos USAGE: cputimes [-ahTV] [-t top] [interval [count]] 79 1.1 christos cputimes # default output 80 1.1 christos -a # print all processes 81 1.1 christos -T # print totals 82 1.1 christos -V # don't print times 83 1.1 christos -t num # print top num lines only 84 1.1 christos eg, 85 1.1 christos cputimes 1 # print every 1 second 86 1.1 christos cputimes -a 10 # all processes per 10 sec 87 1.1 christos cputimes -at 8 5 # top 8 lines every 5 secs 88 1.1 christos END 89 1.1 christos exit 1 90 1.1 christos esac 91 1.1 christos done 92 1.1 christos shift `expr $OPTIND - 1` 93 1.1 christos 94 1.1 christos if [ "$1" -gt 0 ]; then 95 1.1 christos interval=$1; count=-1; shift 96 1.1 christos fi 97 1.1 christos if [ "$1" -gt 0 ]; then 98 1.1 christos count=$1; shift 99 1.1 christos fi 100 1.1 christos 101 1.1 christos 102 1.1 christos ################################# 103 1.1 christos # --- Main Program, DTrace --- 104 1.1 christos # 105 1.1 christos /usr/sbin/dtrace -n ' 106 1.1 christos #pragma D option quiet 107 1.1 christos 108 1.1 christos /* 109 1.1 christos * Command line arguments 110 1.1 christos */ 111 1.1 christos inline int OPT_all = '$opt_all'; 112 1.1 christos inline int OPT_time = '$opt_time'; 113 1.1 christos inline int OPT_totals = '$opt_totals'; 114 1.1 christos inline int OPT_top = '$opt_top'; 115 1.1 christos inline int TOP = '$top'; 116 1.1 christos inline int INTERVAL = '$interval'; 117 1.1 christos inline int COUNTER = '$count'; 118 1.1 christos 119 1.1 christos /* Initialise variables */ 120 1.1 christos dtrace:::BEGIN 121 1.1 christos { 122 1.1 christos cpustart[cpu] = 0; 123 1.1 christos counts = COUNTER; 124 1.1 christos secs = INTERVAL; 125 1.1 christos } 126 1.1 christos 127 1.1 christos /* Flag this thread as idle */ 128 1.1 christos sysinfo:unix:idle_enter:idlethread 129 1.1 christos { 130 1.1 christos idle[cpu] = 1; 131 1.1 christos } 132 1.1 christos 133 1.1 christos /* Save kernel time between running threads */ 134 1.1 christos sched:::on-cpu 135 1.1 christos /cpustart[cpu]/ 136 1.1 christos { 137 1.1 christos this->elapsed = timestamp - cpustart[cpu]; 138 1.1 christos @Procs["KERNEL"] = sum(this->elapsed); 139 1.1 christos } 140 1.1 christos 141 1.1 christos /* Save the elapsed time of a thread */ 142 1.1 christos sched:::off-cpu, 143 1.1 christos sched:::remain-cpu, 144 1.1 christos profile:::profile-1sec 145 1.1 christos /cpustart[cpu]/ 146 1.1 christos { 147 1.1 christos /* determine the name for this thread */ 148 1.1 christos program[cpu] = pid == 0 ? idle[cpu] ? "IDLE" : "KERNEL" : 149 1.1 christos OPT_all ? execname : "PROCESS"; 150 1.1 christos 151 1.1 christos /* save elapsed */ 152 1.1 christos this->elapsed = timestamp - cpustart[cpu]; 153 1.1 christos @Procs[program[cpu]] = sum(this->elapsed); 154 1.1 christos cpustart[cpu] = timestamp; 155 1.1 christos } 156 1.1 christos 157 1.1 christos /* Record the start time of a thread */ 158 1.1 christos sched:::on-cpu, 159 1.1 christos sched:::remain-cpu 160 1.1 christos { 161 1.1 christos idle[cpu] = 0; 162 1.1 christos cpustart[cpu] = timestamp; 163 1.1 christos } 164 1.1 christos 165 1.1 christos 166 1.1 christos profile:::tick-1sec 167 1.1 christos { 168 1.1 christos secs--; 169 1.1 christos } 170 1.1 christos 171 1.1 christos /* Print time */ 172 1.1 christos profile:::tick-1sec 173 1.1 christos /secs == 0/ 174 1.1 christos { 175 1.1 christos OPT_time ? printf("%Y,\n", walltimestamp) : 1; 176 1.1 christos printf("%16s %16s\n", "THREADS", "TIME (ns)"); 177 1.1 christos } 178 1.1 christos 179 1.1 christos /* Print report */ 180 1.1 christos profile:::tick-1sec 181 1.1 christos /secs == 0/ 182 1.1 christos { 183 1.1 christos OPT_top ? trunc(@Procs, TOP) : 1; 184 1.1 christos printa("%16s %@16d\n", @Procs); 185 1.1 christos trunc(@Procs); 186 1.1 christos secs = INTERVAL; 187 1.1 christos counts--; 188 1.1 christos } 189 1.1 christos 190 1.1 christos /* End of program */ 191 1.1 christos profile:::tick-1sec 192 1.1 christos /counts == 0/ 193 1.1 christos { 194 1.1 christos exit(0); 195 1.1 christos } 196 1.1 christos 197 1.1 christos /* cleanup for Ctrl-C */ 198 1.1 christos dtrace:::END 199 1.1 christos { 200 1.1 christos trunc(@Procs); 201 1.1 christos } 202 1.1 christos ' 203 1.1 christos 204