Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 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. |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | #include "defs.h" |
| 37 | |
| 38 | struct call_counts { |
| 39 | struct timeval time; |
| 40 | int calls, errors; |
| 41 | }; |
| 42 | |
| 43 | static struct call_counts *countv[SUPPORTED_PERSONALITIES]; |
| 44 | #define counts (countv[current_personality]) |
| 45 | |
| 46 | static struct timeval shortest = { 1000000, 0 }; |
| 47 | |
Denys Vlasenko | c95a88f | 2011-08-21 17:47:40 +0200 | [diff] [blame] | 48 | void |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 49 | count_syscall(struct tcb *tcp, struct timeval *tv) |
| 50 | { |
Denys Vlasenko | cb6f056 | 2011-08-25 01:13:43 +0200 | [diff] [blame] | 51 | if (!SCNO_IN_RANGE(tcp->scno)) |
Denys Vlasenko | c95a88f | 2011-08-21 17:47:40 +0200 | [diff] [blame] | 52 | return; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 53 | |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 54 | if (!counts) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 55 | counts = calloc(nsyscalls, sizeof(*counts)); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 56 | if (!counts) |
| 57 | die_out_of_memory(); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | counts[tcp->scno].calls++; |
| 61 | if (tcp->u_error) |
| 62 | counts[tcp->scno].errors++; |
| 63 | |
| 64 | tv_sub(tv, tv, &tcp->etime); |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 65 | if (tv_cmp(tv, &tcp->dtime) > 0) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 66 | static struct timeval one_tick; |
| 67 | |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 68 | if (one_tick.tv_usec == 0) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 69 | /* Initialize it. */ |
| 70 | struct itimerval it; |
| 71 | |
| 72 | memset(&it, 0, sizeof it); |
| 73 | it.it_interval.tv_usec = 1; |
| 74 | setitimer(ITIMER_REAL, &it, NULL); |
| 75 | getitimer(ITIMER_REAL, &it); |
| 76 | one_tick = it.it_interval; |
| 77 | } |
| 78 | |
| 79 | if (tv_nz(&tcp->dtime)) |
| 80 | *tv = tcp->dtime; |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 81 | else if (tv_cmp(tv, &one_tick) > 0) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 82 | if (tv_cmp(&shortest, &one_tick) < 0) |
| 83 | *tv = shortest; |
| 84 | else |
| 85 | *tv = one_tick; |
| 86 | } |
| 87 | } |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 88 | if (tv_cmp(tv, &shortest) < 0) |
| 89 | shortest = *tv; |
| 90 | tv_add(&counts[tcp->scno].time, &counts[tcp->scno].time, tv); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static int |
| 94 | time_cmp(void *a, void *b) |
| 95 | { |
| 96 | return -tv_cmp(&counts[*((int *) a)].time, |
| 97 | &counts[*((int *) b)].time); |
| 98 | } |
| 99 | |
| 100 | static int |
| 101 | syscall_cmp(void *a, void *b) |
| 102 | { |
| 103 | return strcmp(sysent[*((int *) a)].sys_name, |
| 104 | sysent[*((int *) b)].sys_name); |
| 105 | } |
| 106 | |
| 107 | static int |
| 108 | count_cmp(void *a, void *b) |
| 109 | { |
| 110 | int m = counts[*((int *) a)].calls; |
| 111 | int n = counts[*((int *) b)].calls; |
| 112 | |
| 113 | return (m < n) ? 1 : (m > n) ? -1 : 0; |
| 114 | } |
| 115 | |
| 116 | static int (*sortfun)(); |
| 117 | static struct timeval overhead = { -1, -1 }; |
| 118 | |
| 119 | void |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 120 | set_sortby(const char *sortby) |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 121 | { |
| 122 | if (strcmp(sortby, "time") == 0) |
| 123 | sortfun = time_cmp; |
| 124 | else if (strcmp(sortby, "calls") == 0) |
| 125 | sortfun = count_cmp; |
| 126 | else if (strcmp(sortby, "name") == 0) |
| 127 | sortfun = syscall_cmp; |
| 128 | else if (strcmp(sortby, "nothing") == 0) |
| 129 | sortfun = NULL; |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 130 | else { |
Denys Vlasenko | 4c65c44 | 2012-03-08 11:54:10 +0100 | [diff] [blame] | 131 | error_msg_and_die("invalid sortby: '%s'", sortby); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | void set_overhead(int n) |
| 136 | { |
| 137 | overhead.tv_sec = n / 1000000; |
| 138 | overhead.tv_usec = n % 1000000; |
| 139 | } |
| 140 | |
| 141 | static void |
| 142 | call_summary_pers(FILE *outf) |
| 143 | { |
| 144 | int i, j; |
| 145 | int call_cum, error_cum; |
| 146 | struct timeval tv_cum, dtv; |
| 147 | double percent; |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 148 | const char *dashes = "-------------------------"; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 149 | char error_str[16]; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 150 | int *sorted_count = calloc(sizeof(int), nsyscalls); |
| 151 | |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 152 | if (!sorted_count) |
| 153 | die_out_of_memory(); |
Dmitry V. Levin | a97f03b | 2008-04-19 14:12:49 +0000 | [diff] [blame] | 154 | |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 155 | call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0; |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 156 | if (overhead.tv_sec == -1) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 157 | tv_mul(&overhead, &shortest, 8); |
| 158 | tv_div(&overhead, &overhead, 10); |
| 159 | } |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 160 | for (i = 0; i < nsyscalls; i++) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 161 | sorted_count[i] = i; |
| 162 | if (counts == NULL || counts[i].calls == 0) |
| 163 | continue; |
| 164 | tv_mul(&dtv, &overhead, counts[i].calls); |
| 165 | tv_sub(&counts[i].time, &counts[i].time, &dtv); |
| 166 | call_cum += counts[i].calls; |
| 167 | error_cum += counts[i].errors; |
| 168 | tv_add(&tv_cum, &tv_cum, &counts[i].time); |
| 169 | } |
| 170 | if (counts && sortfun) |
| 171 | qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun); |
| 172 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n", |
| 173 | "% time", "seconds", "usecs/call", |
| 174 | "calls", "errors", "syscall"); |
| 175 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n", |
| 176 | dashes, dashes, dashes, dashes, dashes, dashes); |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 177 | if (counts) { |
| 178 | for (i = 0; i < nsyscalls; i++) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 179 | j = sorted_count[i]; |
| 180 | if (counts[j].calls == 0) |
| 181 | continue; |
| 182 | tv_div(&dtv, &counts[j].time, counts[j].calls); |
| 183 | if (counts[j].errors) |
| 184 | sprintf(error_str, "%d", counts[j].errors); |
| 185 | else |
| 186 | error_str[0] = '\0'; |
| 187 | percent = (100.0 * tv_float(&counts[j].time) |
| 188 | / tv_float(&tv_cum)); |
Roland McGrath | 14e3174 | 2007-07-11 09:04:23 +0000 | [diff] [blame] | 189 | fprintf(outf, "%6.2f %11.6f %11ld %9d %9.9s %s\n", |
| 190 | percent, tv_float(&counts[j].time), |
H.J. Lu | 0b315b6 | 2012-02-03 10:16:03 -0800 | [diff] [blame] | 191 | (long) (1000000 * dtv.tv_sec + dtv.tv_usec), |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 192 | counts[j].calls, |
| 193 | error_str, sysent[j].sys_name); |
| 194 | } |
| 195 | } |
| 196 | free(sorted_count); |
| 197 | |
| 198 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n", |
| 199 | dashes, dashes, dashes, dashes, dashes, dashes); |
| 200 | if (error_cum) |
| 201 | sprintf(error_str, "%d", error_cum); |
| 202 | else |
| 203 | error_str[0] = '\0'; |
Roland McGrath | 14e3174 | 2007-07-11 09:04:23 +0000 | [diff] [blame] | 204 | fprintf(outf, "%6.6s %11.6f %11.11s %9d %9.9s %s\n", |
| 205 | "100.00", tv_float(&tv_cum), "", |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 206 | call_cum, error_str, "total"); |
| 207 | } |
| 208 | |
| 209 | void |
| 210 | call_summary(FILE *outf) |
| 211 | { |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 212 | int i, old_pers = current_personality; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 213 | |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 214 | for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 215 | if (!countv[i]) |
| 216 | continue; |
| 217 | |
| 218 | if (current_personality != i) |
| 219 | set_personality(i); |
| 220 | if (i) |
| 221 | fprintf(outf, |
Mike Frysinger | aa6d850 | 2012-04-27 18:58:20 -0400 | [diff] [blame] | 222 | "System call usage summary for %d bit mode:\n", |
Denys Vlasenko | c52826c | 2012-05-15 14:28:56 +0200 | [diff] [blame^] | 223 | current_wordsize * 8); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 224 | call_summary_pers(outf); |
| 225 | } |
| 226 | |
| 227 | if (old_pers != current_personality) |
| 228 | set_personality(old_pers); |
| 229 | } |