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