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
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <sys/socket.h>
30 #include <sys/types.h>
32 #include <lttng/ust-ctl.h>
34 #include <common/common.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/compat/fcntl.h>
38 #include "ust-consumer.h"
40 extern struct lttng_consumer_global_data consumer_data
;
41 extern int consumer_poll_timeout
;
42 extern volatile int consumer_quit
;
45 * Mmap the ring buffer, read it and write the data to the tracefile.
47 * Returns the number of bytes written
49 int lttng_ustconsumer_on_read_subbuffer_mmap(
50 struct lttng_consumer_local_data
*ctx
,
51 struct lttng_consumer_stream
*stream
, unsigned long len
)
53 unsigned long mmap_offset
;
55 off_t orig_offset
= stream
->out_fd_offset
;
56 int outfd
= stream
->out_fd
;
58 /* get the offset inside the fd to mmap */
59 ret
= ustctl_get_mmap_read_offset(stream
->chan
->handle
,
60 stream
->buf
, &mmap_offset
);
63 perror("ustctl_get_mmap_read_offset");
67 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
72 perror("Error in file write");
75 /* This won't block, but will start writeout asynchronously */
76 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
77 SYNC_FILE_RANGE_WRITE
);
78 stream
->out_fd_offset
+= ret
;
81 lttng_consumer_sync_trace_file(stream
, orig_offset
);
90 * Splice the data from the ring buffer to the tracefile.
92 * Returns the number of bytes spliced.
94 int lttng_ustconsumer_on_read_subbuffer_splice(
95 struct lttng_consumer_local_data
*ctx
,
96 struct lttng_consumer_stream
*stream
, unsigned long len
)
102 * Take a snapshot for a specific fd
104 * Returns 0 on success, < 0 on error
106 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
107 struct lttng_consumer_stream
*stream
)
111 ret
= ustctl_snapshot(stream
->chan
->handle
, stream
->buf
);
114 perror("Getting sub-buffer snapshot.");
121 * Get the produced position
123 * Returns 0 on success, < 0 on error
125 int lttng_ustconsumer_get_produced_snapshot(
126 struct lttng_consumer_local_data
*ctx
,
127 struct lttng_consumer_stream
*stream
,
132 ret
= ustctl_snapshot_get_produced(stream
->chan
->handle
,
136 perror("kernctl_snapshot_get_produced");
142 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
143 int sock
, struct pollfd
*consumer_sockpoll
)
146 struct lttcomm_consumer_msg msg
;
148 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
149 if (ret
!= sizeof(msg
)) {
150 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
153 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
157 switch (msg
.cmd_type
) {
158 case LTTNG_CONSUMER_ADD_CHANNEL
:
160 struct lttng_consumer_channel
*new_channel
;
165 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
168 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
169 if (ret
!= sizeof(fds
)) {
170 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
174 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
176 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
178 msg
.u
.channel
.mmap_len
,
179 msg
.u
.channel
.max_sb_size
);
180 if (new_channel
== NULL
) {
181 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
184 if (ctx
->on_recv_channel
!= NULL
) {
185 ret
= ctx
->on_recv_channel(new_channel
);
187 consumer_add_channel(new_channel
);
188 } else if (ret
< 0) {
192 consumer_add_channel(new_channel
);
196 case LTTNG_CONSUMER_ADD_STREAM
:
198 struct lttng_consumer_stream
*new_stream
;
203 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
206 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
207 if (ret
!= sizeof(fds
)) {
208 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
212 DBG("consumer_add_stream %s (%d,%d)", msg
.u
.stream
.path_name
,
214 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
215 new_stream
= consumer_allocate_stream(msg
.u
.channel
.channel_key
,
216 msg
.u
.stream
.stream_key
,
219 msg
.u
.stream
.mmap_len
,
221 msg
.u
.stream
.path_name
,
224 if (new_stream
== NULL
) {
225 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
228 if (ctx
->on_recv_stream
!= NULL
) {
229 ret
= ctx
->on_recv_stream(new_stream
);
231 consumer_add_stream(new_stream
);
232 } else if (ret
< 0) {
236 consumer_add_stream(new_stream
);
240 case LTTNG_CONSUMER_UPDATE_STREAM
:
244 if (ctx
->on_update_stream
!= NULL
) {
245 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
247 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
248 } else if (ret
< 0) {
252 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
262 /* signal the poll thread */
263 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
265 perror("write consumer poll");
271 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
273 struct lttng_ust_object_data obj
;
276 obj
.shm_fd
= chan
->shm_fd
;
277 obj
.wait_fd
= chan
->wait_fd
;
278 obj
.memory_map_size
= chan
->mmap_len
;
279 chan
->handle
= ustctl_map_channel(&obj
);
283 chan
->wait_fd_is_copy
= 1;
289 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
291 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
292 stream
->hangup_flush_done
= 1;
295 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
297 ustctl_unmap_channel(chan
->handle
);
300 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
302 struct lttng_ust_object_data obj
;
306 obj
.shm_fd
= stream
->shm_fd
;
307 obj
.wait_fd
= stream
->wait_fd
;
308 obj
.memory_map_size
= stream
->mmap_len
;
309 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
312 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
315 /* ustctl_open_stream_read has closed the shm fd. */
316 stream
->wait_fd_is_copy
= 1;
319 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
320 if (!stream
->mmap_base
) {
327 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
329 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
333 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
334 struct lttng_consumer_local_data
*ctx
)
339 struct lttng_ust_shm_handle
*handle
;
340 struct lttng_ust_lib_ring_buffer
*buf
;
344 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
345 stream
->wait_fd
, stream
->key
);
347 /* We can consume the 1 byte written into the wait_fd by UST */
348 if (!stream
->hangup_flush_done
) {
350 readlen
= read(stream
->wait_fd
, &dummy
, 1);
351 } while (readlen
== -1 && errno
== -EINTR
);
359 handle
= stream
->chan
->handle
;
360 /* Get the next subbuffer */
361 err
= ustctl_get_next_subbuf(handle
, buf
);
363 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
365 * This is a debug message even for single-threaded consumer,
366 * because poll() have more relaxed criterions than get subbuf,
367 * so get_subbuf may fail for short race windows where poll()
368 * would issue wakeups.
370 DBG("Reserving sub buffer failed (everything is normal, "
371 "it is due to concurrency)");
374 assert(stream
->output
== LTTNG_EVENT_MMAP
);
375 /* read the used subbuffer size */
376 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
378 /* write the subbuffer to the tracefile */
379 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
382 * display the error but continue processing to try
383 * to release the subbuffer
385 ERR("Error writing to tracefile");
387 err
= ustctl_put_next_subbuf(handle
, buf
);
393 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
397 /* Opening the tracefile in write mode */
398 if (stream
->path_name
!= NULL
) {
399 ret
= run_as_open(stream
->path_name
,
400 O_WRONLY
|O_CREAT
|O_TRUNC
,
401 S_IRWXU
|S_IRWXG
|S_IRWXO
,
402 stream
->uid
, stream
->gid
);
404 ERR("Opening %s", stream
->path_name
);
408 stream
->out_fd
= ret
;
411 /* we return 0 to let the library handle the FD internally */