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.
26 #include <common/common.h>
27 #include <common/index/index.h>
28 #include <common/relayd/relayd.h>
29 #include <common/ust-consumer/ust-consumer.h>
31 #include "consumer-stream.h"
34 * RCU call to free stream. MUST only be used with call_rcu().
36 static void free_stream_rcu(struct rcu_head
*head
)
38 struct lttng_ht_node_u64
*node
=
39 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
40 struct lttng_consumer_stream
*stream
=
41 caa_container_of(node
, struct lttng_consumer_stream
, node
);
43 pthread_mutex_destroy(&stream
->lock
);
48 * Close stream on the relayd side. This call can destroy a relayd if the
51 * A RCU read side lock MUST be acquired if the relayd object was looked up in
52 * a hash table before calling this.
54 void consumer_stream_relayd_close(struct lttng_consumer_stream
*stream
,
55 struct consumer_relayd_sock_pair
*relayd
)
62 if (stream
->sent_to_relayd
) {
63 uatomic_dec(&relayd
->refcount
);
64 assert(uatomic_read(&relayd
->refcount
) >= 0);
67 /* Closing streams requires to lock the control socket. */
68 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
69 ret
= relayd_send_close_stream(&relayd
->control_sock
,
70 stream
->relayd_stream_id
,
71 stream
->next_net_seq_num
- 1);
72 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
74 DBG("Unable to close stream on the relayd. Continuing");
76 * Continue here. There is nothing we can do for the relayd.
77 * Chances are that the relayd has closed the socket so we just
78 * continue cleaning up.
82 /* Both conditions are met, we destroy the relayd. */
83 if (uatomic_read(&relayd
->refcount
) == 0 &&
84 uatomic_read(&relayd
->destroy_flag
)) {
85 consumer_destroy_relayd(relayd
);
87 stream
->net_seq_idx
= (uint64_t) -1ULL;
88 stream
->sent_to_relayd
= 0;
92 * Close stream's file descriptors and, if needed, close stream also on the
95 * The consumer data lock MUST be acquired.
96 * The stream lock MUST be acquired.
98 void consumer_stream_close(struct lttng_consumer_stream
*stream
)
101 struct consumer_relayd_sock_pair
*relayd
;
105 switch (consumer_data
.type
) {
106 case LTTNG_CONSUMER_KERNEL
:
107 if (stream
->mmap_base
!= NULL
) {
108 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
114 if (stream
->wait_fd
>= 0) {
115 ret
= close(stream
->wait_fd
);
119 stream
->wait_fd
= -1;
122 case LTTNG_CONSUMER32_UST
:
123 case LTTNG_CONSUMER64_UST
:
126 ERR("Unknown consumer_data type");
130 /* Close output fd. Could be a socket or local file at this point. */
131 if (stream
->out_fd
>= 0) {
132 ret
= close(stream
->out_fd
);
139 if (stream
->index_fd
>= 0) {
140 ret
= close(stream
->index_fd
);
142 PERROR("close stream index_fd");
144 stream
->index_fd
= -1;
147 /* Check and cleanup relayd if needed. */
149 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
150 if (relayd
!= NULL
) {
151 consumer_stream_relayd_close(stream
, relayd
);
157 * Delete the stream from all possible hash tables.
159 * The consumer data lock MUST be acquired.
160 * The stream lock MUST be acquired.
162 void consumer_stream_delete(struct lttng_consumer_stream
*stream
,
166 struct lttng_ht_iter iter
;
169 /* Should NEVER be called not in monitor mode. */
170 assert(stream
->chan
->monitor
);
175 iter
.iter
.node
= &stream
->node
.node
;
176 ret
= lttng_ht_del(ht
, &iter
);
180 /* Delete from stream per channel ID hash table. */
181 iter
.iter
.node
= &stream
->node_channel_id
.node
;
183 * The returned value is of no importance. Even if the node is NOT in the
184 * hash table, we continue since we may have been called by a code path
185 * that did not add the stream to a (all) hash table. Same goes for the
186 * next call ht del call.
188 (void) lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
190 /* Delete from the global stream list. */
191 iter
.iter
.node
= &stream
->node_session_id
.node
;
192 /* See the previous ht del on why we ignore the returned value. */
193 (void) lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
197 /* Decrement the stream count of the global consumer data. */
198 assert(consumer_data
.stream_count
> 0);
199 consumer_data
.stream_count
--;
203 * Free the given stream within a RCU call.
205 void consumer_stream_free(struct lttng_consumer_stream
*stream
)
209 call_rcu(&stream
->node
.head
, free_stream_rcu
);
213 * Destroy the stream's buffers of the tracer.
215 void consumer_stream_destroy_buffers(struct lttng_consumer_stream
*stream
)
219 switch (consumer_data
.type
) {
220 case LTTNG_CONSUMER_KERNEL
:
222 case LTTNG_CONSUMER32_UST
:
223 case LTTNG_CONSUMER64_UST
:
224 lttng_ustconsumer_del_stream(stream
);
227 ERR("Unknown consumer_data type");
233 * Destroy and close a already created stream.
235 static void destroy_close_stream(struct lttng_consumer_stream
*stream
)
239 DBG("Consumer stream destroy monitored key: %" PRIu64
, stream
->key
);
241 /* Destroy tracer buffers of the stream. */
242 consumer_stream_destroy_buffers(stream
);
243 /* Close down everything including the relayd if one. */
244 consumer_stream_close(stream
);
248 * Decrement the stream's channel refcount and if down to 0, return the channel
249 * pointer so it can be destroyed by the caller or NULL if not.
251 static struct lttng_consumer_channel
*unref_channel(
252 struct lttng_consumer_stream
*stream
)
254 struct lttng_consumer_channel
*free_chan
= NULL
;
257 assert(stream
->chan
);
259 /* Update refcount of channel and see if we need to destroy it. */
260 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
261 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
262 free_chan
= stream
->chan
;
269 * Destroy a stream completely. This will delete, close and free the stream.
270 * Once return, the stream is NO longer usable. Its channel may get destroyed
271 * if conditions are met for a monitored stream.
273 * This MUST be called WITHOUT the consumer data and stream lock acquired if
274 * the stream is in _monitor_ mode else it does not matter.
276 void consumer_stream_destroy(struct lttng_consumer_stream
*stream
,
281 /* Stream is in monitor mode. */
282 if (stream
->monitor
) {
283 struct lttng_consumer_channel
*free_chan
= NULL
;
286 * This means that the stream was successfully removed from the streams
287 * list of the channel and sent to the right thread managing this
288 * stream thus being globally visible.
290 if (stream
->globally_visible
) {
291 pthread_mutex_lock(&consumer_data
.lock
);
292 pthread_mutex_lock(&stream
->chan
->lock
);
293 pthread_mutex_lock(&stream
->lock
);
294 /* Remove every reference of the stream in the consumer. */
295 consumer_stream_delete(stream
, ht
);
297 destroy_close_stream(stream
);
299 /* Update channel's refcount of the stream. */
300 free_chan
= unref_channel(stream
);
302 /* Indicates that the consumer data state MUST be updated after this. */
303 consumer_data
.need_update
= 1;
305 pthread_mutex_unlock(&stream
->lock
);
306 pthread_mutex_unlock(&stream
->chan
->lock
);
307 pthread_mutex_unlock(&consumer_data
.lock
);
310 * If the stream is not visible globally, this needs to be done
311 * outside of the consumer data lock section.
313 free_chan
= unref_channel(stream
);
317 consumer_del_channel(free_chan
);
320 destroy_close_stream(stream
);
323 /* Free stream within a RCU call. */
324 consumer_stream_free(stream
);
328 * Write index of a specific stream either on the relayd or local disk.
330 * Return 0 on success or else a negative value.
332 int consumer_stream_write_index(struct lttng_consumer_stream
*stream
,
333 struct lttng_packet_index
*index
)
336 struct consumer_relayd_sock_pair
*relayd
;
342 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
344 ret
= relayd_send_index(&relayd
->control_sock
, index
,
345 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
347 ret
= index_write(stream
->index_fd
, index
,
348 sizeof(struct lttng_packet_index
));