From fe7705de36c8398925d3ac64b42a69526e14f28d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Fri, 28 Feb 2025 01:56:20 +0000 Subject: [PATCH 01/48] 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 8e27fa9c3..f5487d633 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -43,6 +43,22 @@ dlinfo_wrapper_CFLAGS = -Wall -Wextra 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 8bfe33effb8a1285f7a3c04537ab64acab788d7b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 3 Mar 2025 22:02:51 +0000 Subject: [PATCH 02/48] 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 037a55520..f5270192f 100644 --- a/configure.ac +++ b/configure.ac @@ -137,6 +137,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 3d0cf51def724a5c8e30dcab587c4167bcc0a91a Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 3 Mar 2025 22:40:31 +0000 Subject: [PATCH 03/48] 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 f5487d633..3a71c359a 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -43,21 +43,23 @@ dlinfo_wrapper_CFLAGS = -Wall -Wextra 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 a066c989336120b3d4206739c068740aafd55960 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 3 Mar 2025 22:46:54 +0000 Subject: [PATCH 04/48] 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 c8aa8f6d0d8a4100c0d5f59f9df69430fe7e89d5 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 01:13:56 +0000 Subject: [PATCH 05/48] Fix `make distcheck` failure --- utils/Makefile.am | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/Makefile.am b/utils/Makefile.am index 3a71c359a..fde37d599 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -92,6 +92,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 736ffd5bb000cff00c327e42f68cd0874f988f67 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 01:30:59 +0000 Subject: [PATCH 06/48] 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 fde37d599..51885a64e 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -43,23 +43,23 @@ dlinfo_wrapper_CFLAGS = -Wall -Wextra 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 @@ -92,7 +92,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 a9432ef65d7bfe7a4d3f4e3628e4d4b69c39c70c Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 02:14:25 +0000 Subject: [PATCH 07/48] 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 f5270192f..13ee0f30e 100644 --- a/configure.ac +++ b/configure.ac @@ -137,22 +137,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 c9d31672e2f7c40d20c925009e94a6509c236237 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 02:43:35 +0000 Subject: [PATCH 08/48] 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 51885a64e..f41baaf8a 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -45,9 +45,6 @@ bin_PROGRAMS += thapi_metadata include_HEADERS = thapi.h -Thapidir = $(pkglibdir) -Thapi_LTLIBRARIES = libThapi.la - BUILT_SOURCES += \ thapi_tracepoints.h \ thapi_tracepoints.c @@ -57,10 +54,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 dc20cad3d5fe9869f2426856cba12410b29f39c9 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 15:24:03 +0000 Subject: [PATCH 09/48] 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 cb33996c3..38add1110 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -103,3 +103,15 @@ bats_require_minimum_version 1.5.0 run bats_pipe echo "FOO" \| iprof cat [[ "$output" =~ "FOO" ]] } + +@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 6127ca8aa30781f45e088203869c06dc27b3d542 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 6 Mar 2025 17:27:53 +0000 Subject: [PATCH 10/48] 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 fc11f1bae8e76650b7992632b8acee269ffaf6a7 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 02:56:32 +0000 Subject: [PATCH 11/48] 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 38add1110..6e4ccaf76 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -107,11 +107,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 f9ec28b216ba06c324168085e4151bbb4ce45247 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 14:24:33 +0000 Subject: [PATCH 12/48] 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 f41baaf8a..8f7004a43 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -53,7 +53,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 a0e4020dc99116343508652dd5c893ae10a0fd6e Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 14:38:51 +0000 Subject: [PATCH 13/48] 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 19433f5b522cc27f3e40a0b7a337a294d8fa0cad Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 18 Mar 2025 14:46:40 +0000 Subject: [PATCH 14/48] 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 13ee0f30e..708e2ba31 100644 --- a/configure.ac +++ b/configure.ac @@ -102,7 +102,7 @@ AC_SUBST([ENABLE_CLANG_PARSER]) 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([cast-to-yaml], [yes]) From 98067535e5e4ccece75ef4f72b86275eda4be429 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 25 Mar 2025 22:38:13 +0000 Subject: [PATCH 15/48] 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 b7338ab4a5a9f6ee5a350b296aed6ed53f465190 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Apr 2025 04:32:55 +0000 Subject: [PATCH 16/48] 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 8f7004a43..672d88856 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -45,19 +45,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 @@ -78,6 +97,7 @@ CLEANFILES = \ version \ optparse_thapi.rb \ lttng/tracepoint_gen.h \ + $(BTX_THAPI_GENERATED) \ $(BUILT_SOURCES) EXTRA_DIST = \ @@ -91,6 +111,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 2c695ed4c54893d1e4700f62cd9eec3393f06fb7 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Apr 2025 06:10:06 +0000 Subject: [PATCH 17/48] 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 26d88226655a474246fd1b969b89426998270728 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 24 Jun 2025 16:09:42 +0000 Subject: [PATCH 18/48] 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 6e4ccaf76..649520038 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -104,10 +104,10 @@ bats_require_minimum_version 1.5.0 [[ "$output" =~ "FOO" ]] } -@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 672d88856..afc17da54 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -18,7 +18,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 @@ -47,36 +48,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 @@ -97,7 +99,7 @@ CLEANFILES = \ version \ optparse_thapi.rb \ lttng/tracepoint_gen.h \ - $(BTX_THAPI_GENERATED) \ + $(BTX_THAPI_TOGGLE_GENERATED) \ $(BUILT_SOURCES) EXTRA_DIST = \ @@ -110,8 +112,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 8b48d04c5b71bdee3efc2cbecd5004085329f735 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 24 Jun 2025 16:10:12 +0000 Subject: [PATCH 19/48] 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 afc17da54..e46372ce3 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -64,16 +64,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 1826c349f620d7f6fef8dd6c19b3c57b9fee29db Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 1 Jul 2025 19:31:49 +0000 Subject: [PATCH 20/48] 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 e46372ce3..1d8b89fdf 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -64,17 +64,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 afcb851b8da1671f6929c76f2d9393e3f6188d0b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 1 Jul 2025 16:36:56 +0000 Subject: [PATCH 21/48] 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 649520038..dbd91a778 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -115,3 +115,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 677493eacfc5dd468f2538183720ec5d4c206a12 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Jul 2025 04:25:18 +0000 Subject: [PATCH 22/48] 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 dbd91a778..b3b42b766 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -145,3 +145,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 f00be3bf47495245357002729a2b18923399247d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 2 Jul 2025 04:32:29 +0000 Subject: [PATCH 23/48] 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 b3b42b766..0cbda7904 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -104,28 +104,37 @@ bats_require_minimum_version 1.5.0 [[ "$output" =~ "FOO" ]] } -@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 } @@ -133,35 +142,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 a18655ecc2a3b3c051d7956baeea7d69f78bdc5c Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 3 Jul 2025 16:31:25 +0000 Subject: [PATCH 24/48] Move toggle tests into toggle.bats --- integration_tests/general.bats | 64 -------------------------- integration_tests/toggle.bats | 84 ++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 64 deletions(-) create mode 100644 integration_tests/toggle.bats diff --git a/integration_tests/general.bats b/integration_tests/general.bats index 0cbda7904..cb33996c3 100644 --- a/integration_tests/general.bats +++ b/integration_tests/general.bats @@ -103,67 +103,3 @@ bats_require_minimum_version 1.5.0 run bats_pipe echo "FOO" \| iprof cat [[ "$output" =~ "FOO" ]] } - -@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 ] -} 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 fd77484f4faa16edad9ed4e67910246fb6f0ef6c Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 3 Jul 2025 16:47:27 +0000 Subject: [PATCH 25/48] 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 5008d215c177925455330007e63501b3be81c552 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 3 Jul 2025 17:22:50 +0000 Subject: [PATCH 26/48] 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 277ea776f4771e693ad8be58b3e1ca124a0a2d8d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 14 Jul 2025 17:31:51 +0000 Subject: [PATCH 27/48] 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 b72687ecd329d1020b0d4479d0de20f4fc35a89a Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 14 Jul 2025 18:33:15 +0000 Subject: [PATCH 28/48] 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 1d8b89fdf..45c4b4778 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -65,7 +65,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 8db67db68b01bb65438b49ed11352abcb7d7d4de Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 14 Jul 2025 18:34:44 +0000 Subject: [PATCH 29/48] 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 8f2fba3df1577b556511270ea2c6a70d25d6a18f Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 15 Jul 2025 17:02:04 +0000 Subject: [PATCH 30/48] 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 45c4b4778..d9c64b5ae 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -18,8 +18,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 f3de4afa630c9b82f453c1a3c2e482873e8b14f9 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 15 Jul 2025 20:21:02 +0000 Subject: [PATCH 31/48] 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 708e2ba31..90a32c03c 100644 --- a/configure.ac +++ b/configure.ac @@ -102,6 +102,9 @@ AC_SUBST([ENABLE_CLANG_PARSER]) 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 f6e62cfdd6d5159a829cc870f85f3496bec28f12 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 29 Sep 2025 18:52:07 +0000 Subject: [PATCH 32/48] 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 d9c64b5ae..b1d0812fb 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -68,7 +68,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 c8e7cff7359a0dc1deda9e160bfb7ede61370dac Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 1 Oct 2025 01:41:38 +0000 Subject: [PATCH 33/48] 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 b1d0812fb..b6a6e9f29 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -63,10 +63,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 @@ -77,6 +77,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 147c24a59d558624762c3368f9c3d747bfcabd87 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 26 Jan 2026 19:30:43 +0000 Subject: [PATCH 34/48] 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 9fb306d5f317e82f6bd4689a1de9b3fb58c6a67d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 26 Jan 2026 19:53:26 +0000 Subject: [PATCH 35/48] 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 0f96b479a29167dccdca1a4cfc19757d577c54f5 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 26 Jan 2026 23:39:06 +0000 Subject: [PATCH 36/48] 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 3fef8a6a2757534333af08565f4ac2691278920e Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 27 Jan 2026 21:28:07 +0000 Subject: [PATCH 37/48] $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 0594d60e1fc06663e8561bb546aa9e7a1f1d0c0b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 28 Jan 2026 22:02:22 +0000 Subject: [PATCH 38/48] 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 ede27ab99..3b2e9d6ce 100755 --- a/xprof/xprof.rb.in +++ b/xprof/xprof.rb.in @@ -656,6 +656,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 @@ -739,7 +744,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 3334b62243bff0f9ef52230f6b8e4019437893fa Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 29 Jan 2026 20:56:18 +0000 Subject: [PATCH 39/48] $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 618e70b56b31735174391034e9c46514790c1aa4 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Sun, 1 Feb 2026 04:48:36 +0000 Subject: [PATCH 40/48] 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 635451f092dfe8c6c7b4db5a10090e400dac980d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Tue, 3 Feb 2026 20:50:40 +0000 Subject: [PATCH 41/48] 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 7ae17f335637731bc9124f4775d0be747b040d81 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Mon, 2 Feb 2026 13:29:07 -0600 Subject: [PATCH 42/48] 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 62e0a30036008e350b82e3f29a68737eb9822fb1 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 5 Feb 2026 23:14:26 +0000 Subject: [PATCH 43/48] 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 d44105b9344818bf7a446070064f841f9f02225b Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 5 Feb 2026 23:15:49 +0000 Subject: [PATCH 44/48] 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 35a6aed1cc9ec18f776118d6e37041e9fdd6f0ab Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Thu, 5 Feb 2026 23:21:16 +0000 Subject: [PATCH 45/48] 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 345e239c3a45d12e7774046c6391e53df7ba95c1 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 4 Mar 2026 18:56:46 +0000 Subject: [PATCH 46/48] 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 7e558990e5e8f30f327d1519aad520b54dbca5a0 Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 4 Mar 2026 20:03:50 +0000 Subject: [PATCH 47/48] 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 a785e65088ef83d7c07bc33de2f5632a9ce89d7d Mon Sep 17 00:00:00 2001 From: Thilina Ratnayaka Date: Wed, 4 Mar 2026 20:33:38 +0000 Subject: [PATCH 48/48] 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 8a7b9a9db..2365fb1d1 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'] })