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
;
90 stream
= consumer_find_stream(key
);
94 * We don't want the lookup to match, but we still need
95 * to iterate on this stream when iterating over the hash table. Just
96 * change the node key.
98 stream
->node
.key
= -1;
103 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
105 struct lttng_ht_iter iter
;
106 struct lttng_ht_node_ulong
*node
;
107 struct lttng_consumer_channel
*channel
= NULL
;
109 /* Negative keys are lookup failures */
115 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
117 node
= lttng_ht_iter_get_node_ulong(&iter
);
119 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
127 static void consumer_steal_channel_key(int key
)
129 struct lttng_consumer_channel
*channel
;
132 channel
= consumer_find_channel(key
);
136 * We don't want the lookup to match, but we still need
137 * to iterate on this channel when iterating over the hash table. Just
138 * change the node key.
140 channel
->node
.key
= -1;
146 void consumer_free_stream(struct rcu_head
*head
)
148 struct lttng_ht_node_ulong
*node
=
149 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
150 struct lttng_consumer_stream
*stream
=
151 caa_container_of(node
, struct lttng_consumer_stream
, node
);
157 * Remove a stream from the global list protected by a mutex. This
158 * function is also responsible for freeing its data structures.
160 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
163 struct lttng_ht_iter iter
;
164 struct lttng_consumer_channel
*free_chan
= NULL
;
166 pthread_mutex_lock(&consumer_data
.lock
);
168 switch (consumer_data
.type
) {
169 case LTTNG_CONSUMER_KERNEL
:
170 if (stream
->mmap_base
!= NULL
) {
171 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
177 case LTTNG_CONSUMER32_UST
:
178 case LTTNG_CONSUMER64_UST
:
179 lttng_ustconsumer_del_stream(stream
);
182 ERR("Unknown consumer_data type");
188 iter
.iter
.node
= &stream
->node
.node
;
189 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
194 if (consumer_data
.stream_count
<= 0) {
197 consumer_data
.stream_count
--;
201 if (stream
->out_fd
>= 0) {
202 ret
= close(stream
->out_fd
);
207 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
208 ret
= close(stream
->wait_fd
);
213 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
214 ret
= close(stream
->shm_fd
);
219 if (!--stream
->chan
->refcount
)
220 free_chan
= stream
->chan
;
222 call_rcu(&stream
->node
.head
, consumer_free_stream
);
224 consumer_data
.need_update
= 1;
225 pthread_mutex_unlock(&consumer_data
.lock
);
228 consumer_del_channel(free_chan
);
231 struct lttng_consumer_stream
*consumer_allocate_stream(
232 int channel_key
, int stream_key
,
233 int shm_fd
, int wait_fd
,
234 enum lttng_consumer_stream_state state
,
236 enum lttng_event_output output
,
237 const char *path_name
,
241 struct lttng_consumer_stream
*stream
;
244 stream
= zmalloc(sizeof(*stream
));
245 if (stream
== NULL
) {
246 perror("malloc struct lttng_consumer_stream");
249 stream
->chan
= consumer_find_channel(channel_key
);
251 perror("Unable to find channel key");
254 stream
->chan
->refcount
++;
255 stream
->key
= stream_key
;
256 stream
->shm_fd
= shm_fd
;
257 stream
->wait_fd
= wait_fd
;
259 stream
->out_fd_offset
= 0;
260 stream
->state
= state
;
261 stream
->mmap_len
= mmap_len
;
262 stream
->mmap_base
= NULL
;
263 stream
->output
= output
;
266 strncpy(stream
->path_name
, path_name
, PATH_MAX
- 1);
267 stream
->path_name
[PATH_MAX
- 1] = '\0';
268 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
270 switch (consumer_data
.type
) {
271 case LTTNG_CONSUMER_KERNEL
:
273 case LTTNG_CONSUMER32_UST
:
274 case LTTNG_CONSUMER64_UST
:
275 stream
->cpu
= stream
->chan
->cpucount
++;
276 ret
= lttng_ustconsumer_allocate_stream(stream
);
283 ERR("Unknown consumer_data type");
287 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
288 stream
->path_name
, stream
->key
,
291 (unsigned long long) stream
->mmap_len
,
298 * Add a stream to the global list protected by a mutex.
300 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
304 pthread_mutex_lock(&consumer_data
.lock
);
305 /* Steal stream identifier, for UST */
306 consumer_steal_stream_key(stream
->key
);
308 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
310 consumer_data
.stream_count
++;
311 consumer_data
.need_update
= 1;
313 switch (consumer_data
.type
) {
314 case LTTNG_CONSUMER_KERNEL
:
316 case LTTNG_CONSUMER32_UST
:
317 case LTTNG_CONSUMER64_UST
:
318 /* Streams are in CPU number order (we rely on this) */
319 stream
->cpu
= stream
->chan
->nr_streams
++;
322 ERR("Unknown consumer_data type");
328 pthread_mutex_unlock(&consumer_data
.lock
);
334 * Update a stream according to what we just received.
336 void consumer_change_stream_state(int stream_key
,
337 enum lttng_consumer_stream_state state
)
339 struct lttng_consumer_stream
*stream
;
341 pthread_mutex_lock(&consumer_data
.lock
);
342 stream
= consumer_find_stream(stream_key
);
344 stream
->state
= state
;
346 consumer_data
.need_update
= 1;
347 pthread_mutex_unlock(&consumer_data
.lock
);
351 void consumer_free_channel(struct rcu_head
*head
)
353 struct lttng_ht_node_ulong
*node
=
354 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
355 struct lttng_consumer_channel
*channel
=
356 caa_container_of(node
, struct lttng_consumer_channel
, node
);
362 * Remove a channel from the global list protected by a mutex. This
363 * function is also responsible for freeing its data structures.
365 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
368 struct lttng_ht_iter iter
;
370 pthread_mutex_lock(&consumer_data
.lock
);
372 switch (consumer_data
.type
) {
373 case LTTNG_CONSUMER_KERNEL
:
375 case LTTNG_CONSUMER32_UST
:
376 case LTTNG_CONSUMER64_UST
:
377 lttng_ustconsumer_del_channel(channel
);
380 ERR("Unknown consumer_data type");
386 iter
.iter
.node
= &channel
->node
.node
;
387 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
391 if (channel
->mmap_base
!= NULL
) {
392 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
397 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
398 ret
= close(channel
->wait_fd
);
403 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
404 ret
= close(channel
->shm_fd
);
410 call_rcu(&channel
->node
.head
, consumer_free_channel
);
412 pthread_mutex_unlock(&consumer_data
.lock
);
415 struct lttng_consumer_channel
*consumer_allocate_channel(
417 int shm_fd
, int wait_fd
,
419 uint64_t max_sb_size
)
421 struct lttng_consumer_channel
*channel
;
424 channel
= zmalloc(sizeof(*channel
));
425 if (channel
== NULL
) {
426 perror("malloc struct lttng_consumer_channel");
429 channel
->key
= channel_key
;
430 channel
->shm_fd
= shm_fd
;
431 channel
->wait_fd
= wait_fd
;
432 channel
->mmap_len
= mmap_len
;
433 channel
->max_sb_size
= max_sb_size
;
434 channel
->refcount
= 0;
435 channel
->nr_streams
= 0;
436 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
438 switch (consumer_data
.type
) {
439 case LTTNG_CONSUMER_KERNEL
:
440 channel
->mmap_base
= NULL
;
441 channel
->mmap_len
= 0;
443 case LTTNG_CONSUMER32_UST
:
444 case LTTNG_CONSUMER64_UST
:
445 ret
= lttng_ustconsumer_allocate_channel(channel
);
452 ERR("Unknown consumer_data type");
456 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
460 (unsigned long long) channel
->mmap_len
,
461 (unsigned long long) channel
->max_sb_size
);
467 * Add a channel to the global list protected by a mutex.
469 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
471 pthread_mutex_lock(&consumer_data
.lock
);
472 /* Steal channel identifier, for UST */
473 consumer_steal_channel_key(channel
->key
);
475 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
477 pthread_mutex_unlock(&consumer_data
.lock
);
483 * Allocate the pollfd structure and the local view of the out fds to avoid
484 * doing a lookup in the linked list and concurrency issues when writing is
485 * needed. Called with consumer_data.lock held.
487 * Returns the number of fds in the structures.
489 int consumer_update_poll_array(
490 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
491 struct lttng_consumer_stream
**local_stream
)
494 struct lttng_ht_iter iter
;
495 struct lttng_consumer_stream
*stream
;
497 DBG("Updating poll fd array");
499 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
501 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
504 DBG("Active FD %d", stream
->wait_fd
);
505 (*pollfd
)[i
].fd
= stream
->wait_fd
;
506 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
507 local_stream
[i
] = stream
;
513 * Insert the consumer_poll_pipe at the end of the array and don't
514 * increment i so nb_fd is the number of real FD.
516 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
517 (*pollfd
)[i
].events
= POLLIN
;
522 * Poll on the should_quit pipe and the command socket return -1 on error and
523 * should exit, 0 if data is available on the command socket
525 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
530 num_rdy
= poll(consumer_sockpoll
, 2, -1);
533 * Restart interrupted system call.
535 if (errno
== EINTR
) {
538 perror("Poll error");
541 if (consumer_sockpoll
[0].revents
== POLLIN
) {
542 DBG("consumer_should_quit wake up");
552 * Set the error socket.
554 void lttng_consumer_set_error_sock(
555 struct lttng_consumer_local_data
*ctx
, int sock
)
557 ctx
->consumer_error_socket
= sock
;
561 * Set the command socket path.
564 void lttng_consumer_set_command_sock_path(
565 struct lttng_consumer_local_data
*ctx
, char *sock
)
567 ctx
->consumer_command_sock_path
= sock
;
571 * Send return code to the session daemon.
572 * If the socket is not defined, we return 0, it is not a fatal error
574 int lttng_consumer_send_error(
575 struct lttng_consumer_local_data
*ctx
, int cmd
)
577 if (ctx
->consumer_error_socket
> 0) {
578 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
579 sizeof(enum lttcomm_sessiond_command
));
586 * Close all the tracefiles and stream fds, should be called when all instances
589 void lttng_consumer_cleanup(void)
591 struct lttng_ht_iter iter
;
592 struct lttng_ht_node_ulong
*node
;
597 * close all outfd. Called when there are no more threads running (after
598 * joining on the threads), no need to protect list iteration with mutex.
600 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
602 struct lttng_consumer_stream
*stream
=
603 caa_container_of(node
, struct lttng_consumer_stream
, node
);
604 consumer_del_stream(stream
);
607 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
609 struct lttng_consumer_channel
*channel
=
610 caa_container_of(node
, struct lttng_consumer_channel
, node
);
611 consumer_del_channel(channel
);
618 * Called from signal handler.
620 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
624 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
626 perror("write consumer quit");
630 void lttng_consumer_sync_trace_file(
631 struct lttng_consumer_stream
*stream
, off_t orig_offset
)
633 int outfd
= stream
->out_fd
;
636 * This does a blocking write-and-wait on any page that belongs to the
637 * subbuffer prior to the one we just wrote.
638 * Don't care about error values, as these are just hints and ways to
639 * limit the amount of page cache used.
641 if (orig_offset
< stream
->chan
->max_sb_size
) {
644 sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
645 stream
->chan
->max_sb_size
,
646 SYNC_FILE_RANGE_WAIT_BEFORE
647 | SYNC_FILE_RANGE_WRITE
648 | SYNC_FILE_RANGE_WAIT_AFTER
);
650 * Give hints to the kernel about how we access the file:
651 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
654 * We need to call fadvise again after the file grows because the
655 * kernel does not seem to apply fadvise to non-existing parts of the
658 * Call fadvise _after_ having waited for the page writeback to
659 * complete because the dirty page writeback semantic is not well
660 * defined. So it can be expected to lead to lower throughput in
663 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
664 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
668 * Initialise the necessary environnement :
669 * - create a new context
670 * - create the poll_pipe
671 * - create the should_quit pipe (for signal handler)
672 * - create the thread pipe (for splice)
674 * Takes a function pointer as argument, this function is called when data is
675 * available on a buffer. This function is responsible to do the
676 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
677 * buffer configuration and then kernctl_put_next_subbuf at the end.
679 * Returns a pointer to the new context or NULL on error.
681 struct lttng_consumer_local_data
*lttng_consumer_create(
682 enum lttng_consumer_type type
,
683 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
684 struct lttng_consumer_local_data
*ctx
),
685 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
686 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
687 int (*update_stream
)(int stream_key
, uint32_t state
))
690 struct lttng_consumer_local_data
*ctx
;
692 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
693 consumer_data
.type
== type
);
694 consumer_data
.type
= type
;
696 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
698 perror("allocating context");
702 ctx
->consumer_error_socket
= -1;
703 /* assign the callbacks */
704 ctx
->on_buffer_ready
= buffer_ready
;
705 ctx
->on_recv_channel
= recv_channel
;
706 ctx
->on_recv_stream
= recv_stream
;
707 ctx
->on_update_stream
= update_stream
;
709 ret
= pipe(ctx
->consumer_poll_pipe
);
711 perror("Error creating poll pipe");
712 goto error_poll_pipe
;
715 ret
= pipe(ctx
->consumer_should_quit
);
717 perror("Error creating recv pipe");
718 goto error_quit_pipe
;
721 ret
= pipe(ctx
->consumer_thread_pipe
);
723 perror("Error creating thread pipe");
724 goto error_thread_pipe
;
731 for (i
= 0; i
< 2; i
++) {
734 err
= close(ctx
->consumer_should_quit
[i
]);
740 for (i
= 0; i
< 2; i
++) {
743 err
= close(ctx
->consumer_poll_pipe
[i
]);
755 * Close all fds associated with the instance and free the context.
757 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
761 ret
= close(ctx
->consumer_error_socket
);
765 ret
= close(ctx
->consumer_thread_pipe
[0]);
769 ret
= close(ctx
->consumer_thread_pipe
[1]);
773 ret
= close(ctx
->consumer_poll_pipe
[0]);
777 ret
= close(ctx
->consumer_poll_pipe
[1]);
781 ret
= close(ctx
->consumer_should_quit
[0]);
785 ret
= close(ctx
->consumer_should_quit
[1]);
789 unlink(ctx
->consumer_command_sock_path
);
794 * Mmap the ring buffer, read it and write the data to the tracefile.
796 * Returns the number of bytes written
798 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
799 struct lttng_consumer_local_data
*ctx
,
800 struct lttng_consumer_stream
*stream
, unsigned long len
)
802 switch (consumer_data
.type
) {
803 case LTTNG_CONSUMER_KERNEL
:
804 return lttng_kconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
805 case LTTNG_CONSUMER32_UST
:
806 case LTTNG_CONSUMER64_UST
:
807 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
809 ERR("Unknown consumer_data type");
815 * Splice the data from the ring buffer to the tracefile.
817 * Returns the number of bytes spliced.
819 ssize_t
lttng_consumer_on_read_subbuffer_splice(
820 struct lttng_consumer_local_data
*ctx
,
821 struct lttng_consumer_stream
*stream
, unsigned long len
)
823 switch (consumer_data
.type
) {
824 case LTTNG_CONSUMER_KERNEL
:
825 return lttng_kconsumer_on_read_subbuffer_splice(ctx
, stream
, len
);
826 case LTTNG_CONSUMER32_UST
:
827 case LTTNG_CONSUMER64_UST
:
830 ERR("Unknown consumer_data type");
838 * Take a snapshot for a specific fd
840 * Returns 0 on success, < 0 on error
842 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
843 struct lttng_consumer_stream
*stream
)
845 switch (consumer_data
.type
) {
846 case LTTNG_CONSUMER_KERNEL
:
847 return lttng_kconsumer_take_snapshot(ctx
, stream
);
848 case LTTNG_CONSUMER32_UST
:
849 case LTTNG_CONSUMER64_UST
:
850 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
852 ERR("Unknown consumer_data type");
860 * Get the produced position
862 * Returns 0 on success, < 0 on error
864 int lttng_consumer_get_produced_snapshot(
865 struct lttng_consumer_local_data
*ctx
,
866 struct lttng_consumer_stream
*stream
,
869 switch (consumer_data
.type
) {
870 case LTTNG_CONSUMER_KERNEL
:
871 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
872 case LTTNG_CONSUMER32_UST
:
873 case LTTNG_CONSUMER64_UST
:
874 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
876 ERR("Unknown consumer_data type");
882 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
883 int sock
, struct pollfd
*consumer_sockpoll
)
885 switch (consumer_data
.type
) {
886 case LTTNG_CONSUMER_KERNEL
:
887 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
888 case LTTNG_CONSUMER32_UST
:
889 case LTTNG_CONSUMER64_UST
:
890 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
892 ERR("Unknown consumer_data type");
899 * This thread polls the fds in the set to consume the data and write
900 * it to tracefile if necessary.
902 void *lttng_consumer_thread_poll_fds(void *data
)
904 int num_rdy
, num_hup
, high_prio
, ret
, i
;
905 struct pollfd
*pollfd
= NULL
;
906 /* local view of the streams */
907 struct lttng_consumer_stream
**local_stream
= NULL
;
908 /* local view of consumer_data.fds_count */
912 struct lttng_consumer_local_data
*ctx
= data
;
914 rcu_register_thread();
916 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
923 * the fds set has been updated, we need to update our
924 * local array as well
926 pthread_mutex_lock(&consumer_data
.lock
);
927 if (consumer_data
.need_update
) {
928 if (pollfd
!= NULL
) {
932 if (local_stream
!= NULL
) {
937 /* allocate for all fds + 1 for the consumer_poll_pipe */
938 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
939 if (pollfd
== NULL
) {
940 perror("pollfd malloc");
941 pthread_mutex_unlock(&consumer_data
.lock
);
945 /* allocate for all fds + 1 for the consumer_poll_pipe */
946 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
947 sizeof(struct lttng_consumer_stream
));
948 if (local_stream
== NULL
) {
949 perror("local_stream malloc");
950 pthread_mutex_unlock(&consumer_data
.lock
);
953 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
955 ERR("Error in allocating pollfd or local_outfds");
956 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
957 pthread_mutex_unlock(&consumer_data
.lock
);
961 consumer_data
.need_update
= 0;
963 pthread_mutex_unlock(&consumer_data
.lock
);
965 /* No FDs and consumer_quit, consumer_cleanup the thread */
966 if (nb_fd
== 0 && consumer_quit
== 1) {
969 /* poll on the array of fds */
971 DBG("polling on %d fd", nb_fd
+ 1);
972 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
973 DBG("poll num_rdy : %d", num_rdy
);
976 * Restart interrupted system call.
978 if (errno
== EINTR
) {
981 perror("Poll error");
982 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
984 } else if (num_rdy
== 0) {
985 DBG("Polling thread timed out");
990 * If the consumer_poll_pipe triggered poll go
991 * directly to the beginning of the loop to update the
992 * array. We want to prioritize array update over
993 * low-priority reads.
995 if (pollfd
[nb_fd
].revents
& POLLIN
) {
996 DBG("consumer_poll_pipe wake up");
997 tmp2
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
999 perror("read consumer poll");
1004 /* Take care of high priority channels first. */
1005 for (i
= 0; i
< nb_fd
; i
++) {
1006 if (pollfd
[i
].revents
& POLLPRI
) {
1009 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
1011 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1012 /* it's ok to have an unavailable sub-buffer */
1013 if (len
< 0 && len
!= -EAGAIN
) {
1015 } else if (len
> 0) {
1016 local_stream
[i
]->data_read
= 1;
1022 * If we read high prio channel in this loop, try again
1023 * for more high prio data.
1029 /* Take care of low priority channels. */
1030 for (i
= 0; i
< nb_fd
; i
++) {
1031 if ((pollfd
[i
].revents
& POLLIN
) ||
1032 local_stream
[i
]->hangup_flush_done
) {
1035 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1036 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1037 /* it's ok to have an unavailable sub-buffer */
1038 if (len
< 0 && len
!= -EAGAIN
) {
1040 } else if (len
> 0) {
1041 local_stream
[i
]->data_read
= 1;
1046 /* Handle hangup and errors */
1047 for (i
= 0; i
< nb_fd
; i
++) {
1048 if (!local_stream
[i
]->hangup_flush_done
1049 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1050 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1051 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1052 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1054 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
1055 /* Attempt read again, for the data we just flushed. */
1056 local_stream
[i
]->data_read
= 1;
1059 * If the poll flag is HUP/ERR/NVAL and we have
1060 * read no data in this pass, we can remove the
1061 * stream from its hash table.
1063 if ((pollfd
[i
].revents
& POLLHUP
)) {
1064 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
1065 if (!local_stream
[i
]->data_read
) {
1066 consumer_del_stream(local_stream
[i
]);
1069 } else if (pollfd
[i
].revents
& POLLERR
) {
1070 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
1071 if (!local_stream
[i
]->data_read
) {
1072 consumer_del_stream(local_stream
[i
]);
1075 } else if (pollfd
[i
].revents
& POLLNVAL
) {
1076 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
1077 if (!local_stream
[i
]->data_read
) {
1078 consumer_del_stream(local_stream
[i
]);
1082 local_stream
[i
]->data_read
= 0;
1086 DBG("polling thread exiting");
1087 if (pollfd
!= NULL
) {
1091 if (local_stream
!= NULL
) {
1093 local_stream
= NULL
;
1095 rcu_unregister_thread();
1100 * This thread listens on the consumerd socket and receives the file
1101 * descriptors from the session daemon.
1103 void *lttng_consumer_thread_receive_fds(void *data
)
1105 int sock
, client_socket
, ret
;
1107 * structure to poll for incoming data on communication socket avoids
1108 * making blocking sockets.
1110 struct pollfd consumer_sockpoll
[2];
1111 struct lttng_consumer_local_data
*ctx
= data
;
1113 rcu_register_thread();
1115 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
1116 unlink(ctx
->consumer_command_sock_path
);
1117 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
1118 if (client_socket
< 0) {
1119 ERR("Cannot create command socket");
1123 ret
= lttcomm_listen_unix_sock(client_socket
);
1128 DBG("Sending ready command to lttng-sessiond");
1129 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
1130 /* return < 0 on error, but == 0 is not fatal */
1132 ERR("Error sending ready command to lttng-sessiond");
1136 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1138 perror("fcntl O_NONBLOCK");
1142 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1143 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1144 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1145 consumer_sockpoll
[1].fd
= client_socket
;
1146 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1148 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1151 DBG("Connection on client_socket");
1153 /* Blocking call, waiting for transmission */
1154 sock
= lttcomm_accept_unix_sock(client_socket
);
1159 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1161 perror("fcntl O_NONBLOCK");
1165 /* update the polling structure to poll on the established socket */
1166 consumer_sockpoll
[1].fd
= sock
;
1167 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1170 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1173 DBG("Incoming command on sock");
1174 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1175 if (ret
== -ENOENT
) {
1176 DBG("Received STOP command");
1180 ERR("Communication interrupted on command socket");
1183 if (consumer_quit
) {
1184 DBG("consumer_thread_receive_fds received quit from signal");
1187 DBG("received fds on sock");
1190 DBG("consumer_thread_receive_fds exiting");
1193 * when all fds have hung up, the polling thread
1199 * 2s of grace period, if no polling events occur during
1200 * this period, the polling thread will exit even if there
1201 * are still open FDs (should not happen, but safety mechanism).
1203 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1205 /* wake up the polling thread */
1206 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
1208 perror("poll pipe write");
1210 rcu_unregister_thread();
1214 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1215 struct lttng_consumer_local_data
*ctx
)
1217 switch (consumer_data
.type
) {
1218 case LTTNG_CONSUMER_KERNEL
:
1219 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1220 case LTTNG_CONSUMER32_UST
:
1221 case LTTNG_CONSUMER64_UST
:
1222 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1224 ERR("Unknown consumer_data type");
1230 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1232 switch (consumer_data
.type
) {
1233 case LTTNG_CONSUMER_KERNEL
:
1234 return lttng_kconsumer_on_recv_stream(stream
);
1235 case LTTNG_CONSUMER32_UST
:
1236 case LTTNG_CONSUMER64_UST
:
1237 return lttng_ustconsumer_on_recv_stream(stream
);
1239 ERR("Unknown consumer_data type");
1246 * Allocate and set consumer data hash tables.
1248 void lttng_consumer_init(void)
1250 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1251 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);