cregit-Linux how code gets into the kernel

Release 4.7 tools/perf/perf.c

Directory: tools/perf
/*
 * perf.c
 *
 * Performance analysis utility.
 *
 * This is the main hub from which the sub-commands (perf stat,
 * perf top, perf record, perf report, etc.) are started.
 */
#include "builtin.h"

#include "util/env.h"
#include <subcmd/exec-cmd.h>
#include "util/cache.h"
#include "util/quote.h"
#include <subcmd/run-command.h>
#include "util/parse-events.h"
#include <subcmd/parse-options.h>
#include "util/bpf-loader.h"
#include "util/debug.h"
#include <api/fs/fs.h>
#include <api/fs/tracing_path.h>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>


const char perf_usage_string[] =
	"perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";


const char perf_more_info_string[] =
	"See 'perf help COMMAND' for more information on a specific command.";


int use_browser = -1;

static int use_pager = -1;

const char *input_name;


struct cmd_struct {
	
const char *cmd;
	
int (*fn)(int, const char **, const char *);
	
int option;
};


static struct cmd_struct commands[] = {
	{ "buildid-cache", cmd_buildid_cache, 0 },
	{ "buildid-list", cmd_buildid_list, 0 },
	{ "config",	cmd_config,	0 },
	{ "diff",	cmd_diff,	0 },
	{ "evlist",	cmd_evlist,	0 },
	{ "help",	cmd_help,	0 },
	{ "list",	cmd_list,	0 },
	{ "record",	cmd_record,	0 },
	{ "report",	cmd_report,	0 },
	{ "bench",	cmd_bench,	0 },
	{ "stat",	cmd_stat,	0 },
	{ "timechart",	cmd_timechart,	0 },
	{ "top",	cmd_top,	0 },
	{ "annotate",	cmd_annotate,	0 },
	{ "version",	cmd_version,	0 },
	{ "script",	cmd_script,	0 },
	{ "sched",	cmd_sched,	0 },
#ifdef HAVE_LIBELF_SUPPORT
	{ "probe",	cmd_probe,	0 },
#endif
	{ "kmem",	cmd_kmem,	0 },
	{ "lock",	cmd_lock,	0 },
	{ "kvm",	cmd_kvm,	0 },
	{ "test",	cmd_test,	0 },
#ifdef HAVE_LIBAUDIT_SUPPORT
	{ "trace",	cmd_trace,	0 },
#endif
	{ "inject",	cmd_inject,	0 },
	{ "mem",	cmd_mem,	0 },
	{ "data",	cmd_data,	0 },
};


struct pager_config {
	
const char *cmd;
	
int val;
};


static int pager_command_config(const char *var, const char *value, void *data) { struct pager_config *c = data; if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd)) c->val = perf_config_bool(var, value); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
ingo molnaringo molnar63100.00%1100.00%
Total63100.00%1100.00%

/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
int check_pager_config(const char *cmd) { struct pager_config c; c.cmd = cmd; c.val = -1; perf_config(pager_command_config, &c); return c.val; }

Contributors

PersonTokensPropCommitsCommitProp
ingo molnaringo molnar40100.00%1100.00%
Total40100.00%1100.00%


static int browser_command_config(const char *var, const char *value, void *data) { struct pager_config *c = data; if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd)) c->val = perf_config_bool(var, value); if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd)) c->val = perf_config_bool(var, value) ? 2 : 0; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo6262.00%150.00%
namhyung kimnamhyung kim3838.00%150.00%
Total100100.00%2100.00%

/* * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk", * and -1 for "not specified" */
static int check_browser_config(const char *cmd) { struct pager_config c; c.cmd = cmd; c.val = -1; perf_config(browser_command_config, &c); return c.val; }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo3995.12%150.00%
namhyung kimnamhyung kim24.88%150.00%
Total41100.00%2100.00%


static void commit_pager_choice(void) { switch (use_pager) { case 0: setenv(PERF_PAGER_ENVIRONMENT, "cat", 1); break; case 1: /* setup_pager(); */ break; default: break; } }

Contributors

PersonTokensPropCommitsCommitProp
ingo molnaringo molnar3397.06%150.00%
josh poimboeufjosh poimboeuf12.94%150.00%
Total34100.00%2100.00%

