perf.c 12 KB

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