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