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("Cannot find relay for network stream\n");
71 /* get the offset inside the fd to mmap */
72 ret
= ustctl_get_mmap_read_offset(stream
->chan
->handle
,
73 stream
->buf
, &mmap_offset
);
76 PERROR("ustctl_get_mmap_read_offset");
81 /* Handle stream on the relayd if the output is on the network */
83 if (stream
->metadata_flag
) {
84 /* Only lock if metadata since we use the control socket. */
85 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
88 ret
= consumer_handle_stream_before_relayd(stream
, len
);
92 /* Write metadata stream id before payload */
93 if (stream
->metadata_flag
) {
94 metadata_id
= htobe64(stream
->relayd_stream_id
);
96 ret
= write(outfd
, (void *) &metadata_id
,
97 sizeof(stream
->relayd_stream_id
));
98 } while (ret
< 0 && errno
== EINTR
);
100 PERROR("write metadata stream id");
104 DBG("Metadata stream id %zu written before data",
105 stream
->relayd_stream_id
);
108 /* Else, use the default set before which is the filesystem. */
113 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
114 } while (ret
< 0 && errno
== EINTR
);
116 PERROR("Error in file write");
121 } else if (ret
> len
) {
122 PERROR("ret %ld > len %lu", ret
, len
);
129 DBG("UST mmap write() ret %ld (len %lu)", ret
, len
);
131 /* This call is useless on a socket so better save a syscall. */
133 /* This won't block, but will start writeout asynchronously */
134 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
135 SYNC_FILE_RANGE_WRITE
);
136 stream
->out_fd_offset
+= ret
;
140 lttng_consumer_sync_trace_file(stream
, orig_offset
);
143 if (relayd
&& stream
->metadata_flag
) {
144 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
151 * Splice the data from the ring buffer to the tracefile.
153 * Returns the number of bytes spliced.
155 ssize_t
lttng_ustconsumer_on_read_subbuffer_splice(
156 struct lttng_consumer_local_data
*ctx
,
157 struct lttng_consumer_stream
*stream
, unsigned long len
)
163 * Take a snapshot for a specific fd
165 * Returns 0 on success, < 0 on error
167 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
168 struct lttng_consumer_stream
*stream
)
172 ret
= ustctl_snapshot(stream
->chan
->handle
, stream
->buf
);
175 PERROR("Getting sub-buffer snapshot.");
182 * Get the produced position
184 * Returns 0 on success, < 0 on error
186 int lttng_ustconsumer_get_produced_snapshot(
187 struct lttng_consumer_local_data
*ctx
,
188 struct lttng_consumer_stream
*stream
,
193 ret
= ustctl_snapshot_get_produced(stream
->chan
->handle
,
197 PERROR("kernctl_snapshot_get_produced");
203 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
204 int sock
, struct pollfd
*consumer_sockpoll
)
207 struct lttcomm_consumer_msg msg
;
209 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
210 if (ret
!= sizeof(msg
)) {
211 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
214 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
218 /* relayd need RCU read-side lock */
221 switch (msg
.cmd_type
) {
222 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
225 struct consumer_relayd_sock_pair
*relayd
;
227 DBG("UST Consumer adding relayd socket");
229 /* Get relayd reference if exists. */
230 relayd
= consumer_find_relayd(msg
.u
.relayd_sock
.net_index
);
231 if (relayd
== NULL
) {
232 /* Not found. Allocate one. */
233 relayd
= consumer_allocate_relayd_sock_pair(
234 msg
.u
.relayd_sock
.net_index
);
235 if (relayd
== NULL
) {
236 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
241 /* Poll on consumer socket. */
242 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
246 /* Get relayd socket from session daemon */
247 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
248 if (ret
!= sizeof(fd
)) {
249 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
253 /* Copy socket information and received FD */
254 switch (msg
.u
.relayd_sock
.type
) {
255 case LTTNG_STREAM_CONTROL
:
256 /* Copy received lttcomm socket */
257 lttcomm_copy_sock(&relayd
->control_sock
, &msg
.u
.relayd_sock
.sock
);
258 ret
= lttcomm_create_sock(&relayd
->control_sock
);
263 /* Close the created socket fd which is useless */
264 close(relayd
->control_sock
.fd
);
266 /* Assign new file descriptor */
267 relayd
->control_sock
.fd
= fd
;
269 case LTTNG_STREAM_DATA
:
270 /* Copy received lttcomm socket */
271 lttcomm_copy_sock(&relayd
->data_sock
, &msg
.u
.relayd_sock
.sock
);
272 ret
= lttcomm_create_sock(&relayd
->data_sock
);
277 /* Close the created socket fd which is useless */
278 close(relayd
->data_sock
.fd
);
280 /* Assign new file descriptor */
281 relayd
->data_sock
.fd
= fd
;
284 ERR("Unknown relayd socket type");
288 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
289 msg
.u
.relayd_sock
.type
== LTTNG_STREAM_CONTROL
? "control" : "data",
290 relayd
->net_seq_idx
, fd
);
293 * Add relayd socket pair to consumer data hashtable. If object already
294 * exists or on error, the function gracefully returns.
296 consumer_add_relayd(relayd
);
300 case LTTNG_CONSUMER_ADD_CHANNEL
:
302 struct lttng_consumer_channel
*new_channel
;
307 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
310 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
311 if (ret
!= sizeof(fds
)) {
312 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
316 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
318 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
320 msg
.u
.channel
.mmap_len
,
321 msg
.u
.channel
.max_sb_size
);
322 if (new_channel
== NULL
) {
323 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
326 if (ctx
->on_recv_channel
!= NULL
) {
327 ret
= ctx
->on_recv_channel(new_channel
);
329 consumer_add_channel(new_channel
);
330 } else if (ret
< 0) {
334 consumer_add_channel(new_channel
);
338 case LTTNG_CONSUMER_ADD_STREAM
:
340 struct lttng_consumer_stream
*new_stream
;
343 struct consumer_relayd_sock_pair
*relayd
= NULL
;
346 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
349 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
350 if (ret
!= sizeof(fds
)) {
351 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
355 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
356 new_stream
= consumer_allocate_stream(msg
.u
.channel
.channel_key
,
357 msg
.u
.stream
.stream_key
,
360 msg
.u
.stream
.mmap_len
,
362 msg
.u
.stream
.path_name
,
365 msg
.u
.stream
.net_index
,
366 msg
.u
.stream
.metadata_flag
);
367 if (new_stream
== NULL
) {
368 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
372 /* The stream is not metadata. Get relayd reference if exists. */
373 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
374 if (relayd
!= NULL
) {
375 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
376 /* Add stream on the relayd */
377 ret
= relayd_add_stream(&relayd
->control_sock
,
378 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
379 &new_stream
->relayd_stream_id
);
380 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
384 } else if (msg
.u
.stream
.net_index
!= -1) {
385 ERR("Network sequence index %d unknown. Not adding stream.",
386 msg
.u
.stream
.net_index
);
391 if (ctx
->on_recv_stream
!= NULL
) {
392 ret
= ctx
->on_recv_stream(new_stream
);
394 consumer_add_stream(new_stream
);
395 } else if (ret
< 0) {
399 consumer_add_stream(new_stream
);
402 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %lu",
403 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
404 new_stream
->relayd_stream_id
);
407 case LTTNG_CONSUMER_UPDATE_STREAM
:
411 if (ctx
->on_update_stream
!= NULL
) {
412 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
414 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
415 } else if (ret
< 0) {
419 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
430 * Wake-up the other end by writing a null byte in the pipe
431 * (non-blocking). Important note: Because writing into the
432 * pipe is non-blocking (and therefore we allow dropping wakeup
433 * data, as long as there is wakeup data present in the pipe
434 * buffer to wake up the other end), the other end should
435 * perform the following sequence for waiting:
436 * 1) empty the pipe (reads).
437 * 2) perform update operation.
438 * 3) wait on the pipe (poll).
441 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
442 } while (ret
< 0 && errno
== EINTR
);
448 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
450 struct lttng_ust_object_data obj
;
453 obj
.shm_fd
= chan
->shm_fd
;
454 obj
.wait_fd
= chan
->wait_fd
;
455 obj
.memory_map_size
= chan
->mmap_len
;
456 chan
->handle
= ustctl_map_channel(&obj
);
460 chan
->wait_fd_is_copy
= 1;
466 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
468 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
469 stream
->hangup_flush_done
= 1;
472 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
474 ustctl_unmap_channel(chan
->handle
);
477 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
479 struct lttng_ust_object_data obj
;
483 obj
.shm_fd
= stream
->shm_fd
;
484 obj
.wait_fd
= stream
->wait_fd
;
485 obj
.memory_map_size
= stream
->mmap_len
;
486 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
489 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
492 /* ustctl_open_stream_read has closed the shm fd. */
493 stream
->wait_fd_is_copy
= 1;
496 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
497 if (!stream
->mmap_base
) {
504 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
506 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
510 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
511 struct lttng_consumer_local_data
*ctx
)
516 struct lttng_ust_shm_handle
*handle
;
517 struct lttng_ust_lib_ring_buffer
*buf
;
521 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
522 stream
->wait_fd
, stream
->key
);
524 /* We can consume the 1 byte written into the wait_fd by UST */
525 if (!stream
->hangup_flush_done
) {
527 readlen
= read(stream
->wait_fd
, &dummy
, 1);
528 } while (readlen
== -1 && errno
== EINTR
);
536 handle
= stream
->chan
->handle
;
537 /* Get the next subbuffer */
538 err
= ustctl_get_next_subbuf(handle
, buf
);
540 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
542 * This is a debug message even for single-threaded consumer,
543 * because poll() have more relaxed criterions than get subbuf,
544 * so get_subbuf may fail for short race windows where poll()
545 * would issue wakeups.
547 DBG("Reserving sub buffer failed (everything is normal, "
548 "it is due to concurrency)");
551 assert(stream
->output
== LTTNG_EVENT_MMAP
);
552 /* read the used subbuffer size */
553 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
555 /* write the subbuffer to the tracefile */
556 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
559 * display the error but continue processing to try
560 * to release the subbuffer
562 ERR("Error writing to tracefile");
564 err
= ustctl_put_next_subbuf(handle
, buf
);
570 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
574 /* Opening the tracefile in write mode */
575 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
576 ret
= run_as_open(stream
->path_name
,
577 O_WRONLY
|O_CREAT
|O_TRUNC
,
578 S_IRWXU
|S_IRWXG
|S_IRWXO
,
579 stream
->uid
, stream
->gid
);
581 ERR("Opening %s", stream
->path_name
);
585 stream
->out_fd
= ret
;
588 /* we return 0 to let the library handle the FD internally */