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