trace_stat.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Infrastructure for statistic tracing (histogram output).
  3. *
  4. * Copyright (C) 2008-2009 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/rbtree.h>
  12. #include <linux/debugfs.h>
  13. #include "trace_stat.h"
  14. #include "trace.h"
  15. /*
  16. * List of stat red-black nodes from a tracer
  17. * We use a such tree to sort quickly the stat
  18. * entries from the tracer.
  19. */
  20. struct stat_node {
  21. struct rb_node node;
  22. void *stat;
  23. };
  24. /* A stat session is the stats output in one file */
  25. struct stat_session {
  26. struct list_head session_list;
  27. struct tracer_stat *ts;
  28. struct rb_root stat_root;
  29. struct mutex stat_mutex;
  30. struct dentry *file;
  31. };
  32. /* All of the sessions currently in use. Each stat file embed one session */
  33. static LIST_HEAD(all_stat_sessions);
  34. static DEFINE_MUTEX(all_stat_sessions_mutex);
  35. /* The root directory for all stat files */
  36. static struct dentry *stat_dir;
  37. /*
  38. * Iterate through the rbtree using a post order traversal path
  39. * to release the next node.
  40. * It won't necessary release one at each iteration
  41. * but it will at least advance closer to the next one
  42. * to be released.
  43. */
  44. static struct rb_node *release_next(struct rb_node *node)
  45. {
  46. struct stat_node *snode;
  47. struct rb_node *parent = rb_parent(node);
  48. if (node->rb_left)
  49. return node->rb_left;
  50. else if (node->rb_right)
  51. return node->rb_right;
  52. else {
  53. if (!parent)
  54. ;
  55. else if (parent->rb_left == node)
  56. parent->rb_left = NULL;
  57. else
  58. parent->rb_right = NULL;
  59. snode = container_of(node, struct stat_node, node);
  60. kfree(snode);
  61. return parent;
  62. }
  63. }
  64. static void reset_stat_session(struct stat_session *session)
  65. {
  66. struct rb_node *node = session->stat_root.rb_node;
  67. while (node)
  68. node = release_next(node);
  69. session->stat_root = RB_ROOT;
  70. }
  71. static void destroy_session(struct stat_session *session)
  72. {
  73. debugfs_remove(session->file);
  74. reset_stat_session(session);
  75. mutex_destroy(&session->stat_mutex);
  76. kfree(session);
  77. }
  78. typedef int (*cmp_stat_t)(void *, void *);
  79. static int insert_stat(struct rb_root *root, void *stat, cmp_stat_t cmp)
  80. {
  81. struct rb_node **new = &(root->rb_node), *parent = NULL;
  82. struct stat_node *data;
  83. data = kzalloc(sizeof(*data), GFP_KERNEL);
  84. if (!data)
  85. return -ENOMEM;
  86. data->stat = stat;
  87. /*
  88. * Figure out where to put new node
  89. * This is a descendent sorting
  90. */
  91. while (*new) {
  92. struct stat_node *this;
  93. int result;
  94. this = container_of(*new, struct stat_node, node);
  95. result = cmp(data->stat, this->stat);
  96. parent = *new;
  97. if (result >= 0)
  98. new = &((*new)->rb_left);
  99. else
  100. new = &((*new)->rb_right);
  101. }
  102. rb_link_node(&data->node, parent, new);
  103. rb_insert_color(&data->node, root);
  104. return 0;
  105. }
  106. /*
  107. * For tracers that don't provide a stat_cmp callback.
  108. * This one will force an insertion as right-most node
  109. * in the rbtree.
  110. */
  111. static int dummy_cmp(void *p1, void *p2)
  112. {
  113. return -1;
  114. }
  115. /*
  116. * Initialize the stat rbtree at each trace_stat file opening.
  117. * All of these copies and sorting are required on all opening
  118. * since the stats could have changed between two file sessions.
  119. */
  120. static int stat_seq_init(struct stat_session *session)
  121. {
  122. struct tracer_stat *ts = session->ts;
  123. struct rb_root *root = &session->stat_root;
  124. void *stat;
  125. int ret = 0;
  126. int i;
  127. mutex_lock(&session->stat_mutex);
  128. reset_stat_session(session);
  129. if (!ts->stat_cmp)
  130. ts->stat_cmp = dummy_cmp;
  131. stat = ts->stat_start(ts);
  132. if (!stat)
  133. goto exit;
  134. ret = insert_stat(root, stat, ts->stat_cmp);
  135. if (ret)
  136. goto exit;
  137. /*
  138. * Iterate over the tracer stat entries and store them in an rbtree.
  139. */
  140. for (i = 1; ; i++) {
  141. stat = ts->stat_next(stat, i);
  142. /* End of insertion */
  143. if (!stat)
  144. break;
  145. ret = insert_stat(root, stat, ts->stat_cmp);
  146. if (ret)
  147. goto exit_free_rbtree;
  148. }
  149. exit:
  150. mutex_unlock(&session->stat_mutex);
  151. return ret;
  152. exit_free_rbtree:
  153. reset_stat_session(session);
  154. mutex_unlock(&session->stat_mutex);
  155. return ret;
  156. }
  157. static void *stat_seq_start(struct seq_file *s, loff_t *pos)
  158. {
  159. struct stat_session *session = s->private;
  160. struct rb_node *node;
  161. int i;
  162. /* Prevent from tracer switch or rbtree modification */
  163. mutex_lock(&session->stat_mutex);
  164. /* If we are in the beginning of the file, print the headers */
  165. if (!*pos && session->ts->stat_headers) {
  166. (*pos)++;
  167. return SEQ_START_TOKEN;
  168. }
  169. node = rb_first(&session->stat_root);
  170. for (i = 0; node && i < *pos; i++)
  171. node = rb_next(node);
  172. (*pos)++;
  173. return node;
  174. }
  175. static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
  176. {
  177. struct stat_session *session = s->private;
  178. struct rb_node *node = p;
  179. (*pos)++;
  180. if (p == SEQ_START_TOKEN)
  181. return rb_first(&session->stat_root);
  182. return rb_next(node);
  183. }
  184. static void stat_seq_stop(struct seq_file *s, void *p)
  185. {
  186. struct stat_session *session = s->private;
  187. mutex_unlock(&session->stat_mutex);
  188. }
  189. static int stat_seq_show(struct seq_file *s, void *v)
  190. {
  191. struct stat_session *session = s->private;
  192. struct stat_node *l = container_of(v, struct stat_node, node);
  193. if (v == SEQ_START_TOKEN)
  194. return session->ts->stat_headers(s);
  195. return session->ts->stat_show(s, l->stat);
  196. }
  197. static const struct seq_operations trace_stat_seq_ops = {
  198. .start = stat_seq_start,
  199. .next = stat_seq_next,
  200. .stop = stat_seq_stop,
  201. .show = stat_seq_show
  202. };
  203. /* The session stat is refilled and resorted at each stat file opening */
  204. static int tracing_stat_open(struct inode *inode, struct file *file)
  205. {
  206. int ret;
  207. struct stat_session *session = inode->i_private;
  208. ret = seq_open(file, &trace_stat_seq_ops);
  209. if (!ret) {
  210. struct seq_file *m = file->private_data;
  211. m->private = session;
  212. ret = stat_seq_init(session);
  213. }
  214. return ret;
  215. }
  216. /*
  217. * Avoid consuming memory with our now useless rbtree.
  218. */
  219. static int tracing_stat_release(struct inode *i, struct file *f)
  220. {
  221. struct stat_session *session = i->i_private;
  222. mutex_lock(&session->stat_mutex);
  223. reset_stat_session(session);
  224. mutex_unlock(&session->stat_mutex);
  225. return 0;
  226. }
  227. static const struct file_operations tracing_stat_fops = {
  228. .open = tracing_stat_open,
  229. .read = seq_read,
  230. .llseek = seq_lseek,
  231. .release = tracing_stat_release
  232. };
  233. static int tracing_stat_init(void)
  234. {
  235. struct dentry *d_tracing;
  236. d_tracing = tracing_init_dentry();
  237. stat_dir = debugfs_create_dir("trace_stat", d_tracing);
  238. if (!stat_dir)
  239. pr_warning("Could not create debugfs "
  240. "'trace_stat' entry\n");
  241. return 0;
  242. }
  243. static int init_stat_file(struct stat_session *session)
  244. {
  245. if (!stat_dir && tracing_stat_init())
  246. return -ENODEV;
  247. session->file = debugfs_create_file(session->ts->name, 0644,
  248. stat_dir,
  249. session, &tracing_stat_fops);
  250. if (!session->file)
  251. return -ENOMEM;
  252. return 0;
  253. }
  254. int register_stat_tracer(struct tracer_stat *trace)
  255. {
  256. struct stat_session *session, *node;
  257. int ret;
  258. if (!trace)
  259. return -EINVAL;
  260. if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
  261. return -EINVAL;
  262. /* Already registered? */
  263. mutex_lock(&all_stat_sessions_mutex);
  264. list_for_each_entry(node, &all_stat_sessions, session_list) {
  265. if (node->ts == trace) {
  266. mutex_unlock(&all_stat_sessions_mutex);
  267. return -EINVAL;
  268. }
  269. }
  270. mutex_unlock(&all_stat_sessions_mutex);
  271. /* Init the session */
  272. session = kzalloc(sizeof(*session), GFP_KERNEL);
  273. if (!session)
  274. return -ENOMEM;
  275. session->ts = trace;
  276. INIT_LIST_HEAD(&session->session_list);
  277. mutex_init(&session->stat_mutex);
  278. ret = init_stat_file(session);
  279. if (ret) {
  280. destroy_session(session);
  281. return ret;
  282. }
  283. /* Register */
  284. mutex_lock(&all_stat_sessions_mutex);
  285. list_add_tail(&session->session_list, &all_stat_sessions);
  286. mutex_unlock(&all_stat_sessions_mutex);
  287. return 0;
  288. }
  289. void unregister_stat_tracer(struct tracer_stat *trace)
  290. {
  291. struct stat_session *node, *tmp;
  292. mutex_lock(&all_stat_sessions_mutex);
  293. list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
  294. if (node->ts == trace) {
  295. list_del(&node->session_list);
  296. destroy_session(node);
  297. break;
  298. }
  299. }
  300. mutex_unlock(&all_stat_sessions_mutex);
  301. }