perf.c 12 KB

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