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 msg
.u
.channel
.nb_init_streams
);
155 if (new_channel
== NULL
) {
156 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
159 if (ctx
->on_recv_channel
!= NULL
) {
160 ret
= ctx
->on_recv_channel(new_channel
);
162 consumer_add_channel(new_channel
);
163 } else if (ret
< 0) {
167 consumer_add_channel(new_channel
);
171 case LTTNG_CONSUMER_ADD_STREAM
:
173 struct lttng_consumer_stream
*new_stream
;
176 struct consumer_relayd_sock_pair
*relayd
= NULL
;
179 DBG("UST Consumer adding stream");
182 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
186 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
187 if (ret
!= sizeof(fds
)) {
188 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
193 DBG("Consumer command ADD_STREAM chan %d stream %d",
194 msg
.u
.stream
.channel_key
, msg
.u
.stream
.stream_key
);
196 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
197 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
198 msg
.u
.stream
.stream_key
,
201 msg
.u
.stream
.mmap_len
,
203 msg
.u
.stream
.path_name
,
206 msg
.u
.stream
.net_index
,
207 msg
.u
.stream
.metadata_flag
,
209 if (new_stream
== NULL
) {
214 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
218 * We could not find the channel. Can happen if cpu hotplug
219 * happens while tearing down.
221 DBG3("Could not find channel");
227 /* The stream is not metadata. Get relayd reference if exists. */
228 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
229 if (relayd
!= NULL
) {
230 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
231 /* Add stream on the relayd */
232 ret
= relayd_add_stream(&relayd
->control_sock
,
233 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
234 &new_stream
->relayd_stream_id
);
235 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
237 consumer_del_stream(new_stream
, NULL
);
240 } else if (msg
.u
.stream
.net_index
!= -1) {
241 ERR("Network sequence index %d unknown. Not adding stream.",
242 msg
.u
.stream
.net_index
);
243 consumer_del_stream(new_stream
, NULL
);
247 /* Do actions once stream has been received. */
248 if (ctx
->on_recv_stream
) {
249 ret
= ctx
->on_recv_stream(new_stream
);
251 consumer_del_stream(new_stream
, NULL
);
256 /* Send stream to the metadata thread */
257 if (new_stream
->metadata_flag
) {
259 ret
= write(ctx
->consumer_metadata_pipe
[1], &new_stream
,
261 } while (ret
< 0 && errno
== EINTR
);
263 PERROR("write metadata pipe");
264 consumer_del_metadata_stream(new_stream
, NULL
);
268 ret
= consumer_add_stream(new_stream
);
270 ERR("Consumer add stream %d failed. Continuing",
273 * At this point, if the add_stream fails, it is not in the
274 * hash table thus passing the NULL value here.
276 consumer_del_stream(new_stream
, NULL
);
281 DBG("UST consumer_add_stream %s (%d,%d) with relayd id %" PRIu64
,
282 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
283 new_stream
->relayd_stream_id
);
286 case LTTNG_CONSUMER_DESTROY_RELAYD
:
288 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
289 struct consumer_relayd_sock_pair
*relayd
;
291 DBG("UST consumer destroying relayd %" PRIu64
, index
);
293 /* Get relayd reference if exists. */
294 relayd
= consumer_find_relayd(index
);
295 if (relayd
== NULL
) {
296 ERR("Unable to find relayd %" PRIu64
, index
);
301 * Each relayd socket pair has a refcount of stream attached to it
302 * which tells if the relayd is still active or not depending on the
305 * This will set the destroy flag of the relayd object and destroy it
306 * if the refcount reaches zero when called.
308 * The destroy can happen either here or when a stream fd hangs up.
310 consumer_flag_relayd_for_destroy(relayd
);
314 case LTTNG_CONSUMER_UPDATE_STREAM
:
319 if (ctx
->on_update_stream
!= NULL
) {
320 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
322 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
323 } else if (ret
< 0) {
327 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
338 * Wake-up the other end by writing a null byte in the pipe (non-blocking).
339 * Important note: Because writing into the pipe is non-blocking (and
340 * therefore we allow dropping wakeup data, as long as there is wakeup data
341 * present in the pipe buffer to wake up the other end), the other end
342 * should perform the following sequence for waiting:
344 * 1) empty the pipe (reads).
345 * 2) perform update operation.
346 * 3) wait on the pipe (poll).
349 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
350 } while (ret
< 0 && errno
== EINTR
);
355 * Return 1 to indicate success since the 0 value can be a socket
356 * shutdown during the recv() or send() call.
361 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
363 struct lttng_ust_object_data obj
;
366 obj
.shm_fd
= chan
->shm_fd
;
367 obj
.wait_fd
= chan
->wait_fd
;
368 obj
.memory_map_size
= chan
->mmap_len
;
369 chan
->handle
= ustctl_map_channel(&obj
);
373 chan
->wait_fd_is_copy
= 1;
379 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
381 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
382 stream
->hangup_flush_done
= 1;
385 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
387 ustctl_unmap_channel(chan
->handle
);
390 int lttng_ustconsumer_add_stream(struct lttng_consumer_stream
*stream
)
392 struct lttng_ust_object_data obj
;
396 obj
.shm_fd
= stream
->shm_fd
;
397 obj
.wait_fd
= stream
->wait_fd
;
398 obj
.memory_map_size
= stream
->mmap_len
;
399 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
401 ERR("UST ctl add_stream failed with ret %d", ret
);
405 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
407 ERR("UST ctl open_stream_read failed");
412 /* ustctl_open_stream_read has closed the shm fd. */
413 stream
->wait_fd_is_copy
= 1;
416 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
417 if (!stream
->mmap_base
) {
418 ERR("UST ctl get_mmap_base failed");
426 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
431 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
433 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
437 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
438 struct lttng_consumer_local_data
*ctx
)
440 unsigned long len
, subbuf_size
, padding
;
443 struct lttng_ust_shm_handle
*handle
;
444 struct lttng_ust_lib_ring_buffer
*buf
;
448 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
449 stream
->wait_fd
, stream
->key
);
451 /* We can consume the 1 byte written into the wait_fd by UST */
452 if (!stream
->hangup_flush_done
) {
454 readlen
= read(stream
->wait_fd
, &dummy
, 1);
455 } while (readlen
== -1 && errno
== EINTR
);
463 handle
= stream
->chan
->handle
;
464 /* Get the next subbuffer */
465 err
= ustctl_get_next_subbuf(handle
, buf
);
467 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
469 * This is a debug message even for single-threaded consumer,
470 * because poll() have more relaxed criterions than get subbuf,
471 * so get_subbuf may fail for short race windows where poll()
472 * would issue wakeups.
474 DBG("Reserving sub buffer failed (everything is normal, "
475 "it is due to concurrency)");
478 assert(stream
->output
== LTTNG_EVENT_MMAP
);
479 /* Get the full padded subbuffer size */
480 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
483 /* Get subbuffer data size (without padding) */
484 err
= ustctl_get_subbuf_size(handle
, buf
, &subbuf_size
);
487 /* Make sure we don't get a subbuffer size bigger than the padded */
488 assert(len
>= subbuf_size
);
490 padding
= len
- subbuf_size
;
491 /* write the subbuffer to the tracefile */
492 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
, padding
);
494 * The mmap operation should write subbuf_size amount of data when network
495 * streaming or the full padding (len) size when we are _not_ streaming.
497 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= -1) ||
498 (ret
!= len
&& stream
->net_seq_idx
== -1)) {
500 * Display the error but continue processing to try to release the
503 ERR("Error writing to tracefile "
504 "(ret: %zd != len: %lu != subbuf_size: %lu)",
505 ret
, len
, subbuf_size
);
507 err
= ustctl_put_next_subbuf(handle
, buf
);
513 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
517 /* Opening the tracefile in write mode */
518 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
519 ret
= run_as_open(stream
->path_name
,
520 O_WRONLY
|O_CREAT
|O_TRUNC
,
521 S_IRWXU
|S_IRWXG
|S_IRWXO
,
522 stream
->uid
, stream
->gid
);
524 ERR("Opening %s", stream
->path_name
);
528 stream
->out_fd
= ret
;
531 /* we return 0 to let the library handle the FD internally */