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"
28 * Hash table match function for event in the registry.
30 static int ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
32 struct ust_registry_event
*event
;
33 const struct ust_registry_event
*key
;
38 event
= caa_container_of(node
, struct ust_registry_event
, node
.node
);
42 /* It has to be a perfect match. */
43 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
47 /* It has to be a perfect match. */
48 if (strncmp(event
->signature
, key
->signature
,
49 strlen(event
->signature
) != 0)) {
60 static unsigned long ht_hash_event(void *_key
, unsigned long seed
)
63 struct ust_registry_event
*key
= _key
;
67 xored_key
= (uint64_t) (hash_key_str(key
->name
, seed
) ^
68 hash_key_str(key
->signature
, seed
));
70 return hash_key_u64(&xored_key
, seed
);
74 * Allocate event and initialize it. This does NOT set a valid event id from a
77 static struct ust_registry_event
*alloc_event(int session_objd
,
78 int channel_objd
, char *name
, char *sig
, size_t nr_fields
,
79 struct ustctl_field
*fields
, int loglevel
, char *model_emf_uri
)
81 struct ust_registry_event
*event
= NULL
;
83 event
= zmalloc(sizeof(*event
));
85 PERROR("zmalloc ust registry event");
89 event
->session_objd
= session_objd
;
90 event
->channel_objd
= channel_objd
;
91 /* Allocated by ustctl. */
92 event
->signature
= sig
;
93 event
->nr_fields
= nr_fields
;
94 event
->fields
= fields
;
95 event
->loglevel
= loglevel
;
96 event
->model_emf_uri
= model_emf_uri
;
98 /* Copy event name and force NULL byte. */
99 strncpy(event
->name
, name
, sizeof(event
->name
));
100 event
->name
[sizeof(event
->name
) - 1] = '\0';
102 cds_lfht_node_init(&event
->node
.node
);
109 * Free event data structure. This does NOT delete it from any hash table. It's
110 * safe to pass a NULL pointer. This shoudl be called inside a call RCU if the
111 * event is previously deleted from a rcu hash table.
113 static void destroy_event(struct ust_registry_event
*event
)
120 free(event
->model_emf_uri
);
121 free(event
->signature
);
126 * Destroy event function call of the call RCU.
128 static void destroy_event_rcu(struct rcu_head
*head
)
130 struct lttng_ht_node_u64
*node
=
131 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
132 struct ust_registry_event
*event
=
133 caa_container_of(node
, struct ust_registry_event
, node
);
135 destroy_event(event
);
139 * Find an event using the name and signature in the given registry. RCU read
140 * side lock MUST be acquired before calling this function and as long as the
141 * event reference is kept by the caller.
143 * On success, the event pointer is returned else NULL.
145 struct ust_registry_event
*ust_registry_find_event(
146 struct ust_registry_channel
*chan
, char *name
, char *sig
)
148 struct lttng_ht_node_u64
*node
;
149 struct lttng_ht_iter iter
;
150 struct ust_registry_event
*event
= NULL
;
151 struct ust_registry_event key
;
157 /* Setup key for the match function. */
158 strncpy(key
.name
, name
, sizeof(key
.name
));
159 key
.name
[sizeof(key
.name
) - 1] = '\0';
162 cds_lfht_lookup(chan
->ht
->ht
, chan
->ht
->hash_fct(&key
, lttng_ht_seed
),
163 chan
->ht
->match_fct
, &key
, &iter
.iter
);
164 node
= lttng_ht_iter_get_node_u64(&iter
);
168 event
= caa_container_of(node
, struct ust_registry_event
, node
);
175 * Create a ust_registry_event from the given parameters and add it to the
176 * registry hash table. If event_id is valid, it is set with the newly created
179 * On success, return 0 else a negative value. The created event MUST be unique
180 * so on duplicate entry -EINVAL is returned. On error, event_id is untouched.
182 * Should be called with session registry mutex held.
184 int ust_registry_create_event(struct ust_registry_session
*session
,
185 uint64_t chan_key
, int session_objd
, int channel_objd
, char *name
,
186 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
187 char *model_emf_uri
, int buffer_type
, uint32_t *event_id_p
)
191 struct cds_lfht_node
*nptr
;
192 struct ust_registry_event
*event
= NULL
;
193 struct ust_registry_channel
*chan
;
201 * This should not happen but since it comes from the UST tracer, an
202 * external party, don't assert and simply validate values.
204 if (session_objd
< 0 || channel_objd
< 0) {
211 chan
= ust_registry_channel_find(session
, chan_key
);
217 /* Check if we've reached the maximum possible id. */
218 if (ust_registry_is_max_id(chan
->used_event_id
)) {
223 event
= alloc_event(session_objd
, channel_objd
, name
, sig
, nr_fields
,
224 fields
, loglevel
, model_emf_uri
);
230 DBG3("UST registry creating event with event: %s, sig: %s, id: %u, "
231 "chan_objd: %u, sess_objd: %u, chan_id: %u", event
->name
,
232 event
->signature
, event
->id
, event
->channel_objd
,
233 event
->session_objd
, chan
->chan_id
);
236 * This is an add unique with a custom match function for event. The node
237 * are matched using the event name and signature.
239 nptr
= cds_lfht_add_unique(chan
->ht
->ht
, chan
->ht
->hash_fct(event
,
240 lttng_ht_seed
), chan
->ht
->match_fct
, event
, &event
->node
.node
);
241 if (nptr
!= &event
->node
.node
) {
242 if (buffer_type
== LTTNG_BUFFER_PER_UID
) {
244 * This is normal, we just have to send the event id of the
245 * returned node and make sure we destroy the previously allocated
248 destroy_event(event
);
249 event
= caa_container_of(nptr
, struct ust_registry_event
,
252 event_id
= event
->id
;
254 ERR("UST registry create event add unique failed for event: %s, "
255 "sig: %s, id: %u, chan_objd: %u, sess_objd: %u",
256 event
->name
, event
->signature
, event
->id
,
257 event
->channel_objd
, event
->session_objd
);
262 /* Request next event id if the node was successfully added. */
263 event_id
= event
->id
= ust_registry_get_next_event_id(chan
);
266 *event_id_p
= event_id
;
268 if (!event
->metadata_dumped
) {
269 /* Append to metadata */
270 ret
= ust_metadata_event_statedump(session
, chan
, event
);
272 ERR("Error appending event metadata (errno = %d)", ret
);
284 destroy_event(event
);
289 * For a given event in a registry, delete the entry and destroy the event.
290 * This MUST be called within a RCU read side lock section.
292 void ust_registry_destroy_event(struct ust_registry_channel
*chan
,
293 struct ust_registry_event
*event
)
296 struct lttng_ht_iter iter
;
301 /* Delete the node first. */
302 iter
.iter
.node
= &event
->node
.node
;
303 ret
= lttng_ht_del(chan
->ht
, &iter
);
306 call_rcu(&event
->node
.head
, destroy_event_rcu
);
312 * We need to execute ht_destroy outside of RCU read-side critical
313 * section, so we postpone its execution using call_rcu. It is simpler
314 * than to change the semantic of the many callers of
318 void destroy_channel_rcu(struct rcu_head
*head
)
320 struct ust_registry_channel
*chan
=
321 caa_container_of(head
, struct ust_registry_channel
, rcu_head
);
324 lttng_ht_destroy(chan
->ht
);
330 * Destroy every element of the registry and free the memory. This does NOT
331 * free the registry pointer since it might not have been allocated before so
332 * it's the caller responsability.
334 static void destroy_channel(struct ust_registry_channel
*chan
)
336 struct lttng_ht_iter iter
;
337 struct ust_registry_event
*event
;
342 /* Destroy all event associated with this registry. */
343 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter
.iter
, event
, node
.node
) {
344 /* Delete the node from the ht and free it. */
345 ust_registry_destroy_event(chan
, event
);
348 call_rcu(&chan
->rcu_head
, destroy_channel_rcu
);
352 * Initialize registry with default values.
354 int ust_registry_channel_add(struct ust_registry_session
*session
,
358 struct ust_registry_channel
*chan
;
362 chan
= zmalloc(sizeof(*chan
));
364 PERROR("zmalloc ust registry channel");
369 chan
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
375 /* Set custom match function. */
376 chan
->ht
->match_fct
= ht_match_event
;
377 chan
->ht
->hash_fct
= ht_hash_event
;
380 * Assign a channel ID right now since the event notification comes
381 * *before* the channel notify so the ID needs to be set at this point so
382 * the metadata can be dumped for that event.
384 if (ust_registry_is_max_id(session
->used_channel_id
)) {
388 chan
->chan_id
= ust_registry_get_next_chan_id(session
);
391 lttng_ht_node_init_u64(&chan
->node
, key
);
392 lttng_ht_add_unique_u64(session
->channels
, &chan
->node
);
398 destroy_channel(chan
);
404 * Find a channel in the given registry. RCU read side lock MUST be acquired
405 * before calling this function and as long as the event reference is kept by
408 * On success, the pointer is returned else NULL.
410 struct ust_registry_channel
*ust_registry_channel_find(
411 struct ust_registry_session
*session
, uint64_t key
)
413 struct lttng_ht_node_u64
*node
;
414 struct lttng_ht_iter iter
;
415 struct ust_registry_channel
*chan
= NULL
;
418 assert(session
->channels
);
420 DBG3("UST registry channel finding key %" PRIu64
, key
);
422 lttng_ht_lookup(session
->channels
, &key
, &iter
);
423 node
= lttng_ht_iter_get_node_u64(&iter
);
427 chan
= caa_container_of(node
, struct ust_registry_channel
, node
);
434 * Remove channel using key from registry and free memory.
436 void ust_registry_channel_del_free(struct ust_registry_session
*session
,
439 struct lttng_ht_iter iter
;
440 struct ust_registry_channel
*chan
;
446 chan
= ust_registry_channel_find(session
, key
);
452 iter
.iter
.node
= &chan
->node
.node
;
453 ret
= lttng_ht_del(session
->channels
, &iter
);
456 destroy_channel(chan
);
463 * Initialize registry with default values and set the newly allocated session
464 * pointer to sessionp.
466 * Return 0 on success and sessionp is set or else return -1 and sessionp is
469 int ust_registry_session_init(struct ust_registry_session
**sessionp
,
471 uint32_t bits_per_long
,
472 uint32_t uint8_t_alignment
,
473 uint32_t uint16_t_alignment
,
474 uint32_t uint32_t_alignment
,
475 uint32_t uint64_t_alignment
,
476 uint32_t long_alignment
,
482 struct ust_registry_session
*session
;
486 session
= zmalloc(sizeof(*session
));
488 PERROR("zmalloc ust registry session");
492 pthread_mutex_init(&session
->lock
, NULL
);
493 session
->bits_per_long
= bits_per_long
;
494 session
->uint8_t_alignment
= uint8_t_alignment
;
495 session
->uint16_t_alignment
= uint16_t_alignment
;
496 session
->uint32_t_alignment
= uint32_t_alignment
;
497 session
->uint64_t_alignment
= uint64_t_alignment
;
498 session
->long_alignment
= long_alignment
;
499 session
->byte_order
= byte_order
;
501 session
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
502 if (!session
->channels
) {
506 ret
= lttng_uuid_generate(session
->uuid
);
508 ERR("Failed to generate UST uuid (errno = %d)", ret
);
512 pthread_mutex_lock(&session
->lock
);
513 ret
= ust_metadata_session_statedump(session
, app
, major
, minor
);
514 pthread_mutex_unlock(&session
->lock
);
516 ERR("Failed to generate session metadata (errno = %d)", ret
);
525 ust_registry_session_destroy(session
);
531 * Destroy session registry. This does NOT free the given pointer since it
532 * might get passed as a reference. The registry lock should NOT be acquired.
534 void ust_registry_session_destroy(struct ust_registry_session
*reg
)
537 struct lttng_ht_iter iter
;
538 struct ust_registry_channel
*chan
;
540 /* On error, EBUSY can be returned if lock. Code flow error. */
541 ret
= pthread_mutex_destroy(®
->lock
);
545 /* Destroy all event associated with this registry. */
546 cds_lfht_for_each_entry(reg
->channels
->ht
, &iter
.iter
, chan
, node
.node
) {
547 /* Delete the node from the ht and free it. */
548 ret
= lttng_ht_del(reg
->channels
, &iter
);
550 destroy_channel(chan
);
555 lttng_ht_destroy(reg
->channels
);
This page took 0.042885 seconds and 4 git commands to generate.