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