| 1 | /* |
| 2 | * (C) Copyright 2009-2011 - |
| 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 5 | * LTTng UST vpid context. |
| 6 | * |
| 7 | * Dual LGPL v2.1/GPL v2 license. |
| 8 | */ |
| 9 | |
| 10 | #include <sys/types.h> |
| 11 | #include <unistd.h> |
| 12 | #include <ust/lttng-events.h> |
| 13 | #include <ust/lttng-tracer.h> |
| 14 | #include <ust/ringbuffer-config.h> |
| 15 | |
| 16 | #ifdef __linux__ |
| 17 | static inline |
| 18 | pid_t wrapper_getpid(void) |
| 19 | { |
| 20 | return getpid(); |
| 21 | } |
| 22 | |
| 23 | void lttng_context_vpid_reset(void) |
| 24 | { |
| 25 | } |
| 26 | #else |
| 27 | /* |
| 28 | * We cache the result to ensure we don't trigger a system call for |
| 29 | * each event. |
| 30 | */ |
| 31 | static pid_t cached_vpid; |
| 32 | |
| 33 | static inline |
| 34 | pid_t wrapper_getpid(void) |
| 35 | { |
| 36 | if (unlikely(!cached_vpid)) |
| 37 | cached_vpid = getpid(); |
| 38 | return cached_vpid; |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | * Upon fork or clone, the PID assigned to our thread is not the same as |
| 43 | * we kept in cache. |
| 44 | */ |
| 45 | void lttng_context_vpid_reset(void) |
| 46 | { |
| 47 | cached_vpid = 0; |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | static |
| 52 | size_t vpid_get_size(size_t offset) |
| 53 | { |
| 54 | size_t size = 0; |
| 55 | |
| 56 | size += lib_ring_buffer_align(offset, lttng_alignof(pid_t)); |
| 57 | size += sizeof(pid_t); |
| 58 | return size; |
| 59 | } |
| 60 | |
| 61 | static |
| 62 | void vpid_record(struct lttng_ctx_field *field, |
| 63 | struct lib_ring_buffer_ctx *ctx, |
| 64 | struct ltt_channel *chan) |
| 65 | { |
| 66 | pid_t pid; |
| 67 | |
| 68 | pid = wrapper_getpid(); |
| 69 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(pid)); |
| 70 | chan->ops->event_write(ctx, &pid, sizeof(pid)); |
| 71 | } |
| 72 | |
| 73 | int lttng_add_vpid_to_ctx(struct lttng_ctx **ctx) |
| 74 | { |
| 75 | struct lttng_ctx_field *field; |
| 76 | |
| 77 | field = lttng_append_context(ctx); |
| 78 | if (!field) |
| 79 | return -ENOMEM; |
| 80 | if (lttng_find_context(*ctx, "vpid")) { |
| 81 | lttng_remove_context_field(ctx, field); |
| 82 | return -EEXIST; |
| 83 | } |
| 84 | field->event_field.name = "vpid"; |
| 85 | field->event_field.type.atype = atype_integer; |
| 86 | field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT; |
| 87 | field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT; |
| 88 | field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t); |
| 89 | field->event_field.type.u.basic.integer.reverse_byte_order = 0; |
| 90 | field->event_field.type.u.basic.integer.base = 10; |
| 91 | field->event_field.type.u.basic.integer.encoding = lttng_encode_none; |
| 92 | field->get_size = vpid_get_size; |
| 93 | field->record = vpid_record; |
| 94 | return 0; |
| 95 | } |