blob: e41994d3715a9728c7cf1070da9b85d6caed2925 [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
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010038/* Per-syscall stats structure */
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000039struct call_counts {
Mark Hillse53bf232014-05-28 17:52:40 +010040 /* time may be total latency or system time */
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000041 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. Levinac5133d2014-05-29 18:10:00 +000051count_syscall(struct tcb *tcp, const struct timeval *syscall_exiting_tv)
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000052{
Dmitry V. Levinac5133d2014-05-29 18:10:00 +000053 struct timeval wtv;
54 struct timeval *tv = &wtv;
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010055 struct call_counts *cc;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +010056 unsigned long scno = tcp->scno;
57
58 if (!SCNO_IN_RANGE(scno))
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +020059 return;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000060
Denys Vlasenko7b609d52011-06-22 14:32:43 +020061 if (!counts) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000062 counts = calloc(nsyscalls, sizeof(*counts));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +020063 if (!counts)
64 die_out_of_memory();
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000065 }
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010066 cc = &counts[scno];
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000067
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010068 cc->calls++;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000069 if (tcp->u_error)
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010070 cc->errors++;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000071
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010072 /* tv = wall clock time spent while in syscall */
Dmitry V. Levinac5133d2014-05-29 18:10:00 +000073 tv_sub(tv, syscall_exiting_tv, &tcp->etime);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000074
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010075 /* 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 };
78
79 if (one_tick.tv_sec == -1) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000080 /* 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;
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010088//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);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000090 }
91
92 if (tv_nz(&tcp->dtime))
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010093 /* tv = system time spent, if it isn't 0 */
Dmitry V. Levinac5133d2014-05-29 18:10:00 +000094 tv = &tcp->dtime;
Denys Vlasenko7b609d52011-06-22 14:32:43 +020095 else if (tv_cmp(tv, &one_tick) > 0) {
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010096 /* tv = smallest "sane" time interval */
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000097 if (tv_cmp(&shortest, &one_tick) < 0)
Dmitry V. Levinac5133d2014-05-29 18:10:00 +000098 tv = &shortest;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000099 else
Dmitry V. Levinac5133d2014-05-29 18:10:00 +0000100 tv = &one_tick;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000101 }
102 }
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000103 if (tv_cmp(tv, &shortest) < 0)
104 shortest = *tv;
Mark Hillse53bf232014-05-28 17:52:40 +0100105 tv_add(&cc->time, &cc->time, count_wallclock ? &wtv : tv);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000106}
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
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000135set_sortby(const char *sortby)
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000136{
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;
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200145 else {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100146 error_msg_and_die("invalid sortby: '%s'", sortby);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000147 }
148}
149
150void set_overhead(int n)
151{
152 overhead.tv_sec = n / 1000000;
153 overhead.tv_usec = n % 1000000;
154}
155
156static void
157call_summary_pers(FILE *outf)
158{
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100159 int i;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000160 int call_cum, error_cum;
161 struct timeval tv_cum, dtv;
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100162 double float_tv_cum;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000163 double percent;
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100164 const char *dashes = "----------------";
165 char error_str[sizeof(int)*3];
166 int *sorted_count;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000167
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100168 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);
173
174 sorted_count = calloc(sizeof(int), nsyscalls);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200175 if (!sorted_count)
176 die_out_of_memory();
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000177 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200178 if (overhead.tv_sec == -1) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000179 tv_mul(&overhead, &shortest, 8);
180 tv_div(&overhead, &overhead, 10);
181 }
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200182 for (i = 0; i < nsyscalls; i++) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000183 sorted_count[i] = i;
184 if (counts == NULL || counts[i].calls == 0)
185 continue;
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);
191 }
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100192 float_tv_cum = tv_float(&tv_cum);
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200193 if (counts) {
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100194 if (sortfun)
195 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200196 for (i = 0; i < nsyscalls; i++) {
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100197 double float_syscall_time;
198 int idx = sorted_count[i];
199 struct call_counts *cc = &counts[idx];
200 if (cc->calls == 0)
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000201 continue;
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100202 tv_div(&dtv, &cc->time, cc->calls);
203 error_str[0] = '\0';
204 if (cc->errors)
205 sprintf(error_str, "%u", cc->errors);
206 float_syscall_time = tv_float(&cc->time);
207 percent = (100.0 * float_syscall_time);
208 if (percent != 0.0)
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,
H.J. Lu0b315b62012-02-03 10:16:03 -0800213 (long) (1000000 * dtv.tv_sec + dtv.tv_usec),
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100214 cc->calls,
215 error_str, sysent[idx].sys_name);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000216 }
217 }
218 free(sorted_count);
219
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100220 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000221 dashes, dashes, dashes, dashes, dashes, dashes);
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100222 error_str[0] = '\0';
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000223 if (error_cum)
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100224 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, "",
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000227 call_cum, error_str, "total");
228}
229
230void
231call_summary(FILE *outf)
232{
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200233 int i, old_pers = current_personality;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000234
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200235 for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000236 if (!countv[i])
237 continue;
238
239 if (current_personality != i)
240 set_personality(i);
241 if (i)
242 fprintf(outf,
Mike Frysingeraa6d8502012-04-27 18:58:20 -0400243 "System call usage summary for %d bit mode:\n",
Denys Vlasenkoc52826c2012-05-15 14:28:56 +0200244 current_wordsize * 8);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000245 call_summary_pers(outf);
246 }
247
248 if (old_pers != current_personality)
249 set_personality(old_pers);
250}