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