2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
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., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <lttng-share.h>
27 #include "hashtable.h"
28 #include "trace-ust.h"
31 * Find the channel in the hashtable.
33 struct ltt_ust_channel
*trace_ust_find_channel_by_name(struct cds_lfht
*ht
,
36 struct cds_lfht_node
*node
;
37 struct cds_lfht_iter iter
;
40 node
= hashtable_lookup(ht
, (void *) name
, strlen(name
), &iter
);
47 DBG2("Trace UST channel %s found by name", name
);
49 return caa_container_of(node
, struct ltt_ust_channel
, node
);
52 DBG2("Trace UST channel %s not found by name", name
);
57 * Find the event in the hashtable.
59 struct ltt_ust_event
*trace_ust_find_event_by_name(struct cds_lfht
*ht
,
62 struct cds_lfht_node
*node
;
63 struct cds_lfht_iter iter
;
66 node
= hashtable_lookup(ht
, (void *) name
, strlen(name
), &iter
);
73 DBG2("Found UST event by name %s", name
);
75 return caa_container_of(node
, struct ltt_ust_event
, node
);
82 * Allocate and initialize a ust session data structure.
84 * Return pointer to structure or NULL.
86 struct ltt_ust_session
*trace_ust_create_session(char *path
, unsigned int uid
,
87 struct lttng_domain
*domain
)
90 struct ltt_ust_session
*lus
;
92 /* Allocate a new ltt ust session */
93 lus
= malloc(sizeof(struct ltt_ust_session
));
95 perror("create ust session malloc");
99 /* Init data structure */
100 lus
->consumer_fds_sent
= 0;
103 /* Alloc UST domain hash tables */
104 lus
->domain_pid
= hashtable_new(0);
105 lus
->domain_exec
= hashtable_new_str(0);
107 /* Alloc UST global domain channels' HT */
108 lus
->domain_global
.channels
= hashtable_new_str(0);
110 /* Set session path */
111 ret
= snprintf(lus
->pathname
, PATH_MAX
, "%s/ust", path
);
113 PERROR("snprintf kernel traces path");
117 DBG2("UST trace session create successful");
126 * Allocate and initialize a ust channel data structure.
128 * Return pointer to structure or NULL.
130 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
134 struct ltt_ust_channel
*luc
;
136 luc
= malloc(sizeof(struct ltt_ust_channel
));
138 perror("ltt_ust_channel malloc");
142 /* Copy UST channel attributes */
143 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
144 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
145 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
146 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
147 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
148 luc
->attr
.output
= chan
->attr
.output
;
150 /* Translate to UST output enum */
151 switch (luc
->attr
.output
) {
153 luc
->attr
.output
= LTTNG_UST_MMAP
;
157 /* Copy channel name */
158 strncpy(luc
->name
, chan
->name
, sizeof(&luc
->name
));
159 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
162 hashtable_node_init(&luc
->node
, (void *) luc
->name
, strlen(luc
->name
));
163 /* Alloc hash tables */
164 luc
->events
= hashtable_new_str(0);
165 luc
->ctx
= hashtable_new_str(0);
167 /* Set trace output path */
168 ret
= snprintf(luc
->pathname
, PATH_MAX
, "%s", path
);
170 perror("asprintf ust create channel");
174 DBG2("Trace UST channel %s created", luc
->name
);
183 * Allocate and initialize a ust event. Set name and event type.
185 * Return pointer to structure or NULL.
187 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
189 struct ltt_ust_event
*lue
;
191 lue
= malloc(sizeof(struct ltt_ust_event
));
193 PERROR("ust event malloc");
198 case LTTNG_EVENT_PROBE
:
199 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
201 case LTTNG_EVENT_FUNCTION
:
202 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
204 case LTTNG_EVENT_FUNCTION_ENTRY
:
205 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
207 case LTTNG_EVENT_TRACEPOINT
:
208 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
211 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
215 /* Copy event name */
216 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
217 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
220 hashtable_node_init(&lue
->node
, (void *) lue
->attr
.name
,
221 strlen(lue
->attr
.name
));
222 /* Alloc context hash tables */
223 lue
->ctx
= hashtable_new_str(5);
232 * Allocate and initialize a ust metadata.
234 * Return pointer to structure or NULL.
236 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
239 struct ltt_ust_metadata
*lum
;
241 lum
= malloc(sizeof(struct ltt_ust_metadata
));
243 perror("ust metadata malloc");
247 /* Set default attributes */
248 lum
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
249 lum
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
250 lum
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
251 lum
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
252 lum
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
253 lum
->attr
.output
= DEFAULT_UST_CHANNEL_OUTPUT
;
256 /* Set metadata trace path */
257 ret
= snprintf(lum
->pathname
, PATH_MAX
, "%s/metadata", path
);
259 perror("asprintf ust metadata");
270 * RCU safe free context structure.
272 static void destroy_context_rcu(struct rcu_head
*head
)
274 struct cds_lfht_node
*node
=
275 caa_container_of(head
, struct cds_lfht_node
, head
);
276 struct ltt_ust_context
*ctx
=
277 caa_container_of(node
, struct ltt_ust_context
, node
);
283 * Cleanup UST context hash table.
285 static void destroy_context(struct cds_lfht
*ht
)
288 struct cds_lfht_node
*node
;
289 struct cds_lfht_iter iter
;
291 hashtable_get_first(ht
, &iter
);
292 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
293 ret
= hashtable_del(ht
, &iter
);
295 call_rcu(&node
->head
, destroy_context_rcu
);
297 hashtable_get_next(ht
, &iter
);
302 * Cleanup ust event structure.
304 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
306 DBG2("Trace destroy UST event %s", event
->attr
.name
);
307 destroy_context(event
->ctx
);
312 * URCU intermediate call to complete destroy event.
314 static void destroy_event_rcu(struct rcu_head
*head
)
316 struct cds_lfht_node
*node
=
317 caa_container_of(head
, struct cds_lfht_node
, head
);
318 struct ltt_ust_event
*event
=
319 caa_container_of(node
, struct ltt_ust_event
, node
);
321 trace_ust_destroy_event(event
);
325 * Cleanup ust channel structure.
327 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
330 struct cds_lfht_node
*node
;
331 struct cds_lfht_iter iter
;
333 DBG2("Trace destroy UST channel %s", channel
->name
);
337 hashtable_get_first(channel
->events
, &iter
);
338 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
339 ret
= hashtable_del(channel
->events
, &iter
);
341 call_rcu(&node
->head
, destroy_event_rcu
);
343 hashtable_get_next(channel
->events
, &iter
);
346 free(channel
->events
);
347 destroy_context(channel
->ctx
);
354 * URCU intermediate call to complete destroy channel.
356 static void destroy_channel_rcu(struct rcu_head
*head
)
358 struct cds_lfht_node
*node
=
359 caa_container_of(head
, struct cds_lfht_node
, head
);
360 struct ltt_ust_channel
*channel
=
361 caa_container_of(node
, struct ltt_ust_channel
, node
);
363 trace_ust_destroy_channel(channel
);
367 * Cleanup ust metadata structure.
369 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
371 DBG2("Trace UST destroy metadata %d", metadata
->handle
);
377 * Iterate over a hash table containing channels and cleanup safely.
379 static void destroy_channels(struct cds_lfht
*channels
)
382 struct cds_lfht_node
*node
;
383 struct cds_lfht_iter iter
;
385 hashtable_get_first(channels
, &iter
);
386 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
387 ret
= hashtable_del(channels
, &iter
);
389 call_rcu(&node
->head
, destroy_channel_rcu
);
391 hashtable_get_next(channels
, &iter
);
396 * Cleanup UST pid domain.
398 static void destroy_domain_pid(struct cds_lfht
*ht
)
401 struct cds_lfht_node
*node
;
402 struct cds_lfht_iter iter
;
403 struct ltt_ust_domain_pid
*d
;
405 hashtable_get_first(ht
, &iter
);
406 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
407 ret
= hashtable_del(ht
, &iter
);
409 d
= caa_container_of(node
, struct ltt_ust_domain_pid
, node
);
410 destroy_channels(d
->channels
);
412 hashtable_get_next(ht
, &iter
);
417 * Cleanup UST exec name domain.
419 static void destroy_domain_exec(struct cds_lfht
*ht
)
422 struct cds_lfht_node
*node
;
423 struct cds_lfht_iter iter
;
424 struct ltt_ust_domain_exec
*d
;
426 hashtable_get_first(ht
, &iter
);
427 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
428 ret
= hashtable_del(ht
, &iter
);
430 d
= caa_container_of(node
, struct ltt_ust_domain_exec
, node
);
431 destroy_channels(d
->channels
);
433 hashtable_get_next(ht
, &iter
);
438 * Cleanup UST global domain.
440 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
442 destroy_channels(dom
->channels
);
446 * Cleanup ust session structure
448 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
451 if (session
== NULL
) {
457 DBG2("Trace UST destroy session %d", session
->uid
);
459 //if (session->metadata != NULL) {
460 // trace_ust_destroy_metadata(session->metadata);
463 /* Cleaning up UST domain */
464 destroy_domain_global(&session
->domain_global
);
465 destroy_domain_pid(session
->domain_pid
);
466 destroy_domain_exec(session
->domain_exec
);