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.
21 #include <lttng/ust-ctl.h>
27 #include <sys/socket.h>
29 #include <sys/types.h>
33 #include <common/common.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/relayd/relayd.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 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
46 * compiled out, we isolate it in this library.
48 int lttng_ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle
*handle
,
49 struct lttng_ust_lib_ring_buffer
*buf
, unsigned long *off
)
51 return ustctl_get_mmap_read_offset(handle
, buf
, off
);
55 * Take a snapshot for a specific fd
57 * Returns 0 on success, < 0 on error
59 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
60 struct lttng_consumer_stream
*stream
)
64 ret
= ustctl_snapshot(stream
->chan
->handle
, stream
->buf
);
67 PERROR("Getting sub-buffer snapshot.");
74 * Get the produced position
76 * Returns 0 on success, < 0 on error
78 int lttng_ustconsumer_get_produced_snapshot(
79 struct lttng_consumer_local_data
*ctx
,
80 struct lttng_consumer_stream
*stream
,
85 ret
= ustctl_snapshot_get_produced(stream
->chan
->handle
,
89 PERROR("kernctl_snapshot_get_produced");
96 * Receive command from session daemon and process it.
98 * Return 1 on success else a negative value or 0.
100 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
101 int sock
, struct pollfd
*consumer_sockpoll
)
104 enum lttng_error_code ret_code
= LTTNG_OK
;
105 struct lttcomm_consumer_msg msg
;
107 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
108 if (ret
!= sizeof(msg
)) {
109 DBG("Consumer received unexpected message size %zd (expects %zu)",
111 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
114 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
116 * Notify the session daemon that the command is completed.
118 * On transport layer error, the function call will print an error
119 * message so handling the returned code is a bit useless since we
120 * return an error code anyway.
122 (void) consumer_send_status_msg(sock
, ret_code
);
126 /* relayd needs RCU read-side lock */
129 switch (msg
.cmd_type
) {
130 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
132 /* Session daemon status message are handled in the following call. */
133 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
134 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
135 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
);
138 case LTTNG_CONSUMER_ADD_CHANNEL
:
140 struct lttng_consumer_channel
*new_channel
;
144 DBG("UST Consumer adding channel");
146 /* First send a status message before receiving the fds. */
147 ret
= consumer_send_status_msg(sock
, ret_code
);
149 /* Somehow, the session daemon is not responding anymore. */
154 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
158 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
159 if (ret
!= sizeof(fds
)) {
160 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
166 * Send status code to session daemon only if the recv works. If the
167 * above recv() failed, the session daemon is notified through the
168 * error socket and the teardown is eventually done.
170 ret
= consumer_send_status_msg(sock
, ret_code
);
172 /* Somehow, the session daemon is not responding anymore. */
176 DBG("consumer_add_channel %d", msg
.u
.channel
.channel_key
);
178 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
180 msg
.u
.channel
.mmap_len
,
181 msg
.u
.channel
.max_sb_size
,
182 msg
.u
.channel
.nb_init_streams
);
183 if (new_channel
== NULL
) {
184 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
187 if (ctx
->on_recv_channel
!= NULL
) {
188 ret
= ctx
->on_recv_channel(new_channel
);
190 consumer_add_channel(new_channel
);
191 } else if (ret
< 0) {
195 consumer_add_channel(new_channel
);
199 case LTTNG_CONSUMER_ADD_STREAM
:
201 struct lttng_consumer_stream
*new_stream
;
202 int fds
[2], stream_pipe
;
204 struct consumer_relayd_sock_pair
*relayd
= NULL
;
207 DBG("UST Consumer adding stream");
209 /* First send a status message before receiving the fds. */
210 ret
= consumer_send_status_msg(sock
, ret_code
);
212 /* Somehow, the session daemon is not responding anymore. */
217 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
221 ret
= lttcomm_recv_fds_unix_sock(sock
, fds
, nb_fd
);
222 if (ret
!= sizeof(fds
)) {
223 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
229 * Send status code to session daemon only if the recv works. If the
230 * above recv() failed, the session daemon is notified through the
231 * error socket and the teardown is eventually done.
233 ret
= consumer_send_status_msg(sock
, ret_code
);
235 /* Somehow, the session daemon is not responding anymore. */
239 DBG("Consumer command ADD_STREAM chan %d stream %d",
240 msg
.u
.stream
.channel_key
, msg
.u
.stream
.stream_key
);
242 assert(msg
.u
.stream
.output
== LTTNG_EVENT_MMAP
);
243 new_stream
= consumer_allocate_stream(msg
.u
.stream
.channel_key
,
244 msg
.u
.stream
.stream_key
,
247 msg
.u
.stream
.mmap_len
,
249 msg
.u
.stream
.path_name
,
252 msg
.u
.stream
.net_index
,
253 msg
.u
.stream
.metadata_flag
,
254 msg
.u
.stream
.session_id
,
256 if (new_stream
== NULL
) {
261 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
265 * We could not find the channel. Can happen if cpu hotplug
266 * happens while tearing down.
268 DBG3("Could not find channel");
274 /* The stream is not metadata. Get relayd reference if exists. */
275 relayd
= consumer_find_relayd(msg
.u
.stream
.net_index
);
276 if (relayd
!= NULL
) {
277 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
278 /* Add stream on the relayd */
279 ret
= relayd_add_stream(&relayd
->control_sock
,
280 msg
.u
.stream
.name
, msg
.u
.stream
.path_name
,
281 &new_stream
->relayd_stream_id
);
282 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
284 consumer_del_stream(new_stream
, NULL
);
287 } else if (msg
.u
.stream
.net_index
!= -1) {
288 ERR("Network sequence index %d unknown. Not adding stream.",
289 msg
.u
.stream
.net_index
);
290 consumer_del_stream(new_stream
, NULL
);
294 /* Do actions once stream has been received. */
295 if (ctx
->on_recv_stream
) {
296 ret
= ctx
->on_recv_stream(new_stream
);
298 consumer_del_stream(new_stream
, NULL
);
303 /* Get the right pipe where the stream will be sent. */
304 if (new_stream
->metadata_flag
) {
305 stream_pipe
= ctx
->consumer_metadata_pipe
[1];
307 stream_pipe
= ctx
->consumer_data_pipe
[1];
311 ret
= write(stream_pipe
, &new_stream
, sizeof(new_stream
));
312 } while (ret
< 0 && errno
== EINTR
);
314 PERROR("Consumer write %s stream to pipe %d",
315 new_stream
->metadata_flag
? "metadata" : "data",
317 consumer_del_stream(new_stream
, NULL
);
321 DBG("UST consumer ADD_STREAM %s (%d,%d) with relayd id %" PRIu64
,
322 msg
.u
.stream
.path_name
, fds
[0], fds
[1],
323 new_stream
->relayd_stream_id
);
326 case LTTNG_CONSUMER_DESTROY_RELAYD
:
328 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
329 struct consumer_relayd_sock_pair
*relayd
;
331 DBG("UST consumer destroying relayd %" PRIu64
, index
);
333 /* Get relayd reference if exists. */
334 relayd
= consumer_find_relayd(index
);
335 if (relayd
== NULL
) {
336 ERR("Unable to find relayd %" PRIu64
, index
);
337 ret_code
= LTTNG_ERR_NO_CONSUMER
;
341 * Each relayd socket pair has a refcount of stream attached to it
342 * which tells if the relayd is still active or not depending on the
345 * This will set the destroy flag of the relayd object and destroy it
346 * if the refcount reaches zero when called.
348 * The destroy can happen either here or when a stream fd hangs up.
351 consumer_flag_relayd_for_destroy(relayd
);
354 ret
= consumer_send_status_msg(sock
, ret_code
);
356 /* Somehow, the session daemon is not responding anymore. */
362 case LTTNG_CONSUMER_UPDATE_STREAM
:
367 case LTTNG_CONSUMER_DATA_PENDING
:
370 uint64_t id
= msg
.u
.data_pending
.session_id
;
372 DBG("UST consumer data pending command for id %" PRIu64
, id
);
374 ret
= consumer_data_pending(id
);
376 /* Send back returned value to session daemon */
377 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
379 PERROR("send data pending ret code");
383 * No need to send back a status message since the data pending
384 * returned value is the response.
396 * Return 1 to indicate success since the 0 value can be a socket
397 * shutdown during the recv() or send() call.
402 int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel
*chan
)
404 struct lttng_ust_object_data obj
;
407 obj
.shm_fd
= chan
->shm_fd
;
408 obj
.wait_fd
= chan
->wait_fd
;
409 obj
.memory_map_size
= chan
->mmap_len
;
410 chan
->handle
= ustctl_map_channel(&obj
);
414 chan
->wait_fd_is_copy
= 1;
420 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
422 ustctl_flush_buffer(stream
->chan
->handle
, stream
->buf
, 0);
423 stream
->hangup_flush_done
= 1;
426 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
428 ustctl_unmap_channel(chan
->handle
);
431 int lttng_ustconsumer_add_stream(struct lttng_consumer_stream
*stream
)
433 struct lttng_ust_object_data obj
;
437 obj
.shm_fd
= stream
->shm_fd
;
438 obj
.wait_fd
= stream
->wait_fd
;
439 obj
.memory_map_size
= stream
->mmap_len
;
440 ret
= ustctl_add_stream(stream
->chan
->handle
, &obj
);
442 ERR("UST ctl add_stream failed with ret %d", ret
);
446 stream
->buf
= ustctl_open_stream_read(stream
->chan
->handle
, stream
->cpu
);
448 ERR("UST ctl open_stream_read failed");
453 /* ustctl_open_stream_read has closed the shm fd. */
454 stream
->wait_fd_is_copy
= 1;
457 stream
->mmap_base
= ustctl_get_mmap_base(stream
->chan
->handle
, stream
->buf
);
458 if (!stream
->mmap_base
) {
459 ERR("UST ctl get_mmap_base failed");
467 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
472 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
474 ustctl_close_stream_read(stream
->chan
->handle
, stream
->buf
);
478 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
479 struct lttng_consumer_local_data
*ctx
)
481 unsigned long len
, subbuf_size
, padding
;
484 struct lttng_ust_shm_handle
*handle
;
485 struct lttng_ust_lib_ring_buffer
*buf
;
489 DBG("In read_subbuffer (wait_fd: %d, stream key: %d)",
490 stream
->wait_fd
, stream
->key
);
492 /* We can consume the 1 byte written into the wait_fd by UST */
493 if (!stream
->hangup_flush_done
) {
495 readlen
= read(stream
->wait_fd
, &dummy
, 1);
496 } while (readlen
== -1 && errno
== EINTR
);
504 handle
= stream
->chan
->handle
;
505 /* Get the next subbuffer */
506 err
= ustctl_get_next_subbuf(handle
, buf
);
508 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
510 * This is a debug message even for single-threaded consumer,
511 * because poll() have more relaxed criterions than get subbuf,
512 * so get_subbuf may fail for short race windows where poll()
513 * would issue wakeups.
515 DBG("Reserving sub buffer failed (everything is normal, "
516 "it is due to concurrency)");
519 assert(stream
->output
== LTTNG_EVENT_MMAP
);
520 /* Get the full padded subbuffer size */
521 err
= ustctl_get_padded_subbuf_size(handle
, buf
, &len
);
524 /* Get subbuffer data size (without padding) */
525 err
= ustctl_get_subbuf_size(handle
, buf
, &subbuf_size
);
528 /* Make sure we don't get a subbuffer size bigger than the padded */
529 assert(len
>= subbuf_size
);
531 padding
= len
- subbuf_size
;
532 /* write the subbuffer to the tracefile */
533 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
, padding
);
535 * The mmap operation should write subbuf_size amount of data when network
536 * streaming or the full padding (len) size when we are _not_ streaming.
538 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= -1) ||
539 (ret
!= len
&& stream
->net_seq_idx
== -1)) {
541 * Display the error but continue processing to try to release the
544 ERR("Error writing to tracefile "
545 "(ret: %zd != len: %lu != subbuf_size: %lu)",
546 ret
, len
, subbuf_size
);
548 err
= ustctl_put_next_subbuf(handle
, buf
);
554 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
558 /* Opening the tracefile in write mode */
559 if (stream
->path_name
!= NULL
&& stream
->net_seq_idx
== -1) {
560 ret
= run_as_open(stream
->path_name
,
561 O_WRONLY
|O_CREAT
|O_TRUNC
,
562 S_IRWXU
|S_IRWXG
|S_IRWXO
,
563 stream
->uid
, stream
->gid
);
565 ERR("Opening %s", stream
->path_name
);
569 stream
->out_fd
= ret
;
572 ret
= lttng_ustconsumer_add_stream(stream
);
574 consumer_del_stream(stream
, NULL
);
579 /* we return 0 to let the library handle the FD internally */
587 * Check if data is still being extracted from the buffers for a specific
588 * stream. Consumer data lock MUST be acquired before calling this function
589 * and the stream lock.
591 * Return 1 if the traced data are still getting read else 0 meaning that the
592 * data is available for trace viewer reading.
594 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
600 DBG("UST consumer checking data pending");
602 ret
= ustctl_get_next_subbuf(stream
->chan
->handle
, stream
->buf
);
604 /* There is still data so let's put back this subbuffer. */
605 ret
= ustctl_put_subbuf(stream
->chan
->handle
, stream
->buf
);
607 ret
= 1; /* Data is pending */
611 /* Data is NOT pending so ready to be read. */