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