]> granicus.if.org Git - strace/blob - count.c
Remove traces of riscv64 mpers
[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 const struct timespec zero_ts;
30
31 static struct timespec overhead;
32
33 void
34 count_syscall(struct tcb *tcp, const struct timespec *syscall_exiting_ts)
35 {
36         if (!scno_in_range(tcp->scno))
37                 return;
38
39         if (!counts)
40                 counts = xcalloc(nsyscalls, sizeof(*counts));
41         struct call_counts *cc = &counts[tcp->scno];
42
43         cc->calls++;
44         if (syserror(tcp))
45                 cc->errors++;
46
47         struct timespec wts;
48         if (count_wallclock) {
49                 /* wall clock time spent while in syscall */
50                 ts_sub(&wts, syscall_exiting_ts, &tcp->etime);
51         } else {
52                 /* system CPU time spent while in syscall */
53                 ts_sub(&wts, &tcp->stime, &tcp->ltime);
54         }
55
56         ts_sub(&wts, &wts, &overhead);
57         ts_add(&cc->time, &cc->time, ts_max(&wts, &zero_ts));
58 }
59
60 static int
61 time_cmp(const void *a, const void *b)
62 {
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);
66 }
67
68 static int
69 syscall_cmp(const void *a, const void *b)
70 {
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 : "");
76 }
77
78 static int
79 count_cmp(const void *a, const void *b)
80 {
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;
85
86         return (m < n) ? 1 : (m > n) ? -1 : 0;
87 }
88
89 static int
90 error_cmp(const void *a, const void *b)
91 {
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;
96
97         return (m < n) ? 1 : (m > n) ? -1 : 0;
98 }
99
100 static int (*sortfun)(const void *, const void *);
101
102 void
103 set_sortby(const char *sortby)
104 {
105         static const struct {
106                 int (*fn)(const void *, const void *);
107                 const char *name;
108         } sort_fns[] = {
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" },
119                 { NULL,         "none" },
120                 { NULL,         "nothing" },
121         };
122
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;
126                         return;
127                 }
128         }
129
130         error_msg_and_help("invalid sortby: '%s'", sortby);
131 }
132
133 int
134 set_overhead(const char *str)
135 {
136         return parse_ts(str, &overhead);
137 }
138
139 static void
140 call_summary_pers(FILE *outf)
141 {
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";
146
147         unsigned int i;
148         unsigned int call_cum, error_cum;
149         struct timespec tv_cum, dtv;
150         double  float_tv_cum;
151         double  percent;
152         unsigned int *sorted_count;
153
154         fprintf(outf, header,
155                 "% time", "seconds", "usecs/call",
156                 "calls", "errors", "syscall");
157         fprintf(outf, header, dashes, dashes, dashes, dashes, dashes, dashes);
158
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++) {
162                 sorted_count[i] = i;
163                 if (counts == NULL || counts[i].calls == 0)
164                         continue;
165                 call_cum += counts[i].calls;
166                 error_cum += counts[i].errors;
167                 ts_add(&tv_cum, &tv_cum, &counts[i].time);
168         }
169         float_tv_cum = ts_float(&tv_cum);
170         if (counts) {
171                 if (sortfun)
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];
178                         if (cc->calls == 0)
179                                 continue;
180                         ts_div(&dtv, &cc->time, cc->calls);
181                         float_syscall_time = ts_float(&cc->time);
182                         percent = (100.0 * float_syscall_time);
183                         if (percent != 0.0)
184                                    percent /= float_tv_cum;
185                         /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */
186                         fprintf(outf, data,
187                                 percent, float_syscall_time,
188                                 (long) (1000000 * dtv.tv_sec + dtv.tv_nsec / 1000),
189                                 cc->calls, cc->errors, sysent[idx].sys_name);
190                 }
191         }
192         free(sorted_count);
193
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");
198 }
199
200 void
201 call_summary(FILE *outf)
202 {
203         unsigned int i, old_pers = current_personality;
204
205         for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
206                 if (!countv[i])
207                         continue;
208
209                 if (current_personality != i)
210                         set_personality(i);
211                 if (i)
212                         fprintf(outf,
213                                 "System call usage summary for %s mode:\n",
214                                 personality_names[i]);
215                 call_summary_pers(outf);
216         }
217
218         if (old_pers != current_personality)
219                 set_personality(old_pers);
220 }