Branch data Line data Source code
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-2017 The strace developers.
12 : : * All rights reserved.
13 : : *
14 : : * Redistribution and use in source and binary forms, with or without
15 : : * modification, are permitted provided that the following conditions
16 : : * are met:
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.
24 : : *
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.
35 : : */
36 : :
37 : : #include "defs.h"
38 : :
39 : : /* Per-syscall stats structure */
40 : : struct call_counts {
41 : : /* time may be total latency or system time */
42 : : struct timeval time;
43 : : int calls, errors;
44 : : };
45 : :
46 : : static struct call_counts *countv[SUPPORTED_PERSONALITIES];
47 : : #define counts (countv[current_personality])
48 : :
49 : : static struct timeval shortest = { 1000000, 0 };
50 : :
51 : : void
52 : 5172 : count_syscall(struct tcb *tcp, const struct timeval *syscall_exiting_tv)
53 : : {
54 : : struct timeval wtv;
55 : 5172 : struct timeval *tv = &wtv;
56 : : struct call_counts *cc;
57 : :
58 [ + - ]: 5172 : if (!scno_in_range(tcp->scno))
59 : 0 : return;
60 : :
61 [ + + ]: 5172 : if (!counts)
62 : 21 : counts = xcalloc(nsyscalls, sizeof(*counts));
63 : 5172 : cc = &counts[tcp->scno];
64 : :
65 : 5172 : cc->calls++;
66 [ + + ]: 5172 : if (syserror(tcp))
67 : 2107 : cc->errors++;
68 : :
69 : : /* tv = wall clock time spent while in syscall */
70 : 5172 : tv_sub(tv, syscall_exiting_tv, &tcp->etime);
71 : :
72 : : /* Spent more wall clock time than spent system time? (usually yes) */
73 [ + - ]: 5172 : if (tv_cmp(tv, &tcp->dtime) > 0) {
74 : : static struct timeval one_tick = { -1, 0 };
75 : :
76 [ + + ]: 5172 : if (one_tick.tv_sec == -1) {
77 : : /* Initialize it. */
78 : : struct itimerval it;
79 : :
80 : 16 : memset(&it, 0, sizeof(it));
81 : 16 : it.it_interval.tv_usec = 1;
82 : 16 : setitimer(ITIMER_REAL, &it, NULL);
83 : 16 : getitimer(ITIMER_REAL, &it);
84 : 16 : 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);
87 : : }
88 : :
89 [ + - ]: 5172 : if (tv_nz(&tcp->dtime))
90 : : /* tv = system time spent, if it isn't 0 */
91 : : tv = &tcp->dtime;
92 [ + - ]: 5172 : else if (tv_cmp(tv, &one_tick) > 0) {
93 : : /* tv = smallest "sane" time interval */
94 [ + - ]: 5172 : if (tv_cmp(&shortest, &one_tick) < 0)
95 : : tv = &shortest;
96 : : else
97 : 5172 : tv = &one_tick;
98 : : }
99 : : }
100 [ + + ]: 5172 : if (tv_cmp(tv, &shortest) < 0)
101 : 16 : shortest = *tv;
102 [ + + ]: 5172 : tv_add(&cc->time, &cc->time, count_wallclock ? &wtv : tv);
103 : : }
104 : :
105 : : static int
106 : 23450 : time_cmp(void *a, void *b)
107 : : {
108 : 23450 : return -tv_cmp(&counts[*((int *) a)].time,
109 : 23450 : &counts[*((int *) b)].time);
110 : : }
111 : :
112 : : static int
113 : 8050 : syscall_cmp(void *a, void *b)
114 : : {
115 : 8050 : const char *a_name = sysent[*((int *) a)].sys_name;
116 : 8050 : const char *b_name = sysent[*((int *) b)].sys_name;
117 [ + + ][ + + ]: 8050 : return strcmp(a_name ? a_name : "", b_name ? b_name : "");
118 : : }
119 : :
120 : : static int
121 : 4648 : count_cmp(void *a, void *b)
122 : : {
123 : 4648 : int m = counts[*((int *) a)].calls;
124 : 4648 : int n = counts[*((int *) b)].calls;
125 : :
126 [ + + ][ + + ]: 4648 : return (m < n) ? 1 : (m > n) ? -1 : 0;
127 : : }
128 : :
129 : : static int (*sortfun)();
130 : : static struct timeval overhead = { -1, -1 };
131 : :
132 : : void
133 : 10808 : set_sortby(const char *sortby)
134 : : {
135 [ + + ]: 10808 : if (strcmp(sortby, "time") == 0)
136 : 10804 : sortfun = time_cmp;
137 [ + + ]: 4 : else if (strcmp(sortby, "calls") == 0)
138 : 2 : sortfun = count_cmp;
139 [ + - ]: 2 : else if (strcmp(sortby, "name") == 0)
140 : 2 : sortfun = syscall_cmp;
141 [ # # ]: 0 : else if (strcmp(sortby, "nothing") == 0)
142 : 0 : sortfun = NULL;
143 : : else {
144 : 0 : error_msg_and_help("invalid sortby: '%s'", sortby);
145 : : }
146 : 10808 : }
147 : :
148 : 0 : void set_overhead(int n)
149 : : {
150 : 0 : overhead.tv_sec = n / 1000000;
151 : 0 : overhead.tv_usec = n % 1000000;
152 : 0 : }
153 : :
154 : : static void
155 : 21 : call_summary_pers(FILE *outf)
156 : : {
157 : : unsigned int i;
158 : : int call_cum, error_cum;
159 : : struct timeval tv_cum, dtv;
160 : : double float_tv_cum;
161 : : double percent;
162 : 21 : const char *dashes = "----------------";
163 : : char error_str[sizeof(int)*3];
164 : : int *sorted_count;
165 : :
166 : 21 : fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
167 : : "% time", "seconds", "usecs/call",
168 : : "calls", "errors", "syscall");
169 : 21 : fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
170 : : dashes, dashes, dashes, dashes, dashes, dashes);
171 : :
172 : 21 : sorted_count = xcalloc(sizeof(int), nsyscalls);
173 : 21 : call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
174 [ + + ]: 21 : if (overhead.tv_sec == -1) {
175 : 16 : tv_mul(&overhead, &shortest, 8);
176 : 21 : tv_div(&overhead, &overhead, 10);
177 : : }
178 [ + + ]: 7918 : for (i = 0; i < nsyscalls; i++) {
179 : 7897 : sorted_count[i] = i;
180 [ + - ][ + + ]: 7897 : if (counts == NULL || counts[i].calls == 0)
181 : 7730 : continue;
182 : 167 : tv_mul(&dtv, &overhead, counts[i].calls);
183 : 167 : tv_sub(&counts[i].time, &counts[i].time, &dtv);
184 : 167 : call_cum += counts[i].calls;
185 : 167 : error_cum += counts[i].errors;
186 : 167 : tv_add(&tv_cum, &tv_cum, &counts[i].time);
187 : : }
188 : 21 : float_tv_cum = tv_float(&tv_cum);
189 [ + - ]: 21 : if (counts) {
190 [ + - ]: 21 : if (sortfun)
191 : 21 : qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
192 [ + + ]: 7918 : for (i = 0; i < nsyscalls; i++) {
193 : : double float_syscall_time;
194 : 7897 : int idx = sorted_count[i];
195 : 7897 : struct call_counts *cc = &counts[idx];
196 [ + + ]: 7897 : if (cc->calls == 0)
197 : 7730 : continue;
198 : 167 : tv_div(&dtv, &cc->time, cc->calls);
199 : 167 : error_str[0] = '\0';
200 [ + + ]: 167 : if (cc->errors)
201 : 23 : sprintf(error_str, "%u", cc->errors);
202 : 167 : float_syscall_time = tv_float(&cc->time);
203 : 167 : percent = (100.0 * float_syscall_time);
204 [ + + ]: 167 : if (percent != 0.0)
205 : 96 : percent /= float_tv_cum;
206 : : /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */
207 : 167 : fprintf(outf, "%6.2f %11.6f %11lu %9u %9.9s %s\n",
208 : : percent, float_syscall_time,
209 : 167 : (long) (1000000 * dtv.tv_sec + dtv.tv_usec),
210 : : cc->calls,
211 : 167 : error_str, sysent[idx].sys_name);
212 : : }
213 : : }
214 : 21 : free(sorted_count);
215 : :
216 : 21 : fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
217 : : dashes, dashes, dashes, dashes, dashes, dashes);
218 : 21 : error_str[0] = '\0';
219 [ + + ]: 21 : if (error_cum)
220 : 10 : sprintf(error_str, "%u", error_cum);
221 : 21 : fprintf(outf, "%6.6s %11.6f %11.11s %9u %9.9s %s\n",
222 : : "100.00", float_tv_cum, "",
223 : : call_cum, error_str, "total");
224 : 21 : }
225 : :
226 : : void
227 : 16 : call_summary(FILE *outf)
228 : : {
229 : 16 : unsigned int i, old_pers = current_personality;
230 : :
231 [ + + ]: 64 : for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
232 [ + + ]: 48 : if (!countv[i])
233 : 27 : continue;
234 : :
235 [ + + ]: 21 : if (current_personality != i)
236 : 10 : set_personality(i);
237 [ + + ]: 21 : if (i)
238 : 8 : fprintf(outf,
239 : : "System call usage summary for %d bit mode:\n",
240 : : current_wordsize * 8);
241 : 21 : call_summary_pers(outf);
242 : : }
243 : :
244 [ - + ]: 16 : if (old_pers != current_personality)
245 : 0 : set_personality(old_pers);
246 : 16 : }
|