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