perf.c 14 KB

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