Add probe registry
[deliverable/lttng-modules.git] / ltt-events.c
1 /*
2 * ltt-events.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Holds LTTng per-session event registry.
7 */
8
9 #include <linux/module.h>
10 #include <linux/list.h>
11 #include <linux/mutex.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h> /* For vmalloc_sync_all */
15 #include "ltt-events.h"
16
17 static LIST_HEAD(sessions);
18 static LIST_HEAD(ltt_transport_list);
19 static DEFINE_MUTEX(sessions_mutex);
20 static struct kmem_cache *event_cache;
21
22 static void synchronize_trace(void)
23 {
24 synchronize_sched();
25 #ifdef CONFIG_PREEMPT_RT
26 synchronize_rcu();
27 #endif
28 }
29
30 struct ltt_session *ltt_session_create(void)
31 {
32 struct ltt_session *session;
33
34 mutex_lock(&sessions_mutex);
35 session = kzalloc(sizeof(struct ltt_session), GFP_KERNEL);
36 if (!session)
37 return NULL;
38 INIT_LIST_HEAD(&session->chan);
39 INIT_LIST_HEAD(&session->events);
40 list_add(&session->list, &sessions);
41 mutex_unlock(&sessions_mutex);
42 return session;
43 }
44
45 void ltt_session_destroy(struct ltt_session *session)
46 {
47 struct ltt_channel *chan, *tmpchan;
48 struct ltt_event *event, *tmpevent;
49
50 mutex_lock(&sessions_mutex);
51 session->active = 0;
52 synchronize_trace(); /* Wait for in-flight events to complete */
53 list_for_each_entry_safe(event, tmpevent, &session->events, list)
54 _ltt_event_destroy(event);
55 list_for_each_entry_safe(chan, tmpchan, &session->chan, list)
56 _ltt_channel_destroy(chan);
57 list_del(&session->list);
58 mutex_unlock(&sessions_mutex);
59 kfree(session);
60 }
61
62 int ltt_session_start(struct ltt_session *session)
63 {
64 int ret = 0;
65
66 mutex_lock(&sessions_mutex);
67 if (session->active) {
68 ret = -EBUSY;
69 goto end;
70 }
71 session->active = 1;
72 synchronize_trace(); /* Wait for in-flight events to complete */
73 end:
74 mutex_unlock(&sessions_mutex);
75 return ret;
76 }
77
78 int ltt_session_stop(struct ltt_session *session)
79 {
80 int ret = 0;
81
82 mutex_lock(&sessions_mutex);
83 if (!session->active) {
84 ret = -EBUSY;
85 goto end;
86 }
87 session->active = 0;
88 synchronize_trace(); /* Wait for in-flight events to complete */
89 end:
90 mutex_unlock(&sessions_mutex);
91 return ret;
92 }
93
94 static struct ltt_transport *ltt_transport_find(char *name)
95 {
96 struct ltt_transport *transport;
97
98 list_for_each_entry(transport, &ltt_transport_list, node) {
99 if (!strcmp(transport->name, name))
100 return transport;
101 }
102 return NULL;
103 }
104
105 struct ltt_channel *ltt_channel_create(struct ltt_session *session,
106 int overwrite, void *buf_addr,
107 size_t subbuf_size, size_t num_subbuf,
108 unsigned int switch_timer_interval,
109 unsigned int read_timer_interval)
110 {
111 struct ltt_channel *chan;
112 struct ltt_transport *transport;
113 char *transport_name;
114
115 mutex_lock(&sessions_mutex);
116 if (session->active) {
117 printk(KERN_WARNING "LTTng refusing to add channel to active session\n");
118 goto active; /* Refuse to add channel to active session */
119 }
120 transport_name = overwrite ? "relay-overwrite" : "relay-discard";
121 transport = ltt_transport_find(transport_name);
122 if (!transport) {
123 printk(KERN_WARNING "LTTng transport %s not found\n",
124 transport_name);
125 goto notransport;
126 }
127 chan = kzalloc(sizeof(struct ltt_channel), GFP_KERNEL);
128 if (!chan)
129 goto nomem;
130 chan->session = session;
131 init_waitqueue_head(&chan->notify_wait);
132 chan->chan = transport->ops.channel_create("[lttng]", session, buf_addr,
133 subbuf_size, num_subbuf, switch_timer_interval,
134 read_timer_interval);
135 if (!chan->chan)
136 goto create_error;
137 chan->ops = &transport->ops;
138 list_add(&chan->list, &session->chan);
139 mutex_unlock(&sessions_mutex);
140 return chan;
141
142 create_error:
143 kfree(chan);
144 nomem:
145 notransport:
146 active:
147 mutex_unlock(&sessions_mutex);
148 return NULL;
149 }
150
151 /*
152 * Only used internally at session destruction.
153 */
154 void _ltt_channel_destroy(struct ltt_channel *chan)
155 {
156 chan->ops->channel_destroy(chan->chan);
157 list_del(&chan->list);
158 kfree(chan);
159 }
160
161 /*
162 * Supports event creation while tracing session is active.
163 */
164 struct ltt_event *ltt_event_create(struct ltt_channel *chan, char *name,
165 enum instrum_type itype,
166 void *probe, void *filter)
167 {
168 struct ltt_event *event;
169 int ret;
170
171 mutex_lock(&sessions_mutex);
172 if (chan->free_event_id == -1UL)
173 goto full;
174 /*
175 * This is O(n^2) (for each event loop called at event creation).
176 * Might require a hash if we have lots of events.
177 */
178 list_for_each_entry(event, &chan->session->events, list)
179 if (!strcmp(event->name, name))
180 goto exist;
181 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
182 if (!event)
183 goto cache_error;
184 event->name = kmalloc(strlen(name) + 1, GFP_KERNEL);
185 if (!event->name)
186 goto name_error;
187 strcpy(event->name, name);
188 event->chan = chan;
189 event->probe = probe;
190 event->filter = filter;
191 event->id = chan->free_event_id++;
192 event->itype = itype;
193 /* Populate ltt_event structure before tracepoint registration. */
194 smp_wmb();
195 switch (itype) {
196 case INSTRUM_TRACEPOINTS:
197 ret = tracepoint_probe_register(name, probe, event);
198 if (ret)
199 goto register_error;
200 break;
201 default:
202 WARN_ON_ONCE(1);
203 }
204 mutex_unlock(&sessions_mutex);
205 return event;
206
207 register_error:
208 kfree(event->name);
209 name_error:
210 kmem_cache_free(event_cache, event);
211 cache_error:
212 exist:
213 full:
214 mutex_unlock(&sessions_mutex);
215 return NULL;
216 }
217
218 /*
219 * Only used internally at session destruction.
220 */
221 int _ltt_event_destroy(struct ltt_event *event)
222 {
223 int ret = -EINVAL;
224
225 switch (event->itype) {
226 case INSTRUM_TRACEPOINTS:
227 ret = tracepoint_probe_unregister(event->name, event->probe,
228 event);
229 if (ret)
230 return ret;
231 break;
232 default:
233 WARN_ON_ONCE(1);
234 }
235 kfree(event->name);
236 kmem_cache_free(event_cache, event);
237 return ret;
238 }
239
240 /**
241 * ltt_transport_register - LTT transport registration
242 * @transport: transport structure
243 *
244 * Registers a transport which can be used as output to extract the data out of
245 * LTTng. The module calling this registration function must ensure that no
246 * trap-inducing code will be executed by the transport functions. E.g.
247 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
248 * is made visible to the transport function. This registration acts as a
249 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
250 * after its registration must it synchronize the TLBs.
251 */
252 void ltt_transport_register(struct ltt_transport *transport)
253 {
254 /*
255 * Make sure no page fault can be triggered by the module about to be
256 * registered. We deal with this here so we don't have to call
257 * vmalloc_sync_all() in each module's init.
258 */
259 vmalloc_sync_all();
260
261 mutex_lock(&sessions_mutex);
262 list_add_tail(&transport->node, &ltt_transport_list);
263 mutex_unlock(&sessions_mutex);
264 }
265 EXPORT_SYMBOL_GPL(ltt_transport_register);
266
267 /**
268 * ltt_transport_unregister - LTT transport unregistration
269 * @transport: transport structure
270 */
271 void ltt_transport_unregister(struct ltt_transport *transport)
272 {
273 mutex_lock(&sessions_mutex);
274 list_del(&transport->node);
275 mutex_unlock(&sessions_mutex);
276 }
277 EXPORT_SYMBOL_GPL(ltt_transport_unregister);
278
279
280 static int __init ltt_events_init(void)
281 {
282 int ret;
283
284 event_cache = KMEM_CACHE(ltt_event, 0);
285 if (!event_cache)
286 return -ENOMEM;
287 ret = ltt_probes_init();
288 if (ret)
289 goto error;
290 ret = ltt_debugfs_abi_init();
291 if (ret)
292 goto error_abi;
293 return 0;
294 error_abi:
295 ltt_probes_exit();
296 error:
297 kmem_cache_destroy(event_cache);
298 return ret;
299 }
300
301 module_init(ltt_events_init);
302
303 static void __exit ltt_events_exit(void)
304 {
305 struct ltt_session *session, *tmpsession;
306
307 ltt_debugfs_abi_exit();
308 ltt_probes_exit();
309 list_for_each_entry_safe(session, tmpsession, &sessions, list)
310 ltt_session_destroy(session);
311 kmem_cache_destroy(event_cache);
312 }
313
314 module_exit(ltt_events_exit);
315
316 MODULE_LICENSE("GPL and additional rights");
317 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
318 MODULE_DESCRIPTION("LTTng Events");
This page took 0.037164 seconds and 5 git commands to generate.