Skip to content

Commit 8fd9a0a

Browse files
iluetkebhidmic
authored andcommitted
Instrumentation update (#789)
* Add initial instrumentation * Rename function registration method and elaborate on object copy issue * Rely on get_symbol overload instead of using enable_if Signed-off-by: Christophe Bedard <[email protected]> Signed-off-by: Christophe Bedard <[email protected]> Signed-off-by: Ingo Luetkebohle <[email protected]>
1 parent ed0bd16 commit 8fd9a0a

File tree

7 files changed

+76
-1
lines changed

7 files changed

+76
-1
lines changed

rclcpp/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ find_package(rosgraph_msgs REQUIRED)
1414
find_package(rosidl_generator_cpp REQUIRED)
1515
find_package(rosidl_typesupport_c REQUIRED)
1616
find_package(rosidl_typesupport_cpp REQUIRED)
17+
find_package(tracetools REQUIRED)
1718

1819
# Default to C++14
1920
if(NOT CMAKE_CXX_STANDARD)
@@ -114,7 +115,9 @@ ament_target_dependencies(${PROJECT_NAME}
114115
"builtin_interfaces"
115116
"rosgraph_msgs"
116117
"rosidl_typesupport_cpp"
117-
"rosidl_generator_cpp")
118+
"rosidl_generator_cpp"
119+
"tracetools"
120+
)
118121

119122
# Causes the visibility macros to use dllexport rather than dllimport,
120123
# which is appropriate when building the dll but not consuming it.
@@ -141,6 +144,7 @@ ament_export_dependencies(rosidl_typesupport_cpp)
141144
ament_export_dependencies(rosidl_typesupport_c)
142145
ament_export_dependencies(rosidl_generator_cpp)
143146
ament_export_dependencies(rcl_yaml_param_parser)
147+
ament_export_dependencies(tracetools)
144148

145149
if(BUILD_TESTING)
146150
find_package(ament_cmake_gtest REQUIRED)

rclcpp/include/rclcpp/any_service_callback.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "rclcpp/function_traits.hpp"
2424
#include "rclcpp/visibility_control.hpp"
2525
#include "rmw/types.h"
26+
#include "tracetools/tracetools.h"
2627

2728
namespace rclcpp
2829
{
@@ -86,6 +87,7 @@ class AnyServiceCallback
8687
std::shared_ptr<typename ServiceT::Request> request,
8788
std::shared_ptr<typename ServiceT::Response> response)
8889
{
90+
TRACEPOINT(callback_start, (const void *)this, false);
8991
if (shared_ptr_callback_ != nullptr) {
9092
(void)request_header;
9193
shared_ptr_callback_(request, response);
@@ -94,6 +96,7 @@ class AnyServiceCallback
9496
} else {
9597
throw std::runtime_error("unexpected request without any callback set");
9698
}
99+
TRACEPOINT(callback_end, (const void *)this);
97100
}
98101
};
99102

rclcpp/include/rclcpp/any_subscription_callback.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "rclcpp/allocator/allocator_common.hpp"
2727
#include "rclcpp/function_traits.hpp"
2828
#include "rclcpp/visibility_control.hpp"
29+
#include "tracetools/tracetools.h"
30+
#include "tracetools/utils.hpp"
2931

3032
namespace rclcpp
3133
{
@@ -155,6 +157,7 @@ class AnySubscriptionCallback
155157
void dispatch(
156158
std::shared_ptr<MessageT> message, const rmw_message_info_t & message_info)
157159
{
160+
TRACEPOINT(callback_start, (const void *)this, false);
158161
if (shared_ptr_callback_) {
159162
shared_ptr_callback_(message);
160163
} else if (shared_ptr_with_info_callback_) {
@@ -174,11 +177,13 @@ class AnySubscriptionCallback
174177
} else {
175178
throw std::runtime_error("unexpected message without any callback set");
176179
}
180+
TRACEPOINT(callback_end, (const void *)this);
177181
}
178182

179183
void dispatch_intra_process(
180184
ConstMessageSharedPtr message, const rmw_message_info_t & message_info)
181185
{
186+
TRACEPOINT(callback_start, (const void *)this, true);
182187
if (const_shared_ptr_callback_) {
183188
const_shared_ptr_callback_(message);
184189
} else if (const_shared_ptr_with_info_callback_) {
@@ -195,11 +200,13 @@ class AnySubscriptionCallback
195200
throw std::runtime_error("unexpected message without any callback set");
196201
}
197202
}
203+
TRACEPOINT(callback_end, (const void *)this);
198204
}
199205

200206
void dispatch_intra_process(
201207
MessageUniquePtr message, const rmw_message_info_t & message_info)
202208
{
209+
TRACEPOINT(callback_start, (const void *)this, true);
203210
if (shared_ptr_callback_) {
204211
typename std::shared_ptr<MessageT> shared_message = std::move(message);
205212
shared_ptr_callback_(shared_message);
@@ -217,13 +224,39 @@ class AnySubscriptionCallback
217224
} else {
218225
throw std::runtime_error("unexpected message without any callback set");
219226
}
227+
TRACEPOINT(callback_end, (const void *)this);
220228
}
221229

222230
bool use_take_shared_method()
223231
{
224232
return const_shared_ptr_callback_ || const_shared_ptr_with_info_callback_;
225233
}
226234

