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
);
260 if (new_stream
== NULL
) {
261 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
264 if (ctx
->on_recv_stream
!= NULL
) {
265 ret
= ctx
->on_recv_stream(new_stream
);
267 consumer_add_stream(new_stream
);
268 } else if (ret
< 0) {
272 consumer_add_stream(new_stream
);
276 case LTTNG_CONSUMER_UPDATE_STREAM
:
278 if (ctx
->on_update_stream
!= NULL
) {
279 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
281 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
282 } else if (ret
< 0) {
286 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
295 /* signal the poll thread */
296 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
298 perror("write consumer poll");
305 * Consume data on a file descriptor and write it on a trace file.
307 int lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
308 struct lttng_consumer_local_data
*ctx
)
313 int infd
= stream
->wait_fd
;
315 DBG("In read_subbuffer (infd : %d)", infd
);
316 /* Get the next subbuffer */
317 err
= kernctl_get_next_subbuf(infd
);
321 * This is a debug message even for single-threaded consumer,
322 * because poll() have more relaxed criterions than get subbuf,
323 * so get_subbuf may fail for short race windows where poll()
324 * would issue wakeups.
326 DBG("Reserving sub buffer failed (everything is normal, "
327 "it is due to concurrency)");
331 switch (stream
->output
) {
332 case LTTNG_EVENT_SPLICE
:
333 /* read the whole subbuffer */
334 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
337 perror("Getting sub-buffer len failed.");
341 /* splice the subbuffer to the tracefile */
342 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, len
);
345 * display the error but continue processing to try
346 * to release the subbuffer
348 ERR("Error splicing to tracefile");
351 case LTTNG_EVENT_MMAP
:
352 /* read the used subbuffer size */
353 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
356 perror("Getting sub-buffer len failed.");
359 /* write the subbuffer to the tracefile */
360 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
363 * display the error but continue processing to try
364 * to release the subbuffer
366 ERR("Error writing to tracefile");
370 ERR("Unknown output method");
374 err
= kernctl_put_next_subbuf(infd
);
377 if (errno
== EFAULT
) {
378 perror("Error in unreserving sub buffer\n");
379 } else if (errno
== EIO
) {
380 /* Should never happen with newer LTTng versions */
381 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
390 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
394 /* Opening the tracefile in write mode */
395 if (stream
->path_name
!= NULL
) {
396 ret
= open(stream
->path_name
,
397 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
399 ERR("Opening %s", stream
->path_name
);
403 stream
->out_fd
= ret
;
406 if (stream
->output
== LTTNG_EVENT_MMAP
) {
407 /* get the len of the mmap region */
408 unsigned long mmap_len
;
410 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
413 perror("kernctl_get_mmap_len");
416 stream
->mmap_len
= (size_t) mmap_len
;
418 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
,
419 PROT_READ
, MAP_PRIVATE
, stream
->wait_fd
, 0);
420 if (stream
->mmap_base
== MAP_FAILED
) {
421 perror("Error mmaping");
427 /* we return 0 to let the library handle the FD internally */
434 err
= close(stream
->out_fd
);