trace_functions_graph.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. *
  3. * Function graph tracer.
  4. * Copyright (c) 2008 Frederic Weisbecker <fweisbec@gmail.com>
  5. * Mostly borrowed from function tracer which
  6. * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
  7. *
  8. */
  9. #include <linux/debugfs.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/ftrace.h>
  12. #include <linux/fs.h>
  13. #include "trace.h"
  14. #define TRACE_GRAPH_INDENT 2
  15. /* Flag options */
  16. #define TRACE_GRAPH_PRINT_OVERRUN 0x1
  17. #define TRACE_GRAPH_PRINT_CPU 0x2
  18. #define TRACE_GRAPH_PRINT_OVERHEAD 0x4
  19. #define TRACE_GRAPH_PRINT_PROC 0x8
  20. static struct tracer_opt trace_opts[] = {
  21. /* Display overruns ? */
  22. { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
  23. /* Display CPU ? */
  24. { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
  25. /* Display Overhead ? */
  26. { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
  27. /* Display proc name/pid */
  28. { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
  29. { } /* Empty entry */
  30. };
  31. static struct tracer_flags tracer_flags = {
  32. /* Don't display overruns and proc by default */
  33. .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD,
  34. .opts = trace_opts
  35. };
  36. /* pid on the last trace processed */
  37. static pid_t last_pid[NR_CPUS] = { [0 ... NR_CPUS-1] = -1 };
  38. static int graph_trace_init(struct trace_array *tr)
  39. {
  40. int cpu, ret;
  41. for_each_online_cpu(cpu)
  42. tracing_reset(tr, cpu);
  43. ret = register_ftrace_graph(&trace_graph_return,
  44. &trace_graph_entry);
  45. if (ret)
  46. return ret;
  47. tracing_start_cmdline_record();
  48. return 0;
  49. }
  50. static void graph_trace_reset(struct trace_array *tr)
  51. {
  52. tracing_stop_cmdline_record();
  53. unregister_ftrace_graph();
  54. }
  55. static inline int log10_cpu(int nb)
  56. {
  57. if (nb / 100)
  58. return 3;
  59. if (nb / 10)
  60. return 2;
  61. return 1;
  62. }
  63. static enum print_line_t
  64. print_graph_cpu(struct trace_seq *s, int cpu)
  65. {
  66. int i;
  67. int ret;
  68. int log10_this = log10_cpu(cpu);
  69. int log10_all = log10_cpu(cpus_weight_nr(cpu_online_map));
  70. /*
  71. * Start with a space character - to make it stand out
  72. * to the right a bit when trace output is pasted into
  73. * email:
  74. */
  75. ret = trace_seq_printf(s, " ");
  76. /*
  77. * Tricky - we space the CPU field according to the max
  78. * number of online CPUs. On a 2-cpu system it would take
  79. * a maximum of 1 digit - on a 128 cpu system it would
  80. * take up to 3 digits:
  81. */
  82. for (i = 0; i < log10_all - log10_this; i++) {
  83. ret = trace_seq_printf(s, " ");
  84. if (!ret)
  85. return TRACE_TYPE_PARTIAL_LINE;
  86. }
  87. ret = trace_seq_printf(s, "%d) ", cpu);
  88. if (!ret)
  89. return TRACE_TYPE_PARTIAL_LINE;
  90. return TRACE_TYPE_HANDLED;
  91. }
  92. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  93. static enum print_line_t
  94. print_graph_proc(struct trace_seq *s, pid_t pid)
  95. {
  96. int i;
  97. int ret;
  98. int len;
  99. char comm[8];
  100. int spaces = 0;
  101. /* sign + log10(MAX_INT) + '\0' */
  102. char pid_str[11];
  103. strncpy(comm, trace_find_cmdline(pid), 7);
  104. comm[7] = '\0';
  105. sprintf(pid_str, "%d", pid);
  106. /* 1 stands for the "-" character */
  107. len = strlen(comm) + strlen(pid_str) + 1;
  108. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  109. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  110. /* First spaces to align center */
  111. for (i = 0; i < spaces / 2; i++) {
  112. ret = trace_seq_printf(s, " ");
  113. if (!ret)
  114. return TRACE_TYPE_PARTIAL_LINE;
  115. }
  116. ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
  117. if (!ret)
  118. return TRACE_TYPE_PARTIAL_LINE;
  119. /* Last spaces to align center */
  120. for (i = 0; i < spaces - (spaces / 2); i++) {
  121. ret = trace_seq_printf(s, " ");
  122. if (!ret)
  123. return TRACE_TYPE_PARTIAL_LINE;
  124. }
  125. return TRACE_TYPE_HANDLED;
  126. }
  127. /* If the pid changed since the last trace, output this event */
  128. static enum print_line_t
  129. verif_pid(struct trace_seq *s, pid_t pid, int cpu)
  130. {
  131. pid_t prev_pid;
  132. int ret;
  133. if (last_pid[cpu] != -1 && last_pid[cpu] == pid)
  134. return TRACE_TYPE_HANDLED;
  135. prev_pid = last_pid[cpu];
  136. last_pid[cpu] = pid;
  137. /*
  138. * Context-switch trace line:
  139. ------------------------------------------
  140. | 1) migration/0--1 => sshd-1755
  141. ------------------------------------------
  142. */
  143. ret = trace_seq_printf(s,
  144. "\n ------------------------------------------\n |");
  145. if (!ret)
  146. TRACE_TYPE_PARTIAL_LINE;
  147. ret = print_graph_cpu(s, cpu);
  148. if (ret == TRACE_TYPE_PARTIAL_LINE)
  149. TRACE_TYPE_PARTIAL_LINE;
  150. ret = print_graph_proc(s, prev_pid);
  151. if (ret == TRACE_TYPE_PARTIAL_LINE)
  152. TRACE_TYPE_PARTIAL_LINE;
  153. ret = trace_seq_printf(s, " => ");
  154. if (!ret)
  155. TRACE_TYPE_PARTIAL_LINE;
  156. ret = print_graph_proc(s, pid);
  157. if (ret == TRACE_TYPE_PARTIAL_LINE)
  158. TRACE_TYPE_PARTIAL_LINE;
  159. ret = trace_seq_printf(s,
  160. "\n ------------------------------------------\n\n");
  161. if (!ret)
  162. TRACE_TYPE_PARTIAL_LINE;
  163. return ret;
  164. }
  165. static bool
  166. trace_branch_is_leaf(struct trace_iterator *iter,
  167. struct ftrace_graph_ent_entry *curr)
  168. {
  169. struct ring_buffer_iter *ring_iter;
  170. struct ring_buffer_event *event;
  171. struct ftrace_graph_ret_entry *next;
  172. ring_iter = iter->buffer_iter[iter->cpu];
  173. if (!ring_iter)
  174. return false;
  175. event = ring_buffer_iter_peek(ring_iter, NULL);
  176. if (!event)
  177. return false;
  178. next = ring_buffer_event_data(event);
  179. if (next->ent.type != TRACE_GRAPH_RET)
  180. return false;
  181. if (curr->ent.pid != next->ent.pid ||
  182. curr->graph_ent.func != next->ret.func)
  183. return false;
  184. return true;
  185. }
  186. static inline int
  187. print_graph_duration(unsigned long long duration, struct trace_seq *s)
  188. {
  189. unsigned long nsecs_rem = do_div(duration, 1000);
  190. return trace_seq_printf(s, "%4llu.%3lu us | ", duration, nsecs_rem);
  191. }
  192. /* Signal a overhead of time execution to the output */
  193. static int
  194. print_graph_overhead(unsigned long long duration, struct trace_seq *s)
  195. {
  196. /* Duration exceeded 100 msecs */
  197. if (duration > 100000ULL)
  198. return trace_seq_printf(s, "! ");
  199. /* Duration exceeded 10 msecs */
  200. if (duration > 10000ULL)
  201. return trace_seq_printf(s, "+ ");
  202. return trace_seq_printf(s, " ");
  203. }
  204. /* Case of a leaf function on its call entry */
  205. static enum print_line_t
  206. print_graph_entry_leaf(struct trace_iterator *iter,
  207. struct ftrace_graph_ent_entry *entry, struct trace_seq *s)
  208. {
  209. struct ftrace_graph_ret_entry *ret_entry;
  210. struct ftrace_graph_ret *graph_ret;
  211. struct ring_buffer_event *event;
  212. struct ftrace_graph_ent *call;
  213. unsigned long long duration;
  214. int ret;
  215. int i;
  216. event = ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
  217. ret_entry = ring_buffer_event_data(event);
  218. graph_ret = &ret_entry->ret;
  219. call = &entry->graph_ent;
  220. duration = graph_ret->rettime - graph_ret->calltime;
  221. /* Must not exceed 8 characters: 9999.999 us */
  222. if (duration > 10000000ULL)
  223. duration = 9999999ULL;
  224. /* Overhead */
  225. if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
  226. ret = print_graph_overhead(duration, s);
  227. if (!ret)
  228. return TRACE_TYPE_PARTIAL_LINE;
  229. }
  230. /* Duration */
  231. ret = print_graph_duration(duration, s);
  232. if (!ret)
  233. return TRACE_TYPE_PARTIAL_LINE;
  234. /* Function */
  235. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
  236. ret = trace_seq_printf(s, " ");
  237. if (!ret)
  238. return TRACE_TYPE_PARTIAL_LINE;
  239. }
  240. ret = seq_print_ip_sym(s, call->func, 0);
  241. if (!ret)
  242. return TRACE_TYPE_PARTIAL_LINE;
  243. ret = trace_seq_printf(s, "();\n");
  244. if (!ret)
  245. return TRACE_TYPE_PARTIAL_LINE;
  246. return TRACE_TYPE_HANDLED;
  247. }
  248. static enum print_line_t
  249. print_graph_entry_nested(struct ftrace_graph_ent_entry *entry,
  250. struct trace_seq *s)
  251. {
  252. int i;
  253. int ret;
  254. struct ftrace_graph_ent *call = &entry->graph_ent;
  255. /* No overhead */
  256. if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
  257. ret = trace_seq_printf(s, " ");
  258. if (!ret)
  259. return TRACE_TYPE_PARTIAL_LINE;
  260. }
  261. /* No time */
  262. ret = trace_seq_printf(s, " | ");
  263. /* Function */
  264. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
  265. ret = trace_seq_printf(s, " ");
  266. if (!ret)
  267. return TRACE_TYPE_PARTIAL_LINE;
  268. }
  269. ret = seq_print_ip_sym(s, call->func, 0);
  270. if (!ret)
  271. return TRACE_TYPE_PARTIAL_LINE;
  272. ret = trace_seq_printf(s, "() {\n");
  273. if (!ret)
  274. return TRACE_TYPE_PARTIAL_LINE;
  275. return TRACE_TYPE_HANDLED;
  276. }
  277. static enum print_line_t
  278. print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
  279. struct trace_iterator *iter, int cpu)
  280. {
  281. int ret;
  282. struct trace_entry *ent = iter->ent;
  283. /* Pid */
  284. if (verif_pid(s, ent->pid, cpu) == TRACE_TYPE_PARTIAL_LINE)
  285. return TRACE_TYPE_PARTIAL_LINE;
  286. /* Cpu */
  287. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
  288. ret = print_graph_cpu(s, cpu);
  289. if (ret == TRACE_TYPE_PARTIAL_LINE)
  290. return TRACE_TYPE_PARTIAL_LINE;
  291. }
  292. /* Proc */
  293. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
  294. ret = print_graph_proc(s, ent->pid);
  295. if (ret == TRACE_TYPE_PARTIAL_LINE)
  296. return TRACE_TYPE_PARTIAL_LINE;
  297. ret = trace_seq_printf(s, " | ");
  298. if (!ret)
  299. return TRACE_TYPE_PARTIAL_LINE;
  300. }
  301. if (trace_branch_is_leaf(iter, field))
  302. return print_graph_entry_leaf(iter, field, s);
  303. else
  304. return print_graph_entry_nested(field, s);
  305. }
  306. static enum print_line_t
  307. print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
  308. struct trace_entry *ent, int cpu)
  309. {
  310. int i;
  311. int ret;
  312. unsigned long long duration = trace->rettime - trace->calltime;
  313. /* Must not exceed 8 characters: xxxx.yyy us */
  314. if (duration > 10000000ULL)
  315. duration = 9999999ULL;
  316. /* Pid */
  317. if (verif_pid(s, ent->pid, cpu) == TRACE_TYPE_PARTIAL_LINE)
  318. return TRACE_TYPE_PARTIAL_LINE;
  319. /* Cpu */
  320. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
  321. ret = print_graph_cpu(s, cpu);
  322. if (ret == TRACE_TYPE_PARTIAL_LINE)
  323. return TRACE_TYPE_PARTIAL_LINE;
  324. }
  325. /* Proc */
  326. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
  327. ret = print_graph_proc(s, ent->pid);
  328. if (ret == TRACE_TYPE_PARTIAL_LINE)
  329. return TRACE_TYPE_PARTIAL_LINE;
  330. ret = trace_seq_printf(s, " | ");
  331. if (!ret)
  332. return TRACE_TYPE_PARTIAL_LINE;
  333. }
  334. /* Overhead */
  335. if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
  336. ret = print_graph_overhead(duration, s);
  337. if (!ret)
  338. return TRACE_TYPE_PARTIAL_LINE;
  339. }
  340. /* Duration */
  341. ret = print_graph_duration(duration, s);
  342. if (!ret)
  343. return TRACE_TYPE_PARTIAL_LINE;
  344. /* Closing brace */
  345. for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
  346. ret = trace_seq_printf(s, " ");
  347. if (!ret)
  348. return TRACE_TYPE_PARTIAL_LINE;
  349. }
  350. ret = trace_seq_printf(s, "}\n");
  351. if (!ret)
  352. return TRACE_TYPE_PARTIAL_LINE;
  353. /* Overrun */
  354. if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
  355. ret = trace_seq_printf(s, " (Overruns: %lu)\n",
  356. trace->overrun);
  357. if (!ret)
  358. return TRACE_TYPE_PARTIAL_LINE;
  359. }
  360. return TRACE_TYPE_HANDLED;
  361. }
  362. enum print_line_t
  363. print_graph_function(struct trace_iterator *iter)
  364. {
  365. struct trace_seq *s = &iter->seq;
  366. struct trace_entry *entry = iter->ent;
  367. switch (entry->type) {
  368. case TRACE_GRAPH_ENT: {
  369. struct ftrace_graph_ent_entry *field;
  370. trace_assign_type(field, entry);
  371. return print_graph_entry(field, s, iter,
  372. iter->cpu);
  373. }
  374. case TRACE_GRAPH_RET: {
  375. struct ftrace_graph_ret_entry *field;
  376. trace_assign_type(field, entry);
  377. return print_graph_return(&field->ret, s, entry, iter->cpu);
  378. }
  379. default:
  380. return TRACE_TYPE_UNHANDLED;
  381. }
  382. }
  383. static struct tracer graph_trace __read_mostly = {
  384. .name = "function_graph",
  385. .init = graph_trace_init,
  386. .reset = graph_trace_reset,
  387. .print_line = print_graph_function,
  388. .flags = &tracer_flags,
  389. };
  390. static __init int init_graph_trace(void)
  391. {
  392. return register_tracer(&graph_trace);
  393. }
  394. device_initcall(init_graph_trace);