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> |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 11 | * Copyright (c) 2006-2018 The strace developers. |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 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. |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | #include "defs.h" |
| 38 | |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 39 | /* Per-syscall stats structure */ |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 40 | struct call_counts { |
Mark Hills | e53bf23 | 2014-05-28 17:52:40 +0100 | [diff] [blame] | 41 | /* time may be total latency or system time */ |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 42 | struct timespec time; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 43 | unsigned int calls, errors; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static struct call_counts *countv[SUPPORTED_PERSONALITIES]; |
| 47 | #define counts (countv[current_personality]) |
| 48 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 49 | static struct timespec overhead; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 50 | |
Denys Vlasenko | c95a88f | 2011-08-21 17:47:40 +0200 | [diff] [blame] | 51 | void |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 52 | count_syscall(struct tcb *tcp, const struct timespec *syscall_exiting_ts) |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 53 | { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 54 | if (!scno_in_range(tcp->scno)) |
Denys Vlasenko | c95a88f | 2011-08-21 17:47:40 +0200 | [diff] [blame] | 55 | return; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 56 | |
Dmitry V. Levin | 3e9d71f | 2015-05-25 20:41:02 +0000 | [diff] [blame] | 57 | if (!counts) |
| 58 | counts = xcalloc(nsyscalls, sizeof(*counts)); |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 59 | struct call_counts *cc = &counts[tcp->scno]; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 60 | |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 61 | cc->calls++; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 62 | if (syserror(tcp)) |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 63 | cc->errors++; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 64 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 65 | if (count_wallclock) { |
| 66 | /* wall clock time spent while in syscall */ |
| 67 | struct timespec wts; |
| 68 | ts_sub(&wts, syscall_exiting_ts, &tcp->etime); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 69 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 70 | ts_add(&cc->time, &cc->time, &wts); |
| 71 | } else { |
| 72 | /* system CPU time spent while in syscall */ |
| 73 | ts_add(&cc->time, &cc->time, &tcp->dtime); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 74 | } |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static int |
| 78 | time_cmp(void *a, void *b) |
| 79 | { |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 80 | return -ts_cmp(&counts[*((int *) a)].time, |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 81 | &counts[*((int *) b)].time); |
| 82 | } |
| 83 | |
| 84 | static int |
| 85 | syscall_cmp(void *a, void *b) |
| 86 | { |
Dmitry V. Levin | fa925dc | 2016-05-10 00:16:20 +0000 | [diff] [blame] | 87 | const char *a_name = sysent[*((int *) a)].sys_name; |
| 88 | const char *b_name = sysent[*((int *) b)].sys_name; |
| 89 | return strcmp(a_name ? a_name : "", b_name ? b_name : ""); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static int |
| 93 | count_cmp(void *a, void *b) |
| 94 | { |
| 95 | int m = counts[*((int *) a)].calls; |
| 96 | int n = counts[*((int *) b)].calls; |
| 97 | |
| 98 | return (m < n) ? 1 : (m > n) ? -1 : 0; |
| 99 | } |
| 100 | |
| 101 | static int (*sortfun)(); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 102 | |
| 103 | void |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 104 | set_sortby(const char *sortby) |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 105 | { |
| 106 | if (strcmp(sortby, "time") == 0) |
| 107 | sortfun = time_cmp; |
| 108 | else if (strcmp(sortby, "calls") == 0) |
| 109 | sortfun = count_cmp; |
| 110 | else if (strcmp(sortby, "name") == 0) |
| 111 | sortfun = syscall_cmp; |
| 112 | else if (strcmp(sortby, "nothing") == 0) |
| 113 | sortfun = NULL; |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 114 | else { |
Elvira Khabirova | a2fdfe9 | 2015-11-26 17:18:00 +0300 | [diff] [blame] | 115 | error_msg_and_help("invalid sortby: '%s'", sortby); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | void set_overhead(int n) |
| 120 | { |
| 121 | overhead.tv_sec = n / 1000000; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 122 | overhead.tv_nsec = n % 1000000 * 1000; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static void |
| 126 | call_summary_pers(FILE *outf) |
| 127 | { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 128 | static const char dashes[] = "----------------"; |
| 129 | static const char header[] = "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n"; |
| 130 | static const char data[] = "%6.2f %11.6f %11lu %9u %9.u %s\n"; |
| 131 | static const char summary[] = "%6.6s %11.6f %11.11s %9u %9.u %s\n"; |
| 132 | |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 133 | unsigned int i; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 134 | unsigned int call_cum, error_cum; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 135 | struct timespec tv_cum, dtv; |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 136 | double float_tv_cum; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 137 | double percent; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 138 | unsigned int *sorted_count; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 139 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 140 | fprintf(outf, header, |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 141 | "% time", "seconds", "usecs/call", |
| 142 | "calls", "errors", "syscall"); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 143 | fprintf(outf, header, dashes, dashes, dashes, dashes, dashes, dashes); |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 144 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 145 | sorted_count = xcalloc(sizeof(sorted_count[0]), nsyscalls); |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 146 | call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_nsec = 0; |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 147 | for (i = 0; i < nsyscalls; i++) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 148 | sorted_count[i] = i; |
| 149 | if (counts == NULL || counts[i].calls == 0) |
| 150 | continue; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 151 | ts_mul(&dtv, &overhead, counts[i].calls); |
| 152 | ts_sub(&counts[i].time, &counts[i].time, &dtv); |
| 153 | if (counts[i].time.tv_sec < 0 || counts[i].time.tv_nsec < 0) |
| 154 | counts[i].time.tv_sec = counts[i].time.tv_nsec = 0; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 155 | call_cum += counts[i].calls; |
| 156 | error_cum += counts[i].errors; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 157 | ts_add(&tv_cum, &tv_cum, &counts[i].time); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 158 | } |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 159 | float_tv_cum = ts_float(&tv_cum); |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 160 | if (counts) { |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 161 | if (sortfun) |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 162 | qsort((void *) sorted_count, nsyscalls, |
| 163 | sizeof(sorted_count[0]), sortfun); |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 164 | for (i = 0; i < nsyscalls; i++) { |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 165 | double float_syscall_time; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 166 | unsigned int idx = sorted_count[i]; |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 167 | struct call_counts *cc = &counts[idx]; |
| 168 | if (cc->calls == 0) |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 169 | continue; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 170 | ts_div(&dtv, &cc->time, cc->calls); |
| 171 | float_syscall_time = ts_float(&cc->time); |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 172 | percent = (100.0 * float_syscall_time); |
| 173 | if (percent != 0.0) |
| 174 | percent /= float_tv_cum; |
| 175 | /* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 176 | fprintf(outf, data, |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 177 | percent, float_syscall_time, |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 178 | (long) (1000000 * dtv.tv_sec + dtv.tv_nsec / 1000), |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 179 | cc->calls, cc->errors, sysent[idx].sys_name); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | free(sorted_count); |
| 183 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 184 | fprintf(outf, header, dashes, dashes, dashes, dashes, dashes, dashes); |
| 185 | fprintf(outf, summary, |
Denys Vlasenko | 8050cdc | 2013-03-07 12:27:40 +0100 | [diff] [blame] | 186 | "100.00", float_tv_cum, "", |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 187 | call_cum, error_cum, "total"); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void |
| 191 | call_summary(FILE *outf) |
| 192 | { |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 193 | unsigned int i, old_pers = current_personality; |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 194 | |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 195 | for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) { |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 196 | if (!countv[i]) |
| 197 | continue; |
| 198 | |
| 199 | if (current_personality != i) |
| 200 | set_personality(i); |
| 201 | if (i) |
| 202 | fprintf(outf, |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 203 | "System call usage summary for %s mode:\n", |
| 204 | personality_names[i]); |
Dmitry V. Levin | 7d61ff1 | 2006-12-21 21:15:04 +0000 | [diff] [blame] | 205 | call_summary_pers(outf); |
| 206 | } |
| 207 | |
| 208 | if (old_pers != current_personality) |
| 209 | set_personality(old_pers); |
| 210 | } |