builtin-ftrace.c 7.6 KB

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