2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <lttng/ust-ctl.h>
28 #include <sys/socket.h>
30 #include <sys/types.h>
33 #include <urcu/list.h>
37 #include <bin/lttng-consumerd/health-consumerd.h>
38 #include <common/common.h>
39 #include <common/sessiond-comm/sessiond-comm.h>
40 #include <common/relayd/relayd.h>
41 #include <common/compat/fcntl.h>
42 #include <common/compat/endian.h>
43 #include <common/consumer/consumer-metadata-cache.h>
44 #include <common/consumer/consumer-stream.h>
45 #include <common/consumer/consumer-timer.h>
46 #include <common/utils.h>
47 #include <common/index/index.h>
49 #include "ust-consumer.h"
51 #define INT_MAX_STR_LEN 12 /* includes \0 */
53 extern struct lttng_consumer_global_data consumer_data
;
54 extern int consumer_poll_timeout
;
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
);
76 lttng_trace_chunk_put(stream
->trace_chunk
);
81 * If a channel is available meaning that was created before the streams
85 lttng_ustconsumer_del_channel(channel
);
86 lttng_ustconsumer_free_channel(channel
);
92 * Add channel to internal consumer state.
94 * Returns 0 on success or else a negative value.
96 static int add_channel(struct lttng_consumer_channel
*channel
,
97 struct lttng_consumer_local_data
*ctx
)
104 if (ctx
->on_recv_channel
!= NULL
) {
105 ret
= ctx
->on_recv_channel(channel
);
107 ret
= consumer_add_channel(channel
, ctx
);
108 } else if (ret
< 0) {
109 /* Most likely an ENOMEM. */
110 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
114 ret
= consumer_add_channel(channel
, ctx
);
117 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
124 * Allocate and return a consumer channel object.
126 static struct lttng_consumer_channel
*allocate_channel(uint64_t session_id
,
127 const uint64_t *chunk_id
, const char *pathname
, const char *name
,
128 uint64_t relayd_id
, uint64_t key
, enum lttng_event_output output
,
129 uint64_t tracefile_size
, uint64_t tracefile_count
,
130 uint64_t session_id_per_pid
, unsigned int monitor
,
131 unsigned int live_timer_interval
,
132 const char *root_shm_path
, const char *shm_path
)
137 return consumer_allocate_channel(key
, session_id
, chunk_id
, pathname
,
138 name
, relayd_id
, output
, tracefile_size
,
139 tracefile_count
, session_id_per_pid
, monitor
,
140 live_timer_interval
, root_shm_path
, shm_path
);
144 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
145 * error value if applicable is set in it else it is kept untouched.
147 * Return NULL on error else the newly allocated stream object.
149 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
150 struct lttng_consumer_channel
*channel
,
151 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
154 struct lttng_consumer_stream
*stream
= NULL
;
159 stream
= consumer_allocate_stream(channel
->key
,
164 channel
->trace_chunk
,
169 if (stream
== NULL
) {
173 * We could not find the channel. Can happen if cpu hotplug
174 * happens while tearing down.
176 DBG3("Could not find channel");
181 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
187 consumer_stream_update_channel_attributes(stream
, channel
);
188 stream
->chan
= channel
;
192 *_alloc_ret
= alloc_ret
;
198 * Send the given stream pointer to the corresponding thread.
200 * Returns 0 on success else a negative value.
202 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
203 struct lttng_consumer_local_data
*ctx
)
206 struct lttng_pipe
*stream_pipe
;
208 /* Get the right pipe where the stream will be sent. */
209 if (stream
->metadata_flag
) {
210 consumer_add_metadata_stream(stream
);
211 stream_pipe
= ctx
->consumer_metadata_pipe
;
213 consumer_add_data_stream(stream
);
214 stream_pipe
= ctx
->consumer_data_pipe
;
218 * From this point on, the stream's ownership has been moved away from
219 * the channel and it becomes globally visible. Hence, remove it from
220 * the local stream list to prevent the stream from being both local and
223 stream
->globally_visible
= 1;
224 cds_list_del(&stream
->send_node
);
226 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
228 ERR("Consumer write %s stream to pipe %d",
229 stream
->metadata_flag
? "metadata" : "data",
230 lttng_pipe_get_writefd(stream_pipe
));
231 if (stream
->metadata_flag
) {
232 consumer_del_stream_for_metadata(stream
);
234 consumer_del_stream_for_data(stream
);
244 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
246 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
249 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
250 stream_shm_path
[PATH_MAX
- 1] = '\0';
251 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
256 strncat(stream_shm_path
, cpu_nr
,
257 PATH_MAX
- strlen(stream_shm_path
) - 1);
264 * Create streams for the given channel using liblttng-ust-ctl.
265 * The channel lock must be acquired by the caller.
267 * Return 0 on success else a negative value.
269 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
270 struct lttng_consumer_local_data
*ctx
)
273 struct ustctl_consumer_stream
*ustream
;
274 struct lttng_consumer_stream
*stream
;
275 pthread_mutex_t
*current_stream_lock
= NULL
;
281 * While a stream is available from ustctl. When NULL is returned, we've
282 * reached the end of the possible stream for the channel.
284 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
286 int ust_metadata_pipe
[2];
288 health_code_update();
290 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
291 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
293 ERR("Create ust metadata poll pipe");
296 wait_fd
= ust_metadata_pipe
[0];
298 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
301 /* Allocate consumer stream object. */
302 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
306 stream
->ustream
= ustream
;
308 * Store it so we can save multiple function calls afterwards since
309 * this value is used heavily in the stream threads. This is UST
310 * specific so this is why it's done after allocation.
312 stream
->wait_fd
= wait_fd
;
315 * Increment channel refcount since the channel reference has now been
316 * assigned in the allocation process above.
318 if (stream
->chan
->monitor
) {
319 uatomic_inc(&stream
->chan
->refcount
);
322 pthread_mutex_lock(&stream
->lock
);
323 current_stream_lock
= &stream
->lock
;
325 * Order is important this is why a list is used. On error, the caller
326 * should clean this list.
328 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
330 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
331 &stream
->max_sb_size
);
333 ERR("ustctl_get_max_subbuf_size failed for stream %s",
338 /* Do actions once stream has been received. */
339 if (ctx
->on_recv_stream
) {
340 ret
= ctx
->on_recv_stream(stream
);
346 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
347 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
349 /* Set next CPU stream. */
350 channel
->streams
.count
= ++cpu
;
352 /* Keep stream reference when creating metadata. */
353 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
354 channel
->metadata_stream
= stream
;
355 if (channel
->monitor
) {
356 /* Set metadata poll pipe if we created one */
357 memcpy(stream
->ust_metadata_poll_pipe
,
359 sizeof(ust_metadata_pipe
));
362 pthread_mutex_unlock(&stream
->lock
);
363 current_stream_lock
= NULL
;
370 if (current_stream_lock
) {
371 pthread_mutex_unlock(current_stream_lock
);
377 * create_posix_shm is never called concurrently within a process.
380 int create_posix_shm(void)
382 char tmp_name
[NAME_MAX
];
385 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
391 * Allocate shm, and immediately unlink its shm oject, keeping
392 * only the file descriptor as a reference to the object.
393 * We specifically do _not_ use the / at the beginning of the
394 * pathname so that some OS implementations can keep it local to
395 * the process (POSIX leaves this implementation-defined).
397 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
402 ret
= shm_unlink(tmp_name
);
403 if (ret
< 0 && errno
!= ENOENT
) {
404 PERROR("shm_unlink");
405 goto error_shm_release
;
418 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
, int cpu
,
419 const struct lttng_credentials
*session_credentials
)
421 char shm_path
[PATH_MAX
];
424 if (!channel
->shm_path
[0]) {
425 return create_posix_shm();
427 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
431 return run_as_open(shm_path
,
432 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
433 session_credentials
->uid
, session_credentials
->gid
);
440 * Create an UST channel with the given attributes and send it to the session
441 * daemon using the ust ctl API.
443 * Return 0 on success or else a negative value.
445 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
446 struct ustctl_consumer_channel_attr
*attr
,
447 struct ustctl_consumer_channel
**ust_chanp
)
449 int ret
, nr_stream_fds
, i
, j
;
451 struct ustctl_consumer_channel
*ust_channel
;
456 assert(channel
->buffer_credentials
.is_set
);
458 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
459 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
460 "switch_timer_interval: %u, read_timer_interval: %u, "
461 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
462 attr
->num_subbuf
, attr
->switch_timer_interval
,
463 attr
->read_timer_interval
, attr
->output
, attr
->type
);
465 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
468 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
469 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
474 for (i
= 0; i
< nr_stream_fds
; i
++) {
475 stream_fds
[i
] = open_ust_stream_fd(channel
, i
,
476 &channel
->buffer_credentials
.value
);
477 if (stream_fds
[i
] < 0) {
482 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
487 channel
->nr_stream_fds
= nr_stream_fds
;
488 channel
->stream_fds
= stream_fds
;
489 *ust_chanp
= ust_channel
;
495 for (j
= i
- 1; j
>= 0; j
--) {
498 closeret
= close(stream_fds
[j
]);
502 if (channel
->shm_path
[0]) {
503 char shm_path
[PATH_MAX
];
505 closeret
= get_stream_shm_path(shm_path
,
506 channel
->shm_path
, j
);
508 ERR("Cannot get stream shm path");
510 closeret
= run_as_unlink(shm_path
,
511 channel
->buffer_credentials
.value
.uid
,
512 channel
->buffer_credentials
.value
.gid
);
514 PERROR("unlink %s", shm_path
);
518 /* Try to rmdir all directories under shm_path root. */
519 if (channel
->root_shm_path
[0]) {
520 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
521 channel
->buffer_credentials
.value
.uid
,
522 channel
->buffer_credentials
.value
.gid
,
523 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
531 * Send a single given stream to the session daemon using the sock.
533 * Return 0 on success else a negative value.
535 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
542 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
544 /* Send stream to session daemon. */
545 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
555 * Send channel to sessiond and relayd if applicable.
557 * Return 0 on success or else a negative value.
559 static int send_channel_to_sessiond_and_relayd(int sock
,
560 struct lttng_consumer_channel
*channel
,
561 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
563 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
564 struct lttng_consumer_stream
*stream
;
565 uint64_t net_seq_idx
= -1ULL;
571 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
573 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
574 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
576 health_code_update();
578 /* Try to send the stream to the relayd if one is available. */
579 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
580 stream
->key
, channel
->name
);
581 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
584 * Flag that the relayd was the problem here probably due to a
585 * communicaton error on the socket.
590 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
592 if (net_seq_idx
== -1ULL) {
593 net_seq_idx
= stream
->net_seq_idx
;
598 /* Inform sessiond that we are about to send channel and streams. */
599 ret
= consumer_send_status_msg(sock
, ret_code
);
600 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
602 * Either the session daemon is not responding or the relayd died so we
608 /* Send channel to sessiond. */
609 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
614 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
619 /* The channel was sent successfully to the sessiond at this point. */
620 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
622 health_code_update();
624 /* Send stream to session daemon. */
625 ret
= send_sessiond_stream(sock
, stream
);
631 /* Tell sessiond there is no more stream. */
632 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
637 DBG("UST consumer NULL stream sent to sessiond");
642 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
649 * Creates a channel and streams and add the channel it to the channel internal
650 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
653 * Return 0 on success or else, a negative value is returned and the channel
654 * MUST be destroyed by consumer_del_channel().
656 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
657 struct lttng_consumer_channel
*channel
,
658 struct ustctl_consumer_channel_attr
*attr
)
667 * This value is still used by the kernel consumer since for the kernel,
668 * the stream ownership is not IN the consumer so we need to have the
669 * number of left stream that needs to be initialized so we can know when
670 * to delete the channel (see consumer.c).
672 * As for the user space tracer now, the consumer creates and sends the
673 * stream to the session daemon which only sends them to the application
674 * once every stream of a channel is received making this value useless
675 * because we they will be added to the poll thread before the application
676 * receives them. This ensures that a stream can not hang up during
677 * initilization of a channel.
679 channel
->nb_init_stream_left
= 0;
681 /* The reply msg status is handled in the following call. */
682 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
687 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
690 * For the snapshots (no monitor), we create the metadata streams
691 * on demand, not during the channel creation.
693 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
698 /* Open all streams for this channel. */
699 pthread_mutex_lock(&channel
->lock
);
700 ret
= create_ust_streams(channel
, ctx
);
701 pthread_mutex_unlock(&channel
->lock
);
711 * Send all stream of a channel to the right thread handling it.
713 * On error, return a negative value else 0 on success.
715 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
716 struct lttng_consumer_local_data
*ctx
)
719 struct lttng_consumer_stream
*stream
, *stmp
;
724 /* Send streams to the corresponding thread. */
725 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
728 health_code_update();
730 /* Sending the stream to the thread. */
731 ret
= send_stream_to_thread(stream
, ctx
);
734 * If we are unable to send the stream to the thread, there is
735 * a big problem so just stop everything.
746 * Flush channel's streams using the given key to retrieve the channel.
748 * Return 0 on success else an LTTng error code.
750 static int flush_channel(uint64_t chan_key
)
753 struct lttng_consumer_channel
*channel
;
754 struct lttng_consumer_stream
*stream
;
756 struct lttng_ht_iter iter
;
758 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
761 channel
= consumer_find_channel(chan_key
);
763 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
764 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
768 ht
= consumer_data
.stream_per_chan_id_ht
;
770 /* For each stream of the channel id, flush it. */
771 cds_lfht_for_each_entry_duplicate(ht
->ht
,
772 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
773 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
775 health_code_update();
777 pthread_mutex_lock(&stream
->lock
);
780 * Protect against concurrent teardown of a stream.
782 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
786 if (!stream
->quiescent
) {
787 ustctl_flush_buffer(stream
->ustream
, 0);
788 stream
->quiescent
= true;
791 pthread_mutex_unlock(&stream
->lock
);
799 * Clear quiescent state from channel's streams using the given key to
800 * retrieve the channel.
802 * Return 0 on success else an LTTng error code.
804 static int clear_quiescent_channel(uint64_t chan_key
)
807 struct lttng_consumer_channel
*channel
;
808 struct lttng_consumer_stream
*stream
;
810 struct lttng_ht_iter iter
;
812 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
815 channel
= consumer_find_channel(chan_key
);
817 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
818 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
822 ht
= consumer_data
.stream_per_chan_id_ht
;
824 /* For each stream of the channel id, clear quiescent state. */
825 cds_lfht_for_each_entry_duplicate(ht
->ht
,
826 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
827 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
829 health_code_update();
831 pthread_mutex_lock(&stream
->lock
);
832 stream
->quiescent
= false;
833 pthread_mutex_unlock(&stream
->lock
);
841 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
843 * Return 0 on success else an LTTng error code.
845 static int close_metadata(uint64_t chan_key
)
848 struct lttng_consumer_channel
*channel
;
849 unsigned int channel_monitor
;
851 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
853 channel
= consumer_find_channel(chan_key
);
856 * This is possible if the metadata thread has issue a delete because
857 * the endpoint point of the stream hung up. There is no way the
858 * session daemon can know about it thus use a DBG instead of an actual
861 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
862 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
866 pthread_mutex_lock(&consumer_data
.lock
);
867 pthread_mutex_lock(&channel
->lock
);
868 channel_monitor
= channel
->monitor
;
869 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
873 lttng_ustconsumer_close_metadata(channel
);
874 pthread_mutex_unlock(&channel
->lock
);
875 pthread_mutex_unlock(&consumer_data
.lock
);
878 * The ownership of a metadata channel depends on the type of
879 * session to which it belongs. In effect, the monitor flag is checked
880 * to determine if this metadata channel is in "snapshot" mode or not.
882 * In the non-snapshot case, the metadata channel is created along with
883 * a single stream which will remain present until the metadata channel
884 * is destroyed (on the destruction of its session). In this case, the
885 * metadata stream in "monitored" by the metadata poll thread and holds
886 * the ownership of its channel.
888 * Closing the metadata will cause the metadata stream's "metadata poll
889 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
890 * thread which will teardown the metadata stream which, in return,
891 * deletes the metadata channel.
893 * In the snapshot case, the metadata stream is created and destroyed
894 * on every snapshot record. Since the channel doesn't have an owner
895 * other than the session daemon, it is safe to destroy it immediately
896 * on reception of the CLOSE_METADATA command.
898 if (!channel_monitor
) {
900 * The channel and consumer_data locks must be
901 * released before this call since consumer_del_channel
902 * re-acquires the channel and consumer_data locks to teardown
903 * the channel and queue its reclamation by the "call_rcu"
906 consumer_del_channel(channel
);
911 pthread_mutex_unlock(&channel
->lock
);
912 pthread_mutex_unlock(&consumer_data
.lock
);
918 * RCU read side lock MUST be acquired before calling this function.
920 * Return 0 on success else an LTTng error code.
922 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
925 struct lttng_consumer_channel
*metadata
;
927 DBG("UST consumer setup metadata key %" PRIu64
, key
);
929 metadata
= consumer_find_channel(key
);
931 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
932 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
937 * In no monitor mode, the metadata channel has no stream(s) so skip the
938 * ownership transfer to the metadata thread.
940 if (!metadata
->monitor
) {
941 DBG("Metadata channel in no monitor");
947 * Send metadata stream to relayd if one available. Availability is
948 * known if the stream is still in the list of the channel.
950 if (cds_list_empty(&metadata
->streams
.head
)) {
951 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
952 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
953 goto error_no_stream
;
956 /* Send metadata stream to relayd if needed. */
957 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
958 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
961 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
964 ret
= consumer_send_relayd_streams_sent(
965 metadata
->metadata_stream
->net_seq_idx
);
967 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
973 * Ownership of metadata stream is passed along. Freeing is handled by
976 ret
= send_streams_to_thread(metadata
, ctx
);
979 * If we are unable to send the stream to the thread, there is
980 * a big problem so just stop everything.
982 ret
= LTTCOMM_CONSUMERD_FATAL
;
983 goto send_streams_error
;
985 /* List MUST be empty after or else it could be reused. */
986 assert(cds_list_empty(&metadata
->streams
.head
));
993 * Delete metadata channel on error. At this point, the metadata stream can
994 * NOT be monitored by the metadata thread thus having the guarantee that
995 * the stream is still in the local stream list of the channel. This call
996 * will make sure to clean that list.
998 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
999 cds_list_del(&metadata
->metadata_stream
->send_node
);
1000 metadata
->metadata_stream
= NULL
;
1008 * Snapshot the whole metadata.
1009 * RCU read-side lock must be held by the caller.
1011 * Returns 0 on success, < 0 on error
1013 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
1014 uint64_t key
, char *path
, uint64_t relayd_id
,
1015 struct lttng_consumer_local_data
*ctx
)
1018 struct lttng_consumer_stream
*metadata_stream
;
1023 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
1028 assert(!metadata_channel
->monitor
);
1030 health_code_update();
1033 * Ask the sessiond if we have new metadata waiting and update the
1034 * consumer metadata cache.
1036 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1041 health_code_update();
1044 * The metadata stream is NOT created in no monitor mode when the channel
1045 * is created on a sessiond ask channel command.
1047 ret
= create_ust_streams(metadata_channel
, ctx
);
1052 metadata_stream
= metadata_channel
->metadata_stream
;
1053 assert(metadata_stream
);
1055 pthread_mutex_lock(&metadata_stream
->lock
);
1056 if (relayd_id
!= (uint64_t) -1ULL) {
1057 metadata_stream
->net_seq_idx
= relayd_id
;
1058 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1060 ret
= consumer_stream_create_output_files(metadata_stream
,
1063 pthread_mutex_unlock(&metadata_stream
->lock
);
1069 health_code_update();
1071 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
);
1079 * Clean up the stream completly because the next snapshot will use a new
1082 pthread_mutex_lock(&metadata_stream
->lock
);
1083 consumer_stream_destroy(metadata_stream
, NULL
);
1084 cds_list_del(&metadata_stream
->send_node
);
1085 metadata_channel
->metadata_stream
= NULL
;
1093 * Take a snapshot of all the stream of a channel.
1094 * RCU read-side lock and the channel lock must be held by the caller.
1096 * Returns 0 on success, < 0 on error
1098 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1099 uint64_t key
, char *path
, uint64_t relayd_id
,
1100 uint64_t nb_packets_per_stream
,
1101 struct lttng_consumer_local_data
*ctx
)
1104 unsigned use_relayd
= 0;
1105 unsigned long consumed_pos
, produced_pos
;
1106 struct lttng_consumer_stream
*stream
;
1113 if (relayd_id
!= (uint64_t) -1ULL) {
1117 assert(!channel
->monitor
);
1118 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1120 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1121 health_code_update();
1123 /* Lock stream because we are about to change its state. */
1124 pthread_mutex_lock(&stream
->lock
);
1125 assert(channel
->trace_chunk
);
1126 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1128 * Can't happen barring an internal error as the channel
1129 * holds a reference to the trace chunk.
1131 ERR("Failed to acquire reference to channel's trace chunk");
1135 assert(!stream
->trace_chunk
);
1136 stream
->trace_chunk
= channel
->trace_chunk
;
1138 stream
->net_seq_idx
= relayd_id
;
1141 ret
= consumer_send_relayd_stream(stream
, path
);
1146 ret
= consumer_stream_create_output_files(stream
,
1151 DBG("UST consumer snapshot stream (%" PRIu64
")",
1156 * If tracing is active, we want to perform a "full" buffer flush.
1157 * Else, if quiescent, it has already been done by the prior stop.
1159 if (!stream
->quiescent
) {
1160 ustctl_flush_buffer(stream
->ustream
, 0);
1163 ret
= lttng_ustconsumer_take_snapshot(stream
);
1165 ERR("Taking UST snapshot");
1169 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1171 ERR("Produced UST snapshot position");
1175 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1177 ERR("Consumerd UST snapshot position");
1182 * The original value is sent back if max stream size is larger than
1183 * the possible size of the snapshot. Also, we assume that the session
1184 * daemon should never send a maximum stream size that is lower than
1187 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1188 produced_pos
, nb_packets_per_stream
,
1189 stream
->max_sb_size
);
1191 while ((long) (consumed_pos
- produced_pos
) < 0) {
1193 unsigned long len
, padded_len
;
1195 health_code_update();
1197 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1199 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1201 if (ret
!= -EAGAIN
) {
1202 PERROR("ustctl_get_subbuf snapshot");
1203 goto error_close_stream
;
1205 DBG("UST consumer get subbuf failed. Skipping it.");
1206 consumed_pos
+= stream
->max_sb_size
;
1207 stream
->chan
->lost_packets
++;
1211 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1213 ERR("Snapshot ustctl_get_subbuf_size");
1214 goto error_put_subbuf
;
1217 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1219 ERR("Snapshot ustctl_get_padded_subbuf_size");
1220 goto error_put_subbuf
;
1223 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
1224 padded_len
- len
, NULL
);
1226 if (read_len
!= len
) {
1228 goto error_put_subbuf
;
1231 if (read_len
!= padded_len
) {
1233 goto error_put_subbuf
;
1237 ret
= ustctl_put_subbuf(stream
->ustream
);
1239 ERR("Snapshot ustctl_put_subbuf");
1240 goto error_close_stream
;
1242 consumed_pos
+= stream
->max_sb_size
;
1245 /* Simply close the stream so we can use it on the next snapshot. */
1246 consumer_stream_close(stream
);
1247 pthread_mutex_unlock(&stream
->lock
);
1254 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1255 ERR("Snapshot ustctl_put_subbuf");
1258 consumer_stream_close(stream
);
1260 pthread_mutex_unlock(&stream
->lock
);
1266 * Receive the metadata updates from the sessiond. Supports receiving
1267 * overlapping metadata, but is needs to always belong to a contiguous
1268 * range starting from 0.
1269 * Be careful about the locks held when calling this function: it needs
1270 * the metadata cache flush to concurrently progress in order to
1273 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1274 uint64_t len
, uint64_t version
,
1275 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1277 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1280 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1282 metadata_str
= zmalloc(len
* sizeof(char));
1283 if (!metadata_str
) {
1284 PERROR("zmalloc metadata string");
1285 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1289 health_code_update();
1291 /* Receive metadata string. */
1292 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1294 /* Session daemon is dead so return gracefully. */
1299 health_code_update();
1301 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1302 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1305 /* Unable to handle metadata. Notify session daemon. */
1306 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1308 * Skip metadata flush on write error since the offset and len might
1309 * not have been updated which could create an infinite loop below when
1310 * waiting for the metadata cache to be flushed.
1312 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1315 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1320 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1321 DBG("Waiting for metadata to be flushed");
1323 health_code_update();
1325 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1335 * Receive command from session daemon and process it.
1337 * Return 1 on success else a negative value or 0.
1339 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1340 int sock
, struct pollfd
*consumer_sockpoll
)
1343 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1344 struct lttcomm_consumer_msg msg
;
1345 struct lttng_consumer_channel
*channel
= NULL
;
1347 health_code_update();
1349 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1350 if (ret
!= sizeof(msg
)) {
1351 DBG("Consumer received unexpected message size %zd (expects %zu)",
1354 * The ret value might 0 meaning an orderly shutdown but this is ok
1355 * since the caller handles this.
1358 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1364 health_code_update();
1367 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1369 health_code_update();
1371 /* relayd needs RCU read-side lock */
1374 switch (msg
.cmd_type
) {
1375 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1377 /* Session daemon status message are handled in the following call. */
1378 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1379 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1380 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1381 msg
.u
.relayd_sock
.relayd_session_id
);
1384 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1386 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1387 struct consumer_relayd_sock_pair
*relayd
;
1389 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1391 /* Get relayd reference if exists. */
1392 relayd
= consumer_find_relayd(index
);
1393 if (relayd
== NULL
) {
1394 DBG("Unable to find relayd %" PRIu64
, index
);
1395 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1399 * Each relayd socket pair has a refcount of stream attached to it
1400 * which tells if the relayd is still active or not depending on the
1403 * This will set the destroy flag of the relayd object and destroy it
1404 * if the refcount reaches zero when called.
1406 * The destroy can happen either here or when a stream fd hangs up.
1409 consumer_flag_relayd_for_destroy(relayd
);
1412 goto end_msg_sessiond
;
1414 case LTTNG_CONSUMER_UPDATE_STREAM
:
1419 case LTTNG_CONSUMER_DATA_PENDING
:
1421 int ret
, is_data_pending
;
1422 uint64_t id
= msg
.u
.data_pending
.session_id
;
1424 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1426 is_data_pending
= consumer_data_pending(id
);
1428 /* Send back returned value to session daemon */
1429 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1430 sizeof(is_data_pending
));
1432 DBG("Error when sending the data pending ret code: %d", ret
);
1437 * No need to send back a status message since the data pending
1438 * returned value is the response.
1442 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1445 struct ustctl_consumer_channel_attr attr
;
1446 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1447 const struct lttng_credentials buffer_credentials
= {
1448 .uid
= msg
.u
.ask_channel
.buffer_credentials
.uid
,
1449 .gid
= msg
.u
.ask_channel
.buffer_credentials
.gid
,
1452 /* Create a plain object and reserve a channel key. */
1453 channel
= allocate_channel(msg
.u
.ask_channel
.session_id
,
1454 msg
.u
.ask_channel
.chunk_id
.is_set
?
1456 msg
.u
.ask_channel
.pathname
,
1457 msg
.u
.ask_channel
.name
,
1458 msg
.u
.ask_channel
.relayd_id
,
1459 msg
.u
.ask_channel
.key
,
1460 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1461 msg
.u
.ask_channel
.tracefile_size
,
1462 msg
.u
.ask_channel
.tracefile_count
,
1463 msg
.u
.ask_channel
.session_id_per_pid
,
1464 msg
.u
.ask_channel
.monitor
,
1465 msg
.u
.ask_channel
.live_timer_interval
,
1466 msg
.u
.ask_channel
.root_shm_path
,
1467 msg
.u
.ask_channel
.shm_path
);
1469 goto end_channel_error
;
1472 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1473 buffer_credentials
);
1476 * Assign UST application UID to the channel. This value is ignored for
1477 * per PID buffers. This is specific to UST thus setting this after the
1480 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1482 /* Build channel attributes from received message. */
1483 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1484 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1485 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1486 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1487 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1488 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1489 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1490 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1492 /* Match channel buffer type to the UST abi. */
1493 switch (msg
.u
.ask_channel
.output
) {
1494 case LTTNG_EVENT_MMAP
:
1496 attr
.output
= LTTNG_UST_MMAP
;
1500 /* Translate and save channel type. */
1501 switch (msg
.u
.ask_channel
.type
) {
1502 case LTTNG_UST_CHAN_PER_CPU
:
1503 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1504 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1506 * Set refcount to 1 for owner. Below, we will
1507 * pass ownership to the
1508 * consumer_thread_channel_poll() thread.
1510 channel
->refcount
= 1;
1512 case LTTNG_UST_CHAN_METADATA
:
1513 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1514 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1521 health_code_update();
1523 ret
= ask_channel(ctx
, channel
, &attr
);
1525 goto end_channel_error
;
1528 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1529 ret
= consumer_metadata_cache_allocate(channel
);
1531 ERR("Allocating metadata cache");
1532 goto end_channel_error
;
1534 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1535 attr
.switch_timer_interval
= 0;
1537 int monitor_start_ret
;
1539 consumer_timer_live_start(channel
,
1540 msg
.u
.ask_channel
.live_timer_interval
);
1541 monitor_start_ret
= consumer_timer_monitor_start(
1543 msg
.u
.ask_channel
.monitor_timer_interval
);
1544 if (monitor_start_ret
< 0) {
1545 ERR("Starting channel monitoring timer failed");
1546 goto end_channel_error
;
1550 health_code_update();
1553 * Add the channel to the internal state AFTER all streams were created
1554 * and successfully sent to session daemon. This way, all streams must
1555 * be ready before this channel is visible to the threads.
1556 * If add_channel succeeds, ownership of the channel is
1557 * passed to consumer_thread_channel_poll().
1559 ret
= add_channel(channel
, ctx
);
1561 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1562 if (channel
->switch_timer_enabled
== 1) {
1563 consumer_timer_switch_stop(channel
);
1565 consumer_metadata_cache_destroy(channel
);
1567 if (channel
->live_timer_enabled
== 1) {
1568 consumer_timer_live_stop(channel
);
1570 if (channel
->monitor_timer_enabled
== 1) {
1571 consumer_timer_monitor_stop(channel
);
1573 goto end_channel_error
;
1576 health_code_update();
1579 * Channel and streams are now created. Inform the session daemon that
1580 * everything went well and should wait to receive the channel and
1581 * streams with ustctl API.
1583 ret
= consumer_send_status_channel(sock
, channel
);
1586 * There is probably a problem on the socket.
1593 case LTTNG_CONSUMER_GET_CHANNEL
:
1595 int ret
, relayd_err
= 0;
1596 uint64_t key
= msg
.u
.get_channel
.key
;
1597 struct lttng_consumer_channel
*channel
;
1599 channel
= consumer_find_channel(key
);
1601 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1602 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1603 goto end_get_channel
;
1606 health_code_update();
1608 /* Send the channel to sessiond (and relayd, if applicable). */
1609 ret
= send_channel_to_sessiond_and_relayd(sock
, channel
, ctx
,
1614 * We were unable to send to the relayd the stream so avoid
1615 * sending back a fatal error to the thread since this is OK
1616 * and the consumer can continue its work. The above call
1617 * has sent the error status message to the sessiond.
1619 goto end_get_channel_nosignal
;
1622 * The communicaton was broken hence there is a bad state between
1623 * the consumer and sessiond so stop everything.
1625 goto error_get_channel_fatal
;
1628 health_code_update();
1631 * In no monitor mode, the streams ownership is kept inside the channel
1632 * so don't send them to the data thread.
1634 if (!channel
->monitor
) {
1635 goto end_get_channel
;
1638 ret
= send_streams_to_thread(channel
, ctx
);
1641 * If we are unable to send the stream to the thread, there is
1642 * a big problem so just stop everything.
1644 goto error_get_channel_fatal
;
1646 /* List MUST be empty after or else it could be reused. */
1647 assert(cds_list_empty(&channel
->streams
.head
));
1649 goto end_msg_sessiond
;
1650 error_get_channel_fatal
:
1652 end_get_channel_nosignal
:
1655 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1657 uint64_t key
= msg
.u
.destroy_channel
.key
;
1660 * Only called if streams have not been sent to stream
1661 * manager thread. However, channel has been sent to
1662 * channel manager thread.
1664 notify_thread_del_channel(ctx
, key
);
1665 goto end_msg_sessiond
;
1667 case LTTNG_CONSUMER_CLOSE_METADATA
:
1671 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1676 goto end_msg_sessiond
;
1678 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1682 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1687 goto end_msg_sessiond
;
1689 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1693 ret
= clear_quiescent_channel(
1694 msg
.u
.clear_quiescent_channel
.key
);
1699 goto end_msg_sessiond
;
1701 case LTTNG_CONSUMER_PUSH_METADATA
:
1704 uint64_t len
= msg
.u
.push_metadata
.len
;
1705 uint64_t key
= msg
.u
.push_metadata
.key
;
1706 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1707 uint64_t version
= msg
.u
.push_metadata
.version
;
1708 struct lttng_consumer_channel
*channel
;
1710 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1713 channel
= consumer_find_channel(key
);
1716 * This is possible if the metadata creation on the consumer side
1717 * is in flight vis-a-vis a concurrent push metadata from the
1718 * session daemon. Simply return that the channel failed and the
1719 * session daemon will handle that message correctly considering
1720 * that this race is acceptable thus the DBG() statement here.
1722 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1723 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1724 goto end_push_metadata_msg_sessiond
;
1727 health_code_update();
1731 * There is nothing to receive. We have simply
1732 * checked whether the channel can be found.
1734 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1735 goto end_push_metadata_msg_sessiond
;
1738 /* Tell session daemon we are ready to receive the metadata. */
1739 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1741 /* Somehow, the session daemon is not responding anymore. */
1742 goto error_push_metadata_fatal
;
1745 health_code_update();
1747 /* Wait for more data. */
1748 health_poll_entry();
1749 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1752 goto error_push_metadata_fatal
;
1755 health_code_update();
1757 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1758 len
, version
, channel
, 0, 1);
1760 /* error receiving from sessiond */
1761 goto error_push_metadata_fatal
;
1764 goto end_push_metadata_msg_sessiond
;
1766 end_push_metadata_msg_sessiond
:
1767 goto end_msg_sessiond
;
1768 error_push_metadata_fatal
:
1771 case LTTNG_CONSUMER_SETUP_METADATA
:
1775 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1779 goto end_msg_sessiond
;
1781 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1783 struct lttng_consumer_channel
*channel
;
1784 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1786 channel
= consumer_find_channel(key
);
1788 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1789 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1791 if (msg
.u
.snapshot_channel
.metadata
) {
1792 ret
= snapshot_metadata(channel
, key
,
1793 msg
.u
.snapshot_channel
.pathname
,
1794 msg
.u
.snapshot_channel
.relayd_id
,
1797 ERR("Snapshot metadata failed");
1798 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1801 ret
= snapshot_channel(channel
, key
,
1802 msg
.u
.snapshot_channel
.pathname
,
1803 msg
.u
.snapshot_channel
.relayd_id
,
1804 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1807 ERR("Snapshot channel failed");
1808 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1812 health_code_update();
1813 ret
= consumer_send_status_msg(sock
, ret_code
);
1815 /* Somehow, the session daemon is not responding anymore. */
1818 health_code_update();
1821 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1824 uint64_t discarded_events
;
1825 struct lttng_ht_iter iter
;
1826 struct lttng_ht
*ht
;
1827 struct lttng_consumer_stream
*stream
;
1828 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1829 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1831 DBG("UST consumer discarded events command for session id %"
1834 pthread_mutex_lock(&consumer_data
.lock
);
1836 ht
= consumer_data
.stream_list_ht
;
1839 * We only need a reference to the channel, but they are not
1840 * directly indexed, so we just use the first matching stream
1841 * to extract the information we need, we default to 0 if not
1842 * found (no events are dropped if the channel is not yet in
1845 discarded_events
= 0;
1846 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1847 ht
->hash_fct(&id
, lttng_ht_seed
),
1849 &iter
.iter
, stream
, node_session_id
.node
) {
1850 if (stream
->chan
->key
== key
) {
1851 discarded_events
= stream
->chan
->discarded_events
;
1855 pthread_mutex_unlock(&consumer_data
.lock
);
1858 DBG("UST consumer discarded events command for session id %"
1859 PRIu64
", channel key %" PRIu64
, id
, key
);
1861 health_code_update();
1863 /* Send back returned value to session daemon */
1864 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1866 PERROR("send discarded events");
1872 case LTTNG_CONSUMER_LOST_PACKETS
:
1875 uint64_t lost_packets
;
1876 struct lttng_ht_iter iter
;
1877 struct lttng_ht
*ht
;
1878 struct lttng_consumer_stream
*stream
;
1879 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1880 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1882 DBG("UST consumer lost packets command for session id %"
1885 pthread_mutex_lock(&consumer_data
.lock
);
1887 ht
= consumer_data
.stream_list_ht
;
1890 * We only need a reference to the channel, but they are not
1891 * directly indexed, so we just use the first matching stream
1892 * to extract the information we need, we default to 0 if not
1893 * found (no packets lost if the channel is not yet in use).
1896 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1897 ht
->hash_fct(&id
, lttng_ht_seed
),
1899 &iter
.iter
, stream
, node_session_id
.node
) {
1900 if (stream
->chan
->key
== key
) {
1901 lost_packets
= stream
->chan
->lost_packets
;
1905 pthread_mutex_unlock(&consumer_data
.lock
);
1908 DBG("UST consumer lost packets command for session id %"
1909 PRIu64
", channel key %" PRIu64
, id
, key
);
1911 health_code_update();
1913 /* Send back returned value to session daemon */
1914 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1915 sizeof(lost_packets
));
1917 PERROR("send lost packets");
1923 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1925 int channel_monitor_pipe
;
1927 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1928 /* Successfully received the command's type. */
1929 ret
= consumer_send_status_msg(sock
, ret_code
);
1934 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1936 if (ret
!= sizeof(channel_monitor_pipe
)) {
1937 ERR("Failed to receive channel monitor pipe");
1941 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1942 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1943 channel_monitor_pipe
);
1947 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1948 /* Set the pipe as non-blocking. */
1949 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1951 PERROR("fcntl get flags of the channel monitoring pipe");
1956 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1957 flags
| O_NONBLOCK
);
1959 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1962 DBG("Channel monitor pipe set as non-blocking");
1964 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1966 goto end_msg_sessiond
;
1968 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1970 struct lttng_consumer_channel
*channel
;
1971 uint64_t key
= msg
.u
.rotate_channel
.key
;
1973 channel
= consumer_find_channel(key
);
1975 DBG("Channel %" PRIu64
" not found", key
);
1976 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1979 * Sample the rotate position of all the streams in
1982 ret
= lttng_consumer_rotate_channel(channel
, key
,
1983 msg
.u
.rotate_channel
.relayd_id
,
1984 msg
.u
.rotate_channel
.metadata
,
1987 ERR("Rotate channel failed");
1988 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1991 health_code_update();
1993 ret
= consumer_send_status_msg(sock
, ret_code
);
1995 /* Somehow, the session daemon is not responding anymore. */
1996 goto end_rotate_channel_nosignal
;
2000 * Rotate the streams that are ready right now.
2001 * FIXME: this is a second consecutive iteration over the
2002 * streams in a channel, there is probably a better way to
2003 * handle this, but it needs to be after the
2004 * consumer_send_status_msg() call.
2007 ret
= lttng_consumer_rotate_ready_streams(
2010 ERR("Rotate channel failed");
2014 end_rotate_channel_nosignal
:
2017 case LTTNG_CONSUMER_INIT
:
2019 ret_code
= lttng_consumer_init_command(ctx
,
2020 msg
.u
.init
.sessiond_uuid
);
2021 health_code_update();
2022 ret
= consumer_send_status_msg(sock
, ret_code
);
2024 /* Somehow, the session daemon is not responding anymore. */
2029 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2031 const struct lttng_credentials credentials
= {
2032 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
2033 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
2035 const bool is_local_trace
=
2036 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2037 const uint64_t relayd_id
=
2038 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2039 const char *chunk_override_name
=
2040 *msg
.u
.create_trace_chunk
.override_name
?
2041 msg
.u
.create_trace_chunk
.override_name
:
2043 LTTNG_OPTIONAL(struct lttng_directory_handle
) chunk_directory_handle
=
2044 LTTNG_OPTIONAL_INIT
;
2047 * The session daemon will only provide a chunk directory file
2048 * descriptor for local traces.
2050 if (is_local_trace
) {
2053 /* Acnowledge the reception of the command. */
2054 ret
= consumer_send_status_msg(sock
,
2055 LTTCOMM_CONSUMERD_SUCCESS
);
2057 /* Somehow, the session daemon is not responding anymore. */
2061 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
2062 if (ret
!= sizeof(chunk_dirfd
)) {
2063 ERR("Failed to receive trace chunk directory file descriptor");
2067 DBG("Received trace chunk directory fd (%d)",
2069 ret
= lttng_directory_handle_init_from_dirfd(
2070 &chunk_directory_handle
.value
,
2073 ERR("Failed to initialize chunk directory handle from directory file descriptor");
2074 if (close(chunk_dirfd
)) {
2075 PERROR("Failed to close chunk directory file descriptor");
2079 chunk_directory_handle
.is_set
= true;
2082 ret_code
= lttng_consumer_create_trace_chunk(
2083 !is_local_trace
? &relayd_id
: NULL
,
2084 msg
.u
.create_trace_chunk
.session_id
,
2085 msg
.u
.create_trace_chunk
.chunk_id
,
2086 (time_t) msg
.u
.create_trace_chunk
2087 .creation_timestamp
,
2088 chunk_override_name
,
2089 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2092 chunk_directory_handle
.is_set
?
2093 &chunk_directory_handle
.value
:
2096 if (chunk_directory_handle
.is_set
) {
2097 lttng_directory_handle_fini(
2098 &chunk_directory_handle
.value
);
2100 goto end_msg_sessiond
;
2102 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2104 enum lttng_trace_chunk_command_type close_command
=
2105 msg
.u
.close_trace_chunk
.close_command
.value
;
2106 const uint64_t relayd_id
=
2107 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2108 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2109 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2112 ret_code
= lttng_consumer_close_trace_chunk(
2113 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2116 msg
.u
.close_trace_chunk
.session_id
,
2117 msg
.u
.close_trace_chunk
.chunk_id
,
2118 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2119 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2121 NULL
, closed_trace_chunk_path
);
2122 reply
.ret_code
= ret_code
;
2123 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2124 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2125 if (ret
!= sizeof(reply
)) {
2128 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2130 if (ret
!= reply
.path_length
) {
2135 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2137 const uint64_t relayd_id
=
2138 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2140 ret_code
= lttng_consumer_trace_chunk_exists(
2141 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2143 msg
.u
.trace_chunk_exists
.session_id
,
2144 msg
.u
.trace_chunk_exists
.chunk_id
);
2145 goto end_msg_sessiond
;
2153 * Return 1 to indicate success since the 0 value can be a socket
2154 * shutdown during the recv() or send() call.
2161 * The returned value here is not useful since either way we'll return 1 to
2162 * the caller because the session daemon socket management is done
2163 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2165 ret
= consumer_send_status_msg(sock
, ret_code
);
2174 pthread_mutex_unlock(&channel
->lock
);
2176 * Free channel here since no one has a reference to it. We don't
2177 * free after that because a stream can store this pointer.
2179 destroy_channel(channel
);
2181 /* We have to send a status channel message indicating an error. */
2182 ret
= consumer_send_status_channel(sock
, NULL
);
2184 /* Stop everything if session daemon can not be notified. */
2191 /* This will issue a consumer stop. */
2197 health_code_update();
2202 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
2203 * compiled out, we isolate it in this library.
2205 int lttng_ustctl_get_mmap_read_offset(struct lttng_consumer_stream
*stream
,
2209 assert(stream
->ustream
);
2211 return ustctl_get_mmap_read_offset(stream
->ustream
, off
);
2215 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
2216 * compiled out, we isolate it in this library.
2218 void *lttng_ustctl_get_mmap_base(struct lttng_consumer_stream
*stream
)
2221 assert(stream
->ustream
);
2223 return ustctl_get_mmap_base(stream
->ustream
);
2226 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
2227 int producer_active
)
2230 assert(stream
->ustream
);
2232 ustctl_flush_buffer(stream
->ustream
, producer_active
);
2236 * Take a snapshot for a specific stream.
2238 * Returns 0 on success, < 0 on error
2240 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2243 assert(stream
->ustream
);
2245 return ustctl_snapshot(stream
->ustream
);
2249 * Sample consumed and produced positions for a specific stream.
2251 * Returns 0 on success, < 0 on error.
2253 int lttng_ustconsumer_sample_snapshot_positions(
2254 struct lttng_consumer_stream
*stream
)
2257 assert(stream
->ustream
);
2259 return ustctl_snapshot_sample_positions(stream
->ustream
);
2263 * Get the produced position
2265 * Returns 0 on success, < 0 on error
2267 int lttng_ustconsumer_get_produced_snapshot(
2268 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2271 assert(stream
->ustream
);
2274 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2278 * Get the consumed position
2280 * Returns 0 on success, < 0 on error
2282 int lttng_ustconsumer_get_consumed_snapshot(
2283 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2286 assert(stream
->ustream
);
2289 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2292 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2296 assert(stream
->ustream
);
2298 ustctl_flush_buffer(stream
->ustream
, producer
);
2301 int lttng_ustconsumer_get_current_timestamp(
2302 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2305 assert(stream
->ustream
);
2308 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2311 int lttng_ustconsumer_get_sequence_number(
2312 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2315 assert(stream
->ustream
);
2318 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2322 * Called when the stream signals the consumer that it has hung up.
2324 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2327 assert(stream
->ustream
);
2329 pthread_mutex_lock(&stream
->lock
);
2330 if (!stream
->quiescent
) {
2331 ustctl_flush_buffer(stream
->ustream
, 0);
2332 stream
->quiescent
= true;
2334 pthread_mutex_unlock(&stream
->lock
);
2335 stream
->hangup_flush_done
= 1;
2338 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2343 assert(chan
->uchan
);
2344 assert(chan
->buffer_credentials
.is_set
);
2346 if (chan
->switch_timer_enabled
== 1) {
2347 consumer_timer_switch_stop(chan
);
2349 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2352 ret
= close(chan
->stream_fds
[i
]);
2356 if (chan
->shm_path
[0]) {
2357 char shm_path
[PATH_MAX
];
2359 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2361 ERR("Cannot get stream shm path");
2363 ret
= run_as_unlink(shm_path
,
2364 chan
->buffer_credentials
.value
.uid
,
2365 chan
->buffer_credentials
.value
.gid
);
2367 PERROR("unlink %s", shm_path
);
2373 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2376 assert(chan
->uchan
);
2377 assert(chan
->buffer_credentials
.is_set
);
2379 consumer_metadata_cache_destroy(chan
);
2380 ustctl_destroy_channel(chan
->uchan
);
2381 /* Try to rmdir all directories under shm_path root. */
2382 if (chan
->root_shm_path
[0]) {
2383 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2384 chan
->buffer_credentials
.value
.uid
,
2385 chan
->buffer_credentials
.value
.gid
,
2386 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2388 free(chan
->stream_fds
);
2391 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2394 assert(stream
->ustream
);
2396 if (stream
->chan
->switch_timer_enabled
== 1) {
2397 consumer_timer_switch_stop(stream
->chan
);
2399 ustctl_destroy_stream(stream
->ustream
);
2402 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2405 assert(stream
->ustream
);
2407 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2410 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2413 assert(stream
->ustream
);
2415 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2419 * Populate index values of a UST stream. Values are set in big endian order.
2421 * Return 0 on success or else a negative value.
2423 static int get_index_values(struct ctf_packet_index
*index
,
2424 struct ustctl_consumer_stream
*ustream
)
2428 ret
= ustctl_get_timestamp_begin(ustream
, &index
->timestamp_begin
);
2430 PERROR("ustctl_get_timestamp_begin");
2433 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
2435 ret
= ustctl_get_timestamp_end(ustream
, &index
->timestamp_end
);
2437 PERROR("ustctl_get_timestamp_end");
2440 index
->timestamp_end
= htobe64(index
->timestamp_end
);
2442 ret
= ustctl_get_events_discarded(ustream
, &index
->events_discarded
);
2444 PERROR("ustctl_get_events_discarded");
2447 index
->events_discarded
= htobe64(index
->events_discarded
);
2449 ret
= ustctl_get_content_size(ustream
, &index
->content_size
);
2451 PERROR("ustctl_get_content_size");
2454 index
->content_size
= htobe64(index
->content_size
);
2456 ret
= ustctl_get_packet_size(ustream
, &index
->packet_size
);
2458 PERROR("ustctl_get_packet_size");
2461 index
->packet_size
= htobe64(index
->packet_size
);
2463 ret
= ustctl_get_stream_id(ustream
, &index
->stream_id
);
2465 PERROR("ustctl_get_stream_id");
2468 index
->stream_id
= htobe64(index
->stream_id
);
2470 ret
= ustctl_get_instance_id(ustream
, &index
->stream_instance_id
);
2472 PERROR("ustctl_get_instance_id");
2475 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
2477 ret
= ustctl_get_sequence_number(ustream
, &index
->packet_seq_num
);
2479 PERROR("ustctl_get_sequence_number");
2482 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
2489 void metadata_stream_reset_cache(struct lttng_consumer_stream
*stream
,
2490 struct consumer_metadata_cache
*cache
)
2492 DBG("Metadata stream update to version %" PRIu64
,
2494 stream
->ust_metadata_pushed
= 0;
2495 stream
->metadata_version
= cache
->version
;
2496 stream
->reset_metadata_flag
= 1;
2500 * Check if the version of the metadata stream and metadata cache match.
2501 * If the cache got updated, reset the metadata stream.
2502 * The stream lock and metadata cache lock MUST be held.
2503 * Return 0 on success, a negative value on error.
2506 int metadata_stream_check_version(struct lttng_consumer_stream
*stream
)
2509 struct consumer_metadata_cache
*cache
= stream
->chan
->metadata_cache
;
2511 if (cache
->version
== stream
->metadata_version
) {
2514 metadata_stream_reset_cache(stream
, cache
);
2521 * Write up to one packet from the metadata cache to the channel.
2523 * Returns the number of bytes pushed in the cache, or a negative value
2527 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2532 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2533 ret
= metadata_stream_check_version(stream
);
2537 if (stream
->chan
->metadata_cache
->max_offset
2538 == stream
->ust_metadata_pushed
) {
2543 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2544 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2545 stream
->chan
->metadata_cache
->max_offset
2546 - stream
->ust_metadata_pushed
);
2547 assert(write_len
!= 0);
2548 if (write_len
< 0) {
2549 ERR("Writing one metadata packet");
2553 stream
->ust_metadata_pushed
+= write_len
;
2555 assert(stream
->chan
->metadata_cache
->max_offset
>=
2556 stream
->ust_metadata_pushed
);
2560 * Switch packet (but don't open the next one) on every commit of
2561 * a metadata packet. Since the subbuffer is fully filled (with padding,
2562 * if needed), the stream is "quiescent" after this commit.
2564 ustctl_flush_buffer(stream
->ustream
, 1);
2565 stream
->quiescent
= true;
2567 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2573 * Sync metadata meaning request them to the session daemon and snapshot to the
2574 * metadata thread can consumer them.
2576 * Metadata stream lock is held here, but we need to release it when
2577 * interacting with sessiond, else we cause a deadlock with live
2578 * awaiting on metadata to be pushed out.
2580 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
2581 * is empty or a negative value on error.
2583 int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2584 struct lttng_consumer_stream
*metadata
)
2592 pthread_mutex_unlock(&metadata
->lock
);
2594 * Request metadata from the sessiond, but don't wait for the flush
2595 * because we locked the metadata thread.
2597 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata
->chan
, 0, 0);
2598 pthread_mutex_lock(&metadata
->lock
);
2603 ret
= commit_one_metadata_packet(metadata
);
2606 } else if (ret
> 0) {
2610 ret
= ustctl_snapshot(metadata
->ustream
);
2612 if (errno
!= EAGAIN
) {
2613 ERR("Sync metadata, taking UST snapshot");
2616 DBG("No new metadata when syncing them.");
2617 /* No new metadata, exit. */
2623 * After this flush, we still need to extract metadata.
2634 * Return 0 on success else a negative value.
2636 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2637 struct lttng_consumer_local_data
*ctx
)
2640 struct ustctl_consumer_stream
*ustream
;
2645 ustream
= stream
->ustream
;
2648 * First, we are going to check if there is a new subbuffer available
2649 * before reading the stream wait_fd.
2651 /* Get the next subbuffer */
2652 ret
= ustctl_get_next_subbuf(ustream
);
2654 /* No more data found, flag the stream. */
2655 stream
->has_data
= 0;
2660 ret
= ustctl_put_subbuf(ustream
);
2663 /* This stream still has data. Flag it and wake up the data thread. */
2664 stream
->has_data
= 1;
2666 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2669 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2670 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2675 /* The wake up pipe has been notified. */
2676 ctx
->has_wakeup
= 1;
2685 int update_stream_stats(struct lttng_consumer_stream
*stream
)
2688 uint64_t seq
, discarded
;
2690 ret
= ustctl_get_sequence_number(stream
->ustream
, &seq
);
2692 PERROR("ustctl_get_sequence_number");
2696 * Start the sequence when we extract the first packet in case we don't
2697 * start at 0 (for example if a consumer is not connected to the
2698 * session immediately after the beginning).
2700 if (stream
->last_sequence_number
== -1ULL) {
2701 stream
->last_sequence_number
= seq
;
2702 } else if (seq
> stream
->last_sequence_number
) {
2703 stream
->chan
->lost_packets
+= seq
-
2704 stream
->last_sequence_number
- 1;
2706 /* seq <= last_sequence_number */
2707 ERR("Sequence number inconsistent : prev = %" PRIu64
2708 ", current = %" PRIu64
,
2709 stream
->last_sequence_number
, seq
);
2713 stream
->last_sequence_number
= seq
;
2715 ret
= ustctl_get_events_discarded(stream
->ustream
, &discarded
);
2717 PERROR("kernctl_get_events_discarded");
2720 if (discarded
< stream
->last_discarded_events
) {
2722 * Overflow has occurred. We assume only one wrap-around
2725 stream
->chan
->discarded_events
+=
2726 (1ULL << (CAA_BITS_PER_LONG
- 1)) -
2727 stream
->last_discarded_events
+ discarded
;
2729 stream
->chan
->discarded_events
+= discarded
-
2730 stream
->last_discarded_events
;
2732 stream
->last_discarded_events
= discarded
;
2740 * Read subbuffer from the given stream.
2742 * Stream and channel locks MUST be acquired by the caller.
2744 * Return 0 on success else a negative value.
2746 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2747 struct lttng_consumer_local_data
*ctx
)
2749 unsigned long len
, subbuf_size
, padding
;
2750 int err
, write_index
= 1, rotation_ret
;
2752 struct ustctl_consumer_stream
*ustream
;
2753 struct ctf_packet_index index
;
2756 assert(stream
->ustream
);
2759 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream
->wait_fd
,
2762 /* Ease our life for what's next. */
2763 ustream
= stream
->ustream
;
2766 * We can consume the 1 byte written into the wait_fd by UST. Don't trigger
2767 * error if we cannot read this one byte (read returns 0), or if the error
2768 * is EAGAIN or EWOULDBLOCK.
2770 * This is only done when the stream is monitored by a thread, before the
2771 * flush is done after a hangup and if the stream is not flagged with data
2772 * since there might be nothing to consume in the wait fd but still have
2773 * data available flagged by the consumer wake up pipe.
2775 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2779 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2780 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2787 * If the stream was flagged to be ready for rotation before we extract the
2788 * next packet, rotate it now.
2790 if (stream
->rotate_ready
) {
2791 DBG("Rotate stream before extracting data");
2792 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
2793 if (rotation_ret
< 0) {
2794 ERR("Stream rotation error");
2801 /* Get the next subbuffer */
2802 err
= ustctl_get_next_subbuf(ustream
);
2805 * Populate metadata info if the existing info has
2806 * already been read.
2808 if (stream
->metadata_flag
) {
2809 ret
= commit_one_metadata_packet(stream
);
2816 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
2818 * This is a debug message even for single-threaded consumer,
2819 * because poll() have more relaxed criterions than get subbuf,
2820 * so get_subbuf may fail for short race windows where poll()
2821 * would issue wakeups.
2823 DBG("Reserving sub buffer failed (everything is normal, "
2824 "it is due to concurrency) [ret: %d]", err
);
2827 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
2829 if (!stream
->metadata_flag
) {
2830 index
.offset
= htobe64(stream
->out_fd_offset
);
2831 ret
= get_index_values(&index
, ustream
);
2833 err
= ustctl_put_subbuf(ustream
);
2838 /* Update the stream's sequence and discarded events count. */
2839 ret
= update_stream_stats(stream
);
2841 PERROR("kernctl_get_events_discarded");
2842 err
= ustctl_put_subbuf(ustream
);
2850 /* Get the full padded subbuffer size */
2851 err
= ustctl_get_padded_subbuf_size(ustream
, &len
);
2854 /* Get subbuffer data size (without padding) */
2855 err
= ustctl_get_subbuf_size(ustream
, &subbuf_size
);
2858 /* Make sure we don't get a subbuffer size bigger than the padded */
2859 assert(len
>= subbuf_size
);
2861 padding
= len
- subbuf_size
;
2863 /* write the subbuffer to the tracefile */
2864 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
, padding
, &index
);
2866 * The mmap operation should write subbuf_size amount of data when network
2867 * streaming or the full padding (len) size when we are _not_ streaming.
2869 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
2870 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
2872 * Display the error but continue processing to try to release the
2873 * subbuffer. This is a DBG statement since any unexpected kill or
2874 * signal, the application gets unregistered, relayd gets closed or
2875 * anything that affects the buffer lifetime will trigger this error.
2876 * So, for the sake of the user, don't print this error since it can
2877 * happen and it is OK with the code flow.
2879 DBG("Error writing to tracefile "
2880 "(ret: %ld != len: %lu != subbuf_size: %lu)",
2881 ret
, len
, subbuf_size
);
2884 err
= ustctl_put_next_subbuf(ustream
);
2888 * This will consumer the byte on the wait_fd if and only if there is not
2889 * next subbuffer to be acquired.
2891 if (!stream
->metadata_flag
) {
2892 ret
= notify_if_more_data(stream
, ctx
);
2898 /* Write index if needed. */
2903 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
2905 * In live, block until all the metadata is sent.
2907 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2908 assert(!stream
->missed_metadata_flush
);
2909 stream
->waiting_on_metadata
= true;
2910 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2912 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
2914 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2915 stream
->waiting_on_metadata
= false;
2916 if (stream
->missed_metadata_flush
) {
2917 stream
->missed_metadata_flush
= false;
2918 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2919 (void) consumer_flush_ust_index(stream
);
2921 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2929 assert(!stream
->metadata_flag
);
2930 err
= consumer_stream_write_index(stream
, &index
);
2937 * After extracting the packet, we check if the stream is now ready to be
2938 * rotated and perform the action immediately.
2940 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
2941 if (rotation_ret
== 1) {
2942 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
2943 if (rotation_ret
< 0) {
2944 ERR("Stream rotation error");
2948 } else if (rotation_ret
< 0) {
2949 ERR("Checking if stream is ready to rotate");
2958 * Called when a stream is created.
2960 * Return 0 on success or else a negative value.
2962 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2969 * Don't create anything if this is set for streaming or if there is
2970 * no current trace chunk on the parent channel.
2972 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
2973 stream
->chan
->trace_chunk
) {
2974 ret
= consumer_stream_create_output_files(stream
, true);
2986 * Check if data is still being extracted from the buffers for a specific
2987 * stream. Consumer data lock MUST be acquired before calling this function
2988 * and the stream lock.
2990 * Return 1 if the traced data are still getting read else 0 meaning that the
2991 * data is available for trace viewer reading.
2993 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
2998 assert(stream
->ustream
);
3000 DBG("UST consumer checking data pending");
3002 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3007 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3008 uint64_t contiguous
, pushed
;
3010 /* Ease our life a bit. */
3011 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
3012 pushed
= stream
->ust_metadata_pushed
;
3015 * We can simply check whether all contiguously available data
3016 * has been pushed to the ring buffer, since the push operation
3017 * is performed within get_next_subbuf(), and because both
3018 * get_next_subbuf() and put_next_subbuf() are issued atomically
3019 * thanks to the stream lock within
3020 * lttng_ustconsumer_read_subbuffer(). This basically means that
3021 * whetnever ust_metadata_pushed is incremented, the associated
3022 * metadata has been consumed from the metadata stream.
3024 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3025 contiguous
, pushed
);
3026 assert(((int64_t) (contiguous
- pushed
)) >= 0);
3027 if ((contiguous
!= pushed
) ||
3028 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3029 ret
= 1; /* Data is pending */
3033 ret
= ustctl_get_next_subbuf(stream
->ustream
);
3036 * There is still data so let's put back this
3039 ret
= ustctl_put_subbuf(stream
->ustream
);
3041 ret
= 1; /* Data is pending */
3046 /* Data is NOT pending so ready to be read. */
3054 * Stop a given metadata channel timer if enabled and close the wait fd which
3055 * is the poll pipe of the metadata stream.
3057 * This MUST be called with the metadata channel lock acquired.
3059 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3064 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3066 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3068 if (metadata
->switch_timer_enabled
== 1) {
3069 consumer_timer_switch_stop(metadata
);
3072 if (!metadata
->metadata_stream
) {
3077 * Closing write side so the thread monitoring the stream wakes up if any
3078 * and clean the metadata stream.
3080 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3081 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3083 PERROR("closing metadata pipe write side");
3085 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3093 * Close every metadata stream wait fd of the metadata hash table. This
3094 * function MUST be used very carefully so not to run into a race between the
3095 * metadata thread handling streams and this function closing their wait fd.
3097 * For UST, this is used when the session daemon hangs up. Its the metadata
3098 * producer so calling this is safe because we are assured that no state change
3099 * can occur in the metadata thread for the streams in the hash table.
3101 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3103 struct lttng_ht_iter iter
;
3104 struct lttng_consumer_stream
*stream
;
3106 assert(metadata_ht
);
3107 assert(metadata_ht
->ht
);
3109 DBG("UST consumer closing all metadata streams");
3112 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
3115 health_code_update();
3117 pthread_mutex_lock(&stream
->chan
->lock
);
3118 lttng_ustconsumer_close_metadata(stream
->chan
);
3119 pthread_mutex_unlock(&stream
->chan
->lock
);
3125 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3129 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
3131 ERR("Unable to close wakeup fd");
3136 * Please refer to consumer-timer.c before adding any lock within this
3137 * function or any of its callees. Timers have a very strict locking
3138 * semantic with respect to teardown. Failure to respect this semantic
3139 * introduces deadlocks.
3141 * DON'T hold the metadata lock when calling this function, else this
3142 * can cause deadlock involving consumer awaiting for metadata to be
3143 * pushed out due to concurrent interaction with the session daemon.
3145 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3146 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
3148 struct lttcomm_metadata_request_msg request
;
3149 struct lttcomm_consumer_msg msg
;
3150 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3151 uint64_t len
, key
, offset
, version
;
3155 assert(channel
->metadata_cache
);
3157 memset(&request
, 0, sizeof(request
));
3159 /* send the metadata request to sessiond */
3160 switch (consumer_data
.type
) {
3161 case LTTNG_CONSUMER64_UST
:
3162 request
.bits_per_long
= 64;
3164 case LTTNG_CONSUMER32_UST
:
3165 request
.bits_per_long
= 32;
3168 request
.bits_per_long
= 0;
3172 request
.session_id
= channel
->session_id
;
3173 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3175 * Request the application UID here so the metadata of that application can
3176 * be sent back. The channel UID corresponds to the user UID of the session
3177 * used for the rights on the stream file(s).
3179 request
.uid
= channel
->ust_app_uid
;
3180 request
.key
= channel
->key
;
3182 DBG("Sending metadata request to sessiond, session id %" PRIu64
3183 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
3184 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
3187 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3189 health_code_update();
3191 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
3194 ERR("Asking metadata to sessiond");
3198 health_code_update();
3200 /* Receive the metadata from sessiond */
3201 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
3203 if (ret
!= sizeof(msg
)) {
3204 DBG("Consumer received unexpected message size %d (expects %zu)",
3206 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3208 * The ret value might 0 meaning an orderly shutdown but this is ok
3209 * since the caller handles this.
3214 health_code_update();
3216 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3217 /* No registry found */
3218 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3222 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3223 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3228 len
= msg
.u
.push_metadata
.len
;
3229 key
= msg
.u
.push_metadata
.key
;
3230 offset
= msg
.u
.push_metadata
.target_offset
;
3231 version
= msg
.u
.push_metadata
.version
;
3233 assert(key
== channel
->key
);
3235 DBG("No new metadata to receive for key %" PRIu64
, key
);
3238 health_code_update();
3240 /* Tell session daemon we are ready to receive the metadata. */
3241 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3242 LTTCOMM_CONSUMERD_SUCCESS
);
3243 if (ret
< 0 || len
== 0) {
3245 * Somehow, the session daemon is not responding anymore or there is
3246 * nothing to receive.
3251 health_code_update();
3253 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3254 key
, offset
, len
, version
, channel
, timer
, wait
);
3257 * Only send the status msg if the sessiond is alive meaning a positive
3260 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3265 health_code_update();
3267 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3272 * Return the ustctl call for the get stream id.
3274 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3275 uint64_t *stream_id
)
3280 return ustctl_get_stream_id(stream
->ustream
, stream_id
);