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-2018 The strace developers.
12 * All rights reserved.
14 * SPDX-License-Identifier: LGPL-2.1-or-later
19 /* Per-syscall stats structure */
21 /* time may be total latency or system time */
23 unsigned int calls, errors;
26 static struct call_counts *countv[SUPPORTED_PERSONALITIES];
27 #define counts (countv[current_personality])
29 static struct timespec overhead;
32 count_syscall(struct tcb *tcp, const struct timespec *syscall_exiting_ts)
34 if (!scno_in_range(tcp->scno))
38 counts = xcalloc(nsyscalls, sizeof(*counts));
39 struct call_counts *cc = &counts[tcp->scno];
45 if (count_wallclock) {
46 /* wall clock time spent while in syscall */
48 ts_sub(&wts, syscall_exiting_ts, &tcp->etime);
50 ts_add(&cc->time, &cc->time, &wts);
52 /* system CPU time spent while in syscall */
53 ts_add(&cc->time, &cc->time, &tcp->dtime);
58 time_cmp(const void *a, const void *b)
60 const unsigned int *a_int = a;
61 const unsigned int *b_int = b;
62 return -ts_cmp(&counts[*a_int].time, &counts[*b_int].time);
66 syscall_cmp(const void *a, const void *b)
68 const unsigned int *a_int = a;
69 const unsigned int *b_int = b;
70 const char *a_name = sysent[*a_int].sys_name;
71 const char *b_name = sysent[*b_int].sys_name;
72 return strcmp(a_name ? a_name : "", b_name ? b_name : "");
76 count_cmp(const void *a, const void *b)
78 const unsigned int *a_int = a;
79 const unsigned int *b_int = b;
80 unsigned int m = counts[*a_int].calls;
81 unsigned int n = counts[*b_int].calls;
83 return (m < n) ? 1 : (m > n) ? -1 : 0;
86 static int (*sortfun)(const void *, const void *);
89 set_sortby(const char *sortby)
91 if (strcmp(sortby, "time") == 0)
93 else if (strcmp(sortby, "calls") == 0)
95 else if (strcmp(sortby, "name") == 0)
96 sortfun = syscall_cmp;
97 else if (strcmp(sortby, "nothing") == 0)
100 error_msg_and_help("invalid sortby: '%s'", sortby);
105 set_overhead(const char *str)
107 return parse_ts(str, &overhead);
111 call_summary_pers(FILE *outf)
113 static const char dashes[] = "----------------";
114 static const char header[] = "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n";
115 static const char data[] = "%6.2f %11.6f %11lu %9u %9.u %s\n";
116 static const char summary[] = "%6.6s %11.6f %11.11s %9u %9.u %s\n";
119 unsigned int call_cum, error_cum;
120 struct timespec tv_cum, dtv;
123 unsigned int *sorted_count;
125 fprintf(outf, header,
126 "% time", "seconds", "usecs/call",
127 "calls", "errors", "syscall");
128 fprintf(outf, header, dashes, dashes, dashes, dashes, dashes, dashes);
130 sorted_count = xcalloc(sizeof(sorted_count[0]), nsyscalls);
131 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_nsec = 0;
132 for (i = 0; i < nsyscalls; i++) {
134 if (counts == NULL || counts[i].calls == 0)
136 ts_mul(&dtv, &overhead, counts[i].calls);
137 ts_sub(&counts[i].time, &counts[i].time, &dtv);
138 if (counts[i].time.tv_sec < 0 || counts[i].time.tv_nsec < 0)
139 counts[i].time.tv_sec = counts[i].time.tv_nsec = 0;
140 call_cum += counts[i].calls;
141 error_cum += counts[i].errors;
142 ts_add(&tv_cum, &tv_cum, &counts[i].time);
144 float_tv_cum = ts_float(&tv_cum);
147 qsort((void *) sorted_count, nsyscalls,
148 sizeof(sorted_count[0]), sortfun);
149 for (i = 0; i < nsyscalls; i++) {
150 double float_syscall_time;
151 unsigned int idx = sorted_count[i];
152 struct call_counts *cc = &counts[idx];
155 ts_div(&dtv, &cc->time, cc->calls);
156 float_syscall_time = ts_float(&cc->time);
157 percent = (100.0 * float_syscall_time);
159 percent /= float_tv_cum;
160 /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */
162 percent, float_syscall_time,
163 (long) (1000000 * dtv.tv_sec + dtv.tv_nsec / 1000),
164 cc->calls, cc->errors, sysent[idx].sys_name);
169 fprintf(outf, header, dashes, dashes, dashes, dashes, dashes, dashes);
170 fprintf(outf, summary,
171 "100.00", float_tv_cum, "",
172 call_cum, error_cum, "total");
176 call_summary(FILE *outf)
178 unsigned int i, old_pers = current_personality;
180 for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
184 if (current_personality != i)
188 "System call usage summary for %s mode:\n",
189 personality_names[i]);
190 call_summary_pers(outf);
193 if (old_pers != current_personality)
194 set_personality(old_pers);