532675c809b9f793939634e23ba8b92efeca8510
[lttng-tools.git] / src / common / consumer / consumer-stream.c
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
20 #define _LGPL_SOURCE
21 #include <assert.h>
22 #include <inttypes.h>
23 #include <sys/mman.h>
24 #include <unistd.h>
25
26 #include <common/common.h>
27 #include <common/index/index.h>
28 #include <common/kernel-consumer/kernel-consumer.h>
29 #include <common/relayd/relayd.h>
30 #include <common/ust-consumer/ust-consumer.h>
31 #include <common/utils.h>
32 #include <common/consumer/consumer.h>
33 #include <common/consumer/consumer-timer.h>
34 #include <common/consumer/metadata-bucket.h>
35
36 #include "consumer-stream.h"
37
38 /*
39 * RCU call to free stream. MUST only be used with call_rcu().
40 */
41 static void free_stream_rcu(struct rcu_head *head)
42 {
43 struct lttng_ht_node_u64 *node =
44 caa_container_of(head, struct lttng_ht_node_u64, head);
45 struct lttng_consumer_stream *stream =
46 caa_container_of(node, struct lttng_consumer_stream, node);
47
48 pthread_mutex_destroy(&stream->lock);
49 free(stream);
50 }
51
52 static void consumer_stream_data_lock_all(struct lttng_consumer_stream *stream)
53 {
54 pthread_mutex_lock(&stream->chan->lock);
55 pthread_mutex_lock(&stream->lock);
56 }
57
58 static void consumer_stream_data_unlock_all(struct lttng_consumer_stream *stream)
59 {
60 pthread_mutex_unlock(&stream->lock);
61 pthread_mutex_unlock(&stream->chan->lock);
62 }
63
64 static void consumer_stream_metadata_lock_all(struct lttng_consumer_stream *stream)
65 {
66 consumer_stream_data_lock_all(stream);
67 pthread_mutex_lock(&stream->metadata_rdv_lock);
68 }
69
70 static void consumer_stream_metadata_unlock_all(struct lttng_consumer_stream *stream)
71 {
72 pthread_mutex_unlock(&stream->metadata_rdv_lock);
73 consumer_stream_data_unlock_all(stream);
74 }
75
76 /* Only used for data streams. */
77 static int consumer_stream_update_stats(struct lttng_consumer_stream *stream,
78 const struct stream_subbuffer *subbuf)
79 {
80 int ret = 0;
81 uint64_t sequence_number;
82 const uint64_t discarded_events = subbuf->info.data.events_discarded;
83
84 if (!subbuf->info.data.sequence_number.is_set) {
85 /* Command not supported by the tracer. */
86 sequence_number = -1ULL;
87 stream->sequence_number_unavailable = true;
88 } else {
89 sequence_number = subbuf->info.data.sequence_number.value;
90 }
91
92 /*
93 * Start the sequence when we extract the first packet in case we don't
94 * start at 0 (for example if a consumer is not connected to the
95 * session immediately after the beginning).
96 */
97 if (stream->last_sequence_number == -1ULL) {
98 stream->last_sequence_number = sequence_number;
99 } else if (sequence_number > stream->last_sequence_number) {
100 stream->chan->lost_packets += sequence_number -
101 stream->last_sequence_number - 1;
102 } else {
103 /* seq <= last_sequence_number */
104 ERR("Sequence number inconsistent : prev = %" PRIu64
105 ", current = %" PRIu64,
106 stream->last_sequence_number, sequence_number);
107 ret = -1;
108 goto end;
109 }
110 stream->last_sequence_number = sequence_number;
111
112 if (discarded_events < stream->last_discarded_events) {
113 /*
114 * Overflow has occurred. We assume only one wrap-around
115 * has occurred.
116 */
117 stream->chan->discarded_events +=
118 (1ULL << (CAA_BITS_PER_LONG - 1)) -
119 stream->last_discarded_events +
120 discarded_events;
121 } else {
122 stream->chan->discarded_events += discarded_events -
123 stream->last_discarded_events;
124 }
125 stream->last_discarded_events = discarded_events;
126 ret = 0;
127
128 end:
129 return ret;
130 }
131
132 static
133 void ctf_packet_index_populate(struct ctf_packet_index *index,
134 off_t offset, const struct stream_subbuffer *subbuffer)
135 {
136 *index = (typeof(*index)){
137 .offset = htobe64(offset),
138 .packet_size = htobe64(subbuffer->info.data.packet_size),
139 .content_size = htobe64(subbuffer->info.data.content_size),
140 .timestamp_begin = htobe64(
141 subbuffer->info.data.timestamp_begin),
142 .timestamp_end = htobe64(
143 subbuffer->info.data.timestamp_end),
144 .events_discarded = htobe64(
145 subbuffer->info.data.events_discarded),
146 .stream_id = htobe64(subbuffer->info.data.stream_id),
147 .stream_instance_id = htobe64(
148 subbuffer->info.data.stream_instance_id.is_set ?
149 subbuffer->info.data.stream_instance_id.value : -1ULL),
150 .packet_seq_num = htobe64(
151 subbuffer->info.data.sequence_number.is_set ?
152 subbuffer->info.data.sequence_number.value : -1ULL),
153 };
154 }
155
156 static ssize_t consumer_stream_consume_mmap(
157 struct lttng_consumer_local_data *ctx,
158 struct lttng_consumer_stream *stream,
159 const struct stream_subbuffer *subbuffer)
160 {
161 const unsigned long padding_size =
162 subbuffer->info.data.padded_subbuf_size -
163 subbuffer->info.data.subbuf_size;
164 const ssize_t written_bytes = lttng_consumer_on_read_subbuffer_mmap(
165 stream, &subbuffer->buffer.buffer, padding_size);
166
167 if (stream->net_seq_idx == -1ULL) {
168 /*
169 * When writing on disk, check that only the subbuffer (no
170 * padding) was written to disk.
171 */
172 if (written_bytes != subbuffer->info.data.padded_subbuf_size) {
173 DBG("Failed to write the entire padded subbuffer on disk (written_bytes: %zd, padded subbuffer size %lu)",
174 written_bytes,
175 subbuffer->info.data.padded_subbuf_size);
176 }
177 } else {
178 /*
179 * When streaming over the network, check that the entire
180 * subbuffer including padding was successfully written.
181 */
182 if (written_bytes != subbuffer->info.data.subbuf_size) {
183 DBG("Failed to write only the subbuffer over the network (written_bytes: %zd, subbuffer size %lu)",
184 written_bytes,
185 subbuffer->info.data.subbuf_size);
186 }
187 }
188
189 /*
190 * If `lttng_consumer_on_read_subbuffer_mmap()` returned an error, pass
191 * it along to the caller, else return zero.
192 */
193 if (written_bytes < 0) {
194 ERR("Error reading mmap subbuffer: %zd", written_bytes);
195 }
196
197 return written_bytes;
198 }
199
200 static ssize_t consumer_stream_consume_splice(
201 struct lttng_consumer_local_data *ctx,
202 struct lttng_consumer_stream *stream,
203 const struct stream_subbuffer *subbuffer)
204 {
205 const ssize_t written_bytes = lttng_consumer_on_read_subbuffer_splice(
206 ctx, stream, subbuffer->info.data.padded_subbuf_size, 0);
207
208 if (written_bytes != subbuffer->info.data.padded_subbuf_size) {
209 DBG("Failed to write the entire padded subbuffer (written_bytes: %zd, padded subbuffer size %lu)",
210 written_bytes,
211 subbuffer->info.data.padded_subbuf_size);
212 }
213
214 /*
215 * If `lttng_consumer_on_read_subbuffer_splice()` returned an error,
216 * pass it along to the caller, else return zero.
217 */
218 if (written_bytes < 0) {
219 ERR("Error reading splice subbuffer: %zd", written_bytes);
220 }
221
222 return written_bytes;
223 }
224
225 static int consumer_stream_send_index(
226 struct lttng_consumer_stream *stream,
227 const struct stream_subbuffer *subbuffer,
228 struct lttng_consumer_local_data *ctx)
229 {
230 off_t packet_offset = 0;
231 struct ctf_packet_index index = {};
232
233 /*
234 * This is called after consuming the sub-buffer; substract the
235 * effect this sub-buffer from the offset.
236 */
237 if (stream->net_seq_idx == (uint64_t) -1ULL) {
238 packet_offset = stream->out_fd_offset -
239 subbuffer->info.data.padded_subbuf_size;
240 }
241
242 ctf_packet_index_populate(&index, packet_offset, subbuffer);
243 return consumer_stream_write_index(stream, &index);
244 }
245
246 /*
247 * Actually do the metadata sync using the given metadata stream.
248 *
249 * Return 0 on success else a negative value. ENODATA can be returned also
250 * indicating that there is no metadata available for that stream.
251 */
252 static int do_sync_metadata(struct lttng_consumer_stream *metadata,
253 struct lttng_consumer_local_data *ctx)
254 {
255 int ret;
256 enum sync_metadata_status status;
257
258 assert(metadata);
259 assert(metadata->metadata_flag);
260 assert(ctx);
261
262 /*
263 * In UST, since we have to write the metadata from the cache packet
264 * by packet, we might need to start this procedure multiple times
265 * until all the metadata from the cache has been extracted.
266 */
267 do {
268 /*
269 * Steps :
270 * - Lock the metadata stream
271 * - Check if metadata stream node was deleted before locking.
272 * - if yes, release and return success
273 * - Check if new metadata is ready (flush + snapshot pos)
274 * - If nothing : release and return.
275 * - Lock the metadata_rdv_lock
276 * - Unlock the metadata stream
277 * - cond_wait on metadata_rdv to wait the wakeup from the
278 * metadata thread
279 * - Unlock the metadata_rdv_lock
280 */
281 pthread_mutex_lock(&metadata->lock);
282
283 /*
284 * There is a possibility that we were able to acquire a reference on the
285 * stream from the RCU hash table but between then and now, the node might
286 * have been deleted just before the lock is acquired. Thus, after locking,
287 * we make sure the metadata node has not been deleted which means that the
288 * buffers are closed.
289 *
290 * In that case, there is no need to sync the metadata hence returning a
291 * success return code.
292 */
293 ret = cds_lfht_is_node_deleted(&metadata->node.node);
294 if (ret) {
295 ret = 0;
296 goto end_unlock_mutex;
297 }
298
299 switch (ctx->type) {
300 case LTTNG_CONSUMER_KERNEL:
301 /*
302 * Empty the metadata cache and flush the current stream.
303 */
304 status = lttng_kconsumer_sync_metadata(metadata);
305 break;
306 case LTTNG_CONSUMER32_UST:
307 case LTTNG_CONSUMER64_UST:
308 /*
309 * Ask the sessiond if we have new metadata waiting and update the
310 * consumer metadata cache.
311 */
312 status = lttng_ustconsumer_sync_metadata(ctx, metadata);
313 break;
314 default:
315 abort();
316 }
317
318 switch (status) {
319 case SYNC_METADATA_STATUS_NEW_DATA:
320 break;
321 case SYNC_METADATA_STATUS_NO_DATA:
322 ret = 0;
323 goto end_unlock_mutex;
324 case SYNC_METADATA_STATUS_ERROR:
325 ret = -1;
326 goto end_unlock_mutex;
327 default:
328 abort();
329 }
330
331 /*
332 * At this point, new metadata have been flushed, so we wait on the
333 * rendez-vous point for the metadata thread to wake us up when it
334 * finishes consuming the metadata and continue execution.
335 */
336
337 pthread_mutex_lock(&metadata->metadata_rdv_lock);
338
339 /*
340 * Release metadata stream lock so the metadata thread can process it.
341 */
342 pthread_mutex_unlock(&metadata->lock);
343
344 /*
345 * Wait on the rendez-vous point. Once woken up, it means the metadata was
346 * consumed and thus synchronization is achieved.
347 */
348 pthread_cond_wait(&metadata->metadata_rdv, &metadata->metadata_rdv_lock);
349 pthread_mutex_unlock(&metadata->metadata_rdv_lock);
350 } while (status == SYNC_METADATA_STATUS_NEW_DATA);
351
352 /* Success */
353 return 0;
354
355 end_unlock_mutex:
356 pthread_mutex_unlock(&metadata->lock);
357 return ret;
358 }
359
360 /*
361 * Synchronize the metadata using a given session ID. A successful acquisition
362 * of a metadata stream will trigger a request to the session daemon and a
363 * snapshot so the metadata thread can consume it.
364 *
365 * This function call is a rendez-vous point between the metadata thread and
366 * the data thread.
367 *
368 * Return 0 on success or else a negative value.
369 */
370 int consumer_stream_sync_metadata(struct lttng_consumer_local_data *ctx,
371 uint64_t session_id)
372 {
373 int ret;
374 struct lttng_consumer_stream *stream = NULL;
375 struct lttng_ht_iter iter;
376 struct lttng_ht *ht;
377
378 assert(ctx);
379
380 /* Ease our life a bit. */
381 ht = consumer_data.stream_list_ht;
382
383 rcu_read_lock();
384
385 /* Search the metadata associated with the session id of the given stream. */
386
387 cds_lfht_for_each_entry_duplicate(ht->ht,
388 ht->hash_fct(&session_id, lttng_ht_seed), ht->match_fct,
389 &session_id, &iter.iter, stream, node_session_id.node) {
390 if (!stream->metadata_flag) {
391 continue;
392 }
393
394 ret = do_sync_metadata(stream, ctx);
395 if (ret < 0) {
396 goto end;
397 }
398 }
399
400 /*
401 * Force return code to 0 (success) since ret might be ENODATA for instance
402 * which is not an error but rather that we should come back.
403 */
404 ret = 0;
405
406 end:
407 rcu_read_unlock();
408 return ret;
409 }
410
411 static int consumer_stream_sync_metadata_index(
412 struct lttng_consumer_stream *stream,
413 const struct stream_subbuffer *subbuffer,
414 struct lttng_consumer_local_data *ctx)
415 {
416 int ret;
417
418 /* Block until all the metadata is sent. */
419 pthread_mutex_lock(&stream->metadata_timer_lock);
420 assert(!stream->missed_metadata_flush);
421 stream->waiting_on_metadata = true;
422 pthread_mutex_unlock(&stream->metadata_timer_lock);
423
424 ret = consumer_stream_sync_metadata(ctx, stream->session_id);
425
426 pthread_mutex_lock(&stream->metadata_timer_lock);
427 stream->waiting_on_metadata = false;
428 if (stream->missed_metadata_flush) {
429 stream->missed_metadata_flush = false;
430 pthread_mutex_unlock(&stream->metadata_timer_lock);
431 (void) stream->read_subbuffer_ops.send_live_beacon(stream);
432 } else {
433 pthread_mutex_unlock(&stream->metadata_timer_lock);
434 }
435 if (ret < 0) {
436 goto end;
437 }
438
439 ret = consumer_stream_send_index(stream, subbuffer, ctx);
440 end:
441 return ret;
442 }
443
444 /*
445 * Check if the local version of the metadata stream matches with the version
446 * of the metadata stream in the kernel. If it was updated, set the reset flag
447 * on the stream.
448 */
449 static
450 int metadata_stream_check_version(struct lttng_consumer_stream *stream,
451 const struct stream_subbuffer *subbuffer)
452 {
453 if (stream->metadata_version == subbuffer->info.metadata.version) {
454 goto end;
455 }
456
457 DBG("New metadata version detected");
458 consumer_stream_metadata_set_version(stream,
459 subbuffer->info.metadata.version);
460
461 if (stream->read_subbuffer_ops.reset_metadata) {
462 stream->read_subbuffer_ops.reset_metadata(stream);
463 }
464
465 end:
466 return 0;
467 }
468
469 struct lttng_consumer_stream *consumer_stream_create(
470 struct lttng_consumer_channel *channel,
471 uint64_t channel_key,
472 uint64_t stream_key,
473 const char *channel_name,
474 uint64_t relayd_id,
475 uint64_t session_id,
476 struct lttng_trace_chunk *trace_chunk,
477 int cpu,
478 int *alloc_ret,
479 enum consumer_channel_type type,
480 unsigned int monitor)
481 {
482 int ret;
483 struct lttng_consumer_stream *stream;
484
485 stream = zmalloc(sizeof(*stream));
486 if (stream == NULL) {
487 PERROR("malloc struct lttng_consumer_stream");
488 ret = -ENOMEM;
489 goto end;
490 }
491
492 if (trace_chunk && !lttng_trace_chunk_get(trace_chunk)) {
493 ERR("Failed to acquire trace chunk reference during the creation of a stream");
494 ret = -1;
495 goto error;
496 }
497
498 rcu_read_lock();
499 stream->chan = channel;
500 stream->key = stream_key;
501 stream->trace_chunk = trace_chunk;
502 stream->out_fd = -1;
503 stream->out_fd_offset = 0;
504 stream->output_written = 0;
505 stream->net_seq_idx = relayd_id;
506 stream->session_id = session_id;
507 stream->monitor = monitor;
508 stream->endpoint_status = CONSUMER_ENDPOINT_ACTIVE;
509 stream->index_file = NULL;
510 stream->last_sequence_number = -1ULL;
511 stream->rotate_position = -1ULL;
512 pthread_mutex_init(&stream->lock, NULL);
513 pthread_mutex_init(&stream->metadata_timer_lock, NULL);
514
515 /* If channel is the metadata, flag this stream as metadata. */
516 if (type == CONSUMER_CHANNEL_TYPE_METADATA) {
517 stream->metadata_flag = 1;
518 /* Metadata is flat out. */
519 strncpy(stream->name, DEFAULT_METADATA_NAME, sizeof(stream->name));
520 /* Live rendez-vous point. */
521 pthread_cond_init(&stream->metadata_rdv, NULL);
522 pthread_mutex_init(&stream->metadata_rdv_lock, NULL);
523 } else {
524 /* Format stream name to <channel_name>_<cpu_number> */
525 ret = snprintf(stream->name, sizeof(stream->name), "%s_%d",
526 channel_name, cpu);
527 if (ret < 0) {
528 PERROR("snprintf stream name");
529 goto error;
530 }
531 }
532
533 switch (channel->output) {
534 case CONSUMER_CHANNEL_SPLICE:
535 stream->output = LTTNG_EVENT_SPLICE;
536 ret = utils_create_pipe(stream->splice_pipe);
537 if (ret < 0) {
538 goto error;
539 }
540 break;
541 case CONSUMER_CHANNEL_MMAP:
542 stream->output = LTTNG_EVENT_MMAP;
543 break;
544 default:
545 abort();
546 }
547
548 /* Key is always the wait_fd for streams. */
549 lttng_ht_node_init_u64(&stream->node, stream->key);
550
551 /* Init node per channel id key */
552 lttng_ht_node_init_u64(&stream->node_channel_id, channel_key);
553
554 /* Init session id node with the stream session id */
555 lttng_ht_node_init_u64(&stream->node_session_id, stream->session_id);
556
557 DBG3("Allocated stream %s (key %" PRIu64 ", chan_key %" PRIu64
558 " relayd_id %" PRIu64 ", session_id %" PRIu64,
559 stream->name, stream->key, channel_key,
560 stream->net_seq_idx, stream->session_id);
561
562 rcu_read_unlock();
563
564 if (type == CONSUMER_CHANNEL_TYPE_METADATA) {
565 stream->read_subbuffer_ops.lock =
566 consumer_stream_metadata_lock_all;
567 stream->read_subbuffer_ops.unlock =
568 consumer_stream_metadata_unlock_all;
569 stream->read_subbuffer_ops.pre_consume_subbuffer =
570 metadata_stream_check_version;
571 } else {
572 stream->read_subbuffer_ops.lock = consumer_stream_data_lock_all;
573 stream->read_subbuffer_ops.unlock =
574 consumer_stream_data_unlock_all;
575 stream->read_subbuffer_ops.pre_consume_subbuffer =
576 consumer_stream_update_stats;
577 if (channel->is_live) {
578 stream->read_subbuffer_ops.post_consume =
579 consumer_stream_sync_metadata_index;
580 } else {
581 stream->read_subbuffer_ops.post_consume =
582 consumer_stream_send_index;
583 }
584 }
585
586 if (channel->output == CONSUMER_CHANNEL_MMAP) {
587 stream->read_subbuffer_ops.consume_subbuffer =
588 consumer_stream_consume_mmap;
589 } else {
590 stream->read_subbuffer_ops.consume_subbuffer =
591 consumer_stream_consume_splice;
592 }
593
594 return stream;
595
596 error:
597 rcu_read_unlock();
598 lttng_trace_chunk_put(stream->trace_chunk);
599 free(stream);
600 end:
601 if (alloc_ret) {
602 *alloc_ret = ret;
603 }
604 return NULL;
605 }
606
607 /*
608 * Close stream on the relayd side. This call can destroy a relayd if the
609 * conditions are met.
610 *
611 * A RCU read side lock MUST be acquired if the relayd object was looked up in
612 * a hash table before calling this.
613 */
614 void consumer_stream_relayd_close(struct lttng_consumer_stream *stream,
615 struct consumer_relayd_sock_pair *relayd)
616 {
617 int ret;
618
619 assert(stream);
620 assert(relayd);
621
622 if (stream->sent_to_relayd) {
623 uatomic_dec(&relayd->refcount);
624 assert(uatomic_read(&relayd->refcount) >= 0);
625 }
626
627 /* Closing streams requires to lock the control socket. */
628 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
629 ret = relayd_send_close_stream(&relayd->control_sock,
630 stream->relayd_stream_id,
631 stream->next_net_seq_num - 1);
632 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
633 if (ret < 0) {
634 ERR("Relayd send close stream failed. Cleaning up relayd %" PRIu64 ".", relayd->net_seq_idx);
635 lttng_consumer_cleanup_relayd(relayd);
636 }
637
638 /* Both conditions are met, we destroy the relayd. */
639 if (uatomic_read(&relayd->refcount) == 0 &&
640 uatomic_read(&relayd->destroy_flag)) {
641 consumer_destroy_relayd(relayd);
642 }
643 stream->net_seq_idx = (uint64_t) -1ULL;
644 stream->sent_to_relayd = 0;
645 }
646
647 /*
648 * Close stream's file descriptors and, if needed, close stream also on the
649 * relayd side.
650 *
651 * The consumer data lock MUST be acquired.
652 * The stream lock MUST be acquired.
653 */
654 void consumer_stream_close(struct lttng_consumer_stream *stream)
655 {
656 int ret;
657 struct consumer_relayd_sock_pair *relayd;
658
659 assert(stream);
660
661 switch (consumer_data.type) {
662 case LTTNG_CONSUMER_KERNEL:
663 if (stream->mmap_base != NULL) {
664 ret = munmap(stream->mmap_base, stream->mmap_len);
665 if (ret != 0) {
666 PERROR("munmap");
667 }
668 }
669
670 if (stream->wait_fd >= 0) {
671 ret = close(stream->wait_fd);
672 if (ret) {
673 PERROR("close");
674 }
675 stream->wait_fd = -1;
676 }
677 if (stream->chan->output == CONSUMER_CHANNEL_SPLICE) {
678 utils_close_pipe(stream->splice_pipe);
679 }
680 break;
681 case LTTNG_CONSUMER32_UST:
682 case LTTNG_CONSUMER64_UST:
683 {
684 /*
685 * Special case for the metadata since the wait fd is an internal pipe
686 * polled in the metadata thread.
687 */
688 if (stream->metadata_flag && stream->chan->monitor) {
689 int rpipe = stream->ust_metadata_poll_pipe[0];
690
691 /*
692 * This will stop the channel timer if one and close the write side
693 * of the metadata poll pipe.
694 */
695 lttng_ustconsumer_close_metadata(stream->chan);
696 if (rpipe >= 0) {
697 ret = close(rpipe);
698 if (ret < 0) {
699 PERROR("closing metadata pipe read side");
700 }
701 stream->ust_metadata_poll_pipe[0] = -1;
702 }
703 }
704 break;
705 }
706 default:
707 ERR("Unknown consumer_data type");
708 assert(0);
709 }
710
711 /* Close output fd. Could be a socket or local file at this point. */
712 if (stream->out_fd >= 0) {
713 ret = close(stream->out_fd);
714 if (ret) {
715 PERROR("close");
716 }
717 stream->out_fd = -1;
718 }
719
720 if (stream->index_file) {
721 lttng_index_file_put(stream->index_file);
722 stream->index_file = NULL;
723 }
724
725 lttng_trace_chunk_put(stream->trace_chunk);
726 stream->trace_chunk = NULL;
727
728 /* Check and cleanup relayd if needed. */
729 rcu_read_lock();
730 relayd = consumer_find_relayd(stream->net_seq_idx);
731 if (relayd != NULL) {
732 consumer_stream_relayd_close(stream, relayd);
733 }
734 rcu_read_unlock();
735 }
736
737 /*
738 * Delete the stream from all possible hash tables.
739 *
740 * The consumer data lock MUST be acquired.
741 * The stream lock MUST be acquired.
742 */
743 void consumer_stream_delete(struct lttng_consumer_stream *stream,
744 struct lttng_ht *ht)
745 {
746 int ret;
747 struct lttng_ht_iter iter;
748
749 assert(stream);
750 /* Should NEVER be called not in monitor mode. */
751 assert(stream->chan->monitor);
752
753 rcu_read_lock();
754
755 if (ht) {
756 iter.iter.node = &stream->node.node;
757 ret = lttng_ht_del(ht, &iter);
758 assert(!ret);
759 }
760
761 /* Delete from stream per channel ID hash table. */
762 iter.iter.node = &stream->node_channel_id.node;
763 /*
764 * The returned value is of no importance. Even if the node is NOT in the
765 * hash table, we continue since we may have been called by a code path
766 * that did not add the stream to a (all) hash table. Same goes for the
767 * next call ht del call.
768 */
769 (void) lttng_ht_del(consumer_data.stream_per_chan_id_ht, &iter);
770
771 /* Delete from the global stream list. */
772 iter.iter.node = &stream->node_session_id.node;
773 /* See the previous ht del on why we ignore the returned value. */
774 (void) lttng_ht_del(consumer_data.stream_list_ht, &iter);
775
776 rcu_read_unlock();
777
778 if (!stream->metadata_flag) {
779 /* Decrement the stream count of the global consumer data. */
780 assert(consumer_data.stream_count > 0);
781 consumer_data.stream_count--;
782 }
783 }
784
785 /*
786 * Free the given stream within a RCU call.
787 */
788 void consumer_stream_free(struct lttng_consumer_stream *stream)
789 {
790 assert(stream);
791
792 metadata_bucket_destroy(stream->metadata_bucket);
793 call_rcu(&stream->node.head, free_stream_rcu);
794 }
795
796 /*
797 * Destroy the stream's buffers of the tracer.
798 */
799 void consumer_stream_destroy_buffers(struct lttng_consumer_stream *stream)
800 {
801 assert(stream);
802
803 switch (consumer_data.type) {
804 case LTTNG_CONSUMER_KERNEL:
805 break;
806 case LTTNG_CONSUMER32_UST:
807 case LTTNG_CONSUMER64_UST:
808 lttng_ustconsumer_del_stream(stream);
809 break;
810 default:
811 ERR("Unknown consumer_data type");
812 assert(0);
813 }
814 }
815
816 /*
817 * Destroy and close a already created stream.
818 */
819 static void destroy_close_stream(struct lttng_consumer_stream *stream)
820 {
821 assert(stream);
822
823 DBG("Consumer stream destroy monitored key: %" PRIu64, stream->key);
824
825 /* Destroy tracer buffers of the stream. */
826 consumer_stream_destroy_buffers(stream);
827 /* Close down everything including the relayd if one. */
828 consumer_stream_close(stream);
829 }
830
831 /*
832 * Decrement the stream's channel refcount and if down to 0, return the channel
833 * pointer so it can be destroyed by the caller or NULL if not.
834 */
835 static struct lttng_consumer_channel *unref_channel(
836 struct lttng_consumer_stream *stream)
837 {
838 struct lttng_consumer_channel *free_chan = NULL;
839
840 assert(stream);
841 assert(stream->chan);
842
843 /* Update refcount of channel and see if we need to destroy it. */
844 if (!uatomic_sub_return(&stream->chan->refcount, 1)
845 && !uatomic_read(&stream->chan->nb_init_stream_left)) {
846 free_chan = stream->chan;
847 }
848
849 return free_chan;
850 }
851
852 /*
853 * Destroy a stream completely. This will delete, close and free the stream.
854 * Once return, the stream is NO longer usable. Its channel may get destroyed
855 * if conditions are met for a monitored stream.
856 *
857 * This MUST be called WITHOUT the consumer data and stream lock acquired if
858 * the stream is in _monitor_ mode else it does not matter.
859 */
860 void consumer_stream_destroy(struct lttng_consumer_stream *stream,
861 struct lttng_ht *ht)
862 {
863 assert(stream);
864
865 /* Stream is in monitor mode. */
866 if (stream->monitor) {
867 struct lttng_consumer_channel *free_chan = NULL;
868
869 /*
870 * This means that the stream was successfully removed from the streams
871 * list of the channel and sent to the right thread managing this
872 * stream thus being globally visible.
873 */
874 if (stream->globally_visible) {
875 pthread_mutex_lock(&consumer_data.lock);
876 pthread_mutex_lock(&stream->chan->lock);
877 pthread_mutex_lock(&stream->lock);
878 /* Remove every reference of the stream in the consumer. */
879 consumer_stream_delete(stream, ht);
880
881 destroy_close_stream(stream);
882
883 /* Update channel's refcount of the stream. */
884 free_chan = unref_channel(stream);
885
886 /* Indicates that the consumer data state MUST be updated after this. */
887 consumer_data.need_update = 1;
888
889 pthread_mutex_unlock(&stream->lock);
890 pthread_mutex_unlock(&stream->chan->lock);
891 pthread_mutex_unlock(&consumer_data.lock);
892 } else {
893 /*
894 * If the stream is not visible globally, this needs to be done
895 * outside of the consumer data lock section.
896 */
897 free_chan = unref_channel(stream);
898 }
899
900 if (free_chan) {
901 consumer_del_channel(free_chan);
902 }
903 } else {
904 destroy_close_stream(stream);
905 }
906
907 /* Free stream within a RCU call. */
908 lttng_trace_chunk_put(stream->trace_chunk);
909 stream->trace_chunk = NULL;
910 consumer_stream_free(stream);
911 }
912
913 /*
914 * Write index of a specific stream either on the relayd or local disk.
915 *
916 * Return 0 on success or else a negative value.
917 */
918 int consumer_stream_write_index(struct lttng_consumer_stream *stream,
919 struct ctf_packet_index *element)
920 {
921 int ret;
922
923 assert(stream);
924 assert(element);
925
926 rcu_read_lock();
927 if (stream->net_seq_idx != (uint64_t) -1ULL) {
928 struct consumer_relayd_sock_pair *relayd;
929 relayd = consumer_find_relayd(stream->net_seq_idx);
930 if (relayd) {
931 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
932 ret = relayd_send_index(&relayd->control_sock, element,
933 stream->relayd_stream_id, stream->next_net_seq_num - 1);
934 if (ret < 0) {
935 /*
936 * Communication error with lttng-relayd,
937 * perform cleanup now
938 */
939 ERR("Relayd send index failed. Cleaning up relayd %" PRIu64 ".", relayd->net_seq_idx);
940 lttng_consumer_cleanup_relayd(relayd);
941 ret = -1;
942 }
943 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
944 } else {
945 ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't write index.",
946 stream->key, stream->net_seq_idx);
947 ret = -1;
948 }
949 } else {
950 if (lttng_index_file_write(stream->index_file, element)) {
951 ret = -1;
952 } else {
953 ret = 0;
954 }
955 }
956 if (ret < 0) {
957 goto error;
958 }
959
960 error:
961 rcu_read_unlock();
962 return ret;
963 }
964
965 int consumer_stream_create_output_files(struct lttng_consumer_stream *stream,
966 bool create_index)
967 {
968 int ret;
969 enum lttng_trace_chunk_status chunk_status;
970 const int flags = O_WRONLY | O_CREAT | O_TRUNC;
971 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
972 char stream_path[LTTNG_PATH_MAX];
973
974 ASSERT_LOCKED(stream->lock);
975 assert(stream->trace_chunk);
976
977 ret = utils_stream_file_path(stream->chan->pathname, stream->name,
978 stream->chan->tracefile_size,
979 stream->tracefile_count_current, NULL,
980 stream_path, sizeof(stream_path));
981 if (ret < 0) {
982 goto end;
983 }
984
985 if (stream->out_fd >= 0) {
986 ret = close(stream->out_fd);
987 if (ret < 0) {
988 PERROR("Failed to close stream file \"%s\"",
989 stream->name);
990 goto end;
991 }
992 stream->out_fd = -1;
993 }
994
995 DBG("Opening stream output file \"%s\"", stream_path);
996 chunk_status = lttng_trace_chunk_open_file(stream->trace_chunk, stream_path,
997 flags, mode, &stream->out_fd);
998 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
999 ERR("Failed to open stream file \"%s\"", stream->name);
1000 ret = -1;
1001 goto end;
1002 }
1003
1004 if (!stream->metadata_flag && (create_index || stream->index_file)) {
1005 if (stream->index_file) {
1006 lttng_index_file_put(stream->index_file);
1007 }
1008 stream->index_file = lttng_index_file_create_from_trace_chunk(
1009 stream->trace_chunk,
1010 stream->chan->pathname,
1011 stream->name,
1012 stream->chan->tracefile_size,
1013 stream->tracefile_count_current,
1014 CTF_INDEX_MAJOR, CTF_INDEX_MINOR,
1015 false);
1016 if (!stream->index_file) {
1017 ret = -1;
1018 goto end;
1019 }
1020 }
1021
1022 /* Reset current size because we just perform a rotation. */
1023 stream->tracefile_size_current = 0;
1024 stream->out_fd_offset = 0;
1025 end:
1026 return ret;
1027 }
1028
1029 int consumer_stream_rotate_output_files(struct lttng_consumer_stream *stream)
1030 {
1031 int ret;
1032
1033 stream->tracefile_count_current++;
1034 if (stream->chan->tracefile_count > 0) {
1035 stream->tracefile_count_current %=
1036 stream->chan->tracefile_count;
1037 }
1038
1039 DBG("Rotating output files of stream \"%s\"", stream->name);
1040 ret = consumer_stream_create_output_files(stream, true);
1041 if (ret) {
1042 goto end;
1043 }
1044
1045 end:
1046 return ret;
1047 }
1048
1049 bool consumer_stream_is_deleted(struct lttng_consumer_stream *stream)
1050 {
1051 /*
1052 * This function does not take a const stream since
1053 * cds_lfht_is_node_deleted was not const before liburcu 0.12.
1054 */
1055 assert(stream);
1056 return cds_lfht_is_node_deleted(&stream->node.node);
1057 }
1058
1059 static ssize_t metadata_bucket_flush(
1060 const struct stream_subbuffer *buffer, void *data)
1061 {
1062 ssize_t ret;
1063 struct lttng_consumer_stream *stream = data;
1064
1065 ret = consumer_stream_consume_mmap(NULL, stream, buffer);
1066 if (ret < 0) {
1067 goto end;
1068 }
1069 end:
1070 return ret;
1071 }
1072
1073 static ssize_t metadata_bucket_consume(
1074 struct lttng_consumer_local_data *unused,
1075 struct lttng_consumer_stream *stream,
1076 const struct stream_subbuffer *subbuffer)
1077 {
1078 ssize_t ret;
1079 enum metadata_bucket_status status;
1080
1081 status = metadata_bucket_fill(stream->metadata_bucket, subbuffer);
1082 switch (status) {
1083 case METADATA_BUCKET_STATUS_OK:
1084 /* Return consumed size. */
1085 ret = subbuffer->buffer.buffer.size;
1086 break;
1087 default:
1088 ret = -1;
1089 }
1090
1091 return ret;
1092 }
1093
1094 int consumer_stream_enable_metadata_bucketization(
1095 struct lttng_consumer_stream *stream)
1096 {
1097 int ret = 0;
1098
1099 assert(stream->metadata_flag);
1100 assert(!stream->metadata_bucket);
1101 assert(stream->chan->output == CONSUMER_CHANNEL_MMAP);
1102
1103 stream->metadata_bucket = metadata_bucket_create(
1104 metadata_bucket_flush, stream);
1105 if (!stream->metadata_bucket) {
1106 ret = -1;
1107 goto end;
1108 }
1109
1110 stream->read_subbuffer_ops.consume_subbuffer = metadata_bucket_consume;
1111 end:
1112 return ret;
1113 }
1114
1115 void consumer_stream_metadata_set_version(
1116 struct lttng_consumer_stream *stream, uint64_t new_version)
1117 {
1118 assert(new_version > stream->metadata_version);
1119 stream->metadata_version = new_version;
1120 stream->reset_metadata_flag = 1;
1121
1122 if (stream->metadata_bucket) {
1123 metadata_bucket_reset(stream->metadata_bucket);
1124 }
1125 }
This page took 0.079977 seconds and 3 git commands to generate.