perf.c 12 KB

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