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.
38 /* Per-syscall stats structure */
40 /* time may be total latency or system time */
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, const struct timeval *syscall_exiting_tv)
54 struct timeval *tv = &wtv;
55 struct call_counts *cc;
56 unsigned long scno = tcp->scno;
58 if (!SCNO_IN_RANGE(scno))
62 counts = calloc(nsyscalls, sizeof(*counts));
72 /* tv = wall clock time spent while in syscall */
73 tv_sub(tv, syscall_exiting_tv, &tcp->etime);
75 /* Spent more wall clock time than spent system time? (usually yes) */
76 if (tv_cmp(tv, &tcp->dtime) > 0) {
77 static struct timeval one_tick = { -1, 0 };
79 if (one_tick.tv_sec == -1) {
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 //FIXME: this hack doesn't work (tested on linux-3.6.11): one_tick = 0.000000
89 //tprintf(" one_tick.tv_usec:%u\n", (unsigned)one_tick.tv_usec);
92 if (tv_nz(&tcp->dtime))
93 /* tv = system time spent, if it isn't 0 */
95 else if (tv_cmp(tv, &one_tick) > 0) {
96 /* tv = smallest "sane" time interval */
97 if (tv_cmp(&shortest, &one_tick) < 0)
103 if (tv_cmp(tv, &shortest) < 0)
105 tv_add(&cc->time, &cc->time, count_wallclock ? &wtv : tv);
109 time_cmp(void *a, void *b)
111 return -tv_cmp(&counts[*((int *) a)].time,
112 &counts[*((int *) b)].time);
116 syscall_cmp(void *a, void *b)
118 return strcmp(sysent[*((int *) a)].sys_name,
119 sysent[*((int *) b)].sys_name);
123 count_cmp(void *a, void *b)
125 int m = counts[*((int *) a)].calls;
126 int n = counts[*((int *) b)].calls;
128 return (m < n) ? 1 : (m > n) ? -1 : 0;
131 static int (*sortfun)();
132 static struct timeval overhead = { -1, -1 };
135 set_sortby(const char *sortby)
137 if (strcmp(sortby, "time") == 0)
139 else if (strcmp(sortby, "calls") == 0)
141 else if (strcmp(sortby, "name") == 0)
142 sortfun = syscall_cmp;
143 else if (strcmp(sortby, "nothing") == 0)
146 error_msg_and_die("invalid sortby: '%s'", sortby);
150 void set_overhead(int n)
152 overhead.tv_sec = n / 1000000;
153 overhead.tv_usec = n % 1000000;
157 call_summary_pers(FILE *outf)
160 int call_cum, error_cum;
161 struct timeval tv_cum, dtv;
164 const char *dashes = "----------------";
165 char error_str[sizeof(int)*3];
168 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
169 "% time", "seconds", "usecs/call",
170 "calls", "errors", "syscall");
171 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
172 dashes, dashes, dashes, dashes, dashes, dashes);
174 sorted_count = calloc(sizeof(int), nsyscalls);
177 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
178 if (overhead.tv_sec == -1) {
179 tv_mul(&overhead, &shortest, 8);
180 tv_div(&overhead, &overhead, 10);
182 for (i = 0; i < nsyscalls; i++) {
184 if (counts == NULL || counts[i].calls == 0)
186 tv_mul(&dtv, &overhead, counts[i].calls);
187 tv_sub(&counts[i].time, &counts[i].time, &dtv);
188 call_cum += counts[i].calls;
189 error_cum += counts[i].errors;
190 tv_add(&tv_cum, &tv_cum, &counts[i].time);
192 float_tv_cum = tv_float(&tv_cum);
195 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
196 for (i = 0; i < nsyscalls; i++) {
197 double float_syscall_time;
198 int idx = sorted_count[i];
199 struct call_counts *cc = &counts[idx];
202 tv_div(&dtv, &cc->time, cc->calls);
205 sprintf(error_str, "%u", cc->errors);
206 float_syscall_time = tv_float(&cc->time);
207 percent = (100.0 * float_syscall_time);
209 percent /= float_tv_cum;
210 /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */
211 fprintf(outf, "%6.2f %11.6f %11lu %9u %9.9s %s\n",
212 percent, float_syscall_time,
213 (long) (1000000 * dtv.tv_sec + dtv.tv_usec),
215 error_str, sysent[idx].sys_name);
220 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
221 dashes, dashes, dashes, dashes, dashes, dashes);
224 sprintf(error_str, "%u", error_cum);
225 fprintf(outf, "%6.6s %11.6f %11.11s %9u %9.9s %s\n",
226 "100.00", float_tv_cum, "",
227 call_cum, error_str, "total");
231 call_summary(FILE *outf)
233 unsigned int i, old_pers = current_personality;
235 for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
239 if (current_personality != i)
243 "System call usage summary for %d bit mode:\n",
244 current_wordsize * 8);
245 call_summary_pers(outf);
248 if (old_pers != current_personality)
249 set_personality(old_pers);