4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Holds LTTng per-session event registry.
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/jiffies.h>
15 #include <linux/uuid.h>
16 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
17 #include "ltt-events.h"
18 #include "ltt-tracer.h"
20 static LIST_HEAD(sessions
);
21 static LIST_HEAD(ltt_transport_list
);
22 static DEFINE_MUTEX(sessions_mutex
);
23 static struct kmem_cache
*event_cache
;
25 static void _ltt_event_destroy(struct ltt_event
*event
);
26 static int _ltt_event_unregister(struct ltt_event
*event
);
28 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
29 struct ltt_channel
*chan
,
30 struct ltt_event
*event
);
32 int _ltt_session_metadata_statedump(struct ltt_session
*session
);
36 void synchronize_trace(void)
39 #ifdef CONFIG_PREEMPT_RT
44 struct ltt_session
*ltt_session_create(void)
46 struct ltt_session
*session
;
48 mutex_lock(&sessions_mutex
);
49 session
= kzalloc(sizeof(struct ltt_session
), GFP_KERNEL
);
52 INIT_LIST_HEAD(&session
->chan
);
53 INIT_LIST_HEAD(&session
->events
);
54 uuid_le_gen(&session
->uuid
);
55 list_add(&session
->list
, &sessions
);
56 mutex_unlock(&sessions_mutex
);
60 void ltt_session_destroy(struct ltt_session
*session
)
62 struct ltt_channel
*chan
, *tmpchan
;
63 struct ltt_event
*event
, *tmpevent
;
66 mutex_lock(&sessions_mutex
);
67 ACCESS_ONCE(session
->active
) = 0;
68 list_for_each_entry(event
, &session
->events
, list
) {
69 ret
= _ltt_event_unregister(event
);
72 synchronize_trace(); /* Wait for in-flight events to complete */
73 list_for_each_entry_safe(event
, tmpevent
, &session
->events
, list
)
74 _ltt_event_destroy(event
);
75 list_for_each_entry_safe(chan
, tmpchan
, &session
->chan
, list
)
76 _ltt_channel_destroy(chan
);
77 list_del(&session
->list
);
78 mutex_unlock(&sessions_mutex
);
82 int ltt_session_start(struct ltt_session
*session
)
85 struct ltt_channel
*chan
;
87 mutex_lock(&sessions_mutex
);
88 if (session
->active
) {
94 * Snapshot the number of events per channel to know the type of header
97 list_for_each_entry(chan
, &session
->chan
, list
) {
98 if (chan
->header_type
)
99 continue; /* don't change it if session stop/restart */
100 if (chan
->free_event_id
< 31)
101 chan
->header_type
= 1; /* compact */
103 chan
->header_type
= 2; /* large */
106 ACCESS_ONCE(session
->active
) = 1;
107 synchronize_trace(); /* Wait for in-flight events to complete */
108 ret
= _ltt_session_metadata_statedump(session
);
110 ACCESS_ONCE(session
->active
) = 0;
111 synchronize_trace(); /* Wait for in-flight events to complete */
114 mutex_unlock(&sessions_mutex
);
118 int ltt_session_stop(struct ltt_session
*session
)
122 mutex_lock(&sessions_mutex
);
123 if (!session
->active
) {
127 ACCESS_ONCE(session
->active
) = 0;
128 synchronize_trace(); /* Wait for in-flight events to complete */
130 mutex_unlock(&sessions_mutex
);
134 static struct ltt_transport
*ltt_transport_find(const char *name
)
136 struct ltt_transport
*transport
;
138 list_for_each_entry(transport
, <t_transport_list
, node
) {
139 if (!strcmp(transport
->name
, name
))
145 struct ltt_channel
*ltt_channel_create(struct ltt_session
*session
,
146 const char *transport_name
,
148 size_t subbuf_size
, size_t num_subbuf
,
149 unsigned int switch_timer_interval
,
150 unsigned int read_timer_interval
)
152 struct ltt_channel
*chan
;
153 struct ltt_transport
*transport
;
155 mutex_lock(&sessions_mutex
);
156 if (session
->active
) {
157 printk(KERN_WARNING
"LTTng refusing to add channel to active session\n");
158 goto active
; /* Refuse to add channel to active session */
160 transport
= ltt_transport_find(transport_name
);
162 printk(KERN_WARNING
"LTTng transport %s not found\n",
166 chan
= kzalloc(sizeof(struct ltt_channel
), GFP_KERNEL
);
169 chan
->session
= session
;
170 init_waitqueue_head(&chan
->notify_wait
);
171 chan
->chan
= transport
->ops
.channel_create("[lttng]", session
, buf_addr
,
172 subbuf_size
, num_subbuf
, switch_timer_interval
,
173 read_timer_interval
);
176 chan
->id
= session
->free_chan_id
++;
177 chan
->ops
= &transport
->ops
;
178 list_add(&chan
->list
, &session
->chan
);
179 mutex_unlock(&sessions_mutex
);
187 mutex_unlock(&sessions_mutex
);
192 * Only used internally at session destruction.
194 void _ltt_channel_destroy(struct ltt_channel
*chan
)
196 chan
->ops
->channel_destroy(chan
->chan
);
197 list_del(&chan
->list
);
202 * Supports event creation while tracing session is active.
204 struct ltt_event
*ltt_event_create(struct ltt_channel
*chan
, char *name
,
205 enum instrum_type itype
,
206 const struct lttng_event_desc
*event_desc
,
209 struct ltt_event
*event
;
212 mutex_lock(&sessions_mutex
);
213 if (chan
->free_event_id
== -1UL)
216 * This is O(n^2) (for each event, the loop is called at event
217 * creation). Might require a hash if we have lots of events.
219 list_for_each_entry(event
, &chan
->session
->events
, list
)
220 if (!strcmp(event
->desc
->name
, name
))
222 event
= kmem_cache_zalloc(event_cache
, GFP_KERNEL
);
226 event
->desc
= event_desc
;
227 event
->filter
= filter
;
228 event
->id
= chan
->free_event_id
++;
229 event
->itype
= itype
;
230 /* Populate ltt_event structure before tracepoint registration. */
233 case INSTRUM_TRACEPOINTS
:
234 ret
= tracepoint_probe_register(name
, event_desc
->probe_callback
,
242 ret
= _ltt_event_metadata_statedump(chan
->session
, chan
, event
);
244 goto statedump_error
;
245 list_add(&event
->list
, &chan
->session
->events
);
246 mutex_unlock(&sessions_mutex
);
250 WARN_ON_ONCE(tracepoint_probe_unregister(name
, event_desc
->probe_callback
,
253 kmem_cache_free(event_cache
, event
);
257 mutex_unlock(&sessions_mutex
);
262 * Only used internally at session destruction.
264 int _ltt_event_unregister(struct ltt_event
*event
)
268 switch (event
->itype
) {
269 case INSTRUM_TRACEPOINTS
:
270 ret
= tracepoint_probe_unregister(event
->desc
->name
,
271 event
->desc
->probe_callback
,
283 * Used when an event FD is released.
285 int ltt_event_unregister(struct ltt_event
*event
)
289 mutex_lock(&sessions_mutex
);
290 ret
= ltt_event_unregister(event
);
291 mutex_unlock(&sessions_mutex
);
296 * Only used internally at session destruction.
299 void _ltt_event_destroy(struct ltt_event
*event
)
301 ltt_event_put(event
->desc
);
302 list_del(&event
->list
);
303 kmem_cache_free(event_cache
, event
);
307 * We have exclusive access to our metadata buffer (protected by the
308 * sessions_mutex), so we can do racy operations such as looking for
309 * remaining space left in packet and write, since mutual exclusion
310 * protects us from concurrent writes.
312 int lttng_metadata_printf(struct ltt_session
*session
,
313 const char *fmt
, ...)
315 struct lib_ring_buffer_ctx ctx
;
316 struct ltt_channel
*chan
= session
->metadata
;
318 int ret
= 0, waitret
;
319 size_t len
, reserve_len
, pos
;
322 WARN_ON_ONCE(!ACCESS_ONCE(session
->active
));
325 str
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
333 for (pos
= 0; pos
< len
; pos
+= reserve_len
) {
334 reserve_len
= min_t(size_t,
335 chan
->ops
->packet_avail_size(chan
->chan
),
337 lib_ring_buffer_ctx_init(&ctx
, chan
->chan
, NULL
, reserve_len
,
340 * We don't care about metadata buffer's records lost
341 * count, because we always retry here. Report error if
342 * we need to bail out after timeout or being
345 waitret
= wait_event_interruptible_timeout(*chan
->ops
->get_reader_wait_queue(chan
),
347 ret
= chan
->ops
->event_reserve(&ctx
);
348 ret
!= -ENOBUFS
|| !ret
;
350 msecs_to_jiffies(LTTNG_METADATA_TIMEOUT_MSEC
));
351 if (!waitret
|| waitret
== -ERESTARTSYS
|| ret
) {
352 printk(KERN_WARNING
"LTTng: Failure to write metadata to buffers (%s)\n",
353 waitret
== -ERESTARTSYS
? "interrupted" :
354 (ret
== -ENOBUFS
? "timeout" : "I/O error"));
355 if (waitret
== -ERESTARTSYS
)
359 chan
->ops
->event_write(&ctx
, &str
[pos
], len
);
360 chan
->ops
->event_commit(&ctx
);
368 int _ltt_fields_metadata_statedump(struct ltt_session
*session
,
369 struct ltt_event
*event
)
371 const struct lttng_event_desc
*desc
= event
->desc
;
375 for (i
= 0; i
< desc
->nr_fields
; i
++) {
376 const struct lttng_event_field
*field
= &desc
->fields
[i
];
378 switch (field
->type
.atype
) {
380 ret
= lttng_metadata_printf(session
,
381 " integer { size = %u; align = %u; signed = %u;%s } %s;\n",
382 field
->type
.u
.basic
.integer
.size
,
383 field
->type
.u
.basic
.integer
.alignment
,
384 field
->type
.u
.basic
.integer
.signedness
,
386 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
388 field
->type
.u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
393 ret
= lttng_metadata_printf(session
,
395 field
->type
.u
.basic
.enumeration
.name
,
400 const struct lttng_basic_type
*elem_type
;
402 elem_type
= &field
->type
.u
.array
.elem_type
;
403 ret
= lttng_metadata_printf(session
,
404 " integer { size = %u; align = %u; signed = %u;%s } %s[%u];\n",
405 elem_type
->u
.basic
.integer
.size
,
406 elem_type
->u
.basic
.integer
.alignment
,
407 elem_type
->u
.basic
.integer
.signedness
,
409 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
411 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
413 field
->name
, field
->type
.u
.array
.length
);
418 const struct lttng_basic_type
*elem_type
;
419 const struct lttng_basic_type
*length_type
;
421 elem_type
= &field
->type
.u
.sequence
.elem_type
;
422 length_type
= &field
->type
.u
.sequence
.length_type
;
423 ret
= lttng_metadata_printf(session
,
424 " integer { size = %u; align = %u; signed = %u;%s } %s[ integer { size = %u; align = %u; signed = %u;%s } ];\n",
425 elem_type
->u
.basic
.integer
.size
,
426 elem_type
->u
.basic
.integer
.alignment
,
427 elem_type
->u
.basic
.integer
.signedness
,
429 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
431 elem_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : "",
434 length_type
->u
.basic
.integer
.size
,
435 length_type
->u
.basic
.integer
.alignment
,
436 length_type
->u
.basic
.integer
.signedness
,
438 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = le;" : "",
440 length_type
->u
.basic
.integer
.reverse_byte_order
? " byte_order = be;" : ""
447 ret
= lttng_metadata_printf(session
,
449 field
->type
.u
.basic
.string
.encoding
== lttng_encode_ASCII
?
450 " { encoding = ASCII; }" : "",
462 int _ltt_event_metadata_statedump(struct ltt_session
*session
,
463 struct ltt_channel
*chan
,
464 struct ltt_event
*event
)
468 if (event
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
470 if (chan
== session
->metadata
)
473 ret
= lttng_metadata_printf(session
,
478 " event.fields := struct {\n",
485 ret
= _ltt_fields_metadata_statedump(session
, event
);
490 * LTTng space reservation can only reserve multiples of the
493 ret
= lttng_metadata_printf(session
,
495 "};\n\n", ltt_get_header_alignment());
502 event
->metadata_dumped
= 1;
509 int _ltt_channel_metadata_statedump(struct ltt_session
*session
,
510 struct ltt_channel
*chan
)
514 if (chan
->metadata_dumped
|| !ACCESS_ONCE(session
->active
))
516 if (chan
== session
->metadata
)
519 WARN_ON_ONCE(!chan
->header_type
);
520 ret
= lttng_metadata_printf(session
,
523 " event.header := %s;\n",
526 chan
->header_type
== 1 ? "struct event_header_compact" :
527 "struct event_header_large");
531 chan
->metadata_dumped
= 1;
537 * Output metadata into this session's metadata buffers.
540 int _ltt_session_metadata_statedump(struct ltt_session
*session
)
543 struct ltt_channel
*chan
;
544 struct ltt_event
*event
;
547 if (!ACCESS_ONCE(session
->active
))
549 if (session
->metadata_dumped
)
551 if (!session
->metadata
) {
552 printk(KERN_WARNING
"LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
556 snprintf(uuid_s
, sizeof(uuid_s
),
557 "%x%x%x%x-%x%x-%x%x-%x%x-%x%x%x%x%x%x",
558 uuid_s
[0], uuid_s
[1], uuid_s
[2], uuid_s
[3],
559 uuid_s
[4], uuid_s
[5], uuid_s
[6], uuid_s
[7],
560 uuid_s
[8], uuid_s
[9], uuid_s
[10], uuid_s
[11],
561 uuid_s
[12], uuid_s
[13], uuid_s
[14], uuid_s
[15]);
563 ret
= lttng_metadata_printf(session
,
564 "typealias integer {size = 8; align = %u; signed = false; } := uint8_t;\n"
565 "typealias integer {size = 32; align = %u; signed = false; } := uint32_t;\n"
566 "typealias integer {size = 64; align = %u; signed = false; } := uint64_t;\n"
572 " byte_order = %s;\n"
573 " packet.header := struct {\n"
575 " uint8_t uuid[16];\n"
576 " uint32_t stream_id;\n"
577 " uint64_t timestamp_begin;\n"
578 " uint64_t timestamp_end;\n"
579 " uint32_t content_size;\n"
580 " uint32_t packet_size;\n"
581 " uint32_t events_lost;\n"
584 ltt_alignof(uint8_t) * CHAR_BIT
,
585 ltt_alignof(uint32_t) * CHAR_BIT
,
586 ltt_alignof(uint64_t) * CHAR_BIT
,
600 list_for_each_entry(chan
, &session
->chan
, list
) {
601 ret
= _ltt_channel_metadata_statedump(session
, chan
);
606 list_for_each_entry(event
, &session
->events
, list
) {
607 ret
= _ltt_event_metadata_statedump(session
, chan
, event
);
611 session
->metadata_dumped
= 1;
617 * ltt_transport_register - LTT transport registration
618 * @transport: transport structure
620 * Registers a transport which can be used as output to extract the data out of
621 * LTTng. The module calling this registration function must ensure that no
622 * trap-inducing code will be executed by the transport functions. E.g.
623 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
624 * is made visible to the transport function. This registration acts as a
625 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
626 * after its registration must it synchronize the TLBs.
628 void ltt_transport_register(struct ltt_transport
*transport
)
631 * Make sure no page fault can be triggered by the module about to be
632 * registered. We deal with this here so we don't have to call
633 * vmalloc_sync_all() in each module's init.
635 wrapper_vmalloc_sync_all();
637 mutex_lock(&sessions_mutex
);
638 list_add_tail(&transport
->node
, <t_transport_list
);
639 mutex_unlock(&sessions_mutex
);
641 EXPORT_SYMBOL_GPL(ltt_transport_register
);
644 * ltt_transport_unregister - LTT transport unregistration
645 * @transport: transport structure
647 void ltt_transport_unregister(struct ltt_transport
*transport
)
649 mutex_lock(&sessions_mutex
);
650 list_del(&transport
->node
);
651 mutex_unlock(&sessions_mutex
);
653 EXPORT_SYMBOL_GPL(ltt_transport_unregister
);
655 static int __init
ltt_events_init(void)
659 event_cache
= KMEM_CACHE(ltt_event
, 0);
662 ret
= ltt_debugfs_abi_init();
667 kmem_cache_destroy(event_cache
);
671 module_init(ltt_events_init
);
673 static void __exit
ltt_events_exit(void)
675 struct ltt_session
*session
, *tmpsession
;
677 ltt_debugfs_abi_exit();
678 list_for_each_entry_safe(session
, tmpsession
, &sessions
, list
)
679 ltt_session_destroy(session
);
680 kmem_cache_destroy(event_cache
);
683 module_exit(ltt_events_exit
);
685 MODULE_LICENSE("GPL and additional rights");
686 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
687 MODULE_DESCRIPTION("LTTng Events");