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.
26 #include <sys/socket.h>
27 #include <sys/types.h>
32 #include <common/common.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/sessiond-comm/relayd.h>
36 #include <common/compat/fcntl.h>
37 #include <common/pipe.h>
38 #include <common/relayd/relayd.h>
39 #include <common/utils.h>
41 #include "kernel-consumer.h"
43 extern struct lttng_consumer_global_data consumer_data
;
44 extern int consumer_poll_timeout
;
45 extern volatile int consumer_quit
;
48 * Take a snapshot for a specific fd
50 * Returns 0 on success, < 0 on error
52 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
55 int infd
= stream
->wait_fd
;
57 ret
= kernctl_snapshot(infd
);
60 perror("Getting sub-buffer snapshot.");
67 * Get the produced position
69 * Returns 0 on success, < 0 on error
71 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
75 int infd
= stream
->wait_fd
;
77 ret
= kernctl_snapshot_get_produced(infd
, pos
);
80 perror("kernctl_snapshot_get_produced");
87 * Receive command from session daemon and process it.
89 * Return 1 on success else a negative value or 0.
91 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
92 int sock
, struct pollfd
*consumer_sockpoll
)
95 enum lttng_error_code ret_code
= LTTNG_OK
;
96 struct lttcomm_consumer_msg msg
;
98 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
99 if (ret
!= sizeof(msg
)) {
100 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
106 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
108 * Notify the session daemon that the command is completed.
110 * On transport layer error, the function call will print an error
111 * message so handling the returned code is a bit useless since we
112 * return an error code anyway.
114 (void) consumer_send_status_msg(sock
, ret_code
);
118 /* relayd needs RCU read-side protection */
121 switch (msg
.cmd_type
) {
122 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
124 /* Session daemon status message are handled in the following call. */
125 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
126 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
127 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
);
130 case LTTNG_CONSUMER_ADD_CHANNEL
:
132 struct lttng_consumer_channel
*new_channel
;
135 /* First send a status message before receiving the fds. */
136 ret
= consumer_send_status_msg(sock
, ret_code
);
138 /* Somehow, the session daemon is not responding anymore. */
141 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
142 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
143 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
144 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
145 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
146 msg
.u
.channel
.tracefile_size
,
147 msg
.u
.channel
.tracefile_count
,
148 msg
.u
.channel
.monitor
);
149 if (new_channel
== NULL
) {
150 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
153 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
155 /* Translate and save channel type. */
156 switch (msg
.u
.channel
.type
) {
157 case CONSUMER_CHANNEL_TYPE_DATA
:
158 case CONSUMER_CHANNEL_TYPE_METADATA
:
159 new_channel
->type
= msg
.u
.channel
.type
;
166 if (ctx
->on_recv_channel
!= NULL
) {
167 ret_recv
= ctx
->on_recv_channel(new_channel
);
169 ret
= consumer_add_channel(new_channel
, ctx
);
170 } else if (ret_recv
< 0) {
174 ret
= consumer_add_channel(new_channel
, ctx
);
177 /* If we received an error in add_channel, we need to report it. */
179 ret
= consumer_send_status_msg(sock
, ret
);
188 case LTTNG_CONSUMER_ADD_STREAM
:
191 struct lttng_pipe
*stream_pipe
;
192 struct consumer_relayd_sock_pair
*relayd
= NULL
;
193 struct lttng_consumer_stream
*new_stream
;
194 struct lttng_consumer_channel
*channel
;
198 * Get stream's channel reference. Needed when adding the stream to the
201 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
204 * We could not find the channel. Can happen if cpu hotplug
205 * happens while tearing down.
207 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
208 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
211 /* First send a status message before receiving the fds. */
212 ret
= consumer_send_status_msg(sock
, ret_code
);
215 * Somehow, the session daemon is not responding
220 if (ret_code
!= LTTNG_OK
) {
222 * Channel was not found.
228 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
233 /* Get stream file descriptor from socket */
234 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
235 if (ret
!= sizeof(fd
)) {
236 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
242 * Send status code to session daemon only if the recv works. If the
243 * above recv() failed, the session daemon is notified through the
244 * error socket and the teardown is eventually done.
246 ret
= consumer_send_status_msg(sock
, ret_code
);
248 /* Somehow, the session daemon is not responding anymore. */
252 new_stream
= consumer_allocate_stream(channel
->key
,
254 LTTNG_CONSUMER_ACTIVE_STREAM
,
263 if (new_stream
== NULL
) {
268 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
273 new_stream
->chan
= channel
;
274 new_stream
->wait_fd
= fd
;
277 * We've just assigned the channel to the stream so increment the
278 * refcount right now.
280 uatomic_inc(&new_stream
->chan
->refcount
);
283 * The buffer flush is done on the session daemon side for the kernel
284 * so no need for the stream "hangup_flush_done" variable to be
285 * tracked. This is important for a kernel stream since we don't rely
286 * on the flush state of the stream to read data. It's not the case for
287 * user space tracing.
289 new_stream
->hangup_flush_done
= 0;
291 /* The stream is not metadata. Get relayd reference if exists. */
292 relayd
= consumer_find_relayd(new_stream
->net_seq_idx
);
293 if (relayd
!= NULL
) {
294 /* Add stream on the relayd */
295 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
296 ret
= relayd_add_stream(&relayd
->control_sock
,
297 new_stream
->name
, new_stream
->chan
->pathname
,
298 &new_stream
->relayd_stream_id
,
299 new_stream
->chan
->tracefile_size
,
300 new_stream
->chan
->tracefile_count
);
301 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
303 consumer_del_stream(new_stream
, NULL
);
306 } else if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
307 ERR("Network sequence index %" PRIu64
" unknown. Not adding stream.",
308 new_stream
->net_seq_idx
);
309 consumer_del_stream(new_stream
, NULL
);
313 if (ctx
->on_recv_stream
) {
314 ret
= ctx
->on_recv_stream(new_stream
);
316 consumer_del_stream(new_stream
, NULL
);
321 /* Do not monitor this stream. */
322 if (!channel
->monitor
) {
323 DBG("Kernel consumer add stream %s in no monitor mode with"
324 "relayd id %" PRIu64
, new_stream
->name
,
325 new_stream
->relayd_stream_id
);
329 /* Get the right pipe where the stream will be sent. */
330 if (new_stream
->metadata_flag
) {
331 stream_pipe
= ctx
->consumer_metadata_pipe
;
333 stream_pipe
= ctx
->consumer_data_pipe
;
336 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
338 ERR("Consumer write %s stream to pipe %d",
339 new_stream
->metadata_flag
? "metadata" : "data",
340 lttng_pipe_get_writefd(stream_pipe
));
341 consumer_del_stream(new_stream
, NULL
);
345 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
346 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
349 case LTTNG_CONSUMER_UPDATE_STREAM
:
354 case LTTNG_CONSUMER_DESTROY_RELAYD
:
356 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
357 struct consumer_relayd_sock_pair
*relayd
;
359 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
361 /* Get relayd reference if exists. */
362 relayd
= consumer_find_relayd(index
);
363 if (relayd
== NULL
) {
364 DBG("Unable to find relayd %" PRIu64
, index
);
365 ret_code
= LTTNG_ERR_NO_CONSUMER
;
369 * Each relayd socket pair has a refcount of stream attached to it
370 * which tells if the relayd is still active or not depending on the
373 * This will set the destroy flag of the relayd object and destroy it
374 * if the refcount reaches zero when called.
376 * The destroy can happen either here or when a stream fd hangs up.
379 consumer_flag_relayd_for_destroy(relayd
);
382 ret
= consumer_send_status_msg(sock
, ret_code
);
384 /* Somehow, the session daemon is not responding anymore. */
390 case LTTNG_CONSUMER_DATA_PENDING
:
393 uint64_t id
= msg
.u
.data_pending
.session_id
;
395 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
397 ret
= consumer_data_pending(id
);
399 /* Send back returned value to session daemon */
400 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
402 PERROR("send data pending ret code");
407 * No need to send back a status message since the data pending
408 * returned value is the response.
412 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
414 ret
= consumer_send_status_msg(sock
, ret_code
);
416 /* Somehow, the session daemon is not responding anymore. */
429 * Return 1 to indicate success since the 0 value can be a socket
430 * shutdown during the recv() or send() call.
436 /* This will issue a consumer stop. */
441 * Consume data on a file descriptor and write it on a trace file.
443 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
444 struct lttng_consumer_local_data
*ctx
)
446 unsigned long len
, subbuf_size
, padding
;
449 int infd
= stream
->wait_fd
;
451 DBG("In read_subbuffer (infd : %d)", infd
);
452 /* Get the next subbuffer */
453 err
= kernctl_get_next_subbuf(infd
);
457 * This is a debug message even for single-threaded consumer,
458 * because poll() have more relaxed criterions than get subbuf,
459 * so get_subbuf may fail for short race windows where poll()
460 * would issue wakeups.
462 DBG("Reserving sub buffer failed (everything is normal, "
463 "it is due to concurrency)");
467 /* Get the full subbuffer size including padding */
468 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
471 perror("Getting sub-buffer len failed.");
476 switch (stream
->chan
->output
) {
477 case LTTNG_EVENT_SPLICE
:
480 * XXX: The lttng-modules splice "actor" does not handle copying
481 * partial pages hence only using the subbuffer size without the
482 * padding makes the splice fail.
487 /* splice the subbuffer to the tracefile */
488 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
491 * XXX: Splice does not support network streaming so the return value
492 * is simply checked against subbuf_size and not like the mmap() op.
494 if (ret
!= subbuf_size
) {
496 * display the error but continue processing to try
497 * to release the subbuffer
499 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
503 case LTTNG_EVENT_MMAP
:
504 /* Get subbuffer size without padding */
505 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
508 perror("Getting sub-buffer len failed.");
513 /* Make sure the tracer is not gone mad on us! */
514 assert(len
>= subbuf_size
);
516 padding
= len
- subbuf_size
;
518 /* write the subbuffer to the tracefile */
519 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
522 * The mmap operation should write subbuf_size amount of data when
523 * network streaming or the full padding (len) size when we are _not_
526 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
527 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
529 * Display the error but continue processing to try to release the
532 ERR("Error writing to tracefile "
533 "(ret: %zd != len: %lu != subbuf_size: %lu)",
534 ret
, len
, subbuf_size
);
538 ERR("Unknown output method");
542 err
= kernctl_put_next_subbuf(infd
);
545 if (errno
== EFAULT
) {
546 perror("Error in unreserving sub buffer\n");
547 } else if (errno
== EIO
) {
548 /* Should never happen with newer LTTng versions */
549 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
560 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
567 * Don't create anything if this is set for streaming or should not be
570 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
571 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
572 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
573 stream
->uid
, stream
->gid
);
577 stream
->out_fd
= ret
;
578 stream
->tracefile_size_current
= 0;
581 if (stream
->output
== LTTNG_EVENT_MMAP
) {
582 /* get the len of the mmap region */
583 unsigned long mmap_len
;
585 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
588 PERROR("kernctl_get_mmap_len");
591 stream
->mmap_len
= (size_t) mmap_len
;
593 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
594 MAP_PRIVATE
, stream
->wait_fd
, 0);
595 if (stream
->mmap_base
== MAP_FAILED
) {
596 PERROR("Error mmaping");
602 /* we return 0 to let the library handle the FD internally */
609 err
= close(stream
->out_fd
);
617 * Check if data is still being extracted from the buffers for a specific
618 * stream. Consumer data lock MUST be acquired before calling this function
619 * and the stream lock.
621 * Return 1 if the traced data are still getting read else 0 meaning that the
622 * data is available for trace viewer reading.
624 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
630 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
632 /* There is still data so let's put back this subbuffer. */
633 ret
= kernctl_put_subbuf(stream
->wait_fd
);
635 ret
= 1; /* Data is pending */
639 /* Data is NOT pending and ready to be read. */