2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "ust-registry-channel.hpp"
10 #include "ust-registry-event.hpp"
12 #include <common/error.hpp>
13 #include <common/exception.hpp>
14 #include <common/hashtable/utils.hpp>
15 #include <common/make-unique-wrapper.hpp>
16 #include <common/make-unique.hpp>
17 #include <common/urcu.hpp>
19 namespace lst
= lttng::sessiond::trace
;
20 namespace lsu
= lttng::sessiond::ust
;
23 bool is_max_event_id(uint32_t id
)
25 return id
== UINT32_MAX
;
28 unsigned long ht_hash_event(const void *_key
, unsigned long seed
)
31 const lttng::sessiond::ust::registry_event
*key
=
32 (lttng::sessiond::ust::registry_event
*) _key
;
36 hashed_key
= (uint64_t) hash_key_str(key
->name
.c_str(), seed
);
38 return hash_key_u64(&hashed_key
, seed
);
42 * Hash table match function for event in the registry.
44 int ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
46 const lttng::sessiond::ust::registry_event
*key
;
47 lttng::sessiond::ust::registry_event
*event
;
52 event
= lttng::utils::container_of(node
, <tng::sessiond::ust::registry_event::_node
);
53 key
= (lttng::sessiond::ust::registry_event
*) _key
;
55 /* It has to be a perfect match. First, compare the event names. */
56 if (event
->name
!= key
->name
) {
60 /* Compare log levels. */
61 if (event
->log_level
!= key
->log_level
) {
65 /* Compare the arrays of fields. */
66 if (*event
->payload
!= *key
->payload
) {
70 /* Compare model URI. */
71 if (event
->model_emf_uri
!= key
->model_emf_uri
) {
82 lst::type::cuptr
create_event_header(const lst::abi
& trace_abi
, lst::stream_class::header_type header_type
)
84 lst::structure_type::fields event_header_fields
;
86 if (header_type
== lst::stream_class::header_type::COMPACT
) {
87 auto enum_mappings
= std::make_shared
<lst::unsigned_enumeration_type::mappings
>();
89 enum_mappings
->emplace_back("compact", lst::unsigned_enumeration_type::mapping::range_t(0, 30));
90 enum_mappings
->emplace_back("extended");
92 lst::type::cuptr choice_enum
= lttng::make_unique
<lst::unsigned_enumeration_type
>(1,
93 trace_abi
.byte_order
, 5, lst::integer_type::base::DECIMAL
,
94 std::move(enum_mappings
),
95 std::initializer_list
<lst::integer_type::role
>(
96 {lst::integer_type::role::EVENT_RECORD_CLASS_ID
}));
98 lst::variant_type::choices variant_choices
;
100 lst::structure_type::fields compact_fields
;
101 compact_fields
.emplace_back(lttng::make_unique
<lst::field
>("timestamp",
102 lttng::make_unique
<lst::integer_type
>(1, trace_abi
.byte_order
, 27,
103 lst::integer_type::signedness::UNSIGNED
,
104 lst::integer_type::base::DECIMAL
,
105 std::initializer_list
<lst::integer_type::
106 role
>({lst::integer_type::role::
107 DEFAULT_CLOCK_TIMESTAMP
}))));
109 lst::type::cuptr compact
= lttng::make_unique
<lst::structure_type
>(
110 0, std::move(compact_fields
));
111 variant_choices
.emplace_back(lttng::make_unique
<lst::field
>("compact", std::move(compact
)));
113 lst::structure_type::fields extended_fields
;
114 extended_fields
.emplace_back(lttng::make_unique
<lst::field
>("id",
115 lttng::make_unique
<lst::integer_type
>(trace_abi
.uint32_t_alignment
,
116 trace_abi
.byte_order
, 32,
117 lst::integer_type::signedness::UNSIGNED
,
118 lst::integer_type::base::DECIMAL
,
119 std::initializer_list
<lst::integer_type::
120 role
>({lst::integer_type::role::
121 EVENT_RECORD_CLASS_ID
}))));
122 extended_fields
.emplace_back(lttng::make_unique
<lst::field
>("timestamp",
123 lttng::make_unique
<lst::integer_type
>(trace_abi
.uint64_t_alignment
,
124 trace_abi
.byte_order
, 64,
125 lst::integer_type::signedness::UNSIGNED
,
126 lst::integer_type::base::DECIMAL
,
127 std::initializer_list
<lst::integer_type::
128 role
>({lst::integer_type::role::
129 DEFAULT_CLOCK_TIMESTAMP
}))));
131 lst::type::cuptr extended
= lttng::make_unique
<lst::structure_type
>(0, std::move(extended_fields
));
132 variant_choices
.emplace_back(lttng::make_unique
<lst::field
>("extended", std::move(extended
)));
134 lst::type::cuptr variant
= lttng::make_unique
<lst::variant_type
>(
135 0, "id", std::move(variant_choices
));
137 event_header_fields
.emplace_back(lttng::make_unique
<lst::field
>("id", std::move(choice_enum
)));
138 event_header_fields
.emplace_back(
139 lttng::make_unique
<lst::field
>("v", std::move(variant
)));
141 auto enum_mappings
= std::make_shared
<lst::unsigned_enumeration_type::mappings
>();
143 enum_mappings
->emplace_back("compact", lst::unsigned_enumeration_type::mapping::range_t(0, 65534));
144 enum_mappings
->emplace_back("extended");
146 lst::type::cuptr choice_enum
= lttng::make_unique
<lst::unsigned_enumeration_type
>(
147 trace_abi
.uint16_t_alignment
, trace_abi
.byte_order
, 16,
148 lst::integer_type::base::DECIMAL
, std::move(enum_mappings
),
149 std::initializer_list
<lst::integer_type::role
>(
150 {lst::integer_type::role::EVENT_RECORD_CLASS_ID
}));
152 lst::variant_type::choices variant_choices
;
154 lst::structure_type::fields compact_fields
;
155 compact_fields
.emplace_back(lttng::make_unique
<lst::field
>("timestamp",
156 lttng::make_unique
<lst::integer_type
>(trace_abi
.uint32_t_alignment
,
157 trace_abi
.byte_order
, 32,
158 lst::integer_type::signedness::UNSIGNED
,
159 lst::integer_type::base::DECIMAL
,
160 std::initializer_list
<lst::integer_type::
161 role
>({lst::integer_type::role::
162 DEFAULT_CLOCK_TIMESTAMP
}))));
164 lst::type::cuptr compact
= lttng::make_unique
<lst::structure_type
>(
165 0, std::move(compact_fields
));
166 variant_choices
.emplace_back(
167 lttng::make_unique
<lst::field
>("compact", std::move(compact
)));
169 lst::structure_type::fields extended_fields
;
170 extended_fields
.emplace_back(lttng::make_unique
<lst::field
>("id",
171 lttng::make_unique
<lst::integer_type
>(trace_abi
.uint32_t_alignment
,
172 trace_abi
.byte_order
, 32,
173 lst::integer_type::signedness::UNSIGNED
,
174 lst::integer_type::base::DECIMAL
,
175 std::initializer_list
<lst::integer_type::
176 role
>({lst::integer_type::role::
177 EVENT_RECORD_CLASS_ID
}))));
178 extended_fields
.emplace_back(lttng::make_unique
<lst::field
>("timestamp",
179 lttng::make_unique
<lst::integer_type
>(trace_abi
.uint64_t_alignment
,
180 trace_abi
.byte_order
, 64,
181 lst::integer_type::signedness::UNSIGNED
,
182 lst::integer_type::base::DECIMAL
,
183 std::initializer_list
<lst::integer_type::
184 role
>({lst::integer_type::role::
185 DEFAULT_CLOCK_TIMESTAMP
}))));
187 lst::type::cuptr extended
= lttng::make_unique
<lst::structure_type
>(0, std::move(extended_fields
));
188 variant_choices
.emplace_back(lttng::make_unique
<lst::field
>("extended", std::move(extended
)));
190 lst::type::cuptr variant
= lttng::make_unique
<lst::variant_type
>(
191 0, "id", std::move(variant_choices
));
193 event_header_fields
.emplace_back(lttng::make_unique
<lst::field
>("id", std::move(choice_enum
)));
194 event_header_fields
.emplace_back(
195 lttng::make_unique
<lst::field
>("v", std::move(variant
)));
198 return lttng::make_unique
<lst::structure_type
>(0, std::move(event_header_fields
));
201 lst::type::cuptr
create_packet_context(const lst::abi
& trace_abi
)
203 lst::structure_type::fields packet_context_fields
;
205 /* uint64_t timestamp_begin */
206 packet_context_fields
.emplace_back(lttng::make_unique
<lst::field
>("timestamp_begin",
207 lttng::make_unique
<lst::integer_type
>(trace_abi
.uint64_t_alignment
,
208 trace_abi
.byte_order
, 64,
209 lst::integer_type::signedness::UNSIGNED
,
210 lst::integer_type::base::DECIMAL
,
211 std::initializer_list
<lst::integer_type::role
>({lst::integer_type::role::DEFAULT_CLOCK_TIMESTAMP
}))));
213 /* uint64_t timestamp_end */
214 packet_context_fields
.emplace_back(lttng::make_unique
<lst::field
>("timestamp_end",
215 lttng::make_unique
<lst::integer_type
>(trace_abi
.uint64_t_alignment
,
216 trace_abi
.byte_order
, 64,
217 lst::integer_type::signedness::UNSIGNED
,
218 lst::integer_type::base::DECIMAL
,
219 std::initializer_list
<lst::integer_type::role
>({lst::integer_type::role::PACKET_END_DEFAULT_CLOCK_TIMESTAMP
}))));
221 /* uint64_t content_size */
222 packet_context_fields
.emplace_back(lttng::make_unique
<lst::field
>("content_size",
223 lttng::make_unique
<lst::integer_type
>(trace_abi
.uint64_t_alignment
,
224 trace_abi
.byte_order
, 64,
225 lst::integer_type::signedness::UNSIGNED
,
226 lst::integer_type::base::DECIMAL
,
227 std::initializer_list
<lst::integer_type::role
>({lst::integer_type::role::PACKET_CONTENT_LENGTH
}))));
229 /* uint64_t packet_size */
230 packet_context_fields
.emplace_back(lttng::make_unique
<lst::field
>("packet_size",
231 lttng::make_unique
<lst::integer_type
>(trace_abi
.uint64_t_alignment
,
232 trace_abi
.byte_order
, 64,
233 lst::integer_type::signedness::UNSIGNED
,
234 lst::integer_type::base::DECIMAL
,
235 std::initializer_list
<lst::integer_type::role
>({lst::integer_type::role::PACKET_TOTAL_LENGTH
}))));
237 /* uint64_t packet_seq_num */
238 packet_context_fields
.emplace_back(lttng::make_unique
<lst::field
>("packet_seq_num",
239 lttng::make_unique
<lst::integer_type
>(trace_abi
.uint64_t_alignment
,
240 trace_abi
.byte_order
, 64,
241 lst::integer_type::signedness::UNSIGNED
,
242 lst::integer_type::base::DECIMAL
,
243 std::initializer_list
<lst::integer_type::role
>({lst::integer_type::role::PACKET_SEQUENCE_NUMBER
}))));
245 /* unsigned long events_discarded */
246 packet_context_fields
.emplace_back(lttng::make_unique
<lst::field
>("events_discarded",
247 lttng::make_unique
<lst::integer_type
>(trace_abi
.long_alignment
,
248 trace_abi
.byte_order
, trace_abi
.bits_per_long
,
249 lst::integer_type::signedness::UNSIGNED
,
250 lst::integer_type::base::DECIMAL
,
251 std::initializer_list
<lst::integer_type::role
>({lst::integer_type::role::DISCARDED_EVENT_RECORD_COUNTER_SNAPSHOT
}))));
253 /* uint32_t cpu_id */
254 packet_context_fields
.emplace_back(lttng::make_unique
<lst::field
>("cpu_id",
255 lttng::make_unique
<lst::integer_type
>(trace_abi
.uint32_t_alignment
,
256 trace_abi
.byte_order
, 32,
257 lst::integer_type::signedness::UNSIGNED
,
258 lst::integer_type::base::DECIMAL
)));
260 return lttng::make_unique
<lst::structure_type
>(0, std::move(packet_context_fields
));
264 lsu::registry_channel::registry_channel(unsigned int channel_id
,
265 const lst::abi
& trace_abi
,
266 std::string in_default_clock_class_name
,
267 lsu::registry_channel::registered_listener_fn channel_registered_listener
,
268 lsu::registry_channel::event_added_listener_fn event_added_listener
) :
269 lst::stream_class(channel_id
,
270 lst::stream_class::header_type::LARGE
,
271 std::move(in_default_clock_class_name
)),
273 _consumer_key
{-1ULL},
275 _is_registered_listener
{channel_registered_listener
},
276 _event_added_listener
{event_added_listener
},
277 _is_registered
{false}
279 _events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
281 LTTNG_THROW_POSIX("Failed to allocate urcu events hash table", ENOMEM
);
284 /* Set custom match function. */
285 _events
->match_fct
= ht_match_event
;
286 _events
->hash_fct
= ht_hash_event
;
290 * Node's key is initialized by the channel's parent session. Its value is irrelevant to the
291 * channel object itself.
295 _packet_context
= create_packet_context(trace_abi
);
296 _event_header
= create_event_header(trace_abi
, header_type_
);
299 void lsu::registry_channel::add_event(
303 std::string signature
,
304 std::vector
<lst::field::cuptr
> event_fields
,
306 nonstd::optional
<std::string
> model_emf_uri
,
307 lttng_buffer_type buffer_type
,
309 uint32_t& out_event_id
)
312 struct cds_lfht_node
*nptr
;
313 lttng::urcu::read_lock_guard read_lock_guard
;
316 * This should not happen but since it comes from the UST tracer, an
317 * external party, don't assert and simply validate values.
319 if (session_objd
< 0) {
320 LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format(
321 "Invalid session object descriptor provided by application: session descriptor = {}, app = {}",
325 if (channel_objd
< 0) {
326 LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format(
327 "Invalid channel object descriptor provided by application: channel descriptor = {}, app = {}",
331 /* Check if we've reached the maximum possible id. */
332 if (is_max_event_id(_next_event_id
)) {
333 LTTNG_THROW_ERROR(fmt::format(
334 "Failed to allocate new event id (id would overflow): app = {}",
338 auto event
= lttng::make_unique_wrapper
<lsu::registry_event
, registry_event_destroy
>(
339 new lsu::registry_event(_next_event_id
, id
, session_objd
, channel_objd
,
340 std::move(name
), std::move(signature
),
341 std::move(event_fields
), loglevel_value
,
342 std::move(model_emf_uri
)));
344 DBG3("%s", fmt::format("UST registry creating event: event = {}", *event
).c_str());
347 * This is an add unique with a custom match function for event. The node
348 * are matched using the event name and signature.
350 nptr
= cds_lfht_add_unique(_events
->ht
, _events
->hash_fct(event
.get(), lttng_ht_seed
),
351 _events
->match_fct
, event
.get(), &event
->_node
);
352 if (nptr
!= &event
->_node
) {
353 if (buffer_type
== LTTNG_BUFFER_PER_UID
) {
355 * This is normal, we just have to send the event id of the
358 const auto existing_event
= lttng::utils::container_of(
359 nptr
, <tng::sessiond::ust::registry_event::_node
);
360 event_id
= existing_event
->id
;
362 LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format(
363 "UST registry create event add unique failed for event: event = {}",
367 const auto& event_ref
= *event
;
369 /* Ownership transferred to _events hash table. */
372 /* Request next event id if the node was successfully added. */
373 event_id
= event_ref
.id
;
376 * Only increment the next id here since we don't want to waste an ID when the event
377 * matches an existing one.
380 _event_added_listener(*this, event_ref
);
383 out_event_id
= event_id
;
386 lsu::registry_channel::~registry_channel()
388 lttng_ht_destroy(_events
);
391 const lttng::sessiond::trace::type
* lsu::registry_channel::get_event_context() const
393 LTTNG_ASSERT(_is_registered
);
394 return lst::stream_class::get_event_context();
397 void lsu::registry_channel::set_event_context(lttng::sessiond::trace::type::cuptr context
)
399 /* Must only be set once, on the first channel registration provided by an application. */
400 LTTNG_ASSERT(!_event_context
);
401 _event_context
= std::move(context
);
404 bool lsu::registry_channel::is_registered() const
406 return _is_registered
;
409 void lsu::registry_channel::set_as_registered()
411 if (!_is_registered
) {
412 _is_registered
= true;
413 _is_registered_listener(*this);
417 void lsu::registry_channel::_accept_on_event_classes(
418 lttng::sessiond::trace::trace_class_visitor
& visitor
) const
420 std::vector
<const lttng::sessiond::ust::registry_event
*> sorted_event_classes
;
423 lttng::urcu::read_lock_guard read_lock_guard
;
424 struct lttng_ht_iter iter
;
425 const lttng::sessiond::ust::registry_event
*event
;
428 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
429 cds_lfht_for_each_entry(_events
->ht
, &iter
.iter
, event
, _node
) {
430 sorted_event_classes
.emplace_back(event
);
435 std::sort(sorted_event_classes
.begin(), sorted_event_classes
.end(),
436 [](const lttng::sessiond::ust::registry_event
*a
,
437 const lttng::sessiond::ust::registry_event
*b
) {
438 return a
->id
< b
->id
;
441 for (const auto event
: sorted_event_classes
) {
442 event
->accept(visitor
);