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