Linux 4.1.18
[linux/fpc-iii.git] / tools / perf / perf.c
blobb857fcbd00cf291f9dc7d8473e36cdadeac81987
1 /*
2 * perf.c
4 * Performance analysis utility.
6 * This is the main hub from which the sub-commands (perf stat,
7 * perf top, perf record, perf report, etc.) are started.
8 */
9 #include "builtin.h"
11 #include "util/exec_cmd.h"
12 #include "util/cache.h"
13 #include "util/quote.h"
14 #include "util/run-command.h"
15 #include "util/parse-events.h"
16 #include "util/parse-options.h"
17 #include "util/debug.h"
18 #include <api/fs/debugfs.h>
19 #include <pthread.h>
21 const char perf_usage_string[] =
22 "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
24 const char perf_more_info_string[] =
25 "See 'perf help COMMAND' for more information on a specific command.";
27 int use_browser = -1;
28 static int use_pager = -1;
29 const char *input_name;
31 struct cmd_struct {
32 const char *cmd;
33 int (*fn)(int, const char **, const char *);
34 int option;
37 static struct cmd_struct commands[] = {
38 { "buildid-cache", cmd_buildid_cache, 0 },
39 { "buildid-list", cmd_buildid_list, 0 },
40 { "diff", cmd_diff, 0 },
41 { "evlist", cmd_evlist, 0 },
42 { "help", cmd_help, 0 },
43 { "list", cmd_list, 0 },
44 { "record", cmd_record, 0 },
45 { "report", cmd_report, 0 },
46 { "bench", cmd_bench, 0 },
47 { "stat", cmd_stat, 0 },
48 { "timechart", cmd_timechart, 0 },
49 { "top", cmd_top, 0 },
50 { "annotate", cmd_annotate, 0 },
51 { "version", cmd_version, 0 },
52 { "script", cmd_script, 0 },
53 { "sched", cmd_sched, 0 },
54 #ifdef HAVE_LIBELF_SUPPORT
55 { "probe", cmd_probe, 0 },
56 #endif
57 { "kmem", cmd_kmem, 0 },
58 { "lock", cmd_lock, 0 },
59 { "kvm", cmd_kvm, 0 },
60 { "test", cmd_test, 0 },
61 #ifdef HAVE_LIBAUDIT_SUPPORT
62 { "trace", cmd_trace, 0 },
63 #endif
64 { "inject", cmd_inject, 0 },
65 { "mem", cmd_mem, 0 },
66 { "data", cmd_data, 0 },
69 struct pager_config {
70 const char *cmd;
71 int val;
74 static int pager_command_config(const char *var, const char *value, void *data)
76 struct pager_config *c = data;
77 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
78 c->val = perf_config_bool(var, value);
79 return 0;
82 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
83 int check_pager_config(const char *cmd)
85 struct pager_config c;
86 c.cmd = cmd;
87 c.val = -1;
88 perf_config(pager_command_config, &c);
89 return c.val;
92 static int browser_command_config(const char *var, const char *value, void *data)
94 struct pager_config *c = data;
95 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
96 c->val = perf_config_bool(var, value);
97 if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
98 c->val = perf_config_bool(var, value) ? 2 : 0;
99 return 0;
103 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
104 * and -1 for "not specified"
106 static int check_browser_config(const char *cmd)
108 struct pager_config c;
109 c.cmd = cmd;
110 c.val = -1;
111 perf_config(browser_command_config, &c);
112 return c.val;
115 static void commit_pager_choice(void)
117 switch (use_pager) {
118 case 0:
119 setenv("PERF_PAGER", "cat", 1);
120 break;
121 case 1:
122 /* setup_pager(); */
123 break;
124 default:
125 break;
129 struct option options[] = {
130 OPT_ARGUMENT("help", "help"),
131 OPT_ARGUMENT("version", "version"),
132 OPT_ARGUMENT("exec-path", "exec-path"),
133 OPT_ARGUMENT("html-path", "html-path"),
134 OPT_ARGUMENT("paginate", "paginate"),
135 OPT_ARGUMENT("no-pager", "no-pager"),
136 OPT_ARGUMENT("perf-dir", "perf-dir"),
137 OPT_ARGUMENT("work-tree", "work-tree"),
138 OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
139 OPT_ARGUMENT("buildid-dir", "buildid-dir"),
140 OPT_ARGUMENT("list-cmds", "list-cmds"),
141 OPT_ARGUMENT("list-opts", "list-opts"),
142 OPT_ARGUMENT("debug", "debug"),
143 OPT_END()
146 static int handle_options(const char ***argv, int *argc, int *envchanged)
148 int handled = 0;
150 while (*argc > 0) {
151 const char *cmd = (*argv)[0];
152 if (cmd[0] != '-')
153 break;
156 * For legacy reasons, the "version" and "help"
157 * commands can be written with "--" prepended
158 * to make them look like flags.
160 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
161 break;
164 * Check remaining flags.
166 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
167 cmd += strlen(CMD_EXEC_PATH);
168 if (*cmd == '=')
169 perf_set_argv_exec_path(cmd + 1);
170 else {
171 puts(perf_exec_path());
172 exit(0);
174 } else if (!strcmp(cmd, "--html-path")) {
175 puts(system_path(PERF_HTML_PATH));
176 exit(0);
177 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
178 use_pager = 1;
179 } else if (!strcmp(cmd, "--no-pager")) {
180 use_pager = 0;
181 if (envchanged)
182 *envchanged = 1;
183 } else if (!strcmp(cmd, "--perf-dir")) {
184 if (*argc < 2) {
185 fprintf(stderr, "No directory given for --perf-dir.\n");
186 usage(perf_usage_string);
188 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
189 if (envchanged)
190 *envchanged = 1;
191 (*argv)++;
192 (*argc)--;
193 handled++;
194 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
195 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
196 if (envchanged)
197 *envchanged = 1;
198 } else if (!strcmp(cmd, "--work-tree")) {
199 if (*argc < 2) {
200 fprintf(stderr, "No directory given for --work-tree.\n");
201 usage(perf_usage_string);
203 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
204 if (envchanged)
205 *envchanged = 1;
206 (*argv)++;
207 (*argc)--;
208 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
209 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
210 if (envchanged)
211 *envchanged = 1;
212 } else if (!strcmp(cmd, "--debugfs-dir")) {
213 if (*argc < 2) {
214 fprintf(stderr, "No directory given for --debugfs-dir.\n");
215 usage(perf_usage_string);
217 perf_debugfs_set_path((*argv)[1]);
218 if (envchanged)
219 *envchanged = 1;
220 (*argv)++;
221 (*argc)--;
222 } else if (!strcmp(cmd, "--buildid-dir")) {
223 if (*argc < 2) {
224 fprintf(stderr, "No directory given for --buildid-dir.\n");
225 usage(perf_usage_string);
227 set_buildid_dir((*argv)[1]);
228 if (envchanged)
229 *envchanged = 1;
230 (*argv)++;
231 (*argc)--;
232 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
233 perf_debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR));
234 fprintf(stderr, "dir: %s\n", debugfs_mountpoint);
235 if (envchanged)
236 *envchanged = 1;
237 } else if (!strcmp(cmd, "--list-cmds")) {
238 unsigned int i;
240 for (i = 0; i < ARRAY_SIZE(commands); i++) {
241 struct cmd_struct *p = commands+i;
242 printf("%s ", p->cmd);
244 putchar('\n');
245 exit(0);
246 } else if (!strcmp(cmd, "--list-opts")) {
247 unsigned int i;
249 for (i = 0; i < ARRAY_SIZE(options)-1; i++) {
250 struct option *p = options+i;
251 printf("--%s ", p->long_name);
253 putchar('\n');
254 exit(0);
255 } else if (!strcmp(cmd, "--debug")) {
256 if (*argc < 2) {
257 fprintf(stderr, "No variable specified for --debug.\n");
258 usage(perf_usage_string);
260 if (perf_debug_option((*argv)[1]))
261 usage(perf_usage_string);
263 (*argv)++;
264 (*argc)--;
265 } else {
266 fprintf(stderr, "Unknown option: %s\n", cmd);
267 usage(perf_usage_string);
270 (*argv)++;
271 (*argc)--;
272 handled++;
274 return handled;
277 static int handle_alias(int *argcp, const char ***argv)
279 int envchanged = 0, ret = 0, saved_errno = errno;
280 int count, option_count;
281 const char **new_argv;
282 const char *alias_command;
283 char *alias_string;
285 alias_command = (*argv)[0];
286 alias_string = alias_lookup(alias_command);
287 if (alias_string) {
288 if (alias_string[0] == '!') {
289 if (*argcp > 1) {
290 struct strbuf buf;
292 strbuf_init(&buf, PATH_MAX);
293 strbuf_addstr(&buf, alias_string);
294 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
295 free(alias_string);
296 alias_string = buf.buf;
298 ret = system(alias_string + 1);
299 if (ret >= 0 && WIFEXITED(ret) &&
300 WEXITSTATUS(ret) != 127)
301 exit(WEXITSTATUS(ret));
302 die("Failed to run '%s' when expanding alias '%s'",
303 alias_string + 1, alias_command);
305 count = split_cmdline(alias_string, &new_argv);
306 if (count < 0)
307 die("Bad alias.%s string", alias_command);
308 option_count = handle_options(&new_argv, &count, &envchanged);
309 if (envchanged)
310 die("alias '%s' changes environment variables\n"
311 "You can use '!perf' in the alias to do this.",
312 alias_command);
313 memmove(new_argv - option_count, new_argv,
314 count * sizeof(char *));
315 new_argv -= option_count;
317 if (count < 1)
318 die("empty alias for %s", alias_command);
320 if (!strcmp(alias_command, new_argv[0]))
321 die("recursive alias: %s", alias_command);
323 new_argv = realloc(new_argv, sizeof(char *) *
324 (count + *argcp + 1));
325 /* insert after command name */
326 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
327 new_argv[count + *argcp] = NULL;
329 *argv = new_argv;
330 *argcp += count - 1;
332 ret = 1;
335 errno = saved_errno;
337 return ret;
340 const char perf_version_string[] = PERF_VERSION;
342 #define RUN_SETUP (1<<0)
343 #define USE_PAGER (1<<1)
345 * require working tree to be present -- anything uses this needs
346 * RUN_SETUP for reading from the configuration file.
348 #define NEED_WORK_TREE (1<<2)
350 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
352 int status;
353 struct stat st;
354 const char *prefix;
355 char sbuf[STRERR_BUFSIZE];
357 prefix = NULL;
358 if (p->option & RUN_SETUP)
359 prefix = NULL; /* setup_perf_directory(); */
361 if (use_browser == -1)
362 use_browser = check_browser_config(p->cmd);
364 if (use_pager == -1 && p->option & RUN_SETUP)
365 use_pager = check_pager_config(p->cmd);
366 if (use_pager == -1 && p->option & USE_PAGER)
367 use_pager = 1;
368 commit_pager_choice();
370 status = p->fn(argc, argv, prefix);
371 exit_browser(status);
373 if (status)
374 return status & 0xff;
376 /* Somebody closed stdout? */
377 if (fstat(fileno(stdout), &st))
378 return 0;
379 /* Ignore write errors for pipes and sockets.. */
380 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
381 return 0;
383 status = 1;
384 /* Check for ENOSPC and EIO errors.. */
385 if (fflush(stdout)) {
386 fprintf(stderr, "write failure on standard output: %s",
387 strerror_r(errno, sbuf, sizeof(sbuf)));
388 goto out;
390 if (ferror(stdout)) {
391 fprintf(stderr, "unknown write failure on standard output");
392 goto out;
394 if (fclose(stdout)) {
395 fprintf(stderr, "close failed on standard output: %s",
396 strerror_r(errno, sbuf, sizeof(sbuf)));
397 goto out;
399 status = 0;
400 out:
401 return status;
404 static void handle_internal_command(int argc, const char **argv)
406 const char *cmd = argv[0];
407 unsigned int i;
408 static const char ext[] = STRIP_EXTENSION;
410 if (sizeof(ext) > 1) {
411 i = strlen(argv[0]) - strlen(ext);
412 if (i > 0 && !strcmp(argv[0] + i, ext)) {
413 char *argv0 = strdup(argv[0]);
414 argv[0] = cmd = argv0;
415 argv0[i] = '\0';
419 /* Turn "perf cmd --help" into "perf help cmd" */
420 if (argc > 1 && !strcmp(argv[1], "--help")) {
421 argv[1] = argv[0];
422 argv[0] = cmd = "help";
425 for (i = 0; i < ARRAY_SIZE(commands); i++) {
426 struct cmd_struct *p = commands+i;
427 if (strcmp(p->cmd, cmd))
428 continue;
429 exit(run_builtin(p, argc, argv));
433 static void execv_dashed_external(const char **argv)
435 struct strbuf cmd = STRBUF_INIT;
436 const char *tmp;
437 int status;
439 strbuf_addf(&cmd, "perf-%s", argv[0]);
442 * argv[0] must be the perf command, but the argv array
443 * belongs to the caller, and may be reused in
444 * subsequent loop iterations. Save argv[0] and
445 * restore it on error.
447 tmp = argv[0];
448 argv[0] = cmd.buf;
451 * if we fail because the command is not found, it is
452 * OK to return. Otherwise, we just pass along the status code.
454 status = run_command_v_opt(argv, 0);
455 if (status != -ERR_RUN_COMMAND_EXEC) {
456 if (IS_RUN_COMMAND_ERR(status))
457 die("unable to run '%s'", argv[0]);
458 exit(-status);
460 errno = ENOENT; /* as if we called execvp */
462 argv[0] = tmp;
464 strbuf_release(&cmd);
467 static int run_argv(int *argcp, const char ***argv)
469 int done_alias = 0;
471 while (1) {
472 /* See if it's an internal command */
473 handle_internal_command(*argcp, *argv);
475 /* .. then try the external ones */
476 execv_dashed_external(*argv);
478 /* It could be an alias -- this works around the insanity
479 * of overriding "perf log" with "perf show" by having
480 * alias.log = show
482 if (done_alias || !handle_alias(argcp, argv))
483 break;
484 done_alias = 1;
487 return done_alias;
490 static void pthread__block_sigwinch(void)
492 sigset_t set;
494 sigemptyset(&set);
495 sigaddset(&set, SIGWINCH);
496 pthread_sigmask(SIG_BLOCK, &set, NULL);
499 void pthread__unblock_sigwinch(void)
501 sigset_t set;
503 sigemptyset(&set);
504 sigaddset(&set, SIGWINCH);
505 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
508 int main(int argc, const char **argv)
510 const char *cmd;
511 char sbuf[STRERR_BUFSIZE];
513 /* The page_size is placed in util object. */
514 page_size = sysconf(_SC_PAGE_SIZE);
515 cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
517 cmd = perf_extract_argv0_path(argv[0]);
518 if (!cmd)
519 cmd = "perf-help";
520 /* get debugfs mount point from /proc/mounts */
521 perf_debugfs_mount(NULL);
523 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
525 * - cannot take flags in between the "perf" and the "xxxx".
526 * - cannot execute it externally (since it would just do
527 * the same thing over again)
529 * So we just directly call the internal command handler, and
530 * die if that one cannot handle it.
532 if (!prefixcmp(cmd, "perf-")) {
533 cmd += 5;
534 argv[0] = cmd;
535 handle_internal_command(argc, argv);
536 fprintf(stderr, "cannot handle %s internally", cmd);
537 goto out;
539 if (!prefixcmp(cmd, "trace")) {
540 #ifdef HAVE_LIBAUDIT_SUPPORT
541 set_buildid_dir(NULL);
542 setup_path();
543 argv[0] = "trace";
544 return cmd_trace(argc, argv, NULL);
545 #else
546 fprintf(stderr,
547 "trace command not available: missing audit-libs devel package at build time.\n");
548 goto out;
549 #endif
551 /* Look for flags.. */
552 argv++;
553 argc--;
554 handle_options(&argv, &argc, NULL);
555 commit_pager_choice();
556 set_buildid_dir(NULL);
558 if (argc > 0) {
559 if (!prefixcmp(argv[0], "--"))
560 argv[0] += 2;
561 } else {
562 /* The user didn't specify a command; give them help */
563 printf("\n usage: %s\n\n", perf_usage_string);
564 list_common_cmds_help();
565 printf("\n %s\n\n", perf_more_info_string);
566 goto out;
568 cmd = argv[0];
570 test_attr__init();
573 * We use PATH to find perf commands, but we prepend some higher
574 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
575 * environment, and the $(perfexecdir) from the Makefile at build
576 * time.
578 setup_path();
580 * Block SIGWINCH notifications so that the thread that wants it can
581 * unblock and get syscalls like select interrupted instead of waiting
582 * forever while the signal goes to some other non interested thread.
584 pthread__block_sigwinch();
586 while (1) {
587 static int done_help;
588 int was_alias = run_argv(&argc, &argv);
590 if (errno != ENOENT)
591 break;
593 if (was_alias) {
594 fprintf(stderr, "Expansion of alias '%s' failed; "
595 "'%s' is not a perf-command\n",
596 cmd, argv[0]);
597 goto out;
599 if (!done_help) {
600 cmd = argv[0] = help_unknown_cmd(cmd);
601 done_help = 1;
602 } else
603 break;
606 fprintf(stderr, "Failed to run command '%s': %s\n",
607 cmd, strerror_r(errno, sbuf, sizeof(sbuf)));
608 out:
609 return 1;