blob: 625ed4e53d3e537a6ee616ee01761179ff2a7430 [file] [log] [blame]
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +00001/*
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.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
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.
23 *
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.
34 *
35 * $Id$
36 */
37
38#include "defs.h"
39
40struct call_counts {
41 struct timeval time;
42 int calls, errors;
43};
44
45static struct call_counts *countv[SUPPORTED_PERSONALITIES];
46#define counts (countv[current_personality])
47
48static struct timeval shortest = { 1000000, 0 };
49
50int
51count_syscall(struct tcb *tcp, struct timeval *tv)
52{
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000053 if (tcp->scno < 0 || tcp->scno >= nsyscalls)
54 return 0;
55
Denys Vlasenko7b609d52011-06-22 14:32:43 +020056 if (!counts) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000057 counts = calloc(nsyscalls, sizeof(*counts));
Denys Vlasenko7b609d52011-06-22 14:32:43 +020058 if (!counts) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000059 fprintf(stderr,
60 "strace: out of memory for call counts\n");
61 exit(1);
62 }
63 }
64
65 counts[tcp->scno].calls++;
66 if (tcp->u_error)
67 counts[tcp->scno].errors++;
68
69 tv_sub(tv, tv, &tcp->etime);
70#ifdef LINUX
Denys Vlasenko7b609d52011-06-22 14:32:43 +020071 if (tv_cmp(tv, &tcp->dtime) > 0) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000072 static struct timeval one_tick;
73
Denys Vlasenko7b609d52011-06-22 14:32:43 +020074 if (one_tick.tv_usec == 0) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000075 /* Initialize it. */
76 struct itimerval it;
77
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;
83 }
84
85 if (tv_nz(&tcp->dtime))
86 *tv = tcp->dtime;
Denys Vlasenko7b609d52011-06-22 14:32:43 +020087 else if (tv_cmp(tv, &one_tick) > 0) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000088 if (tv_cmp(&shortest, &one_tick) < 0)
89 *tv = shortest;
90 else
91 *tv = one_tick;
92 }
93 }
94#endif /* LINUX */
95 if (tv_cmp(tv, &shortest) < 0)
96 shortest = *tv;
97 tv_add(&counts[tcp->scno].time, &counts[tcp->scno].time, tv);
98
99 return 0;
100}
101
102static int
103time_cmp(void *a, void *b)
104{
105 return -tv_cmp(&counts[*((int *) a)].time,
106 &counts[*((int *) b)].time);
107}
108
109static int
110syscall_cmp(void *a, void *b)
111{
112 return strcmp(sysent[*((int *) a)].sys_name,
113 sysent[*((int *) b)].sys_name);
114}
115
116static int
117count_cmp(void *a, void *b)
118{
119 int m = counts[*((int *) a)].calls;
120 int n = counts[*((int *) b)].calls;
121
122 return (m < n) ? 1 : (m > n) ? -1 : 0;
123}
124
125static int (*sortfun)();
126static struct timeval overhead = { -1, -1 };
127
128void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000129set_sortby(const char *sortby)
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000130{
131 if (strcmp(sortby, "time") == 0)
132 sortfun = time_cmp;
133 else if (strcmp(sortby, "calls") == 0)
134 sortfun = count_cmp;
135 else if (strcmp(sortby, "name") == 0)
136 sortfun = syscall_cmp;
137 else if (strcmp(sortby, "nothing") == 0)
138 sortfun = NULL;
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200139 else {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000140 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
141 exit(1);
142 }
143}
144
145void set_overhead(int n)
146{
147 overhead.tv_sec = n / 1000000;
148 overhead.tv_usec = n % 1000000;
149}
150
151static void
152call_summary_pers(FILE *outf)
153{
154 int i, j;
155 int call_cum, error_cum;
156 struct timeval tv_cum, dtv;
157 double percent;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000158 const char *dashes = "-------------------------";
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000159 char error_str[16];
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000160 int *sorted_count = calloc(sizeof(int), nsyscalls);
161
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200162 if (!sorted_count) {
Dmitry V. Levina97f03b2008-04-19 14:12:49 +0000163 fprintf(stderr, "strace: out of memory for call summary\n");
164 return;
165 }
166
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000167 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200168 if (overhead.tv_sec == -1) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000169 tv_mul(&overhead, &shortest, 8);
170 tv_div(&overhead, &overhead, 10);
171 }
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200172 for (i = 0; i < nsyscalls; i++) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000173 sorted_count[i] = i;
174 if (counts == NULL || counts[i].calls == 0)
175 continue;
176 tv_mul(&dtv, &overhead, counts[i].calls);
177 tv_sub(&counts[i].time, &counts[i].time, &dtv);
178 call_cum += counts[i].calls;
179 error_cum += counts[i].errors;
180 tv_add(&tv_cum, &tv_cum, &counts[i].time);
181 }
182 if (counts && sortfun)
183 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
184 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
185 "% time", "seconds", "usecs/call",
186 "calls", "errors", "syscall");
187 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
188 dashes, dashes, dashes, dashes, dashes, dashes);
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200189 if (counts) {
190 for (i = 0; i < nsyscalls; i++) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000191 j = sorted_count[i];
192 if (counts[j].calls == 0)
193 continue;
194 tv_div(&dtv, &counts[j].time, counts[j].calls);
195 if (counts[j].errors)
196 sprintf(error_str, "%d", counts[j].errors);
197 else
198 error_str[0] = '\0';
199 percent = (100.0 * tv_float(&counts[j].time)
200 / tv_float(&tv_cum));
Roland McGrath14e31742007-07-11 09:04:23 +0000201 fprintf(outf, "%6.2f %11.6f %11ld %9d %9.9s %s\n",
202 percent, tv_float(&counts[j].time),
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000203 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
204 counts[j].calls,
205 error_str, sysent[j].sys_name);
206 }
207 }
208 free(sorted_count);
209
210 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
211 dashes, dashes, dashes, dashes, dashes, dashes);
212 if (error_cum)
213 sprintf(error_str, "%d", error_cum);
214 else
215 error_str[0] = '\0';
Roland McGrath14e31742007-07-11 09:04:23 +0000216 fprintf(outf, "%6.6s %11.6f %11.11s %9d %9.9s %s\n",
217 "100.00", tv_float(&tv_cum), "",
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000218 call_cum, error_str, "total");
219}
220
221void
222call_summary(FILE *outf)
223{
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200224 int i, old_pers = current_personality;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000225
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200226 for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000227 if (!countv[i])
228 continue;
229
230 if (current_personality != i)
231 set_personality(i);
232 if (i)
233 fprintf(outf,
234 "System call usage summary for %u bit mode:\n",
235 personality_wordsize[current_personality] * 8);
236 call_summary_pers(outf);
237 }
238
239 if (old_pers != current_personality)
240 set_personality(old_pers);
241}