From 412d818269b6e8e5b7eae32f7e245935bd30f4a7 Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Mon, 19 May 2025 13:16:35 +0200 Subject: [PATCH] WIP: backport message flow tracing --- src/CMakeLists.txt | 12 +++ src/core/ddsc/CMakeLists.txt | 9 +++ src/core/ddsc/src/dds_read.c | 2 + src/core/ddsc/src/dds_reader.c | 6 ++ src/core/ddsc/src/dds_write.c | 3 + src/core/ddsc/src/dds_writer.c | 6 ++ src/core/ddsc/src/tracing_lttng.c | 16 ++++ src/core/ddsc/src/tracing_lttng.h | 110 ++++++++++++++++++++++++++ src/ddsrt/src/threads/posix/threads.c | 2 +- src/features.h.in | 3 + 10 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/core/ddsc/src/tracing_lttng.c create mode 100644 src/core/ddsc/src/tracing_lttng.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4cea551..424bd27 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -37,6 +37,18 @@ endif() if(ENABLE_DEADLINE_MISSED) set(DDS_HAS_DEADLINE_MISSED "1") endif() + +find_package(PkgConfig) +if(PkgConfig_FOUND) + pkg_check_modules(LTTNG lttng-ust) + if(LTTNG_FOUND) + message(STATUS "Building with LTTng instrumentation support") + set(DDS_HAS_LTTNG_TRACING "1") + else() + message(FATAL_ERROR "CYCLONE_ENABLE_TRACING requires lttng-ust") + endif() +endif() + configure_file(features.h.in "${CMAKE_CURRENT_BINARY_DIR}/core/include/dds/features.h") add_definitions(-DDDSI_INCLUDE_NETWORK_PARTITIONS -DDDSI_INCLUDE_SSM) diff --git a/src/core/ddsc/CMakeLists.txt b/src/core/ddsc/CMakeLists.txt index cfba755..f8b80d7 100644 --- a/src/core/ddsc/CMakeLists.txt +++ b/src/core/ddsc/CMakeLists.txt @@ -42,6 +42,10 @@ PREPEND(srcs_ddsc "${CMAKE_CURRENT_LIST_DIR}/src" dds_sertopic_builtintopic.c ) +if (DDS_HAS_LTTNG_TRACING) + list(APPEND srcs_ddsc "${CMAKE_CURRENT_LIST_DIR}/src/tracing_lttng.c") +endif() + PREPEND(hdrs_public_ddsc "$$" dds.h ddsc/dds_public_error.h @@ -81,6 +85,7 @@ PREPEND(hdrs_private_ddsc "${CMAKE_CURRENT_LIST_DIR}/src" dds__whc_builtintopic.h dds__serdata_builtintopic.h dds__get_status.h + tracing_lttng.h ) generate_export_header( @@ -108,6 +113,10 @@ target_include_directories(ddsc INTERFACE $) +if (DDS_HAS_LTTNG_TRACING) + target_link_libraries(ddsc PRIVATE ${LTTNG_LIBRARIES}) +endif() + install( DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/include/" diff --git a/src/core/ddsc/src/dds_read.c b/src/core/ddsc/src/dds_read.c index a5c932d..fa0d3f9 100644 --- a/src/core/ddsc/src/dds_read.c +++ b/src/core/ddsc/src/dds_read.c @@ -20,6 +20,7 @@ #include "dds/ddsi/q_entity.h" #include "dds/ddsi/ddsi_domaingv.h" #include "dds/ddsi/ddsi_sertopic.h" +#include "tracing_lttng.h" /* dds_read_impl: Core read/take function. Usually maxs is size of buf and si @@ -128,6 +129,7 @@ static dds_return_t dds_read_impl (bool take, dds_entity_t reader_or_condition, } dds_entity_unpin (entity); thread_state_asleep (ts1); + TRACEPOINT(read, (const void *)rd, (const void *)*buf); return ret; #undef NC_CLEAR_LOAN_OUT diff --git a/src/core/ddsc/src/dds_reader.c b/src/core/ddsc/src/dds_reader.c index 0901b0c..e872f80 100644 --- a/src/core/ddsc/src/dds_reader.c +++ b/src/core/ddsc/src/dds_reader.c @@ -33,6 +33,7 @@ #include "dds/ddsi/ddsi_entity_index.h" #include "dds/ddsi/ddsi_security_omg.h" #include "dds/ddsi/ddsi_statistics.h" +#include "tracing_lttng.h" DECL_ENTITY_LOCK_UNLOCK (extern inline, dds_reader) @@ -547,6 +548,11 @@ static dds_entity_t dds_create_reader_int (dds_entity_t participant_or_subscribe dds_topic_allow_set_qos (tp); dds_topic_unpin (tp); dds_subscriber_unlock (sub); + + dds_guid_t guid; + (void)dds_get_guid(reader, &guid); + TRACEPOINT(create_reader, (const void *)rd, rd->m_topic->m_ktopic->name, guid.v); + return reader; #ifdef DDSI_INCLUDE_SECURITY diff --git a/src/core/ddsc/src/dds_write.c b/src/core/ddsc/src/dds_write.c index 5d7539c..bb385f0 100644 --- a/src/core/ddsc/src/dds_write.c +++ b/src/core/ddsc/src/dds_write.c @@ -26,6 +26,7 @@ #include "dds/ddsi/q_radmin.h" #include "dds/ddsi/ddsi_domaingv.h" #include "dds/ddsi/ddsi_deliver_locally.h" +#include "tracing_lttng.h" dds_return_t dds_write (dds_entity_t writer, const void *data) { @@ -168,6 +169,8 @@ dds_return_t dds_write_impl (dds_writer *wr, const void * data, dds_time_t tstam if (data == NULL) return DDS_RETCODE_BAD_PARAMETER; + TRACEPOINT(write, (const void *)wr, (const char *)data, tstamp); + /* Check for topic filter */ if (wr->m_topic->filter_fn && !writekey) if (! wr->m_topic->filter_fn (data, wr->m_topic->filter_ctx)) diff --git a/src/core/ddsc/src/dds_writer.c b/src/core/ddsc/src/dds_writer.c index 166b2da..47712a9 100644 --- a/src/core/ddsc/src/dds_writer.c +++ b/src/core/ddsc/src/dds_writer.c @@ -32,6 +32,7 @@ #include "dds__whc.h" #include "dds__statistics.h" #include "dds/ddsi/ddsi_statistics.h" +#include "tracing_lttng.h" DECL_ENTITY_LOCK_UNLOCK (extern inline, dds_writer) @@ -395,6 +396,11 @@ dds_entity_t dds_create_writer (dds_entity_t participant_or_publisher, dds_entit dds_topic_allow_set_qos (tp); dds_topic_unpin (tp); dds_publisher_unlock (pub); + + dds_guid_t guid; + (void)dds_get_guid(writer, &guid); + TRACEPOINT(create_writer, (const void *)wr, wr->m_topic->m_ktopic->name, guid.v); + return writer; #ifdef DDSI_INCLUDE_SECURITY diff --git a/src/core/ddsc/src/tracing_lttng.c b/src/core/ddsc/src/tracing_lttng.c new file mode 100644 index 0000000..5deca53 --- /dev/null +++ b/src/core/ddsc/src/tracing_lttng.c @@ -0,0 +1,16 @@ +/* + * Copyright(c) 2021 Christophe Bedard + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License + * v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ + +#define TRACEPOINT_CREATE_PROBES + +#define TRACEPOINT_DEFINE +#include "tracing_lttng.h" diff --git a/src/core/ddsc/src/tracing_lttng.h b/src/core/ddsc/src/tracing_lttng.h new file mode 100644 index 0000000..345444b --- /dev/null +++ b/src/core/ddsc/src/tracing_lttng.h @@ -0,0 +1,110 @@ +/* + * Copyright(c) 2021 Christophe Bedard + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License + * v. 1.0 which is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + */ + +// Provide fake header guard for cpplint +#undef _DDS_LTTNG_H_ +#ifndef _DDS_LTTNG_H_ +#define _DDS_LTTNG_H_ + +#include "dds/features.h" + +#undef TRACEPOINT_PROVIDER +#define TRACEPOINT_PROVIDER dds + +#define TRACEPOINT(event_name, ...) \ + tracepoint(TRACEPOINT_PROVIDER, event_name, __VA_ARGS__) + +#undef TRACEPOINT_INCLUDE +#define TRACEPOINT_INCLUDE "tracing_lttng.h" + +#if !defined(__DDS_LTTNG_H_) || defined(TRACEPOINT_HEADER_MULTI_READ) +#define __DDS_LTTNG_H_ + +#include + +#ifndef LTTNG_UST_HAVE_SDT_INTEGRATION +# ifdef _MSC_VER +# pragma message ("lttng-ust has not been configured & built with SDT integration (--with-sdt)") +# else +# warning lttng-ust has not been configured & built with SDT integration (--with-sdt) +# endif +#endif + +#include +#include + +#define DDS_GID_STORAGE_SIZE 16u + +TRACEPOINT_EVENT( + TRACEPOINT_PROVIDER, + create_writer, + TP_ARGS( + const void *, writer_arg, + char *, topic_name_arg, + const uint8_t *, gid_arg + ), + TP_FIELDS( + ctf_integer_hex(const void *, writer, writer_arg) + ctf_string(topic_name, topic_name_arg) + ctf_array(uint8_t, gid, gid_arg, DDS_GID_STORAGE_SIZE) + ) +) + +TRACEPOINT_EVENT( + TRACEPOINT_PROVIDER, + create_reader, + TP_ARGS( + const void *, reader_arg, + char *, topic_name_arg, + const uint8_t *, gid_arg + ), + TP_FIELDS( + ctf_integer_hex(const void *, reader, reader_arg) + ctf_string(topic_name, topic_name_arg) + ctf_array(uint8_t, gid, gid_arg, DDS_GID_STORAGE_SIZE) + ) +) + +TRACEPOINT_EVENT( + TRACEPOINT_PROVIDER, + write, + TP_ARGS( + const void *, writer_arg, + const void *, data_arg, + int64_t, timestamp_arg + ), + TP_FIELDS( + ctf_integer_hex(const void *, writer, writer_arg) + ctf_integer_hex(const void *, data, data_arg) + ctf_integer(int64_t, timestamp, timestamp_arg) + ) +) + +TRACEPOINT_EVENT( + TRACEPOINT_PROVIDER, + read, + TP_ARGS( + const void *, reader_arg, + const void *, buffer_arg + ), + TP_FIELDS( + ctf_integer_hex(const void *, reader, reader_arg) + ctf_integer_hex(const void *, buffer, buffer_arg) + ) +) + +#endif // __DDS_LTTNG_H_ + +#include + +#endif // _DDS_LTTNG_H_ + diff --git a/src/ddsrt/src/threads/posix/threads.c b/src/ddsrt/src/threads/posix/threads.c index 3244202..a243550 100644 --- a/src/ddsrt/src/threads/posix/threads.c +++ b/src/ddsrt/src/threads/posix/threads.c @@ -129,7 +129,7 @@ ddsrt_thread_setname(const char *__restrict name) name exceeds the limit, so silently truncate. */ char buf[MAXTHREADNAMESIZE + 1] = ""; (void)ddsrt_strlcpy(buf, name, sizeof(buf)); - (void)pthread_setname_np(pthread_self(), name); + (void)pthread_setname_np(pthread_self(), buf); #elif defined(__APPLE__) (void)pthread_setname_np(name); #elif defined(__FreeBSD__) diff --git a/src/features.h.in b/src/features.h.in index 636f19f..1a645f2 100644 --- a/src/features.h.in +++ b/src/features.h.in @@ -21,4 +21,7 @@ /* Whether or not support for generating "deadline missed" events is included */ #cmakedefine DDS_HAS_DEADLINE_MISSED @DDS_HAS_DEADLINE_MISSED@ +/* Whether or not support for LTTng tracing instrumentation is included */ +#cmakedefine DDS_HAS_LTTNG_TRACING @DDS_HAS_LTTNG_TRACING@ + #endif