perf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * perf.c
  3. *
  4. * Performance analysis utility.
  5. *
  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"
  10. #include "util/env.h"
  11. #include <subcmd/exec-cmd.h>
  12. #include "util/config.h"
  13. #include "util/quote.h"
  14. #include <subcmd/run-command.h>
  15. #include "util/parse-events.h"
  16. #include <subcmd/parse-options.h>
  17. #include "util/bpf-loader.h"
  18. #include "util/debug.h"
  19. #include "util/event.h"
  20. #include <api/fs/fs.h>
  21. #include <api/fs/tracing_path.h>
  22. #include <errno.h>
  23. #include <pthread.h>
  24. #include <signal.h>
  25. #include <stdlib.h>
  26. #include <time.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <unistd.h>
  30. #include <linux/kernel.h>
  31. const char perf_usage_string[] =
  32. "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
  33. const char perf_more_info_string[] =
  34. "See 'perf help COMMAND' for more information on a specific command.";
  35. static int use_pager = -1;
  36. const char *input_name;
  37. struct cmd_struct {
  38. const char *cmd;
  39. int (*fn)(int, const char **);
  40. int option;
  41. };
  42. static struct cmd_struct commands[] = {
  43. { "buildid-cache", cmd_buildid_cache, 0 },
  44. { "buildid-list", cmd_buildid_list, 0 },
  45. { "config", cmd_config, 0 },
  46. { "c2c", cmd_c2c, 0 },
  47. { "diff", cmd_diff, 0 },
  48. { "evlist", cmd_evlist, 0 },
  49. { "help", cmd_help, 0 },
  50. { "kallsyms", cmd_kallsyms, 0 },
  51. { "list", cmd_list, 0 },
  52. { "record", cmd_record, 0 },
  53. { "report", cmd_report, 0 },
  54. { "bench", cmd_bench, 0 },
  55. { "stat", cmd_stat, 0 },
  56. { "timechart", cmd_timechart, 0 },
  57. { "top", cmd_top, 0 },
  58. { "annotate", cmd_annotate, 0 },
  59. { "version", cmd_version, 0 },
  60. { "script", cmd_script, 0 },
  61. { "sched", cmd_sched, 0 },
  62. #ifdef HAVE_LIBELF_SUPPORT
  63. { "probe", cmd_probe, 0 },
  64. #endif
  65. { "kmem", cmd_kmem, 0 },
  66. { "lock", cmd_lock, 0 },
  67. { "kvm", cmd_kvm, 0 },
  68. { "test", cmd_test, 0 },
  69. #ifdef HAVE_LIBAUDIT_SUPPORT
  70. { "trace", cmd_trace, 0 },
  71. #endif
  72. { "inject", cmd_inject, 0 },
  73. { "mem", cmd_mem, 0 },
  74. { "data", cmd_data, 0 },
  75. { "ftrace", cmd_ftrace, 0 },
  76. };
  77. struct pager_config {
  78. const char *cmd;
  79. int val;
  80. };
  81. static int pager_command_config(const char *var, const char *value, void *data)
  82. {
  83. struct pager_config *c = data;
  84. if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
  85. c->val = perf_config_bool(var, value);
  86. return 0;
  87. }
  88. /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
  89. static int check_pager_config(const char *cmd)
  90. {
  91. int err;
  92. struct pager_config c;
  93. c.cmd = cmd;
  94. c.val = -1;
  95. err = perf_config(pager_command_config, &c);
  96. return err ?: c.val;
  97. }
  98. static int browser_command_config(const char *var, const char *value, void *data)
  99. {
  100. struct pager_config *c = data;
  101. if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
  102. c->val = perf_config_bool(var, value);
  103. if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
  104. c->val = perf_config_bool(var, value) ? 2 : 0;
  105. return 0;
  106. }
  107. /*
  108. * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
  109. * and -1 for "not specified"
  110. */
  111. static int check_browser_config(const char *cmd)
  112. {
  113. int err;
  114. struct pager_config c;
  115. c.cmd = cmd;
  116. c.val = -1;
  117. err = perf_config(browser_command_config, &c);
  118. return err ?: c.val;
  119. }
  120. static void commit_pager_choice(void)
  121. {
  122. switch (use_pager) {
  123. case 0:
  124. setenv(PERF_PAGER_ENVIRONMENT, "cat", 1);
  125. break;
  126. case 1:
  127. /* setup_pager(); */
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. struct option options[] = {
  134. OPT_ARGUMENT("help", "help"),
  135. OPT_ARGUMENT("version", "version"),
  136. OPT_ARGUMENT("exec-path", "exec-path"),
  137. OPT_ARGUMENT("html-path", "html-path"),
  138. OPT_ARGUMENT("paginate", "paginate"),
  139. OPT_ARGUMENT("no-pager", "no-pager"),
  140. OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
  141. OPT_ARGUMENT("buildid-dir", "buildid-dir"),
  142. OPT_ARGUMENT("list-cmds", "list-cmds"),
  143. OPT_ARGUMENT("list-opts", "list-opts"),
  144. OPT_ARGUMENT("debug", "debug"),
  145. OPT_END()
  146. };
  147. static int handle_options(const char ***argv, int *argc, int *envchanged)
  148. {
  149. int handled = 0;
  150. while (*argc > 0) {
  151. const char *cmd = (*argv)[0];
  152. if (cmd[0] != '-')
  153. break;
  154. /*
  155. * For legacy reasons, the "version" and "help"
  156. * commands can be written with "--" prepended
  157. * to make them look like flags.
  158. */
  159. if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
  160. break;
  161. /*
  162. * Shortcut for '-h' and '-v' options to invoke help
  163. * and version command.
  164. */
  165. if (!strcmp(cmd, "-h")) {
  166. (*argv)[0] = "--help";
  167. break;
  168. }
  169. if (!strcmp(cmd, "-v")) {
  170. (*argv)[0] = "--version";
  171. break;
  172. }
  173. /*
  174. * Check remaining flags.
  175. */
  176. if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
  177. cmd += strlen(CMD_EXEC_PATH);
  178. if (*cmd == '=')
  179. set_argv_exec_path(cmd + 1);
  180. else {
  181. puts(get_argv_exec_path());
  182. exit(0);
  183. }
  184. } else if (!strcmp(cmd, "--html-path")) {
  185. puts(system_path(PERF_HTML_PATH));
  186. exit(0);
  187. } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
  188. use_pager = 1;
  189. } else if (!strcmp(cmd, "--no-pager")) {
  190. use_pager = 0;
  191. if (envchanged)
  192. *envchanged = 1;
  193. } else if (!strcmp(cmd, "--debugfs-dir")) {
  194. if (*argc < 2) {
  195. fprintf(stderr, "No directory given for --debugfs-dir.\n");
  196. usage(perf_usage_string);
  197. }
  198. tracing_path_set((*argv)[1]);
  199. if (envchanged)
  200. *envchanged = 1;
  201. (*argv)++;
  202. (*argc)--;
  203. } else if (!strcmp(cmd, "--buildid-dir")) {
  204. if (*argc < 2) {
  205. fprintf(stderr, "No directory given for --buildid-dir.\n");
  206. usage(perf_usage_string);
  207. }
  208. set_buildid_dir((*argv)[1]);
  209. if (envchanged)
  210. *envchanged = 1;
  211. (*argv)++;
  212. (*argc)--;
  213. } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
  214. tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR));
  215. fprintf(stderr, "dir: %s\n", tracing_path);
  216. if (envchanged)
  217. *envchanged = 1;
  218. } else if (!strcmp(cmd, "--list-cmds")) {
  219. unsigned int i;
  220. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  221. struct cmd_struct *p = commands+i;
  222. printf("%s ", p->cmd);
  223. }
  224. putchar('\n');
  225. exit(0);
  226. } else if (!strcmp(cmd, "--list-opts")) {
  227. unsigned int i;
  228. for (i = 0; i < ARRAY_SIZE(options)-1; i++) {
  229. struct option *p = options+i;
  230. printf("--%s ", p->long_name);
  231. }
  232. putchar('\n');
  233. exit(0);
  234. } else if (!strcmp(cmd, "--debug")) {
  235. if (*argc < 2) {
  236. fprintf(stderr, "No variable specified for --debug.\n");
  237. usage(perf_usage_string);
  238. }
  239. if (perf_debug_option((*argv)[1]))
  240. usage(perf_usage_string);
  241. (*argv)++;
  242. (*argc)--;
  243. } else {
  244. fprintf(stderr, "Unknown option: %s\n", cmd);
  245. usage(perf_usage_string);
  246. }
  247. (*argv)++;
  248. (*argc)--;
  249. handled++;
  250. }
  251. return handled;
  252. }
  253. #define RUN_SETUP (1<<0)
  254. #define USE_PAGER (1<<1)
  255. static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
  256. {
  257. int status;
  258. struct stat st;
  259. char sbuf[STRERR_BUFSIZE];
  260. if (use_browser == -1)
  261. use_browser = check_browser_config(p->cmd);
  262. if (use_pager == -1 && p->option & RUN_SETUP)
  263. use_pager = check_pager_config(p->cmd);
  264. if (use_pager == -1 && p->option & USE_PAGER)
  265. use_pager = 1;
  266. commit_pager_choice();
  267. perf_env__set_cmdline(&perf_env, argc, argv);
  268. status = p->fn(argc, argv);
  269. perf_config__exit();
  270. exit_browser(status);
  271. perf_env__exit(&perf_env);
  272. bpf__clear();
  273. if (status)
  274. return status & 0xff;
  275. /* Somebody closed stdout? */
  276. if (fstat(fileno(stdout), &st))
  277. return 0;
  278. /* Ignore write errors for pipes and sockets.. */
  279. if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
  280. return 0;
  281. status = 1;
  282. /* Check for ENOSPC and EIO errors.. */
  283. if (fflush(stdout)) {
  284. fprintf(stderr, "write failure on standard output: %s",
  285. str_error_r(errno, sbuf, sizeof(sbuf)));
  286. goto out;
  287. }
  288. if (ferror(stdout)) {
  289. fprintf(stderr, "unknown write failure on standard output");
  290. goto out;
  291. }
  292. if (fclose(stdout)) {
  293. fprintf(stderr, "close failed on standard output: %s",
  294. str_error_r(errno, sbuf, sizeof(sbuf)));
  295. goto out;
  296. }
  297. status = 0;
  298. out:
  299. return status;
  300. }
  301. static void handle_internal_command(int argc, const char **argv)
  302. {
  303. const char *cmd = argv[0];
  304. unsigned int i;
  305. /* Turn "perf cmd --help" into "perf help cmd" */
  306. if (argc > 1 && !strcmp(argv[1], "--help")) {
  307. argv[1] = argv[0];
  308. argv[0] = cmd = "help";
  309. }
  310. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  311. struct cmd_struct *p = commands+i;
  312. if (strcmp(p->cmd, cmd))
  313. continue;
  314. exit(run_builtin(p, argc, argv));
  315. }
  316. }
  317. static void execv_dashed_external(const char **argv)
  318. {
  319. char *cmd;
  320. const char *tmp;
  321. int status;
  322. if (asprintf(&cmd, "perf-%s", argv[0]) < 0)
  323. goto do_die;
  324. /*
  325. * argv[0] must be the perf command, but the argv array
  326. * belongs to the caller, and may be reused in
  327. * subsequent loop iterations. Save argv[0] and
  328. * restore it on error.
  329. */
  330. tmp = argv[0];
  331. argv[0] = cmd;
  332. /*
  333. * if we fail because the command is not found, it is
  334. * OK to return. Otherwise, we just pass along the status code.
  335. */
  336. status = run_command_v_opt(argv, 0);
  337. if (status != -ERR_RUN_COMMAND_EXEC) {
  338. if (IS_RUN_COMMAND_ERR(status)) {
  339. do_die:
  340. pr_err("FATAL: unable to run '%s'", argv[0]);
  341. status = -128;
  342. }
  343. exit(-status);
  344. }
  345. errno = ENOENT; /* as if we called execvp */
  346. argv[0] = tmp;
  347. zfree(&cmd);
  348. }
  349. static int run_argv(int *argcp, const char ***argv)
  350. {
  351. /* See if it's an internal command */
  352. handle_internal_command(*argcp, *argv);
  353. /* .. then try the external ones */
  354. execv_dashed_external(*argv);
  355. return 0;
  356. }
  357. static void pthread__block_sigwinch(void)
  358. {
  359. sigset_t set;
  360. sigemptyset(&set);
  361. sigaddset(&set, SIGWINCH);
  362. pthread_sigmask(SIG_BLOCK, &set, NULL);
  363. }
  364. void pthread__unblock_sigwinch(void)
  365. {
  366. sigset_t set;
  367. sigemptyset(&set);
  368. sigaddset(&set, SIGWINCH);
  369. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  370. }
  371. #ifdef _SC_LEVEL1_DCACHE_LINESIZE
  372. #define cache_line_size(cacheline_sizep) *cacheline_sizep = sysconf(_SC_LEVEL1_DCACHE_LINESIZE)
  373. #else
  374. static void cache_line_size(int *cacheline_sizep)
  375. {
  376. if (sysfs__read_int("devices/system/cpu/cpu0/cache/index0/coherency_line_size", cacheline_sizep))
  377. pr_debug("cannot determine cache line size");
  378. }
  379. #endif
  380. int main(int argc, const char **argv)
  381. {
  382. int err;
  383. const char *cmd;
  384. char sbuf[STRERR_BUFSIZE];
  385. int value;
  386. /* libsubcmd init */
  387. exec_cmd_init("perf", PREFIX, PERF_EXEC_PATH, EXEC_PATH_ENVIRONMENT);
  388. pager_init(PERF_PAGER_ENVIRONMENT);
  389. /* The page_size is placed in util object. */
  390. page_size = sysconf(_SC_PAGE_SIZE);
  391. cache_line_size(&cacheline_size);
  392. if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
  393. sysctl_perf_event_max_stack = value;
  394. if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0)
  395. sysctl_perf_event_max_contexts_per_stack = value;
  396. cmd = extract_argv0_path(argv[0]);
  397. if (!cmd)
  398. cmd = "perf-help";
  399. srandom(time(NULL));
  400. perf_config__init();
  401. err = perf_config(perf_default_config, NULL);
  402. if (err)
  403. return err;
  404. set_buildid_dir(NULL);
  405. /* get debugfs/tracefs mount point from /proc/mounts */
  406. tracing_path_mount();
  407. /*
  408. * "perf-xxxx" is the same as "perf xxxx", but we obviously:
  409. *
  410. * - cannot take flags in between the "perf" and the "xxxx".
  411. * - cannot execute it externally (since it would just do
  412. * the same thing over again)
  413. *
  414. * So we just directly call the internal command handler, and
  415. * die if that one cannot handle it.
  416. */
  417. if (!prefixcmp(cmd, "perf-")) {
  418. cmd += 5;
  419. argv[0] = cmd;
  420. handle_internal_command(argc, argv);
  421. fprintf(stderr, "cannot handle %s internally", cmd);
  422. goto out;
  423. }
  424. if (!prefixcmp(cmd, "trace")) {
  425. #ifdef HAVE_LIBAUDIT_SUPPORT
  426. setup_path();
  427. argv[0] = "trace";
  428. return cmd_trace(argc, argv);
  429. #else
  430. fprintf(stderr,
  431. "trace command not available: missing audit-libs devel package at build time.\n");
  432. goto out;
  433. #endif
  434. }
  435. /* Look for flags.. */
  436. argv++;
  437. argc--;
  438. handle_options(&argv, &argc, NULL);
  439. commit_pager_choice();
  440. if (argc > 0) {
  441. if (!prefixcmp(argv[0], "--"))
  442. argv[0] += 2;
  443. } else {
  444. /* The user didn't specify a command; give them help */
  445. printf("\n usage: %s\n\n", perf_usage_string);
  446. list_common_cmds_help();
  447. printf("\n %s\n\n", perf_more_info_string);
  448. goto out;
  449. }
  450. cmd = argv[0];
  451. test_attr__init();
  452. /*
  453. * We use PATH to find perf commands, but we prepend some higher
  454. * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
  455. * environment, and the $(perfexecdir) from the Makefile at build
  456. * time.
  457. */
  458. setup_path();
  459. /*
  460. * Block SIGWINCH notifications so that the thread that wants it can
  461. * unblock and get syscalls like select interrupted instead of waiting
  462. * forever while the signal goes to some other non interested thread.
  463. */
  464. pthread__block_sigwinch();
  465. perf_debug_setup();
  466. while (1) {
  467. static int done_help;
  468. run_argv(&argc, &argv);
  469. if (errno != ENOENT)
  470. break;
  471. if (!done_help) {
  472. cmd = argv[0] = help_unknown_cmd(cmd);
  473. done_help = 1;
  474. } else
  475. break;
  476. }
  477. fprintf(stderr, "Failed to run command '%s': %s\n",
  478. cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
  479. out:
  480. return 1;
  481. }