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/relayd/relayd.h>
39 #include "kernel-consumer.h"
41 extern struct lttng_consumer_global_data consumer_data
;
42 extern int consumer_poll_timeout
;
43 extern volatile int consumer_quit
;
46 * Take a snapshot for a specific fd
48 * Returns 0 on success, < 0 on error
50 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
53 int infd
= stream
->wait_fd
;
55 ret
= kernctl_snapshot(infd
);
58 perror("Getting sub-buffer snapshot.");
65 * Get the produced position
67 * Returns 0 on success, < 0 on error
69 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
73 int infd
= stream
->wait_fd
;
75 ret
= kernctl_snapshot_get_produced(infd
, pos
);
78 perror("kernctl_snapshot_get_produced");
84 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
85 int sock
, struct pollfd
*consumer_sockpoll
)
88 enum lttng_error_code ret_code
= LTTNG_OK
;
89 struct lttcomm_consumer_msg msg
;
91 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
92 if (ret
!= sizeof(msg
)) {
93 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
96 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
98 * Notify the session daemon that the command is completed.
100 * On transport layer error, the function call will print an error
101 * message so handling the returned code is a bit useless since we
102 * return an error code anyway.
104 (void) consumer_send_status_msg(sock
, ret_code
);
108 /* relayd needs RCU read-side protection */
111 switch (msg
.cmd_type
) {
112 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
114 /* Session daemon status message are handled in the following call. */
115 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
116 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
117 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
);
120 case LTTNG_CONSUMER_ADD_CHANNEL
:
122 struct lttng_consumer_channel
*new_channel
;
124 /* First send a status message before receiving the fds. */
125 ret
= consumer_send_status_msg(sock
, ret_code
);
127 /* Somehow, the session daemon is not responding anymore. */
131 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
132 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
133 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
134 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
135 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
);
136 if (new_channel
== NULL
) {
137 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
140 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
142 /* Translate and save channel type. */
143 switch (msg
.u
.channel
.type
) {
144 case CONSUMER_CHANNEL_TYPE_DATA
:
145 case CONSUMER_CHANNEL_TYPE_METADATA
:
146 new_channel
->type
= msg
.u
.channel
.type
;
153 if (ctx
->on_recv_channel
!= NULL
) {
154 ret
= ctx
->on_recv_channel(new_channel
);
156 consumer_add_channel(new_channel
, ctx
);
157 } else if (ret
< 0) {
161 consumer_add_channel(new_channel
, ctx
);
165 case LTTNG_CONSUMER_ADD_STREAM
:
168 struct consumer_relayd_sock_pair
*relayd
= NULL
;
169 struct lttng_consumer_stream
*new_stream
;
170 struct lttng_consumer_channel
*channel
;
174 * Get stream's channel reference. Needed when adding the stream to the
177 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
180 * We could not find the channel. Can happen if cpu hotplug
181 * happens while tearing down.
183 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
184 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
187 /* First send a status message before receiving the fds. */
188 ret
= consumer_send_status_msg(sock
, ret_code
);
189 if (ret
< 0 || ret_code
!= LTTNG_OK
) {
191 * Somehow, the session daemon is not responding anymore or the
192 * channel was not found.
198 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
203 /* Get stream file descriptor from socket */
204 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
205 if (ret
!= sizeof(fd
)) {
206 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
212 * Send status code to session daemon only if the recv works. If the
213 * above recv() failed, the session daemon is notified through the
214 * error socket and the teardown is eventually done.
216 ret
= consumer_send_status_msg(sock
, ret_code
);
218 /* Somehow, the session daemon is not responding anymore. */
222 new_stream
= consumer_allocate_stream(channel
->key
,
224 LTTNG_CONSUMER_ACTIVE_STREAM
,
233 if (new_stream
== NULL
) {
238 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
243 new_stream
->chan
= channel
;
244 new_stream
->wait_fd
= fd
;
247 * The buffer flush is done on the session daemon side for the kernel
248 * so no need for the stream "hangup_flush_done" variable to be
249 * tracked. This is important for a kernel stream since we don't rely
250 * on the flush state of the stream to read data. It's not the case for
251 * user space tracing.
253 new_stream
->hangup_flush_done
= 0;
255 /* The stream is not metadata. Get relayd reference if exists. */
256 relayd
= consumer_find_relayd(new_stream
->net_seq_idx
);
257 if (relayd
!= NULL
) {
258 /* Add stream on the relayd */
259 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
260 ret
= relayd_add_stream(&relayd
->control_sock
,
261 new_stream
->name
, new_stream
->chan
->pathname
,
262 &new_stream
->relayd_stream_id
);
263 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
265 consumer_del_stream(new_stream
, NULL
);
268 } else if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
269 ERR("Network sequence index %" PRIu64
" unknown. Not adding stream.",
270 new_stream
->net_seq_idx
);
271 consumer_del_stream(new_stream
, NULL
);
275 if (ctx
->on_recv_stream
) {
276 ret
= ctx
->on_recv_stream(new_stream
);
278 consumer_del_stream(new_stream
, NULL
);
283 /* Get the right pipe where the stream will be sent. */
284 if (new_stream
->metadata_flag
) {
285 stream_pipe
= ctx
->consumer_metadata_pipe
[1];
287 stream_pipe
= ctx
->consumer_data_pipe
[1];
291 ret
= write(stream_pipe
, &new_stream
, sizeof(new_stream
));
292 } while (ret
< 0 && errno
== EINTR
);
294 PERROR("Consumer write %s stream to pipe %d",
295 new_stream
->metadata_flag
? "metadata" : "data",
297 consumer_del_stream(new_stream
, NULL
);
301 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
302 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
305 case LTTNG_CONSUMER_UPDATE_STREAM
:
310 case LTTNG_CONSUMER_DESTROY_RELAYD
:
312 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
313 struct consumer_relayd_sock_pair
*relayd
;
315 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
317 /* Get relayd reference if exists. */
318 relayd
= consumer_find_relayd(index
);
319 if (relayd
== NULL
) {
320 DBG("Unable to find relayd %" PRIu64
, index
);
321 ret_code
= LTTNG_ERR_NO_CONSUMER
;
325 * Each relayd socket pair has a refcount of stream attached to it
326 * which tells if the relayd is still active or not depending on the
329 * This will set the destroy flag of the relayd object and destroy it
330 * if the refcount reaches zero when called.
332 * The destroy can happen either here or when a stream fd hangs up.
335 consumer_flag_relayd_for_destroy(relayd
);
338 ret
= consumer_send_status_msg(sock
, ret_code
);
340 /* Somehow, the session daemon is not responding anymore. */
346 case LTTNG_CONSUMER_DATA_PENDING
:
349 uint64_t id
= msg
.u
.data_pending
.session_id
;
351 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
353 ret
= consumer_data_pending(id
);
355 /* Send back returned value to session daemon */
356 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
358 PERROR("send data pending ret code");
362 * No need to send back a status message since the data pending
363 * returned value is the response.
375 * Return 1 to indicate success since the 0 value can be a socket
376 * shutdown during the recv() or send() call.
382 * Consume data on a file descriptor and write it on a trace file.
384 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
385 struct lttng_consumer_local_data
*ctx
)
387 unsigned long len
, subbuf_size
, padding
;
390 int infd
= stream
->wait_fd
;
392 DBG("In read_subbuffer (infd : %d)", infd
);
393 /* Get the next subbuffer */
394 err
= kernctl_get_next_subbuf(infd
);
398 * This is a debug message even for single-threaded consumer,
399 * because poll() have more relaxed criterions than get subbuf,
400 * so get_subbuf may fail for short race windows where poll()
401 * would issue wakeups.
403 DBG("Reserving sub buffer failed (everything is normal, "
404 "it is due to concurrency)");
408 /* Get the full subbuffer size including padding */
409 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
412 perror("Getting sub-buffer len failed.");
417 switch (stream
->chan
->output
) {
418 case LTTNG_EVENT_SPLICE
:
421 * XXX: The lttng-modules splice "actor" does not handle copying
422 * partial pages hence only using the subbuffer size without the
423 * padding makes the splice fail.
428 /* splice the subbuffer to the tracefile */
429 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
432 * XXX: Splice does not support network streaming so the return value
433 * is simply checked against subbuf_size and not like the mmap() op.
435 if (ret
!= subbuf_size
) {
437 * display the error but continue processing to try
438 * to release the subbuffer
440 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
444 case LTTNG_EVENT_MMAP
:
445 /* Get subbuffer size without padding */
446 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
449 perror("Getting sub-buffer len failed.");
454 /* Make sure the tracer is not gone mad on us! */
455 assert(len
>= subbuf_size
);
457 padding
= len
- subbuf_size
;
459 /* write the subbuffer to the tracefile */
460 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
463 * The mmap operation should write subbuf_size amount of data when
464 * network streaming or the full padding (len) size when we are _not_
467 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
468 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
470 * Display the error but continue processing to try to release the
473 ERR("Error writing to tracefile "
474 "(ret: %zd != len: %lu != subbuf_size: %lu)",
475 ret
, len
, subbuf_size
);
479 ERR("Unknown output method");
483 err
= kernctl_put_next_subbuf(infd
);
486 if (errno
== EFAULT
) {
487 perror("Error in unreserving sub buffer\n");
488 } else if (errno
== EIO
) {
489 /* Should never happen with newer LTTng versions */
490 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
501 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
504 char full_path
[PATH_MAX
];
508 ret
= snprintf(full_path
, sizeof(full_path
), "%s/%s",
509 stream
->chan
->pathname
, stream
->name
);
511 PERROR("snprintf on_recv_stream");
515 /* Opening the tracefile in write mode */
516 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
517 ret
= run_as_open(full_path
, O_WRONLY
| O_CREAT
| O_TRUNC
,
518 S_IRWXU
|S_IRWXG
|S_IRWXO
, stream
->uid
, stream
->gid
);
520 PERROR("open kernel stream path %s", full_path
);
523 stream
->out_fd
= ret
;
526 if (stream
->output
== LTTNG_EVENT_MMAP
) {
527 /* get the len of the mmap region */
528 unsigned long mmap_len
;
530 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
533 PERROR("kernctl_get_mmap_len");
536 stream
->mmap_len
= (size_t) mmap_len
;
538 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
539 MAP_PRIVATE
, stream
->wait_fd
, 0);
540 if (stream
->mmap_base
== MAP_FAILED
) {
541 PERROR("Error mmaping");
547 /* we return 0 to let the library handle the FD internally */
554 err
= close(stream
->out_fd
);
562 * Check if data is still being extracted from the buffers for a specific
563 * stream. Consumer data lock MUST be acquired before calling this function
564 * and the stream lock.
566 * Return 1 if the traced data are still getting read else 0 meaning that the
567 * data is available for trace viewer reading.
569 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
575 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
577 /* There is still data so let's put back this subbuffer. */
578 ret
= kernctl_put_subbuf(stream
->wait_fd
);
580 ret
= 1; /* Data is pending */
584 /* Data is NOT pending and ready to be read. */