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");
96 * Receive command from session daemon and process it.
98 * Return 1 on success else a negative value or 0.
100 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
101 int sock
, struct pollfd
*consumer_sockpoll
)
104 struct lttcomm_consumer_msg msg
;
106 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
107 if (ret
!= sizeof(msg
)) {
108 DBG("Consumer received unexpected message size %zd (expects %zu)",
110 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
113 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
117 /* relayd needs RCU read-side lock */
120 switch (msg
.cmd_type
) {
121 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
123 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
124 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
125 &msg
.u
.relayd_sock
.sock
);
128 case LTTNG_CONSUMER_ADD_CHANNEL
:
130 struct lttng_consumer_channel
*new_channel
;
134 DBG("UST Consumer adding channel");
137 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
141 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
142 if (ret
!= sizeof(fds
)) {
143 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
148 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
150 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
152 msg
.u
.channel
.mmap_len
,
153 msg
.u
.channel
.max_sb_size
);
154 if (new_channel
== NULL
) {
155 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
158 if (ctx
->on_recv_channel
!= NULL
) {
159 ret
= ctx
->on_recv_channel(new_channel
);
161 consumer_add_channel(new_channel
);
162 } else if (ret
< 0) {
166 consumer_add_channel(new_channel
);
170 case LTTNG_CONSUMER_ADD_STREAM
:
172 struct lttng_consumer_stream
*new_stream
;
175 struct consumer_relayd_sock_pair
*relayd
= NULL
;
177 DBG("UST Consumer adding stream");
180 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
184 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
185 if (ret
!= sizeof(fds
)) {
186 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
191 DBG("consumer_add_stream chan %d stream %d",
192 msg
.u
.stream
.channel_key
,
193 msg
.u
.stream
.stream_key
);
195 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
196 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
197 msg
.u
.stream
.stream_key
,
200 msg
.u
.stream
.mmap_len
,
202 msg
.u
.stream
.path_name
,
205 msg
.u
.stream
.net_index
,
206 msg
.u
.stream
.metadata_flag
);
207 if (new_stream
== NULL
) {
208 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
212 /* The stream is not metadata. Get relayd reference if exists. */
213 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
214 if (relayd
!= NULL
) {
215 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
216 /* Add stream on the relayd */
217 ret
= relayd_add_stream(&relayd
->control_sock
,
218 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
219 &new_stream
->relayd_stream_id
);
220 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
224 } else if (msg
.u
.stream
.net_index
!= -1) {
225 ERR("Network sequence index %d unknown. Not adding stream.",
226 msg
.u
.stream
.net_index
);
231 if (ctx
->on_recv_stream
!= NULL
) {
232 ret
= ctx
->on_recv_stream(new_stream
);
234 consumer_add_stream(new_stream
);
235 } else if (ret
< 0) {
239 consumer_add_stream(new_stream
);
242 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %" PRIu64
,
243 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
244 new_stream
->relayd_stream_id
);
247 case LTTNG_CONSUMER_DESTROY_RELAYD
:
249 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
250 struct consumer_relayd_sock_pair
*relayd
;
252 DBG("UST consumer destroying relayd %" PRIu64
, index
);
254 /* Get relayd reference if exists. */
255 relayd
= consumer_find_relayd(index
);
256 if (relayd
== NULL
) {
257 ERR("Unable to find relayd %" PRIu64
, index
);
262 * Each relayd socket pair has a refcount of stream attached to it
263 * which tells if the relayd is still active or not depending on the
266 * This will set the destroy flag of the relayd object and destroy it
267 * if the refcount reaches zero when called.
269 * The destroy can happen either here or when a stream fd hangs up.
271 consumer_flag_relayd_for_destroy(relayd
);
275 case LTTNG_CONSUMER_UPDATE_STREAM
:
280 if (ctx
->on_update_stream
!= NULL
) {
281 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
283 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
284 } else if (ret
< 0) {
288 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
299 * Wake-up the other end by writing a null byte in the pipe (non-blocking).
300 * Important note: Because writing into the pipe is non-blocking (and
301 * therefore we allow dropping wakeup data, as long as there is wakeup data
302 * present in the pipe buffer to wake up the other end), the other end
303 * should perform the following sequence for waiting:
305 * 1) empty the pipe (reads).
306 * 2) perform update operation.
307 * 3) wait on the pipe (poll).
310 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
311 } while (ret
< 0 && errno
== EINTR
);
316 * Return 1 to indicate success since the 0 value can be a socket
317 * shutdown during the recv() or send() call.
322 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
324 struct lttng_ust_object_data obj
;
327 obj
.shm_fd
= chan
->shm_fd
;
328 obj
.wait_fd
= chan
->wait_fd
;
329 obj
.memory_map_size
= chan
->mmap_len
;
330 chan
->handle
= ustctl_map_channel(&obj
);
334 chan
->wait_fd_is_copy
= 1;
340 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
342 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
343 stream
->hangup_flush_done
= 1;
346 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
348 ustctl_unmap_channel(chan
->handle
);
351 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
353 struct lttng_ust_object_data obj
;
357 obj
.shm_fd
= stream
->shm_fd
;
358 obj
.wait_fd
= stream
->wait_fd
;
359 obj
.memory_map_size
= stream
->mmap_len
;
360 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
363 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
366 /* ustctl_open_stream_read has closed the shm fd. */
367 stream
->wait_fd_is_copy
= 1;
370 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
371 if (!stream
->mmap_base
) {
378 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
380 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
384 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
385 struct lttng_consumer_local_data
*ctx
)
390 struct lttng_ust_shm_handle
*handle
;
391 struct lttng_ust_lib_ring_buffer
*buf
;
395 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
396 stream
->wait_fd
, stream
->key
);
398 /* We can consume the 1 byte written into the wait_fd by UST */
399 if (!stream
->hangup_flush_done
) {
401 readlen
= read(stream
->wait_fd
, &dummy
, 1);
402 } while (readlen
== -1 && errno
== EINTR
);
410 handle
= stream
->chan
->handle
;
411 /* Get the next subbuffer */
412 err
= ustctl_get_next_subbuf(handle
, buf
);
414 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
416 * This is a debug message even for single-threaded consumer,
417 * because poll() have more relaxed criterions than get subbuf,
418 * so get_subbuf may fail for short race windows where poll()
419 * would issue wakeups.
421 DBG("Reserving sub buffer failed (everything is normal, "
422 "it is due to concurrency)");
425 assert(stream
->output
== LTTNG_EVENT_MMAP
);
426 /* read the used subbuffer size */
427 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
429 /* write the subbuffer to the tracefile */
430 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
433 * display the error but continue processing to try
434 * to release the subbuffer
436 ERR("Error writing to tracefile (expected: %ld, got: %ld)", ret
, len
);
438 err
= ustctl_put_next_subbuf(handle
, buf
);
444 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
448 /* Opening the tracefile in write mode */
449 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
450 ret
= run_as_open(stream
->path_name
,
451 O_WRONLY
|O_CREAT
|O_TRUNC
,
452 S_IRWXU
|S_IRWXG
|S_IRWXO
,
453 stream
->uid
, stream
->gid
);
455 ERR("Opening %s", stream
->path_name
);
459 stream
->out_fd
= ret
;
462 /* we return 0 to let the library handle the FD internally */