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 /* RCU lock for the relayd pointer */
62 /* Flag that the current stream if set for network streaming. */
63 if (stream
->net_seq_idx
!= -1) {
64 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
66 ERR("UST consumer mmap(), unable to find relay for index %d",
72 /* get the offset inside the fd to mmap */
73 ret
= ustctl_get_mmap_read_offset(stream
->chan
->handle
,
74 stream
->buf
, &mmap_offset
);
77 PERROR("ustctl_get_mmap_read_offset");
82 /* Handle stream on the relayd if the output is on the network */
84 unsigned long netlen
= len
;
86 if (stream
->metadata_flag
) {
87 /* Only lock if metadata since we use the control socket. */
88 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
89 netlen
+= sizeof(stream
->relayd_stream_id
);
92 ret
= consumer_handle_stream_before_relayd(stream
, netlen
);
96 /* Write metadata stream id before payload */
97 if (stream
->metadata_flag
) {
98 metadata_id
= htobe64(stream
->relayd_stream_id
);
100 ret
= write(outfd
, (void *) &metadata_id
,
101 sizeof(stream
->relayd_stream_id
));
102 } while (ret
< 0 && errno
== EINTR
);
104 PERROR("write metadata stream id");
108 DBG("Metadata stream id %zu written before data",
109 stream
->relayd_stream_id
);
112 /* Else, use the default set before which is the filesystem. */
117 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
118 } while (ret
< 0 && errno
== EINTR
);
120 PERROR("Error in file write");
125 } else if (ret
> len
) {
126 PERROR("ret %ld > len %lu", ret
, len
);
133 DBG("UST mmap write() ret %ld (len %lu)", ret
, len
);
135 /* This call is useless on a socket so better save a syscall. */
137 /* This won't block, but will start writeout asynchronously */
138 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
139 SYNC_FILE_RANGE_WRITE
);
140 stream
->out_fd_offset
+= ret
;
144 lttng_consumer_sync_trace_file(stream
, orig_offset
);
147 if (relayd
&& stream
->metadata_flag
) {
148 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
155 * Splice the data from the ring buffer to the tracefile.
157 * Returns the number of bytes spliced.
159 ssize_t
lttng_ustconsumer_on_read_subbuffer_splice(
160 struct lttng_consumer_local_data
*ctx
,
161 struct lttng_consumer_stream
*stream
, unsigned long len
)
167 * Take a snapshot for a specific fd
169 * Returns 0 on success, < 0 on error
171 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
172 struct lttng_consumer_stream
*stream
)
176 ret
= ustctl_snapshot(stream
->chan
->handle
, stream
->buf
);
179 PERROR("Getting sub-buffer snapshot.");
186 * Get the produced position
188 * Returns 0 on success, < 0 on error
190 int lttng_ustconsumer_get_produced_snapshot(
191 struct lttng_consumer_local_data
*ctx
,
192 struct lttng_consumer_stream
*stream
,
197 ret
= ustctl_snapshot_get_produced(stream
->chan
->handle
,
201 PERROR("kernctl_snapshot_get_produced");
207 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
208 int sock
, struct pollfd
*consumer_sockpoll
)
211 struct lttcomm_consumer_msg msg
;
213 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
214 if (ret
!= sizeof(msg
)) {
215 DBG("Consumer received unexpected message size %zd (expects %zu)",
217 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
220 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
224 /* relayd need RCU read-side lock */
227 switch (msg
.cmd_type
) {
228 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
231 struct consumer_relayd_sock_pair
*relayd
;
233 DBG("UST Consumer adding relayd socket");
235 /* Get relayd reference if exists. */
236 relayd
= consumer_find_relayd(msg
.u
.relayd_sock
.net_index
);
237 if (relayd
== NULL
) {
238 /* Not found. Allocate one. */
239 relayd
= consumer_allocate_relayd_sock_pair(
240 msg
.u
.relayd_sock
.net_index
);
241 if (relayd
== NULL
) {
242 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
247 /* Poll on consumer socket. */
248 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
252 /* Get relayd socket from session daemon */
253 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
254 if (ret
!= sizeof(fd
)) {
255 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
259 /* Copy socket information and received FD */
260 switch (msg
.u
.relayd_sock
.type
) {
261 case LTTNG_STREAM_CONTROL
:
262 /* Copy received lttcomm socket */
263 lttcomm_copy_sock(&relayd
->control_sock
, &msg
.u
.relayd_sock
.sock
);
264 ret
= lttcomm_create_sock(&relayd
->control_sock
);
269 /* Close the created socket fd which is useless */
270 close(relayd
->control_sock
.fd
);
272 /* Assign new file descriptor */
273 relayd
->control_sock
.fd
= fd
;
275 case LTTNG_STREAM_DATA
:
276 /* Copy received lttcomm socket */
277 lttcomm_copy_sock(&relayd
->data_sock
, &msg
.u
.relayd_sock
.sock
);
278 ret
= lttcomm_create_sock(&relayd
->data_sock
);
283 /* Close the created socket fd which is useless */
284 close(relayd
->data_sock
.fd
);
286 /* Assign new file descriptor */
287 relayd
->data_sock
.fd
= fd
;
290 ERR("Unknown relayd socket type");
294 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
295 msg
.u
.relayd_sock
.type
== LTTNG_STREAM_CONTROL
? "control" : "data",
296 relayd
->net_seq_idx
, fd
);
299 * Add relayd socket pair to consumer data hashtable. If object already
300 * exists or on error, the function gracefully returns.
302 consumer_add_relayd(relayd
);
306 case LTTNG_CONSUMER_ADD_CHANNEL
:
308 struct lttng_consumer_channel
*new_channel
;
312 DBG("UST Consumer adding channel");
315 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
318 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
319 if (ret
!= sizeof(fds
)) {
320 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
324 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
326 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
328 msg
.u
.channel
.mmap_len
,
329 msg
.u
.channel
.max_sb_size
);
330 if (new_channel
== NULL
) {
331 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
334 if (ctx
->on_recv_channel
!= NULL
) {
335 ret
= ctx
->on_recv_channel(new_channel
);
337 consumer_add_channel(new_channel
);
338 } else if (ret
< 0) {
342 consumer_add_channel(new_channel
);
346 case LTTNG_CONSUMER_ADD_STREAM
:
348 struct lttng_consumer_stream
*new_stream
;
351 struct consumer_relayd_sock_pair
*relayd
= NULL
;
353 DBG("UST Consumer adding stream");
356 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
359 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
360 if (ret
!= sizeof(fds
)) {
361 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
365 DBG("consumer_add_stream chan %d stream %d",
366 msg
.u
.stream
.channel_key
,
367 msg
.u
.stream
.stream_key
);
369 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
370 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
371 msg
.u
.stream
.stream_key
,
374 msg
.u
.stream
.mmap_len
,
376 msg
.u
.stream
.path_name
,
379 msg
.u
.stream
.net_index
,
380 msg
.u
.stream
.metadata_flag
);
381 if (new_stream
== NULL
) {
382 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
386 /* The stream is not metadata. Get relayd reference if exists. */
387 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
388 if (relayd
!= NULL
) {
389 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
390 /* Add stream on the relayd */
391 ret
= relayd_add_stream(&relayd
->control_sock
,
392 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
393 &new_stream
->relayd_stream_id
);
394 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
398 } else if (msg
.u
.stream
.net_index
!= -1) {
399 ERR("Network sequence index %d unknown. Not adding stream.",
400 msg
.u
.stream
.net_index
);
405 if (ctx
->on_recv_stream
!= NULL
) {
406 ret
= ctx
->on_recv_stream(new_stream
);
408 consumer_add_stream(new_stream
);
409 } else if (ret
< 0) {
413 consumer_add_stream(new_stream
);
416 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %lu",
417 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
418 new_stream
->relayd_stream_id
);
421 case LTTNG_CONSUMER_DESTROY_RELAYD
:
423 struct consumer_relayd_sock_pair
*relayd
;
425 DBG("UST consumer destroying relayd %zu",
426 msg
.u
.destroy_relayd
.net_seq_idx
);
428 /* Get relayd reference if exists. */
429 relayd
= consumer_find_relayd(msg
.u
.destroy_relayd
.net_seq_idx
);
430 if (relayd
== NULL
) {
431 ERR("Unable to find relayd %zu",
432 msg
.u
.destroy_relayd
.net_seq_idx
);
435 /* Set destroy flag for this object */
436 uatomic_set(&relayd
->destroy_flag
, 1);
438 /* Destroy the relayd if refcount is 0 else set the destroy flag. */
439 if (uatomic_read(&relayd
->refcount
) == 0) {
440 consumer_destroy_relayd(relayd
);
444 case LTTNG_CONSUMER_UPDATE_STREAM
:
448 if (ctx
->on_update_stream
!= NULL
) {
449 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
451 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
452 } else if (ret
< 0) {
456 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
467 * Wake-up the other end by writing a null byte in the pipe
468 * (non-blocking). Important note: Because writing into the
469 * pipe is non-blocking (and therefore we allow dropping wakeup
470 * data, as long as there is wakeup data present in the pipe
471 * buffer to wake up the other end), the other end should
472 * perform the following sequence for waiting:
473 * 1) empty the pipe (reads).
474 * 2) perform update operation.
475 * 3) wait on the pipe (poll).
478 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
479 } while (ret
< 0 && errno
== EINTR
);
485 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
487 struct lttng_ust_object_data obj
;
490 obj
.shm_fd
= chan
->shm_fd
;
491 obj
.wait_fd
= chan
->wait_fd
;
492 obj
.memory_map_size
= chan
->mmap_len
;
493 chan
->handle
= ustctl_map_channel(&obj
);
497 chan
->wait_fd_is_copy
= 1;
503 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
505 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
506 stream
->hangup_flush_done
= 1;
509 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
511 ustctl_unmap_channel(chan
->handle
);
514 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
516 struct lttng_ust_object_data obj
;
520 obj
.shm_fd
= stream
->shm_fd
;
521 obj
.wait_fd
= stream
->wait_fd
;
522 obj
.memory_map_size
= stream
->mmap_len
;
523 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
526 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
529 /* ustctl_open_stream_read has closed the shm fd. */
530 stream
->wait_fd_is_copy
= 1;
533 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
534 if (!stream
->mmap_base
) {
541 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
543 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
547 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
548 struct lttng_consumer_local_data
*ctx
)
553 struct lttng_ust_shm_handle
*handle
;
554 struct lttng_ust_lib_ring_buffer
*buf
;
558 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
559 stream
->wait_fd
, stream
->key
);
561 /* We can consume the 1 byte written into the wait_fd by UST */
562 if (!stream
->hangup_flush_done
) {
564 readlen
= read(stream
->wait_fd
, &dummy
, 1);
565 } while (readlen
== -1 && errno
== EINTR
);
573 handle
= stream
->chan
->handle
;
574 /* Get the next subbuffer */
575 err
= ustctl_get_next_subbuf(handle
, buf
);
577 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
579 * This is a debug message even for single-threaded consumer,
580 * because poll() have more relaxed criterions than get subbuf,
581 * so get_subbuf may fail for short race windows where poll()
582 * would issue wakeups.
584 DBG("Reserving sub buffer failed (everything is normal, "
585 "it is due to concurrency)");
588 assert(stream
->output
== LTTNG_EVENT_MMAP
);
589 /* read the used subbuffer size */
590 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
592 /* write the subbuffer to the tracefile */
593 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
596 * display the error but continue processing to try
597 * to release the subbuffer
599 ERR("Error writing to tracefile");
601 err
= ustctl_put_next_subbuf(handle
, buf
);
607 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
611 /* Opening the tracefile in write mode */
612 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
613 ret
= run_as_open(stream
->path_name
,
614 O_WRONLY
|O_CREAT
|O_TRUNC
,
615 S_IRWXU
|S_IRWXG
|S_IRWXO
,
616 stream
->uid
, stream
->gid
);
618 ERR("Opening %s", stream
->path_name
);
622 stream
->out_fd
= ret
;
625 /* we return 0 to let the library handle the FD internally */