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>
37 #include <bin/lttng-consumerd/health-consumerd.h>
38 #include <common/common.h>
39 #include <common/sessiond-comm/sessiond-comm.h>
40 #include <common/relayd/relayd.h>
41 #include <common/compat/fcntl.h>
42 #include <common/compat/endian.h>
43 #include <common/consumer/consumer-metadata-cache.h>
44 #include <common/consumer/consumer-stream.h>
45 #include <common/consumer/consumer-timer.h>
46 #include <common/utils.h>
47 #include <common/index/index.h>
48 #include <common/consumer/consumer.h>
49 #include <common/optional.h>
51 #include "ust-consumer.h"
53 #define INT_MAX_STR_LEN 12 /* includes \0 */
55 extern struct lttng_consumer_global_data consumer_data
;
56 extern int consumer_poll_timeout
;
57 extern volatile int consumer_quit
;
60 * Free channel object and all streams associated with it. This MUST be used
61 * only and only if the channel has _NEVER_ been added to the global channel
64 static void destroy_channel(struct lttng_consumer_channel
*channel
)
66 struct lttng_consumer_stream
*stream
, *stmp
;
70 DBG("UST consumer cleaning stream list");
72 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
77 cds_list_del(&stream
->send_node
);
78 ustctl_destroy_stream(stream
->ustream
);
83 * If a channel is available meaning that was created before the streams
87 lttng_ustconsumer_del_channel(channel
);
88 lttng_ustconsumer_free_channel(channel
);
94 * Add channel to internal consumer state.
96 * Returns 0 on success or else a negative value.
98 static int add_channel(struct lttng_consumer_channel
*channel
,
99 struct lttng_consumer_local_data
*ctx
)
106 if (ctx
->on_recv_channel
!= NULL
) {
107 ret
= ctx
->on_recv_channel(channel
);
109 ret
= consumer_add_channel(channel
, ctx
);
110 } else if (ret
< 0) {
111 /* Most likely an ENOMEM. */
112 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
116 ret
= consumer_add_channel(channel
, ctx
);
119 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
126 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
127 * error value if applicable is set in it else it is kept untouched.
129 * Return NULL on error else the newly allocated stream object.
131 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
132 struct lttng_consumer_channel
*channel
,
133 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
136 struct lttng_consumer_stream
*stream
= NULL
;
141 stream
= consumer_stream_create(
145 LTTNG_CONSUMER_ACTIVE_STREAM
,
155 if (stream
== NULL
) {
159 * We could not find the channel. Can happen if cpu hotplug
160 * happens while tearing down.
162 DBG3("Could not find channel");
167 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
175 *_alloc_ret
= alloc_ret
;
181 * Send the given stream pointer to the corresponding thread.
183 * Returns 0 on success else a negative value.
185 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
186 struct lttng_consumer_local_data
*ctx
)
189 struct lttng_pipe
*stream_pipe
;
191 /* Get the right pipe where the stream will be sent. */
192 if (stream
->metadata_flag
) {
193 ret
= consumer_add_metadata_stream(stream
);
195 ERR("Consumer add metadata stream %" PRIu64
" failed.",
199 stream_pipe
= ctx
->consumer_metadata_pipe
;
201 ret
= consumer_add_data_stream(stream
);
203 ERR("Consumer add stream %" PRIu64
" failed.",
207 stream_pipe
= ctx
->consumer_data_pipe
;
211 * From this point on, the stream's ownership has been moved away from
212 * the channel and becomes globally visible.
214 stream
->globally_visible
= 1;
216 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
218 ERR("Consumer write %s stream to pipe %d",
219 stream
->metadata_flag
? "metadata" : "data",
220 lttng_pipe_get_writefd(stream_pipe
));
221 if (stream
->metadata_flag
) {
222 consumer_del_stream_for_metadata(stream
);
224 consumer_del_stream_for_data(stream
);
232 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
234 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
237 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
238 stream_shm_path
[PATH_MAX
- 1] = '\0';
239 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
244 strncat(stream_shm_path
, cpu_nr
,
245 PATH_MAX
- strlen(stream_shm_path
) - 1);
252 * Create streams for the given channel using liblttng-ust-ctl.
254 * Return 0 on success else a negative value.
256 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
257 struct lttng_consumer_local_data
*ctx
)
260 struct ustctl_consumer_stream
*ustream
;
261 struct lttng_consumer_stream
*stream
;
267 * While a stream is available from ustctl. When NULL is returned, we've
268 * reached the end of the possible stream for the channel.
270 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
272 int ust_metadata_pipe
[2];
274 health_code_update();
276 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
277 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
279 ERR("Create ust metadata poll pipe");
282 wait_fd
= ust_metadata_pipe
[0];
284 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
287 /* Allocate consumer stream object. */
288 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
292 stream
->ustream
= ustream
;
294 * Store it so we can save multiple function calls afterwards since
295 * this value is used heavily in the stream threads. This is UST
296 * specific so this is why it's done after allocation.
298 stream
->wait_fd
= wait_fd
;
301 * Increment channel refcount since the channel reference has now been
302 * assigned in the allocation process above.
304 if (stream
->chan
->monitor
) {
305 uatomic_inc(&stream
->chan
->refcount
);
309 * Order is important this is why a list is used. On error, the caller
310 * should clean this list.
312 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
314 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
315 &stream
->max_sb_size
);
317 ERR("ustctl_get_max_subbuf_size failed for stream %s",
322 /* Do actions once stream has been received. */
323 if (ctx
->on_recv_stream
) {
324 ret
= ctx
->on_recv_stream(stream
);
330 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
331 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
333 /* Set next CPU stream. */
334 channel
->streams
.count
= ++cpu
;
336 /* Keep stream reference when creating metadata. */
337 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
338 channel
->metadata_stream
= stream
;
339 if (channel
->monitor
) {
340 /* Set metadata poll pipe if we created one */
341 memcpy(stream
->ust_metadata_poll_pipe
,
343 sizeof(ust_metadata_pipe
));
356 * create_posix_shm is never called concurrently within a process.
359 int create_posix_shm(void)
361 char tmp_name
[NAME_MAX
];
364 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
370 * Allocate shm, and immediately unlink its shm oject, keeping
371 * only the file descriptor as a reference to the object.
372 * We specifically do _not_ use the / at the beginning of the
373 * pathname so that some OS implementations can keep it local to
374 * the process (POSIX leaves this implementation-defined).
376 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
381 ret
= shm_unlink(tmp_name
);
382 if (ret
< 0 && errno
!= ENOENT
) {
383 PERROR("shm_unlink");
384 goto error_shm_release
;
397 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
,
398 struct ustctl_consumer_channel_attr
*attr
,
401 char shm_path
[PATH_MAX
];
404 if (!channel
->shm_path
[0]) {
405 return create_posix_shm();
407 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
411 return run_as_open(shm_path
,
412 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
413 channel
->uid
, channel
->gid
);
420 * Create an UST channel with the given attributes and send it to the session
421 * daemon using the ust ctl API.
423 * Return 0 on success or else a negative value.
425 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
426 struct ustctl_consumer_channel_attr
*attr
,
427 struct ustctl_consumer_channel
**ust_chanp
)
429 int ret
, nr_stream_fds
, i
, j
;
431 struct ustctl_consumer_channel
*ust_channel
;
437 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
438 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
439 "switch_timer_interval: %u, read_timer_interval: %u, "
440 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
441 attr
->num_subbuf
, attr
->switch_timer_interval
,
442 attr
->read_timer_interval
, attr
->output
, attr
->type
);
444 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
447 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
448 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
453 for (i
= 0; i
< nr_stream_fds
; i
++) {
454 stream_fds
[i
] = open_ust_stream_fd(channel
, attr
, i
);
455 if (stream_fds
[i
] < 0) {
460 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
465 channel
->nr_stream_fds
= nr_stream_fds
;
466 channel
->stream_fds
= stream_fds
;
467 *ust_chanp
= ust_channel
;
473 for (j
= i
- 1; j
>= 0; j
--) {
476 closeret
= close(stream_fds
[j
]);
480 if (channel
->shm_path
[0]) {
481 char shm_path
[PATH_MAX
];
483 closeret
= get_stream_shm_path(shm_path
,
484 channel
->shm_path
, j
);
486 ERR("Cannot get stream shm path");
488 closeret
= run_as_unlink(shm_path
,
489 channel
->uid
, channel
->gid
);
491 PERROR("unlink %s", shm_path
);
495 /* Try to rmdir all directories under shm_path root. */
496 if (channel
->root_shm_path
[0]) {
497 (void) run_as_recursive_rmdir(channel
->root_shm_path
,
498 channel
->uid
, channel
->gid
);
506 * Send a single given stream to the session daemon using the sock.
508 * Return 0 on success else a negative value.
510 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
517 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
519 /* Send stream to session daemon. */
520 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
530 * Send channel to sessiond.
532 * Return 0 on success or else a negative value.
534 static int send_sessiond_channel(int sock
,
535 struct lttng_consumer_channel
*channel
,
536 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
538 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
539 struct lttng_consumer_stream
*stream
;
540 uint64_t relayd_id
= -1ULL;
546 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
548 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
549 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
551 health_code_update();
553 /* Try to send the stream to the relayd if one is available. */
554 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
557 * Flag that the relayd was the problem here probably due to a
558 * communicaton error on the socket.
563 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
565 if (relayd_id
== -1ULL) {
566 relayd_id
= stream
->relayd_id
;
571 /* Inform sessiond that we are about to send channel and streams. */
572 ret
= consumer_send_status_msg(sock
, ret_code
);
573 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
575 * Either the session daemon is not responding or the relayd died so we
581 /* Send channel to sessiond. */
582 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
587 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
592 /* The channel was sent successfully to the sessiond at this point. */
593 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
595 health_code_update();
597 /* Send stream to session daemon. */
598 ret
= send_sessiond_stream(sock
, stream
);
604 /* Tell sessiond there is no more stream. */
605 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
610 DBG("UST consumer NULL stream sent to sessiond");
615 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
622 * Creates a channel and streams and add the channel it to the channel internal
623 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
626 * Return 0 on success or else, a negative value is returned and the channel
627 * MUST be destroyed by consumer_del_channel().
629 static int ask_channel(struct lttng_consumer_local_data
*ctx
, int sock
,
630 struct lttng_consumer_channel
*channel
,
631 struct ustctl_consumer_channel_attr
*attr
)
640 * This value is still used by the kernel consumer since for the kernel,
641 * the stream ownership is not IN the consumer so we need to have the
642 * number of left stream that needs to be initialized so we can know when
643 * to delete the channel (see consumer.c).
645 * As for the user space tracer now, the consumer creates and sends the
646 * stream to the session daemon which only sends them to the application
647 * once every stream of a channel is received making this value useless
648 * because we they will be added to the poll thread before the application
649 * receives them. This ensures that a stream can not hang up during
650 * initilization of a channel.
652 channel
->nb_init_stream_left
= 0;
654 /* The reply msg status is handled in the following call. */
655 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
660 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
663 * For the snapshots (no monitor), we create the metadata streams
664 * on demand, not during the channel creation.
666 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
671 /* Open all streams for this channel. */
672 ret
= create_ust_streams(channel
, ctx
);
682 * Send all stream of a channel to the right thread handling it.
684 * On error, return a negative value else 0 on success.
686 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
687 struct lttng_consumer_local_data
*ctx
)
690 struct lttng_consumer_stream
*stream
, *stmp
;
695 /* Send streams to the corresponding thread. */
696 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
699 health_code_update();
701 /* Sending the stream to the thread. */
702 ret
= send_stream_to_thread(stream
, ctx
);
705 * If we are unable to send the stream to the thread, there is
706 * a big problem so just stop everything.
708 /* Remove node from the channel stream list. */
709 cds_list_del(&stream
->send_node
);
713 /* Remove node from the channel stream list. */
714 cds_list_del(&stream
->send_node
);
723 * Flush channel's streams using the given key to retrieve the channel.
725 * Return 0 on success else an LTTng error code.
727 static int flush_channel(uint64_t chan_key
)
730 struct lttng_consumer_channel
*channel
;
731 struct lttng_consumer_stream
*stream
;
733 struct lttng_ht_iter iter
;
735 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
738 channel
= consumer_find_channel(chan_key
);
740 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
741 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
745 ht
= consumer_data
.stream_per_chan_id_ht
;
747 /* For each stream of the channel id, flush it. */
748 cds_lfht_for_each_entry_duplicate(ht
->ht
,
749 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
750 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
752 health_code_update();
754 pthread_mutex_lock(&stream
->lock
);
757 * Protect against concurrent teardown of a stream.
759 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
763 if (!stream
->quiescent
) {
764 ustctl_flush_buffer(stream
->ustream
, 0);
765 stream
->quiescent
= true;
768 pthread_mutex_unlock(&stream
->lock
);
776 * Clear quiescent state from channel's streams using the given key to
777 * retrieve the channel.
779 * Return 0 on success else an LTTng error code.
781 static int clear_quiescent_channel(uint64_t chan_key
)
784 struct lttng_consumer_channel
*channel
;
785 struct lttng_consumer_stream
*stream
;
787 struct lttng_ht_iter iter
;
789 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
792 channel
= consumer_find_channel(chan_key
);
794 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
795 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
799 ht
= consumer_data
.stream_per_chan_id_ht
;
801 /* For each stream of the channel id, clear quiescent state. */
802 cds_lfht_for_each_entry_duplicate(ht
->ht
,
803 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
804 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
806 health_code_update();
808 pthread_mutex_lock(&stream
->lock
);
809 stream
->quiescent
= false;
810 pthread_mutex_unlock(&stream
->lock
);
818 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
819 * RCU read side lock MUST be acquired before calling this function.
821 * Return 0 on success else an LTTng error code.
823 static int close_metadata(uint64_t chan_key
)
826 struct lttng_consumer_channel
*channel
;
827 unsigned int channel_monitor
;
829 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
831 channel
= consumer_find_channel(chan_key
);
834 * This is possible if the metadata thread has issue a delete because
835 * the endpoint point of the stream hung up. There is no way the
836 * session daemon can know about it thus use a DBG instead of an actual
839 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
840 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
844 pthread_mutex_lock(&consumer_data
.lock
);
845 pthread_mutex_lock(&channel
->lock
);
846 channel_monitor
= channel
->monitor
;
847 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
851 lttng_ustconsumer_close_metadata(channel
);
852 pthread_mutex_unlock(&channel
->lock
);
853 pthread_mutex_unlock(&consumer_data
.lock
);
856 * The ownership of a metadata channel depends on the type of
857 * session to which it belongs. In effect, the monitor flag is checked
858 * to determine if this metadata channel is in "snapshot" mode or not.
860 * In the non-snapshot case, the metadata channel is created along with
861 * a single stream which will remain present until the metadata channel
862 * is destroyed (on the destruction of its session). In this case, the
863 * metadata stream in "monitored" by the metadata poll thread and holds
864 * the ownership of its channel.
866 * Closing the metadata will cause the metadata stream's "metadata poll
867 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
868 * thread which will teardown the metadata stream which, in return,
869 * deletes the metadata channel.
871 * In the snapshot case, the metadata stream is created and destroyed
872 * on every snapshot record. Since the channel doesn't have an owner
873 * other than the session daemon, it is safe to destroy it immediately
874 * on reception of the CLOSE_METADATA command.
876 if (!channel_monitor
) {
878 * The channel and consumer_data locks must be
879 * released before this call since consumer_del_channel
880 * re-acquires the channel and consumer_data locks to teardown
881 * the channel and queue its reclamation by the "call_rcu"
884 consumer_del_channel(channel
);
889 pthread_mutex_unlock(&channel
->lock
);
890 pthread_mutex_unlock(&consumer_data
.lock
);
896 * RCU read side lock MUST be acquired before calling this function.
898 * Return 0 on success else an LTTng error code.
900 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
903 struct lttng_consumer_channel
*metadata
;
905 DBG("UST consumer setup metadata key %" PRIu64
, key
);
907 metadata
= consumer_find_channel(key
);
909 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
910 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
915 * In no monitor mode, the metadata channel has no stream(s) so skip the
916 * ownership transfer to the metadata thread.
918 if (!metadata
->monitor
) {
919 DBG("Metadata channel in no monitor");
925 * Send metadata stream to relayd if one available. Availability is
926 * known if the stream is still in the list of the channel.
928 if (cds_list_empty(&metadata
->streams
.head
)) {
929 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
930 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
931 goto error_no_stream
;
934 /* Send metadata stream to relayd if needed. */
935 if (metadata
->metadata_stream
->relayd_id
!= (uint64_t) -1ULL) {
936 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
939 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
942 ret
= consumer_send_relayd_streams_sent(
943 metadata
->metadata_stream
->relayd_id
);
945 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
950 ret
= send_streams_to_thread(metadata
, ctx
);
953 * If we are unable to send the stream to the thread, there is
954 * a big problem so just stop everything.
956 ret
= LTTCOMM_CONSUMERD_FATAL
;
959 /* List MUST be empty after or else it could be reused. */
960 assert(cds_list_empty(&metadata
->streams
.head
));
967 * Delete metadata channel on error. At this point, the metadata stream can
968 * NOT be monitored by the metadata thread thus having the guarantee that
969 * the stream is still in the local stream list of the channel. This call
970 * will make sure to clean that list.
972 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
973 cds_list_del(&metadata
->metadata_stream
->send_node
);
974 metadata
->metadata_stream
= NULL
;
981 * Snapshot the whole metadata.
983 * Returns 0 on success, < 0 on error
985 static int snapshot_metadata(uint64_t key
, char *path
, uint64_t relayd_id
,
986 struct lttng_consumer_local_data
*ctx
)
989 struct lttng_consumer_channel
*metadata_channel
;
990 struct lttng_consumer_stream
*metadata_stream
;
995 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
1000 metadata_channel
= consumer_find_channel(key
);
1001 if (!metadata_channel
) {
1002 ERR("UST snapshot metadata channel not found for key %" PRIu64
,
1007 assert(!metadata_channel
->monitor
);
1009 health_code_update();
1012 * Ask the sessiond if we have new metadata waiting and update the
1013 * consumer metadata cache.
1015 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1020 health_code_update();
1023 * The metadata stream is NOT created in no monitor mode when the channel
1024 * is created on a sessiond ask channel command.
1026 ret
= create_ust_streams(metadata_channel
, ctx
);
1031 metadata_stream
= metadata_channel
->metadata_stream
;
1032 assert(metadata_stream
);
1034 if (relayd_id
!= (uint64_t) -1ULL) {
1035 metadata_stream
->relayd_id
= relayd_id
;
1036 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1041 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
1042 metadata_stream
->chan
->tracefile_size
,
1043 metadata_stream
->tracefile_count_current
,
1044 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
1048 metadata_stream
->out_fd
= ret
;
1049 metadata_stream
->tracefile_size_current
= 0;
1053 health_code_update();
1055 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
1063 * Clean up the stream completly because the next snapshot will use a new
1066 consumer_stream_destroy(metadata_stream
, NULL
);
1067 cds_list_del(&metadata_stream
->send_node
);
1068 metadata_channel
->metadata_stream
= NULL
;
1076 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1080 unsigned long mmap_offset
;
1081 const char *mmap_base
;
1083 mmap_base
= ustctl_get_mmap_base(stream
->ustream
);
1085 ERR("Failed to get mmap base for stream `%s`",
1091 ret
= ustctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1093 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1098 *addr
= mmap_base
+ mmap_offset
;
1105 * Take a snapshot of all the stream of a channel.
1107 * Returns 0 on success, < 0 on error
1109 static int snapshot_channel(uint64_t key
, char *path
, uint64_t relayd_id
,
1110 uint64_t nb_packets_per_stream
, struct lttng_consumer_local_data
*ctx
)
1113 unsigned use_relayd
= 0;
1114 unsigned long consumed_pos
, produced_pos
;
1115 struct lttng_consumer_channel
*channel
;
1116 struct lttng_consumer_stream
*stream
;
1123 if (relayd_id
!= (uint64_t) -1ULL) {
1127 channel
= consumer_find_channel(key
);
1129 ERR("UST snapshot channel not found for key %" PRIu64
, key
);
1133 assert(!channel
->monitor
);
1134 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1136 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1137 health_code_update();
1139 /* Lock stream because we are about to change its state. */
1140 pthread_mutex_lock(&stream
->lock
);
1141 stream
->relayd_id
= relayd_id
;
1144 ret
= consumer_send_relayd_stream(stream
, path
);
1149 ret
= utils_create_stream_file(path
, stream
->name
,
1150 stream
->chan
->tracefile_size
,
1151 stream
->tracefile_count_current
,
1152 stream
->uid
, stream
->gid
, NULL
);
1156 stream
->out_fd
= ret
;
1157 stream
->tracefile_size_current
= 0;
1159 DBG("UST consumer snapshot stream %s/%s (%" PRIu64
")", path
,
1160 stream
->name
, stream
->key
);
1162 if (relayd_id
!= -1ULL) {
1163 ret
= consumer_send_relayd_streams_sent(relayd_id
);
1170 * If tracing is active, we want to perform a "full" buffer flush.
1171 * Else, if quiescent, it has already been done by the prior stop.
1173 if (!stream
->quiescent
) {
1174 ustctl_flush_buffer(stream
->ustream
, 0);
1177 ret
= lttng_ustconsumer_take_snapshot(stream
);
1179 ERR("Taking UST snapshot");
1183 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1185 ERR("Produced UST snapshot position");
1189 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1191 ERR("Consumerd UST snapshot position");
1196 * The original value is sent back if max stream size is larger than
1197 * the possible size of the snapshot. Also, we assume that the session
1198 * daemon should never send a maximum stream size that is lower than
1201 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1202 produced_pos
, nb_packets_per_stream
,
1203 stream
->max_sb_size
);
1205 while (consumed_pos
< produced_pos
) {
1207 unsigned long len
, padded_len
;
1208 const char *subbuf_addr
;
1209 struct lttng_buffer_view subbuf_view
;
1211 health_code_update();
1213 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1215 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1217 if (ret
!= -EAGAIN
) {
1218 PERROR("ustctl_get_subbuf snapshot");
1219 goto error_close_stream
;
1221 DBG("UST consumer get subbuf failed. Skipping it.");
1222 consumed_pos
+= stream
->max_sb_size
;
1223 stream
->chan
->lost_packets
++;
1227 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1229 ERR("Snapshot ustctl_get_subbuf_size");
1230 goto error_put_subbuf
;
1233 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1235 ERR("Snapshot ustctl_get_padded_subbuf_size");
1236 goto error_put_subbuf
;
1239 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1241 goto error_put_subbuf
;
1244 subbuf_view
= lttng_buffer_view_init(
1245 subbuf_addr
, 0, padded_len
);
1246 read_len
= lttng_consumer_on_read_subbuffer_mmap(
1247 stream
, &subbuf_view
, padded_len
- len
);
1249 if (read_len
!= len
) {
1251 goto error_put_subbuf
;
1254 if (read_len
!= padded_len
) {
1256 goto error_put_subbuf
;
1260 ret
= ustctl_put_subbuf(stream
->ustream
);
1262 ERR("Snapshot ustctl_put_subbuf");
1263 goto error_close_stream
;
1265 consumed_pos
+= stream
->max_sb_size
;
1268 /* Simply close the stream so we can use it on the next snapshot. */
1269 consumer_stream_close(stream
);
1270 pthread_mutex_unlock(&stream
->lock
);
1277 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1278 ERR("Snapshot ustctl_put_subbuf");
1281 consumer_stream_close(stream
);
1283 pthread_mutex_unlock(&stream
->lock
);
1290 * Receive the metadata updates from the sessiond. Supports receiving
1291 * overlapping metadata, but is needs to always belong to a contiguous
1292 * range starting from 0.
1293 * Be careful about the locks held when calling this function: it needs
1294 * the metadata cache flush to concurrently progress in order to
1297 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1298 uint64_t len
, uint64_t version
,
1299 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1301 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1304 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1306 metadata_str
= zmalloc(len
* sizeof(char));
1307 if (!metadata_str
) {
1308 PERROR("zmalloc metadata string");
1309 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1313 health_code_update();
1315 /* Receive metadata string. */
1316 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1318 /* Session daemon is dead so return gracefully. */
1323 health_code_update();
1325 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1326 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1329 /* Unable to handle metadata. Notify session daemon. */
1330 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1332 * Skip metadata flush on write error since the offset and len might
1333 * not have been updated which could create an infinite loop below when
1334 * waiting for the metadata cache to be flushed.
1336 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1339 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1344 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1345 DBG("Waiting for metadata to be flushed");
1347 health_code_update();
1349 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1359 * Receive command from session daemon and process it.
1361 * Return 1 on success else a negative value or 0.
1363 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1364 int sock
, struct pollfd
*consumer_sockpoll
)
1367 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1368 struct lttcomm_consumer_msg msg
;
1369 struct lttng_consumer_channel
*channel
= NULL
;
1371 health_code_update();
1373 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1374 if (ret
!= sizeof(msg
)) {
1375 DBG("Consumer received unexpected message size %zd (expects %zu)",
1378 * The ret value might 0 meaning an orderly shutdown but this is ok
1379 * since the caller handles this.
1382 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1388 health_code_update();
1391 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1393 health_code_update();
1395 /* relayd needs RCU read-side lock */
1398 switch (msg
.cmd_type
) {
1399 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1401 /* Session daemon status message are handled in the following call. */
1402 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1403 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1404 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1405 msg
.u
.relayd_sock
.relayd_session_id
);
1408 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1410 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1411 struct consumer_relayd_sock_pair
*relayd
;
1413 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1415 /* Get relayd reference if exists. */
1416 relayd
= consumer_find_relayd(index
);
1417 if (relayd
== NULL
) {
1418 DBG("Unable to find relayd %" PRIu64
, index
);
1419 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1423 * Each relayd socket pair has a refcount of stream attached to it
1424 * which tells if the relayd is still active or not depending on the
1427 * This will set the destroy flag of the relayd object and destroy it
1428 * if the refcount reaches zero when called.
1430 * The destroy can happen either here or when a stream fd hangs up.
1433 consumer_flag_relayd_for_destroy(relayd
);
1436 goto end_msg_sessiond
;
1438 case LTTNG_CONSUMER_UPDATE_STREAM
:
1443 case LTTNG_CONSUMER_DATA_PENDING
:
1445 int ret
, is_data_pending
;
1446 uint64_t id
= msg
.u
.data_pending
.session_id
;
1448 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1450 is_data_pending
= consumer_data_pending(id
);
1452 /* Send back returned value to session daemon */
1453 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1454 sizeof(is_data_pending
));
1456 DBG("Error when sending the data pending ret code: %d", ret
);
1461 * No need to send back a status message since the data pending
1462 * returned value is the response.
1466 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1469 struct ustctl_consumer_channel_attr attr
;
1471 /* Create a plain object and reserve a channel key. */
1472 channel
= consumer_allocate_channel(
1473 msg
.u
.ask_channel
.key
,
1474 msg
.u
.ask_channel
.session_id
,
1475 msg
.u
.ask_channel
.pathname
,
1476 msg
.u
.ask_channel
.name
,
1477 msg
.u
.ask_channel
.uid
,
1478 msg
.u
.ask_channel
.gid
,
1479 msg
.u
.ask_channel
.relayd_id
,
1480 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1481 msg
.u
.ask_channel
.tracefile_size
,
1482 msg
.u
.ask_channel
.tracefile_count
,
1483 msg
.u
.ask_channel
.session_id_per_pid
,
1484 msg
.u
.ask_channel
.monitor
,
1485 msg
.u
.ask_channel
.live_timer_interval
,
1486 msg
.u
.ask_channel
.is_live
,
1487 msg
.u
.ask_channel
.root_shm_path
,
1488 msg
.u
.ask_channel
.shm_path
);
1490 goto end_channel_error
;
1494 * Assign UST application UID to the channel. This value is ignored for
1495 * per PID buffers. This is specific to UST thus setting this after the
1498 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1500 /* Build channel attributes from received message. */
1501 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1502 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1503 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1504 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1505 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1506 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1507 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1509 /* Match channel buffer type to the UST abi. */
1510 switch (msg
.u
.ask_channel
.output
) {
1511 case LTTNG_EVENT_MMAP
:
1513 attr
.output
= LTTNG_UST_MMAP
;
1517 /* Translate and save channel type. */
1518 switch (msg
.u
.ask_channel
.type
) {
1519 case LTTNG_UST_CHAN_PER_CPU
:
1520 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1521 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1523 * Set refcount to 1 for owner. Below, we will
1524 * pass ownership to the
1525 * consumer_thread_channel_poll() thread.
1527 channel
->refcount
= 1;
1529 case LTTNG_UST_CHAN_METADATA
:
1530 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1531 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1538 health_code_update();
1540 ret
= ask_channel(ctx
, sock
, channel
, &attr
);
1542 goto end_channel_error
;
1545 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1546 ret
= consumer_metadata_cache_allocate(channel
);
1548 ERR("Allocating metadata cache");
1549 goto end_channel_error
;
1551 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1552 attr
.switch_timer_interval
= 0;
1554 consumer_timer_live_start(channel
,
1555 msg
.u
.ask_channel
.live_timer_interval
);
1558 health_code_update();
1561 * Add the channel to the internal state AFTER all streams were created
1562 * and successfully sent to session daemon. This way, all streams must
1563 * be ready before this channel is visible to the threads.
1564 * If add_channel succeeds, ownership of the channel is
1565 * passed to consumer_thread_channel_poll().
1567 ret
= add_channel(channel
, ctx
);
1569 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1570 if (channel
->switch_timer_enabled
== 1) {
1571 consumer_timer_switch_stop(channel
);
1573 consumer_metadata_cache_destroy(channel
);
1575 if (channel
->live_timer_enabled
== 1) {
1576 consumer_timer_live_stop(channel
);
1578 goto end_channel_error
;
1581 health_code_update();
1584 * Channel and streams are now created. Inform the session daemon that
1585 * everything went well and should wait to receive the channel and
1586 * streams with ustctl API.
1588 ret
= consumer_send_status_channel(sock
, channel
);
1591 * There is probably a problem on the socket.
1598 case LTTNG_CONSUMER_GET_CHANNEL
:
1600 int ret
, relayd_err
= 0;
1601 uint64_t key
= msg
.u
.get_channel
.key
;
1602 struct lttng_consumer_channel
*channel
;
1604 channel
= consumer_find_channel(key
);
1606 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1607 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1608 goto end_msg_sessiond
;
1611 health_code_update();
1613 /* Send everything to sessiond. */
1614 ret
= send_sessiond_channel(sock
, channel
, ctx
, &relayd_err
);
1618 * We were unable to send to the relayd the stream so avoid
1619 * sending back a fatal error to the thread since this is OK
1620 * and the consumer can continue its work. The above call
1621 * has sent the error status message to the sessiond.
1626 * The communicaton was broken hence there is a bad state between
1627 * the consumer and sessiond so stop everything.
1632 health_code_update();
1635 * In no monitor mode, the streams ownership is kept inside the channel
1636 * so don't send them to the data thread.
1638 if (!channel
->monitor
) {
1639 goto end_msg_sessiond
;
1642 ret
= send_streams_to_thread(channel
, ctx
);
1645 * If we are unable to send the stream to the thread, there is
1646 * a big problem so just stop everything.
1650 /* List MUST be empty after or else it could be reused. */
1651 assert(cds_list_empty(&channel
->streams
.head
));
1652 goto end_msg_sessiond
;
1654 case LTTNG_CONSUMER_CHANNEL_STOP_LIVE_TIMER
:
1656 uint64_t key
= msg
.u
.get_channel
.key
;
1657 struct lttng_consumer_channel
*channel
;
1659 channel
= consumer_find_channel(key
);
1661 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1662 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1663 goto end_msg_sessiond
;
1666 health_code_update();
1668 if (channel
->live_timer_enabled
== 1) {
1669 consumer_timer_live_stop(channel
);
1672 health_code_update();
1674 goto end_msg_sessiond
;
1676 case LTTNG_CONSUMER_CHANNEL_START_LIVE_TIMER
:
1678 uint64_t key
= msg
.u
.get_channel
.key
;
1679 struct lttng_consumer_channel
*channel
;
1681 channel
= consumer_find_channel(key
);
1683 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1684 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1685 goto end_msg_sessiond
;
1688 health_code_update();
1690 if (channel
->live_timer_enabled
== 0) {
1691 consumer_timer_live_start(channel
, channel
->live_timer_interval
);
1694 health_code_update();
1696 goto end_msg_sessiond
;
1699 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1701 uint64_t key
= msg
.u
.destroy_channel
.key
;
1704 * Only called if streams have not been sent to stream
1705 * manager thread. However, channel has been sent to
1706 * channel manager thread.
1708 notify_thread_del_channel(ctx
, key
);
1709 goto end_msg_sessiond
;
1711 case LTTNG_CONSUMER_CLOSE_METADATA
:
1715 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1720 goto end_msg_sessiond
;
1722 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1726 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1731 goto end_msg_sessiond
;
1733 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1737 ret
= clear_quiescent_channel(
1738 msg
.u
.clear_quiescent_channel
.key
);
1743 goto end_msg_sessiond
;
1745 case LTTNG_CONSUMER_PUSH_METADATA
:
1748 uint64_t len
= msg
.u
.push_metadata
.len
;
1749 uint64_t key
= msg
.u
.push_metadata
.key
;
1750 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1751 uint64_t version
= msg
.u
.push_metadata
.version
;
1752 struct lttng_consumer_channel
*channel
;
1754 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1757 channel
= consumer_find_channel(key
);
1760 * This is possible if the metadata creation on the consumer side
1761 * is in flight vis-a-vis a concurrent push metadata from the
1762 * session daemon. Simply return that the channel failed and the
1763 * session daemon will handle that message correctly considering
1764 * that this race is acceptable thus the DBG() statement here.
1766 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1767 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1768 goto end_msg_sessiond
;
1771 health_code_update();
1775 * There is nothing to receive. We have simply
1776 * checked whether the channel can be found.
1778 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1779 goto end_msg_sessiond
;
1782 /* Tell session daemon we are ready to receive the metadata. */
1783 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1785 /* Somehow, the session daemon is not responding anymore. */
1789 health_code_update();
1791 /* Wait for more data. */
1792 health_poll_entry();
1793 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1799 health_code_update();
1801 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1802 len
, version
, channel
, 0, 1);
1804 /* error receiving from sessiond */
1808 goto end_msg_sessiond
;
1811 case LTTNG_CONSUMER_SETUP_METADATA
:
1815 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1819 goto end_msg_sessiond
;
1821 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1823 if (msg
.u
.snapshot_channel
.metadata
) {
1824 ret
= snapshot_metadata(msg
.u
.snapshot_channel
.key
,
1825 msg
.u
.snapshot_channel
.pathname
,
1826 msg
.u
.snapshot_channel
.relayd_id
,
1829 ERR("Snapshot metadata failed");
1830 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1833 ret
= snapshot_channel(msg
.u
.snapshot_channel
.key
,
1834 msg
.u
.snapshot_channel
.pathname
,
1835 msg
.u
.snapshot_channel
.relayd_id
,
1836 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1839 ERR("Snapshot channel failed");
1840 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1844 health_code_update();
1845 ret
= consumer_send_status_msg(sock
, ret_code
);
1847 /* Somehow, the session daemon is not responding anymore. */
1850 health_code_update();
1853 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1856 uint64_t discarded_events
;
1857 struct lttng_ht_iter iter
;
1858 struct lttng_ht
*ht
;
1859 struct lttng_consumer_stream
*stream
;
1860 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1861 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1863 DBG("UST consumer discarded events command for session id %"
1866 pthread_mutex_lock(&consumer_data
.lock
);
1868 ht
= consumer_data
.stream_list_ht
;
1871 * We only need a reference to the channel, but they are not
1872 * directly indexed, so we just use the first matching stream
1873 * to extract the information we need, we default to 0 if not
1874 * found (no events are dropped if the channel is not yet in
1877 discarded_events
= 0;
1878 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1879 ht
->hash_fct(&id
, lttng_ht_seed
),
1881 &iter
.iter
, stream
, node_session_id
.node
) {
1882 if (stream
->chan
->key
== key
) {
1883 discarded_events
= stream
->chan
->discarded_events
;
1887 pthread_mutex_unlock(&consumer_data
.lock
);
1890 DBG("UST consumer discarded events command for session id %"
1891 PRIu64
", channel key %" PRIu64
, id
, key
);
1893 health_code_update();
1895 /* Send back returned value to session daemon */
1896 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1898 PERROR("send discarded events");
1904 case LTTNG_CONSUMER_LOST_PACKETS
:
1907 uint64_t lost_packets
;
1908 struct lttng_ht_iter iter
;
1909 struct lttng_ht
*ht
;
1910 struct lttng_consumer_stream
*stream
;
1911 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1912 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1914 DBG("UST consumer lost packets command for session id %"
1917 pthread_mutex_lock(&consumer_data
.lock
);
1919 ht
= consumer_data
.stream_list_ht
;
1922 * We only need a reference to the channel, but they are not
1923 * directly indexed, so we just use the first matching stream
1924 * to extract the information we need, we default to 0 if not
1925 * found (no packets lost if the channel is not yet in use).
1928 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1929 ht
->hash_fct(&id
, lttng_ht_seed
),
1931 &iter
.iter
, stream
, node_session_id
.node
) {
1932 if (stream
->chan
->key
== key
) {
1933 lost_packets
= stream
->chan
->lost_packets
;
1937 pthread_mutex_unlock(&consumer_data
.lock
);
1940 DBG("UST consumer lost packets command for session id %"
1941 PRIu64
", channel key %" PRIu64
, id
, key
);
1943 health_code_update();
1945 /* Send back returned value to session daemon */
1946 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1947 sizeof(lost_packets
));
1949 PERROR("send lost packets");
1962 health_code_update();
1965 * Return 1 to indicate success since the 0 value can be a socket
1966 * shutdown during the recv() or send() call.
1972 * The returned value here is not useful since either way we'll return 1 to
1973 * the caller because the session daemon socket management is done
1974 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1976 ret
= consumer_send_status_msg(sock
, ret_code
);
1982 health_code_update();
1988 * Free channel here since no one has a reference to it. We don't
1989 * free after that because a stream can store this pointer.
1991 destroy_channel(channel
);
1993 /* We have to send a status channel message indicating an error. */
1994 ret
= consumer_send_status_channel(sock
, NULL
);
1996 /* Stop everything if session daemon can not be notified. */
2001 health_code_update();
2006 /* This will issue a consumer stop. */
2010 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
2011 int producer_active
)
2014 assert(stream
->ustream
);
2016 ustctl_flush_buffer(stream
->ustream
, producer_active
);
2020 * Take a snapshot for a specific fd
2022 * Returns 0 on success, < 0 on error
2024 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2027 assert(stream
->ustream
);
2029 return ustctl_snapshot(stream
->ustream
);
2033 * Get the produced position
2035 * Returns 0 on success, < 0 on error
2037 int lttng_ustconsumer_get_produced_snapshot(
2038 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2041 assert(stream
->ustream
);
2044 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2048 * Get the consumed position
2050 * Returns 0 on success, < 0 on error
2052 int lttng_ustconsumer_get_consumed_snapshot(
2053 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2056 assert(stream
->ustream
);
2059 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2062 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2066 assert(stream
->ustream
);
2068 ustctl_flush_buffer(stream
->ustream
, producer
);
2071 int lttng_ustconsumer_get_current_timestamp(
2072 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2075 assert(stream
->ustream
);
2078 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2081 int lttng_ustconsumer_get_sequence_number(
2082 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2085 assert(stream
->ustream
);
2088 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2092 * Called when the stream signals the consumer that it has hung up.
2094 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2097 assert(stream
->ustream
);
2099 pthread_mutex_lock(&stream
->lock
);
2100 if (!stream
->quiescent
) {
2101 ustctl_flush_buffer(stream
->ustream
, 0);
2102 stream
->quiescent
= true;
2104 pthread_mutex_unlock(&stream
->lock
);
2105 stream
->hangup_flush_done
= 1;
2108 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2113 assert(chan
->uchan
);
2115 if (chan
->switch_timer_enabled
== 1) {
2116 consumer_timer_switch_stop(chan
);
2118 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2121 ret
= close(chan
->stream_fds
[i
]);
2125 if (chan
->shm_path
[0]) {
2126 char shm_path
[PATH_MAX
];
2128 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2130 ERR("Cannot get stream shm path");
2132 ret
= run_as_unlink(shm_path
, chan
->uid
, chan
->gid
);
2134 PERROR("unlink %s", shm_path
);
2140 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2143 assert(chan
->uchan
);
2145 consumer_metadata_cache_destroy(chan
);
2146 ustctl_destroy_channel(chan
->uchan
);
2147 /* Try to rmdir all directories under shm_path root. */
2148 if (chan
->root_shm_path
[0]) {
2149 (void) run_as_recursive_rmdir(chan
->root_shm_path
,
2150 chan
->uid
, chan
->gid
);
2152 free(chan
->stream_fds
);
2155 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2158 assert(stream
->ustream
);
2160 if (stream
->chan
->switch_timer_enabled
== 1) {
2161 consumer_timer_switch_stop(stream
->chan
);
2163 ustctl_destroy_stream(stream
->ustream
);
2166 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2169 assert(stream
->ustream
);
2171 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2174 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2177 assert(stream
->ustream
);
2179 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2183 void metadata_stream_reset_cache_consumed_position(
2184 struct lttng_consumer_stream
*stream
)
2186 DBG("Reset metadata cache of session %" PRIu64
,
2187 stream
->chan
->session_id
);
2188 stream
->ust_metadata_pushed
= 0;
2192 * Write up to one packet from the metadata cache to the channel.
2194 * Returns the number of bytes pushed in the cache, or a negative value
2198 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2203 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2204 if (stream
->chan
->metadata_cache
->max_offset
==
2205 stream
->ust_metadata_pushed
) {
2207 * In the context of a user space metadata channel, a
2208 * change in version can be detected in two ways:
2209 * 1) During the pre-consume of the `read_subbuffer` loop,
2210 * 2) When populating the metadata ring buffer (i.e. here).
2212 * This function is invoked when there is no metadata
2213 * available in the ring-buffer. If all data was consumed
2214 * up to the size of the metadata cache, there is no metadata
2215 * to insert in the ring-buffer.
2217 * However, the metadata version could still have changed (a
2218 * regeneration without any new data will yield the same cache
2221 * The cache's version is checked for a version change and the
2222 * consumed position is reset if one occurred.
2224 * This check is only necessary for the user space domain as
2225 * it has to manage the cache explicitly. If this reset was not
2226 * performed, no metadata would be consumed (and no reset would
2227 * occur as part of the pre-consume) until the metadata size
2228 * exceeded the cache size.
2230 if (stream
->metadata_version
!=
2231 stream
->chan
->metadata_cache
->version
) {
2232 metadata_stream_reset_cache_consumed_position(stream
);
2233 consumer_stream_metadata_set_version(stream
,
2234 stream
->chan
->metadata_cache
->version
);
2241 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2242 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2243 stream
->chan
->metadata_cache
->max_offset
2244 - stream
->ust_metadata_pushed
);
2245 assert(write_len
!= 0);
2246 if (write_len
< 0) {
2247 ERR("Writing one metadata packet");
2251 stream
->ust_metadata_pushed
+= write_len
;
2253 assert(stream
->chan
->metadata_cache
->max_offset
>=
2254 stream
->ust_metadata_pushed
);
2258 * Switch packet (but don't open the next one) on every commit of
2259 * a metadata packet. Since the subbuffer is fully filled (with padding,
2260 * if needed), the stream is "quiescent" after this commit.
2262 ustctl_flush_buffer(stream
->ustream
, 1);
2264 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2270 * Sync metadata meaning request them to the session daemon and snapshot to the
2271 * metadata thread can consumer them.
2273 * Metadata stream lock is held here, but we need to release it when
2274 * interacting with sessiond, else we cause a deadlock with live
2275 * awaiting on metadata to be pushed out.
2277 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
2278 * is empty or a negative value on error.
2280 int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2281 struct lttng_consumer_stream
*metadata
)
2289 pthread_mutex_unlock(&metadata
->lock
);
2291 * Request metadata from the sessiond, but don't wait for the flush
2292 * because we locked the metadata thread.
2294 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata
->chan
, 0, 0);
2295 pthread_mutex_lock(&metadata
->lock
);
2300 ret
= commit_one_metadata_packet(metadata
);
2303 } else if (ret
> 0) {
2307 ustctl_flush_buffer(metadata
->ustream
, 1);
2308 ret
= ustctl_snapshot(metadata
->ustream
);
2310 if (errno
!= EAGAIN
) {
2311 ERR("Sync metadata, taking UST snapshot");
2314 DBG("No new metadata when syncing them.");
2315 /* No new metadata, exit. */
2321 * After this flush, we still need to extract metadata.
2332 * Return 0 on success else a negative value.
2334 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2335 struct lttng_consumer_local_data
*ctx
)
2338 struct ustctl_consumer_stream
*ustream
;
2343 ustream
= stream
->ustream
;
2346 * First, we are going to check if there is a new subbuffer available
2347 * before reading the stream wait_fd.
2349 /* Get the next subbuffer */
2350 ret
= ustctl_get_next_subbuf(ustream
);
2352 /* No more data found, flag the stream. */
2353 stream
->has_data
= 0;
2358 ret
= ustctl_put_subbuf(ustream
);
2361 /* This stream still has data. Flag it and wake up the data thread. */
2362 stream
->has_data
= 1;
2364 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2367 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2368 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2373 /* The wake up pipe has been notified. */
2374 ctx
->has_wakeup
= 1;
2382 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream
*stream
)
2387 * We can consume the 1 byte written into the wait_fd by
2388 * UST. Don't trigger error if we cannot read this one byte
2389 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2391 * This is only done when the stream is monitored by a thread,
2392 * before the flush is done after a hangup and if the stream
2393 * is not flagged with data since there might be nothing to
2394 * consume in the wait fd but still have data available
2395 * flagged by the consumer wake up pipe.
2397 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2401 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2402 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2410 static int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
2411 struct stream_subbuffer
*subbuf
)
2415 ret
= ustctl_get_subbuf_size(
2416 stream
->ustream
, &subbuf
->info
.data
.subbuf_size
);
2421 ret
= ustctl_get_padded_subbuf_size(
2422 stream
->ustream
, &subbuf
->info
.data
.padded_subbuf_size
);
2431 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
2432 struct stream_subbuffer
*subbuf
)
2436 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2441 subbuf
->info
.metadata
.version
= stream
->metadata_version
;
2447 static int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
2448 struct stream_subbuffer
*subbuf
)
2452 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2457 ret
= ustctl_get_packet_size(
2458 stream
->ustream
, &subbuf
->info
.data
.packet_size
);
2460 PERROR("Failed to get sub-buffer packet size");
2464 ret
= ustctl_get_content_size(
2465 stream
->ustream
, &subbuf
->info
.data
.content_size
);
2467 PERROR("Failed to get sub-buffer content size");
2471 ret
= ustctl_get_timestamp_begin(
2472 stream
->ustream
, &subbuf
->info
.data
.timestamp_begin
);
2474 PERROR("Failed to get sub-buffer begin timestamp");
2478 ret
= ustctl_get_timestamp_end(
2479 stream
->ustream
, &subbuf
->info
.data
.timestamp_end
);
2481 PERROR("Failed to get sub-buffer end timestamp");
2485 ret
= ustctl_get_events_discarded(
2486 stream
->ustream
, &subbuf
->info
.data
.events_discarded
);
2488 PERROR("Failed to get sub-buffer events discarded count");
2492 ret
= ustctl_get_sequence_number(stream
->ustream
,
2493 &subbuf
->info
.data
.sequence_number
.value
);
2495 /* May not be supported by older LTTng-modules. */
2496 if (ret
!= -ENOTTY
) {
2497 PERROR("Failed to get sub-buffer sequence number");
2501 subbuf
->info
.data
.sequence_number
.is_set
= true;
2504 ret
= ustctl_get_stream_id(
2505 stream
->ustream
, &subbuf
->info
.data
.stream_id
);
2507 PERROR("Failed to get stream id");
2511 ret
= ustctl_get_instance_id(stream
->ustream
,
2512 &subbuf
->info
.data
.stream_instance_id
.value
);
2514 /* May not be supported by older LTTng-modules. */
2515 if (ret
!= -ENOTTY
) {
2516 PERROR("Failed to get stream instance id");
2520 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
2526 static int get_next_subbuffer_common(struct lttng_consumer_stream
*stream
,
2527 struct stream_subbuffer
*subbuffer
)
2532 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
2538 ret
= get_current_subbuf_addr(stream
, &addr
);
2543 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
2544 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
2545 assert(subbuffer
->buffer
.buffer
.data
!= NULL
);
2550 static int get_next_subbuffer(struct lttng_consumer_stream
*stream
,
2551 struct stream_subbuffer
*subbuffer
)
2555 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2560 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2568 static int get_next_subbuffer_metadata(struct lttng_consumer_stream
*stream
,
2569 struct stream_subbuffer
*subbuffer
)
2576 unsigned long consumed_pos
, produced_pos
;
2579 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2581 got_subbuffer
= true;
2583 got_subbuffer
= false;
2584 if (ret
!= -EAGAIN
) {
2591 * Determine if the cache is empty and ensure that a sub-buffer
2592 * is made available if the cache is not empty.
2594 if (!got_subbuffer
) {
2595 ret
= commit_one_metadata_packet(stream
);
2596 if (ret
< 0 && ret
!= -ENOBUFS
) {
2598 } else if (ret
== 0) {
2599 /* Not an error, the cache is empty. */
2604 cache_empty
= false;
2607 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2608 cache_empty
= stream
->chan
->metadata_cache
->max_offset
==
2609 stream
->ust_metadata_pushed
;
2610 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2612 } while (!got_subbuffer
);
2614 /* Populate sub-buffer infos and view. */
2615 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2620 ret
= lttng_ustconsumer_take_snapshot(stream
);
2623 * -EAGAIN is not expected since we got a sub-buffer and haven't
2624 * pushed the consumption position yet (on put_next).
2626 PERROR("Failed to take a snapshot of metadata buffer positions");
2630 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
2632 PERROR("Failed to get metadata consumed position");
2636 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
2638 PERROR("Failed to get metadata produced position");
2642 /* Last sub-buffer of the ring buffer ? */
2643 buffer_empty
= (consumed_pos
+ stream
->max_sb_size
) == produced_pos
;
2646 * The sessiond registry lock ensures that coherent units of metadata
2647 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
2648 * acquired, the cache is empty, and it is the only available sub-buffer
2649 * available, it is safe to assume that it is "coherent".
2651 coherent
= got_subbuffer
&& cache_empty
&& buffer_empty
;
2653 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
2658 static int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
2659 struct stream_subbuffer
*subbuffer
)
2661 const int ret
= ustctl_put_next_subbuf(stream
->ustream
);
2667 static int signal_metadata(struct lttng_consumer_stream
*stream
,
2668 struct lttng_consumer_local_data
*ctx
)
2670 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
2673 static int lttng_ustconsumer_set_stream_ops(
2674 struct lttng_consumer_stream
*stream
)
2678 stream
->read_subbuffer_ops
.on_wake_up
= consumer_stream_ust_on_wake_up
;
2679 if (stream
->metadata_flag
) {
2680 stream
->read_subbuffer_ops
.get_next_subbuffer
=
2681 get_next_subbuffer_metadata
;
2682 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
2683 extract_metadata_subbuffer_info
;
2684 stream
->read_subbuffer_ops
.reset_metadata
=
2685 metadata_stream_reset_cache_consumed_position
;
2686 if (stream
->chan
->is_live
) {
2687 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
2688 ret
= consumer_stream_enable_metadata_bucketization(
2695 stream
->read_subbuffer_ops
.get_next_subbuffer
=
2697 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
2698 extract_data_subbuffer_info
;
2699 stream
->read_subbuffer_ops
.on_sleep
= notify_if_more_data
;
2700 if (stream
->chan
->is_live
) {
2701 stream
->read_subbuffer_ops
.send_live_beacon
=
2702 consumer_flush_ust_index
;
2706 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
2712 * Called when a stream is created.
2714 * Return 0 on success or else a negative value.
2716 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2722 /* Don't create anything if this is set for streaming. */
2723 if (stream
->relayd_id
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
2724 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
2725 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
2726 stream
->uid
, stream
->gid
, NULL
);
2730 stream
->out_fd
= ret
;
2731 stream
->tracefile_size_current
= 0;
2733 if (!stream
->metadata_flag
) {
2734 struct lttng_index_file
*index_file
;
2736 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
2737 stream
->name
, stream
->uid
, stream
->gid
,
2738 stream
->chan
->tracefile_size
,
2739 stream
->tracefile_count_current
,
2740 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
2744 stream
->index_file
= index_file
;
2748 lttng_ustconsumer_set_stream_ops(stream
);
2756 * Check if data is still being extracted from the buffers for a specific
2757 * stream. Consumer data lock MUST be acquired before calling this function
2758 * and the stream lock.
2760 * Return 1 if the traced data are still getting read else 0 meaning that the
2761 * data is available for trace viewer reading.
2763 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
2768 assert(stream
->ustream
);
2770 DBG("UST consumer checking data pending");
2772 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
2777 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
2778 uint64_t contiguous
, pushed
;
2780 /* Ease our life a bit. */
2781 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
2782 pushed
= stream
->ust_metadata_pushed
;
2785 * We can simply check whether all contiguously available data
2786 * has been pushed to the ring buffer, since the push operation
2787 * is performed within get_next_subbuf(), and because both
2788 * get_next_subbuf() and put_next_subbuf() are issued atomically
2789 * thanks to the stream lock within
2790 * lttng_ustconsumer_read_subbuffer(). This basically means that
2791 * whetnever ust_metadata_pushed is incremented, the associated
2792 * metadata has been consumed from the metadata stream.
2794 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
2795 contiguous
, pushed
);
2796 assert(((int64_t) (contiguous
- pushed
)) >= 0);
2797 if ((contiguous
!= pushed
) ||
2798 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
2799 ret
= 1; /* Data is pending */
2803 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2806 * There is still data so let's put back this
2809 ret
= ustctl_put_subbuf(stream
->ustream
);
2811 ret
= 1; /* Data is pending */
2816 /* Data is NOT pending so ready to be read. */
2824 * Stop a given metadata channel timer if enabled and close the wait fd which
2825 * is the poll pipe of the metadata stream.
2827 * This MUST be called with the metadata channel acquired.
2829 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
2834 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
2836 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
2838 if (metadata
->switch_timer_enabled
== 1) {
2839 consumer_timer_switch_stop(metadata
);
2842 if (!metadata
->metadata_stream
) {
2847 * Closing write side so the thread monitoring the stream wakes up if any
2848 * and clean the metadata stream.
2850 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
2851 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
2853 PERROR("closing metadata pipe write side");
2855 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
2863 * Close every metadata stream wait fd of the metadata hash table. This
2864 * function MUST be used very carefully so not to run into a race between the
2865 * metadata thread handling streams and this function closing their wait fd.
2867 * For UST, this is used when the session daemon hangs up. Its the metadata
2868 * producer so calling this is safe because we are assured that no state change
2869 * can occur in the metadata thread for the streams in the hash table.
2871 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
2873 struct lttng_ht_iter iter
;
2874 struct lttng_consumer_stream
*stream
;
2876 assert(metadata_ht
);
2877 assert(metadata_ht
->ht
);
2879 DBG("UST consumer closing all metadata streams");
2882 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
2885 health_code_update();
2887 pthread_mutex_lock(&stream
->chan
->lock
);
2888 lttng_ustconsumer_close_metadata(stream
->chan
);
2889 pthread_mutex_unlock(&stream
->chan
->lock
);
2895 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
2899 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
2901 ERR("Unable to close wakeup fd");
2906 * Please refer to consumer-timer.c before adding any lock within this
2907 * function or any of its callees. Timers have a very strict locking
2908 * semantic with respect to teardown. Failure to respect this semantic
2909 * introduces deadlocks.
2911 * DON'T hold the metadata lock when calling this function, else this
2912 * can cause deadlock involving consumer awaiting for metadata to be
2913 * pushed out due to concurrent interaction with the session daemon.
2915 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
2916 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
2918 struct lttcomm_metadata_request_msg request
;
2919 struct lttcomm_consumer_msg msg
;
2920 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
2921 uint64_t len
, key
, offset
, version
;
2925 assert(channel
->metadata_cache
);
2927 memset(&request
, 0, sizeof(request
));
2929 /* send the metadata request to sessiond */
2930 switch (consumer_data
.type
) {
2931 case LTTNG_CONSUMER64_UST
:
2932 request
.bits_per_long
= 64;
2934 case LTTNG_CONSUMER32_UST
:
2935 request
.bits_per_long
= 32;
2938 request
.bits_per_long
= 0;
2942 request
.session_id
= channel
->session_id
;
2943 request
.session_id_per_pid
= channel
->session_id_per_pid
;
2945 * Request the application UID here so the metadata of that application can
2946 * be sent back. The channel UID corresponds to the user UID of the session
2947 * used for the rights on the stream file(s).
2949 request
.uid
= channel
->ust_app_uid
;
2950 request
.key
= channel
->key
;
2952 DBG("Sending metadata request to sessiond, session id %" PRIu64
2953 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
2954 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
2957 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
2959 health_code_update();
2961 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
2964 ERR("Asking metadata to sessiond");
2968 health_code_update();
2970 /* Receive the metadata from sessiond */
2971 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
2973 if (ret
!= sizeof(msg
)) {
2974 DBG("Consumer received unexpected message size %d (expects %zu)",
2976 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
2978 * The ret value might 0 meaning an orderly shutdown but this is ok
2979 * since the caller handles this.
2984 health_code_update();
2986 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
2987 /* No registry found */
2988 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
2992 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
2993 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
2998 len
= msg
.u
.push_metadata
.len
;
2999 key
= msg
.u
.push_metadata
.key
;
3000 offset
= msg
.u
.push_metadata
.target_offset
;
3001 version
= msg
.u
.push_metadata
.version
;
3003 assert(key
== channel
->key
);
3005 DBG("No new metadata to receive for key %" PRIu64
, key
);
3008 health_code_update();
3010 /* Tell session daemon we are ready to receive the metadata. */
3011 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3012 LTTCOMM_CONSUMERD_SUCCESS
);
3013 if (ret
< 0 || len
== 0) {
3015 * Somehow, the session daemon is not responding anymore or there is
3016 * nothing to receive.
3021 health_code_update();
3023 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3024 key
, offset
, len
, version
, channel
, timer
, wait
);
3027 * Only send the status msg if the sessiond is alive meaning a positive
3030 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3035 health_code_update();
3037 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3042 * Return the ustctl call for the get stream id.
3044 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3045 uint64_t *stream_id
)
3050 return ustctl_get_stream_id(stream
->ustream
, stream_id
);