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 <common/common.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/kernel-consumer/kernel-consumer.h>
36 #include <common/ust-consumer/ust-consumer.h>
40 struct lttng_consumer_global_data consumer_data
= {
43 .type
= LTTNG_CONSUMER_UNKNOWN
,
46 /* timeout parameter, to control the polling thread grace period. */
47 int consumer_poll_timeout
= -1;
50 * Flag to inform the polling thread to quit when all fd hung up. Updated by
51 * the consumer_thread_receive_fds when it notices that all fds has hung up.
52 * Also updated by the signal handler (consumer_should_exit()). Read by the
55 volatile int consumer_quit
= 0;
58 * Find a stream. The consumer_data.lock must be locked during this
61 static struct lttng_consumer_stream
*consumer_find_stream(int key
)
63 struct lttng_ht_iter iter
;
64 struct lttng_ht_node_ulong
*node
;
65 struct lttng_consumer_stream
*stream
= NULL
;
67 /* Negative keys are lookup failures */
73 lttng_ht_lookup(consumer_data
.stream_ht
, (void *)((unsigned long) key
),
75 node
= lttng_ht_iter_get_node_ulong(&iter
);
77 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
85 static void consumer_steal_stream_key(int key
)
87 struct lttng_consumer_stream
*stream
;
89 stream
= consumer_find_stream(key
);
94 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
96 struct lttng_ht_iter iter
;
97 struct lttng_ht_node_ulong
*node
;
98 struct lttng_consumer_channel
*channel
= NULL
;
100 /* Negative keys are lookup failures */
106 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
108 node
= lttng_ht_iter_get_node_ulong(&iter
);
110 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
118 static void consumer_steal_channel_key(int key
)
120 struct lttng_consumer_channel
*channel
;
122 channel
= consumer_find_channel(key
);
128 * Remove a stream from the global list protected by a mutex. This
129 * function is also responsible for freeing its data structures.
131 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
134 struct lttng_ht_iter iter
;
135 struct lttng_consumer_channel
*free_chan
= NULL
;
137 pthread_mutex_lock(&consumer_data
.lock
);
139 switch (consumer_data
.type
) {
140 case LTTNG_CONSUMER_KERNEL
:
141 if (stream
->mmap_base
!= NULL
) {
142 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
148 case LTTNG_CONSUMER32_UST
:
149 case LTTNG_CONSUMER64_UST
:
150 lttng_ustconsumer_del_stream(stream
);
153 ERR("Unknown consumer_data type");
160 /* Get stream node from hash table */
161 lttng_ht_lookup(consumer_data
.stream_ht
,
162 (void *)((unsigned long) stream
->key
), &iter
);
163 /* Remove stream node from hash table */
164 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
169 if (consumer_data
.stream_count
<= 0) {
172 consumer_data
.stream_count
--;
176 if (stream
->out_fd
>= 0) {
177 ret
= close(stream
->out_fd
);
182 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
183 ret
= close(stream
->wait_fd
);
188 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
189 ret
= close(stream
->shm_fd
);
194 if (!--stream
->chan
->refcount
)
195 free_chan
= stream
->chan
;
198 consumer_data
.need_update
= 1;
199 pthread_mutex_unlock(&consumer_data
.lock
);
202 consumer_del_channel(free_chan
);
205 static void consumer_del_stream_rcu(struct rcu_head
*head
)
207 struct lttng_ht_node_ulong
*node
=
208 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
209 struct lttng_consumer_stream
*stream
=
210 caa_container_of(node
, struct lttng_consumer_stream
, node
);
212 consumer_del_stream(stream
);
215 struct lttng_consumer_stream
*consumer_allocate_stream(
216 int channel_key
, int stream_key
,
217 int shm_fd
, int wait_fd
,
218 enum lttng_consumer_stream_state state
,
220 enum lttng_event_output output
,
221 const char *path_name
,
225 struct lttng_consumer_stream
*stream
;
228 stream
= zmalloc(sizeof(*stream
));
229 if (stream
== NULL
) {
230 perror("malloc struct lttng_consumer_stream");
233 stream
->chan
= consumer_find_channel(channel_key
);
235 perror("Unable to find channel key");
238 stream
->chan
->refcount
++;
239 stream
->key
= stream_key
;
240 stream
->shm_fd
= shm_fd
;
241 stream
->wait_fd
= wait_fd
;
243 stream
->out_fd_offset
= 0;
244 stream
->state
= state
;
245 stream
->mmap_len
= mmap_len
;
246 stream
->mmap_base
= NULL
;
247 stream
->output
= output
;
250 strncpy(stream
->path_name
, path_name
, PATH_MAX
- 1);
251 stream
->path_name
[PATH_MAX
- 1] = '\0';
252 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
254 switch (consumer_data
.type
) {
255 case LTTNG_CONSUMER_KERNEL
:
257 case LTTNG_CONSUMER32_UST
:
258 case LTTNG_CONSUMER64_UST
:
259 stream
->cpu
= stream
->chan
->cpucount
++;
260 ret
= lttng_ustconsumer_allocate_stream(stream
);
267 ERR("Unknown consumer_data type");
271 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
272 stream
->path_name
, stream
->key
,
275 (unsigned long long) stream
->mmap_len
,
282 * Add a stream to the global list protected by a mutex.
284 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
288 pthread_mutex_lock(&consumer_data
.lock
);
289 /* Steal stream identifier, for UST */
290 consumer_steal_stream_key(stream
->key
);
292 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
294 consumer_data
.stream_count
++;
295 consumer_data
.need_update
= 1;
297 switch (consumer_data
.type
) {
298 case LTTNG_CONSUMER_KERNEL
:
300 case LTTNG_CONSUMER32_UST
:
301 case LTTNG_CONSUMER64_UST
:
302 /* Streams are in CPU number order (we rely on this) */
303 stream
->cpu
= stream
->chan
->nr_streams
++;
306 ERR("Unknown consumer_data type");
312 pthread_mutex_unlock(&consumer_data
.lock
);
317 * Update a stream according to what we just received.
319 void consumer_change_stream_state(int stream_key
,
320 enum lttng_consumer_stream_state state
)
322 struct lttng_consumer_stream
*stream
;
324 pthread_mutex_lock(&consumer_data
.lock
);
325 stream
= consumer_find_stream(stream_key
);
327 stream
->state
= state
;
329 consumer_data
.need_update
= 1;
330 pthread_mutex_unlock(&consumer_data
.lock
);
334 * Remove a channel from the global list protected by a mutex. This
335 * function is also responsible for freeing its data structures.
337 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
340 struct lttng_ht_iter iter
;
342 pthread_mutex_lock(&consumer_data
.lock
);
344 switch (consumer_data
.type
) {
345 case LTTNG_CONSUMER_KERNEL
:
347 case LTTNG_CONSUMER32_UST
:
348 case LTTNG_CONSUMER64_UST
:
349 lttng_ustconsumer_del_channel(channel
);
352 ERR("Unknown consumer_data type");
359 lttng_ht_lookup(consumer_data
.channel_ht
,
360 (void *)((unsigned long) channel
->key
), &iter
);
361 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
366 if (channel
->mmap_base
!= NULL
) {
367 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
372 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
373 ret
= close(channel
->wait_fd
);
378 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
379 ret
= close(channel
->shm_fd
);
386 pthread_mutex_unlock(&consumer_data
.lock
);
389 static void consumer_del_channel_rcu(struct rcu_head
*head
)
391 struct lttng_ht_node_ulong
*node
=
392 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
393 struct lttng_consumer_channel
*channel
=
394 caa_container_of(node
, struct lttng_consumer_channel
, node
);
396 consumer_del_channel(channel
);
399 struct lttng_consumer_channel
*consumer_allocate_channel(
401 int shm_fd
, int wait_fd
,
403 uint64_t max_sb_size
)
405 struct lttng_consumer_channel
*channel
;
408 channel
= zmalloc(sizeof(*channel
));
409 if (channel
== NULL
) {
410 perror("malloc struct lttng_consumer_channel");
413 channel
->key
= channel_key
;
414 channel
->shm_fd
= shm_fd
;
415 channel
->wait_fd
= wait_fd
;
416 channel
->mmap_len
= mmap_len
;
417 channel
->max_sb_size
= max_sb_size
;
418 channel
->refcount
= 0;
419 channel
->nr_streams
= 0;
420 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
422 switch (consumer_data
.type
) {
423 case LTTNG_CONSUMER_KERNEL
:
424 channel
->mmap_base
= NULL
;
425 channel
->mmap_len
= 0;
427 case LTTNG_CONSUMER32_UST
:
428 case LTTNG_CONSUMER64_UST
:
429 ret
= lttng_ustconsumer_allocate_channel(channel
);
436 ERR("Unknown consumer_data type");
440 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
444 (unsigned long long) channel
->mmap_len
,
445 (unsigned long long) channel
->max_sb_size
);
451 * Add a channel to the global list protected by a mutex.
453 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
455 pthread_mutex_lock(&consumer_data
.lock
);
456 /* Steal channel identifier, for UST */
457 consumer_steal_channel_key(channel
->key
);
459 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
461 pthread_mutex_unlock(&consumer_data
.lock
);
466 * Allocate the pollfd structure and the local view of the out fds to avoid
467 * doing a lookup in the linked list and concurrency issues when writing is
468 * needed. Called with consumer_data.lock held.
470 * Returns the number of fds in the structures.
472 int consumer_update_poll_array(
473 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
474 struct lttng_consumer_stream
**local_stream
)
477 struct lttng_ht_iter iter
;
478 struct lttng_consumer_stream
*stream
;
480 DBG("Updating poll fd array");
481 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
483 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
486 DBG("Active FD %d", stream
->wait_fd
);
487 (*pollfd
)[i
].fd
= stream
->wait_fd
;
488 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
489 local_stream
[i
] = stream
;
494 * Insert the consumer_poll_pipe at the end of the array and don't
495 * increment i so nb_fd is the number of real FD.
497 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
498 (*pollfd
)[i
].events
= POLLIN
;
503 * Poll on the should_quit pipe and the command socket return -1 on error and
504 * should exit, 0 if data is available on the command socket
506 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
511 num_rdy
= poll(consumer_sockpoll
, 2, -1);
514 * Restart interrupted system call.
516 if (errno
== EINTR
) {
519 perror("Poll error");
522 if (consumer_sockpoll
[0].revents
== POLLIN
) {
523 DBG("consumer_should_quit wake up");
533 * Set the error socket.
535 void lttng_consumer_set_error_sock(
536 struct lttng_consumer_local_data
*ctx
, int sock
)
538 ctx
->consumer_error_socket
= sock
;
542 * Set the command socket path.
545 void lttng_consumer_set_command_sock_path(
546 struct lttng_consumer_local_data
*ctx
, char *sock
)
548 ctx
->consumer_command_sock_path
= sock
;
552 * Send return code to the session daemon.
553 * If the socket is not defined, we return 0, it is not a fatal error
555 int lttng_consumer_send_error(
556 struct lttng_consumer_local_data
*ctx
, int cmd
)
558 if (ctx
->consumer_error_socket
> 0) {
559 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
560 sizeof(enum lttcomm_sessiond_command
));
567 * Close all the tracefiles and stream fds, should be called when all instances
570 void lttng_consumer_cleanup(void)
573 struct lttng_ht_iter iter
;
574 struct lttng_ht_node_ulong
*node
;
579 * close all outfd. Called when there are no more threads running (after
580 * joining on the threads), no need to protect list iteration with mutex.
582 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
584 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
586 call_rcu(&node
->head
, consumer_del_stream_rcu
);
589 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
591 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
593 call_rcu(&node
->head
, consumer_del_channel_rcu
);
600 * Called from signal handler.
602 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
606 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
608 perror("write consumer quit");
612 void lttng_consumer_sync_trace_file(
613 struct lttng_consumer_stream
*stream
, off_t orig_offset
)
615 int outfd
= stream
->out_fd
;
618 * This does a blocking write-and-wait on any page that belongs to the
619 * subbuffer prior to the one we just wrote.
620 * Don't care about error values, as these are just hints and ways to
621 * limit the amount of page cache used.
623 if (orig_offset
< stream
->chan
->max_sb_size
) {
626 sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
627 stream
->chan
->max_sb_size
,
628 SYNC_FILE_RANGE_WAIT_BEFORE
629 | SYNC_FILE_RANGE_WRITE
630 | SYNC_FILE_RANGE_WAIT_AFTER
);
632 * Give hints to the kernel about how we access the file:
633 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
636 * We need to call fadvise again after the file grows because the
637 * kernel does not seem to apply fadvise to non-existing parts of the
640 * Call fadvise _after_ having waited for the page writeback to
641 * complete because the dirty page writeback semantic is not well
642 * defined. So it can be expected to lead to lower throughput in
645 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
646 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
650 * Initialise the necessary environnement :
651 * - create a new context
652 * - create the poll_pipe
653 * - create the should_quit pipe (for signal handler)
654 * - create the thread pipe (for splice)
656 * Takes a function pointer as argument, this function is called when data is
657 * available on a buffer. This function is responsible to do the
658 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
659 * buffer configuration and then kernctl_put_next_subbuf at the end.
661 * Returns a pointer to the new context or NULL on error.
663 struct lttng_consumer_local_data
*lttng_consumer_create(
664 enum lttng_consumer_type type
,
665 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
666 struct lttng_consumer_local_data
*ctx
),
667 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
668 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
669 int (*update_stream
)(int stream_key
, uint32_t state
))
672 struct lttng_consumer_local_data
*ctx
;
674 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
675 consumer_data
.type
== type
);
676 consumer_data
.type
= type
;
678 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
680 perror("allocating context");
684 ctx
->consumer_error_socket
= -1;
685 /* assign the callbacks */
686 ctx
->on_buffer_ready
= buffer_ready
;
687 ctx
->on_recv_channel
= recv_channel
;
688 ctx
->on_recv_stream
= recv_stream
;
689 ctx
->on_update_stream
= update_stream
;
691 ret
= pipe(ctx
->consumer_poll_pipe
);
693 perror("Error creating poll pipe");
694 goto error_poll_pipe
;
697 ret
= pipe(ctx
->consumer_should_quit
);
699 perror("Error creating recv pipe");
700 goto error_quit_pipe
;
703 ret
= pipe(ctx
->consumer_thread_pipe
);
705 perror("Error creating thread pipe");
706 goto error_thread_pipe
;
713 for (i
= 0; i
< 2; i
++) {
716 err
= close(ctx
->consumer_should_quit
[i
]);
722 for (i
= 0; i
< 2; i
++) {
725 err
= close(ctx
->consumer_poll_pipe
[i
]);
737 * Close all fds associated with the instance and free the context.
739 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
743 ret
= close(ctx
->consumer_error_socket
);
747 ret
= close(ctx
->consumer_thread_pipe
[0]);
751 ret
= close(ctx
->consumer_thread_pipe
[1]);
755 ret
= close(ctx
->consumer_poll_pipe
[0]);
759 ret
= close(ctx
->consumer_poll_pipe
[1]);
763 ret
= close(ctx
->consumer_should_quit
[0]);
767 ret
= close(ctx
->consumer_should_quit
[1]);
771 unlink(ctx
->consumer_command_sock_path
);
776 * Mmap the ring buffer, read it and write the data to the tracefile.
778 * Returns the number of bytes written
780 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
781 struct lttng_consumer_local_data
*ctx
,
782 struct lttng_consumer_stream
*stream
, unsigned long len
)
784 switch (consumer_data
.type
) {
785 case LTTNG_CONSUMER_KERNEL
:
786 return lttng_kconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
787 case LTTNG_CONSUMER32_UST
:
788 case LTTNG_CONSUMER64_UST
:
789 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
791 ERR("Unknown consumer_data type");
797 * Splice the data from the ring buffer to the tracefile.
799 * Returns the number of bytes spliced.
801 ssize_t
lttng_consumer_on_read_subbuffer_splice(
802 struct lttng_consumer_local_data
*ctx
,
803 struct lttng_consumer_stream
*stream
, unsigned long len
)
805 switch (consumer_data
.type
) {
806 case LTTNG_CONSUMER_KERNEL
:
807 return lttng_kconsumer_on_read_subbuffer_splice(ctx
, stream
, len
);
808 case LTTNG_CONSUMER32_UST
:
809 case LTTNG_CONSUMER64_UST
:
812 ERR("Unknown consumer_data type");
820 * Take a snapshot for a specific fd
822 * Returns 0 on success, < 0 on error
824 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
825 struct lttng_consumer_stream
*stream
)
827 switch (consumer_data
.type
) {
828 case LTTNG_CONSUMER_KERNEL
:
829 return lttng_kconsumer_take_snapshot(ctx
, stream
);
830 case LTTNG_CONSUMER32_UST
:
831 case LTTNG_CONSUMER64_UST
:
832 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
834 ERR("Unknown consumer_data type");
842 * Get the produced position
844 * Returns 0 on success, < 0 on error
846 int lttng_consumer_get_produced_snapshot(
847 struct lttng_consumer_local_data
*ctx
,
848 struct lttng_consumer_stream
*stream
,
851 switch (consumer_data
.type
) {
852 case LTTNG_CONSUMER_KERNEL
:
853 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
854 case LTTNG_CONSUMER32_UST
:
855 case LTTNG_CONSUMER64_UST
:
856 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
858 ERR("Unknown consumer_data type");
864 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
865 int sock
, struct pollfd
*consumer_sockpoll
)
867 switch (consumer_data
.type
) {
868 case LTTNG_CONSUMER_KERNEL
:
869 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
870 case LTTNG_CONSUMER32_UST
:
871 case LTTNG_CONSUMER64_UST
:
872 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
874 ERR("Unknown consumer_data type");
881 * This thread polls the fds in the set to consume the data and write
882 * it to tracefile if necessary.
884 void *lttng_consumer_thread_poll_fds(void *data
)
886 int num_rdy
, num_hup
, high_prio
, ret
, i
;
887 struct pollfd
*pollfd
= NULL
;
888 /* local view of the streams */
889 struct lttng_consumer_stream
**local_stream
= NULL
;
890 /* local view of consumer_data.fds_count */
894 struct lttng_consumer_local_data
*ctx
= data
;
896 rcu_register_thread();
898 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
905 * the fds set has been updated, we need to update our
906 * local array as well
908 pthread_mutex_lock(&consumer_data
.lock
);
909 if (consumer_data
.need_update
) {
910 if (pollfd
!= NULL
) {
914 if (local_stream
!= NULL
) {
919 /* allocate for all fds + 1 for the consumer_poll_pipe */
920 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
921 if (pollfd
== NULL
) {
922 perror("pollfd malloc");
923 pthread_mutex_unlock(&consumer_data
.lock
);
927 /* allocate for all fds + 1 for the consumer_poll_pipe */
928 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
929 sizeof(struct lttng_consumer_stream
));
930 if (local_stream
== NULL
) {
931 perror("local_stream malloc");
932 pthread_mutex_unlock(&consumer_data
.lock
);
935 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
937 ERR("Error in allocating pollfd or local_outfds");
938 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
939 pthread_mutex_unlock(&consumer_data
.lock
);
943 consumer_data
.need_update
= 0;
945 pthread_mutex_unlock(&consumer_data
.lock
);
947 /* No FDs and consumer_quit, consumer_cleanup the thread */
948 if (nb_fd
== 0 && consumer_quit
== 1) {
951 /* poll on the array of fds */
953 DBG("polling on %d fd", nb_fd
+ 1);
954 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
955 DBG("poll num_rdy : %d", num_rdy
);
958 * Restart interrupted system call.
960 if (errno
== EINTR
) {
963 perror("Poll error");
964 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
966 } else if (num_rdy
== 0) {
967 DBG("Polling thread timed out");
972 * If the consumer_poll_pipe triggered poll go
973 * directly to the beginning of the loop to update the
974 * array. We want to prioritize array update over
975 * low-priority reads.
977 if (pollfd
[nb_fd
].revents
& POLLIN
) {
978 DBG("consumer_poll_pipe wake up");
979 tmp2
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
981 perror("read consumer poll");
986 /* Take care of high priority channels first. */
987 for (i
= 0; i
< nb_fd
; i
++) {
988 if (pollfd
[i
].revents
& POLLPRI
) {
991 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
993 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
994 /* it's ok to have an unavailable sub-buffer */
995 if (len
< 0 && len
!= -EAGAIN
) {
997 } else if (len
> 0) {
998 local_stream
[i
]->data_read
= 1;
1004 * If we read high prio channel in this loop, try again
1005 * for more high prio data.
1011 /* Take care of low priority channels. */
1012 for (i
= 0; i
< nb_fd
; i
++) {
1013 if ((pollfd
[i
].revents
& POLLIN
) ||
1014 local_stream
[i
]->hangup_flush_done
) {
1017 assert(!(pollfd
[i
].revents
& POLLERR
));
1018 assert(!(pollfd
[i
].revents
& POLLNVAL
));
1019 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1020 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1021 /* it's ok to have an unavailable sub-buffer */
1022 if (len
< 0 && len
!= -EAGAIN
) {
1024 } else if (len
> 0) {
1025 local_stream
[i
]->data_read
= 1;
1030 /* Handle hangup and errors */
1031 for (i
= 0; i
< nb_fd
; i
++) {
1032 if (!local_stream
[i
]->hangup_flush_done
1033 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1034 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1035 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1036 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1038 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
1039 /* Attempt read again, for the data we just flushed. */
1040 local_stream
[i
]->data_read
= 1;
1043 * If the poll flag is HUP/ERR/NVAL and we have
1044 * read no data in this pass, we can remove the
1045 * stream from its hash table.
1047 if ((pollfd
[i
].revents
& POLLHUP
)) {
1048 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
1049 if (!local_stream
[i
]->data_read
) {
1051 consumer_del_stream_rcu(&local_stream
[i
]->node
.head
);
1055 } else if (pollfd
[i
].revents
& POLLERR
) {
1056 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
1057 if (!local_stream
[i
]->data_read
) {
1059 consumer_del_stream_rcu(&local_stream
[i
]->node
.head
);
1063 } else if (pollfd
[i
].revents
& POLLNVAL
) {
1064 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
1065 if (!local_stream
[i
]->data_read
) {
1067 consumer_del_stream_rcu(&local_stream
[i
]->node
.head
);
1072 local_stream
[i
]->data_read
= 0;
1076 DBG("polling thread exiting");
1077 if (pollfd
!= NULL
) {
1081 if (local_stream
!= NULL
) {
1083 local_stream
= NULL
;
1085 rcu_unregister_thread();
1090 * This thread listens on the consumerd socket and receives the file
1091 * descriptors from the session daemon.
1093 void *lttng_consumer_thread_receive_fds(void *data
)
1095 int sock
, client_socket
, ret
;
1097 * structure to poll for incoming data on communication socket avoids
1098 * making blocking sockets.
1100 struct pollfd consumer_sockpoll
[2];
1101 struct lttng_consumer_local_data
*ctx
= data
;
1103 rcu_register_thread();
1105 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
1106 unlink(ctx
->consumer_command_sock_path
);
1107 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
1108 if (client_socket
< 0) {
1109 ERR("Cannot create command socket");
1113 ret
= lttcomm_listen_unix_sock(client_socket
);
1118 DBG("Sending ready command to lttng-sessiond");
1119 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
1120 /* return < 0 on error, but == 0 is not fatal */
1122 ERR("Error sending ready command to lttng-sessiond");
1126 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1128 perror("fcntl O_NONBLOCK");
1132 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1133 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1134 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1135 consumer_sockpoll
[1].fd
= client_socket
;
1136 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1138 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1141 DBG("Connection on client_socket");
1143 /* Blocking call, waiting for transmission */
1144 sock
= lttcomm_accept_unix_sock(client_socket
);
1149 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1151 perror("fcntl O_NONBLOCK");
1155 /* update the polling structure to poll on the established socket */
1156 consumer_sockpoll
[1].fd
= sock
;
1157 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1160 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1163 DBG("Incoming command on sock");
1164 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1165 if (ret
== -ENOENT
) {
1166 DBG("Received STOP command");
1170 ERR("Communication interrupted on command socket");
1173 if (consumer_quit
) {
1174 DBG("consumer_thread_receive_fds received quit from signal");
1177 DBG("received fds on sock");
1180 DBG("consumer_thread_receive_fds exiting");
1183 * when all fds have hung up, the polling thread
1189 * 2s of grace period, if no polling events occur during
1190 * this period, the polling thread will exit even if there
1191 * are still open FDs (should not happen, but safety mechanism).
1193 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1195 /* wake up the polling thread */
1196 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
1198 perror("poll pipe write");
1200 rcu_unregister_thread();
1204 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1205 struct lttng_consumer_local_data
*ctx
)
1207 switch (consumer_data
.type
) {
1208 case LTTNG_CONSUMER_KERNEL
:
1209 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1210 case LTTNG_CONSUMER32_UST
:
1211 case LTTNG_CONSUMER64_UST
:
1212 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1214 ERR("Unknown consumer_data type");
1220 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1222 switch (consumer_data
.type
) {
1223 case LTTNG_CONSUMER_KERNEL
:
1224 return lttng_kconsumer_on_recv_stream(stream
);
1225 case LTTNG_CONSUMER32_UST
:
1226 case LTTNG_CONSUMER64_UST
:
1227 return lttng_ustconsumer_on_recv_stream(stream
);
1229 ERR("Unknown consumer_data type");
1236 * Allocate and set consumer data hash tables.
1238 void lttng_consumer_init(void)
1240 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1241 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);