From ac2be887829e89b197833282c73bdab1f1d3aeeb Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Fri, 28 Feb 2025 01:56:20 +0000 Subject: [PATCH 01/54] 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 c3b96593a..d4460ce5c 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 ba3fbfb5fc9a837c4f44a7a133f4a5503643eaa5 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 3 Mar 2025 22:02:51 +0000 Subject: [PATCH 02/54] 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 b62685827..9cb83b5d2 100644 --- a/configure.ac +++ b/configure.ac @@ -138,6 +138,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 6bce142c1e24e5793330402bc9853ee4ad1d6fd7 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 3 Mar 2025 22:40:31 +0000 Subject: [PATCH 03/54] 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 d4460ce5c..bff62ce2c 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 a7fbbd288506454323f5345b1e8c40924e9bc720 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 3 Mar 2025 22:46:54 +0000 Subject: [PATCH 04/54] 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 3fa640e44b9bad5936bf5285afff891e2de42376 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 01:13:56 +0000 Subject: [PATCH 05/54] Fix `make distcheck` failure --- utils/Makefile.am | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/Makefile.am b/utils/Makefile.am index bff62ce2c..2b709e32a 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -93,6 +93,7 @@ EXTRA_DIST = \ gen_library_base.rb \ dump_trace_format.rb \ thapi_metadata_tracepoints.tp \ + thapi_profiler_tracepoints.tp \ command.rb \ meta_parameters.rb \ optparse_thapi.rb \ From aa43ce2729595b9f6db297ab2536cce3fc1410fd Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 01:30:59 +0000 Subject: [PATCH 06/54] 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 2b709e32a..654be2c5c 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 @@ -93,7 +93,7 @@ EXTRA_DIST = \ gen_library_base.rb \ dump_trace_format.rb \ thapi_metadata_tracepoints.tp \ - thapi_profiler_tracepoints.tp \ + thapi_tracepoints.tp \ command.rb \ meta_parameters.rb \ optparse_thapi.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 7648e790e5a33aac549e0a3da457150a4861e012 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 02:14:25 +0000 Subject: [PATCH 07/54] 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 9cb83b5d2..16860cbdb 100644 --- a/configure.ac +++ b/configure.ac @@ -138,22 +138,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 feb3163040cc49d52dd86e0dba683940248b0465 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 02:43:35 +0000 Subject: [PATCH 08/54] 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 654be2c5c..407a7460c 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 f4dc0ba1bcb88706076cba36d92a910a1f4b6620 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 15:24:03 +0000 Subject: [PATCH 09/54] 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 c0976f891cd8080d8fb2a56fc76d0751ca29a532 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 17:27:53 +0000 Subject: [PATCH 10/54] 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 b5a9d86c9d761904058f16ee95cc897fe47cfad8 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 02:56:32 +0000 Subject: [PATCH 11/54] 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 21d4b0a24dc2c1c78b33f24e322e55305b3fded3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 14:24:33 +0000 Subject: [PATCH 12/54] 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 407a7460c..58145d75d 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 358678996a5044c0d6e441a07af13aa898c49cb9 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 14:38:51 +0000 Subject: [PATCH 13/54] 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 1895aaa53eba881978e8eee4bf5829b0b539e74f Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 14:46:40 +0000 Subject: [PATCH 14/54] Set minimum lttng-ust version We need one which supports __attribute__((constructor)) in user code. --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 16860cbdb..bcd3013ad 100644 --- a/configure.ac +++ b/configure.ac @@ -104,7 +104,7 @@ AX_COMPARE_VERSION([$H2YAML_VERSION], [ge], [$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.10]) +PKG_CHECK_MODULES([LTTNG_UST], [lttng-ust >= 2.12.8]) PKG_CHECK_MODULES([PROTOBUF], [protobuf >= 3.0]) AX_RUBY_EXTENSION([nokogiri], [yes]) From bfabb96b6c257b98cc59bd1929990be9a1cd4fb3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 25 Mar 2025 22:38:13 +0000 Subject: [PATCH 15/54] 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 d6496ffb02ae591e87d894282cd7f5f859da9885 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Apr 2025 04:32:55 +0000 Subject: [PATCH 16/54] 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 58145d75d..66ee72ee3 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 = \ version \ optparse_thapi.rb \ lttng/tracepoint_gen.h \ + $(BTX_THAPI_GENERATED) \ $(BUILT_SOURCES) EXTRA_DIST = \ @@ -92,6 +112,7 @@ EXTRA_DIST = \ dump_trace_format.rb \ thapi_metadata_tracepoints.tp \ thapi_tracepoints.tp \ + btx_thapi.yaml \ command.rb \ meta_parameters.rb \ optparse_thapi.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 96bdf34d0e38737e6bf1e9e7425f546a147c053a Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Apr 2025 06:10:06 +0000 Subject: [PATCH 17/54] 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 e051963ba7a39a486b63a62940c60a46102fd920 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 24 Jun 2025 16:09:42 +0000 Subject: [PATCH 18/54] 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 66ee72ee3..63e3fead8 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 = \ version \ optparse_thapi.rb \ lttng/tracepoint_gen.h \ - $(BTX_THAPI_GENERATED) \ + $(BTX_THAPI_TOGGLE_GENERATED) \ $(BUILT_SOURCES) EXTRA_DIST = \ @@ -111,8 +113,8 @@ EXTRA_DIST = \ gen_library_base.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 \ meta_parameters.rb \ optparse_thapi.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 b944e69f4396eb68c23e1d025db17d64cf71623b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 24 Jun 2025 16:10:12 +0000 Subject: [PATCH 19/54] 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 63e3fead8..7bfbc9b9e 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 541e19bf81a64271ab98e962dda4a4a284a5edd0 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 1 Jul 2025 19:31:49 +0000 Subject: [PATCH 20/54] 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 7bfbc9b9e..a01f56ce1 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 c9fddfaa9ba3df873e38d9e4786ce59711aee34a Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 1 Jul 2025 16:36:56 +0000 Subject: [PATCH 21/54] 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 8089b3fe57ee7634b0be46b0902d1e3534b6c83b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Jul 2025 04:25:18 +0000 Subject: [PATCH 22/54] 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 e488928821e8b2629909574dbd359b93ba0ae5c3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Jul 2025 04:32:29 +0000 Subject: [PATCH 23/54] 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 453c1e76cfb61e6549936b61a06a0d64ca6cfd08 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 3 Jul 2025 16:31:25 +0000 Subject: [PATCH 24/54] 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 876d1b219feefb9f6eb0acb583dabca205c1f17a Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 3 Jul 2025 16:47:27 +0000 Subject: [PATCH 25/54] 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 3fa33a1f2ad6dc96362836e1570185cd886c83f3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 3 Jul 2025 17:22:50 +0000 Subject: [PATCH 26/54] 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 f4360195ac17d46bf48c0d18a77f9c15c0bd98cb Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 14 Jul 2025 17:31:51 +0000 Subject: [PATCH 27/54] 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 1bbcccbe29671be6fefa5c21c059b2dfe54cbb94 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 14 Jul 2025 18:33:15 +0000 Subject: [PATCH 28/54] 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 a01f56ce1..659d5896c 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 944ba7c7c0ae154edfac73235febbb4270367ad0 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 14 Jul 2025 18:34:44 +0000 Subject: [PATCH 29/54] 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 3a8be59d3fbd245a7a4b549a57ee3932bcaba06d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 15 Jul 2025 17:02:04 +0000 Subject: [PATCH 30/54] 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 659d5896c..f405e3009 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 50a81fbcb7f9b9567c170700bae3e1b7fa79f4bc Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 15 Jul 2025 20:21:02 +0000 Subject: [PATCH 31/54] 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 bcd3013ad..3ade3a1b0 100644 --- a/configure.ac +++ b/configure.ac @@ -104,6 +104,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]) From f1bff63c43eaece7a3593254d47f384ad663e0df Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 29 Sep 2025 18:52:07 +0000 Subject: [PATCH 32/54] 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 f405e3009..696a0eeff 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 f73737e35d3a05fbdc39c674146a9b174c7422ab Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 1 Oct 2025 01:41:38 +0000 Subject: [PATCH 33/54] 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 696a0eeff..85c4b5c95 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 421b849f0960f3aae846f1c0c78bc2443c7c53b3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 26 Jan 2026 19:30:43 +0000 Subject: [PATCH 34/54] 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 17f57a66ba7624f9e2cfb12db1a81babb8f6f7a8 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 26 Jan 2026 19:53:26 +0000 Subject: [PATCH 35/54] 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 e05b8773336de68fa6123c6b578dce418c6db06b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 26 Jan 2026 23:39:06 +0000 Subject: [PATCH 36/54] 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 f5ed08a9a5b6589b1d68f569c8cc20344df06dfd Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 27 Jan 2026 21:28:07 +0000 Subject: [PATCH 37/54] $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 cdbc653fedf31d6250243aaf4bd6788dc03a6af9 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 28 Jan 2026 22:02:22 +0000 Subject: [PATCH 38/54] 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 95af63b0d..5efe86043 100755 --- a/xprof/xprof.rb.in +++ b/xprof/xprof.rb.in @@ -653,6 +653,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 @@ -738,7 +743,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 4a6a04a741db18ed45bb02d44f6b0604d5f27ade Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 29 Jan 2026 20:56:18 +0000 Subject: [PATCH 39/54] $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 0fc73e65281d152e2102a87ff597fdbfc16d1a6e Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Sun, 1 Feb 2026 04:48:36 +0000 Subject: [PATCH 40/54] 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 e14ec5328b353b8f2057c730df7836a7e41af35d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 3 Feb 2026 20:50:40 +0000 Subject: [PATCH 41/54] 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 57baf73790b16979456f5d23b2ab7fedf9e53fae Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 2 Feb 2026 13:29:07 -0600 Subject: [PATCH 42/54] Add a dlopen test for toggle traces --- integration_tests/setup_suite.bash | 3 +++ integration_tests/toggle.bats | 18 +++++++++++++-- integration_tests/toggle.c | 3 ++- integration_tests/toggle_dlopen.c | 35 ++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 integration_tests/toggle_dlopen.c diff --git a/integration_tests/setup_suite.bash b/integration_tests/setup_suite.bash index 96a616436..1f97886da 100644 --- a/integration_tests/setup_suite.bash +++ b/integration_tests/setup_suite.bash @@ -3,7 +3,10 @@ setup_suite() { export MPIRUN=${MPIRUN:-mpirun} + # Set the path to find iprof, babeltrace_thapi, etc. export PATH=$(pkg-config --variable=bindir thapi):${PATH} + # We need this for the toggle_api/toggle_dlopen test. + export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(pkg-config --variable=libdir thapi) missing_tools=() diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 1a64a38a9..8315b4e42 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -10,10 +10,9 @@ get_unique_jobid() { } @test "toggle_api" { - rm -rf toggle_traces 2>/dev/null - cc ${THAPI_INCFLAGS} ./integration_tests/toggle.c -o toggle ${THAPI_LDFLAGS} + rm -rf toggle_traces 2>/dev/null iprof --trace-output toggle_traces --no-analysis -- ./toggle dir=$(ls -d -1 ./toggle_traces/*/) @@ -33,6 +32,21 @@ get_unique_jobid() { auto_stop_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:auto_stop | wc -l) [ "$auto_stop_count" -eq 1 ] + cc ./integration_tests/toggle_dlopen.c -o toggle_dlopen -ldl + + rm -rf toggle_traces 2>/dev/null + iprof --trace-output toggle_traces --no-analysis -- ./toggle_dlopen + dir=$(ls -d -1 ./toggle_traces/*/) + + # Check expected trace counts. + start_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:start | wc -l) + [ "$start_count" -eq 2 ] + + stop_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:stop | wc -l) + [ "$stop_count" -eq 2 ] + + auto_stop_count=$(babeltrace_thapi -c $dir | grep lttng_ust_toggle:auto_stop | wc -l) + [ "$auto_stop_count" -eq 2 ] } toggle_count_base() { diff --git a/integration_tests/toggle.c b/integration_tests/toggle.c index bc1e1e900..c3243230f 100644 --- a/integration_tests/toggle.c +++ b/integration_tests/toggle.c @@ -1,6 +1,7 @@ #include -int main(int argc, char *argv[]) { +int main(void) { thapi_start(); thapi_stop(); + return 0; } diff --git a/integration_tests/toggle_dlopen.c b/integration_tests/toggle_dlopen.c new file mode 100644 index 000000000..f50f5a2cd --- /dev/null +++ b/integration_tests/toggle_dlopen.c @@ -0,0 +1,35 @@ +#include +#include +#include + +#define check_error(ptr_) \ + { \ + void *ptr = (void *)ptr_; \ + if (!ptr) { \ + printf("%s:%d -- %s\n", __FILE__, __LINE__, dlerror()); \ + return 1; \ + } \ + } + +int main(void) { + dlerror(); + + for (int i = 0; i < 2; i++) { + void *thapi = dlopen("libThapi.so", RTLD_NOW | RTLD_LOCAL); + check_error(thapi); + + void (*start)(void) = (void (*)(void))dlsym(thapi, "thapi_start"); + check_error(start); + + void (*stop)(void) = (void (*)(void))dlsym(thapi, "thapi_stop"); + check_error(stop); + + (*start)(), (*stop)(); + + dlclose(thapi); + } + + return 0; +} + +#undef check_error From e59e4f70d66e79fa4eb36f8a926ee5a6a0f9c62b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 5 Feb 2026 23:14:26 +0000 Subject: [PATCH 43/54] 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 9dfae45eca7f6a079bb6421c522306103fa67249 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 5 Feb 2026 23:15:49 +0000 Subject: [PATCH 44/54] 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 2b902db334b5ccd084f0d255d5a5ffd5add2f1db Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 5 Feb 2026 23:21:16 +0000 Subject: [PATCH 45/54] 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 7d0d97526e7b590e8a0baa3e1154b7a41331c351 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 4 Mar 2026 18:56:46 +0000 Subject: [PATCH 46/54] 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 67a819f5905bcea6764f12a07ffbe66c3d444384 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 4 Mar 2026 20:03:50 +0000 Subject: [PATCH 47/54] 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 6d77aed679419ed6f5ec64d48d68196fe5eeea56 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 4 Mar 2026 20:33:38 +0000 Subject: [PATCH 48/54] 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 111b0768a..6afeae0c7 100755 --- a/utils/babeltrace_thapi.in +++ b/utils/babeltrace_thapi.in @@ -251,6 +251,8 @@ def get_and_add_components(graph, names, l_inputs) 'offset' => $options[:'output-offset'] }) 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 fc5838d9cd16bbe8d807ba2eb50a3d7b23514ccf Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 14 Jul 2026 15:24:53 +0000 Subject: [PATCH 49/54] 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 f6dcf9b11d3abb8b4803862c94bb9c68c4fd25e3 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 14 Jul 2026 15:59:05 +0000 Subject: [PATCH 50/54] 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 5efe86043..0d6ed8ec9 100755 --- a/xprof/xprof.rb.in +++ b/xprof/xprof.rb.in @@ -655,6 +655,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 @@ -743,7 +745,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 1468e31143586ac36d7966f8efde8fbf0131473d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 15 Jul 2026 21:06:16 +0000 Subject: [PATCH 51/54] 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 7e2f938b93de0f23ef9ee9b871514c407ed39815 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 16 Jul 2026 02:01:59 +0000 Subject: [PATCH 52/54] 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 6afeae0c7..372106465 100755 --- a/utils/babeltrace_thapi.in +++ b/utils/babeltrace_thapi.in @@ -318,6 +318,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', @@ -402,6 +407,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, 'version')) exit diff --git a/xprof/xprof.rb.in b/xprof/xprof.rb.in index 0d6ed8ec9..a552ce1da 100755 --- a/xprof/xprof.rb.in +++ b/xprof/xprof.rb.in @@ -693,6 +693,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] @@ -841,16 +842,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 @@ -864,6 +868,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 @@ -1052,6 +1057,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']) 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 547ce954aa15c6093870060c8cf7b7f6af6f308b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 16 Jul 2026 02:48:45 +0000 Subject: [PATCH 53/54] 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 2e63a99e5f0becf296820a1f3a25f63ec118f8a8 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 16 Jul 2026 19:17:37 +0000 Subject: [PATCH 54/54] Refactor tests --- integration_tests/setup_suite.bash | 2 -- integration_tests/toggle.bats | 8 ++++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/integration_tests/setup_suite.bash b/integration_tests/setup_suite.bash index 1f97886da..ca742809f 100644 --- a/integration_tests/setup_suite.bash +++ b/integration_tests/setup_suite.bash @@ -5,8 +5,6 @@ setup_suite() { # Set the path to find iprof, babeltrace_thapi, etc. export PATH=$(pkg-config --variable=bindir thapi):${PATH} - # We need this for the toggle_api/toggle_dlopen test. - export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(pkg-config --variable=libdir thapi) missing_tools=() diff --git a/integration_tests/toggle.bats b/integration_tests/toggle.bats index 78e80c115..678dba7da 100644 --- a/integration_tests/toggle.bats +++ b/integration_tests/toggle.bats @@ -3,6 +3,8 @@ setup_file() { export THAPI_INCFLAGS="-I$(pkg-config --variable=includedir thapi)" export THAPI_LDFLAGS="-Wl,-rpath,$(pkg-config --variable=libdir thapi) $(pkg-config --libs thapi)" + # needed for toggle api dlopen test. + export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(pkg-config --variable=libdir thapi) } get_unique_jobid() { @@ -56,6 +58,8 @@ get_unique_jobid() { count_base() { rm -rf toggle_traces 2>/dev/null + mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_mpi.c -o toggle_mpi ${THAPI_LDFLAGS} + THAPI_SYNC_DAEMON=fs THAPI_JOBID=$(get_unique_jobid) timeout 40s mpirun -n $1 \ iprof --toggle-on --trace-output toggle_traces -- ./toggle_mpi $2 } @@ -67,8 +71,6 @@ count_traces() { } @test "toggle_plugin_mpi_np_1" { - mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_mpi.c -o toggle_mpi ${THAPI_LDFLAGS} - count_0=$(count_traces 1 0) count_1=$(count_traces 1 1) count_2=$(count_traces 1 2) @@ -85,8 +87,6 @@ count_processors() { } @test "toggle_plugin_mpi_np_2" { - mpicc ${THAPI_INCFLAGS} ./integration_tests/toggle_mpi.c -o toggle_mpi ${THAPI_LDFLAGS} - count_0=$(count_processors 2 0) count_1=$(count_processors 2 1) count_2=$(count_processors 2 2)