2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
16 #include <common/common.h>
17 #include <common/index/index.h>
18 #include <common/kernel-consumer/kernel-consumer.h>
19 #include <common/relayd/relayd.h>
20 #include <common/ust-consumer/ust-consumer.h>
21 #include <common/utils.h>
23 #include "consumer-stream.h"
26 * RCU call to free stream. MUST only be used with call_rcu().
28 static void free_stream_rcu(struct rcu_head
*head
)
30 struct lttng_ht_node_u64
*node
=
31 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
32 struct lttng_consumer_stream
*stream
=
33 caa_container_of(node
, struct lttng_consumer_stream
, node
);
35 pthread_mutex_destroy(&stream
->lock
);
40 * Close stream on the relayd side. This call can destroy a relayd if the
43 * A RCU read side lock MUST be acquired if the relayd object was looked up in
44 * a hash table before calling this.
46 void consumer_stream_relayd_close(struct lttng_consumer_stream
*stream
,
47 struct consumer_relayd_sock_pair
*relayd
)
54 if (stream
->sent_to_relayd
) {
55 uatomic_dec(&relayd
->refcount
);
56 assert(uatomic_read(&relayd
->refcount
) >= 0);
59 /* Closing streams requires to lock the control socket. */
60 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
61 ret
= relayd_send_close_stream(&relayd
->control_sock
,
62 stream
->relayd_stream_id
,
63 stream
->next_net_seq_num
- 1);
64 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
66 ERR("Relayd send close stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
67 lttng_consumer_cleanup_relayd(relayd
);
70 /* Both conditions are met, we destroy the relayd. */
71 if (uatomic_read(&relayd
->refcount
) == 0 &&
72 uatomic_read(&relayd
->destroy_flag
)) {
73 consumer_destroy_relayd(relayd
);
75 stream
->net_seq_idx
= (uint64_t) -1ULL;
76 stream
->sent_to_relayd
= 0;
80 * Close stream's file descriptors and, if needed, close stream also on the
83 * The consumer data lock MUST be acquired.
84 * The stream lock MUST be acquired.
86 void consumer_stream_close(struct lttng_consumer_stream
*stream
)
89 struct consumer_relayd_sock_pair
*relayd
;
93 switch (consumer_data
.type
) {
94 case LTTNG_CONSUMER_KERNEL
:
95 if (stream
->mmap_base
!= NULL
) {
96 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
102 if (stream
->wait_fd
>= 0) {
103 ret
= close(stream
->wait_fd
);
107 stream
->wait_fd
= -1;
109 if (stream
->chan
->output
== CONSUMER_CHANNEL_SPLICE
) {
110 utils_close_pipe(stream
->splice_pipe
);
113 case LTTNG_CONSUMER32_UST
:
114 case LTTNG_CONSUMER64_UST
:
117 * Special case for the metadata since the wait fd is an internal pipe
118 * polled in the metadata thread.
120 if (stream
->metadata_flag
&& stream
->chan
->monitor
) {
121 int rpipe
= stream
->ust_metadata_poll_pipe
[0];
124 * This will stop the channel timer if one and close the write side
125 * of the metadata poll pipe.
127 lttng_ustconsumer_close_metadata(stream
->chan
);
131 PERROR("closing metadata pipe read side");
133 stream
->ust_metadata_poll_pipe
[0] = -1;
139 ERR("Unknown consumer_data type");
143 /* Close output fd. Could be a socket or local file at this point. */
144 if (stream
->out_fd
>= 0) {
145 ret
= close(stream
->out_fd
);
152 if (stream
->index_file
) {
153 lttng_index_file_put(stream
->index_file
);
154 stream
->index_file
= NULL
;
157 lttng_trace_chunk_put(stream
->trace_chunk
);
158 stream
->trace_chunk
= NULL
;
160 /* Check and cleanup relayd if needed. */
162 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
163 if (relayd
!= NULL
) {
164 consumer_stream_relayd_close(stream
, relayd
);
170 * Delete the stream from all possible hash tables.
172 * The consumer data lock MUST be acquired.
173 * The stream lock MUST be acquired.
175 void consumer_stream_delete(struct lttng_consumer_stream
*stream
,
179 struct lttng_ht_iter iter
;
182 /* Should NEVER be called not in monitor mode. */
183 assert(stream
->chan
->monitor
);
188 iter
.iter
.node
= &stream
->node
.node
;
189 ret
= lttng_ht_del(ht
, &iter
);
193 /* Delete from stream per channel ID hash table. */
194 iter
.iter
.node
= &stream
->node_channel_id
.node
;
196 * The returned value is of no importance. Even if the node is NOT in the
197 * hash table, we continue since we may have been called by a code path
198 * that did not add the stream to a (all) hash table. Same goes for the
199 * next call ht del call.
201 (void) lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
203 /* Delete from the global stream list. */
204 iter
.iter
.node
= &stream
->node_session_id
.node
;
205 /* See the previous ht del on why we ignore the returned value. */
206 (void) lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
210 if (!stream
->metadata_flag
) {
211 /* Decrement the stream count of the global consumer data. */
212 assert(consumer_data
.stream_count
> 0);
213 consumer_data
.stream_count
--;
218 * Free the given stream within a RCU call.
220 void consumer_stream_free(struct lttng_consumer_stream
*stream
)
224 call_rcu(&stream
->node
.head
, free_stream_rcu
);
228 * Destroy the stream's buffers of the tracer.
230 void consumer_stream_destroy_buffers(struct lttng_consumer_stream
*stream
)
234 switch (consumer_data
.type
) {
235 case LTTNG_CONSUMER_KERNEL
:
237 case LTTNG_CONSUMER32_UST
:
238 case LTTNG_CONSUMER64_UST
:
239 lttng_ustconsumer_del_stream(stream
);
242 ERR("Unknown consumer_data type");
248 * Destroy and close a already created stream.
250 static void destroy_close_stream(struct lttng_consumer_stream
*stream
)
254 DBG("Consumer stream destroy monitored key: %" PRIu64
, stream
->key
);
256 /* Destroy tracer buffers of the stream. */
257 consumer_stream_destroy_buffers(stream
);
258 /* Close down everything including the relayd if one. */
259 consumer_stream_close(stream
);
263 * Decrement the stream's channel refcount and if down to 0, return the channel
264 * pointer so it can be destroyed by the caller or NULL if not.
266 static struct lttng_consumer_channel
*unref_channel(
267 struct lttng_consumer_stream
*stream
)
269 struct lttng_consumer_channel
*free_chan
= NULL
;
272 assert(stream
->chan
);
274 /* Update refcount of channel and see if we need to destroy it. */
275 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
276 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
277 free_chan
= stream
->chan
;
284 * Destroy a stream completely. This will delete, close and free the stream.
285 * Once return, the stream is NO longer usable. Its channel may get destroyed
286 * if conditions are met for a monitored stream.
288 * This MUST be called WITHOUT the consumer data and stream lock acquired if
289 * the stream is in _monitor_ mode else it does not matter.
291 void consumer_stream_destroy(struct lttng_consumer_stream
*stream
,
296 /* Stream is in monitor mode. */
297 if (stream
->monitor
) {
298 struct lttng_consumer_channel
*free_chan
= NULL
;
301 * This means that the stream was successfully removed from the streams
302 * list of the channel and sent to the right thread managing this
303 * stream thus being globally visible.
305 if (stream
->globally_visible
) {
306 pthread_mutex_lock(&consumer_data
.lock
);
307 pthread_mutex_lock(&stream
->chan
->lock
);
308 pthread_mutex_lock(&stream
->lock
);
309 /* Remove every reference of the stream in the consumer. */
310 consumer_stream_delete(stream
, ht
);
312 destroy_close_stream(stream
);
314 /* Update channel's refcount of the stream. */
315 free_chan
= unref_channel(stream
);
317 /* Indicates that the consumer data state MUST be updated after this. */
318 consumer_data
.need_update
= 1;
320 pthread_mutex_unlock(&stream
->lock
);
321 pthread_mutex_unlock(&stream
->chan
->lock
);
322 pthread_mutex_unlock(&consumer_data
.lock
);
325 * If the stream is not visible globally, this needs to be done
326 * outside of the consumer data lock section.
328 free_chan
= unref_channel(stream
);
332 consumer_del_channel(free_chan
);
335 destroy_close_stream(stream
);
338 /* Free stream within a RCU call. */
339 lttng_trace_chunk_put(stream
->trace_chunk
);
340 stream
->trace_chunk
= NULL
;
341 consumer_stream_free(stream
);
345 * Write index of a specific stream either on the relayd or local disk.
347 * Return 0 on success or else a negative value.
349 int consumer_stream_write_index(struct lttng_consumer_stream
*stream
,
350 struct ctf_packet_index
*element
)
358 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
359 struct consumer_relayd_sock_pair
*relayd
;
360 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
362 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
363 ret
= relayd_send_index(&relayd
->control_sock
, element
,
364 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
367 * Communication error with lttng-relayd,
368 * perform cleanup now
370 ERR("Relayd send index failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
371 lttng_consumer_cleanup_relayd(relayd
);
374 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
376 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't write index.",
377 stream
->key
, stream
->net_seq_idx
);
381 if (lttng_index_file_write(stream
->index_file
, element
)) {
397 * Actually do the metadata sync using the given metadata stream.
399 * Return 0 on success else a negative value. ENODATA can be returned also
400 * indicating that there is no metadata available for that stream.
402 static int do_sync_metadata(struct lttng_consumer_stream
*metadata
,
403 struct lttng_consumer_local_data
*ctx
)
408 assert(metadata
->metadata_flag
);
412 * In UST, since we have to write the metadata from the cache packet
413 * by packet, we might need to start this procedure multiple times
414 * until all the metadata from the cache has been extracted.
419 * - Lock the metadata stream
420 * - Check if metadata stream node was deleted before locking.
421 * - if yes, release and return success
422 * - Check if new metadata is ready (flush + snapshot pos)
423 * - If nothing : release and return.
424 * - Lock the metadata_rdv_lock
425 * - Unlock the metadata stream
426 * - cond_wait on metadata_rdv to wait the wakeup from the
428 * - Unlock the metadata_rdv_lock
430 pthread_mutex_lock(&metadata
->lock
);
433 * There is a possibility that we were able to acquire a reference on the
434 * stream from the RCU hash table but between then and now, the node might
435 * have been deleted just before the lock is acquired. Thus, after locking,
436 * we make sure the metadata node has not been deleted which means that the
437 * buffers are closed.
439 * In that case, there is no need to sync the metadata hence returning a
440 * success return code.
442 ret
= cds_lfht_is_node_deleted(&metadata
->node
.node
);
445 goto end_unlock_mutex
;
449 case LTTNG_CONSUMER_KERNEL
:
451 * Empty the metadata cache and flush the current stream.
453 ret
= lttng_kconsumer_sync_metadata(metadata
);
455 case LTTNG_CONSUMER32_UST
:
456 case LTTNG_CONSUMER64_UST
:
458 * Ask the sessiond if we have new metadata waiting and update the
459 * consumer metadata cache.
461 ret
= lttng_ustconsumer_sync_metadata(ctx
, metadata
);
469 * Error or no new metadata, we exit here.
471 if (ret
<= 0 || ret
== ENODATA
) {
472 goto end_unlock_mutex
;
476 * At this point, new metadata have been flushed, so we wait on the
477 * rendez-vous point for the metadata thread to wake us up when it
478 * finishes consuming the metadata and continue execution.
481 pthread_mutex_lock(&metadata
->metadata_rdv_lock
);
484 * Release metadata stream lock so the metadata thread can process it.
486 pthread_mutex_unlock(&metadata
->lock
);
489 * Wait on the rendez-vous point. Once woken up, it means the metadata was
490 * consumed and thus synchronization is achieved.
492 pthread_cond_wait(&metadata
->metadata_rdv
, &metadata
->metadata_rdv_lock
);
493 pthread_mutex_unlock(&metadata
->metadata_rdv_lock
);
494 } while (ret
== EAGAIN
);
500 pthread_mutex_unlock(&metadata
->lock
);
505 * Synchronize the metadata using a given session ID. A successful acquisition
506 * of a metadata stream will trigger a request to the session daemon and a
507 * snapshot so the metadata thread can consume it.
509 * This function call is a rendez-vous point between the metadata thread and
512 * Return 0 on success or else a negative value.
514 int consumer_stream_sync_metadata(struct lttng_consumer_local_data
*ctx
,
518 struct lttng_consumer_stream
*stream
= NULL
;
519 struct lttng_ht_iter iter
;
524 /* Ease our life a bit. */
525 ht
= consumer_data
.stream_list_ht
;
529 /* Search the metadata associated with the session id of the given stream. */
531 cds_lfht_for_each_entry_duplicate(ht
->ht
,
532 ht
->hash_fct(&session_id
, lttng_ht_seed
), ht
->match_fct
,
533 &session_id
, &iter
.iter
, stream
, node_session_id
.node
) {
534 if (!stream
->metadata_flag
) {
538 ret
= do_sync_metadata(stream
, ctx
);
545 * Force return code to 0 (success) since ret might be ENODATA for instance
546 * which is not an error but rather that we should come back.
555 int consumer_stream_create_output_files(struct lttng_consumer_stream
*stream
,
559 enum lttng_trace_chunk_status chunk_status
;
560 const int flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
561 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
562 char stream_path
[LTTNG_PATH_MAX
];
564 ASSERT_LOCKED(stream
->lock
);
565 assert(stream
->trace_chunk
);
567 ret
= utils_stream_file_path(stream
->chan
->pathname
, stream
->name
,
568 stream
->chan
->tracefile_size
,
569 stream
->tracefile_count_current
, NULL
,
570 stream_path
, sizeof(stream_path
));
575 if (stream
->out_fd
>= 0) {
576 ret
= close(stream
->out_fd
);
578 PERROR("Failed to close stream file \"%s\"",
585 DBG("Opening stream output file \"%s\"", stream_path
);
586 chunk_status
= lttng_trace_chunk_open_file(stream
->trace_chunk
, stream_path
,
587 flags
, mode
, &stream
->out_fd
, false);
588 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
589 ERR("Failed to open stream file \"%s\"", stream
->name
);
594 if (!stream
->metadata_flag
&& (create_index
|| stream
->index_file
)) {
595 if (stream
->index_file
) {
596 lttng_index_file_put(stream
->index_file
);
598 chunk_status
= lttng_index_file_create_from_trace_chunk(
600 stream
->chan
->pathname
,
602 stream
->chan
->tracefile_size
,
603 stream
->tracefile_count_current
,
604 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
,
605 false, &stream
->index_file
);
606 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
612 /* Reset current size because we just perform a rotation. */
613 stream
->tracefile_size_current
= 0;
614 stream
->out_fd_offset
= 0;
619 int consumer_stream_rotate_output_files(struct lttng_consumer_stream
*stream
)
623 stream
->tracefile_count_current
++;
624 if (stream
->chan
->tracefile_count
> 0) {
625 stream
->tracefile_count_current
%=
626 stream
->chan
->tracefile_count
;
629 DBG("Rotating output files of stream \"%s\"", stream
->name
);
630 ret
= consumer_stream_create_output_files(stream
, true);
639 bool consumer_stream_is_deleted(struct lttng_consumer_stream
*stream
)
642 * This function does not take a const stream since
643 * cds_lfht_is_node_deleted was not const before liburcu 0.12.
646 return cds_lfht_is_node_deleted(&stream
->node
.node
);