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>
28 #include <sys/types.h>
30 #include <lttng/ust-ctl.h>
32 #include <common/common.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/compat/fcntl.h>
36 #include "ust-consumer.h"
38 extern struct lttng_consumer_global_data consumer_data
;
39 extern int consumer_poll_timeout
;
40 extern volatile int consumer_quit
;
43 * Mmap the ring buffer, read it and write the data to the tracefile.
45 * Returns the number of bytes written, else negative value on error.
47 ssize_t
lttng_ustconsumer_on_read_subbuffer_mmap(
48 struct lttng_consumer_local_data
*ctx
,
49 struct lttng_consumer_stream
*stream
, unsigned long len
)
51 unsigned long mmap_offset
;
52 long ret
= 0, written
= 0;
53 off_t orig_offset
= stream
->out_fd_offset
;
54 int outfd
= stream
->out_fd
;
56 /* get the offset inside the fd to mmap */
57 ret
= ustctl_get_mmap_read_offset(stream
->chan
->handle
,
58 stream
->buf
, &mmap_offset
);
61 PERROR("ustctl_get_mmap_read_offset");
66 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
69 /* restart the interrupted system call */
72 PERROR("Error in file write");
78 } else if (ret
> len
) {
79 PERROR("Error in file write");
86 /* This won't block, but will start writeout asynchronously */
87 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
88 SYNC_FILE_RANGE_WRITE
);
89 stream
->out_fd_offset
+= ret
;
92 lttng_consumer_sync_trace_file(stream
, orig_offset
);
98 * Splice the data from the ring buffer to the tracefile.
100 * Returns the number of bytes spliced.
102 ssize_t
lttng_ustconsumer_on_read_subbuffer_splice(
103 struct lttng_consumer_local_data
*ctx
,
104 struct lttng_consumer_stream
*stream
, unsigned long len
)
110 * Take a snapshot for a specific fd
112 * Returns 0 on success, < 0 on error
114 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
115 struct lttng_consumer_stream
*stream
)
119 ret
= ustctl_snapshot(stream
->chan
->handle
, stream
->buf
);
122 PERROR("Getting sub-buffer snapshot.");
129 * Get the produced position
131 * Returns 0 on success, < 0 on error
133 int lttng_ustconsumer_get_produced_snapshot(
134 struct lttng_consumer_local_data
*ctx
,
135 struct lttng_consumer_stream
*stream
,
140 ret
= ustctl_snapshot_get_produced(stream
->chan
->handle
,
144 PERROR("kernctl_snapshot_get_produced");
150 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
151 int sock
, struct pollfd
*consumer_sockpoll
)
154 struct lttcomm_consumer_msg msg
;
156 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
157 if (ret
!= sizeof(msg
)) {
158 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
161 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
165 switch (msg
.cmd_type
) {
166 case LTTNG_CONSUMER_ADD_CHANNEL
:
168 struct lttng_consumer_channel
*new_channel
;
173 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
177 path
= lttcomm_recv_string(sock
);
180 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
184 DBG("consumer_add_channel received path %s", path
);
186 fds
[0] = open(path
, O_RDWR
);
189 DBG("consumer_add_channel open error on path %s", path
);
194 if (fcntl(fds
[0], F_SETFD
, FD_CLOEXEC
) < 0) {
195 DBG("consumer_add_channel fcntl error");
202 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
204 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
206 msg
.u
.channel
.mmap_len
,
207 msg
.u
.channel
.max_sb_size
);
208 if (new_channel
== NULL
) {
209 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
212 if (ctx
->on_recv_channel
!= NULL
) {
213 ret
= ctx
->on_recv_channel(new_channel
);
215 consumer_add_channel(new_channel
);
216 } else if (ret
< 0) {
220 consumer_add_channel(new_channel
);
224 case LTTNG_CONSUMER_ADD_STREAM
:
226 struct lttng_consumer_stream
*new_stream
;
228 char *shm_path
, *wait_pipe_path
;
231 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
235 shm_path
= lttcomm_recv_string(sock
);
238 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
242 wait_pipe_path
= lttcomm_recv_string(sock
);
244 if (!wait_pipe_path
) {
245 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
250 DBG("consumer_add_stream received path %s", shm_path
);
251 DBG("consumer_add_stream received path %s", wait_pipe_path
);
253 fds
[0] = open(shm_path
, O_RDWR
);
256 DBG("consumer_add_stream open error on path %s", shm_path
);
258 free(wait_pipe_path
);
262 fds
[1] = open(wait_pipe_path
, O_RDONLY
);
265 DBG("consumer_add_stream open error on path %s", wait_pipe_path
);
268 free(wait_pipe_path
);
273 free(wait_pipe_path
);
275 for (i
= 0; i
< 2; ++i
) {
276 if (fcntl(fds
[i
], F_SETFD
, FD_CLOEXEC
) < 0) {
277 DBG("consumer_add_stream fcntl error");
282 DBG("consumer_add_stream %s (%d,%d)", msg
.u
.stream
.path_name
,
284 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
285 new_stream
= consumer_allocate_stream(msg
.u
.channel
.channel_key
,
286 msg
.u
.stream
.stream_key
,
289 msg
.u
.stream
.mmap_len
,
291 msg
.u
.stream
.path_name
,
294 if (new_stream
== NULL
) {
295 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
298 if (ctx
->on_recv_stream
!= NULL
) {
299 ret
= ctx
->on_recv_stream(new_stream
);
301 consumer_add_stream(new_stream
);
302 } else if (ret
< 0) {
306 consumer_add_stream(new_stream
);
310 case LTTNG_CONSUMER_UPDATE_STREAM
:
314 if (ctx
->on_update_stream
!= NULL
) {
315 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
317 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
318 } else if (ret
< 0) {
322 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
333 * Wake-up the other end by writing a null byte in the pipe
334 * (non-blocking). Important note: Because writing into the
335 * pipe is non-blocking (and therefore we allow dropping wakeup
336 * data, as long as there is wakeup data present in the pipe
337 * buffer to wake up the other end), the other end should
338 * perform the following sequence for waiting:
339 * 1) empty the pipe (reads).
340 * 2) perform update operation.
341 * 3) wait on the pipe (poll).
344 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
345 } while (ret
== -1UL && errno
== EINTR
);
349 * Return 1 to indicate success since the 0 value can be a socket shutdown
350 * during the recv() or send() call.
355 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
357 struct lttng_ust_object_data obj
;
360 obj
.shm_fd
= chan
->shm_fd
;
361 obj
.wait_fd
= chan
->wait_fd
;
362 obj
.memory_map_size
= chan
->mmap_len
;
363 chan
->handle
= ustctl_map_channel(&obj
);
367 chan
->wait_fd_is_copy
= 1;
373 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
375 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
376 stream
->hangup_flush_done
= 1;
379 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
381 ustctl_unmap_channel(chan
->handle
);
384 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
386 struct lttng_ust_object_data obj
;
390 obj
.shm_fd
= stream
->shm_fd
;
391 obj
.wait_fd
= stream
->wait_fd
;
392 obj
.memory_map_size
= stream
->mmap_len
;
393 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
396 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
399 /* ustctl_open_stream_read has closed the shm fd. */
400 stream
->wait_fd_is_copy
= 1;
403 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
404 if (!stream
->mmap_base
) {
411 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
413 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
417 int lttng_ustconsumer_check_pipe(struct lttng_consumer_stream
*stream
,
418 struct lttng_consumer_local_data
*ctx
)
423 DBG("In check_pipe (wait_fd: %d, stream key: %d)\n",
424 stream
->wait_fd
, stream
->key
);
426 /* We consume the 1 byte written into the wait_fd by UST */
429 readlen
= read(stream
->wait_fd
, &dummy
, 1);
430 } while (readlen
== -1 && errno
== EINTR
);
432 return -1; /* error */
435 DBG("Read %zu byte from pipe: %c\n", readlen
, readlen
? dummy
: '\0');
438 return 1; /* POLLHUP */
440 return 0; /* no error nor HUP */
444 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
445 struct lttng_consumer_local_data
*ctx
)
450 struct lttng_ust_shm_handle
*handle
;
451 struct lttng_ust_lib_ring_buffer
*buf
;
455 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
456 stream
->wait_fd
, stream
->key
);
458 /* We can consume the 1 byte written into the wait_fd by UST */
459 if (!stream
->hangup_flush_done
) {
461 readlen
= read(stream
->wait_fd
, &dummy
, 1);
462 } while (readlen
== -1 && errno
== EINTR
);
467 DBG("Read %zu byte from pipe: %c\n", readlen
, dummy
);
471 handle
= stream
->chan
->handle
;
472 /* Get the next subbuffer */
473 err
= ustctl_get_next_subbuf(handle
, buf
);
475 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
477 * This is a debug message even for single-threaded consumer,
478 * because poll() have more relaxed criterions than get subbuf,
479 * so get_subbuf may fail for short race windows where poll()
480 * would issue wakeups.
482 DBG("Reserving sub buffer failed (everything is normal, "
483 "it is due to concurrency)");
486 assert(stream
->output
== LTTNG_EVENT_MMAP
);
487 /* read the used subbuffer size */
488 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
490 /* write the subbuffer to the tracefile */
491 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
494 * display the error but continue processing to try
495 * to release the subbuffer
497 ERR("Error writing to tracefile");
499 err
= ustctl_put_next_subbuf(handle
, buf
);
505 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
509 /* Opening the tracefile in write mode */
510 if (stream
->path_name
!= NULL
) {
511 ret
= run_as_open(stream
->path_name
,
512 O_WRONLY
|O_CREAT
|O_TRUNC
,
513 S_IRWXU
|S_IRWXG
|S_IRWXO
,
514 stream
->uid
, stream
->gid
);
516 ERR("Opening %s", stream
->path_name
);
520 stream
->out_fd
= ret
;
523 /* we return 0 to let the library handle the FD internally */