From cefef7a70a723c31ab6a0746c7611f145337dabe Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Mon, 6 Jul 2020 11:48:16 -0400 Subject: [PATCH] Add userspace time namespace context This was introduced in kernel v5.6.0, on a system running an older kernel, zero will be returned. Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers Change-Id: I1ea66a98c96a4de084c02521f6982e8c7d1b53bb --- doc/man/lttng-ust.3.txt | 4 + include/lttng/ust-abi.h | 1 + include/lttng/ust-events.h | 2 + liblttng-ust/Makefile.am | 1 + liblttng-ust/lttng-context-time-ns.c | 164 +++++++++++++++++++++++++++ liblttng-ust/lttng-context.c | 5 + liblttng-ust/lttng-events.c | 2 + liblttng-ust/lttng-tracer-core.h | 1 + liblttng-ust/lttng-ust-comm.c | 2 + 9 files changed, 182 insertions(+) create mode 100644 liblttng-ust/lttng-context-time-ns.c diff --git a/doc/man/lttng-ust.3.txt b/doc/man/lttng-ust.3.txt index 507aaca5..9b86906a 100644 --- a/doc/man/lttng-ust.3.txt +++ b/doc/man/lttng-ust.3.txt @@ -818,6 +818,10 @@ The following man:namespaces(7) context fields are supported by LTTng-UST: Process IDs namespace: inode number of the current man:pid_namespaces(7) in the proc filesystem. +`time_ns`:: + Time and system clock namespace: inode number of the current + man:time_namespaces(7) in the proc filesystem. + `user_ns`:: User and group IDs namespace: inode number of the current man:user_namespaces(7) in the proc filesystem. diff --git a/include/lttng/ust-abi.h b/include/lttng/ust-abi.h index a823de54..8bf77f64 100644 --- a/include/lttng/ust-abi.h +++ b/include/lttng/ust-abi.h @@ -159,6 +159,7 @@ enum lttng_ust_context_type { LTTNG_UST_CONTEXT_VGID = 18, LTTNG_UST_CONTEXT_VEGID = 19, LTTNG_UST_CONTEXT_VSGID = 20, + LTTNG_UST_CONTEXT_TIME_NS = 21, }; struct lttng_ust_perf_counter_ctx { diff --git a/include/lttng/ust-events.h b/include/lttng/ust-events.h index b050d30e..3fee93c4 100644 --- a/include/lttng/ust-events.h +++ b/include/lttng/ust-events.h @@ -735,6 +735,7 @@ int lttng_add_ipc_ns_to_ctx(struct lttng_ctx **ctx); int lttng_add_mnt_ns_to_ctx(struct lttng_ctx **ctx); int lttng_add_net_ns_to_ctx(struct lttng_ctx **ctx); int lttng_add_pid_ns_to_ctx(struct lttng_ctx **ctx); +int lttng_add_time_ns_to_ctx(struct lttng_ctx **ctx); int lttng_add_user_ns_to_ctx(struct lttng_ctx **ctx); int lttng_add_uts_ns_to_ctx(struct lttng_ctx **ctx); int lttng_add_vuid_to_ctx(struct lttng_ctx **ctx); @@ -751,6 +752,7 @@ void lttng_context_ipc_ns_reset(void); void lttng_context_mnt_ns_reset(void); void lttng_context_net_ns_reset(void); void lttng_context_pid_ns_reset(void); +void lttng_context_time_ns_reset(void); void lttng_context_user_ns_reset(void); void lttng_context_uts_ns_reset(void); void lttng_context_vuid_reset(void); diff --git a/liblttng-ust/Makefile.am b/liblttng-ust/Makefile.am index abb7a8dc..3f4c023d 100644 --- a/liblttng-ust/Makefile.am +++ b/liblttng-ust/Makefile.am @@ -38,6 +38,7 @@ liblttng_ust_runtime_la_SOURCES = \ lttng-context-mnt-ns.c \ lttng-context-net-ns.c \ lttng-context-pid-ns.c \ + lttng-context-time-ns.c \ lttng-context-user-ns.c \ lttng-context-uts-ns.c \ lttng-context-vuid.c \ diff --git a/liblttng-ust/lttng-context-time-ns.c b/liblttng-ust/lttng-context-time-ns.c new file mode 100644 index 00000000..dda8fb37 --- /dev/null +++ b/liblttng-ust/lttng-context-time-ns.c @@ -0,0 +1,164 @@ +/* + * lttng-context-time-ns.c + * + * LTTng UST time namespace context. + * + * Copyright (C) 2009-2012 Mathieu Desnoyers + * 2020 Michael Jeanson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; only + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define _LGPL_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "lttng-tracer-core.h" +#include "ns.h" + + +/* + * We cache the result to ensure we don't stat(2) the proc filesystem on + * each event. + */ +static DEFINE_URCU_TLS_INIT(ino_t, cached_time_ns, NS_INO_UNINITIALIZED); + +static +ino_t get_time_ns(void) +{ + struct stat sb; + ino_t time_ns; + + time_ns = CMM_LOAD_SHARED(URCU_TLS(cached_time_ns)); + + /* + * If the cache is populated, do nothing and return the + * cached inode number. + */ + if (caa_likely(time_ns != NS_INO_UNINITIALIZED)) + return time_ns; + + /* + * At this point we have to populate the cache, set the initial + * value to NS_INO_UNAVAILABLE (0), if we fail to get the inode + * number from the proc filesystem, this is the value we will + * cache. + */ + time_ns = NS_INO_UNAVAILABLE; + + /* + * /proc/thread-self was introduced in kernel v3.17 + */ + if (stat("/proc/thread-self/ns/time", &sb) == 0) { + time_ns = sb.st_ino; + } else { + char proc_ns_path[LTTNG_PROC_NS_PATH_MAX]; + + if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX, + "/proc/self/task/%d/ns/time", + lttng_gettid()) >= 0) { + + if (stat(proc_ns_path, &sb) == 0) { + time_ns = sb.st_ino; + } + } + } + + /* + * And finally, store the inode number in the cache. + */ + CMM_STORE_SHARED(URCU_TLS(cached_time_ns), time_ns); + + return time_ns; +} + +/* + * The time namespace can change for 2 reasons + * * setns(2) called with the fd of a different time ns + * * clone(2) / fork(2) after a call to unshare(2) with the CLONE_NEWTIME flag + */ +void lttng_context_time_ns_reset(void) +{ + CMM_STORE_SHARED(URCU_TLS(cached_time_ns), NS_INO_UNINITIALIZED); +} + +static +size_t time_ns_get_size(struct lttng_ctx_field *field, size_t offset) +{ + size_t size = 0; + + size += lib_ring_buffer_align(offset, lttng_alignof(ino_t)); + size += sizeof(ino_t); + return size; +} + +static +void time_ns_record(struct lttng_ctx_field *field, + struct lttng_ust_lib_ring_buffer_ctx *ctx, + struct lttng_channel *chan) +{ + ino_t time_ns; + + time_ns = get_time_ns(); + lib_ring_buffer_align_ctx(ctx, lttng_alignof(time_ns)); + chan->ops->event_write(ctx, &time_ns, sizeof(time_ns)); +} + +static +void time_ns_get_value(struct lttng_ctx_field *field, + struct lttng_ctx_value *value) +{ + value->u.s64 = get_time_ns(); +} + +int lttng_add_time_ns_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, "time_ns")) { + lttng_remove_context_field(ctx, field); + return -EEXIST; + } + field->event_field.name = "time_ns"; + field->event_field.type.atype = atype_integer; + field->event_field.type.u.integer.size = sizeof(ino_t) * CHAR_BIT; + field->event_field.type.u.integer.alignment = lttng_alignof(ino_t) * CHAR_BIT; + field->event_field.type.u.integer.signedness = lttng_is_signed_type(ino_t); + field->event_field.type.u.integer.reverse_byte_order = 0; + field->event_field.type.u.integer.base = 10; + field->event_field.type.u.integer.encoding = lttng_encode_none; + field->get_size = time_ns_get_size; + field->record = time_ns_record; + field->get_value = time_ns_get_value; + lttng_context_update(*ctx); + return 0; +} + +/* + * * Force a read (imply TLS fixup for dlopen) of TLS variables. + * */ +void lttng_fixup_time_ns_tls(void) +{ + asm volatile ("" : : "m" (URCU_TLS(cached_time_ns))); +} diff --git a/liblttng-ust/lttng-context.c b/liblttng-ust/lttng-context.c index 5637028b..fc564b6a 100644 --- a/liblttng-ust/lttng-context.c +++ b/liblttng-ust/lttng-context.c @@ -454,6 +454,11 @@ int lttng_session_context_init(struct lttng_ctx **ctx) WARN("Cannot add context lttng_add_pid_ns_to_ctx"); goto error; } + ret = lttng_add_time_ns_to_ctx(ctx); + if (ret) { + WARN("Cannot add context lttng_add_time_ns_to_ctx"); + goto error; + } ret = lttng_add_user_ns_to_ctx(ctx); if (ret) { WARN("Cannot add context lttng_add_user_ns_to_ctx"); diff --git a/liblttng-ust/lttng-events.c b/liblttng-ust/lttng-events.c index cbed800b..627cd4e1 100644 --- a/liblttng-ust/lttng-events.c +++ b/liblttng-ust/lttng-events.c @@ -1145,6 +1145,8 @@ int lttng_attach_context(struct lttng_ust_context *context_param, return lttng_add_net_ns_to_ctx(ctx); case LTTNG_UST_CONTEXT_PID_NS: return lttng_add_pid_ns_to_ctx(ctx); + case LTTNG_UST_CONTEXT_TIME_NS: + return lttng_add_time_ns_to_ctx(ctx); case LTTNG_UST_CONTEXT_USER_NS: return lttng_add_user_ns_to_ctx(ctx); case LTTNG_UST_CONTEXT_UTS_NS: diff --git a/liblttng-ust/lttng-tracer-core.h b/liblttng-ust/lttng-tracer-core.h index 1a0f03a1..bce6c239 100644 --- a/liblttng-ust/lttng-tracer-core.h +++ b/liblttng-ust/lttng-tracer-core.h @@ -55,6 +55,7 @@ void lttng_fixup_procname_tls(void); void lttng_fixup_cgroup_ns_tls(void); void lttng_fixup_ipc_ns_tls(void); void lttng_fixup_net_ns_tls(void); +void lttng_fixup_time_ns_tls(void); void lttng_fixup_uts_ns_tls(void); const char *lttng_ust_obj_get_name(int id); diff --git a/liblttng-ust/lttng-ust-comm.c b/liblttng-ust/lttng-ust-comm.c index 900a0e84..3847c976 100644 --- a/liblttng-ust/lttng-ust-comm.c +++ b/liblttng-ust/lttng-ust-comm.c @@ -434,6 +434,7 @@ void lttng_ust_fixup_tls(void) lttng_fixup_cgroup_ns_tls(); lttng_fixup_ipc_ns_tls(); lttng_fixup_net_ns_tls(); + lttng_fixup_time_ns_tls(); lttng_fixup_uts_ns_tls(); } @@ -2065,6 +2066,7 @@ void ust_context_ns_reset(void) lttng_context_mnt_ns_reset(); lttng_context_net_ns_reset(); lttng_context_user_ns_reset(); + lttng_context_time_ns_reset(); lttng_context_uts_ns_reset(); } -- 2.34.1