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