perf.c 15 KB

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