Add default subbuf sizes getter functions
[lttng-tools.git] / src / bin / lttng-sessiond / channel.c
CommitLineData
54d01ffb
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.
54d01ffb 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.
54d01ffb 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.
54d01ffb
DG
16 */
17
7885e399 18#define _GNU_SOURCE
56fff090 19#include <string.h>
54d01ffb
DG
20#include <unistd.h>
21
990570ed
DG
22#include <common/common.h>
23#include <common/defaults.h>
db758600 24#include <common/sessiond-comm/sessiond-comm.h>
54d01ffb
DG
25
26#include "channel.h"
4771f025 27#include "kernel.h"
44d3bd01 28#include "ust-ctl.h"
54d01ffb 29#include "utils.h"
7885e399 30#include "ust-app.h"
54d01ffb
DG
31
32/*
33 * Return allocated channel attributes.
34 */
f6cd6b0f 35struct lttng_channel *channel_new_default_attr(int dom)
54d01ffb
DG
36{
37 struct lttng_channel *chan;
38
39 chan = zmalloc(sizeof(struct lttng_channel));
40 if (chan == NULL) {
7885e399 41 PERROR("zmalloc channel init");
54d01ffb
DG
42 goto error_alloc;
43 }
44
44d3bd01
DG
45 if (snprintf(chan->name, sizeof(chan->name), "%s",
46 DEFAULT_CHANNEL_NAME) < 0) {
7885e399 47 PERROR("snprintf default channel name");
54d01ffb
DG
48 goto error;
49 }
50
51 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
52 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
53 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
54
55 switch (dom) {
1b1c65fa
MD
56 case LTTNG_DOMAIN_KERNEL:
57 chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
58 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
59 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
60 break;
61 case LTTNG_DOMAIN_UST:
d78d6610 62#if 0
1b1c65fa 63 case LTTNG_DOMAIN_UST_PID:
d78d6610
DG
64 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
65 case LTTNG_DOMAIN_UST_EXEC_NAME:
66#endif
1b1c65fa
MD
67 chan->attr.subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
68 chan->attr.num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
69 chan->attr.output = DEFAULT_UST_CHANNEL_OUTPUT;
70 break;
71 default:
72 goto error; /* Not implemented */
54d01ffb
DG
73 }
74
75 return chan;
76
77error:
78 free(chan);
79error_alloc:
80 return NULL;
81}
82
83/*
84 * Disable kernel channel of the kernel session.
85 */
86int channel_kernel_disable(struct ltt_kernel_session *ksession,
87 char *channel_name)
88{
89 int ret;
90 struct ltt_kernel_channel *kchan;
91
92 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
93 if (kchan == NULL) {
f73fabfd 94 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
54d01ffb
DG
95 goto error;
96 } else if (kchan->enabled == 1) {
97 ret = kernel_disable_channel(kchan);
7885e399 98 if (ret < 0 && ret != -EEXIST) {
f73fabfd 99 ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL;
54d01ffb
DG
100 goto error;
101 }
102 }
103
f73fabfd 104 ret = LTTNG_OK;
54d01ffb
DG
105
106error:
107 return ret;
108}
109
110/*
111 * Enable kernel channel of the kernel session.
112 */
113int channel_kernel_enable(struct ltt_kernel_session *ksession,
114 struct ltt_kernel_channel *kchan)
115{
116 int ret;
117
118 if (kchan->enabled == 0) {
119 ret = kernel_enable_channel(kchan);
120 if (ret < 0) {
f73fabfd 121 ret = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL;
54d01ffb
DG
122 goto error;
123 }
42224349 124 } else {
f73fabfd 125 ret = LTTNG_ERR_KERN_CHAN_EXIST;
42224349 126 goto error;
54d01ffb
DG
127 }
128
f73fabfd 129 ret = LTTNG_OK;
54d01ffb
DG
130
131error:
132 return ret;
133}
134
135/*
136 * Create kernel channel of the kernel session and notify kernel thread.
137 */
138int channel_kernel_create(struct ltt_kernel_session *ksession,
ff4d74e6 139 struct lttng_channel *attr, int kernel_pipe)
54d01ffb
DG
140{
141 int ret;
ff4d74e6 142 struct lttng_channel *defattr = NULL;
54d01ffb
DG
143
144 /* Creating channel attributes if needed */
145 if (attr == NULL) {
ff4d74e6
MD
146 defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL);
147 if (defattr == NULL) {
f73fabfd 148 ret = LTTNG_ERR_FATAL;
54d01ffb
DG
149 goto error;
150 }
ff4d74e6 151 attr = defattr;
54d01ffb
DG
152 }
153
154 /* Channel not found, creating it */
155 ret = kernel_create_channel(ksession, attr, ksession->trace_path);
156 if (ret < 0) {
f73fabfd 157 ret = LTTNG_ERR_KERN_CHAN_FAIL;
54d01ffb
DG
158 goto error;
159 }
160
161 /* Notify kernel thread that there is a new channel */
162 ret = notify_thread_pipe(kernel_pipe);
163 if (ret < 0) {
f73fabfd 164 ret = LTTNG_ERR_FATAL;
54d01ffb
DG
165 goto error;
166 }
167
f73fabfd 168 ret = LTTNG_OK;
54d01ffb 169error:
ff4d74e6 170 free(defattr);
54d01ffb
DG
171 return ret;
172}
7885e399
DG
173
174/*
175 * Enable UST channel for session and domain.
176 */
177int channel_ust_enable(struct ltt_ust_session *usess, int domain,
178 struct ltt_ust_channel *uchan)
179{
f73fabfd 180 int ret = LTTNG_OK;
7885e399
DG
181
182 /* If already enabled, everything is OK */
183 if (uchan->enabled) {
184 DBG3("Channel %s already enabled. Skipping", uchan->name);
f73fabfd 185 ret = LTTNG_ERR_UST_CHAN_EXIST;
7885e399
DG
186 goto end;
187 }
188
189 switch (domain) {
190 case LTTNG_DOMAIN_UST:
191 DBG2("Channel %s being enabled in UST global domain", uchan->name);
192 /* Enable channel for global domain */
193 ret = ust_app_enable_channel_glb(usess, uchan);
194 break;
d78d6610 195#if 0
7885e399
DG
196 case LTTNG_DOMAIN_UST_PID:
197 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
198 case LTTNG_DOMAIN_UST_EXEC_NAME:
d78d6610
DG
199#endif
200 default:
f73fabfd 201 ret = LTTNG_ERR_UND;
7885e399
DG
202 goto error;
203 }
204
205 if (ret < 0) {
49c336c1 206 if (ret != -LTTNG_UST_ERR_EXIST) {
f73fabfd 207 ret = LTTNG_ERR_UST_CHAN_ENABLE_FAIL;
7885e399
DG
208 goto error;
209 } else {
f73fabfd 210 ret = LTTNG_OK;
7885e399
DG
211 }
212 }
213
214 uchan->enabled = 1;
215 DBG2("Channel %s enabled successfully", uchan->name);
216
217end:
218error:
219 return ret;
220}
221
222/*
223 * Create UST channel for session and domain.
224 */
225int channel_ust_create(struct ltt_ust_session *usess, int domain,
226 struct lttng_channel *attr)
227{
f73fabfd 228 int ret = LTTNG_OK;
7885e399
DG
229 struct ltt_ust_channel *uchan = NULL;
230 struct lttng_channel *defattr = NULL;
231
232 /* Creating channel attributes if needed */
233 if (attr == NULL) {
234 defattr = channel_new_default_attr(domain);
235 if (defattr == NULL) {
f73fabfd 236 ret = LTTNG_ERR_FATAL;
7885e399
DG
237 goto error;
238 }
239 attr = defattr;
240 }
241
b024d072
MD
242 /*
243 * Validate UST buffer size and number of buffers: must both be
244 * power of 2 and nonzero. We validate right here for UST,
245 * because applications will not report the error to the user
246 * (unlike kernel tracing).
247 */
248 if (!attr->attr.subbuf_size || (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
f73fabfd 249 ret = LTTNG_ERR_INVALID;
b024d072
MD
250 goto error;
251 }
252 if (!attr->attr.num_subbuf || (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
f73fabfd 253 ret = LTTNG_ERR_INVALID;
b024d072
MD
254 goto error;
255 }
256
a79d84dd
DG
257 if (attr->attr.output != LTTNG_EVENT_MMAP) {
258 ret = LTTNG_ERR_NOT_SUPPORTED;
259 goto error;
260 }
261
7885e399
DG
262 /* Create UST channel */
263 uchan = trace_ust_create_channel(attr, usess->pathname);
264 if (uchan == NULL) {
f73fabfd 265 ret = LTTNG_ERR_FATAL;
7885e399
DG
266 goto error;
267 }
58f3ca76 268 uchan->enabled = 1;
7885e399
DG
269
270 switch (domain) {
271 case LTTNG_DOMAIN_UST:
272 DBG2("Channel %s being created in UST global domain", uchan->name);
58f3ca76 273
7885e399
DG
274 /* Enable channel for global domain */
275 ret = ust_app_create_channel_glb(usess, uchan);
276 break;
d78d6610 277#if 0
7885e399
DG
278 case LTTNG_DOMAIN_UST_PID:
279 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
280 case LTTNG_DOMAIN_UST_EXEC_NAME:
d78d6610 281#endif
7885e399 282 default:
f73fabfd 283 ret = LTTNG_ERR_UND;
7885e399
DG
284 goto error_free_chan;
285 }
286
49c336c1 287 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
f73fabfd 288 ret = LTTNG_ERR_UST_CHAN_FAIL;
7885e399
DG
289 goto error_free_chan;
290 }
291
fc34caaa
DG
292 /* Adding the channel to the channel hash table. */
293 rcu_read_lock();
294 lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node);
295 rcu_read_unlock();
296
7885e399
DG
297 DBG2("Channel %s created successfully", uchan->name);
298
299 free(defattr);
f73fabfd 300 return LTTNG_OK;
7885e399
DG
301
302error_free_chan:
24d1723f
DG
303 /*
304 * No need to remove the channel from the hash table because at this point
305 * it was not added hence the direct call and no call_rcu().
306 */
7885e399
DG
307 trace_ust_destroy_channel(uchan);
308error:
309 free(defattr);
310 return ret;
311}
312
313/*
314 * Disable UST channel for session and domain.
315 */
316int channel_ust_disable(struct ltt_ust_session *usess, int domain,
317 struct ltt_ust_channel *uchan)
318{
f73fabfd 319 int ret = LTTNG_OK;
7885e399
DG
320
321 /* Already disabled */
322 if (uchan->enabled == 0) {
323 DBG2("Channel UST %s already disabled", uchan->name);
324 goto end;
325 }
326
327 /* Get the right channel's hashtable */
328 switch (domain) {
329 case LTTNG_DOMAIN_UST:
330 DBG2("Channel %s being disabled in UST global domain", uchan->name);
7885e399
DG
331 /* Disable channel for global domain */
332 ret = ust_app_disable_channel_glb(usess, uchan);
333 break;
d78d6610 334#if 0
7885e399
DG
335 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
336 case LTTNG_DOMAIN_UST_EXEC_NAME:
337 case LTTNG_DOMAIN_UST_PID:
d78d6610
DG
338#endif
339 default:
f73fabfd 340 ret = LTTNG_ERR_UND;
7885e399
DG
341 goto error;
342 }
343
49c336c1 344 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
f73fabfd 345 ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL;
7885e399
DG
346 goto error;
347 }
348
349 uchan->enabled = 0;
350
351 DBG2("Channel %s disabled successfully", uchan->name);
352
f73fabfd 353 return LTTNG_OK;
7885e399
DG
354
355end:
356error:
357 return ret;
358}
This page took 0.044242 seconds and 4 git commands to generate.