perf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. if (strbuf_init(&buf, PATH_MAX) < 0 ||
  287. strbuf_addstr(&buf, alias_string) < 0 ||
  288. sq_quote_argv(&buf, (*argv) + 1,
  289. PATH_MAX) < 0)
  290. die("Failed to allocate memory.");
  291. free(alias_string);
  292. alias_string = buf.buf;
  293. }
  294. ret = system(alias_string + 1);
  295. if (ret >= 0 && WIFEXITED(ret) &&
  296. WEXITSTATUS(ret) != 127)
  297. exit(WEXITSTATUS(ret));
  298. die("Failed to run '%s' when expanding alias '%s'",
  299. alias_string + 1, alias_command);
  300. }
  301. count = split_cmdline(alias_string, &new_argv);
  302. if (count < 0)
  303. die("Bad alias.%s string", alias_command);
  304. option_count = handle_options(&new_argv, &count, &envchanged);
  305. if (envchanged)
  306. die("alias '%s' changes environment variables\n"
  307. "You can use '!perf' in the alias to do this.",
  308. alias_command);
  309. memmove(new_argv - option_count, new_argv,
  310. count * sizeof(char *));
  311. new_argv -= option_count;
  312. if (count < 1)
  313. die("empty alias for %s", alias_command);
  314. if (!strcmp(alias_command, new_argv[0]))
  315. die("recursive alias: %s", alias_command);
  316. new_argv = realloc(new_argv, sizeof(char *) *
  317. (count + *argcp + 1));
  318. /* insert after command name */
  319. memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
  320. new_argv[count + *argcp] = NULL;
  321. *argv = new_argv;
  322. *argcp += count - 1;
  323. ret = 1;
  324. }
  325. errno = saved_errno;
  326. return ret;
  327. }
  328. const char perf_version_string[] = PERF_VERSION;
  329. #define RUN_SETUP (1<<0)
  330. #define USE_PAGER (1<<1)
  331. /*
  332. * require working tree to be present -- anything uses this needs
  333. * RUN_SETUP for reading from the configuration file.
  334. */
  335. #define NEED_WORK_TREE (1<<2)
  336. static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
  337. {
  338. int status;
  339. struct stat st;
  340. const char *prefix;
  341. char sbuf[STRERR_BUFSIZE];
  342. prefix = NULL;
  343. if (p->option & RUN_SETUP)
  344. prefix = NULL; /* setup_perf_directory(); */
  345. if (use_browser == -1)
  346. use_browser = check_browser_config(p->cmd);
  347. if (use_pager == -1 && p->option & RUN_SETUP)
  348. use_pager = check_pager_config(p->cmd);
  349. if (use_pager == -1 && p->option & USE_PAGER)
  350. use_pager = 1;
  351. commit_pager_choice();
  352. perf_env__set_cmdline(&perf_env, argc, argv);
  353. status = p->fn(argc, argv, prefix);
  354. exit_browser(status);
  355. perf_env__exit(&perf_env);
  356. bpf__clear();
  357. if (status)
  358. return status & 0xff;
  359. /* Somebody closed stdout? */
  360. if (fstat(fileno(stdout), &st))
  361. return 0;
  362. /* Ignore write errors for pipes and sockets.. */
  363. if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
  364. return 0;
  365. status = 1;
  366. /* Check for ENOSPC and EIO errors.. */
  367. if (fflush(stdout)) {
  368. fprintf(stderr, "write failure on standard output: %s",
  369. strerror_r(errno, sbuf, sizeof(sbuf)));
  370. goto out;
  371. }
  372. if (ferror(stdout)) {
  373. fprintf(stderr, "unknown write failure on standard output");
  374. goto out;
  375. }
  376. if (fclose(stdout)) {
  377. fprintf(stderr, "close failed on standard output: %s",
  378. strerror_r(errno, sbuf, sizeof(sbuf)));
  379. goto out;
  380. }
  381. status = 0;
  382. out:
  383. return status;
  384. }
  385. static void handle_internal_command(int argc, const char **argv)
  386. {
  387. const char *cmd = argv[0];
  388. unsigned int i;
  389. static const char ext[] = STRIP_EXTENSION;
  390. if (sizeof(ext) > 1) {
  391. i = strlen(argv[0]) - strlen(ext);
  392. if (i > 0 && !strcmp(argv[0] + i, ext)) {
  393. char *argv0 = strdup(argv[0]);
  394. argv[0] = cmd = argv0;
  395. argv0[i] = '\0';
  396. }
  397. }
  398. /* Turn "perf cmd --help" into "perf help cmd" */
  399. if (argc > 1 && !strcmp(argv[1], "--help")) {
  400. argv[1] = argv[0];
  401. argv[0] = cmd = "help";
  402. }
  403. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  404. struct cmd_struct *p = commands+i;
  405. if (strcmp(p->cmd, cmd))
  406. continue;
  407. exit(run_builtin(p, argc, argv));
  408. }
  409. }
  410. static void execv_dashed_external(const char **argv)
  411. {
  412. char *cmd;
  413. const char *tmp;
  414. int status;
  415. if (asprintf(&cmd, "perf-%s", argv[0]) < 0)
  416. goto do_die;
  417. /*
  418. * argv[0] must be the perf command, but the argv array
  419. * belongs to the caller, and may be reused in
  420. * subsequent loop iterations. Save argv[0] and
  421. * restore it on error.
  422. */
  423. tmp = argv[0];
  424. argv[0] = cmd;
  425. /*
  426. * if we fail because the command is not found, it is
  427. * OK to return. Otherwise, we just pass along the status code.
  428. */
  429. status = run_command_v_opt(argv, 0);
  430. if (status != -ERR_RUN_COMMAND_EXEC) {
  431. if (IS_RUN_COMMAND_ERR(status)) {
  432. do_die:
  433. die("unable to run '%s'", argv[0]);
  434. }
  435. exit(-status);
  436. }
  437. errno = ENOENT; /* as if we called execvp */
  438. argv[0] = tmp;
  439. zfree(&cmd);
  440. }
  441. static int run_argv(int *argcp, const char ***argv)
  442. {
  443. int done_alias = 0;
  444. while (1) {
  445. /* See if it's an internal command */
  446. handle_internal_command(*argcp, *argv);
  447. /* .. then try the external ones */
  448. execv_dashed_external(*argv);
  449. /* It could be an alias -- this works around the insanity
  450. * of overriding "perf log" with "perf show" by having
  451. * alias.log = show
  452. */
  453. if (done_alias || !handle_alias(argcp, argv))
  454. break;
  455. done_alias = 1;
  456. }
  457. return done_alias;
  458. }
  459. static void pthread__block_sigwinch(void)
  460. {
  461. sigset_t set;
  462. sigemptyset(&set);
  463. sigaddset(&set, SIGWINCH);
  464. pthread_sigmask(SIG_BLOCK, &set, NULL);
  465. }
  466. void pthread__unblock_sigwinch(void)
  467. {
  468. sigset_t set;
  469. sigemptyset(&set);
  470. sigaddset(&set, SIGWINCH);
  471. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  472. }
  473. int main(int argc, const char **argv)
  474. {
  475. const char *cmd;
  476. char sbuf[STRERR_BUFSIZE];
  477. int value;
  478. /* libsubcmd init */
  479. exec_cmd_init("perf", PREFIX, PERF_EXEC_PATH, EXEC_PATH_ENVIRONMENT);
  480. pager_init(PERF_PAGER_ENVIRONMENT);
  481. /* The page_size is placed in util object. */
  482. page_size = sysconf(_SC_PAGE_SIZE);
  483. cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
  484. if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
  485. sysctl_perf_event_max_stack = value;
  486. if (sysctl__read_int("kernel/perf_event_max_contexts_per_stack", &value) == 0)
  487. sysctl_perf_event_max_contexts_per_stack = value;
  488. cmd = extract_argv0_path(argv[0]);
  489. if (!cmd)
  490. cmd = "perf-help";
  491. srandom(time(NULL));
  492. perf_config(perf_default_config, NULL);
  493. set_buildid_dir(NULL);
  494. /* get debugfs/tracefs mount point from /proc/mounts */
  495. tracing_path_mount();
  496. /*
  497. * "perf-xxxx" is the same as "perf xxxx", but we obviously:
  498. *
  499. * - cannot take flags in between the "perf" and the "xxxx".
  500. * - cannot execute it externally (since it would just do
  501. * the same thing over again)
  502. *
  503. * So we just directly call the internal command handler, and
  504. * die if that one cannot handle it.
  505. */
  506. if (!prefixcmp(cmd, "perf-")) {
  507. cmd += 5;
  508. argv[0] = cmd;
  509. handle_internal_command(argc, argv);
  510. fprintf(stderr, "cannot handle %s internally", cmd);
  511. goto out;
  512. }
  513. if (!prefixcmp(cmd, "trace")) {
  514. #ifdef HAVE_LIBAUDIT_SUPPORT
  515. setup_path();
  516. argv[0] = "trace";
  517. return cmd_trace(argc, argv, NULL);
  518. #else
  519. fprintf(stderr,
  520. "trace command not available: missing audit-libs devel package at build time.\n");
  521. goto out;
  522. #endif
  523. }
  524. /* Look for flags.. */
  525. argv++;
  526. argc--;
  527. handle_options(&argv, &argc, NULL);
  528. commit_pager_choice();
  529. if (argc > 0) {
  530. if (!prefixcmp(argv[0], "--"))
  531. argv[0] += 2;
  532. } else {
  533. /* The user didn't specify a command; give them help */
  534. printf("\n usage: %s\n\n", perf_usage_string);
  535. list_common_cmds_help();
  536. printf("\n %s\n\n", perf_more_info_string);
  537. goto out;
  538. }
  539. cmd = argv[0];
  540. test_attr__init();
  541. /*
  542. * We use PATH to find perf commands, but we prepend some higher
  543. * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
  544. * environment, and the $(perfexecdir) from the Makefile at build
  545. * time.
  546. */
  547. setup_path();
  548. /*
  549. * Block SIGWINCH notifications so that the thread that wants it can
  550. * unblock and get syscalls like select interrupted instead of waiting
  551. * forever while the signal goes to some other non interested thread.
  552. */
  553. pthread__block_sigwinch();
  554. perf_debug_setup();
  555. while (1) {
  556. static int done_help;
  557. int was_alias = run_argv(&argc, &argv);
  558. if (errno != ENOENT)
  559. break;
  560. if (was_alias) {
  561. fprintf(stderr, "Expansion of alias '%s' failed; "
  562. "'%s' is not a perf-command\n",
  563. cmd, argv[0]);
  564. goto out;
  565. }
  566. if (!done_help) {
  567. cmd = argv[0] = help_unknown_cmd(cmd);
  568. done_help = 1;
  569. } else
  570. break;
  571. }
  572. fprintf(stderr, "Failed to run command '%s': %s\n",
  573. cmd, strerror_r(errno, sbuf, sizeof(sbuf)));
  574. out:
  575. return 1;
  576. }