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