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_local_data
*ctx
,
51 struct lttng_consumer_stream
*stream
)
54 int infd
= stream
->wait_fd
;
56 ret
= kernctl_snapshot(infd
);
59 perror("Getting sub-buffer snapshot.");
66 * Get the produced position
68 * Returns 0 on success, < 0 on error
70 int lttng_kconsumer_get_produced_snapshot(
71 struct lttng_consumer_local_data
*ctx
,
72 struct lttng_consumer_stream
*stream
,
76 int infd
= stream
->wait_fd
;
78 ret
= kernctl_snapshot_get_produced(infd
, pos
);
81 perror("kernctl_snapshot_get_produced");
87 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
88 int sock
, struct pollfd
*consumer_sockpoll
)
91 enum lttng_error_code ret_code
= LTTNG_OK
;
92 struct lttcomm_consumer_msg msg
;
94 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
95 if (ret
!= sizeof(msg
)) {
96 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
99 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
101 * Notify the session daemon that the command is completed.
103 * On transport layer error, the function call will print an error
104 * message so handling the returned code is a bit useless since we
105 * return an error code anyway.
107 (void) consumer_send_status_msg(sock
, ret_code
);
111 /* relayd needs RCU read-side protection */
114 switch (msg
.cmd_type
) {
115 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
117 /* Session daemon status message are handled in the following call. */
118 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
119 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
120 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
);
123 case LTTNG_CONSUMER_ADD_CHANNEL
:
125 struct lttng_consumer_channel
*new_channel
;
127 /* First send a status message before receiving the fds. */
128 ret
= consumer_send_status_msg(sock
, ret_code
);
130 /* Somehow, the session daemon is not responding anymore. */
134 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
135 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
137 msg
.u
.channel
.mmap_len
,
138 msg
.u
.channel
.max_sb_size
,
139 msg
.u
.channel
.nb_init_streams
);
140 if (new_channel
== NULL
) {
141 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
144 if (ctx
->on_recv_channel
!= NULL
) {
145 ret
= ctx
->on_recv_channel(new_channel
);
147 consumer_add_channel(new_channel
);
148 } else if (ret
< 0) {
152 consumer_add_channel(new_channel
);
156 case LTTNG_CONSUMER_ADD_STREAM
:
159 struct consumer_relayd_sock_pair
*relayd
= NULL
;
160 struct lttng_consumer_stream
*new_stream
;
163 /* First send a status message before receiving the fds. */
164 ret
= consumer_send_status_msg(sock
, ret_code
);
166 /* Somehow, the session daemon is not responding anymore. */
171 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
176 /* Get stream file descriptor from socket */
177 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
178 if (ret
!= sizeof(fd
)) {
179 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
185 * Send status code to session daemon only if the recv works. If the
186 * above recv() failed, the session daemon is notified through the
187 * error socket and the teardown is eventually done.
189 ret
= consumer_send_status_msg(sock
, ret_code
);
191 /* Somehow, the session daemon is not responding anymore. */
195 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
196 msg
.u
.stream
.stream_key
,
199 msg
.u
.stream
.mmap_len
,
201 msg
.u
.stream
.path_name
,
204 msg
.u
.stream
.net_index
,
205 msg
.u
.stream
.metadata_flag
,
206 msg
.u
.stream
.session_id
,
208 if (new_stream
== NULL
) {
213 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
217 * We could not find the channel. Can happen if cpu hotplug
218 * happens while tearing down.
220 DBG3("Could not find channel");
227 * The buffer flush is done on the session daemon side for the kernel
228 * so no need for the stream "hangup_flush_done" variable to be
229 * tracked. This is important for a kernel stream since we don't rely
230 * on the flush state of the stream to read data. It's not the case for
231 * user space tracing.
233 new_stream
->hangup_flush_done
= 0;
235 /* The stream is not metadata. Get relayd reference if exists. */
236 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
237 if (relayd
!= NULL
) {
238 /* Add stream on the relayd */
239 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
240 ret
= relayd_add_stream(&relayd
->control_sock
,
241 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
242 &new_stream
->relayd_stream_id
);
243 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
245 consumer_del_stream(new_stream
, NULL
);
248 } else if (msg
.u
.stream
.net_index
!= -1) {
249 ERR("Network sequence index %d unknown. Not adding stream.",
250 msg
.u
.stream
.net_index
);
251 consumer_del_stream(new_stream
, NULL
);
255 if (ctx
->on_recv_stream
) {
256 ret
= ctx
->on_recv_stream(new_stream
);
258 consumer_del_stream(new_stream
, NULL
);
263 /* Get the right pipe where the stream will be sent. */
264 if (new_stream
->metadata_flag
) {
265 stream_pipe
= ctx
->consumer_metadata_pipe
[1];
267 stream_pipe
= ctx
->consumer_data_pipe
[1];
271 ret
= write(stream_pipe
, &new_stream
, sizeof(new_stream
));
272 } while (ret
< 0 && errno
== EINTR
);
274 PERROR("Consumer write %s stream to pipe %d",
275 new_stream
->metadata_flag
? "metadata" : "data",
277 consumer_del_stream(new_stream
, NULL
);
281 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
282 msg
.u
.stream
.path_name
, fd
, new_stream
->relayd_stream_id
);
285 case LTTNG_CONSUMER_UPDATE_STREAM
:
290 case LTTNG_CONSUMER_DESTROY_RELAYD
:
292 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
293 struct consumer_relayd_sock_pair
*relayd
;
295 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
297 /* Get relayd reference if exists. */
298 relayd
= consumer_find_relayd(index
);
299 if (relayd
== NULL
) {
300 ERR("Unable to find relayd %" PRIu64
, index
);
301 ret_code
= LTTNG_ERR_NO_CONSUMER
;
305 * Each relayd socket pair has a refcount of stream attached to it
306 * which tells if the relayd is still active or not depending on the
309 * This will set the destroy flag of the relayd object and destroy it
310 * if the refcount reaches zero when called.
312 * The destroy can happen either here or when a stream fd hangs up.
315 consumer_flag_relayd_for_destroy(relayd
);
318 ret
= consumer_send_status_msg(sock
, ret_code
);
320 /* Somehow, the session daemon is not responding anymore. */
326 case LTTNG_CONSUMER_DATA_PENDING
:
329 uint64_t id
= msg
.u
.data_pending
.session_id
;
331 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
333 ret
= consumer_data_pending(id
);
335 /* Send back returned value to session daemon */
336 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
338 PERROR("send data pending ret code");
342 * No need to send back a status message since the data pending
343 * returned value is the response.
355 * Return 1 to indicate success since the 0 value can be a socket
356 * shutdown during the recv() or send() call.
362 * Consume data on a file descriptor and write it on a trace file.
364 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
365 struct lttng_consumer_local_data
*ctx
)
367 unsigned long len
, subbuf_size
, padding
;
370 int infd
= stream
->wait_fd
;
372 DBG("In read_subbuffer (infd : %d)", infd
);
373 /* Get the next subbuffer */
374 err
= kernctl_get_next_subbuf(infd
);
378 * This is a debug message even for single-threaded consumer,
379 * because poll() have more relaxed criterions than get subbuf,
380 * so get_subbuf may fail for short race windows where poll()
381 * would issue wakeups.
383 DBG("Reserving sub buffer failed (everything is normal, "
384 "it is due to concurrency)");
388 /* Get the full subbuffer size including padding */
389 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
392 perror("Getting sub-buffer len failed.");
397 switch (stream
->output
) {
398 case LTTNG_EVENT_SPLICE
:
401 * XXX: The lttng-modules splice "actor" does not handle copying
402 * partial pages hence only using the subbuffer size without the
403 * padding makes the splice fail.
408 /* splice the subbuffer to the tracefile */
409 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
412 * XXX: Splice does not support network streaming so the return value
413 * is simply checked against subbuf_size and not like the mmap() op.
415 if (ret
!= subbuf_size
) {
417 * display the error but continue processing to try
418 * to release the subbuffer
420 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
424 case LTTNG_EVENT_MMAP
:
425 /* Get subbuffer size without padding */
426 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
429 perror("Getting sub-buffer len failed.");
434 /* Make sure the tracer is not gone mad on us! */
435 assert(len
>= subbuf_size
);
437 padding
= len
- subbuf_size
;
439 /* write the subbuffer to the tracefile */
440 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
443 * The mmap operation should write subbuf_size amount of data when
444 * network streaming or the full padding (len) size when we are _not_
447 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= -1) ||
448 (ret
!= len
&& stream
->net_seq_idx
== -1)) {
450 * Display the error but continue processing to try to release the
453 ERR("Error writing to tracefile "
454 "(ret: %zd != len: %lu != subbuf_size: %lu)",
455 ret
, len
, subbuf_size
);
459 ERR("Unknown output method");
463 err
= kernctl_put_next_subbuf(infd
);
466 if (errno
== EFAULT
) {
467 perror("Error in unreserving sub buffer\n");
468 } else if (errno
== EIO
) {
469 /* Should never happen with newer LTTng versions */
470 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
481 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
485 /* Opening the tracefile in write mode */
486 if (strlen(stream
->path_name
) > 0 && stream
->net_seq_idx
== -1) {
487 ret
= run_as_open(stream
->path_name
,
488 O_WRONLY
|O_CREAT
|O_TRUNC
,
489 S_IRWXU
|S_IRWXG
|S_IRWXO
,
490 stream
->uid
, stream
->gid
);
492 ERR("Opening %s", stream
->path_name
);
496 stream
->out_fd
= ret
;
499 if (stream
->output
== LTTNG_EVENT_MMAP
) {
500 /* get the len of the mmap region */
501 unsigned long mmap_len
;
503 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
506 perror("kernctl_get_mmap_len");
509 stream
->mmap_len
= (size_t) mmap_len
;
511 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
,
512 PROT_READ
, MAP_PRIVATE
, stream
->wait_fd
, 0);
513 if (stream
->mmap_base
== MAP_FAILED
) {
514 perror("Error mmaping");
520 /* we return 0 to let the library handle the FD internally */
527 err
= close(stream
->out_fd
);
535 * Check if data is still being extracted from the buffers for a specific
536 * stream. Consumer data lock MUST be acquired before calling this function
537 * and the stream lock.
539 * Return 1 if the traced data are still getting read else 0 meaning that the
540 * data is available for trace viewer reading.
542 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
548 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
550 /* There is still data so let's put back this subbuffer. */
551 ret
= kernctl_put_subbuf(stream
->wait_fd
);
553 ret
= 1; /* Data is pending */
557 /* Data is NOT pending and ready to be read. */