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