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
;
545 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
547 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
548 ret
= consumer_send_relayd_channel_bulk(channel
);
551 * Flag that the relayd was the problem here probably due to a
552 * communicaton error on the socket.
557 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
561 /* Inform sessiond that we are about to send channel and streams. */
562 ret
= consumer_send_status_msg(sock
, ret_code
);
563 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
565 * Either the session daemon is not responding or the relayd died so we
571 /* Send channel to sessiond. */
572 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
577 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
582 /* The channel was sent successfully to the sessiond at this point. */
583 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
585 health_code_update();
587 /* Send stream to session daemon. */
588 ret
= send_sessiond_stream(sock
, stream
);
594 /* Tell sessiond there is no more stream. */
595 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
600 DBG("UST consumer NULL stream sent to sessiond");
605 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
612 * Creates a channel and streams and add the channel it to the channel internal
613 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
616 * Return 0 on success or else, a negative value is returned and the channel
617 * MUST be destroyed by consumer_del_channel().
619 static int ask_channel(struct lttng_consumer_local_data
*ctx
, int sock
,
620 struct lttng_consumer_channel
*channel
,
621 struct ustctl_consumer_channel_attr
*attr
)
630 * This value is still used by the kernel consumer since for the kernel,
631 * the stream ownership is not IN the consumer so we need to have the
632 * number of left stream that needs to be initialized so we can know when
633 * to delete the channel (see consumer.c).
635 * As for the user space tracer now, the consumer creates and sends the
636 * stream to the session daemon which only sends them to the application
637 * once every stream of a channel is received making this value useless
638 * because we they will be added to the poll thread before the application
639 * receives them. This ensures that a stream can not hang up during
640 * initilization of a channel.
642 channel
->nb_init_stream_left
= 0;
644 /* The reply msg status is handled in the following call. */
645 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
650 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
653 * For the snapshots (no monitor), we create the metadata streams
654 * on demand, not during the channel creation.
656 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
661 /* Open all streams for this channel. */
662 ret
= create_ust_streams(channel
, ctx
);
672 * Send all stream of a channel to the right thread handling it.
674 * On error, return a negative value else 0 on success.
676 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
677 struct lttng_consumer_local_data
*ctx
)
680 struct lttng_consumer_stream
*stream
, *stmp
;
685 /* Send streams to the corresponding thread. */
686 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
689 health_code_update();
691 /* Sending the stream to the thread. */
692 ret
= send_stream_to_thread(stream
, ctx
);
695 * If we are unable to send the stream to the thread, there is
696 * a big problem so just stop everything.
698 /* Remove node from the channel stream list. */
699 cds_list_del(&stream
->send_node
);
703 /* Remove node from the channel stream list. */
704 cds_list_del(&stream
->send_node
);
713 * Flush channel's streams using the given key to retrieve the channel.
715 * Return 0 on success else an LTTng error code.
717 static int flush_channel(uint64_t chan_key
)
720 struct lttng_consumer_channel
*channel
;
721 struct lttng_consumer_stream
*stream
;
723 struct lttng_ht_iter iter
;
725 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
728 channel
= consumer_find_channel(chan_key
);
730 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
731 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
735 ht
= consumer_data
.stream_per_chan_id_ht
;
737 /* For each stream of the channel id, flush it. */
738 cds_lfht_for_each_entry_duplicate(ht
->ht
,
739 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
740 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
742 health_code_update();
744 pthread_mutex_lock(&stream
->lock
);
747 * Protect against concurrent teardown of a stream.
749 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
753 if (!stream
->quiescent
) {
754 ustctl_flush_buffer(stream
->ustream
, 0);
755 stream
->quiescent
= true;
758 pthread_mutex_unlock(&stream
->lock
);
766 * Clear quiescent state from channel's streams using the given key to
767 * retrieve the channel.
769 * Return 0 on success else an LTTng error code.
771 static int clear_quiescent_channel(uint64_t chan_key
)
774 struct lttng_consumer_channel
*channel
;
775 struct lttng_consumer_stream
*stream
;
777 struct lttng_ht_iter iter
;
779 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
782 channel
= consumer_find_channel(chan_key
);
784 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
785 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
789 ht
= consumer_data
.stream_per_chan_id_ht
;
791 /* For each stream of the channel id, clear quiescent state. */
792 cds_lfht_for_each_entry_duplicate(ht
->ht
,
793 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
794 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
796 health_code_update();
798 pthread_mutex_lock(&stream
->lock
);
799 stream
->quiescent
= false;
800 pthread_mutex_unlock(&stream
->lock
);
808 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
809 * RCU read side lock MUST be acquired before calling this function.
811 * Return 0 on success else an LTTng error code.
813 static int close_metadata(uint64_t chan_key
)
816 struct lttng_consumer_channel
*channel
;
817 unsigned int channel_monitor
;
819 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
821 channel
= consumer_find_channel(chan_key
);
824 * This is possible if the metadata thread has issue a delete because
825 * the endpoint point of the stream hung up. There is no way the
826 * session daemon can know about it thus use a DBG instead of an actual
829 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
830 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
834 pthread_mutex_lock(&consumer_data
.lock
);
835 pthread_mutex_lock(&channel
->lock
);
836 channel_monitor
= channel
->monitor
;
837 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
841 lttng_ustconsumer_close_metadata(channel
);
842 pthread_mutex_unlock(&channel
->lock
);
843 pthread_mutex_unlock(&consumer_data
.lock
);
846 * The ownership of a metadata channel depends on the type of
847 * session to which it belongs. In effect, the monitor flag is checked
848 * to determine if this metadata channel is in "snapshot" mode or not.
850 * In the non-snapshot case, the metadata channel is created along with
851 * a single stream which will remain present until the metadata channel
852 * is destroyed (on the destruction of its session). In this case, the
853 * metadata stream in "monitored" by the metadata poll thread and holds
854 * the ownership of its channel.
856 * Closing the metadata will cause the metadata stream's "metadata poll
857 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
858 * thread which will teardown the metadata stream which, in return,
859 * deletes the metadata channel.
861 * In the snapshot case, the metadata stream is created and destroyed
862 * on every snapshot record. Since the channel doesn't have an owner
863 * other than the session daemon, it is safe to destroy it immediately
864 * on reception of the CLOSE_METADATA command.
866 if (!channel_monitor
) {
868 * The channel and consumer_data locks must be
869 * released before this call since consumer_del_channel
870 * re-acquires the channel and consumer_data locks to teardown
871 * the channel and queue its reclamation by the "call_rcu"
874 consumer_del_channel(channel
);
879 pthread_mutex_unlock(&channel
->lock
);
880 pthread_mutex_unlock(&consumer_data
.lock
);
886 * RCU read side lock MUST be acquired before calling this function.
888 * Return 0 on success else an LTTng error code.
890 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
893 struct lttng_consumer_channel
*metadata
;
895 DBG("UST consumer setup metadata key %" PRIu64
, key
);
897 metadata
= consumer_find_channel(key
);
899 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
900 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
905 * In no monitor mode, the metadata channel has no stream(s) so skip the
906 * ownership transfer to the metadata thread.
908 if (!metadata
->monitor
) {
909 DBG("Metadata channel in no monitor");
915 * Send metadata stream to relayd if one available. Availability is
916 * known if the stream is still in the list of the channel.
918 if (cds_list_empty(&metadata
->streams
.head
)) {
919 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
920 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
921 goto error_no_stream
;
924 /* Send metadata stream to relayd if needed. */
925 if (metadata
->metadata_stream
->relayd_id
!= (uint64_t) -1ULL) {
926 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
929 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
932 ret
= consumer_send_relayd_streams_sent(
933 metadata
->metadata_stream
->relayd_id
);
935 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
940 ret
= send_streams_to_thread(metadata
, ctx
);
943 * If we are unable to send the stream to the thread, there is
944 * a big problem so just stop everything.
946 ret
= LTTCOMM_CONSUMERD_FATAL
;
949 /* List MUST be empty after or else it could be reused. */
950 assert(cds_list_empty(&metadata
->streams
.head
));
957 * Delete metadata channel on error. At this point, the metadata stream can
958 * NOT be monitored by the metadata thread thus having the guarantee that
959 * the stream is still in the local stream list of the channel. This call
960 * will make sure to clean that list.
962 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
963 cds_list_del(&metadata
->metadata_stream
->send_node
);
964 metadata
->metadata_stream
= NULL
;
971 * Snapshot the whole metadata.
973 * Returns 0 on success, < 0 on error
975 static int snapshot_metadata(uint64_t key
, char *path
, uint64_t relayd_id
,
976 struct lttng_consumer_local_data
*ctx
)
979 struct lttng_consumer_channel
*metadata_channel
;
980 struct lttng_consumer_stream
*metadata_stream
;
985 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
990 metadata_channel
= consumer_find_channel(key
);
991 if (!metadata_channel
) {
992 ERR("UST snapshot metadata channel not found for key %" PRIu64
,
997 assert(!metadata_channel
->monitor
);
999 health_code_update();
1002 * Ask the sessiond if we have new metadata waiting and update the
1003 * consumer metadata cache.
1005 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1010 health_code_update();
1013 * The metadata stream is NOT created in no monitor mode when the channel
1014 * is created on a sessiond ask channel command.
1016 ret
= create_ust_streams(metadata_channel
, ctx
);
1021 metadata_stream
= metadata_channel
->metadata_stream
;
1022 assert(metadata_stream
);
1024 if (relayd_id
!= (uint64_t) -1ULL) {
1025 metadata_stream
->relayd_id
= relayd_id
;
1026 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1031 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
1032 metadata_stream
->chan
->tracefile_size
,
1033 metadata_stream
->tracefile_count_current
,
1034 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
1038 metadata_stream
->out_fd
= ret
;
1039 metadata_stream
->tracefile_size_current
= 0;
1043 health_code_update();
1045 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
1053 * Clean up the stream completly because the next snapshot will use a new
1056 consumer_stream_destroy(metadata_stream
, NULL
);
1057 cds_list_del(&metadata_stream
->send_node
);
1058 metadata_channel
->metadata_stream
= NULL
;
1066 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1070 unsigned long mmap_offset
;
1071 const char *mmap_base
;
1073 mmap_base
= ustctl_get_mmap_base(stream
->ustream
);
1075 ERR("Failed to get mmap base for stream `%s`",
1081 ret
= ustctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1083 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1088 *addr
= mmap_base
+ mmap_offset
;
1095 * Take a snapshot of all the stream of a channel.
1097 * Returns 0 on success, < 0 on error
1099 static int snapshot_channel(uint64_t key
, char *path
, uint64_t relayd_id
,
1100 uint64_t nb_packets_per_stream
, struct lttng_consumer_local_data
*ctx
)
1103 unsigned use_relayd
= 0;
1104 unsigned long consumed_pos
, produced_pos
;
1105 struct lttng_consumer_channel
*channel
;
1106 struct lttng_consumer_stream
*stream
;
1113 if (relayd_id
!= (uint64_t) -1ULL) {
1117 channel
= consumer_find_channel(key
);
1119 ERR("UST snapshot channel not found for key %" PRIu64
, key
);
1123 assert(!channel
->monitor
);
1124 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1126 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1127 health_code_update();
1129 /* Lock stream because we are about to change its state. */
1130 pthread_mutex_lock(&stream
->lock
);
1131 stream
->relayd_id
= relayd_id
;
1134 ret
= consumer_send_relayd_stream(stream
, path
);
1139 ret
= utils_create_stream_file(path
, stream
->name
,
1140 stream
->chan
->tracefile_size
,
1141 stream
->tracefile_count_current
,
1142 stream
->uid
, stream
->gid
, NULL
);
1146 stream
->out_fd
= ret
;
1147 stream
->tracefile_size_current
= 0;
1149 DBG("UST consumer snapshot stream %s/%s (%" PRIu64
")", path
,
1150 stream
->name
, stream
->key
);
1152 if (relayd_id
!= -1ULL) {
1153 ret
= consumer_send_relayd_streams_sent(relayd_id
);
1160 * If tracing is active, we want to perform a "full" buffer flush.
1161 * Else, if quiescent, it has already been done by the prior stop.
1163 if (!stream
->quiescent
) {
1164 ustctl_flush_buffer(stream
->ustream
, 0);
1167 ret
= lttng_ustconsumer_take_snapshot(stream
);
1169 ERR("Taking UST snapshot");
1173 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1175 ERR("Produced UST snapshot position");
1179 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1181 ERR("Consumerd UST snapshot position");
1186 * The original value is sent back if max stream size is larger than
1187 * the possible size of the snapshot. Also, we assume that the session
1188 * daemon should never send a maximum stream size that is lower than
1191 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1192 produced_pos
, nb_packets_per_stream
,
1193 stream
->max_sb_size
);
1195 while (consumed_pos
< produced_pos
) {
1197 unsigned long len
, padded_len
;
1198 const char *subbuf_addr
;
1199 struct lttng_buffer_view subbuf_view
;
1201 health_code_update();
1203 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1205 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1207 if (ret
!= -EAGAIN
) {
1208 PERROR("ustctl_get_subbuf snapshot");
1209 goto error_close_stream
;
1211 DBG("UST consumer get subbuf failed. Skipping it.");
1212 consumed_pos
+= stream
->max_sb_size
;
1213 stream
->chan
->lost_packets
++;
1217 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1219 ERR("Snapshot ustctl_get_subbuf_size");
1220 goto error_put_subbuf
;
1223 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1225 ERR("Snapshot ustctl_get_padded_subbuf_size");
1226 goto error_put_subbuf
;
1229 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1231 goto error_put_subbuf
;
1234 subbuf_view
= lttng_buffer_view_init(
1235 subbuf_addr
, 0, padded_len
);
1236 read_len
= lttng_consumer_on_read_subbuffer_mmap(
1237 stream
, &subbuf_view
, padded_len
- len
);
1239 if (read_len
!= len
) {
1241 goto error_put_subbuf
;
1244 if (read_len
!= padded_len
) {
1246 goto error_put_subbuf
;
1250 ret
= ustctl_put_subbuf(stream
->ustream
);
1252 ERR("Snapshot ustctl_put_subbuf");
1253 goto error_close_stream
;
1255 consumed_pos
+= stream
->max_sb_size
;
1258 /* Simply close the stream so we can use it on the next snapshot. */
1259 consumer_stream_close(stream
);
1260 pthread_mutex_unlock(&stream
->lock
);
1267 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1268 ERR("Snapshot ustctl_put_subbuf");
1271 consumer_stream_close(stream
);
1273 pthread_mutex_unlock(&stream
->lock
);
1280 * Receive the metadata updates from the sessiond. Supports receiving
1281 * overlapping metadata, but is needs to always belong to a contiguous
1282 * range starting from 0.
1283 * Be careful about the locks held when calling this function: it needs
1284 * the metadata cache flush to concurrently progress in order to
1287 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1288 uint64_t len
, uint64_t version
,
1289 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1291 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1294 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1296 metadata_str
= zmalloc(len
* sizeof(char));
1297 if (!metadata_str
) {
1298 PERROR("zmalloc metadata string");
1299 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1303 health_code_update();
1305 /* Receive metadata string. */
1306 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1308 /* Session daemon is dead so return gracefully. */
1313 health_code_update();
1315 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1316 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1319 /* Unable to handle metadata. Notify session daemon. */
1320 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1322 * Skip metadata flush on write error since the offset and len might
1323 * not have been updated which could create an infinite loop below when
1324 * waiting for the metadata cache to be flushed.
1326 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1329 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1334 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1335 DBG("Waiting for metadata to be flushed");
1337 health_code_update();
1339 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1349 * Receive command from session daemon and process it.
1351 * Return 1 on success else a negative value or 0.
1353 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1354 int sock
, struct pollfd
*consumer_sockpoll
)
1357 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1358 struct lttcomm_consumer_msg msg
;
1359 struct lttng_consumer_channel
*channel
= NULL
;
1361 health_code_update();
1363 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1364 if (ret
!= sizeof(msg
)) {
1365 DBG("Consumer received unexpected message size %zd (expects %zu)",
1368 * The ret value might 0 meaning an orderly shutdown but this is ok
1369 * since the caller handles this.
1372 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1378 health_code_update();
1381 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1383 health_code_update();
1385 /* relayd needs RCU read-side lock */
1388 switch (msg
.cmd_type
) {
1389 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1391 /* Session daemon status message are handled in the following call. */
1392 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1393 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1394 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1395 msg
.u
.relayd_sock
.relayd_session_id
);
1398 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1400 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1401 struct consumer_relayd_sock_pair
*relayd
;
1403 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1405 /* Get relayd reference if exists. */
1406 relayd
= consumer_find_relayd(index
);
1407 if (relayd
== NULL
) {
1408 DBG("Unable to find relayd %" PRIu64
, index
);
1409 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1413 * Each relayd socket pair has a refcount of stream attached to it
1414 * which tells if the relayd is still active or not depending on the
1417 * This will set the destroy flag of the relayd object and destroy it
1418 * if the refcount reaches zero when called.
1420 * The destroy can happen either here or when a stream fd hangs up.
1423 consumer_flag_relayd_for_destroy(relayd
);
1426 goto end_msg_sessiond
;
1428 case LTTNG_CONSUMER_UPDATE_STREAM
:
1433 case LTTNG_CONSUMER_DATA_PENDING
:
1435 int ret
, is_data_pending
;
1436 uint64_t id
= msg
.u
.data_pending
.session_id
;
1438 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1440 is_data_pending
= consumer_data_pending(id
);
1442 /* Send back returned value to session daemon */
1443 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1444 sizeof(is_data_pending
));
1446 DBG("Error when sending the data pending ret code: %d", ret
);
1451 * No need to send back a status message since the data pending
1452 * returned value is the response.
1456 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1459 struct ustctl_consumer_channel_attr attr
;
1461 /* Create a plain object and reserve a channel key. */
1462 channel
= consumer_allocate_channel(
1463 msg
.u
.ask_channel
.key
,
1464 msg
.u
.ask_channel
.session_id
,
1465 msg
.u
.ask_channel
.pathname
,
1466 msg
.u
.ask_channel
.name
,
1467 msg
.u
.ask_channel
.uid
,
1468 msg
.u
.ask_channel
.gid
,
1469 msg
.u
.ask_channel
.relayd_id
,
1470 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1471 msg
.u
.ask_channel
.tracefile_size
,
1472 msg
.u
.ask_channel
.tracefile_count
,
1473 msg
.u
.ask_channel
.session_id_per_pid
,
1474 msg
.u
.ask_channel
.monitor
,
1475 msg
.u
.ask_channel
.live_timer_interval
,
1476 msg
.u
.ask_channel
.is_live
,
1477 msg
.u
.ask_channel
.root_shm_path
,
1478 msg
.u
.ask_channel
.shm_path
);
1480 goto end_channel_error
;
1484 * Assign UST application UID to the channel. This value is ignored for
1485 * per PID buffers. This is specific to UST thus setting this after the
1488 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1490 /* Build channel attributes from received message. */
1491 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1492 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1493 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1494 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1495 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1496 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1497 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1499 /* Match channel buffer type to the UST abi. */
1500 switch (msg
.u
.ask_channel
.output
) {
1501 case LTTNG_EVENT_MMAP
:
1503 attr
.output
= LTTNG_UST_MMAP
;
1507 /* Translate and save channel type. */
1508 switch (msg
.u
.ask_channel
.type
) {
1509 case LTTNG_UST_CHAN_PER_CPU
:
1510 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1511 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1513 * Set refcount to 1 for owner. Below, we will
1514 * pass ownership to the
1515 * consumer_thread_channel_poll() thread.
1517 channel
->refcount
= 1;
1519 case LTTNG_UST_CHAN_METADATA
:
1520 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1521 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1528 health_code_update();
1530 ret
= ask_channel(ctx
, sock
, channel
, &attr
);
1532 goto end_channel_error
;
1535 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1536 ret
= consumer_metadata_cache_allocate(channel
);
1538 ERR("Allocating metadata cache");
1539 goto end_channel_error
;
1541 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1542 attr
.switch_timer_interval
= 0;
1544 consumer_timer_live_start(channel
,
1545 msg
.u
.ask_channel
.live_timer_interval
);
1548 health_code_update();
1551 * Add the channel to the internal state AFTER all streams were created
1552 * and successfully sent to session daemon. This way, all streams must
1553 * be ready before this channel is visible to the threads.
1554 * If add_channel succeeds, ownership of the channel is
1555 * passed to consumer_thread_channel_poll().
1557 ret
= add_channel(channel
, ctx
);
1559 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1560 if (channel
->switch_timer_enabled
== 1) {
1561 consumer_timer_switch_stop(channel
);
1563 consumer_metadata_cache_destroy(channel
);
1565 if (channel
->live_timer_enabled
== 1) {
1566 consumer_timer_live_stop(channel
);
1568 goto end_channel_error
;
1571 health_code_update();
1574 * Channel and streams are now created. Inform the session daemon that
1575 * everything went well and should wait to receive the channel and
1576 * streams with ustctl API.
1578 ret
= consumer_send_status_channel(sock
, channel
);
1581 * There is probably a problem on the socket.
1588 case LTTNG_CONSUMER_GET_CHANNEL
:
1590 int ret
, relayd_err
= 0;
1591 uint64_t key
= msg
.u
.get_channel
.key
;
1592 struct lttng_consumer_channel
*channel
;
1594 channel
= consumer_find_channel(key
);
1596 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1597 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1598 goto end_msg_sessiond
;
1601 health_code_update();
1603 /* Send everything to sessiond. */
1604 ret
= send_sessiond_channel(sock
, channel
, ctx
, &relayd_err
);
1608 * We were unable to send to the relayd the stream so avoid
1609 * sending back a fatal error to the thread since this is OK
1610 * and the consumer can continue its work. The above call
1611 * has sent the error status message to the sessiond.
1616 * The communicaton was broken hence there is a bad state between
1617 * the consumer and sessiond so stop everything.
1622 health_code_update();
1625 * In no monitor mode, the streams ownership is kept inside the channel
1626 * so don't send them to the data thread.
1628 if (!channel
->monitor
) {
1629 goto end_msg_sessiond
;
1632 ret
= send_streams_to_thread(channel
, ctx
);
1635 * If we are unable to send the stream to the thread, there is
1636 * a big problem so just stop everything.
1640 /* List MUST be empty after or else it could be reused. */
1641 assert(cds_list_empty(&channel
->streams
.head
));
1642 goto end_msg_sessiond
;
1644 case LTTNG_CONSUMER_CHANNEL_STOP_LIVE_TIMER
:
1646 uint64_t key
= msg
.u
.get_channel
.key
;
1647 struct lttng_consumer_channel
*channel
;
1649 channel
= consumer_find_channel(key
);
1651 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1652 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1653 goto end_msg_sessiond
;
1656 health_code_update();
1658 if (channel
->live_timer_enabled
== 1) {
1659 consumer_timer_live_stop(channel
);
1662 health_code_update();
1664 goto end_msg_sessiond
;
1666 case LTTNG_CONSUMER_CHANNEL_START_LIVE_TIMER
:
1668 uint64_t key
= msg
.u
.get_channel
.key
;
1669 struct lttng_consumer_channel
*channel
;
1671 channel
= consumer_find_channel(key
);
1673 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1674 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1675 goto end_msg_sessiond
;
1678 health_code_update();
1680 if (channel
->live_timer_enabled
== 0) {
1681 consumer_timer_live_start(channel
, channel
->live_timer_interval
);
1684 health_code_update();
1686 goto end_msg_sessiond
;
1689 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1691 uint64_t key
= msg
.u
.destroy_channel
.key
;
1694 * Only called if streams have not been sent to stream
1695 * manager thread. However, channel has been sent to
1696 * channel manager thread.
1698 notify_thread_del_channel(ctx
, key
);
1699 goto end_msg_sessiond
;
1701 case LTTNG_CONSUMER_CLOSE_METADATA
:
1705 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1710 goto end_msg_sessiond
;
1712 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1716 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1721 goto end_msg_sessiond
;
1723 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1727 ret
= clear_quiescent_channel(
1728 msg
.u
.clear_quiescent_channel
.key
);
1733 goto end_msg_sessiond
;
1735 case LTTNG_CONSUMER_PUSH_METADATA
:
1738 uint64_t len
= msg
.u
.push_metadata
.len
;
1739 uint64_t key
= msg
.u
.push_metadata
.key
;
1740 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1741 uint64_t version
= msg
.u
.push_metadata
.version
;
1742 struct lttng_consumer_channel
*channel
;
1744 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1747 channel
= consumer_find_channel(key
);
1750 * This is possible if the metadata creation on the consumer side
1751 * is in flight vis-a-vis a concurrent push metadata from the
1752 * session daemon. Simply return that the channel failed and the
1753 * session daemon will handle that message correctly considering
1754 * that this race is acceptable thus the DBG() statement here.
1756 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1757 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1758 goto end_msg_sessiond
;
1761 health_code_update();
1765 * There is nothing to receive. We have simply
1766 * checked whether the channel can be found.
1768 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1769 goto end_msg_sessiond
;
1772 /* Tell session daemon we are ready to receive the metadata. */
1773 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1775 /* Somehow, the session daemon is not responding anymore. */
1779 health_code_update();
1781 /* Wait for more data. */
1782 health_poll_entry();
1783 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1789 health_code_update();
1791 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1792 len
, version
, channel
, 0, 1);
1794 /* error receiving from sessiond */
1798 goto end_msg_sessiond
;
1801 case LTTNG_CONSUMER_SETUP_METADATA
:
1805 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1809 goto end_msg_sessiond
;
1811 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1813 if (msg
.u
.snapshot_channel
.metadata
) {
1814 ret
= snapshot_metadata(msg
.u
.snapshot_channel
.key
,
1815 msg
.u
.snapshot_channel
.pathname
,
1816 msg
.u
.snapshot_channel
.relayd_id
,
1819 ERR("Snapshot metadata failed");
1820 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1823 ret
= snapshot_channel(msg
.u
.snapshot_channel
.key
,
1824 msg
.u
.snapshot_channel
.pathname
,
1825 msg
.u
.snapshot_channel
.relayd_id
,
1826 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1829 ERR("Snapshot channel failed");
1830 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1834 health_code_update();
1835 ret
= consumer_send_status_msg(sock
, ret_code
);
1837 /* Somehow, the session daemon is not responding anymore. */
1840 health_code_update();
1843 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1846 uint64_t discarded_events
;
1847 struct lttng_ht_iter iter
;
1848 struct lttng_ht
*ht
;
1849 struct lttng_consumer_stream
*stream
;
1850 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1851 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1853 DBG("UST consumer discarded events command for session id %"
1856 pthread_mutex_lock(&consumer_data
.lock
);
1858 ht
= consumer_data
.stream_list_ht
;
1861 * We only need a reference to the channel, but they are not
1862 * directly indexed, so we just use the first matching stream
1863 * to extract the information we need, we default to 0 if not
1864 * found (no events are dropped if the channel is not yet in
1867 discarded_events
= 0;
1868 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1869 ht
->hash_fct(&id
, lttng_ht_seed
),
1871 &iter
.iter
, stream
, node_session_id
.node
) {
1872 if (stream
->chan
->key
== key
) {
1873 discarded_events
= stream
->chan
->discarded_events
;
1877 pthread_mutex_unlock(&consumer_data
.lock
);
1880 DBG("UST consumer discarded events command for session id %"
1881 PRIu64
", channel key %" PRIu64
, id
, key
);
1883 health_code_update();
1885 /* Send back returned value to session daemon */
1886 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1888 PERROR("send discarded events");
1894 case LTTNG_CONSUMER_LOST_PACKETS
:
1897 uint64_t lost_packets
;
1898 struct lttng_ht_iter iter
;
1899 struct lttng_ht
*ht
;
1900 struct lttng_consumer_stream
*stream
;
1901 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1902 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1904 DBG("UST consumer lost packets command for session id %"
1907 pthread_mutex_lock(&consumer_data
.lock
);
1909 ht
= consumer_data
.stream_list_ht
;
1912 * We only need a reference to the channel, but they are not
1913 * directly indexed, so we just use the first matching stream
1914 * to extract the information we need, we default to 0 if not
1915 * found (no packets lost if the channel is not yet in use).
1918 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1919 ht
->hash_fct(&id
, lttng_ht_seed
),
1921 &iter
.iter
, stream
, node_session_id
.node
) {
1922 if (stream
->chan
->key
== key
) {
1923 lost_packets
= stream
->chan
->lost_packets
;
1927 pthread_mutex_unlock(&consumer_data
.lock
);
1930 DBG("UST consumer lost packets command for session id %"
1931 PRIu64
", channel key %" PRIu64
, id
, key
);
1933 health_code_update();
1935 /* Send back returned value to session daemon */
1936 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1937 sizeof(lost_packets
));
1939 PERROR("send lost packets");
1952 health_code_update();
1955 * Return 1 to indicate success since the 0 value can be a socket
1956 * shutdown during the recv() or send() call.
1962 * The returned value here is not useful since either way we'll return 1 to
1963 * the caller because the session daemon socket management is done
1964 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1966 ret
= consumer_send_status_msg(sock
, ret_code
);
1972 health_code_update();
1978 * Free channel here since no one has a reference to it. We don't
1979 * free after that because a stream can store this pointer.
1981 destroy_channel(channel
);
1983 /* We have to send a status channel message indicating an error. */
1984 ret
= consumer_send_status_channel(sock
, NULL
);
1986 /* Stop everything if session daemon can not be notified. */
1991 health_code_update();
1996 /* This will issue a consumer stop. */
2000 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
2001 int producer_active
)
2004 assert(stream
->ustream
);
2006 ustctl_flush_buffer(stream
->ustream
, producer_active
);
2010 * Take a snapshot for a specific fd
2012 * Returns 0 on success, < 0 on error
2014 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2017 assert(stream
->ustream
);
2019 return ustctl_snapshot(stream
->ustream
);
2023 * Get the produced position
2025 * Returns 0 on success, < 0 on error
2027 int lttng_ustconsumer_get_produced_snapshot(
2028 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2031 assert(stream
->ustream
);
2034 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2038 * Get the consumed position
2040 * Returns 0 on success, < 0 on error
2042 int lttng_ustconsumer_get_consumed_snapshot(
2043 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2046 assert(stream
->ustream
);
2049 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2052 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2056 assert(stream
->ustream
);
2058 ustctl_flush_buffer(stream
->ustream
, producer
);
2061 int lttng_ustconsumer_get_current_timestamp(
2062 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2065 assert(stream
->ustream
);
2068 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2071 int lttng_ustconsumer_get_sequence_number(
2072 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2075 assert(stream
->ustream
);
2078 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2082 * Called when the stream signals the consumer that it has hung up.
2084 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2087 assert(stream
->ustream
);
2089 pthread_mutex_lock(&stream
->lock
);
2090 if (!stream
->quiescent
) {
2091 ustctl_flush_buffer(stream
->ustream
, 0);
2092 stream
->quiescent
= true;
2094 pthread_mutex_unlock(&stream
->lock
);
2095 stream
->hangup_flush_done
= 1;
2098 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2103 assert(chan
->uchan
);
2105 if (chan
->switch_timer_enabled
== 1) {
2106 consumer_timer_switch_stop(chan
);
2108 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2111 ret
= close(chan
->stream_fds
[i
]);
2115 if (chan
->shm_path
[0]) {
2116 char shm_path
[PATH_MAX
];
2118 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2120 ERR("Cannot get stream shm path");
2122 ret
= run_as_unlink(shm_path
, chan
->uid
, chan
->gid
);
2124 PERROR("unlink %s", shm_path
);
2130 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2133 assert(chan
->uchan
);
2135 consumer_metadata_cache_destroy(chan
);
2136 ustctl_destroy_channel(chan
->uchan
);
2137 /* Try to rmdir all directories under shm_path root. */
2138 if (chan
->root_shm_path
[0]) {
2139 (void) run_as_recursive_rmdir(chan
->root_shm_path
,
2140 chan
->uid
, chan
->gid
);
2142 free(chan
->stream_fds
);
2145 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2148 assert(stream
->ustream
);
2150 if (stream
->chan
->switch_timer_enabled
== 1) {
2151 consumer_timer_switch_stop(stream
->chan
);
2153 ustctl_destroy_stream(stream
->ustream
);
2156 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2159 assert(stream
->ustream
);
2161 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2164 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2167 assert(stream
->ustream
);
2169 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2173 void metadata_stream_reset_cache_consumed_position(
2174 struct lttng_consumer_stream
*stream
)
2176 DBG("Reset metadata cache of session %" PRIu64
,
2177 stream
->chan
->session_id
);
2178 stream
->ust_metadata_pushed
= 0;
2182 * Write up to one packet from the metadata cache to the channel.
2184 * Returns the number of bytes pushed in the cache, or a negative value
2188 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2193 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2194 if (stream
->chan
->metadata_cache
->max_offset
==
2195 stream
->ust_metadata_pushed
) {
2197 * In the context of a user space metadata channel, a
2198 * change in version can be detected in two ways:
2199 * 1) During the pre-consume of the `read_subbuffer` loop,
2200 * 2) When populating the metadata ring buffer (i.e. here).
2202 * This function is invoked when there is no metadata
2203 * available in the ring-buffer. If all data was consumed
2204 * up to the size of the metadata cache, there is no metadata
2205 * to insert in the ring-buffer.
2207 * However, the metadata version could still have changed (a
2208 * regeneration without any new data will yield the same cache
2211 * The cache's version is checked for a version change and the
2212 * consumed position is reset if one occurred.
2214 * This check is only necessary for the user space domain as
2215 * it has to manage the cache explicitly. If this reset was not
2216 * performed, no metadata would be consumed (and no reset would
2217 * occur as part of the pre-consume) until the metadata size
2218 * exceeded the cache size.
2220 if (stream
->metadata_version
!=
2221 stream
->chan
->metadata_cache
->version
) {
2222 metadata_stream_reset_cache_consumed_position(stream
);
2223 consumer_stream_metadata_set_version(stream
,
2224 stream
->chan
->metadata_cache
->version
);
2231 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2232 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2233 stream
->chan
->metadata_cache
->max_offset
2234 - stream
->ust_metadata_pushed
);
2235 assert(write_len
!= 0);
2236 if (write_len
< 0) {
2237 ERR("Writing one metadata packet");
2241 stream
->ust_metadata_pushed
+= write_len
;
2243 assert(stream
->chan
->metadata_cache
->max_offset
>=
2244 stream
->ust_metadata_pushed
);
2248 * Switch packet (but don't open the next one) on every commit of
2249 * a metadata packet. Since the subbuffer is fully filled (with padding,
2250 * if needed), the stream is "quiescent" after this commit.
2252 ustctl_flush_buffer(stream
->ustream
, 1);
2254 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2260 * Sync metadata meaning request them to the session daemon and snapshot to the
2261 * metadata thread can consumer them.
2263 * Metadata stream lock is held here, but we need to release it when
2264 * interacting with sessiond, else we cause a deadlock with live
2265 * awaiting on metadata to be pushed out.
2267 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
2268 * is empty or a negative value on error.
2270 int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2271 struct lttng_consumer_stream
*metadata
)
2279 pthread_mutex_unlock(&metadata
->lock
);
2281 * Request metadata from the sessiond, but don't wait for the flush
2282 * because we locked the metadata thread.
2284 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata
->chan
, 0, 0);
2285 pthread_mutex_lock(&metadata
->lock
);
2290 ret
= commit_one_metadata_packet(metadata
);
2293 } else if (ret
> 0) {
2297 ustctl_flush_buffer(metadata
->ustream
, 1);
2298 ret
= ustctl_snapshot(metadata
->ustream
);
2300 if (errno
!= EAGAIN
) {
2301 ERR("Sync metadata, taking UST snapshot");
2304 DBG("No new metadata when syncing them.");
2305 /* No new metadata, exit. */
2311 * After this flush, we still need to extract metadata.
2322 * Return 0 on success else a negative value.
2324 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2325 struct lttng_consumer_local_data
*ctx
)
2328 struct ustctl_consumer_stream
*ustream
;
2333 ustream
= stream
->ustream
;
2336 * First, we are going to check if there is a new subbuffer available
2337 * before reading the stream wait_fd.
2339 /* Get the next subbuffer */
2340 ret
= ustctl_get_next_subbuf(ustream
);
2342 /* No more data found, flag the stream. */
2343 stream
->has_data
= 0;
2348 ret
= ustctl_put_subbuf(ustream
);
2351 /* This stream still has data. Flag it and wake up the data thread. */
2352 stream
->has_data
= 1;
2354 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2357 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2358 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2363 /* The wake up pipe has been notified. */
2364 ctx
->has_wakeup
= 1;
2372 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream
*stream
)
2377 * We can consume the 1 byte written into the wait_fd by
2378 * UST. Don't trigger error if we cannot read this one byte
2379 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2381 * This is only done when the stream is monitored by a thread,
2382 * before the flush is done after a hangup and if the stream
2383 * is not flagged with data since there might be nothing to
2384 * consume in the wait fd but still have data available
2385 * flagged by the consumer wake up pipe.
2387 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2391 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2392 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2400 static int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
2401 struct stream_subbuffer
*subbuf
)
2405 ret
= ustctl_get_subbuf_size(
2406 stream
->ustream
, &subbuf
->info
.data
.subbuf_size
);
2411 ret
= ustctl_get_padded_subbuf_size(
2412 stream
->ustream
, &subbuf
->info
.data
.padded_subbuf_size
);
2421 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
2422 struct stream_subbuffer
*subbuf
)
2426 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2431 subbuf
->info
.metadata
.version
= stream
->metadata_version
;
2437 static int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
2438 struct stream_subbuffer
*subbuf
)
2442 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2447 ret
= ustctl_get_packet_size(
2448 stream
->ustream
, &subbuf
->info
.data
.packet_size
);
2450 PERROR("Failed to get sub-buffer packet size");
2454 ret
= ustctl_get_content_size(
2455 stream
->ustream
, &subbuf
->info
.data
.content_size
);
2457 PERROR("Failed to get sub-buffer content size");
2461 ret
= ustctl_get_timestamp_begin(
2462 stream
->ustream
, &subbuf
->info
.data
.timestamp_begin
);
2464 PERROR("Failed to get sub-buffer begin timestamp");
2468 ret
= ustctl_get_timestamp_end(
2469 stream
->ustream
, &subbuf
->info
.data
.timestamp_end
);
2471 PERROR("Failed to get sub-buffer end timestamp");
2475 ret
= ustctl_get_events_discarded(
2476 stream
->ustream
, &subbuf
->info
.data
.events_discarded
);
2478 PERROR("Failed to get sub-buffer events discarded count");
2482 ret
= ustctl_get_sequence_number(stream
->ustream
,
2483 &subbuf
->info
.data
.sequence_number
.value
);
2485 /* May not be supported by older LTTng-modules. */
2486 if (ret
!= -ENOTTY
) {
2487 PERROR("Failed to get sub-buffer sequence number");
2491 subbuf
->info
.data
.sequence_number
.is_set
= true;
2494 ret
= ustctl_get_stream_id(
2495 stream
->ustream
, &subbuf
->info
.data
.stream_id
);
2497 PERROR("Failed to get stream id");
2501 ret
= ustctl_get_instance_id(stream
->ustream
,
2502 &subbuf
->info
.data
.stream_instance_id
.value
);
2504 /* May not be supported by older LTTng-modules. */
2505 if (ret
!= -ENOTTY
) {
2506 PERROR("Failed to get stream instance id");
2510 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
2516 static int get_next_subbuffer_common(struct lttng_consumer_stream
*stream
,
2517 struct stream_subbuffer
*subbuffer
)
2522 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
2528 ret
= get_current_subbuf_addr(stream
, &addr
);
2533 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
2534 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
2535 assert(subbuffer
->buffer
.buffer
.data
!= NULL
);
2540 static int get_next_subbuffer(struct lttng_consumer_stream
*stream
,
2541 struct stream_subbuffer
*subbuffer
)
2545 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2550 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2558 static int get_next_subbuffer_metadata(struct lttng_consumer_stream
*stream
,
2559 struct stream_subbuffer
*subbuffer
)
2566 unsigned long consumed_pos
, produced_pos
;
2569 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2571 got_subbuffer
= true;
2573 got_subbuffer
= false;
2574 if (ret
!= -EAGAIN
) {
2581 * Determine if the cache is empty and ensure that a sub-buffer
2582 * is made available if the cache is not empty.
2584 if (!got_subbuffer
) {
2585 ret
= commit_one_metadata_packet(stream
);
2586 if (ret
< 0 && ret
!= -ENOBUFS
) {
2588 } else if (ret
== 0) {
2589 /* Not an error, the cache is empty. */
2594 cache_empty
= false;
2597 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2598 cache_empty
= stream
->chan
->metadata_cache
->max_offset
==
2599 stream
->ust_metadata_pushed
;
2600 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2602 } while (!got_subbuffer
);
2604 /* Populate sub-buffer infos and view. */
2605 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2610 ret
= lttng_ustconsumer_take_snapshot(stream
);
2613 * -EAGAIN is not expected since we got a sub-buffer and haven't
2614 * pushed the consumption position yet (on put_next).
2616 PERROR("Failed to take a snapshot of metadata buffer positions");
2620 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
2622 PERROR("Failed to get metadata consumed position");
2626 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
2628 PERROR("Failed to get metadata produced position");
2632 /* Last sub-buffer of the ring buffer ? */
2633 buffer_empty
= (consumed_pos
+ stream
->max_sb_size
) == produced_pos
;
2636 * The sessiond registry lock ensures that coherent units of metadata
2637 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
2638 * acquired, the cache is empty, and it is the only available sub-buffer
2639 * available, it is safe to assume that it is "coherent".
2641 coherent
= got_subbuffer
&& cache_empty
&& buffer_empty
;
2643 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
2648 static int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
2649 struct stream_subbuffer
*subbuffer
)
2651 const int ret
= ustctl_put_next_subbuf(stream
->ustream
);
2657 static int signal_metadata(struct lttng_consumer_stream
*stream
,
2658 struct lttng_consumer_local_data
*ctx
)
2660 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
2663 static int lttng_ustconsumer_set_stream_ops(
2664 struct lttng_consumer_stream
*stream
)
2668 stream
->read_subbuffer_ops
.on_wake_up
= consumer_stream_ust_on_wake_up
;
2669 if (stream
->metadata_flag
) {
2670 stream
->read_subbuffer_ops
.get_next_subbuffer
=
2671 get_next_subbuffer_metadata
;
2672 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
2673 extract_metadata_subbuffer_info
;
2674 stream
->read_subbuffer_ops
.reset_metadata
=
2675 metadata_stream_reset_cache_consumed_position
;
2676 if (stream
->chan
->is_live
) {
2677 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
2678 ret
= consumer_stream_enable_metadata_bucketization(
2685 stream
->read_subbuffer_ops
.get_next_subbuffer
=
2687 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
2688 extract_data_subbuffer_info
;
2689 stream
->read_subbuffer_ops
.on_sleep
= notify_if_more_data
;
2690 if (stream
->chan
->is_live
) {
2691 stream
->read_subbuffer_ops
.send_live_beacon
=
2692 consumer_flush_ust_index
;
2696 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
2702 * Called when a stream is created.
2704 * Return 0 on success or else a negative value.
2706 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2712 /* Don't create anything if this is set for streaming. */
2713 if (stream
->relayd_id
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
2714 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
2715 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
2716 stream
->uid
, stream
->gid
, NULL
);
2720 stream
->out_fd
= ret
;
2721 stream
->tracefile_size_current
= 0;
2723 if (!stream
->metadata_flag
) {
2724 struct lttng_index_file
*index_file
;
2726 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
2727 stream
->name
, stream
->uid
, stream
->gid
,
2728 stream
->chan
->tracefile_size
,
2729 stream
->tracefile_count_current
,
2730 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
2734 stream
->index_file
= index_file
;
2738 lttng_ustconsumer_set_stream_ops(stream
);
2746 * Check if data is still being extracted from the buffers for a specific
2747 * stream. Consumer data lock MUST be acquired before calling this function
2748 * and the stream lock.
2750 * Return 1 if the traced data are still getting read else 0 meaning that the
2751 * data is available for trace viewer reading.
2753 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
2758 assert(stream
->ustream
);
2760 DBG("UST consumer checking data pending");
2762 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
2767 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
2768 uint64_t contiguous
, pushed
;
2770 /* Ease our life a bit. */
2771 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
2772 pushed
= stream
->ust_metadata_pushed
;
2775 * We can simply check whether all contiguously available data
2776 * has been pushed to the ring buffer, since the push operation
2777 * is performed within get_next_subbuf(), and because both
2778 * get_next_subbuf() and put_next_subbuf() are issued atomically
2779 * thanks to the stream lock within
2780 * lttng_ustconsumer_read_subbuffer(). This basically means that
2781 * whetnever ust_metadata_pushed is incremented, the associated
2782 * metadata has been consumed from the metadata stream.
2784 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
2785 contiguous
, pushed
);
2786 assert(((int64_t) (contiguous
- pushed
)) >= 0);
2787 if ((contiguous
!= pushed
) ||
2788 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
2789 ret
= 1; /* Data is pending */
2793 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2796 * There is still data so let's put back this
2799 ret
= ustctl_put_subbuf(stream
->ustream
);
2801 ret
= 1; /* Data is pending */
2806 /* Data is NOT pending so ready to be read. */
2814 * Stop a given metadata channel timer if enabled and close the wait fd which
2815 * is the poll pipe of the metadata stream.
2817 * This MUST be called with the metadata channel acquired.
2819 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
2824 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
2826 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
2828 if (metadata
->switch_timer_enabled
== 1) {
2829 consumer_timer_switch_stop(metadata
);
2832 if (!metadata
->metadata_stream
) {
2837 * Closing write side so the thread monitoring the stream wakes up if any
2838 * and clean the metadata stream.
2840 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
2841 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
2843 PERROR("closing metadata pipe write side");
2845 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
2853 * Close every metadata stream wait fd of the metadata hash table. This
2854 * function MUST be used very carefully so not to run into a race between the
2855 * metadata thread handling streams and this function closing their wait fd.
2857 * For UST, this is used when the session daemon hangs up. Its the metadata
2858 * producer so calling this is safe because we are assured that no state change
2859 * can occur in the metadata thread for the streams in the hash table.
2861 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
2863 struct lttng_ht_iter iter
;
2864 struct lttng_consumer_stream
*stream
;
2866 assert(metadata_ht
);
2867 assert(metadata_ht
->ht
);
2869 DBG("UST consumer closing all metadata streams");
2872 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
2875 health_code_update();
2877 pthread_mutex_lock(&stream
->chan
->lock
);
2878 lttng_ustconsumer_close_metadata(stream
->chan
);
2879 pthread_mutex_unlock(&stream
->chan
->lock
);
2885 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
2889 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
2891 ERR("Unable to close wakeup fd");
2896 * Please refer to consumer-timer.c before adding any lock within this
2897 * function or any of its callees. Timers have a very strict locking
2898 * semantic with respect to teardown. Failure to respect this semantic
2899 * introduces deadlocks.
2901 * DON'T hold the metadata lock when calling this function, else this
2902 * can cause deadlock involving consumer awaiting for metadata to be
2903 * pushed out due to concurrent interaction with the session daemon.
2905 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
2906 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
2908 struct lttcomm_metadata_request_msg request
;
2909 struct lttcomm_consumer_msg msg
;
2910 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
2911 uint64_t len
, key
, offset
, version
;
2915 assert(channel
->metadata_cache
);
2917 memset(&request
, 0, sizeof(request
));
2919 /* send the metadata request to sessiond */
2920 switch (consumer_data
.type
) {
2921 case LTTNG_CONSUMER64_UST
:
2922 request
.bits_per_long
= 64;
2924 case LTTNG_CONSUMER32_UST
:
2925 request
.bits_per_long
= 32;
2928 request
.bits_per_long
= 0;
2932 request
.session_id
= channel
->session_id
;
2933 request
.session_id_per_pid
= channel
->session_id_per_pid
;
2935 * Request the application UID here so the metadata of that application can
2936 * be sent back. The channel UID corresponds to the user UID of the session
2937 * used for the rights on the stream file(s).
2939 request
.uid
= channel
->ust_app_uid
;
2940 request
.key
= channel
->key
;
2942 DBG("Sending metadata request to sessiond, session id %" PRIu64
2943 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
2944 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
2947 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
2949 health_code_update();
2951 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
2954 ERR("Asking metadata to sessiond");
2958 health_code_update();
2960 /* Receive the metadata from sessiond */
2961 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
2963 if (ret
!= sizeof(msg
)) {
2964 DBG("Consumer received unexpected message size %d (expects %zu)",
2966 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
2968 * The ret value might 0 meaning an orderly shutdown but this is ok
2969 * since the caller handles this.
2974 health_code_update();
2976 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
2977 /* No registry found */
2978 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
2982 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
2983 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
2988 len
= msg
.u
.push_metadata
.len
;
2989 key
= msg
.u
.push_metadata
.key
;
2990 offset
= msg
.u
.push_metadata
.target_offset
;
2991 version
= msg
.u
.push_metadata
.version
;
2993 assert(key
== channel
->key
);
2995 DBG("No new metadata to receive for key %" PRIu64
, key
);
2998 health_code_update();
3000 /* Tell session daemon we are ready to receive the metadata. */
3001 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3002 LTTCOMM_CONSUMERD_SUCCESS
);
3003 if (ret
< 0 || len
== 0) {
3005 * Somehow, the session daemon is not responding anymore or there is
3006 * nothing to receive.
3011 health_code_update();
3013 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3014 key
, offset
, len
, version
, channel
, timer
, wait
);
3017 * Only send the status msg if the sessiond is alive meaning a positive
3020 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3025 health_code_update();
3027 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3032 * Return the ustctl call for the get stream id.
3034 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3035 uint64_t *stream_id
)
3040 return ustctl_get_stream_id(stream
->ustream
, stream_id
);