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>
29 #include <sys/types.h>
32 #include <lttng-sessiond-comm.h>
33 #include <lttng/lttng-ustconsumer.h>
34 #include <lttng/ust-ctl.h>
37 extern struct lttng_consumer_global_data consumer_data
;
38 extern int consumer_poll_timeout
;
39 extern volatile int consumer_quit
;
42 * Mmap the ring buffer, read it and write the data to the tracefile.
44 * Returns the number of bytes written
46 int lttng_ustconsumer_on_read_subbuffer_mmap(
47 struct lttng_consumer_local_data
*ctx
,
48 struct lttng_consumer_stream
*stream
, unsigned long len
)
50 unsigned long mmap_offset
;
52 off_t orig_offset
= stream
->out_fd_offset
;
53 int outfd
= stream
->out_fd
;
55 /* get the offset inside the fd to mmap */
56 ret
= ustctl_get_mmap_read_offset(stream
->chan
->handle
,
57 stream
->buf
, &mmap_offset
);
60 perror("ustctl_get_mmap_read_offset");
64 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
69 perror("Error in file write");
72 /* This won't block, but will start writeout asynchronously */
73 sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
74 SYNC_FILE_RANGE_WRITE
);
75 stream
->out_fd_offset
+= ret
;
78 lttng_consumer_sync_trace_file(stream
, orig_offset
);
87 * Splice the data from the ring buffer to the tracefile.
89 * Returns the number of bytes spliced.
91 int lttng_ustconsumer_on_read_subbuffer_splice(
92 struct lttng_consumer_local_data
*ctx
,
93 struct lttng_consumer_stream
*stream
, unsigned long len
)
99 * Take a snapshot for a specific fd
101 * Returns 0 on success, < 0 on error
103 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
104 struct lttng_consumer_stream
*stream
)
108 ret
= ustctl_snapshot(stream
->chan
->handle
, stream
->buf
);
111 perror("Getting sub-buffer snapshot.");
118 * Get the produced position
120 * Returns 0 on success, < 0 on error
122 int lttng_ustconsumer_get_produced_snapshot(
123 struct lttng_consumer_local_data
*ctx
,
124 struct lttng_consumer_stream
*stream
,
129 ret
= ustctl_snapshot_get_produced(stream
->chan
->handle
,
133 perror("kernctl_snapshot_get_produced");
139 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
140 int sock
, struct pollfd
*consumer_sockpoll
)
143 struct lttcomm_consumer_msg msg
;
145 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
146 if (ret
!= sizeof(msg
)) {
147 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
150 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
154 switch (msg
.cmd_type
) {
155 case LTTNG_CONSUMER_ADD_CHANNEL
:
157 struct lttng_consumer_channel
*new_channel
;
162 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
165 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
166 if (ret
!= sizeof(fds
)) {
167 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
171 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
173 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
175 msg
.u
.channel
.mmap_len
,
176 msg
.u
.channel
.max_sb_size
);
177 if (new_channel
== NULL
) {
178 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
181 if (ctx
->on_recv_channel
!= NULL
) {
182 ret
= ctx
->on_recv_channel(new_channel
);
184 consumer_add_channel(new_channel
);
185 } else if (ret
< 0) {
189 consumer_add_channel(new_channel
);
193 case LTTNG_CONSUMER_ADD_STREAM
:
195 struct lttng_consumer_stream
*new_stream
;
200 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
203 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
204 if (ret
!= sizeof(fds
)) {
205 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
209 DBG("consumer_add_stream %s (%d,%d)", msg
.u
.stream
.path_name
,
211 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
212 new_stream
= consumer_allocate_stream(msg
.u
.channel
.channel_key
,
213 msg
.u
.stream
.stream_key
,
216 msg
.u
.stream
.mmap_len
,
218 msg
.u
.stream
.path_name
);
219 if (new_stream
== NULL
) {
220 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
223 if (ctx
->on_recv_stream
!= NULL
) {
224 ret
= ctx
->on_recv_stream(new_stream
);
226 consumer_add_stream(new_stream
);
227 } else if (ret
< 0) {
231 consumer_add_stream(new_stream
);
235 case LTTNG_CONSUMER_UPDATE_STREAM
:
239 if (ctx
->on_update_stream
!= NULL
) {
240 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
242 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
243 } else if (ret
< 0) {
247 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
257 /* signal the poll thread */
258 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
260 perror("write consumer poll");
266 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
268 struct lttng_ust_object_data obj
;
271 obj
.shm_fd
= chan
->shm_fd
;
272 obj
.wait_fd
= chan
->wait_fd
;
273 obj
.memory_map_size
= chan
->mmap_len
;
274 chan
->handle
= ustctl_map_channel(&obj
);
279 * The channel fds are passed to ustctl, we only keep a copy.
281 chan
->shm_fd_is_copy
= 1;
282 chan
->wait_fd_is_copy
= 1;
287 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
289 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
290 stream
->hangup_flush_done
= 1;
293 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
295 ustctl_unmap_channel(chan
->handle
);
298 int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream
*stream
)
300 struct lttng_ust_object_data obj
;
304 obj
.shm_fd
= stream
->shm_fd
;
305 obj
.wait_fd
= stream
->wait_fd
;
306 obj
.memory_map_size
= stream
->mmap_len
;
307 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
310 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
313 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
314 if (!stream
->mmap_base
) {
318 * The stream fds are passed to ustctl, we only keep a copy.
320 stream
->shm_fd_is_copy
= 1;
321 stream
->wait_fd_is_copy
= 1;
326 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
328 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
332 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
333 struct lttng_consumer_local_data
*ctx
)
338 struct lttng_ust_shm_handle
*handle
;
339 struct lttng_ust_lib_ring_buffer
*buf
;
343 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
344 stream
->wait_fd
, stream
->key
);
346 /* We can consume the 1 byte written into the wait_fd by UST */
347 if (!stream
->hangup_flush_done
) {
349 readlen
= read(stream
->wait_fd
, &dummy
, 1);
350 } while (readlen
== -1 && errno
== -EINTR
);
358 handle
= stream
->chan
->handle
;
359 /* Get the next subbuffer */
360 err
= ustctl_get_next_subbuf(handle
, buf
);
362 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
364 * This is a debug message even for single-threaded consumer,
365 * because poll() have more relaxed criterions than get subbuf,
366 * so get_subbuf may fail for short race windows where poll()
367 * would issue wakeups.
369 DBG("Reserving sub buffer failed (everything is normal, "
370 "it is due to concurrency)");
373 assert(stream
->output
== LTTNG_EVENT_MMAP
);
374 /* read the used subbuffer size */
375 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
377 /* write the subbuffer to the tracefile */
378 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
381 * display the error but continue processing to try
382 * to release the subbuffer
384 ERR("Error writing to tracefile");
386 err
= ustctl_put_next_subbuf(handle
, buf
);
392 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
396 /* Opening the tracefile in write mode */
397 if (stream
->path_name
!= NULL
) {
398 ret
= open(stream
->path_name
,
399 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
401 ERR("Opening %s", stream
->path_name
);
405 stream
->out_fd
= ret
;
408 /* we return 0 to let the library handle the FD internally */