perf.c 15 KB

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