trace_stat.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Infrastructure for statistic tracing (histogram output).
  3. *
  4. * Copyright (C) 2008 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/seq_file.h>
  12. #include <linux/debugfs.h>
  13. #include "trace.h"
  14. /* List of stat entries from a tracer */
  15. struct trace_stat_list {
  16. struct list_head list;
  17. void *stat;
  18. };
  19. /* A stat session is the stats output in one file */
  20. struct tracer_stat_session {
  21. struct tracer_stat *ts;
  22. struct list_head stat_list;
  23. struct mutex stat_mutex;
  24. };
  25. /* All of the sessions currently in use. Each stat file embeed one session */
  26. static struct tracer_stat_session **all_stat_sessions;
  27. static int nb_sessions;
  28. static struct dentry *stat_dir, **stat_files;
  29. static void reset_stat_session(struct tracer_stat_session *session)
  30. {
  31. struct trace_stat_list *node, *next;
  32. list_for_each_entry_safe(node, next, &session->stat_list, list)
  33. kfree(node);
  34. INIT_LIST_HEAD(&session->stat_list);
  35. }
  36. /* Called when a tracer is initialized */
  37. static int init_all_sessions(int nb, struct tracer_stat *ts)
  38. {
  39. int i, j;
  40. struct tracer_stat_session *session;
  41. nb_sessions = 0;
  42. if (all_stat_sessions) {
  43. for (i = 0; i < nb_sessions; i++) {
  44. session = all_stat_sessions[i];
  45. reset_stat_session(session);
  46. mutex_destroy(&session->stat_mutex);
  47. kfree(session);
  48. }
  49. }
  50. all_stat_sessions = kmalloc(sizeof(struct tracer_stat_session *) * nb,
  51. GFP_KERNEL);
  52. if (!all_stat_sessions)
  53. return -ENOMEM;
  54. for (i = 0; i < nb; i++) {
  55. session = kmalloc(sizeof(struct tracer_stat_session) * nb,
  56. GFP_KERNEL);
  57. if (!session)
  58. goto free_sessions;
  59. INIT_LIST_HEAD(&session->stat_list);
  60. mutex_init(&session->stat_mutex);
  61. session->ts = &ts[i];
  62. all_stat_sessions[i] = session;
  63. }
  64. nb_sessions = nb;
  65. return 0;
  66. free_sessions:
  67. for (j = 0; j < i; j++)
  68. kfree(all_stat_sessions[i]);
  69. kfree(all_stat_sessions);
  70. all_stat_sessions = NULL;
  71. return -ENOMEM;
  72. }
  73. static int basic_tracer_stat_checks(struct tracer_stat *ts)
  74. {
  75. int i;
  76. if (!ts)
  77. return 0;
  78. for (i = 0; ts[i].name; i++) {
  79. if (!ts[i].stat_start || !ts[i].stat_next || !ts[i].stat_show)
  80. return -EBUSY;
  81. }
  82. return i;
  83. }
  84. /*
  85. * For tracers that don't provide a stat_cmp callback.
  86. * This one will force an immediate insertion on tail of
  87. * the list.
  88. */
  89. static int dummy_cmp(void *p1, void *p2)
  90. {
  91. return 1;
  92. }
  93. /*
  94. * Initialize the stat list at each trace_stat file opening.
  95. * All of these copies and sorting are required on all opening
  96. * since the stats could have changed between two file sessions.
  97. */
  98. static int stat_seq_init(struct tracer_stat_session *session)
  99. {
  100. struct trace_stat_list *iter_entry, *new_entry;
  101. struct tracer_stat *ts = session->ts;
  102. void *prev_stat;
  103. int ret = 0;
  104. int i;
  105. mutex_lock(&session->stat_mutex);
  106. reset_stat_session(session);
  107. if (!ts->stat_cmp)
  108. ts->stat_cmp = dummy_cmp;
  109. /*
  110. * The first entry. Actually this is the second, but the first
  111. * one (the stat_list head) is pointless.
  112. */
  113. new_entry = kmalloc(sizeof(struct trace_stat_list), GFP_KERNEL);
  114. if (!new_entry) {
  115. ret = -ENOMEM;
  116. goto exit;
  117. }
  118. INIT_LIST_HEAD(&new_entry->list);
  119. list_add(&new_entry->list, &session->stat_list);
  120. new_entry->stat = ts->stat_start();
  121. prev_stat = new_entry->stat;
  122. /*
  123. * Iterate over the tracer stat entries and store them in a sorted
  124. * list.
  125. */
  126. for (i = 1; ; i++) {
  127. new_entry = kmalloc(sizeof(struct trace_stat_list), GFP_KERNEL);
  128. if (!new_entry) {
  129. ret = -ENOMEM;
  130. goto exit_free_list;
  131. }
  132. INIT_LIST_HEAD(&new_entry->list);
  133. new_entry->stat = ts->stat_next(prev_stat, i);
  134. /* End of insertion */
  135. if (!new_entry->stat)
  136. break;
  137. list_for_each_entry(iter_entry, &session->stat_list, list) {
  138. /* Insertion with a descendent sorting */
  139. if (ts->stat_cmp(new_entry->stat,
  140. iter_entry->stat) > 0) {
  141. list_add_tail(&new_entry->list,
  142. &iter_entry->list);
  143. break;
  144. /* The current smaller value */
  145. } else if (list_is_last(&iter_entry->list,
  146. &session->stat_list)) {
  147. list_add(&new_entry->list, &iter_entry->list);
  148. break;
  149. }
  150. }
  151. prev_stat = new_entry->stat;
  152. }
  153. exit:
  154. mutex_unlock(&session->stat_mutex);
  155. return ret;
  156. exit_free_list:
  157. reset_stat_session(session);
  158. mutex_unlock(&session->stat_mutex);
  159. return ret;
  160. }
  161. static void *stat_seq_start(struct seq_file *s, loff_t *pos)
  162. {
  163. struct tracer_stat_session *session = s->private;
  164. /* Prevent from tracer switch or stat_list modification */
  165. mutex_lock(&session->stat_mutex);
  166. /* If we are in the beginning of the file, print the headers */
  167. if (!*pos && session->ts->stat_headers)
  168. session->ts->stat_headers(s);
  169. return seq_list_start(&session->stat_list, *pos);
  170. }
  171. static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
  172. {
  173. struct tracer_stat_session *session = s->private;
  174. return seq_list_next(p, &session->stat_list, pos);
  175. }
  176. static void stat_seq_stop(struct seq_file *s, void *p)
  177. {
  178. struct tracer_stat_session *session = s->private;
  179. mutex_unlock(&session->stat_mutex);
  180. }
  181. static int stat_seq_show(struct seq_file *s, void *v)
  182. {
  183. struct tracer_stat_session *session = s->private;
  184. struct trace_stat_list *l = list_entry(v, struct trace_stat_list, list);
  185. return session->ts->stat_show(s, l->stat);
  186. }
  187. static const struct seq_operations trace_stat_seq_ops = {
  188. .start = stat_seq_start,
  189. .next = stat_seq_next,
  190. .stop = stat_seq_stop,
  191. .show = stat_seq_show
  192. };
  193. /* The session stat is refilled and resorted at each stat file opening */
  194. static int tracing_stat_open(struct inode *inode, struct file *file)
  195. {
  196. int ret;
  197. struct tracer_stat_session *session = inode->i_private;
  198. ret = seq_open(file, &trace_stat_seq_ops);
  199. if (!ret) {
  200. struct seq_file *m = file->private_data;
  201. m->private = session;
  202. ret = stat_seq_init(session);
  203. }
  204. return ret;
  205. }
  206. /*
  207. * Avoid consuming memory with our now useless list.
  208. */
  209. static int tracing_stat_release(struct inode *i, struct file *f)
  210. {
  211. struct tracer_stat_session *session = i->i_private;
  212. mutex_lock(&session->stat_mutex);
  213. reset_stat_session(session);
  214. mutex_unlock(&session->stat_mutex);
  215. return 0;
  216. }
  217. static const struct file_operations tracing_stat_fops = {
  218. .open = tracing_stat_open,
  219. .read = seq_read,
  220. .llseek = seq_lseek,
  221. .release = tracing_stat_release
  222. };
  223. static void destroy_trace_stat_files(void)
  224. {
  225. int i;
  226. if (stat_files) {
  227. for (i = 0; i < nb_sessions; i++)
  228. debugfs_remove(stat_files[i]);
  229. kfree(stat_files);
  230. stat_files = NULL;
  231. }
  232. }
  233. static void init_trace_stat_files(void)
  234. {
  235. int i;
  236. if (!stat_dir || !nb_sessions)
  237. return;
  238. stat_files = kmalloc(sizeof(struct dentry *) * nb_sessions, GFP_KERNEL);
  239. if (!stat_files) {
  240. pr_warning("trace stat: not enough memory\n");
  241. return;
  242. }
  243. for (i = 0; i < nb_sessions; i++) {
  244. struct tracer_stat_session *session = all_stat_sessions[i];
  245. stat_files[i] = debugfs_create_file(session->ts->name, 0644,
  246. stat_dir,
  247. session, &tracing_stat_fops);
  248. if (!stat_files[i])
  249. pr_warning("cannot create %s entry\n",
  250. session->ts->name);
  251. }
  252. }
  253. void init_tracer_stat(struct tracer *trace)
  254. {
  255. int nb = basic_tracer_stat_checks(trace->stats);
  256. destroy_trace_stat_files();
  257. if (nb < 0) {
  258. pr_warning("stat tracing: missing stat callback on %s\n",
  259. trace->name);
  260. return;
  261. }
  262. if (!nb)
  263. return;
  264. init_all_sessions(nb, trace->stats);
  265. init_trace_stat_files();
  266. }
  267. static int __init tracing_stat_init(void)
  268. {
  269. struct dentry *d_tracing;
  270. d_tracing = tracing_init_dentry();
  271. stat_dir = debugfs_create_dir("trace_stat", d_tracing);
  272. if (!stat_dir)
  273. pr_warning("Could not create debugfs "
  274. "'trace_stat' entry\n");
  275. return 0;
  276. }
  277. fs_initcall(tracing_stat_init);