tests: Implement tests for lttng-ust LTTNG_UST_BLOCKING_RETRY_TIMEOUT
[lttng-tools.git] / src / common / consumer / consumer-stream.c
CommitLineData
51230d70
DG
1/*
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>
5 *
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.
9 *
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
13 * more details.
14 *
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.
18 */
19
6c1c0768 20#define _LGPL_SOURCE
51230d70 21#include <assert.h>
10a50311 22#include <inttypes.h>
51230d70
DG
23#include <sys/mman.h>
24#include <unistd.h>
25
26#include <common/common.h>
1c20f0e2 27#include <common/index/index.h>
94d49140 28#include <common/kernel-consumer/kernel-consumer.h>
51230d70
DG
29#include <common/relayd/relayd.h>
30#include <common/ust-consumer/ust-consumer.h>
a2361a61 31#include <common/utils.h>
51230d70
DG
32
33#include "consumer-stream.h"
34
35/*
36 * RCU call to free stream. MUST only be used with call_rcu().
37 */
38static void free_stream_rcu(struct rcu_head *head)
39{
40 struct lttng_ht_node_u64 *node =
41 caa_container_of(head, struct lttng_ht_node_u64, head);
42 struct lttng_consumer_stream *stream =
43 caa_container_of(node, struct lttng_consumer_stream, node);
44
45 pthread_mutex_destroy(&stream->lock);
46 free(stream);
47}
48
49/*
50 * Close stream on the relayd side. This call can destroy a relayd if the
51 * conditions are met.
52 *
53 * A RCU read side lock MUST be acquired if the relayd object was looked up in
54 * a hash table before calling this.
55 */
56void consumer_stream_relayd_close(struct lttng_consumer_stream *stream,
57 struct consumer_relayd_sock_pair *relayd)
58{
59 int ret;
60
61 assert(stream);
62 assert(relayd);
63
d01178b6
DG
64 if (stream->sent_to_relayd) {
65 uatomic_dec(&relayd->refcount);
66 assert(uatomic_read(&relayd->refcount) >= 0);
67 }
51230d70
DG
68
69 /* Closing streams requires to lock the control socket. */
70 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
71 ret = relayd_send_close_stream(&relayd->control_sock,
72 stream->relayd_stream_id,
73 stream->next_net_seq_num - 1);
74 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
75 if (ret < 0) {
76 DBG("Unable to close stream on the relayd. Continuing");
77 /*
78 * Continue here. There is nothing we can do for the relayd.
79 * Chances are that the relayd has closed the socket so we just
80 * continue cleaning up.
81 */
82 }
83
84 /* Both conditions are met, we destroy the relayd. */
85 if (uatomic_read(&relayd->refcount) == 0 &&
86 uatomic_read(&relayd->destroy_flag)) {
87 consumer_destroy_relayd(relayd);
88 }
10a50311 89 stream->net_seq_idx = (uint64_t) -1ULL;
d01178b6 90 stream->sent_to_relayd = 0;
51230d70
DG
91}
92
93/*
94 * Close stream's file descriptors and, if needed, close stream also on the
95 * relayd side.
96 *
97 * The consumer data lock MUST be acquired.
98 * The stream lock MUST be acquired.
99 */
100void consumer_stream_close(struct lttng_consumer_stream *stream)
101{
102 int ret;
103 struct consumer_relayd_sock_pair *relayd;
104
105 assert(stream);
106
107 switch (consumer_data.type) {
108 case LTTNG_CONSUMER_KERNEL:
109 if (stream->mmap_base != NULL) {
110 ret = munmap(stream->mmap_base, stream->mmap_len);
111 if (ret != 0) {
112 PERROR("munmap");
113 }
114 }
115
116 if (stream->wait_fd >= 0) {
117 ret = close(stream->wait_fd);
118 if (ret) {
119 PERROR("close");
120 }
10a50311 121 stream->wait_fd = -1;
51230d70 122 }
a2361a61
JD
123 if (stream->chan->output == CONSUMER_CHANNEL_SPLICE) {
124 utils_close_pipe(stream->splice_pipe);
125 }
51230d70
DG
126 break;
127 case LTTNG_CONSUMER32_UST:
128 case LTTNG_CONSUMER64_UST:
6d574024
DG
129 {
130 /*
131 * Special case for the metadata since the wait fd is an internal pipe
132 * polled in the metadata thread.
133 */
134 if (stream->metadata_flag && stream->chan->monitor) {
135 int rpipe = stream->ust_metadata_poll_pipe[0];
136
137 /*
138 * This will stop the channel timer if one and close the write side
139 * of the metadata poll pipe.
140 */
141 lttng_ustconsumer_close_metadata(stream->chan);
142 if (rpipe >= 0) {
143 ret = close(rpipe);
144 if (ret < 0) {
b4a650f3 145 PERROR("closing metadata pipe read side");
6d574024
DG
146 }
147 stream->ust_metadata_poll_pipe[0] = -1;
148 }
149 }
51230d70 150 break;
6d574024 151 }
51230d70
DG
152 default:
153 ERR("Unknown consumer_data type");
154 assert(0);
155 }
156
157 /* Close output fd. Could be a socket or local file at this point. */
158 if (stream->out_fd >= 0) {
159 ret = close(stream->out_fd);
160 if (ret) {
161 PERROR("close");
162 }
10a50311 163 stream->out_fd = -1;
51230d70
DG
164 }
165
309167d2
JD
166 if (stream->index_fd >= 0) {
167 ret = close(stream->index_fd);
168 if (ret) {
169 PERROR("close stream index_fd");
170 }
171 stream->index_fd = -1;
172 }
173
51230d70
DG
174 /* Check and cleanup relayd if needed. */
175 rcu_read_lock();
176 relayd = consumer_find_relayd(stream->net_seq_idx);
177 if (relayd != NULL) {
178 consumer_stream_relayd_close(stream, relayd);
179 }
180 rcu_read_unlock();
181}
182
183/*
184 * Delete the stream from all possible hash tables.
185 *
186 * The consumer data lock MUST be acquired.
187 * The stream lock MUST be acquired.
188 */
189void consumer_stream_delete(struct lttng_consumer_stream *stream,
190 struct lttng_ht *ht)
191{
192 int ret;
193 struct lttng_ht_iter iter;
194
195 assert(stream);
10a50311
JD
196 /* Should NEVER be called not in monitor mode. */
197 assert(stream->chan->monitor);
51230d70
DG
198
199 rcu_read_lock();
200
201 if (ht) {
202 iter.iter.node = &stream->node.node;
203 ret = lttng_ht_del(ht, &iter);
204 assert(!ret);
205 }
206
207 /* Delete from stream per channel ID hash table. */
208 iter.iter.node = &stream->node_channel_id.node;
209 /*
210 * The returned value is of no importance. Even if the node is NOT in the
211 * hash table, we continue since we may have been called by a code path
212 * that did not add the stream to a (all) hash table. Same goes for the
213 * next call ht del call.
214 */
215 (void) lttng_ht_del(consumer_data.stream_per_chan_id_ht, &iter);
216
217 /* Delete from the global stream list. */
218 iter.iter.node = &stream->node_session_id.node;
219 /* See the previous ht del on why we ignore the returned value. */
220 (void) lttng_ht_del(consumer_data.stream_list_ht, &iter);
221
222 rcu_read_unlock();
223
6d574024
DG
224 if (!stream->metadata_flag) {
225 /* Decrement the stream count of the global consumer data. */
226 assert(consumer_data.stream_count > 0);
227 consumer_data.stream_count--;
228 }
51230d70
DG
229}
230
231/*
232 * Free the given stream within a RCU call.
233 */
234void consumer_stream_free(struct lttng_consumer_stream *stream)
235{
236 assert(stream);
237
238 call_rcu(&stream->node.head, free_stream_rcu);
239}
240
241/*
10a50311 242 * Destroy the stream's buffers of the tracer.
51230d70 243 */
10a50311 244void consumer_stream_destroy_buffers(struct lttng_consumer_stream *stream)
51230d70 245{
10a50311
JD
246 assert(stream);
247
248 switch (consumer_data.type) {
249 case LTTNG_CONSUMER_KERNEL:
250 break;
251 case LTTNG_CONSUMER32_UST:
252 case LTTNG_CONSUMER64_UST:
253 lttng_ustconsumer_del_stream(stream);
254 break;
255 default:
256 ERR("Unknown consumer_data type");
257 assert(0);
258 }
259}
51230d70 260
10a50311 261/*
4891ece8 262 * Destroy and close a already created stream.
10a50311 263 */
4891ece8 264static void destroy_close_stream(struct lttng_consumer_stream *stream)
10a50311 265{
51230d70
DG
266 assert(stream);
267
4891ece8 268 DBG("Consumer stream destroy monitored key: %" PRIu64, stream->key);
10a50311
JD
269
270 /* Destroy tracer buffers of the stream. */
271 consumer_stream_destroy_buffers(stream);
272 /* Close down everything including the relayd if one. */
273 consumer_stream_close(stream);
274}
51230d70 275
10a50311 276/*
4891ece8
DG
277 * Decrement the stream's channel refcount and if down to 0, return the channel
278 * pointer so it can be destroyed by the caller or NULL if not.
10a50311 279 */
4891ece8
DG
280static struct lttng_consumer_channel *unref_channel(
281 struct lttng_consumer_stream *stream)
10a50311 282{
4891ece8
DG
283 struct lttng_consumer_channel *free_chan = NULL;
284
10a50311 285 assert(stream);
4891ece8 286 assert(stream->chan);
10a50311 287
4891ece8
DG
288 /* Update refcount of channel and see if we need to destroy it. */
289 if (!uatomic_sub_return(&stream->chan->refcount, 1)
290 && !uatomic_read(&stream->chan->nb_init_stream_left)) {
291 free_chan = stream->chan;
292 }
51230d70 293
4891ece8 294 return free_chan;
10a50311 295}
51230d70 296
10a50311
JD
297/*
298 * Destroy a stream completely. This will delete, close and free the stream.
299 * Once return, the stream is NO longer usable. Its channel may get destroyed
300 * if conditions are met for a monitored stream.
301 *
302 * This MUST be called WITHOUT the consumer data and stream lock acquired if
303 * the stream is in _monitor_ mode else it does not matter.
304 */
305void consumer_stream_destroy(struct lttng_consumer_stream *stream,
306 struct lttng_ht *ht)
307{
308 assert(stream);
309
310 /* Stream is in monitor mode. */
4891ece8 311 if (stream->monitor) {
10a50311 312 struct lttng_consumer_channel *free_chan = NULL;
51230d70 313
4891ece8
DG
314 /*
315 * This means that the stream was successfully removed from the streams
316 * list of the channel and sent to the right thread managing this
317 * stream thus being globally visible.
318 */
319 if (stream->globally_visible) {
320 pthread_mutex_lock(&consumer_data.lock);
a9838785 321 pthread_mutex_lock(&stream->chan->lock);
4891ece8
DG
322 pthread_mutex_lock(&stream->lock);
323 /* Remove every reference of the stream in the consumer. */
324 consumer_stream_delete(stream, ht);
325
326 destroy_close_stream(stream);
327
328 /* Update channel's refcount of the stream. */
329 free_chan = unref_channel(stream);
330
331 /* Indicates that the consumer data state MUST be updated after this. */
332 consumer_data.need_update = 1;
333
334 pthread_mutex_unlock(&stream->lock);
a9838785 335 pthread_mutex_unlock(&stream->chan->lock);
4891ece8
DG
336 pthread_mutex_unlock(&consumer_data.lock);
337 } else {
338 /*
339 * If the stream is not visible globally, this needs to be done
340 * outside of the consumer data lock section.
341 */
342 free_chan = unref_channel(stream);
10a50311
JD
343 }
344
10a50311
JD
345 if (free_chan) {
346 consumer_del_channel(free_chan);
347 }
348 } else {
4891ece8 349 destroy_close_stream(stream);
51230d70
DG
350 }
351
352 /* Free stream within a RCU call. */
353 consumer_stream_free(stream);
354}
1c20f0e2
JD
355
356/*
357 * Write index of a specific stream either on the relayd or local disk.
358 *
359 * Return 0 on success or else a negative value.
360 */
361int consumer_stream_write_index(struct lttng_consumer_stream *stream,
50adc264 362 struct ctf_packet_index *index)
1c20f0e2
JD
363{
364 int ret;
365 struct consumer_relayd_sock_pair *relayd;
366
367 assert(stream);
368 assert(index);
369
370 rcu_read_lock();
371 relayd = consumer_find_relayd(stream->net_seq_idx);
372 if (relayd) {
3ccf4e95 373 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1c20f0e2
JD
374 ret = relayd_send_index(&relayd->control_sock, index,
375 stream->relayd_stream_id, stream->next_net_seq_num - 1);
3ccf4e95 376 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1c20f0e2 377 } else {
6cd525e8
MD
378 ssize_t size_ret;
379
380 size_ret = index_write(stream->index_fd, index,
50adc264
JD
381 sizeof(struct ctf_packet_index));
382 if (size_ret < sizeof(struct ctf_packet_index)) {
6cd525e8
MD
383 ret = -1;
384 } else {
385 ret = 0;
386 }
1c20f0e2
JD
387 }
388 if (ret < 0) {
389 goto error;
390 }
391
392error:
393 rcu_read_unlock();
394 return ret;
395}
94d49140
JD
396
397/*
e5ca40ee 398 * Actually do the metadata sync using the given metadata stream.
94d49140 399 *
e5ca40ee
DG
400 * Return 0 on success else a negative value. ENODATA can be returned also
401 * indicating that there is no metadata available for that stream.
94d49140 402 */
e5ca40ee
DG
403static int do_sync_metadata(struct lttng_consumer_stream *metadata,
404 struct lttng_consumer_local_data *ctx)
94d49140
JD
405{
406 int ret;
94d49140 407
e5ca40ee
DG
408 assert(metadata);
409 assert(metadata->metadata_flag);
94d49140
JD
410 assert(ctx);
411
94d49140
JD
412 /*
413 * In UST, since we have to write the metadata from the cache packet
414 * by packet, we might need to start this procedure multiple times
415 * until all the metadata from the cache has been extracted.
416 */
417 do {
418 /*
419 * Steps :
420 * - Lock the metadata stream
421 * - Check if metadata stream node was deleted before locking.
422 * - if yes, release and return success
423 * - Check if new metadata is ready (flush + snapshot pos)
424 * - If nothing : release and return.
425 * - Lock the metadata_rdv_lock
426 * - Unlock the metadata stream
427 * - cond_wait on metadata_rdv to wait the wakeup from the
428 * metadata thread
429 * - Unlock the metadata_rdv_lock
430 */
431 pthread_mutex_lock(&metadata->lock);
432
433 /*
434 * There is a possibility that we were able to acquire a reference on the
435 * stream from the RCU hash table but between then and now, the node might
436 * have been deleted just before the lock is acquired. Thus, after locking,
437 * we make sure the metadata node has not been deleted which means that the
438 * buffers are closed.
439 *
440 * In that case, there is no need to sync the metadata hence returning a
441 * success return code.
442 */
443 ret = cds_lfht_is_node_deleted(&metadata->node.node);
444 if (ret) {
445 ret = 0;
446 goto end_unlock_mutex;
447 }
448
449 switch (ctx->type) {
450 case LTTNG_CONSUMER_KERNEL:
451 /*
452 * Empty the metadata cache and flush the current stream.
453 */
454 ret = lttng_kconsumer_sync_metadata(metadata);
455 break;
456 case LTTNG_CONSUMER32_UST:
457 case LTTNG_CONSUMER64_UST:
458 /*
459 * Ask the sessiond if we have new metadata waiting and update the
460 * consumer metadata cache.
461 */
462 ret = lttng_ustconsumer_sync_metadata(ctx, metadata);
463 break;
464 default:
465 assert(0);
466 ret = -1;
467 break;
468 }
469 /*
470 * Error or no new metadata, we exit here.
471 */
472 if (ret <= 0 || ret == ENODATA) {
473 goto end_unlock_mutex;
474 }
475
476 /*
477 * At this point, new metadata have been flushed, so we wait on the
478 * rendez-vous point for the metadata thread to wake us up when it
479 * finishes consuming the metadata and continue execution.
480 */
481
482 pthread_mutex_lock(&metadata->metadata_rdv_lock);
483
484 /*
485 * Release metadata stream lock so the metadata thread can process it.
486 */
487 pthread_mutex_unlock(&metadata->lock);
488
489 /*
490 * Wait on the rendez-vous point. Once woken up, it means the metadata was
491 * consumed and thus synchronization is achieved.
492 */
493 pthread_cond_wait(&metadata->metadata_rdv, &metadata->metadata_rdv_lock);
494 pthread_mutex_unlock(&metadata->metadata_rdv_lock);
495 } while (ret == EAGAIN);
496
e5ca40ee
DG
497 /* Success */
498 return 0;
94d49140
JD
499
500end_unlock_mutex:
501 pthread_mutex_unlock(&metadata->lock);
e5ca40ee
DG
502 return ret;
503}
504
505/*
506 * Synchronize the metadata using a given session ID. A successful acquisition
507 * of a metadata stream will trigger a request to the session daemon and a
508 * snapshot so the metadata thread can consume it.
509 *
510 * This function call is a rendez-vous point between the metadata thread and
511 * the data thread.
512 *
513 * Return 0 on success or else a negative value.
514 */
515int consumer_stream_sync_metadata(struct lttng_consumer_local_data *ctx,
516 uint64_t session_id)
517{
518 int ret;
519 struct lttng_consumer_stream *stream = NULL;
520 struct lttng_ht_iter iter;
521 struct lttng_ht *ht;
522
523 assert(ctx);
524
525 /* Ease our life a bit. */
526 ht = consumer_data.stream_list_ht;
527
528 rcu_read_lock();
529
530 /* Search the metadata associated with the session id of the given stream. */
531
532 cds_lfht_for_each_entry_duplicate(ht->ht,
533 ht->hash_fct(&session_id, lttng_ht_seed), ht->match_fct,
534 &session_id, &iter.iter, stream, node_session_id.node) {
535 if (!stream->metadata_flag) {
536 continue;
537 }
538
539 ret = do_sync_metadata(stream, ctx);
540 if (ret < 0) {
541 goto end;
542 }
543 }
544
545 /*
546 * Force return code to 0 (success) since ret might be ENODATA for instance
547 * which is not an error but rather that we should come back.
548 */
549 ret = 0;
550
551end:
94d49140
JD
552 rcu_read_unlock();
553 return ret;
554}
This page took 0.088654 seconds and 4 git commands to generate.