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 if (stream
->metadata_flag
) {
85 /* Only lock if metadata since we use the control socket. */
86 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
89 ret
= consumer_handle_stream_before_relayd(stream
, len
);
93 /* Write metadata stream id before payload */
94 if (stream
->metadata_flag
) {
95 metadata_id
= htobe64(stream
->relayd_stream_id
);
97 ret
= write(outfd
, (void *) &metadata_id
,
98 sizeof(stream
->relayd_stream_id
));
99 } while (ret
< 0 && errno
== EINTR
);
101 PERROR("write metadata stream id");
105 DBG("Metadata stream id %zu written before data",
106 stream
->relayd_stream_id
);
109 /* Else, use the default set before which is the filesystem. */
114 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
115 } while (ret
< 0 && errno
== EINTR
);
117 PERROR("Error in file write");
122 } else if (ret
> len
) {
123 PERROR("ret %ld > len %lu", ret
, len
);
130 DBG("UST mmap write() ret %ld (len %lu)", ret
, len
);
132 /* This call is useless on a socket so better save a syscall. */
134 /* This won't block, but will start writeout asynchronously */
135 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
136 SYNC_FILE_RANGE_WRITE
);
137 stream
->out_fd_offset
+= ret
;
141 lttng_consumer_sync_trace_file(stream
, orig_offset
);
144 if (relayd
&& stream
->metadata_flag
) {
145 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
152 * Splice the data from the ring buffer to the tracefile.
154 * Returns the number of bytes spliced.
156 ssize_t
lttng_ustconsumer_on_read_subbuffer_splice(
157 struct lttng_consumer_local_data
*ctx
,
158 struct lttng_consumer_stream
*stream
, unsigned long len
)
164 * Take a snapshot for a specific fd
166 * Returns 0 on success, < 0 on error
168 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
169 struct lttng_consumer_stream
*stream
)
173 ret
= ustctl_snapshot(stream
->chan
->handle
, stream
->buf
);
176 PERROR("Getting sub-buffer snapshot.");
183 * Get the produced position
185 * Returns 0 on success, < 0 on error
187 int lttng_ustconsumer_get_produced_snapshot(
188 struct lttng_consumer_local_data
*ctx
,
189 struct lttng_consumer_stream
*stream
,
194 ret
= ustctl_snapshot_get_produced(stream
->chan
->handle
,
198 PERROR("kernctl_snapshot_get_produced");
204 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
205 int sock
, struct pollfd
*consumer_sockpoll
)
208 struct lttcomm_consumer_msg msg
;
210 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
211 if (ret
!= sizeof(msg
)) {
212 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
215 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
219 /* relayd need RCU read-side lock */
222 switch (msg
.cmd_type
) {
223 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
226 struct consumer_relayd_sock_pair
*relayd
;
228 DBG("UST Consumer adding relayd socket");
230 /* Get relayd reference if exists. */
231 relayd
= consumer_find_relayd(msg
.u
.relayd_sock
.net_index
);
232 if (relayd
== NULL
) {
233 /* Not found. Allocate one. */
234 relayd
= consumer_allocate_relayd_sock_pair(
235 msg
.u
.relayd_sock
.net_index
);
236 if (relayd
== NULL
) {
237 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
242 /* Poll on consumer socket. */
243 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
247 /* Get relayd socket from session daemon */
248 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
249 if (ret
!= sizeof(fd
)) {
250 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
254 /* Copy socket information and received FD */
255 switch (msg
.u
.relayd_sock
.type
) {
256 case LTTNG_STREAM_CONTROL
:
257 /* Copy received lttcomm socket */
258 lttcomm_copy_sock(&relayd
->control_sock
, &msg
.u
.relayd_sock
.sock
);
259 ret
= lttcomm_create_sock(&relayd
->control_sock
);
264 /* Close the created socket fd which is useless */
265 close(relayd
->control_sock
.fd
);
267 /* Assign new file descriptor */
268 relayd
->control_sock
.fd
= fd
;
270 case LTTNG_STREAM_DATA
:
271 /* Copy received lttcomm socket */
272 lttcomm_copy_sock(&relayd
->data_sock
, &msg
.u
.relayd_sock
.sock
);
273 ret
= lttcomm_create_sock(&relayd
->data_sock
);
278 /* Close the created socket fd which is useless */
279 close(relayd
->data_sock
.fd
);
281 /* Assign new file descriptor */
282 relayd
->data_sock
.fd
= fd
;
285 ERR("Unknown relayd socket type");
289 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
290 msg
.u
.relayd_sock
.type
== LTTNG_STREAM_CONTROL
? "control" : "data",
291 relayd
->net_seq_idx
, fd
);
294 * Add relayd socket pair to consumer data hashtable. If object already
295 * exists or on error, the function gracefully returns.
297 consumer_add_relayd(relayd
);
301 case LTTNG_CONSUMER_ADD_CHANNEL
:
303 struct lttng_consumer_channel
*new_channel
;
308 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
311 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
312 if (ret
!= sizeof(fds
)) {
313 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
317 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
319 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
321 msg
.u
.channel
.mmap_len
,
322 msg
.u
.channel
.max_sb_size
);
323 if (new_channel
== NULL
) {
324 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
327 if (ctx
->on_recv_channel
!= NULL
) {
328 ret
= ctx
->on_recv_channel(new_channel
);
330 consumer_add_channel(new_channel
);
331 } else if (ret
< 0) {
335 consumer_add_channel(new_channel
);
339 case LTTNG_CONSUMER_ADD_STREAM
:
341 struct lttng_consumer_stream
*new_stream
;
344 struct consumer_relayd_sock_pair
*relayd
= NULL
;
347 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
350 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
351 if (ret
!= sizeof(fds
)) {
352 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
356 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
357 new_stream
= consumer_allocate_stream(msg
.u
.channel
.channel_key
,
358 msg
.u
.stream
.stream_key
,
361 msg
.u
.stream
.mmap_len
,
363 msg
.u
.stream
.path_name
,
366 msg
.u
.stream
.net_index
,
367 msg
.u
.stream
.metadata_flag
);
368 if (new_stream
== NULL
) {
369 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
373 /* The stream is not metadata. Get relayd reference if exists. */
374 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
375 if (relayd
!= NULL
) {
376 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
377 /* Add stream on the relayd */
378 ret
= relayd_add_stream(&relayd
->control_sock
,
379 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
380 &new_stream
->relayd_stream_id
);
381 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
385 } else if (msg
.u
.stream
.net_index
!= -1) {
386 ERR("Network sequence index %d unknown. Not adding stream.",
387 msg
.u
.stream
.net_index
);
392 if (ctx
->on_recv_stream
!= NULL
) {
393 ret
= ctx
->on_recv_stream(new_stream
);
395 consumer_add_stream(new_stream
);
396 } else if (ret
< 0) {
400 consumer_add_stream(new_stream
);
403 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %lu",
404 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
405 new_stream
->relayd_stream_id
);
408 case LTTNG_CONSUMER_UPDATE_STREAM
:
412 if (ctx
->on_update_stream
!= NULL
) {
413 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
415 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
416 } else if (ret
< 0) {
420 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
431 * Wake-up the other end by writing a null byte in the pipe
432 * (non-blocking). Important note: Because writing into the
433 * pipe is non-blocking (and therefore we allow dropping wakeup
434 * data, as long as there is wakeup data present in the pipe
435 * buffer to wake up the other end), the other end should
436 * perform the following sequence for waiting:
437 * 1) empty the pipe (reads).
438 * 2) perform update operation.
439 * 3) wait on the pipe (poll).
442 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
443 } while (ret
< 0 && errno
== EINTR
);
449 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
451 struct lttng_ust_object_data obj
;
454 obj
.shm_fd
= chan
->shm_fd
;
455 obj
.wait_fd
= chan
->wait_fd
;
456 obj
.memory_map_size
= chan
->mmap_len
;
457 chan
->handle
= ustctl_map_channel(&obj
);
461 chan
->wait_fd_is_copy
= 1;
467 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
469 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
470 stream
->hangup_flush_done
= 1;
473 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
475 ustctl_unmap_channel(chan
->handle
);
478 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
480 struct lttng_ust_object_data obj
;
484 obj
.shm_fd
= stream
->shm_fd
;
485 obj
.wait_fd
= stream
->wait_fd
;
486 obj
.memory_map_size
= stream
->mmap_len
;
487 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
490 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
493 /* ustctl_open_stream_read has closed the shm fd. */
494 stream
->wait_fd_is_copy
= 1;
497 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
498 if (!stream
->mmap_base
) {
505 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
507 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
511 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
512 struct lttng_consumer_local_data
*ctx
)
517 struct lttng_ust_shm_handle
*handle
;
518 struct lttng_ust_lib_ring_buffer
*buf
;
522 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
523 stream
->wait_fd
, stream
->key
);
525 /* We can consume the 1 byte written into the wait_fd by UST */
526 if (!stream
->hangup_flush_done
) {
528 readlen
= read(stream
->wait_fd
, &dummy
, 1);
529 } while (readlen
== -1 && errno
== EINTR
);
537 handle
= stream
->chan
->handle
;
538 /* Get the next subbuffer */
539 err
= ustctl_get_next_subbuf(handle
, buf
);
541 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
543 * This is a debug message even for single-threaded consumer,
544 * because poll() have more relaxed criterions than get subbuf,
545 * so get_subbuf may fail for short race windows where poll()
546 * would issue wakeups.
548 DBG("Reserving sub buffer failed (everything is normal, "
549 "it is due to concurrency)");
552 assert(stream
->output
== LTTNG_EVENT_MMAP
);
553 /* read the used subbuffer size */
554 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
556 /* write the subbuffer to the tracefile */
557 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
560 * display the error but continue processing to try
561 * to release the subbuffer
563 ERR("Error writing to tracefile");
565 err
= ustctl_put_next_subbuf(handle
, buf
);
571 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
575 /* Opening the tracefile in write mode */
576 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
577 ret
= run_as_open(stream
->path_name
,
578 O_WRONLY
|O_CREAT
|O_TRUNC
,
579 S_IRWXU
|S_IRWXG
|S_IRWXO
,
580 stream
->uid
, stream
->gid
);
582 ERR("Opening %s", stream
->path_name
);
586 stream
->out_fd
= ret
;
589 /* we return 0 to let the library handle the FD internally */