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>
31 #include <lttng/ust-ctl.h>
33 #include <common/common.h>
34 #include <common/sessiond-comm/sessiond-comm.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
;
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");
65 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
70 PERROR("Error in file write");
73 /* This won't block, but will start writeout asynchronously */
74 sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
75 SYNC_FILE_RANGE_WRITE
);
76 stream
->out_fd_offset
+= ret
;
79 lttng_consumer_sync_trace_file(stream
, orig_offset
);
88 * Splice the data from the ring buffer to the tracefile.
90 * Returns the number of bytes spliced.
92 ssize_t
lttng_ustconsumer_on_read_subbuffer_splice(
93 struct lttng_consumer_local_data
*ctx
,
94 struct lttng_consumer_stream
*stream
, unsigned long len
)
100 * Take a snapshot for a specific fd
102 * Returns 0 on success, < 0 on error
104 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
105 struct lttng_consumer_stream
*stream
)
109 ret
= ustctl_snapshot(stream
->chan
->handle
, stream
->buf
);
112 PERROR("Getting sub-buffer snapshot.");
119 * Get the produced position
121 * Returns 0 on success, < 0 on error
123 int lttng_ustconsumer_get_produced_snapshot(
124 struct lttng_consumer_local_data
*ctx
,
125 struct lttng_consumer_stream
*stream
,
130 ret
= ustctl_snapshot_get_produced(stream
->chan
->handle
,
134 PERROR("kernctl_snapshot_get_produced");
140 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
141 int sock
, struct pollfd
*consumer_sockpoll
)
144 struct lttcomm_consumer_msg msg
;
146 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
147 if (ret
!= sizeof(msg
)) {
148 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
151 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
155 switch (msg
.cmd_type
) {
156 case LTTNG_CONSUMER_ADD_CHANNEL
:
158 struct lttng_consumer_channel
*new_channel
;
163 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
166 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
167 if (ret
!= sizeof(fds
)) {
168 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
172 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
174 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
176 msg
.u
.channel
.mmap_len
,
177 msg
.u
.channel
.max_sb_size
);
178 if (new_channel
== NULL
) {
179 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
182 if (ctx
->on_recv_channel
!= NULL
) {
183 ret
= ctx
->on_recv_channel(new_channel
);
185 consumer_add_channel(new_channel
);
186 } else if (ret
< 0) {
190 consumer_add_channel(new_channel
);
194 case LTTNG_CONSUMER_ADD_STREAM
:
196 struct lttng_consumer_stream
*new_stream
;
201 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
204 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
205 if (ret
!= sizeof(fds
)) {
206 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
210 DBG("consumer_add_stream %s (%d,%d)", msg
.u
.stream
.path_name
,
212 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
213 new_stream
= consumer_allocate_stream(msg
.u
.channel
.channel_key
,
214 msg
.u
.stream
.stream_key
,
217 msg
.u
.stream
.mmap_len
,
219 msg
.u
.stream
.path_name
,
222 if (new_stream
== NULL
) {
223 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
226 if (ctx
->on_recv_stream
!= NULL
) {
227 ret
= ctx
->on_recv_stream(new_stream
);
229 consumer_add_stream(new_stream
);
230 } else if (ret
< 0) {
234 consumer_add_stream(new_stream
);
238 case LTTNG_CONSUMER_UPDATE_STREAM
:
242 if (ctx
->on_update_stream
!= NULL
) {
243 ret
= ctx
->on_update_stream(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
245 consumer_change_stream_state(msg
.u
.stream
.stream_key
, msg
.u
.stream
.state
);
246 } else if (ret
< 0) {
250 consumer_change_stream_state(msg
.u
.stream
.stream_key
,
260 /* signal the poll thread */
261 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
263 PERROR("write consumer poll");
269 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
271 struct lttng_ust_object_data obj
;
274 obj
.shm_fd
= chan
->shm_fd
;
275 obj
.wait_fd
= chan
->wait_fd
;
276 obj
.memory_map_size
= chan
->mmap_len
;
277 chan
->handle
= ustctl_map_channel(&obj
);
281 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 /* ustctl_open_stream_read has closed the shm fd. */
314 stream
->wait_fd_is_copy
= 1;
317 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
318 if (!stream
->mmap_base
) {
325 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
327 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
331 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
332 struct lttng_consumer_local_data
*ctx
)
337 struct lttng_ust_shm_handle
*handle
;
338 struct lttng_ust_lib_ring_buffer
*buf
;
342 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
343 stream
->wait_fd
, stream
->key
);
345 /* We can consume the 1 byte written into the wait_fd by UST */
346 if (!stream
->hangup_flush_done
) {
348 readlen
= read(stream
->wait_fd
, &dummy
, 1);
349 } while (readlen
== -1 && errno
== EINTR
);
357 handle
= stream
->chan
->handle
;
358 /* Get the next subbuffer */
359 err
= ustctl_get_next_subbuf(handle
, buf
);
361 ret
= -ret
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
363 * This is a debug message even for single-threaded consumer,
364 * because poll() have more relaxed criterions than get subbuf,
365 * so get_subbuf may fail for short race windows where poll()
366 * would issue wakeups.
368 DBG("Reserving sub buffer failed (everything is normal, "
369 "it is due to concurrency)");
372 assert(stream
->output
== LTTNG_EVENT_MMAP
);
373 /* read the used subbuffer size */
374 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
376 /* write the subbuffer to the tracefile */
377 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
380 * display the error but continue processing to try
381 * to release the subbuffer
383 ERR("Error writing to tracefile");
385 err
= ustctl_put_next_subbuf(handle
, buf
);
391 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
395 /* Opening the tracefile in write mode */
396 if (stream
->path_name
!= NULL
) {
397 ret
= run_as_open(stream
->path_name
,
398 O_WRONLY
|O_CREAT
|O_TRUNC
,
399 S_IRWXU
|S_IRWXG
|S_IRWXO
,
400 stream
->uid
, stream
->gid
);
402 ERR("Opening %s", stream
->path_name
);
406 stream
->out_fd
= ret
;
409 /* we return 0 to let the library handle the FD internally */