perf.c 11 KB

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