2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <common/common.h>
28 #include <common/index/index.h>
29 #include <common/kernel-consumer/kernel-consumer.h>
30 #include <common/relayd/relayd.h>
31 #include <common/ust-consumer/ust-consumer.h>
32 #include <common/utils.h>
34 #include "consumer-stream.h"
37 * RCU call to free stream. MUST only be used with call_rcu().
39 static void free_stream_rcu(struct rcu_head
*head
)
41 struct lttng_ht_node_u64
*node
=
42 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
43 struct lttng_consumer_stream
*stream
=
44 caa_container_of(node
, struct lttng_consumer_stream
, node
);
46 pthread_mutex_destroy(&stream
->lock
);
51 * Close stream on the relayd side. This call can destroy a relayd if the
54 * A RCU read side lock MUST be acquired if the relayd object was looked up in
55 * a hash table before calling this.
57 void consumer_stream_relayd_close(struct lttng_consumer_stream
*stream
,
58 struct consumer_relayd_sock_pair
*relayd
)
65 if (stream
->sent_to_relayd
) {
66 uatomic_dec(&relayd
->refcount
);
67 assert(uatomic_read(&relayd
->refcount
) >= 0);
70 /* Closing streams requires to lock the control socket. */
71 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
72 ret
= relayd_send_close_stream(&relayd
->control_sock
,
73 stream
->relayd_stream_id
,
74 stream
->next_net_seq_num
- 1);
75 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
77 DBG("Unable to close stream on the relayd. Continuing");
79 * Continue here. There is nothing we can do for the relayd.
80 * Chances are that the relayd has closed the socket so we just
81 * continue cleaning up.
85 /* Both conditions are met, we destroy the relayd. */
86 if (uatomic_read(&relayd
->refcount
) == 0 &&
87 uatomic_read(&relayd
->destroy_flag
)) {
88 consumer_destroy_relayd(relayd
);
90 stream
->net_seq_idx
= (uint64_t) -1ULL;
91 stream
->sent_to_relayd
= 0;
95 * Close stream's file descriptors and, if needed, close stream also on the
98 * The consumer data lock MUST be acquired.
99 * The stream lock MUST be acquired.
101 void consumer_stream_close(struct lttng_consumer_stream
*stream
)
104 struct consumer_relayd_sock_pair
*relayd
;
108 switch (consumer_data
.type
) {
109 case LTTNG_CONSUMER_KERNEL
:
110 if (stream
->mmap_base
!= NULL
) {
111 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
117 if (stream
->wait_fd
>= 0) {
118 ret
= close(stream
->wait_fd
);
122 stream
->wait_fd
= -1;
124 if (stream
->chan
->output
== CONSUMER_CHANNEL_SPLICE
) {
125 utils_close_pipe(stream
->splice_pipe
);
128 case LTTNG_CONSUMER32_UST
:
129 case LTTNG_CONSUMER64_UST
:
132 * Special case for the metadata since the wait fd is an internal pipe
133 * polled in the metadata thread.
135 if (stream
->metadata_flag
&& stream
->chan
->monitor
) {
136 int rpipe
= stream
->ust_metadata_poll_pipe
[0];
139 * This will stop the channel timer if one and close the write side
140 * of the metadata poll pipe.
142 lttng_ustconsumer_close_metadata(stream
->chan
);
146 PERROR("closing metadata pipe read side");
148 stream
->ust_metadata_poll_pipe
[0] = -1;
154 ERR("Unknown consumer_data type");
158 /* Close output fd. Could be a socket or local file at this point. */
159 if (stream
->out_fd
>= 0) {
160 ret
= close(stream
->out_fd
);
167 if (stream
->index_fd
>= 0) {
168 ret
= close(stream
->index_fd
);
170 PERROR("close stream index_fd");
172 stream
->index_fd
= -1;
175 /* Check and cleanup relayd if needed. */
177 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
178 if (relayd
!= NULL
) {
179 consumer_stream_relayd_close(stream
, relayd
);
185 * Delete the stream from all possible hash tables.
187 * The consumer data lock MUST be acquired.
188 * The stream lock MUST be acquired.
190 void consumer_stream_delete(struct lttng_consumer_stream
*stream
,
194 struct lttng_ht_iter iter
;
197 /* Should NEVER be called not in monitor mode. */
198 assert(stream
->chan
->monitor
);
203 iter
.iter
.node
= &stream
->node
.node
;
204 ret
= lttng_ht_del(ht
, &iter
);
208 /* Delete from stream per channel ID hash table. */
209 iter
.iter
.node
= &stream
->node_channel_id
.node
;
211 * The returned value is of no importance. Even if the node is NOT in the
212 * hash table, we continue since we may have been called by a code path
213 * that did not add the stream to a (all) hash table. Same goes for the
214 * next call ht del call.
216 (void) lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
218 /* Delete from the global stream list. */
219 iter
.iter
.node
= &stream
->node_session_id
.node
;
220 /* See the previous ht del on why we ignore the returned value. */
221 (void) lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
225 if (!stream
->metadata_flag
) {
226 /* Decrement the stream count of the global consumer data. */
227 assert(consumer_data
.stream_count
> 0);
228 consumer_data
.stream_count
--;
233 * Free the given stream within a RCU call.
235 void consumer_stream_free(struct lttng_consumer_stream
*stream
)
239 call_rcu(&stream
->node
.head
, free_stream_rcu
);
243 * Destroy the stream's buffers of the tracer.
245 void consumer_stream_destroy_buffers(struct lttng_consumer_stream
*stream
)
249 switch (consumer_data
.type
) {
250 case LTTNG_CONSUMER_KERNEL
:
252 case LTTNG_CONSUMER32_UST
:
253 case LTTNG_CONSUMER64_UST
:
254 lttng_ustconsumer_del_stream(stream
);
257 ERR("Unknown consumer_data type");
263 * Destroy and close a already created stream.
265 static void destroy_close_stream(struct lttng_consumer_stream
*stream
)
269 DBG("Consumer stream destroy monitored key: %" PRIu64
, stream
->key
);
271 /* Destroy tracer buffers of the stream. */
272 consumer_stream_destroy_buffers(stream
);
273 /* Close down everything including the relayd if one. */
274 consumer_stream_close(stream
);
278 * Decrement the stream's channel refcount and if down to 0, return the channel
279 * pointer so it can be destroyed by the caller or NULL if not.
281 static struct lttng_consumer_channel
*unref_channel(
282 struct lttng_consumer_stream
*stream
)
284 struct lttng_consumer_channel
*free_chan
= NULL
;
287 assert(stream
->chan
);
289 /* Update refcount of channel and see if we need to destroy it. */
290 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
291 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
292 free_chan
= stream
->chan
;
299 * Destroy a stream completely. This will delete, close and free the stream.
300 * Once return, the stream is NO longer usable. Its channel may get destroyed
301 * if conditions are met for a monitored stream.
303 * This MUST be called WITHOUT the consumer data and stream lock acquired if
304 * the stream is in _monitor_ mode else it does not matter.
306 void consumer_stream_destroy(struct lttng_consumer_stream
*stream
,
311 /* Stream is in monitor mode. */
312 if (stream
->monitor
) {
313 struct lttng_consumer_channel
*free_chan
= NULL
;
316 * This means that the stream was successfully removed from the streams
317 * list of the channel and sent to the right thread managing this
318 * stream thus being globally visible.
320 if (stream
->globally_visible
) {
321 pthread_mutex_lock(&consumer_data
.lock
);
322 pthread_mutex_lock(&stream
->chan
->lock
);
323 pthread_mutex_lock(&stream
->lock
);
324 /* Remove every reference of the stream in the consumer. */
325 consumer_stream_delete(stream
, ht
);
327 destroy_close_stream(stream
);
329 /* Update channel's refcount of the stream. */
330 free_chan
= unref_channel(stream
);
332 /* Indicates that the consumer data state MUST be updated after this. */
333 consumer_data
.need_update
= 1;
335 pthread_mutex_unlock(&stream
->lock
);
336 pthread_mutex_unlock(&stream
->chan
->lock
);
337 pthread_mutex_unlock(&consumer_data
.lock
);
340 * If the stream is not visible globally, this needs to be done
341 * outside of the consumer data lock section.
343 free_chan
= unref_channel(stream
);
347 consumer_del_channel(free_chan
);
350 destroy_close_stream(stream
);
353 /* Free stream within a RCU call. */
354 consumer_stream_free(stream
);
358 * Write index of a specific stream either on the relayd or local disk.
360 * Return 0 on success or else a negative value.
362 int consumer_stream_write_index(struct lttng_consumer_stream
*stream
,
363 struct ctf_packet_index
*index
)
366 struct consumer_relayd_sock_pair
*relayd
;
372 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
374 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
375 ret
= relayd_send_index(&relayd
->control_sock
, index
,
376 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
377 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
381 size_ret
= index_write(stream
->index_fd
, index
,
382 sizeof(struct ctf_packet_index
));
383 if (size_ret
< sizeof(struct ctf_packet_index
)) {
399 * Actually do the metadata sync using the given metadata stream.
401 * Return 0 on success else a negative value. ENODATA can be returned also
402 * indicating that there is no metadata available for that stream.
404 static int do_sync_metadata(struct lttng_consumer_stream
*metadata
,
405 struct lttng_consumer_local_data
*ctx
)
410 assert(metadata
->metadata_flag
);
414 * In UST, since we have to write the metadata from the cache packet
415 * by packet, we might need to start this procedure multiple times
416 * until all the metadata from the cache has been extracted.
421 * - Lock the metadata stream
422 * - Check if metadata stream node was deleted before locking.
423 * - if yes, release and return success
424 * - Check if new metadata is ready (flush + snapshot pos)
425 * - If nothing : release and return.
426 * - Lock the metadata_rdv_lock
427 * - Unlock the metadata stream
428 * - cond_wait on metadata_rdv to wait the wakeup from the
430 * - Unlock the metadata_rdv_lock
432 pthread_mutex_lock(&metadata
->lock
);
435 * There is a possibility that we were able to acquire a reference on the
436 * stream from the RCU hash table but between then and now, the node might
437 * have been deleted just before the lock is acquired. Thus, after locking,
438 * we make sure the metadata node has not been deleted which means that the
439 * buffers are closed.
441 * In that case, there is no need to sync the metadata hence returning a
442 * success return code.
444 ret
= cds_lfht_is_node_deleted(&metadata
->node
.node
);
447 goto end_unlock_mutex
;
451 case LTTNG_CONSUMER_KERNEL
:
453 * Empty the metadata cache and flush the current stream.
455 ret
= lttng_kconsumer_sync_metadata(metadata
);
457 case LTTNG_CONSUMER32_UST
:
458 case LTTNG_CONSUMER64_UST
:
460 * Ask the sessiond if we have new metadata waiting and update the
461 * consumer metadata cache.
463 ret
= lttng_ustconsumer_sync_metadata(ctx
, metadata
);
471 * Error or no new metadata, we exit here.
473 if (ret
<= 0 || ret
== ENODATA
) {
474 goto end_unlock_mutex
;
478 * At this point, new metadata have been flushed, so we wait on the
479 * rendez-vous point for the metadata thread to wake us up when it
480 * finishes consuming the metadata and continue execution.
483 pthread_mutex_lock(&metadata
->metadata_rdv_lock
);
486 * Release metadata stream lock so the metadata thread can process it.
488 pthread_mutex_unlock(&metadata
->lock
);
491 * Wait on the rendez-vous point. Once woken up, it means the metadata was
492 * consumed and thus synchronization is achieved.
494 pthread_cond_wait(&metadata
->metadata_rdv
, &metadata
->metadata_rdv_lock
);
495 pthread_mutex_unlock(&metadata
->metadata_rdv_lock
);
496 } while (ret
== EAGAIN
);
502 pthread_mutex_unlock(&metadata
->lock
);
507 * Synchronize the metadata using a given session ID. A successful acquisition
508 * of a metadata stream will trigger a request to the session daemon and a
509 * snapshot so the metadata thread can consume it.
511 * This function call is a rendez-vous point between the metadata thread and
514 * Return 0 on success or else a negative value.
516 int consumer_stream_sync_metadata(struct lttng_consumer_local_data
*ctx
,
520 struct lttng_consumer_stream
*stream
= NULL
;
521 struct lttng_ht_iter iter
;
526 /* Ease our life a bit. */
527 ht
= consumer_data
.stream_list_ht
;
531 /* Search the metadata associated with the session id of the given stream. */
533 cds_lfht_for_each_entry_duplicate(ht
->ht
,
534 ht
->hash_fct(&session_id
, lttng_ht_seed
), ht
->match_fct
,
535 &session_id
, &iter
.iter
, stream
, node_session_id
.node
) {
536 if (!stream
->metadata_flag
) {
540 ret
= do_sync_metadata(stream
, ctx
);
547 * Force return code to 0 (success) since ret might be ENODATA for instance
548 * which is not an error but rather that we should come back.