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