perf.c 15 KB

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