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>
31 #include <common/common.h>
32 #include <common/kernel-ctl/kernel-ctl.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/sessiond-comm/relayd.h>
35 #include <common/compat/fcntl.h>
36 #include <common/relayd/relayd.h>
38 #include "kernel-consumer.h"
40 extern struct lttng_consumer_global_data consumer_data
;
41 extern int consumer_poll_timeout
;
42 extern volatile int consumer_quit
;
45 * Take a snapshot for a specific fd
47 * Returns 0 on success, < 0 on error
49 int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
50 struct lttng_consumer_stream
*stream
)
53 int infd
= stream
->wait_fd
;
55 ret
= kernctl_snapshot(infd
);
58 perror("Getting sub-buffer snapshot.");
65 * Get the produced position
67 * Returns 0 on success, < 0 on error
69 int lttng_kconsumer_get_produced_snapshot(
70 struct lttng_consumer_local_data
*ctx
,
71 struct lttng_consumer_stream
*stream
,
75 int infd
= stream
->wait_fd
;
77 ret
= kernctl_snapshot_get_produced(infd
, pos
);
80 perror("kernctl_snapshot_get_produced");
86 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
87 int sock
, struct pollfd
*consumer_sockpoll
)
90 struct lttcomm_consumer_msg msg
;
92 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
93 if (ret
!= sizeof(msg
)) {
94 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_CMD
);
97 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
101 /* relayd needs RCU read-side protection */
104 switch (msg
.cmd_type
) {
105 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
108 struct consumer_relayd_sock_pair
*relayd
;
110 DBG("Consumer adding relayd socket");
112 /* Get relayd reference if exists. */
113 relayd
= consumer_find_relayd(msg
.u
.relayd_sock
.net_index
);
114 if (relayd
== NULL
) {
115 /* Not found. Allocate one. */
116 relayd
= consumer_allocate_relayd_sock_pair(
117 msg
.u
.relayd_sock
.net_index
);
118 if (relayd
== NULL
) {
119 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
124 /* Poll on consumer socket. */
125 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
130 /* Get relayd socket from session daemon */
131 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
132 if (ret
!= sizeof(fd
)) {
133 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
137 /* Copy socket information and received FD */
138 switch (msg
.u
.relayd_sock
.type
) {
139 case LTTNG_STREAM_CONTROL
:
140 /* Copy received lttcomm socket */
141 lttcomm_copy_sock(&relayd
->control_sock
, &msg
.u
.relayd_sock
.sock
);
143 ret
= lttcomm_create_sock(&relayd
->control_sock
);
148 /* Close the created socket fd which is useless */
149 close(relayd
->control_sock
.fd
);
151 /* Assign new file descriptor */
152 relayd
->control_sock
.fd
= fd
;
154 case LTTNG_STREAM_DATA
:
155 /* Copy received lttcomm socket */
156 lttcomm_copy_sock(&relayd
->data_sock
, &msg
.u
.relayd_sock
.sock
);
157 ret
= lttcomm_create_sock(&relayd
->data_sock
);
162 /* Close the created socket fd which is useless */
163 close(relayd
->data_sock
.fd
);
165 /* Assign new file descriptor */
166 relayd
->data_sock
.fd
= fd
;
169 ERR("Unknown relayd socket type");
173 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
174 msg
.u
.relayd_sock
.type
== LTTNG_STREAM_CONTROL
? "control" : "data",
175 relayd
->net_seq_idx
, fd
);
178 * Add relayd socket pair to consumer data hashtable. If object already
179 * exists or on error, the function gracefully returns.
181 consumer_add_relayd(relayd
);
185 case LTTNG_CONSUMER_ADD_CHANNEL
:
187 struct lttng_consumer_channel
*new_channel
;
189 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
190 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
192 msg
.u
.channel
.mmap_len
,
193 msg
.u
.channel
.max_sb_size
);
194 if (new_channel
== NULL
) {
195 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
198 if (ctx
->on_recv_channel
!= NULL
) {
199 ret
= ctx
->on_recv_channel(new_channel
);
201 consumer_add_channel(new_channel
);
202 } else if (ret
< 0) {
206 consumer_add_channel(new_channel
);
210 case LTTNG_CONSUMER_ADD_STREAM
:
213 struct consumer_relayd_sock_pair
*relayd
= NULL
;
214 struct lttng_consumer_stream
*new_stream
;
217 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
222 /* Get stream file descriptor from socket */
223 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
224 if (ret
!= sizeof(fd
)) {
225 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
230 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
231 msg
.u
.stream
.stream_key
,
234 msg
.u
.stream
.mmap_len
,
236 msg
.u
.stream
.path_name
,
239 msg
.u
.stream
.net_index
,
240 msg
.u
.stream
.metadata_flag
);
241 if (new_stream
== NULL
) {
242 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
246 /* The stream is not metadata. Get relayd reference if exists. */
247 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
248 if (relayd
!= NULL
) {
249 /* Add stream on the relayd */
250 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
251 ret
= relayd_add_stream(&relayd
->control_sock
,
252 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
253 &new_stream
->relayd_stream_id
);
254 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
258 } else if (msg
.u
.stream
.net_index
!= -1) {
259 ERR("Network sequence index %d unknown. Not adding stream.",
260 msg
.u
.stream
.net_index
);
265 if (ctx
->on_recv_stream
!= NULL
) {
266 ret
= ctx
->on_recv_stream(new_stream
);
268 consumer_add_stream(new_stream
);
269 } else if (ret
< 0) {
273 consumer_add_stream(new_stream
);
276 DBG("Kernel consumer_add_stream (%d)", fd
);
279 case LTTNG_CONSUMER_UPDATE_STREAM
:
284 case LTTNG_CONSUMER_DESTROY_RELAYD
:
286 struct consumer_relayd_sock_pair
*relayd
;
288 DBG("Kernel consumer destroying relayd %zu",
289 msg
.u
.destroy_relayd
.net_seq_idx
);
291 /* Get relayd reference if exists. */
292 relayd
= consumer_find_relayd(msg
.u
.destroy_relayd
.net_seq_idx
);
293 if (relayd
== NULL
) {
294 ERR("Unable to find relayd %zu", msg
.u
.destroy_relayd
.net_seq_idx
);
298 /* Set destroy flag for this object */
299 uatomic_set(&relayd
->destroy_flag
, 1);
301 /* Destroy the relayd if refcount is 0 else set the destroy flag. */
302 if (uatomic_read(&relayd
->refcount
) == 0) {
303 consumer_destroy_relayd(relayd
);
312 * Wake-up the other end by writing a null byte in the pipe (non-blocking).
313 * Important note: Because writing into the pipe is non-blocking (and
314 * therefore we allow dropping wakeup data, as long as there is wakeup data
315 * present in the pipe buffer to wake up the other end), the other end
316 * should perform the following sequence for waiting:
318 * 1) empty the pipe (reads).
319 * 2) perform update operation.
320 * 3) wait on the pipe (poll).
323 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
324 } while (ret
< 0 && errno
== EINTR
);
331 * Consume data on a file descriptor and write it on a trace file.
333 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
334 struct lttng_consumer_local_data
*ctx
)
339 int infd
= stream
->wait_fd
;
341 DBG("In read_subbuffer (infd : %d)", infd
);
342 /* Get the next subbuffer */
343 err
= kernctl_get_next_subbuf(infd
);
346 * This is a debug message even for single-threaded consumer,
347 * because poll() have more relaxed criterions than get subbuf,
348 * so get_subbuf may fail for short race windows where poll()
349 * would issue wakeups.
351 DBG("Reserving sub buffer failed (everything is normal, "
352 "it is due to concurrency)");
356 switch (stream
->output
) {
357 case LTTNG_EVENT_SPLICE
:
358 /* read the whole subbuffer */
359 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
362 perror("Getting sub-buffer len failed.");
366 /* splice the subbuffer to the tracefile */
367 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, len
);
370 * display the error but continue processing to try
371 * to release the subbuffer
373 ERR("Error splicing to tracefile (ret: %ld != len: %ld)",
378 case LTTNG_EVENT_MMAP
:
379 /* read the used subbuffer size */
380 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
383 perror("Getting sub-buffer len failed.");
386 /* write the subbuffer to the tracefile */
387 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
390 * display the error but continue processing to try
391 * to release the subbuffer
393 ERR("Error writing to tracefile");
397 ERR("Unknown output method");
401 err
= kernctl_put_next_subbuf(infd
);
404 if (errno
== EFAULT
) {
405 perror("Error in unreserving sub buffer\n");
406 } else if (errno
== EIO
) {
407 /* Should never happen with newer LTTng versions */
408 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
417 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
421 /* Opening the tracefile in write mode */
422 if (strlen(stream
->path_name
) > 0 && stream
->net_seq_idx
== -1) {
423 ret
= run_as_open(stream
->path_name
,
424 O_WRONLY
|O_CREAT
|O_TRUNC
,
425 S_IRWXU
|S_IRWXG
|S_IRWXO
,
426 stream
->uid
, stream
->gid
);
428 ERR("Opening %s", stream
->path_name
);
432 stream
->out_fd
= ret
;
435 if (stream
->output
== LTTNG_EVENT_MMAP
) {
436 /* get the len of the mmap region */
437 unsigned long mmap_len
;
439 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
442 perror("kernctl_get_mmap_len");
445 stream
->mmap_len
= (size_t) mmap_len
;
447 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
,
448 PROT_READ
, MAP_PRIVATE
, stream
->wait_fd
, 0);
449 if (stream
->mmap_base
== MAP_FAILED
) {
450 perror("Error mmaping");
456 /* we return 0 to let the library handle the FD internally */
463 err
= close(stream
->out_fd
);