blob: 1785242ac335cfc995f0836a7bd229f640643d30 [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
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +020050void
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000051count_syscall(struct tcb *tcp, struct timeval *tv)
52{
Denys Vlasenkocb6f0562011-08-25 01:13:43 +020053 if (!SCNO_IN_RANGE(tcp->scno))
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +020054 return;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000055
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 Vlasenko1d46ba52011-08-31 14:00:02 +020058 if (!counts)
59 die_out_of_memory();
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000060 }
61
62 counts[tcp->scno].calls++;
63 if (tcp->u_error)
64 counts[tcp->scno].errors++;
65
66 tv_sub(tv, tv, &tcp->etime);
67#ifdef LINUX
Denys Vlasenko7b609d52011-06-22 14:32:43 +020068 if (tv_cmp(tv, &tcp->dtime) > 0) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000069 static struct timeval one_tick;
70
Denys Vlasenko7b609d52011-06-22 14:32:43 +020071 if (one_tick.tv_usec == 0) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000072 /* Initialize it. */
73 struct itimerval it;
74
75 memset(&it, 0, sizeof it);
76 it.it_interval.tv_usec = 1;
77 setitimer(ITIMER_REAL, &it, NULL);
78 getitimer(ITIMER_REAL, &it);
79 one_tick = it.it_interval;
80 }
81
82 if (tv_nz(&tcp->dtime))
83 *tv = tcp->dtime;
Denys Vlasenko7b609d52011-06-22 14:32:43 +020084 else if (tv_cmp(tv, &one_tick) > 0) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000085 if (tv_cmp(&shortest, &one_tick) < 0)
86 *tv = shortest;
87 else
88 *tv = one_tick;
89 }
90 }
91#endif /* LINUX */
92 if (tv_cmp(tv, &shortest) < 0)
93 shortest = *tv;
94 tv_add(&counts[tcp->scno].time, &counts[tcp->scno].time, tv);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000095}
96
97static int
98time_cmp(void *a, void *b)
99{
100 return -tv_cmp(&counts[*((int *) a)].time,
101 &counts[*((int *) b)].time);
102}
103
104static int
105syscall_cmp(void *a, void *b)
106{
107 return strcmp(sysent[*((int *) a)].sys_name,
108 sysent[*((int *) b)].sys_name);
109}
110
111static int
112count_cmp(void *a, void *b)
113{
114 int m = counts[*((int *) a)].calls;
115 int n = counts[*((int *) b)].calls;
116
117 return (m < n) ? 1 : (m > n) ? -1 : 0;
118}
119
120static int (*sortfun)();
121static struct timeval overhead = { -1, -1 };
122
123void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000124set_sortby(const char *sortby)
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000125{
126 if (strcmp(sortby, "time") == 0)
127 sortfun = time_cmp;
128 else if (strcmp(sortby, "calls") == 0)
129 sortfun = count_cmp;
130 else if (strcmp(sortby, "name") == 0)
131 sortfun = syscall_cmp;
132 else if (strcmp(sortby, "nothing") == 0)
133 sortfun = NULL;
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200134 else {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000135 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
136 exit(1);
137 }
138}
139
140void set_overhead(int n)
141{
142 overhead.tv_sec = n / 1000000;
143 overhead.tv_usec = n % 1000000;
144}
145
146static void
147call_summary_pers(FILE *outf)
148{
149 int i, j;
150 int call_cum, error_cum;
151 struct timeval tv_cum, dtv;
152 double percent;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000153 const char *dashes = "-------------------------";
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000154 char error_str[16];
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000155 int *sorted_count = calloc(sizeof(int), nsyscalls);
156
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200157 if (!sorted_count)
158 die_out_of_memory();
Dmitry V. Levina97f03b2008-04-19 14:12:49 +0000159
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000160 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200161 if (overhead.tv_sec == -1) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000162 tv_mul(&overhead, &shortest, 8);
163 tv_div(&overhead, &overhead, 10);
164 }
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200165 for (i = 0; i < nsyscalls; i++) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000166 sorted_count[i] = i;
167 if (counts == NULL || counts[i].calls == 0)
168 continue;
169 tv_mul(&dtv, &overhead, counts[i].calls);
170 tv_sub(&counts[i].time, &counts[i].time, &dtv);
171 call_cum += counts[i].calls;
172 error_cum += counts[i].errors;
173 tv_add(&tv_cum, &tv_cum, &counts[i].time);
174 }
175 if (counts && sortfun)
176 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
177 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
178 "% time", "seconds", "usecs/call",
179 "calls", "errors", "syscall");
180 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
181 dashes, dashes, dashes, dashes, dashes, dashes);
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200182 if (counts) {
183 for (i = 0; i < nsyscalls; i++) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000184 j = sorted_count[i];
185 if (counts[j].calls == 0)
186 continue;
187 tv_div(&dtv, &counts[j].time, counts[j].calls);
188 if (counts[j].errors)
189 sprintf(error_str, "%d", counts[j].errors);
190 else
191 error_str[0] = '\0';
192 percent = (100.0 * tv_float(&counts[j].time)
193 / tv_float(&tv_cum));
Roland McGrath14e31742007-07-11 09:04:23 +0000194 fprintf(outf, "%6.2f %11.6f %11ld %9d %9.9s %s\n",
195 percent, tv_float(&counts[j].time),
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000196 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
197 counts[j].calls,
198 error_str, sysent[j].sys_name);
199 }
200 }
201 free(sorted_count);
202
203 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
204 dashes, dashes, dashes, dashes, dashes, dashes);
205 if (error_cum)
206 sprintf(error_str, "%d", error_cum);
207 else
208 error_str[0] = '\0';
Roland McGrath14e31742007-07-11 09:04:23 +0000209 fprintf(outf, "%6.6s %11.6f %11.11s %9d %9.9s %s\n",
210 "100.00", tv_float(&tv_cum), "",
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000211 call_cum, error_str, "total");
212}
213
214void
215call_summary(FILE *outf)
216{
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200217 int i, old_pers = current_personality;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000218
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200219 for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000220 if (!countv[i])
221 continue;
222
223 if (current_personality != i)
224 set_personality(i);
225 if (i)
226 fprintf(outf,
227 "System call usage summary for %u bit mode:\n",
228 personality_wordsize[current_personality] * 8);
229 call_summary_pers(outf);
230 }
231
232 if (old_pers != current_personality)
233 set_personality(old_pers);
234}