2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
9 * Copyright (c) 2004 Roland McGrath <roland@redhat.com>
10 * Copyright (c) 2006 Dmitry V. Levin <ldv@altlinux.org>
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 static struct call_counts *countv[SUPPORTED_PERSONALITIES];
46 #define counts (countv[current_personality])
48 static struct timeval shortest = { 1000000, 0 };
51 count_syscall(struct tcb *tcp, struct timeval *tv)
53 if (!SCNO_IN_RANGE(tcp->scno))
57 counts = calloc(nsyscalls, sizeof(*counts));
60 "strace: out of memory for call counts\n");
65 counts[tcp->scno].calls++;
67 counts[tcp->scno].errors++;
69 tv_sub(tv, tv, &tcp->etime);
71 if (tv_cmp(tv, &tcp->dtime) > 0) {
72 static struct timeval one_tick;
74 if (one_tick.tv_usec == 0) {
78 memset(&it, 0, sizeof it);
79 it.it_interval.tv_usec = 1;
80 setitimer(ITIMER_REAL, &it, NULL);
81 getitimer(ITIMER_REAL, &it);
82 one_tick = it.it_interval;
85 if (tv_nz(&tcp->dtime))
87 else if (tv_cmp(tv, &one_tick) > 0) {
88 if (tv_cmp(&shortest, &one_tick) < 0)
95 if (tv_cmp(tv, &shortest) < 0)
97 tv_add(&counts[tcp->scno].time, &counts[tcp->scno].time, tv);
101 time_cmp(void *a, void *b)
103 return -tv_cmp(&counts[*((int *) a)].time,
104 &counts[*((int *) b)].time);
108 syscall_cmp(void *a, void *b)
110 return strcmp(sysent[*((int *) a)].sys_name,
111 sysent[*((int *) b)].sys_name);
115 count_cmp(void *a, void *b)
117 int m = counts[*((int *) a)].calls;
118 int n = counts[*((int *) b)].calls;
120 return (m < n) ? 1 : (m > n) ? -1 : 0;
123 static int (*sortfun)();
124 static struct timeval overhead = { -1, -1 };
127 set_sortby(const char *sortby)
129 if (strcmp(sortby, "time") == 0)
131 else if (strcmp(sortby, "calls") == 0)
133 else if (strcmp(sortby, "name") == 0)
134 sortfun = syscall_cmp;
135 else if (strcmp(sortby, "nothing") == 0)
138 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
143 void set_overhead(int n)
145 overhead.tv_sec = n / 1000000;
146 overhead.tv_usec = n % 1000000;
150 call_summary_pers(FILE *outf)
153 int call_cum, error_cum;
154 struct timeval tv_cum, dtv;
156 const char *dashes = "-------------------------";
158 int *sorted_count = calloc(sizeof(int), nsyscalls);
161 fprintf(stderr, "strace: out of memory for call summary\n");
165 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
166 if (overhead.tv_sec == -1) {
167 tv_mul(&overhead, &shortest, 8);
168 tv_div(&overhead, &overhead, 10);
170 for (i = 0; i < nsyscalls; i++) {
172 if (counts == NULL || counts[i].calls == 0)
174 tv_mul(&dtv, &overhead, counts[i].calls);
175 tv_sub(&counts[i].time, &counts[i].time, &dtv);
176 call_cum += counts[i].calls;
177 error_cum += counts[i].errors;
178 tv_add(&tv_cum, &tv_cum, &counts[i].time);
180 if (counts && sortfun)
181 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
182 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
183 "% time", "seconds", "usecs/call",
184 "calls", "errors", "syscall");
185 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
186 dashes, dashes, dashes, dashes, dashes, dashes);
188 for (i = 0; i < nsyscalls; i++) {
190 if (counts[j].calls == 0)
192 tv_div(&dtv, &counts[j].time, counts[j].calls);
193 if (counts[j].errors)
194 sprintf(error_str, "%d", counts[j].errors);
197 percent = (100.0 * tv_float(&counts[j].time)
198 / tv_float(&tv_cum));
199 fprintf(outf, "%6.2f %11.6f %11ld %9d %9.9s %s\n",
200 percent, tv_float(&counts[j].time),
201 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
203 error_str, sysent[j].sys_name);
208 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
209 dashes, dashes, dashes, dashes, dashes, dashes);
211 sprintf(error_str, "%d", error_cum);
214 fprintf(outf, "%6.6s %11.6f %11.11s %9d %9.9s %s\n",
215 "100.00", tv_float(&tv_cum), "",
216 call_cum, error_str, "total");
220 call_summary(FILE *outf)
222 int i, old_pers = current_personality;
224 for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
228 if (current_personality != i)
232 "System call usage summary for %u bit mode:\n",
233 personality_wordsize[current_personality] * 8);
234 call_summary_pers(outf);
237 if (old_pers != current_personality)
238 set_personality(old_pers);