PageRenderTime 73ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/perf/perf.c

https://github.com/dmitriy103/bravo_kernel-2.6.35
C | 506 lines | 377 code | 65 blank | 64 comment | 95 complexity | 9f9243a7100455208748fe5583c1ae21 MD5 | raw file
  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. { "help", cmd_help, 0 },
  277. { "list", cmd_list, 0 },
  278. { "record", cmd_record, 0 },
  279. { "report", cmd_report, 0 },
  280. { "bench", cmd_bench, 0 },
  281. { "stat", cmd_stat, 0 },
  282. { "timechart", cmd_timechart, 0 },
  283. { "top", cmd_top, 0 },
  284. { "annotate", cmd_annotate, 0 },
  285. { "version", cmd_version, 0 },
  286. { "trace", cmd_trace, 0 },
  287. { "sched", cmd_sched, 0 },
  288. { "probe", cmd_probe, 0 },
  289. { "kmem", cmd_kmem, 0 },
  290. { "lock", cmd_lock, 0 },
  291. { "kvm", cmd_kvm, 0 },
  292. { "test", cmd_test, 0 },
  293. { "inject", cmd_inject, 0 },
  294. };
  295. unsigned int i;
  296. static const char ext[] = STRIP_EXTENSION;
  297. if (sizeof(ext) > 1) {
  298. i = strlen(argv[0]) - strlen(ext);
  299. if (i > 0 && !strcmp(argv[0] + i, ext)) {
  300. char *argv0 = strdup(argv[0]);
  301. argv[0] = cmd = argv0;
  302. argv0[i] = '\0';
  303. }
  304. }
  305. /* Turn "perf cmd --help" into "perf help cmd" */
  306. if (argc > 1 && !strcmp(argv[1], "--help")) {
  307. argv[1] = argv[0];
  308. argv[0] = cmd = "help";
  309. }
  310. for (i = 0; i < ARRAY_SIZE(commands); i++) {
  311. struct cmd_struct *p = commands+i;
  312. if (strcmp(p->cmd, cmd))
  313. continue;
  314. exit(run_builtin(p, argc, argv));
  315. }
  316. }
  317. static void execv_dashed_external(const char **argv)
  318. {
  319. struct strbuf cmd = STRBUF_INIT;
  320. const char *tmp;
  321. int status;
  322. strbuf_addf(&cmd, "perf-%s", argv[0]);
  323. /*
  324. * argv[0] must be the perf command, but the argv array
  325. * belongs to the caller, and may be reused in
  326. * subsequent loop iterations. Save argv[0] and
  327. * restore it on error.
  328. */
  329. tmp = argv[0];
  330. argv[0] = cmd.buf;
  331. /*
  332. * if we fail because the command is not found, it is
  333. * OK to return. Otherwise, we just pass along the status code.
  334. */
  335. status = run_command_v_opt(argv, 0);
  336. if (status != -ERR_RUN_COMMAND_EXEC) {
  337. if (IS_RUN_COMMAND_ERR(status))
  338. die("unable to run '%s'", argv[0]);
  339. exit(-status);
  340. }
  341. errno = ENOENT; /* as if we called execvp */
  342. argv[0] = tmp;
  343. strbuf_release(&cmd);
  344. }
  345. static int run_argv(int *argcp, const char ***argv)
  346. {
  347. int done_alias = 0;
  348. while (1) {
  349. /* See if it's an internal command */
  350. handle_internal_command(*argcp, *argv);
  351. /* .. then try the external ones */
  352. execv_dashed_external(*argv);
  353. /* It could be an alias -- this works around the insanity
  354. * of overriding "perf log" with "perf show" by having
  355. * alias.log = show
  356. */
  357. if (done_alias || !handle_alias(argcp, argv))
  358. break;
  359. done_alias = 1;
  360. }
  361. return done_alias;
  362. }
  363. /* mini /proc/mounts parser: searching for "^blah /mount/point debugfs" */
  364. static void get_debugfs_mntpt(void)
  365. {
  366. const char *path = debugfs_mount(NULL);
  367. if (path)
  368. strncpy(debugfs_mntpt, path, sizeof(debugfs_mntpt));
  369. else
  370. debugfs_mntpt[0] = '\0';
  371. }
  372. int main(int argc, const char **argv)
  373. {
  374. const char *cmd;
  375. cmd = perf_extract_argv0_path(argv[0]);
  376. if (!cmd)
  377. cmd = "perf-help";
  378. /* get debugfs mount point from /proc/mounts */
  379. get_debugfs_mntpt();
  380. /*
  381. * "perf-xxxx" is the same as "perf xxxx", but we obviously:
  382. *
  383. * - cannot take flags in between the "perf" and the "xxxx".
  384. * - cannot execute it externally (since it would just do
  385. * the same thing over again)
  386. *
  387. * So we just directly call the internal command handler, and
  388. * die if that one cannot handle it.
  389. */
  390. if (!prefixcmp(cmd, "perf-")) {
  391. cmd += 5;
  392. argv[0] = cmd;
  393. handle_internal_command(argc, argv);
  394. die("cannot handle %s internally", cmd);
  395. }
  396. /* Look for flags.. */
  397. argv++;
  398. argc--;
  399. handle_options(&argv, &argc, NULL);
  400. commit_pager_choice();
  401. set_debugfs_path();
  402. if (argc > 0) {
  403. if (!prefixcmp(argv[0], "--"))
  404. argv[0] += 2;
  405. } else {
  406. /* The user didn't specify a command; give them help */
  407. printf("\n usage: %s\n\n", perf_usage_string);
  408. list_common_cmds_help();
  409. printf("\n %s\n\n", perf_more_info_string);
  410. exit(1);
  411. }
  412. cmd = argv[0];
  413. /*
  414. * We use PATH to find perf commands, but we prepend some higher
  415. * precedence paths: the "--exec-path" option, the PERF_EXEC_PATH
  416. * environment, and the $(perfexecdir) from the Makefile at build
  417. * time.
  418. */
  419. setup_path();
  420. while (1) {
  421. static int done_help;
  422. static int was_alias;
  423. was_alias = run_argv(&argc, &argv);
  424. if (errno != ENOENT)
  425. break;
  426. if (was_alias) {
  427. fprintf(stderr, "Expansion of alias '%s' failed; "
  428. "'%s' is not a perf-command\n",
  429. cmd, argv[0]);
  430. exit(1);
  431. }
  432. if (!done_help) {
  433. cmd = argv[0] = help_unknown_cmd(cmd);
  434. done_help = 1;
  435. } else
  436. break;
  437. }
  438. fprintf(stderr, "Failed to run command '%s': %s\n",
  439. cmd, strerror(errno));
  440. return 1;
  441. }