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