235+
void register_callback_for_tracing()
236+
{
237+
if (shared_ptr_callback_) {
238+
TRACEPOINT(
239+
rclcpp_callback_register,
240+
(const void *)this,
241+
get_symbol(shared_ptr_callback_));
242+
} else if (shared_ptr_with_info_callback_) {
243+
TRACEPOINT(
244+
rclcpp_callback_register,
245+
(const void *)this,
246+
get_symbol(shared_ptr_with_info_callback_));
247+
} else if (unique_ptr_callback_) {
248+
TRACEPOINT(
249+
rclcpp_callback_register,
250+
(const void *)this,
251+
get_symbol(unique_ptr_callback_));
252+
} else if (unique_ptr_with_info_callback_) {
253+
TRACEPOINT(
254+
rclcpp_callback_register,
255+
(const void *)this,
256+
get_symbol(unique_ptr_with_info_callback_));
257+
}
258+
}
259+
227260
private:
228261
std::shared_ptr<MessageAlloc> message_allocator_;
229262
MessageDeleter message_deleter_;

rclcpp/include/rclcpp/service.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "rclcpp/logging.hpp"
3434
#include "rmw/error_handling.h"
3535
#include "rmw/rmw.h"
36+
#include "tracetools/tracetools.h"
3637

3738
namespace rclcpp
3839
{
@@ -154,6 +155,10 @@ class Service : public ServiceBase
154155

155156
rclcpp::exceptions::throw_from_rcl_error(ret, "could not create service");
156157
}
158+
TRACEPOINT(
159+
rclcpp_service_callback_added,
160+
(const void *)get_service_handle().get(),
161+
(const void *)&any_callback_);
157162
}
158163

159164
Service(
@@ -172,6 +177,10 @@ class Service : public ServiceBase
172177
}
173178

174179
service_handle_ = service_handle;
180+
TRACEPOINT(
181+
rclcpp_service_callback_added,
182+
(const void *)get_service_handle().get(),
183+
(const void *)&any_callback_);
175184
}
176185

177186
Service(
@@ -192,6 +201,10 @@ class Service : public ServiceBase
192201
// In this case, rcl owns the service handle memory
193202
service_handle_ = std::shared_ptr<rcl_service_t>(new rcl_service_t);
194203
service_handle_->impl = service_handle->impl;
204+
TRACEPOINT(
205+
rclcpp_service_callback_added,
206+
(const void *)get_service_handle().get(),
207+
(const void *)&any_callback_);
195208
}
196209

197210
Service() = delete;

rclcpp/include/rclcpp/subscription.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "rclcpp/type_support_decl.hpp"
4747
#include "rclcpp/visibility_control.hpp"
4848
#include "rclcpp/waitable.hpp"
49+
#include "tracetools/tracetools.h"
4950

5051
namespace rclcpp
5152
{
@@ -117,6 +118,14 @@ class Subscription : public SubscriptionBase
117118
options.event_callbacks.liveliness_callback,
118119
RCL_SUBSCRIPTION_LIVELINESS_CHANGED);
119120
}
121+
TRACEPOINT(
122+
rclcpp_subscription_callback_added,
123+
(const void *)get_subscription_handle().get(),
124+
(const void *)&any_callback_);
125+
// The callback object gets copied, so if registration is done too early/before this point
126+
// (e.g. in `AnySubscriptionCallback::set()`), its address won't match any address used later
127+
// in subsequent tracepoints.
128+
any_callback_.register_callback_for_tracing();
120129
}
121130

122131
/// Called after construction to continue setup that requires shared_from_this().

rclcpp/include/rclcpp/timer.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "rclcpp/rate.hpp"
3131
#include "rclcpp/utilities.hpp"
3232
#include "rclcpp/visibility_control.hpp"
33+
#include "tracetools/tracetools.h"
34+
#include "tracetools/utils.hpp"
3335

3436
#include "rcl/error_handling.h"
3537
#include "rcl/timer.h"
@@ -133,6 +135,14 @@ class GenericTimer : public TimerBase
133135
)
134136
: TimerBase(clock, period, context), callback_(std::forward<FunctorT>(callback))
135137
{
138+
TRACEPOINT(
139+
rclcpp_timer_callback_added,
140+
(const void *)get_timer_handle().get(),
141+
(const void *)&callback_);
142+
TRACEPOINT(
143+
rclcpp_callback_register,
144+
(const void *)&callback_,
145+
get_symbol(callback_));
136146
}
137147

138148
/// Default destructor.
@@ -152,7 +162,9 @@ class GenericTimer : public TimerBase
152162
if (ret != RCL_RET_OK) {
153163
throw std::runtime_error("Failed to notify timer that callback occurred");
154164
}
165+
TRACEPOINT(callback_start, (const void *)&callback_, false);
155166
execute_callback_delegate<>();
167+
TRACEPOINT(callback_end, (const void *)&callback_);
156168
}
157169

158170
// void specialization

rclcpp/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<depend>rcl_yaml_param_parser</depend>
2929
<depend>rcpputils</depend>
3030
<depend>rmw_implementation</depend>
31+
<depend>tracetools</depend>
3132

3233
<exec_depend>ament_cmake</exec_depend>
3334

0 commit comments

Comments
 (0)