Use the new functions for default subbuf sizes
[lttng-tools.git] / src / bin / lttng-sessiond / trace-ust.c
CommitLineData
97ee3a89
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
97ee3a89 7 *
d14d33bf
AM
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
97ee3a89 12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
97ee3a89
DG
16 */
17
18#define _GNU_SOURCE
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
990570ed
DG
24#include <common/common.h>
25#include <common/defaults.h>
97ee3a89
DG
26
27#include "trace-ust.h"
28
0177d773 29/*
f6a9efaa 30 * Find the channel in the hashtable.
0177d773 31 */
bec39940 32struct ltt_ust_channel *trace_ust_find_channel_by_name(struct lttng_ht *ht,
f6a9efaa 33 char *name)
0177d773 34{
bec39940
DG
35 struct lttng_ht_node_str *node;
36 struct lttng_ht_iter iter;
0177d773 37
f6a9efaa 38 rcu_read_lock();
bec39940
DG
39 lttng_ht_lookup(ht, (void *)name, &iter);
40 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa
DG
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 */
bec39940 59struct ltt_ust_event *trace_ust_find_event_by_name(struct lttng_ht *ht,
f6a9efaa 60 char *name)
97ee3a89 61{
bec39940
DG
62 struct lttng_ht_node_str *node;
63 struct lttng_ht_iter iter;
97ee3a89 64
f6a9efaa 65 rcu_read_lock();
bec39940
DG
66 lttng_ht_lookup(ht, (void *) name, &iter);
67 node = lttng_ht_iter_get_node_str(&iter);
f6a9efaa
DG
68 if (node == NULL) {
69 rcu_read_unlock();
97ee3a89
DG
70 goto error;
71 }
f6a9efaa 72 rcu_read_unlock();
97ee3a89 73
284d8f55 74 DBG2("Trace UST event found by name %s", name);
f6a9efaa
DG
75
76 return caa_container_of(node, struct ltt_ust_event, node);
97ee3a89
DG
77
78error:
284d8f55 79 DBG2("Trace UST event NOT found by name %s", name);
97ee3a89
DG
80 return NULL;
81}
82
83/*
84 * Allocate and initialize a ust session data structure.
85 *
86 * Return pointer to structure or NULL.
87 */
fc4705fe
DG
88struct ltt_ust_session *trace_ust_create_session(char *path,
89 unsigned int session_id, struct lttng_domain *domain)
97ee3a89 90{
0177d773 91 int ret;
97ee3a89
DG
92 struct ltt_ust_session *lus;
93
94 /* Allocate a new ltt ust session */
ba7f0ae5 95 lus = zmalloc(sizeof(struct ltt_ust_session));
97ee3a89 96 if (lus == NULL) {
ba7f0ae5 97 PERROR("create ust session zmalloc");
97ee3a89
DG
98 goto error;
99 }
100
101 /* Init data structure */
a991f516 102 lus->id = session_id;
36dc12cc 103 lus->start_trace = 0;
97ee3a89 104
f6a9efaa 105 /* Alloc UST domain hash tables */
bec39940
DG
106 lus->domain_pid = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
107 lus->domain_exec = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
f6a9efaa
DG
108
109 /* Alloc UST global domain channels' HT */
bec39940 110 lus->domain_global.channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
44d3bd01 111
00e2e675
DG
112 lus->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
113 if (lus->consumer == NULL) {
09a90bcd 114 goto error_consumer;
00e2e675
DG
115 }
116
117 /*
118 * The tmp_consumer stays NULL until a set_consumer_uri command is
119 * executed. At this point, the consumer should be nullify until an
120 * enable_consumer command. This assignment is symbolic since we've zmalloc
121 * the struct.
122 */
123 lus->tmp_consumer = NULL;
124
125 /* Use the default consumer output which is the tracing session path. */
a4b92340
DG
126 if (path && strlen(path) > 0) {
127 ret = snprintf(lus->consumer->dst.trace_path, PATH_MAX,
128 "%s" DEFAULT_UST_TRACE_DIR, path);
129 if (ret < 0) {
130 PERROR("snprintf UST consumer trace path");
09a90bcd 131 goto error_path;
a4b92340
DG
132 }
133
134 /* Set session path */
135 ret = snprintf(lus->pathname, PATH_MAX, "%s" DEFAULT_UST_TRACE_DIR,
136 path);
137 if (ret < 0) {
138 PERROR("snprintf kernel traces path");
09a90bcd 139 goto error_path;
a4b92340 140 }
0177d773
DG
141 }
142
44d3bd01
DG
143 DBG2("UST trace session create successful");
144
97ee3a89
DG
145 return lus;
146
09a90bcd
DG
147error_path:
148 consumer_destroy_output(lus->consumer);
149error_consumer:
a2c0da86
MD
150 lttng_ht_destroy(lus->domain_global.channels);
151 lttng_ht_destroy(lus->domain_exec);
152 lttng_ht_destroy(lus->domain_pid);
44844c29 153 free(lus);
97ee3a89
DG
154error:
155 return NULL;
156}
157
158/*
159 * Allocate and initialize a ust channel data structure.
160 *
161 * Return pointer to structure or NULL.
162 */
44d3bd01
DG
163struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
164 char *path)
97ee3a89
DG
165{
166 int ret;
167 struct ltt_ust_channel *luc;
168
37357452 169 luc = zmalloc(sizeof(struct ltt_ust_channel));
97ee3a89 170 if (luc == NULL) {
df0f840b 171 PERROR("ltt_ust_channel zmalloc");
97ee3a89
DG
172 goto error;
173 }
174
44d3bd01 175 /* Copy UST channel attributes */
48842b30
DG
176 luc->attr.overwrite = chan->attr.overwrite;
177 luc->attr.subbuf_size = chan->attr.subbuf_size;
178 luc->attr.num_subbuf = chan->attr.num_subbuf;
179 luc->attr.switch_timer_interval = chan->attr.switch_timer_interval;
180 luc->attr.read_timer_interval = chan->attr.read_timer_interval;
6775595e 181 luc->attr.output = (enum lttng_ust_output) chan->attr.output;
44d3bd01
DG
182
183 /* Translate to UST output enum */
184 switch (luc->attr.output) {
185 default:
186 luc->attr.output = LTTNG_UST_MMAP;
187 break;
97ee3a89 188 }
97ee3a89 189
97ee3a89 190 /* Copy channel name */
0a1459bb 191 strncpy(luc->name, chan->name, sizeof(luc->name));
97ee3a89
DG
192 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
193
f6a9efaa 194 /* Init node */
bec39940 195 lttng_ht_node_init_str(&luc->node, luc->name);
f6a9efaa 196 /* Alloc hash tables */
bec39940
DG
197 luc->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
198 luc->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
f6a9efaa 199
97ee3a89 200 /* Set trace output path */
48842b30 201 ret = snprintf(luc->pathname, PATH_MAX, "%s", path);
97ee3a89 202 if (ret < 0) {
df0f840b 203 PERROR("asprintf ust create channel");
44844c29 204 goto error_free_channel;
97ee3a89
DG
205 }
206
f6a9efaa
DG
207 DBG2("Trace UST channel %s created", luc->name);
208
97ee3a89
DG
209 return luc;
210
44844c29 211error_free_channel:
a2c0da86
MD
212 lttng_ht_destroy(luc->ctx);
213 lttng_ht_destroy(luc->events);
44844c29 214 free(luc);
97ee3a89
DG
215error:
216 return NULL;
217}
218
219/*
220 * Allocate and initialize a ust event. Set name and event type.
221 *
222 * Return pointer to structure or NULL.
223 */
224struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
225{
226 struct ltt_ust_event *lue;
97ee3a89 227
37357452 228 lue = zmalloc(sizeof(struct ltt_ust_event));
44d3bd01 229 if (lue == NULL) {
ba7f0ae5 230 PERROR("ust event zmalloc");
97ee3a89
DG
231 goto error;
232 }
233
234 switch (ev->type) {
235 case LTTNG_EVENT_PROBE:
44d3bd01 236 lue->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
237 break;
238 case LTTNG_EVENT_FUNCTION:
44d3bd01 239 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
240 break;
241 case LTTNG_EVENT_FUNCTION_ENTRY:
44d3bd01 242 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
243 break;
244 case LTTNG_EVENT_TRACEPOINT:
44d3bd01 245 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89
DG
246 break;
247 default:
248 ERR("Unknown ust instrumentation type (%d)", ev->type);
44844c29 249 goto error_free_event;
97ee3a89
DG
250 }
251
252 /* Copy event name */
44d3bd01
DG
253 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
254 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89 255
0cda4f28 256 switch (ev->loglevel_type) {
8005f29a
MD
257 case LTTNG_EVENT_LOGLEVEL_ALL:
258 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_ALL;
22e25b71 259 lue->attr.loglevel = -1; /* Force to -1 */
0cda4f28 260 break;
8005f29a
MD
261 case LTTNG_EVENT_LOGLEVEL_RANGE:
262 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_RANGE;
22e25b71 263 lue->attr.loglevel = ev->loglevel;
8005f29a
MD
264 break;
265 case LTTNG_EVENT_LOGLEVEL_SINGLE:
266 lue->attr.loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE;
22e25b71 267 lue->attr.loglevel = ev->loglevel;
0cda4f28
MD
268 break;
269 default:
4431494b 270 ERR("Unknown ust loglevel type (%d)", ev->loglevel_type);
0cda4f28
MD
271 goto error_free_event;
272 }
273
0cda4f28 274
f6a9efaa 275 /* Init node */
bec39940 276 lttng_ht_node_init_str(&lue->node, lue->attr.name);
f6a9efaa 277 /* Alloc context hash tables */
bec39940 278 lue->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
af061a47
DG
279 if (lue->ctx == NULL) {
280 ERR("Unable to create context hash table for event %s", ev->name);
281 goto error_free_event;
282 }
97ee3a89 283
300b8fd5
MD
284 DBG2("Trace UST event %s, loglevel (%d,%d) created",
285 lue->attr.name, lue->attr.loglevel_type,
286 lue->attr.loglevel);
284d8f55 287
97ee3a89
DG
288 return lue;
289
44844c29
MD
290error_free_event:
291 free(lue);
97ee3a89
DG
292error:
293 return NULL;
294}
295
296/*
297 * Allocate and initialize a ust metadata.
298 *
299 * Return pointer to structure or NULL.
300 */
301struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
302{
303 int ret;
304 struct ltt_ust_metadata *lum;
97ee3a89 305
3a009adb 306 lum = zmalloc(sizeof(struct ltt_ust_metadata));
44d3bd01 307 if (lum == NULL) {
df0f840b 308 PERROR("ust metadata zmalloc");
97ee3a89
DG
309 goto error;
310 }
311
312 /* Set default attributes */
44d3bd01 313 lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
3e230f92 314 lum->attr.subbuf_size = default_get_metadata_subbuf_size();
44d3bd01
DG
315 lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
316 lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
317 lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
d41f73b7 318 lum->attr.output = LTTNG_UST_MMAP;
44d3bd01 319
97ee3a89
DG
320 lum->handle = -1;
321 /* Set metadata trace path */
f6a9efaa 322 ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path);
97ee3a89 323 if (ret < 0) {
df0f840b 324 PERROR("asprintf ust metadata");
44844c29 325 goto error_free_metadata;
97ee3a89
DG
326 }
327
328 return lum;
329
44844c29
MD
330error_free_metadata:
331 free(lum);
97ee3a89
DG
332error:
333 return NULL;
334}
335
55cc08a6
DG
336/*
337 * Allocate and initialize an UST context.
338 *
339 * Return pointer to structure or NULL.
340 */
341struct ltt_ust_context *trace_ust_create_context(
342 struct lttng_event_context *ctx)
343{
344 struct ltt_ust_context *uctx;
9197c5c4
MD
345 enum lttng_ust_context_type utype;
346
347 switch (ctx->ctx) {
348 case LTTNG_EVENT_CONTEXT_VTID:
349 utype = LTTNG_UST_CONTEXT_VTID;
350 break;
351 case LTTNG_EVENT_CONTEXT_VPID:
352 utype = LTTNG_UST_CONTEXT_VPID;
353 break;
354 case LTTNG_EVENT_CONTEXT_PTHREAD_ID:
355 utype = LTTNG_UST_CONTEXT_PTHREAD_ID;
356 break;
357 case LTTNG_EVENT_CONTEXT_PROCNAME:
358 utype = LTTNG_UST_CONTEXT_PROCNAME;
359 break;
360 default:
361 ERR("Invalid UST context");
362 return NULL;
363 }
55cc08a6
DG
364
365 uctx = zmalloc(sizeof(struct ltt_ust_context));
366 if (uctx == NULL) {
367 PERROR("zmalloc ltt_ust_context");
368 goto error;
369 }
370
9197c5c4 371 uctx->ctx.ctx = utype;
bec39940 372 lttng_ht_node_init_ulong(&uctx->node, (unsigned long) uctx->ctx.ctx);
55cc08a6
DG
373
374 return uctx;
375
376error:
377 return NULL;
378}
379
f6a9efaa
DG
380/*
381 * RCU safe free context structure.
382 */
383static void destroy_context_rcu(struct rcu_head *head)
384{
bec39940
DG
385 struct lttng_ht_node_ulong *node =
386 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa
DG
387 struct ltt_ust_context *ctx =
388 caa_container_of(node, struct ltt_ust_context, node);
389
390 free(ctx);
391}
392
393/*
394 * Cleanup UST context hash table.
395 */
7bd39047 396static void destroy_contexts(struct lttng_ht *ht)
f6a9efaa
DG
397{
398 int ret;
bec39940
DG
399 struct lttng_ht_node_ulong *node;
400 struct lttng_ht_iter iter;
f6a9efaa 401
bec39940
DG
402 cds_lfht_for_each_entry(ht->ht, &iter.iter, node, node) {
403 ret = lttng_ht_del(ht, &iter);
f6a9efaa
DG
404 if (!ret) {
405 call_rcu(&node->head, destroy_context_rcu);
406 }
f6a9efaa 407 }
ed52805d 408
bec39940 409 lttng_ht_destroy(ht);
f6a9efaa
DG
410}
411
97ee3a89
DG
412/*
413 * Cleanup ust event structure.
414 */
415void trace_ust_destroy_event(struct ltt_ust_event *event)
416{
f6a9efaa 417 DBG2("Trace destroy UST event %s", event->attr.name);
7bd39047 418 destroy_contexts(event->ctx);
53a80697 419 free(event->filter);
97ee3a89
DG
420 free(event);
421}
422
f6a9efaa
DG
423/*
424 * URCU intermediate call to complete destroy event.
425 */
426static void destroy_event_rcu(struct rcu_head *head)
427{
bec39940
DG
428 struct lttng_ht_node_str *node =
429 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
430 struct ltt_ust_event *event =
431 caa_container_of(node, struct ltt_ust_event, node);
432
433 trace_ust_destroy_event(event);
434}
435
284d8f55
DG
436/*
437 * Cleanup UST events hashtable.
438 */
7bd39047 439static void destroy_events(struct lttng_ht *events)
ed52805d
DG
440{
441 int ret;
bec39940
DG
442 struct lttng_ht_node_str *node;
443 struct lttng_ht_iter iter;
ed52805d 444
bec39940
DG
445 cds_lfht_for_each_entry(events->ht, &iter.iter, node, node) {
446 ret = lttng_ht_del(events, &iter);
7bd39047
DG
447 assert(!ret);
448 call_rcu(&node->head, destroy_event_rcu);
ed52805d
DG
449 }
450
bec39940 451 lttng_ht_destroy(events);
ed52805d
DG
452}
453
97ee3a89
DG
454/*
455 * Cleanup ust channel structure.
456 */
457void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
458{
f6a9efaa 459 DBG2("Trace destroy UST channel %s", channel->name);
97ee3a89 460
f6a9efaa
DG
461 rcu_read_lock();
462
7bd39047
DG
463 /* Destroying all events of the channel */
464 destroy_events(channel->events);
465 /* Destroying all context of the channel */
466 destroy_contexts(channel->ctx);
97ee3a89 467
97ee3a89 468 free(channel);
f6a9efaa
DG
469
470 rcu_read_unlock();
471}
472
473/*
474 * URCU intermediate call to complete destroy channel.
475 */
476static void destroy_channel_rcu(struct rcu_head *head)
477{
bec39940
DG
478 struct lttng_ht_node_str *node =
479 caa_container_of(head, struct lttng_ht_node_str, head);
f6a9efaa
DG
480 struct ltt_ust_channel *channel =
481 caa_container_of(node, struct ltt_ust_channel, node);
482
483 trace_ust_destroy_channel(channel);
97ee3a89
DG
484}
485
486/*
487 * Cleanup ust metadata structure.
488 */
489void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
490{
abc9138a
MD
491 if (!metadata->handle) {
492 return;
493 }
f6a9efaa 494 DBG2("Trace UST destroy metadata %d", metadata->handle);
97ee3a89
DG
495 free(metadata);
496}
497
498/*
f6a9efaa 499 * Iterate over a hash table containing channels and cleanup safely.
97ee3a89 500 */
bec39940 501static void destroy_channels(struct lttng_ht *channels)
f6a9efaa
DG
502{
503 int ret;
bec39940
DG
504 struct lttng_ht_node_str *node;
505 struct lttng_ht_iter iter;
f6a9efaa 506
24d1723f
DG
507 rcu_read_lock();
508
bec39940
DG
509 cds_lfht_for_each_entry(channels->ht, &iter.iter, node, node) {
510 ret = lttng_ht_del(channels, &iter);
7bd39047
DG
511 assert(!ret);
512 call_rcu(&node->head, destroy_channel_rcu);
f6a9efaa 513 }
ed52805d 514
bec39940 515 lttng_ht_destroy(channels);
24d1723f
DG
516
517 rcu_read_unlock();
f6a9efaa
DG
518}
519
520/*
521 * Cleanup UST pid domain.
522 */
bec39940 523static void destroy_domain_pid(struct lttng_ht *ht)
f6a9efaa
DG
524{
525 int ret;
bec39940 526 struct lttng_ht_iter iter;
ed52805d 527 struct ltt_ust_domain_pid *dpid;
f6a9efaa 528
bec39940
DG
529 cds_lfht_for_each_entry(ht->ht, &iter.iter, dpid, node.node) {
530 ret = lttng_ht_del(ht , &iter);
7bd39047
DG
531 assert(!ret);
532 destroy_channels(dpid->channels);
f6a9efaa 533 }
ed52805d 534
bec39940 535 lttng_ht_destroy(ht);
f6a9efaa
DG
536}
537
538/*
539 * Cleanup UST exec name domain.
540 */
bec39940 541static void destroy_domain_exec(struct lttng_ht *ht)
97ee3a89 542{
f6a9efaa 543 int ret;
bec39940 544 struct lttng_ht_iter iter;
ed52805d 545 struct ltt_ust_domain_exec *dexec;
f6a9efaa 546
bec39940
DG
547 cds_lfht_for_each_entry(ht->ht, &iter.iter, dexec, node.node) {
548 ret = lttng_ht_del(ht , &iter);
7bd39047
DG
549 assert(!ret);
550 destroy_channels(dexec->channels);
f6a9efaa 551 }
ed52805d 552
bec39940 553 lttng_ht_destroy(ht);
f6a9efaa 554}
97ee3a89 555
f6a9efaa
DG
556/*
557 * Cleanup UST global domain.
558 */
559static void destroy_domain_global(struct ltt_ust_domain_global *dom)
560{
561 destroy_channels(dom->channels);
562}
97ee3a89 563
f6a9efaa
DG
564/*
565 * Cleanup ust session structure
566 */
567void trace_ust_destroy_session(struct ltt_ust_session *session)
568{
ed52805d 569 /* Extra protection */
97ee3a89
DG
570 if (session == NULL) {
571 return;
572 }
573
f6a9efaa
DG
574 rcu_read_lock();
575
fc4705fe 576 DBG2("Trace UST destroy session %u", session->id);
f6a9efaa 577
f6a9efaa
DG
578 /* Cleaning up UST domain */
579 destroy_domain_global(&session->domain_global);
580 destroy_domain_pid(session->domain_pid);
581 destroy_domain_exec(session->domain_exec);
44d3bd01 582
3f8e211f
DG
583 consumer_destroy_output(session->consumer);
584 consumer_destroy_output(session->tmp_consumer);
585
97ee3a89 586 free(session);
f6a9efaa
DG
587
588 rcu_read_unlock();
97ee3a89 589}
This page took 0.059186 seconds and 4 git commands to generate.