b92345fec8debf336eed80331d95eb2b5a159dab
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.
20 #include <common/common.h>
21 #include "ust-registry.h"
24 * Hash table match function for event in the registry.
26 static int ht_match_event(struct cds_lfht_node
*node
, const void *_key
)
28 struct ust_registry_event
*event
;
29 const struct ust_registry_event
*key
;
34 event
= caa_container_of(node
, struct ust_registry_event
, node
.node
);
38 /* It has to be a perfect match. */
39 if (strncmp(event
->name
, key
->name
, sizeof(event
->name
)) != 0) {
43 /* It has to be a perfect match. */
44 if (strncmp(event
->signature
, key
->signature
,
45 strlen(event
->signature
) != 0)) {
57 * Allocate event and initialize it. This does NOT set a valid event id from a
60 static struct ust_registry_event
*alloc_event(int session_objd
,
61 int channel_objd
, char *name
, char *sig
, size_t nr_fields
,
62 struct ustctl_field
*fields
, int loglevel
, char *model_emf_uri
)
64 struct ust_registry_event
*event
= NULL
;
66 event
= zmalloc(sizeof(*event
));
68 PERROR("zmalloc ust registry event");
72 event
->session_objd
= session_objd
;
73 event
->channel_objd
= channel_objd
;
74 /* Allocated by ustctl. */
75 event
->signature
= sig
;
76 event
->nr_fields
= nr_fields
;
77 event
->fields
= fields
;
78 event
->loglevel
= loglevel
;
79 event
->model_emf_uri
= model_emf_uri
;
81 /* Copy event name and force NULL byte. */
82 strncpy(event
->name
, name
, sizeof(event
->name
));
83 event
->name
[sizeof(event
->name
) - 1] = '\0';
85 lttng_ht_node_init_str(&event
->node
, event
->name
);
92 * Free event data structure. This does NOT delete it from any hash table. It's
93 * safe to pass a NULL pointer. This shoudl be called inside a call RCU if the
94 * event is previously deleted from a rcu hash table.
96 static void destroy_event(struct ust_registry_event
*event
)
103 free(event
->model_emf_uri
);
104 free(event
->signature
);
109 * Destroy event function call of the call RCU.
111 static void destroy_event_rcu(struct rcu_head
*head
)
113 struct lttng_ht_node_str
*node
=
114 caa_container_of(head
, struct lttng_ht_node_str
, head
);
115 struct ust_registry_event
*event
=
116 caa_container_of(node
, struct ust_registry_event
, node
);
118 destroy_event(event
);
122 * Find an event using the name and signature in the given registry. RCU read
123 * side lock MUST be acquired before calling this function and as long as the
124 * event reference is kept by the caller.
126 * On success, the event pointer is returned else NULL.
128 struct ust_registry_event
*ust_registry_find_event(
129 struct ust_registry_channel
*chan
, char *name
, char *sig
)
131 struct lttng_ht_node_str
*node
;
132 struct lttng_ht_iter iter
;
133 struct ust_registry_event
*event
= NULL
;
134 struct ust_registry_event key
;
140 /* Setup key for the match function. */
141 strncpy(key
.name
, name
, sizeof(key
.name
));
142 key
.name
[sizeof(key
.name
) - 1] = '\0';
145 cds_lfht_lookup(chan
->ht
->ht
, chan
->ht
->hash_fct(name
, lttng_ht_seed
),
146 chan
->ht
->match_fct
, &key
, &iter
.iter
);
147 node
= lttng_ht_iter_get_node_str(&iter
);
151 event
= caa_container_of(node
, struct ust_registry_event
, node
);
158 * Create a ust_registry_event from the given parameters and add it to the
159 * registry hash table. If event_id is valid, it is set with the newly created
162 * On success, return 0 else a negative value. The created event MUST be unique
163 * so on duplicate entry -EINVAL is returned. On error, event_id is untouched.
165 * Should be called with session registry mutex held.
167 int ust_registry_create_event(struct ust_registry_session
*session
,
168 uint64_t chan_key
, int session_objd
, int channel_objd
, char *name
,
169 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
170 char *model_emf_uri
, uint32_t *event_id
)
173 struct cds_lfht_node
*nptr
;
174 struct ust_registry_event
*event
= NULL
;
175 struct ust_registry_channel
*chan
;
182 * This should not happen but since it comes from the UST tracer, an
183 * external party, don't assert and simply validate values.
185 if (session_objd
< 0 || channel_objd
< 0) {
192 chan
= ust_registry_channel_find(session
, chan_key
);
198 /* Check if we've reached the maximum possible id. */
199 if (ust_registry_is_max_id(chan
->used_event_id
)) {
204 event
= alloc_event(session_objd
, channel_objd
, name
, sig
, nr_fields
,
205 fields
, loglevel
, model_emf_uri
);
211 event
->id
= ust_registry_get_next_event_id(chan
);
213 DBG3("UST registry creating event with event: %s, sig: %s, id: %u, "
214 "chan_objd: %u, sess_objd: %u", event
->name
, event
->signature
,
215 event
->id
, event
->channel_objd
, event
->session_objd
);
218 * This is an add unique with a custom match function for event. The node
219 * are matched using the event name and signature.
221 nptr
= cds_lfht_add_unique(chan
->ht
->ht
, chan
->ht
->hash_fct(event
->node
.key
,
222 lttng_ht_seed
), chan
->ht
->match_fct
, event
, &event
->node
.node
);
223 if (nptr
!= &event
->node
.node
) {
224 ERR("UST registry create event add unique failed for event: %s, "
225 "sig: %s, id: %u, chan_objd: %u, sess_objd: %u", event
->name
,
226 event
->signature
, event
->id
, event
->channel_objd
,
227 event
->session_objd
);
232 /* Set event id if user wants it. */
234 *event_id
= event
->id
;
237 /* Append to metadata */
238 ret
= ust_metadata_event_statedump(session
, chan
, event
);
240 ERR("Error appending event metadata (errno = %d)", ret
);
251 destroy_event(event
);
256 * For a given event in a registry, delete the entry and destroy the event.
257 * This MUST be called within a RCU read side lock section.
259 void ust_registry_destroy_event(struct ust_registry_channel
*chan
,
260 struct ust_registry_event
*event
)
263 struct lttng_ht_iter iter
;
268 /* Delete the node first. */
269 iter
.iter
.node
= &event
->node
.node
;
270 ret
= lttng_ht_del(chan
->ht
, &iter
);
273 call_rcu(&event
->node
.head
, destroy_event_rcu
);
279 * Destroy every element of the registry and free the memory. This does NOT
280 * free the registry pointer since it might not have been allocated before so
281 * it's the caller responsability.
283 * This MUST be called within a RCU read side lock section.
285 static void destroy_channel(struct ust_registry_channel
*chan
)
287 struct lttng_ht_iter iter
;
288 struct ust_registry_event
*event
;
292 /* Destroy all event associated with this registry. */
293 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter
.iter
, event
, node
.node
) {
294 /* Delete the node from the ht and free it. */
295 ust_registry_destroy_event(chan
, event
);
297 lttng_ht_destroy(chan
->ht
);
303 * Initialize registry with default values.
305 int ust_registry_channel_add(struct ust_registry_session
*session
,
309 struct ust_registry_channel
*chan
;
313 chan
= zmalloc(sizeof(*chan
));
315 PERROR("zmalloc ust registry channel");
320 chan
->ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
326 /* Set custom match function. */
327 chan
->ht
->match_fct
= ht_match_event
;
330 lttng_ht_node_init_u64(&chan
->node
, key
);
331 lttng_ht_add_unique_u64(session
->channels
, &chan
->node
);
339 * Find a channel in the given registry. RCU read side lock MUST be acquired
340 * before calling this function and as long as the event reference is kept by
343 * On success, the pointer is returned else NULL.
345 struct ust_registry_channel
*ust_registry_channel_find(
346 struct ust_registry_session
*session
, uint64_t key
)
348 struct lttng_ht_node_u64
*node
;
349 struct lttng_ht_iter iter
;
350 struct ust_registry_channel
*chan
= NULL
;
353 assert(session
->channels
);
355 lttng_ht_lookup(session
->channels
, &key
, &iter
);
356 node
= lttng_ht_iter_get_node_u64(&iter
);
360 chan
= caa_container_of(node
, struct ust_registry_channel
, node
);
367 * Remove channel using key from registry and free memory.
369 void ust_registry_channel_del_free(struct ust_registry_session
*session
,
372 struct lttng_ht_iter iter
;
373 struct ust_registry_channel
*chan
;
378 chan
= ust_registry_channel_find(session
, key
);
383 iter
.iter
.node
= &chan
->node
.node
;
384 lttng_ht_del(session
->channels
, &iter
);
386 destroy_channel(chan
);
394 * Initialize registry with default values and set the newly allocated session
395 * pointer to sessionp.
397 * Return 0 on success and sessionp is set or else return -1 and sessionp is
400 int ust_registry_session_init(struct ust_registry_session
**sessionp
,
402 uint32_t bits_per_long
,
403 uint32_t uint8_t_alignment
,
404 uint32_t uint16_t_alignment
,
405 uint32_t uint32_t_alignment
,
406 uint32_t uint64_t_alignment
,
407 uint32_t long_alignment
,
411 struct ust_registry_session
*session
;
416 session
= zmalloc(sizeof(*session
));
418 PERROR("zmalloc ust registry session");
422 pthread_mutex_init(&session
->lock
, NULL
);
423 session
->bits_per_long
= bits_per_long
;
424 session
->uint8_t_alignment
= uint8_t_alignment
;
425 session
->uint16_t_alignment
= uint16_t_alignment
;
426 session
->uint32_t_alignment
= uint32_t_alignment
;
427 session
->uint64_t_alignment
= uint64_t_alignment
;
428 session
->long_alignment
= long_alignment
;
429 session
->byte_order
= byte_order
;
431 session
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
432 if (!session
->channels
) {
436 ret
= lttng_uuid_generate(session
->uuid
);
438 ERR("Failed to generate UST uuid (errno = %d)", ret
);
442 pthread_mutex_lock(&session
->lock
);
443 ret
= ust_metadata_session_statedump(session
, app
);
444 pthread_mutex_unlock(&session
->lock
);
446 ERR("Failed to generate session metadata (errno = %d)", ret
);
459 * Destroy session registry. This does NOT free the given pointer since it
460 * might get passed as a reference. The registry lock should NOT be acquired.
462 void ust_registry_session_destroy(struct ust_registry_session
*reg
)
465 struct lttng_ht_iter iter
;
466 struct ust_registry_channel
*chan
;
468 /* On error, EBUSY can be returned if lock. Code flow error. */
469 ret
= pthread_mutex_destroy(®
->lock
);
473 /* Destroy all event associated with this registry. */
474 cds_lfht_for_each_entry(reg
->channels
->ht
, &iter
.iter
, chan
, node
.node
) {
475 /* Delete the node from the ht and free it. */
476 ret
= lttng_ht_del(reg
->channels
, &iter
);
478 destroy_channel(chan
);
480 lttng_ht_destroy(reg
->channels
);
This page took 0.043537 seconds and 4 git commands to generate.