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 needs 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) {
140 /* Get relayd socket from session daemon */
141 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
142 if (ret
!= sizeof(fd
)) {
143 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
147 /* Copy socket information and received FD */
148 switch (msg
.u
.relayd_sock
.type
) {
149 case LTTNG_STREAM_CONTROL
:
150 /* Copy received lttcomm socket */
151 lttcomm_copy_sock(&relayd
->control_sock
, &msg
.u
.relayd_sock
.sock
);
152 ret
= lttcomm_create_sock(&relayd
->control_sock
);
157 /* Close the created socket fd which is useless */
158 close(relayd
->control_sock
.fd
);
160 /* Assign new file descriptor */
161 relayd
->control_sock
.fd
= fd
;
163 case LTTNG_STREAM_DATA
:
164 /* Copy received lttcomm socket */
165 lttcomm_copy_sock(&relayd
->data_sock
, &msg
.u
.relayd_sock
.sock
);
166 ret
= lttcomm_create_sock(&relayd
->data_sock
);
171 /* Close the created socket fd which is useless */
172 close(relayd
->data_sock
.fd
);
174 /* Assign new file descriptor */
175 relayd
->data_sock
.fd
= fd
;
178 ERR("Unknown relayd socket type");
182 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
183 msg
.u
.relayd_sock
.type
== LTTNG_STREAM_CONTROL
? "control" : "data",
184 relayd
->net_seq_idx
, fd
);
187 * Add relayd socket pair to consumer data hashtable. If object already
188 * exists or on error, the function gracefully returns.
190 consumer_add_relayd(relayd
);
194 case LTTNG_CONSUMER_ADD_CHANNEL
:
196 struct lttng_consumer_channel
*new_channel
;
200 DBG("UST Consumer adding channel");
203 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
207 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
208 if (ret
!= sizeof(fds
)) {
209 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
214 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
216 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
218 msg
.u
.channel
.mmap_len
,
219 msg
.u
.channel
.max_sb_size
);
220 if (new_channel
== NULL
) {
221 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
224 if (ctx
->on_recv_channel
!= NULL
) {
225 ret
= ctx
->on_recv_channel(new_channel
);
227 consumer_add_channel(new_channel
);
228 } else if (ret
< 0) {
232 consumer_add_channel(new_channel
);
236 case LTTNG_CONSUMER_ADD_STREAM
:
238 struct lttng_consumer_stream
*new_stream
;
241 struct consumer_relayd_sock_pair
*relayd
= NULL
;
243 DBG("UST Consumer adding stream");
246 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
250 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
251 if (ret
!= sizeof(fds
)) {
252 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
257 DBG("consumer_add_stream chan %d stream %d",
258 msg
.u
.stream
.channel_key
,
259 msg
.u
.stream
.stream_key
);
261 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
262 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
263 msg
.u
.stream
.stream_key
,
266 msg
.u
.stream
.mmap_len
,
268 msg
.u
.stream
.path_name
,
271 msg
.u
.stream
.net_index
,
272 msg
.u
.stream
.metadata_flag
);
273 if (new_stream
== NULL
) {
274 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
278 /* The stream is not metadata. Get relayd reference if exists. */
279 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
280 if (relayd
!= NULL
) {
281 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
282 /* Add stream on the relayd */
283 ret
= relayd_add_stream(&relayd
->control_sock
,
284 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
285 &new_stream
->relayd_stream_id
);
286 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
290 } else if (msg
.u
.stream
.net_index
!= -1) {
291 ERR("Network sequence index %d unknown. Not adding stream.",
292 msg
.u
.stream
.net_index
);
297 if (ctx
->on_recv_stream
!= NULL
) {
298 ret
= ctx
->on_recv_stream(new_stream
);
300 consumer_add_stream(new_stream
);
301 } else if (ret
< 0) {
305 consumer_add_stream(new_stream
);
308 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %lu",
309 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
310 new_stream
->relayd_stream_id
);
313 case LTTNG_CONSUMER_DESTROY_RELAYD
:
315 struct consumer_relayd_sock_pair
*relayd
;
317 DBG("UST consumer destroying relayd %zu",
318 msg
.u
.destroy_relayd
.net_seq_idx
);
320 /* Get relayd reference if exists. */
321 relayd
= consumer_find_relayd(msg
.u
.destroy_relayd
.net_seq_idx
);
322 if (relayd
== NULL
) {
323 ERR("Unable to find relayd %zu", msg
.u
.destroy_relayd
.net_seq_idx
);
327 /* Set destroy flag for this object */
328 uatomic_set(&relayd
->destroy_flag
, 1);
330 /* Destroy the relayd if refcount is 0 else set the destroy flag. */
331 if (uatomic_read(&relayd
->refcount
) == 0) {
332 consumer_destroy_relayd(relayd
);
336 case LTTNG_CONSUMER_UPDATE_STREAM
:
341 if (ctx
->on_update_stream
!= NULL
) {
342 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
344 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
345 } else if (ret
< 0) {
349 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
360 * Wake-up the other end by writing a null byte in the pipe (non-blocking).
361 * Important note: Because writing into the pipe is non-blocking (and
362 * therefore we allow dropping wakeup data, as long as there is wakeup data
363 * present in the pipe buffer to wake up the other end), the other end
364 * should perform the following sequence for waiting:
366 * 1) empty the pipe (reads).
367 * 2) perform update operation.
368 * 3) wait on the pipe (poll).
371 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
372 } while (ret
< 0 && errno
== EINTR
);
378 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
380 struct lttng_ust_object_data obj
;
383 obj
.shm_fd
= chan
->shm_fd
;
384 obj
.wait_fd
= chan
->wait_fd
;
385 obj
.memory_map_size
= chan
->mmap_len
;
386 chan
->handle
= ustctl_map_channel(&obj
);
390 chan
->wait_fd_is_copy
= 1;
396 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
398 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
399 stream
->hangup_flush_done
= 1;
402 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
404 ustctl_unmap_channel(chan
->handle
);
407 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
409 struct lttng_ust_object_data obj
;
413 obj
.shm_fd
= stream
->shm_fd
;
414 obj
.wait_fd
= stream
->wait_fd
;
415 obj
.memory_map_size
= stream
->mmap_len
;
416 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
419 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
422 /* ustctl_open_stream_read has closed the shm fd. */
423 stream
->wait_fd_is_copy
= 1;
426 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
427 if (!stream
->mmap_base
) {
434 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
436 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
440 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
441 struct lttng_consumer_local_data
*ctx
)
446 struct lttng_ust_shm_handle
*handle
;
447 struct lttng_ust_lib_ring_buffer
*buf
;
451 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
452 stream
->wait_fd
, stream
->key
);
454 /* We can consume the 1 byte written into the wait_fd by UST */
455 if (!stream
->hangup_flush_done
) {
457 readlen
= read(stream
->wait_fd
, &dummy
, 1);
458 } while (readlen
== -1 && errno
== EINTR
);
466 handle
= stream
->chan
->handle
;
467 /* Get the next subbuffer */
468 err
= ustctl_get_next_subbuf(handle
, buf
);
470 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
472 * This is a debug message even for single-threaded consumer,
473 * because poll() have more relaxed criterions than get subbuf,
474 * so get_subbuf may fail for short race windows where poll()
475 * would issue wakeups.
477 DBG("Reserving sub buffer failed (everything is normal, "
478 "it is due to concurrency)");
481 assert(stream
->output
== LTTNG_EVENT_MMAP
);
482 /* read the used subbuffer size */
483 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
485 /* write the subbuffer to the tracefile */
486 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
489 * display the error but continue processing to try
490 * to release the subbuffer
492 ERR("Error writing to tracefile (expected: %ld, got: %ld)", ret
, len
);
494 err
= ustctl_put_next_subbuf(handle
, buf
);
500 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
504 /* Opening the tracefile in write mode */
505 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
506 ret
= run_as_open(stream
->path_name
,
507 O_WRONLY
|O_CREAT
|O_TRUNC
,
508 S_IRWXU
|S_IRWXG
|S_IRWXO
,
509 stream
->uid
, stream
->gid
);
511 ERR("Opening %s", stream
->path_name
);
515 stream
->out_fd
= ret
;
518 /* we return 0 to let the library handle the FD internally */