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>
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 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
45 * compiled out, we isolate it in this library.
47 int lttng_ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle
*handle
,
48 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *off
)
50 return ustctl_get_mmap_read_offset(handle
, buf
, off
);
54 * Take a snapshot for a specific fd
56 * Returns 0 on success, < 0 on error
58 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
59 struct lttng_consumer_stream
*stream
)
63 ret
= ustctl_snapshot(stream
->chan
->handle
, stream
->buf
);
66 PERROR("Getting sub-buffer snapshot.");
73 * Get the produced position
75 * Returns 0 on success, < 0 on error
77 int lttng_ustconsumer_get_produced_snapshot(
78 struct lttng_consumer_local_data
*ctx
,
79 struct lttng_consumer_stream
*stream
,
84 ret
= ustctl_snapshot_get_produced(stream
->chan
->handle
,
88 PERROR("kernctl_snapshot_get_produced");
94 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
95 int sock
, struct pollfd
*consumer_sockpoll
)
98 struct lttcomm_consumer_msg msg
;
100 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
101 if (ret
!= sizeof(msg
)) {
102 DBG("Consumer received unexpected message size %zd (expects %zu)",
104 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
107 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
111 /* relayd need RCU read-side lock */
114 switch (msg
.cmd_type
) {
115 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
118 struct consumer_relayd_sock_pair
*relayd
;
120 DBG("UST Consumer adding relayd socket");
122 /* Get relayd reference if exists. */
123 relayd
= consumer_find_relayd(msg
.u
.relayd_sock
.net_index
);
124 if (relayd
== NULL
) {
125 /* Not found. Allocate one. */
126 relayd
= consumer_allocate_relayd_sock_pair(
127 msg
.u
.relayd_sock
.net_index
);
128 if (relayd
== NULL
) {
129 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
134 /* Poll on consumer socket. */
135 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
139 /* Get relayd socket from session daemon */
140 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
141 if (ret
!= sizeof(fd
)) {
142 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
146 /* Copy socket information and received FD */
147 switch (msg
.u
.relayd_sock
.type
) {
148 case LTTNG_STREAM_CONTROL
:
149 /* Copy received lttcomm socket */
150 lttcomm_copy_sock(&relayd
->control_sock
, &msg
.u
.relayd_sock
.sock
);
151 ret
= lttcomm_create_sock(&relayd
->control_sock
);
156 /* Close the created socket fd which is useless */
157 close(relayd
->control_sock
.fd
);
159 /* Assign new file descriptor */
160 relayd
->control_sock
.fd
= fd
;
162 case LTTNG_STREAM_DATA
:
163 /* Copy received lttcomm socket */
164 lttcomm_copy_sock(&relayd
->data_sock
, &msg
.u
.relayd_sock
.sock
);
165 ret
= lttcomm_create_sock(&relayd
->data_sock
);
170 /* Close the created socket fd which is useless */
171 close(relayd
->data_sock
.fd
);
173 /* Assign new file descriptor */
174 relayd
->data_sock
.fd
= fd
;
177 ERR("Unknown relayd socket type");
181 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
182 msg
.u
.relayd_sock
.type
== LTTNG_STREAM_CONTROL
? "control" : "data",
183 relayd
->net_seq_idx
, fd
);
186 * Add relayd socket pair to consumer data hashtable. If object already
187 * exists or on error, the function gracefully returns.
189 consumer_add_relayd(relayd
);
193 case LTTNG_CONSUMER_ADD_CHANNEL
:
195 struct lttng_consumer_channel
*new_channel
;
199 DBG("UST Consumer adding channel");
202 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
205 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
206 if (ret
!= sizeof(fds
)) {
207 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
211 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
213 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
215 msg
.u
.channel
.mmap_len
,
216 msg
.u
.channel
.max_sb_size
);
217 if (new_channel
== NULL
) {
218 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
221 if (ctx
->on_recv_channel
!= NULL
) {
222 ret
= ctx
->on_recv_channel(new_channel
);
224 consumer_add_channel(new_channel
);
225 } else if (ret
< 0) {
229 consumer_add_channel(new_channel
);
233 case LTTNG_CONSUMER_ADD_STREAM
:
235 struct lttng_consumer_stream
*new_stream
;
238 struct consumer_relayd_sock_pair
*relayd
= NULL
;
240 DBG("UST Consumer adding stream");
243 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
246 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
247 if (ret
!= sizeof(fds
)) {
248 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
252 DBG("consumer_add_stream chan %d stream %d",
253 msg
.u
.stream
.channel_key
,
254 msg
.u
.stream
.stream_key
);
256 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
257 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
258 msg
.u
.stream
.stream_key
,
261 msg
.u
.stream
.mmap_len
,
263 msg
.u
.stream
.path_name
,
266 msg
.u
.stream
.net_index
,
267 msg
.u
.stream
.metadata_flag
);
268 if (new_stream
== NULL
) {
269 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
273 /* The stream is not metadata. Get relayd reference if exists. */
274 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
275 if (relayd
!= NULL
) {
276 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
277 /* Add stream on the relayd */
278 ret
= relayd_add_stream(&relayd
->control_sock
,
279 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
280 &new_stream
->relayd_stream_id
);
281 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
285 } else if (msg
.u
.stream
.net_index
!= -1) {
286 ERR("Network sequence index %d unknown. Not adding stream.",
287 msg
.u
.stream
.net_index
);
292 if (ctx
->on_recv_stream
!= NULL
) {
293 ret
= ctx
->on_recv_stream(new_stream
);
295 consumer_add_stream(new_stream
);
296 } else if (ret
< 0) {
300 consumer_add_stream(new_stream
);
303 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %lu",
304 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
305 new_stream
->relayd_stream_id
);
308 case LTTNG_CONSUMER_DESTROY_RELAYD
:
310 struct consumer_relayd_sock_pair
*relayd
;
312 DBG("UST consumer destroying relayd %zu",
313 msg
.u
.destroy_relayd
.net_seq_idx
);
315 /* Get relayd reference if exists. */
316 relayd
= consumer_find_relayd(msg
.u
.destroy_relayd
.net_seq_idx
);
317 if (relayd
== NULL
) {
318 ERR("Unable to find relayd %zu",
319 msg
.u
.destroy_relayd
.net_seq_idx
);
322 /* Set destroy flag for this object */
323 uatomic_set(&relayd
->destroy_flag
, 1);
325 /* Destroy the relayd if refcount is 0 else set the destroy flag. */
326 if (uatomic_read(&relayd
->refcount
) == 0) {
327 consumer_destroy_relayd(relayd
);
331 case LTTNG_CONSUMER_UPDATE_STREAM
:
335 if (ctx
->on_update_stream
!= NULL
) {
336 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
338 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
339 } else if (ret
< 0) {
343 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
354 * Wake-up the other end by writing a null byte in the pipe
355 * (non-blocking). Important note: Because writing into the
356 * pipe is non-blocking (and therefore we allow dropping wakeup
357 * data, as long as there is wakeup data present in the pipe
358 * buffer to wake up the other end), the other end should
359 * perform the following sequence for waiting:
360 * 1) empty the pipe (reads).
361 * 2) perform update operation.
362 * 3) wait on the pipe (poll).
365 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
366 } while (ret
< 0 && errno
== EINTR
);
372 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
374 struct lttng_ust_object_data obj
;
377 obj
.shm_fd
= chan
->shm_fd
;
378 obj
.wait_fd
= chan
->wait_fd
;
379 obj
.memory_map_size
= chan
->mmap_len
;
380 chan
->handle
= ustctl_map_channel(&obj
);
384 chan
->wait_fd_is_copy
= 1;
390 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
392 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
393 stream
->hangup_flush_done
= 1;
396 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
398 ustctl_unmap_channel(chan
->handle
);
401 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
403 struct lttng_ust_object_data obj
;
407 obj
.shm_fd
= stream
->shm_fd
;
408 obj
.wait_fd
= stream
->wait_fd
;
409 obj
.memory_map_size
= stream
->mmap_len
;
410 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
413 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
416 /* ustctl_open_stream_read has closed the shm fd. */
417 stream
->wait_fd_is_copy
= 1;
420 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
421 if (!stream
->mmap_base
) {
428 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
430 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
434 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
435 struct lttng_consumer_local_data
*ctx
)
440 struct lttng_ust_shm_handle
*handle
;
441 struct lttng_ust_lib_ring_buffer
*buf
;
445 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
446 stream
->wait_fd
, stream
->key
);
448 /* We can consume the 1 byte written into the wait_fd by UST */
449 if (!stream
->hangup_flush_done
) {
451 readlen
= read(stream
->wait_fd
, &dummy
, 1);
452 } while (readlen
== -1 && errno
== EINTR
);
460 handle
= stream
->chan
->handle
;
461 /* Get the next subbuffer */
462 err
= ustctl_get_next_subbuf(handle
, buf
);
464 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
466 * This is a debug message even for single-threaded consumer,
467 * because poll() have more relaxed criterions than get subbuf,
468 * so get_subbuf may fail for short race windows where poll()
469 * would issue wakeups.
471 DBG("Reserving sub buffer failed (everything is normal, "
472 "it is due to concurrency)");
475 assert(stream
->output
== LTTNG_EVENT_MMAP
);
476 /* read the used subbuffer size */
477 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
479 /* write the subbuffer to the tracefile */
480 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
483 * display the error but continue processing to try
484 * to release the subbuffer
486 ERR("Error writing to tracefile");
488 err
= ustctl_put_next_subbuf(handle
, buf
);
494 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
498 /* Opening the tracefile in write mode */
499 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
500 ret
= run_as_open(stream
->path_name
,
501 O_WRONLY
|O_CREAT
|O_TRUNC
,
502 S_IRWXU
|S_IRWXG
|S_IRWXO
,
503 stream
->uid
, stream
->gid
);
505 ERR("Opening %s", stream
->path_name
);
509 stream
->out_fd
= ret
;
512 /* we return 0 to let the library handle the FD internally */