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