2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <lttng/ust-ctl.h>
28 #include <sys/socket.h>
30 #include <sys/types.h>
33 #include <urcu/list.h>
36 #include <bin/lttng-consumerd/health-consumerd.h>
37 #include <common/common.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/relayd/relayd.h>
40 #include <common/compat/fcntl.h>
41 #include <common/compat/endian.h>
42 #include <common/consumer/consumer-metadata-cache.h>
43 #include <common/consumer/consumer-stream.h>
44 #include <common/consumer/consumer-timer.h>
45 #include <common/utils.h>
46 #include <common/index/index.h>
48 #include "ust-consumer.h"
50 #define INT_MAX_STR_LEN 12 /* includes \0 */
52 extern struct lttng_consumer_global_data consumer_data
;
53 extern int consumer_poll_timeout
;
56 * Free channel object and all streams associated with it. This MUST be used
57 * only and only if the channel has _NEVER_ been added to the global channel
60 static void destroy_channel(struct lttng_consumer_channel
*channel
)
62 struct lttng_consumer_stream
*stream
, *stmp
;
66 DBG("UST consumer cleaning stream list");
68 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
73 cds_list_del(&stream
->send_node
);
74 ustctl_destroy_stream(stream
->ustream
);
79 * If a channel is available meaning that was created before the streams
83 lttng_ustconsumer_del_channel(channel
);
84 lttng_ustconsumer_free_channel(channel
);
90 * Add channel to internal consumer state.
92 * Returns 0 on success or else a negative value.
94 static int add_channel(struct lttng_consumer_channel
*channel
,
95 struct lttng_consumer_local_data
*ctx
)
102 if (ctx
->on_recv_channel
!= NULL
) {
103 ret
= ctx
->on_recv_channel(channel
);
105 ret
= consumer_add_channel(channel
, ctx
);
106 } else if (ret
< 0) {
107 /* Most likely an ENOMEM. */
108 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
112 ret
= consumer_add_channel(channel
, ctx
);
115 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
122 * Allocate and return a consumer channel object.
124 static struct lttng_consumer_channel
*allocate_channel(uint64_t session_id
,
125 const char *pathname
, const char *name
, uid_t uid
, gid_t gid
,
126 uint64_t relayd_id
, uint64_t key
, enum lttng_event_output output
,
127 uint64_t tracefile_size
, uint64_t tracefile_count
,
128 uint64_t session_id_per_pid
, unsigned int monitor
,
129 unsigned int live_timer_interval
,
130 const char *root_shm_path
, const char *shm_path
)
135 return consumer_allocate_channel(key
, session_id
, pathname
, name
, uid
,
136 gid
, relayd_id
, output
, tracefile_size
,
137 tracefile_count
, session_id_per_pid
, monitor
,
138 live_timer_interval
, root_shm_path
, shm_path
);
142 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
143 * error value if applicable is set in it else it is kept untouched.
145 * Return NULL on error else the newly allocated stream object.
147 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
148 struct lttng_consumer_channel
*channel
,
149 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
152 struct lttng_consumer_stream
*stream
= NULL
;
157 stream
= consumer_allocate_stream(channel
->key
,
159 LTTNG_CONSUMER_ACTIVE_STREAM
,
169 if (stream
== NULL
) {
173 * We could not find the channel. Can happen if cpu hotplug
174 * happens while tearing down.
176 DBG3("Could not find channel");
181 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
187 stream
->chan
= channel
;
191 *_alloc_ret
= alloc_ret
;
197 * Send the given stream pointer to the corresponding thread.
199 * Returns 0 on success else a negative value.
201 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
202 struct lttng_consumer_local_data
*ctx
)
205 struct lttng_pipe
*stream_pipe
;
207 /* Get the right pipe where the stream will be sent. */
208 if (stream
->metadata_flag
) {
209 ret
= consumer_add_metadata_stream(stream
);
211 ERR("Consumer add metadata stream %" PRIu64
" failed.",
215 stream_pipe
= ctx
->consumer_metadata_pipe
;
217 ret
= consumer_add_data_stream(stream
);
219 ERR("Consumer add stream %" PRIu64
" failed.",
223 stream_pipe
= ctx
->consumer_data_pipe
;
227 * From this point on, the stream's ownership has been moved away from
228 * the channel and it becomes globally visible. Hence, remove it from
229 * the local stream list to prevent the stream from being both local and
232 stream
->globally_visible
= 1;
233 cds_list_del(&stream
->send_node
);
235 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
237 ERR("Consumer write %s stream to pipe %d",
238 stream
->metadata_flag
? "metadata" : "data",
239 lttng_pipe_get_writefd(stream_pipe
));
240 if (stream
->metadata_flag
) {
241 consumer_del_stream_for_metadata(stream
);
243 consumer_del_stream_for_data(stream
);
253 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
255 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
258 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
259 stream_shm_path
[PATH_MAX
- 1] = '\0';
260 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
265 strncat(stream_shm_path
, cpu_nr
,
266 PATH_MAX
- strlen(stream_shm_path
) - 1);
273 * Create streams for the given channel using liblttng-ust-ctl.
275 * Return 0 on success else a negative value.
277 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
278 struct lttng_consumer_local_data
*ctx
)
281 struct ustctl_consumer_stream
*ustream
;
282 struct lttng_consumer_stream
*stream
;
288 * While a stream is available from ustctl. When NULL is returned, we've
289 * reached the end of the possible stream for the channel.
291 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
293 int ust_metadata_pipe
[2];
295 health_code_update();
297 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
298 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
300 ERR("Create ust metadata poll pipe");
303 wait_fd
= ust_metadata_pipe
[0];
305 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
308 /* Allocate consumer stream object. */
309 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
313 stream
->ustream
= ustream
;
315 * Store it so we can save multiple function calls afterwards since
316 * this value is used heavily in the stream threads. This is UST
317 * specific so this is why it's done after allocation.
319 stream
->wait_fd
= wait_fd
;
322 * Increment channel refcount since the channel reference has now been
323 * assigned in the allocation process above.
325 if (stream
->chan
->monitor
) {
326 uatomic_inc(&stream
->chan
->refcount
);
330 * Order is important this is why a list is used. On error, the caller
331 * should clean this list.
333 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
335 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
336 &stream
->max_sb_size
);
338 ERR("ustctl_get_max_subbuf_size failed for stream %s",
343 /* Do actions once stream has been received. */
344 if (ctx
->on_recv_stream
) {
345 ret
= ctx
->on_recv_stream(stream
);
351 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
352 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
354 /* Set next CPU stream. */
355 channel
->streams
.count
= ++cpu
;
357 /* Keep stream reference when creating metadata. */
358 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
359 channel
->metadata_stream
= stream
;
360 if (channel
->monitor
) {
361 /* Set metadata poll pipe if we created one */
362 memcpy(stream
->ust_metadata_poll_pipe
,
364 sizeof(ust_metadata_pipe
));
377 * create_posix_shm is never called concurrently within a process.
380 int create_posix_shm(void)
382 char tmp_name
[NAME_MAX
];
385 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
391 * Allocate shm, and immediately unlink its shm oject, keeping
392 * only the file descriptor as a reference to the object.
393 * We specifically do _not_ use the / at the beginning of the
394 * pathname so that some OS implementations can keep it local to
395 * the process (POSIX leaves this implementation-defined).
397 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
402 ret
= shm_unlink(tmp_name
);
403 if (ret
< 0 && errno
!= ENOENT
) {
404 PERROR("shm_unlink");
405 goto error_shm_release
;
418 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
,
419 struct ustctl_consumer_channel_attr
*attr
,
422 char shm_path
[PATH_MAX
];
425 if (!channel
->shm_path
[0]) {
426 return create_posix_shm();
428 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
432 return run_as_open(shm_path
,
433 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
434 channel
->uid
, channel
->gid
);
441 * Create an UST channel with the given attributes and send it to the session
442 * daemon using the ust ctl API.
444 * Return 0 on success or else a negative value.
446 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
447 struct ustctl_consumer_channel_attr
*attr
,
448 struct ustctl_consumer_channel
**ust_chanp
)
450 int ret
, nr_stream_fds
, i
, j
;
452 struct ustctl_consumer_channel
*ust_channel
;
458 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
459 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
460 "switch_timer_interval: %u, read_timer_interval: %u, "
461 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
462 attr
->num_subbuf
, attr
->switch_timer_interval
,
463 attr
->read_timer_interval
, attr
->output
, attr
->type
);
465 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
468 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
469 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
474 for (i
= 0; i
< nr_stream_fds
; i
++) {
475 stream_fds
[i
] = open_ust_stream_fd(channel
, attr
, i
);
476 if (stream_fds
[i
] < 0) {
481 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
486 channel
->nr_stream_fds
= nr_stream_fds
;
487 channel
->stream_fds
= stream_fds
;
488 *ust_chanp
= ust_channel
;
494 for (j
= i
- 1; j
>= 0; j
--) {
497 closeret
= close(stream_fds
[j
]);
501 if (channel
->shm_path
[0]) {
502 char shm_path
[PATH_MAX
];
504 closeret
= get_stream_shm_path(shm_path
,
505 channel
->shm_path
, j
);
507 ERR("Cannot get stream shm path");
509 closeret
= run_as_unlink(shm_path
,
510 channel
->uid
, channel
->gid
);
512 PERROR("unlink %s", shm_path
);
516 /* Try to rmdir all directories under shm_path root. */
517 if (channel
->root_shm_path
[0]) {
518 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
519 channel
->uid
, channel
->gid
);
527 * Send a single given stream to the session daemon using the sock.
529 * Return 0 on success else a negative value.
531 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
538 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
540 /* Send stream to session daemon. */
541 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
551 * Send channel to sessiond.
553 * Return 0 on success or else a negative value.
555 static int send_sessiond_channel(int sock
,
556 struct lttng_consumer_channel
*channel
,
557 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
559 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
560 struct lttng_consumer_stream
*stream
;
561 uint64_t net_seq_idx
= -1ULL;
567 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
569 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
570 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
572 health_code_update();
574 /* Try to send the stream to the relayd if one is available. */
575 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
578 * Flag that the relayd was the problem here probably due to a
579 * communicaton error on the socket.
584 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
586 if (net_seq_idx
== -1ULL) {
587 net_seq_idx
= stream
->net_seq_idx
;
592 /* Inform sessiond that we are about to send channel and streams. */
593 ret
= consumer_send_status_msg(sock
, ret_code
);
594 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
596 * Either the session daemon is not responding or the relayd died so we
602 /* Send channel to sessiond. */
603 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
608 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
613 /* The channel was sent successfully to the sessiond at this point. */
614 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
616 health_code_update();
618 /* Send stream to session daemon. */
619 ret
= send_sessiond_stream(sock
, stream
);
625 /* Tell sessiond there is no more stream. */
626 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
631 DBG("UST consumer NULL stream sent to sessiond");
636 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
643 * Creates a channel and streams and add the channel it to the channel internal
644 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
647 * Return 0 on success or else, a negative value is returned and the channel
648 * MUST be destroyed by consumer_del_channel().
650 static int ask_channel(struct lttng_consumer_local_data
*ctx
, int sock
,
651 struct lttng_consumer_channel
*channel
,
652 struct ustctl_consumer_channel_attr
*attr
)
661 * This value is still used by the kernel consumer since for the kernel,
662 * the stream ownership is not IN the consumer so we need to have the
663 * number of left stream that needs to be initialized so we can know when
664 * to delete the channel (see consumer.c).
666 * As for the user space tracer now, the consumer creates and sends the
667 * stream to the session daemon which only sends them to the application
668 * once every stream of a channel is received making this value useless
669 * because we they will be added to the poll thread before the application
670 * receives them. This ensures that a stream can not hang up during
671 * initilization of a channel.
673 channel
->nb_init_stream_left
= 0;
675 /* The reply msg status is handled in the following call. */
676 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
681 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
684 * For the snapshots (no monitor), we create the metadata streams
685 * on demand, not during the channel creation.
687 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
692 /* Open all streams for this channel. */
693 ret
= create_ust_streams(channel
, ctx
);
703 * Send all stream of a channel to the right thread handling it.
705 * On error, return a negative value else 0 on success.
707 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
708 struct lttng_consumer_local_data
*ctx
)
711 struct lttng_consumer_stream
*stream
, *stmp
;
716 /* Send streams to the corresponding thread. */
717 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
720 health_code_update();
722 /* Sending the stream to the thread. */
723 ret
= send_stream_to_thread(stream
, ctx
);
726 * If we are unable to send the stream to the thread, there is
727 * a big problem so just stop everything.
738 * Flush channel's streams using the given key to retrieve the channel.
740 * Return 0 on success else an LTTng error code.
742 static int flush_channel(uint64_t chan_key
)
745 struct lttng_consumer_channel
*channel
;
746 struct lttng_consumer_stream
*stream
;
748 struct lttng_ht_iter iter
;
750 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
753 channel
= consumer_find_channel(chan_key
);
755 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
756 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
760 ht
= consumer_data
.stream_per_chan_id_ht
;
762 /* For each stream of the channel id, flush it. */
763 cds_lfht_for_each_entry_duplicate(ht
->ht
,
764 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
765 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
767 health_code_update();
769 pthread_mutex_lock(&stream
->lock
);
770 if (!stream
->quiescent
) {
771 ustctl_flush_buffer(stream
->ustream
, 0);
772 stream
->quiescent
= true;
774 pthread_mutex_unlock(&stream
->lock
);
782 * Clear quiescent state from channel's streams using the given key to
783 * retrieve the channel.
785 * Return 0 on success else an LTTng error code.
787 static int clear_quiescent_channel(uint64_t chan_key
)
790 struct lttng_consumer_channel
*channel
;
791 struct lttng_consumer_stream
*stream
;
793 struct lttng_ht_iter iter
;
795 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
798 channel
= consumer_find_channel(chan_key
);
800 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
801 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
805 ht
= consumer_data
.stream_per_chan_id_ht
;
807 /* For each stream of the channel id, clear quiescent state. */
808 cds_lfht_for_each_entry_duplicate(ht
->ht
,
809 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
810 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
812 health_code_update();
814 pthread_mutex_lock(&stream
->lock
);
815 stream
->quiescent
= false;
816 pthread_mutex_unlock(&stream
->lock
);
824 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
825 * RCU read side lock MUST be acquired before calling this function.
827 * Return 0 on success else an LTTng error code.
829 static int close_metadata(uint64_t chan_key
)
832 struct lttng_consumer_channel
*channel
;
834 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
836 channel
= consumer_find_channel(chan_key
);
839 * This is possible if the metadata thread has issue a delete because
840 * the endpoint point of the stream hung up. There is no way the
841 * session daemon can know about it thus use a DBG instead of an actual
844 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
845 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
849 pthread_mutex_lock(&consumer_data
.lock
);
850 pthread_mutex_lock(&channel
->lock
);
852 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
856 lttng_ustconsumer_close_metadata(channel
);
859 pthread_mutex_unlock(&channel
->lock
);
860 pthread_mutex_unlock(&consumer_data
.lock
);
866 * RCU read side lock MUST be acquired before calling this function.
868 * Return 0 on success else an LTTng error code.
870 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
873 struct lttng_consumer_channel
*metadata
;
875 DBG("UST consumer setup metadata key %" PRIu64
, key
);
877 metadata
= consumer_find_channel(key
);
879 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
880 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
885 * In no monitor mode, the metadata channel has no stream(s) so skip the
886 * ownership transfer to the metadata thread.
888 if (!metadata
->monitor
) {
889 DBG("Metadata channel in no monitor");
895 * Send metadata stream to relayd if one available. Availability is
896 * known if the stream is still in the list of the channel.
898 if (cds_list_empty(&metadata
->streams
.head
)) {
899 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
900 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
901 goto error_no_stream
;
904 /* Send metadata stream to relayd if needed. */
905 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
906 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
909 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
912 ret
= consumer_send_relayd_streams_sent(
913 metadata
->metadata_stream
->net_seq_idx
);
915 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
921 * Ownership of metadata stream is passed along. Freeing is handled by
924 ret
= send_streams_to_thread(metadata
, ctx
);
927 * If we are unable to send the stream to the thread, there is
928 * a big problem so just stop everything.
930 ret
= LTTCOMM_CONSUMERD_FATAL
;
931 goto send_streams_error
;
933 /* List MUST be empty after or else it could be reused. */
934 assert(cds_list_empty(&metadata
->streams
.head
));
941 * Delete metadata channel on error. At this point, the metadata stream can
942 * NOT be monitored by the metadata thread thus having the guarantee that
943 * the stream is still in the local stream list of the channel. This call
944 * will make sure to clean that list.
946 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
947 cds_list_del(&metadata
->metadata_stream
->send_node
);
948 metadata
->metadata_stream
= NULL
;
956 * Snapshot the whole metadata.
958 * Returns 0 on success, < 0 on error
960 static int snapshot_metadata(uint64_t key
, char *path
, uint64_t relayd_id
,
961 struct lttng_consumer_local_data
*ctx
)
964 struct lttng_consumer_channel
*metadata_channel
;
965 struct lttng_consumer_stream
*metadata_stream
;
970 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
975 metadata_channel
= consumer_find_channel(key
);
976 if (!metadata_channel
) {
977 ERR("UST snapshot metadata channel not found for key %" PRIu64
,
982 assert(!metadata_channel
->monitor
);
984 health_code_update();
987 * Ask the sessiond if we have new metadata waiting and update the
988 * consumer metadata cache.
990 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
995 health_code_update();
998 * The metadata stream is NOT created in no monitor mode when the channel
999 * is created on a sessiond ask channel command.
1001 ret
= create_ust_streams(metadata_channel
, ctx
);
1006 metadata_stream
= metadata_channel
->metadata_stream
;
1007 assert(metadata_stream
);
1009 if (relayd_id
!= (uint64_t) -1ULL) {
1010 metadata_stream
->net_seq_idx
= relayd_id
;
1011 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1016 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
1017 metadata_stream
->chan
->tracefile_size
,
1018 metadata_stream
->tracefile_count_current
,
1019 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
1023 metadata_stream
->out_fd
= ret
;
1024 metadata_stream
->tracefile_size_current
= 0;
1028 health_code_update();
1030 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
);
1038 * Clean up the stream completly because the next snapshot will use a new
1041 consumer_stream_destroy(metadata_stream
, NULL
);
1042 cds_list_del(&metadata_stream
->send_node
);
1043 metadata_channel
->metadata_stream
= NULL
;
1051 * Take a snapshot of all the stream of a channel.
1053 * Returns 0 on success, < 0 on error
1055 static int snapshot_channel(uint64_t key
, char *path
, uint64_t relayd_id
,
1056 uint64_t nb_packets_per_stream
, struct lttng_consumer_local_data
*ctx
)
1059 unsigned use_relayd
= 0;
1060 unsigned long consumed_pos
, produced_pos
;
1061 struct lttng_consumer_channel
*channel
;
1062 struct lttng_consumer_stream
*stream
;
1069 if (relayd_id
!= (uint64_t) -1ULL) {
1073 channel
= consumer_find_channel(key
);
1075 ERR("UST snapshot channel not found for key %" PRIu64
, key
);
1079 assert(!channel
->monitor
);
1080 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1082 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1083 health_code_update();
1085 /* Lock stream because we are about to change its state. */
1086 pthread_mutex_lock(&stream
->lock
);
1087 stream
->net_seq_idx
= relayd_id
;
1090 ret
= consumer_send_relayd_stream(stream
, path
);
1095 ret
= utils_create_stream_file(path
, stream
->name
,
1096 stream
->chan
->tracefile_size
,
1097 stream
->tracefile_count_current
,
1098 stream
->uid
, stream
->gid
, NULL
);
1102 stream
->out_fd
= ret
;
1103 stream
->tracefile_size_current
= 0;
1105 DBG("UST consumer snapshot stream %s/%s (%" PRIu64
")", path
,
1106 stream
->name
, stream
->key
);
1110 * If tracing is active, we want to perform a "full" buffer flush.
1111 * Else, if quiescent, it has already been done by the prior stop.
1113 if (!stream
->quiescent
) {
1114 ustctl_flush_buffer(stream
->ustream
, 0);
1117 ret
= lttng_ustconsumer_take_snapshot(stream
);
1119 ERR("Taking UST snapshot");
1123 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1125 ERR("Produced UST snapshot position");
1129 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1131 ERR("Consumerd UST snapshot position");
1136 * The original value is sent back if max stream size is larger than
1137 * the possible size of the snapshot. Also, we assume that the session
1138 * daemon should never send a maximum stream size that is lower than
1141 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1142 produced_pos
, nb_packets_per_stream
,
1143 stream
->max_sb_size
);
1145 while (consumed_pos
< produced_pos
) {
1147 unsigned long len
, padded_len
;
1149 health_code_update();
1151 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1153 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1155 if (ret
!= -EAGAIN
) {
1156 PERROR("ustctl_get_subbuf snapshot");
1157 goto error_close_stream
;
1159 DBG("UST consumer get subbuf failed. Skipping it.");
1160 consumed_pos
+= stream
->max_sb_size
;
1161 stream
->chan
->lost_packets
++;
1165 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1167 ERR("Snapshot ustctl_get_subbuf_size");
1168 goto error_put_subbuf
;
1171 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1173 ERR("Snapshot ustctl_get_padded_subbuf_size");
1174 goto error_put_subbuf
;
1177 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
1178 padded_len
- len
, NULL
);
1180 if (read_len
!= len
) {
1182 goto error_put_subbuf
;
1185 if (read_len
!= padded_len
) {
1187 goto error_put_subbuf
;
1191 ret
= ustctl_put_subbuf(stream
->ustream
);
1193 ERR("Snapshot ustctl_put_subbuf");
1194 goto error_close_stream
;
1196 consumed_pos
+= stream
->max_sb_size
;
1199 /* Simply close the stream so we can use it on the next snapshot. */
1200 consumer_stream_close(stream
);
1201 pthread_mutex_unlock(&stream
->lock
);
1208 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1209 ERR("Snapshot ustctl_put_subbuf");
1212 consumer_stream_close(stream
);
1214 pthread_mutex_unlock(&stream
->lock
);
1221 * Receive the metadata updates from the sessiond. Supports receiving
1222 * overlapping metadata, but is needs to always belong to a contiguous
1223 * range starting from 0.
1224 * Be careful about the locks held when calling this function: it needs
1225 * the metadata cache flush to concurrently progress in order to
1228 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1229 uint64_t len
, uint64_t version
,
1230 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1232 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1235 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1237 metadata_str
= zmalloc(len
* sizeof(char));
1238 if (!metadata_str
) {
1239 PERROR("zmalloc metadata string");
1240 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1244 health_code_update();
1246 /* Receive metadata string. */
1247 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1249 /* Session daemon is dead so return gracefully. */
1254 health_code_update();
1256 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1257 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1260 /* Unable to handle metadata. Notify session daemon. */
1261 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1263 * Skip metadata flush on write error since the offset and len might
1264 * not have been updated which could create an infinite loop below when
1265 * waiting for the metadata cache to be flushed.
1267 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1270 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1275 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1276 DBG("Waiting for metadata to be flushed");
1278 health_code_update();
1280 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1290 * Receive command from session daemon and process it.
1292 * Return 1 on success else a negative value or 0.
1294 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1295 int sock
, struct pollfd
*consumer_sockpoll
)
1298 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1299 struct lttcomm_consumer_msg msg
;
1300 struct lttng_consumer_channel
*channel
= NULL
;
1302 health_code_update();
1304 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1305 if (ret
!= sizeof(msg
)) {
1306 DBG("Consumer received unexpected message size %zd (expects %zu)",
1309 * The ret value might 0 meaning an orderly shutdown but this is ok
1310 * since the caller handles this.
1313 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1319 health_code_update();
1322 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1324 health_code_update();
1326 /* relayd needs RCU read-side lock */
1329 switch (msg
.cmd_type
) {
1330 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1332 /* Session daemon status message are handled in the following call. */
1333 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1334 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1335 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1336 msg
.u
.relayd_sock
.relayd_session_id
);
1339 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1341 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1342 struct consumer_relayd_sock_pair
*relayd
;
1344 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1346 /* Get relayd reference if exists. */
1347 relayd
= consumer_find_relayd(index
);
1348 if (relayd
== NULL
) {
1349 DBG("Unable to find relayd %" PRIu64
, index
);
1350 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1354 * Each relayd socket pair has a refcount of stream attached to it
1355 * which tells if the relayd is still active or not depending on the
1358 * This will set the destroy flag of the relayd object and destroy it
1359 * if the refcount reaches zero when called.
1361 * The destroy can happen either here or when a stream fd hangs up.
1364 consumer_flag_relayd_for_destroy(relayd
);
1367 goto end_msg_sessiond
;
1369 case LTTNG_CONSUMER_UPDATE_STREAM
:
1374 case LTTNG_CONSUMER_DATA_PENDING
:
1376 int ret
, is_data_pending
;
1377 uint64_t id
= msg
.u
.data_pending
.session_id
;
1379 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1381 is_data_pending
= consumer_data_pending(id
);
1383 /* Send back returned value to session daemon */
1384 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1385 sizeof(is_data_pending
));
1387 DBG("Error when sending the data pending ret code: %d", ret
);
1392 * No need to send back a status message since the data pending
1393 * returned value is the response.
1397 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1400 struct ustctl_consumer_channel_attr attr
;
1402 /* Create a plain object and reserve a channel key. */
1403 channel
= allocate_channel(msg
.u
.ask_channel
.session_id
,
1404 msg
.u
.ask_channel
.pathname
, msg
.u
.ask_channel
.name
,
1405 msg
.u
.ask_channel
.uid
, msg
.u
.ask_channel
.gid
,
1406 msg
.u
.ask_channel
.relayd_id
, msg
.u
.ask_channel
.key
,
1407 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1408 msg
.u
.ask_channel
.tracefile_size
,
1409 msg
.u
.ask_channel
.tracefile_count
,
1410 msg
.u
.ask_channel
.session_id_per_pid
,
1411 msg
.u
.ask_channel
.monitor
,
1412 msg
.u
.ask_channel
.live_timer_interval
,
1413 msg
.u
.ask_channel
.root_shm_path
,
1414 msg
.u
.ask_channel
.shm_path
);
1416 goto end_channel_error
;
1420 * Assign UST application UID to the channel. This value is ignored for
1421 * per PID buffers. This is specific to UST thus setting this after the
1424 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1426 /* Build channel attributes from received message. */
1427 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1428 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1429 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1430 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1431 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1432 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1433 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1434 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1436 /* Match channel buffer type to the UST abi. */
1437 switch (msg
.u
.ask_channel
.output
) {
1438 case LTTNG_EVENT_MMAP
:
1440 attr
.output
= LTTNG_UST_MMAP
;
1444 /* Translate and save channel type. */
1445 switch (msg
.u
.ask_channel
.type
) {
1446 case LTTNG_UST_CHAN_PER_CPU
:
1447 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1448 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1450 * Set refcount to 1 for owner. Below, we will
1451 * pass ownership to the
1452 * consumer_thread_channel_poll() thread.
1454 channel
->refcount
= 1;
1456 case LTTNG_UST_CHAN_METADATA
:
1457 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1458 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1465 health_code_update();
1467 ret
= ask_channel(ctx
, sock
, channel
, &attr
);
1469 goto end_channel_error
;
1472 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1473 ret
= consumer_metadata_cache_allocate(channel
);
1475 ERR("Allocating metadata cache");
1476 goto end_channel_error
;
1478 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1479 attr
.switch_timer_interval
= 0;
1481 int monitor_start_ret
;
1483 consumer_timer_live_start(channel
,
1484 msg
.u
.ask_channel
.live_timer_interval
);
1485 monitor_start_ret
= consumer_timer_monitor_start(
1487 msg
.u
.ask_channel
.monitor_timer_interval
);
1488 if (monitor_start_ret
< 0) {
1489 ERR("Starting channel monitoring timer failed");
1490 goto end_channel_error
;
1494 health_code_update();
1497 * Add the channel to the internal state AFTER all streams were created
1498 * and successfully sent to session daemon. This way, all streams must
1499 * be ready before this channel is visible to the threads.
1500 * If add_channel succeeds, ownership of the channel is
1501 * passed to consumer_thread_channel_poll().
1503 ret
= add_channel(channel
, ctx
);
1505 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1506 if (channel
->switch_timer_enabled
== 1) {
1507 consumer_timer_switch_stop(channel
);
1509 consumer_metadata_cache_destroy(channel
);
1511 if (channel
->live_timer_enabled
== 1) {
1512 consumer_timer_live_stop(channel
);
1514 if (channel
->monitor_timer_enabled
== 1) {
1515 consumer_timer_monitor_stop(channel
);
1517 goto end_channel_error
;
1520 health_code_update();
1523 * Channel and streams are now created. Inform the session daemon that
1524 * everything went well and should wait to receive the channel and
1525 * streams with ustctl API.
1527 ret
= consumer_send_status_channel(sock
, channel
);
1530 * There is probably a problem on the socket.
1537 case LTTNG_CONSUMER_GET_CHANNEL
:
1539 int ret
, relayd_err
= 0;
1540 uint64_t key
= msg
.u
.get_channel
.key
;
1541 struct lttng_consumer_channel
*channel
;
1543 channel
= consumer_find_channel(key
);
1545 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1546 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1547 goto end_msg_sessiond
;
1550 health_code_update();
1552 /* Send everything to sessiond. */
1553 ret
= send_sessiond_channel(sock
, channel
, ctx
, &relayd_err
);
1557 * We were unable to send to the relayd the stream so avoid
1558 * sending back a fatal error to the thread since this is OK
1559 * and the consumer can continue its work. The above call
1560 * has sent the error status message to the sessiond.
1565 * The communicaton was broken hence there is a bad state between
1566 * the consumer and sessiond so stop everything.
1571 health_code_update();
1574 * In no monitor mode, the streams ownership is kept inside the channel
1575 * so don't send them to the data thread.
1577 if (!channel
->monitor
) {
1578 goto end_msg_sessiond
;
1581 ret
= send_streams_to_thread(channel
, ctx
);
1584 * If we are unable to send the stream to the thread, there is
1585 * a big problem so just stop everything.
1589 /* List MUST be empty after or else it could be reused. */
1590 assert(cds_list_empty(&channel
->streams
.head
));
1591 goto end_msg_sessiond
;
1593 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1595 uint64_t key
= msg
.u
.destroy_channel
.key
;
1598 * Only called if streams have not been sent to stream
1599 * manager thread. However, channel has been sent to
1600 * channel manager thread.
1602 notify_thread_del_channel(ctx
, key
);
1603 goto end_msg_sessiond
;
1605 case LTTNG_CONSUMER_CLOSE_METADATA
:
1609 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1614 goto end_msg_sessiond
;
1616 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1620 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1625 goto end_msg_sessiond
;
1627 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1631 ret
= clear_quiescent_channel(
1632 msg
.u
.clear_quiescent_channel
.key
);
1637 goto end_msg_sessiond
;
1639 case LTTNG_CONSUMER_PUSH_METADATA
:
1642 uint64_t len
= msg
.u
.push_metadata
.len
;
1643 uint64_t key
= msg
.u
.push_metadata
.key
;
1644 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1645 uint64_t version
= msg
.u
.push_metadata
.version
;
1646 struct lttng_consumer_channel
*channel
;
1648 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1651 channel
= consumer_find_channel(key
);
1654 * This is possible if the metadata creation on the consumer side
1655 * is in flight vis-a-vis a concurrent push metadata from the
1656 * session daemon. Simply return that the channel failed and the
1657 * session daemon will handle that message correctly considering
1658 * that this race is acceptable thus the DBG() statement here.
1660 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1661 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1662 goto end_msg_sessiond
;
1665 health_code_update();
1669 * There is nothing to receive. We have simply
1670 * checked whether the channel can be found.
1672 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1673 goto end_msg_sessiond
;
1676 /* Tell session daemon we are ready to receive the metadata. */
1677 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1679 /* Somehow, the session daemon is not responding anymore. */
1683 health_code_update();
1685 /* Wait for more data. */
1686 health_poll_entry();
1687 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1693 health_code_update();
1695 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1696 len
, version
, channel
, 0, 1);
1698 /* error receiving from sessiond */
1702 goto end_msg_sessiond
;
1705 case LTTNG_CONSUMER_SETUP_METADATA
:
1709 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1713 goto end_msg_sessiond
;
1715 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1717 if (msg
.u
.snapshot_channel
.metadata
) {
1718 ret
= snapshot_metadata(msg
.u
.snapshot_channel
.key
,
1719 msg
.u
.snapshot_channel
.pathname
,
1720 msg
.u
.snapshot_channel
.relayd_id
,
1723 ERR("Snapshot metadata failed");
1724 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1727 ret
= snapshot_channel(msg
.u
.snapshot_channel
.key
,
1728 msg
.u
.snapshot_channel
.pathname
,
1729 msg
.u
.snapshot_channel
.relayd_id
,
1730 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1733 ERR("Snapshot channel failed");
1734 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1738 health_code_update();
1739 ret
= consumer_send_status_msg(sock
, ret_code
);
1741 /* Somehow, the session daemon is not responding anymore. */
1744 health_code_update();
1747 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1750 uint64_t discarded_events
;
1751 struct lttng_ht_iter iter
;
1752 struct lttng_ht
*ht
;
1753 struct lttng_consumer_stream
*stream
;
1754 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1755 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1757 DBG("UST consumer discarded events command for session id %"
1760 pthread_mutex_lock(&consumer_data
.lock
);
1762 ht
= consumer_data
.stream_list_ht
;
1765 * We only need a reference to the channel, but they are not
1766 * directly indexed, so we just use the first matching stream
1767 * to extract the information we need, we default to 0 if not
1768 * found (no events are dropped if the channel is not yet in
1771 discarded_events
= 0;
1772 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1773 ht
->hash_fct(&id
, lttng_ht_seed
),
1775 &iter
.iter
, stream
, node_session_id
.node
) {
1776 if (stream
->chan
->key
== key
) {
1777 discarded_events
= stream
->chan
->discarded_events
;
1781 pthread_mutex_unlock(&consumer_data
.lock
);
1784 DBG("UST consumer discarded events command for session id %"
1785 PRIu64
", channel key %" PRIu64
, id
, key
);
1787 health_code_update();
1789 /* Send back returned value to session daemon */
1790 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1792 PERROR("send discarded events");
1798 case LTTNG_CONSUMER_LOST_PACKETS
:
1801 uint64_t lost_packets
;
1802 struct lttng_ht_iter iter
;
1803 struct lttng_ht
*ht
;
1804 struct lttng_consumer_stream
*stream
;
1805 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1806 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1808 DBG("UST consumer lost packets command for session id %"
1811 pthread_mutex_lock(&consumer_data
.lock
);
1813 ht
= consumer_data
.stream_list_ht
;
1816 * We only need a reference to the channel, but they are not
1817 * directly indexed, so we just use the first matching stream
1818 * to extract the information we need, we default to 0 if not
1819 * found (no packets lost if the channel is not yet in use).
1822 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1823 ht
->hash_fct(&id
, lttng_ht_seed
),
1825 &iter
.iter
, stream
, node_session_id
.node
) {
1826 if (stream
->chan
->key
== key
) {
1827 lost_packets
= stream
->chan
->lost_packets
;
1831 pthread_mutex_unlock(&consumer_data
.lock
);
1834 DBG("UST consumer lost packets command for session id %"
1835 PRIu64
", channel key %" PRIu64
, id
, key
);
1837 health_code_update();
1839 /* Send back returned value to session daemon */
1840 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1841 sizeof(lost_packets
));
1843 PERROR("send lost packets");
1849 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1851 int channel_monitor_pipe
;
1853 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1854 /* Successfully received the command's type. */
1855 ret
= consumer_send_status_msg(sock
, ret_code
);
1860 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1862 if (ret
!= sizeof(channel_monitor_pipe
)) {
1863 ERR("Failed to receive channel monitor pipe");
1867 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1868 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1869 channel_monitor_pipe
);
1873 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1874 /* Set the pipe as non-blocking. */
1875 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1877 PERROR("fcntl get flags of the channel monitoring pipe");
1882 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1883 flags
| O_NONBLOCK
);
1885 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1888 DBG("Channel monitor pipe set as non-blocking");
1890 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1892 goto end_msg_sessiond
;
1901 health_code_update();
1904 * Return 1 to indicate success since the 0 value can be a socket
1905 * shutdown during the recv() or send() call.
1911 * The returned value here is not useful since either way we'll return 1 to
1912 * the caller because the session daemon socket management is done
1913 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1915 ret
= consumer_send_status_msg(sock
, ret_code
);
1921 health_code_update();
1927 * Free channel here since no one has a reference to it. We don't
1928 * free after that because a stream can store this pointer.
1930 destroy_channel(channel
);
1932 /* We have to send a status channel message indicating an error. */
1933 ret
= consumer_send_status_channel(sock
, NULL
);
1935 /* Stop everything if session daemon can not be notified. */
1940 health_code_update();
1945 /* This will issue a consumer stop. */
1950 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1951 * compiled out, we isolate it in this library.
1953 int lttng_ustctl_get_mmap_read_offset(struct lttng_consumer_stream
*stream
,
1957 assert(stream
->ustream
);
1959 return ustctl_get_mmap_read_offset(stream
->ustream
, off
);
1963 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1964 * compiled out, we isolate it in this library.
1966 void *lttng_ustctl_get_mmap_base(struct lttng_consumer_stream
*stream
)
1969 assert(stream
->ustream
);
1971 return ustctl_get_mmap_base(stream
->ustream
);
1975 * Take a snapshot for a specific stream.
1977 * Returns 0 on success, < 0 on error
1979 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1982 assert(stream
->ustream
);
1984 return ustctl_snapshot(stream
->ustream
);
1988 * Sample consumed and produced positions for a specific stream.
1990 * Returns 0 on success, < 0 on error.
1992 int lttng_ustconsumer_sample_snapshot_positions(
1993 struct lttng_consumer_stream
*stream
)
1996 assert(stream
->ustream
);
1998 return ustctl_snapshot_sample_positions(stream
->ustream
);
2002 * Get the produced position
2004 * Returns 0 on success, < 0 on error
2006 int lttng_ustconsumer_get_produced_snapshot(
2007 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2010 assert(stream
->ustream
);
2013 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2017 * Get the consumed position
2019 * Returns 0 on success, < 0 on error
2021 int lttng_ustconsumer_get_consumed_snapshot(
2022 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2025 assert(stream
->ustream
);
2028 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2031 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2035 assert(stream
->ustream
);
2037 ustctl_flush_buffer(stream
->ustream
, producer
);
2040 int lttng_ustconsumer_get_current_timestamp(
2041 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2044 assert(stream
->ustream
);
2047 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2050 int lttng_ustconsumer_get_sequence_number(
2051 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2054 assert(stream
->ustream
);
2057 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2061 * Called when the stream signals the consumer that it has hung up.
2063 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2066 assert(stream
->ustream
);
2068 pthread_mutex_lock(&stream
->lock
);
2069 if (!stream
->quiescent
) {
2070 ustctl_flush_buffer(stream
->ustream
, 0);
2071 stream
->quiescent
= true;
2073 pthread_mutex_unlock(&stream
->lock
);
2074 stream
->hangup_flush_done
= 1;
2077 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2082 assert(chan
->uchan
);
2084 if (chan
->switch_timer_enabled
== 1) {
2085 consumer_timer_switch_stop(chan
);
2087 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2090 ret
= close(chan
->stream_fds
[i
]);
2094 if (chan
->shm_path
[0]) {
2095 char shm_path
[PATH_MAX
];
2097 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2099 ERR("Cannot get stream shm path");
2101 ret
= run_as_unlink(shm_path
, chan
->uid
, chan
->gid
);
2103 PERROR("unlink %s", shm_path
);
2109 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2112 assert(chan
->uchan
);
2114 consumer_metadata_cache_destroy(chan
);
2115 ustctl_destroy_channel(chan
->uchan
);
2116 /* Try to rmdir all directories under shm_path root. */
2117 if (chan
->root_shm_path
[0]) {
2118 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2119 chan
->uid
, chan
->gid
);
2121 free(chan
->stream_fds
);
2124 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2127 assert(stream
->ustream
);
2129 if (stream
->chan
->switch_timer_enabled
== 1) {
2130 consumer_timer_switch_stop(stream
->chan
);
2132 ustctl_destroy_stream(stream
->ustream
);
2135 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2138 assert(stream
->ustream
);
2140 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2143 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2146 assert(stream
->ustream
);
2148 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2152 * Populate index values of a UST stream. Values are set in big endian order.
2154 * Return 0 on success or else a negative value.
2156 static int get_index_values(struct ctf_packet_index
*index
,
2157 struct ustctl_consumer_stream
*ustream
)
2161 ret
= ustctl_get_timestamp_begin(ustream
, &index
->timestamp_begin
);
2163 PERROR("ustctl_get_timestamp_begin");
2166 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
2168 ret
= ustctl_get_timestamp_end(ustream
, &index
->timestamp_end
);
2170 PERROR("ustctl_get_timestamp_end");
2173 index
->timestamp_end
= htobe64(index
->timestamp_end
);
2175 ret
= ustctl_get_events_discarded(ustream
, &index
->events_discarded
);
2177 PERROR("ustctl_get_events_discarded");
2180 index
->events_discarded
= htobe64(index
->events_discarded
);
2182 ret
= ustctl_get_content_size(ustream
, &index
->content_size
);
2184 PERROR("ustctl_get_content_size");
2187 index
->content_size
= htobe64(index
->content_size
);
2189 ret
= ustctl_get_packet_size(ustream
, &index
->packet_size
);
2191 PERROR("ustctl_get_packet_size");
2194 index
->packet_size
= htobe64(index
->packet_size
);
2196 ret
= ustctl_get_stream_id(ustream
, &index
->stream_id
);
2198 PERROR("ustctl_get_stream_id");
2201 index
->stream_id
= htobe64(index
->stream_id
);
2203 ret
= ustctl_get_instance_id(ustream
, &index
->stream_instance_id
);
2205 PERROR("ustctl_get_instance_id");
2208 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
2210 ret
= ustctl_get_sequence_number(ustream
, &index
->packet_seq_num
);
2212 PERROR("ustctl_get_sequence_number");
2215 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
2222 void metadata_stream_reset_cache(struct lttng_consumer_stream
*stream
,
2223 struct consumer_metadata_cache
*cache
)
2225 DBG("Metadata stream update to version %" PRIu64
,
2227 stream
->ust_metadata_pushed
= 0;
2228 stream
->metadata_version
= cache
->version
;
2229 stream
->reset_metadata_flag
= 1;
2233 * Check if the version of the metadata stream and metadata cache match.
2234 * If the cache got updated, reset the metadata stream.
2235 * The stream lock and metadata cache lock MUST be held.
2236 * Return 0 on success, a negative value on error.
2239 int metadata_stream_check_version(struct lttng_consumer_stream
*stream
)
2242 struct consumer_metadata_cache
*cache
= stream
->chan
->metadata_cache
;
2244 if (cache
->version
== stream
->metadata_version
) {
2247 metadata_stream_reset_cache(stream
, cache
);
2254 * Write up to one packet from the metadata cache to the channel.
2256 * Returns the number of bytes pushed in the cache, or a negative value
2260 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2265 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2266 ret
= metadata_stream_check_version(stream
);
2270 if (stream
->chan
->metadata_cache
->max_offset
2271 == stream
->ust_metadata_pushed
) {
2276 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2277 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2278 stream
->chan
->metadata_cache
->max_offset
2279 - stream
->ust_metadata_pushed
);
2280 assert(write_len
!= 0);
2281 if (write_len
< 0) {
2282 ERR("Writing one metadata packet");
2286 stream
->ust_metadata_pushed
+= write_len
;
2288 assert(stream
->chan
->metadata_cache
->max_offset
>=
2289 stream
->ust_metadata_pushed
);
2293 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2299 * Sync metadata meaning request them to the session daemon and snapshot to the
2300 * metadata thread can consumer them.
2302 * Metadata stream lock is held here, but we need to release it when
2303 * interacting with sessiond, else we cause a deadlock with live
2304 * awaiting on metadata to be pushed out.
2306 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
2307 * is empty or a negative value on error.
2309 int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2310 struct lttng_consumer_stream
*metadata
)
2318 pthread_mutex_unlock(&metadata
->lock
);
2320 * Request metadata from the sessiond, but don't wait for the flush
2321 * because we locked the metadata thread.
2323 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata
->chan
, 0, 0);
2324 pthread_mutex_lock(&metadata
->lock
);
2329 ret
= commit_one_metadata_packet(metadata
);
2332 } else if (ret
> 0) {
2336 ustctl_flush_buffer(metadata
->ustream
, 1);
2337 ret
= ustctl_snapshot(metadata
->ustream
);
2339 if (errno
!= EAGAIN
) {
2340 ERR("Sync metadata, taking UST snapshot");
2343 DBG("No new metadata when syncing them.");
2344 /* No new metadata, exit. */
2350 * After this flush, we still need to extract metadata.
2361 * Return 0 on success else a negative value.
2363 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2364 struct lttng_consumer_local_data
*ctx
)
2367 struct ustctl_consumer_stream
*ustream
;
2372 ustream
= stream
->ustream
;
2375 * First, we are going to check if there is a new subbuffer available
2376 * before reading the stream wait_fd.
2378 /* Get the next subbuffer */
2379 ret
= ustctl_get_next_subbuf(ustream
);
2381 /* No more data found, flag the stream. */
2382 stream
->has_data
= 0;
2387 ret
= ustctl_put_subbuf(ustream
);
2390 /* This stream still has data. Flag it and wake up the data thread. */
2391 stream
->has_data
= 1;
2393 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2396 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2397 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2402 /* The wake up pipe has been notified. */
2403 ctx
->has_wakeup
= 1;
2412 int update_stream_stats(struct lttng_consumer_stream
*stream
)
2415 uint64_t seq
, discarded
;
2417 ret
= ustctl_get_sequence_number(stream
->ustream
, &seq
);
2419 PERROR("ustctl_get_sequence_number");
2423 * Start the sequence when we extract the first packet in case we don't
2424 * start at 0 (for example if a consumer is not connected to the
2425 * session immediately after the beginning).
2427 if (stream
->last_sequence_number
== -1ULL) {
2428 stream
->last_sequence_number
= seq
;
2429 } else if (seq
> stream
->last_sequence_number
) {
2430 stream
->chan
->lost_packets
+= seq
-
2431 stream
->last_sequence_number
- 1;
2433 /* seq <= last_sequence_number */
2434 ERR("Sequence number inconsistent : prev = %" PRIu64
2435 ", current = %" PRIu64
,
2436 stream
->last_sequence_number
, seq
);
2440 stream
->last_sequence_number
= seq
;
2442 ret
= ustctl_get_events_discarded(stream
->ustream
, &discarded
);
2444 PERROR("kernctl_get_events_discarded");
2447 if (discarded
< stream
->last_discarded_events
) {
2449 * Overflow has occurred. We assume only one wrap-around
2452 stream
->chan
->discarded_events
+=
2453 (1ULL << (CAA_BITS_PER_LONG
- 1)) -
2454 stream
->last_discarded_events
+ discarded
;
2456 stream
->chan
->discarded_events
+= discarded
-
2457 stream
->last_discarded_events
;
2459 stream
->last_discarded_events
= discarded
;
2467 * Read subbuffer from the given stream.
2469 * Stream lock MUST be acquired.
2471 * Return 0 on success else a negative value.
2473 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2474 struct lttng_consumer_local_data
*ctx
)
2476 unsigned long len
, subbuf_size
, padding
;
2477 int err
, write_index
= 1;
2479 struct ustctl_consumer_stream
*ustream
;
2480 struct ctf_packet_index index
;
2483 assert(stream
->ustream
);
2486 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream
->wait_fd
,
2489 /* Ease our life for what's next. */
2490 ustream
= stream
->ustream
;
2493 * We can consume the 1 byte written into the wait_fd by UST. Don't trigger
2494 * error if we cannot read this one byte (read returns 0), or if the error
2495 * is EAGAIN or EWOULDBLOCK.
2497 * This is only done when the stream is monitored by a thread, before the
2498 * flush is done after a hangup and if the stream is not flagged with data
2499 * since there might be nothing to consume in the wait fd but still have
2500 * data available flagged by the consumer wake up pipe.
2502 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2506 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2507 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2514 /* Get the next subbuffer */
2515 err
= ustctl_get_next_subbuf(ustream
);
2518 * Populate metadata info if the existing info has
2519 * already been read.
2521 if (stream
->metadata_flag
) {
2522 ret
= commit_one_metadata_packet(stream
);
2526 ustctl_flush_buffer(stream
->ustream
, 1);
2530 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
2532 * This is a debug message even for single-threaded consumer,
2533 * because poll() have more relaxed criterions than get subbuf,
2534 * so get_subbuf may fail for short race windows where poll()
2535 * would issue wakeups.
2537 DBG("Reserving sub buffer failed (everything is normal, "
2538 "it is due to concurrency) [ret: %d]", err
);
2541 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
2543 if (!stream
->metadata_flag
) {
2544 index
.offset
= htobe64(stream
->out_fd_offset
);
2545 ret
= get_index_values(&index
, ustream
);
2547 err
= ustctl_put_subbuf(ustream
);
2552 /* Update the stream's sequence and discarded events count. */
2553 ret
= update_stream_stats(stream
);
2555 PERROR("kernctl_get_events_discarded");
2556 err
= ustctl_put_subbuf(ustream
);
2564 /* Get the full padded subbuffer size */
2565 err
= ustctl_get_padded_subbuf_size(ustream
, &len
);
2568 /* Get subbuffer data size (without padding) */
2569 err
= ustctl_get_subbuf_size(ustream
, &subbuf_size
);
2572 /* Make sure we don't get a subbuffer size bigger than the padded */
2573 assert(len
>= subbuf_size
);
2575 padding
= len
- subbuf_size
;
2576 /* write the subbuffer to the tracefile */
2577 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
, padding
, &index
);
2579 * The mmap operation should write subbuf_size amount of data when network
2580 * streaming or the full padding (len) size when we are _not_ streaming.
2582 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
2583 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
2585 * Display the error but continue processing to try to release the
2586 * subbuffer. This is a DBG statement since any unexpected kill or
2587 * signal, the application gets unregistered, relayd gets closed or
2588 * anything that affects the buffer lifetime will trigger this error.
2589 * So, for the sake of the user, don't print this error since it can
2590 * happen and it is OK with the code flow.
2592 DBG("Error writing to tracefile "
2593 "(ret: %ld != len: %lu != subbuf_size: %lu)",
2594 ret
, len
, subbuf_size
);
2597 err
= ustctl_put_next_subbuf(ustream
);
2601 * This will consumer the byte on the wait_fd if and only if there is not
2602 * next subbuffer to be acquired.
2604 if (!stream
->metadata_flag
) {
2605 ret
= notify_if_more_data(stream
, ctx
);
2611 /* Write index if needed. */
2616 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
2618 * In live, block until all the metadata is sent.
2620 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2621 assert(!stream
->missed_metadata_flush
);
2622 stream
->waiting_on_metadata
= true;
2623 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2625 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
2627 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2628 stream
->waiting_on_metadata
= false;
2629 if (stream
->missed_metadata_flush
) {
2630 stream
->missed_metadata_flush
= false;
2631 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2632 (void) consumer_flush_ust_index(stream
);
2634 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2642 assert(!stream
->metadata_flag
);
2643 err
= consumer_stream_write_index(stream
, &index
);
2653 * Called when a stream is created.
2655 * Return 0 on success or else a negative value.
2657 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2663 /* Don't create anything if this is set for streaming. */
2664 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
2665 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
2666 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
2667 stream
->uid
, stream
->gid
, NULL
);
2671 stream
->out_fd
= ret
;
2672 stream
->tracefile_size_current
= 0;
2674 if (!stream
->metadata_flag
) {
2675 struct lttng_index_file
*index_file
;
2677 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
2678 stream
->name
, stream
->uid
, stream
->gid
,
2679 stream
->chan
->tracefile_size
,
2680 stream
->tracefile_count_current
,
2681 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
2685 assert(!stream
->index_file
);
2686 stream
->index_file
= index_file
;
2696 * Check if data is still being extracted from the buffers for a specific
2697 * stream. Consumer data lock MUST be acquired before calling this function
2698 * and the stream lock.
2700 * Return 1 if the traced data are still getting read else 0 meaning that the
2701 * data is available for trace viewer reading.
2703 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
2708 assert(stream
->ustream
);
2710 DBG("UST consumer checking data pending");
2712 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
2717 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
2718 uint64_t contiguous
, pushed
;
2720 /* Ease our life a bit. */
2721 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
2722 pushed
= stream
->ust_metadata_pushed
;
2725 * We can simply check whether all contiguously available data
2726 * has been pushed to the ring buffer, since the push operation
2727 * is performed within get_next_subbuf(), and because both
2728 * get_next_subbuf() and put_next_subbuf() are issued atomically
2729 * thanks to the stream lock within
2730 * lttng_ustconsumer_read_subbuffer(). This basically means that
2731 * whetnever ust_metadata_pushed is incremented, the associated
2732 * metadata has been consumed from the metadata stream.
2734 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
2735 contiguous
, pushed
);
2736 assert(((int64_t) (contiguous
- pushed
)) >= 0);
2737 if ((contiguous
!= pushed
) ||
2738 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
2739 ret
= 1; /* Data is pending */
2743 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2746 * There is still data so let's put back this
2749 ret
= ustctl_put_subbuf(stream
->ustream
);
2751 ret
= 1; /* Data is pending */
2756 /* Data is NOT pending so ready to be read. */
2764 * Stop a given metadata channel timer if enabled and close the wait fd which
2765 * is the poll pipe of the metadata stream.
2767 * This MUST be called with the metadata channel acquired.
2769 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
2774 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
2776 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
2778 if (metadata
->switch_timer_enabled
== 1) {
2779 consumer_timer_switch_stop(metadata
);
2782 if (!metadata
->metadata_stream
) {
2787 * Closing write side so the thread monitoring the stream wakes up if any
2788 * and clean the metadata stream.
2790 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
2791 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
2793 PERROR("closing metadata pipe write side");
2795 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
2803 * Close every metadata stream wait fd of the metadata hash table. This
2804 * function MUST be used very carefully so not to run into a race between the
2805 * metadata thread handling streams and this function closing their wait fd.
2807 * For UST, this is used when the session daemon hangs up. Its the metadata
2808 * producer so calling this is safe because we are assured that no state change
2809 * can occur in the metadata thread for the streams in the hash table.
2811 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
2813 struct lttng_ht_iter iter
;
2814 struct lttng_consumer_stream
*stream
;
2816 assert(metadata_ht
);
2817 assert(metadata_ht
->ht
);
2819 DBG("UST consumer closing all metadata streams");
2822 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
2825 health_code_update();
2827 pthread_mutex_lock(&stream
->chan
->lock
);
2828 lttng_ustconsumer_close_metadata(stream
->chan
);
2829 pthread_mutex_unlock(&stream
->chan
->lock
);
2835 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
2839 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
2841 ERR("Unable to close wakeup fd");
2846 * Please refer to consumer-timer.c before adding any lock within this
2847 * function or any of its callees. Timers have a very strict locking
2848 * semantic with respect to teardown. Failure to respect this semantic
2849 * introduces deadlocks.
2851 * DON'T hold the metadata lock when calling this function, else this
2852 * can cause deadlock involving consumer awaiting for metadata to be
2853 * pushed out due to concurrent interaction with the session daemon.
2855 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
2856 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
2858 struct lttcomm_metadata_request_msg request
;
2859 struct lttcomm_consumer_msg msg
;
2860 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
2861 uint64_t len
, key
, offset
, version
;
2865 assert(channel
->metadata_cache
);
2867 memset(&request
, 0, sizeof(request
));
2869 /* send the metadata request to sessiond */
2870 switch (consumer_data
.type
) {
2871 case LTTNG_CONSUMER64_UST
:
2872 request
.bits_per_long
= 64;
2874 case LTTNG_CONSUMER32_UST
:
2875 request
.bits_per_long
= 32;
2878 request
.bits_per_long
= 0;
2882 request
.session_id
= channel
->session_id
;
2883 request
.session_id_per_pid
= channel
->session_id_per_pid
;
2885 * Request the application UID here so the metadata of that application can
2886 * be sent back. The channel UID corresponds to the user UID of the session
2887 * used for the rights on the stream file(s).
2889 request
.uid
= channel
->ust_app_uid
;
2890 request
.key
= channel
->key
;
2892 DBG("Sending metadata request to sessiond, session id %" PRIu64
2893 ", per-pid %" PRIu64
", app UID %u and channek key %" PRIu64
,
2894 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
2897 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
2899 health_code_update();
2901 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
2904 ERR("Asking metadata to sessiond");
2908 health_code_update();
2910 /* Receive the metadata from sessiond */
2911 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
2913 if (ret
!= sizeof(msg
)) {
2914 DBG("Consumer received unexpected message size %d (expects %zu)",
2916 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
2918 * The ret value might 0 meaning an orderly shutdown but this is ok
2919 * since the caller handles this.
2924 health_code_update();
2926 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
2927 /* No registry found */
2928 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
2932 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
2933 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
2938 len
= msg
.u
.push_metadata
.len
;
2939 key
= msg
.u
.push_metadata
.key
;
2940 offset
= msg
.u
.push_metadata
.target_offset
;
2941 version
= msg
.u
.push_metadata
.version
;
2943 assert(key
== channel
->key
);
2945 DBG("No new metadata to receive for key %" PRIu64
, key
);
2948 health_code_update();
2950 /* Tell session daemon we are ready to receive the metadata. */
2951 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
2952 LTTCOMM_CONSUMERD_SUCCESS
);
2953 if (ret
< 0 || len
== 0) {
2955 * Somehow, the session daemon is not responding anymore or there is
2956 * nothing to receive.
2961 health_code_update();
2963 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
2964 key
, offset
, len
, version
, channel
, timer
, wait
);
2967 * Only send the status msg if the sessiond is alive meaning a positive
2970 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
2975 health_code_update();
2977 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
2982 * Return the ustctl call for the get stream id.
2984 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
2985 uint64_t *stream_id
)
2990 return ustctl_get_stream_id(stream
->ustream
, stream_id
);