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_FD
);
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
;
243 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
246 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
247 if (ret
!= sizeof(fds
)) {
248 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
252 fds
[1] = fds
[0]; /* duplicate same fd if recv only one */
254 DBG("consumer_add_stream %s (%d,%d)", msg
.u
.stream
.path_name
,
256 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
257 msg
.u
.stream
.stream_key
,
260 msg
.u
.stream
.mmap_len
,
262 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");