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