perf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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/cache.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 <api/fs/tracing_path.h>
  20. #include <pthread.h>
  21. #include <stdlib.h>
  22. #include <time.h>
  23. const char perf_usage_string[] =
  24. "perf [--version] [--help] [OPTIONS] COMMAND [ARGS]";
  25. const char perf_more_info_string[] =
  26. "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;
  30. struct cmd_struct {
  31. const char *cmd;
  32. int (*fn)(int, const char **, const char *);
  33. int option;
  34. };
  35. static struct cmd_struct commands[] = {
  36. { "buildid-cache", cmd_buildid_cache, 0 },
  37. { "buildid-list", cmd_buildid_list, 0 },
  38. { "config", cmd_config, 0 },
  39. { "diff", cmd_diff, 0 },
  40. { "evlist", cmd_evlist, 0 },
  41. { "help", cmd_help, 0 },
  42. { "list", cmd_list, 0 },
  43. { "record", cmd_record, 0 },
  44. { "report", cmd_report, 0 },
  45. { "bench", cmd_bench, 0 },
  46. { "stat", cmd_stat, 0 },
  47. { "timechart", cmd_timechart, 0 },
  48. { "top", cmd_top, 0 },
  49. { "annotate", cmd_annotate, 0 },
  50. { "version", cmd_version, 0 },
  51. { "script", cmd_script, 0 },
  52. { "sched", cmd_sched, 0 },
  53. #ifdef HAVE_LIBELF_SUPPORT
  54. { "probe", cmd_probe, 0 },
  55. #endif
  56. { "kmem", cmd_kmem, 0 },
  57. { "lock", cmd_lock, 0 },
  58. { "kvm", cmd_kvm, 0 },
  59. { "test", cmd_test, 0 },
  60. #ifdef HAVE_LIBAUDIT_SUPPORT
  61. { "trace", cmd_trace, 0 },
  62. #endif
  63. { "inject", cmd_inject, 0 },
  64. { "mem", cmd_mem, 0 },
  65. { "data", cmd_data, 0 },
  66. };
  67. struct pager_config {
  68. const char *cmd;
  69. int val;
  70. };
  71. static int pager_command_config(const char *var, const char *value, void *data)
  72. {
  73. struct pager_config *c = data;
  74. if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
  75. c->val = perf_config_bool(var, value);
  76. return 0;
  77. }
  78. /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
  79. int check_pager_config(const char *cmd)
  80. {
  81. struct pager_config c;
  82. c.cmd = cmd;
  83. c.val = -1;
  84. perf_config(pager_command_config, &c);
  85. return c.val;
  86. }
  87. static int browser_command_config(const char *var, const char *value, void *data)
  88. {
  89. struct pager_config *c = data;
  90. if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
  91. c->val = perf_config_bool(var, value);
  92. if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
  93. c->val = perf_config_bool(var, value) ? 2 : 0;
  94. return 0;
  95. }
  96. /*
  97. * returns 0 for "no tui", 1 for "use tui", 2 for "use gtk",
  98. * and -1 for "not specified"
  99. */
  100. static int check_browser_config(const char *cmd)
  101. {
  102. struct pager_config c;
  103. c.cmd = cmd;
  104. c.val = -1;
  105. perf_config(browser_command_config, &c);
  106. return c.val;
  107. }
  108. static void commit_pager_choice(void)
  109. {
  110. switch (use_pager) {
  111. case 0:
  112. setenv(PERF_PAGER_ENVIRONMENT, "cat", 1);
  113. break;
  114. case 1:
  115. /* setup_pager(); */
  116. break;
  117. default:
  118. break;
  119. }
  120. }
  121. struct option options[] = {
  122. OPT_ARGUMENT("help", "help"),
  123. OPT_ARGUMENT("version", "version"),
  124. OPT_ARGUMENT("exec-path", "exec-path"),
  125. OPT_ARGUMENT("html-path", "html-path"),
  126. OPT_ARGUMENT("paginate", "paginate"),
  127. OPT_ARGUMENT("no-pager", "no-pager"),
  128. OPT_ARGUMENT("perf-dir", "perf-dir"),
  129. OPT_ARGUMENT("work-tree", "work-tree"),
  130. OPT_ARGUMENT("debugfs-dir", "debugfs-dir"),
  131. OPT_ARGUMENT("buildid-dir", "buildid-dir"),
  132. OPT_ARGUMENT("list-cmds", "list-cmds"),
  133. OPT_ARGUMENT("list-opts", "list-opts"),
  134. OPT_ARGUMENT("debug", "debug"),
  135. OPT_END()
  136. };
  137. static int handle_options(const char ***argv, int *argc, int *envchanged)
  138. {
  139. int handled = 0;
  140. while (*argc > 0) {
  141. const char *cmd = (*argv)[0];
  142. if (cmd[0] != '-')
  143. break;
  144. /*
  145. * For legacy reasons, the "version" and "help"
  146. * commands can be written with "--" prepended
  147. * to make them look like flags.
  148. */
  149. if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
  150. break;
  151. /*
  152. * Shortcut for '-h' and '-v' options to invoke help
  153. * and version command.
  154. */
  155. if (!strcmp(cmd, "-h")) {
  156. (*argv)[0] = "--help";
  157. break;
  158. }
  159. if (!strcmp(cmd, "-v")) {
  160. (*argv)[0] = "--version";
  161. break;
  162. }
  163. /*
  164. * Check remaining flags.
  165. */
  166. if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
  167. cmd += strlen(CMD_EXEC_PATH);
  168. if (*cmd == '=')
  169. set_argv_exec_path(cmd + 1);
  170. else {
  171. puts(get_argv_exec_path());
  172. exit(0);
  173. }
  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);
  187. }
  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);
  202. }
  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);
  216. }
  217. tracing_path_set((*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);
  226. }
  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. tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR));
  234. fprintf(stderr, "dir: %s\n", tracing_path);
  235. if (envchanged)
  236. *envchanged = 1;
  237. } else if (!strcmp(cmd, "--list-cmds")) {
  238. unsigned int i;
  239. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  240. struct cmd_struct *p = commands+i;
  241. printf("%s ", p->cmd);
  242. }
  243. putchar('\n');
  244. exit(0);
  245. } else if (!strcmp(cmd, "--list-opts")) {
  246. unsigned int i;
  247. for (i = 0; i < ARRAY_SIZE(options)-1; i++) {
  248. struct option *p = options+i;
  249. printf("--%s ", p->long_name);
  250. }
  251. putchar('\n');
  252. exit(0);
  253. } else if (!strcmp(cmd, "--debug")) {
  254. if (*argc < 2) {
  255. fprintf(stderr, "No variable specified for --debug.\n");
  256. usage(perf_usage_string);
  257. }
  258. if (perf_debug_option((*argv)[1]))
  259. usage(perf_usage_string);
  260. (*argv)++;
  261. (*argc)--;
  262. } else {
  263. fprintf(stderr, "Unknown option: %s\n", cmd);
  264. usage(perf_usage_string);
  265. }
  266. (*argv)++;
  267. (*argc)--;
  268. handled++;
  269. }
  270. return handled;
  271. }
  272. static int handle_alias(int *argcp, const char ***argv)
  273. {
  274. int envchanged = 0, ret = 0, saved_errno = errno;
  275. int count, option_count;
  276. const char **new_argv;
  277. const char *alias_command;
  278. char *alias_string;
  279. alias_command = (*argv)[0];
  280. alias_string = alias_lookup(alias_command);
  281. if (alias_string) {
  282. if (alias_string[0] == '!') {
  283. if (*argcp > 1) {
  284. struct strbuf buf;
  285. strbuf_init(&buf, PATH_MAX);
  286. strbuf_addstr(&buf, alias_string);
  287. sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
  288. free(alias_string);
  289. alias_string = buf.buf;
  290. }
  291. ret = system(alias_string + 1);
  292. if (ret >= 0 && WIFEXITED(ret) &&
  293. WEXITSTATUS(ret) != 127)
  294. exit(WEXITSTATUS(ret));
  295. die("Failed to run '%s' when expanding alias '%s'",
  296. alias_string + 1, alias_command);
  297. }
  298. count = split_cmdline(alias_string, &new_argv);
  299. if (count < 0)
  300. die("Bad alias.%s string", alias_command);
  301. option_count = handle_options(&new_argv, &count, &envchanged);
  302. if (envchanged)
  303. die("alias '%s' changes environment variables\n"
  304. "You can use '!perf' in the alias to do this.",
  305. alias_command);
  306. memmove(new_argv - option_count, new_argv,
  307. count * sizeof(char *));
  308. new_argv -= option_count;
  309. if (count < 1)
  310. die("empty alias for %s", alias_command);
  311. if (!strcmp(alias_command, new_argv[0]))
  312. die("recursive alias: %s", alias_command);
  313. new_argv = realloc(new_argv, sizeof(char *) *
  314. (count + *argcp + 1));
  315. /* insert after command name */
  316. memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
  317. new_argv[count + *argcp] = NULL;
  318. *argv = new_argv;
  319. *argcp += count - 1;
  320. ret = 1;
  321. }
  322. errno = saved_errno;
  323. return ret;
  324. }
  325. const char perf_version_string[] = PERF_VERSION;
  326. #define RUN_SETUP (1<<0)
  327. #define USE_PAGER (1<<1)
  328. /*
  329. * require working tree to be present -- anything uses this needs
  330. * RUN_SETUP for reading from the configuration file.
  331. */
  332. #define NEED_WORK_TREE (1<<2)
  333. static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
  334. {
  335. int status;
  336. struct stat st;
  337. const char *prefix;
  338. char sbuf[STRERR_BUFSIZE];
  339. prefix = NULL;
  340. if (p->option & RUN_SETUP)
  341. prefix = NULL; /* setup_perf_directory(); */
  342. if (use_browser == -1)
  343. use_browser = check_browser_config(p->cmd);
  344. if (use_pager == -1 && p->option & RUN_SETUP)
  345. use_pager = check_pager_config(p->cmd);
  346. if (use_pager == -1 && p->option & USE_PAGER)
  347. use_pager = 1;
  348. commit_pager_choice();
  349. perf_env__set_cmdline(&perf_env, argc, argv);
  350. status = p->fn(argc, argv, prefix);
  351. exit_browser(status);
  352. perf_env__exit(&perf_env);
  353. bpf__clear();
  354. if (status)
  355. return status & 0xff;
  356. /* Somebody closed stdout? */
  357. if (fstat(fileno(stdout), &st))
  358. return 0;
  359. /* Ignore write errors for pipes and sockets.. */
  360. if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
  361. return 0;
  362. status = 1;
  363. /* Check for ENOSPC and EIO errors.. */
  364. if (fflush(stdout)) {
  365. fprintf(stderr, "write failure on standard output: %s",
  366. strerror_r(errno, sbuf, sizeof(sbuf)));
  367. goto out;
  368. }
  369. if (ferror(stdout)) {
  370. fprintf(stderr, "unknown write failure on standard output");
  371. goto out;
  372. }
  373. if (fclose(stdout)) {
  374. fprintf(stderr, "close failed on standard output: %s",
  375. strerror_r(errno, sbuf, sizeof(sbuf)));
  376. goto out;
  377. }
  378. status = 0;
  379. out:
  380. return status;
  381. }
  382. static void handle_internal_command(int argc, const char **argv)
  383. {
  384. const char *cmd = argv[0];
  385. unsigned int i;
  386. static const char ext[] = STRIP_EXTENSION;
  387. if (sizeof(ext) > 1) {
  388. i = strlen(argv[0]) - strlen(ext);
  389. if (i > 0 && !strcmp(argv[0] + i, ext)) {
  390. char *argv0 = strdup(argv[0]);
  391. argv[0] = cmd = argv0;
  392. argv0[i] = '\0';
  393. }
  394. }
  395. /* Turn "perf cmd --help" into "perf help cmd" */
  396. if (argc > 1 && !strcmp(argv[1], "--help")) {
  397. argv[1] = argv[0];
  398. argv[0] = cmd = "help";
  399. }
  400. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  401. struct cmd_struct *p = commands+i;
  402. if (strcmp(p->cmd, cmd))
  403. continue;
  404. exit(run_builtin(p, argc, argv));
  405. }
  406. }
  407. static void execv_dashed_external(const char **argv)
  408. {
  409. struct strbuf cmd = STRBUF_INIT;
  410. const char *tmp;
  411. int status;
  412. strbuf_addf(&cmd, "perf-%s", argv[0]);
  413. /*
  414. * argv[0] must be the perf command, but the argv array
  415. * belongs to the caller, and may be reused in
  416. * subsequent loop iterations. Save argv[0] and
  417. * restore it on error.
  418. */
  419. tmp = argv[0];
  420. argv[0] = cmd.buf;
  421. /*
  422. * if we fail because the command is not found, it is
  423. * OK to return. Otherwise, we just pass along the status code.
  424. */
  425. status = run_command_v_opt(argv, 0);
  426. if (status != -ERR_RUN_COMMAND_EXEC) {
  427. if (IS_RUN_COMMAND_ERR(status))
  428. die("unable to run '%s'", argv[0]);
  429. exit(-status);
  430. }
  431. errno = ENOENT; /* as if we called execvp */
  432. argv[0] = tmp;
  433. strbuf_release(&cmd);
  434. }
  435. static int run_argv(int *argcp, const char ***argv)
  436. {
  437. int done_alias = 0;
  438. while (1) {
  439. /* See if it's an internal command */
  440. handle_internal_command(*argcp, *argv);
  441. /* .. then try the external ones */
  442. execv_dashed_external(*argv);
  443. /* It could be an alias -- this works around the insanity
  444. * of overriding "perf log" with "perf show" by having
  445. * alias.log = show
  446. */
  447. if (done_alias || !handle_alias(argcp, argv))
  448. break;
  449. done_alias = 1;
  450. }
  451. return done_alias;
  452. }
  453. static void pthread__block_sigwinch(void)
  454. {
  455. sigset_t set;
  456. sigemptyset(&set);
  457. sigaddset(&set, SIGWINCH);
  458. pthread_sigmask(SIG_BLOCK, &set, NULL);
  459. }
  460. void pthread__unblock_sigwinch(void)
  461. {
  462. sigset_t set;
  463. sigemptyset(&set);
  464. sigaddset(&set, SIGWINCH);
  465. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  466. }
  467. int main(int argc, const char **argv)
  468. {
  469. const char *cmd;
  470. char sbuf[STRERR_BUFSIZE];
  471. /* libsubcmd init */
  472. exec_cmd_init("perf", PREFIX, PERF_EXEC_PATH, EXEC_PATH_ENVIRONMENT);
  473. pager_init(PERF_PAGER_ENVIRONMENT);
  474. /* The page_size is placed in util object. */
  475. page_size = sysconf(_SC_PAGE_SIZE);
  476. cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
  477. cmd = extract_argv0_path(argv[0]);
  478. if (!cmd)
  479. cmd = "perf-help";
  480. srandom(time(NULL));
  481. /* get debugfs/tracefs mount point from /proc/mounts */
  482. tracing_path_mount();
  483. /*
  484. * "perf-xxxx" is the same as "perf xxxx", but we obviously:
  485. *
  486. * - cannot take flags in between the "perf" and the "xxxx".
  487. * - cannot execute it externally (since it would just do
  488. * the same thing over again)
  489. *
  490. * So we just directly call the internal command handler, and
  491. * die if that one cannot handle it.
  492. */
  493. if (!prefixcmp(cmd, "perf-")) {
  494. cmd += 5;
  495. argv[0] = cmd;
  496. handle_internal_command(argc, argv);
  497. fprintf(stderr, "cannot handle %s internally", cmd);
  498. goto out;
  499. }
  500. if (!prefixcmp(cmd, "trace")) {
  501. #ifdef HAVE_LIBAUDIT_SUPPORT
  502. set_buildid_dir(NULL);
  503. setup_path();
  504. argv[0] = "trace";
  505. return cmd_trace(argc, argv, NULL);
  506. #else
  507. fprintf(stderr,
  508. "trace command not available: missing audit-libs devel package at build time.\n");
  509. goto out;
  510. #endif
  511. }
  512. /* Look for flags.. */
  513. argv++;
  514. argc--;
  515. handle_options(&argv, &argc, NULL);
  516. commit_pager_choice();
  517. set_buildid_dir(NULL);
  518. if (argc > 0) {
  519. if (!prefixcmp(argv[0], "--"))
  520. argv[0] += 2;
  521. } else {
  522. /* The user didn't specify a command; give them help */
  523. printf("\n usage: %s\n\n", perf_usage_string);
  524. list_common_cmds_help();
  525. printf("\n %s\n\n", perf_more_info_string);
  526. goto out;
  527. }
  528. cmd = argv[0];
  529. test_attr__init();
  530. /*
  531. * We use PATH to find perf commands, but we prepend some higher
  532. * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
  533. * environment, and the $(perfexecdir) from the Makefile at build
  534. * time.
  535. */
  536. setup_path();
  537. /*
  538. * Block SIGWINCH notifications so that the thread that wants it can
  539. * unblock and get syscalls like select interrupted instead of waiting
  540. * forever while the signal goes to some other non interested thread.
  541. */
  542. pthread__block_sigwinch();
  543. while (1) {
  544. static int done_help;
  545. int was_alias = run_argv(&argc, &argv);
  546. if (errno != ENOENT)
  547. break;
  548. if (was_alias) {
  549. fprintf(stderr, "Expansion of alias '%s' failed; "
  550. "'%s' is not a perf-command\n",
  551. cmd, argv[0]);
  552. goto out;
  553. }
  554. if (!done_help) {
  555. cmd = argv[0] = help_unknown_cmd(cmd);
  556. done_help = 1;
  557. } else
  558. break;
  559. }
  560. fprintf(stderr, "Failed to run command '%s': %s\n",
  561. cmd, strerror_r(errno, sbuf, sizeof(sbuf)));
  562. out:
  563. return 1;
  564. }