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 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(iter_entry, &session->stat_list, list) {
  103. /* Insertion with a descendent sorting */
  104. if (ts->stat_cmp(new_entry->stat,
  105. iter_entry->stat) > 0) {
  106. list_add_tail(&new_entry->list,
  107. &iter_entry->list);
  108. break;
  109. /* The current smaller value */
  110. } else if (list_is_last(&iter_entry->list,
  111. &session->stat_list)) {
  112. list_add(&new_entry->list, &iter_entry->list);
  113. break;
  114. }
  115. }
  116. }
  117. exit:
  118. mutex_unlock(&session->stat_mutex);
  119. return ret;
  120. exit_free_list:
  121. reset_stat_session(session);
  122. mutex_unlock(&session->stat_mutex);
  123. return ret;
  124. }
  125. static void *stat_seq_start(struct seq_file *s, loff_t *pos)
  126. {
  127. struct tracer_stat_session *session = s->private;
  128. /* Prevent from tracer switch or stat_list modification */
  129. mutex_lock(&session->stat_mutex);
  130. /* If we are in the beginning of the file, print the headers */
  131. if (!*pos && session->ts->stat_headers)
  132. session->ts->stat_headers(s);
  133. return seq_list_start(&session->stat_list, *pos);
  134. }
  135. static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
  136. {
  137. struct tracer_stat_session *session = s->private;
  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. return session->ts->stat_show(s, l->stat);
  150. }
  151. static const struct seq_operations trace_stat_seq_ops = {
  152. .start = stat_seq_start,
  153. .next = stat_seq_next,
  154. .stop = stat_seq_stop,
  155. .show = stat_seq_show
  156. };
  157. /* The session stat is refilled and resorted at each stat file opening */
  158. static int tracing_stat_open(struct inode *inode, struct file *file)
  159. {
  160. int ret;
  161. struct tracer_stat_session *session = inode->i_private;
  162. ret = seq_open(file, &trace_stat_seq_ops);
  163. if (!ret) {
  164. struct seq_file *m = file->private_data;
  165. m->private = session;
  166. ret = stat_seq_init(session);
  167. }
  168. return ret;
  169. }
  170. /*
  171. * Avoid consuming memory with our now useless list.
  172. */
  173. static int tracing_stat_release(struct inode *i, struct file *f)
  174. {
  175. struct tracer_stat_session *session = i->i_private;
  176. mutex_lock(&session->stat_mutex);
  177. reset_stat_session(session);
  178. mutex_unlock(&session->stat_mutex);
  179. return 0;
  180. }
  181. static const struct file_operations tracing_stat_fops = {
  182. .open = tracing_stat_open,
  183. .read = seq_read,
  184. .llseek = seq_lseek,
  185. .release = tracing_stat_release
  186. };
  187. static int tracing_stat_init(void)
  188. {
  189. struct dentry *d_tracing;
  190. d_tracing = tracing_init_dentry();
  191. stat_dir = debugfs_create_dir("trace_stat", d_tracing);
  192. if (!stat_dir)
  193. pr_warning("Could not create debugfs "
  194. "'trace_stat' entry\n");
  195. return 0;
  196. }
  197. static int init_stat_file(struct tracer_stat_session *session)
  198. {
  199. if (!stat_dir && tracing_stat_init())
  200. return -ENODEV;
  201. session->file = debugfs_create_file(session->ts->name, 0644,
  202. stat_dir,
  203. session, &tracing_stat_fops);
  204. if (!session->file)
  205. return -ENOMEM;
  206. return 0;
  207. }
  208. int register_stat_tracer(struct tracer_stat *trace)
  209. {
  210. struct tracer_stat_session *session, *node, *tmp;
  211. int ret;
  212. if (!trace)
  213. return -EINVAL;
  214. if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
  215. return -EINVAL;
  216. /* Already registered? */
  217. mutex_lock(&all_stat_sessions_mutex);
  218. list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
  219. if (node->ts == trace) {
  220. mutex_unlock(&all_stat_sessions_mutex);
  221. return -EINVAL;
  222. }
  223. }
  224. mutex_unlock(&all_stat_sessions_mutex);
  225. /* Init the session */
  226. session = kmalloc(sizeof(struct tracer_stat_session), GFP_KERNEL);
  227. if (!session)
  228. return -ENOMEM;
  229. session->ts = trace;
  230. INIT_LIST_HEAD(&session->session_list);
  231. INIT_LIST_HEAD(&session->stat_list);
  232. mutex_init(&session->stat_mutex);
  233. session->file = NULL;
  234. ret = init_stat_file(session);
  235. if (ret) {
  236. destroy_session(session);
  237. return ret;
  238. }
  239. /* Register */
  240. mutex_lock(&all_stat_sessions_mutex);
  241. list_add_tail(&session->session_list, &all_stat_sessions);
  242. mutex_unlock(&all_stat_sessions_mutex);
  243. return 0;
  244. }
  245. void unregister_stat_tracer(struct tracer_stat *trace)
  246. {
  247. struct tracer_stat_session *node, *tmp;
  248. mutex_lock(&all_stat_sessions_mutex);
  249. list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
  250. if (node->ts == trace) {
  251. list_del(&node->session_list);
  252. destroy_session(node);
  253. break;
  254. }
  255. }
  256. mutex_unlock(&all_stat_sessions_mutex);
  257. }