Metadata cache and multiple metadata channels
[deliverable/lttng-modules.git] / lttng-events.c
1 /*
2 * lttng-events.c
3 *
4 * Holds LTTng per-session event registry.
5 *
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <linux/module.h>
24 #include <linux/list.h>
25 #include <linux/mutex.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/utsname.h>
30 #include "wrapper/uuid.h"
31 #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
32 #include "wrapper/random.h"
33 #include "wrapper/tracepoint.h"
34 #include "lttng-events.h"
35 #include "lttng-tracer.h"
36 #include "lttng-abi-old.h"
37
38 #define METADATA_CACHE_DEFAULT_SIZE 4096
39
40 static LIST_HEAD(sessions);
41 static LIST_HEAD(lttng_transport_list);
42 /*
43 * Protect the sessions and metadata caches.
44 */
45 static DEFINE_MUTEX(sessions_mutex);
46 static struct kmem_cache *event_cache;
47
48 static void _lttng_event_destroy(struct lttng_event *event);
49 static void _lttng_channel_destroy(struct lttng_channel *chan);
50 static int _lttng_event_unregister(struct lttng_event *event);
51 static
52 int _lttng_event_metadata_statedump(struct lttng_session *session,
53 struct lttng_channel *chan,
54 struct lttng_event *event);
55 static
56 int _lttng_session_metadata_statedump(struct lttng_session *session);
57 static
58 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
59
60 void synchronize_trace(void)
61 {
62 synchronize_sched();
63 #ifdef CONFIG_PREEMPT_RT
64 synchronize_rcu();
65 #endif
66 }
67
68 struct lttng_session *lttng_session_create(void)
69 {
70 struct lttng_session *session;
71 struct lttng_metadata_cache *metadata_cache;
72
73 mutex_lock(&sessions_mutex);
74 session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL);
75 if (!session)
76 goto err;
77 INIT_LIST_HEAD(&session->chan);
78 INIT_LIST_HEAD(&session->events);
79 uuid_le_gen(&session->uuid);
80
81 metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache),
82 GFP_KERNEL);
83 if (!metadata_cache)
84 goto err_free_session;
85 metadata_cache->data = kzalloc(METADATA_CACHE_DEFAULT_SIZE,
86 GFP_KERNEL);
87 if (!metadata_cache->data)
88 goto err_free_cache;
89 metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE;
90 kref_init(&metadata_cache->refcount);
91 session->metadata_cache = metadata_cache;
92 INIT_LIST_HEAD(&metadata_cache->metadata_stream);
93 list_add(&session->list, &sessions);
94 mutex_unlock(&sessions_mutex);
95 return session;
96
97 err_free_cache:
98 kfree(metadata_cache);
99 err_free_session:
100 kfree(session);
101 err:
102 mutex_unlock(&sessions_mutex);
103 return NULL;
104 }
105
106 void metadata_cache_destroy(struct kref *kref)
107 {
108 struct lttng_metadata_cache *cache =
109 container_of(kref, struct lttng_metadata_cache, refcount);
110 kfree(cache->data);
111 kfree(cache);
112 }
113
114 void lttng_session_destroy(struct lttng_session *session)
115 {
116 struct lttng_channel *chan, *tmpchan;
117 struct lttng_event *event, *tmpevent;
118 struct lttng_metadata_stream *metadata_stream;
119 int ret;
120
121 mutex_lock(&sessions_mutex);
122 ACCESS_ONCE(session->active) = 0;
123 list_for_each_entry(chan, &session->chan, list) {
124 ret = lttng_syscalls_unregister(chan);
125 WARN_ON(ret);
126 }
127 list_for_each_entry(event, &session->events, list) {
128 ret = _lttng_event_unregister(event);
129 WARN_ON(ret);
130 }
131 synchronize_trace(); /* Wait for in-flight events to complete */
132 list_for_each_entry_safe(event, tmpevent, &session->events, list)
133 _lttng_event_destroy(event);
134 list_for_each_entry_safe(chan, tmpchan, &session->chan, list) {
135 BUG_ON(chan->channel_type == METADATA_CHANNEL);
136 _lttng_channel_destroy(chan);
137 }
138 list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list)
139 _lttng_metadata_channel_hangup(metadata_stream);
140 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
141 list_del(&session->list);
142 mutex_unlock(&sessions_mutex);
143 kfree(session);
144 }
145
146 int lttng_session_enable(struct lttng_session *session)
147 {
148 int ret = 0;
149 struct lttng_channel *chan;
150
151 mutex_lock(&sessions_mutex);
152 if (session->active) {
153 ret = -EBUSY;
154 goto end;
155 }
156
157 /*
158 * Snapshot the number of events per channel to know the type of header
159 * we need to use.
160 */
161 list_for_each_entry(chan, &session->chan, list) {
162 if (chan->header_type)
163 continue; /* don't change it if session stop/restart */
164 if (chan->free_event_id < 31)
165 chan->header_type = 1; /* compact */
166 else
167 chan->header_type = 2; /* large */
168 }
169
170 ACCESS_ONCE(session->active) = 1;
171 ACCESS_ONCE(session->been_active) = 1;
172 ret = _lttng_session_metadata_statedump(session);
173 if (ret) {
174 ACCESS_ONCE(session->active) = 0;
175 goto end;
176 }
177 ret = lttng_statedump_start(session);
178 if (ret)
179 ACCESS_ONCE(session->active) = 0;
180 end:
181 mutex_unlock(&sessions_mutex);
182 return ret;
183 }
184
185 int lttng_session_disable(struct lttng_session *session)
186 {
187 int ret = 0;
188
189 mutex_lock(&sessions_mutex);
190 if (!session->active) {
191 ret = -EBUSY;
192 goto end;
193 }
194 ACCESS_ONCE(session->active) = 0;
195 end:
196 mutex_unlock(&sessions_mutex);
197 return ret;
198 }
199
200 int lttng_channel_enable(struct lttng_channel *channel)
201 {
202 int old;
203
204 if (channel->channel_type == METADATA_CHANNEL)
205 return -EPERM;
206 old = xchg(&channel->enabled, 1);
207 if (old)
208 return -EEXIST;
209 return 0;
210 }
211
212 int lttng_channel_disable(struct lttng_channel *channel)
213 {
214 int old;
215
216 if (channel->channel_type == METADATA_CHANNEL)
217 return -EPERM;
218 old = xchg(&channel->enabled, 0);
219 if (!old)
220 return -EEXIST;
221 return 0;
222 }
223
224 int lttng_event_enable(struct lttng_event *event)
225 {
226 int old;
227
228 if (event->chan->channel_type == METADATA_CHANNEL)
229 return -EPERM;
230 old = xchg(&event->enabled, 1);
231 if (old)
232 return -EEXIST;
233 return 0;
234 }
235
236 int lttng_event_disable(struct lttng_event *event)
237 {
238 int old;
239
240 if (event->chan->channel_type == METADATA_CHANNEL)
241 return -EPERM;
242 old = xchg(&event->enabled, 0);
243 if (!old)
244 return -EEXIST;
245 return 0;
246 }
247
248 static struct lttng_transport *lttng_transport_find(const char *name)
249 {
250 struct lttng_transport *transport;
251
252 list_for_each_entry(transport, &lttng_transport_list, node) {
253 if (!strcmp(transport->name, name))
254 return transport;
255 }
256 return NULL;
257 }
258
259 struct lttng_channel *lttng_channel_create(struct lttng_session *session,
260 const char *transport_name,
261 void *buf_addr,
262 size_t subbuf_size, size_t num_subbuf,
263 unsigned int switch_timer_interval,
264 unsigned int read_timer_interval,
265 enum channel_type channel_type)
266 {
267 struct lttng_channel *chan;
268 struct lttng_transport *transport = NULL;
269
270 mutex_lock(&sessions_mutex);
271 if (session->been_active && channel_type != METADATA_CHANNEL)
272 goto active; /* Refuse to add channel to active session */
273 transport = lttng_transport_find(transport_name);
274 if (!transport) {
275 printk(KERN_WARNING "LTTng transport %s not found\n",
276 transport_name);
277 goto notransport;
278 }
279 if (!try_module_get(transport->owner)) {
280 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
281 goto notransport;
282 }
283 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
284 if (!chan)
285 goto nomem;
286 chan->session = session;
287 chan->id = session->free_chan_id++;
288 /*
289 * Note: the channel creation op already writes into the packet
290 * headers. Therefore the "chan" information used as input
291 * should be already accessible.
292 */
293 chan->chan = transport->ops.channel_create(transport_name,
294 chan, buf_addr, subbuf_size, num_subbuf,
295 switch_timer_interval, read_timer_interval);
296 if (!chan->chan)
297 goto create_error;
298 chan->enabled = 1;
299 chan->ops = &transport->ops;
300 chan->transport = transport;
301 chan->channel_type = channel_type;
302 list_add(&chan->list, &session->chan);
303 mutex_unlock(&sessions_mutex);
304 return chan;
305
306 create_error:
307 kfree(chan);
308 nomem:
309 if (transport)
310 module_put(transport->owner);
311 notransport:
312 active:
313 mutex_unlock(&sessions_mutex);
314 return NULL;
315 }
316
317 /*
318 * Only used internally at session destruction for per-cpu channels, and
319 * when metadata channel is released.
320 * Needs to be called with sessions mutex held.
321 */
322 static
323 void _lttng_channel_destroy(struct lttng_channel *chan)
324 {
325 chan->ops->channel_destroy(chan->chan);
326 module_put(chan->transport->owner);
327 list_del(&chan->list);
328 lttng_destroy_context(chan->ctx);
329 kfree(chan);
330 }
331
332 void lttng_metadata_channel_destroy(struct lttng_channel *chan)
333 {
334 BUG_ON(chan->channel_type != METADATA_CHANNEL);
335
336 /* Protect the metadata cache with the sessions_mutex. */
337 mutex_lock(&sessions_mutex);
338 _lttng_channel_destroy(chan);
339 mutex_unlock(&sessions_mutex);
340 }
341 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
342
343 static
344 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
345 {
346 stream->finalized = 1;
347 wake_up_interruptible(&stream->read_wait);
348 }
349
350 /*
351 * Supports event creation while tracing session is active.
352 */
353 struct lttng_event *lttng_event_create(struct lttng_channel *chan,
354 struct lttng_kernel_event *event_param,
355 void *filter,
356 const struct lttng_event_desc *internal_desc)
357 {
358 struct lttng_event *event;
359 int ret;
360
361 mutex_lock(&sessions_mutex);
362 if (chan->free_event_id == -1U)
363 goto full;
364 /*
365 * This is O(n^2) (for each event, the loop is called at event
366 * creation). Might require a hash if we have lots of events.
367 */
368 list_for_each_entry(event, &chan->session->events, list)
369 if (!strcmp(event->desc->name, event_param->name))
370 goto exist;
371 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
372 if (!event)
373 goto cache_error;
374 event->chan = chan;
375 event->filter = filter;
376 event->id = chan->free_event_id++;
377 event->enabled = 1;
378 event->instrumentation = event_param->instrumentation;
379 /* Populate lttng_event structure before tracepoint registration. */
380 smp_wmb();
381 switch (event_param->instrumentation) {
382 case LTTNG_KERNEL_TRACEPOINT:
383 event->desc = lttng_event_get(event_param->name);
384 if (!event->desc)
385 goto register_error;
386 ret = kabi_2635_tracepoint_probe_register(event_param->name,
387 event->desc->probe_callback,
388 event);
389 if (ret)
390 goto register_error;
391 break;
392 case LTTNG_KERNEL_KPROBE:
393 ret = lttng_kprobes_register(event_param->name,
394 event_param->u.kprobe.symbol_name,
395 event_param->u.kprobe.offset,
396 event_param->u.kprobe.addr,
397 event);
398 if (ret)
399 goto register_error;
400 ret = try_module_get(event->desc->owner);
401 WARN_ON_ONCE(!ret);
402 break;
403 case LTTNG_KERNEL_KRETPROBE:
404 {
405 struct lttng_event *event_return;
406
407 /* kretprobe defines 2 events */
408 event_return =
409 kmem_cache_zalloc(event_cache, GFP_KERNEL);
410 if (!event_return)
411 goto register_error;
412 event_return->chan = chan;
413 event_return->filter = filter;
414 event_return->id = chan->free_event_id++;
415 event_return->enabled = 1;
416 event_return->instrumentation = event_param->instrumentation;
417 /*
418 * Populate lttng_event structure before kretprobe registration.
419 */
420 smp_wmb();
421 ret = lttng_kretprobes_register(event_param->name,
422 event_param->u.kretprobe.symbol_name,
423 event_param->u.kretprobe.offset,
424 event_param->u.kretprobe.addr,
425 event, event_return);
426 if (ret) {
427 kmem_cache_free(event_cache, event_return);
428 goto register_error;
429 }
430 /* Take 2 refs on the module: one per event. */
431 ret = try_module_get(event->desc->owner);
432 WARN_ON_ONCE(!ret);
433 ret = try_module_get(event->desc->owner);
434 WARN_ON_ONCE(!ret);
435 ret = _lttng_event_metadata_statedump(chan->session, chan,
436 event_return);
437 if (ret) {
438 kmem_cache_free(event_cache, event_return);
439 module_put(event->desc->owner);
440 module_put(event->desc->owner);
441 goto statedump_error;
442 }
443 list_add(&event_return->list, &chan->session->events);
444 break;
445 }
446 case LTTNG_KERNEL_FUNCTION:
447 ret = lttng_ftrace_register(event_param->name,
448 event_param->u.ftrace.symbol_name,
449 event);
450 if (ret)
451 goto register_error;
452 ret = try_module_get(event->desc->owner);
453 WARN_ON_ONCE(!ret);
454 break;
455 case LTTNG_KERNEL_NOOP:
456 event->desc = internal_desc;
457 if (!event->desc)
458 goto register_error;
459 break;
460 default:
461 WARN_ON_ONCE(1);
462 }
463 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
464 if (ret)
465 goto statedump_error;
466 list_add(&event->list, &chan->session->events);
467 mutex_unlock(&sessions_mutex);
468 return event;
469
470 statedump_error:
471 /* If a statedump error occurs, events will not be readable. */
472 register_error:
473 kmem_cache_free(event_cache, event);
474 cache_error:
475 exist:
476 full:
477 mutex_unlock(&sessions_mutex);
478 return NULL;
479 }
480
481 /*
482 * Only used internally at session destruction.
483 */
484 int _lttng_event_unregister(struct lttng_event *event)
485 {
486 int ret = -EINVAL;
487
488 switch (event->instrumentation) {
489 case LTTNG_KERNEL_TRACEPOINT:
490 ret = kabi_2635_tracepoint_probe_unregister(event->desc->name,
491 event->desc->probe_callback,
492 event);
493 if (ret)
494 return ret;
495 break;
496 case LTTNG_KERNEL_KPROBE:
497 lttng_kprobes_unregister(event);
498 ret = 0;
499 break;
500 case LTTNG_KERNEL_KRETPROBE:
501 lttng_kretprobes_unregister(event);
502 ret = 0;
503 break;
504 case LTTNG_KERNEL_FUNCTION:
505 lttng_ftrace_unregister(event);
506 ret = 0;
507 break;
508 case LTTNG_KERNEL_NOOP:
509 ret = 0;
510 break;
511 default:
512 WARN_ON_ONCE(1);
513 }
514 return ret;
515 }
516
517 /*
518 * Only used internally at session destruction.
519 */
520 static
521 void _lttng_event_destroy(struct lttng_event *event)
522 {
523 switch (event->instrumentation) {
524 case LTTNG_KERNEL_TRACEPOINT:
525 lttng_event_put(event->desc);
526 break;
527 case LTTNG_KERNEL_KPROBE:
528 module_put(event->desc->owner);
529 lttng_kprobes_destroy_private(event);
530 break;
531 case LTTNG_KERNEL_KRETPROBE:
532 module_put(event->desc->owner);
533 lttng_kretprobes_destroy_private(event);
534 break;
535 case LTTNG_KERNEL_FUNCTION:
536 module_put(event->desc->owner);
537 lttng_ftrace_destroy_private(event);
538 break;
539 case LTTNG_KERNEL_NOOP:
540 break;
541 default:
542 WARN_ON_ONCE(1);
543 }
544 list_del(&event->list);
545 lttng_destroy_context(event->ctx);
546 kmem_cache_free(event_cache, event);
547 }
548
549 /*
550 * Serialize at most one packet worth of metadata into a metadata
551 * channel.
552 * We have exclusive access to our metadata buffer (protected by the
553 * sessions_mutex), so we can do racy operations such as looking for
554 * remaining space left in packet and write, since mutual exclusion
555 * protects us from concurrent writes.
556 */
557 int lttng_metadata_output_channel(struct lttng_channel *chan,
558 struct lttng_metadata_stream *stream)
559 {
560 struct lib_ring_buffer_ctx ctx;
561 int ret = 0;
562 size_t len, reserve_len;
563
564 len = stream->metadata_cache->metadata_written -
565 stream->metadata_cache_read;
566 reserve_len = min_t(size_t,
567 chan->ops->packet_avail_size(chan->chan),
568 len);
569 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
570 sizeof(char), -1);
571 /*
572 * If reservation failed, return an error to the caller.
573 */
574 ret = chan->ops->event_reserve(&ctx, 0);
575 if (ret != 0) {
576 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
577 goto end;
578 }
579 chan->ops->event_write(&ctx,
580 stream->metadata_cache->data + stream->metadata_cache_read,
581 reserve_len);
582 chan->ops->event_commit(&ctx);
583 stream->metadata_cache_read += reserve_len;
584 ret = reserve_len;
585
586 end:
587 return ret;
588 }
589
590 /*
591 * Write the metadata to the metadata cache.
592 * Must be called with sessions_mutex held.
593 */
594 int lttng_metadata_printf(struct lttng_session *session,
595 const char *fmt, ...)
596 {
597 char *str;
598 size_t len;
599 va_list ap;
600 struct lttng_metadata_stream *stream;
601
602 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
603
604 va_start(ap, fmt);
605 str = kvasprintf(GFP_KERNEL, fmt, ap);
606 va_end(ap);
607 if (!str)
608 return -ENOMEM;
609
610 len = strlen(str);
611 if (session->metadata_cache->metadata_written + len >
612 session->metadata_cache->cache_alloc) {
613 char *tmp_cache_realloc;
614 unsigned int tmp_cache_alloc_size;
615
616 tmp_cache_alloc_size = max_t(unsigned int,
617 session->metadata_cache->cache_alloc + len,
618 session->metadata_cache->cache_alloc << 1);
619 tmp_cache_realloc = krealloc(session->metadata_cache->data,
620 tmp_cache_alloc_size, GFP_KERNEL);
621 if (!tmp_cache_realloc)
622 goto err;
623 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
624 session->metadata_cache->data = tmp_cache_realloc;
625 }
626 memcpy(session->metadata_cache->data +
627 session->metadata_cache->metadata_written,
628 str, len);
629 session->metadata_cache->metadata_written += len;
630 kfree(str);
631
632 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
633 wake_up_interruptible(&stream->read_wait);
634
635 return 0;
636
637 err:
638 kfree(str);
639 return -ENOMEM;
640 }
641
642 /*
643 * Must be called with sessions_mutex held.
644 */
645 static
646 int _lttng_field_statedump(struct lttng_session *session,
647 const struct lttng_event_field *field)
648 {
649 int ret = 0;
650
651 switch (field->type.atype) {
652 case atype_integer:
653 ret = lttng_metadata_printf(session,
654 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
655 field->type.u.basic.integer.size,
656 field->type.u.basic.integer.alignment,
657 field->type.u.basic.integer.signedness,
658 (field->type.u.basic.integer.encoding == lttng_encode_none)
659 ? "none"
660 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
661 ? "UTF8"
662 : "ASCII",
663 field->type.u.basic.integer.base,
664 #ifdef __BIG_ENDIAN
665 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
666 #else
667 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
668 #endif
669 field->name);
670 break;
671 case atype_enum:
672 ret = lttng_metadata_printf(session,
673 " %s _%s;\n",
674 field->type.u.basic.enumeration.name,
675 field->name);
676 break;
677 case atype_array:
678 {
679 const struct lttng_basic_type *elem_type;
680
681 elem_type = &field->type.u.array.elem_type;
682 ret = lttng_metadata_printf(session,
683 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
684 elem_type->u.basic.integer.size,
685 elem_type->u.basic.integer.alignment,
686 elem_type->u.basic.integer.signedness,
687 (elem_type->u.basic.integer.encoding == lttng_encode_none)
688 ? "none"
689 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
690 ? "UTF8"
691 : "ASCII",
692 elem_type->u.basic.integer.base,
693 #ifdef __BIG_ENDIAN
694 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
695 #else
696 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
697 #endif
698 field->name, field->type.u.array.length);
699 break;
700 }
701 case atype_sequence:
702 {
703 const struct lttng_basic_type *elem_type;
704 const struct lttng_basic_type *length_type;
705
706 elem_type = &field->type.u.sequence.elem_type;
707 length_type = &field->type.u.sequence.length_type;
708 ret = lttng_metadata_printf(session,
709 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
710 length_type->u.basic.integer.size,
711 (unsigned int) length_type->u.basic.integer.alignment,
712 length_type->u.basic.integer.signedness,
713 (length_type->u.basic.integer.encoding == lttng_encode_none)
714 ? "none"
715 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
716 ? "UTF8"
717 : "ASCII"),
718 length_type->u.basic.integer.base,
719 #ifdef __BIG_ENDIAN
720 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
721 #else
722 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
723 #endif
724 field->name);
725 if (ret)
726 return ret;
727
728 ret = lttng_metadata_printf(session,
729 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
730 elem_type->u.basic.integer.size,
731 (unsigned int) elem_type->u.basic.integer.alignment,
732 elem_type->u.basic.integer.signedness,
733 (elem_type->u.basic.integer.encoding == lttng_encode_none)
734 ? "none"
735 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
736 ? "UTF8"
737 : "ASCII"),
738 elem_type->u.basic.integer.base,
739 #ifdef __BIG_ENDIAN
740 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
741 #else
742 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
743 #endif
744 field->name,
745 field->name);
746 break;
747 }
748
749 case atype_string:
750 /* Default encoding is UTF8 */
751 ret = lttng_metadata_printf(session,
752 " string%s _%s;\n",
753 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
754 " { encoding = ASCII; }" : "",
755 field->name);
756 break;
757 default:
758 WARN_ON_ONCE(1);
759 return -EINVAL;
760 }
761 return ret;
762 }
763
764 static
765 int _lttng_context_metadata_statedump(struct lttng_session *session,
766 struct lttng_ctx *ctx)
767 {
768 int ret = 0;
769 int i;
770
771 if (!ctx)
772 return 0;
773 for (i = 0; i < ctx->nr_fields; i++) {
774 const struct lttng_ctx_field *field = &ctx->fields[i];
775
776 ret = _lttng_field_statedump(session, &field->event_field);
777 if (ret)
778 return ret;
779 }
780 return ret;
781 }
782
783 static
784 int _lttng_fields_metadata_statedump(struct lttng_session *session,
785 struct lttng_event *event)
786 {
787 const struct lttng_event_desc *desc = event->desc;
788 int ret = 0;
789 int i;
790
791 for (i = 0; i < desc->nr_fields; i++) {
792 const struct lttng_event_field *field = &desc->fields[i];
793
794 ret = _lttng_field_statedump(session, field);
795 if (ret)
796 return ret;
797 }
798 return ret;
799 }
800
801 /*
802 * Must be called with sessions_mutex held.
803 */
804 static
805 int _lttng_event_metadata_statedump(struct lttng_session *session,
806 struct lttng_channel *chan,
807 struct lttng_event *event)
808 {
809 int ret = 0;
810
811 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
812 return 0;
813 if (chan->channel_type == METADATA_CHANNEL)
814 return 0;
815
816 ret = lttng_metadata_printf(session,
817 "event {\n"
818 " name = %s;\n"
819 " id = %u;\n"
820 " stream_id = %u;\n",
821 event->desc->name,
822 event->id,
823 event->chan->id);
824 if (ret)
825 goto end;
826
827 if (event->ctx) {
828 ret = lttng_metadata_printf(session,
829 " context := struct {\n");
830 if (ret)
831 goto end;
832 }
833 ret = _lttng_context_metadata_statedump(session, event->ctx);
834 if (ret)
835 goto end;
836 if (event->ctx) {
837 ret = lttng_metadata_printf(session,
838 " };\n");
839 if (ret)
840 goto end;
841 }
842
843 ret = lttng_metadata_printf(session,
844 " fields := struct {\n"
845 );
846 if (ret)
847 goto end;
848
849 ret = _lttng_fields_metadata_statedump(session, event);
850 if (ret)
851 goto end;
852
853 /*
854 * LTTng space reservation can only reserve multiples of the
855 * byte size.
856 */
857 ret = lttng_metadata_printf(session,
858 " };\n"
859 "};\n\n");
860 if (ret)
861 goto end;
862
863 event->metadata_dumped = 1;
864 end:
865 return ret;
866
867 }
868
869 /*
870 * Must be called with sessions_mutex held.
871 */
872 static
873 int _lttng_channel_metadata_statedump(struct lttng_session *session,
874 struct lttng_channel *chan)
875 {
876 int ret = 0;
877
878 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
879 return 0;
880
881 if (chan->channel_type == METADATA_CHANNEL)
882 return 0;
883
884 WARN_ON_ONCE(!chan->header_type);
885 ret = lttng_metadata_printf(session,
886 "stream {\n"
887 " id = %u;\n"
888 " event.header := %s;\n"
889 " packet.context := struct packet_context;\n",
890 chan->id,
891 chan->header_type == 1 ? "struct event_header_compact" :
892 "struct event_header_large");
893 if (ret)
894 goto end;
895
896 if (chan->ctx) {
897 ret = lttng_metadata_printf(session,
898 " event.context := struct {\n");
899 if (ret)
900 goto end;
901 }
902 ret = _lttng_context_metadata_statedump(session, chan->ctx);
903 if (ret)
904 goto end;
905 if (chan->ctx) {
906 ret = lttng_metadata_printf(session,
907 " };\n");
908 if (ret)
909 goto end;
910 }
911
912 ret = lttng_metadata_printf(session,
913 "};\n\n");
914
915 chan->metadata_dumped = 1;
916 end:
917 return ret;
918 }
919
920 /*
921 * Must be called with sessions_mutex held.
922 */
923 static
924 int _lttng_stream_packet_context_declare(struct lttng_session *session)
925 {
926 return lttng_metadata_printf(session,
927 "struct packet_context {\n"
928 " uint64_clock_monotonic_t timestamp_begin;\n"
929 " uint64_clock_monotonic_t timestamp_end;\n"
930 " uint64_t content_size;\n"
931 " uint64_t packet_size;\n"
932 " unsigned long events_discarded;\n"
933 " uint32_t cpu_id;\n"
934 "};\n\n"
935 );
936 }
937
938 /*
939 * Compact header:
940 * id: range: 0 - 30.
941 * id 31 is reserved to indicate an extended header.
942 *
943 * Large header:
944 * id: range: 0 - 65534.
945 * id 65535 is reserved to indicate an extended header.
946 *
947 * Must be called with sessions_mutex held.
948 */
949 static
950 int _lttng_event_header_declare(struct lttng_session *session)
951 {
952 return lttng_metadata_printf(session,
953 "struct event_header_compact {\n"
954 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
955 " variant <id> {\n"
956 " struct {\n"
957 " uint27_clock_monotonic_t timestamp;\n"
958 " } compact;\n"
959 " struct {\n"
960 " uint32_t id;\n"
961 " uint64_clock_monotonic_t timestamp;\n"
962 " } extended;\n"
963 " } v;\n"
964 "} align(%u);\n"
965 "\n"
966 "struct event_header_large {\n"
967 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
968 " variant <id> {\n"
969 " struct {\n"
970 " uint32_clock_monotonic_t timestamp;\n"
971 " } compact;\n"
972 " struct {\n"
973 " uint32_t id;\n"
974 " uint64_clock_monotonic_t timestamp;\n"
975 " } extended;\n"
976 " } v;\n"
977 "} align(%u);\n\n",
978 lttng_alignof(uint32_t) * CHAR_BIT,
979 lttng_alignof(uint16_t) * CHAR_BIT
980 );
981 }
982
983 /*
984 * Approximation of NTP time of day to clock monotonic correlation,
985 * taken at start of trace.
986 * Yes, this is only an approximation. Yes, we can (and will) do better
987 * in future versions.
988 */
989 static
990 uint64_t measure_clock_offset(void)
991 {
992 uint64_t offset, monotonic[2], realtime;
993 struct timespec rts = { 0, 0 };
994 unsigned long flags;
995
996 /* Disable interrupts to increase correlation precision. */
997 local_irq_save(flags);
998 monotonic[0] = trace_clock_read64();
999 getnstimeofday(&rts);
1000 monotonic[1] = trace_clock_read64();
1001 local_irq_restore(flags);
1002
1003 offset = (monotonic[0] + monotonic[1]) >> 1;
1004 realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC;
1005 realtime += rts.tv_nsec;
1006 offset = realtime - offset;
1007 return offset;
1008 }
1009
1010 /*
1011 * Output metadata into this session's metadata buffers.
1012 * Must be called with sessions_mutex held.
1013 */
1014 static
1015 int _lttng_session_metadata_statedump(struct lttng_session *session)
1016 {
1017 unsigned char *uuid_c = session->uuid.b;
1018 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
1019 struct lttng_channel *chan;
1020 struct lttng_event *event;
1021 int ret = 0;
1022
1023 if (!ACCESS_ONCE(session->active))
1024 return 0;
1025 if (session->metadata_dumped)
1026 goto skip_session;
1027
1028 snprintf(uuid_s, sizeof(uuid_s),
1029 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1030 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
1031 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
1032 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
1033 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
1034
1035 ret = lttng_metadata_printf(session,
1036 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1037 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1038 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1039 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1040 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1041 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1042 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1043 "\n"
1044 "trace {\n"
1045 " major = %u;\n"
1046 " minor = %u;\n"
1047 " uuid = \"%s\";\n"
1048 " byte_order = %s;\n"
1049 " packet.header := struct {\n"
1050 " uint32_t magic;\n"
1051 " uint8_t uuid[16];\n"
1052 " uint32_t stream_id;\n"
1053 " };\n"
1054 "};\n\n",
1055 lttng_alignof(uint8_t) * CHAR_BIT,
1056 lttng_alignof(uint16_t) * CHAR_BIT,
1057 lttng_alignof(uint32_t) * CHAR_BIT,
1058 lttng_alignof(uint64_t) * CHAR_BIT,
1059 sizeof(unsigned long) * CHAR_BIT,
1060 lttng_alignof(unsigned long) * CHAR_BIT,
1061 CTF_SPEC_MAJOR,
1062 CTF_SPEC_MINOR,
1063 uuid_s,
1064 #ifdef __BIG_ENDIAN
1065 "be"
1066 #else
1067 "le"
1068 #endif
1069 );
1070 if (ret)
1071 goto end;
1072
1073 ret = lttng_metadata_printf(session,
1074 "env {\n"
1075 " hostname = \"%s\";\n"
1076 " domain = \"kernel\";\n"
1077 " sysname = \"%s\";\n"
1078 " kernel_release = \"%s\";\n"
1079 " kernel_version = \"%s\";\n"
1080 " tracer_name = \"lttng-modules\";\n"
1081 " tracer_major = %d;\n"
1082 " tracer_minor = %d;\n"
1083 " tracer_patchlevel = %d;\n"
1084 "};\n\n",
1085 current->nsproxy->uts_ns->name.nodename,
1086 utsname()->sysname,
1087 utsname()->release,
1088 utsname()->version,
1089 LTTNG_MODULES_MAJOR_VERSION,
1090 LTTNG_MODULES_MINOR_VERSION,
1091 LTTNG_MODULES_PATCHLEVEL_VERSION
1092 );
1093 if (ret)
1094 goto end;
1095
1096 ret = lttng_metadata_printf(session,
1097 "clock {\n"
1098 " name = %s;\n",
1099 "monotonic"
1100 );
1101 if (ret)
1102 goto end;
1103
1104 if (!trace_clock_uuid(clock_uuid_s)) {
1105 ret = lttng_metadata_printf(session,
1106 " uuid = \"%s\";\n",
1107 clock_uuid_s
1108 );
1109 if (ret)
1110 goto end;
1111 }
1112
1113 ret = lttng_metadata_printf(session,
1114 " description = \"Monotonic Clock\";\n"
1115 " freq = %llu; /* Frequency, in Hz */\n"
1116 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1117 " offset = %llu;\n"
1118 "};\n\n",
1119 (unsigned long long) trace_clock_freq(),
1120 (unsigned long long) measure_clock_offset()
1121 );
1122 if (ret)
1123 goto end;
1124
1125 ret = lttng_metadata_printf(session,
1126 "typealias integer {\n"
1127 " size = 27; align = 1; signed = false;\n"
1128 " map = clock.monotonic.value;\n"
1129 "} := uint27_clock_monotonic_t;\n"
1130 "\n"
1131 "typealias integer {\n"
1132 " size = 32; align = %u; signed = false;\n"
1133 " map = clock.monotonic.value;\n"
1134 "} := uint32_clock_monotonic_t;\n"
1135 "\n"
1136 "typealias integer {\n"
1137 " size = 64; align = %u; signed = false;\n"
1138 " map = clock.monotonic.value;\n"
1139 "} := uint64_clock_monotonic_t;\n\n",
1140 lttng_alignof(uint32_t) * CHAR_BIT,
1141 lttng_alignof(uint64_t) * CHAR_BIT
1142 );
1143 if (ret)
1144 goto end;
1145
1146 ret = _lttng_stream_packet_context_declare(session);
1147 if (ret)
1148 goto end;
1149
1150 ret = _lttng_event_header_declare(session);
1151 if (ret)
1152 goto end;
1153
1154 skip_session:
1155 list_for_each_entry(chan, &session->chan, list) {
1156 ret = _lttng_channel_metadata_statedump(session, chan);
1157 if (ret)
1158 goto end;
1159 }
1160
1161 list_for_each_entry(event, &session->events, list) {
1162 ret = _lttng_event_metadata_statedump(session, event->chan, event);
1163 if (ret)
1164 goto end;
1165 }
1166 session->metadata_dumped = 1;
1167 end:
1168 return ret;
1169 }
1170
1171 /**
1172 * lttng_transport_register - LTT transport registration
1173 * @transport: transport structure
1174 *
1175 * Registers a transport which can be used as output to extract the data out of
1176 * LTTng. The module calling this registration function must ensure that no
1177 * trap-inducing code will be executed by the transport functions. E.g.
1178 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1179 * is made visible to the transport function. This registration acts as a
1180 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1181 * after its registration must it synchronize the TLBs.
1182 */
1183 void lttng_transport_register(struct lttng_transport *transport)
1184 {
1185 /*
1186 * Make sure no page fault can be triggered by the module about to be
1187 * registered. We deal with this here so we don't have to call
1188 * vmalloc_sync_all() in each module's init.
1189 */
1190 wrapper_vmalloc_sync_all();
1191
1192 mutex_lock(&sessions_mutex);
1193 list_add_tail(&transport->node, &lttng_transport_list);
1194 mutex_unlock(&sessions_mutex);
1195 }
1196 EXPORT_SYMBOL_GPL(lttng_transport_register);
1197
1198 /**
1199 * lttng_transport_unregister - LTT transport unregistration
1200 * @transport: transport structure
1201 */
1202 void lttng_transport_unregister(struct lttng_transport *transport)
1203 {
1204 mutex_lock(&sessions_mutex);
1205 list_del(&transport->node);
1206 mutex_unlock(&sessions_mutex);
1207 }
1208 EXPORT_SYMBOL_GPL(lttng_transport_unregister);
1209
1210 static int __init lttng_events_init(void)
1211 {
1212 int ret;
1213
1214 event_cache = KMEM_CACHE(lttng_event, 0);
1215 if (!event_cache)
1216 return -ENOMEM;
1217 ret = lttng_abi_init();
1218 if (ret)
1219 goto error_abi;
1220 return 0;
1221 error_abi:
1222 kmem_cache_destroy(event_cache);
1223 return ret;
1224 }
1225
1226 module_init(lttng_events_init);
1227
1228 static void __exit lttng_events_exit(void)
1229 {
1230 struct lttng_session *session, *tmpsession;
1231
1232 lttng_abi_exit();
1233 list_for_each_entry_safe(session, tmpsession, &sessions, list)
1234 lttng_session_destroy(session);
1235 kmem_cache_destroy(event_cache);
1236 }
1237
1238 module_exit(lttng_events_exit);
1239
1240 MODULE_LICENSE("GPL and additional rights");
1241 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1242 MODULE_DESCRIPTION("LTTng Events");
This page took 0.058578 seconds and 5 git commands to generate.