struct option options[] = { OPT_ARGUMENT("help", "help"), OPT_ARGUMENT("version", "version"), OPT_ARGUMENT("exec-path", "exec-path"), OPT_ARGUMENT("html-path", "html-path"), OPT_ARGUMENT("paginate", "paginate"), OPT_ARGUMENT("no-pager", "no-pager"), OPT_ARGUMENT("perf-dir", "perf-dir"), OPT_ARGUMENT("work-tree", "work-tree"), OPT_ARGUMENT("debugfs-dir", "debugfs-dir"), OPT_ARGUMENT("buildid-dir", "buildid-dir"), OPT_ARGUMENT("list-cmds", "list-cmds"), OPT_ARGUMENT("list-opts", "list-opts"), OPT_ARGUMENT("debug", "debug"), OPT_END() };
static int handle_options(const char ***argv, int *argc, int *envchanged) { int handled = 0; while (*argc > 0) { const char *cmd = (*argv)[0]; if (cmd[0] != '-') break; /* * For legacy reasons, the "version" and "help" * commands can be written with "--" prepended * to make them look like flags. */ if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version")) break; /* * Shortcut for '-h' and '-v' options to invoke help * and version command. */ if (!strcmp(cmd, "-h")) { (*argv)[0] = "--help"; break; } if (!strcmp(cmd, "-v")) { (*argv)[0] = "--version"; break; } /* * Check remaining flags. */ if (!prefixcmp(cmd, CMD_EXEC_PATH)) { cmd += strlen(CMD_EXEC_PATH); if (*cmd == '=') set_argv_exec_path(cmd + 1); else { puts(get_argv_exec_path()); exit(0); } } else if (!strcmp(cmd, "--html-path")) { puts(system_path(PERF_HTML_PATH)); exit(0); } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { use_pager = 1; } else if (!strcmp(cmd, "--no-pager")) { use_pager = 0; if (envchanged) *envchanged = 1; } else if (!strcmp(cmd, "--perf-dir")) { if (*argc < 2) { fprintf(stderr, "No directory given for --perf-dir.\n"); usage(perf_usage_string); } setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1); if (envchanged) *envchanged = 1; (*argv)++; (*argc)--; handled++; } else if (!prefixcmp(cmd, CMD_PERF_DIR)) { setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1); if (envchanged) *envchanged = 1; } else if (!strcmp(cmd, "--work-tree")) { if (*argc < 2) { fprintf(stderr, "No directory given for --work-tree.\n"); usage(perf_usage_string); } setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1); if (envchanged) *envchanged = 1; (*argv)++; (*argc)--; } else if (!prefixcmp(cmd, CMD_WORK_TREE)) { setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1); if (envchanged) *envchanged = 1; } else if (!strcmp(cmd, "--debugfs-dir")) { if (*argc < 2) { fprintf(stderr, "No directory given for --debugfs-dir.\n"); usage(perf_usage_string); } tracing_path_set((*argv)[1]); if (envchanged) *envchanged = 1; (*argv)++; (*argc)--; } else if (!strcmp(cmd, "--buildid-dir")) { if (*argc < 2) { fprintf(stderr, "No directory given for --buildid-dir.\n"); usage(perf_usage_string); } set_buildid_dir((*argv)[1]); if (envchanged) *envchanged = 1; (*argv)++; (*argc)--; } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) { tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR)); fprintf(stderr, "dir: %s\n", tracing_path); if (envchanged) *envchanged = 1; } else if (!strcmp(cmd, "--list-cmds")) { unsigned int i; for (i = 0; i < ARRAY_SIZE(commands); i++) { struct cmd_struct *p = commands+i; printf("%s ", p->cmd); } putchar('\n'); exit(0); } else if (!strcmp(cmd, "--list-opts")) { unsigned int i; for (i = 0; i < ARRAY_SIZE(options)-1; i++) { struct option *p = options+i; printf("--%s ", p->long_name); } putchar('\n'); exit(0); } else if (!strcmp(cmd, "--debug")) { if (*argc < 2) { fprintf(stderr, "No variable specified for --debug.\n"); usage(perf_usage_string); } if (perf_debug_option((*argv)[1])) usage(perf_usage_string); (*argv)++; (*argc)--; } else { fprintf(stderr, "Unknown option: %s\n", cmd); usage(perf_usage_string); } (*argv)++; (*argc)--; handled++; } return handled; }

Contributors

PersonTokensPropCommitsCommitProp
ingo molnaringo molnar43650.40%17.69%
jiri olsajiri olsa18020.81%538.46%
jason baronjason baron9210.64%17.69%
yunlong songyunlong song708.09%215.38%
frederic weisbeckerfrederic weisbecker586.71%17.69%
vincent legollvincent legoll202.31%17.69%
arnaldo carvalho de meloarnaldo carvalho de melo70.81%17.69%
josh poimboeufjosh poimboeuf20.23%17.69%
Total865100.00%13100.00%


static int handle_alias(int *argcp, const char ***argv) { int envchanged = 0, ret = 0, saved_errno = errno; int count, option_count; const char **new_argv; const char *alias_command; char *alias_string; alias_command = (*argv)[0]; alias_string = alias_lookup(alias_command); if (alias_string) { if (alias_string[0] == '!') { if (*argcp > 1) { struct strbuf buf; if (strbuf_init(&buf, PATH_MAX) < 0 || strbuf_addstr(&buf, alias_string) < 0 || sq_quote_argv(&buf, (*argv) + 1, PATH_MAX) < 0) die("Failed to allocate memory."); free(alias_string); alias_string = buf.buf; } ret = system(alias_string + 1); if (ret >= 0 && WIFEXITED(ret) && WEXITSTATUS(ret) != 127) exit(WEXITSTATUS(ret)); die("Failed to run '%s' when expanding alias '%s'", alias_string + 1, alias_command); } count = split_cmdline(alias_string, &new_argv); if (count < 0) die("Bad alias.%s string", alias_command); option_count = handle_options(&new_argv, &count, &envchanged); if (envchanged) die("alias '%s' changes environment variables\n" "You can use '!perf' in the alias to do this.", alias_command); memmove(new_argv - option_count, new_argv, count * sizeof(char *)); new_argv -= option_count; if (count < 1) die("empty alias for %s", alias_command); if (!strcmp(alias_command, new_argv[0])) die("recursive alias: %s", alias_command); new_argv = realloc(new_argv, sizeof(char *) * (count + *argcp + 1)); /* insert after command name */ memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp); new_argv[count + *argcp] = NULL; *argv = new_argv; *argcp += count - 1; ret = 1; } errno = saved_errno; return ret; }

Contributors

PersonTokensPropCommitsCommitProp
ingo molnaringo molnar36396.03%150.00%
masami hiramatsumasami hiramatsu153.97%150.00%
Total378100.00%2100.00%

const char perf_version_string[] = PERF_VERSION; #define RUN_SETUP (1<<0) #define USE_PAGER (1<<1) /* * require working tree to be present -- anything uses this needs * RUN_SETUP for reading from the configuration file. */ #define NEED_WORK_TREE (1<<2)
static int run_builtin(struct cmd_struct *p, int argc, const char **argv) { int status; struct stat st; const char *prefix; char sbuf[STRERR_BUFSIZE]; prefix = NULL; if (p->option & RUN_SETUP) prefix = NULL; /* setup_perf_directory(); */ if (use_browser == -1) use_browser = check_browser_config(p->cmd); if (use_pager == -1 && p->option & RUN_SETUP) use_pager = check_pager_config(p->cmd); if (use_pager == -1 && p->option & USE_PAGER) use_pager = 1; commit_pager_choice(); perf_env__set_cmdline(&perf_env, argc, argv); status = p->fn(argc, argv, prefix); exit_browser(status); perf_env__exit(&perf_env); bpf__clear(); if (status) return status & 0xff; /* Somebody closed stdout? */ if (fstat(fileno(stdout), &st)) return 0; /* Ignore write errors for pipes and sockets.. */ if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) return 0; status = 1; /* Check for ENOSPC and EIO errors.. */ if (fflush(stdout)) { fprintf(stderr, "write failure on standard output: %s", strerror_r(errno, sbuf, sizeof(sbuf))); goto out; } if (ferror(stdout)) { fprintf(stderr, "unknown write failure on standard output"); goto out; } if (fclose(stdout)) { fprintf(stderr, "close failed on standard output: %s", strerror_r(errno, sbuf, sizeof(sbuf))); goto out; } status = 0; out: return status; }

