blob: 4b16ee6606adedbb20583249994bdfffc38976de [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
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +000061 if (!counts)
62 counts = xcalloc(nsyscalls, sizeof(*counts));
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010063 cc = &counts[scno];
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000064
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010065 cc->calls++;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000066 if (tcp->u_error)
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010067 cc->errors++;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000068
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010069 /* tv = wall clock time spent while in syscall */
Dmitry V. Levinac5133d2014-05-29 18:10:00 +000070 tv_sub(tv, syscall_exiting_tv, &tcp->etime);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000071
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010072 /* Spent more wall clock time than spent system time? (usually yes) */
73 if (tv_cmp(tv, &tcp->dtime) > 0) {
74 static struct timeval one_tick = { -1, 0 };
75
76 if (one_tick.tv_sec == -1) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000077 /* Initialize it. */
78 struct itimerval it;
79
80 memset(&it, 0, sizeof it);
81 it.it_interval.tv_usec = 1;
82 setitimer(ITIMER_REAL, &it, NULL);
83 getitimer(ITIMER_REAL, &it);
84 one_tick = it.it_interval;
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010085//FIXME: this hack doesn't work (tested on linux-3.6.11): one_tick = 0.000000
86//tprintf(" one_tick.tv_usec:%u\n", (unsigned)one_tick.tv_usec);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000087 }
88
89 if (tv_nz(&tcp->dtime))
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010090 /* tv = system time spent, if it isn't 0 */
Dmitry V. Levinac5133d2014-05-29 18:10:00 +000091 tv = &tcp->dtime;
Denys Vlasenko7b609d52011-06-22 14:32:43 +020092 else if (tv_cmp(tv, &one_tick) > 0) {
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010093 /* tv = smallest "sane" time interval */
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000094 if (tv_cmp(&shortest, &one_tick) < 0)
Dmitry V. Levinac5133d2014-05-29 18:10:00 +000095 tv = &shortest;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000096 else
Dmitry V. Levinac5133d2014-05-29 18:10:00 +000097 tv = &one_tick;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000098 }
99 }
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000100 if (tv_cmp(tv, &shortest) < 0)
101 shortest = *tv;
Mark Hillse53bf232014-05-28 17:52:40 +0100102 tv_add(&cc->time, &cc->time, count_wallclock ? &wtv : tv);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000103}
104
105static int
106time_cmp(void *a, void *b)
107{
108 return -tv_cmp(&counts[*((int *) a)].time,
109 &counts[*((int *) b)].time);
110}
111
112static int
113syscall_cmp(void *a, void *b)
114{
115 return strcmp(sysent[*((int *) a)].sys_name,
116 sysent[*((int *) b)].sys_name);
117}
118
119static int
120count_cmp(void *a, void *b)
121{
122 int m = counts[*((int *) a)].calls;
123 int n = counts[*((int *) b)].calls;
124
125 return (m < n) ? 1 : (m > n) ? -1 : 0;
126}
127
128static int (*sortfun)();
129static struct timeval overhead = { -1, -1 };
130
131void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000132set_sortby(const char *sortby)
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000133{
134 if (strcmp(sortby, "time") == 0)
135 sortfun = time_cmp;
136 else if (strcmp(sortby, "calls") == 0)
137 sortfun = count_cmp;
138 else if (strcmp(sortby, "name") == 0)
139 sortfun = syscall_cmp;
140 else if (strcmp(sortby, "nothing") == 0)
141 sortfun = NULL;
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200142 else {
Elvira Khabirovaa2fdfe92015-11-26 17:18:00 +0300143 error_msg_and_help("invalid sortby: '%s'", sortby);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000144 }
145}
146
147void set_overhead(int n)
148{
149 overhead.tv_sec = n / 1000000;
150 overhead.tv_usec = n % 1000000;
151}
152
153static void
154call_summary_pers(FILE *outf)
155{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000156 unsigned int i;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000157 int call_cum, error_cum;
158 struct timeval tv_cum, dtv;
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100159 double float_tv_cum;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000160 double percent;
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100161 const char *dashes = "----------------";
162 char error_str[sizeof(int)*3];
163 int *sorted_count;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000164
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100165 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
166 "% time", "seconds", "usecs/call",
167 "calls", "errors", "syscall");
168 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
169 dashes, dashes, dashes, dashes, dashes, dashes);
170
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000171 sorted_count = xcalloc(sizeof(int), nsyscalls);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000172 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200173 if (overhead.tv_sec == -1) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000174 tv_mul(&overhead, &shortest, 8);
175 tv_div(&overhead, &overhead, 10);
176 }
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200177 for (i = 0; i < nsyscalls; i++) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000178 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 }
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100187 float_tv_cum = tv_float(&tv_cum);
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200188 if (counts) {
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100189 if (sortfun)
190 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200191 for (i = 0; i < nsyscalls; i++) {
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100192 double float_syscall_time;
193 int idx = sorted_count[i];
194 struct call_counts *cc = &counts[idx];
195 if (cc->calls == 0)
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000196 continue;
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100197 tv_div(&dtv, &cc->time, cc->calls);
198 error_str[0] = '\0';
199 if (cc->errors)
200 sprintf(error_str, "%u", cc->errors);
201 float_syscall_time = tv_float(&cc->time);
202 percent = (100.0 * float_syscall_time);
203 if (percent != 0.0)
204 percent /= float_tv_cum;
205 /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */
206 fprintf(outf, "%6.2f %11.6f %11lu %9u %9.9s %s\n",
207 percent, float_syscall_time,
H.J. Lu0b315b62012-02-03 10:16:03 -0800208 (long) (1000000 * dtv.tv_sec + dtv.tv_usec),
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100209 cc->calls,
210 error_str, sysent[idx].sys_name);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000211 }
212 }
213 free(sorted_count);
214
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100215 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000216 dashes, dashes, dashes, dashes, dashes, dashes);
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100217 error_str[0] = '\0';
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000218 if (error_cum)
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100219 sprintf(error_str, "%u", error_cum);
220 fprintf(outf, "%6.6s %11.6f %11.11s %9u %9.9s %s\n",
221 "100.00", float_tv_cum, "",
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000222 call_cum, error_str, "total");
223}
224
225void
226call_summary(FILE *outf)
227{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000228 unsigned int i, old_pers = current_personality;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000229
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200230 for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000231 if (!countv[i])
232 continue;
233
234 if (current_personality != i)
235 set_personality(i);
236 if (i)
237 fprintf(outf,
Mike Frysingeraa6d8502012-04-27 18:58:20 -0400238 "System call usage summary for %d bit mode:\n",
Denys Vlasenkoc52826c2012-05-15 14:28:56 +0200239 current_wordsize * 8);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000240 call_summary_pers(outf);
241 }
242
243 if (old_pers != current_personality)
244 set_personality(old_pers);
245}