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.
28 #include <sys/socket.h>
29 #include <sys/types.h>
32 #include <lttng-kernel-ctl.h>
33 #include <lttng-sessiond-comm.h>
34 #include <lttng/lttng-kconsumer.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 int 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 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 int 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 %ld", 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 %ld", ret
);
120 perror("Error in file splice");
124 /* This won't block, but will start writeout asynchronously */
125 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
,
298 /* signal the poll thread */
299 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
301 perror("write consumer poll");
308 * Consume data on a file descriptor and write it on a trace file.
310 int lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
311 struct lttng_consumer_local_data
*ctx
)
316 int infd
= stream
->wait_fd
;
318 DBG("In read_subbuffer (infd : %d)", infd
);
319 /* Get the next subbuffer */
320 err
= kernctl_get_next_subbuf(infd
);
324 * This is a debug message even for single-threaded consumer,
325 * because poll() have more relaxed criterions than get subbuf,
326 * so get_subbuf may fail for short race windows where poll()
327 * would issue wakeups.
329 DBG("Reserving sub buffer failed (everything is normal, "
330 "it is due to concurrency)");
334 switch (stream
->output
) {
335 case LTTNG_EVENT_SPLICE
:
336 /* read the whole subbuffer */
337 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
340 perror("Getting sub-buffer len failed.");
344 /* splice the subbuffer to the tracefile */
345 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, len
);
348 * display the error but continue processing to try
349 * to release the subbuffer
351 ERR("Error splicing to tracefile");
354 case LTTNG_EVENT_MMAP
:
355 /* read the used subbuffer size */
356 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
359 perror("Getting sub-buffer len failed.");
362 /* write the subbuffer to the tracefile */
363 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
366 * display the error but continue processing to try
367 * to release the subbuffer
369 ERR("Error writing to tracefile");
373 ERR("Unknown output method");
377 err
= kernctl_put_next_subbuf(infd
);
380 if (errno
== EFAULT
) {
381 perror("Error in unreserving sub buffer\n");
382 } else if (errno
== EIO
) {
383 /* Should never happen with newer LTTng versions */
384 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
393 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
397 /* Opening the tracefile in write mode */
398 if (stream
->path_name
!= NULL
) {
399 ret
= open_run_as(stream
->path_name
,
400 O_WRONLY
|O_CREAT
|O_TRUNC
,
401 S_IRWXU
|S_IRWXG
|S_IRWXO
,
402 stream
->uid
, stream
->gid
);
404 ERR("Opening %s", stream
->path_name
);
408 stream
->out_fd
= ret
;
411 if (stream
->output
== LTTNG_EVENT_MMAP
) {
412 /* get the len of the mmap region */
413 unsigned long mmap_len
;
415 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
418 perror("kernctl_get_mmap_len");
421 stream
->mmap_len
= (size_t) mmap_len
;
423 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
,
424 PROT_READ
, MAP_PRIVATE
, stream
->wait_fd
, 0);
425 if (stream
->mmap_base
== MAP_FAILED
) {
426 perror("Error mmaping");
432 /* we return 0 to let the library handle the FD internally */
439 err
= close(stream
->out_fd
);