perf.c 15 KB

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