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>
28 #include <sys/types.h>
30 #include <lttng/ust-ctl.h>
32 #include <common/common.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/relayd/relayd.h>
35 #include <common/compat/fcntl.h>
37 #include "ust-consumer.h"
39 extern struct lttng_consumer_global_data consumer_data
;
40 extern int consumer_poll_timeout
;
41 extern volatile int consumer_quit
;
44 * Mmap the ring buffer, read it and write the data to the tracefile.
46 * Returns the number of bytes written, else negative value on error.
48 ssize_t
lttng_ustconsumer_on_read_subbuffer_mmap(
49 struct lttng_consumer_local_data
*ctx
,
50 struct lttng_consumer_stream
*stream
, unsigned long len
)
52 unsigned long mmap_offset
;
53 long ret
= 0, written
= 0;
54 off_t orig_offset
= stream
->out_fd_offset
;
55 int outfd
= stream
->out_fd
;
57 struct consumer_relayd_sock_pair
*relayd
= NULL
;
59 /* Flag that the current stream if set for network streaming. */
60 if (stream
->net_seq_idx
!= -1) {
61 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
67 /* get the offset inside the fd to mmap */
68 ret
= ustctl_get_mmap_read_offset(stream
->chan
->handle
,
69 stream
->buf
, &mmap_offset
);
72 PERROR("ustctl_get_mmap_read_offset");
77 /* Handle stream on the relayd if the output is on the network */
79 if (stream
->metadata_flag
) {
80 /* Only lock if metadata since we use the control socket. */
81 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
84 ret
= consumer_handle_stream_before_relayd(stream
, len
);
88 /* Write metadata stream id before payload */
89 if (stream
->metadata_flag
) {
90 metadata_id
= htobe64(stream
->relayd_stream_id
);
92 ret
= write(outfd
, (void *) &metadata_id
,
93 sizeof(stream
->relayd_stream_id
));
95 PERROR("write metadata stream id");
99 } while (errno
== EINTR
);
100 DBG("Metadata stream id %zu written before data",
101 stream
->relayd_stream_id
);
104 /* Else, use the default set before which is the filesystem. */
108 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
110 if (errno
== EINTR
) {
111 /* restart the interrupted system call */
114 PERROR("Error in file write");
120 } else if (ret
> len
) {
121 PERROR("ret %ld > len %lu", ret
, len
);
128 DBG("UST mmap write() ret %ld (len %lu)", ret
, len
);
130 /* This call is useless on a socket so better save a syscall. */
132 /* This won't block, but will start writeout asynchronously */
133 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
134 SYNC_FILE_RANGE_WRITE
);
135 stream
->out_fd_offset
+= ret
;
139 lttng_consumer_sync_trace_file(stream
, orig_offset
);
142 if (relayd
&& stream
->metadata_flag
) {
143 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
149 * Splice the data from the ring buffer to the tracefile.
151 * Returns the number of bytes spliced.
153 ssize_t
lttng_ustconsumer_on_read_subbuffer_splice(
154 struct lttng_consumer_local_data
*ctx
,
155 struct lttng_consumer_stream
*stream
, unsigned long len
)
161 * Take a snapshot for a specific fd
163 * Returns 0 on success, < 0 on error
165 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
166 struct lttng_consumer_stream
*stream
)
170 ret
= ustctl_snapshot(stream
->chan
->handle
, stream
->buf
);
173 PERROR("Getting sub-buffer snapshot.");
180 * Get the produced position
182 * Returns 0 on success, < 0 on error
184 int lttng_ustconsumer_get_produced_snapshot(
185 struct lttng_consumer_local_data
*ctx
,
186 struct lttng_consumer_stream
*stream
,
191 ret
= ustctl_snapshot_get_produced(stream
->chan
->handle
,
195 PERROR("kernctl_snapshot_get_produced");
201 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
202 int sock
, struct pollfd
*consumer_sockpoll
)
205 struct lttcomm_consumer_msg msg
;
207 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
208 if (ret
!= sizeof(msg
)) {
209 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
212 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
216 switch (msg
.cmd_type
) {
217 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
220 struct consumer_relayd_sock_pair
*relayd
;
222 DBG("UST Consumer adding relayd socket");
224 /* Get relayd reference if exists. */
225 relayd
= consumer_find_relayd(msg
.u
.relayd_sock
.net_index
);
226 if (relayd
== NULL
) {
227 /* Not found. Allocate one. */
228 relayd
= consumer_allocate_relayd_sock_pair(
229 msg
.u
.relayd_sock
.net_index
);
230 if (relayd
== NULL
) {
231 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
236 /* Poll on consumer socket. */
237 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
241 /* Get relayd socket from session daemon */
242 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
243 if (ret
!= sizeof(fd
)) {
244 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
248 /* Copy socket information and received FD */
249 switch (msg
.u
.relayd_sock
.type
) {
250 case LTTNG_STREAM_CONTROL
:
251 /* Copy received lttcomm socket */
252 lttcomm_copy_sock(&relayd
->control_sock
, &msg
.u
.relayd_sock
.sock
);
253 ret
= lttcomm_create_sock(&relayd
->control_sock
);
258 /* Close the created socket fd which is useless */
259 close(relayd
->control_sock
.fd
);
261 /* Assign new file descriptor */
262 relayd
->control_sock
.fd
= fd
;
264 case LTTNG_STREAM_DATA
:
265 /* Copy received lttcomm socket */
266 lttcomm_copy_sock(&relayd
->data_sock
, &msg
.u
.relayd_sock
.sock
);
267 ret
= lttcomm_create_sock(&relayd
->data_sock
);
272 /* Close the created socket fd which is useless */
273 close(relayd
->data_sock
.fd
);
275 /* Assign new file descriptor */
276 relayd
->data_sock
.fd
= fd
;
279 ERR("Unknown relayd socket type");
283 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
284 msg
.u
.relayd_sock
.type
== LTTNG_STREAM_CONTROL
? "control" : "data",
285 relayd
->net_seq_idx
, fd
);
288 * Add relayd socket pair to consumer data hashtable. If object already
289 * exists or on error, the function gracefully returns.
291 consumer_add_relayd(relayd
);
295 case LTTNG_CONSUMER_ADD_CHANNEL
:
297 struct lttng_consumer_channel
*new_channel
;
302 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
305 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
306 if (ret
!= sizeof(fds
)) {
307 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
311 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
313 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
315 msg
.u
.channel
.mmap_len
,
316 msg
.u
.channel
.max_sb_size
);
317 if (new_channel
== NULL
) {
318 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
321 if (ctx
->on_recv_channel
!= NULL
) {
322 ret
= ctx
->on_recv_channel(new_channel
);
324 consumer_add_channel(new_channel
);
325 } else if (ret
< 0) {
329 consumer_add_channel(new_channel
);
333 case LTTNG_CONSUMER_ADD_STREAM
:
335 struct lttng_consumer_stream
*new_stream
;
338 struct consumer_relayd_sock_pair
*relayd
= NULL
;
341 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
344 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
345 if (ret
!= sizeof(fds
)) {
346 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
350 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
351 new_stream
= consumer_allocate_stream(msg
.u
.channel
.channel_key
,
352 msg
.u
.stream
.stream_key
,
355 msg
.u
.stream
.mmap_len
,
357 msg
.u
.stream
.path_name
,
360 msg
.u
.stream
.net_index
,
361 msg
.u
.stream
.metadata_flag
);
362 if (new_stream
== NULL
) {
363 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
367 /* The stream is not metadata. Get relayd reference if exists. */
368 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
369 if (relayd
!= NULL
) {
370 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
371 /* Add stream on the relayd */
372 ret
= relayd_add_stream(&relayd
->control_sock
,
373 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
374 &new_stream
->relayd_stream_id
);
375 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
379 } else if (msg
.u
.stream
.net_index
!= -1) {
380 ERR("Network sequence index %d unknown. Not adding stream.",
381 msg
.u
.stream
.net_index
);
386 if (ctx
->on_recv_stream
!= NULL
) {
387 ret
= ctx
->on_recv_stream(new_stream
);
389 consumer_add_stream(new_stream
);
390 } else if (ret
< 0) {
394 consumer_add_stream(new_stream
);
397 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %lu",
398 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
399 new_stream
->relayd_stream_id
);
402 case LTTNG_CONSUMER_UPDATE_STREAM
:
406 if (ctx
->on_update_stream
!= NULL
) {
407 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
409 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
410 } else if (ret
< 0) {
414 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
425 * Wake-up the other end by writing a null byte in the pipe
426 * (non-blocking). Important note: Because writing into the
427 * pipe is non-blocking (and therefore we allow dropping wakeup
428 * data, as long as there is wakeup data present in the pipe
429 * buffer to wake up the other end), the other end should
430 * perform the following sequence for waiting:
431 * 1) empty the pipe (reads).
432 * 2) perform update operation.
433 * 3) wait on the pipe (poll).
436 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
437 } while (ret
== -1UL && errno
== EINTR
);
442 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
444 struct lttng_ust_object_data obj
;
447 obj
.shm_fd
= chan
->shm_fd
;
448 obj
.wait_fd
= chan
->wait_fd
;
449 obj
.memory_map_size
= chan
->mmap_len
;
450 chan
->handle
= ustctl_map_channel(&obj
);
454 chan
->wait_fd_is_copy
= 1;
460 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
462 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
463 stream
->hangup_flush_done
= 1;
466 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
468 ustctl_unmap_channel(chan
->handle
);
471 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
473 struct lttng_ust_object_data obj
;
477 obj
.shm_fd
= stream
->shm_fd
;
478 obj
.wait_fd
= stream
->wait_fd
;
479 obj
.memory_map_size
= stream
->mmap_len
;
480 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
483 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
486 /* ustctl_open_stream_read has closed the shm fd. */
487 stream
->wait_fd_is_copy
= 1;
490 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
491 if (!stream
->mmap_base
) {
498 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
500 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
504 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
505 struct lttng_consumer_local_data
*ctx
)
510 struct lttng_ust_shm_handle
*handle
;
511 struct lttng_ust_lib_ring_buffer
*buf
;
515 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
516 stream
->wait_fd
, stream
->key
);
518 /* We can consume the 1 byte written into the wait_fd by UST */
519 if (!stream
->hangup_flush_done
) {
521 readlen
= read(stream
->wait_fd
, &dummy
, 1);
522 } while (readlen
== -1 && errno
== EINTR
);
530 handle
= stream
->chan
->handle
;
531 /* Get the next subbuffer */
532 err
= ustctl_get_next_subbuf(handle
, buf
);
534 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
536 * This is a debug message even for single-threaded consumer,
537 * because poll() have more relaxed criterions than get subbuf,
538 * so get_subbuf may fail for short race windows where poll()
539 * would issue wakeups.
541 DBG("Reserving sub buffer failed (everything is normal, "
542 "it is due to concurrency)");
545 assert(stream
->output
== LTTNG_EVENT_MMAP
);
546 /* read the used subbuffer size */
547 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
549 /* write the subbuffer to the tracefile */
550 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
553 * display the error but continue processing to try
554 * to release the subbuffer
556 ERR("Error writing to tracefile");
558 err
= ustctl_put_next_subbuf(handle
, buf
);
564 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
568 /* Opening the tracefile in write mode */
569 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
570 ret
= run_as_open(stream
->path_name
,
571 O_WRONLY
|O_CREAT
|O_TRUNC
,
572 S_IRWXU
|S_IRWXG
|S_IRWXO
,
573 stream
->uid
, stream
->gid
);
575 ERR("Opening %s", stream
->path_name
);
579 stream
->out_fd
= ret
;
582 /* we return 0 to let the library handle the FD internally */