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.
21 #include <lttng/ust-ctl.h>
27 #include <sys/socket.h>
29 #include <sys/types.h>
33 #include <common/common.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/relayd/relayd.h>
36 #include <common/compat/fcntl.h>
38 #include "ust-consumer.h"
40 extern struct lttng_consumer_global_data consumer_data
;
41 extern int consumer_poll_timeout
;
42 extern volatile int consumer_quit
;
45 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
46 * compiled out, we isolate it in this library.
48 int lttng_ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle
*handle
,
49 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *off
)
51 return ustctl_get_mmap_read_offset(handle
, buf
, off
);
55 * Take a snapshot for a specific fd
57 * Returns 0 on success, < 0 on error
59 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
60 struct lttng_consumer_stream
*stream
)
64 ret
= ustctl_snapshot(stream
->chan
->handle
, stream
->buf
);
67 PERROR("Getting sub-buffer snapshot.");
74 * Get the produced position
76 * Returns 0 on success, < 0 on error
78 int lttng_ustconsumer_get_produced_snapshot(
79 struct lttng_consumer_local_data
*ctx
,
80 struct lttng_consumer_stream
*stream
,
85 ret
= ustctl_snapshot_get_produced(stream
->chan
->handle
,
89 PERROR("kernctl_snapshot_get_produced");
95 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
96 int sock
, struct pollfd
*consumer_sockpoll
)
99 struct lttcomm_consumer_msg msg
;
101 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
102 if (ret
!= sizeof(msg
)) {
103 DBG("Consumer received unexpected message size %zd (expects %zu)",
105 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
108 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
112 /* relayd needs RCU read-side lock */
115 switch (msg
.cmd_type
) {
116 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
119 struct consumer_relayd_sock_pair
*relayd
;
121 DBG("UST Consumer adding relayd socket");
123 /* Get relayd reference if exists. */
124 relayd
= consumer_find_relayd(msg
.u
.relayd_sock
.net_index
);
125 if (relayd
== NULL
) {
126 /* Not found. Allocate one. */
127 relayd
= consumer_allocate_relayd_sock_pair(
128 msg
.u
.relayd_sock
.net_index
);
129 if (relayd
== NULL
) {
130 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
135 /* Poll on consumer socket. */
136 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
141 /* Get relayd socket from session daemon */
142 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
143 if (ret
!= sizeof(fd
)) {
144 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
148 /* Copy socket information and received FD */
149 switch (msg
.u
.relayd_sock
.type
) {
150 case LTTNG_STREAM_CONTROL
:
151 /* Copy received lttcomm socket */
152 lttcomm_copy_sock(&relayd
->control_sock
, &msg
.u
.relayd_sock
.sock
);
153 ret
= lttcomm_create_sock(&relayd
->control_sock
);
158 /* Close the created socket fd which is useless */
159 close(relayd
->control_sock
.fd
);
161 /* Assign new file descriptor */
162 relayd
->control_sock
.fd
= fd
;
164 case LTTNG_STREAM_DATA
:
165 /* Copy received lttcomm socket */
166 lttcomm_copy_sock(&relayd
->data_sock
, &msg
.u
.relayd_sock
.sock
);
167 ret
= lttcomm_create_sock(&relayd
->data_sock
);
172 /* Close the created socket fd which is useless */
173 close(relayd
->data_sock
.fd
);
175 /* Assign new file descriptor */
176 relayd
->data_sock
.fd
= fd
;
179 ERR("Unknown relayd socket type");
183 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
184 msg
.u
.relayd_sock
.type
== LTTNG_STREAM_CONTROL
? "control" : "data",
185 relayd
->net_seq_idx
, fd
);
188 * Add relayd socket pair to consumer data hashtable. If object already
189 * exists or on error, the function gracefully returns.
191 consumer_add_relayd(relayd
);
195 case LTTNG_CONSUMER_ADD_CHANNEL
:
197 struct lttng_consumer_channel
*new_channel
;
201 DBG("UST Consumer adding channel");
204 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
208 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
209 if (ret
!= sizeof(fds
)) {
210 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
215 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
217 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
219 msg
.u
.channel
.mmap_len
,
220 msg
.u
.channel
.max_sb_size
);
221 if (new_channel
== NULL
) {
222 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
225 if (ctx
->on_recv_channel
!= NULL
) {
226 ret
= ctx
->on_recv_channel(new_channel
);
228 consumer_add_channel(new_channel
);
229 } else if (ret
< 0) {
233 consumer_add_channel(new_channel
);
237 case LTTNG_CONSUMER_ADD_STREAM
:
239 struct lttng_consumer_stream
*new_stream
;
242 struct consumer_relayd_sock_pair
*relayd
= NULL
;
244 DBG("UST Consumer adding stream");
247 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
251 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
252 if (ret
!= sizeof(fds
)) {
253 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
258 DBG("consumer_add_stream chan %d stream %d",
259 msg
.u
.stream
.channel_key
,
260 msg
.u
.stream
.stream_key
);
262 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
263 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
264 msg
.u
.stream
.stream_key
,
267 msg
.u
.stream
.mmap_len
,
269 msg
.u
.stream
.path_name
,
272 msg
.u
.stream
.net_index
,
273 msg
.u
.stream
.metadata_flag
);
274 if (new_stream
== NULL
) {
275 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
279 /* The stream is not metadata. Get relayd reference if exists. */
280 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
281 if (relayd
!= NULL
) {
282 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
283 /* Add stream on the relayd */
284 ret
= relayd_add_stream(&relayd
->control_sock
,
285 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
286 &new_stream
->relayd_stream_id
);
287 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
291 } else if (msg
.u
.stream
.net_index
!= -1) {
292 ERR("Network sequence index %d unknown. Not adding stream.",
293 msg
.u
.stream
.net_index
);
298 if (ctx
->on_recv_stream
!= NULL
) {
299 ret
= ctx
->on_recv_stream(new_stream
);
301 consumer_add_stream(new_stream
);
302 } else if (ret
< 0) {
306 consumer_add_stream(new_stream
);
309 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %" PRIu64
,
310 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
311 new_stream
->relayd_stream_id
);
314 case LTTNG_CONSUMER_DESTROY_RELAYD
:
316 struct consumer_relayd_sock_pair
*relayd
;
318 DBG("UST consumer destroying relayd %" PRIu64
,
319 msg
.u
.destroy_relayd
.net_seq_idx
);
321 /* Get relayd reference if exists. */
322 relayd
= consumer_find_relayd(msg
.u
.destroy_relayd
.net_seq_idx
);
323 if (relayd
== NULL
) {
324 ERR("Unable to find relayd %" PRIu64
, msg
.u
.destroy_relayd
.net_seq_idx
);
328 /* Set destroy flag for this object */
329 uatomic_set(&relayd
->destroy_flag
, 1);
331 /* Destroy the relayd if refcount is 0 else set the destroy flag. */
332 if (uatomic_read(&relayd
->refcount
) == 0) {
333 consumer_destroy_relayd(relayd
);
337 case LTTNG_CONSUMER_UPDATE_STREAM
:
342 if (ctx
->on_update_stream
!= NULL
) {
343 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
345 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
346 } else if (ret
< 0) {
350 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
361 * Wake-up the other end by writing a null byte in the pipe (non-blocking).
362 * Important note: Because writing into the pipe is non-blocking (and
363 * therefore we allow dropping wakeup data, as long as there is wakeup data
364 * present in the pipe buffer to wake up the other end), the other end
365 * should perform the following sequence for waiting:
367 * 1) empty the pipe (reads).
368 * 2) perform update operation.
369 * 3) wait on the pipe (poll).
372 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
373 } while (ret
< 0 && errno
== EINTR
);
379 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
381 struct lttng_ust_object_data obj
;
384 obj
.shm_fd
= chan
->shm_fd
;
385 obj
.wait_fd
= chan
->wait_fd
;
386 obj
.memory_map_size
= chan
->mmap_len
;
387 chan
->handle
= ustctl_map_channel(&obj
);
391 chan
->wait_fd_is_copy
= 1;
397 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
399 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
400 stream
->hangup_flush_done
= 1;
403 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
405 ustctl_unmap_channel(chan
->handle
);
408 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
410 struct lttng_ust_object_data obj
;
414 obj
.shm_fd
= stream
->shm_fd
;
415 obj
.wait_fd
= stream
->wait_fd
;
416 obj
.memory_map_size
= stream
->mmap_len
;
417 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
420 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
423 /* ustctl_open_stream_read has closed the shm fd. */
424 stream
->wait_fd_is_copy
= 1;
427 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
428 if (!stream
->mmap_base
) {
435 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
437 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
441 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
442 struct lttng_consumer_local_data
*ctx
)
447 struct lttng_ust_shm_handle
*handle
;
448 struct lttng_ust_lib_ring_buffer
*buf
;
452 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
453 stream
->wait_fd
, stream
->key
);
455 /* We can consume the 1 byte written into the wait_fd by UST */
456 if (!stream
->hangup_flush_done
) {
458 readlen
= read(stream
->wait_fd
, &dummy
, 1);
459 } while (readlen
== -1 && errno
== EINTR
);
467 handle
= stream
->chan
->handle
;
468 /* Get the next subbuffer */
469 err
= ustctl_get_next_subbuf(handle
, buf
);
471 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
473 * This is a debug message even for single-threaded consumer,
474 * because poll() have more relaxed criterions than get subbuf,
475 * so get_subbuf may fail for short race windows where poll()
476 * would issue wakeups.
478 DBG("Reserving sub buffer failed (everything is normal, "
479 "it is due to concurrency)");
482 assert(stream
->output
== LTTNG_EVENT_MMAP
);
483 /* read the used subbuffer size */
484 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
486 /* write the subbuffer to the tracefile */
487 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
490 * display the error but continue processing to try
491 * to release the subbuffer
493 ERR("Error writing to tracefile (expected: %ld, got: %ld)", ret
, len
);
495 err
= ustctl_put_next_subbuf(handle
, buf
);
501 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
505 /* Opening the tracefile in write mode */
506 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
507 ret
= run_as_open(stream
->path_name
,
508 O_WRONLY
|O_CREAT
|O_TRUNC
,
509 S_IRWXU
|S_IRWXG
|S_IRWXO
,
510 stream
->uid
, stream
->gid
);
512 ERR("Opening %s", stream
->path_name
);
516 stream
->out_fd
= ret
;
519 /* we return 0 to let the library handle the FD internally */