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 struct lttcomm_consumer_msg msg
;
93 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
94 if (ret
!= sizeof(msg
)) {
95 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
98 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
102 /* relayd needs RCU read-side protection */
105 switch (msg
.cmd_type
) {
106 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
108 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
109 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
110 &msg
.u
.relayd_sock
.sock
);
113 case LTTNG_CONSUMER_ADD_CHANNEL
:
115 struct lttng_consumer_channel
*new_channel
;
117 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
118 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
120 msg
.u
.channel
.mmap_len
,
121 msg
.u
.channel
.max_sb_size
,
122 msg
.u
.channel
.nb_init_streams
);
123 if (new_channel
== NULL
) {
124 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
127 if (ctx
->on_recv_channel
!= NULL
) {
128 ret
= ctx
->on_recv_channel(new_channel
);
130 consumer_add_channel(new_channel
);
131 } else if (ret
< 0) {
135 consumer_add_channel(new_channel
);
139 case LTTNG_CONSUMER_ADD_STREAM
:
142 struct consumer_relayd_sock_pair
*relayd
= NULL
;
143 struct lttng_consumer_stream
*new_stream
;
147 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
152 /* Get stream file descriptor from socket */
153 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
154 if (ret
!= sizeof(fd
)) {
155 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
160 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
161 msg
.u
.stream
.stream_key
,
164 msg
.u
.stream
.mmap_len
,
166 msg
.u
.stream
.path_name
,
169 msg
.u
.stream
.net_index
,
170 msg
.u
.stream
.metadata_flag
,
171 msg
.u
.stream
.session_id
,
173 if (new_stream
== NULL
) {
178 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
182 * We could not find the channel. Can happen if cpu hotplug
183 * happens while tearing down.
185 DBG3("Could not find channel");
192 * The buffer flush is done on the session daemon side for the kernel
193 * so no need for the stream "hangup_flush_done" variable to be
194 * tracked. This is important for a kernel stream since we don't rely
195 * on the flush state of the stream to read data. It's not the case for
196 * user space tracing.
198 new_stream
->hangup_flush_done
= 0;
200 /* The stream is not metadata. Get relayd reference if exists. */
201 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
202 if (relayd
!= NULL
) {
203 /* Add stream on the relayd */
204 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
205 ret
= relayd_add_stream(&relayd
->control_sock
,
206 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
207 &new_stream
->relayd_stream_id
);
208 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
210 consumer_del_stream(new_stream
, NULL
);
213 } else if (msg
.u
.stream
.net_index
!= -1) {
214 ERR("Network sequence index %d unknown. Not adding stream.",
215 msg
.u
.stream
.net_index
);
216 consumer_del_stream(new_stream
, NULL
);
220 if (ctx
->on_recv_stream
) {
221 ret
= ctx
->on_recv_stream(new_stream
);
223 consumer_del_stream(new_stream
, NULL
);
228 /* Get the right pipe where the stream will be sent. */
229 if (new_stream
->metadata_flag
) {
230 stream_pipe
= ctx
->consumer_metadata_pipe
[1];
232 stream_pipe
= ctx
->consumer_data_pipe
[1];
236 ret
= write(stream_pipe
, &new_stream
, sizeof(new_stream
));
237 } while (ret
< 0 && errno
== EINTR
);
239 PERROR("Consumer write %s stream to pipe %d",
240 new_stream
->metadata_flag
? "metadata" : "data",
242 consumer_del_stream(new_stream
, NULL
);
246 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
247 msg
.u
.stream
.path_name
, fd
, new_stream
->relayd_stream_id
);
250 case LTTNG_CONSUMER_UPDATE_STREAM
:
255 case LTTNG_CONSUMER_DESTROY_RELAYD
:
257 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
258 struct consumer_relayd_sock_pair
*relayd
;
260 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
262 /* Get relayd reference if exists. */
263 relayd
= consumer_find_relayd(index
);
264 if (relayd
== NULL
) {
265 ERR("Unable to find relayd %" PRIu64
, index
);
270 * Each relayd socket pair has a refcount of stream attached to it
271 * which tells if the relayd is still active or not depending on the
274 * This will set the destroy flag of the relayd object and destroy it
275 * if the refcount reaches zero when called.
277 * The destroy can happen either here or when a stream fd hangs up.
279 consumer_flag_relayd_for_destroy(relayd
);
283 case LTTNG_CONSUMER_DATA_PENDING
:
286 uint64_t id
= msg
.u
.data_pending
.session_id
;
288 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
290 ret
= consumer_data_pending(id
);
292 /* Send back returned value to session daemon */
293 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
295 PERROR("send data pending ret code");
307 * Return 1 to indicate success since the 0 value can be a socket
308 * shutdown during the recv() or send() call.
314 * Consume data on a file descriptor and write it on a trace file.
316 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
317 struct lttng_consumer_local_data
*ctx
)
319 unsigned long len
, subbuf_size
, padding
;
322 int infd
= stream
->wait_fd
;
324 DBG("In read_subbuffer (infd : %d)", infd
);
325 /* Get the next subbuffer */
326 err
= kernctl_get_next_subbuf(infd
);
330 * This is a debug message even for single-threaded consumer,
331 * because poll() have more relaxed criterions than get subbuf,
332 * so get_subbuf may fail for short race windows where poll()
333 * would issue wakeups.
335 DBG("Reserving sub buffer failed (everything is normal, "
336 "it is due to concurrency)");
340 /* Get the full subbuffer size including padding */
341 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
344 perror("Getting sub-buffer len failed.");
349 switch (stream
->output
) {
350 case LTTNG_EVENT_SPLICE
:
353 * XXX: The lttng-modules splice "actor" does not handle copying
354 * partial pages hence only using the subbuffer size without the
355 * padding makes the splice fail.
360 /* splice the subbuffer to the tracefile */
361 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
364 * XXX: Splice does not support network streaming so the return value
365 * is simply checked against subbuf_size and not like the mmap() op.
367 if (ret
!= subbuf_size
) {
369 * display the error but continue processing to try
370 * to release the subbuffer
372 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
376 case LTTNG_EVENT_MMAP
:
377 /* Get subbuffer size without padding */
378 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
381 perror("Getting sub-buffer len failed.");
386 /* Make sure the tracer is not gone mad on us! */
387 assert(len
>= subbuf_size
);
389 padding
= len
- subbuf_size
;
391 /* write the subbuffer to the tracefile */
392 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
395 * The mmap operation should write subbuf_size amount of data when
396 * network streaming or the full padding (len) size when we are _not_
399 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= -1) ||
400 (ret
!= len
&& stream
->net_seq_idx
== -1)) {
402 * Display the error but continue processing to try to release the
405 ERR("Error writing to tracefile "
406 "(ret: %zd != len: %lu != subbuf_size: %lu)",
407 ret
, len
, subbuf_size
);
411 ERR("Unknown output method");
415 err
= kernctl_put_next_subbuf(infd
);
418 if (errno
== EFAULT
) {
419 perror("Error in unreserving sub buffer\n");
420 } else if (errno
== EIO
) {
421 /* Should never happen with newer LTTng versions */
422 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
433 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
437 /* Opening the tracefile in write mode */
438 if (strlen(stream
->path_name
) > 0 && stream
->net_seq_idx
== -1) {
439 ret
= run_as_open(stream
->path_name
,
440 O_WRONLY
|O_CREAT
|O_TRUNC
,
441 S_IRWXU
|S_IRWXG
|S_IRWXO
,
442 stream
->uid
, stream
->gid
);
444 ERR("Opening %s", stream
->path_name
);
448 stream
->out_fd
= ret
;
451 if (stream
->output
== LTTNG_EVENT_MMAP
) {
452 /* get the len of the mmap region */
453 unsigned long mmap_len
;
455 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
458 perror("kernctl_get_mmap_len");
461 stream
->mmap_len
= (size_t) mmap_len
;
463 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
,
464 PROT_READ
, MAP_PRIVATE
, stream
->wait_fd
, 0);
465 if (stream
->mmap_base
== MAP_FAILED
) {
466 perror("Error mmaping");
472 /* we return 0 to let the library handle the FD internally */
479 err
= close(stream
->out_fd
);
487 * Check if data is still being extracted from the buffers for a specific
488 * stream. Consumer data lock MUST be acquired before calling this function
489 * and the stream lock.
491 * Return 1 if the traced data are still getting read else 0 meaning that the
492 * data is available for trace viewer reading.
494 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
500 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
502 /* There is still data so let's put back this subbuffer. */
503 ret
= kernctl_put_subbuf(stream
->wait_fd
);
505 ret
= 1; /* Data is pending */
509 /* Data is NOT pending and ready to be read. */