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
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
32 #include <common/common.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/compat/fcntl.h>
37 #include "kernel-consumer.h"
39 extern struct lttng_consumer_global_data consumer_data
;
40 extern int consumer_poll_timeout
;
41 extern volatile int consumer_quit
;
44 * Mmap the ring buffer, read it and write the data to the tracefile.
46 * Returns the number of bytes written
48 int lttng_kconsumer_on_read_subbuffer_mmap(
49 struct lttng_consumer_local_data
*ctx
,
50 struct lttng_consumer_stream
*stream
, unsigned long len
)
52 unsigned long mmap_offset
;
54 off_t orig_offset
= stream
->out_fd_offset
;
55 int fd
= stream
->wait_fd
;
56 int outfd
= stream
->out_fd
;
58 /* get the offset inside the fd to mmap */
59 ret
= kernctl_get_mmap_read_offset(fd
, &mmap_offset
);
62 perror("kernctl_get_mmap_read_offset");
67 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
72 perror("Error in file write");
75 /* This won't block, but will start writeout asynchronously */
76 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
77 SYNC_FILE_RANGE_WRITE
);
78 stream
->out_fd_offset
+= ret
;
81 lttng_consumer_sync_trace_file(stream
, orig_offset
);
90 * Splice the data from the ring buffer to the tracefile.
92 * Returns the number of bytes spliced.
94 int lttng_kconsumer_on_read_subbuffer_splice(
95 struct lttng_consumer_local_data
*ctx
,
96 struct lttng_consumer_stream
*stream
, unsigned long len
)
100 off_t orig_offset
= stream
->out_fd_offset
;
101 int fd
= stream
->wait_fd
;
102 int outfd
= stream
->out_fd
;
105 DBG("splice chan to pipe offset %lu (fd : %d)",
106 (unsigned long)offset
, fd
);
107 ret
= splice(fd
, &offset
, ctx
->consumer_thread_pipe
[1], NULL
, len
,
108 SPLICE_F_MOVE
| SPLICE_F_MORE
);
109 DBG("splice chan to pipe ret %ld", ret
);
112 perror("Error in relay splice");
116 ret
= splice(ctx
->consumer_thread_pipe
[0], NULL
, outfd
, NULL
, ret
,
117 SPLICE_F_MOVE
| SPLICE_F_MORE
);
118 DBG("splice pipe to file %ld", ret
);
121 perror("Error in file splice");
125 /* This won't block, but will start writeout asynchronously */
126 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
127 SYNC_FILE_RANGE_WRITE
);
128 stream
->out_fd_offset
+= ret
;
130 lttng_consumer_sync_trace_file(stream
, orig_offset
);
135 /* send the appropriate error description to sessiond */
138 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_EBADF
);
141 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_EINVAL
);
144 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_ENOMEM
);
147 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_ESPIPE
);
156 * Take a snapshot for a specific fd
158 * Returns 0 on success, < 0 on error
160 int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
161 struct lttng_consumer_stream
*stream
)
164 int infd
= stream
->wait_fd
;
166 ret
= kernctl_snapshot(infd
);
169 perror("Getting sub-buffer snapshot.");
176 * Get the produced position
178 * Returns 0 on success, < 0 on error
180 int lttng_kconsumer_get_produced_snapshot(
181 struct lttng_consumer_local_data
*ctx
,
182 struct lttng_consumer_stream
*stream
,
186 int infd
= stream
->wait_fd
;
188 ret
= kernctl_snapshot_get_produced(infd
, pos
);
191 perror("kernctl_snapshot_get_produced");
197 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
198 int sock
, struct pollfd
*consumer_sockpoll
)
201 struct lttcomm_consumer_msg msg
;
203 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
204 if (ret
!= sizeof(msg
)) {
205 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_CMD
);
208 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
212 switch (msg
.cmd_type
) {
213 case LTTNG_CONSUMER_ADD_CHANNEL
:
215 struct lttng_consumer_channel
*new_channel
;
217 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
218 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
220 msg
.u
.channel
.mmap_len
,
221 msg
.u
.channel
.max_sb_size
);
222 if (new_channel
== NULL
) {
223 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
226 if (ctx
->on_recv_channel
!= NULL
) {
227 ret
= ctx
->on_recv_channel(new_channel
);
229 consumer_add_channel(new_channel
);
230 } else if (ret
< 0) {
234 consumer_add_channel(new_channel
);
238 case LTTNG_CONSUMER_ADD_STREAM
:
240 struct lttng_consumer_stream
*new_stream
;
244 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
247 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
248 if (ret
!= sizeof(fd
)) {
249 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
253 DBG("consumer_add_stream %s (%d)", msg
.u
.stream
.path_name
,
255 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
256 msg
.u
.stream
.stream_key
,
259 msg
.u
.stream
.mmap_len
,
261 msg
.u
.stream
.path_name
,
264 if (new_stream
== NULL
) {
265 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
268 if (ctx
->on_recv_stream
!= NULL
) {
269 ret
= ctx
->on_recv_stream(new_stream
);
271 consumer_add_stream(new_stream
);
272 } else if (ret
< 0) {
276 consumer_add_stream(new_stream
);
280 case LTTNG_CONSUMER_UPDATE_STREAM
:
282 if (ctx
->on_update_stream
!= NULL
) {
283 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
285 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
286 } else if (ret
< 0) {
290 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
299 /* signal the poll thread */
300 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
302 perror("write consumer poll");
309 * Consume data on a file descriptor and write it on a trace file.
311 int lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
312 struct lttng_consumer_local_data
*ctx
)
317 int infd
= stream
->wait_fd
;
319 DBG("In read_subbuffer (infd : %d)", infd
);
320 /* Get the next subbuffer */
321 err
= kernctl_get_next_subbuf(infd
);
325 * This is a debug message even for single-threaded consumer,
326 * because poll() have more relaxed criterions than get subbuf,
327 * so get_subbuf may fail for short race windows where poll()
328 * would issue wakeups.
330 DBG("Reserving sub buffer failed (everything is normal, "
331 "it is due to concurrency)");
335 switch (stream
->output
) {
336 case LTTNG_EVENT_SPLICE
:
337 /* read the whole subbuffer */
338 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
341 perror("Getting sub-buffer len failed.");
345 /* splice the subbuffer to the tracefile */
346 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, len
);
349 * display the error but continue processing to try
350 * to release the subbuffer
352 ERR("Error splicing to tracefile");
355 case LTTNG_EVENT_MMAP
:
356 /* read the used subbuffer size */
357 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
360 perror("Getting sub-buffer len failed.");
363 /* write the subbuffer to the tracefile */
364 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
367 * display the error but continue processing to try
368 * to release the subbuffer
370 ERR("Error writing to tracefile");
374 ERR("Unknown output method");
378 err
= kernctl_put_next_subbuf(infd
);
381 if (errno
== EFAULT
) {
382 perror("Error in unreserving sub buffer\n");
383 } else if (errno
== EIO
) {
384 /* Should never happen with newer LTTng versions */
385 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
394 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
398 /* Opening the tracefile in write mode */
399 if (stream
->path_name
!= NULL
) {
400 ret
= run_as_open(stream
->path_name
,
401 O_WRONLY
|O_CREAT
|O_TRUNC
,
402 S_IRWXU
|S_IRWXG
|S_IRWXO
,
403 stream
->uid
, stream
->gid
);
405 ERR("Opening %s", stream
->path_name
);
409 stream
->out_fd
= ret
;
412 if (stream
->output
== LTTNG_EVENT_MMAP
) {
413 /* get the len of the mmap region */
414 unsigned long mmap_len
;
416 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
419 perror("kernctl_get_mmap_len");
422 stream
->mmap_len
= (size_t) mmap_len
;
424 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
,
425 PROT_READ
, MAP_PRIVATE
, stream
->wait_fd
, 0);
426 if (stream
->mmap_base
== MAP_FAILED
) {
427 perror("Error mmaping");
433 /* we return 0 to let the library handle the FD internally */
440 err
= close(stream
->out_fd
);