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
:
118 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
119 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
120 &msg
.u
.relayd_sock
.sock
);
123 case LTTNG_CONSUMER_ADD_CHANNEL
:
125 struct lttng_consumer_channel
*new_channel
;
129 DBG("UST Consumer adding channel");
132 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
136 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
137 if (ret
!= sizeof(fds
)) {
138 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
143 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
145 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
147 msg
.u
.channel
.mmap_len
,
148 msg
.u
.channel
.max_sb_size
);
149 if (new_channel
== NULL
) {
150 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
153 if (ctx
->on_recv_channel
!= NULL
) {
154 ret
= ctx
->on_recv_channel(new_channel
);
156 consumer_add_channel(new_channel
);
157 } else if (ret
< 0) {
161 consumer_add_channel(new_channel
);
165 case LTTNG_CONSUMER_ADD_STREAM
:
167 struct lttng_consumer_stream
*new_stream
;
170 struct consumer_relayd_sock_pair
*relayd
= NULL
;
172 DBG("UST Consumer adding stream");
175 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
179 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
180 if (ret
!= sizeof(fds
)) {
181 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
186 DBG("consumer_add_stream chan %d stream %d",
187 msg
.u
.stream
.channel_key
,
188 msg
.u
.stream
.stream_key
);
190 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
191 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
192 msg
.u
.stream
.stream_key
,
195 msg
.u
.stream
.mmap_len
,
197 msg
.u
.stream
.path_name
,
200 msg
.u
.stream
.net_index
,
201 msg
.u
.stream
.metadata_flag
);
202 if (new_stream
== NULL
) {
203 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
207 /* The stream is not metadata. Get relayd reference if exists. */
208 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
209 if (relayd
!= NULL
) {
210 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
211 /* Add stream on the relayd */
212 ret
= relayd_add_stream(&relayd
->control_sock
,
213 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
214 &new_stream
->relayd_stream_id
);
215 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
219 } else if (msg
.u
.stream
.net_index
!= -1) {
220 ERR("Network sequence index %d unknown. Not adding stream.",
221 msg
.u
.stream
.net_index
);
226 if (ctx
->on_recv_stream
!= NULL
) {
227 ret
= ctx
->on_recv_stream(new_stream
);
229 consumer_add_stream(new_stream
);
230 } else if (ret
< 0) {
234 consumer_add_stream(new_stream
);
237 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %" PRIu64
,
238 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
239 new_stream
->relayd_stream_id
);
242 case LTTNG_CONSUMER_DESTROY_RELAYD
:
244 struct consumer_relayd_sock_pair
*relayd
;
246 DBG("UST consumer destroying relayd %" PRIu64
,
247 msg
.u
.destroy_relayd
.net_seq_idx
);
249 /* Get relayd reference if exists. */
250 relayd
= consumer_find_relayd(msg
.u
.destroy_relayd
.net_seq_idx
);
251 if (relayd
== NULL
) {
252 ERR("Unable to find relayd %" PRIu64
, msg
.u
.destroy_relayd
.net_seq_idx
);
256 /* Set destroy flag for this object */
257 uatomic_set(&relayd
->destroy_flag
, 1);
259 /* Destroy the relayd if refcount is 0 else set the destroy flag. */
260 if (uatomic_read(&relayd
->refcount
) == 0) {
261 consumer_destroy_relayd(relayd
);
265 case LTTNG_CONSUMER_UPDATE_STREAM
:
270 if (ctx
->on_update_stream
!= NULL
) {
271 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
273 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
274 } else if (ret
< 0) {
278 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
289 * Wake-up the other end by writing a null byte in the pipe (non-blocking).
290 * Important note: Because writing into the pipe is non-blocking (and
291 * therefore we allow dropping wakeup data, as long as there is wakeup data
292 * present in the pipe buffer to wake up the other end), the other end
293 * should perform the following sequence for waiting:
295 * 1) empty the pipe (reads).
296 * 2) perform update operation.
297 * 3) wait on the pipe (poll).
300 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
301 } while (ret
< 0 && errno
== EINTR
);
303 /* XXX: At some point we might want to return something else than zero */
308 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
310 struct lttng_ust_object_data obj
;
313 obj
.shm_fd
= chan
->shm_fd
;
314 obj
.wait_fd
= chan
->wait_fd
;
315 obj
.memory_map_size
= chan
->mmap_len
;
316 chan
->handle
= ustctl_map_channel(&obj
);
320 chan
->wait_fd_is_copy
= 1;
326 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
328 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
329 stream
->hangup_flush_done
= 1;
332 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
334 ustctl_unmap_channel(chan
->handle
);
337 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
339 struct lttng_ust_object_data obj
;
343 obj
.shm_fd
= stream
->shm_fd
;
344 obj
.wait_fd
= stream
->wait_fd
;
345 obj
.memory_map_size
= stream
->mmap_len
;
346 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
349 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
352 /* ustctl_open_stream_read has closed the shm fd. */
353 stream
->wait_fd_is_copy
= 1;
356 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
357 if (!stream
->mmap_base
) {
364 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
366 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
370 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
371 struct lttng_consumer_local_data
*ctx
)
376 struct lttng_ust_shm_handle
*handle
;
377 struct lttng_ust_lib_ring_buffer
*buf
;
381 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
382 stream
->wait_fd
, stream
->key
);
384 /* We can consume the 1 byte written into the wait_fd by UST */
385 if (!stream
->hangup_flush_done
) {
387 readlen
= read(stream
->wait_fd
, &dummy
, 1);
388 } while (readlen
== -1 && errno
== EINTR
);
396 handle
= stream
->chan
->handle
;
397 /* Get the next subbuffer */
398 err
= ustctl_get_next_subbuf(handle
, buf
);
400 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
402 * This is a debug message even for single-threaded consumer,
403 * because poll() have more relaxed criterions than get subbuf,
404 * so get_subbuf may fail for short race windows where poll()
405 * would issue wakeups.
407 DBG("Reserving sub buffer failed (everything is normal, "
408 "it is due to concurrency)");
411 assert(stream
->output
== LTTNG_EVENT_MMAP
);
412 /* read the used subbuffer size */
413 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
415 /* write the subbuffer to the tracefile */
416 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
419 * display the error but continue processing to try
420 * to release the subbuffer
422 ERR("Error writing to tracefile (expected: %ld, got: %ld)", ret
, len
);
424 err
= ustctl_put_next_subbuf(handle
, buf
);
430 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
434 /* Opening the tracefile in write mode */
435 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
436 ret
= run_as_open(stream
->path_name
,
437 O_WRONLY
|O_CREAT
|O_TRUNC
,
438 S_IRWXU
|S_IRWXG
|S_IRWXO
,
439 stream
->uid
, stream
->gid
);
441 ERR("Opening %s", stream
->path_name
);
445 stream
->out_fd
= ret
;
448 /* we return 0 to let the library handle the FD internally */