2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <common/common.h>
22 #include <common/hashtable/utils.h>
23 #include <lttng/lttng.h>
25 #include "ust-registry.h"
29 * Hash table match function for event in the registry.
31 static int ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
33 struct ust_registry_event
*event
;
34 const struct ust_registry_event
*key
;
39 event
= caa_container_of(node
, struct ust_registry_event
, node
.node
);
43 /* It has to be a perfect match. */
44 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
48 /* It has to be a perfect match. */
49 if (strncmp(event
->signature
, key
->signature
,
50 strlen(event
->signature
) != 0)) {
61 static unsigned long ht_hash_event(void *_key
, unsigned long seed
)
64 struct ust_registry_event
*key
= _key
;
68 xored_key
= (uint64_t) (hash_key_str(key
->name
, seed
) ^
69 hash_key_str(key
->signature
, seed
));
71 return hash_key_u64(&xored_key
, seed
);
75 * Allocate event and initialize it. This does NOT set a valid event id from a
78 static struct ust_registry_event
*alloc_event(int session_objd
,
79 int channel_objd
, char *name
, char *sig
, size_t nr_fields
,
80 struct ustctl_field
*fields
, int loglevel
, char *model_emf_uri
)
82 struct ust_registry_event
*event
= NULL
;
84 event
= zmalloc(sizeof(*event
));
86 PERROR("zmalloc ust registry event");
90 event
->session_objd
= session_objd
;
91 event
->channel_objd
= channel_objd
;
92 /* Allocated by ustctl. */
93 event
->signature
= sig
;
94 event
->nr_fields
= nr_fields
;
95 event
->fields
= fields
;
96 event
->loglevel
= loglevel
;
97 event
->model_emf_uri
= model_emf_uri
;
99 /* Copy event name and force NULL byte. */
100 strncpy(event
->name
, name
, sizeof(event
->name
));
101 event
->name
[sizeof(event
->name
) - 1] = '\0';
103 cds_lfht_node_init(&event
->node
.node
);
110 * Free event data structure. This does NOT delete it from any hash table. It's
111 * safe to pass a NULL pointer. This shoudl be called inside a call RCU if the
112 * event is previously deleted from a rcu hash table.
114 static void destroy_event(struct ust_registry_event
*event
)
121 free(event
->model_emf_uri
);
122 free(event
->signature
);
127 * Destroy event function call of the call RCU.
129 static void destroy_event_rcu(struct rcu_head
*head
)
131 struct lttng_ht_node_u64
*node
=
132 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
133 struct ust_registry_event
*event
=
134 caa_container_of(node
, struct ust_registry_event
, node
);
136 destroy_event(event
);
140 * Find an event using the name and signature in the given registry. RCU read
141 * side lock MUST be acquired before calling this function and as long as the
142 * event reference is kept by the caller.
144 * On success, the event pointer is returned else NULL.
146 struct ust_registry_event
*ust_registry_find_event(
147 struct ust_registry_channel
*chan
, char *name
, char *sig
)
149 struct lttng_ht_node_u64
*node
;
150 struct lttng_ht_iter iter
;
151 struct ust_registry_event
*event
= NULL
;
152 struct ust_registry_event key
;
158 /* Setup key for the match function. */
159 strncpy(key
.name
, name
, sizeof(key
.name
));
160 key
.name
[sizeof(key
.name
) - 1] = '\0';
163 cds_lfht_lookup(chan
->ht
->ht
, chan
->ht
->hash_fct(&key
, lttng_ht_seed
),
164 chan
->ht
->match_fct
, &key
, &iter
.iter
);
165 node
= lttng_ht_iter_get_node_u64(&iter
);
169 event
= caa_container_of(node
, struct ust_registry_event
, node
);
176 * Create a ust_registry_event from the given parameters and add it to the
177 * registry hash table. If event_id is valid, it is set with the newly created
180 * On success, return 0 else a negative value. The created event MUST be unique
181 * so on duplicate entry -EINVAL is returned. On error, event_id is untouched.
183 * Should be called with session registry mutex held.
185 int ust_registry_create_event(struct ust_registry_session
*session
,
186 uint64_t chan_key
, int session_objd
, int channel_objd
, char *name
,
187 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
188 char *model_emf_uri
, int buffer_type
, uint32_t *event_id_p
)
192 struct cds_lfht_node
*nptr
;
193 struct ust_registry_event
*event
= NULL
;
194 struct ust_registry_channel
*chan
;
204 * This should not happen but since it comes from the UST tracer, an
205 * external party, don't assert and simply validate values.
207 if (session_objd
< 0 || channel_objd
< 0) {
212 chan
= ust_registry_channel_find(session
, chan_key
);
218 /* Check if we've reached the maximum possible id. */
219 if (ust_registry_is_max_id(chan
->used_event_id
)) {
224 event
= alloc_event(session_objd
, channel_objd
, name
, sig
, nr_fields
,
225 fields
, loglevel
, model_emf_uri
);
231 DBG3("UST registry creating event with event: %s, sig: %s, id: %u, "
232 "chan_objd: %u, sess_objd: %u, chan_id: %u", event
->name
,
233 event
->signature
, event
->id
, event
->channel_objd
,
234 event
->session_objd
, chan
->chan_id
);
237 * This is an add unique with a custom match function for event. The node
238 * are matched using the event name and signature.
240 nptr
= cds_lfht_add_unique(chan
->ht
->ht
, chan
->ht
->hash_fct(event
,
241 lttng_ht_seed
), chan
->ht
->match_fct
, event
, &event
->node
.node
);
242 if (nptr
!= &event
->node
.node
) {
243 if (buffer_type
== LTTNG_BUFFER_PER_UID
) {
245 * This is normal, we just have to send the event id of the
246 * returned node and make sure we destroy the previously allocated
249 destroy_event(event
);
250 event
= caa_container_of(nptr
, struct ust_registry_event
,
253 event_id
= event
->id
;
255 ERR("UST registry create event add unique failed for event: %s, "
256 "sig: %s, id: %u, chan_objd: %u, sess_objd: %u",
257 event
->name
, event
->signature
, event
->id
,
258 event
->channel_objd
, event
->session_objd
);
263 /* Request next event id if the node was successfully added. */
264 event_id
= event
->id
= ust_registry_get_next_event_id(chan
);
267 *event_id_p
= event_id
;
269 if (!event
->metadata_dumped
) {
270 /* Append to metadata */
271 ret
= ust_metadata_event_statedump(session
, chan
, event
);
273 ERR("Error appending event metadata (errno = %d)", ret
);
288 destroy_event(event
);
293 * For a given event in a registry, delete the entry and destroy the event.
294 * This MUST be called within a RCU read side lock section.
296 void ust_registry_destroy_event(struct ust_registry_channel
*chan
,
297 struct ust_registry_event
*event
)
300 struct lttng_ht_iter iter
;
305 /* Delete the node first. */
306 iter
.iter
.node
= &event
->node
.node
;
307 ret
= lttng_ht_del(chan
->ht
, &iter
);
310 call_rcu(&event
->node
.head
, destroy_event_rcu
);
316 * We need to execute ht_destroy outside of RCU read-side critical
317 * section and outside of call_rcu thread, so we postpone its execution
318 * using ht_cleanup_push. It is simpler than to change the semantic of
319 * the many callers of delete_ust_app_session().
322 void destroy_channel_rcu(struct rcu_head
*head
)
324 struct ust_registry_channel
*chan
=
325 caa_container_of(head
, struct ust_registry_channel
, rcu_head
);
328 ht_cleanup_push(chan
->ht
);
330 free(chan
->ctx_fields
);
335 * Destroy every element of the registry and free the memory. This does NOT
336 * free the registry pointer since it might not have been allocated before so
337 * it's the caller responsability.
339 static void destroy_channel(struct ust_registry_channel
*chan
)
341 struct lttng_ht_iter iter
;
342 struct ust_registry_event
*event
;
347 /* Destroy all event associated with this registry. */
348 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter
.iter
, event
, node
.node
) {
349 /* Delete the node from the ht and free it. */
350 ust_registry_destroy_event(chan
, event
);
353 call_rcu(&chan
->rcu_head
, destroy_channel_rcu
);
357 * Initialize registry with default values.
359 int ust_registry_channel_add(struct ust_registry_session
*session
,
363 struct ust_registry_channel
*chan
;
367 chan
= zmalloc(sizeof(*chan
));
369 PERROR("zmalloc ust registry channel");
374 chan
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
380 /* Set custom match function. */
381 chan
->ht
->match_fct
= ht_match_event
;
382 chan
->ht
->hash_fct
= ht_hash_event
;
385 * Assign a channel ID right now since the event notification comes
386 * *before* the channel notify so the ID needs to be set at this point so
387 * the metadata can be dumped for that event.
389 if (ust_registry_is_max_id(session
->used_channel_id
)) {
393 chan
->chan_id
= ust_registry_get_next_chan_id(session
);
396 lttng_ht_node_init_u64(&chan
->node
, key
);
397 lttng_ht_add_unique_u64(session
->channels
, &chan
->node
);
403 destroy_channel(chan
);
409 * Find a channel in the given registry. RCU read side lock MUST be acquired
410 * before calling this function and as long as the event reference is kept by
413 * On success, the pointer is returned else NULL.
415 struct ust_registry_channel
*ust_registry_channel_find(
416 struct ust_registry_session
*session
, uint64_t key
)
418 struct lttng_ht_node_u64
*node
;
419 struct lttng_ht_iter iter
;
420 struct ust_registry_channel
*chan
= NULL
;
423 assert(session
->channels
);
425 DBG3("UST registry channel finding key %" PRIu64
, key
);
427 lttng_ht_lookup(session
->channels
, &key
, &iter
);
428 node
= lttng_ht_iter_get_node_u64(&iter
);
432 chan
= caa_container_of(node
, struct ust_registry_channel
, node
);
439 * Remove channel using key from registry and free memory.
441 void ust_registry_channel_del_free(struct ust_registry_session
*session
,
444 struct lttng_ht_iter iter
;
445 struct ust_registry_channel
*chan
;
451 chan
= ust_registry_channel_find(session
, key
);
457 iter
.iter
.node
= &chan
->node
.node
;
458 ret
= lttng_ht_del(session
->channels
, &iter
);
461 destroy_channel(chan
);
468 * Initialize registry with default values and set the newly allocated session
469 * pointer to sessionp.
471 * Return 0 on success and sessionp is set or else return -1 and sessionp is
474 int ust_registry_session_init(struct ust_registry_session
**sessionp
,
476 uint32_t bits_per_long
,
477 uint32_t uint8_t_alignment
,
478 uint32_t uint16_t_alignment
,
479 uint32_t uint32_t_alignment
,
480 uint32_t uint64_t_alignment
,
481 uint32_t long_alignment
,
487 struct ust_registry_session
*session
;
491 session
= zmalloc(sizeof(*session
));
493 PERROR("zmalloc ust registry session");
497 pthread_mutex_init(&session
->lock
, NULL
);
498 session
->bits_per_long
= bits_per_long
;
499 session
->uint8_t_alignment
= uint8_t_alignment
;
500 session
->uint16_t_alignment
= uint16_t_alignment
;
501 session
->uint32_t_alignment
= uint32_t_alignment
;
502 session
->uint64_t_alignment
= uint64_t_alignment
;
503 session
->long_alignment
= long_alignment
;
504 session
->byte_order
= byte_order
;
506 session
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
507 if (!session
->channels
) {
511 ret
= lttng_uuid_generate(session
->uuid
);
513 ERR("Failed to generate UST uuid (errno = %d)", ret
);
517 pthread_mutex_lock(&session
->lock
);
518 ret
= ust_metadata_session_statedump(session
, app
, major
, minor
);
519 pthread_mutex_unlock(&session
->lock
);
521 ERR("Failed to generate session metadata (errno = %d)", ret
);
530 ust_registry_session_destroy(session
);
536 * Destroy session registry. This does NOT free the given pointer since it
537 * might get passed as a reference. The registry lock should NOT be acquired.
539 void ust_registry_session_destroy(struct ust_registry_session
*reg
)
542 struct lttng_ht_iter iter
;
543 struct ust_registry_channel
*chan
;
545 /* On error, EBUSY can be returned if lock. Code flow error. */
546 ret
= pthread_mutex_destroy(®
->lock
);
550 /* Destroy all event associated with this registry. */
551 cds_lfht_for_each_entry(reg
->channels
->ht
, &iter
.iter
, chan
, node
.node
) {
552 /* Delete the node from the ht and free it. */
553 ret
= lttng_ht_del(reg
->channels
, &iter
);
555 destroy_channel(chan
);
559 ht_cleanup_push(reg
->channels
);
This page took 0.041606 seconds and 4 git commands to generate.