trace_stat.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/security.h>
  12. #include <linux/list.h>
  13. #include <linux/slab.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/tracefs.h>
  16. #include "trace_stat.h"
  17. #include "trace.h"
  18. /*
  19. * List of stat red-black nodes from a tracer
  20. * We use a such tree to sort quickly the stat
  21. * entries from the tracer.
  22. */
  23. struct stat_node {
  24. struct rb_node node;
  25. void *stat;
  26. };
  27. /* A stat session is the stats output in one file */
  28. struct stat_session {
  29. struct list_head session_list;
  30. struct tracer_stat *ts;
  31. struct rb_root stat_root;
  32. struct mutex stat_mutex;
  33. struct dentry *file;
  34. };
  35. /* All of the sessions currently in use. Each stat file embed one session */
  36. static LIST_HEAD(all_stat_sessions);
  37. static DEFINE_MUTEX(all_stat_sessions_mutex);
  38. /* The root directory for all stat files */
  39. static struct dentry *stat_dir;
  40. static void __reset_stat_session(struct stat_session *session)
  41. {
  42. struct stat_node *snode, *n;
  43. rbtree_postorder_for_each_entry_safe(snode, n, &session->stat_root, node) {
  44. if (session->ts->stat_release)
  45. session->ts->stat_release(snode->stat);
  46. kfree(snode);
  47. }
  48. session->stat_root = RB_ROOT;
  49. }
  50. static void reset_stat_session(struct stat_session *session)
  51. {
  52. mutex_lock(&session->stat_mutex);
  53. __reset_stat_session(session);
  54. mutex_unlock(&session->stat_mutex);
  55. }
  56. static void destroy_session(struct stat_session *session)
  57. {
  58. tracefs_remove(session->file);
  59. __reset_stat_session(session);
  60. mutex_destroy(&session->stat_mutex);
  61. kfree(session);
  62. }
  63. static int insert_stat(struct rb_root *root, void *stat, cmp_func_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(const void *p1, const 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 = security_locked_down(LOCKDOWN_TRACEFS);
  195. if (ret)
  196. return ret;
  197. ret = stat_seq_init(session);
  198. if (ret)
  199. return ret;
  200. ret = seq_open(file, &trace_stat_seq_ops);
  201. if (ret) {
  202. reset_stat_session(session);
  203. return ret;
  204. }
  205. m = file->private_data;
  206. m->private = session;
  207. return ret;
  208. }
  209. /*
  210. * Avoid consuming memory with our now useless rbtree.
  211. */
  212. static int tracing_stat_release(struct inode *i, struct file *f)
  213. {
  214. struct stat_session *session = i->i_private;
  215. reset_stat_session(session);
  216. return seq_release(i, f);
  217. }
  218. static const struct file_operations tracing_stat_fops = {
  219. .open = tracing_stat_open,
  220. .read = seq_read,
  221. .llseek = seq_lseek,
  222. .release = tracing_stat_release
  223. };
  224. static int tracing_stat_init(void)
  225. {
  226. int ret;
  227. ret = tracing_init_dentry();
  228. if (ret)
  229. return -ENODEV;
  230. stat_dir = tracefs_create_dir("trace_stat", NULL);
  231. if (!stat_dir) {
  232. pr_warn("Could not create tracefs 'trace_stat' entry\n");
  233. return -ENOMEM;
  234. }
  235. return 0;
  236. }
  237. static int init_stat_file(struct stat_session *session)
  238. {
  239. int ret;
  240. if (!stat_dir && (ret = tracing_stat_init()))
  241. return ret;
  242. session->file = tracefs_create_file(session->ts->name, 0644,
  243. stat_dir,
  244. session, &tracing_stat_fops);
  245. if (!session->file)
  246. return -ENOMEM;
  247. return 0;
  248. }
  249. int register_stat_tracer(struct tracer_stat *trace)
  250. {
  251. struct stat_session *session, *node;
  252. int ret = -EINVAL;
  253. if (!trace)
  254. return -EINVAL;
  255. if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
  256. return -EINVAL;
  257. /* Already registered? */
  258. mutex_lock(&all_stat_sessions_mutex);
  259. list_for_each_entry(node, &all_stat_sessions, session_list) {
  260. if (node->ts == trace)
  261. goto out;
  262. }
  263. ret = -ENOMEM;
  264. /* Init the session */
  265. session = kzalloc(sizeof(*session), GFP_KERNEL);
  266. if (!session)
  267. goto out;
  268. session->ts = trace;
  269. INIT_LIST_HEAD(&session->session_list);
  270. mutex_init(&session->stat_mutex);
  271. ret = init_stat_file(session);
  272. if (ret) {
  273. destroy_session(session);
  274. goto out;
  275. }
  276. ret = 0;
  277. /* Register */
  278. list_add_tail(&session->session_list, &all_stat_sessions);
  279. out:
  280. mutex_unlock(&all_stat_sessions_mutex);
  281. return ret;
  282. }
  283. void unregister_stat_tracer(struct tracer_stat *trace)
  284. {
  285. struct stat_session *node, *tmp;
  286. mutex_lock(&all_stat_sessions_mutex);
  287. list_for_each_entry_safe(node, tmp, &all_stat_sessions, session_list) {
  288. if (node->ts == trace) {
  289. list_del(&node->session_list);
  290. destroy_session(node);
  291. break;
  292. }
  293. }
  294. mutex_unlock(&all_stat_sessions_mutex);
  295. }