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 * Copyright (c) 2006-2017 The strace developers.
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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.
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.
39 /* Per-syscall stats structure */
41 /* time may be total latency or system time */
43 unsigned int calls, errors;
46 static struct call_counts *countv[SUPPORTED_PERSONALITIES];
47 #define counts (countv[current_personality])
49 static struct timeval shortest = { 1000000, 0 };
52 count_syscall(struct tcb *tcp, const struct timeval *syscall_exiting_tv)
55 struct timeval *tv = &wtv;
56 struct call_counts *cc;
58 if (!scno_in_range(tcp->scno))
62 counts = xcalloc(nsyscalls, sizeof(*counts));
63 cc = &counts[tcp->scno];
69 /* tv = wall clock time spent while in syscall */
70 tv_sub(tv, syscall_exiting_tv, &tcp->etime);
72 /* 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 };
76 if (one_tick.tv_sec == -1) {
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;
85 //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);
89 if (tv_nz(&tcp->dtime))
90 /* tv = system time spent, if it isn't 0 */
92 else if (tv_cmp(tv, &one_tick) > 0) {
93 /* tv = smallest "sane" time interval */
94 if (tv_cmp(&shortest, &one_tick) < 0)
100 if (tv_cmp(tv, &shortest) < 0)
102 tv_add(&cc->time, &cc->time, count_wallclock ? &wtv : tv);
106 time_cmp(void *a, void *b)
108 return -tv_cmp(&counts[*((int *) a)].time,
109 &counts[*((int *) b)].time);
113 syscall_cmp(void *a, void *b)
115 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 : "");
121 count_cmp(void *a, void *b)
123 int m = counts[*((int *) a)].calls;
124 int n = counts[*((int *) b)].calls;
126 return (m < n) ? 1 : (m > n) ? -1 : 0;
129 static int (*sortfun)();
130 static struct timeval overhead = { -1, -1 };
133 set_sortby(const char *sortby)
135 if (strcmp(sortby, "time") == 0)
137 else if (strcmp(sortby, "calls") == 0)
139 else if (strcmp(sortby, "name") == 0)
140 sortfun = syscall_cmp;
141 else if (strcmp(sortby, "nothing") == 0)
144 error_msg_and_help("invalid sortby: '%s'", sortby);
148 void set_overhead(int n)
150 overhead.tv_sec = n / 1000000;
151 overhead.tv_usec = n % 1000000;
155 call_summary_pers(FILE *outf)
157 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";
163 unsigned int call_cum, error_cum;
164 struct timeval tv_cum, dtv;
167 unsigned int *sorted_count;
169 fprintf(outf, header,
170 "% time", "seconds", "usecs/call",
171 "calls", "errors", "syscall");
172 fprintf(outf, header, dashes, dashes, dashes, dashes, dashes, dashes);
174 sorted_count = xcalloc(sizeof(sorted_count[0]), nsyscalls);
175 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
176 if (overhead.tv_sec == -1) {
177 tv_mul(&overhead, &shortest, 8);
178 tv_div(&overhead, &overhead, 10);
180 for (i = 0; i < nsyscalls; i++) {
182 if (counts == NULL || counts[i].calls == 0)
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);
190 float_tv_cum = tv_float(&tv_cum);
193 qsort((void *) sorted_count, nsyscalls,
194 sizeof(sorted_count[0]), sortfun);
195 for (i = 0; i < nsyscalls; i++) {
196 double float_syscall_time;
197 unsigned int idx = sorted_count[i];
198 struct call_counts *cc = &counts[idx];
201 tv_div(&dtv, &cc->time, cc->calls);
202 float_syscall_time = tv_float(&cc->time);
203 percent = (100.0 * float_syscall_time);
205 percent /= float_tv_cum;
206 /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */
208 percent, float_syscall_time,
209 (long) (1000000 * dtv.tv_sec + dtv.tv_usec),
210 cc->calls, cc->errors, sysent[idx].sys_name);
215 fprintf(outf, header, dashes, dashes, dashes, dashes, dashes, dashes);
216 fprintf(outf, summary,
217 "100.00", float_tv_cum, "",
218 call_cum, error_cum, "total");
222 call_summary(FILE *outf)
224 unsigned int i, old_pers = current_personality;
226 for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
230 if (current_personality != i)
234 "System call usage summary for %s mode:\n",
235 personality_names[i]);
236 call_summary_pers(outf);
239 if (old_pers != current_personality)
240 set_personality(old_pers);