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 | |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 38 | /* Per-syscall stats structure */ |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 39 | struct call_counts { |
Mark Hills | e53bf23 | 2014-05-28 17:52:40 +0100 | [diff] [blame] | 40 | /* time may be total latency or system time */ |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 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 | |
Denys Vlasenko | c95a88f | 2011-08-21 17:47:40 +0200 | [diff] [blame] | 50 | void |
Dmitry V. Levin | ac5133d | 2014-05-29 18:10:00 +0000 | [diff] [blame] | 51 | count_syscall(struct tcb *tcp, const struct timeval *syscall_exiting_tv) |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 52 | { |
Dmitry V. Levin | ac5133d | 2014-05-29 18:10:00 +0000 | [diff] [blame] | 53 | struct timeval wtv; |
| 54 | struct timeval *tv = &wtv; |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 55 | struct call_counts *cc; |
Denys Vlasenko | 74ec14f | 2013-02-21 16:13:47 +0100 | [diff] [blame] | 56 | unsigned long scno = tcp->scno; |
| 57 | |
| 58 | if (!SCNO_IN_RANGE(scno)) |
Denys Vlasenko | c95a88f | 2011-08-21 17:47:40 +0200 | [diff] [blame] | 59 | return; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 60 | |
Dmitry V. Levin | 3e9d71f | 2015-05-25 20:41:02 +0000 | [diff] [blame] | 61 | if (!counts) |
| 62 | counts = xcalloc(nsyscalls, sizeof(*counts)); |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 63 | cc = &counts[scno]; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 64 | |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 65 | cc->calls++; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 66 | if (tcp->u_error) |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 67 | cc->errors++; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 68 | |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 69 | /* tv = wall clock time spent while in syscall */ |
Dmitry V. Levin | ac5133d | 2014-05-29 18:10:00 +0000 | [diff] [blame] | 70 | tv_sub(tv, syscall_exiting_tv, &tcp->etime); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 71 | |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 72 | /* Spent more wall clock time than spent system time? (usually yes) */ |
| 73 | if (tv_cmp(tv, &tcp->dtime) > 0) { |
| 74 | static struct timeval one_tick = { -1, 0 }; |
| 75 | |
| 76 | if (one_tick.tv_sec == -1) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 77 | /* Initialize it. */ |
| 78 | struct itimerval it; |
| 79 | |
| 80 | memset(&it, 0, sizeof it); |
| 81 | it.it_interval.tv_usec = 1; |
| 82 | setitimer(ITIMER_REAL, &it, NULL); |
| 83 | getitimer(ITIMER_REAL, &it); |
| 84 | one_tick = it.it_interval; |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 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); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (tv_nz(&tcp->dtime)) |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 90 | /* tv = system time spent, if it isn't 0 */ |
Dmitry V. Levin | ac5133d | 2014-05-29 18:10:00 +0000 | [diff] [blame] | 91 | tv = &tcp->dtime; |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 92 | else if (tv_cmp(tv, &one_tick) > 0) { |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 93 | /* tv = smallest "sane" time interval */ |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 94 | if (tv_cmp(&shortest, &one_tick) < 0) |
Dmitry V. Levin | ac5133d | 2014-05-29 18:10:00 +0000 | [diff] [blame] | 95 | tv = &shortest; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 96 | else |
Dmitry V. Levin | ac5133d | 2014-05-29 18:10:00 +0000 | [diff] [blame] | 97 | tv = &one_tick; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 100 | if (tv_cmp(tv, &shortest) < 0) |
| 101 | shortest = *tv; |
Mark Hills | e53bf23 | 2014-05-28 17:52:40 +0100 | [diff] [blame] | 102 | tv_add(&cc->time, &cc->time, count_wallclock ? &wtv : tv); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static int |
| 106 | time_cmp(void *a, void *b) |
| 107 | { |
| 108 | return -tv_cmp(&counts[*((int *) a)].time, |
| 109 | &counts[*((int *) b)].time); |
| 110 | } |
| 111 | |
| 112 | static int |
| 113 | syscall_cmp(void *a, void *b) |
| 114 | { |
Dmitry V. Levin | fa925dc | 2016-05-10 00:16:20 +0000 | [diff] [blame] | 115 | const char *a_name = sysent[*((int *) a)].sys_name; |
| 116 | const char *b_name = sysent[*((int *) b)].sys_name; |
| 117 | return strcmp(a_name ? a_name : "", b_name ? b_name : ""); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static int |
| 121 | count_cmp(void *a, void *b) |
| 122 | { |
| 123 | int m = counts[*((int *) a)].calls; |
| 124 | int n = counts[*((int *) b)].calls; |
| 125 | |
| 126 | return (m < n) ? 1 : (m > n) ? -1 : 0; |
| 127 | } |
| 128 | |
| 129 | static int (*sortfun)(); |
| 130 | static struct timeval overhead = { -1, -1 }; |
| 131 | |
| 132 | void |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 133 | set_sortby(const char *sortby) |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 134 | { |
| 135 | if (strcmp(sortby, "time") == 0) |
| 136 | sortfun = time_cmp; |
| 137 | else if (strcmp(sortby, "calls") == 0) |
| 138 | sortfun = count_cmp; |
| 139 | else if (strcmp(sortby, "name") == 0) |
| 140 | sortfun = syscall_cmp; |
| 141 | else if (strcmp(sortby, "nothing") == 0) |
| 142 | sortfun = NULL; |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 143 | else { |
Elvira Khabirova | a2fdfe9 | 2015-11-26 17:18:00 +0300 | [diff] [blame] | 144 | error_msg_and_help("invalid sortby: '%s'", sortby); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | void set_overhead(int n) |
| 149 | { |
| 150 | overhead.tv_sec = n / 1000000; |
| 151 | overhead.tv_usec = n % 1000000; |
| 152 | } |
| 153 | |
| 154 | static void |
| 155 | call_summary_pers(FILE *outf) |
| 156 | { |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 157 | unsigned int i; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 158 | int call_cum, error_cum; |
| 159 | struct timeval tv_cum, dtv; |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 160 | double float_tv_cum; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 161 | double percent; |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 162 | const char *dashes = "----------------"; |
| 163 | char error_str[sizeof(int)*3]; |
| 164 | int *sorted_count; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 165 | |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 166 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n", |
| 167 | "% time", "seconds", "usecs/call", |
| 168 | "calls", "errors", "syscall"); |
| 169 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n", |
| 170 | dashes, dashes, dashes, dashes, dashes, dashes); |
| 171 | |
Dmitry V. Levin | 3e9d71f | 2015-05-25 20:41:02 +0000 | [diff] [blame] | 172 | sorted_count = xcalloc(sizeof(int), nsyscalls); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 173 | 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] | 174 | if (overhead.tv_sec == -1) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 175 | tv_mul(&overhead, &shortest, 8); |
| 176 | tv_div(&overhead, &overhead, 10); |
| 177 | } |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 178 | for (i = 0; i < nsyscalls; i++) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 179 | sorted_count[i] = i; |
| 180 | if (counts == NULL || counts[i].calls == 0) |
| 181 | continue; |
| 182 | tv_mul(&dtv, &overhead, counts[i].calls); |
| 183 | tv_sub(&counts[i].time, &counts[i].time, &dtv); |
| 184 | call_cum += counts[i].calls; |
| 185 | error_cum += counts[i].errors; |
| 186 | tv_add(&tv_cum, &tv_cum, &counts[i].time); |
| 187 | } |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 188 | float_tv_cum = tv_float(&tv_cum); |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 189 | if (counts) { |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 190 | if (sortfun) |
| 191 | qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun); |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 192 | for (i = 0; i < nsyscalls; i++) { |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 193 | double float_syscall_time; |
| 194 | int idx = sorted_count[i]; |
| 195 | struct call_counts *cc = &counts[idx]; |
| 196 | if (cc->calls == 0) |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 197 | continue; |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 198 | tv_div(&dtv, &cc->time, cc->calls); |
| 199 | error_str[0] = '\0'; |
| 200 | if (cc->errors) |
| 201 | sprintf(error_str, "%u", cc->errors); |
| 202 | float_syscall_time = tv_float(&cc->time); |
| 203 | percent = (100.0 * float_syscall_time); |
| 204 | if (percent != 0.0) |
| 205 | percent /= float_tv_cum; |
| 206 | /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */ |
| 207 | fprintf(outf, "%6.2f %11.6f %11lu %9u %9.9s %s\n", |
| 208 | percent, float_syscall_time, |
H.J. Lu | 0b315b6 | 2012-02-03 10:16:03 -0800 | [diff] [blame] | 209 | (long) (1000000 * dtv.tv_sec + dtv.tv_usec), |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 210 | cc->calls, |
| 211 | error_str, sysent[idx].sys_name); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | free(sorted_count); |
| 215 | |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 216 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n", |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 217 | dashes, dashes, dashes, dashes, dashes, dashes); |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 218 | error_str[0] = '\0'; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 219 | if (error_cum) |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 220 | sprintf(error_str, "%u", error_cum); |
| 221 | fprintf(outf, "%6.6s %11.6f %11.11s %9u %9.9s %s\n", |
| 222 | "100.00", float_tv_cum, "", |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 223 | call_cum, error_str, "total"); |
| 224 | } |
| 225 | |
| 226 | void |
| 227 | call_summary(FILE *outf) |
| 228 | { |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 229 | unsigned int i, old_pers = current_personality; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 230 | |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 231 | for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 232 | if (!countv[i]) |
| 233 | continue; |
| 234 | |
| 235 | if (current_personality != i) |
| 236 | set_personality(i); |
| 237 | if (i) |
| 238 | fprintf(outf, |
Mike Frysinger | aa6d850 | 2012-04-27 18:58:20 -0400 | [diff] [blame] | 239 | "System call usage summary for %d bit mode:\n", |
Denys Vlasenko | c52826c | 2012-05-15 14:28:56 +0200 | [diff] [blame] | 240 | current_wordsize * 8); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 241 | call_summary_pers(outf); |
| 242 | } |
| 243 | |
| 244 | if (old_pers != current_personality) |
| 245 | set_personality(old_pers); |
| 246 | } |