blob: 6c9b1c20c90e3c2682a6ecdf1b4d77ec62350353 [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.
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000034 */
35
36#include "defs.h"
37
38struct call_counts {
39 struct timeval time;
40 int calls, errors;
41};
42
43static struct call_counts *countv[SUPPORTED_PERSONALITIES];
44#define counts (countv[current_personality])
45
46static struct timeval shortest = { 1000000, 0 };
47
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +020048void
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000049count_syscall(struct tcb *tcp, struct timeval *tv)
50{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +010051 unsigned long scno = tcp->scno;
52
53 if (!SCNO_IN_RANGE(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
Denys Vlasenko74ec14f2013-02-21 16:13:47 +010062 counts[scno].calls++;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000063 if (tcp->u_error)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +010064 counts[scno].errors++;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000065
66 tv_sub(tv, tv, &tcp->etime);
Denys Vlasenko7b609d52011-06-22 14:32:43 +020067 if (tv_cmp(tv, &tcp->dtime) > 0) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000068 static struct timeval one_tick;
69
Denys Vlasenko7b609d52011-06-22 14:32:43 +020070 if (one_tick.tv_usec == 0) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000071 /* Initialize it. */
72 struct itimerval it;
73
74 memset(&it, 0, sizeof it);
75 it.it_interval.tv_usec = 1;
76 setitimer(ITIMER_REAL, &it, NULL);
77 getitimer(ITIMER_REAL, &it);
78 one_tick = it.it_interval;
79 }
80
81 if (tv_nz(&tcp->dtime))
82 *tv = tcp->dtime;
Denys Vlasenko7b609d52011-06-22 14:32:43 +020083 else if (tv_cmp(tv, &one_tick) > 0) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000084 if (tv_cmp(&shortest, &one_tick) < 0)
85 *tv = shortest;
86 else
87 *tv = one_tick;
88 }
89 }
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000090 if (tv_cmp(tv, &shortest) < 0)
91 shortest = *tv;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +010092 tv_add(&counts[scno].time, &counts[scno].time, tv);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000093}
94
95static int
96time_cmp(void *a, void *b)
97{
98 return -tv_cmp(&counts[*((int *) a)].time,
99 &counts[*((int *) b)].time);
100}
101
102static int
103syscall_cmp(void *a, void *b)
104{
105 return strcmp(sysent[*((int *) a)].sys_name,
106 sysent[*((int *) b)].sys_name);
107}
108
109static int
110count_cmp(void *a, void *b)
111{
112 int m = counts[*((int *) a)].calls;
113 int n = counts[*((int *) b)].calls;
114
115 return (m < n) ? 1 : (m > n) ? -1 : 0;
116}
117
118static int (*sortfun)();
119static struct timeval overhead = { -1, -1 };
120
121void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000122set_sortby(const char *sortby)
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000123{
124 if (strcmp(sortby, "time") == 0)
125 sortfun = time_cmp;
126 else if (strcmp(sortby, "calls") == 0)
127 sortfun = count_cmp;
128 else if (strcmp(sortby, "name") == 0)
129 sortfun = syscall_cmp;
130 else if (strcmp(sortby, "nothing") == 0)
131 sortfun = NULL;
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200132 else {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100133 error_msg_and_die("invalid sortby: '%s'", sortby);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000134 }
135}
136
137void set_overhead(int n)
138{
139 overhead.tv_sec = n / 1000000;
140 overhead.tv_usec = n % 1000000;
141}
142
143static void
144call_summary_pers(FILE *outf)
145{
146 int i, j;
147 int call_cum, error_cum;
148 struct timeval tv_cum, dtv;
149 double percent;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000150 const char *dashes = "-------------------------";
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000151 char error_str[16];
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000152 int *sorted_count = calloc(sizeof(int), nsyscalls);
153
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200154 if (!sorted_count)
155 die_out_of_memory();
Dmitry V. Levina97f03b2008-04-19 14:12:49 +0000156
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000157 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200158 if (overhead.tv_sec == -1) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000159 tv_mul(&overhead, &shortest, 8);
160 tv_div(&overhead, &overhead, 10);
161 }
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200162 for (i = 0; i < nsyscalls; i++) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000163 sorted_count[i] = i;
164 if (counts == NULL || counts[i].calls == 0)
165 continue;
166 tv_mul(&dtv, &overhead, counts[i].calls);
167 tv_sub(&counts[i].time, &counts[i].time, &dtv);
168 call_cum += counts[i].calls;
169 error_cum += counts[i].errors;
170 tv_add(&tv_cum, &tv_cum, &counts[i].time);
171 }
172 if (counts && sortfun)
173 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
174 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
175 "% time", "seconds", "usecs/call",
176 "calls", "errors", "syscall");
177 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
178 dashes, dashes, dashes, dashes, dashes, dashes);
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200179 if (counts) {
180 for (i = 0; i < nsyscalls; i++) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000181 j = sorted_count[i];
182 if (counts[j].calls == 0)
183 continue;
184 tv_div(&dtv, &counts[j].time, counts[j].calls);
185 if (counts[j].errors)
186 sprintf(error_str, "%d", counts[j].errors);
187 else
188 error_str[0] = '\0';
189 percent = (100.0 * tv_float(&counts[j].time)
190 / tv_float(&tv_cum));
Roland McGrath14e31742007-07-11 09:04:23 +0000191 fprintf(outf, "%6.2f %11.6f %11ld %9d %9.9s %s\n",
192 percent, tv_float(&counts[j].time),
H.J. Lu0b315b62012-02-03 10:16:03 -0800193 (long) (1000000 * dtv.tv_sec + dtv.tv_usec),
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000194 counts[j].calls,
195 error_str, sysent[j].sys_name);
196 }
197 }
198 free(sorted_count);
199
200 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
201 dashes, dashes, dashes, dashes, dashes, dashes);
202 if (error_cum)
203 sprintf(error_str, "%d", error_cum);
204 else
205 error_str[0] = '\0';
Roland McGrath14e31742007-07-11 09:04:23 +0000206 fprintf(outf, "%6.6s %11.6f %11.11s %9d %9.9s %s\n",
207 "100.00", tv_float(&tv_cum), "",
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000208 call_cum, error_str, "total");
209}
210
211void
212call_summary(FILE *outf)
213{
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200214 int i, old_pers = current_personality;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000215
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200216 for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000217 if (!countv[i])
218 continue;
219
220 if (current_personality != i)
221 set_personality(i);
222 if (i)
223 fprintf(outf,
Mike Frysingeraa6d8502012-04-27 18:58:20 -0400224 "System call usage summary for %d bit mode:\n",
Denys Vlasenkoc52826c2012-05-15 14:28:56 +0200225 current_wordsize * 8);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000226 call_summary_pers(outf);
227 }
228
229 if (old_pers != current_personality)
230 set_personality(old_pers);
231}