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