blob: 2065e715ba994a89938eb47bcc34eecf33fe5d5f [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>
Elliott Hughesb7556142018-02-20 17:03:16 -080011 * Copyright (c) 2006-2018 The strace developers.
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000012 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000035 */
36
37#include "defs.h"
38
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010039/* Per-syscall stats structure */
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000040struct call_counts {
Mark Hillse53bf232014-05-28 17:52:40 +010041 /* time may be total latency or system time */
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000042 struct timeval time;
Elliott Hughesb7556142018-02-20 17:03:16 -080043 unsigned int calls, errors;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000044};
45
46static struct call_counts *countv[SUPPORTED_PERSONALITIES];
47#define counts (countv[current_personality])
48
49static struct timeval shortest = { 1000000, 0 };
50
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +020051void
Dmitry V. Levinac5133d2014-05-29 18:10:00 +000052count_syscall(struct tcb *tcp, const struct timeval *syscall_exiting_tv)
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000053{
Dmitry V. Levinac5133d2014-05-29 18:10:00 +000054 struct timeval wtv;
55 struct timeval *tv = &wtv;
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010056 struct call_counts *cc;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +010057
Elliott Hughesd35df492017-02-15 15:19:05 -080058 if (!scno_in_range(tcp->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));
Elliott Hughesd35df492017-02-15 15:19:05 -080063 cc = &counts[tcp->scno];
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000064
Denys Vlasenko8050cdc2013-03-07 12:27:40 +010065 cc->calls++;
Elliott Hughesd35df492017-02-15 15:19:05 -080066 if (syserror(tcp))
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
Elliott Hughesdc75b012017-07-05 13:54:44 -070080 memset(&it, 0, sizeof(it));
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +000081 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{
Dmitry V. Levinfa925dc2016-05-10 00:16:20 +0000115 const char *a_name = sysent[*((int *) a)].sys_name;
116 const char *b_name = sysent[*((int *) b)].sys_name;
117 return strcmp(a_name ? a_name : "", b_name ? b_name : "");
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000118}
119
120static int
121count_cmp(void *a, void *b)
122{
123 int m = counts[*((int *) a)].calls;
124 int n = counts[*((int *) b)].calls;
125
126 return (m < n) ? 1 : (m > n) ? -1 : 0;
127}
128
129static int (*sortfun)();
130static struct timeval overhead = { -1, -1 };
131
132void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000133set_sortby(const char *sortby)
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000134{
135 if (strcmp(sortby, "time") == 0)
136 sortfun = time_cmp;
137 else if (strcmp(sortby, "calls") == 0)
138 sortfun = count_cmp;
139 else if (strcmp(sortby, "name") == 0)
140 sortfun = syscall_cmp;
141 else if (strcmp(sortby, "nothing") == 0)
142 sortfun = NULL;
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200143 else {
Elvira Khabirovaa2fdfe92015-11-26 17:18:00 +0300144 error_msg_and_help("invalid sortby: '%s'", sortby);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000145 }
146}
147
148void set_overhead(int n)
149{
150 overhead.tv_sec = n / 1000000;
151 overhead.tv_usec = n % 1000000;
152}
153
154static void
155call_summary_pers(FILE *outf)
156{
Elliott Hughesb7556142018-02-20 17:03:16 -0800157 static const char dashes[] = "----------------";
158 static const char header[] = "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n";
159 static const char data[] = "%6.2f %11.6f %11lu %9u %9.u %s\n";
160 static const char summary[] = "%6.6s %11.6f %11.11s %9u %9.u %s\n";
161
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000162 unsigned int i;
Elliott Hughesb7556142018-02-20 17:03:16 -0800163 unsigned int call_cum, error_cum;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000164 struct timeval tv_cum, dtv;
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100165 double float_tv_cum;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000166 double percent;
Elliott Hughesb7556142018-02-20 17:03:16 -0800167 unsigned int *sorted_count;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000168
Elliott Hughesb7556142018-02-20 17:03:16 -0800169 fprintf(outf, header,
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100170 "% time", "seconds", "usecs/call",
171 "calls", "errors", "syscall");
Elliott Hughesb7556142018-02-20 17:03:16 -0800172 fprintf(outf, header, dashes, dashes, dashes, dashes, dashes, dashes);
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100173
Elliott Hughesb7556142018-02-20 17:03:16 -0800174 sorted_count = xcalloc(sizeof(sorted_count[0]), nsyscalls);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000175 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200176 if (overhead.tv_sec == -1) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000177 tv_mul(&overhead, &shortest, 8);
178 tv_div(&overhead, &overhead, 10);
179 }
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200180 for (i = 0; i < nsyscalls; i++) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000181 sorted_count[i] = i;
182 if (counts == NULL || counts[i].calls == 0)
183 continue;
184 tv_mul(&dtv, &overhead, counts[i].calls);
185 tv_sub(&counts[i].time, &counts[i].time, &dtv);
186 call_cum += counts[i].calls;
187 error_cum += counts[i].errors;
188 tv_add(&tv_cum, &tv_cum, &counts[i].time);
189 }
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100190 float_tv_cum = tv_float(&tv_cum);
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200191 if (counts) {
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100192 if (sortfun)
Elliott Hughesb7556142018-02-20 17:03:16 -0800193 qsort((void *) sorted_count, nsyscalls,
194 sizeof(sorted_count[0]), sortfun);
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200195 for (i = 0; i < nsyscalls; i++) {
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100196 double float_syscall_time;
Elliott Hughesb7556142018-02-20 17:03:16 -0800197 unsigned int idx = sorted_count[i];
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100198 struct call_counts *cc = &counts[idx];
199 if (cc->calls == 0)
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000200 continue;
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100201 tv_div(&dtv, &cc->time, cc->calls);
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100202 float_syscall_time = tv_float(&cc->time);
203 percent = (100.0 * float_syscall_time);
204 if (percent != 0.0)
205 percent /= float_tv_cum;
206 /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */
Elliott Hughesb7556142018-02-20 17:03:16 -0800207 fprintf(outf, data,
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100208 percent, float_syscall_time,
H.J. Lu0b315b62012-02-03 10:16:03 -0800209 (long) (1000000 * dtv.tv_sec + dtv.tv_usec),
Elliott Hughesb7556142018-02-20 17:03:16 -0800210 cc->calls, cc->errors, sysent[idx].sys_name);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000211 }
212 }
213 free(sorted_count);
214
Elliott Hughesb7556142018-02-20 17:03:16 -0800215 fprintf(outf, header, dashes, dashes, dashes, dashes, dashes, dashes);
216 fprintf(outf, summary,
Denys Vlasenko8050cdc2013-03-07 12:27:40 +0100217 "100.00", float_tv_cum, "",
Elliott Hughesb7556142018-02-20 17:03:16 -0800218 call_cum, error_cum, "total");
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000219}
220
221void
222call_summary(FILE *outf)
223{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000224 unsigned int i, old_pers = current_personality;
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000225
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200226 for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000227 if (!countv[i])
228 continue;
229
230 if (current_personality != i)
231 set_personality(i);
232 if (i)
233 fprintf(outf,
Elliott Hughesb7556142018-02-20 17:03:16 -0800234 "System call usage summary for %s mode:\n",
235 personality_names[i]);
Dmitry V. Levin7d61ff12006-12-21 21:15:04 +0000236 call_summary_pers(outf);
237 }
238
239 if (old_pers != current_personality)
240 set_personality(old_pers);
241}