Commit | Line | Data |
---|---|---|
4e319027 | 1 | #include <traceevent/event-parse.h> |
514f1c67 | 2 | #include "builtin.h" |
752fde44 | 3 | #include "util/color.h" |
514f1c67 | 4 | #include "util/evlist.h" |
752fde44 ACM |
5 | #include "util/machine.h" |
6 | #include "util/thread.h" | |
514f1c67 ACM |
7 | #include "util/parse-options.h" |
8 | #include "util/thread_map.h" | |
514f1c67 ACM |
9 | |
10 | #include <libaudit.h> | |
11 | #include <stdlib.h> | |
12 | ||
13 | static struct syscall_fmt { | |
14 | const char *name; | |
aec1930b | 15 | const char *alias; |
514f1c67 ACM |
16 | bool errmsg; |
17 | bool timeout; | |
18 | } syscall_fmts[] = { | |
8b745263 | 19 | { .name = "access", .errmsg = true, }, |
aec1930b | 20 | { .name = "arch_prctl", .errmsg = true, .alias = "prctl", }, |
a14bb860 | 21 | { .name = "connect", .errmsg = true, }, |
aec1930b ACM |
22 | { .name = "fstat", .errmsg = true, .alias = "newfstat", }, |
23 | { .name = "fstatat", .errmsg = true, .alias = "newfstatat", }, | |
24 | { .name = "futex", .errmsg = true, }, | |
8b745263 | 25 | { .name = "open", .errmsg = true, }, |
aec1930b ACM |
26 | { .name = "poll", .errmsg = true, .timeout = true, }, |
27 | { .name = "ppoll", .errmsg = true, .timeout = true, }, | |
28 | { .name = "read", .errmsg = true, }, | |
29 | { .name = "recvfrom", .errmsg = true, }, | |
30 | { .name = "select", .errmsg = true, .timeout = true, }, | |
8b745263 | 31 | { .name = "socket", .errmsg = true, }, |
aec1930b | 32 | { .name = "stat", .errmsg = true, .alias = "newstat", }, |
514f1c67 ACM |
33 | }; |
34 | ||
35 | static int syscall_fmt__cmp(const void *name, const void *fmtp) | |
36 | { | |
37 | const struct syscall_fmt *fmt = fmtp; | |
38 | return strcmp(name, fmt->name); | |
39 | } | |
40 | ||
41 | static struct syscall_fmt *syscall_fmt__find(const char *name) | |
42 | { | |
43 | const int nmemb = ARRAY_SIZE(syscall_fmts); | |
44 | return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp); | |
45 | } | |
46 | ||
47 | struct syscall { | |
48 | struct event_format *tp_format; | |
49 | const char *name; | |
50 | struct syscall_fmt *fmt; | |
51 | }; | |
52 | ||
60c907ab ACM |
53 | static size_t fprintf_duration(unsigned long t, FILE *fp) |
54 | { | |
55 | double duration = (double)t / NSEC_PER_MSEC; | |
56 | size_t printed = fprintf(fp, "("); | |
57 | ||
58 | if (duration >= 1.0) | |
59 | printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration); | |
60 | else if (duration >= 0.01) | |
61 | printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration); | |
62 | else | |
63 | printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration); | |
64 | return printed + fprintf(stdout, "): "); | |
65 | } | |
66 | ||
752fde44 ACM |
67 | struct thread_trace { |
68 | u64 entry_time; | |
69 | u64 exit_time; | |
70 | bool entry_pending; | |
efd5745e | 71 | unsigned long nr_events; |
752fde44 | 72 | char *entry_str; |
1302d88e | 73 | double runtime_ms; |
752fde44 ACM |
74 | }; |
75 | ||
76 | static struct thread_trace *thread_trace__new(void) | |
77 | { | |
78 | return zalloc(sizeof(struct thread_trace)); | |
79 | } | |
80 | ||
81 | static struct thread_trace *thread__trace(struct thread *thread) | |
82 | { | |
efd5745e ACM |
83 | struct thread_trace *ttrace; |
84 | ||
752fde44 ACM |
85 | if (thread == NULL) |
86 | goto fail; | |
87 | ||
88 | if (thread->priv == NULL) | |
89 | thread->priv = thread_trace__new(); | |
efd5745e | 90 | |
752fde44 ACM |
91 | if (thread->priv == NULL) |
92 | goto fail; | |
93 | ||
efd5745e ACM |
94 | ttrace = thread->priv; |
95 | ++ttrace->nr_events; | |
96 | ||
97 | return ttrace; | |
752fde44 ACM |
98 | fail: |
99 | color_fprintf(stdout, PERF_COLOR_RED, | |
100 | "WARNING: not enough memory, dropping samples!\n"); | |
101 | return NULL; | |
102 | } | |
103 | ||
514f1c67 ACM |
104 | struct trace { |
105 | int audit_machine; | |
106 | struct { | |
107 | int max; | |
108 | struct syscall *table; | |
109 | } syscalls; | |
110 | struct perf_record_opts opts; | |
752fde44 ACM |
111 | struct machine host; |
112 | u64 base_time; | |
efd5745e | 113 | unsigned long nr_events; |
1302d88e | 114 | bool sched; |
752fde44 | 115 | bool multiple_threads; |
ae9ed035 | 116 | double duration_filter; |
1302d88e | 117 | double runtime_ms; |
514f1c67 ACM |
118 | }; |
119 | ||
ae9ed035 ACM |
120 | static bool trace__filter_duration(struct trace *trace, double t) |
121 | { | |
122 | return t < (trace->duration_filter * NSEC_PER_MSEC); | |
123 | } | |
124 | ||
752fde44 ACM |
125 | static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) |
126 | { | |
127 | double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC; | |
128 | ||
60c907ab | 129 | return fprintf(fp, "%10.3f ", ts); |
752fde44 ACM |
130 | } |
131 | ||
f15eb531 NK |
132 | static bool done = false; |
133 | ||
134 | static void sig_handler(int sig __maybe_unused) | |
135 | { | |
136 | done = true; | |
137 | } | |
138 | ||
752fde44 | 139 | static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread, |
60c907ab | 140 | u64 duration, u64 tstamp, FILE *fp) |
752fde44 ACM |
141 | { |
142 | size_t printed = trace__fprintf_tstamp(trace, tstamp, fp); | |
60c907ab | 143 | printed += fprintf_duration(duration, fp); |
752fde44 ACM |
144 | |
145 | if (trace->multiple_threads) | |
38051234 | 146 | printed += fprintf(fp, "%d ", thread->tid); |
752fde44 ACM |
147 | |
148 | return printed; | |
149 | } | |
150 | ||
151 | static int trace__process_event(struct machine *machine, union perf_event *event) | |
152 | { | |
153 | int ret = 0; | |
154 | ||
155 | switch (event->header.type) { | |
156 | case PERF_RECORD_LOST: | |
157 | color_fprintf(stdout, PERF_COLOR_RED, | |
158 | "LOST %" PRIu64 " events!\n", event->lost.lost); | |
159 | ret = machine__process_lost_event(machine, event); | |
160 | default: | |
161 | ret = machine__process_event(machine, event); | |
162 | break; | |
163 | } | |
164 | ||
165 | return ret; | |
166 | } | |
167 | ||
168 | static int trace__tool_process(struct perf_tool *tool __maybe_unused, | |
169 | union perf_event *event, | |
170 | struct perf_sample *sample __maybe_unused, | |
171 | struct machine *machine) | |
172 | { | |
173 | return trace__process_event(machine, event); | |
174 | } | |
175 | ||
176 | static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist) | |
177 | { | |
178 | int err = symbol__init(); | |
179 | ||
180 | if (err) | |
181 | return err; | |
182 | ||
183 | machine__init(&trace->host, "", HOST_KERNEL_ID); | |
184 | machine__create_kernel_maps(&trace->host); | |
185 | ||
186 | if (perf_target__has_task(&trace->opts.target)) { | |
187 | err = perf_event__synthesize_thread_map(NULL, evlist->threads, | |
188 | trace__tool_process, | |
189 | &trace->host); | |
190 | } else { | |
191 | err = perf_event__synthesize_threads(NULL, trace__tool_process, | |
192 | &trace->host); | |
193 | } | |
194 | ||
195 | if (err) | |
196 | symbol__exit(); | |
197 | ||
198 | return err; | |
199 | } | |
200 | ||
514f1c67 ACM |
201 | static int trace__read_syscall_info(struct trace *trace, int id) |
202 | { | |
203 | char tp_name[128]; | |
204 | struct syscall *sc; | |
3a531260 ACM |
205 | const char *name = audit_syscall_to_name(id, trace->audit_machine); |
206 | ||
207 | if (name == NULL) | |
208 | return -1; | |
514f1c67 ACM |
209 | |
210 | if (id > trace->syscalls.max) { | |
211 | struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc)); | |
212 | ||
213 | if (nsyscalls == NULL) | |
214 | return -1; | |
215 | ||
216 | if (trace->syscalls.max != -1) { | |
217 | memset(nsyscalls + trace->syscalls.max + 1, 0, | |
218 | (id - trace->syscalls.max) * sizeof(*sc)); | |
219 | } else { | |
220 | memset(nsyscalls, 0, (id + 1) * sizeof(*sc)); | |
221 | } | |
222 | ||
223 | trace->syscalls.table = nsyscalls; | |
224 | trace->syscalls.max = id; | |
225 | } | |
226 | ||
227 | sc = trace->syscalls.table + id; | |
3a531260 ACM |
228 | sc->name = name; |
229 | sc->fmt = syscall_fmt__find(sc->name); | |
514f1c67 | 230 | |
aec1930b | 231 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); |
514f1c67 | 232 | sc->tp_format = event_format__new("syscalls", tp_name); |
aec1930b ACM |
233 | |
234 | if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) { | |
235 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); | |
236 | sc->tp_format = event_format__new("syscalls", tp_name); | |
237 | } | |
514f1c67 ACM |
238 | |
239 | return sc->tp_format != NULL ? 0 : -1; | |
240 | } | |
241 | ||
752fde44 ACM |
242 | static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, |
243 | unsigned long *args) | |
514f1c67 ACM |
244 | { |
245 | int i = 0; | |
246 | size_t printed = 0; | |
247 | ||
248 | if (sc->tp_format != NULL) { | |
249 | struct format_field *field; | |
250 | ||
251 | for (field = sc->tp_format->format.fields->next; field; field = field->next) { | |
752fde44 ACM |
252 | printed += scnprintf(bf + printed, size - printed, |
253 | "%s%s: %ld", printed ? ", " : "", | |
254 | field->name, args[i++]); | |
514f1c67 ACM |
255 | } |
256 | } else { | |
257 | while (i < 6) { | |
752fde44 ACM |
258 | printed += scnprintf(bf + printed, size - printed, |
259 | "%sarg%d: %ld", | |
260 | printed ? ", " : "", i, args[i]); | |
514f1c67 ACM |
261 | ++i; |
262 | } | |
263 | } | |
264 | ||
265 | return printed; | |
266 | } | |
267 | ||
ba3d7dee ACM |
268 | typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel, |
269 | struct perf_sample *sample); | |
270 | ||
271 | static struct syscall *trace__syscall_info(struct trace *trace, | |
272 | struct perf_evsel *evsel, | |
273 | struct perf_sample *sample) | |
274 | { | |
275 | int id = perf_evsel__intval(evsel, sample, "id"); | |
276 | ||
277 | if (id < 0) { | |
278 | printf("Invalid syscall %d id, skipping...\n", id); | |
279 | return NULL; | |
280 | } | |
281 | ||
282 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) && | |
283 | trace__read_syscall_info(trace, id)) | |
284 | goto out_cant_read; | |
285 | ||
286 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL)) | |
287 | goto out_cant_read; | |
288 | ||
289 | return &trace->syscalls.table[id]; | |
290 | ||
291 | out_cant_read: | |
814d7a4d ACM |
292 | printf("Problems reading syscall %d", id); |
293 | if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL) | |
294 | printf("(%s)", trace->syscalls.table[id].name); | |
295 | puts(" information"); | |
ba3d7dee ACM |
296 | return NULL; |
297 | } | |
298 | ||
299 | static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, | |
300 | struct perf_sample *sample) | |
301 | { | |
752fde44 | 302 | char *msg; |
ba3d7dee | 303 | void *args; |
752fde44 ACM |
304 | size_t printed = 0; |
305 | struct thread *thread = machine__findnew_thread(&trace->host, sample->tid); | |
ba3d7dee | 306 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); |
752fde44 | 307 | struct thread_trace *ttrace = thread__trace(thread); |
ba3d7dee | 308 | |
752fde44 | 309 | if (ttrace == NULL || sc == NULL) |
ba3d7dee ACM |
310 | return -1; |
311 | ||
312 | args = perf_evsel__rawptr(evsel, sample, "args"); | |
313 | if (args == NULL) { | |
314 | printf("Problems reading syscall arguments\n"); | |
315 | return -1; | |
316 | } | |
317 | ||
752fde44 ACM |
318 | ttrace = thread->priv; |
319 | ||
320 | if (ttrace->entry_str == NULL) { | |
321 | ttrace->entry_str = malloc(1024); | |
322 | if (!ttrace->entry_str) | |
323 | return -1; | |
324 | } | |
325 | ||
326 | ttrace->entry_time = sample->time; | |
327 | msg = ttrace->entry_str; | |
328 | printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name); | |
329 | ||
330 | printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args); | |
331 | ||
332 | if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) { | |
ae9ed035 ACM |
333 | if (!trace->duration_filter) { |
334 | trace__fprintf_entry_head(trace, thread, 1, sample->time, stdout); | |
335 | printf("%-70s\n", ttrace->entry_str); | |
336 | } | |
752fde44 ACM |
337 | } else |
338 | ttrace->entry_pending = true; | |
ba3d7dee ACM |
339 | |
340 | return 0; | |
341 | } | |
342 | ||
343 | static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel, | |
344 | struct perf_sample *sample) | |
345 | { | |
346 | int ret; | |
60c907ab | 347 | u64 duration = 0; |
752fde44 ACM |
348 | struct thread *thread = machine__findnew_thread(&trace->host, sample->tid); |
349 | struct thread_trace *ttrace = thread__trace(thread); | |
ba3d7dee ACM |
350 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); |
351 | ||
752fde44 | 352 | if (ttrace == NULL || sc == NULL) |
ba3d7dee ACM |
353 | return -1; |
354 | ||
355 | ret = perf_evsel__intval(evsel, sample, "ret"); | |
356 | ||
752fde44 ACM |
357 | ttrace = thread->priv; |
358 | ||
359 | ttrace->exit_time = sample->time; | |
360 | ||
ae9ed035 | 361 | if (ttrace->entry_time) { |
60c907ab | 362 | duration = sample->time - ttrace->entry_time; |
ae9ed035 ACM |
363 | if (trace__filter_duration(trace, duration)) |
364 | goto out; | |
365 | } else if (trace->duration_filter) | |
366 | goto out; | |
60c907ab ACM |
367 | |
368 | trace__fprintf_entry_head(trace, thread, duration, sample->time, stdout); | |
752fde44 ACM |
369 | |
370 | if (ttrace->entry_pending) { | |
371 | printf("%-70s", ttrace->entry_str); | |
372 | } else { | |
373 | printf(" ... ["); | |
374 | color_fprintf(stdout, PERF_COLOR_YELLOW, "continued"); | |
375 | printf("]: %s()", sc->name); | |
376 | } | |
377 | ||
ba3d7dee ACM |
378 | if (ret < 0 && sc->fmt && sc->fmt->errmsg) { |
379 | char bf[256]; | |
380 | const char *emsg = strerror_r(-ret, bf, sizeof(bf)), | |
381 | *e = audit_errno_to_name(-ret); | |
382 | ||
383 | printf(") = -1 %s %s", e, emsg); | |
384 | } else if (ret == 0 && sc->fmt && sc->fmt->timeout) | |
385 | printf(") = 0 Timeout"); | |
386 | else | |
387 | printf(") = %d", ret); | |
388 | ||
389 | putchar('\n'); | |
ae9ed035 | 390 | out: |
752fde44 ACM |
391 | ttrace->entry_pending = false; |
392 | ||
ba3d7dee ACM |
393 | return 0; |
394 | } | |
395 | ||
1302d88e ACM |
396 | static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel, |
397 | struct perf_sample *sample) | |
398 | { | |
399 | u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); | |
400 | double runtime_ms = (double)runtime / NSEC_PER_MSEC; | |
401 | struct thread *thread = machine__findnew_thread(&trace->host, sample->tid); | |
402 | struct thread_trace *ttrace = thread__trace(thread); | |
403 | ||
404 | if (ttrace == NULL) | |
405 | goto out_dump; | |
406 | ||
407 | ttrace->runtime_ms += runtime_ms; | |
408 | trace->runtime_ms += runtime_ms; | |
409 | return 0; | |
410 | ||
411 | out_dump: | |
412 | printf("%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n", | |
413 | evsel->name, | |
414 | perf_evsel__strval(evsel, sample, "comm"), | |
415 | (pid_t)perf_evsel__intval(evsel, sample, "pid"), | |
416 | runtime, | |
417 | perf_evsel__intval(evsel, sample, "vruntime")); | |
418 | return 0; | |
419 | } | |
420 | ||
f15eb531 | 421 | static int trace__run(struct trace *trace, int argc, const char **argv) |
514f1c67 | 422 | { |
334fe7a3 | 423 | struct perf_evlist *evlist = perf_evlist__new(); |
ba3d7dee | 424 | struct perf_evsel *evsel; |
efd5745e ACM |
425 | int err = -1, i; |
426 | unsigned long before; | |
f15eb531 | 427 | const bool forks = argc > 0; |
514f1c67 ACM |
428 | |
429 | if (evlist == NULL) { | |
430 | printf("Not enough memory to run!\n"); | |
431 | goto out; | |
432 | } | |
433 | ||
39876e7d ACM |
434 | if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) || |
435 | perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) { | |
436 | printf("Couldn't read the raw_syscalls tracepoints information!\n"); | |
514f1c67 ACM |
437 | goto out_delete_evlist; |
438 | } | |
439 | ||
1302d88e ACM |
440 | if (trace->sched && |
441 | perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime", | |
442 | trace__sched_stat_runtime)) { | |
443 | printf("Couldn't read the sched_stat_runtime tracepoint information!\n"); | |
444 | goto out_delete_evlist; | |
445 | } | |
446 | ||
514f1c67 ACM |
447 | err = perf_evlist__create_maps(evlist, &trace->opts.target); |
448 | if (err < 0) { | |
449 | printf("Problems parsing the target to trace, check your options!\n"); | |
450 | goto out_delete_evlist; | |
451 | } | |
452 | ||
752fde44 ACM |
453 | err = trace__symbols_init(trace, evlist); |
454 | if (err < 0) { | |
455 | printf("Problems initializing symbol libraries!\n"); | |
3beb0861 | 456 | goto out_delete_maps; |
752fde44 ACM |
457 | } |
458 | ||
f77a9518 | 459 | perf_evlist__config(evlist, &trace->opts); |
514f1c67 | 460 | |
f15eb531 NK |
461 | signal(SIGCHLD, sig_handler); |
462 | signal(SIGINT, sig_handler); | |
463 | ||
464 | if (forks) { | |
6ef73ec4 | 465 | err = perf_evlist__prepare_workload(evlist, &trace->opts.target, |
55e162ea | 466 | argv, false, false); |
f15eb531 NK |
467 | if (err < 0) { |
468 | printf("Couldn't run the workload!\n"); | |
3beb0861 | 469 | goto out_delete_maps; |
f15eb531 NK |
470 | } |
471 | } | |
472 | ||
514f1c67 ACM |
473 | err = perf_evlist__open(evlist); |
474 | if (err < 0) { | |
475 | printf("Couldn't create the events: %s\n", strerror(errno)); | |
3beb0861 | 476 | goto out_delete_maps; |
514f1c67 ACM |
477 | } |
478 | ||
479 | err = perf_evlist__mmap(evlist, UINT_MAX, false); | |
480 | if (err < 0) { | |
481 | printf("Couldn't mmap the events: %s\n", strerror(errno)); | |
3beb0861 | 482 | goto out_close_evlist; |
514f1c67 ACM |
483 | } |
484 | ||
485 | perf_evlist__enable(evlist); | |
f15eb531 NK |
486 | |
487 | if (forks) | |
488 | perf_evlist__start_workload(evlist); | |
489 | ||
752fde44 | 490 | trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1; |
514f1c67 | 491 | again: |
efd5745e | 492 | before = trace->nr_events; |
514f1c67 ACM |
493 | |
494 | for (i = 0; i < evlist->nr_mmaps; i++) { | |
495 | union perf_event *event; | |
496 | ||
497 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { | |
498 | const u32 type = event->header.type; | |
ba3d7dee | 499 | tracepoint_handler handler; |
514f1c67 | 500 | struct perf_sample sample; |
514f1c67 | 501 | |
efd5745e | 502 | ++trace->nr_events; |
514f1c67 | 503 | |
514f1c67 ACM |
504 | err = perf_evlist__parse_sample(evlist, event, &sample); |
505 | if (err) { | |
506 | printf("Can't parse sample, err = %d, skipping...\n", err); | |
507 | continue; | |
508 | } | |
509 | ||
752fde44 ACM |
510 | if (trace->base_time == 0) |
511 | trace->base_time = sample.time; | |
512 | ||
513 | if (type != PERF_RECORD_SAMPLE) { | |
514 | trace__process_event(&trace->host, event); | |
515 | continue; | |
516 | } | |
517 | ||
514f1c67 ACM |
518 | evsel = perf_evlist__id2evsel(evlist, sample.id); |
519 | if (evsel == NULL) { | |
520 | printf("Unknown tp ID %" PRIu64 ", skipping...\n", sample.id); | |
521 | continue; | |
522 | } | |
523 | ||
fc551f8d ACM |
524 | if (sample.raw_data == NULL) { |
525 | printf("%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n", | |
526 | perf_evsel__name(evsel), sample.tid, | |
527 | sample.cpu, sample.raw_size); | |
528 | continue; | |
529 | } | |
530 | ||
ba3d7dee ACM |
531 | handler = evsel->handler.func; |
532 | handler(trace, evsel, &sample); | |
514f1c67 ACM |
533 | } |
534 | } | |
535 | ||
efd5745e | 536 | if (trace->nr_events == before) { |
f15eb531 | 537 | if (done) |
3beb0861 | 538 | goto out_unmap_evlist; |
f15eb531 | 539 | |
514f1c67 | 540 | poll(evlist->pollfd, evlist->nr_fds, -1); |
f15eb531 NK |
541 | } |
542 | ||
543 | if (done) | |
544 | perf_evlist__disable(evlist); | |
514f1c67 ACM |
545 | |
546 | goto again; | |
547 | ||
3beb0861 NK |
548 | out_unmap_evlist: |
549 | perf_evlist__munmap(evlist); | |
550 | out_close_evlist: | |
551 | perf_evlist__close(evlist); | |
552 | out_delete_maps: | |
553 | perf_evlist__delete_maps(evlist); | |
514f1c67 ACM |
554 | out_delete_evlist: |
555 | perf_evlist__delete(evlist); | |
556 | out: | |
557 | return err; | |
558 | } | |
559 | ||
1302d88e ACM |
560 | static size_t trace__fprintf_threads_header(FILE *fp) |
561 | { | |
562 | size_t printed; | |
563 | ||
564 | printed = fprintf(fp, "\n _____________________________________________________________________\n"); | |
565 | printed += fprintf(fp," __) Summary of events (__\n\n"); | |
566 | printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n"); | |
567 | printed += fprintf(fp," _____________________________________________________________________\n\n"); | |
568 | ||
569 | return printed; | |
570 | } | |
571 | ||
572 | static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) | |
573 | { | |
574 | size_t printed = trace__fprintf_threads_header(fp); | |
575 | struct rb_node *nd; | |
576 | ||
577 | for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) { | |
578 | struct thread *thread = rb_entry(nd, struct thread, rb_node); | |
579 | struct thread_trace *ttrace = thread->priv; | |
580 | const char *color; | |
581 | double ratio; | |
582 | ||
583 | if (ttrace == NULL) | |
584 | continue; | |
585 | ||
586 | ratio = (double)ttrace->nr_events / trace->nr_events * 100.0; | |
587 | ||
588 | color = PERF_COLOR_NORMAL; | |
589 | if (ratio > 50.0) | |
590 | color = PERF_COLOR_RED; | |
591 | else if (ratio > 25.0) | |
592 | color = PERF_COLOR_GREEN; | |
593 | else if (ratio > 5.0) | |
594 | color = PERF_COLOR_YELLOW; | |
595 | ||
596 | printed += color_fprintf(fp, color, "%20s", thread->comm); | |
38051234 | 597 | printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events); |
1302d88e ACM |
598 | printed += color_fprintf(fp, color, "%5.1f%%", ratio); |
599 | printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms); | |
600 | } | |
601 | ||
602 | return printed; | |
603 | } | |
604 | ||
ae9ed035 ACM |
605 | static int trace__set_duration(const struct option *opt, const char *str, |
606 | int unset __maybe_unused) | |
607 | { | |
608 | struct trace *trace = opt->value; | |
609 | ||
610 | trace->duration_filter = atof(str); | |
611 | return 0; | |
612 | } | |
613 | ||
514f1c67 ACM |
614 | int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) |
615 | { | |
616 | const char * const trace_usage[] = { | |
f15eb531 NK |
617 | "perf trace [<options>] [<command>]", |
618 | "perf trace [<options>] -- <command> [<options>]", | |
514f1c67 ACM |
619 | NULL |
620 | }; | |
621 | struct trace trace = { | |
622 | .audit_machine = audit_detect_machine(), | |
623 | .syscalls = { | |
624 | . max = -1, | |
625 | }, | |
626 | .opts = { | |
627 | .target = { | |
628 | .uid = UINT_MAX, | |
629 | .uses_mmap = true, | |
630 | }, | |
631 | .user_freq = UINT_MAX, | |
632 | .user_interval = ULLONG_MAX, | |
633 | .no_delay = true, | |
634 | .mmap_pages = 1024, | |
635 | }, | |
636 | }; | |
637 | const struct option trace_options[] = { | |
638 | OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", | |
639 | "trace events on existing process id"), | |
640 | OPT_STRING(0, "tid", &trace.opts.target.tid, "tid", | |
641 | "trace events on existing thread id"), | |
642 | OPT_BOOLEAN(0, "all-cpus", &trace.opts.target.system_wide, | |
643 | "system-wide collection from all CPUs"), | |
644 | OPT_STRING(0, "cpu", &trace.opts.target.cpu_list, "cpu", | |
645 | "list of cpus to monitor"), | |
646 | OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit, | |
647 | "child tasks do not inherit counters"), | |
648 | OPT_UINTEGER(0, "mmap-pages", &trace.opts.mmap_pages, | |
649 | "number of mmap data pages"), | |
650 | OPT_STRING(0, "uid", &trace.opts.target.uid_str, "user", | |
651 | "user to profile"), | |
ae9ed035 ACM |
652 | OPT_CALLBACK(0, "duration", &trace, "float", |
653 | "show only events with duration > N.M ms", | |
654 | trace__set_duration), | |
1302d88e | 655 | OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"), |
514f1c67 ACM |
656 | OPT_END() |
657 | }; | |
658 | int err; | |
32caf0d1 | 659 | char bf[BUFSIZ]; |
514f1c67 ACM |
660 | |
661 | argc = parse_options(argc, argv, trace_options, trace_usage, 0); | |
514f1c67 | 662 | |
32caf0d1 NK |
663 | err = perf_target__validate(&trace.opts.target); |
664 | if (err) { | |
665 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); | |
666 | printf("%s", bf); | |
667 | return err; | |
668 | } | |
669 | ||
514f1c67 ACM |
670 | err = perf_target__parse_uid(&trace.opts.target); |
671 | if (err) { | |
514f1c67 ACM |
672 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
673 | printf("%s", bf); | |
674 | return err; | |
675 | } | |
676 | ||
f15eb531 | 677 | if (!argc && perf_target__none(&trace.opts.target)) |
ee76120e NK |
678 | trace.opts.target.system_wide = true; |
679 | ||
1302d88e ACM |
680 | err = trace__run(&trace, argc, argv); |
681 | ||
682 | if (trace.sched && !err) | |
683 | trace__fprintf_thread_summary(&trace, stdout); | |
684 | ||
685 | return err; | |
514f1c67 | 686 | } |