]> granicus.if.org Git - strace/blob - count.c
Tweaks for -c: fixed setitimer/getitimer hack; optimized call_summary_pers()
[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  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include "defs.h"
37
38 /* Per-syscall stats structure */
39 struct call_counts {
40         /* system time spent in syscall (not wall clock time) */
41         struct timeval time;
42         int calls, errors;
43 };
44
45 static struct call_counts *countv[SUPPORTED_PERSONALITIES];
46 #define counts (countv[current_personality])
47
48 static struct timeval shortest = { 1000000, 0 };
49
50 /* On entry, tv is syscall exit timestamp */
51 void
52 count_syscall(struct tcb *tcp, struct timeval *tv)
53 {
54         struct call_counts *cc;
55         unsigned long scno = tcp->scno;
56
57         if (!SCNO_IN_RANGE(scno))
58                 return;
59
60         if (!counts) {
61                 counts = calloc(nsyscalls, sizeof(*counts));
62                 if (!counts)
63                         die_out_of_memory();
64         }
65         cc = &counts[scno];
66
67         cc->calls++;
68         if (tcp->u_error)
69                 cc->errors++;
70
71         /* tv = wall clock time spent while in syscall */
72         tv_sub(tv, tv, &tcp->etime);
73
74         /* Spent more wall clock time than spent system time? (usually yes) */
75         if (tv_cmp(tv, &tcp->dtime) > 0) {
76                 static struct timeval one_tick = { -1, 0 };
77
78                 if (one_tick.tv_sec == -1) {
79                         /* Initialize it.  */
80                         struct itimerval it;
81
82                         memset(&it, 0, sizeof it);
83                         it.it_interval.tv_usec = 1;
84                         setitimer(ITIMER_REAL, &it, NULL);
85                         getitimer(ITIMER_REAL, &it);
86                         one_tick = it.it_interval;
87 //FIXME: this hack doesn't work (tested on linux-3.6.11): one_tick = 0.000000
88 //tprintf(" one_tick.tv_usec:%u\n", (unsigned)one_tick.tv_usec);
89                 }
90
91                 if (tv_nz(&tcp->dtime))
92                         /* tv = system time spent, if it isn't 0 */
93                         *tv = tcp->dtime;
94                 else if (tv_cmp(tv, &one_tick) > 0) {
95                         /* tv = smallest "sane" time interval */
96                         if (tv_cmp(&shortest, &one_tick) < 0)
97                                 *tv = shortest;
98                         else
99                                 *tv = one_tick;
100                 }
101         }
102         if (tv_cmp(tv, &shortest) < 0)
103                 shortest = *tv;
104         tv_add(&cc->time, &cc->time, tv);
105 }
106
107 static int
108 time_cmp(void *a, void *b)
109 {
110         return -tv_cmp(&counts[*((int *) a)].time,
111                        &counts[*((int *) b)].time);
112 }
113
114 static int
115 syscall_cmp(void *a, void *b)
116 {
117         return strcmp(sysent[*((int *) a)].sys_name,
118                       sysent[*((int *) b)].sys_name);
119 }
120
121 static int
122 count_cmp(void *a, void *b)
123 {
124         int     m = counts[*((int *) a)].calls;
125         int     n = counts[*((int *) b)].calls;
126
127         return (m < n) ? 1 : (m > n) ? -1 : 0;
128 }
129
130 static int (*sortfun)();
131 static struct timeval overhead = { -1, -1 };
132
133 void
134 set_sortby(const char *sortby)
135 {
136         if (strcmp(sortby, "time") == 0)
137                 sortfun = time_cmp;
138         else if (strcmp(sortby, "calls") == 0)
139                 sortfun = count_cmp;
140         else if (strcmp(sortby, "name") == 0)
141                 sortfun = syscall_cmp;
142         else if (strcmp(sortby, "nothing") == 0)
143                 sortfun = NULL;
144         else {
145                 error_msg_and_die("invalid sortby: '%s'", sortby);
146         }
147 }
148
149 void set_overhead(int n)
150 {
151         overhead.tv_sec = n / 1000000;
152         overhead.tv_usec = n % 1000000;
153 }
154
155 static void
156 call_summary_pers(FILE *outf)
157 {
158         int     i;
159         int     call_cum, error_cum;
160         struct timeval tv_cum, dtv;
161         double  float_tv_cum;
162         double  percent;
163         const char *dashes = "----------------";
164         char    error_str[sizeof(int)*3];
165         int    *sorted_count;
166
167         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
168                 "% time", "seconds", "usecs/call",
169                 "calls", "errors", "syscall");
170         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
171                 dashes, dashes, dashes, dashes, dashes, dashes);
172
173         sorted_count = calloc(sizeof(int), nsyscalls);
174         if (!sorted_count)
175                 die_out_of_memory();
176         call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
177         if (overhead.tv_sec == -1) {
178                 tv_mul(&overhead, &shortest, 8);
179                 tv_div(&overhead, &overhead, 10);
180         }
181         for (i = 0; i < nsyscalls; i++) {
182                 sorted_count[i] = i;
183                 if (counts == NULL || counts[i].calls == 0)
184                         continue;
185                 tv_mul(&dtv, &overhead, counts[i].calls);
186                 tv_sub(&counts[i].time, &counts[i].time, &dtv);
187                 call_cum += counts[i].calls;
188                 error_cum += counts[i].errors;
189                 tv_add(&tv_cum, &tv_cum, &counts[i].time);
190         }
191         float_tv_cum = tv_float(&tv_cum);
192         if (counts) {
193                 if (sortfun)
194                         qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
195                 for (i = 0; i < nsyscalls; i++) {
196                         double float_syscall_time;
197                         int idx = sorted_count[i];
198                         struct call_counts *cc = &counts[idx];
199                         if (cc->calls == 0)
200                                 continue;
201                         tv_div(&dtv, &cc->time, cc->calls);
202                         error_str[0] = '\0';
203                         if (cc->errors)
204                                 sprintf(error_str, "%u", cc->errors);
205                         float_syscall_time = tv_float(&cc->time);
206                         percent = (100.0 * float_syscall_time);
207                         if (percent != 0.0)
208                                    percent /= float_tv_cum;
209                         /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */
210                         fprintf(outf, "%6.2f %11.6f %11lu %9u %9.9s %s\n",
211                                 percent, float_syscall_time,
212                                 (long) (1000000 * dtv.tv_sec + dtv.tv_usec),
213                                 cc->calls,
214                                 error_str, sysent[idx].sys_name);
215                 }
216         }
217         free(sorted_count);
218
219         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
220                 dashes, dashes, dashes, dashes, dashes, dashes);
221         error_str[0] = '\0';
222         if (error_cum)
223                 sprintf(error_str, "%u", error_cum);
224         fprintf(outf, "%6.6s %11.6f %11.11s %9u %9.9s %s\n",
225                 "100.00", float_tv_cum, "",
226                 call_cum, error_str, "total");
227 }
228
229 void
230 call_summary(FILE *outf)
231 {
232         int i, old_pers = current_personality;
233
234         for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
235                 if (!countv[i])
236                         continue;
237
238                 if (current_personality != i)
239                         set_personality(i);
240                 if (i)
241                         fprintf(outf,
242                                 "System call usage summary for %d bit mode:\n",
243                                 current_wordsize * 8);
244                 call_summary_pers(outf);
245         }
246
247         if (old_pers != current_personality)
248                 set_personality(old_pers);
249 }