From ef1f5953e9177aed17a614cc391c657b9ceb0095 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Fri, 28 Feb 2025 01:56:20 +0000 Subject: [PATCH 01/64] 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 c5e265eea9036e24728af438be56c8159fbd0cc2 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 3 Mar 2025 22:02:51 +0000 Subject: [PATCH 02/64] 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 10871f2d9076f932add1158cb59e55e7102e043a Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 3 Mar 2025 22:40:31 +0000 Subject: [PATCH 03/64] 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 0d3bcc84db8edc8c05f5e9feaabe2310c28cd6ad Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 3 Mar 2025 22:46:54 +0000 Subject: [PATCH 04/64] 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 92d2c37b9c1bd4e4042595c36fe49f6242f5af2f Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 01:13:56 +0000 Subject: [PATCH 05/64] 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 43ec850d18f5cf0da221e2fbea741b2522a2e748 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 01:30:59 +0000 Subject: [PATCH 06/64] 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 88e8af717a2a7f898c03760cf1d1a3996e326c45 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 02:14:25 +0000 Subject: [PATCH 07/64] 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 b6f3f349e0b1a043fb50688492f6b38a444a0db8 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 02:43:35 +0000 Subject: [PATCH 08/64] 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 9350608e4cc2961ff0a4cb20ddaf0645d76e1b5a Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 15:24:03 +0000 Subject: [PATCH 09/64] 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 245726f4c7c56aae2c4fcf4456c042b0afa2be72 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 17:27:53 +0000 Subject: [PATCH 10/64] 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 04f4f66c8cd70b61e4c82b8be95897d3d44ce203 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 02:56:32 +0000 Subject: [PATCH 11/64] 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 3ba3fab20e113b2b06df70a0b94be6c62b7e20ed Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 14:24:33 +0000 Subject: [PATCH 12/64] 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 ff5ba531be7c60d7cc7594d8f84c2baa5242c172 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 14:38:51 +0000 Subject: [PATCH 13/64] 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 dad6a50791a2439a5c14bdfc879b7699a2ac3d5c Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 14:46:40 +0000 Subject: [PATCH 14/64] 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 f1fe36565ca1791a16bc65a9b991a66ade01471c Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 25 Mar 2025 22:38:13 +0000 Subject: [PATCH 15/64] 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 49b45b29ea288093e861dd1112584d6f9875087f Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Apr 2025 04:32:55 +0000 Subject: [PATCH 16/64] 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 2123a88a47932d6174466f4edd5d2bfd857896f6 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Apr 2025 06:10:06 +0000 Subject: [PATCH 17/64] 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 1b41a4fa1debc44b95b0c7f19f2ba3123ea7208e Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 24 Jun 2025 16:09:42 +0000 Subject: [PATCH 18/64] 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 ed4ea96b3da7e77056165195546126eeb4d410cc Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 24 Jun 2025 16:10:12 +0000 Subject: [PATCH 19/64] 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 79949029d8e5cea623b0345f31cbf234706d4be4 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 1 Jul 2025 19:31:49 +0000 Subject: [PATCH 20/64] 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 189493ca81ff3d7c7dd11592bcbb60c25a3ad7d9 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 1 Jul 2025 16:36:56 +0000 Subject: [PATCH 21/64] 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 ecbf273eb89fe5998849a0725c0cfb68c5f2b670 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Jul 2025 04:25:18 +0000 Subject: [PATCH 22/64] 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 153d4fcd42d7f7004dd2388fc2e96d0bbe543b65 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Jul 2025 04:32:29 +0000 Subject: [PATCH 23/64] 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 1ad74a4fbba5e1a1908925bbf82f2aee32044b8b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 3 Jul 2025 16:31:25 +0000 Subject: [PATCH 24/64] 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 b9bd86a9f60c4df1584437528e3e74bff7234abb Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 3 Jul 2025 16:47:27 +0000 Subject: [PATCH 25/64] 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 8fc7eaf1ea228a32c1598d70c604feae6e812417 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 3 Jul 2025 17:22:50 +0000 Subject: [PATCH 26/64] 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 ad3778304a244b31846be5d0ad82c9dc9fc355e3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 14 Jul 2025 17:31:51 +0000 Subject: [PATCH 27/64] 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 44d85fb91de8de642ccdb5c2976f3f9eaad884a4 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 14 Jul 2025 18:33:15 +0000 Subject: [PATCH 28/64] 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 983dfedf6dee38e4027e25f27f90dca5af1ae8c3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 14 Jul 2025 18:34:44 +0000 Subject: [PATCH 29/64] 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 5e3597bd2b056d8cb173a18cce90f698610cc078 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 15 Jul 2025 17:02:04 +0000 Subject: [PATCH 30/64] 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 79bef3660a4f7a1ac1694ddf1dfe08014f4590e7 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 15 Jul 2025 20:21:02 +0000 Subject: [PATCH 31/64] 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 f4390c0cc5f11ff8de37b635652e5fbd771149ff Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 29 Sep 2025 18:52:07 +0000 Subject: [PATCH 32/64] 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 c7919b7bb1a74d50f8f1a74c1a0452374cc86a17 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 1 Oct 2025 01:41:38 +0000 Subject: [PATCH 33/64] 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 03c549d8fb729cbc8033a357234e15bb3c6def92 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 26 Jan 2026 19:30:43 +0000 Subject: [PATCH 34/64] 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 06d29614edbb08b953229e0e97587d430b795016 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 26 Jan 2026 19:53:26 +0000 Subject: [PATCH 35/64] 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 6bba02a2bea468df62611db98ac24248977a9719 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 26 Jan 2026 23:39:06 +0000 Subject: [PATCH 36/64] 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 80ffc625aca5908b1782eb3681047d14c29d7e78 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 27 Jan 2026 21:28:07 +0000 Subject: [PATCH 37/64] $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 1684238376e2747e4b56f430b1c8657a2f155258 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 28 Jan 2026 22:02:22 +0000 Subject: [PATCH 38/64] 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 2a0d2246a531e7c88bff0cd5d29edaf33ee69510 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 29 Jan 2026 20:56:18 +0000 Subject: [PATCH 39/64] $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 ed05092896a480b715f8da3efa9b3e0274690293 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Sun, 1 Feb 2026 04:48:36 +0000 Subject: [PATCH 40/64] 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 81e9df2ac7f9344db95192ccc24246827e68cb6c Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 3 Feb 2026 20:50:40 +0000 Subject: [PATCH 41/64] 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 7cfc63fd0640a1c4b968156210b79698ffecf6d8 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 2 Feb 2026 13:29:07 -0600 Subject: [PATCH 42/64] Add a dlopen test for toggle traces --- integration_tests/toggle.bats | 18 ++++++++++++++-- integration_tests/toggle.c | 3 ++- integration_tests/toggle_dlopen.c | 35 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 integration_tests/toggle_dlopen.c 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 e51d11afc163d3ccbb351aac61016568b88b698a Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 5 Feb 2026 23:14:26 +0000 Subject: [PATCH 43/64] 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 69525a332d545022cb067a07bf8e3fac6e428e15 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 5 Feb 2026 23:15:49 +0000 Subject: [PATCH 44/64] 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 0785aba972afecf6bbb3a6a9585ef802a91014fc Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 5 Feb 2026 23:21:16 +0000 Subject: [PATCH 45/64] 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 708b17fec7c5721d5655b0b00dc9ccb18da2e001 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 4 Mar 2026 18:56:46 +0000 Subject: [PATCH 46/64] 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 6f34603e79a03aabf4f507a50274d0bf4ebe3292 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 4 Mar 2026 20:03:50 +0000 Subject: [PATCH 47/64] 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 853dd0c3dc86956957fdd4c699138a83fb14a103 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 4 Mar 2026 20:33:38 +0000 Subject: [PATCH 48/64] 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 7df2bd347094d5ddade64152d3887bb7dc6a8d34 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 14 Jul 2026 15:24:53 +0000 Subject: [PATCH 49/64] 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 639eee018523fdd126b367c4a81e2ac0cee5039b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 14 Jul 2026 15:59:05 +0000 Subject: [PATCH 50/64] 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 27b732829ae5c81d3e9aa0ff6dc13ffec052801b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 15 Jul 2026 21:06:16 +0000 Subject: [PATCH 51/64] 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 cc8edfcded5c4db625e9089edce6310e628b33af Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 16 Jul 2026 02:01:59 +0000 Subject: [PATCH 52/64] 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 c9b1bf297486c58641f3221d6e6319499f13a80d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 16 Jul 2026 02:48:45 +0000 Subject: [PATCH 53/64] 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 91d21f131c8e244f2ef3f60825c72f29631c13a3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 16 Jul 2026 19:17:37 +0000 Subject: [PATCH 54/64] Refactor tests --- integration_tests/toggle.bats | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 90f488ce61d2da56b54cbe26d6d0587551f8a19c Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 8 Oct 2026 19:46:48 -0700 Subject: [PATCH 55/64] 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 | 106 +++++++++++++++++++++++ integration_tests/toggle_fake_hostname.c | 18 ++++ integration_tests/toggle_multi_host.c | 23 +++++ utils/thapi_toggle_callbacks.cpp | 11 +-- 4 files changed, 153 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..57742e26b 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -95,3 +95,109 @@ 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 name ($1, MPI rank id in these +# tests) gets its own session daemon, with LTTNG_HOME set to +# ${BATS_TEST_TMPDIR}/lttng_home/$1, and writes its trace to +# ${BATS_TEST_TMPDIR}/lttng_home/$1/trace. $2 is context or no-context: whether +# events carry the vpid/vtid context. Prints the session's LTTNG_HOME, which +# the traced application and lttng_stop need to reach its session daemon, and +# babeltrace_thapi to find its trace. +# +# The body is a subshell with `set -e`: callers capture the output with $(...), +# where bats would otherwise not stop on a failing command. +lttng_start() ( + set -e + local home=${BATS_TEST_TMPDIR}/lttng_home/$1 + local out=${home}/trace + local context=$2 + + # Store pipe's write end (fd 1) to fd 3 and point fd 1 (stdout) to + # fd 2 (stderr). + exec 3>&1 1>&2 + + local fake_hostname_so=${BATS_TEST_TMPDIR}/toggle_fake_hostname.so + if [ ! -f "${fake_hostname_so}" ]; then + cc -shared -fPIC ./integration_tests/toggle_fake_hostname.c -o \ + "${fake_hostname_so}" + fi + + mkdir -p "${home}" + # The session daemon takes the trace's hostname from gethostname() when + # the session is created, and LTTng has no option to set it. Preloading + # toggle_fake_hostname.c overrides gethostname() in this daemon to return + # the value of THAPI_FAKE_HOSTNAME when it is set. + LTTNG_HOME="${home}" LD_PRELOAD="${fake_hostname_so}" \ + THAPI_FAKE_HOSTNAME="host$1" 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 + + # echo lttng_home to pipe's write end. + echo "${home}" >&3 +) + +# Stop and destroy the session of the LTTNG_HOME $1 printed by lttng_start. +lttng_stop() { + LTTNG_HOME="$1" lttng stop + LTTNG_HOME="$1" 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, which records its own hostname (see lttng_start). +@test "toggle_plugin_multi_host" { + mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_multi_host.c -o \ + toggle_multi_host ${THAPI_LDFLAGS} + + home0=$(lttng_start 0 context) + home1=$(lttng_start 1 context) + + # We launch two programs below and each registers with the session daemon based + # on its own LTTNG_HOME (set using -env). + local prog=(env $(mpi_tracer_env ./toggle_multi_host) ./toggle_multi_host) + timeout 40s ${MPIRUN} -n 1 -env LTTNG_HOME "${home0}" "${prog[@]}" : \ + -n 1 -env LTTNG_HOME "${home1}" "${prog[@]}" + + lttng_stop "${home0}" + lttng_stop "${home1}" + + run babeltrace_thapi tally --toggle-on --backends mpi "${home0}" "${home1}" + [ "$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..ca4a3f4f3 --- /dev/null +++ b/integration_tests/toggle_fake_hostname.c @@ -0,0 +1,18 @@ +#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. + * LTTng sees the gethostname() defined below, not the actual libc version. */ + +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 f97df9a2d4cb67de276610b52f7f9b8104fee1aa Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 8 Oct 2026 19:46:58 -0700 Subject: [PATCH 56/64] 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 | 17 +++++++++++++++++ utils/thapi_toggle_callbacks.cpp | 14 +++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 57742e26b..142e535c7 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -201,3 +201,20 @@ 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 + + home=$(lttng_start 0 no-context) + env LTTNG_HOME="${home}" $(mpi_tracer_env ./mpi_helloworld) ./mpi_helloworld + lttng_stop "${home}" + + # We use `trace` here instead of `tally` as `tally` won't show any counted MPI + # API calls due to the lack of context (vpid and vtid). Without context, the MPI + # interval filter can't match entry/exit events. + run babeltrace_thapi trace --toggle-on "${home}" + [ "$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 afd19f7940169d0340edf35cfeebaa388dd82dd7 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 8 Oct 2026 19:47:55 -0700 Subject: [PATCH 57/64] 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 | 20 ++++++++++ integration_tests/toggle_threads.c | 60 ++++++++++++++++++++++++++++++ utils/thapi_toggle_callbacks.cpp | 17 +++++---- 3 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 integration_tests/toggle_threads.c diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 142e535c7..e8f0f2f97 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -218,3 +218,23 @@ 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, so we should see one MPI_Comm_rank() (see toggle_threads.c). + [ "$(count_calls MPI_Comm_rank <<<"$iprof_summary")" -eq 1 ] + # Not traced: the worker thread never called thapi_start(), so we shouldn't + # see MPI_Comm_size() (see toggle_threads.c). + [ "$(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]; } } From b9a400902025c9b035a139dcd7e80d891d786b3b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Sat, 10 Oct 2026 11:11:09 -0700 Subject: [PATCH 58/64] Unify the counting helpers and clean up the toggle tests Replace count_base, count_traces, count_processors and count_calls with iprof_toggle_mpi, which runs toggle_mpi under iprof, and two filters over the tally on stdin: count_calls (one function's calls, or Total) and count_processes. count_calls now matches the function name exactly instead of as a regex. Also some general cleanup of the toggle tests: use $MPIRUN instead of mpirun, and set LD_LIBRARY_PATH only for the iprof run of toggle_api_dlopen, the one test that needs it, instead of exporting it for the whole file in setup_file. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Thilina Ratnayaka --- integration_tests/toggle.bats | 61 ++++++++++++++--------------------- 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index e8f0f2f97..2000b47fa 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -3,8 +3,6 @@ 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() { @@ -39,7 +37,8 @@ get_unique_jobid() { 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 + LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(pkg-config --variable=libdir thapi) \ + iprof --trace-output toggle_traces --no-analysis -- ./toggle_dlopen # Check expected trace counts. dir=$(ls -d -1 ./toggle_traces/*/) @@ -53,52 +52,40 @@ get_unique_jobid() { [ "$auto_stop_count" -eq 2 ] } -# 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() { +# Run toggle_mpi variant $2 on $1 ranks with `iprof --toggle-on`. iprof prints +# the tally to stdout. +iprof_toggle_mpi() { rm -rf toggle_traces 2>/dev/null - mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_mpi.c -o toggle_mpi ${THAPI_LDFLAGS} + 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 + THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s \ + ${MPIRUN} -n $1 iprof --toggle-on --trace-output toggle_traces -- ./toggle_mpi $2 } -# 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}' +# Count the tally of calls of API function $1, or of all functions with `Total`: +# the 4th `|`-separated column of that row (see general.bats `default_summary`). +count_calls() { + awk -F'|' -v name="$1" '{n = $1; gsub(/ /, "", n)} n == name {c = int($4)} END {print c + 0}' } -@test "toggle_plugin_mpi_np_1" { - 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 the processes that emitted traced API calls: the `Processes` field of +# the header of the tally. +count_processes() { + grep -oP '\d+(?= Processes)' || echo 0 } -# 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_1" { + [ "$(iprof_toggle_mpi 1 0 | count_calls Total)" -eq 2 ] + [ "$(iprof_toggle_mpi 1 1 | count_calls Total)" -eq 1 ] + [ "$(iprof_toggle_mpi 1 2 | count_calls Total)" -eq 0 ] } @test "toggle_plugin_mpi_np_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 ] - [ "$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}' + [ "$(iprof_toggle_mpi 2 0 | count_processes)" -eq 2 ] + [ "$(iprof_toggle_mpi 2 1 | count_processes)" -eq 1 ] + [ "$(iprof_toggle_mpi 2 2 | count_processes)" -eq 0 ] } # The tests below trace with hand-made LTTng sessions instead of iprof, to From 0701fba37c027f49de9ddb4683e11436b4e3a6f4 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Sun, 11 Oct 2026 02:40:52 -0700 Subject: [PATCH 59/64] Name the toggle tests after the programs they run Rename toggle.c to toggle_api.c, and the binary it builds, after the toggle_api test that uses it, like the other toggle test programs. Rename the tests to match their programs (toggle_dlopen, toggle_mpi_np_1/2, toggle_multi_host, toggle_threads) and drop "plugin" from the remaining names (toggle_events_without_vpid). Co-Authored-By: Claude Opus 5.5 Signed-off-by: Thilina Ratnayaka --- integration_tests/toggle.bats | 30 +++++++++---------- integration_tests/{toggle.c => toggle_api.c} | 0 ...name.c => toggle_multi_host_gethostname.c} | 0 3 files changed, 15 insertions(+), 15 deletions(-) rename integration_tests/{toggle.c => toggle_api.c} (100%) rename integration_tests/{toggle_fake_hostname.c => toggle_multi_host_gethostname.c} (100%) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 2000b47fa..8b9253054 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -12,8 +12,8 @@ get_unique_jobid() { @test "toggle_api" { 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 + cc ${THAPI_INCFLAGS} ./integration_tests/toggle_api.c -o toggle_api ${THAPI_LDFLAGS} + iprof --trace-output toggle_traces --no-analysis -- ./toggle_api # Make sure auto_stop comes before stop. babeltrace_thapi ./toggle_traces | awk 'BEGIN { seen_auto = 0 } @@ -33,7 +33,7 @@ get_unique_jobid() { [ "$auto_stop_count" -eq 1 ] } -@test "toggle_api_dlopen" { +@test "toggle_dlopen" { rm -rf toggle_traces 2>/dev/null cc ./integration_tests/toggle_dlopen.c -o toggle_dlopen -ldl @@ -76,13 +76,13 @@ count_processes() { grep -oP '\d+(?= Processes)' || echo 0 } -@test "toggle_plugin_mpi_np_1" { +@test "toggle_mpi_np_1" { [ "$(iprof_toggle_mpi 1 0 | count_calls Total)" -eq 2 ] [ "$(iprof_toggle_mpi 1 1 | count_calls Total)" -eq 1 ] [ "$(iprof_toggle_mpi 1 2 | count_calls Total)" -eq 0 ] } -@test "toggle_plugin_mpi_np_2" { +@test "toggle_mpi_np_2" { [ "$(iprof_toggle_mpi 2 0 | count_processes)" -eq 2 ] [ "$(iprof_toggle_mpi 2 1 | count_processes)" -eq 1 ] [ "$(iprof_toggle_mpi 2 2 | count_processes)" -eq 0 ] @@ -109,18 +109,18 @@ lttng_start() ( # fd 2 (stderr). exec 3>&1 1>&2 - local fake_hostname_so=${BATS_TEST_TMPDIR}/toggle_fake_hostname.so - if [ ! -f "${fake_hostname_so}" ]; then - cc -shared -fPIC ./integration_tests/toggle_fake_hostname.c -o \ - "${fake_hostname_so}" + local hostname_so=${BATS_TEST_TMPDIR}/toggle_multi_host_gethostname.so + if [ ! -f "${hostname_so}" ]; then + cc -shared -fPIC ./integration_tests/toggle_multi_host_gethostname.c -o \ + "${hostname_so}" fi mkdir -p "${home}" # The session daemon takes the trace's hostname from gethostname() when # the session is created, and LTTng has no option to set it. Preloading - # toggle_fake_hostname.c overrides gethostname() in this daemon to return - # the value of THAPI_FAKE_HOSTNAME when it is set. - LTTNG_HOME="${home}" LD_PRELOAD="${fake_hostname_so}" \ + # toggle_multi_host_gethostname.c overrides gethostname() in this daemon + # to return the value of THAPI_FAKE_HOSTNAME when it is set. + LTTNG_HOME="${home}" LD_PRELOAD="${hostname_so}" \ THAPI_FAKE_HOSTNAME="host$1" lttng-sessiond --daemonize --quiet LTTNG_HOME="${home}" lttng create toggle_test -o "${out}" LTTNG_HOME="${home}" lttng enable-channel --userspace --blocking-timeout=inf \ @@ -166,7 +166,7 @@ mpi_tracer_env() { # 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, which records its own hostname (see lttng_start). -@test "toggle_plugin_multi_host" { +@test "toggle_multi_host" { mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_multi_host.c -o \ toggle_multi_host ${THAPI_LDFLAGS} @@ -191,7 +191,7 @@ mpi_tracer_env() { # 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" { +@test "toggle_events_without_vpid" { mpicc ./integration_tests/mpi_helloworld.c -o mpi_helloworld home=$(lttng_start 0 no-context) @@ -208,7 +208,7 @@ mpi_tracer_env() { # 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" { +@test "toggle_threads" { rm -rf toggle_traces 2>/dev/null mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_threads.c \ diff --git a/integration_tests/toggle.c b/integration_tests/toggle_api.c similarity index 100% rename from integration_tests/toggle.c rename to integration_tests/toggle_api.c diff --git a/integration_tests/toggle_fake_hostname.c b/integration_tests/toggle_multi_host_gethostname.c similarity index 100% rename from integration_tests/toggle_fake_hostname.c rename to integration_tests/toggle_multi_host_gethostname.c From 6f1749e77ba2c1bda098dc9d84266c18a9946d92 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Sun, 11 Oct 2026 11:06:07 -0700 Subject: [PATCH 60/64] Remove duplicate version checks left by the rebase Rebasing onto Blob v2 (#543) kept both this branch's `babeltrace2 >= 2.0` / `lttng-ust >= 2.12.8` checks and devel's `babeltrace2 >= 2.1` / `lttng-ust >= 2.16` ones. The later checks silently won, so drop the older ones. lttng-ust 2.16 already includes the 2.12.8 fix that __attribute__((constructor)) needs, so move that comment to the remaining lttng-ust check. Co-Authored-By: Claude Opus 5.5 --- configure.ac | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/configure.ac b/configure.ac index 057ec5834..d2305d827 100644 --- a/configure.ac +++ b/configure.ac @@ -125,14 +125,12 @@ 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]) -# 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 PKG_CHECK_MODULES([BABELTRACE2], [babeltrace2 >= 2.1]) +# Also for blob support. This covers `lttng-ust >= 2.12.8` as well, which +# __attribute__((constructor)) needs to work properly, specifically this fix: +# https://github.com/lttng/lttng-ust/commit/a8fafb675a9f580f6a889223e26664ea11cb0c99. PKG_CHECK_MODULES([LTTNG_UST], [lttng-ust >= 2.16]) AX_RUBY_EXTENSION([nokogiri], [yes]) From f27b5fb266f3f2145f2980df928790636b25f427 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Sun, 11 Oct 2026 11:08:23 -0700 Subject: [PATCH 61/64] Test that the toggle filter consumes all toggle events Metababel drops every event that has a registered callback after running it, so the lttng_ust_toggle start, stop and auto_stop events never reach push_downstream and are never forwarded: start and stop are treated alike. Check that the raw trace has start and stop events, and that with --toggle-on the MPI calls are kept but no toggle event is. Co-Authored-By: Claude Opus 5.5 --- integration_tests/toggle.bats | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 8b9253054..899c53f50 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -88,6 +88,30 @@ count_processes() { [ "$(iprof_toggle_mpi 2 2 | count_processes)" -eq 0 ] } +# The toggle filter consumes the lttng_ust_toggle events (start, stop and +# auto_stop alike) in their callbacks: none of them is forwarded downstream, +# while the MPI calls between thapi_start() and thapi_stop() are. +@test "toggle_events_consumed" { + 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 --no-analysis --trace-output toggle_traces -- ./toggle_mpi 0 + + # Without the toggle filter, the trace has the start and stop events. + run babeltrace_thapi trace ./toggle_traces + [ "$status" -eq 0 ] + [[ "$output" == *lttng_ust_toggle:start* ]] + [[ "$output" == *lttng_ust_toggle:stop* ]] + + # With it, the MPI calls are kept, but none of the toggle events. + run babeltrace_thapi trace --toggle-on ./toggle_traces + [ "$status" -eq 0 ] + [[ "$output" == *lttng_ust_mpi:MPI_Comm_rank_entry* ]] + [[ "$output" != *lttng_ust_toggle:* ]] +} + # The tests below trace with hand-made LTTng sessions instead of iprof, to # control what iprof always sets up. Session name ($1, MPI rank id in these # tests) gets its own session daemon, with LTTNG_HOME set to From 9e455bbdd8d593df5f725254b9261ca89a69892a Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Sun, 11 Oct 2026 11:10:04 -0700 Subject: [PATCH 62/64] Look up the toggle state without inserting into the map push_downstream read the toggle state with std::map::operator[], which adds a false entry for every thread whose events it sees before any toggle event of its own. Use find() instead, treating a missing entry as not traced, so the map only holds threads with toggle events. Co-Authored-By: Claude Opus 5.5 --- utils/thapi_toggle_callbacks.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/utils/thapi_toggle_callbacks.cpp b/utils/thapi_toggle_callbacks.cpp index 98e71fe71..aa3413153 100644 --- a/utils/thapi_toggle_callbacks.cpp +++ b/utils/thapi_toggle_callbacks.cpp @@ -63,7 +63,10 @@ static void push_downstream(void *btx_handle, void *maps, const bt_message *msg) 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, vtid_v}; - push_msg = map[key]; + /* A thread with no toggle event yet is not traced. find() rather than + * operator[], so that looking it up does not add it to the map. */ + auto it = map.find(key); + push_msg = it != map.end() && it->second; } } From 68368a2644732cb145b42a1aa5097bbbea804de3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Sun, 11 Oct 2026 11:14:42 -0700 Subject: [PATCH 63/64] Check that LTTNG_UST_CONSTRUCTOR_PRIO + 1 is a valid priority thapi_auto_stop runs right after lttng-ust's constructors, with priority LTTNG_UST_CONSTRUCTOR_PRIO + 1. lttng-ust defines it as 150, but it can be overridden, and constructor priorities must be at most 65535. Fail with a clear message instead of the compiler's if it is too large. Co-Authored-By: Claude Opus 5.5 --- utils/thapi_toggle.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/utils/thapi_toggle.c b/utils/thapi_toggle.c index 27a9a815c..bd3db403c 100644 --- a/utils/thapi_toggle.c +++ b/utils/thapi_toggle.c @@ -5,6 +5,10 @@ #error "LTTNG_UST_CONSTRUCTOR_PRIO is not defined." #endif +#if LTTNG_UST_CONSTRUCTOR_PRIO >= 65535 +#error "LTTNG_UST_CONSTRUCTOR_PRIO + 1 is not a valid constructor priority." +#endif + void thapi_start(void) { tracepoint(lttng_ust_toggle, start); } void thapi_stop(void) { tracepoint(lttng_ust_toggle, stop); } From 10ddde767233492283c0cf5a5b94937f64635b77 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Sun, 11 Oct 2026 11:15:37 -0700 Subject: [PATCH 64/64] Document the toggle_mpi variants and the case 0 fall-through Variant 0 deliberately falls through to variant 1, so that every rank turns tracing on before the MPI calls. Say so, and what each variant traces, so the fall-through does not look like a missing break. Co-Authored-By: Claude Opus 5.5 --- integration_tests/toggle_mpi.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/integration_tests/toggle_mpi.c b/integration_tests/toggle_mpi.c index 99a431331..146bc1e80 100644 --- a/integration_tests/toggle_mpi.c +++ b/integration_tests/toggle_mpi.c @@ -3,6 +3,13 @@ #include +/* Variants, checked by the toggle_mpi_np_* tests in toggle.bats: + * 0: every rank turns tracing on first, so MPI_Comm_rank and MPI_Comm_size are + * traced on every rank. + * 1: only rank 0 turns tracing on, after MPI_Comm_rank, so only its + * MPI_Comm_size is traced. + * Any other value: tracing is never turned on, so nothing is traced. */ + int main(int argc, char *argv[]) { int variant = (argc > 1) ? atoi(argv[1]) : 0; @@ -13,6 +20,9 @@ int main(int argc, char *argv[]) { switch (variant) { case 0: thapi_start(); + /* Variant 0 makes the same calls as variant 1, with tracing already on. + * Rank 0's second thapi_start() leaves it on. */ + /* fall through */ case 1: MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (rank == 0)