Contributors

PersonTokensPropCommitsCommitProp
ingo molnaringo molnar19466.67%111.11%
arnaldo carvalho de meloarnaldo carvalho de melo6120.96%444.44%
masami hiramatsumasami hiramatsu227.56%111.11%
josh poimboeufjosh poimboeuf103.44%111.11%
wang nanwang nan31.03%111.11%
namhyung kimnamhyung kim10.34%111.11%
Total291100.00%9100.00%


static void handle_internal_command(int argc, const char **argv) { const char *cmd = argv[0]; unsigned int i; static const char ext[] = STRIP_EXTENSION; if (sizeof(ext) > 1) { i = strlen(argv[0]) - strlen(ext); if (i > 0 && !strcmp(argv[0] + i, ext)) { char *argv0 = strdup(argv[0]); argv[0] = cmd = argv0; argv0[i] = '\0'; } } /* Turn "perf cmd --help" into "perf help cmd" */ if (argc > 1 && !strcmp(argv[1], "--help")) { argv[1] = argv[0]; argv[0] = cmd = "help"; } for (i = 0; i < ARRAY_SIZE(commands); i++) { struct cmd_struct *p = commands+i; if (strcmp(p->cmd, cmd)) continue; exit(run_builtin(p, argc, argv)); } }

Contributors

