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