Skip to content

Commit

Permalink
Add tracing instrumentation using tracetools
Browse files Browse the repository at this point in the history
Signed-off-by: Christophe Bedard <[email protected]>
  • Loading branch information
christophebedard committed Dec 16, 2024
1 parent dd82e84 commit 11a0ea5
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 13 deletions.
2 changes: 2 additions & 0 deletions rmw_zenoh_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ find_package(rcutils REQUIRED)
find_package(rosidl_typesupport_fastrtps_c REQUIRED)
find_package(rosidl_typesupport_fastrtps_cpp REQUIRED)
find_package(rmw REQUIRED)
find_package(tracetools REQUIRED)
find_package(zenoh_c_vendor REQUIRED)

add_library(rmw_zenoh_cpp SHARED
Expand Down Expand Up @@ -68,6 +69,7 @@ target_link_libraries(rmw_zenoh_cpp
rosidl_typesupport_fastrtps_c::rosidl_typesupport_fastrtps_c
rosidl_typesupport_fastrtps_cpp::rosidl_typesupport_fastrtps_cpp
rmw::rmw
tracetools::tracetools
zenohc::lib
)

Expand Down
1 change: 1 addition & 0 deletions rmw_zenoh_cpp/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<depend>rosidl_typesupport_fastrtps_c</depend>
<depend>rosidl_typesupport_fastrtps_cpp</depend>
<depend>rmw</depend>
<depend>tracetools</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
Expand Down
8 changes: 8 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_client_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include "rmw/get_topic_endpoint_info.h"
#include "rmw/impl/cpp/macros.hpp"

#include "tracetools/tracetools.h"

namespace
{
///=============================================================================
Expand Down Expand Up @@ -451,6 +453,12 @@ rmw_ret_t ClientData::send_request(
size_t data_length = ser.get_serialized_data_length();
*sequence_id = sequence_number_++;

TRACETOOLS_TRACEPOINT(
rmw_send_request,
static_cast<const void *>(rmw_client_),
static_cast<const void *>(ros_request),
*sequence_id);

// Send request
z_get_options_t opts;
z_get_options_default(&opts);
Expand Down
2 changes: 2 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_node_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ bool NodeData::create_pub_data(

auto pub_data = PublisherData::make(
std::move(session),
publisher,
node_,
entity_->node_info(),
id_,
Expand Down Expand Up @@ -279,6 +280,7 @@ bool NodeData::create_service_data(
auto service_data = ServiceData::make(
std::move(session),
node_,
service,
entity_->node_info(),
id_,
std::move(id),
Expand Down
18 changes: 15 additions & 3 deletions rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "rmw/get_topic_endpoint_info.h"
#include "rmw/impl/cpp/macros.hpp"

#include "tracetools/tracetools.h"

namespace rmw_zenoh_cpp
{
// TODO(yuyuan): SHM, make this configurable
Expand All @@ -43,6 +45,7 @@ namespace rmw_zenoh_cpp
///=============================================================================
std::shared_ptr<PublisherData> PublisherData::make(
std::shared_ptr<ZenohSession> session,
const rmw_publisher_t * const rmw_publisher,
const rmw_node_t * const node,
liveliness::NodeInfo node_info,
std::size_t node_id,
Expand Down Expand Up @@ -192,6 +195,7 @@ std::shared_ptr<PublisherData> PublisherData::make(

return std::shared_ptr<PublisherData>(
new PublisherData{
rmw_publisher,
node,
std::move(entity),
std::move(session),
Expand All @@ -205,6 +209,7 @@ std::shared_ptr<PublisherData> PublisherData::make(

///=============================================================================
PublisherData::PublisherData(
const rmw_publisher_t * const rmw_publisher,
const rmw_node_t * rmw_node,
std::shared_ptr<liveliness::Entity> entity,
std::shared_ptr<ZenohSession> sess,
Expand All @@ -213,7 +218,8 @@ PublisherData::PublisherData(
z_owned_liveliness_token_t token,
const void * type_support_impl,
std::unique_ptr<MessageTypeSupport> type_support)
: rmw_node_(rmw_node),
: rmw_publisher_(rmw_publisher),
rmw_node_(rmw_node),
entity_(std::move(entity)),
sess_(std::move(sess)),
pub_(std::move(pub)),
Expand Down Expand Up @@ -311,9 +317,12 @@ rmw_ret_t PublisherData::publish(
z_owned_bytes_t attachment;
uint8_t local_gid[RMW_GID_STORAGE_SIZE];
entity_->copy_gid(local_gid);
create_map_and_set_sequence_num(&attachment, sequence_number_++, local_gid);
int64_t source_timestamp = 0;
create_map_and_set_sequence_num(&attachment, sequence_number_++, local_gid, &source_timestamp);
options.attachment = z_move(attachment);

TRACETOOLS_TRACEPOINT(
rmw_publish, static_cast<const void *>(rmw_publisher_), ros_message, source_timestamp);
z_owned_bytes_t payload;
if (shmbuf.has_value()) {
z_bytes_from_shm_mut(&payload, z_move(shmbuf.value()));
Expand Down Expand Up @@ -360,11 +369,14 @@ rmw_ret_t PublisherData::publish_serialized_message(
uint8_t local_gid[RMW_GID_STORAGE_SIZE];
entity_->copy_gid(local_gid);
z_owned_bytes_t attachment;
create_map_and_set_sequence_num(&attachment, sequence_number_++, local_gid);
int64_t source_timestamp = 0;
create_map_and_set_sequence_num(&attachment, sequence_number_++, local_gid, &source_timestamp);
options.attachment = z_move(attachment);
z_owned_bytes_t payload;
z_bytes_copy_from_buf(&payload, serialized_message->buffer, data_length);

TRACETOOLS_TRACEPOINT(
rmw_publish, static_cast<const void *>(rmw_publisher_), serialized_message, source_timestamp);
z_result_t res = z_publisher_put(z_loan(pub_), z_move(payload), &options);
if (res != Z_OK) {
if (res == Z_ESESSION_CLOSED) {
Expand Down
4 changes: 4 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_publisher_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class PublisherData final
// Make a shared_ptr of PublisherData.
static std::shared_ptr<PublisherData> make(
std::shared_ptr<ZenohSession> session,
const rmw_publisher_t * const rmw_publisher,
const rmw_node_t * const node,
liveliness::NodeInfo node_info,
std::size_t node_id,
Expand Down Expand Up @@ -89,6 +90,7 @@ class PublisherData final
private:
// Constructor.
PublisherData(
const rmw_publisher_t * const rmw_publisher,
const rmw_node_t * rmw_node,
std::shared_ptr<liveliness::Entity> entity,
std::shared_ptr<ZenohSession> sess,
Expand All @@ -100,6 +102,8 @@ class PublisherData final

// Internal mutex.
mutable std::mutex mutex_;
// The rmw publisher
const rmw_publisher_t * rmw_publisher_;
// The parent node.
const rmw_node_t * rmw_node_;
// The Entity generated for the publisher.
Expand Down
17 changes: 16 additions & 1 deletion rmw_zenoh_cpp/src/detail/rmw_service_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "rmw/get_topic_endpoint_info.h"
#include "rmw/impl/cpp/macros.hpp"

#include "tracetools/tracetools.h"

namespace rmw_zenoh_cpp
{
///==============================================================================
Expand Down Expand Up @@ -67,6 +69,7 @@ void service_data_handler(z_loaned_query_t * query, void * data)
std::shared_ptr<ServiceData> ServiceData::make(
std::shared_ptr<ZenohSession> session,
const rmw_node_t * const node,
const rmw_service_t * rmw_service,
liveliness::NodeInfo node_info,
std::size_t node_id,
std::size_t service_id,
Expand Down Expand Up @@ -149,6 +152,7 @@ std::shared_ptr<ServiceData> ServiceData::make(
auto service_data = std::shared_ptr<ServiceData>(
new ServiceData{
node,
rmw_service,
std::move(entity),
session,
request_members,
Expand Down Expand Up @@ -228,13 +232,15 @@ std::shared_ptr<ServiceData> ServiceData::make(
///=============================================================================
ServiceData::ServiceData(
const rmw_node_t * rmw_node,
const rmw_service_t * rmw_service,
std::shared_ptr<liveliness::Entity> entity,
std::shared_ptr<ZenohSession> sess,
const void * request_type_support_impl,
const void * response_type_support_impl,
std::unique_ptr<RequestTypeSupport> request_type_support,
std::unique_ptr<ResponseTypeSupport> response_type_support)
: rmw_node_(rmw_node),
rmw_service_(rmw_service),
entity_(std::move(entity)),
sess_(std::move(sess)),
request_type_support_impl_(request_type_support_impl),
Expand Down Expand Up @@ -455,11 +461,20 @@ rmw_ret_t ServiceData::send_response(
z_query_reply_options_default(&options);

z_owned_bytes_t attachment;
int64_t source_timestamp = 0;
rmw_zenoh_cpp::create_map_and_set_sequence_num(
&attachment, request_id->sequence_number,
request_id->writer_guid);
request_id->writer_guid, &source_timestamp);
options.attachment = z_move(attachment);

TRACETOOLS_TRACEPOINT(
rmw_send_response,
static_cast<const void *>(rmw_service_),
static_cast<const void *>(ros_response),
request_id->writer_guid,
request_id->sequence_number,
source_timestamp);

z_owned_bytes_t payload;
z_bytes_copy_from_buf(
&payload, reinterpret_cast<const uint8_t *>(response_bytes), data_length);
Expand Down
3 changes: 3 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_service_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ServiceData final
static std::shared_ptr<ServiceData> make(
std::shared_ptr<ZenohSession> session,
const rmw_node_t * const node,
const rmw_service_t * rmw_service,
liveliness::NodeInfo node_info,
std::size_t node_id,
std::size_t service_id,
Expand Down Expand Up @@ -98,6 +99,7 @@ class ServiceData final
// Constructor.
ServiceData(
const rmw_node_t * rmw_node,
const rmw_service_t * rmw_service,
std::shared_ptr<liveliness::Entity> entity,
std::shared_ptr<ZenohSession> sess,
const void * request_type_support_impl,
Expand All @@ -109,6 +111,7 @@ class ServiceData final
mutable std::mutex mutex_;
// The parent node.
const rmw_node_t * rmw_node_;
const rmw_service_t * rmw_service_;
// The Entity generated for the service.
std::shared_ptr<liveliness::Entity> entity_;
// An owned keyexpression.
Expand Down
12 changes: 9 additions & 3 deletions rmw_zenoh_cpp/src/detail/zenoh_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ ZenohSession::~ZenohSession()

///=============================================================================
void create_map_and_set_sequence_num(
z_owned_bytes_t * out_bytes, int64_t sequence_number, uint8_t gid[RMW_GID_STORAGE_SIZE])
z_owned_bytes_t * out_bytes,
int64_t sequence_number,
uint8_t gid[RMW_GID_STORAGE_SIZE],
int64_t * source_timestamp)
{
auto now = std::chrono::system_clock::now().time_since_epoch();
auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now);
int64_t source_timestamp = now_ns.count();
int64_t timestamp = now_ns.count();
if (nullptr != source_timestamp) {
*source_timestamp = timestamp;
}

AttachmentData data(sequence_number, source_timestamp, gid);
AttachmentData data(sequence_number, timestamp, gid);
data.serialize_to_zbytes(out_bytes);
}

Expand Down
3 changes: 2 additions & 1 deletion rmw_zenoh_cpp/src/detail/zenoh_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class ZenohSession final
void
create_map_and_set_sequence_num(
z_owned_bytes_t * out_bytes, int64_t sequence_number,
uint8_t gid[RMW_GID_STORAGE_SIZE]);
uint8_t gid[RMW_GID_STORAGE_SIZE],
int64_t * source_timestamp = nullptr);

///=============================================================================
// A class to store the replies to service requests.
Expand Down
Loading

0 comments on commit 11a0ea5

Please sign in to comment.