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