Implement consumer ring buffer position sampling
[lttng-tools.git] / src / bin / lttng-sessiond / channel.c
CommitLineData
54d01ffb
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
54d01ffb 4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
54d01ffb 8 *
d14d33bf
AM
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
54d01ffb 13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
54d01ffb
DG
17 */
18
6c1c0768 19#define _LGPL_SOURCE
7972aab2 20#include <inttypes.h>
56fff090 21#include <string.h>
54d01ffb
DG
22#include <unistd.h>
23
990570ed
DG
24#include <common/common.h>
25#include <common/defaults.h>
db758600 26#include <common/sessiond-comm/sessiond-comm.h>
54d01ffb
DG
27
28#include "channel.h"
12744796 29#include "lttng-sessiond.h"
4771f025 30#include "kernel.h"
44d3bd01 31#include "ust-ctl.h"
54d01ffb 32#include "utils.h"
7885e399 33#include "ust-app.h"
b63d638b 34#include "agent.h"
54d01ffb
DG
35
36/*
37 * Return allocated channel attributes.
38 */
0a9c6494
DG
39struct lttng_channel *channel_new_default_attr(int dom,
40 enum lttng_buffer_type type)
54d01ffb
DG
41{
42 struct lttng_channel *chan;
bdf64013 43 const char *channel_name = DEFAULT_CHANNEL_NAME;
e9404c27 44 struct lttng_channel_extended *extended_attr = NULL;
54d01ffb
DG
45
46 chan = zmalloc(sizeof(struct lttng_channel));
47 if (chan == NULL) {
7885e399 48 PERROR("zmalloc channel init");
54d01ffb
DG
49 goto error_alloc;
50 }
51
e9404c27
JG
52 extended_attr = zmalloc(sizeof(struct lttng_channel_extended));
53 if (!extended_attr) {
54 PERROR("zmalloc channel extended init");
55 goto error;
56 }
57
58 chan->attr.extended.ptr = extended_attr;
59
0a9c6494 60 /* Same for all domains. */
54d01ffb 61 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
0a9c6494
DG
62 chan->attr.tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
63 chan->attr.tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
54d01ffb
DG
64
65 switch (dom) {
1b1c65fa 66 case LTTNG_DOMAIN_KERNEL:
0a9c6494 67 assert(type == LTTNG_BUFFER_GLOBAL);
3e230f92
SM
68 chan->attr.subbuf_size =
69 default_get_kernel_channel_subbuf_size();
1b1c65fa
MD
70 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
71 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
6bb9e85f
MD
72 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
73 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
ecc48a90 74 chan->attr.live_timer_interval = DEFAULT_KERNEL_CHANNEL_LIVE_TIMER;
e9404c27
JG
75 extended_attr->monitor_timer_interval =
76 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
1b1c65fa 77 break;
bdf64013
JG
78 case LTTNG_DOMAIN_JUL:
79 channel_name = DEFAULT_JUL_CHANNEL_NAME;
80 goto common_ust;
81 case LTTNG_DOMAIN_LOG4J:
82 channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
83 goto common_ust;
84 case LTTNG_DOMAIN_PYTHON:
85 channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
86 goto common_ust;
1b1c65fa 87 case LTTNG_DOMAIN_UST:
bdf64013 88common_ust:
0a9c6494
DG
89 switch (type) {
90 case LTTNG_BUFFER_PER_UID:
91 chan->attr.subbuf_size = default_get_ust_uid_channel_subbuf_size();
92 chan->attr.num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
93 chan->attr.output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
94 chan->attr.switch_timer_interval =
95 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
96 chan->attr.read_timer_interval =
97 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
ecc48a90
JD
98 chan->attr.live_timer_interval =
99 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER;
e9404c27
JG
100 extended_attr->monitor_timer_interval =
101 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
0a9c6494
DG
102 break;
103 case LTTNG_BUFFER_PER_PID:
104 default:
105 chan->attr.subbuf_size = default_get_ust_pid_channel_subbuf_size();
106 chan->attr.num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
107 chan->attr.output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
108 chan->attr.switch_timer_interval =
109 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
110 chan->attr.read_timer_interval =
111 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
ecc48a90 112 chan->attr.live_timer_interval =
e9404c27
JG
113 DEFAULT_UST_PID_CHANNEL_LIVE_TIMER;
114 extended_attr->monitor_timer_interval =
115 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
0a9c6494
DG
116 break;
117 }
1b1c65fa
MD
118 break;
119 default:
120 goto error; /* Not implemented */
54d01ffb
DG
121 }
122
bdf64013
JG
123 if (snprintf(chan->name, sizeof(chan->name), "%s",
124 channel_name) < 0) {
125 PERROR("snprintf default channel name");
126 goto error;
127 }
54d01ffb
DG
128 return chan;
129
130error:
e9404c27 131 free(extended_attr);
54d01ffb
DG
132 free(chan);
133error_alloc:
134 return NULL;
135}
136
e9404c27
JG
137void channel_attr_destroy(struct lttng_channel *channel)
138{
139 if (!channel) {
140 return;
141 }
142 free(channel->attr.extended.ptr);
143 free(channel);
144}
145
54d01ffb
DG
146/*
147 * Disable kernel channel of the kernel session.
148 */
149int channel_kernel_disable(struct ltt_kernel_session *ksession,
150 char *channel_name)
151{
152 int ret;
153 struct ltt_kernel_channel *kchan;
154
0525e9ae
DG
155 assert(ksession);
156 assert(channel_name);
157
54d01ffb
DG
158 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
159 if (kchan == NULL) {
f73fabfd 160 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
54d01ffb 161 goto error;
0525e9ae
DG
162 }
163
164 /* Only if channel is enabled disable it. */
165 if (kchan->enabled == 1) {
54d01ffb 166 ret = kernel_disable_channel(kchan);
7885e399 167 if (ret < 0 && ret != -EEXIST) {
f73fabfd 168 ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL;
54d01ffb
DG
169 goto error;
170 }
171 }
172
f73fabfd 173 ret = LTTNG_OK;
54d01ffb
DG
174
175error:
176 return ret;
177}
178
179/*
180 * Enable kernel channel of the kernel session.
181 */
182int channel_kernel_enable(struct ltt_kernel_session *ksession,
183 struct ltt_kernel_channel *kchan)
184{
185 int ret;
186
0525e9ae
DG
187 assert(ksession);
188 assert(kchan);
189
54d01ffb
DG
190 if (kchan->enabled == 0) {
191 ret = kernel_enable_channel(kchan);
192 if (ret < 0) {
f73fabfd 193 ret = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL;
54d01ffb
DG
194 goto error;
195 }
42224349 196 } else {
f73fabfd 197 ret = LTTNG_ERR_KERN_CHAN_EXIST;
42224349 198 goto error;
54d01ffb
DG
199 }
200
f73fabfd 201 ret = LTTNG_OK;
54d01ffb
DG
202
203error:
204 return ret;
205}
206
da9d9d9c
MD
207static int channel_validate(struct lttng_channel *attr)
208{
209 /*
210 * The ringbuffer (both in user space and kernel) behaves badly
211 * in overwrite mode and with less than 2 subbuffers so block it
212 * right away and send back an invalid attribute error.
213 */
214 if (attr->attr.overwrite && attr->attr.num_subbuf < 2) {
215 return -1;
216 }
217 return 0;
218}
219
54d01ffb
DG
220/*
221 * Create kernel channel of the kernel session and notify kernel thread.
222 */
223int channel_kernel_create(struct ltt_kernel_session *ksession,
ff4d74e6 224 struct lttng_channel *attr, int kernel_pipe)
54d01ffb
DG
225{
226 int ret;
ff4d74e6 227 struct lttng_channel *defattr = NULL;
54d01ffb 228
0525e9ae
DG
229 assert(ksession);
230
54d01ffb
DG
231 /* Creating channel attributes if needed */
232 if (attr == NULL) {
0a9c6494
DG
233 defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
234 LTTNG_BUFFER_GLOBAL);
ff4d74e6 235 if (defattr == NULL) {
f73fabfd 236 ret = LTTNG_ERR_FATAL;
54d01ffb
DG
237 goto error;
238 }
ff4d74e6 239 attr = defattr;
54d01ffb
DG
240 }
241
8d5841ea
MD
242 /*
243 * Set the overwrite mode for this channel based on the session
244 * type unless the client explicitly overrides the channel mode.
245 */
246 if (attr->attr.overwrite == DEFAULT_CHANNEL_OVERWRITE) {
247 attr->attr.overwrite = !!ksession->snapshot_mode;
248 }
249
250 /* Enforce mmap output for snapshot sessions. */
27babd3a 251 if (ksession->snapshot_mode) {
27babd3a
DG
252 attr->attr.output = LTTNG_EVENT_MMAP;
253 }
254
da9d9d9c
MD
255 /* Validate common channel properties. */
256 if (channel_validate(attr) < 0) {
257 ret = LTTNG_ERR_INVALID;
258 goto error;
259 }
260
54d01ffb 261 /* Channel not found, creating it */
fdd9eb17 262 ret = kernel_create_channel(ksession, attr);
54d01ffb 263 if (ret < 0) {
f73fabfd 264 ret = LTTNG_ERR_KERN_CHAN_FAIL;
54d01ffb
DG
265 goto error;
266 }
267
268 /* Notify kernel thread that there is a new channel */
269 ret = notify_thread_pipe(kernel_pipe);
270 if (ret < 0) {
f73fabfd 271 ret = LTTNG_ERR_FATAL;
54d01ffb
DG
272 goto error;
273 }
274
f73fabfd 275 ret = LTTNG_OK;
54d01ffb 276error:
e9404c27 277 channel_attr_destroy(defattr);
54d01ffb
DG
278 return ret;
279}
7885e399
DG
280
281/*
282 * Enable UST channel for session and domain.
283 */
7972aab2 284int channel_ust_enable(struct ltt_ust_session *usess,
7885e399
DG
285 struct ltt_ust_channel *uchan)
286{
f73fabfd 287 int ret = LTTNG_OK;
7885e399 288
0525e9ae
DG
289 assert(usess);
290 assert(uchan);
291
7885e399
DG
292 /* If already enabled, everything is OK */
293 if (uchan->enabled) {
294 DBG3("Channel %s already enabled. Skipping", uchan->name);
f73fabfd 295 ret = LTTNG_ERR_UST_CHAN_EXIST;
7885e399
DG
296 goto end;
297 }
298
7972aab2
DG
299 DBG2("Channel %s being enabled in UST domain", uchan->name);
300
301 /*
302 * Enable channel for UST global domain on all applications. Ignore return
303 * value here since whatever error we got, it means that the channel was
304 * not created on one or many registered applications and we can not report
305 * this to the user yet. However, at this stage, the channel was
306 * successfully created on the session daemon side so the enable-channel
307 * command is a success.
308 */
d54b4440 309 (void) ust_app_enable_channel_glb(usess, uchan);
7885e399 310
7885e399
DG
311 uchan->enabled = 1;
312 DBG2("Channel %s enabled successfully", uchan->name);
313
314end:
7885e399
DG
315 return ret;
316}
317
318/*
319 * Create UST channel for session and domain.
320 */
7972aab2
DG
321int channel_ust_create(struct ltt_ust_session *usess,
322 struct lttng_channel *attr, enum lttng_buffer_type type)
7885e399 323{
f73fabfd 324 int ret = LTTNG_OK;
7885e399
DG
325 struct ltt_ust_channel *uchan = NULL;
326 struct lttng_channel *defattr = NULL;
b63d638b 327 enum lttng_domain_type domain = LTTNG_DOMAIN_UST;
7885e399 328
0525e9ae
DG
329 assert(usess);
330
7885e399
DG
331 /* Creating channel attributes if needed */
332 if (attr == NULL) {
0a9c6494 333 defattr = channel_new_default_attr(LTTNG_DOMAIN_UST, type);
7885e399 334 if (defattr == NULL) {
f73fabfd 335 ret = LTTNG_ERR_FATAL;
7885e399
DG
336 goto error;
337 }
338 attr = defattr;
b63d638b
JG
339 } else {
340 /*
341 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
342 * based on the default name.
343 */
344 if (!strcmp(attr->name, DEFAULT_JUL_CHANNEL_NAME)) {
345 domain = LTTNG_DOMAIN_JUL;
346 } else if (!strcmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME)) {
347 domain = LTTNG_DOMAIN_LOG4J;
348 } else if (!strcmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME)) {
349 domain = LTTNG_DOMAIN_PYTHON;
350 }
7885e399
DG
351 }
352
8d5841ea
MD
353 /*
354 * Set the overwrite mode for this channel based on the session
355 * type unless the client explicitly overrides the channel mode.
356 */
357 if (attr->attr.overwrite == DEFAULT_CHANNEL_OVERWRITE) {
358 attr->attr.overwrite = !!usess->snapshot_mode;
359 }
360
361 /* Enforce mmap output for snapshot sessions. */
27babd3a 362 if (usess->snapshot_mode) {
27babd3a
DG
363 attr->attr.output = LTTNG_EVENT_MMAP;
364 }
365
da9d9d9c
MD
366 /* Validate common channel properties. */
367 if (channel_validate(attr) < 0) {
368 ret = LTTNG_ERR_INVALID;
369 goto error;
370 }
371
b024d072 372 /*
0525e9ae
DG
373 * Validate UST buffer size and number of buffers: must both be power of 2
374 * and nonzero. We validate right here for UST, because applications will
375 * not report the error to the user (unlike kernel tracing).
b024d072 376 */
0525e9ae
DG
377 if (!attr->attr.subbuf_size ||
378 (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
f73fabfd 379 ret = LTTNG_ERR_INVALID;
b024d072
MD
380 goto error;
381 }
0525e9ae 382
12744796
DG
383 /*
384 * Invalid subbuffer size if it's lower then the page size.
385 */
386 if (attr->attr.subbuf_size < page_size) {
387 ret = LTTNG_ERR_INVALID;
388 goto error;
389 }
390
0525e9ae
DG
391 if (!attr->attr.num_subbuf ||
392 (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
f73fabfd 393 ret = LTTNG_ERR_INVALID;
b024d072
MD
394 goto error;
395 }
396
a79d84dd
DG
397 if (attr->attr.output != LTTNG_EVENT_MMAP) {
398 ret = LTTNG_ERR_NOT_SUPPORTED;
399 goto error;
400 }
401
1624d5b7
JD
402 /*
403 * The tracefile_size should not be < to the subbuf_size, otherwise
404 * we won't be able to write the packets on disk
405 */
406 if ((attr->attr.tracefile_size > 0) &&
407 (attr->attr.tracefile_size < attr->attr.subbuf_size)) {
408 ret = LTTNG_ERR_INVALID;
409 goto error;
410 }
411
2e8269f7
DG
412 /* Validate buffer type. */
413 switch (type) {
414 case LTTNG_BUFFER_PER_PID:
0a9c6494 415 break;
2e8269f7
DG
416 case LTTNG_BUFFER_PER_UID:
417 break;
418 default:
419 ret = LTTNG_ERR_BUFFER_NOT_SUPPORTED;
420 goto error;
421 }
422
7885e399 423 /* Create UST channel */
b63d638b 424 uchan = trace_ust_create_channel(attr, domain);
7885e399 425 if (uchan == NULL) {
f73fabfd 426 ret = LTTNG_ERR_FATAL;
7885e399
DG
427 goto error;
428 }
51755dc8 429
58f3ca76 430 uchan->enabled = 1;
7972aab2
DG
431 if (trace_ust_is_max_id(usess->used_channel_id)) {
432 ret = LTTNG_ERR_UST_CHAN_FAIL;
433 goto error;
434 }
435 uchan->id = trace_ust_get_next_chan_id(usess);
436
437 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64,
438 uchan->name, type, uchan->id);
439
440 /* Flag session buffer type. */
441 if (!usess->buffer_type_changed) {
442 usess->buffer_type = type;
443 usess->buffer_type_changed = 1;
444 } else if (usess->buffer_type != type) {
445 /* Buffer type was already set. Refuse to create channel. */
446 ret = LTTNG_ERR_BUFFER_TYPE_MISMATCH;
7885e399
DG
447 goto error_free_chan;
448 }
449
7972aab2
DG
450 /* Enable channel for global domain */
451 ret = ust_app_create_channel_glb(usess, uchan);
49c336c1 452 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
f73fabfd 453 ret = LTTNG_ERR_UST_CHAN_FAIL;
7885e399
DG
454 goto error_free_chan;
455 }
456
fc34caaa
DG
457 /* Adding the channel to the channel hash table. */
458 rcu_read_lock();
ad7a9107
DG
459 if (strncmp(uchan->name, DEFAULT_METADATA_NAME,
460 sizeof(uchan->name))) {
461 lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node);
84ad93e8
DG
462 } else {
463 /*
464 * Copy channel attribute to session if this is metadata so if NO
465 * application exists we can access that data in the shadow copy during
466 * the global update of newly registered application.
467 */
468 memcpy(&usess->metadata_attr, &uchan->attr,
469 sizeof(usess->metadata_attr));
ad7a9107 470 }
fc34caaa
DG
471 rcu_read_unlock();
472
7885e399 473 DBG2("Channel %s created successfully", uchan->name);
b63d638b
JG
474 if (domain != LTTNG_DOMAIN_UST) {
475 struct agent *agt = trace_ust_find_agent(usess, domain);
476
477 if (!agt) {
478 agt = agent_create(domain);
479 if (!agt) {
480 ret = LTTNG_ERR_NOMEM;
481 goto error_free_chan;
482 }
483 agent_add(agt, usess->agents);
484 }
485 }
7885e399 486
e9404c27 487 channel_attr_destroy(defattr);
f73fabfd 488 return LTTNG_OK;
7885e399
DG
489
490error_free_chan:
24d1723f
DG
491 /*
492 * No need to remove the channel from the hash table because at this point
493 * it was not added hence the direct call and no call_rcu().
494 */
7885e399
DG
495 trace_ust_destroy_channel(uchan);
496error:
e9404c27 497 channel_attr_destroy(defattr);
7885e399
DG
498 return ret;
499}
500
501/*
502 * Disable UST channel for session and domain.
503 */
7972aab2 504int channel_ust_disable(struct ltt_ust_session *usess,
7885e399
DG
505 struct ltt_ust_channel *uchan)
506{
f73fabfd 507 int ret = LTTNG_OK;
7885e399 508
0525e9ae
DG
509 assert(usess);
510 assert(uchan);
511
7885e399
DG
512 /* Already disabled */
513 if (uchan->enabled == 0) {
514 DBG2("Channel UST %s already disabled", uchan->name);
515 goto end;
516 }
517
7972aab2
DG
518 DBG2("Channel %s being disabled in UST global domain", uchan->name);
519 /* Disable channel for global domain */
520 ret = ust_app_disable_channel_glb(usess, uchan);
49c336c1 521 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
f73fabfd 522 ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL;
7885e399
DG
523 goto error;
524 }
525
526 uchan->enabled = 0;
527
528 DBG2("Channel %s disabled successfully", uchan->name);
529
f73fabfd 530 return LTTNG_OK;
7885e399
DG
531
532end:
533error:
534 return ret;
535}
This page took 0.117074 seconds and 4 git commands to generate.