]> granicus.if.org Git - strace/blob - count.c
298cb6815e88bd5f69e231f5738b0abcaa5687f3
[strace] / count.c
1 /*
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.
13  *
14  * SPDX-License-Identifier: LGPL-2.1-or-later
15  */
16
17 #include "defs.h"
18
19 /* Per-syscall stats structure */
20 struct call_counts {
21         /* time may be total latency or system time */
22         struct timespec time;
23         unsigned int calls, errors;
24 };
25
26 static struct call_counts *countv[SUPPORTED_PERSONALITIES];
27 #define counts (countv[current_personality])
28
29 static struct timespec overhead;
30
31 void
32 count_syscall(struct tcb *tcp, const struct timespec *syscall_exiting_ts)
33 {
34         if (!scno_in_range(tcp->scno))
35                 return;
36
37         if (!counts)
38                 counts = xcalloc(nsyscalls, sizeof(*counts));
39         struct call_counts *cc = &counts[tcp->scno];
40
41         cc->calls++;
42         if (syserror(tcp))
43                 cc->errors++;
44
45         if (count_wallclock) {
46                 /* wall clock time spent while in syscall */
47                 struct timespec wts;
48                 ts_sub(&wts, syscall_exiting_ts, &tcp->etime);
49
50                 ts_add(&cc->time, &cc->time, &wts);
51         } else {
52                 /* system CPU time spent while in syscall */
53                 ts_add(&cc->time, &cc->time, &tcp->dtime);
54         }
55 }
56
57 static int
58 time_cmp(const void *a, const void *b)
59 {
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);
63 }
64
65 static int
66 syscall_cmp(const void *a, const void *b)
67 {
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 : "");
73 }
74
75 static int
76 count_cmp(const void *a, const void *b)
77 {
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;
82
83         return (m < n) ? 1 : (m > n) ? -1 : 0;
84 }
85
86 static int (*sortfun)(const void *, const void *);
87
88 void
89 set_sortby(const char *sortby)
90 {
91         if (strcmp(sortby, "time") == 0)
92                 sortfun = time_cmp;
93         else if (strcmp(sortby, "calls") == 0)
94                 sortfun = count_cmp;
95         else if (strcmp(sortby, "name") == 0)
96                 sortfun = syscall_cmp;
97         else if (strcmp(sortby, "nothing") == 0)
98                 sortfun = NULL;
99         else {
100                 error_msg_and_help("invalid sortby: '%s'", sortby);
101         }
102 }
103
104 int
105 set_overhead(const char *str)
106 {
107         return parse_ts(str, &overhead);
108 }
109
110 static void
111 call_summary_pers(FILE *outf)
112 {
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";
117
118         unsigned int i;
119         unsigned int call_cum, error_cum;
120         struct timespec tv_cum, dtv;
121         double  float_tv_cum;
122         double  percent;
123         unsigned int *sorted_count;
124
125         fprintf(outf, header,
126                 "% time", "seconds", "usecs/call",
127                 "calls", "errors", "syscall");
128         fprintf(outf, header, dashes, dashes, dashes, dashes, dashes, dashes);
129
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++) {
133                 sorted_count[i] = i;
134                 if (counts == NULL || counts[i].calls == 0)
135                         continue;
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);
143         }
144         float_tv_cum = ts_float(&tv_cum);
145         if (counts) {
146                 if (sortfun)
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];
153                         if (cc->calls == 0)
154                                 continue;
155                         ts_div(&dtv, &cc->time, cc->calls);
156                         float_syscall_time = ts_float(&cc->time);
157                         percent = (100.0 * float_syscall_time);
158                         if (percent != 0.0)
159                                    percent /= float_tv_cum;
160                         /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */
161                         fprintf(outf, data,
162                                 percent, float_syscall_time,
163                                 (long) (1000000 * dtv.tv_sec + dtv.tv_nsec / 1000),
164                                 cc->calls, cc->errors, sysent[idx].sys_name);
165                 }
166         }
167         free(sorted_count);
168
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");
173 }
174
175 void
176 call_summary(FILE *outf)
177 {
178         unsigned int i, old_pers = current_personality;
179
180         for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
181                 if (!countv[i])
182                         continue;
183
184                 if (current_personality != i)
185                         set_personality(i);
186                 if (i)
187                         fprintf(outf,
188                                 "System call usage summary for %s mode:\n",
189                                 personality_names[i]);
190                 call_summary_pers(outf);
191         }
192
193         if (old_pers != current_personality)
194                 set_personality(old_pers);
195 }