PersonTokensPropCommitsCommitProp
ingo molnaringo molnar202100.00%2100.00%
Total202100.00%2100.00%


static void execv_dashed_external(const char **argv) { char *cmd; const char *tmp; int status; if (asprintf(&cmd, "perf-%s", argv[0]) < 0) goto do_die; /* * argv[0] must be the perf command, but the argv array * belongs to the caller, and may be reused in * subsequent loop iterations. Save argv[0] and * restore it on error. */ tmp = argv[0]; argv[0] = cmd; /* * if we fail because the command is not found, it is * OK to return. Otherwise, we just pass along the status code. */ status = run_command_v_opt(argv, 0); if (status != -ERR_RUN_COMMAND_EXEC) { if (IS_RUN_COMMAND_ERR(status)) { do_die: die("unable to run '%s'", argv[0]); } exit(-status); } errno = ENOENT; /* as if we called execvp */ argv[0] = tmp; zfree(&cmd); }

Contributors

PersonTokensPropCommitsCommitProp
ingo molnaringo molnar10786.99%150.00%
arnaldo carvalho de meloarnaldo carvalho de melo1613.01%150.00%
Total123100.00%2100.00%


static int run_argv(int *argcp, const char ***argv) { int done_alias = 0; while (1) { /* See if it's an internal command */ handle_internal_command(*argcp, *argv); /* .. then try the external ones */ execv_dashed_external(*argv); /* It could be an alias -- this works around the insanity * of overriding "perf log" with "perf show" by having * alias.log = show */ if (done_alias || !handle_alias(argcp, argv)) break; done_alias = 1; } return done_alias; }

Contributors

PersonTokensPropCommitsCommitProp
ingo molnaringo molnar66100.00%1100.00%
Total66100.00%1100.00%


static void pthread__block_sigwinch(void) { sigset_t set; sigemptyset(&set); sigaddset(&set, SIGWINCH); pthread_sigmask(SIG_BLOCK, &set, NULL); }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo35100.00%1100.00%
Total35100.00%1100.00%


void pthread__unblock_sigwinch(void) { sigset_t set; sigemptyset(&set); sigaddset(&set, SIGWINCH); pthread_sigmask(SIG_UNBLOCK, &set, NULL); }

Contributors

PersonTokensPropCommitsCommitProp
arnaldo carvalho de meloarnaldo carvalho de melo34100.00%1100.00%
Total34100.00%1100.00%


