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 modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/socket.h>
27 #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/sessiond-comm/relayd.h>
36 #include <common/compat/fcntl.h>
37 #include <common/relayd/relayd.h>
39 #include "kernel-consumer.h"
41 extern struct lttng_consumer_global_data consumer_data
;
42 extern int consumer_poll_timeout
;
43 extern volatile int consumer_quit
;
46 * Take a snapshot for a specific fd
48 * Returns 0 on success, < 0 on error
50 int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
51 struct lttng_consumer_stream
*stream
)
54 int infd
= stream
->wait_fd
;
56 ret
= kernctl_snapshot(infd
);
59 perror("Getting sub-buffer snapshot.");
66 * Get the produced position
68 * Returns 0 on success, < 0 on error
70 int lttng_kconsumer_get_produced_snapshot(
71 struct lttng_consumer_local_data
*ctx
,
72 struct lttng_consumer_stream
*stream
,
76 int infd
= stream
->wait_fd
;
78 ret
= kernctl_snapshot_get_produced(infd
, pos
);
81 perror("kernctl_snapshot_get_produced");
87 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
88 int sock
, struct pollfd
*consumer_sockpoll
)
91 struct lttcomm_consumer_msg msg
;
93 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
94 if (ret
!= sizeof(msg
)) {
95 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_CMD
);
98 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
102 /* relayd needs RCU read-side protection */
105 switch (msg
.cmd_type
) {
106 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
108 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
109 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
110 &msg
.u
.relayd_sock
.sock
);
113 case LTTNG_CONSUMER_ADD_CHANNEL
:
115 struct lttng_consumer_channel
*new_channel
;
117 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
118 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
120 msg
.u
.channel
.mmap_len
,
121 msg
.u
.channel
.max_sb_size
);
122 if (new_channel
== NULL
) {
123 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
126 if (ctx
->on_recv_channel
!= NULL
) {
127 ret
= ctx
->on_recv_channel(new_channel
);
129 consumer_add_channel(new_channel
);
130 } else if (ret
< 0) {
134 consumer_add_channel(new_channel
);
138 case LTTNG_CONSUMER_ADD_STREAM
:
141 struct consumer_relayd_sock_pair
*relayd
= NULL
;
142 struct lttng_consumer_stream
*new_stream
;
145 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
150 /* Get stream file descriptor from socket */
151 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
152 if (ret
!= sizeof(fd
)) {
153 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
158 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
159 msg
.u
.stream
.stream_key
,
162 msg
.u
.stream
.mmap_len
,
164 msg
.u
.stream
.path_name
,
167 msg
.u
.stream
.net_index
,
168 msg
.u
.stream
.metadata_flag
);
169 if (new_stream
== NULL
) {
170 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
174 /* The stream is not metadata. Get relayd reference if exists. */
175 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
176 if (relayd
!= NULL
) {
177 /* Add stream on the relayd */
178 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
179 ret
= relayd_add_stream(&relayd
->control_sock
,
180 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
181 &new_stream
->relayd_stream_id
);
182 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
186 } else if (msg
.u
.stream
.net_index
!= -1) {
187 ERR("Network sequence index %d unknown. Not adding stream.",
188 msg
.u
.stream
.net_index
);
193 if (ctx
->on_recv_stream
!= NULL
) {
194 ret
= ctx
->on_recv_stream(new_stream
);
196 consumer_add_stream(new_stream
);
197 } else if (ret
< 0) {
201 consumer_add_stream(new_stream
);
204 DBG("Kernel consumer_add_stream (%d)", fd
);
207 case LTTNG_CONSUMER_UPDATE_STREAM
:
212 case LTTNG_CONSUMER_DESTROY_RELAYD
:
214 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
215 struct consumer_relayd_sock_pair
*relayd
;
217 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
219 /* Get relayd reference if exists. */
220 relayd
= consumer_find_relayd(index
);
221 if (relayd
== NULL
) {
222 ERR("Unable to find relayd %" PRIu64
, index
);
227 * Each relayd socket pair has a refcount of stream attached to it
228 * which tells if the relayd is still active or not depending on the
231 * This will set the destroy flag of the relayd object and destroy it
232 * if the refcount reaches zero when called.
234 * The destroy can happen either here or when a stream fd hangs up.
236 consumer_flag_relayd_for_destroy(relayd
);
245 * Wake-up the other end by writing a null byte in the pipe (non-blocking).
246 * Important note: Because writing into the pipe is non-blocking (and
247 * therefore we allow dropping wakeup data, as long as there is wakeup data
248 * present in the pipe buffer to wake up the other end), the other end
249 * should perform the following sequence for waiting:
251 * 1) empty the pipe (reads).
252 * 2) perform update operation.
253 * 3) wait on the pipe (poll).
256 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
257 } while (ret
< 0 && errno
== EINTR
);
264 * Consume data on a file descriptor and write it on a trace file.
266 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
267 struct lttng_consumer_local_data
*ctx
)
272 int infd
= stream
->wait_fd
;
274 DBG("In read_subbuffer (infd : %d)", infd
);
275 /* Get the next subbuffer */
276 err
= kernctl_get_next_subbuf(infd
);
279 * This is a debug message even for single-threaded consumer,
280 * because poll() have more relaxed criterions than get subbuf,
281 * so get_subbuf may fail for short race windows where poll()
282 * would issue wakeups.
284 DBG("Reserving sub buffer failed (everything is normal, "
285 "it is due to concurrency)");
289 switch (stream
->output
) {
290 case LTTNG_EVENT_SPLICE
:
291 /* read the whole subbuffer */
292 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
295 perror("Getting sub-buffer len failed.");
299 /* splice the subbuffer to the tracefile */
300 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, len
);
303 * display the error but continue processing to try
304 * to release the subbuffer
306 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
311 case LTTNG_EVENT_MMAP
:
312 /* read the used subbuffer size */
313 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
316 perror("Getting sub-buffer len failed.");
319 /* write the subbuffer to the tracefile */
320 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
323 * display the error but continue processing to try
324 * to release the subbuffer
326 ERR("Error writing to tracefile");
330 ERR("Unknown output method");
334 err
= kernctl_put_next_subbuf(infd
);
337 if (errno
== EFAULT
) {
338 perror("Error in unreserving sub buffer\n");
339 } else if (errno
== EIO
) {
340 /* Should never happen with newer LTTng versions */
341 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
350 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
354 /* Opening the tracefile in write mode */
355 if (strlen(stream
->path_name
) > 0 && stream
->net_seq_idx
== -1) {
356 ret
= run_as_open(stream
->path_name
,
357 O_WRONLY
|O_CREAT
|O_TRUNC
,
358 S_IRWXU
|S_IRWXG
|S_IRWXO
,
359 stream
->uid
, stream
->gid
);
361 ERR("Opening %s", stream
->path_name
);
365 stream
->out_fd
= ret
;
368 if (stream
->output
== LTTNG_EVENT_MMAP
) {
369 /* get the len of the mmap region */
370 unsigned long mmap_len
;
372 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
375 perror("kernctl_get_mmap_len");
378 stream
->mmap_len
= (size_t) mmap_len
;
380 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
,
381 PROT_READ
, MAP_PRIVATE
, stream
->wait_fd
, 0);
382 if (stream
->mmap_base
== MAP_FAILED
) {
383 perror("Error mmaping");
389 /* we return 0 to let the library handle the FD internally */
396 err
= close(stream
->out_fd
);