2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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.
26 #include <sys/socket.h>
27 #include <sys/types.h>
31 #include <common/common.h>
32 #include <common/kernel-ctl/kernel-ctl.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/compat/fcntl.h>
36 #include "kernel-consumer.h"
38 extern struct lttng_consumer_global_data consumer_data
;
39 extern int consumer_poll_timeout
;
40 extern volatile int consumer_quit
;
43 * Mmap the ring buffer, read it and write the data to the tracefile.
45 * Returns the number of bytes written
47 ssize_t
lttng_kconsumer_on_read_subbuffer_mmap(
48 struct lttng_consumer_local_data
*ctx
,
49 struct lttng_consumer_stream
*stream
, unsigned long len
)
51 unsigned long mmap_offset
;
53 off_t orig_offset
= stream
->out_fd_offset
;
54 int fd
= stream
->wait_fd
;
55 int outfd
= stream
->out_fd
;
57 /* get the offset inside the fd to mmap */
58 ret
= kernctl_get_mmap_read_offset(fd
, &mmap_offset
);
61 perror("kernctl_get_mmap_read_offset");
66 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
71 perror("Error in file write");
74 /* This won't block, but will start writeout asynchronously */
75 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
76 SYNC_FILE_RANGE_WRITE
);
77 stream
->out_fd_offset
+= ret
;
80 lttng_consumer_sync_trace_file(stream
, orig_offset
);
89 * Splice the data from the ring buffer to the tracefile.
91 * Returns the number of bytes spliced.
93 ssize_t
lttng_kconsumer_on_read_subbuffer_splice(
94 struct lttng_consumer_local_data
*ctx
,
95 struct lttng_consumer_stream
*stream
, unsigned long len
)
99 off_t orig_offset
= stream
->out_fd_offset
;
100 int fd
= stream
->wait_fd
;
101 int outfd
= stream
->out_fd
;
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
);
108 DBG("splice chan to pipe ret %zd", ret
);
111 perror("Error in relay splice");
115 ret
= splice(ctx
->consumer_thread_pipe
[0], NULL
, outfd
, NULL
, ret
,
116 SPLICE_F_MOVE
| SPLICE_F_MORE
);
117 DBG("splice pipe to file %zd", ret
);
120 perror("Error in file splice");
124 /* This won't block, but will start writeout asynchronously */
125 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
126 SYNC_FILE_RANGE_WRITE
);
127 stream
->out_fd_offset
+= ret
;
129 lttng_consumer_sync_trace_file(stream
, orig_offset
);
134 /* send the appropriate error description to sessiond */
137 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_EBADF
);
140 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_EINVAL
);
143 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_ENOMEM
);
146 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_ESPIPE
);
155 * Take a snapshot for a specific fd
157 * Returns 0 on success, < 0 on error
159 int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
160 struct lttng_consumer_stream
*stream
)
163 int infd
= stream
->wait_fd
;
165 ret
= kernctl_snapshot(infd
);
168 perror("Getting sub-buffer snapshot.");
175 * Get the produced position
177 * Returns 0 on success, < 0 on error
179 int lttng_kconsumer_get_produced_snapshot(
180 struct lttng_consumer_local_data
*ctx
,
181 struct lttng_consumer_stream
*stream
,
185 int infd
= stream
->wait_fd
;
187 ret
= kernctl_snapshot_get_produced(infd
, pos
);
190 perror("kernctl_snapshot_get_produced");
196 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
197 int sock
, struct pollfd
*consumer_sockpoll
)
200 struct lttcomm_consumer_msg msg
;
202 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
203 if (ret
!= sizeof(msg
)) {
204 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_CMD
);
207 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
211 switch (msg
.cmd_type
) {
212 case LTTNG_CONSUMER_ADD_CHANNEL
:
214 struct lttng_consumer_channel
*new_channel
;
216 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
217 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
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
);
225 if (ctx
->on_recv_channel
!= NULL
) {
226 ret
= ctx
->on_recv_channel(new_channel
);
228 consumer_add_channel(new_channel
);
229 } else if (ret
< 0) {
233 consumer_add_channel(new_channel
);
237 case LTTNG_CONSUMER_ADD_STREAM
:
239 struct lttng_consumer_stream
*new_stream
;
243 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
246 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
247 if (ret
!= sizeof(fd
)) {
248 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
252 DBG("consumer_add_stream %s (%d)", msg
.u
.stream
.path_name
,
254 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
255 msg
.u
.stream
.stream_key
,
258 msg
.u
.stream
.mmap_len
,
260 msg
.u
.stream
.path_name
,
263 if (new_stream
== NULL
) {
264 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
267 if (ctx
->on_recv_stream
!= NULL
) {
268 ret
= ctx
->on_recv_stream(new_stream
);
270 consumer_add_stream(new_stream
);
271 } else if (ret
< 0) {
275 consumer_add_stream(new_stream
);
279 case LTTNG_CONSUMER_UPDATE_STREAM
:
281 if (ctx
->on_update_stream
!= NULL
) {
282 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
284 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
285 } else if (ret
< 0) {
289 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
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).
310 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
311 } while (ret
== -1UL && errno
== EINTR
);
317 * Consume data on a file descriptor and write it on a trace file.
319 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
320 struct lttng_consumer_local_data
*ctx
)
325 int infd
= stream
->wait_fd
;
327 DBG("In read_subbuffer (infd : %d)", infd
);
328 /* Get the next subbuffer */
329 err
= kernctl_get_next_subbuf(infd
);
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.
337 DBG("Reserving sub buffer failed (everything is normal, "
338 "it is due to concurrency)");
342 switch (stream
->output
) {
343 case LTTNG_EVENT_SPLICE
:
344 /* read the whole subbuffer */
345 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
348 perror("Getting sub-buffer len failed.");
352 /* splice the subbuffer to the tracefile */
353 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, len
);
356 * display the error but continue processing to try
357 * to release the subbuffer
359 ERR("Error splicing to tracefile");
362 case LTTNG_EVENT_MMAP
:
363 /* read the used subbuffer size */
364 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
367 perror("Getting sub-buffer len failed.");
370 /* write the subbuffer to the tracefile */
371 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
374 * display the error but continue processing to try
375 * to release the subbuffer
377 ERR("Error writing to tracefile");
381 ERR("Unknown output method");
385 err
= kernctl_put_next_subbuf(infd
);
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.");
401 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
405 /* Opening the tracefile in write mode */
406 if (stream
->path_name
!= NULL
) {
407 ret
= run_as_open(stream
->path_name
,
408 O_WRONLY
|O_CREAT
|O_TRUNC
,
409 S_IRWXU
|S_IRWXG
|S_IRWXO
,
410 stream
->uid
, stream
->gid
);
412 ERR("Opening %s", stream
->path_name
);
416 stream
->out_fd
= ret
;
419 if (stream
->output
== LTTNG_EVENT_MMAP
) {
420 /* get the len of the mmap region */
421 unsigned long mmap_len
;
423 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
426 perror("kernctl_get_mmap_len");
429 stream
->mmap_len
= (size_t) mmap_len
;
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");
440 /* we return 0 to let the library handle the FD internally */
447 err
= close(stream
->out_fd
);