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