trace_stat.c 8.2 KB

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