Fix: consumer fd recv thread should write into non-blocking pipe
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
CommitLineData
3bd1e081
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
3bd1e081
MD
8 *
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.
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.
3bd1e081
MD
17 */
18
19#define _GNU_SOURCE
20#include <assert.h>
3bd1e081
MD
21#include <poll.h>
22#include <pthread.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/mman.h>
26#include <sys/socket.h>
27#include <sys/types.h>
28#include <unistd.h>
dbb5dfe6 29#include <sys/stat.h>
3bd1e081 30
990570ed 31#include <common/common.h>
10a8a223 32#include <common/kernel-ctl/kernel-ctl.h>
10a8a223 33#include <common/sessiond-comm/sessiond-comm.h>
dbb5dfe6 34#include <common/compat/fcntl.h>
0857097f 35
10a8a223 36#include "kernel-consumer.h"
3bd1e081
MD
37
38extern struct lttng_consumer_global_data consumer_data;
39extern int consumer_poll_timeout;
40extern volatile int consumer_quit;
41
42/*
43 * Mmap the ring buffer, read it and write the data to the tracefile.
44 *
45 * Returns the number of bytes written
46 */
4078b776 47ssize_t lttng_kconsumer_on_read_subbuffer_mmap(
3bd1e081
MD
48 struct lttng_consumer_local_data *ctx,
49 struct lttng_consumer_stream *stream, unsigned long len)
50{
51 unsigned long mmap_offset;
4078b776 52 ssize_t ret = 0;
3bd1e081
MD
53 off_t orig_offset = stream->out_fd_offset;
54 int fd = stream->wait_fd;
55 int outfd = stream->out_fd;
56
57 /* get the offset inside the fd to mmap */
58 ret = kernctl_get_mmap_read_offset(fd, &mmap_offset);
59 if (ret != 0) {
87dc6a9c 60 errno = -ret;
3bd1e081
MD
61 perror("kernctl_get_mmap_read_offset");
62 goto end;
63 }
64
65 while (len > 0) {
66 ret = write(outfd, stream->mmap_base + mmap_offset, len);
67 if (ret >= len) {
68 len = 0;
69 } else if (ret < 0) {
87dc6a9c 70 errno = -ret;
3bd1e081
MD
71 perror("Error in file write");
72 goto end;
73 }
74 /* This won't block, but will start writeout asynchronously */
dbb5dfe6 75 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
3bd1e081
MD
76 SYNC_FILE_RANGE_WRITE);
77 stream->out_fd_offset += ret;
78 }
79
80 lttng_consumer_sync_trace_file(stream, orig_offset);
81
82 goto end;
83
84end:
85 return ret;
86}
87
88/*
89 * Splice the data from the ring buffer to the tracefile.
90 *
91 * Returns the number of bytes spliced.
92 */
4078b776 93ssize_t lttng_kconsumer_on_read_subbuffer_splice(
3bd1e081
MD
94 struct lttng_consumer_local_data *ctx,
95 struct lttng_consumer_stream *stream, unsigned long len)
96{
4078b776 97 ssize_t ret = 0;
3bd1e081
MD
98 loff_t offset = 0;
99 off_t orig_offset = stream->out_fd_offset;
100 int fd = stream->wait_fd;
101 int outfd = stream->out_fd;
102
103 while (len > 0) {
104 DBG("splice chan to pipe offset %lu (fd : %d)",
105 (unsigned long)offset, fd);
106 ret = splice(fd, &offset, ctx->consumer_thread_pipe[1], NULL, len,
107 SPLICE_F_MOVE | SPLICE_F_MORE);
630543f2 108 DBG("splice chan to pipe ret %zd", ret);
3bd1e081 109 if (ret < 0) {
87dc6a9c 110 errno = -ret;
3bd1e081
MD
111 perror("Error in relay splice");
112 goto splice_error;
113 }
114
115 ret = splice(ctx->consumer_thread_pipe[0], NULL, outfd, NULL, ret,
116 SPLICE_F_MOVE | SPLICE_F_MORE);
630543f2 117 DBG("splice pipe to file %zd", ret);
3bd1e081 118 if (ret < 0) {
87dc6a9c 119 errno = -ret;
3bd1e081
MD
120 perror("Error in file splice");
121 goto splice_error;
122 }
123 len -= ret;
124 /* This won't block, but will start writeout asynchronously */
dbb5dfe6 125 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
3bd1e081
MD
126 SYNC_FILE_RANGE_WRITE);
127 stream->out_fd_offset += ret;
128 }
129 lttng_consumer_sync_trace_file(stream, orig_offset);
130
131 goto end;
132
133splice_error:
134 /* send the appropriate error description to sessiond */
135 switch(ret) {
136 case EBADF:
137 lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_EBADF);
138 break;
139 case EINVAL:
140 lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_EINVAL);
141 break;
142 case ENOMEM:
143 lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_ENOMEM);
144 break;
145 case ESPIPE:
146 lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_ESPIPE);
147 break;
148 }
149
150end:
151 return ret;
152}
153
154/*
155 * Take a snapshot for a specific fd
156 *
157 * Returns 0 on success, < 0 on error
158 */
159int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
160 struct lttng_consumer_stream *stream)
161{
162 int ret = 0;
163 int infd = stream->wait_fd;
164
165 ret = kernctl_snapshot(infd);
166 if (ret != 0) {
87dc6a9c 167 errno = -ret;
3bd1e081
MD
168 perror("Getting sub-buffer snapshot.");
169 }
170
171 return ret;
172}
173
174/*
175 * Get the produced position
176 *
177 * Returns 0 on success, < 0 on error
178 */
179int lttng_kconsumer_get_produced_snapshot(
180 struct lttng_consumer_local_data *ctx,
181 struct lttng_consumer_stream *stream,
182 unsigned long *pos)
183{
184 int ret;
185 int infd = stream->wait_fd;
186
187 ret = kernctl_snapshot_get_produced(infd, pos);
188 if (ret != 0) {
87dc6a9c 189 errno = -ret;
3bd1e081
MD
190 perror("kernctl_snapshot_get_produced");
191 }
192
193 return ret;
194}
195
196int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
197 int sock, struct pollfd *consumer_sockpoll)
198{
199 ssize_t ret;
200 struct lttcomm_consumer_msg msg;
201
202 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
203 if (ret != sizeof(msg)) {
f2fc6720 204 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_CMD);
3bd1e081
MD
205 return ret;
206 }
207 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
208 return -ENOENT;
209 }
210
211 switch (msg.cmd_type) {
212 case LTTNG_CONSUMER_ADD_CHANNEL:
213 {
214 struct lttng_consumer_channel *new_channel;
215
216 DBG("consumer_add_channel %d", msg.u.channel.channel_key);
217 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
218 -1, -1,
219 msg.u.channel.mmap_len,
220 msg.u.channel.max_sb_size);
221 if (new_channel == NULL) {
222 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
223 goto end_nosignal;
224 }
225 if (ctx->on_recv_channel != NULL) {
226 ret = ctx->on_recv_channel(new_channel);
227 if (ret == 0) {
228 consumer_add_channel(new_channel);
229 } else if (ret < 0) {
230 goto end_nosignal;
231 }
232 } else {
233 consumer_add_channel(new_channel);
234 }
235 goto end_nosignal;
236 }
237 case LTTNG_CONSUMER_ADD_STREAM:
238 {
239 struct lttng_consumer_stream *new_stream;
f2fc6720 240 int fd;
3bd1e081
MD
241
242 /* block */
243 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
244 return -EINTR;
245 }
f2fc6720
MD
246 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
247 if (ret != sizeof(fd)) {
3bd1e081
MD
248 lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD);
249 return ret;
250 }
3bd1e081 251
f2fc6720
MD
252 DBG("consumer_add_stream %s (%d)", msg.u.stream.path_name,
253 fd);
3bd1e081
MD
254 new_stream = consumer_allocate_stream(msg.u.stream.channel_key,
255 msg.u.stream.stream_key,
f2fc6720 256 fd, fd,
3bd1e081
MD
257 msg.u.stream.state,
258 msg.u.stream.mmap_len,
259 msg.u.stream.output,
6df2e2c9
MD
260 msg.u.stream.path_name,
261 msg.u.stream.uid,
262 msg.u.stream.gid);
3bd1e081
MD
263 if (new_stream == NULL) {
264 lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR);
265 goto end;
266 }
267 if (ctx->on_recv_stream != NULL) {
268 ret = ctx->on_recv_stream(new_stream);
269 if (ret == 0) {
270 consumer_add_stream(new_stream);
271 } else if (ret < 0) {
272 goto end;
273 }
274 } else {
275 consumer_add_stream(new_stream);
276 }
277 break;
278 }
279 case LTTNG_CONSUMER_UPDATE_STREAM:
280 {
281 if (ctx->on_update_stream != NULL) {
282 ret = ctx->on_update_stream(msg.u.stream.stream_key, msg.u.stream.state);
283 if (ret == 0) {
284 consumer_change_stream_state(msg.u.stream.stream_key, msg.u.stream.state);
285 } else if (ret < 0) {
286 goto end;
287 }
288 } else {
289 consumer_change_stream_state(msg.u.stream.stream_key,
290 msg.u.stream.state);
291 }
292 break;
293 }
294 default:
295 break;
296 }
297end:
04fdd819
MD
298 /*
299 * Wake-up the other end by writing a null byte in the pipe
300 * (non-blocking). Important note: Because writing into the
301 * pipe is non-blocking (and therefore we allow dropping wakeup
302 * data, as long as there is wakeup data present in the pipe
303 * buffer to wake up the other end), the other end should
304 * perform the following sequence for waiting:
305 * 1) empty the pipe (reads).
306 * 2) perform update operation.
307 * 3) wait on the pipe (poll).
308 */
309 do {
310 ret = write(ctx->consumer_poll_pipe[1], "", 1);
311 } while (ret == -1UL && errno == EINTR);
3bd1e081
MD
312end_nosignal:
313 return 0;
314}
d41f73b7
MD
315
316/*
317 * Consume data on a file descriptor and write it on a trace file.
318 */
4078b776 319ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
320 struct lttng_consumer_local_data *ctx)
321{
322 unsigned long len;
323 int err;
4078b776 324 ssize_t ret = 0;
d41f73b7
MD
325 int infd = stream->wait_fd;
326
327 DBG("In read_subbuffer (infd : %d)", infd);
328 /* Get the next subbuffer */
329 err = kernctl_get_next_subbuf(infd);
330 if (err != 0) {
d41f73b7
MD
331 /*
332 * This is a debug message even for single-threaded consumer,
333 * because poll() have more relaxed criterions than get subbuf,
334 * so get_subbuf may fail for short race windows where poll()
335 * would issue wakeups.
336 */
337 DBG("Reserving sub buffer failed (everything is normal, "
338 "it is due to concurrency)");
339 goto end;
340 }
341
342 switch (stream->output) {
343 case LTTNG_EVENT_SPLICE:
344 /* read the whole subbuffer */
345 err = kernctl_get_padded_subbuf_size(infd, &len);
346 if (err != 0) {
87dc6a9c 347 errno = -ret;
d41f73b7
MD
348 perror("Getting sub-buffer len failed.");
349 goto end;
350 }
351
352 /* splice the subbuffer to the tracefile */
353 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, len);
354 if (ret < 0) {
355 /*
356 * display the error but continue processing to try
357 * to release the subbuffer
358 */
359 ERR("Error splicing to tracefile");
360 }
361 break;
362 case LTTNG_EVENT_MMAP:
363 /* read the used subbuffer size */
364 err = kernctl_get_padded_subbuf_size(infd, &len);
365 if (err != 0) {
87dc6a9c 366 errno = -ret;
d41f73b7
MD
367 perror("Getting sub-buffer len failed.");
368 goto end;
369 }
370 /* write the subbuffer to the tracefile */
371 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len);
372 if (ret < 0) {
373 /*
374 * display the error but continue processing to try
375 * to release the subbuffer
376 */
377 ERR("Error writing to tracefile");
378 }
379 break;
380 default:
381 ERR("Unknown output method");
382 ret = -1;
383 }
384
385 err = kernctl_put_next_subbuf(infd);
386 if (err != 0) {
87dc6a9c 387 errno = -ret;
d41f73b7
MD
388 if (errno == EFAULT) {
389 perror("Error in unreserving sub buffer\n");
390 } else if (errno == EIO) {
391 /* Should never happen with newer LTTng versions */
392 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
393 }
394 goto end;
395 }
396
397end:
398 return ret;
399}
400
401int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
402{
403 int ret;
404
405 /* Opening the tracefile in write mode */
406 if (stream->path_name != NULL) {
e11d277b 407 ret = run_as_open(stream->path_name,
60b6c79c
MD
408 O_WRONLY|O_CREAT|O_TRUNC,
409 S_IRWXU|S_IRWXG|S_IRWXO,
410 stream->uid, stream->gid);
d41f73b7
MD
411 if (ret < 0) {
412 ERR("Opening %s", stream->path_name);
413 perror("open");
414 goto error;
415 }
416 stream->out_fd = ret;
417 }
418
419 if (stream->output == LTTNG_EVENT_MMAP) {
420 /* get the len of the mmap region */
421 unsigned long mmap_len;
422
423 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
424 if (ret != 0) {
87dc6a9c 425 errno = -ret;
d41f73b7
MD
426 perror("kernctl_get_mmap_len");
427 goto error_close_fd;
428 }
429 stream->mmap_len = (size_t) mmap_len;
430
431 stream->mmap_base = mmap(NULL, stream->mmap_len,
432 PROT_READ, MAP_PRIVATE, stream->wait_fd, 0);
433 if (stream->mmap_base == MAP_FAILED) {
434 perror("Error mmaping");
435 ret = -1;
436 goto error_close_fd;
437 }
438 }
439
440 /* we return 0 to let the library handle the FD internally */
441 return 0;
442
443error_close_fd:
444 {
445 int err;
446
447 err = close(stream->out_fd);
448 assert(!err);
449 }
450error:
451 return ret;
452}
453
This page took 0.049451 seconds and 4 git commands to generate.