perf.c 12 KB

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