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>
37 extern struct lttng_consumer_global_data consumer_data
;
38 extern int consumer_poll_timeout
;
39 extern volatile int consumer_quit
;
42 * Mmap the ring buffer, read it and write the data to the tracefile.
44 * Returns the number of bytes written
46 int lttng_kconsumer_on_read_subbuffer_mmap(
47 struct lttng_consumer_local_data
*ctx
,
48 struct lttng_consumer_stream
*stream
, unsigned long len
)
50 unsigned long mmap_offset
;
52 off_t orig_offset
= stream
->out_fd_offset
;
53 int fd
= stream
->wait_fd
;
54 int outfd
= stream
->out_fd
;
56 /* get the offset inside the fd to mmap */
57 ret
= kernctl_get_mmap_read_offset(fd
, &mmap_offset
);
60 perror("kernctl_get_mmap_read_offset");
65 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
70 perror("Error in file write");
73 /* This won't block, but will start writeout asynchronously */
74 sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
75 SYNC_FILE_RANGE_WRITE
);
76 stream
->out_fd_offset
+= ret
;
79 lttng_consumer_sync_trace_file(stream
, orig_offset
);
88 * Splice the data from the ring buffer to the tracefile.
90 * Returns the number of bytes spliced.
92 int lttng_kconsumer_on_read_subbuffer_splice(
93 struct lttng_consumer_local_data
*ctx
,
94 struct lttng_consumer_stream
*stream
, unsigned long len
)
98 off_t orig_offset
= stream
->out_fd_offset
;
99 int fd
= stream
->wait_fd
;
100 int outfd
= stream
->out_fd
;
103 DBG("splice chan to pipe offset %lu (fd : %d)",
104 (unsigned long)offset
, fd
);
105 ret
= splice(fd
, &offset
, ctx
->consumer_thread_pipe
[1], NULL
, len
,
106 SPLICE_F_MOVE
| SPLICE_F_MORE
);
107 DBG("splice chan to pipe ret %ld", ret
);
110 perror("Error in relay splice");
114 ret
= splice(ctx
->consumer_thread_pipe
[0], NULL
, outfd
, NULL
, ret
,
115 SPLICE_F_MOVE
| SPLICE_F_MORE
);
116 DBG("splice pipe to file %ld", ret
);
119 perror("Error in file splice");
123 /* This won't block, but will start writeout asynchronously */
124 sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
125 SYNC_FILE_RANGE_WRITE
);
126 stream
->out_fd_offset
+= ret
;
128 lttng_consumer_sync_trace_file(stream
, orig_offset
);
133 /* send the appropriate error description to sessiond */
136 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_EBADF
);
139 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_EINVAL
);
142 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_ENOMEM
);
145 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_ESPIPE
);
154 * Take a snapshot for a specific fd
156 * Returns 0 on success, < 0 on error
158 int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
159 struct lttng_consumer_stream
*stream
)
162 int infd
= stream
->wait_fd
;
164 ret
= kernctl_snapshot(infd
);
167 perror("Getting sub-buffer snapshot.");
174 * Get the produced position
176 * Returns 0 on success, < 0 on error
178 int lttng_kconsumer_get_produced_snapshot(
179 struct lttng_consumer_local_data
*ctx
,
180 struct lttng_consumer_stream
*stream
,
184 int infd
= stream
->wait_fd
;
186 ret
= kernctl_snapshot_get_produced(infd
, pos
);
189 perror("kernctl_snapshot_get_produced");
195 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
196 int sock
, struct pollfd
*consumer_sockpoll
)
199 struct lttcomm_consumer_msg msg
;
201 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
202 if (ret
!= sizeof(msg
)) {
203 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_CMD
);
206 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
210 switch (msg
.cmd_type
) {
211 case LTTNG_CONSUMER_ADD_CHANNEL
:
213 struct lttng_consumer_channel
*new_channel
;
215 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
216 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
218 msg
.u
.channel
.mmap_len
,
219 msg
.u
.channel
.max_sb_size
);
220 if (new_channel
== NULL
) {
221 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
224 if (ctx
->on_recv_channel
!= NULL
) {
225 ret
= ctx
->on_recv_channel(new_channel
);
227 consumer_add_channel(new_channel
);
228 } else if (ret
< 0) {
232 consumer_add_channel(new_channel
);
236 case LTTNG_CONSUMER_ADD_STREAM
:
238 struct lttng_consumer_stream
*new_stream
;
242 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
245 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
246 if (ret
!= sizeof(fd
)) {
247 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
251 DBG("consumer_add_stream %s (%d)", msg
.u
.stream
.path_name
,
253 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
254 msg
.u
.stream
.stream_key
,
257 msg
.u
.stream
.mmap_len
,
259 msg
.u
.stream
.path_name
,
262 if (new_stream
== NULL
) {
263 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
266 if (ctx
->on_recv_stream
!= NULL
) {
267 ret
= ctx
->on_recv_stream(new_stream
);
269 consumer_add_stream(new_stream
);
270 } else if (ret
< 0) {
274 consumer_add_stream(new_stream
);
278 case LTTNG_CONSUMER_UPDATE_STREAM
:
280 if (ctx
->on_update_stream
!= NULL
) {
281 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
283 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
284 } else if (ret
< 0) {
288 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
297 /* signal the poll thread */
298 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
300 perror("write consumer poll");
307 * Consume data on a file descriptor and write it on a trace file.
309 int lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
310 struct lttng_consumer_local_data
*ctx
)
315 int infd
= stream
->wait_fd
;
317 DBG("In read_subbuffer (infd : %d)", infd
);
318 /* Get the next subbuffer */
319 err
= kernctl_get_next_subbuf(infd
);
323 * This is a debug message even for single-threaded consumer,
324 * because poll() have more relaxed criterions than get subbuf,
325 * so get_subbuf may fail for short race windows where poll()
326 * would issue wakeups.
328 DBG("Reserving sub buffer failed (everything is normal, "
329 "it is due to concurrency)");
333 switch (stream
->output
) {
334 case LTTNG_EVENT_SPLICE
:
335 /* read the whole subbuffer */
336 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
339 perror("Getting sub-buffer len failed.");
343 /* splice the subbuffer to the tracefile */
344 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, len
);
347 * display the error but continue processing to try
348 * to release the subbuffer
350 ERR("Error splicing to tracefile");
353 case LTTNG_EVENT_MMAP
:
354 /* read the used subbuffer size */
355 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
358 perror("Getting sub-buffer len failed.");
361 /* write the subbuffer to the tracefile */
362 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
365 * display the error but continue processing to try
366 * to release the subbuffer
368 ERR("Error writing to tracefile");
372 ERR("Unknown output method");
376 err
= kernctl_put_next_subbuf(infd
);
379 if (errno
== EFAULT
) {
380 perror("Error in unreserving sub buffer\n");
381 } else if (errno
== EIO
) {
382 /* Should never happen with newer LTTng versions */
383 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
392 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
396 /* Opening the tracefile in write mode */
397 if (stream
->path_name
!= NULL
) {
398 ret
= open(stream
->path_name
,
399 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
401 ERR("Opening %s", stream
->path_name
);
405 stream
->out_fd
= ret
;
406 ret
= chown(stream
->path_name
, stream
->uid
, stream
->gid
);
408 ERR("Changing ownership of %s", stream
->path_name
);
413 if (stream
->output
== LTTNG_EVENT_MMAP
) {
414 /* get the len of the mmap region */
415 unsigned long mmap_len
;
417 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
420 perror("kernctl_get_mmap_len");
423 stream
->mmap_len
= (size_t) mmap_len
;
425 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
,
426 PROT_READ
, MAP_PRIVATE
, stream
->wait_fd
, 0);
427 if (stream
->mmap_base
== MAP_FAILED
) {
428 perror("Error mmaping");
434 /* we return 0 to let the library handle the FD internally */
441 err
= close(stream
->out_fd
);