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