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 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
218 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
222 /* relayd need RCU read-side lock */
225 switch (msg
.cmd_type
) {
226 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
229 struct consumer_relayd_sock_pair
*relayd
;
231 DBG("UST Consumer adding relayd socket");
233 /* Get relayd reference if exists. */
234 relayd
= consumer_find_relayd(msg
.u
.relayd_sock
.net_index
);
235 if (relayd
== NULL
) {
236 /* Not found. Allocate one. */
237 relayd
= consumer_allocate_relayd_sock_pair(
238 msg
.u
.relayd_sock
.net_index
);
239 if (relayd
== NULL
) {
240 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
245 /* Poll on consumer socket. */
246 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
250 /* Get relayd socket from session daemon */
251 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
252 if (ret
!= sizeof(fd
)) {
253 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
257 /* Copy socket information and received FD */
258 switch (msg
.u
.relayd_sock
.type
) {
259 case LTTNG_STREAM_CONTROL
:
260 /* Copy received lttcomm socket */
261 lttcomm_copy_sock(&relayd
->control_sock
, &msg
.u
.relayd_sock
.sock
);
262 ret
= lttcomm_create_sock(&relayd
->control_sock
);
267 /* Close the created socket fd which is useless */
268 close(relayd
->control_sock
.fd
);
270 /* Assign new file descriptor */
271 relayd
->control_sock
.fd
= fd
;
273 case LTTNG_STREAM_DATA
:
274 /* Copy received lttcomm socket */
275 lttcomm_copy_sock(&relayd
->data_sock
, &msg
.u
.relayd_sock
.sock
);
276 ret
= lttcomm_create_sock(&relayd
->data_sock
);
281 /* Close the created socket fd which is useless */
282 close(relayd
->data_sock
.fd
);
284 /* Assign new file descriptor */
285 relayd
->data_sock
.fd
= fd
;
288 ERR("Unknown relayd socket type");
292 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
293 msg
.u
.relayd_sock
.type
== LTTNG_STREAM_CONTROL
? "control" : "data",
294 relayd
->net_seq_idx
, fd
);
297 * Add relayd socket pair to consumer data hashtable. If object already
298 * exists or on error, the function gracefully returns.
300 consumer_add_relayd(relayd
);
304 case LTTNG_CONSUMER_ADD_CHANNEL
:
306 struct lttng_consumer_channel
*new_channel
;
311 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
314 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
315 if (ret
!= sizeof(fds
)) {
316 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
320 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
322 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
324 msg
.u
.channel
.mmap_len
,
325 msg
.u
.channel
.max_sb_size
);
326 if (new_channel
== NULL
) {
327 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
330 if (ctx
->on_recv_channel
!= NULL
) {
331 ret
= ctx
->on_recv_channel(new_channel
);
333 consumer_add_channel(new_channel
);
334 } else if (ret
< 0) {
338 consumer_add_channel(new_channel
);
342 case LTTNG_CONSUMER_ADD_STREAM
:
344 struct lttng_consumer_stream
*new_stream
;
347 struct consumer_relayd_sock_pair
*relayd
= NULL
;
350 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
353 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
354 if (ret
!= sizeof(fds
)) {
355 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
359 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
360 new_stream
= consumer_allocate_stream(msg
.u
.channel
.channel_key
,
361 msg
.u
.stream
.stream_key
,
364 msg
.u
.stream
.mmap_len
,
366 msg
.u
.stream
.path_name
,
369 msg
.u
.stream
.net_index
,
370 msg
.u
.stream
.metadata_flag
);
371 if (new_stream
== NULL
) {
372 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
376 /* The stream is not metadata. Get relayd reference if exists. */
377 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
378 if (relayd
!= NULL
) {
379 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
380 /* Add stream on the relayd */
381 ret
= relayd_add_stream(&relayd
->control_sock
,
382 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
383 &new_stream
->relayd_stream_id
);
384 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
388 } else if (msg
.u
.stream
.net_index
!= -1) {
389 ERR("Network sequence index %d unknown. Not adding stream.",
390 msg
.u
.stream
.net_index
);
395 if (ctx
->on_recv_stream
!= NULL
) {
396 ret
= ctx
->on_recv_stream(new_stream
);
398 consumer_add_stream(new_stream
);
399 } else if (ret
< 0) {
403 consumer_add_stream(new_stream
);
406 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %lu",
407 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
408 new_stream
->relayd_stream_id
);
411 case LTTNG_CONSUMER_UPDATE_STREAM
:
415 if (ctx
->on_update_stream
!= NULL
) {
416 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
418 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
419 } else if (ret
< 0) {
423 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
434 * Wake-up the other end by writing a null byte in the pipe
435 * (non-blocking). Important note: Because writing into the
436 * pipe is non-blocking (and therefore we allow dropping wakeup
437 * data, as long as there is wakeup data present in the pipe
438 * buffer to wake up the other end), the other end should
439 * perform the following sequence for waiting:
440 * 1) empty the pipe (reads).
441 * 2) perform update operation.
442 * 3) wait on the pipe (poll).
445 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
446 } while (ret
< 0 && errno
== EINTR
);
452 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
454 struct lttng_ust_object_data obj
;
457 obj
.shm_fd
= chan
->shm_fd
;
458 obj
.wait_fd
= chan
->wait_fd
;
459 obj
.memory_map_size
= chan
->mmap_len
;
460 chan
->handle
= ustctl_map_channel(&obj
);
464 chan
->wait_fd_is_copy
= 1;
470 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
472 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
473 stream
->hangup_flush_done
= 1;
476 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
478 ustctl_unmap_channel(chan
->handle
);
481 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
483 struct lttng_ust_object_data obj
;
487 obj
.shm_fd
= stream
->shm_fd
;
488 obj
.wait_fd
= stream
->wait_fd
;
489 obj
.memory_map_size
= stream
->mmap_len
;
490 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
493 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
496 /* ustctl_open_stream_read has closed the shm fd. */
497 stream
->wait_fd_is_copy
= 1;
500 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
501 if (!stream
->mmap_base
) {
508 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
510 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
514 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
515 struct lttng_consumer_local_data
*ctx
)
520 struct lttng_ust_shm_handle
*handle
;
521 struct lttng_ust_lib_ring_buffer
*buf
;
525 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
526 stream
->wait_fd
, stream
->key
);
528 /* We can consume the 1 byte written into the wait_fd by UST */
529 if (!stream
->hangup_flush_done
) {
531 readlen
= read(stream
->wait_fd
, &dummy
, 1);
532 } while (readlen
== -1 && errno
== EINTR
);
540 handle
= stream
->chan
->handle
;
541 /* Get the next subbuffer */
542 err
= ustctl_get_next_subbuf(handle
, buf
);
544 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
546 * This is a debug message even for single-threaded consumer,
547 * because poll() have more relaxed criterions than get subbuf,
548 * so get_subbuf may fail for short race windows where poll()
549 * would issue wakeups.
551 DBG("Reserving sub buffer failed (everything is normal, "
552 "it is due to concurrency)");
555 assert(stream
->output
== LTTNG_EVENT_MMAP
);
556 /* read the used subbuffer size */
557 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
559 /* write the subbuffer to the tracefile */
560 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
563 * display the error but continue processing to try
564 * to release the subbuffer
566 ERR("Error writing to tracefile");
568 err
= ustctl_put_next_subbuf(handle
, buf
);
574 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
578 /* Opening the tracefile in write mode */
579 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
580 ret
= run_as_open(stream
->path_name
,
581 O_WRONLY
|O_CREAT
|O_TRUNC
,
582 S_IRWXU
|S_IRWXG
|S_IRWXO
,
583 stream
->uid
, stream
->gid
);
585 ERR("Opening %s", stream
->path_name
);
589 stream
->out_fd
= ret
;
592 /* we return 0 to let the library handle the FD internally */