| 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/urcu.hpp> |
| 17 | |
| 18 | namespace lst = lttng::sessiond::trace; |
| 19 | namespace lsu = lttng::sessiond::ust; |
| 20 | |
| 21 | namespace { |
| 22 | bool is_max_event_id(uint32_t id) |
| 23 | { |
| 24 | return id == UINT32_MAX; |
| 25 | } |
| 26 | |
| 27 | unsigned long ht_hash_event(const void *_key, unsigned long seed) |
| 28 | { |
| 29 | uint64_t hashed_key; |
| 30 | const lttng::sessiond::ust::registry_event *key = |
| 31 | (lttng::sessiond::ust::registry_event *) _key; |
| 32 | |
| 33 | LTTNG_ASSERT(key); |
| 34 | |
| 35 | hashed_key = (uint64_t) hash_key_str(key->name.c_str(), seed); |
| 36 | |
| 37 | return hash_key_u64(&hashed_key, seed); |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * Hash table match function for event in the registry. |
| 42 | */ |
| 43 | int ht_match_event(struct cds_lfht_node *node, const void *_key) |
| 44 | { |
| 45 | const lttng::sessiond::ust::registry_event *key; |
| 46 | lttng::sessiond::ust::registry_event *event; |
| 47 | |
| 48 | LTTNG_ASSERT(node); |
| 49 | LTTNG_ASSERT(_key); |
| 50 | |
| 51 | event = lttng::utils::container_of(node, <tng::sessiond::ust::registry_event::_node); |
| 52 | key = (lttng::sessiond::ust::registry_event *) _key; |
| 53 | |
| 54 | /* It has to be a perfect match. First, compare the event names. */ |
| 55 | if (event->name != key->name) { |
| 56 | goto no_match; |
| 57 | } |
| 58 | |
| 59 | /* Compare log levels. */ |
| 60 | if (event->log_level != key->log_level) { |
| 61 | goto no_match; |
| 62 | } |
| 63 | |
| 64 | /* Compare the arrays of fields. */ |
| 65 | if (*event->payload != *key->payload) { |
| 66 | goto no_match; |
| 67 | } |
| 68 | |
| 69 | /* Compare model URI. */ |
| 70 | if (event->model_emf_uri != key->model_emf_uri) { |
| 71 | goto no_match; |
| 72 | } |
| 73 | |
| 74 | /* Match */ |
| 75 | return 1; |
| 76 | |
| 77 | no_match: |
| 78 | return 0; |
| 79 | } |
| 80 | }; /* namespace */ |
| 81 | |
| 82 | lsu::registry_channel::registry_channel(unsigned int channel_id, |
| 83 | lsu::registry_channel::registered_listener_fn channel_registered_listener, |
| 84 | lsu::registry_channel::event_added_listener_fn event_added_listener) : |
| 85 | lst::stream_class(channel_id, lst::stream_class::header_type::LARGE), |
| 86 | _key{-1ULL}, |
| 87 | _consumer_key{-1ULL}, |
| 88 | _metadata_dumped{false}, |
| 89 | _next_event_id{0}, |
| 90 | _is_registered_listener{channel_registered_listener}, |
| 91 | _event_added_listener{event_added_listener}, |
| 92 | _is_registered{false} |
| 93 | { |
| 94 | _events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
| 95 | if (!_events) { |
| 96 | LTTNG_THROW_POSIX("Failed to allocate urcu events hash table", ENOMEM); |
| 97 | } |
| 98 | |
| 99 | /* Set custom match function. */ |
| 100 | _events->match_fct = ht_match_event; |
| 101 | _events->hash_fct = ht_hash_event; |
| 102 | } |
| 103 | |
| 104 | void lsu::registry_channel::add_event( |
| 105 | int session_objd, |
| 106 | int channel_objd, |
| 107 | std::string name, |
| 108 | std::string signature, |
| 109 | std::vector<lst::field::cuptr> event_fields, |
| 110 | int loglevel_value, |
| 111 | nonstd::optional<std::string> model_emf_uri, |
| 112 | lttng_buffer_type buffer_type, |
| 113 | const ust_app& app, |
| 114 | uint32_t& out_event_id) |
| 115 | { |
| 116 | uint32_t event_id; |
| 117 | struct cds_lfht_node *nptr; |
| 118 | lttng::urcu::read_lock_guard read_lock_guard; |
| 119 | |
| 120 | /* |
| 121 | * This should not happen but since it comes from the UST tracer, an |
| 122 | * external party, don't assert and simply validate values. |
| 123 | */ |
| 124 | if (session_objd < 0) { |
| 125 | LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format( |
| 126 | "Invalid session object descriptor provided by application: session descriptor = {}, app = {}", |
| 127 | session_objd, app)); |
| 128 | } |
| 129 | |
| 130 | if (channel_objd < 0) { |
| 131 | LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format( |
| 132 | "Invalid channel object descriptor provided by application: channel descriptor = {}, app = {}", |
| 133 | channel_objd, app)); |
| 134 | } |
| 135 | |
| 136 | /* Check if we've reached the maximum possible id. */ |
| 137 | if (is_max_event_id(_next_event_id)) { |
| 138 | LTTNG_THROW_ERROR(fmt::format( |
| 139 | "Failed to allocate new event id (id would overflow): app = {}", |
| 140 | app)); |
| 141 | } |
| 142 | |
| 143 | auto event = lttng::make_unique_wrapper<lsu::registry_event, registry_event_destroy>( |
| 144 | new lsu::registry_event(_next_event_id, id, session_objd, channel_objd, |
| 145 | std::move(name), std::move(signature), |
| 146 | std::move(event_fields), loglevel_value, |
| 147 | std::move(model_emf_uri))); |
| 148 | |
| 149 | DBG3("%s", fmt::format("UST registry creating event: event = {}", *event).c_str()); |
| 150 | |
| 151 | /* |
| 152 | * This is an add unique with a custom match function for event. The node |
| 153 | * are matched using the event name and signature. |
| 154 | */ |
| 155 | nptr = cds_lfht_add_unique(_events->ht, _events->hash_fct(event.get(), lttng_ht_seed), |
| 156 | _events->match_fct, event.get(), &event->_node); |
| 157 | if (nptr != &event->_node) { |
| 158 | if (buffer_type == LTTNG_BUFFER_PER_UID) { |
| 159 | /* |
| 160 | * This is normal, we just have to send the event id of the |
| 161 | * returned node. |
| 162 | */ |
| 163 | const auto existing_event = lttng::utils::container_of( |
| 164 | nptr, <tng::sessiond::ust::registry_event::_node); |
| 165 | event_id = existing_event->id; |
| 166 | } else { |
| 167 | LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format( |
| 168 | "UST registry create event add unique failed for event: event = {}", |
| 169 | *event)); |
| 170 | } |
| 171 | } else { |
| 172 | const auto& event_ref = *event; |
| 173 | |
| 174 | /* Ownership transferred to _events hash table. */ |
| 175 | event.release(); |
| 176 | |
| 177 | /* Request next event id if the node was successfully added. */ |
| 178 | event_id = event_ref.id; |
| 179 | |
| 180 | /* |
| 181 | * Only increment the next id here since we don't want to waste an ID when the event |
| 182 | * matches an existing one. |
| 183 | */ |
| 184 | _next_event_id++; |
| 185 | _event_added_listener(*this, event_ref); |
| 186 | } |
| 187 | |
| 188 | out_event_id = event_id; |
| 189 | } |
| 190 | |
| 191 | lsu::registry_channel::~registry_channel() |
| 192 | { |
| 193 | lttng_ht_destroy(_events); |
| 194 | } |
| 195 | |
| 196 | const lttng::sessiond::trace::type& lsu::registry_channel::get_context() const |
| 197 | { |
| 198 | LTTNG_ASSERT(_is_registered); |
| 199 | return lst::stream_class::get_context(); |
| 200 | } |
| 201 | |
| 202 | void lsu::registry_channel::set_context(lttng::sessiond::trace::type::cuptr context) |
| 203 | { |
| 204 | /* Must only be set once, on the first channel registration provided by an application. */ |
| 205 | LTTNG_ASSERT(!_context); |
| 206 | _context = std::move(context); |
| 207 | } |
| 208 | |
| 209 | bool lsu::registry_channel::is_registered() const |
| 210 | { |
| 211 | return _is_registered; |
| 212 | } |
| 213 | |
| 214 | void lsu::registry_channel::set_as_registered() |
| 215 | { |
| 216 | if (!_is_registered) { |
| 217 | _is_registered = true; |
| 218 | _is_registered_listener(*this); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void lsu::registry_channel::_accept_on_event_classes( |
| 223 | lttng::sessiond::trace::trace_class_visitor& visitor) const |
| 224 | { |
| 225 | std::vector<const lttng::sessiond::ust::registry_event *> sorted_event_classes; |
| 226 | |
| 227 | { |
| 228 | lttng::urcu::read_lock_guard read_lock_guard; |
| 229 | struct lttng_ht_iter iter; |
| 230 | const lttng::sessiond::ust::registry_event *event; |
| 231 | |
| 232 | DIAGNOSTIC_PUSH |
| 233 | DIAGNOSTIC_IGNORE_INVALID_OFFSETOF |
| 234 | cds_lfht_for_each_entry(_events->ht, &iter.iter, event, _node) { |
| 235 | sorted_event_classes.emplace_back(event); |
| 236 | } |
| 237 | DIAGNOSTIC_POP |
| 238 | } |
| 239 | |
| 240 | std::sort(sorted_event_classes.begin(), sorted_event_classes.end(), |
| 241 | [](const lttng::sessiond::ust::registry_event *a, |
| 242 | const lttng::sessiond::ust::registry_event *b) { |
| 243 | return a->id < b->id; |
| 244 | }); |
| 245 | |
| 246 | for (const auto event : sorted_event_classes) { |
| 247 | event->accept(visitor); |
| 248 | } |
| 249 | } |