perf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
  188. perf_debugfs_set_path(cmd + strlen(CMD_DEBUGFS_DIR));
  189. fprintf(stderr, "dir: %s\n", debugfs_mountpoint);
  190. if (envchanged)
  191. *envchanged = 1;
  192. } else if (!strcmp(cmd, "--list-cmds")) {
  193. unsigned int i;
  194. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  195. struct cmd_struct *p = commands+i;
  196. printf("%s ", p->cmd);
  197. }
  198. exit(0);
  199. } else if (!strcmp(cmd, "--debug")) {
  200. if (*argc < 2) {
  201. fprintf(stderr, "No variable specified for --debug.\n");
  202. usage(perf_usage_string);
  203. }
  204. if (perf_debug_option((*argv)[1]))
  205. usage(perf_usage_string);
  206. (*argv)++;
  207. (*argc)--;
  208. } else {
  209. fprintf(stderr, "Unknown option: %s\n", cmd);
  210. usage(perf_usage_string);
  211. }
  212. (*argv)++;
  213. (*argc)--;
  214. handled++;
  215. }
  216. return handled;
  217. }
  218. static int handle_alias(int *argcp, const char ***argv)
  219. {
  220. int envchanged = 0, ret = 0, saved_errno = errno;
  221. int count, option_count;
  222. const char **new_argv;
  223. const char *alias_command;
  224. char *alias_string;
  225. alias_command = (*argv)[0];
  226. alias_string = alias_lookup(alias_command);
  227. if (alias_string) {
  228. if (alias_string[0] == '!') {
  229. if (*argcp > 1) {
  230. struct strbuf buf;
  231. strbuf_init(&buf, PATH_MAX);
  232. strbuf_addstr(&buf, alias_string);
  233. sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
  234. free(alias_string);
  235. alias_string = buf.buf;
  236. }
  237. ret = system(alias_string + 1);
  238. if (ret >= 0 && WIFEXITED(ret) &&
  239. WEXITSTATUS(ret) != 127)
  240. exit(WEXITSTATUS(ret));
  241. die("Failed to run '%s' when expanding alias '%s'",
  242. alias_string + 1, alias_command);
  243. }
  244. count = split_cmdline(alias_string, &new_argv);
  245. if (count < 0)
  246. die("Bad alias.%s string", alias_command);
  247. option_count = handle_options(&new_argv, &count, &envchanged);
  248. if (envchanged)
  249. die("alias '%s' changes environment variables\n"
  250. "You can use '!perf' in the alias to do this.",
  251. alias_command);
  252. memmove(new_argv - option_count, new_argv,
  253. count * sizeof(char *));
  254. new_argv -= option_count;
  255. if (count < 1)
  256. die("empty alias for %s", alias_command);
  257. if (!strcmp(alias_command, new_argv[0]))
  258. die("recursive alias: %s", alias_command);
  259. new_argv = realloc(new_argv, sizeof(char *) *
  260. (count + *argcp + 1));
  261. /* insert after command name */
  262. memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
  263. new_argv[count + *argcp] = NULL;
  264. *argv = new_argv;
  265. *argcp += count - 1;
  266. ret = 1;
  267. }
  268. errno = saved_errno;
  269. return ret;
  270. }
  271. const char perf_version_string[] = PERF_VERSION;
  272. #define RUN_SETUP (1<<0)
  273. #define USE_PAGER (1<<1)
  274. /*
  275. * require working tree to be present -- anything uses this needs
  276. * RUN_SETUP for reading from the configuration file.
  277. */
  278. #define NEED_WORK_TREE (1<<2)
  279. static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
  280. {
  281. int status;
  282. struct stat st;
  283. const char *prefix;
  284. char sbuf[STRERR_BUFSIZE];
  285. prefix = NULL;
  286. if (p->option & RUN_SETUP)
  287. prefix = NULL; /* setup_perf_directory(); */
  288. if (use_browser == -1)
  289. use_browser = check_browser_config(p->cmd);
  290. if (use_pager == -1 && p->option & RUN_SETUP)
  291. use_pager = check_pager_config(p->cmd);
  292. if (use_pager == -1 && p->option & USE_PAGER)
  293. use_pager = 1;
  294. commit_pager_choice();
  295. status = p->fn(argc, argv, prefix);
  296. exit_browser(status);
  297. if (status)
  298. return status & 0xff;
  299. /* Somebody closed stdout? */
  300. if (fstat(fileno(stdout), &st))
  301. return 0;
  302. /* Ignore write errors for pipes and sockets.. */
  303. if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
  304. return 0;
  305. status = 1;
  306. /* Check for ENOSPC and EIO errors.. */
  307. if (fflush(stdout)) {
  308. fprintf(stderr, "write failure on standard output: %s",
  309. strerror_r(errno, sbuf, sizeof(sbuf)));
  310. goto out;
  311. }
  312. if (ferror(stdout)) {
  313. fprintf(stderr, "unknown write failure on standard output");
  314. goto out;
  315. }
  316. if (fclose(stdout)) {
  317. fprintf(stderr, "close failed on standard output: %s",
  318. strerror_r(errno, sbuf, sizeof(sbuf)));
  319. goto out;
  320. }
  321. status = 0;
  322. out:
  323. return status;
  324. }
  325. static void handle_internal_command(int argc, const char **argv)
  326. {
  327. const char *cmd = argv[0];
  328. unsigned int i;
  329. static const char ext[] = STRIP_EXTENSION;
  330. if (sizeof(ext) > 1) {
  331. i = strlen(argv[0]) - strlen(ext);
  332. if (i > 0 && !strcmp(argv[0] + i, ext)) {
  333. char *argv0 = strdup(argv[0]);
  334. argv[0] = cmd = argv0;
  335. argv0[i] = '\0';
  336. }
  337. }
  338. /* Turn "perf cmd --help" into "perf help cmd" */
  339. if (argc > 1 && !strcmp(argv[1], "--help")) {
  340. argv[1] = argv[0];
  341. argv[0] = cmd = "help";
  342. }
  343. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  344. struct cmd_struct *p = commands+i;
  345. if (strcmp(p->cmd, cmd))
  346. continue;
  347. exit(run_builtin(p, argc, argv));
  348. }
  349. }
  350. static void execv_dashed_external(const char **argv)
  351. {
  352. struct strbuf cmd = STRBUF_INIT;
  353. const char *tmp;
  354. int status;
  355. strbuf_addf(&cmd, "perf-%s", argv[0]);
  356. /*
  357. * argv[0] must be the perf command, but the argv array
  358. * belongs to the caller, and may be reused in
  359. * subsequent loop iterations. Save argv[0] and
  360. * restore it on error.
  361. */
  362. tmp = argv[0];
  363. argv[0] = cmd.buf;
  364. /*
  365. * if we fail because the command is not found, it is
  366. * OK to return. Otherwise, we just pass along the status code.
  367. */
  368. status = run_command_v_opt(argv, 0);
  369. if (status != -ERR_RUN_COMMAND_EXEC) {
  370. if (IS_RUN_COMMAND_ERR(status))
  371. die("unable to run '%s'", argv[0]);
  372. exit(-status);
  373. }
  374. errno = ENOENT; /* as if we called execvp */
  375. argv[0] = tmp;
  376. strbuf_release(&cmd);
  377. }
  378. static int run_argv(int *argcp, const char ***argv)
  379. {
  380. int done_alias = 0;
  381. while (1) {
  382. /* See if it's an internal command */
  383. handle_internal_command(*argcp, *argv);
  384. /* .. then try the external ones */
  385. execv_dashed_external(*argv);
  386. /* It could be an alias -- this works around the insanity
  387. * of overriding "perf log" with "perf show" by having
  388. * alias.log = show
  389. */
  390. if (done_alias || !handle_alias(argcp, argv))
  391. break;
  392. done_alias = 1;
  393. }
  394. return done_alias;
  395. }
  396. static void pthread__block_sigwinch(void)
  397. {
  398. sigset_t set;
  399. sigemptyset(&set);
  400. sigaddset(&set, SIGWINCH);
  401. pthread_sigmask(SIG_BLOCK, &set, NULL);
  402. }
  403. void pthread__unblock_sigwinch(void)
  404. {
  405. sigset_t set;
  406. sigemptyset(&set);
  407. sigaddset(&set, SIGWINCH);
  408. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  409. }
  410. int main(int argc, const char **argv)
  411. {
  412. const char *cmd;
  413. char sbuf[STRERR_BUFSIZE];
  414. /* The page_size is placed in util object. */
  415. page_size = sysconf(_SC_PAGE_SIZE);
  416. cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
  417. cmd = perf_extract_argv0_path(argv[0]);
  418. if (!cmd)
  419. cmd = "perf-help";
  420. /* get debugfs mount point from /proc/mounts */
  421. perf_debugfs_mount(NULL);
  422. /*
  423. * "perf-xxxx" is the same as "perf xxxx", but we obviously:
  424. *
  425. * - cannot take flags in between the "perf" and the "xxxx".
  426. * - cannot execute it externally (since it would just do
  427. * the same thing over again)
  428. *
  429. * So we just directly call the internal command handler, and
  430. * die if that one cannot handle it.
  431. */
  432. if (!prefixcmp(cmd, "perf-")) {
  433. cmd += 5;
  434. argv[0] = cmd;
  435. handle_internal_command(argc, argv);
  436. fprintf(stderr, "cannot handle %s internally", cmd);
  437. goto out;
  438. }
  439. if (!prefixcmp(cmd, "trace")) {
  440. #ifdef HAVE_LIBAUDIT_SUPPORT
  441. set_buildid_dir();
  442. setup_path();
  443. argv[0] = "trace";
  444. return cmd_trace(argc, argv, NULL);
  445. #else
  446. fprintf(stderr,
  447. "trace command not available: missing audit-libs devel package at build time.\n");
  448. goto out;
  449. #endif
  450. }
  451. /* Look for flags.. */
  452. argv++;
  453. argc--;
  454. handle_options(&argv, &argc, NULL);
  455. commit_pager_choice();
  456. set_buildid_dir();
  457. if (argc > 0) {
  458. if (!prefixcmp(argv[0], "--"))
  459. argv[0] += 2;
  460. } else {
  461. /* The user didn't specify a command; give them help */
  462. printf("\n usage: %s\n\n", perf_usage_string);
  463. list_common_cmds_help();
  464. printf("\n %s\n\n", perf_more_info_string);
  465. goto out;
  466. }
  467. cmd = argv[0];
  468. test_attr__init();
  469. /*
  470. * We use PATH to find perf commands, but we prepend some higher
  471. * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
  472. * environment, and the $(perfexecdir) from the Makefile at build
  473. * time.
  474. */
  475. setup_path();
  476. /*
  477. * Block SIGWINCH notifications so that the thread that wants it can
  478. * unblock and get syscalls like select interrupted instead of waiting
  479. * forever while the signal goes to some other non interested thread.
  480. */
  481. pthread__block_sigwinch();
  482. while (1) {
  483. static int done_help;
  484. int was_alias = run_argv(&argc, &argv);
  485. if (errno != ENOENT)
  486. break;
  487. if (was_alias) {
  488. fprintf(stderr, "Expansion of alias '%s' failed; "
  489. "'%s' is not a perf-command\n",
  490. cmd, argv[0]);
  491. goto out;
  492. }
  493. if (!done_help) {
  494. cmd = argv[0] = help_unknown_cmd(cmd);
  495. done_help = 1;
  496. } else
  497. break;
  498. }
  499. fprintf(stderr, "Failed to run command '%s': %s\n",
  500. cmd, strerror_r(errno, sbuf, sizeof(sbuf)));
  501. out:
  502. return 1;
  503. }