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