builtin-ftrace.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * builtin-ftrace.c
  3. *
  4. * Copyright (c) 2013 LG Electronics, Namhyung Kim <namhyung@kernel.org>
  5. *
  6. * Released under the GPL v2.
  7. */
  8. #include "builtin.h"
  9. #include "perf.h"
  10. #include <errno.h>
  11. #include <unistd.h>
  12. #include <signal.h>
  13. #include <fcntl.h>
  14. #include "debug.h"
  15. #include <subcmd/parse-options.h>
  16. #include <api/fs/tracing_path.h>
  17. #include "evlist.h"
  18. #include "target.h"
  19. #include "cpumap.h"
  20. #include "thread_map.h"
  21. #include "util/config.h"
  22. #define DEFAULT_TRACER "function_graph"
  23. struct perf_ftrace {
  24. struct perf_evlist *evlist;
  25. struct target target;
  26. const char *tracer;
  27. };
  28. static bool done;
  29. static void sig_handler(int sig __maybe_unused)
  30. {
  31. done = true;
  32. }
  33. /*
  34. * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
  35. * we asked by setting its exec_error to the function below,
  36. * ftrace__workload_exec_failed_signal.
  37. *
  38. * XXX We need to handle this more appropriately, emitting an error, etc.
  39. */
  40. static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
  41. siginfo_t *info __maybe_unused,
  42. void *ucontext __maybe_unused)
  43. {
  44. /* workload_exec_errno = info->si_value.sival_int; */
  45. done = true;
  46. }
  47. static int __write_tracing_file(const char *name, const char *val, bool append)
  48. {
  49. char *file;
  50. int fd, ret = -1;
  51. ssize_t size = strlen(val);
  52. int flags = O_WRONLY;
  53. file = get_tracing_file(name);
  54. if (!file) {
  55. pr_debug("cannot get tracing file: %s\n", name);
  56. return -1;
  57. }
  58. if (append)
  59. flags |= O_APPEND;
  60. else
  61. flags |= O_TRUNC;
  62. fd = open(file, flags);
  63. if (fd < 0) {
  64. pr_debug("cannot open tracing file: %s\n", name);
  65. goto out;
  66. }
  67. if (write(fd, val, size) == size)
  68. ret = 0;
  69. else
  70. pr_debug("write '%s' to tracing/%s failed\n", val, name);
  71. close(fd);
  72. out:
  73. put_tracing_file(file);
  74. return ret;
  75. }
  76. static int write_tracing_file(const char *name, const char *val)
  77. {
  78. return __write_tracing_file(name, val, false);
  79. }
  80. static int append_tracing_file(const char *name, const char *val)
  81. {
  82. return __write_tracing_file(name, val, true);
  83. }
  84. static int reset_tracing_cpu(void);
  85. static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
  86. {
  87. if (write_tracing_file("tracing_on", "0") < 0)
  88. return -1;
  89. if (write_tracing_file("current_tracer", "nop") < 0)
  90. return -1;
  91. if (write_tracing_file("set_ftrace_pid", " ") < 0)
  92. return -1;
  93. if (reset_tracing_cpu() < 0)
  94. return -1;
  95. return 0;
  96. }
  97. static int set_tracing_pid(struct perf_ftrace *ftrace)
  98. {
  99. int i;
  100. char buf[16];
  101. if (target__has_cpu(&ftrace->target))
  102. return 0;
  103. for (i = 0; i < thread_map__nr(ftrace->evlist->threads); i++) {
  104. scnprintf(buf, sizeof(buf), "%d",
  105. ftrace->evlist->threads->map[i]);
  106. if (append_tracing_file("set_ftrace_pid", buf) < 0)
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. static int set_tracing_cpumask(struct cpu_map *cpumap)
  112. {
  113. char *cpumask;
  114. size_t mask_size;
  115. int ret;
  116. int last_cpu;
  117. last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
  118. mask_size = (last_cpu + 3) / 4 + 1;
  119. mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */
  120. cpumask = malloc(mask_size);
  121. if (cpumask == NULL) {
  122. pr_debug("failed to allocate cpu mask\n");
  123. return -1;
  124. }
  125. cpu_map__snprint_mask(cpumap, cpumask, mask_size);
  126. ret = write_tracing_file("tracing_cpumask", cpumask);
  127. free(cpumask);
  128. return ret;
  129. }
  130. static int set_tracing_cpu(struct perf_ftrace *ftrace)
  131. {
  132. struct cpu_map *cpumap = ftrace->evlist->cpus;
  133. if (!target__has_cpu(&ftrace->target))
  134. return 0;
  135. return set_tracing_cpumask(cpumap);
  136. }
  137. static int reset_tracing_cpu(void)
  138. {
  139. struct cpu_map *cpumap = cpu_map__new(NULL);
  140. int ret;
  141. ret = set_tracing_cpumask(cpumap);
  142. cpu_map__put(cpumap);
  143. return ret;
  144. }
  145. static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
  146. {
  147. char *trace_file;
  148. int trace_fd;
  149. char buf[4096];
  150. struct pollfd pollfd = {
  151. .events = POLLIN,
  152. };
  153. if (geteuid() != 0) {
  154. pr_err("ftrace only works for root!\n");
  155. return -1;
  156. }
  157. signal(SIGINT, sig_handler);
  158. signal(SIGUSR1, sig_handler);
  159. signal(SIGCHLD, sig_handler);
  160. signal(SIGPIPE, sig_handler);
  161. if (reset_tracing_files(ftrace) < 0)
  162. goto out;
  163. /* reset ftrace buffer */
  164. if (write_tracing_file("trace", "0") < 0)
  165. goto out;
  166. if (argc && perf_evlist__prepare_workload(ftrace->evlist,
  167. &ftrace->target, argv, false,
  168. ftrace__workload_exec_failed_signal) < 0) {
  169. goto out;
  170. }
  171. if (set_tracing_pid(ftrace) < 0) {
  172. pr_err("failed to set ftrace pid\n");
  173. goto out_reset;
  174. }
  175. if (set_tracing_cpu(ftrace) < 0) {
  176. pr_err("failed to set tracing cpumask\n");
  177. goto out_reset;
  178. }
  179. if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
  180. pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
  181. goto out_reset;
  182. }
  183. trace_file = get_tracing_file("trace_pipe");
  184. if (!trace_file) {
  185. pr_err("failed to open trace_pipe\n");
  186. goto out_reset;
  187. }
  188. trace_fd = open(trace_file, O_RDONLY);
  189. put_tracing_file(trace_file);
  190. if (trace_fd < 0) {
  191. pr_err("failed to open trace_pipe\n");
  192. goto out_reset;
  193. }
  194. fcntl(trace_fd, F_SETFL, O_NONBLOCK);
  195. pollfd.fd = trace_fd;
  196. if (write_tracing_file("tracing_on", "1") < 0) {
  197. pr_err("can't enable tracing\n");
  198. goto out_close_fd;
  199. }
  200. setup_pager();
  201. perf_evlist__start_workload(ftrace->evlist);
  202. while (!done) {
  203. if (poll(&pollfd, 1, -1) < 0)
  204. break;
  205. if (pollfd.revents & POLLIN) {
  206. int n = read(trace_fd, buf, sizeof(buf));
  207. if (n < 0)
  208. break;
  209. if (fwrite(buf, n, 1, stdout) != 1)
  210. break;
  211. }
  212. }
  213. write_tracing_file("tracing_on", "0");
  214. /* read remaining buffer contents */
  215. while (true) {
  216. int n = read(trace_fd, buf, sizeof(buf));
  217. if (n <= 0)
  218. break;
  219. if (fwrite(buf, n, 1, stdout) != 1)
  220. break;
  221. }
  222. out_close_fd:
  223. close(trace_fd);
  224. out_reset:
  225. reset_tracing_files(ftrace);
  226. out:
  227. return done ? 0 : -1;
  228. }
  229. static int perf_ftrace_config(const char *var, const char *value, void *cb)
  230. {
  231. struct perf_ftrace *ftrace = cb;
  232. if (prefixcmp(var, "ftrace."))
  233. return 0;
  234. if (strcmp(var, "ftrace.tracer"))
  235. return -1;
  236. if (!strcmp(value, "function_graph") ||
  237. !strcmp(value, "function")) {
  238. ftrace->tracer = value;
  239. return 0;
  240. }
  241. pr_err("Please select \"function_graph\" (default) or \"function\"\n");
  242. return -1;
  243. }
  244. int cmd_ftrace(int argc, const char **argv)
  245. {
  246. int ret;
  247. struct perf_ftrace ftrace = {
  248. .tracer = DEFAULT_TRACER,
  249. .target = { .uid = UINT_MAX, },
  250. };
  251. const char * const ftrace_usage[] = {
  252. "perf ftrace [<options>] [<command>]",
  253. "perf ftrace [<options>] -- <command> [<options>]",
  254. NULL
  255. };
  256. const struct option ftrace_options[] = {
  257. OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
  258. "tracer to use: function_graph(default) or function"),
  259. OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
  260. "trace on existing process id"),
  261. OPT_INCR('v', "verbose", &verbose,
  262. "be more verbose"),
  263. OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
  264. "system-wide collection from all CPUs"),
  265. OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
  266. "list of cpus to monitor"),
  267. OPT_END()
  268. };
  269. ret = perf_config(perf_ftrace_config, &ftrace);
  270. if (ret < 0)
  271. return -1;
  272. argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
  273. PARSE_OPT_STOP_AT_NON_OPTION);
  274. if (!argc && target__none(&ftrace.target))
  275. usage_with_options(ftrace_usage, ftrace_options);
  276. ret = target__validate(&ftrace.target);
  277. if (ret) {
  278. char errbuf[512];
  279. target__strerror(&ftrace.target, ret, errbuf, 512);
  280. pr_err("%s\n", errbuf);
  281. return -EINVAL;
  282. }
  283. ftrace.evlist = perf_evlist__new();
  284. if (ftrace.evlist == NULL)
  285. return -ENOMEM;
  286. ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
  287. if (ret < 0)
  288. goto out_delete_evlist;
  289. ret = __cmd_ftrace(&ftrace, argc, argv);
  290. out_delete_evlist:
  291. perf_evlist__delete(ftrace.evlist);
  292. return ret;
  293. }