| 1 | /* |
| 2 | * (C) Copyright 2009-2011 - |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * LTTng UST vtid context. |
| 6 | * |
| 7 | * Dual LGPL v2.1/GPL v2 license. |
| 8 | */ |
| 9 | |
| 10 | #include <sys/types.h> |
| 11 | #include <unistd.h> |
| 12 | #include <lttng/ust-events.h> |
| 13 | #include <lttng/ust-tracer.h> |
| 14 | #include <lttng/ringbuffer-config.h> |
| 15 | #include <lttng/ust-tid.h> |
| 16 | |
| 17 | /* |
| 18 | * We cache the result to ensure we don't trigger a system call for |
| 19 | * each event. |
| 20 | */ |
| 21 | static __thread pid_t cached_vtid; |
| 22 | |
| 23 | /* |
| 24 | * Upon fork or clone, the TID assigned to our thread is not the same as |
| 25 | * we kept in cache. Luckily, we are the only thread surviving in the |
| 26 | * child process, so we can simply clear our cached version. |
| 27 | */ |
| 28 | void lttng_context_vtid_reset(void) |
| 29 | { |
| 30 | cached_vtid = 0; |
| 31 | } |
| 32 | |
| 33 | static |
| 34 | size_t vtid_get_size(size_t offset) |
| 35 | { |
| 36 | size_t size = 0; |
| 37 | |
| 38 | size += lib_ring_buffer_align(offset, lttng_alignof(pid_t)); |
| 39 | size += sizeof(pid_t); |
| 40 | return size; |
| 41 | } |
| 42 | |
| 43 | static |
| 44 | void vtid_record(struct lttng_ctx_field *field, |
| 45 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 46 | struct ltt_channel *chan) |
| 47 | { |
| 48 | if (caa_unlikely(!cached_vtid)) |
| 49 | cached_vtid = gettid(); |
| 50 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(cached_vtid)); |
| 51 | chan->ops->event_write(ctx, &cached_vtid, sizeof(cached_vtid)); |
| 52 | } |
| 53 | |
| 54 | int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx) |
| 55 | { |
| 56 | struct lttng_ctx_field *field; |
| 57 | |
| 58 | field = lttng_append_context(ctx); |
| 59 | if (!field) |
| 60 | return -ENOMEM; |
| 61 | if (lttng_find_context(*ctx, "vtid")) { |
| 62 | lttng_remove_context_field(ctx, field); |
| 63 | return -EEXIST; |
| 64 | } |
| 65 | field->event_field.name = "vtid"; |
| 66 | field->event_field.type.atype = atype_integer; |
| 67 | field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT; |
| 68 | field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT; |
| 69 | field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t); |
| 70 | field->event_field.type.u.basic.integer.reverse_byte_order = 0; |
| 71 | field->event_field.type.u.basic.integer.base = 10; |
| 72 | field->event_field.type.u.basic.integer.encoding = lttng_encode_none; |
| 73 | field->get_size = vtid_get_size; |
| 74 | field->record = vtid_record; |
| 75 | return 0; |
| 76 | } |