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.
22 #include <lttng/ust-ctl.h>
28 #include <sys/socket.h>
30 #include <sys/types.h>
33 #include <urcu/list.h>
36 #include <bin/lttng-consumerd/health-consumerd.h>
37 #include <common/common.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/relayd/relayd.h>
40 #include <common/compat/fcntl.h>
41 #include <common/compat/endian.h>
42 #include <common/consumer/consumer-metadata-cache.h>
43 #include <common/consumer/consumer-stream.h>
44 #include <common/consumer/consumer-timer.h>
45 #include <common/utils.h>
46 #include <common/index/index.h>
48 #include "ust-consumer.h"
50 #define INT_MAX_STR_LEN 12 /* includes \0 */
52 extern struct lttng_consumer_global_data consumer_data
;
53 extern int consumer_poll_timeout
;
54 extern volatile int consumer_quit
;
57 * Free channel object and all streams associated with it. This MUST be used
58 * only and only if the channel has _NEVER_ been added to the global channel
61 static void destroy_channel(struct lttng_consumer_channel
*channel
)
63 struct lttng_consumer_stream
*stream
, *stmp
;
67 DBG("UST consumer cleaning stream list");
69 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
74 cds_list_del(&stream
->send_node
);
75 ustctl_destroy_stream(stream
->ustream
);
80 * If a channel is available meaning that was created before the streams
84 lttng_ustconsumer_del_channel(channel
);
85 lttng_ustconsumer_free_channel(channel
);
91 * Add channel to internal consumer state.
93 * Returns 0 on success or else a negative value.
95 static int add_channel(struct lttng_consumer_channel
*channel
,
96 struct lttng_consumer_local_data
*ctx
)
103 if (ctx
->on_recv_channel
!= NULL
) {
104 ret
= ctx
->on_recv_channel(channel
);
106 ret
= consumer_add_channel(channel
, ctx
);
107 } else if (ret
< 0) {
108 /* Most likely an ENOMEM. */
109 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
113 ret
= consumer_add_channel(channel
, ctx
);
116 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
123 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
124 * error value if applicable is set in it else it is kept untouched.
126 * Return NULL on error else the newly allocated stream object.
128 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
129 struct lttng_consumer_channel
*channel
,
130 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
133 struct lttng_consumer_stream
*stream
= NULL
;
138 stream
= consumer_allocate_stream(
142 LTTNG_CONSUMER_ACTIVE_STREAM
,
152 if (stream
== NULL
) {
156 * We could not find the channel. Can happen if cpu hotplug
157 * happens while tearing down.
159 DBG3("Could not find channel");
164 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
172 *_alloc_ret
= alloc_ret
;
178 * Send the given stream pointer to the corresponding thread.
180 * Returns 0 on success else a negative value.
182 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
183 struct lttng_consumer_local_data
*ctx
)
186 struct lttng_pipe
*stream_pipe
;
188 /* Get the right pipe where the stream will be sent. */
189 if (stream
->metadata_flag
) {
190 ret
= consumer_add_metadata_stream(stream
);
192 ERR("Consumer add metadata stream %" PRIu64
" failed.",
196 stream_pipe
= ctx
->consumer_metadata_pipe
;
198 ret
= consumer_add_data_stream(stream
);
200 ERR("Consumer add stream %" PRIu64
" failed.",
204 stream_pipe
= ctx
->consumer_data_pipe
;
208 * From this point on, the stream's ownership has been moved away from
209 * the channel and becomes globally visible.
211 stream
->globally_visible
= 1;
213 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
215 ERR("Consumer write %s stream to pipe %d",
216 stream
->metadata_flag
? "metadata" : "data",
217 lttng_pipe_get_writefd(stream_pipe
));
218 if (stream
->metadata_flag
) {
219 consumer_del_stream_for_metadata(stream
);
221 consumer_del_stream_for_data(stream
);
229 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
231 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
234 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
235 stream_shm_path
[PATH_MAX
- 1] = '\0';
236 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
241 strncat(stream_shm_path
, cpu_nr
,
242 PATH_MAX
- strlen(stream_shm_path
) - 1);
249 * Create streams for the given channel using liblttng-ust-ctl.
251 * Return 0 on success else a negative value.
253 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
254 struct lttng_consumer_local_data
*ctx
)
257 struct ustctl_consumer_stream
*ustream
;
258 struct lttng_consumer_stream
*stream
;
264 * While a stream is available from ustctl. When NULL is returned, we've
265 * reached the end of the possible stream for the channel.
267 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
269 int ust_metadata_pipe
[2];
271 health_code_update();
273 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
274 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
276 ERR("Create ust metadata poll pipe");
279 wait_fd
= ust_metadata_pipe
[0];
281 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
284 /* Allocate consumer stream object. */
285 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
289 stream
->ustream
= ustream
;
291 * Store it so we can save multiple function calls afterwards since
292 * this value is used heavily in the stream threads. This is UST
293 * specific so this is why it's done after allocation.
295 stream
->wait_fd
= wait_fd
;
298 * Increment channel refcount since the channel reference has now been
299 * assigned in the allocation process above.
301 if (stream
->chan
->monitor
) {
302 uatomic_inc(&stream
->chan
->refcount
);
306 * Order is important this is why a list is used. On error, the caller
307 * should clean this list.
309 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
311 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
312 &stream
->max_sb_size
);
314 ERR("ustctl_get_max_subbuf_size failed for stream %s",
319 /* Do actions once stream has been received. */
320 if (ctx
->on_recv_stream
) {
321 ret
= ctx
->on_recv_stream(stream
);
327 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
328 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
330 /* Set next CPU stream. */
331 channel
->streams
.count
= ++cpu
;
333 /* Keep stream reference when creating metadata. */
334 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
335 channel
->metadata_stream
= stream
;
336 if (channel
->monitor
) {
337 /* Set metadata poll pipe if we created one */
338 memcpy(stream
->ust_metadata_poll_pipe
,
340 sizeof(ust_metadata_pipe
));
353 * create_posix_shm is never called concurrently within a process.
356 int create_posix_shm(void)
358 char tmp_name
[NAME_MAX
];
361 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
367 * Allocate shm, and immediately unlink its shm oject, keeping
368 * only the file descriptor as a reference to the object.
369 * We specifically do _not_ use the / at the beginning of the
370 * pathname so that some OS implementations can keep it local to
371 * the process (POSIX leaves this implementation-defined).
373 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
378 ret
= shm_unlink(tmp_name
);
379 if (ret
< 0 && errno
!= ENOENT
) {
380 PERROR("shm_unlink");
381 goto error_shm_release
;
394 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
,
395 struct ustctl_consumer_channel_attr
*attr
,
398 char shm_path
[PATH_MAX
];
401 if (!channel
->shm_path
[0]) {
402 return create_posix_shm();
404 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
408 return run_as_open(shm_path
,
409 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
410 channel
->uid
, channel
->gid
);
417 * Create an UST channel with the given attributes and send it to the session
418 * daemon using the ust ctl API.
420 * Return 0 on success or else a negative value.
422 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
423 struct ustctl_consumer_channel_attr
*attr
,
424 struct ustctl_consumer_channel
**ust_chanp
)
426 int ret
, nr_stream_fds
, i
, j
;
428 struct ustctl_consumer_channel
*ust_channel
;
434 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
435 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
436 "switch_timer_interval: %u, read_timer_interval: %u, "
437 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
438 attr
->num_subbuf
, attr
->switch_timer_interval
,
439 attr
->read_timer_interval
, attr
->output
, attr
->type
);
441 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
444 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
445 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
450 for (i
= 0; i
< nr_stream_fds
; i
++) {
451 stream_fds
[i
] = open_ust_stream_fd(channel
, attr
, i
);
452 if (stream_fds
[i
] < 0) {
457 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
462 channel
->nr_stream_fds
= nr_stream_fds
;
463 channel
->stream_fds
= stream_fds
;
464 *ust_chanp
= ust_channel
;
470 for (j
= i
- 1; j
>= 0; j
--) {
473 closeret
= close(stream_fds
[j
]);
477 if (channel
->shm_path
[0]) {
478 char shm_path
[PATH_MAX
];
480 closeret
= get_stream_shm_path(shm_path
,
481 channel
->shm_path
, j
);
483 ERR("Cannot get stream shm path");
485 closeret
= run_as_unlink(shm_path
,
486 channel
->uid
, channel
->gid
);
488 PERROR("unlink %s", shm_path
);
492 /* Try to rmdir all directories under shm_path root. */
493 if (channel
->root_shm_path
[0]) {
494 (void) run_as_recursive_rmdir(channel
->root_shm_path
,
495 channel
->uid
, channel
->gid
);
503 * Send a single given stream to the session daemon using the sock.
505 * Return 0 on success else a negative value.
507 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
514 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
516 /* Send stream to session daemon. */
517 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
527 * Send channel to sessiond.
529 * Return 0 on success or else a negative value.
531 static int send_sessiond_channel(int sock
,
532 struct lttng_consumer_channel
*channel
,
533 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
535 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
536 struct lttng_consumer_stream
*stream
;
537 uint64_t relayd_id
= -1ULL;
543 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
545 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
546 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
548 health_code_update();
550 /* Try to send the stream to the relayd if one is available. */
551 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
554 * Flag that the relayd was the problem here probably due to a
555 * communicaton error on the socket.
560 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
562 if (relayd_id
== -1ULL) {
563 relayd_id
= stream
->relayd_id
;
568 /* Inform sessiond that we are about to send channel and streams. */
569 ret
= consumer_send_status_msg(sock
, ret_code
);
570 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
572 * Either the session daemon is not responding or the relayd died so we
578 /* Send channel to sessiond. */
579 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
584 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
589 /* The channel was sent successfully to the sessiond at this point. */
590 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
592 health_code_update();
594 /* Send stream to session daemon. */
595 ret
= send_sessiond_stream(sock
, stream
);
601 /* Tell sessiond there is no more stream. */
602 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
607 DBG("UST consumer NULL stream sent to sessiond");
612 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
619 * Creates a channel and streams and add the channel it to the channel internal
620 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
623 * Return 0 on success or else, a negative value is returned and the channel
624 * MUST be destroyed by consumer_del_channel().
626 static int ask_channel(struct lttng_consumer_local_data
*ctx
, int sock
,
627 struct lttng_consumer_channel
*channel
,
628 struct ustctl_consumer_channel_attr
*attr
)
637 * This value is still used by the kernel consumer since for the kernel,
638 * the stream ownership is not IN the consumer so we need to have the
639 * number of left stream that needs to be initialized so we can know when
640 * to delete the channel (see consumer.c).
642 * As for the user space tracer now, the consumer creates and sends the
643 * stream to the session daemon which only sends them to the application
644 * once every stream of a channel is received making this value useless
645 * because we they will be added to the poll thread before the application
646 * receives them. This ensures that a stream can not hang up during
647 * initilization of a channel.
649 channel
->nb_init_stream_left
= 0;
651 /* The reply msg status is handled in the following call. */
652 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
657 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
660 * For the snapshots (no monitor), we create the metadata streams
661 * on demand, not during the channel creation.
663 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
668 /* Open all streams for this channel. */
669 ret
= create_ust_streams(channel
, ctx
);
679 * Send all stream of a channel to the right thread handling it.
681 * On error, return a negative value else 0 on success.
683 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
684 struct lttng_consumer_local_data
*ctx
)
687 struct lttng_consumer_stream
*stream
, *stmp
;
692 /* Send streams to the corresponding thread. */
693 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
696 health_code_update();
698 /* Sending the stream to the thread. */
699 ret
= send_stream_to_thread(stream
, ctx
);
702 * If we are unable to send the stream to the thread, there is
703 * a big problem so just stop everything.
705 /* Remove node from the channel stream list. */
706 cds_list_del(&stream
->send_node
);
710 /* Remove node from the channel stream list. */
711 cds_list_del(&stream
->send_node
);
720 * Flush channel's streams using the given key to retrieve the channel.
722 * Return 0 on success else an LTTng error code.
724 static int flush_channel(uint64_t chan_key
)
727 struct lttng_consumer_channel
*channel
;
728 struct lttng_consumer_stream
*stream
;
730 struct lttng_ht_iter iter
;
732 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
735 channel
= consumer_find_channel(chan_key
);
737 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
738 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
742 ht
= consumer_data
.stream_per_chan_id_ht
;
744 /* For each stream of the channel id, flush it. */
745 cds_lfht_for_each_entry_duplicate(ht
->ht
,
746 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
747 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
749 health_code_update();
751 pthread_mutex_lock(&stream
->lock
);
754 * Protect against concurrent teardown of a stream.
756 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
760 if (!stream
->quiescent
) {
761 ustctl_flush_buffer(stream
->ustream
, 0);
762 stream
->quiescent
= true;
765 pthread_mutex_unlock(&stream
->lock
);
773 * Clear quiescent state from channel's streams using the given key to
774 * retrieve the channel.
776 * Return 0 on success else an LTTng error code.
778 static int clear_quiescent_channel(uint64_t chan_key
)
781 struct lttng_consumer_channel
*channel
;
782 struct lttng_consumer_stream
*stream
;
784 struct lttng_ht_iter iter
;
786 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
789 channel
= consumer_find_channel(chan_key
);
791 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
792 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
796 ht
= consumer_data
.stream_per_chan_id_ht
;
798 /* For each stream of the channel id, clear quiescent state. */
799 cds_lfht_for_each_entry_duplicate(ht
->ht
,
800 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
801 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
803 health_code_update();
805 pthread_mutex_lock(&stream
->lock
);
806 stream
->quiescent
= false;
807 pthread_mutex_unlock(&stream
->lock
);
815 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
816 * RCU read side lock MUST be acquired before calling this function.
818 * Return 0 on success else an LTTng error code.
820 static int close_metadata(uint64_t chan_key
)
823 struct lttng_consumer_channel
*channel
;
824 unsigned int channel_monitor
;
826 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
828 channel
= consumer_find_channel(chan_key
);
831 * This is possible if the metadata thread has issue a delete because
832 * the endpoint point of the stream hung up. There is no way the
833 * session daemon can know about it thus use a DBG instead of an actual
836 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
837 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
841 pthread_mutex_lock(&consumer_data
.lock
);
842 pthread_mutex_lock(&channel
->lock
);
843 channel_monitor
= channel
->monitor
;
844 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
848 lttng_ustconsumer_close_metadata(channel
);
849 pthread_mutex_unlock(&channel
->lock
);
850 pthread_mutex_unlock(&consumer_data
.lock
);
853 * The ownership of a metadata channel depends on the type of
854 * session to which it belongs. In effect, the monitor flag is checked
855 * to determine if this metadata channel is in "snapshot" mode or not.
857 * In the non-snapshot case, the metadata channel is created along with
858 * a single stream which will remain present until the metadata channel
859 * is destroyed (on the destruction of its session). In this case, the
860 * metadata stream in "monitored" by the metadata poll thread and holds
861 * the ownership of its channel.
863 * Closing the metadata will cause the metadata stream's "metadata poll
864 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
865 * thread which will teardown the metadata stream which, in return,
866 * deletes the metadata channel.
868 * In the snapshot case, the metadata stream is created and destroyed
869 * on every snapshot record. Since the channel doesn't have an owner
870 * other than the session daemon, it is safe to destroy it immediately
871 * on reception of the CLOSE_METADATA command.
873 if (!channel_monitor
) {
875 * The channel and consumer_data locks must be
876 * released before this call since consumer_del_channel
877 * re-acquires the channel and consumer_data locks to teardown
878 * the channel and queue its reclamation by the "call_rcu"
881 consumer_del_channel(channel
);
886 pthread_mutex_unlock(&channel
->lock
);
887 pthread_mutex_unlock(&consumer_data
.lock
);
893 * RCU read side lock MUST be acquired before calling this function.
895 * Return 0 on success else an LTTng error code.
897 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
900 struct lttng_consumer_channel
*metadata
;
902 DBG("UST consumer setup metadata key %" PRIu64
, key
);
904 metadata
= consumer_find_channel(key
);
906 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
907 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
912 * In no monitor mode, the metadata channel has no stream(s) so skip the
913 * ownership transfer to the metadata thread.
915 if (!metadata
->monitor
) {
916 DBG("Metadata channel in no monitor");
922 * Send metadata stream to relayd if one available. Availability is
923 * known if the stream is still in the list of the channel.
925 if (cds_list_empty(&metadata
->streams
.head
)) {
926 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
927 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
928 goto error_no_stream
;
931 /* Send metadata stream to relayd if needed. */
932 if (metadata
->metadata_stream
->relayd_id
!= (uint64_t) -1ULL) {
933 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
936 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
939 ret
= consumer_send_relayd_streams_sent(
940 metadata
->metadata_stream
->relayd_id
);
942 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
947 ret
= send_streams_to_thread(metadata
, ctx
);
950 * If we are unable to send the stream to the thread, there is
951 * a big problem so just stop everything.
953 ret
= LTTCOMM_CONSUMERD_FATAL
;
956 /* List MUST be empty after or else it could be reused. */
957 assert(cds_list_empty(&metadata
->streams
.head
));
964 * Delete metadata channel on error. At this point, the metadata stream can
965 * NOT be monitored by the metadata thread thus having the guarantee that
966 * the stream is still in the local stream list of the channel. This call
967 * will make sure to clean that list.
969 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
970 cds_list_del(&metadata
->metadata_stream
->send_node
);
971 metadata
->metadata_stream
= NULL
;
978 * Snapshot the whole metadata.
980 * Returns 0 on success, < 0 on error
982 static int snapshot_metadata(uint64_t key
, char *path
, uint64_t relayd_id
,
983 struct lttng_consumer_local_data
*ctx
)
986 struct lttng_consumer_channel
*metadata_channel
;
987 struct lttng_consumer_stream
*metadata_stream
;
992 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
997 metadata_channel
= consumer_find_channel(key
);
998 if (!metadata_channel
) {
999 ERR("UST snapshot metadata channel not found for key %" PRIu64
,
1004 assert(!metadata_channel
->monitor
);
1006 health_code_update();
1009 * Ask the sessiond if we have new metadata waiting and update the
1010 * consumer metadata cache.
1012 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1017 health_code_update();
1020 * The metadata stream is NOT created in no monitor mode when the channel
1021 * is created on a sessiond ask channel command.
1023 ret
= create_ust_streams(metadata_channel
, ctx
);
1028 metadata_stream
= metadata_channel
->metadata_stream
;
1029 assert(metadata_stream
);
1031 if (relayd_id
!= (uint64_t) -1ULL) {
1032 metadata_stream
->relayd_id
= relayd_id
;
1033 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1038 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
1039 metadata_stream
->chan
->tracefile_size
,
1040 metadata_stream
->tracefile_count_current
,
1041 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
1045 metadata_stream
->out_fd
= ret
;
1046 metadata_stream
->tracefile_size_current
= 0;
1050 health_code_update();
1052 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
);
1060 * Clean up the stream completly because the next snapshot will use a new
1063 consumer_stream_destroy(metadata_stream
, NULL
);
1064 cds_list_del(&metadata_stream
->send_node
);
1065 metadata_channel
->metadata_stream
= NULL
;
1073 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1077 unsigned long mmap_offset
;
1078 const char *mmap_base
;
1080 mmap_base
= ustctl_get_mmap_base(stream
->ustream
);
1082 ERR("Failed to get mmap base for stream `%s`",
1088 ret
= ustctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1090 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1095 *addr
= mmap_base
+ mmap_offset
;
1102 * Take a snapshot of all the stream of a channel.
1104 * Returns 0 on success, < 0 on error
1106 static int snapshot_channel(uint64_t key
, char *path
, uint64_t relayd_id
,
1107 uint64_t nb_packets_per_stream
, struct lttng_consumer_local_data
*ctx
)
1110 unsigned use_relayd
= 0;
1111 unsigned long consumed_pos
, produced_pos
;
1112 struct lttng_consumer_channel
*channel
;
1113 struct lttng_consumer_stream
*stream
;
1120 if (relayd_id
!= (uint64_t) -1ULL) {
1124 channel
= consumer_find_channel(key
);
1126 ERR("UST snapshot channel not found for key %" PRIu64
, key
);
1130 assert(!channel
->monitor
);
1131 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1133 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1134 health_code_update();
1136 /* Lock stream because we are about to change its state. */
1137 pthread_mutex_lock(&stream
->lock
);
1138 stream
->relayd_id
= relayd_id
;
1141 ret
= consumer_send_relayd_stream(stream
, path
);
1146 ret
= utils_create_stream_file(path
, stream
->name
,
1147 stream
->chan
->tracefile_size
,
1148 stream
->tracefile_count_current
,
1149 stream
->uid
, stream
->gid
, NULL
);
1153 stream
->out_fd
= ret
;
1154 stream
->tracefile_size_current
= 0;
1156 DBG("UST consumer snapshot stream %s/%s (%" PRIu64
")", path
,
1157 stream
->name
, stream
->key
);
1159 if (relayd_id
!= -1ULL) {
1160 ret
= consumer_send_relayd_streams_sent(relayd_id
);
1167 * If tracing is active, we want to perform a "full" buffer flush.
1168 * Else, if quiescent, it has already been done by the prior stop.
1170 if (!stream
->quiescent
) {
1171 ustctl_flush_buffer(stream
->ustream
, 0);
1174 ret
= lttng_ustconsumer_take_snapshot(stream
);
1176 ERR("Taking UST snapshot");
1180 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1182 ERR("Produced UST snapshot position");
1186 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1188 ERR("Consumerd UST snapshot position");
1193 * The original value is sent back if max stream size is larger than
1194 * the possible size of the snapshot. Also, we assume that the session
1195 * daemon should never send a maximum stream size that is lower than
1198 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1199 produced_pos
, nb_packets_per_stream
,
1200 stream
->max_sb_size
);
1202 while (consumed_pos
< produced_pos
) {
1204 unsigned long len
, padded_len
;
1205 const char *subbuf_addr
;
1206 struct lttng_buffer_view subbuf_view
;
1208 health_code_update();
1210 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1212 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1214 if (ret
!= -EAGAIN
) {
1215 PERROR("ustctl_get_subbuf snapshot");
1216 goto error_close_stream
;
1218 DBG("UST consumer get subbuf failed. Skipping it.");
1219 consumed_pos
+= stream
->max_sb_size
;
1220 stream
->chan
->lost_packets
++;
1224 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1226 ERR("Snapshot ustctl_get_subbuf_size");
1227 goto error_put_subbuf
;
1230 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1232 ERR("Snapshot ustctl_get_padded_subbuf_size");
1233 goto error_put_subbuf
;
1236 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1238 goto error_put_subbuf
;
1241 subbuf_view
= lttng_buffer_view_init(
1242 subbuf_addr
, 0, padded_len
);
1243 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
,
1244 stream
, &subbuf_view
, padded_len
- len
,
1247 if (read_len
!= len
) {
1249 goto error_put_subbuf
;
1252 if (read_len
!= padded_len
) {
1254 goto error_put_subbuf
;
1258 ret
= ustctl_put_subbuf(stream
->ustream
);
1260 ERR("Snapshot ustctl_put_subbuf");
1261 goto error_close_stream
;
1263 consumed_pos
+= stream
->max_sb_size
;
1266 /* Simply close the stream so we can use it on the next snapshot. */
1267 consumer_stream_close(stream
);
1268 pthread_mutex_unlock(&stream
->lock
);
1275 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1276 ERR("Snapshot ustctl_put_subbuf");
1279 consumer_stream_close(stream
);
1281 pthread_mutex_unlock(&stream
->lock
);
1288 * Receive the metadata updates from the sessiond. Supports receiving
1289 * overlapping metadata, but is needs to always belong to a contiguous
1290 * range starting from 0.
1291 * Be careful about the locks held when calling this function: it needs
1292 * the metadata cache flush to concurrently progress in order to
1295 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1296 uint64_t len
, uint64_t version
,
1297 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1299 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1302 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1304 metadata_str
= zmalloc(len
* sizeof(char));
1305 if (!metadata_str
) {
1306 PERROR("zmalloc metadata string");
1307 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1311 health_code_update();
1313 /* Receive metadata string. */
1314 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1316 /* Session daemon is dead so return gracefully. */
1321 health_code_update();
1323 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1324 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1327 /* Unable to handle metadata. Notify session daemon. */
1328 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1330 * Skip metadata flush on write error since the offset and len might
1331 * not have been updated which could create an infinite loop below when
1332 * waiting for the metadata cache to be flushed.
1334 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1337 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1342 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1343 DBG("Waiting for metadata to be flushed");
1345 health_code_update();
1347 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1357 * Receive command from session daemon and process it.
1359 * Return 1 on success else a negative value or 0.
1361 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1362 int sock
, struct pollfd
*consumer_sockpoll
)
1365 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1366 struct lttcomm_consumer_msg msg
;
1367 struct lttng_consumer_channel
*channel
= NULL
;
1369 health_code_update();
1371 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1372 if (ret
!= sizeof(msg
)) {
1373 DBG("Consumer received unexpected message size %zd (expects %zu)",
1376 * The ret value might 0 meaning an orderly shutdown but this is ok
1377 * since the caller handles this.
1380 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1386 health_code_update();
1389 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1391 health_code_update();
1393 /* relayd needs RCU read-side lock */
1396 switch (msg
.cmd_type
) {
1397 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1399 /* Session daemon status message are handled in the following call. */
1400 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1401 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1402 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1403 msg
.u
.relayd_sock
.relayd_session_id
);
1406 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1408 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1409 struct consumer_relayd_sock_pair
*relayd
;
1411 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1413 /* Get relayd reference if exists. */
1414 relayd
= consumer_find_relayd(index
);
1415 if (relayd
== NULL
) {
1416 DBG("Unable to find relayd %" PRIu64
, index
);
1417 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1421 * Each relayd socket pair has a refcount of stream attached to it
1422 * which tells if the relayd is still active or not depending on the
1425 * This will set the destroy flag of the relayd object and destroy it
1426 * if the refcount reaches zero when called.
1428 * The destroy can happen either here or when a stream fd hangs up.
1431 consumer_flag_relayd_for_destroy(relayd
);
1434 goto end_msg_sessiond
;
1436 case LTTNG_CONSUMER_UPDATE_STREAM
:
1441 case LTTNG_CONSUMER_DATA_PENDING
:
1443 int ret
, is_data_pending
;
1444 uint64_t id
= msg
.u
.data_pending
.session_id
;
1446 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1448 is_data_pending
= consumer_data_pending(id
);
1450 /* Send back returned value to session daemon */
1451 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1452 sizeof(is_data_pending
));
1454 DBG("Error when sending the data pending ret code: %d", ret
);
1459 * No need to send back a status message since the data pending
1460 * returned value is the response.
1464 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1467 struct ustctl_consumer_channel_attr attr
;
1469 /* Create a plain object and reserve a channel key. */
1470 channel
= consumer_allocate_channel(
1471 msg
.u
.ask_channel
.key
,
1472 msg
.u
.ask_channel
.session_id
,
1473 msg
.u
.ask_channel
.pathname
,
1474 msg
.u
.ask_channel
.name
,
1475 msg
.u
.ask_channel
.uid
,
1476 msg
.u
.ask_channel
.gid
,
1477 msg
.u
.ask_channel
.relayd_id
,
1478 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1479 msg
.u
.ask_channel
.tracefile_size
,
1480 msg
.u
.ask_channel
.tracefile_count
,
1481 msg
.u
.ask_channel
.session_id_per_pid
,
1482 msg
.u
.ask_channel
.monitor
,
1483 msg
.u
.ask_channel
.live_timer_interval
,
1484 msg
.u
.ask_channel
.is_live
,
1485 msg
.u
.ask_channel
.root_shm_path
,
1486 msg
.u
.ask_channel
.shm_path
);
1488 goto end_channel_error
;
1492 * Assign UST application UID to the channel. This value is ignored for
1493 * per PID buffers. This is specific to UST thus setting this after the
1496 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1498 /* Build channel attributes from received message. */
1499 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1500 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1501 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1502 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1503 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1504 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1505 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1507 /* Match channel buffer type to the UST abi. */
1508 switch (msg
.u
.ask_channel
.output
) {
1509 case LTTNG_EVENT_MMAP
:
1511 attr
.output
= LTTNG_UST_MMAP
;
1515 /* Translate and save channel type. */
1516 switch (msg
.u
.ask_channel
.type
) {
1517 case LTTNG_UST_CHAN_PER_CPU
:
1518 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1519 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1521 * Set refcount to 1 for owner. Below, we will
1522 * pass ownership to the
1523 * consumer_thread_channel_poll() thread.
1525 channel
->refcount
= 1;
1527 case LTTNG_UST_CHAN_METADATA
:
1528 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1529 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1536 health_code_update();
1538 ret
= ask_channel(ctx
, sock
, channel
, &attr
);
1540 goto end_channel_error
;
1543 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1544 ret
= consumer_metadata_cache_allocate(channel
);
1546 ERR("Allocating metadata cache");
1547 goto end_channel_error
;
1549 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1550 attr
.switch_timer_interval
= 0;
1552 consumer_timer_live_start(channel
,
1553 msg
.u
.ask_channel
.live_timer_interval
);
1556 health_code_update();
1559 * Add the channel to the internal state AFTER all streams were created
1560 * and successfully sent to session daemon. This way, all streams must
1561 * be ready before this channel is visible to the threads.
1562 * If add_channel succeeds, ownership of the channel is
1563 * passed to consumer_thread_channel_poll().
1565 ret
= add_channel(channel
, ctx
);
1567 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1568 if (channel
->switch_timer_enabled
== 1) {
1569 consumer_timer_switch_stop(channel
);
1571 consumer_metadata_cache_destroy(channel
);
1573 if (channel
->live_timer_enabled
== 1) {
1574 consumer_timer_live_stop(channel
);
1576 goto end_channel_error
;
1579 health_code_update();
1582 * Channel and streams are now created. Inform the session daemon that
1583 * everything went well and should wait to receive the channel and
1584 * streams with ustctl API.
1586 ret
= consumer_send_status_channel(sock
, channel
);
1589 * There is probably a problem on the socket.
1596 case LTTNG_CONSUMER_GET_CHANNEL
:
1598 int ret
, relayd_err
= 0;
1599 uint64_t key
= msg
.u
.get_channel
.key
;
1600 struct lttng_consumer_channel
*channel
;
1602 channel
= consumer_find_channel(key
);
1604 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1605 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1606 goto end_msg_sessiond
;
1609 health_code_update();
1611 /* Send everything to sessiond. */
1612 ret
= send_sessiond_channel(sock
, channel
, ctx
, &relayd_err
);
1616 * We were unable to send to the relayd the stream so avoid
1617 * sending back a fatal error to the thread since this is OK
1618 * and the consumer can continue its work. The above call
1619 * has sent the error status message to the sessiond.
1624 * The communicaton was broken hence there is a bad state between
1625 * the consumer and sessiond so stop everything.
1630 health_code_update();
1633 * In no monitor mode, the streams ownership is kept inside the channel
1634 * so don't send them to the data thread.
1636 if (!channel
->monitor
) {
1637 goto end_msg_sessiond
;
1640 ret
= send_streams_to_thread(channel
, ctx
);
1643 * If we are unable to send the stream to the thread, there is
1644 * a big problem so just stop everything.
1648 /* List MUST be empty after or else it could be reused. */
1649 assert(cds_list_empty(&channel
->streams
.head
));
1650 goto end_msg_sessiond
;
1652 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1654 uint64_t key
= msg
.u
.destroy_channel
.key
;
1657 * Only called if streams have not been sent to stream
1658 * manager thread. However, channel has been sent to
1659 * channel manager thread.
1661 notify_thread_del_channel(ctx
, key
);
1662 goto end_msg_sessiond
;
1664 case LTTNG_CONSUMER_CLOSE_METADATA
:
1668 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1673 goto end_msg_sessiond
;
1675 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1679 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1684 goto end_msg_sessiond
;
1686 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1690 ret
= clear_quiescent_channel(
1691 msg
.u
.clear_quiescent_channel
.key
);
1696 goto end_msg_sessiond
;
1698 case LTTNG_CONSUMER_PUSH_METADATA
:
1701 uint64_t len
= msg
.u
.push_metadata
.len
;
1702 uint64_t key
= msg
.u
.push_metadata
.key
;
1703 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1704 uint64_t version
= msg
.u
.push_metadata
.version
;
1705 struct lttng_consumer_channel
*channel
;
1707 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1710 channel
= consumer_find_channel(key
);
1713 * This is possible if the metadata creation on the consumer side
1714 * is in flight vis-a-vis a concurrent push metadata from the
1715 * session daemon. Simply return that the channel failed and the
1716 * session daemon will handle that message correctly considering
1717 * that this race is acceptable thus the DBG() statement here.
1719 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1720 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1721 goto end_msg_sessiond
;
1724 health_code_update();
1728 * There is nothing to receive. We have simply
1729 * checked whether the channel can be found.
1731 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1732 goto end_msg_sessiond
;
1735 /* Tell session daemon we are ready to receive the metadata. */
1736 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1738 /* Somehow, the session daemon is not responding anymore. */
1742 health_code_update();
1744 /* Wait for more data. */
1745 health_poll_entry();
1746 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1752 health_code_update();
1754 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1755 len
, version
, channel
, 0, 1);
1757 /* error receiving from sessiond */
1761 goto end_msg_sessiond
;
1764 case LTTNG_CONSUMER_SETUP_METADATA
:
1768 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1772 goto end_msg_sessiond
;
1774 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1776 if (msg
.u
.snapshot_channel
.metadata
) {
1777 ret
= snapshot_metadata(msg
.u
.snapshot_channel
.key
,
1778 msg
.u
.snapshot_channel
.pathname
,
1779 msg
.u
.snapshot_channel
.relayd_id
,
1782 ERR("Snapshot metadata failed");
1783 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1786 ret
= snapshot_channel(msg
.u
.snapshot_channel
.key
,
1787 msg
.u
.snapshot_channel
.pathname
,
1788 msg
.u
.snapshot_channel
.relayd_id
,
1789 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1792 ERR("Snapshot channel failed");
1793 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1797 health_code_update();
1798 ret
= consumer_send_status_msg(sock
, ret_code
);
1800 /* Somehow, the session daemon is not responding anymore. */
1803 health_code_update();
1806 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1809 uint64_t discarded_events
;
1810 struct lttng_ht_iter iter
;
1811 struct lttng_ht
*ht
;
1812 struct lttng_consumer_stream
*stream
;
1813 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1814 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1816 DBG("UST consumer discarded events command for session id %"
1819 pthread_mutex_lock(&consumer_data
.lock
);
1821 ht
= consumer_data
.stream_list_ht
;
1824 * We only need a reference to the channel, but they are not
1825 * directly indexed, so we just use the first matching stream
1826 * to extract the information we need, we default to 0 if not
1827 * found (no events are dropped if the channel is not yet in
1830 discarded_events
= 0;
1831 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1832 ht
->hash_fct(&id
, lttng_ht_seed
),
1834 &iter
.iter
, stream
, node_session_id
.node
) {
1835 if (stream
->chan
->key
== key
) {
1836 discarded_events
= stream
->chan
->discarded_events
;
1840 pthread_mutex_unlock(&consumer_data
.lock
);
1843 DBG("UST consumer discarded events command for session id %"
1844 PRIu64
", channel key %" PRIu64
, id
, key
);
1846 health_code_update();
1848 /* Send back returned value to session daemon */
1849 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1851 PERROR("send discarded events");
1857 case LTTNG_CONSUMER_LOST_PACKETS
:
1860 uint64_t lost_packets
;
1861 struct lttng_ht_iter iter
;
1862 struct lttng_ht
*ht
;
1863 struct lttng_consumer_stream
*stream
;
1864 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1865 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1867 DBG("UST consumer lost packets command for session id %"
1870 pthread_mutex_lock(&consumer_data
.lock
);
1872 ht
= consumer_data
.stream_list_ht
;
1875 * We only need a reference to the channel, but they are not
1876 * directly indexed, so we just use the first matching stream
1877 * to extract the information we need, we default to 0 if not
1878 * found (no packets lost if the channel is not yet in use).
1881 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1882 ht
->hash_fct(&id
, lttng_ht_seed
),
1884 &iter
.iter
, stream
, node_session_id
.node
) {
1885 if (stream
->chan
->key
== key
) {
1886 lost_packets
= stream
->chan
->lost_packets
;
1890 pthread_mutex_unlock(&consumer_data
.lock
);
1893 DBG("UST consumer lost packets command for session id %"
1894 PRIu64
", channel key %" PRIu64
, id
, key
);
1896 health_code_update();
1898 /* Send back returned value to session daemon */
1899 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1900 sizeof(lost_packets
));
1902 PERROR("send lost packets");
1915 health_code_update();
1918 * Return 1 to indicate success since the 0 value can be a socket
1919 * shutdown during the recv() or send() call.
1925 * The returned value here is not useful since either way we'll return 1 to
1926 * the caller because the session daemon socket management is done
1927 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1929 ret
= consumer_send_status_msg(sock
, ret_code
);
1935 health_code_update();
1941 * Free channel here since no one has a reference to it. We don't
1942 * free after that because a stream can store this pointer.
1944 destroy_channel(channel
);
1946 /* We have to send a status channel message indicating an error. */
1947 ret
= consumer_send_status_channel(sock
, NULL
);
1949 /* Stop everything if session daemon can not be notified. */
1954 health_code_update();
1959 /* This will issue a consumer stop. */
1963 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
1964 int producer_active
)
1967 assert(stream
->ustream
);
1969 ustctl_flush_buffer(stream
->ustream
, producer_active
);
1973 * Take a snapshot for a specific fd
1975 * Returns 0 on success, < 0 on error
1977 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1980 assert(stream
->ustream
);
1982 return ustctl_snapshot(stream
->ustream
);
1986 * Get the produced position
1988 * Returns 0 on success, < 0 on error
1990 int lttng_ustconsumer_get_produced_snapshot(
1991 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
1994 assert(stream
->ustream
);
1997 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2001 * Get the consumed position
2003 * Returns 0 on success, < 0 on error
2005 int lttng_ustconsumer_get_consumed_snapshot(
2006 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2009 assert(stream
->ustream
);
2012 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2015 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2019 assert(stream
->ustream
);
2021 ustctl_flush_buffer(stream
->ustream
, producer
);
2024 int lttng_ustconsumer_get_current_timestamp(
2025 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2028 assert(stream
->ustream
);
2031 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2034 int lttng_ustconsumer_get_sequence_number(
2035 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2038 assert(stream
->ustream
);
2041 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2045 * Called when the stream signals the consumer that it has hung up.
2047 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2050 assert(stream
->ustream
);
2052 pthread_mutex_lock(&stream
->lock
);
2053 if (!stream
->quiescent
) {
2054 ustctl_flush_buffer(stream
->ustream
, 0);
2055 stream
->quiescent
= true;
2057 pthread_mutex_unlock(&stream
->lock
);
2058 stream
->hangup_flush_done
= 1;
2061 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2066 assert(chan
->uchan
);
2068 if (chan
->switch_timer_enabled
== 1) {
2069 consumer_timer_switch_stop(chan
);
2071 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2074 ret
= close(chan
->stream_fds
[i
]);
2078 if (chan
->shm_path
[0]) {
2079 char shm_path
[PATH_MAX
];
2081 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2083 ERR("Cannot get stream shm path");
2085 ret
= run_as_unlink(shm_path
, chan
->uid
, chan
->gid
);
2087 PERROR("unlink %s", shm_path
);
2093 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2096 assert(chan
->uchan
);
2098 consumer_metadata_cache_destroy(chan
);
2099 ustctl_destroy_channel(chan
->uchan
);
2100 /* Try to rmdir all directories under shm_path root. */
2101 if (chan
->root_shm_path
[0]) {
2102 (void) run_as_recursive_rmdir(chan
->root_shm_path
,
2103 chan
->uid
, chan
->gid
);
2105 free(chan
->stream_fds
);
2108 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2111 assert(stream
->ustream
);
2113 if (stream
->chan
->switch_timer_enabled
== 1) {
2114 consumer_timer_switch_stop(stream
->chan
);
2116 ustctl_destroy_stream(stream
->ustream
);
2119 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2122 assert(stream
->ustream
);
2124 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2127 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2130 assert(stream
->ustream
);
2132 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2136 * Populate index values of a UST stream. Values are set in big endian order.
2138 * Return 0 on success or else a negative value.
2140 static int get_index_values(struct ctf_packet_index
*index
,
2141 struct ustctl_consumer_stream
*ustream
)
2145 ret
= ustctl_get_timestamp_begin(ustream
, &index
->timestamp_begin
);
2147 PERROR("ustctl_get_timestamp_begin");
2150 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
2152 ret
= ustctl_get_timestamp_end(ustream
, &index
->timestamp_end
);
2154 PERROR("ustctl_get_timestamp_end");
2157 index
->timestamp_end
= htobe64(index
->timestamp_end
);
2159 ret
= ustctl_get_events_discarded(ustream
, &index
->events_discarded
);
2161 PERROR("ustctl_get_events_discarded");
2164 index
->events_discarded
= htobe64(index
->events_discarded
);
2166 ret
= ustctl_get_content_size(ustream
, &index
->content_size
);
2168 PERROR("ustctl_get_content_size");
2171 index
->content_size
= htobe64(index
->content_size
);
2173 ret
= ustctl_get_packet_size(ustream
, &index
->packet_size
);
2175 PERROR("ustctl_get_packet_size");
2178 index
->packet_size
= htobe64(index
->packet_size
);
2180 ret
= ustctl_get_stream_id(ustream
, &index
->stream_id
);
2182 PERROR("ustctl_get_stream_id");
2185 index
->stream_id
= htobe64(index
->stream_id
);
2187 ret
= ustctl_get_instance_id(ustream
, &index
->stream_instance_id
);
2189 PERROR("ustctl_get_instance_id");
2192 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
2194 ret
= ustctl_get_sequence_number(ustream
, &index
->packet_seq_num
);
2196 PERROR("ustctl_get_sequence_number");
2199 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
2206 void metadata_stream_reset_cache(struct lttng_consumer_stream
*stream
,
2207 struct consumer_metadata_cache
*cache
)
2209 DBG("Metadata stream update to version %" PRIu64
,
2211 stream
->ust_metadata_pushed
= 0;
2212 stream
->metadata_version
= cache
->version
;
2213 stream
->reset_metadata_flag
= 1;
2217 * Check if the version of the metadata stream and metadata cache match.
2218 * If the cache got updated, reset the metadata stream.
2219 * The stream lock and metadata cache lock MUST be held.
2220 * Return 0 on success, a negative value on error.
2223 int metadata_stream_check_version(struct lttng_consumer_stream
*stream
)
2226 struct consumer_metadata_cache
*cache
= stream
->chan
->metadata_cache
;
2228 if (cache
->version
== stream
->metadata_version
) {
2231 metadata_stream_reset_cache(stream
, cache
);
2238 * Write up to one packet from the metadata cache to the channel.
2240 * Returns the number of bytes pushed in the cache, or a negative value
2244 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2249 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2250 ret
= metadata_stream_check_version(stream
);
2254 if (stream
->chan
->metadata_cache
->max_offset
2255 == stream
->ust_metadata_pushed
) {
2260 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2261 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2262 stream
->chan
->metadata_cache
->max_offset
2263 - stream
->ust_metadata_pushed
);
2264 assert(write_len
!= 0);
2265 if (write_len
< 0) {
2266 ERR("Writing one metadata packet");
2270 stream
->ust_metadata_pushed
+= write_len
;
2272 assert(stream
->chan
->metadata_cache
->max_offset
>=
2273 stream
->ust_metadata_pushed
);
2277 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2283 * Sync metadata meaning request them to the session daemon and snapshot to the
2284 * metadata thread can consumer them.
2286 * Metadata stream lock is held here, but we need to release it when
2287 * interacting with sessiond, else we cause a deadlock with live
2288 * awaiting on metadata to be pushed out.
2290 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
2291 * is empty or a negative value on error.
2293 int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2294 struct lttng_consumer_stream
*metadata
)
2302 pthread_mutex_unlock(&metadata
->lock
);
2304 * Request metadata from the sessiond, but don't wait for the flush
2305 * because we locked the metadata thread.
2307 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata
->chan
, 0, 0);
2308 pthread_mutex_lock(&metadata
->lock
);
2313 ret
= commit_one_metadata_packet(metadata
);
2316 } else if (ret
> 0) {
2320 ustctl_flush_buffer(metadata
->ustream
, 1);
2321 ret
= ustctl_snapshot(metadata
->ustream
);
2323 if (errno
!= EAGAIN
) {
2324 ERR("Sync metadata, taking UST snapshot");
2327 DBG("No new metadata when syncing them.");
2328 /* No new metadata, exit. */
2334 * After this flush, we still need to extract metadata.
2345 * Return 0 on success else a negative value.
2347 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2348 struct lttng_consumer_local_data
*ctx
)
2351 struct ustctl_consumer_stream
*ustream
;
2356 ustream
= stream
->ustream
;
2359 * First, we are going to check if there is a new subbuffer available
2360 * before reading the stream wait_fd.
2362 /* Get the next subbuffer */
2363 ret
= ustctl_get_next_subbuf(ustream
);
2365 /* No more data found, flag the stream. */
2366 stream
->has_data
= 0;
2371 ret
= ustctl_put_subbuf(ustream
);
2374 /* This stream still has data. Flag it and wake up the data thread. */
2375 stream
->has_data
= 1;
2377 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2380 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2381 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2386 /* The wake up pipe has been notified. */
2387 ctx
->has_wakeup
= 1;
2396 int update_stream_stats(struct lttng_consumer_stream
*stream
)
2399 uint64_t seq
, discarded
;
2401 ret
= ustctl_get_sequence_number(stream
->ustream
, &seq
);
2403 PERROR("ustctl_get_sequence_number");
2407 * Start the sequence when we extract the first packet in case we don't
2408 * start at 0 (for example if a consumer is not connected to the
2409 * session immediately after the beginning).
2411 if (stream
->last_sequence_number
== -1ULL) {
2412 stream
->last_sequence_number
= seq
;
2413 } else if (seq
> stream
->last_sequence_number
) {
2414 stream
->chan
->lost_packets
+= seq
-
2415 stream
->last_sequence_number
- 1;
2417 /* seq <= last_sequence_number */
2418 ERR("Sequence number inconsistent : prev = %" PRIu64
2419 ", current = %" PRIu64
,
2420 stream
->last_sequence_number
, seq
);
2424 stream
->last_sequence_number
= seq
;
2426 ret
= ustctl_get_events_discarded(stream
->ustream
, &discarded
);
2428 PERROR("kernctl_get_events_discarded");
2431 if (discarded
< stream
->last_discarded_events
) {
2433 * Overflow has occurred. We assume only one wrap-around
2436 stream
->chan
->discarded_events
+=
2437 (1ULL << (CAA_BITS_PER_LONG
- 1)) -
2438 stream
->last_discarded_events
+ discarded
;
2440 stream
->chan
->discarded_events
+= discarded
-
2441 stream
->last_discarded_events
;
2443 stream
->last_discarded_events
= discarded
;
2451 * Read subbuffer from the given stream.
2453 * Stream lock MUST be acquired.
2455 * Return 0 on success else a negative value.
2457 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2458 struct lttng_consumer_local_data
*ctx
)
2460 unsigned long len
, subbuf_size
, padding
;
2461 int err
, write_index
= 1;
2463 struct ustctl_consumer_stream
*ustream
;
2464 struct ctf_packet_index index
;
2465 const char *subbuf_addr
;
2466 struct lttng_buffer_view subbuf_view
;
2469 assert(stream
->ustream
);
2472 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream
->wait_fd
,
2475 /* Ease our life for what's next. */
2476 ustream
= stream
->ustream
;
2479 * We can consume the 1 byte written into the wait_fd by UST. Don't trigger
2480 * error if we cannot read this one byte (read returns 0), or if the error
2481 * is EAGAIN or EWOULDBLOCK.
2483 * This is only done when the stream is monitored by a thread, before the
2484 * flush is done after a hangup and if the stream is not flagged with data
2485 * since there might be nothing to consume in the wait fd but still have
2486 * data available flagged by the consumer wake up pipe.
2488 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2492 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2493 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2500 /* Get the next subbuffer */
2501 err
= ustctl_get_next_subbuf(ustream
);
2504 * Populate metadata info if the existing info has
2505 * already been read.
2507 if (stream
->metadata_flag
) {
2508 ret
= commit_one_metadata_packet(stream
);
2512 ustctl_flush_buffer(stream
->ustream
, 1);
2516 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
2518 * This is a debug message even for single-threaded consumer,
2519 * because poll() have more relaxed criterions than get subbuf,
2520 * so get_subbuf may fail for short race windows where poll()
2521 * would issue wakeups.
2523 DBG("Reserving sub buffer failed (everything is normal, "
2524 "it is due to concurrency) [ret: %d]", err
);
2527 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
2529 if (!stream
->metadata_flag
) {
2530 index
.offset
= htobe64(stream
->out_fd_offset
);
2531 ret
= get_index_values(&index
, ustream
);
2533 err
= ustctl_put_subbuf(ustream
);
2538 /* Update the stream's sequence and discarded events count. */
2539 ret
= update_stream_stats(stream
);
2541 PERROR("kernctl_get_events_discarded");
2542 err
= ustctl_put_subbuf(ustream
);
2550 /* Get the full padded subbuffer size */
2551 err
= ustctl_get_padded_subbuf_size(ustream
, &len
);
2554 /* Get subbuffer data size (without padding) */
2555 err
= ustctl_get_subbuf_size(ustream
, &subbuf_size
);
2558 /* Make sure we don't get a subbuffer size bigger than the padded */
2559 assert(len
>= subbuf_size
);
2561 padding
= len
- subbuf_size
;
2563 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
2566 goto error_put_subbuf
;
2569 subbuf_view
= lttng_buffer_view_init(subbuf_addr
, 0, len
);
2571 /* write the subbuffer to the tracefile */
2572 ret
= lttng_consumer_on_read_subbuffer_mmap(
2573 ctx
, stream
, &subbuf_view
, padding
, &index
);
2575 * The mmap operation should write subbuf_size amount of data when
2576 * network streaming or the full padding (len) size when we are _not_
2579 if ((ret
!= subbuf_size
&& stream
->relayd_id
!= (uint64_t) -1ULL) ||
2580 (ret
!= len
&& stream
->relayd_id
== (uint64_t) -1ULL)) {
2582 * Display the error but continue processing to try to release the
2583 * subbuffer. This is a DBG statement since any unexpected kill or
2584 * signal, the application gets unregistered, relayd gets closed or
2585 * anything that affects the buffer lifetime will trigger this error.
2586 * So, for the sake of the user, don't print this error since it can
2587 * happen and it is OK with the code flow.
2589 DBG("Error writing to tracefile "
2590 "(ret: %ld != len: %lu != subbuf_size: %lu)",
2591 ret
, len
, subbuf_size
);
2595 err
= ustctl_put_next_subbuf(ustream
);
2599 * This will consumer the byte on the wait_fd if and only if there is not
2600 * next subbuffer to be acquired.
2602 if (!stream
->metadata_flag
) {
2603 ret
= notify_if_more_data(stream
, ctx
);
2609 /* Write index if needed. */
2614 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
2616 * In live, block until all the metadata is sent.
2618 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2619 assert(!stream
->missed_metadata_flush
);
2620 stream
->waiting_on_metadata
= true;
2621 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2623 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
2625 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2626 stream
->waiting_on_metadata
= false;
2627 if (stream
->missed_metadata_flush
) {
2628 stream
->missed_metadata_flush
= false;
2629 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2630 (void) consumer_flush_ust_index(stream
);
2632 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2640 assert(!stream
->metadata_flag
);
2641 err
= consumer_stream_write_index(stream
, &index
);
2651 * Called when a stream is created.
2653 * Return 0 on success or else a negative value.
2655 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2661 /* Don't create anything if this is set for streaming. */
2662 if (stream
->relayd_id
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
2663 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
2664 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
2665 stream
->uid
, stream
->gid
, NULL
);
2669 stream
->out_fd
= ret
;
2670 stream
->tracefile_size_current
= 0;
2672 if (!stream
->metadata_flag
) {
2673 struct lttng_index_file
*index_file
;
2675 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
2676 stream
->name
, stream
->uid
, stream
->gid
,
2677 stream
->chan
->tracefile_size
,
2678 stream
->tracefile_count_current
,
2679 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
2683 stream
->index_file
= index_file
;
2693 * Check if data is still being extracted from the buffers for a specific
2694 * stream. Consumer data lock MUST be acquired before calling this function
2695 * and the stream lock.
2697 * Return 1 if the traced data are still getting read else 0 meaning that the
2698 * data is available for trace viewer reading.
2700 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
2705 assert(stream
->ustream
);
2707 DBG("UST consumer checking data pending");
2709 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
2714 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
2715 uint64_t contiguous
, pushed
;
2717 /* Ease our life a bit. */
2718 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
2719 pushed
= stream
->ust_metadata_pushed
;
2722 * We can simply check whether all contiguously available data
2723 * has been pushed to the ring buffer, since the push operation
2724 * is performed within get_next_subbuf(), and because both
2725 * get_next_subbuf() and put_next_subbuf() are issued atomically
2726 * thanks to the stream lock within
2727 * lttng_ustconsumer_read_subbuffer(). This basically means that
2728 * whetnever ust_metadata_pushed is incremented, the associated
2729 * metadata has been consumed from the metadata stream.
2731 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
2732 contiguous
, pushed
);
2733 assert(((int64_t) (contiguous
- pushed
)) >= 0);
2734 if ((contiguous
!= pushed
) ||
2735 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
2736 ret
= 1; /* Data is pending */
2740 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2743 * There is still data so let's put back this
2746 ret
= ustctl_put_subbuf(stream
->ustream
);
2748 ret
= 1; /* Data is pending */
2753 /* Data is NOT pending so ready to be read. */
2761 * Stop a given metadata channel timer if enabled and close the wait fd which
2762 * is the poll pipe of the metadata stream.
2764 * This MUST be called with the metadata channel acquired.
2766 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
2771 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
2773 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
2775 if (metadata
->switch_timer_enabled
== 1) {
2776 consumer_timer_switch_stop(metadata
);
2779 if (!metadata
->metadata_stream
) {
2784 * Closing write side so the thread monitoring the stream wakes up if any
2785 * and clean the metadata stream.
2787 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
2788 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
2790 PERROR("closing metadata pipe write side");
2792 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
2800 * Close every metadata stream wait fd of the metadata hash table. This
2801 * function MUST be used very carefully so not to run into a race between the
2802 * metadata thread handling streams and this function closing their wait fd.
2804 * For UST, this is used when the session daemon hangs up. Its the metadata
2805 * producer so calling this is safe because we are assured that no state change
2806 * can occur in the metadata thread for the streams in the hash table.
2808 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
2810 struct lttng_ht_iter iter
;
2811 struct lttng_consumer_stream
*stream
;
2813 assert(metadata_ht
);
2814 assert(metadata_ht
->ht
);
2816 DBG("UST consumer closing all metadata streams");
2819 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
2822 health_code_update();
2824 pthread_mutex_lock(&stream
->chan
->lock
);
2825 lttng_ustconsumer_close_metadata(stream
->chan
);
2826 pthread_mutex_unlock(&stream
->chan
->lock
);
2832 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
2836 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
2838 ERR("Unable to close wakeup fd");
2843 * Please refer to consumer-timer.c before adding any lock within this
2844 * function or any of its callees. Timers have a very strict locking
2845 * semantic with respect to teardown. Failure to respect this semantic
2846 * introduces deadlocks.
2848 * DON'T hold the metadata lock when calling this function, else this
2849 * can cause deadlock involving consumer awaiting for metadata to be
2850 * pushed out due to concurrent interaction with the session daemon.
2852 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
2853 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
2855 struct lttcomm_metadata_request_msg request
;
2856 struct lttcomm_consumer_msg msg
;
2857 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
2858 uint64_t len
, key
, offset
, version
;
2862 assert(channel
->metadata_cache
);
2864 memset(&request
, 0, sizeof(request
));
2866 /* send the metadata request to sessiond */
2867 switch (consumer_data
.type
) {
2868 case LTTNG_CONSUMER64_UST
:
2869 request
.bits_per_long
= 64;
2871 case LTTNG_CONSUMER32_UST
:
2872 request
.bits_per_long
= 32;
2875 request
.bits_per_long
= 0;
2879 request
.session_id
= channel
->session_id
;
2880 request
.session_id_per_pid
= channel
->session_id_per_pid
;
2882 * Request the application UID here so the metadata of that application can
2883 * be sent back. The channel UID corresponds to the user UID of the session
2884 * used for the rights on the stream file(s).
2886 request
.uid
= channel
->ust_app_uid
;
2887 request
.key
= channel
->key
;
2889 DBG("Sending metadata request to sessiond, session id %" PRIu64
2890 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
2891 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
2894 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
2896 health_code_update();
2898 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
2901 ERR("Asking metadata to sessiond");
2905 health_code_update();
2907 /* Receive the metadata from sessiond */
2908 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
2910 if (ret
!= sizeof(msg
)) {
2911 DBG("Consumer received unexpected message size %d (expects %zu)",
2913 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
2915 * The ret value might 0 meaning an orderly shutdown but this is ok
2916 * since the caller handles this.
2921 health_code_update();
2923 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
2924 /* No registry found */
2925 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
2929 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
2930 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
2935 len
= msg
.u
.push_metadata
.len
;
2936 key
= msg
.u
.push_metadata
.key
;
2937 offset
= msg
.u
.push_metadata
.target_offset
;
2938 version
= msg
.u
.push_metadata
.version
;
2940 assert(key
== channel
->key
);
2942 DBG("No new metadata to receive for key %" PRIu64
, key
);
2945 health_code_update();
2947 /* Tell session daemon we are ready to receive the metadata. */
2948 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
2949 LTTCOMM_CONSUMERD_SUCCESS
);
2950 if (ret
< 0 || len
== 0) {
2952 * Somehow, the session daemon is not responding anymore or there is
2953 * nothing to receive.
2958 health_code_update();
2960 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
2961 key
, offset
, len
, version
, channel
, timer
, wait
);
2964 * Only send the status msg if the sessiond is alive meaning a positive
2967 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
2972 health_code_update();
2974 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
2979 * Return the ustctl call for the get stream id.
2981 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
2982 uint64_t *stream_id
)
2987 return ustctl_get_stream_id(stream
->ustream
, stream_id
);