perf.c 13 KB

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