From 02954b5aa1530207c08ea83cd19c2695c3d3bb58 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Fri, 28 Feb 2025 01:56:20 +0000 Subject: [PATCH 01/57] Generate traces for thapi_start() and thapi_end() --- utils/Makefile.am | 16 ++++++++++++++++ utils/thapi_toggle_tracepoints.tp | 13 +++++++++++++ utils/toggle_tracer.c | 9 +++++++++ 3 files changed, 38 insertions(+) create mode 100644 utils/thapi_toggle_tracepoints.tp create mode 100644 utils/toggle_tracer.c diff --git a/utils/Makefile.am b/utils/Makefile.am index 31cac05e2..a0ec96781 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -44,6 +44,22 @@ whichlib64_CXXFLAGS = -Wall -Wextra -std=c++17 bin_PROGRAMS += thapi_metadata +toggledir = $(pkglibdir)/toggle +toggle_LTLIBRARIES = libtoggle.la + +BUILT_SOURCES += \ + thapi_toggle_tracepoints.h \ + thapi_toggle_tracepoints.c + +nodist_libtoggle_la_SOURCES = \ + thapi_toggle_tracepoints.h \ + thapi_toggle_tracepoints.c + +libtoggle_la_SOURCES = toggle_tracer.c + +libtoggle_la_CFLAGS = $(LTTNG_FLAGS) $(LTTNG_UST_CFLAGS) +libtoggle_la_LDFLAGS = $(LTTNG_UST_LIBS) + bin_SCRIPTS = \ babeltrace_thapi diff --git a/utils/thapi_toggle_tracepoints.tp b/utils/thapi_toggle_tracepoints.tp new file mode 100644 index 000000000..e4c78b490 --- /dev/null +++ b/utils/thapi_toggle_tracepoints.tp @@ -0,0 +1,13 @@ +TRACEPOINT_EVENT( + lttng_ust_thapi, + start, + TP_ARGS(), + TP_FIELDS() +) + +TRACEPOINT_EVENT( + lttng_ust_thapi, + stop, + TP_ARGS(), + TP_FIELDS() +) diff --git a/utils/toggle_tracer.c b/utils/toggle_tracer.c new file mode 100644 index 000000000..b4c1697ff --- /dev/null +++ b/utils/toggle_tracer.c @@ -0,0 +1,9 @@ +#include "thapi_toggle_tracepoints.h" + +void thapi_start(void) { + tracepoint(lttng_ust_thapi, start); +} + +void thapi_stop(void) { + tracepoint(lttng_ust_thapi, stop); +} From 848561632ae0773357fedcefe5255469536f7503 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 3 Mar 2025 22:02:51 +0000 Subject: [PATCH 02/57] Call thapi_stop() if libtoggle is linked This is done by adding `__attribute((constructor))__` to `thapi_stop()`. Added an autotools check to see if the compiler supports `__attribute((constructor))__`. --- configure.ac | 17 +++++++++++++++++ utils/toggle_tracer.c | 2 ++ 2 files changed, 19 insertions(+) diff --git a/configure.ac b/configure.ac index 623b77b25..21f02f4ee 100644 --- a/configure.ac +++ b/configure.ac @@ -161,6 +161,23 @@ AC_FUNC_MMAP AC_FUNC_REALLOC AC_CHECK_FUNCS([clock_gettime ftruncate memmove memset strdup strstr strtoull strlen strchr]) +# Check if __attribute__((constructor)) works. Source: +# https://github.com/openucx/ucx/blob/72ae40c607067b6dfadf0d208f6811171bbf36b6/config/m4/ucs.m4#L140 +AC_DEFUN([CHECK_CROSS_COMP], [ + AC_RUN_IFELSE([$1], [$2], [$3], + [AC_LINK_IFELSE([$1], [$2], [$3])]) +]) + +AC_MSG_CHECKING([__attribute__((constructor))]) +CHECK_CROSS_COMP([AC_LANG_SOURCE([static int rc = 1; + static void constructor_test() __attribute__((constructor)); + static void constructor_test() { rc = 0; } + int main(int argc, char** argv) { return rc; }])], + [AC_MSG_RESULT([yes])], + [AC_MSG_ERROR([Cannot continue. Please use compiler that + supports __attribute__((constructor))])] + ) + # Required for configuring thapi.pc.in PKG_PROG_PKG_CONFIG diff --git a/utils/toggle_tracer.c b/utils/toggle_tracer.c index b4c1697ff..a68f448f0 100644 --- a/utils/toggle_tracer.c +++ b/utils/toggle_tracer.c @@ -1,5 +1,7 @@ #include "thapi_toggle_tracepoints.h" +void thapi_stop() __attribute__((constructor)); + void thapi_start(void) { tracepoint(lttng_ust_thapi, start); } From 4e47f7bf15c4acb46050cdf5034a287fd753cca3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 3 Mar 2025 22:40:31 +0000 Subject: [PATCH 03/57] Rename toggle to ThapiProfiler and install `thapi_profiler.h` header files to provied declarations of `thapi_start()` and `thapi_stop()`. --- utils/Makefile.am | 22 ++++++++++--------- utils/{toggle_tracer.c => thapi_profiler.c} | 2 +- utils/thapi_profiler.h | 7 ++++++ ...oints.tp => thapi_profiler_tracepoints.tp} | 0 4 files changed, 20 insertions(+), 11 deletions(-) rename utils/{toggle_tracer.c => thapi_profiler.c} (81%) create mode 100644 utils/thapi_profiler.h rename utils/{thapi_toggle_tracepoints.tp => thapi_profiler_tracepoints.tp} (100%) diff --git a/utils/Makefile.am b/utils/Makefile.am index a0ec96781..11dd45251 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -44,21 +44,23 @@ whichlib64_CXXFLAGS = -Wall -Wextra -std=c++17 bin_PROGRAMS += thapi_metadata -toggledir = $(pkglibdir)/toggle -toggle_LTLIBRARIES = libtoggle.la +include_HEADERS = thapi_profiler.h + +ThapiProfilerdir = $(pkglibdir) +ThapiProfiler_LTLIBRARIES = libThapiProfiler.la BUILT_SOURCES += \ - thapi_toggle_tracepoints.h \ - thapi_toggle_tracepoints.c + thapi_profiler_tracepoints.h \ + thapi_profiler_tracepoints.c -nodist_libtoggle_la_SOURCES = \ - thapi_toggle_tracepoints.h \ - thapi_toggle_tracepoints.c +nodist_libThapiProfiler_la_SOURCES = \ + thapi_profiler_tracepoints.h \ + thapi_profiler_tracepoints.c -libtoggle_la_SOURCES = toggle_tracer.c +libThapiProfiler_la_SOURCES = thapi_profiler.c -libtoggle_la_CFLAGS = $(LTTNG_FLAGS) $(LTTNG_UST_CFLAGS) -libtoggle_la_LDFLAGS = $(LTTNG_UST_LIBS) +libThapiProfiler_la_CFLAGS = $(LTTNG_FLAGS) $(LTTNG_UST_CFLAGS) +libThapiProfiler_la_LDFLAGS = $(LTTNG_UST_LIBS) bin_SCRIPTS = \ babeltrace_thapi diff --git a/utils/toggle_tracer.c b/utils/thapi_profiler.c similarity index 81% rename from utils/toggle_tracer.c rename to utils/thapi_profiler.c index a68f448f0..f5ddd7dd9 100644 --- a/utils/toggle_tracer.c +++ b/utils/thapi_profiler.c @@ -1,4 +1,4 @@ -#include "thapi_toggle_tracepoints.h" +#include "thapi_profiler_tracepoints.h" void thapi_stop() __attribute__((constructor)); diff --git a/utils/thapi_profiler.h b/utils/thapi_profiler.h new file mode 100644 index 000000000..d7999c88f --- /dev/null +++ b/utils/thapi_profiler.h @@ -0,0 +1,7 @@ +#if !defined(THAPI_PROFILER) +#define THAPI_PROFILER + +void thapi_stop(); +void thapi_start(); + +#endif // THAPI_PROFILER diff --git a/utils/thapi_toggle_tracepoints.tp b/utils/thapi_profiler_tracepoints.tp similarity index 100% rename from utils/thapi_toggle_tracepoints.tp rename to utils/thapi_profiler_tracepoints.tp From b89a8ac8d1da6704ae514ef300aa1978f4e47db1 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 3 Mar 2025 22:46:54 +0000 Subject: [PATCH 04/57] Add `profiler_` to ThapiProfiler symbols --- utils/thapi_profiler.c | 10 +++++----- utils/thapi_profiler.h | 4 ++-- utils/thapi_profiler_tracepoints.tp | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/utils/thapi_profiler.c b/utils/thapi_profiler.c index f5ddd7dd9..aa8c21ac1 100644 --- a/utils/thapi_profiler.c +++ b/utils/thapi_profiler.c @@ -1,11 +1,11 @@ #include "thapi_profiler_tracepoints.h" -void thapi_stop() __attribute__((constructor)); +void thapi_profiler_stop() __attribute__((constructor)); -void thapi_start(void) { - tracepoint(lttng_ust_thapi, start); +void thapi_profiler_start(void) { + tracepoint(lttng_ust_profiler, start); } -void thapi_stop(void) { - tracepoint(lttng_ust_thapi, stop); +void thapi_profiler_stop(void) { + tracepoint(lttng_ust_profiler, stop); } diff --git a/utils/thapi_profiler.h b/utils/thapi_profiler.h index d7999c88f..664ed7bab 100644 --- a/utils/thapi_profiler.h +++ b/utils/thapi_profiler.h @@ -1,7 +1,7 @@ #if !defined(THAPI_PROFILER) #define THAPI_PROFILER -void thapi_stop(); -void thapi_start(); +void thapi_profiler_start(); +void thapi_profiler_stop(); #endif // THAPI_PROFILER diff --git a/utils/thapi_profiler_tracepoints.tp b/utils/thapi_profiler_tracepoints.tp index e4c78b490..5d74b2be4 100644 --- a/utils/thapi_profiler_tracepoints.tp +++ b/utils/thapi_profiler_tracepoints.tp @@ -1,12 +1,12 @@ TRACEPOINT_EVENT( - lttng_ust_thapi, + lttng_ust_profiler, start, TP_ARGS(), TP_FIELDS() ) TRACEPOINT_EVENT( - lttng_ust_thapi, + lttng_ust_profiler, stop, TP_ARGS(), TP_FIELDS() From 732ab35cf6c1bdc4d43dc8e50b0f2fc9011ea958 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 01:13:56 +0000 Subject: [PATCH 05/57] Fix `make distcheck` failure --- utils/Makefile.am | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/Makefile.am b/utils/Makefile.am index 11dd45251..92ca2594b 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -100,6 +100,7 @@ EXTRA_DIST = \ type_registry.rb \ dump_trace_format.rb \ thapi_metadata_tracepoints.tp \ + thapi_profiler_tracepoints.tp \ command.rb \ command_index.rb \ meta_parameter_spec.rb \ From 23a5817ed1480a51510318b77c1b09dab2f3fc2b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 01:30:59 +0000 Subject: [PATCH 06/57] Remove profiler from symbols --- utils/Makefile.am | 24 +++++++++---------- utils/thapi.c | 11 +++++++++ utils/thapi.h | 7 ++++++ utils/thapi_profiler.c | 11 --------- utils/thapi_profiler.h | 7 ------ ...er_tracepoints.tp => thapi_tracepoints.tp} | 4 ++-- 6 files changed, 32 insertions(+), 32 deletions(-) create mode 100644 utils/thapi.c create mode 100644 utils/thapi.h delete mode 100644 utils/thapi_profiler.c delete mode 100644 utils/thapi_profiler.h rename utils/{thapi_profiler_tracepoints.tp => thapi_tracepoints.tp} (71%) diff --git a/utils/Makefile.am b/utils/Makefile.am index 92ca2594b..01927c490 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -44,23 +44,23 @@ whichlib64_CXXFLAGS = -Wall -Wextra -std=c++17 bin_PROGRAMS += thapi_metadata -include_HEADERS = thapi_profiler.h +include_HEADERS = thapi.h -ThapiProfilerdir = $(pkglibdir) -ThapiProfiler_LTLIBRARIES = libThapiProfiler.la +Thapidir = $(pkglibdir) +Thapi_LTLIBRARIES = libThapi.la BUILT_SOURCES += \ - thapi_profiler_tracepoints.h \ - thapi_profiler_tracepoints.c + thapi_tracepoints.h \ + thapi_tracepoints.c -nodist_libThapiProfiler_la_SOURCES = \ - thapi_profiler_tracepoints.h \ - thapi_profiler_tracepoints.c +nodist_libThapi_la_SOURCES = \ + thapi_tracepoints.h \ + thapi_tracepoints.c -libThapiProfiler_la_SOURCES = thapi_profiler.c +libThapi_la_SOURCES = thapi.c -libThapiProfiler_la_CFLAGS = $(LTTNG_FLAGS) $(LTTNG_UST_CFLAGS) -libThapiProfiler_la_LDFLAGS = $(LTTNG_UST_LIBS) +libThapi_la_CFLAGS = $(LTTNG_FLAGS) $(LTTNG_UST_CFLAGS) +libThapi_la_LDFLAGS = $(LTTNG_UST_LIBS) bin_SCRIPTS = \ babeltrace_thapi @@ -100,7 +100,7 @@ EXTRA_DIST = \ type_registry.rb \ dump_trace_format.rb \ thapi_metadata_tracepoints.tp \ - thapi_profiler_tracepoints.tp \ + thapi_tracepoints.tp \ command.rb \ command_index.rb \ meta_parameter_spec.rb \ diff --git a/utils/thapi.c b/utils/thapi.c new file mode 100644 index 000000000..ffb6ce3bc --- /dev/null +++ b/utils/thapi.c @@ -0,0 +1,11 @@ +#include "thapi_tracepoints.h" + +void thapi_stop() __attribute__((constructor)); + +void thapi_start(void) { + tracepoint(lttng_ust_toggle, start); +} + +void thapi_stop(void) { + tracepoint(lttng_ust_toggle, stop); +} diff --git a/utils/thapi.h b/utils/thapi.h new file mode 100644 index 000000000..5436c1133 --- /dev/null +++ b/utils/thapi.h @@ -0,0 +1,7 @@ +#if !defined(THAPI) +#define THAPI + +void thapi_start(); +void thapi_stop(); + +#endif // THAPI diff --git a/utils/thapi_profiler.c b/utils/thapi_profiler.c deleted file mode 100644 index aa8c21ac1..000000000 --- a/utils/thapi_profiler.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "thapi_profiler_tracepoints.h" - -void thapi_profiler_stop() __attribute__((constructor)); - -void thapi_profiler_start(void) { - tracepoint(lttng_ust_profiler, start); -} - -void thapi_profiler_stop(void) { - tracepoint(lttng_ust_profiler, stop); -} diff --git a/utils/thapi_profiler.h b/utils/thapi_profiler.h deleted file mode 100644 index 664ed7bab..000000000 --- a/utils/thapi_profiler.h +++ /dev/null @@ -1,7 +0,0 @@ -#if !defined(THAPI_PROFILER) -#define THAPI_PROFILER - -void thapi_profiler_start(); -void thapi_profiler_stop(); - -#endif // THAPI_PROFILER diff --git a/utils/thapi_profiler_tracepoints.tp b/utils/thapi_tracepoints.tp similarity index 71% rename from utils/thapi_profiler_tracepoints.tp rename to utils/thapi_tracepoints.tp index 5d74b2be4..83fc7fac0 100644 --- a/utils/thapi_profiler_tracepoints.tp +++ b/utils/thapi_tracepoints.tp @@ -1,12 +1,12 @@ TRACEPOINT_EVENT( - lttng_ust_profiler, + lttng_ust_toggle, start, TP_ARGS(), TP_FIELDS() ) TRACEPOINT_EVENT( - lttng_ust_profiler, + lttng_ust_toggle, stop, TP_ARGS(), TP_FIELDS() From 86a5dbc0df421d9380bef79a5e30d5606d45791c Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 02:14:25 +0000 Subject: [PATCH 07/57] Use AX_GCC_FUNC_ATTRIBUTE to check constructor support --- configure.ac | 17 +-- m4/ax_gcc_func_attribute.m4 | 242 ++++++++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+), 16 deletions(-) create mode 100644 m4/ax_gcc_func_attribute.m4 diff --git a/configure.ac b/configure.ac index 21f02f4ee..5ac6d526f 100644 --- a/configure.ac +++ b/configure.ac @@ -161,22 +161,7 @@ AC_FUNC_MMAP AC_FUNC_REALLOC AC_CHECK_FUNCS([clock_gettime ftruncate memmove memset strdup strstr strtoull strlen strchr]) -# Check if __attribute__((constructor)) works. Source: -# https://github.com/openucx/ucx/blob/72ae40c607067b6dfadf0d208f6811171bbf36b6/config/m4/ucs.m4#L140 -AC_DEFUN([CHECK_CROSS_COMP], [ - AC_RUN_IFELSE([$1], [$2], [$3], - [AC_LINK_IFELSE([$1], [$2], [$3])]) -]) - -AC_MSG_CHECKING([__attribute__((constructor))]) -CHECK_CROSS_COMP([AC_LANG_SOURCE([static int rc = 1; - static void constructor_test() __attribute__((constructor)); - static void constructor_test() { rc = 0; } - int main(int argc, char** argv) { return rc; }])], - [AC_MSG_RESULT([yes])], - [AC_MSG_ERROR([Cannot continue. Please use compiler that - supports __attribute__((constructor))])] - ) +AX_GCC_FUNC_ATTRIBUTE(constructor) # Required for configuring thapi.pc.in PKG_PROG_PKG_CONFIG diff --git a/m4/ax_gcc_func_attribute.m4 b/m4/ax_gcc_func_attribute.m4 new file mode 100644 index 000000000..fa4e089d6 --- /dev/null +++ b/m4/ax_gcc_func_attribute.m4 @@ -0,0 +1,242 @@ +# =========================================================================== +# https://www.gnu.org/software/autoconf-archive/ax_gcc_func_attribute.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_GCC_FUNC_ATTRIBUTE(ATTRIBUTE) +# +# DESCRIPTION +# +# This macro checks if the compiler supports one of GCC's function +# attributes; many other compilers also provide function attributes with +# the same syntax. Compiler warnings are used to detect supported +# attributes as unsupported ones are ignored by default so quieting +# warnings when using this macro will yield false positives. +# +# The ATTRIBUTE parameter holds the name of the attribute to be checked. +# +# If ATTRIBUTE is supported define HAVE_FUNC_ATTRIBUTE_. +# +# The macro caches its result in the ax_cv_have_func_attribute_ +# variable. +# +# The macro currently supports the following function attributes: +# +# alias +# aligned +# alloc_size +# always_inline +# artificial +# cold +# const +# constructor +# constructor_priority for constructor attribute with priority +# deprecated +# destructor +# dllexport +# dllimport +# error +# externally_visible +# fallthrough +# flatten +# format +# format_arg +# gnu_format +# gnu_inline +# hot +# ifunc +# leaf +# malloc +# noclone +# noinline +# nonnull +# noreturn +# nothrow +# optimize +# pure +# sentinel +# sentinel_position +# unused +# used +# visibility +# warning +# warn_unused_result +# weak +# weakref +# +# Unsupported function attributes will be tested with a prototype +# returning an int and not accepting any arguments and the result of the +# check might be wrong or meaningless so use with care. +# +# LICENSE +# +# Copyright (c) 2013 Gabriele Svelto +# +# Copying and distribution of this file, with or without modification, are +# permitted in any medium without royalty provided the copyright notice +# and this notice are preserved. This file is offered as-is, without any +# warranty. + +#serial 13 + +AC_DEFUN([AX_GCC_FUNC_ATTRIBUTE], [ + AS_VAR_PUSHDEF([ac_var], [ax_cv_have_func_attribute_$1]) + + AC_CACHE_CHECK([for __attribute__(($1))], [ac_var], [ + AC_LINK_IFELSE([AC_LANG_PROGRAM([ + m4_case([$1], + [alias], [ + int foo( void ) { return 0; } + int bar( void ) __attribute__(($1("foo"))); + ], + [aligned], [ + int foo( void ) __attribute__(($1(32))); + ], + [alloc_size], [ + void *foo(int a) __attribute__(($1(1))); + ], + [always_inline], [ + inline __attribute__(($1)) int foo( void ) { return 0; } + ], + [artificial], [ + inline __attribute__(($1)) int foo( void ) { return 0; } + ], + [cold], [ + int foo( void ) __attribute__(($1)); + ], + [const], [ + int foo( void ) __attribute__(($1)); + ], + [constructor_priority], [ + int foo( void ) __attribute__((__constructor__(65535/2))); + ], + [constructor], [ + int foo( void ) __attribute__(($1)); + ], + [deprecated], [ + int foo( void ) __attribute__(($1(""))); + ], + [destructor], [ + int foo( void ) __attribute__(($1)); + ], + [dllexport], [ + __attribute__(($1)) int foo( void ) { return 0; } + ], + [dllimport], [ + int foo( void ) __attribute__(($1)); + ], + [error], [ + int foo( void ) __attribute__(($1(""))); + ], + [externally_visible], [ + int foo( void ) __attribute__(($1)); + ], + [fallthrough], [ + void foo( int x ) {switch (x) { case 1: __attribute__(($1)); case 2: break ; }}; + ], + [flatten], [ + int foo( void ) __attribute__(($1)); + ], + [format], [ + int foo(const char *p, ...) __attribute__(($1(printf, 1, 2))); + ], + [gnu_format], [ + int foo(const char *p, ...) __attribute__((format(gnu_printf, 1, 2))); + ], + [format_arg], [ + char *foo(const char *p) __attribute__(($1(1))); + ], + [gnu_inline], [ + inline __attribute__(($1)) int foo( void ) { return 0; } + ], + [hot], [ + int foo( void ) __attribute__(($1)); + ], + [ifunc], [ + int my_foo( void ) { return 0; } + static int (*resolve_foo(void))(void) { return my_foo; } + int foo( void ) __attribute__(($1("resolve_foo"))); + ], + [leaf], [ + __attribute__(($1)) int foo( void ) { return 0; } + ], + [malloc], [ + void *foo( void ) __attribute__(($1)); + ], + [noclone], [ + int foo( void ) __attribute__(($1)); + ], + [noinline], [ + __attribute__(($1)) int foo( void ) { return 0; } + ], + [nonnull], [ + int foo(char *p) __attribute__(($1(1))); + ], + [noreturn], [ + void foo( void ) __attribute__(($1)); + ], + [nothrow], [ + int foo( void ) __attribute__(($1)); + ], + [optimize], [ + __attribute__(($1(3))) int foo( void ) { return 0; } + ], + [pure], [ + int foo( void ) __attribute__(($1)); + ], + [sentinel], [ + int foo(void *p, ...) __attribute__(($1)); + ], + [sentinel_position], [ + int foo(void *p, ...) __attribute__(($1(1))); + ], + [returns_nonnull], [ + void *foo( void ) __attribute__(($1)); + ], + [unused], [ + int foo( void ) __attribute__(($1)); + ], + [used], [ + int foo( void ) __attribute__(($1)); + ], + [visibility], [ + int foo_def( void ) __attribute__(($1("default"))); + int foo_hid( void ) __attribute__(($1("hidden"))); + int foo_int( void ) __attribute__(($1("internal"))); + int foo_pro( void ) __attribute__(($1("protected"))); + ], + [warning], [ + int foo( void ) __attribute__(($1(""))); + ], + [warn_unused_result], [ + int foo( void ) __attribute__(($1)); + ], + [weak], [ + int foo( void ) __attribute__(($1)); + ], + [weakref], [ + static int foo( void ) { return 0; } + static int bar( void ) __attribute__(($1("foo"))); + ], + [ + m4_warn([syntax], [Unsupported attribute $1, the test may fail]) + int foo( void ) __attribute__(($1)); + ] + )], []) + ], + dnl GCC doesn't exit with an error if an unknown attribute is + dnl provided but only outputs a warning, so accept the attribute + dnl only if no warning were issued. + [AS_IF([grep -- -Wattributes conftest.err], + [AS_VAR_SET([ac_var], [no])], + [AS_VAR_SET([ac_var], [yes])])], + [AS_VAR_SET([ac_var], [no])]) + ]) + + AS_IF([test yes = AS_VAR_GET([ac_var])], + [AC_DEFINE_UNQUOTED(AS_TR_CPP(HAVE_FUNC_ATTRIBUTE_$1), 1, + [Define to 1 if the system has the `$1' function attribute])], []) + + AS_VAR_POPDEF([ac_var]) +]) From 0dc0809dc796ca7b79f0bc9b82605d7f8ba0c476 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 02:43:35 +0000 Subject: [PATCH 08/57] Install libThapi.so in lib instead of lib/thapi --- utils/Makefile.am | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/utils/Makefile.am b/utils/Makefile.am index 01927c490..59aa49096 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -46,9 +46,6 @@ bin_PROGRAMS += thapi_metadata include_HEADERS = thapi.h -Thapidir = $(pkglibdir) -Thapi_LTLIBRARIES = libThapi.la - BUILT_SOURCES += \ thapi_tracepoints.h \ thapi_tracepoints.c @@ -58,10 +55,11 @@ nodist_libThapi_la_SOURCES = \ thapi_tracepoints.c libThapi_la_SOURCES = thapi.c - libThapi_la_CFLAGS = $(LTTNG_FLAGS) $(LTTNG_UST_CFLAGS) libThapi_la_LDFLAGS = $(LTTNG_UST_LIBS) +lib_LTLIBRARIES = libThapi.la + bin_SCRIPTS = \ babeltrace_thapi From 35b1f008fa21722b6c241e2ac5298abd3ef8ab8e Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 15:24:03 +0000 Subject: [PATCH 09/57] Add an integration test --- integration_tests/general.bats | 12 ++++++++++++ integration_tests/thapi_start_stop.c | 6 ++++++ 2 files changed, 18 insertions(+) create mode 100644 integration_tests/thapi_start_stop.c diff --git a/integration_tests/general.bats b/integration_tests/general.bats index fb19e8ff5..cbe208198 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -125,3 +125,15 @@ bats_require_minimum_version 1.5.0 cmp out_a.pftrace out_b.pftrace rm -f out_a.pftrace out_b.pftrace } + +@test "thapi_start_stop" { + cc -I${THAPI_INC_DIR} ./integration_tests/thapi_start_stop.c -o thapi_start_stop \ + -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi + $IPROF --no-analysis -- ./thapi_start_stop + + start_count=`babeltrace2 $THAPI_HOME/thapi-traces | grep lttng_ust_toggle:start | wc -l` + [ "$start_count" -eq 1 ] + + stop_count=`babeltrace2 $THAPI_HOME/thapi-traces | grep lttng_ust_toggle:stop | wc -l` + [ "$stop_count" -eq 2 ] +} diff --git a/integration_tests/thapi_start_stop.c b/integration_tests/thapi_start_stop.c new file mode 100644 index 000000000..4a366e57c --- /dev/null +++ b/integration_tests/thapi_start_stop.c @@ -0,0 +1,6 @@ +#include "thapi.h" + +int main(int argc, char *argv[]) { + thapi_start(); + thapi_stop(); +} From 4523728db7a2b9350657ad2e07a37ee28e20c8d0 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 17:27:53 +0000 Subject: [PATCH 10/57] Minor changes in utils/thapi.[ch] --- utils/thapi.c | 3 +-- utils/thapi.h | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/utils/thapi.c b/utils/thapi.c index ffb6ce3bc..0c3f877da 100644 --- a/utils/thapi.c +++ b/utils/thapi.c @@ -1,6 +1,5 @@ #include "thapi_tracepoints.h" - -void thapi_stop() __attribute__((constructor)); +#include "thapi.h" void thapi_start(void) { tracepoint(lttng_ust_toggle, start); diff --git a/utils/thapi.h b/utils/thapi.h index 5436c1133..d81d21ec1 100644 --- a/utils/thapi.h +++ b/utils/thapi.h @@ -1,7 +1,7 @@ #if !defined(THAPI) #define THAPI -void thapi_start(); -void thapi_stop(); +void thapi_start(void); +void thapi_stop(void) __attribute__((constructor)); #endif // THAPI From 1f42a195fd0e40a3bbdfe4e0bc0a91fa5c4d4526 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 02:56:32 +0000 Subject: [PATCH 11/57] Fix the failing integration test --- integration_tests/general.bats | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/integration_tests/general.bats b/integration_tests/general.bats index cbe208198..f7f22e016 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -129,11 +129,11 @@ bats_require_minimum_version 1.5.0 @test "thapi_start_stop" { cc -I${THAPI_INC_DIR} ./integration_tests/thapi_start_stop.c -o thapi_start_stop \ -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi - $IPROF --no-analysis -- ./thapi_start_stop + $IPROF --trace-output trace_toggle --no-analysis -- ./thapi_start_stop - start_count=`babeltrace2 $THAPI_HOME/thapi-traces | grep lttng_ust_toggle:start | wc -l` + start_count=`babeltrace2 trace_toggle | grep lttng_ust_toggle:start | wc -l` [ "$start_count" -eq 1 ] - stop_count=`babeltrace2 $THAPI_HOME/thapi-traces | grep lttng_ust_toggle:stop | wc -l` + stop_count=`babeltrace2 trace_toggle | grep lttng_ust_toggle:stop | wc -l` [ "$stop_count" -eq 2 ] } From 7416b92d9ed4c02825cb4f0090a9fc3bcb4712f4 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 14:24:33 +0000 Subject: [PATCH 12/57] thapi.c->thapi_toggle.c & use toggle over profiler --- utils/Makefile.am | 2 +- utils/{thapi.c => thapi_toggle.c} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename utils/{thapi.c => thapi_toggle.c} (100%) diff --git a/utils/Makefile.am b/utils/Makefile.am index 59aa49096..5d3afa12c 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -54,7 +54,7 @@ nodist_libThapi_la_SOURCES = \ thapi_tracepoints.h \ thapi_tracepoints.c -libThapi_la_SOURCES = thapi.c +libThapi_la_SOURCES = thapi_toggle.c libThapi_la_CFLAGS = $(LTTNG_FLAGS) $(LTTNG_UST_CFLAGS) libThapi_la_LDFLAGS = $(LTTNG_UST_LIBS) diff --git a/utils/thapi.c b/utils/thapi_toggle.c similarity index 100% rename from utils/thapi.c rename to utils/thapi_toggle.c From a608afa4d977b809c8cc1490bb564cbb428ac44a Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 14:38:51 +0000 Subject: [PATCH 13/57] Use constructor priority for thapi_stop() --- utils/thapi.h | 2 +- utils/thapi_toggle.c | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/utils/thapi.h b/utils/thapi.h index d81d21ec1..bd0f9af16 100644 --- a/utils/thapi.h +++ b/utils/thapi.h @@ -2,6 +2,6 @@ #define THAPI void thapi_start(void); -void thapi_stop(void) __attribute__((constructor)); +void thapi_stop(void); #endif // THAPI diff --git a/utils/thapi_toggle.c b/utils/thapi_toggle.c index 0c3f877da..0e363c2f6 100644 --- a/utils/thapi_toggle.c +++ b/utils/thapi_toggle.c @@ -1,10 +1,14 @@ #include "thapi_tracepoints.h" #include "thapi.h" +#ifndef LTTNG_UST_CONSTRUCTOR_PRIO +#error "LTTNG_UST_CONSTRUCTOR_PRIO not defined." +#endif + void thapi_start(void) { tracepoint(lttng_ust_toggle, start); } -void thapi_stop(void) { +void __attribute__((constructor(LTTNG_UST_CONSTRUCTOR_PRIO + 1))) thapi_stop(void) { tracepoint(lttng_ust_toggle, stop); } From 526e84a4606c46d68621b6b9b862d51185632109 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 14:46:40 +0000 Subject: [PATCH 14/57] Set minimum lttng-ust version We need one which supports __attribute__((constructor)) in user code. --- configure.ac | 2 ++ 1 file changed, 2 insertions(+) diff --git a/configure.ac b/configure.ac index 5ac6d526f..cbfd35f68 100644 --- a/configure.ac +++ b/configure.ac @@ -125,6 +125,8 @@ AX_COMPARE_VERSION([$H2YAML_VERSION], [ge], [$H2YAML_MIN_VERSION], AC_MSG_ERROR([h2yaml version $H2YAML_VERSION is too old, need >= $H2YAML_MIN_VERSION])]) PKG_CHECK_MODULES([LIBFFI], [libffi >= 3.2]) +PKG_CHECK_MODULES([BABELTRACE2], [babeltrace2 >= 2.0]) +PKG_CHECK_MODULES([LTTNG_UST], [lttng-ust >= 2.12.8]) PKG_CHECK_MODULES([PROTOBUF], [protobuf >= 3.0]) # For blob support PKG_CHECK_MODULES([BABELTRACE2], [babeltrace2 >= 2.1]) From 6efd0fd700a97862c7d0ce389e52fb1585bea77d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 25 Mar 2025 22:38:13 +0000 Subject: [PATCH 15/57] Check if the header is included by a CXX compiler --- utils/thapi.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/utils/thapi.h b/utils/thapi.h index bd0f9af16..b94918a4c 100644 --- a/utils/thapi.h +++ b/utils/thapi.h @@ -1,7 +1,15 @@ #if !defined(THAPI) #define THAPI +#ifdef __cplusplus +extern "C" { +#endif + void thapi_start(void); void thapi_stop(void); +#ifdef __cplusplus +} +#endif + #endif // THAPI From cf146773af73ddcd67e0642439c647253b2d5863 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Apr 2025 04:32:55 +0000 Subject: [PATCH 16/57] Plugin to filter traces based on thapi_start/stop --- utils/Makefile.am | 33 +++++++++++++++++++++++++++------ utils/btx_thapi.yaml | 27 +++++++++++++++++++++++++++ utils/thapi_callbacks.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 utils/btx_thapi.yaml create mode 100644 utils/thapi_callbacks.c diff --git a/utils/Makefile.am b/utils/Makefile.am index 5d3afa12c..97814e0b1 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -46,19 +46,38 @@ bin_PROGRAMS += thapi_metadata include_HEADERS = thapi.h -BUILT_SOURCES += \ - thapi_tracepoints.h \ - thapi_tracepoints.c - +lib_LTLIBRARIES = libThapi.la nodist_libThapi_la_SOURCES = \ thapi_tracepoints.h \ thapi_tracepoints.c - libThapi_la_SOURCES = thapi_toggle.c libThapi_la_CFLAGS = $(LTTNG_FLAGS) $(LTTNG_UST_CFLAGS) libThapi_la_LDFLAGS = $(LTTNG_UST_LIBS) -lib_LTLIBRARIES = libThapi.la +BTX_THAPI_GENERATED = \ + btx_thapi/metababel/metababel.h \ + btx_thapi/metababel/btx_component.h \ + btx_thapi/metababel/btx_component.c \ + btx_thapi/metababel/btx_upstream.h \ + btx_thapi/metababel/btx_upstream.c \ + btx_thapi/metababel/btx_downstream.h \ + btx_thapi/metababel/btx_downstream.c \ + btx_thapi/btx_main.c + +$(BTX_THAPI_GENERATED): btx_thapi.yaml thapi_callbacks.c + $(METABABEL) --enable-callbacks on_downstream --component-type FILTER \ + --upstream $(top_srcdir)/utils/btx_thapi.yaml --downstream $(top_srcdir)/utils/btx_thapi.yaml \ + -o btx_thapi + +noinst_LTLIBRARIES = libThapiPlugin.la +nodist_libThapiPlugin_la_SOURCES = $(BTX_THAPI_GENERATED) +libThapiPlugin_la_SOURCES = thapi_callbacks.c +libThapiPlugin_la_CFLAGS = -fPIC -shared -Wall -Wextra -Wno-unused-parameter $(BABELTRACE2_CFLAGS) -I./btx_thapi -I$(top_srcdir)/utils/include + +BUILT_SOURCES += \ + thapi_tracepoints.h \ + thapi_tracepoints.c \ + $(BTX_THAPI_GENERATED) bin_SCRIPTS = \ babeltrace_thapi @@ -79,6 +98,7 @@ CLEANFILES = \ thapi_version \ optparse_thapi.rb \ lttng/tracepoint_gen.h \ + $(BTX_THAPI_GENERATED) \ $(BUILT_SOURCES) EXTRA_DIST = \ @@ -99,6 +119,7 @@ EXTRA_DIST = \ dump_trace_format.rb \ thapi_metadata_tracepoints.tp \ thapi_tracepoints.tp \ + btx_thapi.yaml \ command.rb \ command_index.rb \ meta_parameter_spec.rb \ diff --git a/utils/btx_thapi.yaml b/utils/btx_thapi.yaml new file mode 100644 index 000000000..bc908b991 --- /dev/null +++ b/utils/btx_thapi.yaml @@ -0,0 +1,27 @@ +:stream_classes: +- :name: thapi_toggle + :default_clock_class: {} + :packet_context_field_class: + :type: structure + :members: + - :name: cpu_id + :field_class: + :type: integer_unsigned + :cast_type: uint64_t + :field_value_range: 32 + :event_common_context_field_class: + :type: structure + :members: + - :name: vpid + :field_class: + :type: integer_signed + :field_value_range: 32 + :cast_type: int + - :name: vtid + :field_class: + :type: integer_signed + :field_value_range: 32 + :cast_type: int + :event_classes: + - :name: lttng_ust_toggle:start + - :name: lttng_ust_toggle:stop diff --git a/utils/thapi_callbacks.c b/utils/thapi_callbacks.c new file mode 100644 index 000000000..ea500dfc7 --- /dev/null +++ b/utils/thapi_callbacks.c @@ -0,0 +1,35 @@ +#include + +static void init(void **data) { *data = calloc(1, sizeof(int)); } + +static void finalize(void *data) { free(data); } + +static void thapi_start_callback(void *btx_handle, void *push, long int cpuid, + int vpid, int vtid) { + *((int *)push) = 1; +} + +static void thapi_stop_callback(void *btx_handle, void *push, long int cpuid, + int vpid, int vtid) { + *((int *)push) = 0; +} + +static void push_downstream(void *btx_handle, void *push, + const bt_message *msg) { + if (*((int *)push) == 1) + btx_push_message(btx_handle, msg); + else + bt_message_put_ref(msg); +} + +void btx_register_usr_callbacks(void *btx_handle) { + btx_register_callbacks_initialize_component(btx_handle, &init); + btx_register_callbacks_finalize_component(btx_handle, &finalize); + + btx_register_callbacks_lttng_ust_toggle_start(btx_handle, + &thapi_start_callback); + btx_register_callbacks_lttng_ust_toggle_stop(btx_handle, + &thapi_stop_callback); + + btx_register_on_downstream_message_callback(btx_handle, &push_downstream); +} From 86e0b9b8172f4adb7e64f03701c9e883323ae947 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Apr 2025 06:10:06 +0000 Subject: [PATCH 17/57] Format utils/thapi_toggle.c --- utils/thapi_toggle.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/utils/thapi_toggle.c b/utils/thapi_toggle.c index 0e363c2f6..65d812110 100644 --- a/utils/thapi_toggle.c +++ b/utils/thapi_toggle.c @@ -1,14 +1,13 @@ -#include "thapi_tracepoints.h" #include "thapi.h" +#include "thapi_tracepoints.h" #ifndef LTTNG_UST_CONSTRUCTOR_PRIO -#error "LTTNG_UST_CONSTRUCTOR_PRIO not defined." +#error "LTTNG_UST_CONSTRUCTOR_PRIO is not defined." #endif -void thapi_start(void) { - tracepoint(lttng_ust_toggle, start); -} +void thapi_start(void) { tracepoint(lttng_ust_toggle, start); } -void __attribute__((constructor(LTTNG_UST_CONSTRUCTOR_PRIO + 1))) thapi_stop(void) { +void __attribute__((constructor(LTTNG_UST_CONSTRUCTOR_PRIO + 1))) +thapi_stop(void) { tracepoint(lttng_ust_toggle, stop); } From f5f036c8dd3ddf69b900a1b4a07e22538117b54d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 24 Jun 2025 16:09:42 +0000 Subject: [PATCH 18/57] Prefix files related to toggle with `thapi_toggle` Also, minor refactoring in utils/Makefile.am. --- integration_tests/general.bats | 6 +-- .../{thapi_start_stop.c => thapi_toggle.c} | 2 +- utils/Makefile.am | 52 ++++++++++--------- .../{btx_thapi.yaml => btx_thapi_toggle.yaml} | 0 utils/thapi_toggle.c | 2 +- ...i_callbacks.c => thapi_toggle_callbacks.c} | 9 ++-- ...epoints.tp => thapi_toggle_tracepoints.tp} | 0 7 files changed, 35 insertions(+), 36 deletions(-) rename integration_tests/{thapi_start_stop.c => thapi_toggle.c} (78%) rename utils/{btx_thapi.yaml => btx_thapi_toggle.yaml} (100%) rename utils/{thapi_callbacks.c => thapi_toggle_callbacks.c} (79%) rename utils/{thapi_tracepoints.tp => thapi_toggle_tracepoints.tp} (100%) diff --git a/integration_tests/general.bats b/integration_tests/general.bats index f7f22e016..ea26188d4 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -126,10 +126,10 @@ bats_require_minimum_version 1.5.0 rm -f out_a.pftrace out_b.pftrace } -@test "thapi_start_stop" { - cc -I${THAPI_INC_DIR} ./integration_tests/thapi_start_stop.c -o thapi_start_stop \ +@test "thapi_toggle" { + cc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle.c -o thapi_toggle \ -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi - $IPROF --trace-output trace_toggle --no-analysis -- ./thapi_start_stop + $IPROF --trace-output trace_toggle --no-analysis -- ./thapi_toggle start_count=`babeltrace2 trace_toggle | grep lttng_ust_toggle:start | wc -l` [ "$start_count" -eq 1 ] diff --git a/integration_tests/thapi_start_stop.c b/integration_tests/thapi_toggle.c similarity index 78% rename from integration_tests/thapi_start_stop.c rename to integration_tests/thapi_toggle.c index 4a366e57c..bc1e1e900 100644 --- a/integration_tests/thapi_start_stop.c +++ b/integration_tests/thapi_toggle.c @@ -1,4 +1,4 @@ -#include "thapi.h" +#include int main(int argc, char *argv[]) { thapi_start(); diff --git a/utils/Makefile.am b/utils/Makefile.am index 97814e0b1..d9b456299 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -19,7 +19,8 @@ lttng/tracepoint_gen.h: $(srcdir)/tracepoint_gen.rb mkdir -p lttng $(RUBY) $< 25 > $@ -LTTNG_FLAGS=-fPIC -Wall -Wextra -Wno-unused-parameter -Wno-type-limits -Wno-sign-compare $(WERROR) -I$(top_srcdir)/utils -I$(top_srcdir)/utils/include -I./ +LTTNG_FLAGS= -fPIC -Wall -Wextra -Wno-unused-parameter -Wno-type-limits -Wno-sign-compare $(WERROR) \ + -I$(top_srcdir)/utils -I$(top_srcdir)/utils/include -I./ %.h %.c: %.tp lttng/tracepoint_gen.h $(LTTNG_GEN_TP) $< -o $*.c -o $*.h @@ -48,36 +49,37 @@ include_HEADERS = thapi.h lib_LTLIBRARIES = libThapi.la nodist_libThapi_la_SOURCES = \ - thapi_tracepoints.h \ - thapi_tracepoints.c + thapi_toggle_tracepoints.h \ + thapi_toggle_tracepoints.c libThapi_la_SOURCES = thapi_toggle.c libThapi_la_CFLAGS = $(LTTNG_FLAGS) $(LTTNG_UST_CFLAGS) libThapi_la_LDFLAGS = $(LTTNG_UST_LIBS) -BTX_THAPI_GENERATED = \ - btx_thapi/metababel/metababel.h \ - btx_thapi/metababel/btx_component.h \ - btx_thapi/metababel/btx_component.c \ - btx_thapi/metababel/btx_upstream.h \ - btx_thapi/metababel/btx_upstream.c \ - btx_thapi/metababel/btx_downstream.h \ - btx_thapi/metababel/btx_downstream.c \ - btx_thapi/btx_main.c - -$(BTX_THAPI_GENERATED): btx_thapi.yaml thapi_callbacks.c +BTX_THAPI_TOGGLE_GENERATED = \ + btx_thapi_toggle/metababel/metababel.h \ + btx_thapi_toggle/metababel/btx_component.h \ + btx_thapi_toggle/metababel/btx_component.c \ + btx_thapi_toggle/metababel/btx_upstream.h \ + btx_thapi_toggle/metababel/btx_upstream.c \ + btx_thapi_toggle/metababel/btx_downstream.h \ + btx_thapi_toggle/metababel/btx_downstream.c \ + btx_thapi_toggle/btx_main.c + +$(BTX_THAPI_TOGGLE_GENERATED): btx_thapi_toggle.yaml thapi_toggle_callbacks.c $(METABABEL) --enable-callbacks on_downstream --component-type FILTER \ - --upstream $(top_srcdir)/utils/btx_thapi.yaml --downstream $(top_srcdir)/utils/btx_thapi.yaml \ - -o btx_thapi + --upstream $(top_srcdir)/utils/btx_thapi_toggle.yaml --downstream $(top_srcdir)/utils/btx_thapi_toggle.yaml \ + -o btx_thapi_toggle noinst_LTLIBRARIES = libThapiPlugin.la -nodist_libThapiPlugin_la_SOURCES = $(BTX_THAPI_GENERATED) -libThapiPlugin_la_SOURCES = thapi_callbacks.c -libThapiPlugin_la_CFLAGS = -fPIC -shared -Wall -Wextra -Wno-unused-parameter $(BABELTRACE2_CFLAGS) -I./btx_thapi -I$(top_srcdir)/utils/include +nodist_libThapiPlugin_la_SOURCES = $(BTX_THAPI_TOGGLE_GENERATED) +libThapiPlugin_la_SOURCES = thapi_toggle_callbacks.c +libThapiPlugin_la_CFLAGS = -fPIC -shared -Wall -Wextra -Wno-unused-parameter $(BABELTRACE2_CFLAGS) \ + -I./btx_thapi_toggle -I$(top_srcdir)/utils/include BUILT_SOURCES += \ - thapi_tracepoints.h \ - thapi_tracepoints.c \ - $(BTX_THAPI_GENERATED) + thapi_toggle_tracepoints.h \ + thapi_toggle_tracepoints.c \ + $(BTX_THAPI_TOGGLE_GENERATED) bin_SCRIPTS = \ babeltrace_thapi @@ -98,7 +100,7 @@ CLEANFILES = \ thapi_version \ optparse_thapi.rb \ lttng/tracepoint_gen.h \ - $(BTX_THAPI_GENERATED) \ + $(BTX_THAPI_TOGGLE_GENERATED) \ $(BUILT_SOURCES) EXTRA_DIST = \ @@ -118,8 +120,8 @@ EXTRA_DIST = \ type_registry.rb \ dump_trace_format.rb \ thapi_metadata_tracepoints.tp \ - thapi_tracepoints.tp \ - btx_thapi.yaml \ + thapi_toggle_tracepoints.tp \ + btx_thapi_toggle.yaml \ command.rb \ command_index.rb \ meta_parameter_spec.rb \ diff --git a/utils/btx_thapi.yaml b/utils/btx_thapi_toggle.yaml similarity index 100% rename from utils/btx_thapi.yaml rename to utils/btx_thapi_toggle.yaml diff --git a/utils/thapi_toggle.c b/utils/thapi_toggle.c index 65d812110..0fedca261 100644 --- a/utils/thapi_toggle.c +++ b/utils/thapi_toggle.c @@ -1,5 +1,5 @@ #include "thapi.h" -#include "thapi_tracepoints.h" +#include "thapi_toggle_tracepoints.h" #ifndef LTTNG_UST_CONSTRUCTOR_PRIO #error "LTTNG_UST_CONSTRUCTOR_PRIO is not defined." diff --git a/utils/thapi_callbacks.c b/utils/thapi_toggle_callbacks.c similarity index 79% rename from utils/thapi_callbacks.c rename to utils/thapi_toggle_callbacks.c index ea500dfc7..4c1145c0c 100644 --- a/utils/thapi_callbacks.c +++ b/utils/thapi_toggle_callbacks.c @@ -4,18 +4,15 @@ static void init(void **data) { *data = calloc(1, sizeof(int)); } static void finalize(void *data) { free(data); } -static void thapi_start_callback(void *btx_handle, void *push, long int cpuid, - int vpid, int vtid) { +static void thapi_start_callback(void *btx_handle, void *push, long int cpuid, int vpid, int vtid) { *((int *)push) = 1; } -static void thapi_stop_callback(void *btx_handle, void *push, long int cpuid, - int vpid, int vtid) { +static void thapi_stop_callback(void *btx_handle, void *push, long int cpuid, int vpid, int vtid) { *((int *)push) = 0; } -static void push_downstream(void *btx_handle, void *push, - const bt_message *msg) { +static void push_downstream(void *btx_handle, void *push, const bt_message *msg) { if (*((int *)push) == 1) btx_push_message(btx_handle, msg); else diff --git a/utils/thapi_tracepoints.tp b/utils/thapi_toggle_tracepoints.tp similarity index 100% rename from utils/thapi_tracepoints.tp rename to utils/thapi_toggle_tracepoints.tp From c61122ed5875aab6743d98650e6126533e3fa36b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 24 Jun 2025 16:10:12 +0000 Subject: [PATCH 19/57] Check hostname and vpid when using toggle --- utils/Makefile.am | 8 ++-- utils/btx_thapi_toggle.yaml | 15 ++++---- utils/thapi_toggle_callbacks.c | 32 ---------------- utils/thapi_toggle_callbacks.cpp | 65 ++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 42 deletions(-) delete mode 100644 utils/thapi_toggle_callbacks.c create mode 100644 utils/thapi_toggle_callbacks.cpp diff --git a/utils/Makefile.am b/utils/Makefile.am index d9b456299..8198fb067 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -65,16 +65,18 @@ BTX_THAPI_TOGGLE_GENERATED = \ btx_thapi_toggle/metababel/btx_downstream.c \ btx_thapi_toggle/btx_main.c -$(BTX_THAPI_TOGGLE_GENERATED): btx_thapi_toggle.yaml thapi_toggle_callbacks.c +$(BTX_THAPI_TOGGLE_GENERATED): $(srcdir)/btx_thapi_toggle.yaml $(srcdir)/thapi_toggle_callbacks.cpp $(METABABEL) --enable-callbacks on_downstream --component-type FILTER \ - --upstream $(top_srcdir)/utils/btx_thapi_toggle.yaml --downstream $(top_srcdir)/utils/btx_thapi_toggle.yaml \ + --upstream $(srcdir)/btx_thapi_toggle.yaml --downstream $(srcdir)/btx_thapi_toggle.yaml \ -o btx_thapi_toggle noinst_LTLIBRARIES = libThapiPlugin.la nodist_libThapiPlugin_la_SOURCES = $(BTX_THAPI_TOGGLE_GENERATED) -libThapiPlugin_la_SOURCES = thapi_toggle_callbacks.c +libThapiPlugin_la_SOURCES = thapi_toggle_callbacks.cpp libThapiPlugin_la_CFLAGS = -fPIC -shared -Wall -Wextra -Wno-unused-parameter $(BABELTRACE2_CFLAGS) \ -I./btx_thapi_toggle -I$(top_srcdir)/utils/include +libThapiPlugin_la_CXXFLAGS = -fPIC -shared -Wall -Wextra -Wno-unused-parameter $(BABELTRACE2_CFLAGS) \ + -I./btx_thapi_toggle -I$(top_srcdir)/utils/include BUILT_SOURCES += \ thapi_toggle_tracepoints.h \ diff --git a/utils/btx_thapi_toggle.yaml b/utils/btx_thapi_toggle.yaml index bc908b991..53266a1c0 100644 --- a/utils/btx_thapi_toggle.yaml +++ b/utils/btx_thapi_toggle.yaml @@ -1,3 +1,7 @@ +:environment: + :entries: + - :name: hostname + :type: string :stream_classes: - :name: thapi_toggle :default_clock_class: {} @@ -6,22 +10,19 @@ :members: - :name: cpu_id :field_class: - :type: integer_unsigned - :cast_type: uint64_t - :field_value_range: 32 + :type: integer_signed + :cast_type: int64_t :event_common_context_field_class: :type: structure :members: - :name: vpid :field_class: :type: integer_signed - :field_value_range: 32 - :cast_type: int + :cast_type: int64_t - :name: vtid :field_class: :type: integer_signed - :field_value_range: 32 - :cast_type: int + :cast_type: int64_t :event_classes: - :name: lttng_ust_toggle:start - :name: lttng_ust_toggle:stop diff --git a/utils/thapi_toggle_callbacks.c b/utils/thapi_toggle_callbacks.c deleted file mode 100644 index 4c1145c0c..000000000 --- a/utils/thapi_toggle_callbacks.c +++ /dev/null @@ -1,32 +0,0 @@ -#include - -static void init(void **data) { *data = calloc(1, sizeof(int)); } - -static void finalize(void *data) { free(data); } - -static void thapi_start_callback(void *btx_handle, void *push, long int cpuid, int vpid, int vtid) { - *((int *)push) = 1; -} - -static void thapi_stop_callback(void *btx_handle, void *push, long int cpuid, int vpid, int vtid) { - *((int *)push) = 0; -} - -static void push_downstream(void *btx_handle, void *push, const bt_message *msg) { - if (*((int *)push) == 1) - btx_push_message(btx_handle, msg); - else - bt_message_put_ref(msg); -} - -void btx_register_usr_callbacks(void *btx_handle) { - btx_register_callbacks_initialize_component(btx_handle, &init); - btx_register_callbacks_finalize_component(btx_handle, &finalize); - - btx_register_callbacks_lttng_ust_toggle_start(btx_handle, - &thapi_start_callback); - btx_register_callbacks_lttng_ust_toggle_stop(btx_handle, - &thapi_stop_callback); - - btx_register_on_downstream_message_callback(btx_handle, &push_downstream); -} diff --git a/utils/thapi_toggle_callbacks.cpp b/utils/thapi_toggle_callbacks.cpp new file mode 100644 index 000000000..00e6c183d --- /dev/null +++ b/utils/thapi_toggle_callbacks.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +#include +#include + +#include + +using ToggleKey = std::tuple; +using ToggleMap = std::map; + +static char hostname_s[HOST_NAME_MAX + 1]; + +static void init(void **data) { *data = new ToggleMap; } + +static void finalize(void *data) { delete static_cast(data); } + +static void thapi_start_callback(void *btx_handle, void *tmap, int64_t cpuid, const char *hostname, + int64_t vpid, int64_t vtid) { + auto map = static_cast(tmap); + auto key = ToggleKey{std::string(hostname), vpid}; + (*map)[key] = true; + strncpy(hostname_s, hostname, HOST_NAME_MAX); +} + +static void thapi_stop_callback(void *btx_handle, void *tmap, int64_t cpuid, const char *hostname, + int64_t vpid, int64_t vtid) { + auto map = static_cast(tmap); + auto key = ToggleKey{std::string(hostname), vpid}; + (*map)[key] = false; +} + +static void push_downstream(void *btx_handle, void *tmap, const bt_message *msg) { + bool push_msg = true; + + if (bt_message_get_type(msg) == BT_MESSAGE_TYPE_EVENT) { + const bt_event *event = bt_message_event_borrow_event_const(msg); + const bt_field *ccf = bt_event_borrow_common_context_field_const(event); + const bt_field *vpid = bt_field_structure_borrow_member_field_by_name_const(ccf, "vpid"); + uint64_t vpid_v = bt_field_integer_signed_get_value(vpid); + + auto map = static_cast(tmap); + auto key = ToggleKey{std::string(hostname_s), vpid_v}; + push_msg = (*map)[key]; + } + + if (push_msg) { + btx_push_message(btx_handle, msg); + } else { + bt_message_put_ref(msg); + } +} + +void btx_register_usr_callbacks(void *btx_handle) { + btx_register_callbacks_initialize_component(btx_handle, &init); + btx_register_callbacks_lttng_ust_toggle_start(btx_handle, + &thapi_start_callback); + btx_register_callbacks_lttng_ust_toggle_stop(btx_handle, + &thapi_stop_callback); + btx_register_callbacks_finalize_component(btx_handle, &finalize); + + btx_register_on_downstream_message_callback(btx_handle, &push_downstream); +} From fe26e9c5bf7aafc3ec2c57dc2e63a4f6e510941f Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 1 Jul 2025 19:31:49 +0000 Subject: [PATCH 20/57] Install ThapiToggle plugin --- utils/Makefile.am | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/utils/Makefile.am b/utils/Makefile.am index 8198fb067..5584aa7da 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -65,17 +65,17 @@ BTX_THAPI_TOGGLE_GENERATED = \ btx_thapi_toggle/metababel/btx_downstream.c \ btx_thapi_toggle/btx_main.c -$(BTX_THAPI_TOGGLE_GENERATED): $(srcdir)/btx_thapi_toggle.yaml $(srcdir)/thapi_toggle_callbacks.cpp +$(BTX_THAPI_TOGGLE_GENERATED): $(srcdir)/btx_thapi_toggle.yaml $(METABABEL) --enable-callbacks on_downstream --component-type FILTER \ --upstream $(srcdir)/btx_thapi_toggle.yaml --downstream $(srcdir)/btx_thapi_toggle.yaml \ -o btx_thapi_toggle -noinst_LTLIBRARIES = libThapiPlugin.la -nodist_libThapiPlugin_la_SOURCES = $(BTX_THAPI_TOGGLE_GENERATED) -libThapiPlugin_la_SOURCES = thapi_toggle_callbacks.cpp -libThapiPlugin_la_CFLAGS = -fPIC -shared -Wall -Wextra -Wno-unused-parameter $(BABELTRACE2_CFLAGS) \ +lib_LTLIBRARIES += libThapiToggle.la +nodist_libThapiToggle_la_SOURCES = $(BTX_THAPI_TOGGLE_GENERATED) +libThapiToggle_la_SOURCES = thapi_toggle_callbacks.cpp +libThapiToggle_la_CFLAGS = -fPIC -shared -Wall -Wextra -Wno-unused-parameter $(BABELTRACE2_CFLAGS) \ -I./btx_thapi_toggle -I$(top_srcdir)/utils/include -libThapiPlugin_la_CXXFLAGS = -fPIC -shared -Wall -Wextra -Wno-unused-parameter $(BABELTRACE2_CFLAGS) \ +libThapiToggle_la_CXXFLAGS = -fPIC -shared -Wall -Wextra -Wno-unused-parameter $(BABELTRACE2_CFLAGS) \ -I./btx_thapi_toggle -I$(top_srcdir)/utils/include BUILT_SOURCES += \ From 279f74a2c429a3dfce047e165db39d214849510d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 1 Jul 2025 16:36:56 +0000 Subject: [PATCH 21/57] Add integration tests for ThapiToggle --- integration_tests/general.bats | 30 ++++++++++++++++++++++++++++ integration_tests/thapi_toggle_mpi.c | 30 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 integration_tests/thapi_toggle_mpi.c diff --git a/integration_tests/general.bats b/integration_tests/general.bats index ea26188d4..d76a7e7ec 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -137,3 +137,33 @@ bats_require_minimum_version 1.5.0 stop_count=`babeltrace2 trace_toggle | grep lttng_ust_toggle:stop | wc -l` [ "$stop_count" -eq 2 ] } + +toggle_count_traces() { + trace_metadata_file=`find toggle_traces -iname metadata` + trace_metadata_dir=$(dirname "${trace_metadata_file}") + + traces=$(babeltrace2 --plugin-path=${THAPI_LIB_DIR} \ + --component source:source.ctf.fs --params "inputs=[\"${trace_metadata_dir}\"]" \ + --component=filter:filter.metababel_filter.btx \ + --component=sink:sink.text.pretty) + rm -rf toggle_traces + + echo $traces | sed -e "s/ \[/@[/g" | sed "s/@/\n/g" | grep . | wc -l +} + +@test "toggle_plugin_mpi_np_1" { + mpicc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle_mpi.c -o thapi_toggle_mpi \ + -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi + + THAPI_SYNC_DAEMON=fs THAPI_JOBID=0 timeout 40s $MPIRUN -n 1 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi 0 + count_0=$(toggle_count_traces) + + THAPI_SYNC_DAEMON=fs THAPI_JOBID=0 timeout 40s $MPIRUN -n 1 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi 1 + count_1=$(toggle_count_traces) + + THAPI_SYNC_DAEMON=fs THAPI_JOBID=0 timeout 40s $MPIRUN -n 1 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi 2 + count_2=$(toggle_count_traces) + + [ "$count_2" -eq 0 ] + [ "$count_0" -gt "$count_1" ] +} diff --git a/integration_tests/thapi_toggle_mpi.c b/integration_tests/thapi_toggle_mpi.c new file mode 100644 index 000000000..d98fc77cf --- /dev/null +++ b/integration_tests/thapi_toggle_mpi.c @@ -0,0 +1,30 @@ +#include +#include + +#include + +int main(int argc, char *argv[]) { + int variant = (argc > 1) ? atoi(argv[1]) : 0; + + MPI_Init(&argc, &argv); + + int rank, size; + + switch (variant) { + case 0: + thapi_start(); + case 1: + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + if (rank == 0) thapi_start(); + MPI_Comm_size(MPI_COMM_WORLD, &size); + break; + default: + break; + } + + thapi_stop(); + + MPI_Finalize(); + + return 0; +} From 4525221ef11f8c81e13058a83675c2993bac7a19 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Jul 2025 04:25:18 +0000 Subject: [PATCH 22/57] Add integration tests with np=2 for ThapiToggle --- integration_tests/general.bats | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/integration_tests/general.bats b/integration_tests/general.bats index d76a7e7ec..c32f4071d 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -167,3 +167,23 @@ toggle_count_traces() { [ "$count_2" -eq 0 ] [ "$count_0" -gt "$count_1" ] } + +toggle_count_vpids() { + vpids=$(babeltrace2 toggle_traces | sed -e "s/, { vpid = /\nvpid,/g" | grep vpid | awk '{ split($0,a,","); print a[2] }' | sort | uniq | wc -l) + rm -rf toggle_traces + echo $vpids +} + +@test "toggle_plugin_mpi_np_2" { + mpicc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle_mpi.c -o thapi_toggle_mpi \ + -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi + + THAPI_SYNC_DAEMON=fs THAPI_JOBID=0 timeout 40s $MPIRUN -n 2 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi 0 + count_0=$(toggle_count_vpids) + + THAPI_SYNC_DAEMON=fs THAPI_JOBID=0 timeout 40s $MPIRUN -n 2 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi 1 + count_1=$(toggle_count_vpids) + + [ "$count_0" -eq 2 ] + [ "$count_1" -eq 1 ] +} From db2d9eceddbf035b885846d1600b40b0f654c323 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Jul 2025 04:32:29 +0000 Subject: [PATCH 23/57] Fix typos and refactor toggle tests --- integration_tests/general.bats | 48 ++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/integration_tests/general.bats b/integration_tests/general.bats index c32f4071d..af67b6d50 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -126,28 +126,37 @@ bats_require_minimum_version 1.5.0 rm -f out_a.pftrace out_b.pftrace } -@test "thapi_toggle" { +@test "toggle_api" { + rm -rf toggle_traces 2> /dev/null + cc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle.c -o thapi_toggle \ -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi - $IPROF --trace-output trace_toggle --no-analysis -- ./thapi_toggle + $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle - start_count=`babeltrace2 trace_toggle | grep lttng_ust_toggle:start | wc -l` + start_count=`babeltrace2 toggle_traces | grep lttng_ust_toggle:start | wc -l` [ "$start_count" -eq 1 ] - stop_count=`babeltrace2 trace_toggle | grep lttng_ust_toggle:stop | wc -l` + stop_count=`babeltrace2 toggle_traces | grep lttng_ust_toggle:stop | wc -l` [ "$stop_count" -eq 2 ] } -toggle_count_traces() { +toggle_count_base() { + rm -rf toggle_traces 2> /dev/null + + THAPI_SYNC_DAEMON=fs THAPI_JOBID=$3 timeout 40s $MPIRUN -n $1 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi $2 + trace_metadata_file=`find toggle_traces -iname metadata` trace_metadata_dir=$(dirname "${trace_metadata_file}") - traces=$(babeltrace2 --plugin-path=${THAPI_LIB_DIR} \ --component source:source.ctf.fs --params "inputs=[\"${trace_metadata_dir}\"]" \ --component=filter:filter.metababel_filter.btx \ --component=sink:sink.text.pretty) - rm -rf toggle_traces + echo $traces +} + +toggle_count_traces() { + traces=$(toggle_count_base $1 $2 $3) echo $traces | sed -e "s/ \[/@[/g" | sed "s/@/\n/g" | grep . | wc -l } @@ -155,35 +164,28 @@ toggle_count_traces() { mpicc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle_mpi.c -o thapi_toggle_mpi \ -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi - THAPI_SYNC_DAEMON=fs THAPI_JOBID=0 timeout 40s $MPIRUN -n 1 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi 0 - count_0=$(toggle_count_traces) - - THAPI_SYNC_DAEMON=fs THAPI_JOBID=0 timeout 40s $MPIRUN -n 1 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi 1 - count_1=$(toggle_count_traces) - - THAPI_SYNC_DAEMON=fs THAPI_JOBID=0 timeout 40s $MPIRUN -n 1 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi 2 - count_2=$(toggle_count_traces) + count_0=$(toggle_count_traces 1 0 100) + count_1=$(toggle_count_traces 1 1 101) + count_2=$(toggle_count_traces 1 2 102) [ "$count_2" -eq 0 ] [ "$count_0" -gt "$count_1" ] } toggle_count_vpids() { - vpids=$(babeltrace2 toggle_traces | sed -e "s/, { vpid = /\nvpid,/g" | grep vpid | awk '{ split($0,a,","); print a[2] }' | sort | uniq | wc -l) - rm -rf toggle_traces - echo $vpids + traces=$(toggle_count_base $1 $2 $3) + echo $traces | sed -e "s/ - /, /g" | sed -e "s/,/\n/g" | grep vpid | sort | uniq | wc -l } @test "toggle_plugin_mpi_np_2" { mpicc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle_mpi.c -o thapi_toggle_mpi \ -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi - THAPI_SYNC_DAEMON=fs THAPI_JOBID=0 timeout 40s $MPIRUN -n 2 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi 0 - count_0=$(toggle_count_vpids) - - THAPI_SYNC_DAEMON=fs THAPI_JOBID=0 timeout 40s $MPIRUN -n 2 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi 1 - count_1=$(toggle_count_vpids) + count_0=$(toggle_count_vpids 2 0 200) + count_1=$(toggle_count_vpids 2 1 201) + count_2=$(toggle_count_vpids 2 2 202) [ "$count_0" -eq 2 ] [ "$count_1" -eq 1 ] + [ "$count_2" -eq 0 ] } From 66b69d8e6ae4b8847f1a26559e18c896bbc63f37 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 3 Jul 2025 16:31:25 +0000 Subject: [PATCH 24/57] Move toggle tests into toggle.bats --- integration_tests/toggle.bats | 84 +++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 integration_tests/toggle.bats diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats new file mode 100644 index 000000000..ed04563ba --- /dev/null +++ b/integration_tests/toggle.bats @@ -0,0 +1,84 @@ +#!/usr/bin/env bats + +setup_file() { + export THAPI_HOME=${THAPI_HOME:-${PWD}} + export THAPI_INSTALL_DIR=${THAPI_INSTALL_DIR:-${PWD}/build/ici/} + export THAPI_BIN_DIR=${THAPI_BIN_DIR:-${THAPI_INSTALL_DIR}/bin} + export THAPI_INC_DIR=${THAPI_INC_DIR:-${THAPI_INSTALL_DIR}/include} + export THAPI_LIB_DIR=${THAPI_LIB_DIR:-${THAPI_INSTALL_DIR}/lib} + + export IPROF=$THAPI_BIN_DIR/iprof + export MPIRUN=${MPIRUN:-mpirun} +} + +teardown_file() { + rm -rf $THAPI_HOME/thapi-traces +} + +get_unique_jobid() { + echo ${BATS_TEST_NAME}.${RANDOM} +} + +@test "toggle_api" { + rm -rf toggle_traces 2> /dev/null + + cc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle.c -o thapi_toggle \ + -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi + $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle + + start_count=`babeltrace2 toggle_traces | grep lttng_ust_toggle:start | wc -l` + [ "$start_count" -eq 1 ] + + stop_count=`babeltrace2 toggle_traces | grep lttng_ust_toggle:stop | wc -l` + [ "$stop_count" -eq 2 ] +} + +toggle_count_base() { + rm -rf toggle_traces 2> /dev/null + + THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s $MPIRUN -n $1 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi $2 + + trace_metadata_file=`find toggle_traces -iname metadata` + trace_metadata_dir=$(dirname "${trace_metadata_file}") + traces=$(babeltrace2 --plugin-path=${THAPI_LIB_DIR} \ + --component source:source.ctf.fs --params "inputs=[\"${trace_metadata_dir}\"]" \ + --component=filter:filter.metababel_filter.btx \ + --component=sink:sink.text.pretty) + + echo $traces +} + +toggle_count_traces() { + traces=$(toggle_count_base $1 $2) + echo $traces | sed -e "s/ \[/@[/g" | sed "s/@/\n/g" | grep . | wc -l +} + +@test "toggle_plugin_mpi_np_1" { + mpicc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle_mpi.c -o thapi_toggle_mpi \ + -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi + + count_0=$(toggle_count_traces 1 0) + count_1=$(toggle_count_traces 1 1) + count_2=$(toggle_count_traces 1 2) + + [ "$count_2" -eq 0 ] + [ "$count_0" -gt "$count_1" ] +} + +toggle_count_vpids() { + traces=$(toggle_count_base $1 $2) + echo $traces | sed -e "s/ - /, /g" | sed -e "s/,/\n/g" | grep vpid | sort | uniq | wc -l +} + +@test "toggle_plugin_mpi_np_2" { + mpicc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle_mpi.c -o thapi_toggle_mpi \ + -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi + + count_0=$(toggle_count_vpids 2 0) + count_1=$(toggle_count_vpids 2 1) + count_2=$(toggle_count_vpids 2 2) + + [ "$count_0" -eq 2 ] + [ "$count_1" -eq 1 ] + [ "$count_2" -eq 0 ] +} From 67354cbb0bea368481c979b51bb5011943552657 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 3 Jul 2025 16:47:27 +0000 Subject: [PATCH 25/57] Use babeltrace_thapi instead of babeltrace2 --- integration_tests/toggle.bats | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index ed04563ba..d997a8b48 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -9,6 +9,7 @@ setup_file() { export IPROF=$THAPI_BIN_DIR/iprof export MPIRUN=${MPIRUN:-mpirun} + export BBT=${THAPI_BIN_DIR}/babeltrace_thapi } teardown_file() { @@ -24,12 +25,14 @@ get_unique_jobid() { cc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle.c -o thapi_toggle \ -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi + $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle + dir=$(ls -d -1 ./toggle_traces/*/) - start_count=`babeltrace2 toggle_traces | grep lttng_ust_toggle:start | wc -l` + start_count=`$BBT -c $dir | grep lttng_ust_toggle:start | wc -l` [ "$start_count" -eq 1 ] - stop_count=`babeltrace2 toggle_traces | grep lttng_ust_toggle:stop | wc -l` + stop_count=`$BBT -c $dir | grep lttng_ust_toggle:stop | wc -l` [ "$stop_count" -eq 2 ] } From 5946101c4ec4cefb2ea8d4c093cc901219cd29e3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 3 Jul 2025 17:22:50 +0000 Subject: [PATCH 26/57] Simplify trace counting --- integration_tests/toggle.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index d997a8b48..766099683 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -53,7 +53,7 @@ toggle_count_base() { toggle_count_traces() { traces=$(toggle_count_base $1 $2) - echo $traces | sed -e "s/ \[/@[/g" | sed "s/@/\n/g" | grep . | wc -l + echo $traces | sed -e "s/ \[/\n[/g" | grep . | wc -l } @test "toggle_plugin_mpi_np_1" { From 3e5779da1e48c5a21dfed8e6f9df0d72d2ecee1b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 14 Jul 2025 17:31:51 +0000 Subject: [PATCH 27/57] Set and use the env vars from setup_suite.bash --- integration_tests/toggle.bats | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 766099683..880b80771 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -1,17 +1,5 @@ #!/usr/bin/env bats -setup_file() { - export THAPI_HOME=${THAPI_HOME:-${PWD}} - export THAPI_INSTALL_DIR=${THAPI_INSTALL_DIR:-${PWD}/build/ici/} - export THAPI_BIN_DIR=${THAPI_BIN_DIR:-${THAPI_INSTALL_DIR}/bin} - export THAPI_INC_DIR=${THAPI_INC_DIR:-${THAPI_INSTALL_DIR}/include} - export THAPI_LIB_DIR=${THAPI_LIB_DIR:-${THAPI_INSTALL_DIR}/lib} - - export IPROF=$THAPI_BIN_DIR/iprof - export MPIRUN=${MPIRUN:-mpirun} - export BBT=${THAPI_BIN_DIR}/babeltrace_thapi -} - teardown_file() { rm -rf $THAPI_HOME/thapi-traces } From 112194ee3989fdd78fe59f1539a86da7dc3bcd63 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 14 Jul 2025 18:33:15 +0000 Subject: [PATCH 28/57] Rename the filter to `toggle` --- integration_tests/toggle.bats | 2 +- utils/Makefile.am | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 880b80771..0dfeaa6bf 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -33,7 +33,7 @@ toggle_count_base() { trace_metadata_dir=$(dirname "${trace_metadata_file}") traces=$(babeltrace2 --plugin-path=${THAPI_LIB_DIR} \ --component source:source.ctf.fs --params "inputs=[\"${trace_metadata_dir}\"]" \ - --component=filter:filter.metababel_filter.btx \ + --component=filter:filter.toggle.btx \ --component=sink:sink.text.pretty) echo $traces diff --git a/utils/Makefile.am b/utils/Makefile.am index 5584aa7da..648d9cac0 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -66,7 +66,7 @@ BTX_THAPI_TOGGLE_GENERATED = \ btx_thapi_toggle/btx_main.c $(BTX_THAPI_TOGGLE_GENERATED): $(srcdir)/btx_thapi_toggle.yaml - $(METABABEL) --enable-callbacks on_downstream --component-type FILTER \ + $(METABABEL) --enable-callbacks on_downstream --component-type FILTER -p toggle \ --upstream $(srcdir)/btx_thapi_toggle.yaml --downstream $(srcdir)/btx_thapi_toggle.yaml \ -o btx_thapi_toggle From 3f5e8b0e4aeccebbda2a0400b04d316af8566a58 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 14 Jul 2025 18:34:44 +0000 Subject: [PATCH 29/57] Rename `thapi_toggle_* -> toggle_* in tests --- integration_tests/toggle.bats | 11 ++++++----- integration_tests/{thapi_toggle.c => toggle.c} | 0 .../{thapi_toggle_mpi.c => toggle_mpi.c} | 0 3 files changed, 6 insertions(+), 5 deletions(-) rename integration_tests/{thapi_toggle.c => toggle.c} (100%) rename integration_tests/{thapi_toggle_mpi.c => toggle_mpi.c} (100%) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 0dfeaa6bf..4a7b74471 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -11,10 +11,10 @@ get_unique_jobid() { @test "toggle_api" { rm -rf toggle_traces 2> /dev/null - cc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle.c -o thapi_toggle \ + cc -I${THAPI_INC_DIR} ./integration_tests/toggle.c -o toggle \ -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi - $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle + $IPROF --trace-output toggle_traces --no-analysis -- ./toggle dir=$(ls -d -1 ./toggle_traces/*/) start_count=`$BBT -c $dir | grep lttng_ust_toggle:start | wc -l` @@ -27,7 +27,8 @@ get_unique_jobid() { toggle_count_base() { rm -rf toggle_traces 2> /dev/null - THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s $MPIRUN -n $1 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi $2 + THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s $MPIRUN -n $1 \ + $IPROF --trace-output toggle_traces --no-analysis -- ./toggle_mpi $2 trace_metadata_file=`find toggle_traces -iname metadata` trace_metadata_dir=$(dirname "${trace_metadata_file}") @@ -45,7 +46,7 @@ toggle_count_traces() { } @test "toggle_plugin_mpi_np_1" { - mpicc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle_mpi.c -o thapi_toggle_mpi \ + mpicc -I${THAPI_INC_DIR} ./integration_tests/toggle_mpi.c -o toggle_mpi \ -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi count_0=$(toggle_count_traces 1 0) @@ -62,7 +63,7 @@ toggle_count_vpids() { } @test "toggle_plugin_mpi_np_2" { - mpicc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle_mpi.c -o thapi_toggle_mpi \ + mpicc -I${THAPI_INC_DIR} ./integration_tests/toggle_mpi.c -o toggle_mpi \ -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi count_0=$(toggle_count_vpids 2 0) diff --git a/integration_tests/thapi_toggle.c b/integration_tests/toggle.c similarity index 100% rename from integration_tests/thapi_toggle.c rename to integration_tests/toggle.c diff --git a/integration_tests/thapi_toggle_mpi.c b/integration_tests/toggle_mpi.c similarity index 100% rename from integration_tests/thapi_toggle_mpi.c rename to integration_tests/toggle_mpi.c From 84b0e39d477375a3a002f1f287f638f40de28687 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 15 Jul 2025 17:02:04 +0000 Subject: [PATCH 30/57] Undo spliting LTTNNG_FLAGS --- utils/Makefile.am | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/utils/Makefile.am b/utils/Makefile.am index 648d9cac0..d8841e4d8 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -19,8 +19,7 @@ lttng/tracepoint_gen.h: $(srcdir)/tracepoint_gen.rb mkdir -p lttng $(RUBY) $< 25 > $@ -LTTNG_FLAGS= -fPIC -Wall -Wextra -Wno-unused-parameter -Wno-type-limits -Wno-sign-compare $(WERROR) \ - -I$(top_srcdir)/utils -I$(top_srcdir)/utils/include -I./ +LTTNG_FLAGS=-fPIC -Wall -Wextra -Wno-unused-parameter -Wno-type-limits -Wno-sign-compare $(WERROR) -I$(top_srcdir)/utils -I$(top_srcdir)/utils/include -I./ %.h %.c: %.tp lttng/tracepoint_gen.h $(LTTNG_GEN_TP) $< -o $*.c -o $*.h From 99ab634c122d7e483c8485e15808216b466420fa Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 15 Jul 2025 20:21:02 +0000 Subject: [PATCH 31/57] Comment why lttng-ust version change is required --- configure.ac | 3 +++ 1 file changed, 3 insertions(+) diff --git a/configure.ac b/configure.ac index cbfd35f68..057ec5834 100644 --- a/configure.ac +++ b/configure.ac @@ -126,6 +126,9 @@ AX_COMPARE_VERSION([$H2YAML_VERSION], [ge], [$H2YAML_MIN_VERSION], PKG_CHECK_MODULES([LIBFFI], [libffi >= 3.2]) PKG_CHECK_MODULES([BABELTRACE2], [babeltrace2 >= 2.0]) +# Use of __attribute__((constructor)) requires `lttng-ust >= 2.12.8` to work properly. +# Specifically, the following fix: +# https://github.com/lttng/lttng-ust/commit/a8fafb675a9f580f6a889223e26664ea11cb0c99. PKG_CHECK_MODULES([LTTNG_UST], [lttng-ust >= 2.12.8]) PKG_CHECK_MODULES([PROTOBUF], [protobuf >= 3.0]) # For blob support From 115d026664cb4ec965ec9075332202c3152cf9ed Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 29 Sep 2025 18:52:07 +0000 Subject: [PATCH 32/57] Install libThapiToggle.so in bt2 plugin dir --- integration_tests/toggle.bats | 7 +------ utils/Makefile.am | 4 +++- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 4a7b74471..0b7060776 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -30,12 +30,7 @@ toggle_count_base() { THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s $MPIRUN -n $1 \ $IPROF --trace-output toggle_traces --no-analysis -- ./toggle_mpi $2 - trace_metadata_file=`find toggle_traces -iname metadata` - trace_metadata_dir=$(dirname "${trace_metadata_file}") - traces=$(babeltrace2 --plugin-path=${THAPI_LIB_DIR} \ - --component source:source.ctf.fs --params "inputs=[\"${trace_metadata_dir}\"]" \ - --component=filter:filter.toggle.btx \ - --component=sink:sink.text.pretty) + traces=$($BBT ./toggle_traces) echo $traces } diff --git a/utils/Makefile.am b/utils/Makefile.am index d8841e4d8..4a8adee70 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -69,7 +69,9 @@ $(BTX_THAPI_TOGGLE_GENERATED): $(srcdir)/btx_thapi_toggle.yaml --upstream $(srcdir)/btx_thapi_toggle.yaml --downstream $(srcdir)/btx_thapi_toggle.yaml \ -o btx_thapi_toggle -lib_LTLIBRARIES += libThapiToggle.la +bt2dir = $(pkglibdir)/bt2 +bt2_LTLIBRARIES = libThapiToggle.la + nodist_libThapiToggle_la_SOURCES = $(BTX_THAPI_TOGGLE_GENERATED) libThapiToggle_la_SOURCES = thapi_toggle_callbacks.cpp libThapiToggle_la_CFLAGS = -fPIC -shared -Wall -Wextra -Wno-unused-parameter $(BABELTRACE2_CFLAGS) \ From e8ad901041297ceae38e062ad9d5bd5c49085ba8 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 1 Oct 2025 01:41:38 +0000 Subject: [PATCH 33/57] Fix libThapiToggle.so flags --- utils/Makefile.am | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/utils/Makefile.am b/utils/Makefile.am index 4a8adee70..9cad1ab80 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -64,10 +64,10 @@ BTX_THAPI_TOGGLE_GENERATED = \ btx_thapi_toggle/metababel/btx_downstream.c \ btx_thapi_toggle/btx_main.c -$(BTX_THAPI_TOGGLE_GENERATED): $(srcdir)/btx_thapi_toggle.yaml - $(METABABEL) --enable-callbacks on_downstream --component-type FILTER -p toggle \ - --upstream $(srcdir)/btx_thapi_toggle.yaml --downstream $(srcdir)/btx_thapi_toggle.yaml \ - -o btx_thapi_toggle +$(BTX_THAPI_TOGGLE_GENERATED) &: $(srcdir)/btx_thapi_toggle.yaml + $(METABABEL) --enable-callbacks on_downstream -t FILTER -p toggle -c toggle \ + --upstream $(srcdir)/btx_thapi_toggle.yaml --downstream $(srcdir)/btx_thapi_toggle.yaml \ + -o btx_thapi_toggle bt2dir = $(pkglibdir)/bt2 bt2_LTLIBRARIES = libThapiToggle.la @@ -78,6 +78,7 @@ libThapiToggle_la_CFLAGS = -fPIC -shared -Wall -Wextra -Wno-unused-parameter $(B -I./btx_thapi_toggle -I$(top_srcdir)/utils/include libThapiToggle_la_CXXFLAGS = -fPIC -shared -Wall -Wextra -Wno-unused-parameter $(BABELTRACE2_CFLAGS) \ -I./btx_thapi_toggle -I$(top_srcdir)/utils/include +libThapiToggle_la_LDFLAGS = $(BABELTRACE2_LIBS) -avoid-version -module BUILT_SOURCES += \ thapi_toggle_tracepoints.h \ From e6bdfb0ecce5c860309757f605bb0c84e4c37fa3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 26 Jan 2026 19:30:43 +0000 Subject: [PATCH 34/57] Fix `shfmt` errors --- integration_tests/toggle.bats | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 0b7060776..8fbfbcdd3 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -1,7 +1,7 @@ #!/usr/bin/env bats teardown_file() { - rm -rf $THAPI_HOME/thapi-traces + rm -rf $THAPI_HOME/thapi-traces } get_unique_jobid() { @@ -9,7 +9,7 @@ get_unique_jobid() { } @test "toggle_api" { - rm -rf toggle_traces 2> /dev/null + rm -rf toggle_traces 2>/dev/null cc -I${THAPI_INC_DIR} ./integration_tests/toggle.c -o toggle \ -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi @@ -17,15 +17,15 @@ get_unique_jobid() { $IPROF --trace-output toggle_traces --no-analysis -- ./toggle dir=$(ls -d -1 ./toggle_traces/*/) - start_count=`$BBT -c $dir | grep lttng_ust_toggle:start | wc -l` + start_count=$($BBT -c $dir | grep lttng_ust_toggle:start | wc -l) [ "$start_count" -eq 1 ] - stop_count=`$BBT -c $dir | grep lttng_ust_toggle:stop | wc -l` + stop_count=$($BBT -c $dir | grep lttng_ust_toggle:stop | wc -l) [ "$stop_count" -eq 2 ] } toggle_count_base() { - rm -rf toggle_traces 2> /dev/null + rm -rf toggle_traces 2>/dev/null THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s $MPIRUN -n $1 \ $IPROF --trace-output toggle_traces --no-analysis -- ./toggle_mpi $2 From 964d8293dce186f41152b9fce7ba9162bec442c5 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 26 Jan 2026 19:53:26 +0000 Subject: [PATCH 35/57] Add `Cflags` and `Libs` to `thapi.pc.in` --- thapi.pc.in | 2 ++ 1 file changed, 2 insertions(+) diff --git a/thapi.pc.in b/thapi.pc.in index e85150d38..70b3252f5 100644 --- a/thapi.pc.in +++ b/thapi.pc.in @@ -7,3 +7,5 @@ bindir=@bindir@ Name: Thapi Description: A tracing infrastructure for heterogeneous computing applications. Version: @PACKAGE_VERSION@ +Cflags: -I${includedir} +Libs: -L${libdir} -lThapi From 0c0402f7da86cec0b95d407efe6c476eb3358abc Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 26 Jan 2026 23:39:06 +0000 Subject: [PATCH 36/57] Use `pkg-config` to find thapi include and ldflags --- integration_tests/toggle.bats | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 8fbfbcdd3..60a9967c0 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -1,5 +1,10 @@ #!/usr/bin/env bats +setup_file() { + export THAPI_INCFLAGS="-I$(pkg-config --variable=includedir thapi)" + export THAPI_LDFLAGS="-Wl,-rpath,$(pkg-config --variable=libdir thapi) $(pkg-config --libs thapi)" +} + teardown_file() { rm -rf $THAPI_HOME/thapi-traces } @@ -11,8 +16,7 @@ get_unique_jobid() { @test "toggle_api" { rm -rf toggle_traces 2>/dev/null - cc -I${THAPI_INC_DIR} ./integration_tests/toggle.c -o toggle \ - -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi + cc ${THAPI_INCFLAGS} ./integration_tests/toggle.c -o toggle ${THAPI_LDFLAGS} $IPROF --trace-output toggle_traces --no-analysis -- ./toggle dir=$(ls -d -1 ./toggle_traces/*/) @@ -41,8 +45,7 @@ toggle_count_traces() { } @test "toggle_plugin_mpi_np_1" { - mpicc -I${THAPI_INC_DIR} ./integration_tests/toggle_mpi.c -o toggle_mpi \ - -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi + mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_mpi.c -o toggle_mpi ${THAPI_LDFLAGS} count_0=$(toggle_count_traces 1 0) count_1=$(toggle_count_traces 1 1) @@ -58,8 +61,7 @@ toggle_count_vpids() { } @test "toggle_plugin_mpi_np_2" { - mpicc -I${THAPI_INC_DIR} ./integration_tests/toggle_mpi.c -o toggle_mpi \ - -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi + mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_mpi.c -o toggle_mpi ${THAPI_LDFLAGS} count_0=$(toggle_count_vpids 2 0) count_1=$(toggle_count_vpids 2 1) From 85e35e5c0af9dc1a6593cc3677b41c886e64e03e Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 27 Jan 2026 21:28:07 +0000 Subject: [PATCH 37/57] $IPROF -> iprof, $BBT -> babeltrace_thapi --- integration_tests/toggle.bats | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 60a9967c0..da9dc6f07 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -18,13 +18,13 @@ get_unique_jobid() { cc ${THAPI_INCFLAGS} ./integration_tests/toggle.c -o toggle ${THAPI_LDFLAGS} - $IPROF --trace-output toggle_traces --no-analysis -- ./toggle + iprof --trace-output toggle_traces --no-analysis -- ./toggle dir=$(ls -d -1 ./toggle_traces/*/) - start_count=$($BBT -c $dir | grep lttng_ust_toggle:start | wc -l) + start_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:start | wc -l) [ "$start_count" -eq 1 ] - stop_count=$($BBT -c $dir | grep lttng_ust_toggle:stop | wc -l) + stop_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:stop | wc -l) [ "$stop_count" -eq 2 ] } @@ -32,9 +32,9 @@ toggle_count_base() { rm -rf toggle_traces 2>/dev/null THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s $MPIRUN -n $1 \ - $IPROF --trace-output toggle_traces --no-analysis -- ./toggle_mpi $2 + iprof --trace-output toggle_traces --no-analysis -- ./toggle_mpi $2 - traces=$($BBT ./toggle_traces) + traces=$(babeltrace_thapi ./toggle_traces) echo $traces } From 615c680f90646ba5411e3d0af948944ebee59e07 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 28 Jan 2026 22:02:22 +0000 Subject: [PATCH 38/57] Enable toggle events on iprof --- xprof/xprof.rb.in | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/xprof/xprof.rb.in b/xprof/xprof.rb.in index ebc16acde..30e99d634 100755 --- a/xprof/xprof.rb.in +++ b/xprof/xprof.rb.in @@ -666,6 +666,11 @@ def enable_events_metadata(channel_name, tracing_mode: 'default', profiling: tru exec("#{lttng_enable} lttng_ust_thapi:*") end +def enable_events_toggle(channel_name, tracing_mode: 'default', profiling: true) + lttng_enable = "lttng enable-event --userspace --session=#{lttng_session_uuid} --channel=#{channel_name}" + exec("#{lttng_enable} lttng_ust_toggle:*") +end + module LocalMaster extend self @@ -751,7 +756,7 @@ module LocalMaster exec("lttng add-context --userspace --session=#{lttng_session_uuid} --channel=#{channel_name} -t vpid -t vtid") # Enable backend events - (backends + ['metadata']).each do |name| + (backends + ['metadata', 'toggle']).each do |name| send("enable_events_#{name}", channel_name, tracing_mode: OPTIONS[:'tracing-mode'], profiling: OPTIONS[:profile]) From e4637335ba8bc249c7d43e5dc53080483b6b0f31 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 29 Jan 2026 20:56:18 +0000 Subject: [PATCH 39/57] $MPIRUN -> mpirun --- integration_tests/toggle.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index da9dc6f07..a333907a1 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -31,7 +31,7 @@ get_unique_jobid() { toggle_count_base() { rm -rf toggle_traces 2>/dev/null - THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s $MPIRUN -n $1 \ + THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s mpirun -n $1 \ iprof --trace-output toggle_traces --no-analysis -- ./toggle_mpi $2 traces=$(babeltrace_thapi ./toggle_traces) From 74fbd575e10828ae8383be91cd7897b3a7c80fdb Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Sun, 1 Feb 2026 04:48:36 +0000 Subject: [PATCH 40/57] Add `lttng_ust_toggle:auto_stop` --- integration_tests/toggle.bats | 13 ++++++++++++- utils/thapi_toggle.c | 6 +++--- utils/thapi_toggle_tracepoints.tp | 7 +++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index a333907a1..b433bd517 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -21,11 +21,22 @@ get_unique_jobid() { iprof --trace-output toggle_traces --no-analysis -- ./toggle dir=$(ls -d -1 ./toggle_traces/*/) + # Make sure auto_stop comes before stop. + babeltrace_thapi ./toggle_traces | awk 'BEGIN { seen_auto = 0 } + $0 ~ /lttng_ust_toggle:auto_stop/ { seen_auto = 1 } + $0 ~ /lttng_ust_toggle:stop/ { if (seen_auto == 1) { exit 0 } else { exit 1 } } + ' + + # Check expected trace counts. start_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:start | wc -l) [ "$start_count" -eq 1 ] stop_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:stop | wc -l) - [ "$stop_count" -eq 2 ] + [ "$stop_count" -eq 1 ] + + auto_stop_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:auto_stop | wc -l) + [ "$auto_stop_count" -eq 1 ] + } toggle_count_base() { diff --git a/utils/thapi_toggle.c b/utils/thapi_toggle.c index 0fedca261..33124ec64 100644 --- a/utils/thapi_toggle.c +++ b/utils/thapi_toggle.c @@ -7,7 +7,7 @@ void thapi_start(void) { tracepoint(lttng_ust_toggle, start); } +void thapi_stop(void) { tracepoint(lttng_ust_toggle, stop); } + void __attribute__((constructor(LTTNG_UST_CONSTRUCTOR_PRIO + 1))) -thapi_stop(void) { - tracepoint(lttng_ust_toggle, stop); -} +thapi_auto_stop(void) { tracepoint(lttng_ust_toggle, auto_stop); } diff --git a/utils/thapi_toggle_tracepoints.tp b/utils/thapi_toggle_tracepoints.tp index 83fc7fac0..3a2b478ea 100644 --- a/utils/thapi_toggle_tracepoints.tp +++ b/utils/thapi_toggle_tracepoints.tp @@ -5,6 +5,13 @@ TRACEPOINT_EVENT( TP_FIELDS() ) +TRACEPOINT_EVENT( + lttng_ust_toggle, + auto_stop, + TP_ARGS(), + TP_FIELDS() +) + TRACEPOINT_EVENT( lttng_ust_toggle, stop, From c9bc75b141b1bc1b774ae97b32239d231be9cb83 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 3 Feb 2026 20:50:40 +0000 Subject: [PATCH 41/57] Get rid of THAPI_HOME --- integration_tests/toggle.bats | 4 ---- 1 file changed, 4 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index b433bd517..1a64a38a9 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -5,10 +5,6 @@ setup_file() { export THAPI_LDFLAGS="-Wl,-rpath,$(pkg-config --variable=libdir thapi) $(pkg-config --libs thapi)" } -teardown_file() { - rm -rf $THAPI_HOME/thapi-traces -} - get_unique_jobid() { echo ${BATS_TEST_NAME}.${RANDOM} } From c10a6e88a339fe0a87c607ffba4df9be44727f0b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 2 Feb 2026 13:29:07 -0600 Subject: [PATCH 42/57] Add a dlopen test for toggle traces --- integration_tests/setup_suite.bash | 3 +++ integration_tests/toggle.bats | 18 +++++++++++++-- integration_tests/toggle.c | 3 ++- integration_tests/toggle_dlopen.c | 35 ++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 integration_tests/toggle_dlopen.c diff --git a/integration_tests/setup_suite.bash b/integration_tests/setup_suite.bash index 96a616436..1f97886da 100644 --- a/integration_tests/setup_suite.bash +++ b/integration_tests/setup_suite.bash @@ -3,7 +3,10 @@ setup_suite() { export MPIRUN=${MPIRUN:-mpirun} + # Set the path to find iprof, babeltrace_thapi, etc. export PATH=$(pkg-config --variable=bindir thapi):${PATH} + # We need this for the toggle_api/toggle_dlopen test. + export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(pkg-config --variable=libdir thapi) missing_tools=() diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 1a64a38a9..8315b4e42 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -10,10 +10,9 @@ get_unique_jobid() { } @test "toggle_api" { - rm -rf toggle_traces 2>/dev/null - cc ${THAPI_INCFLAGS} ./integration_tests/toggle.c -o toggle ${THAPI_LDFLAGS} + rm -rf toggle_traces 2>/dev/null iprof --trace-output toggle_traces --no-analysis -- ./toggle dir=$(ls -d -1 ./toggle_traces/*/) @@ -33,6 +32,21 @@ get_unique_jobid() { auto_stop_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:auto_stop | wc -l) [ "$auto_stop_count" -eq 1 ] + cc ./integration_tests/toggle_dlopen.c -o toggle_dlopen -ldl + + rm -rf toggle_traces 2>/dev/null + iprof --trace-output toggle_traces --no-analysis -- ./toggle_dlopen + dir=$(ls -d -1 ./toggle_traces/*/) + + # Check expected trace counts. + start_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:start | wc -l) + [ "$start_count" -eq 2 ] + + stop_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:stop | wc -l) + [ "$stop_count" -eq 2 ] + + auto_stop_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:auto_stop | wc -l) + [ "$auto_stop_count" -eq 2 ] } toggle_count_base() { diff --git a/integration_tests/toggle.c b/integration_tests/toggle.c index bc1e1e900..c3243230f 100644 --- a/integration_tests/toggle.c +++ b/integration_tests/toggle.c @@ -1,6 +1,7 @@ #include -int main(int argc, char *argv[]) { +int main(void) { thapi_start(); thapi_stop(); + return 0; } diff --git a/integration_tests/toggle_dlopen.c b/integration_tests/toggle_dlopen.c new file mode 100644 index 000000000..f50f5a2cd --- /dev/null +++ b/integration_tests/toggle_dlopen.c @@ -0,0 +1,35 @@ +#include +#include +#include + +#define check_error(ptr_) \ + { \ + void *ptr = (void *)ptr_; \ + if (!ptr) { \ + printf("%s:%d -- %s\n", __FILE__, __LINE__, dlerror()); \ + return 1; \ + } \ + } + +int main(void) { + dlerror(); + + for (int i = 0; i < 2; i++) { + void *thapi = dlopen("libThapi.so", RTLD_NOW | RTLD_LOCAL); + check_error(thapi); + + void (*start)(void) = (void (*)(void))dlsym(thapi, "thapi_start"); + check_error(start); + + void (*stop)(void) = (void (*)(void))dlsym(thapi, "thapi_stop"); + check_error(stop); + + (*start)(), (*stop)(); + + dlclose(thapi); + } + + return 0; +} + +#undef check_error From 90c579cb2ed887689df319a278dd8db2ca607233 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 5 Feb 2026 23:14:26 +0000 Subject: [PATCH 43/57] Update the thapi babeltrace plugin --- utils/btx_thapi_toggle.yaml | 1 + utils/thapi_toggle_callbacks.cpp | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/utils/btx_thapi_toggle.yaml b/utils/btx_thapi_toggle.yaml index 53266a1c0..370cb9a2e 100644 --- a/utils/btx_thapi_toggle.yaml +++ b/utils/btx_thapi_toggle.yaml @@ -24,5 +24,6 @@ :type: integer_signed :cast_type: int64_t :event_classes: + - :name: lttng_ust_toggle:auto_stop - :name: lttng_ust_toggle:start - :name: lttng_ust_toggle:stop diff --git a/utils/thapi_toggle_callbacks.cpp b/utils/thapi_toggle_callbacks.cpp index 00e6c183d..9029d4d04 100644 --- a/utils/thapi_toggle_callbacks.cpp +++ b/utils/thapi_toggle_callbacks.cpp @@ -13,26 +13,37 @@ using ToggleMap = std::map; static char hostname_s[HOST_NAME_MAX + 1]; -static void init(void **data) { *data = new ToggleMap; } +static void init(void **data) { *data = new ToggleMap[2]; } -static void finalize(void *data) { delete static_cast(data); } +static void finalize(void *data) { delete[] static_cast(data); } -static void thapi_start_callback(void *btx_handle, void *tmap, int64_t cpuid, const char *hostname, +static void thapi_auto_stop_callback(void *btx_handle, void *maps, int64_t cpuid, const char *hostname, + int64_t vpid, int64_t vtid) { + auto auto_map = static_cast(maps)[0]; + auto key = ToggleKey{std::string(hostname), vpid}; + /* If we have seen the auto_map trace before, we will just ignore it. */ + if ((*auto_map)[key]) return; + /* Otherwise, we will stop tracing. */ + auto map = static_cast(maps)[1]; + (*map)[key] = false; +} + +static void thapi_start_callback(void *btx_handle, void *maps, int64_t cpuid, const char *hostname, int64_t vpid, int64_t vtid) { - auto map = static_cast(tmap); + auto map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname), vpid}; (*map)[key] = true; strncpy(hostname_s, hostname, HOST_NAME_MAX); } -static void thapi_stop_callback(void *btx_handle, void *tmap, int64_t cpuid, const char *hostname, +static void thapi_stop_callback(void *btx_handle, void *maps, int64_t cpuid, const char *hostname, int64_t vpid, int64_t vtid) { - auto map = static_cast(tmap); + auto map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname), vpid}; (*map)[key] = false; } -static void push_downstream(void *btx_handle, void *tmap, const bt_message *msg) { +static void push_downstream(void *btx_handle, void *maps, const bt_message *msg) { bool push_msg = true; if (bt_message_get_type(msg) == BT_MESSAGE_TYPE_EVENT) { @@ -41,7 +52,7 @@ static void push_downstream(void *btx_handle, void *tmap, const bt_message *msg) const bt_field *vpid = bt_field_structure_borrow_member_field_by_name_const(ccf, "vpid"); uint64_t vpid_v = bt_field_integer_signed_get_value(vpid); - auto map = static_cast(tmap); + auto map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname_s), vpid_v}; push_msg = (*map)[key]; } @@ -55,6 +66,8 @@ static void push_downstream(void *btx_handle, void *tmap, const bt_message *msg) void btx_register_usr_callbacks(void *btx_handle) { btx_register_callbacks_initialize_component(btx_handle, &init); + btx_register_callbacks_lttng_ust_toggle_auto_stop(btx_handle, + &thapi_auto_stop_callback); btx_register_callbacks_lttng_ust_toggle_start(btx_handle, &thapi_start_callback); btx_register_callbacks_lttng_ust_toggle_stop(btx_handle, From ff907f9735e6705caca9e5e8e7306e57e283f6db Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 5 Feb 2026 23:15:49 +0000 Subject: [PATCH 44/57] clang-format -i thapi_toggle* --- utils/thapi_toggle.c | 4 +++- utils/thapi_toggle_callbacks.cpp | 26 ++++++++++++++++---------- utils/thapi_toggle_tracepoints.tp | 21 +++------------------ 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/utils/thapi_toggle.c b/utils/thapi_toggle.c index 33124ec64..5f60e0ed4 100644 --- a/utils/thapi_toggle.c +++ b/utils/thapi_toggle.c @@ -10,4 +10,6 @@ void thapi_start(void) { tracepoint(lttng_ust_toggle, start); } void thapi_stop(void) { tracepoint(lttng_ust_toggle, stop); } void __attribute__((constructor(LTTNG_UST_CONSTRUCTOR_PRIO + 1))) -thapi_auto_stop(void) { tracepoint(lttng_ust_toggle, auto_stop); } +thapi_auto_stop(void) { + tracepoint(lttng_ust_toggle, auto_stop); +} diff --git a/utils/thapi_toggle_callbacks.cpp b/utils/thapi_toggle_callbacks.cpp index 9029d4d04..23d46f2eb 100644 --- a/utils/thapi_toggle_callbacks.cpp +++ b/utils/thapi_toggle_callbacks.cpp @@ -1,10 +1,10 @@ #include +#include #include #include -#include -#include #include +#include #include @@ -17,39 +17,45 @@ static void init(void **data) { *data = new ToggleMap[2]; } static void finalize(void *data) { delete[] static_cast(data); } -static void thapi_auto_stop_callback(void *btx_handle, void *maps, int64_t cpuid, const char *hostname, +static void thapi_auto_stop_callback(void *btx_handle, void *maps, + int64_t cpuid, const char *hostname, int64_t vpid, int64_t vtid) { auto auto_map = static_cast(maps)[0]; auto key = ToggleKey{std::string(hostname), vpid}; /* If we have seen the auto_map trace before, we will just ignore it. */ - if ((*auto_map)[key]) return; + if ((*auto_map)[key]) + return; /* Otherwise, we will stop tracing. */ auto map = static_cast(maps)[1]; (*map)[key] = false; } -static void thapi_start_callback(void *btx_handle, void *maps, int64_t cpuid, const char *hostname, - int64_t vpid, int64_t vtid) { +static void thapi_start_callback(void *btx_handle, void *maps, int64_t cpuid, + const char *hostname, int64_t vpid, + int64_t vtid) { auto map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname), vpid}; (*map)[key] = true; strncpy(hostname_s, hostname, HOST_NAME_MAX); } -static void thapi_stop_callback(void *btx_handle, void *maps, int64_t cpuid, const char *hostname, - int64_t vpid, int64_t vtid) { +static void thapi_stop_callback(void *btx_handle, void *maps, int64_t cpuid, + const char *hostname, int64_t vpid, + int64_t vtid) { auto map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname), vpid}; (*map)[key] = false; } -static void push_downstream(void *btx_handle, void *maps, const bt_message *msg) { +static void push_downstream(void *btx_handle, void *maps, + const bt_message *msg) { bool push_msg = true; if (bt_message_get_type(msg) == BT_MESSAGE_TYPE_EVENT) { const bt_event *event = bt_message_event_borrow_event_const(msg); const bt_field *ccf = bt_event_borrow_common_context_field_const(event); - const bt_field *vpid = bt_field_structure_borrow_member_field_by_name_const(ccf, "vpid"); + const bt_field *vpid = + bt_field_structure_borrow_member_field_by_name_const(ccf, "vpid"); uint64_t vpid_v = bt_field_integer_signed_get_value(vpid); auto map = static_cast(maps)[1]; diff --git a/utils/thapi_toggle_tracepoints.tp b/utils/thapi_toggle_tracepoints.tp index 3a2b478ea..35ea61fc9 100644 --- a/utils/thapi_toggle_tracepoints.tp +++ b/utils/thapi_toggle_tracepoints.tp @@ -1,20 +1,5 @@ -TRACEPOINT_EVENT( - lttng_ust_toggle, - start, - TP_ARGS(), - TP_FIELDS() -) +TRACEPOINT_EVENT(lttng_ust_toggle, start, TP_ARGS(), TP_FIELDS()) -TRACEPOINT_EVENT( - lttng_ust_toggle, - auto_stop, - TP_ARGS(), - TP_FIELDS() -) +TRACEPOINT_EVENT(lttng_ust_toggle, auto_stop, TP_ARGS(), TP_FIELDS()) -TRACEPOINT_EVENT( - lttng_ust_toggle, - stop, - TP_ARGS(), - TP_FIELDS() -) +TRACEPOINT_EVENT(lttng_ust_toggle, stop, TP_ARGS(), TP_FIELDS()) From 73a66da8388d7ba45f27ce9f3fa138169e8246e4 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 5 Feb 2026 23:21:16 +0000 Subject: [PATCH 45/57] Fix compilation errors --- utils/thapi_toggle_callbacks.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/utils/thapi_toggle_callbacks.cpp b/utils/thapi_toggle_callbacks.cpp index 23d46f2eb..bf6436a5a 100644 --- a/utils/thapi_toggle_callbacks.cpp +++ b/utils/thapi_toggle_callbacks.cpp @@ -23,11 +23,11 @@ static void thapi_auto_stop_callback(void *btx_handle, void *maps, auto auto_map = static_cast(maps)[0]; auto key = ToggleKey{std::string(hostname), vpid}; /* If we have seen the auto_map trace before, we will just ignore it. */ - if ((*auto_map)[key]) + if (auto_map[key]) return; /* Otherwise, we will stop tracing. */ auto map = static_cast(maps)[1]; - (*map)[key] = false; + map[key] = false; } static void thapi_start_callback(void *btx_handle, void *maps, int64_t cpuid, @@ -35,7 +35,7 @@ static void thapi_start_callback(void *btx_handle, void *maps, int64_t cpuid, int64_t vtid) { auto map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname), vpid}; - (*map)[key] = true; + map[key] = true; strncpy(hostname_s, hostname, HOST_NAME_MAX); } @@ -44,7 +44,7 @@ static void thapi_stop_callback(void *btx_handle, void *maps, int64_t cpuid, int64_t vtid) { auto map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname), vpid}; - (*map)[key] = false; + map[key] = false; } static void push_downstream(void *btx_handle, void *maps, @@ -60,7 +60,7 @@ static void push_downstream(void *btx_handle, void *maps, auto map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname_s), vpid_v}; - push_msg = (*map)[key]; + push_msg = map[key]; } if (push_msg) { From 7426c09a120f222c94e350e6f5d74b81310c2a3f Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 4 Mar 2026 18:56:46 +0000 Subject: [PATCH 46/57] Minor fixes * Fix a logic error in thapi_auto_stop callback * Print error messages to stderr --- integration_tests/toggle_dlopen.c | 2 +- utils/thapi_toggle_callbacks.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/integration_tests/toggle_dlopen.c b/integration_tests/toggle_dlopen.c index f50f5a2cd..b107d5864 100644 --- a/integration_tests/toggle_dlopen.c +++ b/integration_tests/toggle_dlopen.c @@ -6,7 +6,7 @@ { \ void *ptr = (void *)ptr_; \ if (!ptr) { \ - printf("%s:%d -- %s\n", __FILE__, __LINE__, dlerror()); \ + fprintf(stderr, "%s:%d -- %s\n", __FILE__, __LINE__, dlerror()); \ return 1; \ } \ } diff --git a/utils/thapi_toggle_callbacks.cpp b/utils/thapi_toggle_callbacks.cpp index bf6436a5a..ab68212d9 100644 --- a/utils/thapi_toggle_callbacks.cpp +++ b/utils/thapi_toggle_callbacks.cpp @@ -23,9 +23,9 @@ static void thapi_auto_stop_callback(void *btx_handle, void *maps, auto auto_map = static_cast(maps)[0]; auto key = ToggleKey{std::string(hostname), vpid}; /* If we have seen the auto_map trace before, we will just ignore it. */ - if (auto_map[key]) - return; + if (auto_map.count(key)) return; /* Otherwise, we will stop tracing. */ + auto_map[key] = true; auto map = static_cast(maps)[1]; map[key] = false; } From 4e0a84926a44d8a65f6fb630c4fe640e5b8ea1bf Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 4 Mar 2026 20:03:50 +0000 Subject: [PATCH 47/57] Split toggle API tests into two tests --- integration_tests/toggle.bats | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 8315b4e42..ac88ed6c3 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -10,11 +10,10 @@ get_unique_jobid() { } @test "toggle_api" { - cc ${THAPI_INCFLAGS} ./integration_tests/toggle.c -o toggle ${THAPI_LDFLAGS} - rm -rf toggle_traces 2>/dev/null + + cc ${THAPI_INCFLAGS} ./integration_tests/toggle.c -o toggle ${THAPI_LDFLAGS} iprof --trace-output toggle_traces --no-analysis -- ./toggle - dir=$(ls -d -1 ./toggle_traces/*/) # Make sure auto_stop comes before stop. babeltrace_thapi ./toggle_traces | awk 'BEGIN { seen_auto = 0 } @@ -23,6 +22,7 @@ get_unique_jobid() { ' # Check expected trace counts. + dir=$(ls -d -1 ./toggle_traces/*/) start_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:start | wc -l) [ "$start_count" -eq 1 ] @@ -31,14 +31,16 @@ get_unique_jobid() { auto_stop_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:auto_stop | wc -l) [ "$auto_stop_count" -eq 1 ] +} - cc ./integration_tests/toggle_dlopen.c -o toggle_dlopen -ldl - +@test "toggle_api_dlopen" { rm -rf toggle_traces 2>/dev/null + + cc ./integration_tests/toggle_dlopen.c -o toggle_dlopen -ldl iprof --trace-output toggle_traces --no-analysis -- ./toggle_dlopen - dir=$(ls -d -1 ./toggle_traces/*/) # Check expected trace counts. + dir=$(ls -d -1 ./toggle_traces/*/) start_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:start | wc -l) [ "$start_count" -eq 2 ] From ae8630b41e4acad035bd5c8b92cdd2cb82b09842 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 4 Mar 2026 20:33:38 +0000 Subject: [PATCH 48/57] Add the toggle filter in babeltrace_thapi --- utils/babeltrace_thapi.in | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utils/babeltrace_thapi.in b/utils/babeltrace_thapi.in index 4715179a4..95439dbf5 100755 --- a/utils/babeltrace_thapi.in +++ b/utils/babeltrace_thapi.in @@ -269,6 +269,8 @@ def get_and_add_components(graph, names, l_inputs) ) }) when 'filter.utils.muxer' graph.add(comp, name) + when 'filter.toggle.toggle' + graph.add(comp, name) when 'filter.btx_aggreg.aggreg' graph.add(comp, 'aggreg', params: { 'discard_metadata' => $options[:'discard-metadata'] }) From 7fb2f8ff13fff1bb360706202e0bdd44a7f864f4 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 14 Jul 2026 15:24:53 +0000 Subject: [PATCH 49/57] Remove redundant code due to a bad rebase --- integration_tests/general.bats | 64 ---------------------------------- 1 file changed, 64 deletions(-) diff --git a/integration_tests/general.bats b/integration_tests/general.bats index af67b6d50..fb19e8ff5 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -125,67 +125,3 @@ bats_require_minimum_version 1.5.0 cmp out_a.pftrace out_b.pftrace rm -f out_a.pftrace out_b.pftrace } - -@test "toggle_api" { - rm -rf toggle_traces 2> /dev/null - - cc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle.c -o thapi_toggle \ - -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi - $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle - - start_count=`babeltrace2 toggle_traces | grep lttng_ust_toggle:start | wc -l` - [ "$start_count" -eq 1 ] - - stop_count=`babeltrace2 toggle_traces | grep lttng_ust_toggle:stop | wc -l` - [ "$stop_count" -eq 2 ] -} - -toggle_count_base() { - rm -rf toggle_traces 2> /dev/null - - THAPI_SYNC_DAEMON=fs THAPI_JOBID=$3 timeout 40s $MPIRUN -n $1 $IPROF --trace-output toggle_traces --no-analysis -- ./thapi_toggle_mpi $2 - - trace_metadata_file=`find toggle_traces -iname metadata` - trace_metadata_dir=$(dirname "${trace_metadata_file}") - traces=$(babeltrace2 --plugin-path=${THAPI_LIB_DIR} \ - --component source:source.ctf.fs --params "inputs=[\"${trace_metadata_dir}\"]" \ - --component=filter:filter.metababel_filter.btx \ - --component=sink:sink.text.pretty) - - echo $traces -} - -toggle_count_traces() { - traces=$(toggle_count_base $1 $2 $3) - echo $traces | sed -e "s/ \[/@[/g" | sed "s/@/\n/g" | grep . | wc -l -} - -@test "toggle_plugin_mpi_np_1" { - mpicc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle_mpi.c -o thapi_toggle_mpi \ - -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi - - count_0=$(toggle_count_traces 1 0 100) - count_1=$(toggle_count_traces 1 1 101) - count_2=$(toggle_count_traces 1 2 102) - - [ "$count_2" -eq 0 ] - [ "$count_0" -gt "$count_1" ] -} - -toggle_count_vpids() { - traces=$(toggle_count_base $1 $2 $3) - echo $traces | sed -e "s/ - /, /g" | sed -e "s/,/\n/g" | grep vpid | sort | uniq | wc -l -} - -@test "toggle_plugin_mpi_np_2" { - mpicc -I${THAPI_INC_DIR} ./integration_tests/thapi_toggle_mpi.c -o thapi_toggle_mpi \ - -Wl,-rpath,${THAPI_LIB_DIR} -L${THAPI_LIB_DIR} -lThapi - - count_0=$(toggle_count_vpids 2 0 200) - count_1=$(toggle_count_vpids 2 1 201) - count_2=$(toggle_count_vpids 2 2 202) - - [ "$count_0" -eq 2 ] - [ "$count_1" -eq 1 ] - [ "$count_2" -eq 0 ] -} From 22b8c19ec6b8722e10c8746a78c2c65022b5c20f Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 14 Jul 2026 15:59:05 +0000 Subject: [PATCH 50/57] Fix linting errors --- integration_tests/toggle.c | 2 +- integration_tests/toggle_dlopen.c | 14 ++++++------- integration_tests/toggle_mpi.c | 5 +++-- utils/thapi_toggle.c | 3 +-- utils/thapi_toggle_callbacks.cpp | 33 ++++++++++++------------------- xprof/xprof.rb.in | 4 +++- 6 files changed, 28 insertions(+), 33 deletions(-) diff --git a/integration_tests/toggle.c b/integration_tests/toggle.c index c3243230f..971f8e3cb 100644 --- a/integration_tests/toggle.c +++ b/integration_tests/toggle.c @@ -3,5 +3,5 @@ int main(void) { thapi_start(); thapi_stop(); - return 0; + return 0; } diff --git a/integration_tests/toggle_dlopen.c b/integration_tests/toggle_dlopen.c index b107d5864..d0026a922 100644 --- a/integration_tests/toggle_dlopen.c +++ b/integration_tests/toggle_dlopen.c @@ -2,13 +2,13 @@ #include #include -#define check_error(ptr_) \ - { \ - void *ptr = (void *)ptr_; \ - if (!ptr) { \ - fprintf(stderr, "%s:%d -- %s\n", __FILE__, __LINE__, dlerror()); \ - return 1; \ - } \ +#define check_error(ptr_) \ + { \ + void *ptr = (void *)ptr_; \ + if (!ptr) { \ + fprintf(stderr, "%s:%d -- %s\n", __FILE__, __LINE__, dlerror()); \ + return 1; \ + } \ } int main(void) { diff --git a/integration_tests/toggle_mpi.c b/integration_tests/toggle_mpi.c index d98fc77cf..99a431331 100644 --- a/integration_tests/toggle_mpi.c +++ b/integration_tests/toggle_mpi.c @@ -1,5 +1,5 @@ -#include #include +#include #include @@ -15,7 +15,8 @@ int main(int argc, char *argv[]) { thapi_start(); case 1: MPI_Comm_rank(MPI_COMM_WORLD, &rank); - if (rank == 0) thapi_start(); + if (rank == 0) + thapi_start(); MPI_Comm_size(MPI_COMM_WORLD, &size); break; default: diff --git a/utils/thapi_toggle.c b/utils/thapi_toggle.c index 5f60e0ed4..27a9a815c 100644 --- a/utils/thapi_toggle.c +++ b/utils/thapi_toggle.c @@ -9,7 +9,6 @@ void thapi_start(void) { tracepoint(lttng_ust_toggle, start); } void thapi_stop(void) { tracepoint(lttng_ust_toggle, stop); } -void __attribute__((constructor(LTTNG_UST_CONSTRUCTOR_PRIO + 1))) -thapi_auto_stop(void) { +void __attribute__((constructor(LTTNG_UST_CONSTRUCTOR_PRIO + 1))) thapi_auto_stop(void) { tracepoint(lttng_ust_toggle, auto_stop); } diff --git a/utils/thapi_toggle_callbacks.cpp b/utils/thapi_toggle_callbacks.cpp index ab68212d9..bda478cf7 100644 --- a/utils/thapi_toggle_callbacks.cpp +++ b/utils/thapi_toggle_callbacks.cpp @@ -17,45 +17,41 @@ static void init(void **data) { *data = new ToggleMap[2]; } static void finalize(void *data) { delete[] static_cast(data); } -static void thapi_auto_stop_callback(void *btx_handle, void *maps, - int64_t cpuid, const char *hostname, - int64_t vpid, int64_t vtid) { +static void thapi_auto_stop_callback( + void *btx_handle, void *maps, int64_t cpuid, const char *hostname, int64_t vpid, int64_t vtid) { auto auto_map = static_cast(maps)[0]; auto key = ToggleKey{std::string(hostname), vpid}; /* If we have seen the auto_map trace before, we will just ignore it. */ - if (auto_map.count(key)) return; + if (auto_map.count(key)) + return; /* Otherwise, we will stop tracing. */ auto_map[key] = true; auto map = static_cast(maps)[1]; map[key] = false; } -static void thapi_start_callback(void *btx_handle, void *maps, int64_t cpuid, - const char *hostname, int64_t vpid, - int64_t vtid) { +static void thapi_start_callback( + void *btx_handle, void *maps, int64_t cpuid, const char *hostname, int64_t vpid, int64_t vtid) { auto map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname), vpid}; map[key] = true; strncpy(hostname_s, hostname, HOST_NAME_MAX); } -static void thapi_stop_callback(void *btx_handle, void *maps, int64_t cpuid, - const char *hostname, int64_t vpid, - int64_t vtid) { +static void thapi_stop_callback( + void *btx_handle, void *maps, int64_t cpuid, const char *hostname, int64_t vpid, int64_t vtid) { auto map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname), vpid}; map[key] = false; } -static void push_downstream(void *btx_handle, void *maps, - const bt_message *msg) { +static void push_downstream(void *btx_handle, void *maps, const bt_message *msg) { bool push_msg = true; if (bt_message_get_type(msg) == BT_MESSAGE_TYPE_EVENT) { const bt_event *event = bt_message_event_borrow_event_const(msg); const bt_field *ccf = bt_event_borrow_common_context_field_const(event); - const bt_field *vpid = - bt_field_structure_borrow_member_field_by_name_const(ccf, "vpid"); + const bt_field *vpid = bt_field_structure_borrow_member_field_by_name_const(ccf, "vpid"); uint64_t vpid_v = bt_field_integer_signed_get_value(vpid); auto map = static_cast(maps)[1]; @@ -72,12 +68,9 @@ static void push_downstream(void *btx_handle, void *maps, void btx_register_usr_callbacks(void *btx_handle) { btx_register_callbacks_initialize_component(btx_handle, &init); - btx_register_callbacks_lttng_ust_toggle_auto_stop(btx_handle, - &thapi_auto_stop_callback); - btx_register_callbacks_lttng_ust_toggle_start(btx_handle, - &thapi_start_callback); - btx_register_callbacks_lttng_ust_toggle_stop(btx_handle, - &thapi_stop_callback); + btx_register_callbacks_lttng_ust_toggle_auto_stop(btx_handle, &thapi_auto_stop_callback); + btx_register_callbacks_lttng_ust_toggle_start(btx_handle, &thapi_start_callback); + btx_register_callbacks_lttng_ust_toggle_stop(btx_handle, &thapi_stop_callback); btx_register_callbacks_finalize_component(btx_handle, &finalize); btx_register_on_downstream_message_callback(btx_handle, &push_downstream); diff --git a/xprof/xprof.rb.in b/xprof/xprof.rb.in index 30e99d634..4d024f678 100755 --- a/xprof/xprof.rb.in +++ b/xprof/xprof.rb.in @@ -668,6 +668,8 @@ end def enable_events_toggle(channel_name, tracing_mode: 'default', profiling: true) lttng_enable = "lttng enable-event --userspace --session=#{lttng_session_uuid} --channel=#{channel_name}" + LOGGER.debug('Profiling is ignored for metadata') if profiling + LOGGER.debug("#{tracing_mode} is ignored for metadata") exec("#{lttng_enable} lttng_ust_toggle:*") end @@ -756,7 +758,7 @@ module LocalMaster exec("lttng add-context --userspace --session=#{lttng_session_uuid} --channel=#{channel_name} -t vpid -t vtid") # Enable backend events - (backends + ['metadata', 'toggle']).each do |name| + (backends + %w[metadata toggle]).each do |name| send("enable_events_#{name}", channel_name, tracing_mode: OPTIONS[:'tracing-mode'], profiling: OPTIONS[:profile]) From 6f380359d7af94c8720274320b2699f35baef87f Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 15 Jul 2026 21:06:16 +0000 Subject: [PATCH 51/57] Avoid copying the map --- utils/thapi_toggle_callbacks.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/utils/thapi_toggle_callbacks.cpp b/utils/thapi_toggle_callbacks.cpp index bda478cf7..b4bd95a7d 100644 --- a/utils/thapi_toggle_callbacks.cpp +++ b/utils/thapi_toggle_callbacks.cpp @@ -19,20 +19,20 @@ static void finalize(void *data) { delete[] static_cast(data); } static void thapi_auto_stop_callback( void *btx_handle, void *maps, int64_t cpuid, const char *hostname, int64_t vpid, int64_t vtid) { - auto auto_map = static_cast(maps)[0]; + auto &auto_map = static_cast(maps)[0]; auto key = ToggleKey{std::string(hostname), vpid}; /* If we have seen the auto_map trace before, we will just ignore it. */ if (auto_map.count(key)) return; /* Otherwise, we will stop tracing. */ auto_map[key] = true; - auto map = static_cast(maps)[1]; + auto &map = static_cast(maps)[1]; map[key] = false; } static void thapi_start_callback( void *btx_handle, void *maps, int64_t cpuid, const char *hostname, int64_t vpid, int64_t vtid) { - auto map = static_cast(maps)[1]; + auto &map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname), vpid}; map[key] = true; strncpy(hostname_s, hostname, HOST_NAME_MAX); @@ -40,7 +40,7 @@ static void thapi_start_callback( static void thapi_stop_callback( void *btx_handle, void *maps, int64_t cpuid, const char *hostname, int64_t vpid, int64_t vtid) { - auto map = static_cast(maps)[1]; + auto &map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname), vpid}; map[key] = false; } @@ -54,7 +54,7 @@ static void push_downstream(void *btx_handle, void *maps, const bt_message *msg) const bt_field *vpid = bt_field_structure_borrow_member_field_by_name_const(ccf, "vpid"); uint64_t vpid_v = bt_field_integer_signed_get_value(vpid); - auto map = static_cast(maps)[1]; + auto &map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname_s), vpid_v}; push_msg = map[key]; } From 76efadfee4f32776b567352e1c1e87c2701b08d0 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 16 Jul 2026 02:01:59 +0000 Subject: [PATCH 52/57] Add toggle filter to bt graph if --toggle-on is present --- integration_tests/toggle.bats | 2 +- utils/babeltrace_thapi.in | 6 ++++++ xprof/xprof.rb.in | 14 +++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index ac88ed6c3..5cd83d2b1 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -57,7 +57,7 @@ toggle_count_base() { THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s mpirun -n $1 \ iprof --trace-output toggle_traces --no-analysis -- ./toggle_mpi $2 - traces=$(babeltrace_thapi ./toggle_traces) + traces=$(babeltrace_thapi --toggle-on ./toggle_traces) echo $traces } diff --git a/utils/babeltrace_thapi.in b/utils/babeltrace_thapi.in index 95439dbf5..24e6f434b 100755 --- a/utils/babeltrace_thapi.in +++ b/utils/babeltrace_thapi.in @@ -336,6 +336,11 @@ def bt_graphs(inputs) if $options[:muxer] || inputs.any? { |trace| thapi_metadata(trace)[:type] == 'lttng' } g_comps << 'filter.utils.muxer' + # The toggle filter gates events on/off based on the lttng_ust_toggle + # start/stop tracepoints. It must come after the muxer so it sees the + # toggle and backend events in timestamp order. It is only added when + # the user requests it via `--toggle-on`. + g_comps << 'filter.toggle.toggle' if $options[:'toggle-on'] end { 'tally' => ['filter.intervals.interval', @@ -420,6 +425,7 @@ class BabeltraceParserThapi < OptionParserWithDefaultAndValidation on('--archive SESSION-NAME') on('--archive-session-found-file-path PATH') on('--[no-]muxer') + on('--toggle-on', default: false) on('-v', '--version', 'Print the version string') do puts File.read(File.join(DATADIR, 'thapi_version')) exit diff --git a/xprof/xprof.rb.in b/xprof/xprof.rb.in index 4d024f678..0d90c0999 100755 --- a/xprof/xprof.rb.in +++ b/xprof/xprof.rb.in @@ -706,6 +706,7 @@ module LocalMaster opts = [trace_bt_prefix[:bt]] opts << "--output #{thapi_trace_dir_tmp}" opts << "--backends #{backends.join(',')}" + opts << '--toggle-on' if OPTIONS[:'toggle-on'] opts << '--no-discard-metadata' if trace_bt_prefix[:bt] == 'to_aggreg' && OPTIONS.include?(:'kernel-verbose') if OPTIONS[:archive] @@ -854,16 +855,19 @@ module GlobalMaster babeltrace_exe = "#{BINDIR}/babeltrace_thapi" backends = OPTIONS[:backends].join(',') + toggle_on = OPTIONS[:'toggle-on'] ? ['--toggle-on'] : [] + if OPTIONS.include?(:trace) - cmdnames = [babeltrace_exe, 'trace', '--restrict', '--context', '--backends', backends, '--', folder] + cmdnames = [babeltrace_exe, 'trace', '--restrict', '--context', '--backends', backends, *toggle_on, '--', + folder] elsif OPTIONS.include?(:timeline) # check if there are any timelines. we check in advance # since if we don't have nay saved ones we should fall # back to generating the timeline from the interval files = Dir[folder + '/*/timeline_*.pftrace'] cmdnames = if files.empty? - [babeltrace_exe, 'timeline', '--backends', backends, '--output-path', OPTIONS[:timeline], '--', - folder] + [babeltrace_exe, 'timeline', '--backends', backends, '--output-path', OPTIONS[:timeline], + *toggle_on, '--', folder] else warn("THAPI: Perfetto trace location: #{OPTIONS[:timeline]}") # String because `>` is not a real command, and cannot be passed to popen @@ -877,6 +881,7 @@ module GlobalMaster cmdnames << '--display_mode' << 'json' if OPTIONS.include?(:json) cmdnames << '--backends' << backends cmdnames << '--display' << 'extended' if OPTIONS.include?(:extended) + cmdnames += toggle_on cmdnames += ['--', folder] end @@ -1094,6 +1099,9 @@ if $thapi_launch || __FILE__ == $PROGRAM_NAME 'Format: backend_name[:backend_level],...', default: ['mpi:3', 'omp:2', 'cl:1', 'ze:1', 'cuda:1', 'hip:1', 'cxi:4', 'itt:5', 'pytorch:6']) parser.on('--[no-]archive', 'Enable or disable archive support.', default: false) + parser.on('--toggle-on', + 'Enable the toggle filter so that tracing can be started/stopped programmatically', + 'via thapi_start()/thapi_stop() in the application.', default: false) # Analysis parser.on('-r', '--replay [PATH]', 'Replay traces for post-mortem analysis.', From 5c89a35eeb69066528ce82a793bc67e7c00f1753 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 16 Jul 2026 02:48:45 +0000 Subject: [PATCH 53/57] Update the tests --- integration_tests/toggle.bats | 39 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 5cd83d2b1..78e80c115 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -51,44 +51,45 @@ get_unique_jobid() { [ "$auto_stop_count" -eq 2 ] } -toggle_count_base() { +# Trace and analyse in a single iprof call: `--toggle-on` gates the events on the +# lttng_ust_toggle start/stop tracepoints, and iprof prints the tally to stdout. +count_base() { rm -rf toggle_traces 2>/dev/null THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s mpirun -n $1 \ - iprof --trace-output toggle_traces --no-analysis -- ./toggle_mpi $2 - - traces=$(babeltrace_thapi --toggle-on ./toggle_traces) - - echo $traces + iprof --toggle-on --trace-output toggle_traces -- ./toggle_mpi $2 } -toggle_count_traces() { - traces=$(toggle_count_base $1 $2) - echo $traces | sed -e "s/ \[/\n[/g" | grep . | wc -l +# Count the number of traced API calls: the tally's `Total` row, whose 4th +# `|`-separated column is the call count (see general.bats `default_summary`). +count_traces() { + count_base $1 $2 | awk -F'|' '/Total/ {c = int($4)} END {print c + 0}' } @test "toggle_plugin_mpi_np_1" { mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_mpi.c -o toggle_mpi ${THAPI_LDFLAGS} - count_0=$(toggle_count_traces 1 0) - count_1=$(toggle_count_traces 1 1) - count_2=$(toggle_count_traces 1 2) + count_0=$(count_traces 1 0) + count_1=$(count_traces 1 1) + count_2=$(count_traces 1 2) + [ "$count_0" -eq 2 ] + [ "$count_1" -eq 1 ] [ "$count_2" -eq 0 ] - [ "$count_0" -gt "$count_1" ] } -toggle_count_vpids() { - traces=$(toggle_count_base $1 $2) - echo $traces | sed -e "s/ - /, /g" | sed -e "s/,/\n/g" | grep vpid | sort | uniq | wc -l +# Count the number of processes that emitted traced API calls: the `Processes` +# field of the tally header. +count_processors() { + count_base $1 $2 | grep -oP '\d+(?= Processes)' || echo 0 } @test "toggle_plugin_mpi_np_2" { mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_mpi.c -o toggle_mpi ${THAPI_LDFLAGS} - count_0=$(toggle_count_vpids 2 0) - count_1=$(toggle_count_vpids 2 1) - count_2=$(toggle_count_vpids 2 2) + count_0=$(count_processors 2 0) + count_1=$(count_processors 2 1) + count_2=$(count_processors 2 2) [ "$count_0" -eq 2 ] [ "$count_1" -eq 1 ] From fa863128a46b7cf74df9582d83a2c5b35498c8fa Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 16 Jul 2026 19:17:37 +0000 Subject: [PATCH 54/57] Refactor tests --- integration_tests/setup_suite.bash | 2 -- integration_tests/toggle.bats | 8 ++++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/integration_tests/setup_suite.bash b/integration_tests/setup_suite.bash index 1f97886da..ca742809f 100644 --- a/integration_tests/setup_suite.bash +++ b/integration_tests/setup_suite.bash @@ -5,8 +5,6 @@ setup_suite() { # Set the path to find iprof, babeltrace_thapi, etc. export PATH=$(pkg-config --variable=bindir thapi):${PATH} - # We need this for the toggle_api/toggle_dlopen test. - export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(pkg-config --variable=libdir thapi) missing_tools=() diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 78e80c115..678dba7da 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -3,6 +3,8 @@ setup_file() { export THAPI_INCFLAGS="-I$(pkg-config --variable=includedir thapi)" export THAPI_LDFLAGS="-Wl,-rpath,$(pkg-config --variable=libdir thapi) $(pkg-config --libs thapi)" + # needed for toggle api dlopen test. + export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(pkg-config --variable=libdir thapi) } get_unique_jobid() { @@ -56,6 +58,8 @@ get_unique_jobid() { count_base() { rm -rf toggle_traces 2>/dev/null + mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_mpi.c -o toggle_mpi ${THAPI_LDFLAGS} + THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s mpirun -n $1 \ iprof --toggle-on --trace-output toggle_traces -- ./toggle_mpi $2 } @@ -67,8 +71,6 @@ count_traces() { } @test "toggle_plugin_mpi_np_1" { - mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_mpi.c -o toggle_mpi ${THAPI_LDFLAGS} - count_0=$(count_traces 1 0) count_1=$(count_traces 1 1) count_2=$(count_traces 1 2) @@ -85,8 +87,6 @@ count_processors() { } @test "toggle_plugin_mpi_np_2" { - mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_mpi.c -o toggle_mpi ${THAPI_LDFLAGS} - count_0=$(count_processors 2 0) count_1=$(count_processors 2 1) count_2=$(count_processors 2 2) From 2354ff7b0b767e672b7b286259647aafd681ec5d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 8 Oct 2026 19:46:48 -0700 Subject: [PATCH 55/57] Use the event's trace hostname in the toggle filter push_downstream keyed the toggle map on a file-scope hostname that was only updated by thapi_start_callback, so on multi-host runs events were looked up under the hostname of the last thapi_start seen rather than their own. Read the hostname from the event's trace environment instead. Co-Authored-By: Claude Opus 5.5 --- integration_tests/toggle.bats | 80 ++++++++++++++++++++++++ integration_tests/toggle_fake_hostname.c | 17 +++++ integration_tests/toggle_multi_host.c | 23 +++++++ utils/thapi_toggle_callbacks.cpp | 11 ++-- 4 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 integration_tests/toggle_fake_hostname.c create mode 100644 integration_tests/toggle_multi_host.c diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 678dba7da..b165e86e5 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -95,3 +95,83 @@ count_processors() { [ "$count_1" -eq 1 ] [ "$count_2" -eq 0 ] } + +# Count the calls of the API function $1 in the tally read on stdin. +count_calls() { + awk -F'|' -v name="$1" '$1 ~ name {c = int($4)} END {print c + 0}' +} + +# The tests below trace with hand-made LTTng sessions instead of iprof, to +# control what iprof always sets up. Session $1 (a rank) gets its own session +# daemon, with LTTNG_HOME ${BATS_TEST_TMPDIR}/lttng_home/$1, and writes its trace +# to ${BATS_TEST_TMPDIR}/lttng_traces/$1. $2 is context or no-context: whether +# events carry the vpid/vtid context. Any remaining arguments are environment +# assignments for the session daemon. +lttng_start() { + local home=${BATS_TEST_TMPDIR}/lttng_home/$1 + local out=${BATS_TEST_TMPDIR}/lttng_traces/$1 + local context=$2 + shift 2 + + mkdir -p "${home}" + env LTTNG_HOME="${home}" "$@" lttng-sessiond --daemonize --quiet + LTTNG_HOME="${home}" lttng create toggle_test -o "${out}" + LTTNG_HOME="${home}" lttng enable-channel --userspace --blocking-timeout=inf chan + if [ "${context}" = context ]; then + LTTNG_HOME="${home}" lttng add-context --userspace --channel=chan -t vpid -t vtid + fi + LTTNG_HOME="${home}" lttng enable-event --userspace --channel=chan 'lttng_ust_mpi:*,lttng_ust_toggle:*' + LTTNG_HOME="${home}" lttng start +} + +lttng_stop() { + local home=${BATS_TEST_TMPDIR}/lttng_home/$1 + + LTTNG_HOME="${home}" lttng stop + LTTNG_HOME="${home}" lttng destroy +} + +# Kill the session daemons started by lttng_start, even if the test failed. +teardown() { + for pid_file in "${BATS_TEST_TMPDIR}"/lttng_home/*/.lttng/lttng-sessiond.pid; do + if [ -f "${pid_file}" ]; then + kill "$(cat "${pid_file}")" || true + fi + done +} + +# Environment preloading THAPI's MPI tracer into binary $1 (as tracer_mpi.sh does). +mpi_tracer_env() { + local pkglibdir + pkglibdir=$(pkg-config --variable=libdir thapi)/thapi + echo LTTNG_UST_ALLOW_BLOCKING=1 \ + LTTNG_UST_MPI_LIBMPI="$(ldd "$1" | awk '/libmpi\.so/ {print $3; exit}')" \ + LD_LIBRARY_PATH="${pkglibdir}/mpi:${LD_LIBRARY_PATH}" \ + LD_PRELOAD="${pkglibdir}/mpi/libmpi.so" +} + +# Each rank must be matched against its own hostname, not the hostname of the +# last thapi_start() seen. Fake a two-host run: each rank registers with its own +# session daemon, and toggle_fake_hostname.c makes each daemon record a +# different hostname in its trace. +@test "toggle_plugin_multi_host" { + cc -shared -fPIC ./integration_tests/toggle_fake_hostname.c -o toggle_fake_hostname.so + mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_multi_host.c -o toggle_multi_host ${THAPI_LDFLAGS} + + for rank in 0 1; do + lttng_start ${rank} context \ + LD_PRELOAD="${PWD}/toggle_fake_hostname.so" THAPI_FAKE_HOSTNAME="host${rank}" + done + # Each rank registers with the session daemon of its own rank. + timeout 40s ${MPIRUN} -n 2 sh -c "exec env LTTNG_HOME=${BATS_TEST_TMPDIR}/lttng_home/\${PMI_RANK} \ + $(mpi_tracer_env ./toggle_multi_host) ./toggle_multi_host" + for rank in 0 1; do + lttng_stop ${rank} + done + + run babeltrace_thapi tally --toggle-on --backends mpi "${BATS_TEST_TMPDIR}/lttng_traces" + [ "$status" -eq 0 ] + [[ "$output" =~ "2 Hostnames | 2 Processes" ]] + # Both ranks called thapi_start() before MPI_Comm_rank. + [ "$(count_calls MPI_Comm_rank <<<"$output")" -eq 2 ] +} diff --git a/integration_tests/toggle_fake_hostname.c b/integration_tests/toggle_fake_hostname.c new file mode 100644 index 000000000..cd168d3c4 --- /dev/null +++ b/integration_tests/toggle_fake_hostname.c @@ -0,0 +1,17 @@ +#include +#include +#include + +/* Preloaded into an LTTng session daemon so that the traces it writes carry + * the hostname $THAPI_FAKE_HOSTNAME, to fake a multi-host run on one host. */ + +int gethostname(char *name, size_t len) { + const char *hostname = getenv("THAPI_FAKE_HOSTNAME"); + + if (!hostname) + hostname = "localhost"; + strncpy(name, hostname, len); + if (len) + name[len - 1] = '\0'; + return 0; +} diff --git a/integration_tests/toggle_multi_host.c b/integration_tests/toggle_multi_host.c new file mode 100644 index 000000000..a4f4d490d --- /dev/null +++ b/integration_tests/toggle_multi_host.c @@ -0,0 +1,23 @@ +#include + +#include + +/* The barrier makes every rank call thapi_start() before any rank calls + * MPI_Comm_rank, so each MPI_Comm_rank comes after another rank's + * thapi_start(). The toggle filter must still match it against its own rank's + * hostname, not the hostname of the last thapi_start() seen. */ + +int main(int argc, char *argv[]) { + int rank; + + MPI_Init(&argc, &argv); + + thapi_start(); + MPI_Barrier(MPI_COMM_WORLD); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + thapi_stop(); + + MPI_Finalize(); + + return 0; +} diff --git a/utils/thapi_toggle_callbacks.cpp b/utils/thapi_toggle_callbacks.cpp index b4bd95a7d..7fa57d2dc 100644 --- a/utils/thapi_toggle_callbacks.cpp +++ b/utils/thapi_toggle_callbacks.cpp @@ -1,5 +1,4 @@ #include -#include #include #include @@ -11,8 +10,6 @@ using ToggleKey = std::tuple; using ToggleMap = std::map; -static char hostname_s[HOST_NAME_MAX + 1]; - static void init(void **data) { *data = new ToggleMap[2]; } static void finalize(void *data) { delete[] static_cast(data); } @@ -35,7 +32,6 @@ static void thapi_start_callback( auto &map = static_cast(maps)[1]; auto key = ToggleKey{std::string(hostname), vpid}; map[key] = true; - strncpy(hostname_s, hostname, HOST_NAME_MAX); } static void thapi_stop_callback( @@ -54,8 +50,13 @@ static void push_downstream(void *btx_handle, void *maps, const bt_message *msg) const bt_field *vpid = bt_field_structure_borrow_member_field_by_name_const(ccf, "vpid"); uint64_t vpid_v = bt_field_integer_signed_get_value(vpid); + /* Each trace (one per host) carries its own hostname in its environment. */ + const bt_trace *trace = bt_stream_borrow_trace_const(bt_event_borrow_stream_const(event)); + const bt_value *hostname = + bt_trace_borrow_environment_entry_value_by_name_const(trace, "hostname"); + auto &map = static_cast(maps)[1]; - auto key = ToggleKey{std::string(hostname_s), vpid_v}; + auto key = ToggleKey{std::string(bt_value_string_get(hostname)), vpid_v}; push_msg = map[key]; } From 522a271652d1835bcc9315fe258fe9a9a3d4d826 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 8 Oct 2026 19:46:58 -0700 Subject: [PATCH 56/57] Guard against events without vpid in the toggle filter The toggle filter sees every event after the muxer. An event whose common context is absent or has no vpid member (or whose trace has no hostname entry) made push_downstream dereference a NULL field. Pass such events through untouched since they cannot be attributed to a process. Co-Authored-By: Claude Opus 5.5 --- integration_tests/toggle.bats | 14 ++++++++++++++ utils/thapi_toggle_callbacks.cpp | 14 +++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index b165e86e5..cc35635a7 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -175,3 +175,17 @@ mpi_tracer_env() { # Both ranks called thapi_start() before MPI_Comm_rank. [ "$(count_calls MPI_Comm_rank <<<"$output")" -eq 2 ] } + +# Events without a vpid in their common context cannot be attributed to a +# process or thread: the toggle filter must let them through, not crash. +@test "toggle_plugin_events_without_vpid" { + mpicc ./integration_tests/mpi_helloworld.c -o mpi_helloworld + + lttng_start 0 no-context + env LTTNG_HOME="${BATS_TEST_TMPDIR}/lttng_home/0" $(mpi_tracer_env ./mpi_helloworld) ./mpi_helloworld + lttng_stop 0 + + run babeltrace_thapi trace --toggle-on "${BATS_TEST_TMPDIR}/lttng_traces" + [ "$status" -eq 0 ] + [[ "$output" == *lttng_ust_mpi:MPI_Init_entry* ]] +} diff --git a/utils/thapi_toggle_callbacks.cpp b/utils/thapi_toggle_callbacks.cpp index 7fa57d2dc..b4e17d6d2 100644 --- a/utils/thapi_toggle_callbacks.cpp +++ b/utils/thapi_toggle_callbacks.cpp @@ -47,17 +47,21 @@ static void push_downstream(void *btx_handle, void *maps, const bt_message *msg) if (bt_message_get_type(msg) == BT_MESSAGE_TYPE_EVENT) { const bt_event *event = bt_message_event_borrow_event_const(msg); const bt_field *ccf = bt_event_borrow_common_context_field_const(event); - const bt_field *vpid = bt_field_structure_borrow_member_field_by_name_const(ccf, "vpid"); - uint64_t vpid_v = bt_field_integer_signed_get_value(vpid); + const bt_field *vpid = + ccf ? bt_field_structure_borrow_member_field_by_name_const(ccf, "vpid") : nullptr; /* Each trace (one per host) carries its own hostname in its environment. */ const bt_trace *trace = bt_stream_borrow_trace_const(bt_event_borrow_stream_const(event)); const bt_value *hostname = bt_trace_borrow_environment_entry_value_by_name_const(trace, "hostname"); - auto &map = static_cast(maps)[1]; - auto key = ToggleKey{std::string(bt_value_string_get(hostname)), vpid_v}; - push_msg = map[key]; + /* Events we cannot attribute to a process are not subject to toggling. */ + if (vpid && hostname) { + uint64_t vpid_v = bt_field_integer_signed_get_value(vpid); + auto &map = static_cast(maps)[1]; + auto key = ToggleKey{std::string(bt_value_string_get(hostname)), vpid_v}; + push_msg = map[key]; + } } if (push_msg) { From 49949bc95f59c662ec17b40c5ed990726f636485 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 8 Oct 2026 19:47:55 -0700 Subject: [PATCH 57/57] Make thapi_start/thapi_stop toggle tracing per thread The toggle map was keyed on (hostname, vpid), so a thapi_stop() in one thread also stopped tracing for every other thread of the process, contrary to the API's stated per-thread scope. Add vtid to the key in all toggle callbacks and in push_downstream. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Thilina Ratnayaka --- integration_tests/toggle.bats | 16 ++++++++ integration_tests/toggle_threads.c | 60 ++++++++++++++++++++++++++++++ utils/thapi_toggle_callbacks.cpp | 17 +++++---- 3 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 integration_tests/toggle_threads.c diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index cc35635a7..4e1c2b022 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -189,3 +189,19 @@ mpi_tracer_env() { [ "$status" -eq 0 ] [[ "$output" == *lttng_ust_mpi:MPI_Init_entry* ]] } + +# Toggling is per thread: a thread's calls are traced only after its own +# thapi_start(), and another thread's thapi_stop() does not affect it. +@test "toggle_plugin_per_thread" { + rm -rf toggle_traces 2>/dev/null + + mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_threads.c -o toggle_threads -pthread ${THAPI_LDFLAGS} + iprof_summary=$(THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s ${MPIRUN} -n 1 \ + iprof --toggle-on --trace-output toggle_traces -- ./toggle_threads) + + # Traced: the main thread called thapi_start(), and the worker's thapi_stop() + # did not stop it. + [ "$(count_calls MPI_Comm_rank <<<"$iprof_summary")" -eq 1 ] + # Not traced: the worker thread never called thapi_start(). + [ "$(count_calls MPI_Comm_size <<<"$iprof_summary")" -eq 0 ] +} diff --git a/integration_tests/toggle_threads.c b/integration_tests/toggle_threads.c new file mode 100644 index 000000000..6a93063bb --- /dev/null +++ b/integration_tests/toggle_threads.c @@ -0,0 +1,60 @@ +#include +#include + +#include + +/* The main thread turns tracing on, then a second thread makes an MPI call and + * turns tracing off. Finally, main thread turns tracing off as well. As toggling + * is per thread, only the main thread's MPI_Comm_rank must be traced: the second + * thread never called thapi_start(), and its thapi_stop() must not affect the + * main thread. */ + +static pthread_barrier_t barrier; + +static void *worker(void *arg) { + (void)arg; + + /* sync1: wait till main thread is done calling thapi_start() */ + pthread_barrier_wait(&barrier); + + int size; + MPI_Comm_size(MPI_COMM_WORLD, &size); + thapi_stop(); + + /* sync2: wait till worker is done calling MPI_Comm_size and thapi_stop() */ + pthread_barrier_wait(&barrier); + + return NULL; +} + +int main(int argc, char *argv[]) { + int provided; + MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided); + if (provided < MPI_THREAD_MULTIPLE) + MPI_Abort(MPI_COMM_WORLD, 1); + + pthread_barrier_init(&barrier, NULL, 2); + + pthread_t thread; + pthread_create(&thread, NULL /* attr */, worker /* startr_routine */, NULL /* arg */); + + /* main thread calls thapi_start() */ + thapi_start(); + + /* sync1: wait till main thread is done calling thapi_start() */ + pthread_barrier_wait(&barrier); + + /* sync2: wait till worker is done calling MPI_Comm_size and thapi_stop() */ + pthread_barrier_wait(&barrier); + + int rank; + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + thapi_stop(); + + pthread_join(thread, NULL); + pthread_barrier_destroy(&barrier); + + MPI_Finalize(); + + return 0; +} diff --git a/utils/thapi_toggle_callbacks.cpp b/utils/thapi_toggle_callbacks.cpp index b4e17d6d2..98e71fe71 100644 --- a/utils/thapi_toggle_callbacks.cpp +++ b/utils/thapi_toggle_callbacks.cpp @@ -7,7 +7,7 @@ #include -using ToggleKey = std::tuple; +using ToggleKey = std::tuple; using ToggleMap = std::map; static void init(void **data) { *data = new ToggleMap[2]; } @@ -17,7 +17,7 @@ static void finalize(void *data) { delete[] static_cast(data); } static void thapi_auto_stop_callback( void *btx_handle, void *maps, int64_t cpuid, const char *hostname, int64_t vpid, int64_t vtid) { auto &auto_map = static_cast(maps)[0]; - auto key = ToggleKey{std::string(hostname), vpid}; + auto key = ToggleKey{std::string(hostname), vpid, vtid}; /* If we have seen the auto_map trace before, we will just ignore it. */ if (auto_map.count(key)) return; @@ -30,14 +30,14 @@ static void thapi_auto_stop_callback( static void thapi_start_callback( void *btx_handle, void *maps, int64_t cpuid, const char *hostname, int64_t vpid, int64_t vtid) { auto &map = static_cast(maps)[1]; - auto key = ToggleKey{std::string(hostname), vpid}; + auto key = ToggleKey{std::string(hostname), vpid, vtid}; map[key] = true; } static void thapi_stop_callback( void *btx_handle, void *maps, int64_t cpuid, const char *hostname, int64_t vpid, int64_t vtid) { auto &map = static_cast(maps)[1]; - auto key = ToggleKey{std::string(hostname), vpid}; + auto key = ToggleKey{std::string(hostname), vpid, vtid}; map[key] = false; } @@ -49,17 +49,20 @@ static void push_downstream(void *btx_handle, void *maps, const bt_message *msg) const bt_field *ccf = bt_event_borrow_common_context_field_const(event); const bt_field *vpid = ccf ? bt_field_structure_borrow_member_field_by_name_const(ccf, "vpid") : nullptr; + const bt_field *vtid = + ccf ? bt_field_structure_borrow_member_field_by_name_const(ccf, "vtid") : nullptr; /* Each trace (one per host) carries its own hostname in its environment. */ const bt_trace *trace = bt_stream_borrow_trace_const(bt_event_borrow_stream_const(event)); const bt_value *hostname = bt_trace_borrow_environment_entry_value_by_name_const(trace, "hostname"); - /* Events we cannot attribute to a process are not subject to toggling. */ - if (vpid && hostname) { + /* Events we cannot attribute to a thread are not subject to toggling. */ + if (vpid && vtid && hostname) { uint64_t vpid_v = bt_field_integer_signed_get_value(vpid); + uint64_t vtid_v = bt_field_integer_signed_get_value(vtid); auto &map = static_cast(maps)[1]; - auto key = ToggleKey{std::string(bt_value_string_get(hostname)), vpid_v}; + auto key = ToggleKey{std::string(bt_value_string_get(hostname)), vpid_v, vtid_v}; push_msg = map[key]; } }