blob: 5c285fb14431b25aad65895987198fcc1d6cb5ec [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{
53 tcp->flags &= ~TCB_INSYSCALL;
54 if (tcp->scno < 0 || tcp->scno >= nsyscalls)
55 return 0;
56
57 if (!counts)
58 {
59 counts = calloc(nsyscalls, sizeof(*counts));
60 if (!counts)
61 {
62 fprintf(stderr,
63 "strace: out of memory for call counts\n");
64 exit(1);
65 }
66 }
67
68 counts[tcp->scno].calls++;
69 if (tcp->u_error)
70 counts[tcp->scno].errors++;
71
72 tv_sub(tv, tv, &tcp->etime);
73#ifdef LINUX
74 if (tv_cmp(tv, &tcp->dtime) > 0)
75 {
76 static struct timeval one_tick;
77
78 if (one_tick.tv_usec == 0)
79 {
80 /* Initialize it. */
81 struct itimerval it;
82
83 memset(&it, 0, sizeof it);
84 it.it_interval.tv_usec = 1;
85 setitimer(ITIMER_REAL, &it, NULL);
86 getitimer(ITIMER_REAL, &it);
87 one_tick = it.it_interval;
88 }
89
90 if (tv_nz(&tcp->dtime))
91 *tv = tcp->dtime;
92 else if (tv_cmp(tv, &one_tick) > 0)
93 {
94 if (tv_cmp(&shortest, &one_tick) < 0)
95 *tv = shortest;
96 else
97 *tv = one_tick;
98 }
99 }
100#endif /* LINUX */
101 if (tv_cmp(tv, &shortest) < 0)
102 shortest = *tv;
103 tv_add(&counts[tcp->scno].time, &counts[tcp->scno].time, tv);
104
105 return 0;
106}
107
108static int
109time_cmp(void *a, void *b)
110{
111 return -tv_cmp(&counts[*((int *) a)].time,
112 &counts[*((int *) b)].time);
113}
114
115static int
116syscall_cmp(void *a, void *b)
117{
118 return strcmp(sysent[*((int *) a)].sys_name,
119 sysent[*((int *) b)].sys_name);
120}
121
122static int
123count_cmp(void *a, void *b)
124{
125 int m = counts[*((int *) a)].calls;
126 int n = counts[*((int *) b)].calls;
127
128 return (m < n) ? 1 : (m > n) ? -1 : 0;
129}
130
131static int (*sortfun)();
132static struct timeval overhead = { -1, -1 };
133
134void
135set_sortby(char *sortby)
136{
137 if (strcmp(sortby, "time") == 0)
138 sortfun = time_cmp;
139 else if (strcmp(sortby, "calls") == 0)
140 sortfun = count_cmp;
141 else if (strcmp(sortby, "name") == 0)
142 sortfun = syscall_cmp;
143 else if (strcmp(sortby, "nothing") == 0)
144 sortfun = NULL;
145 else
146 {
147 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
148 exit(1);
149 }
150}
151
152void set_overhead(int n)
153{
154 overhead.tv_sec = n / 1000000;
155 overhead.tv_usec = n % 1000000;
156}
157
158static void
159call_summary_pers(FILE *outf)
160{
161 int i, j;
162 int call_cum, error_cum;
163 struct timeval tv_cum, dtv;
164 double percent;
165 char *dashes = "-------------------------";
166 char error_str[16];
167
168 int *sorted_count = calloc(sizeof(int), nsyscalls);
169
170 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
171 if (overhead.tv_sec == -1)
172 {
173 tv_mul(&overhead, &shortest, 8);
174 tv_div(&overhead, &overhead, 10);
175 }
176 for (i = 0; i < nsyscalls; i++)
177 {
178 sorted_count[i] = i;
179 if (counts == NULL || counts[i].calls == 0)
180 continue;
181 tv_mul(&dtv, &overhead, counts[i].calls);
182 tv_sub(&counts[i].time, &counts[i].time, &dtv);
183 call_cum += counts[i].calls;
184 error_cum += counts[i].errors;
185 tv_add(&tv_cum, &tv_cum, &counts[i].time);
186 }
187 if (counts && sortfun)
188 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
189 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
190 "% time", "seconds", "usecs/call",
191 "calls", "errors", "syscall");
192 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
193 dashes, dashes, dashes, dashes, dashes, dashes);
194 if (counts)
195 {
196 for (i = 0; i < nsyscalls; i++)
197 {
198 j = sorted_count[i];
199 if (counts[j].calls == 0)
200 continue;
201 tv_div(&dtv, &counts[j].time, counts[j].calls);
202 if (counts[j].errors)
203 sprintf(error_str, "%d", counts[j].errors);
204 else
205 error_str[0] = '\0';
206 percent = (100.0 * tv_float(&counts[j].time)
207 / tv_float(&tv_cum));
208 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
209 percent, (long) counts[j].time.tv_sec,
210 (long) counts[j].time.tv_usec,
211 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
212 counts[j].calls,
213 error_str, sysent[j].sys_name);
214 }
215 }
216 free(sorted_count);
217
218 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
219 dashes, dashes, dashes, dashes, dashes, dashes);
220 if (error_cum)
221 sprintf(error_str, "%d", error_cum);
222 else
223 error_str[0] = '\0';
224 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
225 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
226 call_cum, error_str, "total");
227}
228
229void
230call_summary(FILE *outf)
231{
232 int i, old_pers = current_personality;
233
234 for (i = 0; i < SUPPORTED_PERSONALITIES; ++i)
235 {
236 if (!countv[i])
237 continue;
238
239 if (current_personality != i)
240 set_personality(i);
241 if (i)
242 fprintf(outf,
243 "System call usage summary for %u bit mode:\n",
244 personality_wordsize[current_personality] * 8);
245 call_summary_pers(outf);
246 }
247
248 if (old_pers != current_personality)
249 set_personality(old_pers);
250}