@@ -21,38 +21,32 @@ void Tracker::scan(
21
21
const rclcpp::Time & now,
22
22
std::shared_ptr<EventsLogger> elog)
23
23
{
24
- // If this is first message received store some info about it
25
- if (stat ().n () == 0 ) {
26
- this ->set_size (header.size );
27
- this ->set_frequency (header.frequency );
28
- }
29
-
30
24
// Compute latency
31
25
rclcpp::Time stamp (header.stamp .sec , header.stamp .nanosec , RCL_ROS_TIME);
32
26
auto lat = std::chrono::nanoseconds ((now - stamp).nanoseconds ());
33
27
uint64_t lat_us = lat.count () / 1000 ;
34
28
// store the last latency to be read from node
35
- _last_latency = lat_us;
29
+ m_last_latency = lat_us;
36
30
37
31
bool late = false ;
38
32
bool too_late = false ;
39
33
40
- if (_tracking_options .is_enabled ) {
34
+ if (m_tracking_options .is_enabled ) {
41
35
// Check if we received the correct message. The assumption here is
42
36
// that the messages arrive in chronological order
43
- if (header.tracking_number == _tracking_number_count ) {
44
- _tracking_number_count ++;
37
+ if (header.tracking_number == m_tracking_number_count ) {
38
+ m_tracking_number_count ++;
45
39
} else {
46
40
// We missed some mesages...
47
- int64_t n_lost = header.tracking_number - _tracking_number_count ;
48
- _lost_messages += n_lost;
49
- _tracking_number_count = header.tracking_number + 1 ;
41
+ int64_t n_lost = header.tracking_number - m_tracking_number_count ;
42
+ m_lost_messages += n_lost;
43
+ m_tracking_number_count = header.tracking_number + 1 ;
50
44
51
45
// Log the event
52
46
if (elog != nullptr ) {
53
47
EventsLogger::Event ev;
54
48
std::stringstream description;
55
- ev.caller_name = _topic_srv_name + " ->" + _node_name ;
49
+ ev.caller_name = m_topic_srv_name + " ->" + m_node_name ;
56
50
ev.code = EventsLogger::EventCode::lost_messages;
57
51
58
52
if (n_lost == 1 ) {
@@ -67,13 +61,13 @@ void Tracker::scan(
67
61
}
68
62
69
63
// Check if the message latency qualifies the message as a lost or late message.
70
- const int period_us = 1000000 / m_frequency ;
64
+ const int period_us = 1000000 / header. frequency ;
71
65
const unsigned int latency_late_threshold_us = std::min (
72
- _tracking_options .late_absolute_us ,
73
- _tracking_options .late_percentage * period_us / 100 );
66
+ m_tracking_options .late_absolute_us ,
67
+ m_tracking_options .late_percentage * period_us / 100 );
74
68
const unsigned int latency_too_late_threshold_us = std::min (
75
- _tracking_options .too_late_absolute_us ,
76
- _tracking_options .too_late_percentage * period_us / 100 );
69
+ m_tracking_options .too_late_absolute_us ,
70
+ m_tracking_options .too_late_percentage * period_us / 100 );
77
71
78
72
too_late = lat_us > latency_too_late_threshold_us;
79
73
late = lat_us > latency_late_threshold_us && !too_late;
@@ -86,13 +80,13 @@ void Tracker::scan(
86
80
lat_us << " us > " << latency_late_threshold_us << " us" ;
87
81
88
82
EventsLogger::Event ev;
89
- ev.caller_name = _topic_srv_name + " ->" + _node_name ;
83
+ ev.caller_name = m_topic_srv_name + " ->" + m_node_name ;
90
84
ev.code = EventsLogger::EventCode::late_message;
91
85
ev.description = description.str ();
92
86
93
87
elog->write_event (ev);
94
88
}
95
- _late_messages ++;
89
+ m_late_messages ++;
96
90
}
97
91
98
92
if (too_late) {
@@ -103,35 +97,63 @@ void Tracker::scan(
103
97
lat_us << " us > " << latency_too_late_threshold_us << " us" ;
104
98
105
99
EventsLogger::Event ev;
106
- ev.caller_name = _topic_srv_name + " ->" + _node_name ;
100
+ ev.caller_name = m_topic_srv_name + " ->" + m_node_name ;
107
101
ev.code = EventsLogger::EventCode::too_late_message;
108
102
ev.description = description.str ();
109
103
110
104
elog->write_event (ev);
111
105
}
112
- _too_late_messages ++;
106
+ m_too_late_messages ++;
113
107
}
114
108
}
115
109
116
110
if (!too_late) {
117
111
// Compute statistics with new sample. Don't add to this the msgs
118
112
// that arrived too late.
119
- this ->add_sample (lat_us);
113
+ this ->add_sample (now, lat_us, header. size , header. frequency );
120
114
}
121
115
122
- _received_messages ++;
116
+ m_received_messages ++;
123
117
}
124
118
125
- void Tracker::add_sample (uint64_t latency_sample)
119
+ void Tracker::add_sample (
120
+ const rclcpp::Time & now,
121
+ uint64_t latency_sample,
122
+ size_t size,
123
+ float frequency)
126
124
{
127
- _stat.add_sample (latency_sample);
125
+ // If this is first message received store some info about it
126
+ if (m_stat.n () == 0 ) {
127
+ m_data_size = size;
128
+ m_frequency = frequency;
129
+ m_first_msg_time = now;
130
+ }
131
+
132
+ m_last_msg_time = now;
133
+ m_stat.add_sample (latency_sample);
128
134
}
129
135
130
136
uint32_t Tracker::get_and_update_tracking_number ()
131
137
{
132
- uint32_t old_number = _tracking_number_count ;
133
- _tracking_number_count ++;
138
+ uint32_t old_number = m_tracking_number_count ;
139
+ m_tracking_number_count ++;
134
140
return old_number;
135
141
}
136
142
143
+ double Tracker::throughput () const
144
+ {
145
+ // We compute max because currently publishers update only n() and not m_received_messages,
146
+ // but for subscriptions n() will not include messages received too late.
147
+ uint64_t num_msg_received = std::max (m_stat.n (), m_received_messages);
148
+ if (num_msg_received < 2 ) {
149
+ return 0.0 ;
150
+ }
151
+
152
+ rclcpp::Duration msgs_received_interval = m_last_msg_time - m_first_msg_time;
153
+ double sample_rate_sec = (num_msg_received - 1 ) / msgs_received_interval.seconds ();
154
+ double throughput = sample_rate_sec * m_data_size;
155
+
156
+ return throughput;
157
+ }
158
+
137
159
} // namespace performance_metrics
0 commit comments