trace_stat.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 *prev_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. /*
  72. * The first entry. Actually this is the second, but the first
  73. * one (the stat_list head) is pointless.
  74. */
  75. new_entry = kmalloc(sizeof(struct trace_stat_list), GFP_KERNEL);
  76. if (!new_entry) {
  77. ret = -ENOMEM;
  78. goto exit;
  79. }
  80. INIT_LIST_HEAD(&new_entry->list);
  81. list_add(&new_entry->list, &session->stat_list);
  82. new_entry->stat = ts->stat_start();
  83. prev_stat = new_entry->stat;
  84. /*
  85. * Iterate over the tracer stat entries and store them in a sorted
  86. * list.
  87. */
  88. for (i = 1; ; i++) {
  89. new_entry = kmalloc(sizeof(struct trace_stat_list), GFP_KERNEL);
  90. if (!new_entry) {
  91. ret = -ENOMEM;
  92. goto exit_free_list;
  93. }
  94. INIT_LIST_HEAD(&new_entry->list);
  95. new_entry->stat = ts->stat_next(prev_stat, i);
  96. /* End of insertion */
  97. if (!new_entry->stat)
  98. break;
  99. list_for_each_entry(iter_entry, &session->stat_list, list) {
  100. /* Insertion with a descendent sorting */
  101. if (ts->stat_cmp(new_entry->stat,
  102. iter_entry->stat) > 0) {
  103. list_add_tail(&new_entry->list,
  104. &iter_entry->list);
  105. break;
  106. /* The current smaller value */
  107. } else if (list_is_last(&iter_entry->list,
  108. &session->stat_list)) {
  109. list_add(&new_entry->list, &iter_entry->list);
  110. break;
  111. }
  112. }
  113. prev_stat = new_entry->stat;
  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. session->ts->stat_headers(s);
  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. return seq_list_next(p, &session->stat_list, pos);
  137. }
  138. static void stat_seq_stop(struct seq_file *s, void *p)
  139. {
  140. struct tracer_stat_session *session = s->private;
  141. mutex_unlock(&session->stat_mutex);
  142. }
  143. static int stat_seq_show(struct seq_file *s, void *v)
  144. {
  145. struct tracer_stat_session *session = s->private;
  146. struct trace_stat_list *l = list_entry(v, struct trace_stat_list, list);
  147. return session->ts->stat_show(s, l->stat);
  148. }
  149. static const struct seq_operations trace_stat_seq_ops = {
  150. .start = stat_seq_start,
  151. .next = stat_seq_next,
  152. .stop = stat_seq_stop,
  153. .show = stat_seq_show
  154. };
  155. /* The session stat is refilled and resorted at each stat file opening */
  156. static int tracing_stat_open(struct inode *inode, struct file *file)
  157. {
  158. int ret;
  159. struct tracer_stat_session *session = inode->i_private;
  160. ret = seq_open(file, &trace_stat_seq_ops);
  161. if (!ret) {
  162. struct seq_file *m = file->private_data;
  163. m->private = session;
  164. ret = stat_seq_init(session);
  165. }
  166. return ret;
  167. }
  168. /*
  169. * Avoid consuming memory with our now useless list.
  170. */
  171. static int tracing_stat_release(struct inode *i, struct file *f)
  172. {
  173. struct tracer_stat_session *session = i->i_private;
  174. mutex_lock(&session->stat_mutex);
  175. reset_stat_session(session);
  176. mutex_unlock(&session->stat_mutex);
  177. return 0;
  178. }
  179. static const struct file_operations tracing_stat_fops = {
  180. .open = tracing_stat_open,
  181. .read = seq_read,
  182. .llseek = seq_lseek,
  183. .release = tracing_stat_release
  184. };
  185. static int tracing_stat_init(void)
  186. {
  187. struct dentry *d_tracing;
  188. d_tracing = tracing_init_dentry();
  189. stat_dir = debugfs_create_dir("trace_stat", d_tracing);
  190. if (!stat_dir)
  191. pr_warning("Could not create debugfs "
  192. "'trace_stat' entry\n");
  193. return 0;
  194. }
  195. static int init_stat_file(struct tracer_stat_session *session)
  196. {
  197. if (!stat_dir && tracing_stat_init())
  198. return -ENODEV;
  199. session->file = debugfs_create_file(session->ts->name, 0644,
  200. stat_dir,
  201. session, &tracing_stat_fops);
  202. if (!session->file)
  203. return -ENOMEM;
  204. return 0;
  205. }
  206. int register_stat_tracer(struct tracer_stat *trace)
  207. {
  208. struct tracer_stat_session *session, *node, *tmp;
  209. int ret;
  210. if (!trace)
  211. return -EINVAL;
  212. if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
  213. return -EINVAL;
  214. /* Already registered? */
  215. mutex_lock(&all_stat_sessions_mutex);
  216. list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
  217. if (node->ts == trace) {
  218. mutex_unlock(&all_stat_sessions_mutex);
  219. return -EINVAL;
  220. }
  221. }
  222. mutex_unlock(&all_stat_sessions_mutex);
  223. /* Init the session */
  224. session = kmalloc(sizeof(struct tracer_stat_session), GFP_KERNEL);
  225. if (!session)
  226. return -ENOMEM;
  227. session->ts = trace;
  228. INIT_LIST_HEAD(&session->session_list);
  229. INIT_LIST_HEAD(&session->stat_list);
  230. mutex_init(&session->stat_mutex);
  231. session->file = NULL;
  232. ret = init_stat_file(session);
  233. if (ret) {
  234. destroy_session(session);
  235. return ret;
  236. }
  237. /* Register */
  238. mutex_lock(&all_stat_sessions_mutex);
  239. list_add_tail(&session->session_list, &all_stat_sessions);
  240. mutex_unlock(&all_stat_sessions_mutex);
  241. return 0;
  242. }
  243. void unregister_stat_tracer(struct tracer_stat *trace)
  244. {
  245. struct tracer_stat_session *node, *tmp;
  246. mutex_lock(&all_stat_sessions_mutex);
  247. list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
  248. if (node->ts == trace) {
  249. list_del(&node->session_list);
  250. destroy_session(node);
  251. break;
  252. }
  253. }
  254. mutex_unlock(&all_stat_sessions_mutex);
  255. }