2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
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>
47 #include "kernel-consumer.h"
49 extern struct lttng_consumer_global_data consumer_data
;
50 extern int consumer_poll_timeout
;
51 extern volatile int consumer_quit
;
54 * Take a snapshot for a specific fd
56 * Returns 0 on success, < 0 on error
58 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
61 int infd
= stream
->wait_fd
;
63 ret
= kernctl_snapshot(infd
);
65 * -EAGAIN is not an error, it just means that there is no data to
68 if (ret
!= 0 && ret
!= -EAGAIN
) {
69 PERROR("Getting sub-buffer snapshot.");
76 * Get the produced position
78 * Returns 0 on success, < 0 on error
80 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
84 int infd
= stream
->wait_fd
;
86 ret
= kernctl_snapshot_get_produced(infd
, pos
);
88 PERROR("kernctl_snapshot_get_produced");
95 * Get the consumerd position
97 * Returns 0 on success, < 0 on error
99 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
103 int infd
= stream
->wait_fd
;
105 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
107 PERROR("kernctl_snapshot_get_consumed");
114 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
118 unsigned long mmap_offset
;
119 const char *mmap_base
= stream
->mmap_base
;
121 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
123 PERROR("Failed to get mmap read offset");
127 *addr
= mmap_base
+ mmap_offset
;
133 * Take a snapshot of all the stream of a channel
135 * Returns 0 on success, < 0 on error
137 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
138 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
139 struct lttng_consumer_local_data
*ctx
)
142 struct lttng_consumer_channel
*channel
;
143 struct lttng_consumer_stream
*stream
;
145 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
149 channel
= consumer_find_channel(key
);
151 ERR("No channel found for key %" PRIu64
, key
);
156 /* Splice is not supported yet for channel snapshot. */
157 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
158 ERR("Unsupported output %d", channel
->output
);
163 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
164 unsigned long consumed_pos
, produced_pos
;
166 health_code_update();
169 * Lock stream because we are about to change its state.
171 pthread_mutex_lock(&stream
->lock
);
174 * Assign the received relayd ID so we can use it for streaming. The streams
175 * are not visible to anyone so this is OK to change it.
177 stream
->relayd_id
= relayd_id
;
178 channel
->relayd_id
= relayd_id
;
179 if (relayd_id
!= (uint64_t) -1ULL) {
180 ret
= consumer_send_relayd_stream(stream
, path
);
182 ERR("sending stream to relayd");
186 ret
= utils_create_stream_file(path
, stream
->name
,
187 stream
->chan
->tracefile_size
,
188 stream
->tracefile_count_current
,
189 stream
->uid
, stream
->gid
, NULL
);
191 ERR("utils_create_stream_file");
195 stream
->out_fd
= ret
;
196 stream
->tracefile_size_current
= 0;
198 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
199 path
, stream
->name
, stream
->key
);
201 if (relayd_id
!= -1ULL) {
202 ret
= consumer_send_relayd_streams_sent(relayd_id
);
204 ERR("sending streams sent to relayd");
207 channel
->streams_sent_to_relayd
= true;
210 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
213 * Doing a buffer flush which does not take into
214 * account empty packets. This is not perfect
215 * for stream intersection, but required as a
216 * fall-back when "flush_empty" is not
217 * implemented by lttng-modules.
219 ret
= kernctl_buffer_flush(stream
->wait_fd
);
221 ERR("Failed to flush kernel stream");
227 ret
= lttng_kconsumer_take_snapshot(stream
);
229 ERR("Taking kernel snapshot");
233 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
235 ERR("Produced kernel snapshot position");
239 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
241 ERR("Consumerd kernel snapshot position");
245 if (stream
->max_sb_size
== 0) {
246 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
247 &stream
->max_sb_size
);
249 ERR("Getting kernel max_sb_size");
254 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
255 produced_pos
, nb_packets_per_stream
,
256 stream
->max_sb_size
);
258 while (consumed_pos
< produced_pos
) {
260 unsigned long len
, padded_len
;
261 const char *subbuf_addr
;
263 health_code_update();
265 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
267 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
269 if (ret
!= -EAGAIN
) {
270 PERROR("kernctl_get_subbuf snapshot");
273 DBG("Kernel consumer get subbuf failed. Skipping it.");
274 consumed_pos
+= stream
->max_sb_size
;
275 stream
->chan
->lost_packets
++;
279 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
281 ERR("Snapshot kernctl_get_subbuf_size");
282 goto error_put_subbuf
;
285 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
287 ERR("Snapshot kernctl_get_padded_subbuf_size");
288 goto error_put_subbuf
;
291 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
293 goto error_put_subbuf
;
296 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
,
297 stream
, subbuf_addr
, len
,
298 padded_len
- len
, NULL
);
300 * We write the padded len in local tracefiles but the data len
301 * when using a relay. Display the error but continue processing
302 * to try to release the subbuffer.
304 if (relayd_id
!= (uint64_t) -1ULL) {
305 if (read_len
!= len
) {
306 ERR("Error sending to the relay (ret: %zd != len: %lu)",
310 if (read_len
!= padded_len
) {
311 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
312 read_len
, padded_len
);
316 ret
= kernctl_put_subbuf(stream
->wait_fd
);
318 ERR("Snapshot kernctl_put_subbuf");
321 consumed_pos
+= stream
->max_sb_size
;
324 if (relayd_id
== (uint64_t) -1ULL) {
325 if (stream
->out_fd
>= 0) {
326 ret
= close(stream
->out_fd
);
328 PERROR("Kernel consumer snapshot close out_fd");
334 close_relayd_stream(stream
);
335 stream
->relayd_id
= (uint64_t) -1ULL;
337 pthread_mutex_unlock(&stream
->lock
);
345 ret
= kernctl_put_subbuf(stream
->wait_fd
);
347 ERR("Snapshot kernctl_put_subbuf error path");
350 pthread_mutex_unlock(&stream
->lock
);
357 * Read the whole metadata available for a snapshot.
359 * Returns 0 on success, < 0 on error
361 static int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
362 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
364 int ret
, use_relayd
= 0;
366 struct lttng_consumer_channel
*metadata_channel
;
367 struct lttng_consumer_stream
*metadata_stream
;
371 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
376 metadata_channel
= consumer_find_channel(key
);
377 if (!metadata_channel
) {
378 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
380 goto error_no_channel
;
383 metadata_stream
= metadata_channel
->metadata_stream
;
384 assert(metadata_stream
);
385 pthread_mutex_lock(&metadata_stream
->lock
);
387 /* Flag once that we have a valid relayd for the stream. */
388 if (relayd_id
!= (uint64_t) -1ULL) {
393 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
398 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
399 metadata_stream
->chan
->tracefile_size
,
400 metadata_stream
->tracefile_count_current
,
401 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
405 metadata_stream
->out_fd
= ret
;
409 health_code_update();
411 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
413 if (ret_read
!= -EAGAIN
) {
414 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
419 /* ret_read is negative at this point so we will exit the loop. */
422 } while (ret_read
>= 0);
425 close_relayd_stream(metadata_stream
);
426 metadata_stream
->relayd_id
= (uint64_t) -1ULL;
428 if (metadata_stream
->out_fd
>= 0) {
429 ret
= close(metadata_stream
->out_fd
);
431 PERROR("Kernel consumer snapshot metadata close out_fd");
433 * Don't go on error here since the snapshot was successful at this
434 * point but somehow the close failed.
437 metadata_stream
->out_fd
= -1;
443 pthread_mutex_unlock(&metadata_stream
->lock
);
444 cds_list_del(&metadata_stream
->send_node
);
445 consumer_stream_destroy(metadata_stream
, NULL
);
446 metadata_channel
->metadata_stream
= NULL
;
453 * Receive command from session daemon and process it.
455 * Return 1 on success else a negative value or 0.
457 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
458 int sock
, struct pollfd
*consumer_sockpoll
)
461 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
462 struct lttcomm_consumer_msg msg
;
464 health_code_update();
466 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
467 if (ret
!= sizeof(msg
)) {
469 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
475 health_code_update();
477 /* Deprecated command */
478 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
480 health_code_update();
482 /* relayd needs RCU read-side protection */
485 switch (msg
.cmd_type
) {
486 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
488 /* Session daemon status message are handled in the following call. */
489 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
490 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
491 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
492 msg
.u
.relayd_sock
.relayd_session_id
);
495 case LTTNG_CONSUMER_ADD_CHANNEL
:
497 struct lttng_consumer_channel
*new_channel
;
500 health_code_update();
502 /* First send a status message before receiving the fds. */
503 ret
= consumer_send_status_msg(sock
, ret_code
);
505 /* Somehow, the session daemon is not responding anymore. */
509 health_code_update();
511 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
512 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
513 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
514 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
515 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
516 msg
.u
.channel
.tracefile_size
,
517 msg
.u
.channel
.tracefile_count
, 0,
518 msg
.u
.channel
.monitor
,
519 msg
.u
.channel
.live_timer_interval
,
521 if (new_channel
== NULL
) {
522 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
525 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
526 switch (msg
.u
.channel
.output
) {
527 case LTTNG_EVENT_SPLICE
:
528 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
530 case LTTNG_EVENT_MMAP
:
531 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
534 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
538 /* Translate and save channel type. */
539 switch (msg
.u
.channel
.type
) {
540 case CONSUMER_CHANNEL_TYPE_DATA
:
541 case CONSUMER_CHANNEL_TYPE_METADATA
:
542 new_channel
->type
= msg
.u
.channel
.type
;
549 health_code_update();
551 if (ctx
->on_recv_channel
!= NULL
) {
552 ret_recv
= ctx
->on_recv_channel(new_channel
);
554 ret
= consumer_add_channel(new_channel
, ctx
);
555 } else if (ret_recv
< 0) {
559 ret
= consumer_add_channel(new_channel
, ctx
);
561 if (CONSUMER_CHANNEL_TYPE_DATA
) {
562 consumer_timer_live_start(new_channel
,
563 msg
.u
.channel
.live_timer_interval
);
566 health_code_update();
568 /* If we received an error in add_channel, we need to report it. */
570 ret
= consumer_send_status_msg(sock
, ret
);
579 case LTTNG_CONSUMER_ADD_STREAM
:
582 struct lttng_pipe
*stream_pipe
;
583 struct lttng_consumer_stream
*new_stream
;
584 struct lttng_consumer_channel
*channel
;
588 * Get stream's channel reference. Needed when adding the stream to the
591 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
594 * We could not find the channel. Can happen if cpu hotplug
595 * happens while tearing down.
597 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
598 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
601 health_code_update();
603 /* First send a status message before receiving the fds. */
604 ret
= consumer_send_status_msg(sock
, ret_code
);
606 /* Somehow, the session daemon is not responding anymore. */
610 health_code_update();
612 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
613 /* Channel was not found. */
619 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
625 health_code_update();
627 /* Get stream file descriptor from socket */
628 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
629 if (ret
!= sizeof(fd
)) {
630 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
635 health_code_update();
638 * Send status code to session daemon only if the recv works. If the
639 * above recv() failed, the session daemon is notified through the
640 * error socket and the teardown is eventually done.
642 ret
= consumer_send_status_msg(sock
, ret_code
);
644 /* Somehow, the session daemon is not responding anymore. */
648 health_code_update();
650 new_stream
= consumer_allocate_stream(channel
->key
,
652 LTTNG_CONSUMER_ACTIVE_STREAM
,
662 if (new_stream
== NULL
) {
667 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
673 new_stream
->chan
= channel
;
674 new_stream
->wait_fd
= fd
;
675 switch (channel
->output
) {
676 case CONSUMER_CHANNEL_SPLICE
:
677 new_stream
->output
= LTTNG_EVENT_SPLICE
;
678 ret
= utils_create_pipe(new_stream
->splice_pipe
);
683 case CONSUMER_CHANNEL_MMAP
:
684 new_stream
->output
= LTTNG_EVENT_MMAP
;
687 ERR("Stream output unknown %d", channel
->output
);
692 * We've just assigned the channel to the stream so increment the
693 * refcount right now. We don't need to increment the refcount for
694 * streams in no monitor because we handle manually the cleanup of
695 * those. It is very important to make sure there is NO prior
696 * consumer_del_stream() calls or else the refcount will be unbalanced.
698 if (channel
->monitor
) {
699 uatomic_inc(&new_stream
->chan
->refcount
);
703 * The buffer flush is done on the session daemon side for the kernel
704 * so no need for the stream "hangup_flush_done" variable to be
705 * tracked. This is important for a kernel stream since we don't rely
706 * on the flush state of the stream to read data. It's not the case for
707 * user space tracing.
709 new_stream
->hangup_flush_done
= 0;
711 health_code_update();
713 if (ctx
->on_recv_stream
) {
714 ret
= ctx
->on_recv_stream(new_stream
);
716 consumer_stream_free(new_stream
);
721 health_code_update();
723 if (new_stream
->metadata_flag
) {
724 channel
->metadata_stream
= new_stream
;
727 /* Do not monitor this stream. */
728 if (!channel
->monitor
) {
729 DBG("Kernel consumer add stream %s in no monitor mode with "
730 "relayd id %" PRIu64
, new_stream
->name
,
731 new_stream
->relayd_id
);
732 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
736 /* Send stream to relayd if the stream has an ID. */
737 if (new_stream
->relayd_id
!= (uint64_t) -1ULL) {
738 ret
= consumer_send_relayd_stream(new_stream
,
739 new_stream
->chan
->pathname
);
741 consumer_stream_free(new_stream
);
746 * If adding an extra stream to an already
747 * existing channel (e.g. cpu hotplug), we need
748 * to send the "streams_sent" command to relayd.
750 if (channel
->streams_sent_to_relayd
) {
751 ret
= consumer_send_relayd_streams_sent(
752 new_stream
->relayd_id
);
759 /* Get the right pipe where the stream will be sent. */
760 if (new_stream
->metadata_flag
) {
761 ret
= consumer_add_metadata_stream(new_stream
);
763 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
765 consumer_stream_free(new_stream
);
768 stream_pipe
= ctx
->consumer_metadata_pipe
;
770 ret
= consumer_add_data_stream(new_stream
);
772 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
774 consumer_stream_free(new_stream
);
777 stream_pipe
= ctx
->consumer_data_pipe
;
780 /* Vitible to other threads */
781 new_stream
->globally_visible
= 1;
783 health_code_update();
785 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
787 ERR("Consumer write %s stream to pipe %d",
788 new_stream
->metadata_flag
? "metadata" : "data",
789 lttng_pipe_get_writefd(stream_pipe
));
790 if (new_stream
->metadata_flag
) {
791 consumer_del_stream_for_metadata(new_stream
);
793 consumer_del_stream_for_data(new_stream
);
798 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
799 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
802 case LTTNG_CONSUMER_STREAMS_SENT
:
804 struct lttng_consumer_channel
*channel
;
807 * Get stream's channel reference. Needed when adding the stream to the
810 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
813 * We could not find the channel. Can happen if cpu hotplug
814 * happens while tearing down.
816 ERR("Unable to find channel key %" PRIu64
,
817 msg
.u
.sent_streams
.channel_key
);
818 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
821 health_code_update();
824 * Send status code to session daemon.
826 ret
= consumer_send_status_msg(sock
, ret_code
);
827 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
828 /* Somehow, the session daemon is not responding anymore. */
832 health_code_update();
835 * We should not send this message if we don't monitor the
836 * streams in this channel.
838 if (!channel
->monitor
) {
842 health_code_update();
843 /* Send stream to relayd if the stream has an ID. */
844 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
845 ret
= consumer_send_relayd_streams_sent(
846 msg
.u
.sent_streams
.net_seq_idx
);
850 channel
->streams_sent_to_relayd
= true;
854 case LTTNG_CONSUMER_UPDATE_STREAM
:
859 case LTTNG_CONSUMER_DESTROY_RELAYD
:
861 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
862 struct consumer_relayd_sock_pair
*relayd
;
864 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
866 /* Get relayd reference if exists. */
867 relayd
= consumer_find_relayd(index
);
868 if (relayd
== NULL
) {
869 DBG("Unable to find relayd %" PRIu64
, index
);
870 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
874 * Each relayd socket pair has a refcount of stream attached to it
875 * which tells if the relayd is still active or not depending on the
878 * This will set the destroy flag of the relayd object and destroy it
879 * if the refcount reaches zero when called.
881 * The destroy can happen either here or when a stream fd hangs up.
884 consumer_flag_relayd_for_destroy(relayd
);
887 health_code_update();
889 ret
= consumer_send_status_msg(sock
, ret_code
);
891 /* Somehow, the session daemon is not responding anymore. */
897 case LTTNG_CONSUMER_DATA_PENDING
:
900 uint64_t id
= msg
.u
.data_pending
.session_id
;
902 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
904 ret
= consumer_data_pending(id
);
906 health_code_update();
908 /* Send back returned value to session daemon */
909 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
911 PERROR("send data pending ret code");
916 * No need to send back a status message since the data pending
917 * returned value is the response.
921 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
923 if (msg
.u
.snapshot_channel
.metadata
== 1) {
924 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
925 msg
.u
.snapshot_channel
.pathname
,
926 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
928 ERR("Snapshot metadata failed");
929 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
932 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
933 msg
.u
.snapshot_channel
.pathname
,
934 msg
.u
.snapshot_channel
.relayd_id
,
935 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
938 ERR("Snapshot channel failed");
939 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
943 health_code_update();
945 ret
= consumer_send_status_msg(sock
, ret_code
);
947 /* Somehow, the session daemon is not responding anymore. */
952 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
954 uint64_t key
= msg
.u
.destroy_channel
.key
;
955 struct lttng_consumer_channel
*channel
;
957 channel
= consumer_find_channel(key
);
959 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
960 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
963 health_code_update();
965 ret
= consumer_send_status_msg(sock
, ret_code
);
967 /* Somehow, the session daemon is not responding anymore. */
971 health_code_update();
973 /* Stop right now if no channel was found. */
979 * This command should ONLY be issued for channel with streams set in
982 assert(!channel
->monitor
);
985 * The refcount should ALWAYS be 0 in the case of a channel in no
988 assert(!uatomic_sub_return(&channel
->refcount
, 1));
990 consumer_del_channel(channel
);
994 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
997 struct lttng_consumer_channel
*channel
;
998 uint64_t id
= msg
.u
.discarded_events
.session_id
;
999 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1001 DBG("Kernel consumer discarded events command for session id %"
1002 PRIu64
", channel key %" PRIu64
, id
, key
);
1004 channel
= consumer_find_channel(key
);
1006 ERR("Kernel consumer discarded events channel %"
1007 PRIu64
" not found", key
);
1010 ret
= channel
->discarded_events
;
1013 health_code_update();
1015 /* Send back returned value to session daemon */
1016 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1018 PERROR("send discarded events");
1024 case LTTNG_CONSUMER_LOST_PACKETS
:
1027 struct lttng_consumer_channel
*channel
;
1028 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1029 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1031 DBG("Kernel consumer lost packets command for session id %"
1032 PRIu64
", channel key %" PRIu64
, id
, key
);
1034 channel
= consumer_find_channel(key
);
1036 ERR("Kernel consumer lost packets channel %"
1037 PRIu64
" not found", key
);
1040 ret
= channel
->lost_packets
;
1043 health_code_update();
1045 /* Send back returned value to session daemon */
1046 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1048 PERROR("send lost packets");
1062 * Return 1 to indicate success since the 0 value can be a socket
1063 * shutdown during the recv() or send() call.
1065 health_code_update();
1070 /* This will issue a consumer stop. */
1075 * Populate index values of a kernel stream. Values are set in big endian order.
1077 * Return 0 on success or else a negative value.
1079 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1083 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1085 PERROR("kernctl_get_timestamp_begin");
1088 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1090 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1092 PERROR("kernctl_get_timestamp_end");
1095 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1097 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1099 PERROR("kernctl_get_events_discarded");
1102 index
->events_discarded
= htobe64(index
->events_discarded
);
1104 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1106 PERROR("kernctl_get_content_size");
1109 index
->content_size
= htobe64(index
->content_size
);
1111 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1113 PERROR("kernctl_get_packet_size");
1116 index
->packet_size
= htobe64(index
->packet_size
);
1118 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1120 PERROR("kernctl_get_stream_id");
1123 index
->stream_id
= htobe64(index
->stream_id
);
1125 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1127 if (ret
== -ENOTTY
) {
1128 /* Command not implemented by lttng-modules. */
1129 index
->stream_instance_id
= -1ULL;
1132 PERROR("kernctl_get_instance_id");
1136 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1138 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1140 if (ret
== -ENOTTY
) {
1141 /* Command not implemented by lttng-modules. */
1142 index
->packet_seq_num
= -1ULL;
1145 PERROR("kernctl_get_sequence_number");
1149 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1155 * Sync metadata meaning request them to the session daemon and snapshot to the
1156 * metadata thread can consumer them.
1158 * Metadata stream lock MUST be acquired.
1160 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1161 * is empty or a negative value on error.
1163 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1169 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1171 ERR("Failed to flush kernel stream");
1175 ret
= kernctl_snapshot(metadata
->wait_fd
);
1177 if (ret
!= -EAGAIN
) {
1178 ERR("Sync metadata, taking kernel snapshot failed.");
1181 DBG("Sync metadata, no new kernel metadata");
1182 /* No new metadata, exit. */
1192 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1195 uint64_t seq
, discarded
;
1197 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1199 if (ret
== -ENOTTY
) {
1200 /* Command not implemented by lttng-modules. */
1204 PERROR("kernctl_get_sequence_number");
1210 * Start the sequence when we extract the first packet in case we don't
1211 * start at 0 (for example if a consumer is not connected to the
1212 * session immediately after the beginning).
1214 if (stream
->last_sequence_number
== -1ULL) {
1215 stream
->last_sequence_number
= seq
;
1216 } else if (seq
> stream
->last_sequence_number
) {
1217 stream
->chan
->lost_packets
+= seq
-
1218 stream
->last_sequence_number
- 1;
1220 /* seq <= last_sequence_number */
1221 ERR("Sequence number inconsistent : prev = %" PRIu64
1222 ", current = %" PRIu64
,
1223 stream
->last_sequence_number
, seq
);
1227 stream
->last_sequence_number
= seq
;
1229 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1231 PERROR("kernctl_get_events_discarded");
1234 if (discarded
< stream
->last_discarded_events
) {
1236 * Overflow has occurred. We assume only one wrap-around
1239 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1240 stream
->last_discarded_events
+ discarded
;
1242 stream
->chan
->discarded_events
+= discarded
-
1243 stream
->last_discarded_events
;
1245 stream
->last_discarded_events
= discarded
;
1253 * Check if the local version of the metadata stream matches with the version
1254 * of the metadata stream in the kernel. If it was updated, set the reset flag
1258 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1261 uint64_t cur_version
;
1263 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1265 if (ret
== -ENOTTY
) {
1267 * LTTng-modules does not implement this
1273 ERR("Failed to get the metadata version");
1277 if (stream
->metadata_version
== cur_version
) {
1282 DBG("New metadata version detected");
1283 stream
->metadata_version
= cur_version
;
1284 stream
->reset_metadata_flag
= 1;
1292 * Consume data on a file descriptor and write it on a trace file.
1294 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1295 struct lttng_consumer_local_data
*ctx
)
1297 unsigned long len
, subbuf_size
, padding
;
1298 int err
, write_index
= 1;
1300 int infd
= stream
->wait_fd
;
1301 struct ctf_packet_index index
;
1303 DBG("In read_subbuffer (infd : %d)", infd
);
1305 /* Get the next subbuffer */
1306 err
= kernctl_get_next_subbuf(infd
);
1309 * This is a debug message even for single-threaded consumer,
1310 * because poll() have more relaxed criterions than get subbuf,
1311 * so get_subbuf may fail for short race windows where poll()
1312 * would issue wakeups.
1314 DBG("Reserving sub buffer failed (everything is normal, "
1315 "it is due to concurrency)");
1320 /* Get the full subbuffer size including padding */
1321 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1323 PERROR("Getting sub-buffer len failed.");
1324 err
= kernctl_put_subbuf(infd
);
1326 if (err
== -EFAULT
) {
1327 PERROR("Error in unreserving sub buffer\n");
1328 } else if (err
== -EIO
) {
1329 /* Should never happen with newer LTTng versions */
1330 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1339 if (!stream
->metadata_flag
) {
1340 ret
= get_index_values(&index
, infd
);
1342 err
= kernctl_put_subbuf(infd
);
1344 if (err
== -EFAULT
) {
1345 PERROR("Error in unreserving sub buffer\n");
1346 } else if (err
== -EIO
) {
1347 /* Should never happen with newer LTTng versions */
1348 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1355 ret
= update_stream_stats(stream
);
1357 err
= kernctl_put_subbuf(infd
);
1359 if (err
== -EFAULT
) {
1360 PERROR("Error in unreserving sub buffer\n");
1361 } else if (err
== -EIO
) {
1362 /* Should never happen with newer LTTng versions */
1363 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1372 ret
= metadata_stream_check_version(infd
, stream
);
1374 err
= kernctl_put_subbuf(infd
);
1376 if (err
== -EFAULT
) {
1377 PERROR("Error in unreserving sub buffer\n");
1378 } else if (err
== -EIO
) {
1379 /* Should never happen with newer LTTng versions */
1380 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1389 switch (stream
->chan
->output
) {
1390 case CONSUMER_CHANNEL_SPLICE
:
1392 * XXX: The lttng-modules splice "actor" does not handle copying
1393 * partial pages hence only using the subbuffer size without the
1394 * padding makes the splice fail.
1399 /* splice the subbuffer to the tracefile */
1400 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1403 * XXX: Splice does not support network streaming so the return value
1404 * is simply checked against subbuf_size and not like the mmap() op.
1406 if (ret
!= subbuf_size
) {
1408 * display the error but continue processing to try
1409 * to release the subbuffer
1411 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1416 case CONSUMER_CHANNEL_MMAP
:
1418 const char *subbuf_addr
;
1420 /* Get subbuffer size without padding */
1421 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1423 PERROR("Getting sub-buffer len failed.");
1424 err
= kernctl_put_subbuf(infd
);
1426 if (err
== -EFAULT
) {
1427 PERROR("Error in unreserving sub buffer\n");
1428 } else if (err
== -EIO
) {
1429 /* Should never happen with newer LTTng versions */
1430 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1439 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1441 goto error_put_subbuf
;
1444 /* Make sure the tracer is not gone mad on us! */
1445 assert(len
>= subbuf_size
);
1447 padding
= len
- subbuf_size
;
1449 /* write the subbuffer to the tracefile */
1450 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
,
1455 * The mmap operation should write subbuf_size amount of data when
1456 * network streaming or the full padding (len) size when we are _not_
1459 if ((ret
!= subbuf_size
&& stream
->relayd_id
!= (uint64_t) -1ULL) ||
1460 (ret
!= len
&& stream
->relayd_id
== (uint64_t) -1ULL)) {
1462 * Display the error but continue processing to try to release the
1463 * subbuffer. This is a DBG statement since this is possible to
1464 * happen without being a critical error.
1466 DBG("Error writing to tracefile "
1467 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1468 ret
, len
, subbuf_size
);
1474 ERR("Unknown output method");
1478 err
= kernctl_put_next_subbuf(infd
);
1480 if (err
== -EFAULT
) {
1481 PERROR("Error in unreserving sub buffer\n");
1482 } else if (err
== -EIO
) {
1483 /* Should never happen with newer LTTng versions */
1484 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1490 /* Write index if needed. */
1495 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1497 * In live, block until all the metadata is sent.
1499 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1500 assert(!stream
->missed_metadata_flush
);
1501 stream
->waiting_on_metadata
= true;
1502 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1504 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1506 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1507 stream
->waiting_on_metadata
= false;
1508 if (stream
->missed_metadata_flush
) {
1509 stream
->missed_metadata_flush
= false;
1510 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1511 (void) consumer_flush_kernel_index(stream
);
1513 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1520 err
= consumer_stream_write_index(stream
, &index
);
1529 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1536 * Don't create anything if this is set for streaming or should not be
1539 if (stream
->relayd_id
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1540 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1541 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1542 stream
->uid
, stream
->gid
, NULL
);
1546 stream
->out_fd
= ret
;
1547 stream
->tracefile_size_current
= 0;
1549 if (!stream
->metadata_flag
) {
1550 struct lttng_index_file
*index_file
;
1552 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1553 stream
->name
, stream
->uid
, stream
->gid
,
1554 stream
->chan
->tracefile_size
,
1555 stream
->tracefile_count_current
,
1556 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1560 stream
->index_file
= index_file
;
1564 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1565 /* get the len of the mmap region */
1566 unsigned long mmap_len
;
1568 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1570 PERROR("kernctl_get_mmap_len");
1571 goto error_close_fd
;
1573 stream
->mmap_len
= (size_t) mmap_len
;
1575 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1576 MAP_PRIVATE
, stream
->wait_fd
, 0);
1577 if (stream
->mmap_base
== MAP_FAILED
) {
1578 PERROR("Error mmaping");
1580 goto error_close_fd
;
1584 /* we return 0 to let the library handle the FD internally */
1588 if (stream
->out_fd
>= 0) {
1591 err
= close(stream
->out_fd
);
1593 stream
->out_fd
= -1;
1600 * Check if data is still being extracted from the buffers for a specific
1601 * stream. Consumer data lock MUST be acquired before calling this function
1602 * and the stream lock.
1604 * Return 1 if the traced data are still getting read else 0 meaning that the
1605 * data is available for trace viewer reading.
1607 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1613 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1618 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1620 /* There is still data so let's put back this subbuffer. */
1621 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1623 ret
= 1; /* Data is pending */
1627 /* Data is NOT pending and ready to be read. */