trace_stat.c 7.3 KB

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