RCU support for consumer's hash tables
[lttng-tools.git] / lttng-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>
bec39940 25#include <lttng-ht.h>
97ee3a89
DG
26#include <lttng-share.h>
27
28#include "trace-ust.h"
29
0177d773 30/*
f6a9efaa 31 * Find the channel in the hashtable.
0177d773 32 */
bec39940 33struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
f6a9efaa 34 char *name)
0177d773 35{
bec39940
DG
36 struct lttng_ht_node_str *node;
37 struct lttng_ht_iter iter;
0177d773 38
f6a9efaa 39 rcu_read_lock();
bec39940
DG
40 lttng_ht_lookup(ht, (void *)name, &iter);
41 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa
DG
42 if (node == NULL) {
43 rcu_read_unlock();
44d3bd01
DG
44 goto error;
45 }
f6a9efaa 46 rcu_read_unlock();
44d3bd01 47
f6a9efaa 48 DBG2("Trace UST channel %s found by name", name);
0177d773 49
f6a9efaa 50 return caa_container_of(node, struct ltt_ust_channel, node);
97ee3a89
DG
51
52error:
f6a9efaa 53 DBG2("Trace UST channel %s not found by name", name);
97ee3a89
DG
54 return NULL;
55}
56
57/*
f6a9efaa 58 * Find the event in the hashtable.
97ee3a89 59 */
bec39940 60struct ltt_ust_event *trace_ust_find_event_by_name(struct lttng_ht *ht,
f6a9efaa 61 char *name)
97ee3a89 62{
bec39940
DG
63 struct lttng_ht_node_str *node;
64 struct lttng_ht_iter iter;
97ee3a89 65
f6a9efaa 66 rcu_read_lock();
bec39940
DG
67 lttng_ht_lookup(ht, (void *) name, &iter);
68 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa
DG
69 if (node == NULL) {
70 rcu_read_unlock();
97ee3a89
DG
71 goto error;
72 }
f6a9efaa 73 rcu_read_unlock();
97ee3a89 74
284d8f55 75 DBG2("Trace UST event found by name %s", name);
f6a9efaa
DG
76
77 return caa_container_of(node, struct ltt_ust_event, node);
97ee3a89
DG
78
79error:
284d8f55 80 DBG2("Trace UST event NOT found by name %s", name);
97ee3a89
DG
81 return NULL;
82}
83
84/*
85 * Allocate and initialize a ust session data structure.
86 *
87 * Return pointer to structure or NULL.
88 */
a991f516 89struct ltt_ust_session *trace_ust_create_session(char *path, int session_id,
44d3bd01 90 struct lttng_domain *domain)
97ee3a89 91{
0177d773 92 int ret;
97ee3a89
DG
93 struct ltt_ust_session *lus;
94
95 /* Allocate a new ltt ust session */
ba7f0ae5 96 lus = zmalloc(sizeof(struct ltt_ust_session));
97ee3a89 97 if (lus == NULL) {
ba7f0ae5 98 PERROR("create ust session zmalloc");
97ee3a89
DG
99 goto error;
100 }
101
102 /* Init data structure */
a991f516 103 lus->id = session_id;
36dc12cc 104 lus->start_trace = 0;
97ee3a89 105
f6a9efaa 106 /* Alloc UST domain hash tables */
bec39940
DG
107 lus->domain_pid = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
108 lus->domain_exec = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
f6a9efaa
DG
109
110 /* Alloc UST global domain channels' HT */
bec39940 111 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
44d3bd01 112
0177d773 113 /* Set session path */
48842b30 114 ret = snprintf(lus->pathname, PATH_MAX, "%s/ust", path);
0177d773 115 if (ret < 0) {
44d3bd01 116 PERROR("snprintf kernel traces path");
44844c29 117 goto error_free_session;
0177d773
DG
118 }
119
44d3bd01
DG
120 DBG2("UST trace session create successful");
121
97ee3a89
DG
122 return lus;
123
44844c29
MD
124error_free_session:
125 free(lus);
97ee3a89
DG
126error:
127 return NULL;
128}
129
130/*
131 * Allocate and initialize a ust channel data structure.
132 *
133 * Return pointer to structure or NULL.
134 */
44d3bd01
DG
135struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
136 char *path)
97ee3a89
DG
137{
138 int ret;
139 struct ltt_ust_channel *luc;
140
37357452 141 luc = zmalloc(sizeof(struct ltt_ust_channel));
97ee3a89 142 if (luc == NULL) {
ba7f0ae5 143 perror("ltt_ust_channel zmalloc");
97ee3a89
DG
144 goto error;
145 }
146
44d3bd01 147 /* Copy UST channel attributes */
48842b30
DG
148 luc->attr.overwrite = chan->attr.overwrite;
149 luc->attr.subbuf_size = chan->attr.subbuf_size;
150 luc->attr.num_subbuf = chan->attr.num_subbuf;
151 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
152 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
153 luc->attr.output = chan->attr.output;
44d3bd01
DG
154
155 /* Translate to UST output enum */
156 switch (luc->attr.output) {
157 default:
158 luc->attr.output = LTTNG_UST_MMAP;
159 break;
97ee3a89 160 }
97ee3a89 161
97ee3a89 162 /* Copy channel name */
0a1459bb 163 strncpy(luc->name, chan->name, sizeof(luc->name));
97ee3a89
DG
164 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
165
f6a9efaa 166 /* Init node */
bec39940 167 lttng_ht_node_init_str(&luc->node, luc->name);
f6a9efaa 168 /* Alloc hash tables */
bec39940
DG
169 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
170 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
f6a9efaa 171
97ee3a89 172 /* Set trace output path */
48842b30 173 ret = snprintf(luc->pathname, PATH_MAX, "%s", path);
97ee3a89
DG
174 if (ret < 0) {
175 perror("asprintf ust create channel");
44844c29 176 goto error_free_channel;
97ee3a89
DG
177 }
178
f6a9efaa
DG
179 DBG2("Trace UST channel %s created", luc->name);
180
97ee3a89
DG
181 return luc;
182
44844c29
MD
183error_free_channel:
184 free(luc);
97ee3a89
DG
185error:
186 return NULL;
187}
188
189/*
190 * Allocate and initialize a ust event. Set name and event type.
191 *
192 * Return pointer to structure or NULL.
193 */
194struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
195{
196 struct ltt_ust_event *lue;
97ee3a89 197
37357452 198 lue = zmalloc(sizeof(struct ltt_ust_event));
44d3bd01 199 if (lue == NULL) {
ba7f0ae5 200 PERROR("ust event zmalloc");
97ee3a89
DG
201 goto error;
202 }
203
204 switch (ev->type) {
205 case LTTNG_EVENT_PROBE:
44d3bd01 206 lue->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
207 break;
208 case LTTNG_EVENT_FUNCTION:
44d3bd01 209 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
210 break;
211 case LTTNG_EVENT_FUNCTION_ENTRY:
44d3bd01 212 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
213 break;
214 case LTTNG_EVENT_TRACEPOINT:
44d3bd01 215 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89 216 break;
13dce3b7
MD
217 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL:
218 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT_LOGLEVEL;
219 break;
97ee3a89
DG
220 default:
221 ERR("Unknown ust instrumentation type (%d)", ev->type);
44844c29 222 goto error_free_event;
97ee3a89
DG
223 }
224
225 /* Copy event name */
44d3bd01
DG
226 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
227 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89 228
f6a9efaa 229 /* Init node */
bec39940 230 lttng_ht_node_init_str(&lue->node, lue->attr.name);
f6a9efaa 231 /* Alloc context hash tables */
bec39940 232 lue->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
97ee3a89 233
284d8f55
DG
234 DBG2("Trace UST event %s created", lue->attr.name);
235
97ee3a89
DG
236 return lue;
237
44844c29
MD
238error_free_event:
239 free(lue);
97ee3a89
DG
240error:
241 return NULL;
242}
243
244/*
245 * Allocate and initialize a ust metadata.
246 *
247 * Return pointer to structure or NULL.
248 */
249struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
250{
251 int ret;
252 struct ltt_ust_metadata *lum;
97ee3a89 253
3a009adb 254 lum = zmalloc(sizeof(struct ltt_ust_metadata));
44d3bd01 255 if (lum == NULL) {
ba7f0ae5 256 perror("ust metadata zmalloc");
97ee3a89
DG
257 goto error;
258 }
259
260 /* Set default attributes */
44d3bd01
DG
261 lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
262 lum->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE;
263 lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
264 lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
265 lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
d41f73b7 266 lum->attr.output = LTTNG_UST_MMAP;
44d3bd01 267
97ee3a89
DG
268 lum->handle = -1;
269 /* Set metadata trace path */
f6a9efaa 270 ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path);
97ee3a89
DG
271 if (ret < 0) {
272 perror("asprintf ust metadata");
44844c29 273 goto error_free_metadata;
97ee3a89
DG
274 }
275
276 return lum;
277
44844c29
MD
278error_free_metadata:
279 free(lum);
97ee3a89
DG
280error:
281 return NULL;
282}
283
55cc08a6
DG
284/*
285 * Allocate and initialize an UST context.
286 *
287 * Return pointer to structure or NULL.
288 */
289struct ltt_ust_context *trace_ust_create_context(
290 struct lttng_event_context *ctx)
291{
292 struct ltt_ust_context *uctx;
293
294 uctx = zmalloc(sizeof(struct ltt_ust_context));
295 if (uctx == NULL) {
296 PERROR("zmalloc ltt_ust_context");
297 goto error;
298 }
299
300 uctx->ctx.ctx = ctx->ctx;
bec39940 301 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
55cc08a6
DG
302
303 return uctx;
304
305error:
306 return NULL;
307}
308
f6a9efaa
DG
309/*
310 * RCU safe free context structure.
311 */
312static void destroy_context_rcu(struct rcu_head *head)
313{
bec39940
DG
314 struct lttng_ht_node_ulong *node =
315 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa
DG
316 struct ltt_ust_context *ctx =
317 caa_container_of(node, struct ltt_ust_context, node);
318
319 free(ctx);
320}
321
322/*
323 * Cleanup UST context hash table.
324 */
bec39940 325static void destroy_context(struct lttng_ht *ht)
f6a9efaa
DG
326{
327 int ret;
bec39940
DG
328 struct lttng_ht_node_ulong *node;
329 struct lttng_ht_iter iter;
f6a9efaa 330
bec39940
DG
331 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
332 ret = lttng_ht_del(ht, &iter);
f6a9efaa
DG
333 if (!ret) {
334 call_rcu(&node->head, destroy_context_rcu);
335 }
f6a9efaa 336 }
ed52805d 337
bec39940 338 lttng_ht_destroy(ht);
f6a9efaa
DG
339}
340
97ee3a89
DG
341/*
342 * Cleanup ust event structure.
343 */
344void trace_ust_destroy_event(struct ltt_ust_event *event)
345{
f6a9efaa
DG
346 DBG2("Trace destroy UST event %s", event->attr.name);
347 destroy_context(event->ctx);
ed52805d 348
97ee3a89
DG
349 free(event);
350}
351
f6a9efaa
DG
352/*
353 * URCU intermediate call to complete destroy event.
354 */
355static void destroy_event_rcu(struct rcu_head *head)
356{
bec39940
DG
357 struct lttng_ht_node_str *node =
358 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
359 struct ltt_ust_event *event =
360 caa_container_of(node, struct ltt_ust_event, node);
361
362 trace_ust_destroy_event(event);
363}
364
284d8f55
DG
365/*
366 * Cleanup UST events hashtable.
367 */
bec39940 368static void destroy_event(struct lttng_ht *events)
ed52805d
DG
369{
370 int ret;
bec39940
DG
371 struct lttng_ht_node_str *node;
372 struct lttng_ht_iter iter;
ed52805d 373
bec39940
DG
374 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
375 ret = lttng_ht_del(events, &iter);
ed52805d
DG
376 if (!ret) {
377 call_rcu(&node->head, destroy_event_rcu);
378 }
379 }
380
bec39940 381 lttng_ht_destroy(events);
ed52805d
DG
382}
383
97ee3a89
DG
384/*
385 * Cleanup ust channel structure.
386 */
387void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
388{
f6a9efaa 389 int ret;
bec39940
DG
390 struct lttng_ht_node_str *node;
391 struct lttng_ht_iter iter;
97ee3a89 392
f6a9efaa 393 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 394
f6a9efaa
DG
395 rcu_read_lock();
396
bec39940
DG
397 cds_lfht_for_each_entry(channel->events->ht, &iter.iter, node, node) {
398 ret = lttng_ht_del(channel->events, &iter);
f6a9efaa 399 if (!ret) {
ed52805d 400 destroy_event(channel->events);
f6a9efaa 401 }
97ee3a89
DG
402 }
403
f6a9efaa 404 destroy_context(channel->ctx);
97ee3a89 405 free(channel);
f6a9efaa
DG
406
407 rcu_read_unlock();
408}
409
410/*
411 * URCU intermediate call to complete destroy channel.
412 */
413static void destroy_channel_rcu(struct rcu_head *head)
414{
bec39940
DG
415 struct lttng_ht_node_str *node =
416 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
417 struct ltt_ust_channel *channel =
418 caa_container_of(node, struct ltt_ust_channel, node);
419
420 trace_ust_destroy_channel(channel);
97ee3a89
DG
421}
422
423/*
424 * Cleanup ust metadata structure.
425 */
426void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
427{
f6a9efaa 428 DBG2("Trace UST destroy metadata %d", metadata->handle);
97ee3a89 429
97ee3a89
DG
430 free(metadata);
431}
432
433/*
f6a9efaa 434 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 435 */
bec39940 436static void destroy_channels(struct lttng_ht *channels)
f6a9efaa
DG
437{
438 int ret;
bec39940
DG
439 struct lttng_ht_node_str *node;
440 struct lttng_ht_iter iter;
f6a9efaa 441
bec39940
DG
442 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
443 ret = lttng_ht_del(channels, &iter);
f6a9efaa
DG
444 if (!ret) {
445 call_rcu(&node->head, destroy_channel_rcu);
446 }
f6a9efaa 447 }
ed52805d 448
bec39940 449 lttng_ht_destroy(channels);
f6a9efaa
DG
450}
451
452/*
453 * Cleanup UST pid domain.
454 */
bec39940 455static void destroy_domain_pid(struct lttng_ht *ht)
f6a9efaa
DG
456{
457 int ret;
bec39940 458 struct lttng_ht_iter iter;
ed52805d 459 struct ltt_ust_domain_pid *dpid;
f6a9efaa 460
bec39940
DG
461 cds_lfht_for_each_entry(ht->ht, &iter.iter, dpid, node.node) {
462 ret = lttng_ht_del(ht , &iter);
f6a9efaa 463 if (!ret) {
ed52805d 464 destroy_channels(dpid->channels);
f6a9efaa 465 }
f6a9efaa 466 }
ed52805d 467
bec39940 468 lttng_ht_destroy(ht);
f6a9efaa
DG
469}
470
471/*
472 * Cleanup UST exec name domain.
473 */
bec39940 474static void destroy_domain_exec(struct lttng_ht *ht)
97ee3a89 475{
f6a9efaa 476 int ret;
bec39940 477 struct lttng_ht_iter iter;
ed52805d 478 struct ltt_ust_domain_exec *dexec;
f6a9efaa 479
bec39940
DG
480 cds_lfht_for_each_entry(ht->ht, &iter.iter, dexec, node.node) {
481 ret = lttng_ht_del(ht , &iter);
f6a9efaa 482 if (!ret) {
ed52805d 483 destroy_channels(dexec->channels);
f6a9efaa 484 }
f6a9efaa 485 }
ed52805d 486
bec39940 487 lttng_ht_destroy(ht);
f6a9efaa 488}
97ee3a89 489
f6a9efaa
DG
490/*
491 * Cleanup UST global domain.
492 */
493static void destroy_domain_global(struct ltt_ust_domain_global *dom)
494{
495 destroy_channels(dom->channels);
496}
97ee3a89 497
f6a9efaa
DG
498/*
499 * Cleanup ust session structure
500 */
501void trace_ust_destroy_session(struct ltt_ust_session *session)
502{
ed52805d 503 /* Extra protection */
97ee3a89
DG
504 if (session == NULL) {
505 return;
506 }
507
f6a9efaa
DG
508 rcu_read_lock();
509
a991f516 510 DBG2("Trace UST destroy session %d", session->id);
f6a9efaa 511
f6a9efaa
DG
512 /* Cleaning up UST domain */
513 destroy_domain_global(&session->domain_global);
514 destroy_domain_pid(session->domain_pid);
515 destroy_domain_exec(session->domain_exec);
44d3bd01 516
97ee3a89 517 free(session);
f6a9efaa
DG
518
519 rcu_read_unlock();
97ee3a89 520}
This page took 0.050372 seconds and 4 git commands to generate.