Size-based rotation
[lttng-tools.git] / src / bin / lttng / commands / enable_rotation.c
CommitLineData
259c2674
JD
1/*
2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
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 _LGPL_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>
26#include <inttypes.h>
27#include <ctype.h>
28
29#include <common/sessiond-comm/sessiond-comm.h>
30#include <common/mi-lttng.h>
31#include <common/utils.h>
32
33#include "../command.h"
34#include <lttng/rotation.h>
35
36static char *opt_session_name;
37static struct mi_writer *writer;
38
39enum {
40 OPT_HELP = 1,
41 OPT_LIST_OPTIONS,
42 OPT_TIMER,
90936dcf 43 OPT_SIZE,
259c2674
JD
44};
45
46static struct poptOption long_options[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
49 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
50 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
51 {"timer", 0, POPT_ARG_INT, 0, OPT_TIMER, 0, 0},
90936dcf 52 {"size", 0, POPT_ARG_INT, 0, OPT_SIZE, 0, 0},
259c2674
JD
53 {0, 0, 0, 0, 0, 0, 0}
54};
55
90936dcf 56static int setup_rotate(char *session_name, uint64_t timer, uint64_t size)
259c2674
JD
57{
58 int ret = 0;
59 struct lttng_rotation_schedule_attr *attr = NULL;
60
61 attr = lttng_rotation_schedule_attr_create();
62 if (!attr) {
63 goto error;
64 }
65
66 ret = lttng_rotation_schedule_attr_set_session_name(attr, session_name);
67 if (ret < 0) {
68 goto error;
69 }
70
71 if (lttng_opt_mi) {
72 /* Open rotation_schedule element */
73 ret = mi_lttng_writer_open_element(writer,
74 config_element_rotation_schedule);
75 if (ret) {
76 goto error;
77 }
78
79 ret = mi_lttng_writer_write_element_string(writer,
80 mi_lttng_element_session_name, session_name);
81 if (ret) {
82 goto error;
83 }
84 }
85
86 if (timer) {
87 lttng_rotation_schedule_attr_set_timer_period(attr, timer);
88 MSG("Configuring session %s to rotate every %" PRIu64 " us",
89 session_name, timer);
90 if (lttng_opt_mi) {
91 ret = mi_lttng_writer_write_element_unsigned_int(writer,
92 config_element_rotation_timer_interval, timer);
93 if (ret) {
94 goto end;
95 }
96 }
97 }
90936dcf
JD
98 if (size) {
99 lttng_rotation_schedule_attr_set_size(attr, size);
100 MSG("Configuring session %s to rotate every %" PRIu64 " bytes written",
101 session_name, size);
102 if (lttng_opt_mi) {
103 ret = mi_lttng_writer_write_element_unsigned_int(writer,
104 config_element_rotation_size, size);
105 if (ret) {
106 goto end;
107 }
108 }
109 }
259c2674
JD
110
111 ret = lttng_rotation_set_schedule(attr);
112 if (ret) {
113 ERR("%s", lttng_strerror(ret));
114 if (lttng_opt_mi) {
115 ret = mi_lttng_writer_write_element_string(writer,
116 mi_lttng_element_rotate_status, "error");
117 if (ret) {
118 goto end;
119 }
120 /* Close rotation_schedule element */
121 ret = mi_lttng_writer_close_element(writer);
122 if (ret) {
123 goto end;
124 }
125 }
126 goto error;
127 }
128
129 if (lttng_opt_mi) {
130 ret = mi_lttng_writer_write_element_string(writer,
131 mi_lttng_element_rotate_status, "success");
132 if (ret) {
133 goto end;
134 }
135
136 /* Close rotation_schedule element */
137 ret = mi_lttng_writer_close_element(writer);
138 if (ret) {
139 goto end;
140 }
141 }
142
143 ret = 0;
144 goto end;
145
146error:
147 ret = -1;
148end:
149 return ret;
150}
151
152/*
153 * cmd_enable_rotation
154 *
155 * The 'enable-rotation <options>' first level command
156 */
157int cmd_enable_rotation(int argc, const char **argv)
158{
159 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
160 int popt_ret;
161 static poptContext pc;
162 char *session_name = NULL;
163 char *opt_arg = NULL;
259c2674 164 bool free_session_name = false;
90936dcf 165 uint64_t timer = 0, size = 0;
259c2674
JD
166
167 pc = poptGetContext(NULL, argc, argv, long_options, 0);
168 popt_ret = poptReadDefaultConfig(pc, 0);
169 if (popt_ret) {
170 ret = CMD_ERROR;
171 ERR("poptReadDefaultConfig");
172 goto end;
173 }
174
175 while ((opt = poptGetNextOpt(pc)) != -1) {
176 switch (opt) {
177 case OPT_HELP:
178 SHOW_HELP();
179 goto end;
180 case OPT_LIST_OPTIONS:
181 list_cmd_options(stdout, long_options);
182 goto end;
183 case OPT_TIMER:
184 errno = 0;
185 opt_arg = poptGetOptArg(pc);
186 if (errno != 0 || !isdigit(opt_arg[0])) {
187 ERR("Wrong value for --timer option: %s", opt_arg);
188 ret = CMD_ERROR;
189 goto end;
190 }
191 if (utils_parse_time_suffix(opt_arg, &timer) < 0 || timer == 0) {
192 ERR("Wrong value for --timer option: %s", opt_arg);
193 ret = CMD_ERROR;
194 goto end;
195 }
196 DBG("Rotation timer set to %" PRIu64, timer);
197 break;
90936dcf
JD
198 case OPT_SIZE:
199 errno = 0;
200 opt_arg = poptGetOptArg(pc);
201 if (utils_parse_size_suffix(opt_arg, &size) < 0 || !size) {
202 ERR("Wrong value for --size option: %s", opt_arg);
203 ret = CMD_ERROR;
204 goto end;
205 }
206 DBG("Rotation size set to %" PRIu64, size);
207 break;
259c2674
JD
208 default:
209 ret = CMD_UNDEFINED;
210 goto end;
211 }
212 }
213
214 if (opt_session_name == NULL) {
215 session_name = get_session_name();
216 if (session_name == NULL) {
217 goto end;
218 }
219 free_session_name = true;
220 } else {
221 session_name = opt_session_name;
222 }
223
224 /* Mi check */
225 if (lttng_opt_mi) {
226 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
227 if (!writer) {
228 ret = -LTTNG_ERR_NOMEM;
229 goto end;
230 }
231
232 /* Open command element */
233 ret = mi_lttng_writer_command_open(writer,
234 mi_lttng_element_command_enable_rotation);
235 if (ret) {
236 ret = CMD_ERROR;
237 goto end;
238 }
239
240 /* Open output element */
241 ret = mi_lttng_writer_open_element(writer,
242 mi_lttng_element_command_output);
243 if (ret) {
244 ret = CMD_ERROR;
245 goto end;
246 }
247 }
248
249 /* No config options, just rotate the session now */
90936dcf
JD
250 if (timer == 0 && size == 0) {
251 ERR("No timer or size given");
259c2674
JD
252 success = 0;
253 command_ret = -1;
254 } else {
90936dcf 255 command_ret = setup_rotate(session_name, timer, size);
259c2674
JD
256 }
257
258 if (command_ret) {
259 ERR("%s", lttng_strerror(command_ret));
260 success = 0;
261 }
262
263 /* Mi closing */
264 if (lttng_opt_mi) {
265 /* Close output element */
266 ret = mi_lttng_writer_close_element(writer);
267 if (ret) {
268 goto end;
269 }
270 /* Success ? */
271 ret = mi_lttng_writer_write_element_bool(writer,
272 mi_lttng_element_command_success, success);
273 if (ret) {
274 ret = CMD_ERROR;
275 goto end;
276 }
277
278 /* Command element close */
279 ret = mi_lttng_writer_command_close(writer);
280 if (ret) {
281 ret = CMD_ERROR;
282 goto end;
283 }
284 }
285
286end:
287 /* Mi clean-up */
288 if (writer && mi_lttng_writer_destroy(writer)) {
289 /* Preserve original error code */
290 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
291 }
292
293 /* Overwrite ret if an error occurred with start_tracing */
294 ret = command_ret ? command_ret : ret;
295 poptFreeContext(pc);
296 if (free_session_name) {
297 free(session_name);
298 }
299 return ret;
300}
This page took 0.03531 seconds and 5 git commands to generate.