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