int main(int argc, const char **argv) { const char *cmd; char sbuf[STRERR_BUFSIZE]; int value; /* libsubcmd init */ exec_cmd_init("perf", PREFIX, PERF_EXEC_PATH, EXEC_PATH_ENVIRONMENT); pager_init(PERF_PAGER_ENVIRONMENT); /* The page_size is placed in util object. */ page_size = sysconf(_SC_PAGE_SIZE); cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0) sysctl_perf_event_max_stack = value; if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0) sysctl_perf_event_max_contexts_per_stack = value; cmd = extract_argv0_path(argv[0]); if (!cmd) cmd = "perf-help"; srandom(time(NULL)); perf_config(perf_default_config, NULL); set_buildid_dir(NULL); /* get debugfs/tracefs mount point from /proc/mounts */ tracing_path_mount(); /* * "perf-xxxx" is the same as "perf xxxx", but we obviously: * * - cannot take flags in between the "perf" and the "xxxx". * - cannot execute it externally (since it would just do * the same thing over again) * * So we just directly call the internal command handler, and * die if that one cannot handle it. */ if (!prefixcmp(cmd, "perf-")) { cmd += 5; argv[0] = cmd; handle_internal_command(argc, argv); fprintf(stderr, "cannot handle %s internally", cmd); goto out; } if (!prefixcmp(cmd, "trace")) { #ifdef HAVE_LIBAUDIT_SUPPORT setup_path(); argv[0] = "trace"; return cmd_trace(argc, argv, NULL); #else fprintf(stderr, "trace command not available: missing audit-libs devel package at build time.\n"); goto out; #endif } /* Look for flags.. */ argv++; argc--; handle_options(&argv, &argc, NULL); commit_pager_choice(); if (argc > 0) { if (!prefixcmp(argv[0], "--")) argv[0] += 2; } else { /* The user didn't specify a command; give them help */ printf("\n usage: %s\n\n", perf_usage_string); list_common_cmds_help(); printf("\n %s\n\n", perf_more_info_string); goto out; } cmd = argv[0]; test_attr__init(); /* * We use PATH to find perf commands, but we prepend some higher * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH * environment, and the $(perfexecdir) from the Makefile at build * time. */ setup_path(); /* * Block SIGWINCH notifications so that the thread that wants it can * unblock and get syscalls like select interrupted instead of waiting * forever while the signal goes to some other non interested thread. */ pthread__block_sigwinch(); perf_debug_setup(); while (1) { static int done_help; int was_alias = run_argv(&argc, &argv); if (errno != ENOENT) break; if (was_alias) { fprintf(stderr, "Expansion of alias '%s' failed; " "'%s' is not a perf-command\n", cmd, argv[0]); goto out; } if (!done_help) { cmd = argv[0] = help_unknown_cmd(cmd); done_help = 1; } else break; } fprintf(stderr, "Failed to run command '%s': %s\n", cmd, strerror_r(errno, sbuf, sizeof(sbuf))); out: return 1; }

Contributors

PersonTokensPropCommitsCommitProp
ingo molnaringo molnar24256.94%28.00%
arnaldo carvalho de meloarnaldo carvalho de melo11126.12%832.00%
josh poimboeufjosh poimboeuf184.24%28.00%
masami hiramatsumasami hiramatsu143.29%14.00%
jiri olsajiri olsa102.35%520.00%
namhyung kimnamhyung kim81.88%14.00%
wang nanwang nan71.65%14.00%
don zickusdon zickus71.65%14.00%
taeung songtaeung song51.18%14.00%
jason baronjason baron10.24%14.00%
uwe kleine-koeniguwe kleine-koenig10.24%14.00%
peter zijlstrapeter zijlstra10.24%14.00%
Total425100.00%25100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
ingo molnaringo molnar181657.32%711.67%
arnaldo carvalho de meloarnaldo carvalho de melo38512.15%1423.33%
frederic weisbeckerfrederic weisbecker2738.62%11.67%
jiri olsajiri olsa2036.41%1220.00%
yunlong songyunlong song1735.46%23.33%
jason baronjason baron983.09%11.67%
namhyung kimnamhyung kim642.02%610.00%
masami hiramatsumasami hiramatsu511.61%23.33%
josh poimboeufjosh poimboeuf341.07%46.67%
vincent legollvincent legoll200.63%11.67%
wang nanwang nan130.41%23.33%
taeung songtaeung song130.41%23.33%
stephane eranianstephane eranian80.25%11.67%
don zickusdon zickus70.22%11.67%
feng tangfeng tang50.16%11.67%
irina tirdeairina tirdea30.09%11.67%
uwe kleine-koeniguwe kleine-koenig10.03%11.67%
peter zijlstrapeter zijlstra10.03%11.67%
Total3168100.00%60100.00%
Directory: tools/perf
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
{% endraw %}