trace_stat.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Infrastructure for statistic tracing (histogram output).
  3. *
  4. * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com>
  5. *
  6. * Based on the code from trace_branch.c which is
  7. * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
  8. *
  9. */
  10. #include <linux/list.h>
  11. #include <linux/debugfs.h>
  12. #include "trace_stat.h"
  13. #include "trace.h"
  14. /* List of stat entries from a tracer */
  15. struct trace_stat_list {
  16. struct list_head list;
  17. void *stat;
  18. };
  19. /* A stat session is the stats output in one file */
  20. struct tracer_stat_session {
  21. struct list_head session_list;
  22. struct tracer_stat *ts;
  23. struct list_head stat_list;
  24. struct mutex stat_mutex;
  25. struct dentry *file;
  26. };
  27. /* All of the sessions currently in use. Each stat file embed one session */
  28. static LIST_HEAD(all_stat_sessions);
  29. static DEFINE_MUTEX(all_stat_sessions_mutex);
  30. /* The root directory for all stat files */
  31. static struct dentry *stat_dir;
  32. static void reset_stat_session(struct tracer_stat_session *session)
  33. {
  34. struct trace_stat_list *node, *next;
  35. list_for_each_entry_safe(node, next, &session->stat_list, list)
  36. kfree(node);
  37. INIT_LIST_HEAD(&session->stat_list);
  38. }
  39. static void destroy_session(struct tracer_stat_session *session)
  40. {
  41. debugfs_remove(session->file);
  42. reset_stat_session(session);
  43. mutex_destroy(&session->stat_mutex);
  44. kfree(session);
  45. }
  46. /*
  47. * For tracers that don't provide a stat_cmp callback.
  48. * This one will force an immediate insertion on tail of
  49. * the list.
  50. */
  51. static int dummy_cmp(void *p1, void *p2)
  52. {
  53. return 1;
  54. }
  55. /*
  56. * Initialize the stat list at each trace_stat file opening.
  57. * All of these copies and sorting are required on all opening
  58. * since the stats could have changed between two file sessions.
  59. */
  60. static int stat_seq_init(struct tracer_stat_session *session)
  61. {
  62. struct trace_stat_list *iter_entry, *new_entry;
  63. struct tracer_stat *ts = session->ts;
  64. void *stat;
  65. int ret = 0;
  66. int i;
  67. mutex_lock(&session->stat_mutex);
  68. reset_stat_session(session);
  69. if (!ts->stat_cmp)
  70. ts->stat_cmp = dummy_cmp;
  71. stat = ts->stat_start();
  72. if (!stat)
  73. goto exit;
  74. /*
  75. * The first entry. Actually this is the second, but the first
  76. * one (the stat_list head) is pointless.
  77. */
  78. new_entry = kmalloc(sizeof(struct trace_stat_list), GFP_KERNEL);
  79. if (!new_entry) {
  80. ret = -ENOMEM;
  81. goto exit;
  82. }
  83. INIT_LIST_HEAD(&new_entry->list);
  84. list_add(&new_entry->list, &session->stat_list);
  85. new_entry->stat = stat;
  86. /*
  87. * Iterate over the tracer stat entries and store them in a sorted
  88. * list.
  89. */
  90. for (i = 1; ; i++) {
  91. stat = ts->stat_next(stat, i);
  92. /* End of insertion */
  93. if (!stat)
  94. break;
  95. new_entry = kmalloc(sizeof(struct trace_stat_list), GFP_KERNEL);
  96. if (!new_entry) {
  97. ret = -ENOMEM;
  98. goto exit_free_list;
  99. }
  100. INIT_LIST_HEAD(&new_entry->list);
  101. new_entry->stat = stat;
  102. list_for_each_entry_reverse(iter_entry, &session->stat_list,
  103. list) {
  104. /* Insertion with a descendent sorting */
  105. if (ts->stat_cmp(iter_entry->stat,
  106. new_entry->stat) >= 0) {
  107. list_add(&new_entry->list, &iter_entry->list);
  108. break;
  109. }
  110. }
  111. /* The current larger value */
  112. if (list_empty(&new_entry->list))
  113. list_add(&new_entry->list, &session->stat_list);
  114. }
  115. exit:
  116. mutex_unlock(&session->stat_mutex);
  117. return ret;
  118. exit_free_list:
  119. reset_stat_session(session);
  120. mutex_unlock(&session->stat_mutex);
  121. return ret;
  122. }
  123. static void *stat_seq_start(struct seq_file *s, loff_t *pos)
  124. {
  125. struct tracer_stat_session *session = s->private;
  126. /* Prevent from tracer switch or stat_list modification */
  127. mutex_lock(&session->stat_mutex);
  128. /* If we are in the beginning of the file, print the headers */
  129. if (!*pos && session->ts->stat_headers)
  130. return SEQ_START_TOKEN;
  131. return seq_list_start(&session->stat_list, *pos);
  132. }
  133. static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
  134. {
  135. struct tracer_stat_session *session = s->private;
  136. if (p == SEQ_START_TOKEN)
  137. return seq_list_start(&session->stat_list, *pos);
  138. return seq_list_next(p, &session->stat_list, pos);
  139. }
  140. static void stat_seq_stop(struct seq_file *s, void *p)
  141. {
  142. struct tracer_stat_session *session = s->private;
  143. mutex_unlock(&session->stat_mutex);
  144. }
  145. static int stat_seq_show(struct seq_file *s, void *v)
  146. {
  147. struct tracer_stat_session *session = s->private;
  148. struct trace_stat_list *l = list_entry(v, struct trace_stat_list, list);
  149. if (v == SEQ_START_TOKEN)
  150. return session->ts->stat_headers(s);
  151. return session->ts->stat_show(s, l->stat);
  152. }
  153. static const struct seq_operations trace_stat_seq_ops = {
  154. .start = stat_seq_start,
  155. .next = stat_seq_next,
  156. .stop = stat_seq_stop,
  157. .show = stat_seq_show
  158. };
  159. /* The session stat is refilled and resorted at each stat file opening */
  160. static int tracing_stat_open(struct inode *inode, struct file *file)
  161. {
  162. int ret;
  163. struct tracer_stat_session *session = inode->i_private;
  164. ret = seq_open(file, &trace_stat_seq_ops);
  165. if (!ret) {
  166. struct seq_file *m = file->private_data;
  167. m->private = session;
  168. ret = stat_seq_init(session);
  169. }
  170. return ret;
  171. }
  172. /*
  173. * Avoid consuming memory with our now useless list.
  174. */
  175. static int tracing_stat_release(struct inode *i, struct file *f)
  176. {
  177. struct tracer_stat_session *session = i->i_private;
  178. mutex_lock(&session->stat_mutex);
  179. reset_stat_session(session);
  180. mutex_unlock(&session->stat_mutex);
  181. return 0;
  182. }
  183. static const struct file_operations tracing_stat_fops = {
  184. .open = tracing_stat_open,
  185. .read = seq_read,
  186. .llseek = seq_lseek,
  187. .release = tracing_stat_release
  188. };
  189. static int tracing_stat_init(void)
  190. {
  191. struct dentry *d_tracing;
  192. d_tracing = tracing_init_dentry();
  193. stat_dir = debugfs_create_dir("trace_stat", d_tracing);
  194. if (!stat_dir)
  195. pr_warning("Could not create debugfs "
  196. "'trace_stat' entry\n");
  197. return 0;
  198. }
  199. static int init_stat_file(struct tracer_stat_session *session)
  200. {
  201. if (!stat_dir && tracing_stat_init())
  202. return -ENODEV;
  203. session->file = debugfs_create_file(session->ts->name, 0644,
  204. stat_dir,
  205. session, &tracing_stat_fops);
  206. if (!session->file)
  207. return -ENOMEM;
  208. return 0;
  209. }
  210. int register_stat_tracer(struct tracer_stat *trace)
  211. {
  212. struct tracer_stat_session *session, *node, *tmp;
  213. int ret;
  214. if (!trace)
  215. return -EINVAL;
  216. if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
  217. return -EINVAL;
  218. /* Already registered? */
  219. mutex_lock(&all_stat_sessions_mutex);
  220. list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
  221. if (node->ts == trace) {
  222. mutex_unlock(&all_stat_sessions_mutex);
  223. return -EINVAL;
  224. }
  225. }
  226. mutex_unlock(&all_stat_sessions_mutex);
  227. /* Init the session */
  228. session = kmalloc(sizeof(struct tracer_stat_session), GFP_KERNEL);
  229. if (!session)
  230. return -ENOMEM;
  231. session->ts = trace;
  232. INIT_LIST_HEAD(&session->session_list);
  233. INIT_LIST_HEAD(&session->stat_list);
  234. mutex_init(&session->stat_mutex);
  235. session->file = NULL;
  236. ret = init_stat_file(session);
  237. if (ret) {
  238. destroy_session(session);
  239. return ret;
  240. }
  241. /* Register */
  242. mutex_lock(&all_stat_sessions_mutex);
  243. list_add_tail(&session->session_list, &all_stat_sessions);
  244. mutex_unlock(&all_stat_sessions_mutex);
  245. return 0;
  246. }
  247. void unregister_stat_tracer(struct tracer_stat *trace)
  248. {
  249. struct tracer_stat_session *node, *tmp;
  250. mutex_lock(&all_stat_sessions_mutex);
  251. list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
  252. if (node->ts == trace) {
  253. list_del(&node->session_list);
  254. destroy_session(node);
  255. break;
  256. }
  257. }
  258. mutex_unlock(&all_stat_sessions_mutex);
  259. }