trace_stat.c 7.5 KB

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