forked from strace/strace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.c
524 lines (442 loc) · 12.8 KB
/
count.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
* Copyright (c) 1991, 1992 Paul Kranenburg <[email protected]>
* Copyright (c) 1993 Branko Lankester <[email protected]>
* Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <[email protected]>
* Copyright (c) 1996-1999 Wichert Akkerman <[email protected]>
* Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
* Linux for s390 port by D.J. Barrow
* Copyright (c) 2004 Roland McGrath <[email protected]>
* Copyright (c) 2006 Dmitry V. Levin <[email protected]>
* Copyright (c) 2006-2020 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "defs.h"
#include <stdarg.h>
/* Per-syscall stats structure */
struct call_counts {
/* time may be total latency or system time */
struct timespec time;
struct timespec time_min;
struct timespec time_max;
struct timespec time_avg;
uint64_t calls, errors;
};
static struct call_counts *countv[SUPPORTED_PERSONALITIES];
#define counts (countv[current_personality])
static const struct timespec zero_ts;
static const struct timespec max_ts = {
(time_t) (long long) (zero_extend_signed_to_ull((time_t) -1ULL) >> 1),
999999999 };
static struct timespec overhead;
enum count_summary_columns {
CSC_NONE,
CSC_TIME_100S,
CSC_TIME_TOTAL,
CSC_TIME_MIN,
CSC_TIME_MAX,
CSC_TIME_AVG,
CSC_CALLS,
CSC_ERRORS,
CSC_SC_NAME,
CSC_MAX,
};
static uint8_t columns[CSC_MAX] = {
CSC_TIME_100S,
CSC_TIME_TOTAL,
CSC_TIME_AVG,
CSC_CALLS,
CSC_ERRORS,
CSC_SC_NAME,
};
static const struct {
const char *name;
uint8_t column;
} column_aliases[] = {
{ "time", CSC_TIME_100S },
{ "time_percent", CSC_TIME_100S },
{ "time-percent", CSC_TIME_100S },
{ "time_total", CSC_TIME_TOTAL },
{ "time-total", CSC_TIME_TOTAL },
{ "total_time", CSC_TIME_TOTAL },
{ "total-time", CSC_TIME_TOTAL },
{ "min_time", CSC_TIME_MIN },
{ "min-time", CSC_TIME_MIN },
{ "shortest", CSC_TIME_MIN },
{ "time_min", CSC_TIME_MIN },
{ "time-min", CSC_TIME_MIN },
{ "longest" , CSC_TIME_MAX },
{ "max_time", CSC_TIME_MAX },
{ "max-time", CSC_TIME_MAX },
{ "time_max", CSC_TIME_MAX },
{ "time-max", CSC_TIME_MAX },
{ "avg_time", CSC_TIME_AVG },
{ "avg-time", CSC_TIME_AVG },
{ "time_avg", CSC_TIME_AVG },
{ "time-avg", CSC_TIME_AVG },
{ "calls", CSC_CALLS },
{ "count", CSC_CALLS },
{ "error", CSC_ERRORS },
{ "errors", CSC_ERRORS },
{ "name", CSC_SC_NAME },
{ "syscall", CSC_SC_NAME },
{ "syscall_name", CSC_SC_NAME },
{ "syscall-name", CSC_SC_NAME },
{ "none", CSC_NONE },
{ "nothing", CSC_NONE },
};
void
count_syscall(struct tcb *tcp, const struct timespec *syscall_exiting_ts)
{
if (!scno_in_range(tcp->scno))
return;
if (!counts) {
counts = xcalloc(nsyscalls, sizeof(*counts));
for (size_t i = 0; i < nsyscalls; i++)
counts[i].time_min = max_ts;
}
struct call_counts *cc = &counts[tcp->scno];
cc->calls++;
if (syserror(tcp))
cc->errors++;
struct timespec wts;
if (count_wallclock) {
/* wall clock time spent while in syscall */
ts_sub(&wts, syscall_exiting_ts, &tcp->etime);
} else {
/* system CPU time spent while in syscall */
ts_sub(&wts, &tcp->stime, &tcp->ltime);
}
ts_sub(&wts, &wts, &overhead);
const struct timespec *wts_nonneg = ts_max(&wts, &zero_ts);
ts_add(&cc->time, &cc->time, wts_nonneg);
cc->time_min = *ts_min(&cc->time_min, wts_nonneg);
cc->time_max = *ts_max(&cc->time_max, wts_nonneg);
}
static int
time_cmp(const void *a, const void *b)
{
const unsigned int *a_int = a;
const unsigned int *b_int = b;
return -ts_cmp(&counts[*a_int].time, &counts[*b_int].time);
}
static int
min_time_cmp(const void *a, const void *b)
{
return -ts_cmp(&counts[*((unsigned int *) a)].time_min,
&counts[*((unsigned int *) b)].time_min);
}
static int
max_time_cmp(const void *a, const void *b)
{
return -ts_cmp(&counts[*((unsigned int *) a)].time_max,
&counts[*((unsigned int *) b)].time_max);
}
static int
avg_time_cmp(const void *a, const void *b)
{
return -ts_cmp(&counts[*((unsigned int *) a)].time_avg,
&counts[*((unsigned int *) b)].time_avg);
}
static int
syscall_cmp(const void *a, const void *b)
{
const unsigned int *a_int = a;
const unsigned int *b_int = b;
const char *a_name = sysent[*a_int].sys_name;
const char *b_name = sysent[*b_int].sys_name;
return strcmp(a_name ? a_name : "", b_name ? b_name : "");
}
static int
count_cmp(const void *a, const void *b)
{
const unsigned int *a_int = a;
const unsigned int *b_int = b;
unsigned int m = counts[*a_int].calls;
unsigned int n = counts[*b_int].calls;
return (m < n) ? 1 : (m > n) ? -1 : 0;
}
static int
error_cmp(const void *a, const void *b)
{
const unsigned int *a_int = a;
const unsigned int *b_int = b;
unsigned int m = counts[*a_int].errors;
unsigned int n = counts[*b_int].errors;
return (m < n) ? 1 : (m > n) ? -1 : 0;
}
typedef int (*sort_func)(const void *, const void *);
static sort_func sortfun;
void
set_sortby(const char *sortby)
{
static const sort_func sort_fns[CSC_MAX] = {
[CSC_TIME_100S] = time_cmp,
[CSC_TIME_TOTAL] = time_cmp,
[CSC_TIME_MIN] = min_time_cmp,
[CSC_TIME_MAX] = max_time_cmp,
[CSC_TIME_AVG] = avg_time_cmp,
[CSC_CALLS] = count_cmp,
[CSC_ERRORS] = error_cmp,
[CSC_SC_NAME] = syscall_cmp,
};
for (size_t i = 0; i < ARRAY_SIZE(column_aliases); ++i) {
if (!strcmp(column_aliases[i].name, sortby)) {
sortfun = sort_fns[column_aliases[i].column];
return;
}
}
error_msg_and_help("invalid sortby: '%s'", sortby);
}
void
set_count_summary_columns(const char *s)
{
uint8_t visible[CSC_MAX] = { 0 };
const char *prev = s;
size_t cur = 0;
memset(columns, 0, sizeof(columns));
for (;;) {
bool found = false;
const char *pos = strchr(prev, ',');
size_t len = pos ? (size_t) (pos - prev) : strlen(prev);
for (size_t i = 0; i < ARRAY_SIZE(column_aliases); i++) {
if (strncmp(column_aliases[i].name, prev, len) ||
column_aliases[i].name[len])
continue;
if (column_aliases[i].column == CSC_NONE ||
column_aliases[i].column >= CSC_MAX)
continue;
if (visible[column_aliases[i].column])
error_msg_and_help("call summary column "
"has been provided more "
"than once: '%s' (-U option "
"residual: '%s')",
column_aliases[i].name,
prev);
columns[cur++] = column_aliases[i].column;
visible[column_aliases[i].column] = 1;
found = true;
break;
}
if (!found)
error_msg_and_help("unknown column name: '%.*s'",
(int) MIN(len, INT_MAX), prev);
if (!pos)
break;
prev = pos + 1;
}
/*
* Always enable syscall name column, as without it table is meaningless
*/
if (!visible[CSC_SC_NAME])
columns[cur++] = CSC_SC_NAME;
}
int
set_overhead(const char *str)
{
return parse_ts(str, &overhead);
}
static size_t ATTRIBUTE_FORMAT((printf, 1, 2))
num_chars(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
return (unsigned int) MAX(ret, 0);
}
static void
call_summary_pers(FILE *outf)
{
unsigned int *indices;
size_t last_column = 0;
struct timespec tv_cum = zero_ts;
const struct timespec *tv_min = &max_ts;
const struct timespec *tv_min_max = &zero_ts;
const struct timespec *tv_max = &zero_ts;
const struct timespec *tv_avg_max = &zero_ts;
uint64_t call_cum = 0;
uint64_t error_cum = 0;
double float_tv_cum;
double percent;
size_t sc_name_max = 0;
/* sort, calculate statistics */
indices = xcalloc(sizeof(indices[0]), nsyscalls);
for (size_t i = 0; i < nsyscalls; ++i) {
indices[i] = i;
if (counts[i].calls == 0)
continue;
ts_add(&tv_cum, &tv_cum, &counts[i].time);
tv_min = ts_min(tv_min, &counts[i].time_min);
tv_min_max = ts_max(tv_min_max, &counts[i].time_min);
tv_max = ts_max(tv_max, &counts[i].time_max);
call_cum += counts[i].calls;
error_cum += counts[i].errors;
ts_div(&counts[i].time_avg, &counts[i].time, counts[i].calls);
tv_avg_max = ts_max(tv_avg_max, &counts[i].time_avg);
sc_name_max = MAX(sc_name_max, strlen(sysent[i].sys_name));
}
float_tv_cum = ts_float(&tv_cum);
if (sortfun)
qsort((void *) indices, nsyscalls, sizeof(indices[0]), sortfun);
enum column_flags {
CF_L = 1 << 0, /* Left-aligned column */
};
static const struct {
const char *s;
size_t sz;
const char *fmt;
const char *last_fmt;
uint32_t flags;
} cdesc[] = {
[CSC_TIME_100S] = { ARRSZ_PAIR("% time") - 1, "%1$*2$.2f" },
[CSC_TIME_MIN] = { ARRSZ_PAIR("shortest") - 1, "%1$*2$.6f" },
[CSC_TIME_MAX] = { ARRSZ_PAIR("longest") - 1, "%1$*2$.6f" },
/* Historical field sizes are preserved */
[CSC_TIME_TOTAL] = { "seconds", 11, "%1$*2$.6f" },
[CSC_TIME_AVG] = { "usecs/call", 11, "%1$*2$" PRIu64 },
[CSC_CALLS] = { "calls", 9, "%1$*2$" PRIu64 },
[CSC_ERRORS] = { "errors", 9, "%1$*2$.0" PRIu64 },
[CSC_SC_NAME] = { "syscall", 16, "%1$-*2$s", "%1$s", CF_L },
};
/* calculate column widths */
#define W_(c_, v_) [c_] = MAX(cdesc[c_].sz, (v_))
unsigned int cwidths[CSC_MAX] = {
W_(CSC_TIME_100S, sizeof("100.00") - 1),
W_(CSC_TIME_TOTAL, num_chars("%.6f", float_tv_cum)),
W_(CSC_TIME_MIN, num_chars("%" PRId64 ".000000",
(int64_t) tv_min_max->tv_sec)),
W_(CSC_TIME_MAX, num_chars("%" PRId64 ".000000",
(int64_t) tv_max->tv_sec)),
W_(CSC_TIME_AVG, num_chars("%" PRId64 ,
(uint64_t) (ts_float(tv_avg_max)
* 1e6))),
W_(CSC_CALLS, num_chars("%" PRIu64, call_cum)),
W_(CSC_ERRORS, num_chars("%" PRIu64, error_cum)),
W_(CSC_SC_NAME, sc_name_max + 1),
};
#undef W_
/* find the last column */
for (size_t i = 0; i < ARRAY_SIZE(columns) && columns[i]; ++i)
last_column = i;
/* header */
for (size_t i = 0; i <= last_column; ++i) {
const char *fmt = cdesc[columns[i]].flags & CF_L
? (i == last_column ? "%1$s" : "%1$-*2$s")
: "%1$*2$s";
if (i)
fputc(' ', outf);
fprintf(outf, fmt, cdesc[columns[i]].s, cwidths[columns[i]]);
}
fputc('\n', outf);
/* divider */
for (size_t i = 0; i <= last_column; ++i) {
if (i)
fputc(' ', outf);
for (size_t j = 0; j < cwidths[columns[i]]; ++j)
fputc('-', outf);
}
fputc('\n', outf);
/* cache column formats */
#define FC_(c_) \
case (c_): \
column_fmts[i] = (i == last_column) && cdesc[c].last_fmt \
? cdesc[c].last_fmt : cdesc[c].fmt; \
break
#define PC_(c_, val_) \
case (c_): \
fprintf(outf, column_fmts[i], (val_), cwidths[c]); \
break
const char *column_fmts[last_column + 1];
for (size_t i = 0; i <= last_column; ++i) {
const size_t c = columns[i];
switch (c) {
FC_(CSC_TIME_100S);
FC_(CSC_TIME_TOTAL);
FC_(CSC_TIME_MIN);
FC_(CSC_TIME_MAX);
FC_(CSC_TIME_AVG);
FC_(CSC_CALLS);
FC_(CSC_ERRORS);
FC_(CSC_SC_NAME);
}
}
/* data output */
for (size_t j = 0; j < nsyscalls; ++j) {
unsigned int idx = indices[j];
struct call_counts *cc = &counts[idx];
double float_syscall_time;
if (cc->calls == 0)
continue;
float_syscall_time = ts_float(&cc->time);
percent = (100.0 * float_syscall_time);
/* else: float_tv_cum can be 0.0 too and we get 0/0 = NAN */
if (percent != 0.0)
percent /= float_tv_cum;
for (size_t i = 0; i <= last_column; ++i) {
const size_t c = columns[i];
if (i)
fputc(' ', outf);
switch (c) {
PC_(CSC_TIME_100S, percent);
PC_(CSC_TIME_TOTAL, float_syscall_time);
PC_(CSC_TIME_MIN, ts_float(&cc->time_min));
PC_(CSC_TIME_MAX, ts_float(&cc->time_max));
PC_(CSC_TIME_AVG,
(uint64_t) (ts_float(&cc->time_avg) * 1e6));
PC_(CSC_CALLS, cc->calls);
PC_(CSC_ERRORS, cc->errors);
PC_(CSC_SC_NAME, sysent[idx].sys_name);
}
}
fputc('\n', outf);
}
free(indices);
/* footer */
for (size_t i = 0; i <= last_column; ++i) {
if (i)
fputc(' ', outf);
for (size_t j = 0; j < cwidths[columns[i]]; ++j)
fputc('-', outf);
}
fputc('\n', outf);
/* totals */
for (size_t i = 0; i <= last_column; ++i) {
const size_t c = columns[i];
if (i)
fputc(' ', outf);
switch (c) {
PC_(CSC_TIME_100S, 100.0);
PC_(CSC_TIME_TOTAL, float_tv_cum);
PC_(CSC_TIME_MIN, ts_float(tv_min));
PC_(CSC_TIME_MAX, ts_float(tv_max));
PC_(CSC_TIME_AVG, (uint64_t) (float_tv_cum / call_cum * 1e6));
PC_(CSC_CALLS, call_cum);
PC_(CSC_ERRORS, error_cum);
PC_(CSC_SC_NAME, "total");
}
}
fputc('\n', outf);
#undef PC_
#undef FC_
}
void
call_summary(FILE *outf)
{
unsigned int i, old_pers = current_personality;
for (i = 0; i < SUPPORTED_PERSONALITIES; ++i) {
if (!countv[i])
continue;
if (current_personality != i)
set_personality(i);
if (i)
fprintf(outf,
"System call usage summary for %s mode:\n",
personality_names[i]);
call_summary_pers(outf);
}
if (old_pers != current_personality)
set_personality(old_pers);
}