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.
27 #include <sys/socket.h>
28 #include <sys/types.h>
31 #include <common/common.h>
32 #include <common/kernel-ctl/kernel-ctl.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/kernel-consumer/kernel-consumer.h>
35 #include <common/ust-consumer/ust-consumer.h>
39 struct lttng_consumer_global_data consumer_data
= {
42 .type
= LTTNG_CONSUMER_UNKNOWN
,
45 /* timeout parameter, to control the polling thread grace period. */
46 int consumer_poll_timeout
= -1;
49 * Flag to inform the polling thread to quit when all fd hung up. Updated by
50 * the consumer_thread_receive_fds when it notices that all fds has hung up.
51 * Also updated by the signal handler (consumer_should_exit()). Read by the
54 volatile int consumer_quit
= 0;
57 * Find a stream. The consumer_data.lock must be locked during this
60 static struct lttng_consumer_stream
*consumer_find_stream(int key
)
62 struct lttng_ht_iter iter
;
63 struct lttng_ht_node_ulong
*node
;
64 struct lttng_consumer_stream
*stream
= NULL
;
66 /* Negative keys are lookup failures */
72 lttng_ht_lookup(consumer_data
.stream_ht
, (void *)((unsigned long) key
),
74 node
= lttng_ht_iter_get_node_ulong(&iter
);
76 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
84 static void consumer_steal_stream_key(int key
)
86 struct lttng_consumer_stream
*stream
;
89 stream
= consumer_find_stream(key
);
93 * We don't want the lookup to match, but we still need
94 * to iterate on this stream when iterating over the hash table. Just
95 * change the node key.
97 stream
->node
.key
= -1;
102 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
104 struct lttng_ht_iter iter
;
105 struct lttng_ht_node_ulong
*node
;
106 struct lttng_consumer_channel
*channel
= NULL
;
108 /* Negative keys are lookup failures */
114 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
116 node
= lttng_ht_iter_get_node_ulong(&iter
);
118 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
126 static void consumer_steal_channel_key(int key
)
128 struct lttng_consumer_channel
*channel
;
131 channel
= consumer_find_channel(key
);
135 * We don't want the lookup to match, but we still need
136 * to iterate on this channel when iterating over the hash table. Just
137 * change the node key.
139 channel
->node
.key
= -1;
145 void consumer_free_stream(struct rcu_head
*head
)
147 struct lttng_ht_node_ulong
*node
=
148 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
149 struct lttng_consumer_stream
*stream
=
150 caa_container_of(node
, struct lttng_consumer_stream
, node
);
156 * Remove a stream from the global list protected by a mutex. This
157 * function is also responsible for freeing its data structures.
159 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
162 struct lttng_ht_iter iter
;
163 struct lttng_consumer_channel
*free_chan
= NULL
;
165 pthread_mutex_lock(&consumer_data
.lock
);
167 switch (consumer_data
.type
) {
168 case LTTNG_CONSUMER_KERNEL
:
169 if (stream
->mmap_base
!= NULL
) {
170 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
176 case LTTNG_CONSUMER32_UST
:
177 case LTTNG_CONSUMER64_UST
:
178 lttng_ustconsumer_del_stream(stream
);
181 ERR("Unknown consumer_data type");
187 iter
.iter
.node
= &stream
->node
.node
;
188 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
193 if (consumer_data
.stream_count
<= 0) {
196 consumer_data
.stream_count
--;
200 if (stream
->out_fd
>= 0) {
201 ret
= close(stream
->out_fd
);
206 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
207 ret
= close(stream
->wait_fd
);
212 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
213 ret
= close(stream
->shm_fd
);
218 if (!--stream
->chan
->refcount
)
219 free_chan
= stream
->chan
;
221 call_rcu(&stream
->node
.head
, consumer_free_stream
);
223 consumer_data
.need_update
= 1;
224 pthread_mutex_unlock(&consumer_data
.lock
);
227 consumer_del_channel(free_chan
);
230 struct lttng_consumer_stream
*consumer_allocate_stream(
231 int channel_key
, int stream_key
,
232 int shm_fd
, int wait_fd
,
233 enum lttng_consumer_stream_state state
,
235 enum lttng_event_output output
,
236 const char *path_name
,
240 struct lttng_consumer_stream
*stream
;
243 stream
= zmalloc(sizeof(*stream
));
244 if (stream
== NULL
) {
245 perror("malloc struct lttng_consumer_stream");
248 stream
->chan
= consumer_find_channel(channel_key
);
250 perror("Unable to find channel key");
253 stream
->chan
->refcount
++;
254 stream
->key
= stream_key
;
255 stream
->shm_fd
= shm_fd
;
256 stream
->wait_fd
= wait_fd
;
258 stream
->out_fd_offset
= 0;
259 stream
->state
= state
;
260 stream
->mmap_len
= mmap_len
;
261 stream
->mmap_base
= NULL
;
262 stream
->output
= output
;
265 strncpy(stream
->path_name
, path_name
, PATH_MAX
- 1);
266 stream
->path_name
[PATH_MAX
- 1] = '\0';
267 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
269 switch (consumer_data
.type
) {
270 case LTTNG_CONSUMER_KERNEL
:
272 case LTTNG_CONSUMER32_UST
:
273 case LTTNG_CONSUMER64_UST
:
274 stream
->cpu
= stream
->chan
->cpucount
++;
275 ret
= lttng_ustconsumer_allocate_stream(stream
);
282 ERR("Unknown consumer_data type");
286 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
287 stream
->path_name
, stream
->key
,
290 (unsigned long long) stream
->mmap_len
,
297 * Add a stream to the global list protected by a mutex.
299 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
303 pthread_mutex_lock(&consumer_data
.lock
);
304 /* Steal stream identifier, for UST */
305 consumer_steal_stream_key(stream
->key
);
307 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
309 consumer_data
.stream_count
++;
310 consumer_data
.need_update
= 1;
312 switch (consumer_data
.type
) {
313 case LTTNG_CONSUMER_KERNEL
:
315 case LTTNG_CONSUMER32_UST
:
316 case LTTNG_CONSUMER64_UST
:
317 /* Streams are in CPU number order (we rely on this) */
318 stream
->cpu
= stream
->chan
->nr_streams
++;
321 ERR("Unknown consumer_data type");
327 pthread_mutex_unlock(&consumer_data
.lock
);
333 * Update a stream according to what we just received.
335 void consumer_change_stream_state(int stream_key
,
336 enum lttng_consumer_stream_state state
)
338 struct lttng_consumer_stream
*stream
;
340 pthread_mutex_lock(&consumer_data
.lock
);
341 stream
= consumer_find_stream(stream_key
);
343 stream
->state
= state
;
345 consumer_data
.need_update
= 1;
346 pthread_mutex_unlock(&consumer_data
.lock
);
350 void consumer_free_channel(struct rcu_head
*head
)
352 struct lttng_ht_node_ulong
*node
=
353 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
354 struct lttng_consumer_channel
*channel
=
355 caa_container_of(node
, struct lttng_consumer_channel
, node
);
361 * Remove a channel from the global list protected by a mutex. This
362 * function is also responsible for freeing its data structures.
364 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
367 struct lttng_ht_iter iter
;
369 pthread_mutex_lock(&consumer_data
.lock
);
371 switch (consumer_data
.type
) {
372 case LTTNG_CONSUMER_KERNEL
:
374 case LTTNG_CONSUMER32_UST
:
375 case LTTNG_CONSUMER64_UST
:
376 lttng_ustconsumer_del_channel(channel
);
379 ERR("Unknown consumer_data type");
385 iter
.iter
.node
= &channel
->node
.node
;
386 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
390 if (channel
->mmap_base
!= NULL
) {
391 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
396 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
397 ret
= close(channel
->wait_fd
);
402 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
403 ret
= close(channel
->shm_fd
);
409 call_rcu(&channel
->node
.head
, consumer_free_channel
);
411 pthread_mutex_unlock(&consumer_data
.lock
);
414 struct lttng_consumer_channel
*consumer_allocate_channel(
416 int shm_fd
, int wait_fd
,
418 uint64_t max_sb_size
)
420 struct lttng_consumer_channel
*channel
;
423 channel
= zmalloc(sizeof(*channel
));
424 if (channel
== NULL
) {
425 perror("malloc struct lttng_consumer_channel");
428 channel
->key
= channel_key
;
429 channel
->shm_fd
= shm_fd
;
430 channel
->wait_fd
= wait_fd
;
431 channel
->mmap_len
= mmap_len
;
432 channel
->max_sb_size
= max_sb_size
;
433 channel
->refcount
= 0;
434 channel
->nr_streams
= 0;
435 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
437 switch (consumer_data
.type
) {
438 case LTTNG_CONSUMER_KERNEL
:
439 channel
->mmap_base
= NULL
;
440 channel
->mmap_len
= 0;
442 case LTTNG_CONSUMER32_UST
:
443 case LTTNG_CONSUMER64_UST
:
444 ret
= lttng_ustconsumer_allocate_channel(channel
);
451 ERR("Unknown consumer_data type");
455 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
459 (unsigned long long) channel
->mmap_len
,
460 (unsigned long long) channel
->max_sb_size
);
466 * Add a channel to the global list protected by a mutex.
468 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
470 pthread_mutex_lock(&consumer_data
.lock
);
471 /* Steal channel identifier, for UST */
472 consumer_steal_channel_key(channel
->key
);
474 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
476 pthread_mutex_unlock(&consumer_data
.lock
);
482 * Allocate the pollfd structure and the local view of the out fds to avoid
483 * doing a lookup in the linked list and concurrency issues when writing is
484 * needed. Called with consumer_data.lock held.
486 * Returns the number of fds in the structures.
488 int consumer_update_poll_array(
489 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
490 struct lttng_consumer_stream
**local_stream
)
493 struct lttng_ht_iter iter
;
494 struct lttng_consumer_stream
*stream
;
496 DBG("Updating poll fd array");
498 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
500 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
503 DBG("Active FD %d", stream
->wait_fd
);
504 (*pollfd
)[i
].fd
= stream
->wait_fd
;
505 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
506 local_stream
[i
] = stream
;
512 * Insert the consumer_poll_pipe at the end of the array and don't
513 * increment i so nb_fd is the number of real FD.
515 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
516 (*pollfd
)[i
].events
= POLLIN
;
521 * Poll on the should_quit pipe and the command socket return -1 on error and
522 * should exit, 0 if data is available on the command socket
524 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
529 num_rdy
= poll(consumer_sockpoll
, 2, -1);
532 * Restart interrupted system call.
534 if (errno
== EINTR
) {
537 perror("Poll error");
540 if (consumer_sockpoll
[0].revents
== POLLIN
) {
541 DBG("consumer_should_quit wake up");
551 * Set the error socket.
553 void lttng_consumer_set_error_sock(
554 struct lttng_consumer_local_data
*ctx
, int sock
)
556 ctx
->consumer_error_socket
= sock
;
560 * Set the command socket path.
563 void lttng_consumer_set_command_sock_path(
564 struct lttng_consumer_local_data
*ctx
, char *sock
)
566 ctx
->consumer_command_sock_path
= sock
;
570 * Send return code to the session daemon.
571 * If the socket is not defined, we return 0, it is not a fatal error
573 int lttng_consumer_send_error(
574 struct lttng_consumer_local_data
*ctx
, int cmd
)
576 if (ctx
->consumer_error_socket
> 0) {
577 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
578 sizeof(enum lttcomm_sessiond_command
));
585 * Close all the tracefiles and stream fds, should be called when all instances
588 void lttng_consumer_cleanup(void)
590 struct lttng_ht_iter iter
;
591 struct lttng_ht_node_ulong
*node
;
596 * close all outfd. Called when there are no more threads running (after
597 * joining on the threads), no need to protect list iteration with mutex.
599 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
601 struct lttng_consumer_stream
*stream
=
602 caa_container_of(node
, struct lttng_consumer_stream
, node
);
603 consumer_del_stream(stream
);
606 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
608 struct lttng_consumer_channel
*channel
=
609 caa_container_of(node
, struct lttng_consumer_channel
, node
);
610 consumer_del_channel(channel
);
617 * Called from signal handler.
619 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
623 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
625 perror("write consumer quit");
629 void lttng_consumer_sync_trace_file(
630 struct lttng_consumer_stream
*stream
, off_t orig_offset
)
632 int outfd
= stream
->out_fd
;
635 * This does a blocking write-and-wait on any page that belongs to the
636 * subbuffer prior to the one we just wrote.
637 * Don't care about error values, as these are just hints and ways to
638 * limit the amount of page cache used.
640 if (orig_offset
< stream
->chan
->max_sb_size
) {
643 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
644 stream
->chan
->max_sb_size
,
645 SYNC_FILE_RANGE_WAIT_BEFORE
646 | SYNC_FILE_RANGE_WRITE
647 | SYNC_FILE_RANGE_WAIT_AFTER
);
649 * Give hints to the kernel about how we access the file:
650 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
653 * We need to call fadvise again after the file grows because the
654 * kernel does not seem to apply fadvise to non-existing parts of the
657 * Call fadvise _after_ having waited for the page writeback to
658 * complete because the dirty page writeback semantic is not well
659 * defined. So it can be expected to lead to lower throughput in
662 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
663 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
667 * Initialise the necessary environnement :
668 * - create a new context
669 * - create the poll_pipe
670 * - create the should_quit pipe (for signal handler)
671 * - create the thread pipe (for splice)
673 * Takes a function pointer as argument, this function is called when data is
674 * available on a buffer. This function is responsible to do the
675 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
676 * buffer configuration and then kernctl_put_next_subbuf at the end.
678 * Returns a pointer to the new context or NULL on error.
680 struct lttng_consumer_local_data
*lttng_consumer_create(
681 enum lttng_consumer_type type
,
682 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
683 struct lttng_consumer_local_data
*ctx
),
684 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
685 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
686 int (*update_stream
)(int stream_key
, uint32_t state
))
689 struct lttng_consumer_local_data
*ctx
;
691 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
692 consumer_data
.type
== type
);
693 consumer_data
.type
= type
;
695 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
697 perror("allocating context");
701 ctx
->consumer_error_socket
= -1;
702 /* assign the callbacks */
703 ctx
->on_buffer_ready
= buffer_ready
;
704 ctx
->on_recv_channel
= recv_channel
;
705 ctx
->on_recv_stream
= recv_stream
;
706 ctx
->on_update_stream
= update_stream
;
708 ret
= pipe(ctx
->consumer_poll_pipe
);
710 perror("Error creating poll pipe");
711 goto error_poll_pipe
;
714 ret
= pipe(ctx
->consumer_should_quit
);
716 perror("Error creating recv pipe");
717 goto error_quit_pipe
;
720 ret
= pipe(ctx
->consumer_thread_pipe
);
722 perror("Error creating thread pipe");
723 goto error_thread_pipe
;
730 for (i
= 0; i
< 2; i
++) {
733 err
= close(ctx
->consumer_should_quit
[i
]);
739 for (i
= 0; i
< 2; i
++) {
742 err
= close(ctx
->consumer_poll_pipe
[i
]);
754 * Close all fds associated with the instance and free the context.
756 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
760 ret
= close(ctx
->consumer_error_socket
);
764 ret
= close(ctx
->consumer_thread_pipe
[0]);
768 ret
= close(ctx
->consumer_thread_pipe
[1]);
772 ret
= close(ctx
->consumer_poll_pipe
[0]);
776 ret
= close(ctx
->consumer_poll_pipe
[1]);
780 ret
= close(ctx
->consumer_should_quit
[0]);
784 ret
= close(ctx
->consumer_should_quit
[1]);
788 unlink(ctx
->consumer_command_sock_path
);
793 * Mmap the ring buffer, read it and write the data to the tracefile.
795 * Returns the number of bytes written
797 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
798 struct lttng_consumer_local_data
*ctx
,
799 struct lttng_consumer_stream
*stream
, unsigned long len
)
801 switch (consumer_data
.type
) {
802 case LTTNG_CONSUMER_KERNEL
:
803 return lttng_kconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
804 case LTTNG_CONSUMER32_UST
:
805 case LTTNG_CONSUMER64_UST
:
806 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
808 ERR("Unknown consumer_data type");
816 * Splice the data from the ring buffer to the tracefile.
818 * Returns the number of bytes spliced.
820 ssize_t
lttng_consumer_on_read_subbuffer_splice(
821 struct lttng_consumer_local_data
*ctx
,
822 struct lttng_consumer_stream
*stream
, unsigned long len
)
824 switch (consumer_data
.type
) {
825 case LTTNG_CONSUMER_KERNEL
:
826 return lttng_kconsumer_on_read_subbuffer_splice(ctx
, stream
, len
);
827 case LTTNG_CONSUMER32_UST
:
828 case LTTNG_CONSUMER64_UST
:
831 ERR("Unknown consumer_data type");
839 * Take a snapshot for a specific fd
841 * Returns 0 on success, < 0 on error
843 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
844 struct lttng_consumer_stream
*stream
)
846 switch (consumer_data
.type
) {
847 case LTTNG_CONSUMER_KERNEL
:
848 return lttng_kconsumer_take_snapshot(ctx
, stream
);
849 case LTTNG_CONSUMER32_UST
:
850 case LTTNG_CONSUMER64_UST
:
851 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
853 ERR("Unknown consumer_data type");
861 * Get the produced position
863 * Returns 0 on success, < 0 on error
865 int lttng_consumer_get_produced_snapshot(
866 struct lttng_consumer_local_data
*ctx
,
867 struct lttng_consumer_stream
*stream
,
870 switch (consumer_data
.type
) {
871 case LTTNG_CONSUMER_KERNEL
:
872 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
873 case LTTNG_CONSUMER32_UST
:
874 case LTTNG_CONSUMER64_UST
:
875 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
877 ERR("Unknown consumer_data type");
883 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
884 int sock
, struct pollfd
*consumer_sockpoll
)
886 switch (consumer_data
.type
) {
887 case LTTNG_CONSUMER_KERNEL
:
888 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
889 case LTTNG_CONSUMER32_UST
:
890 case LTTNG_CONSUMER64_UST
:
891 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
893 ERR("Unknown consumer_data type");
900 * This thread polls the fds in the set to consume the data and write
901 * it to tracefile if necessary.
903 void *lttng_consumer_thread_poll_fds(void *data
)
905 int num_rdy
, num_hup
, high_prio
, ret
, i
;
906 struct pollfd
*pollfd
= NULL
;
907 /* local view of the streams */
908 struct lttng_consumer_stream
**local_stream
= NULL
;
909 /* local view of consumer_data.fds_count */
913 struct lttng_consumer_local_data
*ctx
= data
;
915 rcu_register_thread();
917 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
924 * the fds set has been updated, we need to update our
925 * local array as well
927 pthread_mutex_lock(&consumer_data
.lock
);
928 if (consumer_data
.need_update
) {
929 if (pollfd
!= NULL
) {
933 if (local_stream
!= NULL
) {
938 /* allocate for all fds + 1 for the consumer_poll_pipe */
939 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
940 if (pollfd
== NULL
) {
941 perror("pollfd malloc");
942 pthread_mutex_unlock(&consumer_data
.lock
);
946 /* allocate for all fds + 1 for the consumer_poll_pipe */
947 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
948 sizeof(struct lttng_consumer_stream
));
949 if (local_stream
== NULL
) {
950 perror("local_stream malloc");
951 pthread_mutex_unlock(&consumer_data
.lock
);
954 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
956 ERR("Error in allocating pollfd or local_outfds");
957 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
958 pthread_mutex_unlock(&consumer_data
.lock
);
962 consumer_data
.need_update
= 0;
964 pthread_mutex_unlock(&consumer_data
.lock
);
966 /* No FDs and consumer_quit, consumer_cleanup the thread */
967 if (nb_fd
== 0 && consumer_quit
== 1) {
970 /* poll on the array of fds */
972 DBG("polling on %d fd", nb_fd
+ 1);
973 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
974 DBG("poll num_rdy : %d", num_rdy
);
977 * Restart interrupted system call.
979 if (errno
== EINTR
) {
982 perror("Poll error");
983 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
985 } else if (num_rdy
== 0) {
986 DBG("Polling thread timed out");
991 * If the consumer_poll_pipe triggered poll go
992 * directly to the beginning of the loop to update the
993 * array. We want to prioritize array update over
994 * low-priority reads.
996 if (pollfd
[nb_fd
].revents
& POLLIN
) {
997 DBG("consumer_poll_pipe wake up");
998 tmp2
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
1000 perror("read consumer poll");
1005 /* Take care of high priority channels first. */
1006 for (i
= 0; i
< nb_fd
; i
++) {
1007 if (pollfd
[i
].revents
& POLLPRI
) {
1010 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
1012 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1013 /* it's ok to have an unavailable sub-buffer */
1014 if (len
< 0 && len
!= -EAGAIN
) {
1016 } else if (len
> 0) {
1017 local_stream
[i
]->data_read
= 1;
1023 * If we read high prio channel in this loop, try again
1024 * for more high prio data.
1030 /* Take care of low priority channels. */
1031 for (i
= 0; i
< nb_fd
; i
++) {
1032 if ((pollfd
[i
].revents
& POLLIN
) ||
1033 local_stream
[i
]->hangup_flush_done
) {
1036 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1037 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1038 /* it's ok to have an unavailable sub-buffer */
1039 if (len
< 0 && len
!= -EAGAIN
) {
1041 } else if (len
> 0) {
1042 local_stream
[i
]->data_read
= 1;
1047 /* Handle hangup and errors */
1048 for (i
= 0; i
< nb_fd
; i
++) {
1049 if (!local_stream
[i
]->hangup_flush_done
1050 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1051 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1052 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1053 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1055 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
1056 /* Attempt read again, for the data we just flushed. */
1057 local_stream
[i
]->data_read
= 1;
1060 * If the poll flag is HUP/ERR/NVAL and we have
1061 * read no data in this pass, we can remove the
1062 * stream from its hash table.
1064 if ((pollfd
[i
].revents
& POLLHUP
)) {
1065 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
1066 if (!local_stream
[i
]->data_read
) {
1067 consumer_del_stream(local_stream
[i
]);
1070 } else if (pollfd
[i
].revents
& POLLERR
) {
1071 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
1072 if (!local_stream
[i
]->data_read
) {
1073 consumer_del_stream(local_stream
[i
]);
1076 } else if (pollfd
[i
].revents
& POLLNVAL
) {
1077 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
1078 if (!local_stream
[i
]->data_read
) {
1079 consumer_del_stream(local_stream
[i
]);
1083 local_stream
[i
]->data_read
= 0;
1087 DBG("polling thread exiting");
1088 if (pollfd
!= NULL
) {
1092 if (local_stream
!= NULL
) {
1094 local_stream
= NULL
;
1096 rcu_unregister_thread();
1101 * This thread listens on the consumerd socket and receives the file
1102 * descriptors from the session daemon.
1104 void *lttng_consumer_thread_receive_fds(void *data
)
1106 int sock
, client_socket
, ret
;
1108 * structure to poll for incoming data on communication socket avoids
1109 * making blocking sockets.
1111 struct pollfd consumer_sockpoll
[2];
1112 struct lttng_consumer_local_data
*ctx
= data
;
1114 rcu_register_thread();
1116 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
1117 unlink(ctx
->consumer_command_sock_path
);
1118 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
1119 if (client_socket
< 0) {
1120 ERR("Cannot create command socket");
1124 ret
= lttcomm_listen_unix_sock(client_socket
);
1129 DBG("Sending ready command to lttng-sessiond");
1130 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
1131 /* return < 0 on error, but == 0 is not fatal */
1133 ERR("Error sending ready command to lttng-sessiond");
1137 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1139 perror("fcntl O_NONBLOCK");
1143 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1144 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1145 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1146 consumer_sockpoll
[1].fd
= client_socket
;
1147 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1149 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1152 DBG("Connection on client_socket");
1154 /* Blocking call, waiting for transmission */
1155 sock
= lttcomm_accept_unix_sock(client_socket
);
1160 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1162 perror("fcntl O_NONBLOCK");
1166 /* update the polling structure to poll on the established socket */
1167 consumer_sockpoll
[1].fd
= sock
;
1168 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1171 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1174 DBG("Incoming command on sock");
1175 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1176 if (ret
== -ENOENT
) {
1177 DBG("Received STOP command");
1181 ERR("Communication interrupted on command socket");
1184 if (consumer_quit
) {
1185 DBG("consumer_thread_receive_fds received quit from signal");
1188 DBG("received fds on sock");
1191 DBG("consumer_thread_receive_fds exiting");
1194 * when all fds have hung up, the polling thread
1200 * 2s of grace period, if no polling events occur during
1201 * this period, the polling thread will exit even if there
1202 * are still open FDs (should not happen, but safety mechanism).
1204 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1206 /* wake up the polling thread */
1207 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
1209 perror("poll pipe write");
1211 rcu_unregister_thread();
1215 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1216 struct lttng_consumer_local_data
*ctx
)
1218 switch (consumer_data
.type
) {
1219 case LTTNG_CONSUMER_KERNEL
:
1220 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1221 case LTTNG_CONSUMER32_UST
:
1222 case LTTNG_CONSUMER64_UST
:
1223 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1225 ERR("Unknown consumer_data type");
1231 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1233 switch (consumer_data
.type
) {
1234 case LTTNG_CONSUMER_KERNEL
:
1235 return lttng_kconsumer_on_recv_stream(stream
);
1236 case LTTNG_CONSUMER32_UST
:
1237 case LTTNG_CONSUMER64_UST
:
1238 return lttng_ustconsumer_on_recv_stream(stream
);
1240 ERR("Unknown consumer_data type");
1247 * Allocate and set consumer data hash tables.
1249 void lttng_consumer_init(void)
1251 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1252 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);