From c1ef86f0812ff62354175f4c7136930814b8f850 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Tue, 27 Sep 2011 20:46:04 -0400 Subject: [PATCH] Add vpid context Signed-off-by: Mathieu Desnoyers --- include/ust/lttng-events.h | 2 + include/ust/lttng-ust-abi.h | 3 +- libust/Makefile.am | 1 + libust/lttng-context-vpid.c | 95 +++++++++++++++++++++++++++++++++++++ libust/lttng-ust-abi.c | 2 + 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 libust/lttng-context-vpid.c diff --git a/include/ust/lttng-events.h b/include/ust/lttng-events.h index 6bb4e8cc..0439ea7a 100644 --- a/include/ust/lttng-events.h +++ b/include/ust/lttng-events.h @@ -321,7 +321,9 @@ void lttng_remove_context_field(struct lttng_ctx **ctx_p, struct lttng_ctx_field *field); void lttng_destroy_context(struct lttng_ctx *ctx); int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx); +int lttng_add_vpid_to_ctx(struct lttng_ctx **ctx); int lttng_add_pthread_id_to_ctx(struct lttng_ctx **ctx); void lttng_context_vtid_reset(void); +void lttng_context_vpid_reset(void); #endif /* _UST_LTTNG_EVENTS_H */ diff --git a/include/ust/lttng-ust-abi.h b/include/ust/lttng-ust-abi.h index 71465791..c967fb11 100644 --- a/include/ust/lttng-ust-abi.h +++ b/include/ust/lttng-ust-abi.h @@ -67,7 +67,8 @@ struct lttng_ust_event { enum lttng_ust_context_type { LTTNG_UST_CONTEXT_VTID = 0, - LTTNG_UST_CONTEXT_PTHREAD_ID = 1, + LTTNG_UST_CONTEXT_VPID = 1, + LTTNG_UST_CONTEXT_PTHREAD_ID = 2, }; struct lttng_ust_context { diff --git a/libust/Makefile.am b/libust/Makefile.am index 9857dd2d..cb00e179 100644 --- a/libust/Makefile.am +++ b/libust/Makefile.am @@ -20,6 +20,7 @@ libust_la_SOURCES = \ probes/lttng-probe-ust.c \ probes/lttng-probe-ust.h \ lttng-context-vtid.c \ + lttng-context-vpid.c \ lttng-context-pthread-id.c \ ltt-context.c diff --git a/libust/lttng-context-vpid.c b/libust/lttng-context-vpid.c new file mode 100644 index 00000000..71395c95 --- /dev/null +++ b/libust/lttng-context-vpid.c @@ -0,0 +1,95 @@ +/* + * (C) Copyright 2009-2011 - + * Mathieu Desnoyers + * + * LTTng UST vpid context. + * + * Dual LGPL v2.1/GPL v2 license. + */ + +#include +#include +#include +#include +#include + +#ifdef __linux__ +static inline +pid_t wrapper_getpid(void) +{ + return getpid(); +} + +void lttng_context_vpid_reset(void) +{ +} +#else +/* + * We cache the result to ensure we don't trigger a system call for + * each event. + */ +static pid_t cached_vpid; + +static inline +pid_t wrapper_getpid(void) +{ + if (unlikely(!cached_vpid)) + cached_vpid = getpid(); + return cached_vpid; +} + +/* + * Upon fork or clone, the PID assigned to our thread is not the same as + * we kept in cache. + */ +void lttng_context_vpid_reset(void) +{ + cached_vpid = 0; +} +#endif + +static +size_t vpid_get_size(size_t offset) +{ + size_t size = 0; + + size += lib_ring_buffer_align(offset, lttng_alignof(pid_t)); + size += sizeof(pid_t); + return size; +} + +static +void vpid_record(struct lttng_ctx_field *field, + struct lib_ring_buffer_ctx *ctx, + struct ltt_channel *chan) +{ + pid_t pid; + + pid = wrapper_getpid(); + lib_ring_buffer_align_ctx(ctx, lttng_alignof(pid)); + chan->ops->event_write(ctx, &pid, sizeof(pid)); +} + +int lttng_add_vpid_to_ctx(struct lttng_ctx **ctx) +{ + struct lttng_ctx_field *field; + + field = lttng_append_context(ctx); + if (!field) + return -ENOMEM; + if (lttng_find_context(*ctx, "vpid")) { + lttng_remove_context_field(ctx, field); + return -EEXIST; + } + field->event_field.name = "vpid"; + field->event_field.type.atype = atype_integer; + field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT; + field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT; + field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t); + field->event_field.type.u.basic.integer.reverse_byte_order = 0; + field->event_field.type.u.basic.integer.base = 10; + field->event_field.type.u.basic.integer.encoding = lttng_encode_none; + field->get_size = vpid_get_size; + field->record = vpid_record; + return 0; +} diff --git a/libust/lttng-ust-abi.c b/libust/lttng-ust-abi.c index 35c93c91..e125ac6d 100644 --- a/libust/lttng-ust-abi.c +++ b/libust/lttng-ust-abi.c @@ -276,6 +276,8 @@ long lttng_abi_add_context(int objd, return lttng_add_pthread_id_to_ctx(ctx); case LTTNG_UST_CONTEXT_VTID: return lttng_add_vtid_to_ctx(ctx); + case LTTNG_UST_CONTEXT_VPID: + return lttng_add_vpid_to_ctx(ctx); default: return -EINVAL; } -- 2.34.1