lttng cli: Accept human readable sizes for --subbuf-size
[lttng-tools.git] / src / bin / lttng / commands / enable_channels.c
CommitLineData
d36b8583
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.
d36b8583
DG
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 *
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.
d36b8583
DG
16 */
17
18#define _GNU_SOURCE
19#include <popt.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
5a0de755 26#include <inttypes.h>
d36b8583 27
c399183f 28#include "../command.h"
d36b8583 29
42224349 30#include <src/common/sessiond-comm/sessiond-comm.h>
70d0b120 31#include <src/common/utils.h>
42224349 32
d36b8583 33static char *opt_channels;
5edd7e09 34static int opt_kernel;
5440dc42 35static char *opt_session_name;
d36b8583 36static int opt_userspace;
d78d6610 37static struct lttng_channel chan;
a79d84dd 38static char *opt_output;
7972aab2
DG
39static int opt_buffer_uid;
40static int opt_buffer_pid;
41static int opt_buffer_global;
d36b8583
DG
42
43enum {
44 OPT_HELP = 1,
7d29a247
DG
45 OPT_DISCARD,
46 OPT_OVERWRITE,
47 OPT_SUBBUF_SIZE,
48 OPT_NUM_SUBBUF,
49 OPT_SWITCH_TIMER,
50 OPT_READ_TIMER,
d36b8583 51 OPT_USERSPACE,
679b4943 52 OPT_LIST_OPTIONS,
1624d5b7
JD
53 OPT_TRACEFILE_SIZE,
54 OPT_TRACEFILE_COUNT,
d36b8583
DG
55};
56
cd80958d
DG
57static struct lttng_handle *handle;
58
a79d84dd
DG
59const char *output_mmap = "mmap";
60const char *output_splice = "splice";
61
d36b8583
DG
62static struct poptOption long_options[] = {
63 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
64 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 65 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
d36b8583 66 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610 67 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
7d29a247
DG
68 {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0},
69 {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0},
70d0b120 70 {"subbuf-size", 0, POPT_ARG_STRING, 0, OPT_SUBBUF_SIZE, 0, 0},
1b579521
DG
71 {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0},
72 {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0},
73 {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0},
679b4943 74 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
a79d84dd 75 {"output", 0, POPT_ARG_STRING, &opt_output, 0, 0, 0},
7972aab2
DG
76 {"buffers-uid", 0, POPT_ARG_VAL, &opt_buffer_uid, 1, 0, 0},
77 {"buffers-pid", 0, POPT_ARG_VAL, &opt_buffer_pid, 1, 0, 0},
78 {"buffers-global", 0, POPT_ARG_VAL, &opt_buffer_global, 1, 0, 0},
1624d5b7
JD
79 {"tracefile-size", 'C', POPT_ARG_INT, 0, OPT_TRACEFILE_SIZE, 0, 0},
80 {"tracefile-count", 'W', POPT_ARG_INT, 0, OPT_TRACEFILE_COUNT, 0, 0},
d36b8583
DG
81 {0, 0, 0, 0, 0, 0, 0}
82};
83
84/*
85 * usage
86 */
87static void usage(FILE *ofp)
88{
32a6298d 89 fprintf(ofp, "usage: lttng enable-channel NAME[,NAME2,...] [-u|-k] [OPTIONS]\n");
d36b8583 90 fprintf(ofp, "\n");
32a6298d 91 fprintf(ofp, "Options:\n");
7d29a247 92 fprintf(ofp, " -h, --help Show this help\n");
679b4943 93 fprintf(ofp, " --list-options Simple listing of options\n");
32a6298d 94 fprintf(ofp, " -s, --session NAME Apply to session name\n");
27221701 95 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
27221701 96 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
7d29a247
DG
97 fprintf(ofp, "\n");
98 fprintf(ofp, "Channel options:\n");
cabd9892
MD
99 fprintf(ofp, " --discard Discard event when buffers are full%s\n",
100 DEFAULT_CHANNEL_OVERWRITE ? "" : " (default)");
101 fprintf(ofp, " --overwrite Flight recorder mode%s\n",
102 DEFAULT_CHANNEL_OVERWRITE ? " (default)" : "");
70d0b120 103 fprintf(ofp, " --subbuf-size SIZE Subbuffer size in bytes {+k,+M,+G}\n");
3e230f92
SM
104 fprintf(ofp, " (default: %zu, kernel default: %zu)\n",
105 default_get_channel_subbuf_size(),
106 default_get_kernel_channel_subbuf_size());
93e6c8a0
MD
107 fprintf(ofp, " Needs to be a power of 2 for\n");
108 fprintf(ofp, " kernel and ust tracers\n");
cc62c0c0 109 fprintf(ofp, " --num-subbuf NUM Number of subbufers\n");
9f778c9a
MD
110 fprintf(ofp, " (default: %u)\n",
111 DEFAULT_CHANNEL_SUBBUF_NUM);
93e6c8a0
MD
112 fprintf(ofp, " Needs to be a power of 2 for\n");
113 fprintf(ofp, " kernel and ust tracers\n");
cc62c0c0 114 fprintf(ofp, " --switch-timer USEC Switch timer interval in usec (default: %u)\n",
cabd9892 115 DEFAULT_CHANNEL_SWITCH_TIMER);
db77f3a3
DG
116 fprintf(ofp, " --read-timer USEC Read timer interval in usec (UST default: %u, kernel default: %u)\n",
117 DEFAULT_UST_CHANNEL_READ_TIMER, DEFAULT_KERNEL_CHANNEL_READ_TIMER);
a79d84dd
DG
118 fprintf(ofp, " --output TYPE Channel output type (Values: %s, %s)\n",
119 output_mmap, output_splice);
7972aab2
DG
120 fprintf(ofp, " --buffers-uid Use per UID buffer (-u only)\n");
121 fprintf(ofp, " --buffers-pid Use per PID buffer (-u only)\n");
122 fprintf(ofp, " --buffers-global Use shared buffer for the whole system (-k only)\n");
1624d5b7 123 fprintf(ofp, " -C, --tracefile-size SIZE\n");
e132a0d4 124 fprintf(ofp, " Maximum size of each tracefile within a stream (in bytes).\n");
1624d5b7
JD
125 fprintf(ofp, " -W, --tracefile-count COUNT\n");
126 fprintf(ofp, " Used in conjunction with -C option, this will limit the number\n");
127 fprintf(ofp, " of files created to the specified count.\n");
d36b8583
DG
128 fprintf(ofp, "\n");
129}
130
5edd7e09
DG
131/*
132 * Set default attributes depending on those already defined from the command
133 * line.
134 */
135static void set_default_attr(struct lttng_domain *dom)
136{
137 struct lttng_channel_attr default_attr;
138
139 /* Set attributes */
140 lttng_channel_set_default_attr(dom, &default_attr);
141
142 if (chan.attr.overwrite == -1) {
143 chan.attr.overwrite = default_attr.overwrite;
144 }
145 if (chan.attr.subbuf_size == -1) {
146 chan.attr.subbuf_size = default_attr.subbuf_size;
147 }
148 if (chan.attr.num_subbuf == -1) {
149 chan.attr.num_subbuf = default_attr.num_subbuf;
150 }
151 if (chan.attr.switch_timer_interval == -1) {
152 chan.attr.switch_timer_interval = default_attr.switch_timer_interval;
153 }
154 if (chan.attr.read_timer_interval == -1) {
155 chan.attr.read_timer_interval = default_attr.read_timer_interval;
156 }
157 if (chan.attr.output == -1) {
158 chan.attr.output = default_attr.output;
159 }
1624d5b7
JD
160 if (chan.attr.tracefile_count == -1) {
161 chan.attr.tracefile_count = default_attr.tracefile_count;
162 }
163 if (chan.attr.tracefile_size == -1) {
164 chan.attr.tracefile_size = default_attr.tracefile_size;
165 }
5edd7e09
DG
166}
167
d36b8583 168/*
0fdd1e2c 169 * Adding channel using the lttng API.
d36b8583 170 */
cd80958d 171static int enable_channel(char *session_name)
d36b8583 172{
ae856491 173 int ret = CMD_SUCCESS, warn = 0;
d36b8583 174 char *channel_name;
7d29a247 175 struct lttng_domain dom;
d36b8583 176
441c16a7
MD
177 memset(&dom, 0, sizeof(dom));
178
d78d6610 179 /* Create lttng domain */
7d29a247
DG
180 if (opt_kernel) {
181 dom.type = LTTNG_DOMAIN_KERNEL;
7972aab2 182 dom.buf_type = LTTNG_BUFFER_GLOBAL;
88c5f0d8
DG
183 if (opt_buffer_uid || opt_buffer_pid) {
184 ERR("Buffer type not supported for domain -k");
185 ret = CMD_ERROR;
186 goto error;
187 }
d78d6610 188 } else if (opt_userspace) {
f6a9efaa 189 dom.type = LTTNG_DOMAIN_UST;
7972aab2
DG
190 if (opt_buffer_uid) {
191 dom.buf_type = LTTNG_BUFFER_PER_UID;
192 } else {
88c5f0d8
DG
193 if (opt_buffer_global) {
194 ERR("Buffer type not supported for domain -u");
195 ret = CMD_ERROR;
196 goto error;
197 }
7972aab2
DG
198 dom.buf_type = LTTNG_BUFFER_PER_PID;
199 }
0fdd1e2c 200 } else {
e14f64a8 201 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
d16c1a4c 202 ret = CMD_ERROR;
0fdd1e2c 203 goto error;
7d29a247
DG
204 }
205
5edd7e09
DG
206 set_default_attr(&dom);
207
1624d5b7
JD
208 if ((chan.attr.tracefile_size > 0) &&
209 (chan.attr.tracefile_size < chan.attr.subbuf_size)) {
210 ERR("Tracefile_size must be greater than or equal to subbuf_size "
211 "(%" PRIu64 " < %" PRIu64 ")",
212 chan.attr.tracefile_size, chan.attr.subbuf_size);
213 ret = CMD_ERROR;
214 goto error;
215 }
216
a79d84dd
DG
217 /* Setting channel output */
218 if (opt_output) {
219 if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) {
220 chan.attr.output = LTTNG_EVENT_MMAP;
221 } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) {
222 chan.attr.output = LTTNG_EVENT_SPLICE;
223 } else {
224 ERR("Unknown output type %s. Possible values are: %s, %s\n",
225 opt_output, output_mmap, output_splice);
226 usage(stderr);
227 ret = CMD_ERROR;
228 goto error;
229 }
230 }
231
cd80958d
DG
232 handle = lttng_create_handle(session_name, &dom);
233 if (handle == NULL) {
234 ret = -1;
235 goto error;
236 }
237
0fdd1e2c 238 /* Strip channel list (format: chan1,chan2,...) */
d36b8583
DG
239 channel_name = strtok(opt_channels, ",");
240 while (channel_name != NULL) {
0fdd1e2c
DG
241 /* Copy channel name and normalize it */
242 strncpy(chan.name, channel_name, NAME_MAX);
243 chan.name[NAME_MAX - 1] = '\0';
244
245 DBG("Enabling channel %s", channel_name);
246
247 ret = lttng_enable_channel(handle, &chan);
248 if (ret < 0) {
42224349 249 switch (-ret) {
f73fabfd
DG
250 case LTTNG_ERR_KERN_CHAN_EXIST:
251 case LTTNG_ERR_UST_CHAN_EXIST:
88c5f0d8 252 WARN("Channel %s: %s (session %s)", channel_name,
42224349
DG
253 lttng_strerror(ret), session_name);
254 goto error;
255 default:
256 ERR("Channel %s: %s (session %s)", channel_name,
257 lttng_strerror(ret), session_name);
258 break;
259 }
ae856491 260 warn = 1;
d36b8583 261 } else {
7885e399
DG
262 MSG("%s channel %s enabled for session %s",
263 opt_kernel ? "Kernel" : "UST", channel_name,
264 session_name);
d36b8583
DG
265 }
266
267 /* Next event */
268 channel_name = strtok(NULL, ",");
269 }
270
ae856491
DG
271 ret = CMD_SUCCESS;
272
d36b8583 273error:
ae856491
DG
274 if (warn) {
275 ret = CMD_WARNING;
276 }
277
cd80958d
DG
278 lttng_destroy_handle(handle);
279
d36b8583
DG
280 return ret;
281}
282
283/*
0fdd1e2c 284 * Default value for channel configuration.
7d29a247
DG
285 */
286static void init_channel_config(void)
287{
5edd7e09
DG
288 /*
289 * Put -1 everywhere so we can identify those set by the command line and
290 * those needed to be set by the default values.
291 */
292 memset(&chan.attr, -1, sizeof(chan.attr));
7d29a247
DG
293}
294
295/*
0fdd1e2c 296 * Add channel to trace session
d36b8583
DG
297 */
298int cmd_enable_channels(int argc, const char **argv)
299{
ca1c3607 300 int opt, ret = CMD_SUCCESS;
d36b8583 301 static poptContext pc;
cd80958d 302 char *session_name = NULL;
70d0b120 303 char *opt_arg = NULL;
d36b8583 304
7d29a247
DG
305 init_channel_config();
306
d36b8583
DG
307 pc = poptGetContext(NULL, argc, argv, long_options, 0);
308 poptReadDefaultConfig(pc, 0);
309
310 while ((opt = poptGetNextOpt(pc)) != -1) {
311 switch (opt) {
312 case OPT_HELP:
ca1c3607 313 usage(stdout);
d36b8583 314 goto end;
7d29a247
DG
315 case OPT_DISCARD:
316 chan.attr.overwrite = 0;
317 DBG("Channel set to discard");
318 break;
319 case OPT_OVERWRITE:
320 chan.attr.overwrite = 1;
321 DBG("Channel set to overwrite");
322 break;
323 case OPT_SUBBUF_SIZE:
70d0b120
SM
324 /* Parse the size */
325 opt_arg = poptGetOptArg(pc);
326 if (utils_parse_size_suffix(opt_arg, &chan.attr.subbuf_size) < 0) {
327 ERR("Wrong value the --subbuf-size parameter: %s", opt_arg);
328 ret = CMD_ERROR;
329 goto end;
330 }
331
332 /* Check if power of 2 */
333 if ((chan.attr.subbuf_size - 1) & chan.attr.subbuf_size) {
334 ERR("The subbuf size is not a power of 2: %" PRIu64 " (%s)",
335 chan.attr.subbuf_size, opt_arg);
336 ret = CMD_ERROR;
337 goto end;
338 }
339
5a0de755 340 DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size);
7d29a247
DG
341 break;
342 case OPT_NUM_SUBBUF:
ca1c3607 343 /* TODO Replace atoi with strtol and check for errors */
7d29a247 344 chan.attr.num_subbuf = atoi(poptGetOptArg(pc));
5a0de755 345 DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf);
7d29a247
DG
346 break;
347 case OPT_SWITCH_TIMER:
ca1c3607 348 /* TODO Replace atoi with strtol and check for errors */
7d29a247
DG
349 chan.attr.switch_timer_interval = atoi(poptGetOptArg(pc));
350 DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval);
351 break;
352 case OPT_READ_TIMER:
ca1c3607 353 /* TODO Replace atoi with strtol and check for errors */
7d29a247
DG
354 chan.attr.read_timer_interval = atoi(poptGetOptArg(pc));
355 DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval);
d36b8583 356 break;
eeac7d46
MD
357 case OPT_USERSPACE:
358 opt_userspace = 1;
eeac7d46 359 break;
1624d5b7
JD
360 case OPT_TRACEFILE_SIZE:
361 chan.attr.tracefile_size = atoll(poptGetOptArg(pc));
362 DBG("Maximum tracefile size set to %" PRIu64,
363 chan.attr.tracefile_size);
364 break;
365 case OPT_TRACEFILE_COUNT:
366 chan.attr.tracefile_count = atoll(poptGetOptArg(pc));
367 DBG("Maximum tracefile count set to %" PRIu64,
368 chan.attr.tracefile_count);
369 break;
679b4943
SM
370 case OPT_LIST_OPTIONS:
371 list_cmd_options(stdout, long_options);
679b4943 372 goto end;
d36b8583
DG
373 default:
374 usage(stderr);
375 ret = CMD_UNDEFINED;
376 goto end;
377 }
378 }
379
380 opt_channels = (char*) poptGetArg(pc);
381 if (opt_channels == NULL) {
7d29a247 382 ERR("Missing channel name.\n");
d36b8583 383 usage(stderr);
ca1c3607 384 ret = CMD_ERROR;
d36b8583
DG
385 goto end;
386 }
387
cd80958d
DG
388 if (!opt_session_name) {
389 session_name = get_session_name();
390 if (session_name == NULL) {
ca1c3607 391 ret = CMD_ERROR;
cd80958d
DG
392 goto end;
393 }
394 } else {
395 session_name = opt_session_name;
396 }
397
398 ret = enable_channel(session_name);
d36b8583
DG
399
400end:
5853fd43
DG
401 if (!opt_session_name && session_name) {
402 free(session_name);
403 }
ca1c3607 404 poptFreeContext(pc);
d36b8583
DG
405 return ret;
406}
This page took 0.058777 seconds and 5 git commands to generate.