perf.c 13 KB

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