| 1 | /* |
| 2 | * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "ust-registry-channel.hpp" |
| 9 | #include "ust-app.hpp" |
| 10 | #include "ust-registry-event.hpp" |
| 11 | |
| 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> |
| 18 | |
| 19 | namespace lst = lttng::sessiond::trace; |
| 20 | namespace lsu = lttng::sessiond::ust; |
| 21 | |
| 22 | namespace { |
| 23 | bool is_max_event_id(uint32_t id) |
| 24 | { |
| 25 | return id == UINT32_MAX; |
| 26 | } |
| 27 | |
| 28 | unsigned long ht_hash_event(const void *_key, unsigned long seed) |
| 29 | { |
| 30 | uint64_t hashed_key; |
| 31 | const lttng::sessiond::ust::registry_event *key = |
| 32 | (lttng::sessiond::ust::registry_event *) _key; |
| 33 | |
| 34 | LTTNG_ASSERT(key); |
| 35 | |
| 36 | hashed_key = (uint64_t) hash_key_str(key->name.c_str(), seed); |
| 37 | |
| 38 | return hash_key_u64(&hashed_key, seed); |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | * Hash table match function for event in the registry. |
| 43 | */ |
| 44 | int ht_match_event(struct cds_lfht_node *node, const void *_key) |
| 45 | { |
| 46 | const lttng::sessiond::ust::registry_event *key; |
| 47 | lttng::sessiond::ust::registry_event *event; |
| 48 | |
| 49 | LTTNG_ASSERT(node); |
| 50 | LTTNG_ASSERT(_key); |
| 51 | |
| 52 | event = lttng::utils::container_of(node, <tng::sessiond::ust::registry_event::_node); |
| 53 | key = (lttng::sessiond::ust::registry_event *) _key; |
| 54 | |
| 55 | /* It has to be a perfect match. First, compare the event names. */ |
| 56 | if (event->name != key->name) { |
| 57 | goto no_match; |
| 58 | } |
| 59 | |
| 60 | /* Compare log levels. */ |
| 61 | if (event->log_level != key->log_level) { |
| 62 | goto no_match; |
| 63 | } |
| 64 | |
| 65 | /* Compare the arrays of fields. */ |
| 66 | if (*event->payload != *key->payload) { |
| 67 | goto no_match; |
| 68 | } |
| 69 | |
| 70 | /* Compare model URI. */ |
| 71 | if (event->model_emf_uri != key->model_emf_uri) { |
| 72 | goto no_match; |
| 73 | } |
| 74 | |
| 75 | /* Match */ |
| 76 | return 1; |
| 77 | |
| 78 | no_match: |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | lst::type::cuptr create_event_header(const lst::abi& trace_abi, lst::stream_class::header_type header_type) |
| 83 | { |
| 84 | lst::structure_type::fields event_header_fields; |
| 85 | |
| 86 | if (header_type == lst::stream_class::header_type::COMPACT) { |
| 87 | auto enum_mappings = std::make_shared<lst::unsigned_enumeration_type::mappings>(); |
| 88 | |
| 89 | enum_mappings->emplace_back("compact", lst::unsigned_enumeration_type::mapping::range_t(0, 30)); |
| 90 | enum_mappings->emplace_back("extended"); |
| 91 | |
| 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})); |
| 97 | |
| 98 | lst::variant_type::choices variant_choices; |
| 99 | |
| 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})))); |
| 108 | |
| 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))); |
| 112 | |
| 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})))); |
| 130 | |
| 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))); |
| 133 | |
| 134 | lst::type::cuptr variant = lttng::make_unique<lst::variant_type>( |
| 135 | 0, "id", std::move(variant_choices)); |
| 136 | |
| 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))); |
| 140 | } else { |
| 141 | auto enum_mappings = std::make_shared<lst::unsigned_enumeration_type::mappings>(); |
| 142 | |
| 143 | enum_mappings->emplace_back("compact", lst::unsigned_enumeration_type::mapping::range_t(0, 65534)); |
| 144 | enum_mappings->emplace_back("extended"); |
| 145 | |
| 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})); |
| 151 | |
| 152 | lst::variant_type::choices variant_choices; |
| 153 | |
| 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})))); |
| 163 | |
| 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))); |
| 168 | |
| 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})))); |
| 186 | |
| 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))); |
| 189 | |
| 190 | lst::type::cuptr variant = lttng::make_unique<lst::variant_type>( |
| 191 | 0, "id", std::move(variant_choices)); |
| 192 | |
| 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))); |
| 196 | } |
| 197 | |
| 198 | return lttng::make_unique<lst::structure_type>(0, std::move(event_header_fields)); |
| 199 | } |
| 200 | |
| 201 | lst::type::cuptr create_packet_context(const lst::abi& trace_abi) |
| 202 | { |
| 203 | lst::structure_type::fields packet_context_fields; |
| 204 | |
| 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})))); |
| 212 | |
| 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})))); |
| 220 | |
| 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})))); |
| 228 | |
| 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})))); |
| 236 | |
| 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})))); |
| 244 | |
| 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})))); |
| 252 | |
| 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))); |
| 259 | |
| 260 | return lttng::make_unique<lst::structure_type>(0, std::move(packet_context_fields)); |
| 261 | } |
| 262 | }; /* namespace */ |
| 263 | |
| 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)), |
| 272 | _key{-1ULL}, |
| 273 | _consumer_key{-1ULL}, |
| 274 | _next_event_id{0}, |
| 275 | _is_registered_listener{channel_registered_listener}, |
| 276 | _event_added_listener{event_added_listener}, |
| 277 | _is_registered{false} |
| 278 | { |
| 279 | _events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 280 | if (!_events) { |
| 281 | LTTNG_THROW_POSIX("Failed to allocate urcu events hash table", ENOMEM); |
| 282 | } |
| 283 | |
| 284 | /* Set custom match function. */ |
| 285 | _events->match_fct = ht_match_event; |
| 286 | _events->hash_fct = ht_hash_event; |
| 287 | |
| 288 | _rcu_head = {}; |
| 289 | /* |
| 290 | * Node's key is initialized by the channel's parent session. Its value is irrelevant to the |
| 291 | * channel object itself. |
| 292 | */ |
| 293 | _node = {}; |
| 294 | |
| 295 | _packet_context = create_packet_context(trace_abi); |
| 296 | _event_header = create_event_header(trace_abi, header_type_); |
| 297 | } |
| 298 | |
| 299 | void lsu::registry_channel::add_event( |
| 300 | int session_objd, |
| 301 | int channel_objd, |
| 302 | std::string name, |
| 303 | std::string signature, |
| 304 | std::vector<lst::field::cuptr> event_fields, |
| 305 | int loglevel_value, |
| 306 | nonstd::optional<std::string> model_emf_uri, |
| 307 | lttng_buffer_type buffer_type, |
| 308 | const ust_app& app, |
| 309 | uint32_t& out_event_id) |
| 310 | { |
| 311 | uint32_t event_id; |
| 312 | struct cds_lfht_node *nptr; |
| 313 | lttng::urcu::read_lock_guard read_lock_guard; |
| 314 | |
| 315 | /* |
| 316 | * This should not happen but since it comes from the UST tracer, an |
| 317 | * external party, don't assert and simply validate values. |
| 318 | */ |
| 319 | if (session_objd < 0) { |
| 320 | LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format( |
| 321 | "Invalid session object descriptor provided by application: session descriptor = {}, app = {}", |
| 322 | session_objd, app)); |
| 323 | } |
| 324 | |
| 325 | if (channel_objd < 0) { |
| 326 | LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format( |
| 327 | "Invalid channel object descriptor provided by application: channel descriptor = {}, app = {}", |
| 328 | channel_objd, app)); |
| 329 | } |
| 330 | |
| 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 = {}", |
| 335 | app)); |
| 336 | } |
| 337 | |
| 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))); |
| 343 | |
| 344 | DBG3("%s", fmt::format("UST registry creating event: event = {}", *event).c_str()); |
| 345 | |
| 346 | /* |
| 347 | * This is an add unique with a custom match function for event. The node |
| 348 | * are matched using the event name and signature. |
| 349 | */ |
| 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) { |
| 354 | /* |
| 355 | * This is normal, we just have to send the event id of the |
| 356 | * returned node. |
| 357 | */ |
| 358 | const auto existing_event = lttng::utils::container_of( |
| 359 | nptr, <tng::sessiond::ust::registry_event::_node); |
| 360 | event_id = existing_event->id; |
| 361 | } else { |
| 362 | LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format( |
| 363 | "UST registry create event add unique failed for event: event = {}", |
| 364 | *event)); |
| 365 | } |
| 366 | } else { |
| 367 | const auto& event_ref = *event; |
| 368 | |
| 369 | /* Ownership transferred to _events hash table. */ |
| 370 | event.release(); |
| 371 | |
| 372 | /* Request next event id if the node was successfully added. */ |
| 373 | event_id = event_ref.id; |
| 374 | |
| 375 | /* |
| 376 | * Only increment the next id here since we don't want to waste an ID when the event |
| 377 | * matches an existing one. |
| 378 | */ |
| 379 | _next_event_id++; |
| 380 | _event_added_listener(*this, event_ref); |
| 381 | } |
| 382 | |
| 383 | out_event_id = event_id; |
| 384 | } |
| 385 | |
| 386 | lsu::registry_channel::~registry_channel() |
| 387 | { |
| 388 | lttng_ht_destroy(_events); |
| 389 | } |
| 390 | |
| 391 | const lttng::sessiond::trace::type* lsu::registry_channel::get_event_context() const |
| 392 | { |
| 393 | LTTNG_ASSERT(_is_registered); |
| 394 | return lst::stream_class::get_event_context(); |
| 395 | } |
| 396 | |
| 397 | void lsu::registry_channel::set_event_context(lttng::sessiond::trace::type::cuptr context) |
| 398 | { |
| 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); |
| 402 | } |
| 403 | |
| 404 | bool lsu::registry_channel::is_registered() const |
| 405 | { |
| 406 | return _is_registered; |
| 407 | } |
| 408 | |
| 409 | void lsu::registry_channel::set_as_registered() |
| 410 | { |
| 411 | if (!_is_registered) { |
| 412 | _is_registered = true; |
| 413 | _is_registered_listener(*this); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | void lsu::registry_channel::_accept_on_event_classes( |
| 418 | lttng::sessiond::trace::trace_class_visitor& visitor) const |
| 419 | { |
| 420 | std::vector<const lttng::sessiond::ust::registry_event *> sorted_event_classes; |
| 421 | |
| 422 | { |
| 423 | lttng::urcu::read_lock_guard read_lock_guard; |
| 424 | struct lttng_ht_iter iter; |
| 425 | const lttng::sessiond::ust::registry_event *event; |
| 426 | |
| 427 | DIAGNOSTIC_PUSH |
| 428 | DIAGNOSTIC_IGNORE_INVALID_OFFSETOF |
| 429 | cds_lfht_for_each_entry(_events->ht, &iter.iter, event, _node) { |
| 430 | sorted_event_classes.emplace_back(event); |
| 431 | } |
| 432 | DIAGNOSTIC_POP |
| 433 | } |
| 434 | |
| 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; |
| 439 | }); |
| 440 | |
| 441 | for (const auto event : sorted_event_classes) { |
| 442 | event->accept(visitor); |
| 443 | } |
| 444 | } |