Merge tag 'for-3.9-rc3' of git://openrisc.net/jonas/linux
[linux-2.6/cjktty.git] / tools / perf / perf.c
blob095b88207cd3e8f952b1b9676a0abf57f91dde50
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/debugfs.h"
17 #include <pthread.h>
19 const char perf_usage_string[] =
20 "perf [--version] [--help] COMMAND [ARGS]";
22 const char perf_more_info_string[] =
23 "See 'perf help COMMAND' for more information on a specific command.";
25 int use_browser = -1;
26 static int use_pager = -1;
27 const char *input_name;
29 struct cmd_struct {
30 const char *cmd;
31 int (*fn)(int, const char **, const char *);
32 int option;
35 static struct cmd_struct commands[] = {
36 { "buildid-cache", cmd_buildid_cache, 0 },
37 { "buildid-list", cmd_buildid_list, 0 },
38 { "diff", cmd_diff, 0 },
39 { "evlist", cmd_evlist, 0 },
40 { "help", cmd_help, 0 },
41 { "list", cmd_list, 0 },
42 { "record", cmd_record, 0 },
43 { "report", cmd_report, 0 },
44 { "bench", cmd_bench, 0 },
45 { "stat", cmd_stat, 0 },
46 { "timechart", cmd_timechart, 0 },
47 { "top", cmd_top, 0 },
48 { "annotate", cmd_annotate, 0 },
49 { "version", cmd_version, 0 },
50 { "script", cmd_script, 0 },
51 { "sched", cmd_sched, 0 },
52 #ifdef LIBELF_SUPPORT
53 { "probe", cmd_probe, 0 },
54 #endif
55 { "kmem", cmd_kmem, 0 },
56 { "lock", cmd_lock, 0 },
57 { "kvm", cmd_kvm, 0 },
58 { "test", cmd_test, 0 },
59 #ifdef LIBAUDIT_SUPPORT
60 { "trace", cmd_trace, 0 },
61 #endif
62 { "inject", cmd_inject, 0 },
65 struct pager_config {
66 const char *cmd;
67 int val;
70 static int pager_command_config(const char *var, const char *value, void *data)
72 struct pager_config *c = data;
73 if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
74 c->val = perf_config_bool(var, value);
75 return 0;
78 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
79 int check_pager_config(const char *cmd)
81 struct pager_config c;
82 c.cmd = cmd;
83 c.val = -1;
84 perf_config(pager_command_config, &c);
85 return c.val;
88 static int browser_command_config(const char *var, const char *value, void *data)
90 struct pager_config *c = data;
91 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
92 c->val = perf_config_bool(var, value);
93 if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
94 c->val = perf_config_bool(var, value) ? 2 : 0;
95 return 0;
99 * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
100 * and -1 for "not specified"
102 static int check_browser_config(const char *cmd)
104 struct pager_config c;
105 c.cmd = cmd;
106 c.val = -1;
107 perf_config(browser_command_config, &c);
108 return c.val;
111 static void commit_pager_choice(void)
113 switch (use_pager) {
114 case 0:
115 setenv("PERF_PAGER", "cat", 1);
116 break;
117 case 1:
118 /* setup_pager(); */
119 break;
120 default:
121 break;
125 static int handle_options(const char ***argv, int *argc, int *envchanged)
127 int handled = 0;
129 while (*argc > 0) {
130 const char *cmd = (*argv)[0];
131 if (cmd[0] != '-')
132 break;
135 * For legacy reasons, the "version" and "help"
136 * commands can be written with "--" prepended
137 * to make them look like flags.
139 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
140 break;
143 * Check remaining flags.
145 if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
146 cmd += strlen(CMD_EXEC_PATH);
147 if (*cmd == '=')
148 perf_set_argv_exec_path(cmd + 1);
149 else {
150 puts(perf_exec_path());
151 exit(0);
153 } else if (!strcmp(cmd, "--html-path")) {
154 puts(system_path(PERF_HTML_PATH));
155 exit(0);
156 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
157 use_pager = 1;
158 } else if (!strcmp(cmd, "--no-pager")) {
159 use_pager = 0;
160 if (envchanged)
161 *envchanged = 1;
162 } else if (!strcmp(cmd, "--perf-dir")) {
163 if (*argc < 2) {
164 fprintf(stderr, "No directory given for --perf-dir.\n");
165 usage(perf_usage_string);
167 setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
168 if (envchanged)
169 *envchanged = 1;
170 (*argv)++;
171 (*argc)--;
172 handled++;
173 } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
174 setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
175 if (envchanged)
176 *envchanged = 1;
177 } else if (!strcmp(cmd, "--work-tree")) {
178 if (*argc < 2) {
179 fprintf(stderr, "No directory given for --work-tree.\n");
180 usage(perf_usage_string);
182 setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
183 if (envchanged)
184 *envchanged = 1;
185 (*argv)++;
186 (*argc)--;
187 } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
188 setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
189 if (envchanged)
190 *envchanged = 1;
191 } else if (!strcmp(cmd, "--debugfs-dir")) {
192 if (*argc < 2) {
193 fprintf(stderr, "No directory given for --debugfs-dir.\n");
194 usage(perf_usage_string);
196 debugfs_set_path((*argv)[1]);
197 if (envchanged)
198 *envchanged = 1;
199 (*argv)++;
200 (*argc)--;
201 } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
202 debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR));
203 fprintf(stderr, "dir: %s\n", debugfs_mountpoint);
204 if (envchanged)
205 *envchanged = 1;
206 } else if (!strcmp(cmd, "--list-cmds")) {
207 unsigned int i;
209 for (i = 0; i < ARRAY_SIZE(commands); i++) {
210 struct cmd_struct *p = commands+i;
211 printf("%s ", p->cmd);
213 exit(0);
214 } else {
215 fprintf(stderr, "Unknown option: %s\n", cmd);
216 usage(perf_usage_string);
219 (*argv)++;
220 (*argc)--;
221 handled++;
223 return handled;
226 static int handle_alias(int *argcp, const char ***argv)
228 int envchanged = 0, ret = 0, saved_errno = errno;
229 int count, option_count;
230 const char **new_argv;
231 const char *alias_command;
232 char *alias_string;
234 alias_command = (*argv)[0];
235 alias_string = alias_lookup(alias_command);
236 if (alias_string) {
237 if (alias_string[0] == '!') {
238 if (*argcp > 1) {
239 struct strbuf buf;
241 strbuf_init(&buf, PATH_MAX);
242 strbuf_addstr(&buf, alias_string);
243 sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
244 free(alias_string);
245 alias_string = buf.buf;
247 ret = system(alias_string + 1);
248 if (ret >= 0 && WIFEXITED(ret) &&
249 WEXITSTATUS(ret) != 127)
250 exit(WEXITSTATUS(ret));
251 die("Failed to run '%s' when expanding alias '%s'",
252 alias_string + 1, alias_command);
254 count = split_cmdline(alias_string, &new_argv);
255 if (count < 0)
256 die("Bad alias.%s string", alias_command);
257 option_count = handle_options(&new_argv, &count, &envchanged);
258 if (envchanged)
259 die("alias '%s' changes environment variables\n"
260 "You can use '!perf' in the alias to do this.",
261 alias_command);
262 memmove(new_argv - option_count, new_argv,
263 count * sizeof(char *));
264 new_argv -= option_count;
266 if (count < 1)
267 die("empty alias for %s", alias_command);
269 if (!strcmp(alias_command, new_argv[0]))
270 die("recursive alias: %s", alias_command);
272 new_argv = realloc(new_argv, sizeof(char *) *
273 (count + *argcp + 1));
274 /* insert after command name */
275 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
276 new_argv[count + *argcp] = NULL;
278 *argv = new_argv;
279 *argcp += count - 1;
281 ret = 1;
284 errno = saved_errno;
286 return ret;
289 const char perf_version_string[] = PERF_VERSION;
291 #define RUN_SETUP (1<<0)
292 #define USE_PAGER (1<<1)
294 * require working tree to be present -- anything uses this needs
295 * RUN_SETUP for reading from the configuration file.
297 #define NEED_WORK_TREE (1<<2)
299 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
301 int status;
302 struct stat st;
303 const char *prefix;
305 prefix = NULL;
306 if (p->option & RUN_SETUP)
307 prefix = NULL; /* setup_perf_directory(); */
309 if (use_browser == -1)
310 use_browser = check_browser_config(p->cmd);
312 if (use_pager == -1 && p->option & RUN_SETUP)
313 use_pager = check_pager_config(p->cmd);
314 if (use_pager == -1 && p->option & USE_PAGER)
315 use_pager = 1;
316 commit_pager_choice();
318 status = p->fn(argc, argv, prefix);
319 exit_browser(status);
321 if (status)
322 return status & 0xff;
324 /* Somebody closed stdout? */
325 if (fstat(fileno(stdout), &st))
326 return 0;
327 /* Ignore write errors for pipes and sockets.. */
328 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
329 return 0;
331 status = 1;
332 /* Check for ENOSPC and EIO errors.. */
333 if (fflush(stdout)) {
334 fprintf(stderr, "write failure on standard output: %s", strerror(errno));
335 goto out;
337 if (ferror(stdout)) {
338 fprintf(stderr, "unknown write failure on standard output");
339 goto out;
341 if (fclose(stdout)) {
342 fprintf(stderr, "close failed on standard output: %s", strerror(errno));
343 goto out;
345 status = 0;
346 out:
347 return status;
350 static void handle_internal_command(int argc, const char **argv)
352 const char *cmd = argv[0];
353 unsigned int i;
354 static const char ext[] = STRIP_EXTENSION;
356 if (sizeof(ext) > 1) {
357 i = strlen(argv[0]) - strlen(ext);
358 if (i > 0 && !strcmp(argv[0] + i, ext)) {
359 char *argv0 = strdup(argv[0]);
360 argv[0] = cmd = argv0;
361 argv0[i] = '\0';
365 /* Turn "perf cmd --help" into "perf help cmd" */
366 if (argc > 1 && !strcmp(argv[1], "--help")) {
367 argv[1] = argv[0];
368 argv[0] = cmd = "help";
371 for (i = 0; i < ARRAY_SIZE(commands); i++) {
372 struct cmd_struct *p = commands+i;
373 if (strcmp(p->cmd, cmd))
374 continue;
375 exit(run_builtin(p, argc, argv));
379 static void execv_dashed_external(const char **argv)
381 struct strbuf cmd = STRBUF_INIT;
382 const char *tmp;
383 int status;
385 strbuf_addf(&cmd, "perf-%s", argv[0]);
388 * argv[0] must be the perf command, but the argv array
389 * belongs to the caller, and may be reused in
390 * subsequent loop iterations. Save argv[0] and
391 * restore it on error.
393 tmp = argv[0];
394 argv[0] = cmd.buf;
397 * if we fail because the command is not found, it is
398 * OK to return. Otherwise, we just pass along the status code.
400 status = run_command_v_opt(argv, 0);
401 if (status != -ERR_RUN_COMMAND_EXEC) {
402 if (IS_RUN_COMMAND_ERR(status))
403 die("unable to run '%s'", argv[0]);
404 exit(-status);
406 errno = ENOENT; /* as if we called execvp */
408 argv[0] = tmp;
410 strbuf_release(&cmd);
413 static int run_argv(int *argcp, const char ***argv)
415 int done_alias = 0;
417 while (1) {
418 /* See if it's an internal command */
419 handle_internal_command(*argcp, *argv);
421 /* .. then try the external ones */
422 execv_dashed_external(*argv);
424 /* It could be an alias -- this works around the insanity
425 * of overriding "perf log" with "perf show" by having
426 * alias.log = show
428 if (done_alias || !handle_alias(argcp, argv))
429 break;
430 done_alias = 1;
433 return done_alias;
436 static void pthread__block_sigwinch(void)
438 sigset_t set;
440 sigemptyset(&set);
441 sigaddset(&set, SIGWINCH);
442 pthread_sigmask(SIG_BLOCK, &set, NULL);
445 void pthread__unblock_sigwinch(void)
447 sigset_t set;
449 sigemptyset(&set);
450 sigaddset(&set, SIGWINCH);
451 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
454 int main(int argc, const char **argv)
456 const char *cmd;
458 page_size = sysconf(_SC_PAGE_SIZE);
460 cmd = perf_extract_argv0_path(argv[0]);
461 if (!cmd)
462 cmd = "perf-help";
463 /* get debugfs mount point from /proc/mounts */
464 debugfs_mount(NULL);
466 * "perf-xxxx" is the same as "perf xxxx", but we obviously:
468 * - cannot take flags in between the "perf" and the "xxxx".
469 * - cannot execute it externally (since it would just do
470 * the same thing over again)
472 * So we just directly call the internal command handler, and
473 * die if that one cannot handle it.
475 if (!prefixcmp(cmd, "perf-")) {
476 cmd += 5;
477 argv[0] = cmd;
478 handle_internal_command(argc, argv);
479 fprintf(stderr, "cannot handle %s internally", cmd);
480 goto out;
483 /* Look for flags.. */
484 argv++;
485 argc--;
486 handle_options(&argv, &argc, NULL);
487 commit_pager_choice();
488 set_buildid_dir();
490 if (argc > 0) {
491 if (!prefixcmp(argv[0], "--"))
492 argv[0] += 2;
493 } else {
494 /* The user didn't specify a command; give them help */
495 printf("\n usage: %s\n\n", perf_usage_string);
496 list_common_cmds_help();
497 printf("\n %s\n\n", perf_more_info_string);
498 goto out;
500 cmd = argv[0];
502 test_attr__init();
505 * We use PATH to find perf commands, but we prepend some higher
506 * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
507 * environment, and the $(perfexecdir) from the Makefile at build
508 * time.
510 setup_path();
512 * Block SIGWINCH notifications so that the thread that wants it can
513 * unblock and get syscalls like select interrupted instead of waiting
514 * forever while the signal goes to some other non interested thread.
516 pthread__block_sigwinch();
518 while (1) {
519 static int done_help;
520 static int was_alias;
522 was_alias = run_argv(&argc, &argv);
523 if (errno != ENOENT)
524 break;
526 if (was_alias) {
527 fprintf(stderr, "Expansion of alias '%s' failed; "
528 "'%s' is not a perf-command\n",
529 cmd, argv[0]);
530 goto out;
532 if (!done_help) {
533 cmd = argv[0] = help_unknown_cmd(cmd);
534 done_help = 1;
535 } else
536 break;
539 fprintf(stderr, "Failed to run command '%s': %s\n",
540 cmd, strerror(errno));
541 out:
542 return 1;