2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <lttng/ust-ctl.h>
29 #include <sys/socket.h>
31 #include <sys/types.h>
34 #include <urcu/list.h>
38 #include <bin/lttng-consumerd/health-consumerd.h>
39 #include <common/common.h>
40 #include <common/sessiond-comm/sessiond-comm.h>
41 #include <common/relayd/relayd.h>
42 #include <common/compat/fcntl.h>
43 #include <common/compat/endian.h>
44 #include <common/consumer/consumer-metadata-cache.h>
45 #include <common/consumer/consumer-stream.h>
46 #include <common/consumer/consumer-timer.h>
47 #include <common/utils.h>
48 #include <common/index/index.h>
50 #include "ust-consumer.h"
52 #define INT_MAX_STR_LEN 12 /* includes \0 */
54 extern struct lttng_consumer_global_data consumer_data
;
55 extern int consumer_poll_timeout
;
58 * Free channel object and all streams associated with it. This MUST be used
59 * only and only if the channel has _NEVER_ been added to the global channel
62 static void destroy_channel(struct lttng_consumer_channel
*channel
)
64 struct lttng_consumer_stream
*stream
, *stmp
;
68 DBG("UST consumer cleaning stream list");
70 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
75 cds_list_del(&stream
->send_node
);
76 ustctl_destroy_stream(stream
->ustream
);
77 lttng_trace_chunk_put(stream
->trace_chunk
);
82 * If a channel is available meaning that was created before the streams
86 lttng_ustconsumer_del_channel(channel
);
87 lttng_ustconsumer_free_channel(channel
);
93 * Add channel to internal consumer state.
95 * Returns 0 on success or else a negative value.
97 static int add_channel(struct lttng_consumer_channel
*channel
,
98 struct lttng_consumer_local_data
*ctx
)
105 if (ctx
->on_recv_channel
!= NULL
) {
106 ret
= ctx
->on_recv_channel(channel
);
108 ret
= consumer_add_channel(channel
, ctx
);
109 } else if (ret
< 0) {
110 /* Most likely an ENOMEM. */
111 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
115 ret
= consumer_add_channel(channel
, ctx
);
118 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
125 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
126 * error value if applicable is set in it else it is kept untouched.
128 * Return NULL on error else the newly allocated stream object.
130 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
131 struct lttng_consumer_channel
*channel
,
132 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
135 struct lttng_consumer_stream
*stream
= NULL
;
140 stream
= consumer_allocate_stream(
147 channel
->trace_chunk
,
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
);
170 consumer_stream_update_channel_attributes(stream
, channel
);
174 *_alloc_ret
= alloc_ret
;
180 * Send the given stream pointer to the corresponding thread.
182 * Returns 0 on success else a negative value.
184 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
185 struct lttng_consumer_local_data
*ctx
)
188 struct lttng_pipe
*stream_pipe
;
190 /* Get the right pipe where the stream will be sent. */
191 if (stream
->metadata_flag
) {
192 consumer_add_metadata_stream(stream
);
193 stream_pipe
= ctx
->consumer_metadata_pipe
;
195 consumer_add_data_stream(stream
);
196 stream_pipe
= ctx
->consumer_data_pipe
;
200 * From this point on, the stream's ownership has been moved away from
201 * the channel and it becomes globally visible. Hence, remove it from
202 * the local stream list to prevent the stream from being both local and
205 stream
->globally_visible
= 1;
206 cds_list_del(&stream
->send_node
);
208 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
210 ERR("Consumer write %s stream to pipe %d",
211 stream
->metadata_flag
? "metadata" : "data",
212 lttng_pipe_get_writefd(stream_pipe
));
213 if (stream
->metadata_flag
) {
214 consumer_del_stream_for_metadata(stream
);
216 consumer_del_stream_for_data(stream
);
226 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
228 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
231 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
232 stream_shm_path
[PATH_MAX
- 1] = '\0';
233 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
238 strncat(stream_shm_path
, cpu_nr
,
239 PATH_MAX
- strlen(stream_shm_path
) - 1);
246 * Create streams for the given channel using liblttng-ust-ctl.
247 * The channel lock must be acquired by the caller.
249 * Return 0 on success else a negative value.
251 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
252 struct lttng_consumer_local_data
*ctx
)
255 struct ustctl_consumer_stream
*ustream
;
256 struct lttng_consumer_stream
*stream
;
257 pthread_mutex_t
*current_stream_lock
= NULL
;
263 * While a stream is available from ustctl. When NULL is returned, we've
264 * reached the end of the possible stream for the channel.
266 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
268 int ust_metadata_pipe
[2];
270 health_code_update();
272 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
273 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
275 ERR("Create ust metadata poll pipe");
278 wait_fd
= ust_metadata_pipe
[0];
280 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
283 /* Allocate consumer stream object. */
284 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
288 stream
->ustream
= ustream
;
290 * Store it so we can save multiple function calls afterwards since
291 * this value is used heavily in the stream threads. This is UST
292 * specific so this is why it's done after allocation.
294 stream
->wait_fd
= wait_fd
;
297 * Increment channel refcount since the channel reference has now been
298 * assigned in the allocation process above.
300 if (stream
->chan
->monitor
) {
301 uatomic_inc(&stream
->chan
->refcount
);
304 pthread_mutex_lock(&stream
->lock
);
305 current_stream_lock
= &stream
->lock
;
307 * Order is important this is why a list is used. On error, the caller
308 * should clean this list.
310 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
312 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
313 &stream
->max_sb_size
);
315 ERR("ustctl_get_max_subbuf_size failed for stream %s",
320 /* Do actions once stream has been received. */
321 if (ctx
->on_recv_stream
) {
322 ret
= ctx
->on_recv_stream(stream
);
328 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
329 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
331 /* Set next CPU stream. */
332 channel
->streams
.count
= ++cpu
;
334 /* Keep stream reference when creating metadata. */
335 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
336 channel
->metadata_stream
= stream
;
337 if (channel
->monitor
) {
338 /* Set metadata poll pipe if we created one */
339 memcpy(stream
->ust_metadata_poll_pipe
,
341 sizeof(ust_metadata_pipe
));
344 pthread_mutex_unlock(&stream
->lock
);
345 current_stream_lock
= NULL
;
352 if (current_stream_lock
) {
353 pthread_mutex_unlock(current_stream_lock
);
359 * create_posix_shm is never called concurrently within a process.
362 int create_posix_shm(void)
364 char tmp_name
[NAME_MAX
];
367 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
373 * Allocate shm, and immediately unlink its shm oject, keeping
374 * only the file descriptor as a reference to the object.
375 * We specifically do _not_ use the / at the beginning of the
376 * pathname so that some OS implementations can keep it local to
377 * the process (POSIX leaves this implementation-defined).
379 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
384 ret
= shm_unlink(tmp_name
);
385 if (ret
< 0 && errno
!= ENOENT
) {
386 PERROR("shm_unlink");
387 goto error_shm_release
;
400 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
, int cpu
,
401 const struct lttng_credentials
*session_credentials
)
403 char shm_path
[PATH_MAX
];
406 if (!channel
->shm_path
[0]) {
407 return create_posix_shm();
409 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
413 return run_as_open(shm_path
,
414 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
415 session_credentials
->uid
, session_credentials
->gid
);
422 * Create an UST channel with the given attributes and send it to the session
423 * daemon using the ust ctl API.
425 * Return 0 on success or else a negative value.
427 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
428 struct ustctl_consumer_channel_attr
*attr
,
429 struct ustctl_consumer_channel
**ust_chanp
)
431 int ret
, nr_stream_fds
, i
, j
;
433 struct ustctl_consumer_channel
*ust_channel
;
438 assert(channel
->buffer_credentials
.is_set
);
440 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
441 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
442 "switch_timer_interval: %u, read_timer_interval: %u, "
443 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
444 attr
->num_subbuf
, attr
->switch_timer_interval
,
445 attr
->read_timer_interval
, attr
->output
, attr
->type
);
447 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
450 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
451 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
456 for (i
= 0; i
< nr_stream_fds
; i
++) {
457 stream_fds
[i
] = open_ust_stream_fd(channel
, i
,
458 &channel
->buffer_credentials
.value
);
459 if (stream_fds
[i
] < 0) {
464 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
469 channel
->nr_stream_fds
= nr_stream_fds
;
470 channel
->stream_fds
= stream_fds
;
471 *ust_chanp
= ust_channel
;
477 for (j
= i
- 1; j
>= 0; j
--) {
480 closeret
= close(stream_fds
[j
]);
484 if (channel
->shm_path
[0]) {
485 char shm_path
[PATH_MAX
];
487 closeret
= get_stream_shm_path(shm_path
,
488 channel
->shm_path
, j
);
490 ERR("Cannot get stream shm path");
492 closeret
= run_as_unlink(shm_path
,
493 channel
->buffer_credentials
.value
.uid
,
494 channel
->buffer_credentials
.value
.gid
);
496 PERROR("unlink %s", shm_path
);
500 /* Try to rmdir all directories under shm_path root. */
501 if (channel
->root_shm_path
[0]) {
502 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
503 channel
->buffer_credentials
.value
.uid
,
504 channel
->buffer_credentials
.value
.gid
,
505 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
513 * Send a single given stream to the session daemon using the sock.
515 * Return 0 on success else a negative value.
517 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
524 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
526 /* Send stream to session daemon. */
527 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
537 * Send channel to sessiond and relayd if applicable.
539 * Return 0 on success or else a negative value.
541 static int send_channel_to_sessiond_and_relayd(int sock
,
542 struct lttng_consumer_channel
*channel
,
543 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
545 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
546 struct lttng_consumer_stream
*stream
;
547 uint64_t net_seq_idx
= -1ULL;
553 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
555 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
556 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
558 health_code_update();
560 /* Try to send the stream to the relayd if one is available. */
561 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
562 stream
->key
, channel
->name
);
563 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
566 * Flag that the relayd was the problem here probably due to a
567 * communicaton error on the socket.
572 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
574 if (net_seq_idx
== -1ULL) {
575 net_seq_idx
= stream
->net_seq_idx
;
580 /* Inform sessiond that we are about to send channel and streams. */
581 ret
= consumer_send_status_msg(sock
, ret_code
);
582 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
584 * Either the session daemon is not responding or the relayd died so we
590 /* Send channel to sessiond. */
591 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
596 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
601 /* The channel was sent successfully to the sessiond at this point. */
602 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
604 health_code_update();
606 /* Send stream to session daemon. */
607 ret
= send_sessiond_stream(sock
, stream
);
613 /* Tell sessiond there is no more stream. */
614 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
619 DBG("UST consumer NULL stream sent to sessiond");
624 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
631 * Creates a channel and streams and add the channel it to the channel internal
632 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
635 * Return 0 on success or else, a negative value is returned and the channel
636 * MUST be destroyed by consumer_del_channel().
638 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
639 struct lttng_consumer_channel
*channel
,
640 struct ustctl_consumer_channel_attr
*attr
)
649 * This value is still used by the kernel consumer since for the kernel,
650 * the stream ownership is not IN the consumer so we need to have the
651 * number of left stream that needs to be initialized so we can know when
652 * to delete the channel (see consumer.c).
654 * As for the user space tracer now, the consumer creates and sends the
655 * stream to the session daemon which only sends them to the application
656 * once every stream of a channel is received making this value useless
657 * because we they will be added to the poll thread before the application
658 * receives them. This ensures that a stream can not hang up during
659 * initilization of a channel.
661 channel
->nb_init_stream_left
= 0;
663 /* The reply msg status is handled in the following call. */
664 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
669 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
672 * For the snapshots (no monitor), we create the metadata streams
673 * on demand, not during the channel creation.
675 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
680 /* Open all streams for this channel. */
681 pthread_mutex_lock(&channel
->lock
);
682 ret
= create_ust_streams(channel
, ctx
);
683 pthread_mutex_unlock(&channel
->lock
);
693 * Send all stream of a channel to the right thread handling it.
695 * On error, return a negative value else 0 on success.
697 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
698 struct lttng_consumer_local_data
*ctx
)
701 struct lttng_consumer_stream
*stream
, *stmp
;
706 /* Send streams to the corresponding thread. */
707 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
710 health_code_update();
712 /* Sending the stream to the thread. */
713 ret
= send_stream_to_thread(stream
, ctx
);
716 * If we are unable to send the stream to the thread, there is
717 * a big problem so just stop everything.
728 * Flush channel's streams using the given key to retrieve the channel.
730 * Return 0 on success else an LTTng error code.
732 static int flush_channel(uint64_t chan_key
)
735 struct lttng_consumer_channel
*channel
;
736 struct lttng_consumer_stream
*stream
;
738 struct lttng_ht_iter iter
;
740 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
743 channel
= consumer_find_channel(chan_key
);
745 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
746 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
750 ht
= consumer_data
.stream_per_chan_id_ht
;
752 /* For each stream of the channel id, flush it. */
753 cds_lfht_for_each_entry_duplicate(ht
->ht
,
754 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
755 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
757 health_code_update();
759 pthread_mutex_lock(&stream
->lock
);
762 * Protect against concurrent teardown of a stream.
764 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
768 if (!stream
->quiescent
) {
769 ustctl_flush_buffer(stream
->ustream
, 0);
770 stream
->quiescent
= true;
773 pthread_mutex_unlock(&stream
->lock
);
781 * Clear quiescent state from channel's streams using the given key to
782 * retrieve the channel.
784 * Return 0 on success else an LTTng error code.
786 static int clear_quiescent_channel(uint64_t chan_key
)
789 struct lttng_consumer_channel
*channel
;
790 struct lttng_consumer_stream
*stream
;
792 struct lttng_ht_iter iter
;
794 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
797 channel
= consumer_find_channel(chan_key
);
799 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
800 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
804 ht
= consumer_data
.stream_per_chan_id_ht
;
806 /* For each stream of the channel id, clear quiescent state. */
807 cds_lfht_for_each_entry_duplicate(ht
->ht
,
808 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
809 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
811 health_code_update();
813 pthread_mutex_lock(&stream
->lock
);
814 stream
->quiescent
= false;
815 pthread_mutex_unlock(&stream
->lock
);
823 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
825 * Return 0 on success else an LTTng error code.
827 static int close_metadata(uint64_t chan_key
)
830 struct lttng_consumer_channel
*channel
;
831 unsigned int channel_monitor
;
833 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
835 channel
= consumer_find_channel(chan_key
);
838 * This is possible if the metadata thread has issue a delete because
839 * the endpoint point of the stream hung up. There is no way the
840 * session daemon can know about it thus use a DBG instead of an actual
843 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
844 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
848 pthread_mutex_lock(&consumer_data
.lock
);
849 pthread_mutex_lock(&channel
->lock
);
850 channel_monitor
= channel
->monitor
;
851 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
855 lttng_ustconsumer_close_metadata(channel
);
856 pthread_mutex_unlock(&channel
->lock
);
857 pthread_mutex_unlock(&consumer_data
.lock
);
860 * The ownership of a metadata channel depends on the type of
861 * session to which it belongs. In effect, the monitor flag is checked
862 * to determine if this metadata channel is in "snapshot" mode or not.
864 * In the non-snapshot case, the metadata channel is created along with
865 * a single stream which will remain present until the metadata channel
866 * is destroyed (on the destruction of its session). In this case, the
867 * metadata stream in "monitored" by the metadata poll thread and holds
868 * the ownership of its channel.
870 * Closing the metadata will cause the metadata stream's "metadata poll
871 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
872 * thread which will teardown the metadata stream which, in return,
873 * deletes the metadata channel.
875 * In the snapshot case, the metadata stream is created and destroyed
876 * on every snapshot record. Since the channel doesn't have an owner
877 * other than the session daemon, it is safe to destroy it immediately
878 * on reception of the CLOSE_METADATA command.
880 if (!channel_monitor
) {
882 * The channel and consumer_data locks must be
883 * released before this call since consumer_del_channel
884 * re-acquires the channel and consumer_data locks to teardown
885 * the channel and queue its reclamation by the "call_rcu"
888 consumer_del_channel(channel
);
893 pthread_mutex_unlock(&channel
->lock
);
894 pthread_mutex_unlock(&consumer_data
.lock
);
900 * RCU read side lock MUST be acquired before calling this function.
902 * Return 0 on success else an LTTng error code.
904 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
907 struct lttng_consumer_channel
*metadata
;
909 DBG("UST consumer setup metadata key %" PRIu64
, key
);
911 metadata
= consumer_find_channel(key
);
913 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
914 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
919 * In no monitor mode, the metadata channel has no stream(s) so skip the
920 * ownership transfer to the metadata thread.
922 if (!metadata
->monitor
) {
923 DBG("Metadata channel in no monitor");
929 * Send metadata stream to relayd if one available. Availability is
930 * known if the stream is still in the list of the channel.
932 if (cds_list_empty(&metadata
->streams
.head
)) {
933 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
934 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
935 goto error_no_stream
;
938 /* Send metadata stream to relayd if needed. */
939 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
940 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
943 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
946 ret
= consumer_send_relayd_streams_sent(
947 metadata
->metadata_stream
->net_seq_idx
);
949 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
955 * Ownership of metadata stream is passed along. Freeing is handled by
958 ret
= send_streams_to_thread(metadata
, ctx
);
961 * If we are unable to send the stream to the thread, there is
962 * a big problem so just stop everything.
964 ret
= LTTCOMM_CONSUMERD_FATAL
;
965 goto send_streams_error
;
967 /* List MUST be empty after or else it could be reused. */
968 assert(cds_list_empty(&metadata
->streams
.head
));
975 * Delete metadata channel on error. At this point, the metadata stream can
976 * NOT be monitored by the metadata thread thus having the guarantee that
977 * the stream is still in the local stream list of the channel. This call
978 * will make sure to clean that list.
980 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
981 cds_list_del(&metadata
->metadata_stream
->send_node
);
982 metadata
->metadata_stream
= NULL
;
990 * Snapshot the whole metadata.
991 * RCU read-side lock must be held by the caller.
993 * Returns 0 on success, < 0 on error
995 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
996 uint64_t key
, char *path
, uint64_t relayd_id
,
997 struct lttng_consumer_local_data
*ctx
)
1000 struct lttng_consumer_stream
*metadata_stream
;
1005 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
1010 assert(!metadata_channel
->monitor
);
1012 health_code_update();
1015 * Ask the sessiond if we have new metadata waiting and update the
1016 * consumer metadata cache.
1018 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1023 health_code_update();
1026 * The metadata stream is NOT created in no monitor mode when the channel
1027 * is created on a sessiond ask channel command.
1029 ret
= create_ust_streams(metadata_channel
, ctx
);
1034 metadata_stream
= metadata_channel
->metadata_stream
;
1035 assert(metadata_stream
);
1037 pthread_mutex_lock(&metadata_stream
->lock
);
1038 if (relayd_id
!= (uint64_t) -1ULL) {
1039 metadata_stream
->net_seq_idx
= relayd_id
;
1040 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1042 ret
= consumer_stream_create_output_files(metadata_stream
,
1045 pthread_mutex_unlock(&metadata_stream
->lock
);
1051 health_code_update();
1053 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
);
1061 * Clean up the stream completly because the next snapshot will use a new
1064 consumer_stream_destroy(metadata_stream
, NULL
);
1065 cds_list_del(&metadata_stream
->send_node
);
1066 metadata_channel
->metadata_stream
= NULL
;
1074 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1078 unsigned long mmap_offset
;
1079 const char *mmap_base
;
1081 mmap_base
= ustctl_get_mmap_base(stream
->ustream
);
1083 ERR("Failed to get mmap base for stream `%s`",
1089 ret
= ustctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1091 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1096 *addr
= mmap_base
+ mmap_offset
;
1103 * Take a snapshot of all the stream of a channel.
1104 * RCU read-side lock and the channel lock must be held by the caller.
1106 * Returns 0 on success, < 0 on error
1108 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1109 uint64_t key
, char *path
, uint64_t relayd_id
,
1110 uint64_t nb_packets_per_stream
,
1111 struct lttng_consumer_local_data
*ctx
)
1114 unsigned use_relayd
= 0;
1115 unsigned long consumed_pos
, produced_pos
;
1116 struct lttng_consumer_stream
*stream
;
1123 if (relayd_id
!= (uint64_t) -1ULL) {
1127 assert(!channel
->monitor
);
1128 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1130 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1131 health_code_update();
1133 /* Lock stream because we are about to change its state. */
1134 pthread_mutex_lock(&stream
->lock
);
1135 assert(channel
->trace_chunk
);
1136 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1138 * Can't happen barring an internal error as the channel
1139 * holds a reference to the trace chunk.
1141 ERR("Failed to acquire reference to channel's trace chunk");
1145 assert(!stream
->trace_chunk
);
1146 stream
->trace_chunk
= channel
->trace_chunk
;
1148 stream
->net_seq_idx
= relayd_id
;
1151 ret
= consumer_send_relayd_stream(stream
, path
);
1156 ret
= consumer_stream_create_output_files(stream
,
1161 DBG("UST consumer snapshot stream (%" PRIu64
")",
1166 * If tracing is active, we want to perform a "full" buffer flush.
1167 * Else, if quiescent, it has already been done by the prior stop.
1169 if (!stream
->quiescent
) {
1170 ustctl_flush_buffer(stream
->ustream
, 0);
1173 ret
= lttng_ustconsumer_take_snapshot(stream
);
1175 ERR("Taking UST snapshot");
1179 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1181 ERR("Produced UST snapshot position");
1185 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1187 ERR("Consumerd UST snapshot position");
1192 * The original value is sent back if max stream size is larger than
1193 * the possible size of the snapshot. Also, we assume that the session
1194 * daemon should never send a maximum stream size that is lower than
1197 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1198 produced_pos
, nb_packets_per_stream
,
1199 stream
->max_sb_size
);
1201 while ((long) (consumed_pos
- produced_pos
) < 0) {
1203 unsigned long len
, padded_len
;
1204 const char *subbuf_addr
;
1205 struct lttng_buffer_view subbuf_view
;
1207 health_code_update();
1209 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1211 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1213 if (ret
!= -EAGAIN
) {
1214 PERROR("ustctl_get_subbuf snapshot");
1215 goto error_close_stream
;
1217 DBG("UST consumer get subbuf failed. Skipping it.");
1218 consumed_pos
+= stream
->max_sb_size
;
1219 stream
->chan
->lost_packets
++;
1223 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1225 ERR("Snapshot ustctl_get_subbuf_size");
1226 goto error_put_subbuf
;
1229 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1231 ERR("Snapshot ustctl_get_padded_subbuf_size");
1232 goto error_put_subbuf
;
1235 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1237 goto error_put_subbuf
;
1240 subbuf_view
= lttng_buffer_view_init(
1241 subbuf_addr
, 0, padded_len
);
1242 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
,
1243 stream
, &subbuf_view
, padded_len
- len
,
1246 if (read_len
!= len
) {
1248 goto error_put_subbuf
;
1251 if (read_len
!= padded_len
) {
1253 goto error_put_subbuf
;
1257 ret
= ustctl_put_subbuf(stream
->ustream
);
1259 ERR("Snapshot ustctl_put_subbuf");
1260 goto error_close_stream
;
1262 consumed_pos
+= stream
->max_sb_size
;
1265 /* Simply close the stream so we can use it on the next snapshot. */
1266 consumer_stream_close(stream
);
1267 pthread_mutex_unlock(&stream
->lock
);
1274 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1275 ERR("Snapshot ustctl_put_subbuf");
1278 consumer_stream_close(stream
);
1280 pthread_mutex_unlock(&stream
->lock
);
1286 * Receive the metadata updates from the sessiond. Supports receiving
1287 * overlapping metadata, but is needs to always belong to a contiguous
1288 * range starting from 0.
1289 * Be careful about the locks held when calling this function: it needs
1290 * the metadata cache flush to concurrently progress in order to
1293 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1294 uint64_t len
, uint64_t version
,
1295 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1297 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1300 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1302 metadata_str
= zmalloc(len
* sizeof(char));
1303 if (!metadata_str
) {
1304 PERROR("zmalloc metadata string");
1305 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1309 health_code_update();
1311 /* Receive metadata string. */
1312 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1314 /* Session daemon is dead so return gracefully. */
1319 health_code_update();
1321 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1322 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1325 /* Unable to handle metadata. Notify session daemon. */
1326 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1328 * Skip metadata flush on write error since the offset and len might
1329 * not have been updated which could create an infinite loop below when
1330 * waiting for the metadata cache to be flushed.
1332 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1335 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1340 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1341 DBG("Waiting for metadata to be flushed");
1343 health_code_update();
1345 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1355 * Receive command from session daemon and process it.
1357 * Return 1 on success else a negative value or 0.
1359 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1360 int sock
, struct pollfd
*consumer_sockpoll
)
1363 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1364 struct lttcomm_consumer_msg msg
;
1365 struct lttng_consumer_channel
*channel
= NULL
;
1367 health_code_update();
1369 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1370 if (ret
!= sizeof(msg
)) {
1371 DBG("Consumer received unexpected message size %zd (expects %zu)",
1374 * The ret value might 0 meaning an orderly shutdown but this is ok
1375 * since the caller handles this.
1378 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1384 health_code_update();
1387 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1389 health_code_update();
1391 /* relayd needs RCU read-side lock */
1394 switch (msg
.cmd_type
) {
1395 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1397 /* Session daemon status message are handled in the following call. */
1398 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1399 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1400 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1401 msg
.u
.relayd_sock
.relayd_session_id
);
1404 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1406 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1407 struct consumer_relayd_sock_pair
*relayd
;
1409 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1411 /* Get relayd reference if exists. */
1412 relayd
= consumer_find_relayd(index
);
1413 if (relayd
== NULL
) {
1414 DBG("Unable to find relayd %" PRIu64
, index
);
1415 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1419 * Each relayd socket pair has a refcount of stream attached to it
1420 * which tells if the relayd is still active or not depending on the
1423 * This will set the destroy flag of the relayd object and destroy it
1424 * if the refcount reaches zero when called.
1426 * The destroy can happen either here or when a stream fd hangs up.
1429 consumer_flag_relayd_for_destroy(relayd
);
1432 goto end_msg_sessiond
;
1434 case LTTNG_CONSUMER_UPDATE_STREAM
:
1439 case LTTNG_CONSUMER_DATA_PENDING
:
1441 int ret
, is_data_pending
;
1442 uint64_t id
= msg
.u
.data_pending
.session_id
;
1444 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1446 is_data_pending
= consumer_data_pending(id
);
1448 /* Send back returned value to session daemon */
1449 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1450 sizeof(is_data_pending
));
1452 DBG("Error when sending the data pending ret code: %d", ret
);
1457 * No need to send back a status message since the data pending
1458 * returned value is the response.
1462 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1465 struct ustctl_consumer_channel_attr attr
;
1466 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1467 const struct lttng_credentials buffer_credentials
= {
1468 .uid
= msg
.u
.ask_channel
.buffer_credentials
.uid
,
1469 .gid
= msg
.u
.ask_channel
.buffer_credentials
.gid
,
1472 /* Create a plain object and reserve a channel key. */
1473 channel
= consumer_allocate_channel(
1474 msg
.u
.ask_channel
.key
,
1475 msg
.u
.ask_channel
.session_id
,
1476 msg
.u
.ask_channel
.chunk_id
.is_set
?
1478 msg
.u
.ask_channel
.pathname
,
1479 msg
.u
.ask_channel
.name
,
1480 msg
.u
.ask_channel
.relayd_id
,
1481 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1482 msg
.u
.ask_channel
.tracefile_size
,
1483 msg
.u
.ask_channel
.tracefile_count
,
1484 msg
.u
.ask_channel
.session_id_per_pid
,
1485 msg
.u
.ask_channel
.monitor
,
1486 msg
.u
.ask_channel
.live_timer_interval
,
1487 msg
.u
.ask_channel
.is_live
,
1488 msg
.u
.ask_channel
.root_shm_path
,
1489 msg
.u
.ask_channel
.shm_path
);
1491 goto end_channel_error
;
1494 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1495 buffer_credentials
);
1498 * Assign UST application UID to the channel. This value is ignored for
1499 * per PID buffers. This is specific to UST thus setting this after the
1502 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1504 /* Build channel attributes from received message. */
1505 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1506 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1507 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1508 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1509 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1510 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1511 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1512 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1514 /* Match channel buffer type to the UST abi. */
1515 switch (msg
.u
.ask_channel
.output
) {
1516 case LTTNG_EVENT_MMAP
:
1518 attr
.output
= LTTNG_UST_MMAP
;
1522 /* Translate and save channel type. */
1523 switch (msg
.u
.ask_channel
.type
) {
1524 case LTTNG_UST_CHAN_PER_CPU
:
1525 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1526 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1528 * Set refcount to 1 for owner. Below, we will
1529 * pass ownership to the
1530 * consumer_thread_channel_poll() thread.
1532 channel
->refcount
= 1;
1534 case LTTNG_UST_CHAN_METADATA
:
1535 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1536 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1543 health_code_update();
1545 ret
= ask_channel(ctx
, channel
, &attr
);
1547 goto end_channel_error
;
1550 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1551 ret
= consumer_metadata_cache_allocate(channel
);
1553 ERR("Allocating metadata cache");
1554 goto end_channel_error
;
1556 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1557 attr
.switch_timer_interval
= 0;
1559 int monitor_start_ret
;
1561 consumer_timer_live_start(channel
,
1562 msg
.u
.ask_channel
.live_timer_interval
);
1563 monitor_start_ret
= consumer_timer_monitor_start(
1565 msg
.u
.ask_channel
.monitor_timer_interval
);
1566 if (monitor_start_ret
< 0) {
1567 ERR("Starting channel monitoring timer failed");
1568 goto end_channel_error
;
1572 health_code_update();
1575 * Add the channel to the internal state AFTER all streams were created
1576 * and successfully sent to session daemon. This way, all streams must
1577 * be ready before this channel is visible to the threads.
1578 * If add_channel succeeds, ownership of the channel is
1579 * passed to consumer_thread_channel_poll().
1581 ret
= add_channel(channel
, ctx
);
1583 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1584 if (channel
->switch_timer_enabled
== 1) {
1585 consumer_timer_switch_stop(channel
);
1587 consumer_metadata_cache_destroy(channel
);
1589 if (channel
->live_timer_enabled
== 1) {
1590 consumer_timer_live_stop(channel
);
1592 if (channel
->monitor_timer_enabled
== 1) {
1593 consumer_timer_monitor_stop(channel
);
1595 goto end_channel_error
;
1598 health_code_update();
1601 * Channel and streams are now created. Inform the session daemon that
1602 * everything went well and should wait to receive the channel and
1603 * streams with ustctl API.
1605 ret
= consumer_send_status_channel(sock
, channel
);
1608 * There is probably a problem on the socket.
1615 case LTTNG_CONSUMER_GET_CHANNEL
:
1617 int ret
, relayd_err
= 0;
1618 uint64_t key
= msg
.u
.get_channel
.key
;
1619 struct lttng_consumer_channel
*channel
;
1621 channel
= consumer_find_channel(key
);
1623 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1624 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1625 goto end_get_channel
;
1628 health_code_update();
1630 /* Send the channel to sessiond (and relayd, if applicable). */
1631 ret
= send_channel_to_sessiond_and_relayd(sock
, channel
, ctx
,
1636 * We were unable to send to the relayd the stream so avoid
1637 * sending back a fatal error to the thread since this is OK
1638 * and the consumer can continue its work. The above call
1639 * has sent the error status message to the sessiond.
1641 goto end_get_channel_nosignal
;
1644 * The communicaton was broken hence there is a bad state between
1645 * the consumer and sessiond so stop everything.
1647 goto error_get_channel_fatal
;
1650 health_code_update();
1653 * In no monitor mode, the streams ownership is kept inside the channel
1654 * so don't send them to the data thread.
1656 if (!channel
->monitor
) {
1657 goto end_get_channel
;
1660 ret
= send_streams_to_thread(channel
, ctx
);
1663 * If we are unable to send the stream to the thread, there is
1664 * a big problem so just stop everything.
1666 goto error_get_channel_fatal
;
1668 /* List MUST be empty after or else it could be reused. */
1669 assert(cds_list_empty(&channel
->streams
.head
));
1671 goto end_msg_sessiond
;
1672 error_get_channel_fatal
:
1674 end_get_channel_nosignal
:
1677 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1679 uint64_t key
= msg
.u
.destroy_channel
.key
;
1682 * Only called if streams have not been sent to stream
1683 * manager thread. However, channel has been sent to
1684 * channel manager thread.
1686 notify_thread_del_channel(ctx
, key
);
1687 goto end_msg_sessiond
;
1689 case LTTNG_CONSUMER_CLOSE_METADATA
:
1693 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1698 goto end_msg_sessiond
;
1700 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1704 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1709 goto end_msg_sessiond
;
1711 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1715 ret
= clear_quiescent_channel(
1716 msg
.u
.clear_quiescent_channel
.key
);
1721 goto end_msg_sessiond
;
1723 case LTTNG_CONSUMER_PUSH_METADATA
:
1726 uint64_t len
= msg
.u
.push_metadata
.len
;
1727 uint64_t key
= msg
.u
.push_metadata
.key
;
1728 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1729 uint64_t version
= msg
.u
.push_metadata
.version
;
1730 struct lttng_consumer_channel
*channel
;
1732 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1735 channel
= consumer_find_channel(key
);
1738 * This is possible if the metadata creation on the consumer side
1739 * is in flight vis-a-vis a concurrent push metadata from the
1740 * session daemon. Simply return that the channel failed and the
1741 * session daemon will handle that message correctly considering
1742 * that this race is acceptable thus the DBG() statement here.
1744 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1745 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1746 goto end_push_metadata_msg_sessiond
;
1749 health_code_update();
1753 * There is nothing to receive. We have simply
1754 * checked whether the channel can be found.
1756 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1757 goto end_push_metadata_msg_sessiond
;
1760 /* Tell session daemon we are ready to receive the metadata. */
1761 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1763 /* Somehow, the session daemon is not responding anymore. */
1764 goto error_push_metadata_fatal
;
1767 health_code_update();
1769 /* Wait for more data. */
1770 health_poll_entry();
1771 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1774 goto error_push_metadata_fatal
;
1777 health_code_update();
1779 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1780 len
, version
, channel
, 0, 1);
1782 /* error receiving from sessiond */
1783 goto error_push_metadata_fatal
;
1786 goto end_push_metadata_msg_sessiond
;
1788 end_push_metadata_msg_sessiond
:
1789 goto end_msg_sessiond
;
1790 error_push_metadata_fatal
:
1793 case LTTNG_CONSUMER_SETUP_METADATA
:
1797 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1801 goto end_msg_sessiond
;
1803 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1805 struct lttng_consumer_channel
*channel
;
1806 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1808 channel
= consumer_find_channel(key
);
1810 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1811 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1813 if (msg
.u
.snapshot_channel
.metadata
) {
1814 ret
= snapshot_metadata(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_SNAPSHOT_FAILED
;
1823 ret
= snapshot_channel(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_SNAPSHOT_FAILED
;
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");
1945 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1947 int channel_monitor_pipe
;
1949 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1950 /* Successfully received the command's type. */
1951 ret
= consumer_send_status_msg(sock
, ret_code
);
1956 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1958 if (ret
!= sizeof(channel_monitor_pipe
)) {
1959 ERR("Failed to receive channel monitor pipe");
1963 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1964 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1965 channel_monitor_pipe
);
1969 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1970 /* Set the pipe as non-blocking. */
1971 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1973 PERROR("fcntl get flags of the channel monitoring pipe");
1978 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1979 flags
| O_NONBLOCK
);
1981 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1984 DBG("Channel monitor pipe set as non-blocking");
1986 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1988 goto end_msg_sessiond
;
1990 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1992 struct lttng_consumer_channel
*channel
;
1993 uint64_t key
= msg
.u
.rotate_channel
.key
;
1995 channel
= consumer_find_channel(key
);
1997 DBG("Channel %" PRIu64
" not found", key
);
1998 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2001 * Sample the rotate position of all the streams in
2004 ret
= lttng_consumer_rotate_channel(channel
, key
,
2005 msg
.u
.rotate_channel
.relayd_id
,
2006 msg
.u
.rotate_channel
.metadata
,
2009 ERR("Rotate channel failed");
2010 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
2013 health_code_update();
2015 ret
= consumer_send_status_msg(sock
, ret_code
);
2017 /* Somehow, the session daemon is not responding anymore. */
2018 goto end_rotate_channel_nosignal
;
2022 * Rotate the streams that are ready right now.
2023 * FIXME: this is a second consecutive iteration over the
2024 * streams in a channel, there is probably a better way to
2025 * handle this, but it needs to be after the
2026 * consumer_send_status_msg() call.
2029 ret
= lttng_consumer_rotate_ready_streams(
2032 ERR("Rotate channel failed");
2036 end_rotate_channel_nosignal
:
2039 case LTTNG_CONSUMER_INIT
:
2041 ret_code
= lttng_consumer_init_command(ctx
,
2042 msg
.u
.init
.sessiond_uuid
);
2043 health_code_update();
2044 ret
= consumer_send_status_msg(sock
, ret_code
);
2046 /* Somehow, the session daemon is not responding anymore. */
2051 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2053 const struct lttng_credentials credentials
= {
2054 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
2055 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
2057 const bool is_local_trace
=
2058 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2059 const uint64_t relayd_id
=
2060 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2061 const char *chunk_override_name
=
2062 *msg
.u
.create_trace_chunk
.override_name
?
2063 msg
.u
.create_trace_chunk
.override_name
:
2065 LTTNG_OPTIONAL(struct lttng_directory_handle
) chunk_directory_handle
=
2066 LTTNG_OPTIONAL_INIT
;
2069 * The session daemon will only provide a chunk directory file
2070 * descriptor for local traces.
2072 if (is_local_trace
) {
2075 /* Acnowledge the reception of the command. */
2076 ret
= consumer_send_status_msg(sock
,
2077 LTTCOMM_CONSUMERD_SUCCESS
);
2079 /* Somehow, the session daemon is not responding anymore. */
2083 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
2084 if (ret
!= sizeof(chunk_dirfd
)) {
2085 ERR("Failed to receive trace chunk directory file descriptor");
2089 DBG("Received trace chunk directory fd (%d)",
2091 ret
= lttng_directory_handle_init_from_dirfd(
2092 &chunk_directory_handle
.value
,
2095 ERR("Failed to initialize chunk directory handle from directory file descriptor");
2096 if (close(chunk_dirfd
)) {
2097 PERROR("Failed to close chunk directory file descriptor");
2101 chunk_directory_handle
.is_set
= true;
2104 ret_code
= lttng_consumer_create_trace_chunk(
2105 !is_local_trace
? &relayd_id
: NULL
,
2106 msg
.u
.create_trace_chunk
.session_id
,
2107 msg
.u
.create_trace_chunk
.chunk_id
,
2108 (time_t) msg
.u
.create_trace_chunk
2109 .creation_timestamp
,
2110 chunk_override_name
,
2111 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2114 chunk_directory_handle
.is_set
?
2115 &chunk_directory_handle
.value
:
2118 if (chunk_directory_handle
.is_set
) {
2119 lttng_directory_handle_fini(
2120 &chunk_directory_handle
.value
);
2122 goto end_msg_sessiond
;
2124 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2126 enum lttng_trace_chunk_command_type close_command
=
2127 msg
.u
.close_trace_chunk
.close_command
.value
;
2128 const uint64_t relayd_id
=
2129 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2130 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2131 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2134 ret_code
= lttng_consumer_close_trace_chunk(
2135 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2138 msg
.u
.close_trace_chunk
.session_id
,
2139 msg
.u
.close_trace_chunk
.chunk_id
,
2140 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2141 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2143 NULL
, closed_trace_chunk_path
);
2144 reply
.ret_code
= ret_code
;
2145 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2146 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2147 if (ret
!= sizeof(reply
)) {
2150 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2152 if (ret
!= reply
.path_length
) {
2157 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2159 const uint64_t relayd_id
=
2160 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2162 ret_code
= lttng_consumer_trace_chunk_exists(
2163 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2165 msg
.u
.trace_chunk_exists
.session_id
,
2166 msg
.u
.trace_chunk_exists
.chunk_id
);
2167 goto end_msg_sessiond
;
2175 * Return 1 to indicate success since the 0 value can be a socket
2176 * shutdown during the recv() or send() call.
2183 * The returned value here is not useful since either way we'll return 1 to
2184 * the caller because the session daemon socket management is done
2185 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2187 ret
= consumer_send_status_msg(sock
, ret_code
);
2197 * Free channel here since no one has a reference to it. We don't
2198 * free after that because a stream can store this pointer.
2200 destroy_channel(channel
);
2202 /* We have to send a status channel message indicating an error. */
2203 ret
= consumer_send_status_channel(sock
, NULL
);
2205 /* Stop everything if session daemon can not be notified. */
2212 /* This will issue a consumer stop. */
2218 health_code_update();
2222 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
2223 int producer_active
)
2226 assert(stream
->ustream
);
2228 ustctl_flush_buffer(stream
->ustream
, producer_active
);
2232 * Take a snapshot for a specific stream.
2234 * Returns 0 on success, < 0 on error
2236 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2239 assert(stream
->ustream
);
2241 return ustctl_snapshot(stream
->ustream
);
2245 * Sample consumed and produced positions for a specific stream.
2247 * Returns 0 on success, < 0 on error.
2249 int lttng_ustconsumer_sample_snapshot_positions(
2250 struct lttng_consumer_stream
*stream
)
2253 assert(stream
->ustream
);
2255 return ustctl_snapshot_sample_positions(stream
->ustream
);
2259 * Get the produced position
2261 * Returns 0 on success, < 0 on error
2263 int lttng_ustconsumer_get_produced_snapshot(
2264 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2267 assert(stream
->ustream
);
2270 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2274 * Get the consumed position
2276 * Returns 0 on success, < 0 on error
2278 int lttng_ustconsumer_get_consumed_snapshot(
2279 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2282 assert(stream
->ustream
);
2285 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2288 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2292 assert(stream
->ustream
);
2294 ustctl_flush_buffer(stream
->ustream
, producer
);
2297 int lttng_ustconsumer_get_current_timestamp(
2298 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2301 assert(stream
->ustream
);
2304 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2307 int lttng_ustconsumer_get_sequence_number(
2308 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2311 assert(stream
->ustream
);
2314 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2318 * Called when the stream signals the consumer that it has hung up.
2320 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2323 assert(stream
->ustream
);
2325 pthread_mutex_lock(&stream
->lock
);
2326 if (!stream
->quiescent
) {
2327 ustctl_flush_buffer(stream
->ustream
, 0);
2328 stream
->quiescent
= true;
2330 pthread_mutex_unlock(&stream
->lock
);
2331 stream
->hangup_flush_done
= 1;
2334 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2339 assert(chan
->uchan
);
2340 assert(chan
->buffer_credentials
.is_set
);
2342 if (chan
->switch_timer_enabled
== 1) {
2343 consumer_timer_switch_stop(chan
);
2345 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2348 ret
= close(chan
->stream_fds
[i
]);
2352 if (chan
->shm_path
[0]) {
2353 char shm_path
[PATH_MAX
];
2355 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2357 ERR("Cannot get stream shm path");
2359 ret
= run_as_unlink(shm_path
,
2360 chan
->buffer_credentials
.value
.uid
,
2361 chan
->buffer_credentials
.value
.gid
);
2363 PERROR("unlink %s", shm_path
);
2369 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2372 assert(chan
->uchan
);
2373 assert(chan
->buffer_credentials
.is_set
);
2375 consumer_metadata_cache_destroy(chan
);
2376 ustctl_destroy_channel(chan
->uchan
);
2377 /* Try to rmdir all directories under shm_path root. */
2378 if (chan
->root_shm_path
[0]) {
2379 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2380 chan
->buffer_credentials
.value
.uid
,
2381 chan
->buffer_credentials
.value
.gid
,
2382 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2384 free(chan
->stream_fds
);
2387 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2390 assert(stream
->ustream
);
2392 if (stream
->chan
->switch_timer_enabled
== 1) {
2393 consumer_timer_switch_stop(stream
->chan
);
2395 ustctl_destroy_stream(stream
->ustream
);
2398 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2401 assert(stream
->ustream
);
2403 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2406 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2409 assert(stream
->ustream
);
2411 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2415 * Populate index values of a UST stream. Values are set in big endian order.
2417 * Return 0 on success or else a negative value.
2419 static int get_index_values(struct ctf_packet_index
*index
,
2420 struct ustctl_consumer_stream
*ustream
)
2423 uint64_t packet_size
, content_size
, timestamp_begin
, timestamp_end
,
2424 events_discarded
, stream_id
, stream_instance_id
,
2427 ret
= ustctl_get_timestamp_begin(ustream
, ×tamp_begin
);
2429 PERROR("ustctl_get_timestamp_begin");
2433 ret
= ustctl_get_timestamp_end(ustream
, ×tamp_end
);
2435 PERROR("ustctl_get_timestamp_end");
2439 ret
= ustctl_get_events_discarded(ustream
, &events_discarded
);
2441 PERROR("ustctl_get_events_discarded");
2445 ret
= ustctl_get_content_size(ustream
, &content_size
);
2447 PERROR("ustctl_get_content_size");
2451 ret
= ustctl_get_packet_size(ustream
, &packet_size
);
2453 PERROR("ustctl_get_packet_size");
2457 ret
= ustctl_get_stream_id(ustream
, &stream_id
);
2459 PERROR("ustctl_get_stream_id");
2463 ret
= ustctl_get_instance_id(ustream
, &stream_instance_id
);
2465 PERROR("ustctl_get_instance_id");
2469 ret
= ustctl_get_sequence_number(ustream
, &packet_seq_num
);
2471 PERROR("ustctl_get_sequence_number");
2475 *index
= (typeof(*index
)) {
2476 .offset
= index
->offset
,
2477 .packet_size
= htobe64(packet_size
),
2478 .content_size
= htobe64(content_size
),
2479 .timestamp_begin
= htobe64(timestamp_begin
),
2480 .timestamp_end
= htobe64(timestamp_end
),
2481 .events_discarded
= htobe64(events_discarded
),
2482 .stream_id
= htobe64(stream_id
),
2483 .stream_instance_id
= htobe64(stream_instance_id
),
2484 .packet_seq_num
= htobe64(packet_seq_num
),
2492 void metadata_stream_reset_cache(struct lttng_consumer_stream
*stream
,
2493 struct consumer_metadata_cache
*cache
)
2495 DBG("Metadata stream update to version %" PRIu64
,
2497 stream
->ust_metadata_pushed
= 0;
2498 stream
->metadata_version
= cache
->version
;
2499 stream
->reset_metadata_flag
= 1;
2503 * Check if the version of the metadata stream and metadata cache match.
2504 * If the cache got updated, reset the metadata stream.
2505 * The stream lock and metadata cache lock MUST be held.
2506 * Return 0 on success, a negative value on error.
2509 int metadata_stream_check_version(struct lttng_consumer_stream
*stream
)
2512 struct consumer_metadata_cache
*cache
= stream
->chan
->metadata_cache
;
2514 if (cache
->version
== stream
->metadata_version
) {
2517 metadata_stream_reset_cache(stream
, cache
);
2524 * Write up to one packet from the metadata cache to the channel.
2526 * Returns the number of bytes pushed in the cache, or a negative value
2530 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2535 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2536 ret
= metadata_stream_check_version(stream
);
2540 if (stream
->chan
->metadata_cache
->max_offset
2541 == stream
->ust_metadata_pushed
) {
2546 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2547 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2548 stream
->chan
->metadata_cache
->max_offset
2549 - stream
->ust_metadata_pushed
);
2550 assert(write_len
!= 0);
2551 if (write_len
< 0) {
2552 ERR("Writing one metadata packet");
2556 stream
->ust_metadata_pushed
+= write_len
;
2558 assert(stream
->chan
->metadata_cache
->max_offset
>=
2559 stream
->ust_metadata_pushed
);
2563 * Switch packet (but don't open the next one) on every commit of
2564 * a metadata packet. Since the subbuffer is fully filled (with padding,
2565 * if needed), the stream is "quiescent" after this commit.
2567 ustctl_flush_buffer(stream
->ustream
, 1);
2568 stream
->quiescent
= true;
2570 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2576 * Sync metadata meaning request them to the session daemon and snapshot to the
2577 * metadata thread can consumer them.
2579 * Metadata stream lock is held here, but we need to release it when
2580 * interacting with sessiond, else we cause a deadlock with live
2581 * awaiting on metadata to be pushed out.
2583 * The RCU read side lock must be held by the caller.
2585 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
2586 * is empty or a negative value on error.
2588 int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2589 struct lttng_consumer_stream
*metadata_stream
)
2593 struct lttng_consumer_channel
*metadata_channel
;
2596 assert(metadata_stream
);
2598 metadata_channel
= metadata_stream
->chan
;
2599 pthread_mutex_unlock(&metadata_stream
->lock
);
2601 * Request metadata from the sessiond, but don't wait for the flush
2602 * because we locked the metadata thread.
2604 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2605 pthread_mutex_lock(&metadata_stream
->lock
);
2611 * The metadata stream and channel can be deleted while the
2612 * metadata stream lock was released. The streamed is checked
2613 * for deletion before we use it further.
2615 * Note that it is safe to access a logically-deleted stream since its
2616 * existence is still guaranteed by the RCU read side lock. However,
2617 * it should no longer be used. The close/deletion of the metadata
2618 * channel and stream already guarantees that all metadata has been
2619 * consumed. Therefore, there is nothing left to do in this function.
2621 if (consumer_stream_is_deleted(metadata_stream
)) {
2622 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2623 metadata_stream
->key
);
2628 ret
= commit_one_metadata_packet(metadata_stream
);
2631 } else if (ret
> 0) {
2635 ret
= ustctl_snapshot(metadata_stream
->ustream
);
2637 if (errno
!= EAGAIN
) {
2638 ERR("Sync metadata, taking UST snapshot");
2641 DBG("No new metadata when syncing them.");
2642 /* No new metadata, exit. */
2648 * After this flush, we still need to extract metadata.
2659 * Return 0 on success else a negative value.
2661 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2662 struct lttng_consumer_local_data
*ctx
)
2665 struct ustctl_consumer_stream
*ustream
;
2670 ustream
= stream
->ustream
;
2673 * First, we are going to check if there is a new subbuffer available
2674 * before reading the stream wait_fd.
2676 /* Get the next subbuffer */
2677 ret
= ustctl_get_next_subbuf(ustream
);
2679 /* No more data found, flag the stream. */
2680 stream
->has_data
= 0;
2685 ret
= ustctl_put_subbuf(ustream
);
2688 /* This stream still has data. Flag it and wake up the data thread. */
2689 stream
->has_data
= 1;
2691 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2694 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2695 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2700 /* The wake up pipe has been notified. */
2701 ctx
->has_wakeup
= 1;
2710 int update_stream_stats(struct lttng_consumer_stream
*stream
)
2713 uint64_t seq
, discarded
;
2715 ret
= ustctl_get_sequence_number(stream
->ustream
, &seq
);
2717 PERROR("ustctl_get_sequence_number");
2721 * Start the sequence when we extract the first packet in case we don't
2722 * start at 0 (for example if a consumer is not connected to the
2723 * session immediately after the beginning).
2725 if (stream
->last_sequence_number
== -1ULL) {
2726 stream
->last_sequence_number
= seq
;
2727 } else if (seq
> stream
->last_sequence_number
) {
2728 stream
->chan
->lost_packets
+= seq
-
2729 stream
->last_sequence_number
- 1;
2731 /* seq <= last_sequence_number */
2732 ERR("Sequence number inconsistent : prev = %" PRIu64
2733 ", current = %" PRIu64
,
2734 stream
->last_sequence_number
, seq
);
2738 stream
->last_sequence_number
= seq
;
2740 ret
= ustctl_get_events_discarded(stream
->ustream
, &discarded
);
2742 PERROR("kernctl_get_events_discarded");
2745 if (discarded
< stream
->last_discarded_events
) {
2747 * Overflow has occurred. We assume only one wrap-around
2750 stream
->chan
->discarded_events
+=
2751 (1ULL << (CAA_BITS_PER_LONG
- 1)) -
2752 stream
->last_discarded_events
+ discarded
;
2754 stream
->chan
->discarded_events
+= discarded
-
2755 stream
->last_discarded_events
;
2757 stream
->last_discarded_events
= discarded
;
2765 * Read subbuffer from the given stream.
2767 * Stream and channel locks MUST be acquired by the caller.
2769 * Return 0 on success else a negative value.
2771 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2772 struct lttng_consumer_local_data
*ctx
)
2774 unsigned long len
, subbuf_size
, padding
;
2775 int err
, write_index
= 1;
2777 struct ustctl_consumer_stream
*ustream
;
2778 struct ctf_packet_index index
;
2779 const char *subbuf_addr
;
2780 struct lttng_buffer_view subbuf_view
;
2783 assert(stream
->ustream
);
2786 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream
->wait_fd
,
2789 /* Ease our life for what's next. */
2790 ustream
= stream
->ustream
;
2793 * We can consume the 1 byte written into the wait_fd by UST. Don't trigger
2794 * error if we cannot read this one byte (read returns 0), or if the error
2795 * is EAGAIN or EWOULDBLOCK.
2797 * This is only done when the stream is monitored by a thread, before the
2798 * flush is done after a hangup and if the stream is not flagged with data
2799 * since there might be nothing to consume in the wait fd but still have
2800 * data available flagged by the consumer wake up pipe.
2802 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2806 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2807 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2814 /* Get the next subbuffer */
2815 err
= ustctl_get_next_subbuf(ustream
);
2818 * Populate metadata info if the existing info has
2819 * already been read.
2821 if (stream
->metadata_flag
) {
2822 ret
= commit_one_metadata_packet(stream
);
2829 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
2831 * This is a debug message even for single-threaded consumer,
2832 * because poll() have more relaxed criterions than get subbuf,
2833 * so get_subbuf may fail for short race windows where poll()
2834 * would issue wakeups.
2836 DBG("Reserving sub buffer failed (everything is normal, "
2837 "it is due to concurrency) [ret: %d]", err
);
2840 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
2842 if (!stream
->metadata_flag
) {
2843 index
.offset
= htobe64(stream
->out_fd_offset
);
2844 ret
= get_index_values(&index
, ustream
);
2846 err
= ustctl_put_subbuf(ustream
);
2851 /* Update the stream's sequence and discarded events count. */
2852 ret
= update_stream_stats(stream
);
2854 PERROR("kernctl_get_events_discarded");
2855 err
= ustctl_put_subbuf(ustream
);
2863 /* Get the full padded subbuffer size */
2864 err
= ustctl_get_padded_subbuf_size(ustream
, &len
);
2867 /* Get subbuffer data size (without padding) */
2868 err
= ustctl_get_subbuf_size(ustream
, &subbuf_size
);
2871 /* Make sure we don't get a subbuffer size bigger than the padded */
2872 assert(len
>= subbuf_size
);
2874 padding
= len
- subbuf_size
;
2876 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
2879 goto error_put_subbuf
;
2882 subbuf_view
= lttng_buffer_view_init(subbuf_addr
, 0, len
);
2884 /* write the subbuffer to the tracefile */
2885 ret
= lttng_consumer_on_read_subbuffer_mmap(
2886 ctx
, stream
, &subbuf_view
, padding
, &index
);
2888 * The mmap operation should write subbuf_size amount of data when
2889 * network streaming or the full padding (len) size when we are _not_
2892 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
2893 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
2895 * Display the error but continue processing to try to release the
2896 * subbuffer. This is a DBG statement since any unexpected kill or
2897 * signal, the application gets unregistered, relayd gets closed or
2898 * anything that affects the buffer lifetime will trigger this error.
2899 * So, for the sake of the user, don't print this error since it can
2900 * happen and it is OK with the code flow.
2902 DBG("Error writing to tracefile "
2903 "(ret: %ld != len: %lu != subbuf_size: %lu)",
2904 ret
, len
, subbuf_size
);
2908 err
= ustctl_put_next_subbuf(ustream
);
2912 * This will consumer the byte on the wait_fd if and only if there is not
2913 * next subbuffer to be acquired.
2915 if (!stream
->metadata_flag
) {
2916 ret
= notify_if_more_data(stream
, ctx
);
2922 /* Write index if needed. */
2927 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
2929 * In live, block until all the metadata is sent.
2931 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2932 assert(!stream
->missed_metadata_flush
);
2933 stream
->waiting_on_metadata
= true;
2934 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2936 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
2938 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2939 stream
->waiting_on_metadata
= false;
2940 if (stream
->missed_metadata_flush
) {
2941 stream
->missed_metadata_flush
= false;
2942 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2943 (void) consumer_flush_ust_index(stream
);
2945 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2953 assert(!stream
->metadata_flag
);
2954 err
= consumer_stream_write_index(stream
, &index
);
2965 * Called when a stream is created.
2967 * Return 0 on success or else a negative value.
2969 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2976 * Don't create anything if this is set for streaming or if there is
2977 * no current trace chunk on the parent channel.
2979 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
2980 stream
->chan
->trace_chunk
) {
2981 ret
= consumer_stream_create_output_files(stream
, true);
2993 * Check if data is still being extracted from the buffers for a specific
2994 * stream. Consumer data lock MUST be acquired before calling this function
2995 * and the stream lock.
2997 * Return 1 if the traced data are still getting read else 0 meaning that the
2998 * data is available for trace viewer reading.
3000 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3005 assert(stream
->ustream
);
3007 DBG("UST consumer checking data pending");
3009 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3014 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3015 uint64_t contiguous
, pushed
;
3017 /* Ease our life a bit. */
3018 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
3019 pushed
= stream
->ust_metadata_pushed
;
3022 * We can simply check whether all contiguously available data
3023 * has been pushed to the ring buffer, since the push operation
3024 * is performed within get_next_subbuf(), and because both
3025 * get_next_subbuf() and put_next_subbuf() are issued atomically
3026 * thanks to the stream lock within
3027 * lttng_ustconsumer_read_subbuffer(). This basically means that
3028 * whetnever ust_metadata_pushed is incremented, the associated
3029 * metadata has been consumed from the metadata stream.
3031 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3032 contiguous
, pushed
);
3033 assert(((int64_t) (contiguous
- pushed
)) >= 0);
3034 if ((contiguous
!= pushed
) ||
3035 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3036 ret
= 1; /* Data is pending */
3040 ret
= ustctl_get_next_subbuf(stream
->ustream
);
3043 * There is still data so let's put back this
3046 ret
= ustctl_put_subbuf(stream
->ustream
);
3048 ret
= 1; /* Data is pending */
3053 /* Data is NOT pending so ready to be read. */
3061 * Stop a given metadata channel timer if enabled and close the wait fd which
3062 * is the poll pipe of the metadata stream.
3064 * This MUST be called with the metadata channel lock acquired.
3066 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3071 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3073 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3075 if (metadata
->switch_timer_enabled
== 1) {
3076 consumer_timer_switch_stop(metadata
);
3079 if (!metadata
->metadata_stream
) {
3084 * Closing write side so the thread monitoring the stream wakes up if any
3085 * and clean the metadata stream.
3087 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3088 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3090 PERROR("closing metadata pipe write side");
3092 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3100 * Close every metadata stream wait fd of the metadata hash table. This
3101 * function MUST be used very carefully so not to run into a race between the
3102 * metadata thread handling streams and this function closing their wait fd.
3104 * For UST, this is used when the session daemon hangs up. Its the metadata
3105 * producer so calling this is safe because we are assured that no state change
3106 * can occur in the metadata thread for the streams in the hash table.
3108 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3110 struct lttng_ht_iter iter
;
3111 struct lttng_consumer_stream
*stream
;
3113 assert(metadata_ht
);
3114 assert(metadata_ht
->ht
);
3116 DBG("UST consumer closing all metadata streams");
3119 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
3122 health_code_update();
3124 pthread_mutex_lock(&stream
->chan
->lock
);
3125 lttng_ustconsumer_close_metadata(stream
->chan
);
3126 pthread_mutex_unlock(&stream
->chan
->lock
);
3132 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3136 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
3138 ERR("Unable to close wakeup fd");
3143 * Please refer to consumer-timer.c before adding any lock within this
3144 * function or any of its callees. Timers have a very strict locking
3145 * semantic with respect to teardown. Failure to respect this semantic
3146 * introduces deadlocks.
3148 * DON'T hold the metadata lock when calling this function, else this
3149 * can cause deadlock involving consumer awaiting for metadata to be
3150 * pushed out due to concurrent interaction with the session daemon.
3152 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3153 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
3155 struct lttcomm_metadata_request_msg request
;
3156 struct lttcomm_consumer_msg msg
;
3157 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3158 uint64_t len
, key
, offset
, version
;
3162 assert(channel
->metadata_cache
);
3164 memset(&request
, 0, sizeof(request
));
3166 /* send the metadata request to sessiond */
3167 switch (consumer_data
.type
) {
3168 case LTTNG_CONSUMER64_UST
:
3169 request
.bits_per_long
= 64;
3171 case LTTNG_CONSUMER32_UST
:
3172 request
.bits_per_long
= 32;
3175 request
.bits_per_long
= 0;
3179 request
.session_id
= channel
->session_id
;
3180 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3182 * Request the application UID here so the metadata of that application can
3183 * be sent back. The channel UID corresponds to the user UID of the session
3184 * used for the rights on the stream file(s).
3186 request
.uid
= channel
->ust_app_uid
;
3187 request
.key
= channel
->key
;
3189 DBG("Sending metadata request to sessiond, session id %" PRIu64
3190 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
3191 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
3194 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3196 health_code_update();
3198 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
3201 ERR("Asking metadata to sessiond");
3205 health_code_update();
3207 /* Receive the metadata from sessiond */
3208 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
3210 if (ret
!= sizeof(msg
)) {
3211 DBG("Consumer received unexpected message size %d (expects %zu)",
3213 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3215 * The ret value might 0 meaning an orderly shutdown but this is ok
3216 * since the caller handles this.
3221 health_code_update();
3223 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3224 /* No registry found */
3225 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3229 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3230 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3235 len
= msg
.u
.push_metadata
.len
;
3236 key
= msg
.u
.push_metadata
.key
;
3237 offset
= msg
.u
.push_metadata
.target_offset
;
3238 version
= msg
.u
.push_metadata
.version
;
3240 assert(key
== channel
->key
);
3242 DBG("No new metadata to receive for key %" PRIu64
, key
);
3245 health_code_update();
3247 /* Tell session daemon we are ready to receive the metadata. */
3248 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3249 LTTCOMM_CONSUMERD_SUCCESS
);
3250 if (ret
< 0 || len
== 0) {
3252 * Somehow, the session daemon is not responding anymore or there is
3253 * nothing to receive.
3258 health_code_update();
3260 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3261 key
, offset
, len
, version
, channel
, timer
, wait
);
3264 * Only send the status msg if the sessiond is alive meaning a positive
3267 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3272 health_code_update();
3274 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3279 * Return the ustctl call for the get stream id.
3281 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3282 uint64_t *stream_id
)
3287 return ustctl_get_stream_id(stream
->ustream
, stream_id
);