topsyscall revision 1.1 1 1.1 christos #!/usr/bin/ksh
2 1.1 christos #
3 1.1 christos # topsyscall - display top syscalls by syscall name.
4 1.1 christos # Written using DTrace (Solaris 10 3/05).
5 1.1 christos #
6 1.1 christos # This program continually prints a report of the top system calls,
7 1.1 christos # and refreshes the display every 1 second or as specified at the
8 1.1 christos # command line.
9 1.1 christos #
10 1.1 christos # $Id: topsyscall,v 1.1 2015/09/30 22:01:07 christos Exp $
11 1.1 christos #
12 1.1 christos # USAGE: topsyscall [-Cs] [interval [count]]
13 1.1 christos #
14 1.1 christos # -C # don't clear the screen
15 1.1 christos # -s # print per second values
16 1.1 christos #
17 1.1 christos # FIELDS:
18 1.1 christos # load avg load averages, see uptime(1)
19 1.1 christos # syscalls total syscalls in this interval
20 1.1 christos # syscalls/s syscalls per second
21 1.1 christos # SYSCALL system call name
22 1.1 christos # COUNT total syscalls in this interval
23 1.1 christos # COUNT/s syscalls per second
24 1.1 christos #
25 1.1 christos # INSPIRATION: top(1) by William LeFebvre
26 1.1 christos #
27 1.1 christos # COPYRIGHT: Copyright (c) 2005, 2006 Brendan Gregg.
28 1.1 christos #
29 1.1 christos # CDDL HEADER START
30 1.1 christos #
31 1.1 christos # The contents of this file are subject to the terms of the
32 1.1 christos # Common Development and Distribution License, Version 1.0 only
33 1.1 christos # (the "License"). You may not use this file except in compliance
34 1.1 christos # with the License.
35 1.1 christos #
36 1.1 christos # You can obtain a copy of the license at Docs/cddl1.txt
37 1.1 christos # or http://www.opensolaris.org/os/licensing.
38 1.1 christos # See the License for the specific language governing permissions
39 1.1 christos # and limitations under the License.
40 1.1 christos #
41 1.1 christos # CDDL HEADER END
42 1.1 christos #
43 1.1 christos # 13-Jun-2005 Brendan Gregg Created this.
44 1.1 christos # 20-Apr-2006 " " Last update.
45 1.1 christos #
46 1.1 christos
47 1.1 christos ##############################
48 1.1 christos # --- Process Arguments ---
49 1.1 christos #
50 1.1 christos
51 1.1 christos ### Default variables
52 1.1 christos count=-1; interval=1; opt_persec=0; opt_clear=1
53 1.1 christos
54 1.1 christos ### Process options
55 1.1 christos while getopts Chs name
56 1.1 christos do
57 1.1 christos case $name in
58 1.1 christos C) opt_clear=0 ;;
59 1.1 christos s) opt_persec=1 ;;
60 1.1 christos h|?) cat <<-END >&2
61 1.1 christos USAGE: topsyscall [-s] [interval [count]]
62 1.1 christos -C # don't clear the screen
63 1.1 christos -s # print per second values
64 1.1 christos eg,
65 1.1 christos topsyscall # default, 1 second updates
66 1.1 christos topsyscall 5 # 5 second updates
67 1.1 christos END
68 1.1 christos exit 1
69 1.1 christos esac
70 1.1 christos done
71 1.1 christos shift $(( $OPTIND - 1 ))
72 1.1 christos
73 1.1 christos ### option logic
74 1.1 christos if [[ "$1" > 0 ]]; then
75 1.1 christos interval=$1; shift
76 1.1 christos fi
77 1.1 christos if [[ "$1" > 0 ]]; then
78 1.1 christos count=$1; shift
79 1.1 christos fi
80 1.1 christos if (( opt_clear )); then
81 1.1 christos clearstr=`clear`
82 1.1 christos else
83 1.1 christos clearstr=.
84 1.1 christos fi
85 1.1 christos
86 1.1 christos
87 1.1 christos
88 1.1 christos #################################
89 1.1 christos # --- Main Program, DTrace ---
90 1.1 christos #
91 1.1 christos /usr/sbin/dtrace -n '
92 1.1 christos #pragma D option quiet
93 1.1 christos #pragma D option destructive
94 1.1 christos
95 1.1 christos /* constants */
96 1.1 christos inline int OPT_clear = '$opt_clear';
97 1.1 christos inline int OPT_persec = '$opt_persec';
98 1.1 christos inline int INTERVAL = '$interval';
99 1.1 christos inline int COUNTER = '$count';
100 1.1 christos inline int SCREEN = 20;
101 1.1 christos inline string CLEAR = "'$clearstr'";
102 1.1 christos
103 1.1 christos /* variables */
104 1.1 christos dtrace:::BEGIN
105 1.1 christos {
106 1.1 christos secs = INTERVAL;
107 1.1 christos counts = COUNTER;
108 1.1 christos printf("Tracing... Please wait.\n");
109 1.1 christos }
110 1.1 christos
111 1.1 christos /* record syscall event */
112 1.1 christos syscall:::entry
113 1.1 christos {
114 1.1 christos @Name[probefunc] = count();
115 1.1 christos @Total = count();
116 1.1 christos }
117 1.1 christos
118 1.1 christos /* timer */
119 1.1 christos profile:::tick-1sec
120 1.1 christos {
121 1.1 christos secs--;
122 1.1 christos }
123 1.1 christos
124 1.1 christos /* update screen */
125 1.1 christos profile:::tick-1sec
126 1.1 christos /secs == 0/
127 1.1 christos {
128 1.1 christos /* fetch load averages */
129 1.1 christos this->load1a = `hp_avenrun[0] / 65536;
130 1.1 christos this->load5a = `hp_avenrun[1] / 65536;
131 1.1 christos this->load15a = `hp_avenrun[2] / 65536;
132 1.1 christos this->load1b = ((`hp_avenrun[0] % 65536) * 100) / 65536;
133 1.1 christos this->load5b = ((`hp_avenrun[1] % 65536) * 100) / 65536;
134 1.1 christos this->load15b = ((`hp_avenrun[2] % 65536) * 100) / 65536;
135 1.1 christos
136 1.1 christos /* clear screen */
137 1.1 christos OPT_clear ? printf("%s", CLEAR) : 1;
138 1.1 christos
139 1.1 christos /* print load average */
140 1.1 christos printf("%Y, load average: %d.%02d, %d.%02d, %d.%02d",
141 1.1 christos walltimestamp, this->load1a, this->load1b, this->load5a,
142 1.1 christos this->load5b, this->load15a, this->load15b);
143 1.1 christos
144 1.1 christos /* calculate per second values if needed */
145 1.1 christos OPT_persec ? normalize(@Total, INTERVAL) : 1;
146 1.1 christos OPT_persec ? normalize(@Name, INTERVAL) : 1;
147 1.1 christos
148 1.1 christos /* print syscall count */
149 1.1 christos printf(" %s: ", OPT_persec ? "syscalls/s" : "syscalls");
150 1.1 christos printa("%@d\n",@Total);
151 1.1 christos
152 1.1 christos /* print report */
153 1.1 christos trunc(@Name, SCREEN);
154 1.1 christos printf("\n %-25s %12s\n", "SYSCALL",
155 1.1 christos OPT_persec ? "COUNT/s" : "COUNT");
156 1.1 christos printa(" %-25s %@12d\n", @Name);
157 1.1 christos printf("\n");
158 1.1 christos
159 1.1 christos /* reset variables */
160 1.1 christos trunc(@Name);
161 1.1 christos clear(@Total);
162 1.1 christos secs = INTERVAL;
163 1.1 christos counts--;
164 1.1 christos }
165 1.1 christos
166 1.1 christos /*
167 1.1 christos * End of program
168 1.1 christos */
169 1.1 christos profile:::tick-1sec
170 1.1 christos /counts == 0/
171 1.1 christos {
172 1.1 christos exit(0);
173 1.1 christos }
174 1.1 christos
175 1.1 christos /*
176 1.1 christos * Cleanup for Ctrl-C
177 1.1 christos */
178 1.1 christos dtrace:::END
179 1.1 christos {
180 1.1 christos trunc(@Name);
181 1.1 christos trunc(@Total);
182 1.1 christos }
183 1.1 christos '
184 1.1 christos
185