perf.c 11 KB

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