fe1178795d9cf6d4f0bc1a300041e74690fcb535
[lttng-tools.git] / src / common / consumer / consumer-stream.c
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
20 #define _LGPL_SOURCE
21 #include <assert.h>
22 #include <inttypes.h>
23 #include <sys/mman.h>
24 #include <unistd.h>
25
26 #include <common/common.h>
27 #include <common/index/index.h>
28 #include <common/kernel-consumer/kernel-consumer.h>
29 #include <common/relayd/relayd.h>
30 #include <common/ust-consumer/ust-consumer.h>
31 #include <common/utils.h>
32
33 #include "consumer-stream.h"
34
35 /*
36 * RCU call to free stream. MUST only be used with call_rcu().
37 */
38 static void free_stream_rcu(struct rcu_head *head)
39 {
40 struct lttng_ht_node_u64 *node =
41 caa_container_of(head, struct lttng_ht_node_u64, head);
42 struct lttng_consumer_stream *stream =
43 caa_container_of(node, struct lttng_consumer_stream, node);
44
45 pthread_mutex_destroy(&stream->lock);
46 free(stream);
47 }
48
49 /*
50 * Close stream on the relayd side. This call can destroy a relayd if the
51 * conditions are met.
52 *
53 * A RCU read side lock MUST be acquired if the relayd object was looked up in
54 * a hash table before calling this.
55 */
56 void consumer_stream_relayd_close(struct lttng_consumer_stream *stream,
57 struct consumer_relayd_sock_pair *relayd)
58 {
59 int ret;
60
61 assert(stream);
62 assert(relayd);
63
64 if (stream->sent_to_relayd) {
65 uatomic_dec(&relayd->refcount);
66 assert(uatomic_read(&relayd->refcount) >= 0);
67 }
68
69 /* Closing streams requires to lock the control socket. */
70 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
71 ret = relayd_send_close_stream(&relayd->control_sock,
72 stream->relayd_stream_id,
73 stream->next_net_seq_num - 1);
74 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
75 if (ret < 0) {
76 ERR("Relayd send close stream failed. Cleaning up relayd %" PRIu64 ".", relayd->net_seq_idx);
77 lttng_consumer_cleanup_relayd(relayd);
78 }
79
80 /* Both conditions are met, we destroy the relayd. */
81 if (uatomic_read(&relayd->refcount) == 0 &&
82 uatomic_read(&relayd->destroy_flag)) {
83 consumer_destroy_relayd(relayd);
84 }
85 stream->net_seq_idx = (uint64_t) -1ULL;
86 stream->sent_to_relayd = 0;
87 }
88
89 /*
90 * Close stream's file descriptors and, if needed, close stream also on the
91 * relayd side.
92 *
93 * The consumer data lock MUST be acquired.
94 * The stream lock MUST be acquired.
95 */
96 void consumer_stream_close(struct lttng_consumer_stream *stream)
97 {
98 int ret;
99 struct consumer_relayd_sock_pair *relayd;
100
101 assert(stream);
102
103 switch (consumer_data.type) {
104 case LTTNG_CONSUMER_KERNEL:
105 if (stream->mmap_base != NULL) {
106 ret = munmap(stream->mmap_base, stream->mmap_len);
107 if (ret != 0) {
108 PERROR("munmap");
109 }
110 }
111
112 if (stream->wait_fd >= 0) {
113 ret = close(stream->wait_fd);
114 if (ret) {
115 PERROR("close");
116 }
117 stream->wait_fd = -1;
118 }
119 if (stream->chan->output == CONSUMER_CHANNEL_SPLICE) {
120 utils_close_pipe(stream->splice_pipe);
121 }
122 break;
123 case LTTNG_CONSUMER32_UST:
124 case LTTNG_CONSUMER64_UST:
125 {
126 /*
127 * Special case for the metadata since the wait fd is an internal pipe
128 * polled in the metadata thread.
129 */
130 if (stream->metadata_flag && stream->chan->monitor) {
131 int rpipe = stream->ust_metadata_poll_pipe[0];
132
133 /*
134 * This will stop the channel timer if one and close the write side
135 * of the metadata poll pipe.
136 */
137 lttng_ustconsumer_close_metadata(stream->chan);
138 if (rpipe >= 0) {
139 ret = close(rpipe);
140 if (ret < 0) {
141 PERROR("closing metadata pipe read side");
142 }
143 stream->ust_metadata_poll_pipe[0] = -1;
144 }
145 }
146 break;
147 }
148 default:
149 ERR("Unknown consumer_data type");
150 assert(0);
151 }
152
153 /* Close output fd. Could be a socket or local file at this point. */
154 if (stream->out_fd >= 0) {
155 ret = close(stream->out_fd);
156 if (ret) {
157 PERROR("close");
158 }
159 stream->out_fd = -1;
160 }
161
162 if (stream->index_file) {
163 lttng_index_file_put(stream->index_file);
164 stream->index_file = NULL;
165 }
166
167 lttng_trace_chunk_put(stream->trace_chunk);
168 stream->trace_chunk = NULL;
169
170 /* Check and cleanup relayd if needed. */
171 rcu_read_lock();
172 relayd = consumer_find_relayd(stream->net_seq_idx);
173 if (relayd != NULL) {
174 consumer_stream_relayd_close(stream, relayd);
175 }
176 rcu_read_unlock();
177 }
178
179 /*
180 * Delete the stream from all possible hash tables.
181 *
182 * The consumer data lock MUST be acquired.
183 * The stream lock MUST be acquired.
184 */
185 void consumer_stream_delete(struct lttng_consumer_stream *stream,
186 struct lttng_ht *ht)
187 {
188 int ret;
189 struct lttng_ht_iter iter;
190
191 assert(stream);
192 /* Should NEVER be called not in monitor mode. */
193 assert(stream->chan->monitor);
194
195 rcu_read_lock();
196
197 if (ht) {
198 iter.iter.node = &stream->node.node;
199 ret = lttng_ht_del(ht, &iter);
200 assert(!ret);
201 }
202
203 /* Delete from stream per channel ID hash table. */
204 iter.iter.node = &stream->node_channel_id.node;
205 /*
206 * The returned value is of no importance. Even if the node is NOT in the
207 * hash table, we continue since we may have been called by a code path
208 * that did not add the stream to a (all) hash table. Same goes for the
209 * next call ht del call.
210 */
211 (void) lttng_ht_del(consumer_data.stream_per_chan_id_ht, &iter);
212
213 /* Delete from the global stream list. */
214 iter.iter.node = &stream->node_session_id.node;
215 /* See the previous ht del on why we ignore the returned value. */
216 (void) lttng_ht_del(consumer_data.stream_list_ht, &iter);
217
218 rcu_read_unlock();
219
220 if (!stream->metadata_flag) {
221 /* Decrement the stream count of the global consumer data. */
222 assert(consumer_data.stream_count > 0);
223 consumer_data.stream_count--;
224 }
225 }
226
227 /*
228 * Free the given stream within a RCU call.
229 */
230 void consumer_stream_free(struct lttng_consumer_stream *stream)
231 {
232 assert(stream);
233
234 call_rcu(&stream->node.head, free_stream_rcu);
235 }
236
237 /*
238 * Destroy the stream's buffers of the tracer.
239 */
240 void consumer_stream_destroy_buffers(struct lttng_consumer_stream *stream)
241 {
242 assert(stream);
243
244 switch (consumer_data.type) {
245 case LTTNG_CONSUMER_KERNEL:
246 break;
247 case LTTNG_CONSUMER32_UST:
248 case LTTNG_CONSUMER64_UST:
249 lttng_ustconsumer_del_stream(stream);
250 break;
251 default:
252 ERR("Unknown consumer_data type");
253 assert(0);
254 }
255 }
256
257 /*
258 * Destroy and close a already created stream.
259 */
260 static void destroy_close_stream(struct lttng_consumer_stream *stream)
261 {
262 assert(stream);
263
264 DBG("Consumer stream destroy monitored key: %" PRIu64, stream->key);
265
266 /* Destroy tracer buffers of the stream. */
267 consumer_stream_destroy_buffers(stream);
268 /* Close down everything including the relayd if one. */
269 consumer_stream_close(stream);
270 }
271
272 /*
273 * Decrement the stream's channel refcount and if down to 0, return the channel
274 * pointer so it can be destroyed by the caller or NULL if not.
275 */
276 static struct lttng_consumer_channel *unref_channel(
277 struct lttng_consumer_stream *stream)
278 {
279 struct lttng_consumer_channel *free_chan = NULL;
280
281 assert(stream);
282 assert(stream->chan);
283
284 /* Update refcount of channel and see if we need to destroy it. */
285 if (!uatomic_sub_return(&stream->chan->refcount, 1)
286 && !uatomic_read(&stream->chan->nb_init_stream_left)) {
287 free_chan = stream->chan;
288 }
289
290 return free_chan;
291 }
292
293 /*
294 * Destroy a stream completely. This will delete, close and free the stream.
295 * Once return, the stream is NO longer usable. Its channel may get destroyed
296 * if conditions are met for a monitored stream.
297 *
298 * This MUST be called WITHOUT the consumer data and stream lock acquired if
299 * the stream is in _monitor_ mode else it does not matter.
300 */
301 void consumer_stream_destroy(struct lttng_consumer_stream *stream,
302 struct lttng_ht *ht)
303 {
304 assert(stream);
305
306 /* Stream is in monitor mode. */
307 if (stream->monitor) {
308 struct lttng_consumer_channel *free_chan = NULL;
309
310 /*
311 * This means that the stream was successfully removed from the streams
312 * list of the channel and sent to the right thread managing this
313 * stream thus being globally visible.
314 */
315 if (stream->globally_visible) {
316 pthread_mutex_lock(&consumer_data.lock);
317 pthread_mutex_lock(&stream->chan->lock);
318 pthread_mutex_lock(&stream->lock);
319 /* Remove every reference of the stream in the consumer. */
320 consumer_stream_delete(stream, ht);
321
322 destroy_close_stream(stream);
323
324 /* Update channel's refcount of the stream. */
325 free_chan = unref_channel(stream);
326
327 /* Indicates that the consumer data state MUST be updated after this. */
328 consumer_data.need_update = 1;
329
330 pthread_mutex_unlock(&stream->lock);
331 pthread_mutex_unlock(&stream->chan->lock);
332 pthread_mutex_unlock(&consumer_data.lock);
333 } else {
334 /*
335 * If the stream is not visible globally, this needs to be done
336 * outside of the consumer data lock section.
337 */
338 free_chan = unref_channel(stream);
339 }
340
341 if (free_chan) {
342 consumer_del_channel(free_chan);
343 }
344 } else {
345 destroy_close_stream(stream);
346 }
347
348 /* Free stream within a RCU call. */
349 lttng_trace_chunk_put(stream->trace_chunk);
350 stream->trace_chunk = NULL;
351 consumer_stream_free(stream);
352 }
353
354 /*
355 * Write index of a specific stream either on the relayd or local disk.
356 *
357 * Return 0 on success or else a negative value.
358 */
359 int consumer_stream_write_index(struct lttng_consumer_stream *stream,
360 struct ctf_packet_index *element)
361 {
362 int ret;
363
364 assert(stream);
365 assert(element);
366
367 rcu_read_lock();
368 if (stream->net_seq_idx != (uint64_t) -1ULL) {
369 struct consumer_relayd_sock_pair *relayd;
370 relayd = consumer_find_relayd(stream->net_seq_idx);
371 if (relayd) {
372 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
373 ret = relayd_send_index(&relayd->control_sock, element,
374 stream->relayd_stream_id, stream->next_net_seq_num - 1);
375 if (ret < 0) {
376 /*
377 * Communication error with lttng-relayd,
378 * perform cleanup now
379 */
380 ERR("Relayd send index failed. Cleaning up relayd %" PRIu64 ".", relayd->net_seq_idx);
381 lttng_consumer_cleanup_relayd(relayd);
382 ret = -1;
383 }
384 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
385 } else {
386 ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't write index.",
387 stream->key, stream->net_seq_idx);
388 ret = -1;
389 }
390 } else {
391 if (lttng_index_file_write(stream->index_file, element)) {
392 ret = -1;
393 } else {
394 ret = 0;
395 }
396 }
397 if (ret < 0) {
398 goto error;
399 }
400
401 error:
402 rcu_read_unlock();
403 return ret;
404 }
405
406 /*
407 * Actually do the metadata sync using the given metadata stream.
408 *
409 * Return 0 on success else a negative value. ENODATA can be returned also
410 * indicating that there is no metadata available for that stream.
411 */
412 static int do_sync_metadata(struct lttng_consumer_stream *metadata,
413 struct lttng_consumer_local_data *ctx)
414 {
415 int ret;
416
417 assert(metadata);
418 assert(metadata->metadata_flag);
419 assert(ctx);
420
421 /*
422 * In UST, since we have to write the metadata from the cache packet
423 * by packet, we might need to start this procedure multiple times
424 * until all the metadata from the cache has been extracted.
425 */
426 do {
427 /*
428 * Steps :
429 * - Lock the metadata stream
430 * - Check if metadata stream node was deleted before locking.
431 * - if yes, release and return success
432 * - Check if new metadata is ready (flush + snapshot pos)
433 * - If nothing : release and return.
434 * - Lock the metadata_rdv_lock
435 * - Unlock the metadata stream
436 * - cond_wait on metadata_rdv to wait the wakeup from the
437 * metadata thread
438 * - Unlock the metadata_rdv_lock
439 */
440 pthread_mutex_lock(&metadata->lock);
441
442 /*
443 * There is a possibility that we were able to acquire a reference on the
444 * stream from the RCU hash table but between then and now, the node might
445 * have been deleted just before the lock is acquired. Thus, after locking,
446 * we make sure the metadata node has not been deleted which means that the
447 * buffers are closed.
448 *
449 * In that case, there is no need to sync the metadata hence returning a
450 * success return code.
451 */
452 ret = cds_lfht_is_node_deleted(&metadata->node.node);
453 if (ret) {
454 ret = 0;
455 goto end_unlock_mutex;
456 }
457
458 switch (ctx->type) {
459 case LTTNG_CONSUMER_KERNEL:
460 /*
461 * Empty the metadata cache and flush the current stream.
462 */
463 ret = lttng_kconsumer_sync_metadata(metadata);
464 break;
465 case LTTNG_CONSUMER32_UST:
466 case LTTNG_CONSUMER64_UST:
467 /*
468 * Ask the sessiond if we have new metadata waiting and update the
469 * consumer metadata cache.
470 */
471 ret = lttng_ustconsumer_sync_metadata(ctx, metadata);
472 break;
473 default:
474 assert(0);
475 ret = -1;
476 break;
477 }
478 /*
479 * Error or no new metadata, we exit here.
480 */
481 if (ret <= 0 || ret == ENODATA) {
482 goto end_unlock_mutex;
483 }
484
485 /*
486 * At this point, new metadata have been flushed, so we wait on the
487 * rendez-vous point for the metadata thread to wake us up when it
488 * finishes consuming the metadata and continue execution.
489 */
490
491 pthread_mutex_lock(&metadata->metadata_rdv_lock);
492
493 /*
494 * Release metadata stream lock so the metadata thread can process it.
495 */
496 pthread_mutex_unlock(&metadata->lock);
497
498 /*
499 * Wait on the rendez-vous point. Once woken up, it means the metadata was
500 * consumed and thus synchronization is achieved.
501 */
502 pthread_cond_wait(&metadata->metadata_rdv, &metadata->metadata_rdv_lock);
503 pthread_mutex_unlock(&metadata->metadata_rdv_lock);
504 } while (ret == EAGAIN);
505
506 /* Success */
507 return 0;
508
509 end_unlock_mutex:
510 pthread_mutex_unlock(&metadata->lock);
511 return ret;
512 }
513
514 /*
515 * Synchronize the metadata using a given session ID. A successful acquisition
516 * of a metadata stream will trigger a request to the session daemon and a
517 * snapshot so the metadata thread can consume it.
518 *
519 * This function call is a rendez-vous point between the metadata thread and
520 * the data thread.
521 *
522 * Return 0 on success or else a negative value.
523 */
524 int consumer_stream_sync_metadata(struct lttng_consumer_local_data *ctx,
525 uint64_t session_id)
526 {
527 int ret;
528 struct lttng_consumer_stream *stream = NULL;
529 struct lttng_ht_iter iter;
530 struct lttng_ht *ht;
531
532 assert(ctx);
533
534 /* Ease our life a bit. */
535 ht = consumer_data.stream_list_ht;
536
537 rcu_read_lock();
538
539 /* Search the metadata associated with the session id of the given stream. */
540
541 cds_lfht_for_each_entry_duplicate(ht->ht,
542 ht->hash_fct(&session_id, lttng_ht_seed), ht->match_fct,
543 &session_id, &iter.iter, stream, node_session_id.node) {
544 if (!stream->metadata_flag) {
545 continue;
546 }
547
548 ret = do_sync_metadata(stream, ctx);
549 if (ret < 0) {
550 goto end;
551 }
552 }
553
554 /*
555 * Force return code to 0 (success) since ret might be ENODATA for instance
556 * which is not an error but rather that we should come back.
557 */
558 ret = 0;
559
560 end:
561 rcu_read_unlock();
562 return ret;
563 }
564
565 int consumer_stream_create_output_files(struct lttng_consumer_stream *stream,
566 bool create_index)
567 {
568 int ret;
569 enum lttng_trace_chunk_status chunk_status;
570 const int flags = O_WRONLY | O_CREAT | O_TRUNC;
571 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
572 char stream_path[LTTNG_PATH_MAX];
573
574 ASSERT_LOCKED(stream->lock);
575 assert(stream->trace_chunk);
576
577 ret = utils_stream_file_path(stream->chan->pathname, stream->name,
578 stream->chan->tracefile_size,
579 stream->tracefile_count_current, NULL,
580 stream_path, sizeof(stream_path));
581 if (ret < 0) {
582 goto end;
583 }
584
585 if (stream->out_fd >= 0) {
586 ret = close(stream->out_fd);
587 if (ret < 0) {
588 PERROR("Failed to close stream file \"%s\"",
589 stream->name);
590 goto end;
591 }
592 stream->out_fd = -1;
593 }
594
595 DBG("Opening stream output file \"%s\"", stream_path);
596 chunk_status = lttng_trace_chunk_open_file(stream->trace_chunk, stream_path,
597 flags, mode, &stream->out_fd);
598 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
599 ERR("Failed to open stream file \"%s\"", stream->name);
600 ret = -1;
601 goto end;
602 }
603
604 if (!stream->metadata_flag && (create_index || stream->index_file)) {
605 if (stream->index_file) {
606 lttng_index_file_put(stream->index_file);
607 }
608 stream->index_file = lttng_index_file_create_from_trace_chunk(
609 stream->trace_chunk,
610 stream->chan->pathname,
611 stream->name,
612 stream->chan->tracefile_size,
613 stream->tracefile_count_current,
614 CTF_INDEX_MAJOR, CTF_INDEX_MINOR,
615 false);
616 if (!stream->index_file) {
617 ret = -1;
618 goto end;
619 }
620 }
621
622 /* Reset current size because we just perform a rotation. */
623 stream->tracefile_size_current = 0;
624 stream->out_fd_offset = 0;
625 end:
626 return ret;
627 }
628
629 int consumer_stream_rotate_output_files(struct lttng_consumer_stream *stream)
630 {
631 int ret;
632
633 stream->tracefile_count_current++;
634 if (stream->chan->tracefile_count > 0) {
635 stream->tracefile_count_current %=
636 stream->chan->tracefile_count;
637 }
638
639 DBG("Rotating output files of stream \"%s\"", stream->name);
640 ret = consumer_stream_create_output_files(stream, true);
641 if (ret) {
642 goto end;
643 }
644
645 end:
646 return ret;
647 }
This page took 0.041006 seconds and 3 git commands to generate.