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 char debugfs_mntpt[MAXPATHLEN];
  27. static int pager_command_config(const char *var, const char *value, void *data)
  28. {
  29. struct pager_config *c = data;
  30. if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
  31. c->val = perf_config_bool(var, value);
  32. return 0;
  33. }
  34. /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
  35. int check_pager_config(const char *cmd)
  36. {
  37. struct pager_config c;
  38. c.cmd = cmd;
  39. c.val = -1;
  40. perf_config(pager_command_config, &c);
  41. return c.val;
  42. }
  43. static int tui_command_config(const char *var, const char *value, void *data)
  44. {
  45. struct pager_config *c = data;
  46. if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
  47. c->val = perf_config_bool(var, value);
  48. return 0;
  49. }
  50. /* returns 0 for "no tui", 1 for "use tui", and -1 for "not specified" */
  51. static int check_tui_config(const char *cmd)
  52. {
  53. struct pager_config c;
  54. c.cmd = cmd;
  55. c.val = -1;
  56. perf_config(tui_command_config, &c);
  57. return c.val;
  58. }
  59. static void commit_pager_choice(void)
  60. {
  61. switch (use_pager) {
  62. case 0:
  63. setenv("PERF_PAGER", "cat", 1);
  64. break;
  65. case 1:
  66. /* setup_pager(); */
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. static void set_debugfs_path(void)
  73. {
  74. char *path;
  75. path = getenv(PERF_DEBUGFS_ENVIRONMENT);
  76. snprintf(debugfs_path, MAXPATHLEN, "%s/%s", path ?: debugfs_mntpt,
  77. "tracing/events");
  78. }
  79. static int handle_options(const char ***argv, int *argc, int *envchanged)
  80. {
  81. int handled = 0;
  82. while (*argc > 0) {
  83. const char *cmd = (*argv)[0];
  84. if (cmd[0] != '-')
  85. break;
  86. /*
  87. * For legacy reasons, the "version" and "help"
  88. * commands can be written with "--" prepended
  89. * to make them look like flags.
  90. */
  91. if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
  92. break;
  93. /*
  94. * Check remaining flags.
  95. */
  96. if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
  97. cmd += strlen(CMD_EXEC_PATH);
  98. if (*cmd == '=')
  99. perf_set_argv_exec_path(cmd + 1);
  100. else {
  101. puts(perf_exec_path());
  102. exit(0);
  103. }
  104. } else if (!strcmp(cmd, "--html-path")) {
  105. puts(system_path(PERF_HTML_PATH));
  106. exit(0);
  107. } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
  108. use_pager = 1;
  109. } else if (!strcmp(cmd, "--no-pager")) {
  110. use_pager = 0;
  111. if (envchanged)
  112. *envchanged = 1;
  113. } else if (!strcmp(cmd, "--perf-dir")) {
  114. if (*argc < 2) {
  115. fprintf(stderr, "No directory given for --perf-dir.\n");
  116. usage(perf_usage_string);
  117. }
  118. setenv(PERF_DIR_ENVIRONMENT, (*argv)[1], 1);
  119. if (envchanged)
  120. *envchanged = 1;
  121. (*argv)++;
  122. (*argc)--;
  123. handled++;
  124. } else if (!prefixcmp(cmd, CMD_PERF_DIR)) {
  125. setenv(PERF_DIR_ENVIRONMENT, cmd + strlen(CMD_PERF_DIR), 1);
  126. if (envchanged)
  127. *envchanged = 1;
  128. } else if (!strcmp(cmd, "--work-tree")) {
  129. if (*argc < 2) {
  130. fprintf(stderr, "No directory given for --work-tree.\n");
  131. usage(perf_usage_string);
  132. }
  133. setenv(PERF_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
  134. if (envchanged)
  135. *envchanged = 1;
  136. (*argv)++;
  137. (*argc)--;
  138. } else if (!prefixcmp(cmd, CMD_WORK_TREE)) {
  139. setenv(PERF_WORK_TREE_ENVIRONMENT, cmd + strlen(CMD_WORK_TREE), 1);
  140. if (envchanged)
  141. *envchanged = 1;
  142. } else if (!strcmp(cmd, "--debugfs-dir")) {
  143. if (*argc < 2) {
  144. fprintf(stderr, "No directory given for --debugfs-dir.\n");
  145. usage(perf_usage_string);
  146. }
  147. strncpy(debugfs_mntpt, (*argv)[1], MAXPATHLEN);
  148. debugfs_mntpt[MAXPATHLEN - 1] = '\0';
  149. if (envchanged)
  150. *envchanged = 1;
  151. (*argv)++;
  152. (*argc)--;
  153. } else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
  154. strncpy(debugfs_mntpt, cmd + strlen(CMD_DEBUGFS_DIR), MAXPATHLEN);
  155. debugfs_mntpt[MAXPATHLEN - 1] = '\0';
  156. if (envchanged)
  157. *envchanged = 1;
  158. } else {
  159. fprintf(stderr, "Unknown option: %s\n", cmd);
  160. usage(perf_usage_string);
  161. }
  162. (*argv)++;
  163. (*argc)--;
  164. handled++;
  165. }
  166. return handled;
  167. }
  168. static int handle_alias(int *argcp, const char ***argv)
  169. {
  170. int envchanged = 0, ret = 0, saved_errno = errno;
  171. int count, option_count;
  172. const char **new_argv;
  173. const char *alias_command;
  174. char *alias_string;
  175. alias_command = (*argv)[0];
  176. alias_string = alias_lookup(alias_command);
  177. if (alias_string) {
  178. if (alias_string[0] == '!') {
  179. if (*argcp > 1) {
  180. struct strbuf buf;
  181. strbuf_init(&buf, PATH_MAX);
  182. strbuf_addstr(&buf, alias_string);
  183. sq_quote_argv(&buf, (*argv) + 1, PATH_MAX);
  184. free(alias_string);
  185. alias_string = buf.buf;
  186. }
  187. ret = system(alias_string + 1);
  188. if (ret >= 0 && WIFEXITED(ret) &&
  189. WEXITSTATUS(ret) != 127)
  190. exit(WEXITSTATUS(ret));
  191. die("Failed to run '%s' when expanding alias '%s'",
  192. alias_string + 1, alias_command);
  193. }
  194. count = split_cmdline(alias_string, &new_argv);
  195. if (count < 0)
  196. die("Bad alias.%s string", alias_command);
  197. option_count = handle_options(&new_argv, &count, &envchanged);
  198. if (envchanged)
  199. die("alias '%s' changes environment variables\n"
  200. "You can use '!perf' in the alias to do this.",
  201. alias_command);
  202. memmove(new_argv - option_count, new_argv,
  203. count * sizeof(char *));
  204. new_argv -= option_count;
  205. if (count < 1)
  206. die("empty alias for %s", alias_command);
  207. if (!strcmp(alias_command, new_argv[0]))
  208. die("recursive alias: %s", alias_command);
  209. new_argv = realloc(new_argv, sizeof(char *) *
  210. (count + *argcp + 1));
  211. /* insert after command name */
  212. memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
  213. new_argv[count + *argcp] = NULL;
  214. *argv = new_argv;
  215. *argcp += count - 1;
  216. ret = 1;
  217. }
  218. errno = saved_errno;
  219. return ret;
  220. }
  221. const char perf_version_string[] = PERF_VERSION;
  222. #define RUN_SETUP (1<<0)
  223. #define USE_PAGER (1<<1)
  224. /*
  225. * require working tree to be present -- anything uses this needs
  226. * RUN_SETUP for reading from the configuration file.
  227. */
  228. #define NEED_WORK_TREE (1<<2)
  229. struct cmd_struct {
  230. const char *cmd;
  231. int (*fn)(int, const char **, const char *);
  232. int option;
  233. };
  234. static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
  235. {
  236. int status;
  237. struct stat st;
  238. const char *prefix;
  239. prefix = NULL;
  240. if (p->option & RUN_SETUP)
  241. prefix = NULL; /* setup_perf_directory(); */
  242. if (use_browser == -1)
  243. use_browser = check_tui_config(p->cmd);
  244. if (use_pager == -1 && p->option & RUN_SETUP)
  245. use_pager = check_pager_config(p->cmd);
  246. if (use_pager == -1 && p->option & USE_PAGER)
  247. use_pager = 1;
  248. commit_pager_choice();
  249. set_debugfs_path();
  250. status = p->fn(argc, argv, prefix);
  251. exit_browser(status);
  252. if (status)
  253. return status & 0xff;
  254. /* Somebody closed stdout? */
  255. if (fstat(fileno(stdout), &st))
  256. return 0;
  257. /* Ignore write errors for pipes and sockets.. */
  258. if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
  259. return 0;
  260. /* Check for ENOSPC and EIO errors.. */
  261. if (fflush(stdout))
  262. die("write failure on standard output: %s", strerror(errno));
  263. if (ferror(stdout))
  264. die("unknown write failure on standard output");
  265. if (fclose(stdout))
  266. die("close failed on standard output: %s", strerror(errno));
  267. return 0;
  268. }
  269. static void handle_internal_command(int argc, const char **argv)
  270. {
  271. const char *cmd = argv[0];
  272. static struct cmd_struct commands[] = {
  273. { "buildid-cache", cmd_buildid_cache, 0 },
  274. { "buildid-list", cmd_buildid_list, 0 },
  275. { "diff", cmd_diff, 0 },
  276. { "evlist", cmd_evlist, 0 },
  277. { "help", cmd_help, 0 },
  278. { "list", cmd_list, 0 },
  279. { "record", cmd_record, 0 },
  280. { "report", cmd_report, 0 },
  281. { "bench", cmd_bench, 0 },
  282. { "stat", cmd_stat, 0 },
  283. { "timechart", cmd_timechart, 0 },
  284. { "top", cmd_top, 0 },
  285. { "annotate", cmd_annotate, 0 },
  286. { "version", cmd_version, 0 },
  287. { "script", cmd_script, 0 },
  288. { "sched", cmd_sched, 0 },
  289. { "probe", cmd_probe, 0 },
  290. { "kmem", cmd_kmem, 0 },
  291. { "lock", cmd_lock, 0 },
  292. { "kvm", cmd_kvm, 0 },
  293. { "test", cmd_test, 0 },
  294. { "inject", cmd_inject, 0 },
  295. };
  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. /* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */
  365. static void get_debugfs_mntpt(void)
  366. {
  367. const char *path = debugfs_mount(NULL);
  368. if (path)
  369. strncpy(debugfs_mntpt, path, sizeof(debugfs_mntpt));
  370. else
  371. debugfs_mntpt[0] = '\0';
  372. }
  373. int main(int argc, const char **argv)
  374. {
  375. const char *cmd;
  376. cmd = perf_extract_argv0_path(argv[0]);
  377. if (!cmd)
  378. cmd = "perf-help";
  379. /* get debugfs mount point from /proc/mounts */
  380. get_debugfs_mntpt();
  381. /*
  382. * "perf-xxxx" is the same as "perf xxxx", but we obviously:
  383. *
  384. * - cannot take flags in between the "perf" and the "xxxx".
  385. * - cannot execute it externally (since it would just do
  386. * the same thing over again)
  387. *
  388. * So we just directly call the internal command handler, and
  389. * die if that one cannot handle it.
  390. */
  391. if (!prefixcmp(cmd, "perf-")) {
  392. cmd += 5;
  393. argv[0] = cmd;
  394. handle_internal_command(argc, argv);
  395. die("cannot handle %s internally", cmd);
  396. }
  397. /* Look for flags.. */
  398. argv++;
  399. argc--;
  400. handle_options(&argv, &argc, NULL);
  401. commit_pager_choice();
  402. set_debugfs_path();
  403. set_buildid_dir();
  404. if (argc > 0) {
  405. if (!prefixcmp(argv[0], "--"))
  406. argv[0] += 2;
  407. } else {
  408. /* The user didn't specify a command; give them help */
  409. printf("\n usage: %s\n\n", perf_usage_string);
  410. list_common_cmds_help();
  411. printf("\n %s\n\n", perf_more_info_string);
  412. exit(1);
  413. }
  414. cmd = argv[0];
  415. /*
  416. * We use PATH to find perf commands, but we prepend some higher
  417. * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
  418. * environment, and the $(perfexecdir) from the Makefile at build
  419. * time.
  420. */
  421. setup_path();
  422. while (1) {
  423. static int done_help;
  424. static int was_alias;
  425. was_alias = run_argv(&argc, &argv);
  426. if (errno != ENOENT)
  427. break;
  428. if (was_alias) {
  429. fprintf(stderr, "Expansion of alias '%s' failed; "
  430. "'%s' is not a perf-command\n",
  431. cmd, argv[0]);
  432. exit(1);
  433. }
  434. if (!done_help) {
  435. cmd = argv[0] = help_unknown_cmd(cmd);
  436. done_help = 1;
  437. } else
  438. break;
  439. }
  440. fprintf(stderr, "Failed to run command '%s': %s\n",
  441. cmd, strerror(errno));
  442. return 1;
  443. }