perf/trace/scripting: workqueue-stats script cleanup
[deliverable/linux.git] / tools / perf / builtin-trace.c
CommitLineData
5f9c39dc
FW
1#include "builtin.h"
2
3#include "util/util.h"
4#include "util/cache.h"
5#include "util/symbol.h"
6#include "util/thread.h"
7#include "util/header.h"
cf72344d
IM
8#include "util/exec_cmd.h"
9#include "util/trace-event.h"
94c744b6 10#include "util/session.h"
5f9c39dc 11
956ffd02
TZ
12static char const *script_name;
13static char const *generate_script_lang;
e1889d75
FW
14static bool debug_ordering;
15static u64 last_timestamp;
956ffd02 16
586bc5cc
TZ
17static int default_start_script(const char *script __unused,
18 int argc __unused,
19 const char **argv __unused)
956ffd02
TZ
20{
21 return 0;
22}
23
24static int default_stop_script(void)
25{
26 return 0;
27}
28
586bc5cc 29static int default_generate_script(const char *outfile __unused)
956ffd02
TZ
30{
31 return 0;
32}
33
34static struct scripting_ops default_scripting_ops = {
35 .start_script = default_start_script,
36 .stop_script = default_stop_script,
37 .process_event = print_event,
38 .generate_script = default_generate_script,
39};
40
41static struct scripting_ops *scripting_ops;
42
43static void setup_scripting(void)
44{
45 /* make sure PERF_EXEC_PATH is set for scripts */
46 perf_set_argv_exec_path(perf_exec_path());
47
16c632de 48 setup_perl_scripting();
7e4b21b8 49 setup_python_scripting();
16c632de 50
956ffd02
TZ
51 scripting_ops = &default_scripting_ops;
52}
53
54static int cleanup_scripting(void)
55{
56 return scripting_ops->stop_script();
57}
58
5f9c39dc
FW
59#include "util/parse-options.h"
60
61#include "perf.h"
62#include "util/debug.h"
63
64#include "util/trace-event.h"
956ffd02 65#include "util/exec_cmd.h"
5f9c39dc 66
956ffd02 67static char const *input_name = "perf.data";
5f9c39dc 68
b3165f41 69static int process_sample_event(event_t *event, struct perf_session *session)
5f9c39dc 70{
180f95e2
OH
71 struct sample_data data;
72 struct thread *thread;
6ddf259d 73
180f95e2
OH
74 memset(&data, 0, sizeof(data));
75 data.time = -1;
76 data.cpu = -1;
77 data.period = 1;
cd6feeea 78
c019879b 79 event__parse_sample(event, session->sample_type, &data);
5f9c39dc 80
0d755034
ACM
81 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
82 data.pid, data.tid, data.ip, data.period);
5f9c39dc 83
b3165f41 84 thread = perf_session__findnew(session, event->ip.pid);
5f9c39dc 85 if (thread == NULL) {
6beba7ad
ACM
86 pr_debug("problem processing %d event, skipping it.\n",
87 event->header.type);
5f9c39dc
FW
88 return -1;
89 }
90
c019879b 91 if (session->sample_type & PERF_SAMPLE_RAW) {
e1889d75
FW
92 if (debug_ordering) {
93 if (data.time < last_timestamp) {
94 pr_err("Samples misordered, previous: %llu "
95 "this: %llu\n", last_timestamp,
96 data.time);
97 }
98 last_timestamp = data.time;
99 }
5f9c39dc
FW
100 /*
101 * FIXME: better resolve from pid from the struct trace_entry
102 * field, although it should be the same than this perf
103 * event pid
104 */
180f95e2
OH
105 scripting_ops->process_event(data.cpu, data.raw_data,
106 data.raw_size,
107 data.time, thread->comm);
5f9c39dc 108 }
5f9c39dc 109
1c02c4d2 110 session->hists.stats.total += data.period;
5f9c39dc
FW
111 return 0;
112}
113
301a0b02 114static struct perf_event_ops event_ops = {
55aa640f
ACM
115 .sample = process_sample_event,
116 .comm = event__process_comm,
2c46dbb5 117 .attr = event__process_attr,
cd19a035 118 .event_type = event__process_event_type,
9215545e 119 .tracing_data = event__process_tracing_data,
c7929e47 120 .build_id = event__process_build_id,
e0a808c6 121 .ordered_samples = true,
016e92fb
FW
122};
123
c239da3b
TZ
124extern volatile int session_done;
125
126static void sig_handler(int sig __unused)
127{
128 session_done = 1;
129}
130
d8f66248 131static int __cmd_trace(struct perf_session *session)
5f9c39dc 132{
c239da3b
TZ
133 signal(SIGINT, sig_handler);
134
ec913369 135 return perf_session__process_events(session, &event_ops);
5f9c39dc
FW
136}
137
956ffd02
TZ
138struct script_spec {
139 struct list_head node;
140 struct scripting_ops *ops;
141 char spec[0];
142};
143
144LIST_HEAD(script_specs);
145
146static struct script_spec *script_spec__new(const char *spec,
147 struct scripting_ops *ops)
148{
149 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
150
151 if (s != NULL) {
152 strcpy(s->spec, spec);
153 s->ops = ops;
154 }
155
156 return s;
157}
158
159static void script_spec__delete(struct script_spec *s)
160{
161 free(s->spec);
162 free(s);
163}
164
165static void script_spec__add(struct script_spec *s)
166{
167 list_add_tail(&s->node, &script_specs);
168}
169
170static struct script_spec *script_spec__find(const char *spec)
171{
172 struct script_spec *s;
173
174 list_for_each_entry(s, &script_specs, node)
175 if (strcasecmp(s->spec, spec) == 0)
176 return s;
177 return NULL;
178}
179
180static struct script_spec *script_spec__findnew(const char *spec,
181 struct scripting_ops *ops)
182{
183 struct script_spec *s = script_spec__find(spec);
184
185 if (s)
186 return s;
187
188 s = script_spec__new(spec, ops);
189 if (!s)
190 goto out_delete_spec;
191
192 script_spec__add(s);
193
194 return s;
195
196out_delete_spec:
197 script_spec__delete(s);
198
199 return NULL;
200}
201
202int script_spec_register(const char *spec, struct scripting_ops *ops)
203{
204 struct script_spec *s;
205
206 s = script_spec__find(spec);
207 if (s)
208 return -1;
209
210 s = script_spec__findnew(spec, ops);
211 if (!s)
212 return -1;
213
214 return 0;
215}
216
217static struct scripting_ops *script_spec__lookup(const char *spec)
218{
219 struct script_spec *s = script_spec__find(spec);
220 if (!s)
221 return NULL;
222
223 return s->ops;
224}
225
226static void list_available_languages(void)
227{
228 struct script_spec *s;
229
230 fprintf(stderr, "\n");
231 fprintf(stderr, "Scripting language extensions (used in "
232 "perf trace -s [spec:]script.[spec]):\n\n");
233
234 list_for_each_entry(s, &script_specs, node)
235 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
236
237 fprintf(stderr, "\n");
238}
239
240static int parse_scriptname(const struct option *opt __used,
241 const char *str, int unset __used)
242{
243 char spec[PATH_MAX];
244 const char *script, *ext;
245 int len;
246
f526d68b 247 if (strcmp(str, "lang") == 0) {
956ffd02 248 list_available_languages();
f526d68b 249 exit(0);
956ffd02
TZ
250 }
251
252 script = strchr(str, ':');
253 if (script) {
254 len = script - str;
255 if (len >= PATH_MAX) {
256 fprintf(stderr, "invalid language specifier");
257 return -1;
258 }
259 strncpy(spec, str, len);
260 spec[len] = '\0';
261 scripting_ops = script_spec__lookup(spec);
262 if (!scripting_ops) {
263 fprintf(stderr, "invalid language specifier");
264 return -1;
265 }
266 script++;
267 } else {
268 script = str;
269 ext = strchr(script, '.');
270 if (!ext) {
271 fprintf(stderr, "invalid script extension");
272 return -1;
273 }
274 scripting_ops = script_spec__lookup(++ext);
275 if (!scripting_ops) {
276 fprintf(stderr, "invalid script extension");
277 return -1;
278 }
279 }
280
281 script_name = strdup(script);
282
283 return 0;
284}
285
4b9c0c59
TZ
286#define for_each_lang(scripts_dir, lang_dirent, lang_next) \
287 while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
288 lang_next) \
289 if (lang_dirent.d_type == DT_DIR && \
290 (strcmp(lang_dirent.d_name, ".")) && \
291 (strcmp(lang_dirent.d_name, "..")))
292
293#define for_each_script(lang_dir, script_dirent, script_next) \
294 while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
295 script_next) \
296 if (script_dirent.d_type != DT_DIR)
297
298
299#define RECORD_SUFFIX "-record"
300#define REPORT_SUFFIX "-report"
301
302struct script_desc {
303 struct list_head node;
304 char *name;
305 char *half_liner;
306 char *args;
307};
308
309LIST_HEAD(script_descs);
310
311static struct script_desc *script_desc__new(const char *name)
312{
313 struct script_desc *s = zalloc(sizeof(*s));
314
315 if (s != NULL)
316 s->name = strdup(name);
317
318 return s;
319}
320
321static void script_desc__delete(struct script_desc *s)
322{
323 free(s->name);
324 free(s);
325}
326
327static void script_desc__add(struct script_desc *s)
328{
329 list_add_tail(&s->node, &script_descs);
330}
331
332static struct script_desc *script_desc__find(const char *name)
333{
334 struct script_desc *s;
335
336 list_for_each_entry(s, &script_descs, node)
337 if (strcasecmp(s->name, name) == 0)
338 return s;
339 return NULL;
340}
341
342static struct script_desc *script_desc__findnew(const char *name)
343{
344 struct script_desc *s = script_desc__find(name);
345
346 if (s)
347 return s;
348
349 s = script_desc__new(name);
350 if (!s)
351 goto out_delete_desc;
352
353 script_desc__add(s);
354
355 return s;
356
357out_delete_desc:
358 script_desc__delete(s);
359
360 return NULL;
361}
362
363static char *ends_with(char *str, const char *suffix)
364{
365 size_t suffix_len = strlen(suffix);
366 char *p = str;
367
368 if (strlen(str) > suffix_len) {
369 p = str + strlen(str) - suffix_len;
370 if (!strncmp(p, suffix, suffix_len))
371 return p;
372 }
373
374 return NULL;
375}
376
377static char *ltrim(char *str)
378{
379 int len = strlen(str);
380
381 while (len && isspace(*str)) {
382 len--;
383 str++;
384 }
385
386 return str;
387}
388
389static int read_script_info(struct script_desc *desc, const char *filename)
390{
391 char line[BUFSIZ], *p;
392 FILE *fp;
393
394 fp = fopen(filename, "r");
395 if (!fp)
396 return -1;
397
398 while (fgets(line, sizeof(line), fp)) {
399 p = ltrim(line);
400 if (strlen(p) == 0)
401 continue;
402 if (*p != '#')
403 continue;
404 p++;
405 if (strlen(p) && *p == '!')
406 continue;
407
408 p = ltrim(p);
409 if (strlen(p) && p[strlen(p) - 1] == '\n')
410 p[strlen(p) - 1] = '\0';
411
412 if (!strncmp(p, "description:", strlen("description:"))) {
413 p += strlen("description:");
414 desc->half_liner = strdup(ltrim(p));
415 continue;
416 }
417
418 if (!strncmp(p, "args:", strlen("args:"))) {
419 p += strlen("args:");
420 desc->args = strdup(ltrim(p));
421 continue;
422 }
423 }
424
425 fclose(fp);
426
427 return 0;
428}
429
430static int list_available_scripts(const struct option *opt __used,
431 const char *s __used, int unset __used)
432{
433 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
434 char scripts_path[MAXPATHLEN];
435 DIR *scripts_dir, *lang_dir;
436 char script_path[MAXPATHLEN];
437 char lang_path[MAXPATHLEN];
438 struct script_desc *desc;
439 char first_half[BUFSIZ];
440 char *script_root;
441 char *str;
442
443 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
444
445 scripts_dir = opendir(scripts_path);
446 if (!scripts_dir)
447 return -1;
448
449 for_each_lang(scripts_dir, lang_dirent, lang_next) {
450 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
451 lang_dirent.d_name);
452 lang_dir = opendir(lang_path);
453 if (!lang_dir)
454 continue;
455
456 for_each_script(lang_dir, script_dirent, script_next) {
457 script_root = strdup(script_dirent.d_name);
458 str = ends_with(script_root, REPORT_SUFFIX);
459 if (str) {
460 *str = '\0';
461 desc = script_desc__findnew(script_root);
462 snprintf(script_path, MAXPATHLEN, "%s/%s",
463 lang_path, script_dirent.d_name);
464 read_script_info(desc, script_path);
465 }
466 free(script_root);
467 }
468 }
469
470 fprintf(stdout, "List of available trace scripts:\n");
471 list_for_each_entry(desc, &script_descs, node) {
472 sprintf(first_half, "%s %s", desc->name,
473 desc->args ? desc->args : "");
474 fprintf(stdout, " %-36s %s\n", first_half,
475 desc->half_liner ? desc->half_liner : "");
476 }
477
478 exit(0);
479}
480
3875294f
TZ
481static char *get_script_path(const char *script_root, const char *suffix)
482{
483 struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
484 char scripts_path[MAXPATHLEN];
485 char script_path[MAXPATHLEN];
486 DIR *scripts_dir, *lang_dir;
487 char lang_path[MAXPATHLEN];
488 char *str, *__script_root;
489 char *path = NULL;
490
491 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
492
493 scripts_dir = opendir(scripts_path);
494 if (!scripts_dir)
495 return NULL;
496
497 for_each_lang(scripts_dir, lang_dirent, lang_next) {
498 snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
499 lang_dirent.d_name);
500 lang_dir = opendir(lang_path);
501 if (!lang_dir)
502 continue;
503
504 for_each_script(lang_dir, script_dirent, script_next) {
505 __script_root = strdup(script_dirent.d_name);
506 str = ends_with(__script_root, suffix);
507 if (str) {
508 *str = '\0';
509 if (strcmp(__script_root, script_root))
510 continue;
511 snprintf(script_path, MAXPATHLEN, "%s/%s",
512 lang_path, script_dirent.d_name);
513 path = strdup(script_path);
514 free(__script_root);
515 break;
516 }
517 free(__script_root);
518 }
519 }
520
521 return path;
522}
523
0422a4fc 524static const char * const trace_usage[] = {
5f9c39dc
FW
525 "perf trace [<options>] <command>",
526 NULL
527};
528
529static const struct option options[] = {
530 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
531 "dump raw trace in ASCII"),
c0555642 532 OPT_INCR('v', "verbose", &verbose,
5f9c39dc 533 "be more verbose (show symbol address, etc)"),
4b9c0c59 534 OPT_BOOLEAN('L', "Latency", &latency_format,
cda48461 535 "show latency attributes (irqs/preemption disabled, etc)"),
4b9c0c59
TZ
536 OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
537 list_available_scripts),
956ffd02
TZ
538 OPT_CALLBACK('s', "script", NULL, "name",
539 "script file name (lang:script name, script name, or *)",
540 parse_scriptname),
541 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
542 "generate perf-trace.xx script in specified language"),
408f0d18
HM
543 OPT_STRING('i', "input", &input_name, "file",
544 "input file name"),
e1889d75
FW
545 OPT_BOOLEAN('d', "debug-ordering", &debug_ordering,
546 "check that samples time ordering is monotonic"),
956ffd02 547
1909629f 548 OPT_END()
5f9c39dc
FW
549};
550
551int cmd_trace(int argc, const char **argv, const char *prefix __used)
552{
d8f66248 553 struct perf_session *session;
3875294f
TZ
554 const char *suffix = NULL;
555 const char **__argv;
556 char *script_path;
557 int i, err;
558
559 if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) {
560 if (argc < 3) {
561 fprintf(stderr,
562 "Please specify a record script\n");
563 return -1;
564 }
565 suffix = RECORD_SUFFIX;
566 }
567
568 if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) {
569 if (argc < 3) {
570 fprintf(stderr,
571 "Please specify a report script\n");
572 return -1;
573 }
574 suffix = REPORT_SUFFIX;
575 }
576
a0cccc2e
TZ
577 if (!suffix && argc >= 2 && strncmp(argv[1], "-", strlen("-")) != 0) {
578 char *record_script_path, *report_script_path;
579 int live_pipe[2];
580 pid_t pid;
581
582 record_script_path = get_script_path(argv[1], RECORD_SUFFIX);
583 if (!record_script_path) {
584 fprintf(stderr, "record script not found\n");
585 return -1;
586 }
587
588 report_script_path = get_script_path(argv[1], REPORT_SUFFIX);
589 if (!report_script_path) {
590 fprintf(stderr, "report script not found\n");
591 return -1;
592 }
593
594 if (pipe(live_pipe) < 0) {
595 perror("failed to create pipe");
596 exit(-1);
597 }
598
599 pid = fork();
600 if (pid < 0) {
601 perror("failed to fork");
602 exit(-1);
603 }
604
605 if (!pid) {
606 dup2(live_pipe[1], 1);
607 close(live_pipe[0]);
608
609 __argv = malloc(5 * sizeof(const char *));
610 __argv[0] = "/bin/sh";
611 __argv[1] = record_script_path;
612 __argv[2] = "-o";
613 __argv[3] = "-";
614 __argv[4] = NULL;
615
616 execvp("/bin/sh", (char **)__argv);
617 exit(-1);
618 }
619
620 dup2(live_pipe[0], 0);
621 close(live_pipe[1]);
622
623 __argv = malloc((argc + 3) * sizeof(const char *));
624 __argv[0] = "/bin/sh";
625 __argv[1] = report_script_path;
626 for (i = 2; i < argc; i++)
627 __argv[i] = argv[i];
628 __argv[i++] = "-i";
629 __argv[i++] = "-";
630 __argv[i++] = NULL;
631
632 execvp("/bin/sh", (char **)__argv);
633 exit(-1);
634 }
635
3875294f
TZ
636 if (suffix) {
637 script_path = get_script_path(argv[2], suffix);
638 if (!script_path) {
639 fprintf(stderr, "script not found\n");
640 return -1;
641 }
642
643 __argv = malloc((argc + 1) * sizeof(const char *));
644 __argv[0] = "/bin/sh";
645 __argv[1] = script_path;
646 for (i = 3; i < argc; i++)
647 __argv[i - 1] = argv[i];
648 __argv[argc - 1] = NULL;
649
650 execvp("/bin/sh", (char **)__argv);
651 exit(-1);
652 }
956ffd02 653
956ffd02
TZ
654 setup_scripting();
655
0422a4fc 656 argc = parse_options(argc, argv, options, trace_usage,
586bc5cc 657 PARSE_OPT_STOP_AT_NON_OPTION);
5f9c39dc 658
655000e7
ACM
659 if (symbol__init() < 0)
660 return -1;
cf4fee50
TZ
661 if (!script_name)
662 setup_pager();
5f9c39dc 663
454c407e 664 session = perf_session__new(input_name, O_RDONLY, 0, false);
d8f66248
ACM
665 if (session == NULL)
666 return -ENOMEM;
667
c239da3b
TZ
668 if (strcmp(input_name, "-") &&
669 !perf_session__has_traces(session, "record -R"))
d549c769
ACM
670 return -EINVAL;
671
956ffd02
TZ
672 if (generate_script_lang) {
673 struct stat perf_stat;
674
675 int input = open(input_name, O_RDONLY);
676 if (input < 0) {
677 perror("failed to open file");
678 exit(-1);
679 }
680
681 err = fstat(input, &perf_stat);
682 if (err < 0) {
683 perror("failed to stat file");
684 exit(-1);
685 }
686
687 if (!perf_stat.st_size) {
688 fprintf(stderr, "zero-sized file, nothing to do!\n");
689 exit(0);
690 }
691
692 scripting_ops = script_spec__lookup(generate_script_lang);
693 if (!scripting_ops) {
694 fprintf(stderr, "invalid language specifier");
695 return -1;
696 }
697
956ffd02
TZ
698 err = scripting_ops->generate_script("perf-trace");
699 goto out;
700 }
701
702 if (script_name) {
586bc5cc 703 err = scripting_ops->start_script(script_name, argc, argv);
956ffd02
TZ
704 if (err)
705 goto out;
706 }
707
d8f66248 708 err = __cmd_trace(session);
956ffd02 709
d8f66248 710 perf_session__delete(session);
956ffd02
TZ
711 cleanup_scripting();
712out:
713 return err;
5f9c39dc 714}
This page took 0.088081 seconds and 5 git commands to generate.