perf.c 12 KB

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