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
));
94 } while (ret
< 0 && errno
== EINTR
);
96 PERROR("write metadata stream id");
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. */
109 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
110 } while (ret
< 0 && errno
== EINTR
);
112 PERROR("Error in file write");
117 } else if (ret
> len
) {
118 PERROR("ret %ld > len %lu", ret
, len
);
125 DBG("UST mmap write() ret %ld (len %lu)", ret
, len
);
127 /* This call is useless on a socket so better save a syscall. */
129 /* This won't block, but will start writeout asynchronously */
130 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
131 SYNC_FILE_RANGE_WRITE
);
132 stream
->out_fd_offset
+= ret
;
136 lttng_consumer_sync_trace_file(stream
, orig_offset
);
139 if (relayd
&& stream
->metadata_flag
) {
140 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
146 * Splice the data from the ring buffer to the tracefile.
148 * Returns the number of bytes spliced.
150 ssize_t
lttng_ustconsumer_on_read_subbuffer_splice(
151 struct lttng_consumer_local_data
*ctx
,
152 struct lttng_consumer_stream
*stream
, unsigned long len
)
158 * Take a snapshot for a specific fd
160 * Returns 0 on success, < 0 on error
162 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
163 struct lttng_consumer_stream
*stream
)
167 ret
= ustctl_snapshot(stream
->chan
->handle
, stream
->buf
);
170 PERROR("Getting sub-buffer snapshot.");
177 * Get the produced position
179 * Returns 0 on success, < 0 on error
181 int lttng_ustconsumer_get_produced_snapshot(
182 struct lttng_consumer_local_data
*ctx
,
183 struct lttng_consumer_stream
*stream
,
188 ret
= ustctl_snapshot_get_produced(stream
->chan
->handle
,
192 PERROR("kernctl_snapshot_get_produced");
198 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
199 int sock
, struct pollfd
*consumer_sockpoll
)
202 struct lttcomm_consumer_msg msg
;
204 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
205 if (ret
!= sizeof(msg
)) {
206 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
209 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
213 switch (msg
.cmd_type
) {
214 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
217 struct consumer_relayd_sock_pair
*relayd
;
219 DBG("UST Consumer adding relayd socket");
221 /* Get relayd reference if exists. */
222 relayd
= consumer_find_relayd(msg
.u
.relayd_sock
.net_index
);
223 if (relayd
== NULL
) {
224 /* Not found. Allocate one. */
225 relayd
= consumer_allocate_relayd_sock_pair(
226 msg
.u
.relayd_sock
.net_index
);
227 if (relayd
== NULL
) {
228 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
233 /* Poll on consumer socket. */
234 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
238 /* Get relayd socket from session daemon */
239 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
240 if (ret
!= sizeof(fd
)) {
241 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
245 /* Copy socket information and received FD */
246 switch (msg
.u
.relayd_sock
.type
) {
247 case LTTNG_STREAM_CONTROL
:
248 /* Copy received lttcomm socket */
249 lttcomm_copy_sock(&relayd
->control_sock
, &msg
.u
.relayd_sock
.sock
);
250 ret
= lttcomm_create_sock(&relayd
->control_sock
);
255 /* Close the created socket fd which is useless */
256 close(relayd
->control_sock
.fd
);
258 /* Assign new file descriptor */
259 relayd
->control_sock
.fd
= fd
;
261 case LTTNG_STREAM_DATA
:
262 /* Copy received lttcomm socket */
263 lttcomm_copy_sock(&relayd
->data_sock
, &msg
.u
.relayd_sock
.sock
);
264 ret
= lttcomm_create_sock(&relayd
->data_sock
);
269 /* Close the created socket fd which is useless */
270 close(relayd
->data_sock
.fd
);
272 /* Assign new file descriptor */
273 relayd
->data_sock
.fd
= fd
;
276 ERR("Unknown relayd socket type");
280 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
281 msg
.u
.relayd_sock
.type
== LTTNG_STREAM_CONTROL
? "control" : "data",
282 relayd
->net_seq_idx
, fd
);
285 * Add relayd socket pair to consumer data hashtable. If object already
286 * exists or on error, the function gracefully returns.
288 consumer_add_relayd(relayd
);
292 case LTTNG_CONSUMER_ADD_CHANNEL
:
294 struct lttng_consumer_channel
*new_channel
;
299 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
302 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
303 if (ret
!= sizeof(fds
)) {
304 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
308 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
310 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
312 msg
.u
.channel
.mmap_len
,
313 msg
.u
.channel
.max_sb_size
);
314 if (new_channel
== NULL
) {
315 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
318 if (ctx
->on_recv_channel
!= NULL
) {
319 ret
= ctx
->on_recv_channel(new_channel
);
321 consumer_add_channel(new_channel
);
322 } else if (ret
< 0) {
326 consumer_add_channel(new_channel
);
330 case LTTNG_CONSUMER_ADD_STREAM
:
332 struct lttng_consumer_stream
*new_stream
;
335 struct consumer_relayd_sock_pair
*relayd
= NULL
;
338 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
341 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
342 if (ret
!= sizeof(fds
)) {
343 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
347 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
348 new_stream
= consumer_allocate_stream(msg
.u
.channel
.channel_key
,
349 msg
.u
.stream
.stream_key
,
352 msg
.u
.stream
.mmap_len
,
354 msg
.u
.stream
.path_name
,
357 msg
.u
.stream
.net_index
,
358 msg
.u
.stream
.metadata_flag
);
359 if (new_stream
== NULL
) {
360 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
364 /* The stream is not metadata. Get relayd reference if exists. */
365 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
366 if (relayd
!= NULL
) {
367 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
368 /* Add stream on the relayd */
369 ret
= relayd_add_stream(&relayd
->control_sock
,
370 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
371 &new_stream
->relayd_stream_id
);
372 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
376 } else if (msg
.u
.stream
.net_index
!= -1) {
377 ERR("Network sequence index %d unknown. Not adding stream.",
378 msg
.u
.stream
.net_index
);
383 if (ctx
->on_recv_stream
!= NULL
) {
384 ret
= ctx
->on_recv_stream(new_stream
);
386 consumer_add_stream(new_stream
);
387 } else if (ret
< 0) {
391 consumer_add_stream(new_stream
);
394 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %lu",
395 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
396 new_stream
->relayd_stream_id
);
399 case LTTNG_CONSUMER_UPDATE_STREAM
:
403 if (ctx
->on_update_stream
!= NULL
) {
404 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
406 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
407 } else if (ret
< 0) {
411 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
422 * Wake-up the other end by writing a null byte in the pipe
423 * (non-blocking). Important note: Because writing into the
424 * pipe is non-blocking (and therefore we allow dropping wakeup
425 * data, as long as there is wakeup data present in the pipe
426 * buffer to wake up the other end), the other end should
427 * perform the following sequence for waiting:
428 * 1) empty the pipe (reads).
429 * 2) perform update operation.
430 * 3) wait on the pipe (poll).
433 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
434 } while (ret
< 0 && errno
== EINTR
);
439 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
441 struct lttng_ust_object_data obj
;
444 obj
.shm_fd
= chan
->shm_fd
;
445 obj
.wait_fd
= chan
->wait_fd
;
446 obj
.memory_map_size
= chan
->mmap_len
;
447 chan
->handle
= ustctl_map_channel(&obj
);
451 chan
->wait_fd_is_copy
= 1;
457 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
459 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
460 stream
->hangup_flush_done
= 1;
463 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
465 ustctl_unmap_channel(chan
->handle
);
468 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
470 struct lttng_ust_object_data obj
;
474 obj
.shm_fd
= stream
->shm_fd
;
475 obj
.wait_fd
= stream
->wait_fd
;
476 obj
.memory_map_size
= stream
->mmap_len
;
477 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
480 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
483 /* ustctl_open_stream_read has closed the shm fd. */
484 stream
->wait_fd_is_copy
= 1;
487 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
488 if (!stream
->mmap_base
) {
495 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
497 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
501 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
502 struct lttng_consumer_local_data
*ctx
)
507 struct lttng_ust_shm_handle
*handle
;
508 struct lttng_ust_lib_ring_buffer
*buf
;
512 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
513 stream
->wait_fd
, stream
->key
);
515 /* We can consume the 1 byte written into the wait_fd by UST */
516 if (!stream
->hangup_flush_done
) {
518 readlen
= read(stream
->wait_fd
, &dummy
, 1);
519 } while (readlen
== -1 && errno
== EINTR
);
527 handle
= stream
->chan
->handle
;
528 /* Get the next subbuffer */
529 err
= ustctl_get_next_subbuf(handle
, buf
);
531 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
533 * This is a debug message even for single-threaded consumer,
534 * because poll() have more relaxed criterions than get subbuf,
535 * so get_subbuf may fail for short race windows where poll()
536 * would issue wakeups.
538 DBG("Reserving sub buffer failed (everything is normal, "
539 "it is due to concurrency)");
542 assert(stream
->output
== LTTNG_EVENT_MMAP
);
543 /* read the used subbuffer size */
544 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
546 /* write the subbuffer to the tracefile */
547 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
550 * display the error but continue processing to try
551 * to release the subbuffer
553 ERR("Error writing to tracefile");
555 err
= ustctl_put_next_subbuf(handle
, buf
);
561 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
565 /* Opening the tracefile in write mode */
566 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
567 ret
= run_as_open(stream
->path_name
,
568 O_WRONLY
|O_CREAT
|O_TRUNC
,
569 S_IRWXU
|S_IRWXG
|S_IRWXO
,
570 stream
->uid
, stream
->gid
);
572 ERR("Opening %s", stream
->path_name
);
576 stream
->out_fd
= ret
;
579 /* we return 0 to let the library handle the FD internally */