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