5a8157eb5e0256f055edeed168a6a4dad34d3090
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _LGPL_SOURCE
20 #include <assert.h>
21 #include <poll.h>
22 #include <pthread.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/mman.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <inttypes.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31
32 #include <bin/lttng-consumerd/health-consumerd.h>
33 #include <common/common.h>
34 #include <common/kernel-ctl/kernel-ctl.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/compat/fcntl.h>
38 #include <common/compat/endian.h>
39 #include <common/pipe.h>
40 #include <common/relayd/relayd.h>
41 #include <common/utils.h>
42 #include <common/consumer/consumer-stream.h>
43 #include <common/index/index.h>
44 #include <common/consumer/consumer-timer.h>
45
46 #include "kernel-consumer.h"
47
48 extern struct lttng_consumer_global_data consumer_data;
49 extern int consumer_poll_timeout;
50 extern volatile int consumer_quit;
51
52 /*
53 * Take a snapshot for a specific fd
54 *
55 * Returns 0 on success, < 0 on error
56 */
57 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
58 {
59 int ret = 0;
60 int infd = stream->wait_fd;
61
62 ret = kernctl_snapshot(infd);
63 if (ret != 0) {
64 PERROR("Getting sub-buffer snapshot.");
65 }
66
67 return ret;
68 }
69
70 /*
71 * Get the produced position
72 *
73 * Returns 0 on success, < 0 on error
74 */
75 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
76 unsigned long *pos)
77 {
78 int ret;
79 int infd = stream->wait_fd;
80
81 ret = kernctl_snapshot_get_produced(infd, pos);
82 if (ret != 0) {
83 PERROR("kernctl_snapshot_get_produced");
84 }
85
86 return ret;
87 }
88
89 /*
90 * Get the consumerd position
91 *
92 * Returns 0 on success, < 0 on error
93 */
94 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
95 unsigned long *pos)
96 {
97 int ret;
98 int infd = stream->wait_fd;
99
100 ret = kernctl_snapshot_get_consumed(infd, pos);
101 if (ret != 0) {
102 PERROR("kernctl_snapshot_get_consumed");
103 }
104
105 return ret;
106 }
107
108 /*
109 * Take a snapshot of all the stream of a channel
110 *
111 * Returns 0 on success, < 0 on error
112 */
113 int lttng_kconsumer_snapshot_channel(uint64_t key, char *path,
114 uint64_t relayd_id, uint64_t nb_packets_per_stream,
115 struct lttng_consumer_local_data *ctx)
116 {
117 int ret;
118 struct lttng_consumer_channel *channel;
119 struct lttng_consumer_stream *stream;
120
121 DBG("Kernel consumer snapshot channel %" PRIu64, key);
122
123 rcu_read_lock();
124
125 channel = consumer_find_channel(key);
126 if (!channel) {
127 ERR("No channel found for key %" PRIu64, key);
128 ret = -1;
129 goto end;
130 }
131
132 /* Splice is not supported yet for channel snapshot. */
133 if (channel->output != CONSUMER_CHANNEL_MMAP) {
134 ERR("Unsupported output %d", channel->output);
135 ret = -1;
136 goto end;
137 }
138
139 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
140 /* Are we at a position _before_ the first available packet ? */
141 bool before_first_packet = true;
142 unsigned long consumed_pos, produced_pos;
143
144 health_code_update();
145
146 /*
147 * Lock stream because we are about to change its state.
148 */
149 pthread_mutex_lock(&stream->lock);
150
151 /*
152 * Assign the received relayd ID so we can use it for streaming. The streams
153 * are not visible to anyone so this is OK to change it.
154 */
155 stream->net_seq_idx = relayd_id;
156 channel->relayd_id = relayd_id;
157 if (relayd_id != (uint64_t) -1ULL) {
158 ret = consumer_send_relayd_stream(stream, path);
159 if (ret < 0) {
160 ERR("sending stream to relayd");
161 goto end_unlock;
162 }
163 } else {
164 ret = utils_create_stream_file(path, stream->name,
165 stream->chan->tracefile_size,
166 stream->tracefile_count_current,
167 stream->uid, stream->gid, NULL);
168 if (ret < 0) {
169 ERR("utils_create_stream_file");
170 goto end_unlock;
171 }
172
173 stream->out_fd = ret;
174 stream->tracefile_size_current = 0;
175
176 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64 ")",
177 path, stream->name, stream->key);
178 }
179 if (relayd_id != -1ULL) {
180 ret = consumer_send_relayd_streams_sent(relayd_id);
181 if (ret < 0) {
182 ERR("sending streams sent to relayd");
183 goto end_unlock;
184 }
185 channel->streams_sent_to_relayd = true;
186 }
187
188 ret = kernctl_buffer_flush(stream->wait_fd);
189 if (ret < 0) {
190 ERR("Failed to flush kernel stream");
191 goto end_unlock;
192 }
193
194 ret = lttng_kconsumer_take_snapshot(stream);
195 if (ret < 0) {
196 ERR("Taking kernel snapshot");
197 goto end_unlock;
198 }
199
200 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
201 if (ret < 0) {
202 ERR("Produced kernel snapshot position");
203 goto end_unlock;
204 }
205
206 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
207 if (ret < 0) {
208 ERR("Consumerd kernel snapshot position");
209 goto end_unlock;
210 }
211
212 if (stream->max_sb_size == 0) {
213 ret = kernctl_get_max_subbuf_size(stream->wait_fd,
214 &stream->max_sb_size);
215 if (ret < 0) {
216 ERR("Getting kernel max_sb_size");
217 goto end_unlock;
218 }
219 }
220
221 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
222 produced_pos, nb_packets_per_stream,
223 stream->max_sb_size);
224
225 while (consumed_pos < produced_pos) {
226 ssize_t read_len;
227 unsigned long len, padded_len;
228 int lost_packet = 0;
229
230 health_code_update();
231
232 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
233
234 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
235 if (ret < 0) {
236 if (ret != -EAGAIN) {
237 PERROR("kernctl_get_subbuf snapshot");
238 goto end_unlock;
239 }
240 DBG("Kernel consumer get subbuf failed. Skipping it.");
241 consumed_pos += stream->max_sb_size;
242
243 /*
244 * Start accounting lost packets only when we
245 * already have extracted packets (to match the
246 * content of the final snapshot).
247 */
248 if (!before_first_packet) {
249 lost_packet = 1;
250 }
251 continue;
252 }
253
254 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
255 if (ret < 0) {
256 ERR("Snapshot kernctl_get_subbuf_size");
257 goto error_put_subbuf;
258 }
259
260 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
261 if (ret < 0) {
262 ERR("Snapshot kernctl_get_padded_subbuf_size");
263 goto error_put_subbuf;
264 }
265
266 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
267 padded_len - len, NULL);
268 /*
269 * We write the padded len in local tracefiles but the data len
270 * when using a relay. Display the error but continue processing
271 * to try to release the subbuffer.
272 */
273 if (relayd_id != (uint64_t) -1ULL) {
274 if (read_len != len) {
275 ERR("Error sending to the relay (ret: %zd != len: %lu)",
276 read_len, len);
277 }
278 } else {
279 if (read_len != padded_len) {
280 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
281 read_len, padded_len);
282 }
283 }
284
285 ret = kernctl_put_subbuf(stream->wait_fd);
286 if (ret < 0) {
287 ERR("Snapshot kernctl_put_subbuf");
288 goto end_unlock;
289 }
290 consumed_pos += stream->max_sb_size;
291
292 /*
293 * Only account lost packets located between
294 * succesfully extracted packets (do not account before
295 * and after since they are not visible in the
296 * resulting snapshot).
297 */
298 stream->chan->lost_packets += lost_packet;
299 lost_packet = 0;
300 before_first_packet = false;
301 }
302
303 if (relayd_id == (uint64_t) -1ULL) {
304 if (stream->out_fd >= 0) {
305 ret = close(stream->out_fd);
306 if (ret < 0) {
307 PERROR("Kernel consumer snapshot close out_fd");
308 goto end_unlock;
309 }
310 stream->out_fd = -1;
311 }
312 } else {
313 close_relayd_stream(stream);
314 stream->net_seq_idx = (uint64_t) -1ULL;
315 }
316 pthread_mutex_unlock(&stream->lock);
317 }
318
319 /* All good! */
320 ret = 0;
321 goto end;
322
323 error_put_subbuf:
324 ret = kernctl_put_subbuf(stream->wait_fd);
325 if (ret < 0) {
326 ERR("Snapshot kernctl_put_subbuf error path");
327 }
328 end_unlock:
329 pthread_mutex_unlock(&stream->lock);
330 end:
331 rcu_read_unlock();
332 return ret;
333 }
334
335 /*
336 * Read the whole metadata available for a snapshot.
337 *
338 * Returns 0 on success, < 0 on error
339 */
340 int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path,
341 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
342 {
343 int ret, use_relayd = 0;
344 ssize_t ret_read;
345 struct lttng_consumer_channel *metadata_channel;
346 struct lttng_consumer_stream *metadata_stream;
347
348 assert(ctx);
349
350 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
351 key, path);
352
353 rcu_read_lock();
354
355 metadata_channel = consumer_find_channel(key);
356 if (!metadata_channel) {
357 ERR("Kernel snapshot metadata not found for key %" PRIu64, key);
358 ret = -1;
359 goto error;
360 }
361
362 metadata_stream = metadata_channel->metadata_stream;
363 assert(metadata_stream);
364
365 /* Flag once that we have a valid relayd for the stream. */
366 if (relayd_id != (uint64_t) -1ULL) {
367 use_relayd = 1;
368 }
369
370 if (use_relayd) {
371 ret = consumer_send_relayd_stream(metadata_stream, path);
372 if (ret < 0) {
373 goto error;
374 }
375 } else {
376 ret = utils_create_stream_file(path, metadata_stream->name,
377 metadata_stream->chan->tracefile_size,
378 metadata_stream->tracefile_count_current,
379 metadata_stream->uid, metadata_stream->gid, NULL);
380 if (ret < 0) {
381 goto error;
382 }
383 metadata_stream->out_fd = ret;
384 }
385
386 do {
387 health_code_update();
388
389 ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
390 if (ret_read < 0) {
391 if (ret_read != -EAGAIN) {
392 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
393 ret_read);
394 goto error;
395 }
396 /* ret_read is negative at this point so we will exit the loop. */
397 continue;
398 }
399 } while (ret_read >= 0);
400
401 if (use_relayd) {
402 close_relayd_stream(metadata_stream);
403 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
404 } else {
405 if (metadata_stream->out_fd >= 0) {
406 ret = close(metadata_stream->out_fd);
407 if (ret < 0) {
408 PERROR("Kernel consumer snapshot metadata close out_fd");
409 /*
410 * Don't go on error here since the snapshot was successful at this
411 * point but somehow the close failed.
412 */
413 }
414 metadata_stream->out_fd = -1;
415 }
416 }
417
418 ret = 0;
419
420 cds_list_del(&metadata_stream->send_node);
421 consumer_stream_destroy(metadata_stream, NULL);
422 metadata_channel->metadata_stream = NULL;
423 error:
424 rcu_read_unlock();
425 return ret;
426 }
427
428 /*
429 * Receive command from session daemon and process it.
430 *
431 * Return 1 on success else a negative value or 0.
432 */
433 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
434 int sock, struct pollfd *consumer_sockpoll)
435 {
436 ssize_t ret;
437 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
438 struct lttcomm_consumer_msg msg;
439
440 health_code_update();
441
442 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
443 if (ret != sizeof(msg)) {
444 if (ret > 0) {
445 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
446 ret = -1;
447 }
448 return ret;
449 }
450
451 health_code_update();
452
453 /* Deprecated command */
454 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
455
456 health_code_update();
457
458 /* relayd needs RCU read-side protection */
459 rcu_read_lock();
460
461 switch (msg.cmd_type) {
462 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
463 {
464 /* Session daemon status message are handled in the following call. */
465 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
466 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
467 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
468 msg.u.relayd_sock.relayd_session_id);
469 goto end_nosignal;
470 }
471 case LTTNG_CONSUMER_ADD_CHANNEL:
472 {
473 struct lttng_consumer_channel *new_channel;
474 int ret_recv;
475
476 health_code_update();
477
478 /* First send a status message before receiving the fds. */
479 ret = consumer_send_status_msg(sock, ret_code);
480 if (ret < 0) {
481 /* Somehow, the session daemon is not responding anymore. */
482 goto error_fatal;
483 }
484
485 health_code_update();
486
487 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
488 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
489 msg.u.channel.session_id, msg.u.channel.pathname,
490 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
491 msg.u.channel.relayd_id, msg.u.channel.output,
492 msg.u.channel.tracefile_size,
493 msg.u.channel.tracefile_count, 0,
494 msg.u.channel.monitor,
495 msg.u.channel.live_timer_interval,
496 NULL, NULL);
497 if (new_channel == NULL) {
498 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
499 goto end_nosignal;
500 }
501 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
502 switch (msg.u.channel.output) {
503 case LTTNG_EVENT_SPLICE:
504 new_channel->output = CONSUMER_CHANNEL_SPLICE;
505 break;
506 case LTTNG_EVENT_MMAP:
507 new_channel->output = CONSUMER_CHANNEL_MMAP;
508 break;
509 default:
510 ERR("Channel output unknown %d", msg.u.channel.output);
511 goto end_nosignal;
512 }
513
514 /* Translate and save channel type. */
515 switch (msg.u.channel.type) {
516 case CONSUMER_CHANNEL_TYPE_DATA:
517 case CONSUMER_CHANNEL_TYPE_METADATA:
518 new_channel->type = msg.u.channel.type;
519 break;
520 default:
521 assert(0);
522 goto end_nosignal;
523 };
524
525 health_code_update();
526
527 if (ctx->on_recv_channel != NULL) {
528 ret_recv = ctx->on_recv_channel(new_channel);
529 if (ret_recv == 0) {
530 ret = consumer_add_channel(new_channel, ctx);
531 } else if (ret_recv < 0) {
532 goto end_nosignal;
533 }
534 } else {
535 ret = consumer_add_channel(new_channel, ctx);
536 }
537 if (CONSUMER_CHANNEL_TYPE_DATA) {
538 consumer_timer_live_start(new_channel,
539 msg.u.channel.live_timer_interval);
540 }
541
542 health_code_update();
543
544 /* If we received an error in add_channel, we need to report it. */
545 if (ret < 0) {
546 ret = consumer_send_status_msg(sock, ret);
547 if (ret < 0) {
548 goto error_fatal;
549 }
550 goto end_nosignal;
551 }
552
553 goto end_nosignal;
554 }
555 case LTTNG_CONSUMER_ADD_STREAM:
556 {
557 int fd;
558 struct lttng_pipe *stream_pipe;
559 struct lttng_consumer_stream *new_stream;
560 struct lttng_consumer_channel *channel;
561 int alloc_ret = 0;
562
563 /*
564 * Get stream's channel reference. Needed when adding the stream to the
565 * global hash table.
566 */
567 channel = consumer_find_channel(msg.u.stream.channel_key);
568 if (!channel) {
569 /*
570 * We could not find the channel. Can happen if cpu hotplug
571 * happens while tearing down.
572 */
573 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
574 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
575 }
576
577 health_code_update();
578
579 /* First send a status message before receiving the fds. */
580 ret = consumer_send_status_msg(sock, ret_code);
581 if (ret < 0) {
582 /* Somehow, the session daemon is not responding anymore. */
583 goto error_fatal;
584 }
585
586 health_code_update();
587
588 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
589 /* Channel was not found. */
590 goto end_nosignal;
591 }
592
593 /* Blocking call */
594 health_poll_entry();
595 ret = lttng_consumer_poll_socket(consumer_sockpoll);
596 health_poll_exit();
597 if (ret) {
598 goto error_fatal;
599 }
600
601 health_code_update();
602
603 /* Get stream file descriptor from socket */
604 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
605 if (ret != sizeof(fd)) {
606 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
607 rcu_read_unlock();
608 return ret;
609 }
610
611 health_code_update();
612
613 /*
614 * Send status code to session daemon only if the recv works. If the
615 * above recv() failed, the session daemon is notified through the
616 * error socket and the teardown is eventually done.
617 */
618 ret = consumer_send_status_msg(sock, ret_code);
619 if (ret < 0) {
620 /* Somehow, the session daemon is not responding anymore. */
621 goto end_nosignal;
622 }
623
624 health_code_update();
625
626 new_stream = consumer_allocate_stream(channel->key,
627 fd,
628 LTTNG_CONSUMER_ACTIVE_STREAM,
629 channel->name,
630 channel->uid,
631 channel->gid,
632 channel->relayd_id,
633 channel->session_id,
634 msg.u.stream.cpu,
635 &alloc_ret,
636 channel->type,
637 channel->monitor);
638 if (new_stream == NULL) {
639 switch (alloc_ret) {
640 case -ENOMEM:
641 case -EINVAL:
642 default:
643 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
644 break;
645 }
646 goto end_nosignal;
647 }
648
649 new_stream->chan = channel;
650 new_stream->wait_fd = fd;
651 switch (channel->output) {
652 case CONSUMER_CHANNEL_SPLICE:
653 new_stream->output = LTTNG_EVENT_SPLICE;
654 ret = utils_create_pipe(new_stream->splice_pipe);
655 if (ret < 0) {
656 goto end_nosignal;
657 }
658 break;
659 case CONSUMER_CHANNEL_MMAP:
660 new_stream->output = LTTNG_EVENT_MMAP;
661 break;
662 default:
663 ERR("Stream output unknown %d", channel->output);
664 goto end_nosignal;
665 }
666
667 /*
668 * We've just assigned the channel to the stream so increment the
669 * refcount right now. We don't need to increment the refcount for
670 * streams in no monitor because we handle manually the cleanup of
671 * those. It is very important to make sure there is NO prior
672 * consumer_del_stream() calls or else the refcount will be unbalanced.
673 */
674 if (channel->monitor) {
675 uatomic_inc(&new_stream->chan->refcount);
676 }
677
678 /*
679 * The buffer flush is done on the session daemon side for the kernel
680 * so no need for the stream "hangup_flush_done" variable to be
681 * tracked. This is important for a kernel stream since we don't rely
682 * on the flush state of the stream to read data. It's not the case for
683 * user space tracing.
684 */
685 new_stream->hangup_flush_done = 0;
686
687 health_code_update();
688
689 if (ctx->on_recv_stream) {
690 ret = ctx->on_recv_stream(new_stream);
691 if (ret < 0) {
692 consumer_stream_free(new_stream);
693 goto end_nosignal;
694 }
695 }
696
697 health_code_update();
698
699 if (new_stream->metadata_flag) {
700 channel->metadata_stream = new_stream;
701 }
702
703 /* Do not monitor this stream. */
704 if (!channel->monitor) {
705 DBG("Kernel consumer add stream %s in no monitor mode with "
706 "relayd id %" PRIu64, new_stream->name,
707 new_stream->net_seq_idx);
708 cds_list_add(&new_stream->send_node, &channel->streams.head);
709 break;
710 }
711
712 /* Send stream to relayd if the stream has an ID. */
713 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
714 ret = consumer_send_relayd_stream(new_stream,
715 new_stream->chan->pathname);
716 if (ret < 0) {
717 consumer_stream_free(new_stream);
718 goto end_nosignal;
719 }
720
721 /*
722 * If adding an extra stream to an already
723 * existing channel (e.g. cpu hotplug), we need
724 * to send the "streams_sent" command to relayd.
725 */
726 if (channel->streams_sent_to_relayd) {
727 ret = consumer_send_relayd_streams_sent(
728 new_stream->net_seq_idx);
729 if (ret < 0) {
730 goto end_nosignal;
731 }
732 }
733 }
734
735 /* Get the right pipe where the stream will be sent. */
736 if (new_stream->metadata_flag) {
737 ret = consumer_add_metadata_stream(new_stream);
738 if (ret) {
739 ERR("Consumer add metadata stream %" PRIu64 " failed. Continuing",
740 new_stream->key);
741 consumer_stream_free(new_stream);
742 goto end_nosignal;
743 }
744 stream_pipe = ctx->consumer_metadata_pipe;
745 } else {
746 ret = consumer_add_data_stream(new_stream);
747 if (ret) {
748 ERR("Consumer add stream %" PRIu64 " failed. Continuing",
749 new_stream->key);
750 consumer_stream_free(new_stream);
751 goto end_nosignal;
752 }
753 stream_pipe = ctx->consumer_data_pipe;
754 }
755
756 /* Vitible to other threads */
757 new_stream->globally_visible = 1;
758
759 health_code_update();
760
761 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
762 if (ret < 0) {
763 ERR("Consumer write %s stream to pipe %d",
764 new_stream->metadata_flag ? "metadata" : "data",
765 lttng_pipe_get_writefd(stream_pipe));
766 if (new_stream->metadata_flag) {
767 consumer_del_stream_for_metadata(new_stream);
768 } else {
769 consumer_del_stream_for_data(new_stream);
770 }
771 goto end_nosignal;
772 }
773
774 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
775 new_stream->name, fd, new_stream->relayd_stream_id);
776 break;
777 }
778 case LTTNG_CONSUMER_STREAMS_SENT:
779 {
780 struct lttng_consumer_channel *channel;
781
782 /*
783 * Get stream's channel reference. Needed when adding the stream to the
784 * global hash table.
785 */
786 channel = consumer_find_channel(msg.u.sent_streams.channel_key);
787 if (!channel) {
788 /*
789 * We could not find the channel. Can happen if cpu hotplug
790 * happens while tearing down.
791 */
792 ERR("Unable to find channel key %" PRIu64,
793 msg.u.sent_streams.channel_key);
794 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
795 }
796
797 health_code_update();
798
799 /*
800 * Send status code to session daemon.
801 */
802 ret = consumer_send_status_msg(sock, ret_code);
803 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
804 /* Somehow, the session daemon is not responding anymore. */
805 goto end_nosignal;
806 }
807
808 health_code_update();
809
810 /*
811 * We should not send this message if we don't monitor the
812 * streams in this channel.
813 */
814 if (!channel->monitor) {
815 break;
816 }
817
818 health_code_update();
819 /* Send stream to relayd if the stream has an ID. */
820 if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) {
821 ret = consumer_send_relayd_streams_sent(
822 msg.u.sent_streams.net_seq_idx);
823 if (ret < 0) {
824 goto end_nosignal;
825 }
826 channel->streams_sent_to_relayd = true;
827 }
828 break;
829 }
830 case LTTNG_CONSUMER_UPDATE_STREAM:
831 {
832 rcu_read_unlock();
833 return -ENOSYS;
834 }
835 case LTTNG_CONSUMER_DESTROY_RELAYD:
836 {
837 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
838 struct consumer_relayd_sock_pair *relayd;
839
840 DBG("Kernel consumer destroying relayd %" PRIu64, index);
841
842 /* Get relayd reference if exists. */
843 relayd = consumer_find_relayd(index);
844 if (relayd == NULL) {
845 DBG("Unable to find relayd %" PRIu64, index);
846 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
847 }
848
849 /*
850 * Each relayd socket pair has a refcount of stream attached to it
851 * which tells if the relayd is still active or not depending on the
852 * refcount value.
853 *
854 * This will set the destroy flag of the relayd object and destroy it
855 * if the refcount reaches zero when called.
856 *
857 * The destroy can happen either here or when a stream fd hangs up.
858 */
859 if (relayd) {
860 consumer_flag_relayd_for_destroy(relayd);
861 }
862
863 health_code_update();
864
865 ret = consumer_send_status_msg(sock, ret_code);
866 if (ret < 0) {
867 /* Somehow, the session daemon is not responding anymore. */
868 goto error_fatal;
869 }
870
871 goto end_nosignal;
872 }
873 case LTTNG_CONSUMER_DATA_PENDING:
874 {
875 int32_t ret;
876 uint64_t id = msg.u.data_pending.session_id;
877
878 DBG("Kernel consumer data pending command for id %" PRIu64, id);
879
880 ret = consumer_data_pending(id);
881
882 health_code_update();
883
884 /* Send back returned value to session daemon */
885 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
886 if (ret < 0) {
887 PERROR("send data pending ret code");
888 goto error_fatal;
889 }
890
891 /*
892 * No need to send back a status message since the data pending
893 * returned value is the response.
894 */
895 break;
896 }
897 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
898 {
899 if (msg.u.snapshot_channel.metadata == 1) {
900 ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key,
901 msg.u.snapshot_channel.pathname,
902 msg.u.snapshot_channel.relayd_id, ctx);
903 if (ret < 0) {
904 ERR("Snapshot metadata failed");
905 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
906 }
907 } else {
908 ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key,
909 msg.u.snapshot_channel.pathname,
910 msg.u.snapshot_channel.relayd_id,
911 msg.u.snapshot_channel.nb_packets_per_stream,
912 ctx);
913 if (ret < 0) {
914 ERR("Snapshot channel failed");
915 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
916 }
917 }
918
919 health_code_update();
920
921 ret = consumer_send_status_msg(sock, ret_code);
922 if (ret < 0) {
923 /* Somehow, the session daemon is not responding anymore. */
924 goto end_nosignal;
925 }
926 break;
927 }
928 case LTTNG_CONSUMER_DESTROY_CHANNEL:
929 {
930 uint64_t key = msg.u.destroy_channel.key;
931 struct lttng_consumer_channel *channel;
932
933 channel = consumer_find_channel(key);
934 if (!channel) {
935 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
936 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
937 }
938
939 health_code_update();
940
941 ret = consumer_send_status_msg(sock, ret_code);
942 if (ret < 0) {
943 /* Somehow, the session daemon is not responding anymore. */
944 goto end_nosignal;
945 }
946
947 health_code_update();
948
949 /* Stop right now if no channel was found. */
950 if (!channel) {
951 goto end_nosignal;
952 }
953
954 /*
955 * This command should ONLY be issued for channel with streams set in
956 * no monitor mode.
957 */
958 assert(!channel->monitor);
959
960 /*
961 * The refcount should ALWAYS be 0 in the case of a channel in no
962 * monitor mode.
963 */
964 assert(!uatomic_sub_return(&channel->refcount, 1));
965
966 consumer_del_channel(channel);
967
968 goto end_nosignal;
969 }
970 case LTTNG_CONSUMER_DISCARDED_EVENTS:
971 {
972 uint64_t ret;
973 struct lttng_consumer_channel *channel;
974 uint64_t id = msg.u.discarded_events.session_id;
975 uint64_t key = msg.u.discarded_events.channel_key;
976
977 DBG("Kernel consumer discarded events command for session id %"
978 PRIu64 ", channel key %" PRIu64, id, key);
979
980 channel = consumer_find_channel(key);
981 if (!channel) {
982 ERR("Kernel consumer discarded events channel %"
983 PRIu64 " not found", key);
984 ret = 0;
985 } else {
986 ret = channel->discarded_events;
987 }
988
989 health_code_update();
990
991 /* Send back returned value to session daemon */
992 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
993 if (ret < 0) {
994 PERROR("send discarded events");
995 goto error_fatal;
996 }
997
998 break;
999 }
1000 case LTTNG_CONSUMER_LOST_PACKETS:
1001 {
1002 uint64_t ret;
1003 struct lttng_consumer_channel *channel;
1004 uint64_t id = msg.u.lost_packets.session_id;
1005 uint64_t key = msg.u.lost_packets.channel_key;
1006
1007 DBG("Kernel consumer lost packets command for session id %"
1008 PRIu64 ", channel key %" PRIu64, id, key);
1009
1010 channel = consumer_find_channel(key);
1011 if (!channel) {
1012 ERR("Kernel consumer lost packets channel %"
1013 PRIu64 " not found", key);
1014 ret = 0;
1015 } else {
1016 ret = channel->lost_packets;
1017 }
1018
1019 health_code_update();
1020
1021 /* Send back returned value to session daemon */
1022 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
1023 if (ret < 0) {
1024 PERROR("send lost packets");
1025 goto error_fatal;
1026 }
1027
1028 break;
1029 }
1030 default:
1031 goto end_nosignal;
1032 }
1033
1034 end_nosignal:
1035 rcu_read_unlock();
1036
1037 /*
1038 * Return 1 to indicate success since the 0 value can be a socket
1039 * shutdown during the recv() or send() call.
1040 */
1041 health_code_update();
1042 return 1;
1043
1044 error_fatal:
1045 rcu_read_unlock();
1046 /* This will issue a consumer stop. */
1047 return -1;
1048 }
1049
1050 /*
1051 * Populate index values of a kernel stream. Values are set in big endian order.
1052 *
1053 * Return 0 on success or else a negative value.
1054 */
1055 static int get_index_values(struct ctf_packet_index *index, int infd)
1056 {
1057 int ret;
1058
1059 ret = kernctl_get_timestamp_begin(infd, &index->timestamp_begin);
1060 if (ret < 0) {
1061 PERROR("kernctl_get_timestamp_begin");
1062 goto error;
1063 }
1064 index->timestamp_begin = htobe64(index->timestamp_begin);
1065
1066 ret = kernctl_get_timestamp_end(infd, &index->timestamp_end);
1067 if (ret < 0) {
1068 PERROR("kernctl_get_timestamp_end");
1069 goto error;
1070 }
1071 index->timestamp_end = htobe64(index->timestamp_end);
1072
1073 ret = kernctl_get_events_discarded(infd, &index->events_discarded);
1074 if (ret < 0) {
1075 PERROR("kernctl_get_events_discarded");
1076 goto error;
1077 }
1078 index->events_discarded = htobe64(index->events_discarded);
1079
1080 ret = kernctl_get_content_size(infd, &index->content_size);
1081 if (ret < 0) {
1082 PERROR("kernctl_get_content_size");
1083 goto error;
1084 }
1085 index->content_size = htobe64(index->content_size);
1086
1087 ret = kernctl_get_packet_size(infd, &index->packet_size);
1088 if (ret < 0) {
1089 PERROR("kernctl_get_packet_size");
1090 goto error;
1091 }
1092 index->packet_size = htobe64(index->packet_size);
1093
1094 ret = kernctl_get_stream_id(infd, &index->stream_id);
1095 if (ret < 0) {
1096 PERROR("kernctl_get_stream_id");
1097 goto error;
1098 }
1099 index->stream_id = htobe64(index->stream_id);
1100
1101 ret = kernctl_get_instance_id(infd, &index->stream_instance_id);
1102 if (ret < 0) {
1103 if (ret == -ENOTTY) {
1104 /* Command not implemented by lttng-modules. */
1105 index->stream_instance_id = -1ULL;
1106 ret = 0;
1107 } else {
1108 PERROR("kernctl_get_instance_id");
1109 goto error;
1110 }
1111 }
1112 index->stream_instance_id = htobe64(index->stream_instance_id);
1113
1114 ret = kernctl_get_sequence_number(infd, &index->packet_seq_num);
1115 if (ret < 0) {
1116 if (ret == -ENOTTY) {
1117 /* Command not implemented by lttng-modules. */
1118 index->packet_seq_num = -1ULL;
1119 ret = 0;
1120 } else {
1121 PERROR("kernctl_get_sequence_number");
1122 goto error;
1123 }
1124 }
1125 index->packet_seq_num = htobe64(index->packet_seq_num);
1126
1127 error:
1128 return ret;
1129 }
1130 /*
1131 * Sync metadata meaning request them to the session daemon and snapshot to the
1132 * metadata thread can consumer them.
1133 *
1134 * Metadata stream lock MUST be acquired.
1135 *
1136 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1137 * is empty or a negative value on error.
1138 */
1139 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata)
1140 {
1141 int ret;
1142
1143 assert(metadata);
1144
1145 ret = kernctl_buffer_flush(metadata->wait_fd);
1146 if (ret < 0) {
1147 ERR("Failed to flush kernel stream");
1148 goto end;
1149 }
1150
1151 ret = kernctl_snapshot(metadata->wait_fd);
1152 if (ret < 0) {
1153 if (ret != -EAGAIN) {
1154 ERR("Sync metadata, taking kernel snapshot failed.");
1155 goto end;
1156 }
1157 DBG("Sync metadata, no new kernel metadata");
1158 /* No new metadata, exit. */
1159 ret = ENODATA;
1160 goto end;
1161 }
1162
1163 end:
1164 return ret;
1165 }
1166
1167 static
1168 int update_stream_stats(struct lttng_consumer_stream *stream)
1169 {
1170 int ret;
1171 uint64_t seq, discarded;
1172
1173 ret = kernctl_get_sequence_number(stream->wait_fd, &seq);
1174 if (ret < 0) {
1175 if (ret == -ENOTTY) {
1176 /* Command not implemented by lttng-modules. */
1177 seq = -1ULL;
1178 ret = 0;
1179 } else {
1180 PERROR("kernctl_get_sequence_number");
1181 goto end;
1182 }
1183 }
1184
1185 /*
1186 * Start the sequence when we extract the first packet in case we don't
1187 * start at 0 (for example if a consumer is not connected to the
1188 * session immediately after the beginning).
1189 */
1190 if (stream->last_sequence_number == -1ULL) {
1191 stream->last_sequence_number = seq;
1192 } else if (seq > stream->last_sequence_number) {
1193 stream->chan->lost_packets += seq -
1194 stream->last_sequence_number - 1;
1195 } else {
1196 /* seq <= last_sequence_number */
1197 ERR("Sequence number inconsistent : prev = %" PRIu64
1198 ", current = %" PRIu64,
1199 stream->last_sequence_number, seq);
1200 ret = -1;
1201 goto end;
1202 }
1203 stream->last_sequence_number = seq;
1204
1205 ret = kernctl_get_events_discarded(stream->wait_fd, &discarded);
1206 if (ret < 0) {
1207 PERROR("kernctl_get_events_discarded");
1208 goto end;
1209 }
1210 if (discarded < stream->last_discarded_events) {
1211 /*
1212 * Overflow has occurred. We assume only one wrap-around
1213 * has occurred.
1214 */
1215 stream->chan->discarded_events += (1ULL << (CAA_BITS_PER_LONG - 1)) -
1216 stream->last_discarded_events + discarded;
1217 } else {
1218 stream->chan->discarded_events += discarded -
1219 stream->last_discarded_events;
1220 }
1221 stream->last_discarded_events = discarded;
1222 ret = 0;
1223
1224 end:
1225 return ret;
1226 }
1227
1228 /*
1229 * Check if the local version of the metadata stream matches with the version
1230 * of the metadata stream in the kernel. If it was updated, set the reset flag
1231 * on the stream.
1232 */
1233 static
1234 int metadata_stream_check_version(int infd, struct lttng_consumer_stream *stream)
1235 {
1236 int ret;
1237 uint64_t cur_version;
1238
1239 ret = kernctl_get_metadata_version(infd, &cur_version);
1240 if (ret < 0) {
1241 if (ret == -ENOTTY) {
1242 /*
1243 * LTTng-modules does not implement this
1244 * command.
1245 */
1246 ret = 0;
1247 goto end;
1248 }
1249 ERR("Failed to get the metadata version");
1250 goto end;
1251 }
1252
1253 if (stream->metadata_version == cur_version) {
1254 ret = 0;
1255 goto end;
1256 }
1257
1258 DBG("New metadata version detected");
1259 stream->metadata_version = cur_version;
1260 stream->reset_metadata_flag = 1;
1261 ret = 0;
1262
1263 end:
1264 return ret;
1265 }
1266
1267 /*
1268 * Consume data on a file descriptor and write it on a trace file.
1269 */
1270 ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
1271 struct lttng_consumer_local_data *ctx)
1272 {
1273 unsigned long len, subbuf_size, padding;
1274 int err, write_index = 1;
1275 ssize_t ret = 0;
1276 int infd = stream->wait_fd;
1277 struct ctf_packet_index index;
1278
1279 DBG("In read_subbuffer (infd : %d)", infd);
1280
1281 /* Get the next subbuffer */
1282 err = kernctl_get_next_subbuf(infd);
1283 if (err != 0) {
1284 /*
1285 * This is a debug message even for single-threaded consumer,
1286 * because poll() have more relaxed criterions than get subbuf,
1287 * so get_subbuf may fail for short race windows where poll()
1288 * would issue wakeups.
1289 */
1290 DBG("Reserving sub buffer failed (everything is normal, "
1291 "it is due to concurrency)");
1292 ret = err;
1293 goto end;
1294 }
1295
1296 /* Get the full subbuffer size including padding */
1297 err = kernctl_get_padded_subbuf_size(infd, &len);
1298 if (err != 0) {
1299 PERROR("Getting sub-buffer len failed.");
1300 err = kernctl_put_subbuf(infd);
1301 if (err != 0) {
1302 if (err == -EFAULT) {
1303 PERROR("Error in unreserving sub buffer\n");
1304 } else if (err == -EIO) {
1305 /* Should never happen with newer LTTng versions */
1306 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1307 }
1308 ret = err;
1309 goto end;
1310 }
1311 ret = err;
1312 goto end;
1313 }
1314
1315 if (!stream->metadata_flag) {
1316 ret = get_index_values(&index, infd);
1317 if (ret < 0) {
1318 err = kernctl_put_subbuf(infd);
1319 if (err != 0) {
1320 if (err == -EFAULT) {
1321 PERROR("Error in unreserving sub buffer\n");
1322 } else if (err == -EIO) {
1323 /* Should never happen with newer LTTng versions */
1324 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1325 }
1326 ret = err;
1327 goto end;
1328 }
1329 goto end;
1330 }
1331 ret = update_stream_stats(stream);
1332 if (ret < 0) {
1333 err = kernctl_put_subbuf(infd);
1334 if (err != 0) {
1335 if (err == -EFAULT) {
1336 PERROR("Error in unreserving sub buffer\n");
1337 } else if (err == -EIO) {
1338 /* Should never happen with newer LTTng versions */
1339 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1340 }
1341 ret = err;
1342 goto end;
1343 }
1344 goto end;
1345 }
1346 } else {
1347 write_index = 0;
1348 ret = metadata_stream_check_version(infd, stream);
1349 if (ret < 0) {
1350 err = kernctl_put_subbuf(infd);
1351 if (err != 0) {
1352 if (err == -EFAULT) {
1353 PERROR("Error in unreserving sub buffer\n");
1354 } else if (err == -EIO) {
1355 /* Should never happen with newer LTTng versions */
1356 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1357 }
1358 ret = err;
1359 goto end;
1360 }
1361 goto end;
1362 }
1363 }
1364
1365 switch (stream->chan->output) {
1366 case CONSUMER_CHANNEL_SPLICE:
1367 /*
1368 * XXX: The lttng-modules splice "actor" does not handle copying
1369 * partial pages hence only using the subbuffer size without the
1370 * padding makes the splice fail.
1371 */
1372 subbuf_size = len;
1373 padding = 0;
1374
1375 /* splice the subbuffer to the tracefile */
1376 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
1377 padding, &index);
1378 /*
1379 * XXX: Splice does not support network streaming so the return value
1380 * is simply checked against subbuf_size and not like the mmap() op.
1381 */
1382 if (ret != subbuf_size) {
1383 /*
1384 * display the error but continue processing to try
1385 * to release the subbuffer
1386 */
1387 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1388 ret, subbuf_size);
1389 write_index = 0;
1390 }
1391 break;
1392 case CONSUMER_CHANNEL_MMAP:
1393 /* Get subbuffer size without padding */
1394 err = kernctl_get_subbuf_size(infd, &subbuf_size);
1395 if (err != 0) {
1396 PERROR("Getting sub-buffer len failed.");
1397 err = kernctl_put_subbuf(infd);
1398 if (err != 0) {
1399 if (err == -EFAULT) {
1400 PERROR("Error in unreserving sub buffer\n");
1401 } else if (err == -EIO) {
1402 /* Should never happen with newer LTTng versions */
1403 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1404 }
1405 ret = err;
1406 goto end;
1407 }
1408 ret = err;
1409 goto end;
1410 }
1411
1412 /* Make sure the tracer is not gone mad on us! */
1413 assert(len >= subbuf_size);
1414
1415 padding = len - subbuf_size;
1416
1417 /* write the subbuffer to the tracefile */
1418 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
1419 padding, &index);
1420 /*
1421 * The mmap operation should write subbuf_size amount of data when
1422 * network streaming or the full padding (len) size when we are _not_
1423 * streaming.
1424 */
1425 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
1426 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1427 /*
1428 * Display the error but continue processing to try to release the
1429 * subbuffer. This is a DBG statement since this is possible to
1430 * happen without being a critical error.
1431 */
1432 DBG("Error writing to tracefile "
1433 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1434 ret, len, subbuf_size);
1435 write_index = 0;
1436 }
1437 break;
1438 default:
1439 ERR("Unknown output method");
1440 ret = -EPERM;
1441 }
1442
1443 err = kernctl_put_next_subbuf(infd);
1444 if (err != 0) {
1445 if (err == -EFAULT) {
1446 PERROR("Error in unreserving sub buffer\n");
1447 } else if (err == -EIO) {
1448 /* Should never happen with newer LTTng versions */
1449 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1450 }
1451 ret = err;
1452 goto end;
1453 }
1454
1455 /* Write index if needed. */
1456 if (!write_index) {
1457 goto end;
1458 }
1459
1460 if (stream->chan->live_timer_interval && !stream->metadata_flag) {
1461 /*
1462 * In live, block until all the metadata is sent.
1463 */
1464 pthread_mutex_lock(&stream->metadata_timer_lock);
1465 assert(!stream->missed_metadata_flush);
1466 stream->waiting_on_metadata = true;
1467 pthread_mutex_unlock(&stream->metadata_timer_lock);
1468
1469 err = consumer_stream_sync_metadata(ctx, stream->session_id);
1470
1471 pthread_mutex_lock(&stream->metadata_timer_lock);
1472 stream->waiting_on_metadata = false;
1473 if (stream->missed_metadata_flush) {
1474 stream->missed_metadata_flush = false;
1475 pthread_mutex_unlock(&stream->metadata_timer_lock);
1476 (void) consumer_flush_kernel_index(stream);
1477 } else {
1478 pthread_mutex_unlock(&stream->metadata_timer_lock);
1479 }
1480 if (err < 0) {
1481 goto end;
1482 }
1483 }
1484
1485 err = consumer_stream_write_index(stream, &index);
1486 if (err < 0) {
1487 goto end;
1488 }
1489
1490 end:
1491 return ret;
1492 }
1493
1494 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1495 {
1496 int ret;
1497
1498 assert(stream);
1499
1500 /*
1501 * Don't create anything if this is set for streaming or should not be
1502 * monitored.
1503 */
1504 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
1505 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
1506 stream->chan->tracefile_size, stream->tracefile_count_current,
1507 stream->uid, stream->gid, NULL);
1508 if (ret < 0) {
1509 goto error;
1510 }
1511 stream->out_fd = ret;
1512 stream->tracefile_size_current = 0;
1513
1514 if (!stream->metadata_flag) {
1515 struct lttng_index_file *index_file;
1516
1517 index_file = lttng_index_file_create(stream->chan->pathname,
1518 stream->name, stream->uid, stream->gid,
1519 stream->chan->tracefile_size,
1520 stream->tracefile_count_current,
1521 CTF_INDEX_MAJOR, CTF_INDEX_MINOR);
1522 if (!index_file) {
1523 goto error;
1524 }
1525 stream->index_file = index_file;
1526 }
1527 }
1528
1529 if (stream->output == LTTNG_EVENT_MMAP) {
1530 /* get the len of the mmap region */
1531 unsigned long mmap_len;
1532
1533 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
1534 if (ret != 0) {
1535 PERROR("kernctl_get_mmap_len");
1536 goto error_close_fd;
1537 }
1538 stream->mmap_len = (size_t) mmap_len;
1539
1540 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
1541 MAP_PRIVATE, stream->wait_fd, 0);
1542 if (stream->mmap_base == MAP_FAILED) {
1543 PERROR("Error mmaping");
1544 ret = -1;
1545 goto error_close_fd;
1546 }
1547 }
1548
1549 /* we return 0 to let the library handle the FD internally */
1550 return 0;
1551
1552 error_close_fd:
1553 if (stream->out_fd >= 0) {
1554 int err;
1555
1556 err = close(stream->out_fd);
1557 assert(!err);
1558 stream->out_fd = -1;
1559 }
1560 error:
1561 return ret;
1562 }
1563
1564 /*
1565 * Check if data is still being extracted from the buffers for a specific
1566 * stream. Consumer data lock MUST be acquired before calling this function
1567 * and the stream lock.
1568 *
1569 * Return 1 if the traced data are still getting read else 0 meaning that the
1570 * data is available for trace viewer reading.
1571 */
1572 int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
1573 {
1574 int ret;
1575
1576 assert(stream);
1577
1578 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1579 ret = 0;
1580 goto end;
1581 }
1582
1583 ret = kernctl_get_next_subbuf(stream->wait_fd);
1584 if (ret == 0) {
1585 /* There is still data so let's put back this subbuffer. */
1586 ret = kernctl_put_subbuf(stream->wait_fd);
1587 assert(ret == 0);
1588 ret = 1; /* Data is pending */
1589 goto end;
1590 }
1591
1592 /* Data is NOT pending and ready to be read. */
1593 ret = 0;
1594
1595 end:
1596 return ret;
1597 }
This page took 0.106409 seconds and 3 git commands to generate.