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