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("Trace UST event found by name %s", name
);
75 return caa_container_of(node
, struct ltt_ust_event
, node
);
78 DBG2("Trace UST event NOT found by name %s", name
);
83 * Allocate and initialize a ust session data structure.
85 * Return pointer to structure or NULL.
87 struct ltt_ust_session
*trace_ust_create_session(char *path
, unsigned int uid
,
88 struct lttng_domain
*domain
)
91 struct ltt_ust_session
*lus
;
93 /* Allocate a new ltt ust session */
94 lus
= zmalloc(sizeof(struct ltt_ust_session
));
96 PERROR("create ust session zmalloc");
100 /* Init data structure */
101 lus
->consumer_fds_sent
= 0;
103 lus
->start_trace
= 0;
105 /* Alloc UST domain hash tables */
106 lus
->domain_pid
= hashtable_new(0);
107 lus
->domain_exec
= hashtable_new_str(0);
109 /* Alloc UST global domain channels' HT */
110 lus
->domain_global
.channels
= hashtable_new_str(0);
112 /* Set session path */
113 ret
= snprintf(lus
->pathname
, PATH_MAX
, "%s/ust", path
);
115 PERROR("snprintf kernel traces path");
116 goto error_free_session
;
119 DBG2("UST trace session create successful");
130 * Allocate and initialize a ust channel data structure.
132 * Return pointer to structure or NULL.
134 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
138 struct ltt_ust_channel
*luc
;
140 luc
= zmalloc(sizeof(struct ltt_ust_channel
));
142 perror("ltt_ust_channel zmalloc");
146 /* Copy UST channel attributes */
147 luc
->attr
.overwrite
= chan
->attr
.overwrite
;
148 luc
->attr
.subbuf_size
= chan
->attr
.subbuf_size
;
149 luc
->attr
.num_subbuf
= chan
->attr
.num_subbuf
;
150 luc
->attr
.switch_timer_interval
= chan
->attr
.switch_timer_interval
;
151 luc
->attr
.read_timer_interval
= chan
->attr
.read_timer_interval
;
152 luc
->attr
.output
= chan
->attr
.output
;
154 /* Translate to UST output enum */
155 switch (luc
->attr
.output
) {
157 luc
->attr
.output
= LTTNG_UST_MMAP
;
161 /* Copy channel name */
162 strncpy(luc
->name
, chan
->name
, sizeof(&luc
->name
));
163 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
166 hashtable_node_init(&luc
->node
, (void *) luc
->name
, strlen(luc
->name
));
167 /* Alloc hash tables */
168 luc
->events
= hashtable_new_str(0);
169 luc
->ctx
= hashtable_new_str(0);
171 /* Set trace output path */
172 ret
= snprintf(luc
->pathname
, PATH_MAX
, "%s", path
);
174 perror("asprintf ust create channel");
175 goto error_free_channel
;
178 DBG2("Trace UST channel %s created", luc
->name
);
189 * Allocate and initialize a ust event. Set name and event type.
191 * Return pointer to structure or NULL.
193 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
195 struct ltt_ust_event
*lue
;
197 lue
= zmalloc(sizeof(struct ltt_ust_event
));
199 PERROR("ust event zmalloc");
204 case LTTNG_EVENT_PROBE
:
205 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
207 case LTTNG_EVENT_FUNCTION
:
208 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
210 case LTTNG_EVENT_FUNCTION_ENTRY
:
211 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
213 case LTTNG_EVENT_TRACEPOINT
:
214 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
217 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
218 goto error_free_event
;
221 /* Copy event name */
222 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
223 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
226 hashtable_node_init(&lue
->node
, (void *) lue
->attr
.name
,
227 strlen(lue
->attr
.name
));
228 /* Alloc context hash tables */
229 lue
->ctx
= hashtable_new_str(0);
231 DBG2("Trace UST event %s created", lue
->attr
.name
);
242 * Allocate and initialize a ust metadata.
244 * Return pointer to structure or NULL.
246 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
249 struct ltt_ust_metadata
*lum
;
251 lum
= zmalloc(sizeof(struct ltt_ust_metadata
));
253 perror("ust metadata zmalloc");
257 /* Set default attributes */
258 lum
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
259 lum
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
260 lum
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
261 lum
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
262 lum
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
263 lum
->attr
.output
= LTTNG_UST_MMAP
;
266 /* Set metadata trace path */
267 ret
= snprintf(lum
->pathname
, PATH_MAX
, "%s/metadata", path
);
269 perror("asprintf ust metadata");
270 goto error_free_metadata
;
282 * RCU safe free context structure.
284 static void destroy_context_rcu(struct rcu_head
*head
)
286 struct cds_lfht_node
*node
=
287 caa_container_of(head
, struct cds_lfht_node
, head
);
288 struct ltt_ust_context
*ctx
=
289 caa_container_of(node
, struct ltt_ust_context
, node
);
295 * Cleanup UST context hash table.
297 static void destroy_context(struct cds_lfht
*ht
)
300 struct cds_lfht_node
*node
;
301 struct cds_lfht_iter iter
;
303 cds_lfht_for_each(ht
, &iter
, node
) {
304 ret
= hashtable_del(ht
, &iter
);
306 call_rcu(&node
->head
, destroy_context_rcu
);
310 hashtable_destroy(ht
);
314 * Cleanup ust event structure.
316 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
318 DBG2("Trace destroy UST event %s", event
->attr
.name
);
319 destroy_context(event
->ctx
);
325 * URCU intermediate call to complete destroy event.
327 static void destroy_event_rcu(struct rcu_head
*head
)
329 struct cds_lfht_node
*node
=
330 caa_container_of(head
, struct cds_lfht_node
, head
);
331 struct ltt_ust_event
*event
=
332 caa_container_of(node
, struct ltt_ust_event
, node
);
334 trace_ust_destroy_event(event
);
338 * Cleanup UST events hashtable.
340 static void destroy_event(struct cds_lfht
*events
)
343 struct cds_lfht_node
*node
;
344 struct cds_lfht_iter iter
;
346 cds_lfht_for_each(events
, &iter
, node
) {
347 ret
= hashtable_del(events
, &iter
);
349 call_rcu(&node
->head
, destroy_event_rcu
);
353 hashtable_destroy(events
);
357 * Cleanup ust channel structure.
359 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
362 struct cds_lfht_node
*node
;
363 struct cds_lfht_iter iter
;
365 DBG2("Trace destroy UST channel %s", channel
->name
);
369 cds_lfht_for_each(channel
->events
, &iter
, node
) {
370 ret
= hashtable_del(channel
->events
, &iter
);
372 destroy_event(channel
->events
);
376 destroy_context(channel
->ctx
);
383 * URCU intermediate call to complete destroy channel.
385 static void destroy_channel_rcu(struct rcu_head
*head
)
387 struct cds_lfht_node
*node
=
388 caa_container_of(head
, struct cds_lfht_node
, head
);
389 struct ltt_ust_channel
*channel
=
390 caa_container_of(node
, struct ltt_ust_channel
, node
);
392 trace_ust_destroy_channel(channel
);
396 * Cleanup ust metadata structure.
398 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
400 DBG2("Trace UST destroy metadata %d", metadata
->handle
);
406 * Iterate over a hash table containing channels and cleanup safely.
408 static void destroy_channels(struct cds_lfht
*channels
)
411 struct cds_lfht_node
*node
;
412 struct cds_lfht_iter iter
;
414 cds_lfht_for_each(channels
, &iter
, node
) {
415 ret
= hashtable_del(channels
, &iter
);
417 call_rcu(&node
->head
, destroy_channel_rcu
);
421 hashtable_destroy(channels
);
425 * Cleanup UST pid domain.
427 static void destroy_domain_pid(struct cds_lfht
*ht
)
430 struct cds_lfht_iter iter
;
431 struct ltt_ust_domain_pid
*dpid
;
433 cds_lfht_for_each_entry(ht
, &iter
, dpid
, node
) {
434 ret
= hashtable_del(ht
, &iter
);
436 destroy_channels(dpid
->channels
);
440 hashtable_destroy(ht
);
444 * Cleanup UST exec name domain.
446 static void destroy_domain_exec(struct cds_lfht
*ht
)
449 struct cds_lfht_iter iter
;
450 struct ltt_ust_domain_exec
*dexec
;
452 cds_lfht_for_each_entry(ht
, &iter
, dexec
, node
) {
453 ret
= hashtable_del(ht
, &iter
);
455 destroy_channels(dexec
->channels
);
459 hashtable_destroy(ht
);
463 * Cleanup UST global domain.
465 static void destroy_domain_global(struct ltt_ust_domain_global
*dom
)
467 destroy_channels(dom
->channels
);
471 * Cleanup ust session structure
473 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
475 /* Extra protection */
476 if (session
== NULL
) {
482 DBG2("Trace UST destroy session %d", session
->uid
);
484 /* Cleaning up UST domain */
485 destroy_domain_global(&session
->domain_global
);
486 destroy_domain_pid(session
->domain_pid
);
487 destroy_domain_exec(session
->domain_exec
);