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 const struct timespec zero_ts;
31 static struct timespec overhead;
34 count_syscall(struct tcb *tcp, const struct timespec *syscall_exiting_ts)
36 if (!scno_in_range(tcp->scno))
40 counts = xcalloc(nsyscalls, sizeof(*counts));
41 struct call_counts *cc = &counts[tcp->scno];
48 if (count_wallclock) {
49 /* wall clock time spent while in syscall */
50 ts_sub(&wts, syscall_exiting_ts, &tcp->etime);
52 /* system CPU time spent while in syscall */
53 ts_sub(&wts, &tcp->stime, &tcp->ltime);
56 ts_sub(&wts, &wts, &overhead);
57 ts_add(&cc->time, &cc->time, ts_max(&wts, &zero_ts));
61 time_cmp(const void *a, const void *b)
63 const unsigned int *a_int = a;
64 const unsigned int *b_int = b;
65 return -ts_cmp(&counts[*a_int].time, &counts[*b_int].time);
69 syscall_cmp(const void *a, const void *b)
71 const unsigned int *a_int = a;
72 const unsigned int *b_int = b;
73 const char *a_name = sysent[*a_int].sys_name;
74 const char *b_name = sysent[*b_int].sys_name;
75 return strcmp(a_name ? a_name : "", b_name ? b_name : "");
79 count_cmp(const void *a, const void *b)
81 const unsigned int *a_int = a;
82 const unsigned int *b_int = b;
83 unsigned int m = counts[*a_int].calls;
84 unsigned int n = counts[*b_int].calls;
86 return (m < n) ? 1 : (m > n) ? -1 : 0;
90 error_cmp(const void *a, const void *b)
92 const unsigned int *a_int = a;
93 const unsigned int *b_int = b;
94 unsigned int m = counts[*a_int].errors;
95 unsigned int n = counts[*b_int].errors;
97 return (m < n) ? 1 : (m > n) ? -1 : 0;
100 static int (*sortfun)(const void *, const void *);
103 set_sortby(const char *sortby)
105 static const struct {
106 int (*fn)(const void *, const void *);
109 { time_cmp, "time" },
110 { time_cmp, "time_total" },
111 { time_cmp, "total_time" },
112 { count_cmp, "calls" },
113 { count_cmp, "count" },
114 { error_cmp, "error" },
115 { error_cmp, "errors" },
116 { syscall_cmp, "name" },
117 { syscall_cmp, "syscall" },
118 { syscall_cmp, "syscall_name" },
123 for (size_t i = 0; i < ARRAY_SIZE(sort_fns); ++i) {
124 if (!strcmp(sort_fns[i].name, sortby)) {
125 sortfun = sort_fns[i].fn;
130 error_msg_and_help("invalid sortby: '%s'", sortby);
134 set_overhead(const char *str)
136 return parse_ts(str, &overhead);
140 call_summary_pers(FILE *outf)
142 static const char dashes[] = "----------------";
143 static const char header[] = "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n";
144 static const char data[] = "%6.2f %11.6f %11lu %9u %9.u %s\n";
145 static const char summary[] = "%6.6s %11.6f %11.11s %9u %9.u %s\n";
148 unsigned int call_cum, error_cum;
149 struct timespec tv_cum, dtv;
152 unsigned int *sorted_count;
154 fprintf(outf, header,
155 "% time", "seconds", "usecs/call",
156 "calls", "errors", "syscall");
157 fprintf(outf, header, dashes, dashes, dashes, dashes, dashes, dashes);
159 sorted_count = xcalloc(sizeof(sorted_count[0]), nsyscalls);
160 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_nsec = 0;
161 for (i = 0; i < nsyscalls; i++) {
163 if (counts == NULL || counts[i].calls == 0)
165 call_cum += counts[i].calls;
166 error_cum += counts[i].errors;
167 ts_add(&tv_cum, &tv_cum, &counts[i].time);
169 float_tv_cum = ts_float(&tv_cum);
172 qsort((void *) sorted_count, nsyscalls,
173 sizeof(sorted_count[0]), sortfun);
174 for (i = 0; i < nsyscalls; i++) {
175 double float_syscall_time;
176 unsigned int idx = sorted_count[i];
177 struct call_counts *cc = &counts[idx];
180 ts_div(&dtv, &cc->time, cc->calls);
181 float_syscall_time = ts_float(&cc->time);
182 percent = (100.0 * float_syscall_time);
184 percent /= float_tv_cum;
185 /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */
187 percent, float_syscall_time,
188 (long) (1000000 * dtv.tv_sec + dtv.tv_nsec / 1000),
189 cc->calls, cc->errors, sysent[idx].sys_name);
194 fprintf(outf, header, dashes, dashes, dashes, dashes, dashes, dashes);
195 fprintf(outf, summary,
196 "100.00", float_tv_cum, "",
197 call_cum, error_cum, "total");
201 call_summary(FILE *outf)
203 unsigned int i, old_pers = current_personality;
205 for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
209 if (current_personality != i)
213 "System call usage summary for %s mode:\n",
214 personality_names[i]);
215 call_summary_pers(outf);
218 if (old_pers != current_personality)
219 set_personality(old_pers);