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