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