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