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 <urcu/list.h>
35 #include <common/common.h>
36 #include <common/sessiond-comm/sessiond-comm.h>
37 #include <common/relayd/relayd.h>
38 #include <common/compat/fcntl.h>
39 #include <common/consumer-metadata-cache.h>
40 #include <common/consumer-stream.h>
41 #include <common/consumer-timer.h>
42 #include <common/utils.h>
43 #include <common/index/index.h>
45 #include "ust-consumer.h"
47 extern struct lttng_consumer_global_data consumer_data
;
48 extern int consumer_poll_timeout
;
49 extern volatile int consumer_quit
;
52 * Free channel object and all streams associated with it. This MUST be used
53 * only and only if the channel has _NEVER_ been added to the global channel
56 static void destroy_channel(struct lttng_consumer_channel
*channel
)
58 struct lttng_consumer_stream
*stream
, *stmp
;
62 DBG("UST consumer cleaning stream list");
64 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
66 cds_list_del(&stream
->send_node
);
67 ustctl_destroy_stream(stream
->ustream
);
72 * If a channel is available meaning that was created before the streams
76 lttng_ustconsumer_del_channel(channel
);
82 * Add channel to internal consumer state.
84 * Returns 0 on success or else a negative value.
86 static int add_channel(struct lttng_consumer_channel
*channel
,
87 struct lttng_consumer_local_data
*ctx
)
94 if (ctx
->on_recv_channel
!= NULL
) {
95 ret
= ctx
->on_recv_channel(channel
);
97 ret
= consumer_add_channel(channel
, ctx
);
99 /* Most likely an ENOMEM. */
100 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
104 ret
= consumer_add_channel(channel
, ctx
);
107 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
114 * Allocate and return a consumer channel object.
116 static struct lttng_consumer_channel
*allocate_channel(uint64_t session_id
,
117 const char *pathname
, const char *name
, uid_t uid
, gid_t gid
,
118 uint64_t relayd_id
, uint64_t key
, enum lttng_event_output output
,
119 uint64_t tracefile_size
, uint64_t tracefile_count
,
120 uint64_t session_id_per_pid
, unsigned int monitor
)
125 return consumer_allocate_channel(key
, session_id
, pathname
, name
, uid
,
126 gid
, relayd_id
, output
, tracefile_size
,
127 tracefile_count
, session_id_per_pid
, monitor
);
131 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
132 * error value if applicable is set in it else it is kept untouched.
134 * Return NULL on error else the newly allocated stream object.
136 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
137 struct lttng_consumer_channel
*channel
,
138 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
141 struct lttng_consumer_stream
*stream
= NULL
;
146 stream
= consumer_allocate_stream(channel
->key
,
148 LTTNG_CONSUMER_ACTIVE_STREAM
,
158 if (stream
== NULL
) {
162 * We could not find the channel. Can happen if cpu hotplug
163 * happens while tearing down.
165 DBG3("Could not find channel");
170 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
176 stream
->chan
= channel
;
180 *_alloc_ret
= alloc_ret
;
186 * Send the given stream pointer to the corresponding thread.
188 * Returns 0 on success else a negative value.
190 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
191 struct lttng_consumer_local_data
*ctx
)
194 struct lttng_pipe
*stream_pipe
;
196 /* Get the right pipe where the stream will be sent. */
197 if (stream
->metadata_flag
) {
198 ret
= consumer_add_metadata_stream(stream
);
200 ERR("Consumer add metadata stream %" PRIu64
" failed.",
204 stream_pipe
= ctx
->consumer_metadata_pipe
;
206 ret
= consumer_add_data_stream(stream
);
208 ERR("Consumer add stream %" PRIu64
" failed.",
212 stream_pipe
= ctx
->consumer_data_pipe
;
216 * From this point on, the stream's ownership has been moved away from
217 * the channel and becomes globally visible.
219 stream
->globally_visible
= 1;
221 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
223 ERR("Consumer write %s stream to pipe %d",
224 stream
->metadata_flag
? "metadata" : "data",
225 lttng_pipe_get_writefd(stream_pipe
));
226 if (stream
->metadata_flag
) {
227 consumer_del_stream_for_metadata(stream
);
229 consumer_del_stream_for_data(stream
);
237 * Create streams for the given channel using liblttng-ust-ctl.
239 * Return 0 on success else a negative value.
241 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
242 struct lttng_consumer_local_data
*ctx
)
245 struct ustctl_consumer_stream
*ustream
;
246 struct lttng_consumer_stream
*stream
;
252 * While a stream is available from ustctl. When NULL is returned, we've
253 * reached the end of the possible stream for the channel.
255 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
257 int ust_metadata_pipe
[2];
259 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
260 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
262 ERR("Create ust metadata poll pipe");
265 wait_fd
= ust_metadata_pipe
[0];
267 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
270 /* Allocate consumer stream object. */
271 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
275 stream
->ustream
= ustream
;
277 * Store it so we can save multiple function calls afterwards since
278 * this value is used heavily in the stream threads. This is UST
279 * specific so this is why it's done after allocation.
281 stream
->wait_fd
= wait_fd
;
284 * Increment channel refcount since the channel reference has now been
285 * assigned in the allocation process above.
287 if (stream
->chan
->monitor
) {
288 uatomic_inc(&stream
->chan
->refcount
);
292 * Order is important this is why a list is used. On error, the caller
293 * should clean this list.
295 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
297 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
298 &stream
->max_sb_size
);
300 ERR("ustctl_get_max_subbuf_size failed for stream %s",
305 /* Do actions once stream has been received. */
306 if (ctx
->on_recv_stream
) {
307 ret
= ctx
->on_recv_stream(stream
);
313 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
314 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
316 /* Set next CPU stream. */
317 channel
->streams
.count
= ++cpu
;
319 /* Keep stream reference when creating metadata. */
320 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
321 channel
->metadata_stream
= stream
;
322 stream
->ust_metadata_poll_pipe
[0] = ust_metadata_pipe
[0];
323 stream
->ust_metadata_poll_pipe
[1] = ust_metadata_pipe
[1];
335 * Create an UST channel with the given attributes and send it to the session
336 * daemon using the ust ctl API.
338 * Return 0 on success or else a negative value.
340 static int create_ust_channel(struct ustctl_consumer_channel_attr
*attr
,
341 struct ustctl_consumer_channel
**chanp
)
344 struct ustctl_consumer_channel
*channel
;
349 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
350 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
351 "switch_timer_interval: %u, read_timer_interval: %u, "
352 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
353 attr
->num_subbuf
, attr
->switch_timer_interval
,
354 attr
->read_timer_interval
, attr
->output
, attr
->type
);
356 channel
= ustctl_create_channel(attr
);
371 * Send a single given stream to the session daemon using the sock.
373 * Return 0 on success else a negative value.
375 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
382 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
384 /* Send stream to session daemon. */
385 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
395 * Send channel to sessiond.
397 * Return 0 on success or else a negative value.
399 static int send_sessiond_channel(int sock
,
400 struct lttng_consumer_channel
*channel
,
401 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
403 int ret
, ret_code
= LTTNG_OK
;
404 struct lttng_consumer_stream
*stream
;
410 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
412 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
413 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
414 /* Try to send the stream to the relayd if one is available. */
415 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
418 * Flag that the relayd was the problem here probably due to a
419 * communicaton error on the socket.
424 ret_code
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
429 /* Inform sessiond that we are about to send channel and streams. */
430 ret
= consumer_send_status_msg(sock
, ret_code
);
431 if (ret
< 0 || ret_code
!= LTTNG_OK
) {
433 * Either the session daemon is not responding or the relayd died so we
439 /* Send channel to sessiond. */
440 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
445 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
450 /* The channel was sent successfully to the sessiond at this point. */
451 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
452 /* Send stream to session daemon. */
453 ret
= send_sessiond_stream(sock
, stream
);
459 /* Tell sessiond there is no more stream. */
460 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
465 DBG("UST consumer NULL stream sent to sessiond");
470 if (ret_code
!= LTTNG_OK
) {
477 * Creates a channel and streams and add the channel it to the channel internal
478 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
481 * Return 0 on success or else, a negative value is returned and the channel
482 * MUST be destroyed by consumer_del_channel().
484 static int ask_channel(struct lttng_consumer_local_data
*ctx
, int sock
,
485 struct lttng_consumer_channel
*channel
,
486 struct ustctl_consumer_channel_attr
*attr
)
495 * This value is still used by the kernel consumer since for the kernel,
496 * the stream ownership is not IN the consumer so we need to have the
497 * number of left stream that needs to be initialized so we can know when
498 * to delete the channel (see consumer.c).
500 * As for the user space tracer now, the consumer creates and sends the
501 * stream to the session daemon which only sends them to the application
502 * once every stream of a channel is received making this value useless
503 * because we they will be added to the poll thread before the application
504 * receives them. This ensures that a stream can not hang up during
505 * initilization of a channel.
507 channel
->nb_init_stream_left
= 0;
509 /* The reply msg status is handled in the following call. */
510 ret
= create_ust_channel(attr
, &channel
->uchan
);
515 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
518 * For the snapshots (no monitor), we create the metadata streams
519 * on demand, not during the channel creation.
521 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
526 /* Open all streams for this channel. */
527 ret
= create_ust_streams(channel
, ctx
);
537 * Send all stream of a channel to the right thread handling it.
539 * On error, return a negative value else 0 on success.
541 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
542 struct lttng_consumer_local_data
*ctx
)
545 struct lttng_consumer_stream
*stream
, *stmp
;
550 /* Send streams to the corresponding thread. */
551 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
553 /* Sending the stream to the thread. */
554 ret
= send_stream_to_thread(stream
, ctx
);
557 * If we are unable to send the stream to the thread, there is
558 * a big problem so just stop everything.
560 /* Remove node from the channel stream list. */
561 cds_list_del(&stream
->send_node
);
565 /* Remove node from the channel stream list. */
566 cds_list_del(&stream
->send_node
);
575 * Flush channel's streams using the given key to retrieve the channel.
577 * Return 0 on success else an LTTng error code.
579 static int flush_channel(uint64_t chan_key
)
582 struct lttng_consumer_channel
*channel
;
583 struct lttng_consumer_stream
*stream
;
585 struct lttng_ht_iter iter
;
587 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
590 channel
= consumer_find_channel(chan_key
);
592 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
593 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
597 ht
= consumer_data
.stream_per_chan_id_ht
;
599 /* For each stream of the channel id, flush it. */
600 cds_lfht_for_each_entry_duplicate(ht
->ht
,
601 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
602 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
603 ustctl_flush_buffer(stream
->ustream
, 1);
610 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
611 * RCU read side lock MUST be acquired before calling this function.
613 * NOTE: This function does NOT take any channel nor stream lock.
615 * Return 0 on success else LTTng error code.
617 static int _close_metadata(struct lttng_consumer_channel
*channel
)
622 assert(channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
624 if (channel
->switch_timer_enabled
== 1) {
625 DBG("Deleting timer on metadata channel");
626 consumer_timer_switch_stop(channel
);
629 if (channel
->metadata_stream
) {
630 ret
= ustctl_stream_close_wakeup_fd(channel
->metadata_stream
->ustream
);
632 ERR("UST consumer unable to close fd of metadata (ret: %d)", ret
);
633 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
636 if (channel
->monitor
) {
637 /* Close the read-side in consumer_del_metadata_stream */
638 ret
= close(channel
->metadata_stream
->ust_metadata_poll_pipe
[1]);
640 PERROR("Close UST metadata write-side poll pipe");
641 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
650 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
651 * RCU read side lock MUST be acquired before calling this function.
653 * Return 0 on success else an LTTng error code.
655 static int close_metadata(uint64_t chan_key
)
658 struct lttng_consumer_channel
*channel
;
660 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
662 channel
= consumer_find_channel(chan_key
);
665 * This is possible if the metadata thread has issue a delete because
666 * the endpoint point of the stream hung up. There is no way the
667 * session daemon can know about it thus use a DBG instead of an actual
670 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
671 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
675 pthread_mutex_lock(&consumer_data
.lock
);
676 pthread_mutex_lock(&channel
->lock
);
678 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
682 ret
= _close_metadata(channel
);
685 pthread_mutex_unlock(&channel
->lock
);
686 pthread_mutex_unlock(&consumer_data
.lock
);
692 * RCU read side lock MUST be acquired before calling this function.
694 * Return 0 on success else an LTTng error code.
696 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
699 struct lttng_consumer_channel
*metadata
;
701 DBG("UST consumer setup metadata key %" PRIu64
, key
);
703 metadata
= consumer_find_channel(key
);
705 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
706 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
711 * In no monitor mode, the metadata channel has no stream(s) so skip the
712 * ownership transfer to the metadata thread.
714 if (!metadata
->monitor
) {
715 DBG("Metadata channel in no monitor");
721 * Send metadata stream to relayd if one available. Availability is
722 * known if the stream is still in the list of the channel.
724 if (cds_list_empty(&metadata
->streams
.head
)) {
725 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
726 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
727 goto error_no_stream
;
730 /* Send metadata stream to relayd if needed. */
731 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
732 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
735 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
740 ret
= send_streams_to_thread(metadata
, ctx
);
743 * If we are unable to send the stream to the thread, there is
744 * a big problem so just stop everything.
746 ret
= LTTCOMM_CONSUMERD_FATAL
;
749 /* List MUST be empty after or else it could be reused. */
750 assert(cds_list_empty(&metadata
->streams
.head
));
757 * Delete metadata channel on error. At this point, the metadata stream can
758 * NOT be monitored by the metadata thread thus having the guarantee that
759 * the stream is still in the local stream list of the channel. This call
760 * will make sure to clean that list.
762 cds_list_del(&metadata
->metadata_stream
->send_node
);
763 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
770 * Snapshot the whole metadata.
772 * Returns 0 on success, < 0 on error
774 static int snapshot_metadata(uint64_t key
, char *path
, uint64_t relayd_id
,
775 struct lttng_consumer_local_data
*ctx
)
778 struct lttng_consumer_channel
*metadata_channel
;
779 struct lttng_consumer_stream
*metadata_stream
;
784 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
789 metadata_channel
= consumer_find_channel(key
);
790 if (!metadata_channel
) {
791 ERR("UST snapshot metadata channel not found for key %" PRIu64
,
796 assert(!metadata_channel
->monitor
);
799 * Ask the sessiond if we have new metadata waiting and update the
800 * consumer metadata cache.
802 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0);
808 * The metadata stream is NOT created in no monitor mode when the channel
809 * is created on a sessiond ask channel command.
811 ret
= create_ust_streams(metadata_channel
, ctx
);
816 metadata_stream
= metadata_channel
->metadata_stream
;
817 assert(metadata_stream
);
819 if (relayd_id
!= (uint64_t) -1ULL) {
820 metadata_stream
->net_seq_idx
= relayd_id
;
821 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
826 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
827 metadata_stream
->chan
->tracefile_size
,
828 metadata_stream
->tracefile_count_current
,
829 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
833 metadata_stream
->out_fd
= ret
;
834 metadata_stream
->tracefile_size_current
= 0;
837 pthread_mutex_lock(&metadata_channel
->metadata_cache
->lock
);
840 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
);
847 pthread_mutex_unlock(&metadata_channel
->metadata_cache
->lock
);
851 * Clean up the stream completly because the next snapshot will use a new
854 cds_list_del(&metadata_stream
->send_node
);
855 consumer_stream_destroy(metadata_stream
, NULL
);
856 metadata_channel
->metadata_stream
= NULL
;
864 * Take a snapshot of all the stream of a channel.
866 * Returns 0 on success, < 0 on error
868 static int snapshot_channel(uint64_t key
, char *path
, uint64_t relayd_id
,
869 uint64_t max_stream_size
, struct lttng_consumer_local_data
*ctx
)
872 unsigned use_relayd
= 0;
873 unsigned long consumed_pos
, produced_pos
;
874 struct lttng_consumer_channel
*channel
;
875 struct lttng_consumer_stream
*stream
;
882 if (relayd_id
!= (uint64_t) -1ULL) {
886 channel
= consumer_find_channel(key
);
888 ERR("UST snapshot channel not found for key %" PRIu64
, key
);
892 assert(!channel
->monitor
);
893 DBG("UST consumer snapshot channel %" PRIu64
, key
);
895 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
896 /* Lock stream because we are about to change its state. */
897 pthread_mutex_lock(&stream
->lock
);
898 stream
->net_seq_idx
= relayd_id
;
901 ret
= consumer_send_relayd_stream(stream
, path
);
906 ret
= utils_create_stream_file(path
, stream
->name
,
907 stream
->chan
->tracefile_size
,
908 stream
->tracefile_count_current
,
909 stream
->uid
, stream
->gid
, NULL
);
913 stream
->out_fd
= ret
;
914 stream
->tracefile_size_current
= 0;
916 DBG("UST consumer snapshot stream %s/%s (%" PRIu64
")", path
,
917 stream
->name
, stream
->key
);
920 ustctl_flush_buffer(stream
->ustream
, 1);
922 ret
= lttng_ustconsumer_take_snapshot(stream
);
924 ERR("Taking UST snapshot");
928 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
930 ERR("Produced UST snapshot position");
934 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
936 ERR("Consumerd UST snapshot position");
941 * The original value is sent back if max stream size is larger than
942 * the possible size of the snapshot. Also, we asume that the session
943 * daemon should never send a maximum stream size that is lower than
946 consumed_pos
= consumer_get_consumed_maxsize(consumed_pos
,
947 produced_pos
, max_stream_size
);
949 while (consumed_pos
< produced_pos
) {
951 unsigned long len
, padded_len
;
953 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
955 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
957 if (ret
!= -EAGAIN
) {
958 PERROR("ustctl_get_subbuf snapshot");
959 goto error_close_stream
;
961 DBG("UST consumer get subbuf failed. Skipping it.");
962 consumed_pos
+= stream
->max_sb_size
;
966 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
968 ERR("Snapshot ustctl_get_subbuf_size");
969 goto error_put_subbuf
;
972 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
974 ERR("Snapshot ustctl_get_padded_subbuf_size");
975 goto error_put_subbuf
;
978 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
979 padded_len
- len
, NULL
);
981 if (read_len
!= len
) {
983 goto error_put_subbuf
;
986 if (read_len
!= padded_len
) {
988 goto error_put_subbuf
;
992 ret
= ustctl_put_subbuf(stream
->ustream
);
994 ERR("Snapshot ustctl_put_subbuf");
995 goto error_close_stream
;
997 consumed_pos
+= stream
->max_sb_size
;
1000 /* Simply close the stream so we can use it on the next snapshot. */
1001 consumer_stream_close(stream
);
1002 pthread_mutex_unlock(&stream
->lock
);
1009 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1010 ERR("Snapshot ustctl_put_subbuf");
1013 consumer_stream_close(stream
);
1015 pthread_mutex_unlock(&stream
->lock
);
1022 * Receive the metadata updates from the sessiond.
1024 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1025 uint64_t len
, struct lttng_consumer_channel
*channel
,
1028 int ret
, ret_code
= LTTNG_OK
;
1031 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1033 metadata_str
= zmalloc(len
* sizeof(char));
1034 if (!metadata_str
) {
1035 PERROR("zmalloc metadata string");
1036 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1040 /* Receive metadata string. */
1041 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1043 /* Session daemon is dead so return gracefully. */
1048 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1049 ret
= consumer_metadata_cache_write(channel
, offset
, len
, metadata_str
);
1051 /* Unable to handle metadata. Notify session daemon. */
1052 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1054 * Skip metadata flush on write error since the offset and len might
1055 * not have been updated which could create an infinite loop below when
1056 * waiting for the metadata cache to be flushed.
1058 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1061 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1063 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1064 DBG("Waiting for metadata to be flushed");
1065 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1075 * Receive command from session daemon and process it.
1077 * Return 1 on success else a negative value or 0.
1079 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1080 int sock
, struct pollfd
*consumer_sockpoll
)
1083 enum lttng_error_code ret_code
= LTTNG_OK
;
1084 struct lttcomm_consumer_msg msg
;
1085 struct lttng_consumer_channel
*channel
= NULL
;
1087 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1088 if (ret
!= sizeof(msg
)) {
1089 DBG("Consumer received unexpected message size %zd (expects %zu)",
1092 * The ret value might 0 meaning an orderly shutdown but this is ok
1093 * since the caller handles this.
1096 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1101 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
1103 * Notify the session daemon that the command is completed.
1105 * On transport layer error, the function call will print an error
1106 * message so handling the returned code is a bit useless since we
1107 * return an error code anyway.
1109 (void) consumer_send_status_msg(sock
, ret_code
);
1113 /* relayd needs RCU read-side lock */
1116 switch (msg
.cmd_type
) {
1117 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1119 /* Session daemon status message are handled in the following call. */
1120 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1121 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1122 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
);
1125 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1127 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1128 struct consumer_relayd_sock_pair
*relayd
;
1130 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1132 /* Get relayd reference if exists. */
1133 relayd
= consumer_find_relayd(index
);
1134 if (relayd
== NULL
) {
1135 DBG("Unable to find relayd %" PRIu64
, index
);
1136 ret_code
= LTTNG_ERR_NO_CONSUMER
;
1140 * Each relayd socket pair has a refcount of stream attached to it
1141 * which tells if the relayd is still active or not depending on the
1144 * This will set the destroy flag of the relayd object and destroy it
1145 * if the refcount reaches zero when called.
1147 * The destroy can happen either here or when a stream fd hangs up.
1150 consumer_flag_relayd_for_destroy(relayd
);
1153 goto end_msg_sessiond
;
1155 case LTTNG_CONSUMER_UPDATE_STREAM
:
1160 case LTTNG_CONSUMER_DATA_PENDING
:
1162 int ret
, is_data_pending
;
1163 uint64_t id
= msg
.u
.data_pending
.session_id
;
1165 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1167 is_data_pending
= consumer_data_pending(id
);
1169 /* Send back returned value to session daemon */
1170 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1171 sizeof(is_data_pending
));
1173 DBG("Error when sending the data pending ret code: %d", ret
);
1178 * No need to send back a status message since the data pending
1179 * returned value is the response.
1183 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1186 struct ustctl_consumer_channel_attr attr
;
1188 /* Create a plain object and reserve a channel key. */
1189 channel
= allocate_channel(msg
.u
.ask_channel
.session_id
,
1190 msg
.u
.ask_channel
.pathname
, msg
.u
.ask_channel
.name
,
1191 msg
.u
.ask_channel
.uid
, msg
.u
.ask_channel
.gid
,
1192 msg
.u
.ask_channel
.relayd_id
, msg
.u
.ask_channel
.key
,
1193 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1194 msg
.u
.ask_channel
.tracefile_size
,
1195 msg
.u
.ask_channel
.tracefile_count
,
1196 msg
.u
.ask_channel
.session_id_per_pid
,
1197 msg
.u
.ask_channel
.monitor
);
1199 goto end_channel_error
;
1203 * Assign UST application UID to the channel. This value is ignored for
1204 * per PID buffers. This is specific to UST thus setting this after the
1207 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1209 /* Build channel attributes from received message. */
1210 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1211 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1212 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1213 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1214 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1215 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1216 attr
.output
= msg
.u
.ask_channel
.output
;
1217 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1219 /* Translate and save channel type. */
1220 switch (msg
.u
.ask_channel
.type
) {
1221 case LTTNG_UST_CHAN_PER_CPU
:
1222 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1223 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1225 * Set refcount to 1 for owner. Below, we will
1226 * pass ownership to the
1227 * consumer_thread_channel_poll() thread.
1229 channel
->refcount
= 1;
1231 case LTTNG_UST_CHAN_METADATA
:
1232 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1233 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1240 ret
= ask_channel(ctx
, sock
, channel
, &attr
);
1242 goto end_channel_error
;
1245 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1246 ret
= consumer_metadata_cache_allocate(channel
);
1248 ERR("Allocating metadata cache");
1249 goto end_channel_error
;
1251 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1252 attr
.switch_timer_interval
= 0;
1256 * Add the channel to the internal state AFTER all streams were created
1257 * and successfully sent to session daemon. This way, all streams must
1258 * be ready before this channel is visible to the threads.
1259 * If add_channel succeeds, ownership of the channel is
1260 * passed to consumer_thread_channel_poll().
1262 ret
= add_channel(channel
, ctx
);
1264 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1265 if (channel
->switch_timer_enabled
== 1) {
1266 consumer_timer_switch_stop(channel
);
1268 consumer_metadata_cache_destroy(channel
);
1270 goto end_channel_error
;
1274 * Channel and streams are now created. Inform the session daemon that
1275 * everything went well and should wait to receive the channel and
1276 * streams with ustctl API.
1278 ret
= consumer_send_status_channel(sock
, channel
);
1281 * There is probably a problem on the socket.
1288 case LTTNG_CONSUMER_GET_CHANNEL
:
1290 int ret
, relayd_err
= 0;
1291 uint64_t key
= msg
.u
.get_channel
.key
;
1292 struct lttng_consumer_channel
*channel
;
1294 channel
= consumer_find_channel(key
);
1296 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1297 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1298 goto end_msg_sessiond
;
1301 /* Send everything to sessiond. */
1302 ret
= send_sessiond_channel(sock
, channel
, ctx
, &relayd_err
);
1306 * We were unable to send to the relayd the stream so avoid
1307 * sending back a fatal error to the thread since this is OK
1308 * and the consumer can continue its work. The above call
1309 * has sent the error status message to the sessiond.
1314 * The communicaton was broken hence there is a bad state between
1315 * the consumer and sessiond so stop everything.
1321 * In no monitor mode, the streams ownership is kept inside the channel
1322 * so don't send them to the data thread.
1324 if (!channel
->monitor
) {
1325 goto end_msg_sessiond
;
1328 ret
= send_streams_to_thread(channel
, ctx
);
1331 * If we are unable to send the stream to the thread, there is
1332 * a big problem so just stop everything.
1336 /* List MUST be empty after or else it could be reused. */
1337 assert(cds_list_empty(&channel
->streams
.head
));
1338 goto end_msg_sessiond
;
1340 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1342 uint64_t key
= msg
.u
.destroy_channel
.key
;
1345 * Only called if streams have not been sent to stream
1346 * manager thread. However, channel has been sent to
1347 * channel manager thread.
1349 notify_thread_del_channel(ctx
, key
);
1350 goto end_msg_sessiond
;
1352 case LTTNG_CONSUMER_CLOSE_METADATA
:
1356 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1361 goto end_msg_sessiond
;
1363 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1367 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1372 goto end_msg_sessiond
;
1374 case LTTNG_CONSUMER_PUSH_METADATA
:
1377 uint64_t len
= msg
.u
.push_metadata
.len
;
1378 uint64_t key
= msg
.u
.push_metadata
.key
;
1379 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1380 struct lttng_consumer_channel
*channel
;
1382 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1385 channel
= consumer_find_channel(key
);
1387 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
1388 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1389 goto end_msg_sessiond
;
1392 /* Tell session daemon we are ready to receive the metadata. */
1393 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
1395 /* Somehow, the session daemon is not responding anymore. */
1399 /* Wait for more data. */
1400 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1404 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1407 /* error receiving from sessiond */
1411 goto end_msg_sessiond
;
1414 case LTTNG_CONSUMER_SETUP_METADATA
:
1418 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1422 goto end_msg_sessiond
;
1424 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1426 if (msg
.u
.snapshot_channel
.metadata
) {
1427 ret
= snapshot_metadata(msg
.u
.snapshot_channel
.key
,
1428 msg
.u
.snapshot_channel
.pathname
,
1429 msg
.u
.snapshot_channel
.relayd_id
,
1432 ERR("Snapshot metadata failed");
1433 ret_code
= LTTNG_ERR_UST_META_FAIL
;
1436 ret
= snapshot_channel(msg
.u
.snapshot_channel
.key
,
1437 msg
.u
.snapshot_channel
.pathname
,
1438 msg
.u
.snapshot_channel
.relayd_id
,
1439 msg
.u
.snapshot_channel
.max_stream_size
,
1442 ERR("Snapshot channel failed");
1443 ret_code
= LTTNG_ERR_UST_CHAN_FAIL
;
1447 ret
= consumer_send_status_msg(sock
, ret_code
);
1449 /* Somehow, the session daemon is not responding anymore. */
1462 * Return 1 to indicate success since the 0 value can be a socket
1463 * shutdown during the recv() or send() call.
1469 * The returned value here is not useful since either way we'll return 1 to
1470 * the caller because the session daemon socket management is done
1471 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1473 ret
= consumer_send_status_msg(sock
, ret_code
);
1482 * Free channel here since no one has a reference to it. We don't
1483 * free after that because a stream can store this pointer.
1485 destroy_channel(channel
);
1487 /* We have to send a status channel message indicating an error. */
1488 ret
= consumer_send_status_channel(sock
, NULL
);
1490 /* Stop everything if session daemon can not be notified. */
1497 /* This will issue a consumer stop. */
1502 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1503 * compiled out, we isolate it in this library.
1505 int lttng_ustctl_get_mmap_read_offset(struct lttng_consumer_stream
*stream
,
1509 assert(stream
->ustream
);
1511 return ustctl_get_mmap_read_offset(stream
->ustream
, off
);
1515 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1516 * compiled out, we isolate it in this library.
1518 void *lttng_ustctl_get_mmap_base(struct lttng_consumer_stream
*stream
)
1521 assert(stream
->ustream
);
1523 return ustctl_get_mmap_base(stream
->ustream
);
1527 * Take a snapshot for a specific fd
1529 * Returns 0 on success, < 0 on error
1531 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1534 assert(stream
->ustream
);
1536 return ustctl_snapshot(stream
->ustream
);
1540 * Get the produced position
1542 * Returns 0 on success, < 0 on error
1544 int lttng_ustconsumer_get_produced_snapshot(
1545 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
1548 assert(stream
->ustream
);
1551 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
1555 * Get the consumed position
1557 * Returns 0 on success, < 0 on error
1559 int lttng_ustconsumer_get_consumed_snapshot(
1560 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
1563 assert(stream
->ustream
);
1566 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
1570 * Called when the stream signal the consumer that it has hang up.
1572 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
1575 assert(stream
->ustream
);
1577 ustctl_flush_buffer(stream
->ustream
, 0);
1578 stream
->hangup_flush_done
= 1;
1581 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
1584 assert(chan
->uchan
);
1586 if (chan
->switch_timer_enabled
== 1) {
1587 consumer_timer_switch_stop(chan
);
1589 consumer_metadata_cache_destroy(chan
);
1590 ustctl_destroy_channel(chan
->uchan
);
1593 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
1596 assert(stream
->ustream
);
1598 if (stream
->chan
->switch_timer_enabled
== 1) {
1599 consumer_timer_switch_stop(stream
->chan
);
1601 ustctl_destroy_stream(stream
->ustream
);
1605 * Populate index values of a UST stream. Values are set in big endian order.
1607 * Return 0 on success or else a negative value.
1609 static int get_index_values(struct lttng_packet_index
*index
,
1610 struct ustctl_consumer_stream
*ustream
)
1614 ret
= ustctl_get_timestamp_begin(ustream
, &index
->timestamp_begin
);
1616 PERROR("ustctl_get_timestamp_begin");
1619 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1621 ret
= ustctl_get_timestamp_end(ustream
, &index
->timestamp_end
);
1623 PERROR("ustctl_get_timestamp_end");
1626 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1628 ret
= ustctl_get_events_discarded(ustream
, &index
->events_discarded
);
1630 PERROR("ustctl_get_events_discarded");
1633 index
->events_discarded
= htobe64(index
->events_discarded
);
1635 ret
= ustctl_get_content_size(ustream
, &index
->content_size
);
1637 PERROR("ustctl_get_content_size");
1640 index
->content_size
= htobe64(index
->content_size
);
1642 ret
= ustctl_get_packet_size(ustream
, &index
->packet_size
);
1644 PERROR("ustctl_get_packet_size");
1647 index
->packet_size
= htobe64(index
->packet_size
);
1649 ret
= ustctl_get_stream_id(ustream
, &index
->stream_id
);
1651 PERROR("ustctl_get_stream_id");
1654 index
->stream_id
= htobe64(index
->stream_id
);
1661 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1662 struct lttng_consumer_local_data
*ctx
)
1664 unsigned long len
, subbuf_size
, padding
;
1665 int err
, write_index
= 0;
1668 struct ustctl_consumer_stream
*ustream
;
1669 struct lttng_packet_index index
;
1672 assert(stream
->ustream
);
1675 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream
->wait_fd
,
1678 /* Ease our life for what's next. */
1679 ustream
= stream
->ustream
;
1681 /* Indicate that for this stream we have to write the index. */
1682 if (stream
->index_fd
>= 0) {
1686 /* We can consume the 1 byte written into the wait_fd by UST */
1687 if (stream
->monitor
&& !stream
->hangup_flush_done
) {
1691 readlen
= read(stream
->wait_fd
, &dummy
, 1);
1692 } while (readlen
== -1 && errno
== EINTR
);
1693 if (readlen
== -1 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
1700 /* Get the next subbuffer */
1701 err
= ustctl_get_next_subbuf(ustream
);
1704 * Populate metadata info if the existing info has
1705 * already been read.
1707 if (stream
->metadata_flag
) {
1710 if (stream
->chan
->metadata_cache
->contiguous
1711 == stream
->ust_metadata_pushed
) {
1716 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
1717 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
1718 stream
->chan
->metadata_cache
->contiguous
1719 - stream
->ust_metadata_pushed
);
1720 assert(write_len
!= 0);
1721 if (write_len
< 0) {
1722 ERR("Writing one metadata packet");
1726 stream
->ust_metadata_pushed
+= write_len
;
1727 ustctl_flush_buffer(stream
->ustream
, 1);
1731 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
1733 * This is a debug message even for single-threaded consumer,
1734 * because poll() have more relaxed criterions than get subbuf,
1735 * so get_subbuf may fail for short race windows where poll()
1736 * would issue wakeups.
1738 DBG("Reserving sub buffer failed (everything is normal, "
1739 "it is due to concurrency) [ret: %d]", err
);
1742 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
1744 if (!stream
->metadata_flag
&& write_index
) {
1745 index
.offset
= htobe64(stream
->out_fd_offset
);
1746 ret
= get_index_values(&index
, ustream
);
1752 /* Get the full padded subbuffer size */
1753 err
= ustctl_get_padded_subbuf_size(ustream
, &len
);
1756 /* Get subbuffer data size (without padding) */
1757 err
= ustctl_get_subbuf_size(ustream
, &subbuf_size
);
1760 /* Make sure we don't get a subbuffer size bigger than the padded */
1761 assert(len
>= subbuf_size
);
1763 padding
= len
- subbuf_size
;
1764 /* write the subbuffer to the tracefile */
1765 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
, padding
, &index
);
1767 * The mmap operation should write subbuf_size amount of data when network
1768 * streaming or the full padding (len) size when we are _not_ streaming.
1770 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1771 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1773 * Display the error but continue processing to try to release the
1774 * subbuffer. This is a DBG statement since any unexpected kill or
1775 * signal, the application gets unregistered, relayd gets closed or
1776 * anything that affects the buffer lifetime will trigger this error.
1777 * So, for the sake of the user, don't print this error since it can
1778 * happen and it is OK with the code flow.
1780 DBG("Error writing to tracefile "
1781 "(ret: %ld != len: %lu != subbuf_size: %lu)",
1782 ret
, len
, subbuf_size
);
1785 err
= ustctl_put_next_subbuf(ustream
);
1788 /* Write index if needed. */
1790 err
= index_write(stream
->index_fd
, &index
, sizeof(index
));
1802 * Called when a stream is created.
1804 * Return 0 on success or else a negative value.
1806 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1812 /* Don't create anything if this is set for streaming. */
1813 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1814 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1815 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1816 stream
->uid
, stream
->gid
, NULL
);
1820 stream
->out_fd
= ret
;
1821 stream
->tracefile_size_current
= 0;
1823 if (!stream
->metadata_flag
) {
1824 ret
= index_create_file(stream
->chan
->pathname
,
1825 stream
->name
, stream
->uid
, stream
->gid
,
1826 stream
->chan
->tracefile_size
,
1827 stream
->tracefile_count_current
);
1831 stream
->index_fd
= ret
;
1841 * Check if data is still being extracted from the buffers for a specific
1842 * stream. Consumer data lock MUST be acquired before calling this function
1843 * and the stream lock.
1845 * Return 1 if the traced data are still getting read else 0 meaning that the
1846 * data is available for trace viewer reading.
1848 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1853 assert(stream
->ustream
);
1855 DBG("UST consumer checking data pending");
1857 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1862 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
1863 uint64_t contiguous
, pushed
;
1865 /* Ease our life a bit. */
1866 contiguous
= stream
->chan
->metadata_cache
->contiguous
;
1867 pushed
= stream
->ust_metadata_pushed
;
1870 * We can simply check whether all contiguously available data
1871 * has been pushed to the ring buffer, since the push operation
1872 * is performed within get_next_subbuf(), and because both
1873 * get_next_subbuf() and put_next_subbuf() are issued atomically
1874 * thanks to the stream lock within
1875 * lttng_ustconsumer_read_subbuffer(). This basically means that
1876 * whetnever ust_metadata_pushed is incremented, the associated
1877 * metadata has been consumed from the metadata stream.
1879 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
1880 contiguous
, pushed
);
1881 assert(((int64_t) contiguous
- pushed
) >= 0);
1882 if ((contiguous
!= pushed
) ||
1883 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
1884 ret
= 1; /* Data is pending */
1888 ret
= ustctl_get_next_subbuf(stream
->ustream
);
1891 * There is still data so let's put back this
1894 ret
= ustctl_put_subbuf(stream
->ustream
);
1896 ret
= 1; /* Data is pending */
1901 /* Data is NOT pending so ready to be read. */
1909 * Close every metadata stream wait fd of the metadata hash table. This
1910 * function MUST be used very carefully so not to run into a race between the
1911 * metadata thread handling streams and this function closing their wait fd.
1913 * For UST, this is used when the session daemon hangs up. Its the metadata
1914 * producer so calling this is safe because we are assured that no state change
1915 * can occur in the metadata thread for the streams in the hash table.
1917 void lttng_ustconsumer_close_metadata(struct lttng_ht
*metadata_ht
)
1919 struct lttng_ht_iter iter
;
1920 struct lttng_consumer_stream
*stream
;
1922 assert(metadata_ht
);
1923 assert(metadata_ht
->ht
);
1925 DBG("UST consumer closing all metadata streams");
1928 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
1930 pthread_mutex_lock(&stream
->chan
->lock
);
1932 * Whatever returned value, we must continue to try to close everything
1935 (void) _close_metadata(stream
->chan
);
1936 DBG("Metadata wait fd %d and poll pipe fd %d closed", stream
->wait_fd
,
1937 stream
->ust_metadata_poll_pipe
[1]);
1938 pthread_mutex_unlock(&stream
->chan
->lock
);
1944 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
1948 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
1950 ERR("Unable to close wakeup fd");
1955 * Please refer to consumer-timer.c before adding any lock within this
1956 * function or any of its callees. Timers have a very strict locking
1957 * semantic with respect to teardown. Failure to respect this semantic
1958 * introduces deadlocks.
1960 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
1961 struct lttng_consumer_channel
*channel
, int timer
)
1963 struct lttcomm_metadata_request_msg request
;
1964 struct lttcomm_consumer_msg msg
;
1965 enum lttng_error_code ret_code
= LTTNG_OK
;
1966 uint64_t len
, key
, offset
;
1970 assert(channel
->metadata_cache
);
1972 /* send the metadata request to sessiond */
1973 switch (consumer_data
.type
) {
1974 case LTTNG_CONSUMER64_UST
:
1975 request
.bits_per_long
= 64;
1977 case LTTNG_CONSUMER32_UST
:
1978 request
.bits_per_long
= 32;
1981 request
.bits_per_long
= 0;
1985 request
.session_id
= channel
->session_id
;
1986 request
.session_id_per_pid
= channel
->session_id_per_pid
;
1988 * Request the application UID here so the metadata of that application can
1989 * be sent back. The channel UID corresponds to the user UID of the session
1990 * used for the rights on the stream file(s).
1992 request
.uid
= channel
->ust_app_uid
;
1993 request
.key
= channel
->key
;
1995 DBG("Sending metadata request to sessiond, session id %" PRIu64
1996 ", per-pid %" PRIu64
", app UID %u and channek key %" PRIu64
,
1997 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
2000 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
2001 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
2004 ERR("Asking metadata to sessiond");
2008 /* Receive the metadata from sessiond */
2009 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
2011 if (ret
!= sizeof(msg
)) {
2012 DBG("Consumer received unexpected message size %d (expects %zu)",
2014 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
2016 * The ret value might 0 meaning an orderly shutdown but this is ok
2017 * since the caller handles this.
2022 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
2023 /* No registry found */
2024 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
2028 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
2029 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
2034 len
= msg
.u
.push_metadata
.len
;
2035 key
= msg
.u
.push_metadata
.key
;
2036 offset
= msg
.u
.push_metadata
.target_offset
;
2038 assert(key
== channel
->key
);
2040 DBG("No new metadata to receive for key %" PRIu64
, key
);
2043 /* Tell session daemon we are ready to receive the metadata. */
2044 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
2046 if (ret
< 0 || len
== 0) {
2048 * Somehow, the session daemon is not responding anymore or there is
2049 * nothing to receive.
2054 ret_code
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
2055 key
, offset
, len
, channel
, timer
);
2056 if (ret_code
>= 0) {
2058 * Only send the status msg if the sessiond is alive meaning a positive
2061 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret_code
);
2066 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);