trace_stat.c 7.2 KB

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