Update RCU hashtable from upstream urcu
[lttng-tools.git] / ltt-sessiond / trace-ust.c
CommitLineData
97ee3a89
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18#define _GNU_SOURCE
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24#include <lttngerr.h>
25#include <lttng-share.h>
26
f6a9efaa 27#include "hashtable.h"
97ee3a89
DG
28#include "trace-ust.h"
29
0177d773 30/*
f6a9efaa 31 * Find the channel in the hashtable.
0177d773 32 */
f6a9efaa
DG
33struct ltt_ust_channel *trace_ust_find_channel_by_name(struct cds_lfht *ht,
34 char *name)
0177d773 35{
f6a9efaa
DG
36 struct cds_lfht_node *node;
37 struct cds_lfht_iter iter;
0177d773 38
f6a9efaa
DG
39 rcu_read_lock();
40 node = hashtable_lookup(ht, (void *) name, strlen(name), &iter);
41 if (node == NULL) {
42 rcu_read_unlock();
44d3bd01
DG
43 goto error;
44 }
f6a9efaa 45 rcu_read_unlock();
44d3bd01 46
f6a9efaa 47 DBG2("Trace UST channel %s found by name", name);
0177d773 48
f6a9efaa 49 return caa_container_of(node, struct ltt_ust_channel, node);
97ee3a89
DG
50
51error:
f6a9efaa 52 DBG2("Trace UST channel %s not found by name", name);
97ee3a89
DG
53 return NULL;
54}
55
56/*
f6a9efaa 57 * Find the event in the hashtable.
97ee3a89 58 */
f6a9efaa
DG
59struct ltt_ust_event *trace_ust_find_event_by_name(struct cds_lfht *ht,
60 char *name)
97ee3a89 61{
f6a9efaa
DG
62 struct cds_lfht_node *node;
63 struct cds_lfht_iter iter;
97ee3a89 64
f6a9efaa
DG
65 rcu_read_lock();
66 node = hashtable_lookup(ht, (void *) name, strlen(name), &iter);
67 if (node == NULL) {
68 rcu_read_unlock();
97ee3a89
DG
69 goto error;
70 }
f6a9efaa 71 rcu_read_unlock();
97ee3a89 72
f6a9efaa
DG
73 DBG2("Found UST event by name %s", name);
74
75 return caa_container_of(node, struct ltt_ust_event, node);
97ee3a89
DG
76
77error:
78 return NULL;
79}
80
81/*
82 * Allocate and initialize a ust session data structure.
83 *
84 * Return pointer to structure or NULL.
85 */
44d3bd01
DG
86struct ltt_ust_session *trace_ust_create_session(char *path, pid_t pid,
87 struct lttng_domain *domain)
97ee3a89 88{
0177d773 89 int ret;
97ee3a89
DG
90 struct ltt_ust_session *lus;
91
92 /* Allocate a new ltt ust session */
93 lus = malloc(sizeof(struct ltt_ust_session));
94 if (lus == NULL) {
95 perror("create ust session malloc");
96 goto error;
97 }
98
99 /* Init data structure */
100 lus->handle = -1;
101 lus->enabled = 1;
3bd1e081 102 lus->consumer_fds_sent = 0;
97ee3a89 103 lus->metadata = NULL;
97ee3a89 104
f6a9efaa
DG
105 /* Alloc UST domain hash tables */
106 lus->domain_pid = hashtable_new(0);
107 lus->domain_exec = hashtable_new_str(0);
108
109 /* Alloc UST global domain channels' HT */
110 lus->domain_global.channels = hashtable_new_str(0);
44d3bd01 111
0177d773 112 /* Set session path */
44d3bd01 113 ret = snprintf(lus->path, PATH_MAX, "%s/ust_%d", path, pid);
0177d773 114 if (ret < 0) {
44d3bd01 115 PERROR("snprintf kernel traces path");
0177d773
DG
116 goto error;
117 }
118
44d3bd01
DG
119 DBG2("UST trace session create successful");
120
97ee3a89
DG
121 return lus;
122
123error:
124 return NULL;
125}
126
127/*
128 * Allocate and initialize a ust channel data structure.
129 *
130 * Return pointer to structure or NULL.
131 */
44d3bd01
DG
132struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
133 char *path)
97ee3a89
DG
134{
135 int ret;
136 struct ltt_ust_channel *luc;
137
138 luc = malloc(sizeof(struct ltt_ust_channel));
139 if (luc == NULL) {
140 perror("ltt_ust_channel malloc");
141 goto error;
142 }
143
44d3bd01
DG
144 /* Copy UST channel attributes */
145 memcpy(&luc->attr, &chan->attr, sizeof(struct lttng_ust_channel));
146
147 /* Translate to UST output enum */
148 switch (luc->attr.output) {
149 default:
150 luc->attr.output = LTTNG_UST_MMAP;
151 break;
97ee3a89 152 }
97ee3a89
DG
153
154 luc->handle = -1;
155 luc->enabled = 1;
44d3bd01 156
97ee3a89 157 /* Copy channel name */
44d3bd01 158 strncpy(luc->name, chan->name, sizeof(&luc->name));
97ee3a89
DG
159 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
160
f6a9efaa
DG
161 /* Init node */
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);
166
97ee3a89 167 /* Set trace output path */
44d3bd01 168 ret = snprintf(luc->trace_path, PATH_MAX, "%s", path);
97ee3a89
DG
169 if (ret < 0) {
170 perror("asprintf ust create channel");
171 goto error;
172 }
2bdd86d4 173 CDS_INIT_LIST_HEAD(&luc->stream_list.head);
97ee3a89 174
f6a9efaa
DG
175 DBG2("Trace UST channel %s created", luc->name);
176
97ee3a89
DG
177 return luc;
178
179error:
180 return NULL;
181}
182
183/*
184 * Allocate and initialize a ust event. Set name and event type.
185 *
186 * Return pointer to structure or NULL.
187 */
188struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
189{
190 struct ltt_ust_event *lue;
97ee3a89
DG
191
192 lue = malloc(sizeof(struct ltt_ust_event));
44d3bd01
DG
193 if (lue == NULL) {
194 PERROR("ust event malloc");
97ee3a89
DG
195 goto error;
196 }
197
198 switch (ev->type) {
199 case LTTNG_EVENT_PROBE:
44d3bd01 200 lue->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
201 break;
202 case LTTNG_EVENT_FUNCTION:
44d3bd01 203 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
204 break;
205 case LTTNG_EVENT_FUNCTION_ENTRY:
44d3bd01 206 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
207 break;
208 case LTTNG_EVENT_TRACEPOINT:
44d3bd01 209 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89
DG
210 break;
211 default:
212 ERR("Unknown ust instrumentation type (%d)", ev->type);
213 goto error;
214 }
215
216 /* Copy event name */
44d3bd01
DG
217 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
218 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89
DG
219
220 /* Setting up a ust event */
221 lue->handle = -1;
97ee3a89 222 lue->enabled = 1;
f6a9efaa
DG
223
224 /* Init node */
225 hashtable_node_init(&lue->node, (void *) lue->attr.name,
226 strlen(lue->attr.name));
227 /* Alloc context hash tables */
228 lue->ctx = hashtable_new_str(5);
97ee3a89
DG
229
230 return lue;
231
232error:
233 return NULL;
234}
235
236/*
237 * Allocate and initialize a ust metadata.
238 *
239 * Return pointer to structure or NULL.
240 */
241struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
242{
243 int ret;
244 struct ltt_ust_metadata *lum;
97ee3a89
DG
245
246 lum = malloc(sizeof(struct ltt_ust_metadata));
44d3bd01 247 if (lum == NULL) {
97ee3a89
DG
248 perror("ust metadata malloc");
249 goto error;
250 }
251
252 /* Set default attributes */
44d3bd01
DG
253 lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
254 lum->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
255 lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
256 lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
257 lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
258 lum->attr.output = DEFAULT_UST_CHANNEL_OUTPUT;
259
97ee3a89
DG
260 lum->handle = -1;
261 /* Set metadata trace path */
f6a9efaa 262 ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path);
97ee3a89
DG
263 if (ret < 0) {
264 perror("asprintf ust metadata");
265 goto error;
266 }
267
268 return lum;
269
270error:
271 return NULL;
272}
273
f6a9efaa
DG
274/*
275 * RCU safe free context structure.
276 */
277static void destroy_context_rcu(struct rcu_head *head)
278{
279 struct cds_lfht_node *node =
280 caa_container_of(head, struct cds_lfht_node, head);
281 struct ltt_ust_context *ctx =
282 caa_container_of(node, struct ltt_ust_context, node);
283
284 free(ctx);
285}
286
287/*
288 * Cleanup UST context hash table.
289 */
290static void destroy_context(struct cds_lfht *ht)
291{
292 int ret;
293 struct cds_lfht_node *node;
294 struct cds_lfht_iter iter;
295
296 hashtable_get_first(ht, &iter);
297 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
298 ret = hashtable_del(ht, &iter);
299 if (!ret) {
300 call_rcu(&node->head, destroy_context_rcu);
301 }
302 hashtable_get_next(ht, &iter);
303 }
304}
305
97ee3a89
DG
306/*
307 * Cleanup ust event structure.
308 */
309void trace_ust_destroy_event(struct ltt_ust_event *event)
310{
f6a9efaa
DG
311 DBG2("Trace destroy UST event %s", event->attr.name);
312 destroy_context(event->ctx);
97ee3a89
DG
313 free(event);
314}
315
f6a9efaa
DG
316/*
317 * URCU intermediate call to complete destroy event.
318 */
319static void destroy_event_rcu(struct rcu_head *head)
320{
321 struct cds_lfht_node *node =
322 caa_container_of(head, struct cds_lfht_node, head);
323 struct ltt_ust_event *event =
324 caa_container_of(node, struct ltt_ust_event, node);
325
326 trace_ust_destroy_event(event);
327}
328
97ee3a89
DG
329/*
330 * Cleanup ust channel structure.
331 */
332void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
333{
f6a9efaa
DG
334 int ret;
335 struct cds_lfht_node *node;
336 struct cds_lfht_iter iter;
97ee3a89 337
f6a9efaa 338 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 339
f6a9efaa
DG
340 rcu_read_lock();
341
342 hashtable_get_first(channel->events, &iter);
343 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
344 ret = hashtable_del(channel->events, &iter);
345 if (!ret) {
346 call_rcu(&node->head, destroy_event_rcu);
347 }
348 hashtable_get_next(channel->events, &iter);
97ee3a89
DG
349 }
350
f6a9efaa
DG
351 free(channel->events);
352 destroy_context(channel->ctx);
97ee3a89 353 free(channel);
f6a9efaa
DG
354
355 rcu_read_unlock();
356}
357
358/*
359 * URCU intermediate call to complete destroy channel.
360 */
361static void destroy_channel_rcu(struct rcu_head *head)
362{
363 struct cds_lfht_node *node =
364 caa_container_of(head, struct cds_lfht_node, head);
365 struct ltt_ust_channel *channel =
366 caa_container_of(node, struct ltt_ust_channel, node);
367
368 trace_ust_destroy_channel(channel);
97ee3a89
DG
369}
370
371/*
372 * Cleanup ust metadata structure.
373 */
374void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
375{
f6a9efaa 376 DBG2("Trace UST destroy metadata %d", metadata->handle);
97ee3a89 377
97ee3a89
DG
378 free(metadata);
379}
380
381/*
f6a9efaa 382 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 383 */
f6a9efaa
DG
384static void destroy_channels(struct cds_lfht *channels)
385{
386 int ret;
387 struct cds_lfht_node *node;
388 struct cds_lfht_iter iter;
389
390 hashtable_get_first(channels, &iter);
391 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
392 ret = hashtable_del(channels, &iter);
393 if (!ret) {
394 call_rcu(&node->head, destroy_channel_rcu);
395 }
396 hashtable_get_next(channels, &iter);
397 }
398}
399
400/*
401 * Cleanup UST pid domain.
402 */
403static void destroy_domain_pid(struct cds_lfht *ht)
404{
405 int ret;
406 struct cds_lfht_node *node;
407 struct cds_lfht_iter iter;
408 struct ltt_ust_domain_pid *d;
409
410 hashtable_get_first(ht, &iter);
411 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
412 ret = hashtable_del(ht , &iter);
413 if (!ret) {
414 d = caa_container_of(node, struct ltt_ust_domain_pid, node);
415 destroy_channels(d->channels);
416 }
417 hashtable_get_next(ht, &iter);
418 }
419}
420
421/*
422 * Cleanup UST exec name domain.
423 */
424static void destroy_domain_exec(struct cds_lfht *ht)
97ee3a89 425{
f6a9efaa
DG
426 int ret;
427 struct cds_lfht_node *node;
428 struct cds_lfht_iter iter;
429 struct ltt_ust_domain_exec *d;
430
431 hashtable_get_first(ht, &iter);
432 while ((node = hashtable_iter_get_node(&iter)) != NULL) {
433 ret = hashtable_del(ht , &iter);
434 if (!ret) {
435 d = caa_container_of(node, struct ltt_ust_domain_exec, node);
436 destroy_channels(d->channels);
437 }
438 hashtable_get_next(ht, &iter);
439 }
440}
97ee3a89 441
f6a9efaa
DG
442/*
443 * Cleanup UST global domain.
444 */
445static void destroy_domain_global(struct ltt_ust_domain_global *dom)
446{
447 destroy_channels(dom->channels);
448}
97ee3a89 449
f6a9efaa
DG
450/*
451 * Cleanup ust session structure
452 */
453void trace_ust_destroy_session(struct ltt_ust_session *session)
454{
97ee3a89
DG
455 /* Extra safety */
456 if (session == NULL) {
457 return;
458 }
459
f6a9efaa
DG
460 rcu_read_lock();
461
462 DBG2("Trace UST destroy session %d", session->handle);
463
97ee3a89
DG
464 if (session->metadata != NULL) {
465 trace_ust_destroy_metadata(session->metadata);
466 }
467
f6a9efaa
DG
468 /* Cleaning up UST domain */
469 destroy_domain_global(&session->domain_global);
470 destroy_domain_pid(session->domain_pid);
471 destroy_domain_exec(session->domain_exec);
44d3bd01 472
97ee3a89 473 free(session);
f6a9efaa
DG
474
475 rcu_read_unlock();
97ee3a89 476}
This page took 0.044656 seconds and 5 git commands to generate.