trace_stat.c 8.2 KB

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