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 close(stream
->out_fd
);
179 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
180 close(stream
->wait_fd
);
182 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
183 close(stream
->shm_fd
);
185 if (!--stream
->chan
->refcount
)
186 free_chan
= stream
->chan
;
189 consumer_data
.need_update
= 1;
190 pthread_mutex_unlock(&consumer_data
.lock
);
193 consumer_del_channel(free_chan
);
196 static void consumer_del_stream_rcu(struct rcu_head
*head
)
198 struct lttng_ht_node_ulong
*node
=
199 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
200 struct lttng_consumer_stream
*stream
=
201 caa_container_of(node
, struct lttng_consumer_stream
, node
);
203 consumer_del_stream(stream
);
206 struct lttng_consumer_stream
*consumer_allocate_stream(
207 int channel_key
, int stream_key
,
208 int shm_fd
, int wait_fd
,
209 enum lttng_consumer_stream_state state
,
211 enum lttng_event_output output
,
212 const char *path_name
,
216 struct lttng_consumer_stream
*stream
;
219 stream
= zmalloc(sizeof(*stream
));
220 if (stream
== NULL
) {
221 perror("malloc struct lttng_consumer_stream");
224 stream
->chan
= consumer_find_channel(channel_key
);
226 perror("Unable to find channel key");
229 stream
->chan
->refcount
++;
230 stream
->key
= stream_key
;
231 stream
->shm_fd
= shm_fd
;
232 stream
->wait_fd
= wait_fd
;
234 stream
->out_fd_offset
= 0;
235 stream
->state
= state
;
236 stream
->mmap_len
= mmap_len
;
237 stream
->mmap_base
= NULL
;
238 stream
->output
= output
;
241 strncpy(stream
->path_name
, path_name
, PATH_MAX
- 1);
242 stream
->path_name
[PATH_MAX
- 1] = '\0';
243 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
245 switch (consumer_data
.type
) {
246 case LTTNG_CONSUMER_KERNEL
:
248 case LTTNG_CONSUMER32_UST
:
249 case LTTNG_CONSUMER64_UST
:
250 stream
->cpu
= stream
->chan
->cpucount
++;
251 ret
= lttng_ustconsumer_allocate_stream(stream
);
258 ERR("Unknown consumer_data type");
262 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
263 stream
->path_name
, stream
->key
,
266 (unsigned long long) stream
->mmap_len
,
273 * Add a stream to the global list protected by a mutex.
275 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
279 pthread_mutex_lock(&consumer_data
.lock
);
280 /* Steal stream identifier, for UST */
281 consumer_steal_stream_key(stream
->key
);
283 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
285 consumer_data
.stream_count
++;
286 consumer_data
.need_update
= 1;
288 switch (consumer_data
.type
) {
289 case LTTNG_CONSUMER_KERNEL
:
291 case LTTNG_CONSUMER32_UST
:
292 case LTTNG_CONSUMER64_UST
:
293 /* Streams are in CPU number order (we rely on this) */
294 stream
->cpu
= stream
->chan
->nr_streams
++;
297 ERR("Unknown consumer_data type");
303 pthread_mutex_unlock(&consumer_data
.lock
);
308 * Update a stream according to what we just received.
310 void consumer_change_stream_state(int stream_key
,
311 enum lttng_consumer_stream_state state
)
313 struct lttng_consumer_stream
*stream
;
315 pthread_mutex_lock(&consumer_data
.lock
);
316 stream
= consumer_find_stream(stream_key
);
318 stream
->state
= state
;
320 consumer_data
.need_update
= 1;
321 pthread_mutex_unlock(&consumer_data
.lock
);
325 * Remove a channel from the global list protected by a mutex. This
326 * function is also responsible for freeing its data structures.
328 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
331 struct lttng_ht_iter iter
;
333 pthread_mutex_lock(&consumer_data
.lock
);
335 switch (consumer_data
.type
) {
336 case LTTNG_CONSUMER_KERNEL
:
338 case LTTNG_CONSUMER32_UST
:
339 case LTTNG_CONSUMER64_UST
:
340 lttng_ustconsumer_del_channel(channel
);
343 ERR("Unknown consumer_data type");
350 lttng_ht_lookup(consumer_data
.channel_ht
,
351 (void *)((unsigned long) channel
->key
), &iter
);
352 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
357 if (channel
->mmap_base
!= NULL
) {
358 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
363 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
364 close(channel
->wait_fd
);
366 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
367 close(channel
->shm_fd
);
371 pthread_mutex_unlock(&consumer_data
.lock
);
374 static void consumer_del_channel_rcu(struct rcu_head
*head
)
376 struct lttng_ht_node_ulong
*node
=
377 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
378 struct lttng_consumer_channel
*channel
=
379 caa_container_of(node
, struct lttng_consumer_channel
, node
);
381 consumer_del_channel(channel
);
384 struct lttng_consumer_channel
*consumer_allocate_channel(
386 int shm_fd
, int wait_fd
,
388 uint64_t max_sb_size
)
390 struct lttng_consumer_channel
*channel
;
393 channel
= zmalloc(sizeof(*channel
));
394 if (channel
== NULL
) {
395 perror("malloc struct lttng_consumer_channel");
398 channel
->key
= channel_key
;
399 channel
->shm_fd
= shm_fd
;
400 channel
->wait_fd
= wait_fd
;
401 channel
->mmap_len
= mmap_len
;
402 channel
->max_sb_size
= max_sb_size
;
403 channel
->refcount
= 0;
404 channel
->nr_streams
= 0;
405 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
407 switch (consumer_data
.type
) {
408 case LTTNG_CONSUMER_KERNEL
:
409 channel
->mmap_base
= NULL
;
410 channel
->mmap_len
= 0;
412 case LTTNG_CONSUMER32_UST
:
413 case LTTNG_CONSUMER64_UST
:
414 ret
= lttng_ustconsumer_allocate_channel(channel
);
421 ERR("Unknown consumer_data type");
425 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
429 (unsigned long long) channel
->mmap_len
,
430 (unsigned long long) channel
->max_sb_size
);
436 * Add a channel to the global list protected by a mutex.
438 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
440 pthread_mutex_lock(&consumer_data
.lock
);
441 /* Steal channel identifier, for UST */
442 consumer_steal_channel_key(channel
->key
);
444 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
446 pthread_mutex_unlock(&consumer_data
.lock
);
451 * Allocate the pollfd structure and the local view of the out fds to avoid
452 * doing a lookup in the linked list and concurrency issues when writing is
453 * needed. Called with consumer_data.lock held.
455 * Returns the number of fds in the structures.
457 int consumer_update_poll_array(
458 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
459 struct lttng_consumer_stream
**local_stream
)
462 struct lttng_ht_iter iter
;
463 struct lttng_consumer_stream
*stream
;
465 DBG("Updating poll fd array");
466 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
468 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
471 DBG("Active FD %d", stream
->wait_fd
);
472 (*pollfd
)[i
].fd
= stream
->wait_fd
;
473 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
474 local_stream
[i
] = stream
;
479 * Insert the consumer_poll_pipe at the end of the array and don't
480 * increment i so nb_fd is the number of real FD.
482 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
483 (*pollfd
)[i
].events
= POLLIN
;
488 * Poll on the should_quit pipe and the command socket return -1 on error and
489 * should exit, 0 if data is available on the command socket
491 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
496 num_rdy
= poll(consumer_sockpoll
, 2, -1);
499 * Restart interrupted system call.
501 if (errno
== EINTR
) {
504 perror("Poll error");
507 if (consumer_sockpoll
[0].revents
== POLLIN
) {
508 DBG("consumer_should_quit wake up");
518 * Set the error socket.
520 void lttng_consumer_set_error_sock(
521 struct lttng_consumer_local_data
*ctx
, int sock
)
523 ctx
->consumer_error_socket
= sock
;
527 * Set the command socket path.
530 void lttng_consumer_set_command_sock_path(
531 struct lttng_consumer_local_data
*ctx
, char *sock
)
533 ctx
->consumer_command_sock_path
= sock
;
537 * Send return code to the session daemon.
538 * If the socket is not defined, we return 0, it is not a fatal error
540 int lttng_consumer_send_error(
541 struct lttng_consumer_local_data
*ctx
, int cmd
)
543 if (ctx
->consumer_error_socket
> 0) {
544 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
545 sizeof(enum lttcomm_sessiond_command
));
552 * Close all the tracefiles and stream fds, should be called when all instances
555 void lttng_consumer_cleanup(void)
558 struct lttng_ht_iter iter
;
559 struct lttng_ht_node_ulong
*node
;
564 * close all outfd. Called when there are no more threads running (after
565 * joining on the threads), no need to protect list iteration with mutex.
567 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
569 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
571 call_rcu(&node
->head
, consumer_del_stream_rcu
);
574 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
576 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
578 call_rcu(&node
->head
, consumer_del_channel_rcu
);
585 * Called from signal handler.
587 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
591 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
593 perror("write consumer quit");
597 void lttng_consumer_sync_trace_file(
598 struct lttng_consumer_stream
*stream
, off_t orig_offset
)
600 int outfd
= stream
->out_fd
;
603 * This does a blocking write-and-wait on any page that belongs to the
604 * subbuffer prior to the one we just wrote.
605 * Don't care about error values, as these are just hints and ways to
606 * limit the amount of page cache used.
608 if (orig_offset
< stream
->chan
->max_sb_size
) {
611 sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
612 stream
->chan
->max_sb_size
,
613 SYNC_FILE_RANGE_WAIT_BEFORE
614 | SYNC_FILE_RANGE_WRITE
615 | SYNC_FILE_RANGE_WAIT_AFTER
);
617 * Give hints to the kernel about how we access the file:
618 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
621 * We need to call fadvise again after the file grows because the
622 * kernel does not seem to apply fadvise to non-existing parts of the
625 * Call fadvise _after_ having waited for the page writeback to
626 * complete because the dirty page writeback semantic is not well
627 * defined. So it can be expected to lead to lower throughput in
630 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
631 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
635 * Initialise the necessary environnement :
636 * - create a new context
637 * - create the poll_pipe
638 * - create the should_quit pipe (for signal handler)
639 * - create the thread pipe (for splice)
641 * Takes a function pointer as argument, this function is called when data is
642 * available on a buffer. This function is responsible to do the
643 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
644 * buffer configuration and then kernctl_put_next_subbuf at the end.
646 * Returns a pointer to the new context or NULL on error.
648 struct lttng_consumer_local_data
*lttng_consumer_create(
649 enum lttng_consumer_type type
,
650 int (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
651 struct lttng_consumer_local_data
*ctx
),
652 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
653 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
654 int (*update_stream
)(int stream_key
, uint32_t state
))
657 struct lttng_consumer_local_data
*ctx
;
659 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
660 consumer_data
.type
== type
);
661 consumer_data
.type
= type
;
663 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
665 perror("allocating context");
669 ctx
->consumer_error_socket
= -1;
670 /* assign the callbacks */
671 ctx
->on_buffer_ready
= buffer_ready
;
672 ctx
->on_recv_channel
= recv_channel
;
673 ctx
->on_recv_stream
= recv_stream
;
674 ctx
->on_update_stream
= update_stream
;
676 ret
= pipe(ctx
->consumer_poll_pipe
);
678 perror("Error creating poll pipe");
679 goto error_poll_pipe
;
682 ret
= pipe(ctx
->consumer_should_quit
);
684 perror("Error creating recv pipe");
685 goto error_quit_pipe
;
688 ret
= pipe(ctx
->consumer_thread_pipe
);
690 perror("Error creating thread pipe");
691 goto error_thread_pipe
;
698 for (i
= 0; i
< 2; i
++) {
701 err
= close(ctx
->consumer_should_quit
[i
]);
705 for (i
= 0; i
< 2; i
++) {
708 err
= close(ctx
->consumer_poll_pipe
[i
]);
718 * Close all fds associated with the instance and free the context.
720 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
722 close(ctx
->consumer_error_socket
);
723 close(ctx
->consumer_thread_pipe
[0]);
724 close(ctx
->consumer_thread_pipe
[1]);
725 close(ctx
->consumer_poll_pipe
[0]);
726 close(ctx
->consumer_poll_pipe
[1]);
727 close(ctx
->consumer_should_quit
[0]);
728 close(ctx
->consumer_should_quit
[1]);
729 unlink(ctx
->consumer_command_sock_path
);
734 * Mmap the ring buffer, read it and write the data to the tracefile.
736 * Returns the number of bytes written
738 int lttng_consumer_on_read_subbuffer_mmap(
739 struct lttng_consumer_local_data
*ctx
,
740 struct lttng_consumer_stream
*stream
, unsigned long len
)
742 switch (consumer_data
.type
) {
743 case LTTNG_CONSUMER_KERNEL
:
744 return lttng_kconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
745 case LTTNG_CONSUMER32_UST
:
746 case LTTNG_CONSUMER64_UST
:
747 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
749 ERR("Unknown consumer_data type");
755 * Splice the data from the ring buffer to the tracefile.
757 * Returns the number of bytes spliced.
759 int lttng_consumer_on_read_subbuffer_splice(
760 struct lttng_consumer_local_data
*ctx
,
761 struct lttng_consumer_stream
*stream
, unsigned long len
)
763 switch (consumer_data
.type
) {
764 case LTTNG_CONSUMER_KERNEL
:
765 return lttng_kconsumer_on_read_subbuffer_splice(ctx
, stream
, len
);
766 case LTTNG_CONSUMER32_UST
:
767 case LTTNG_CONSUMER64_UST
:
770 ERR("Unknown consumer_data type");
778 * Take a snapshot for a specific fd
780 * Returns 0 on success, < 0 on error
782 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
783 struct lttng_consumer_stream
*stream
)
785 switch (consumer_data
.type
) {
786 case LTTNG_CONSUMER_KERNEL
:
787 return lttng_kconsumer_take_snapshot(ctx
, stream
);
788 case LTTNG_CONSUMER32_UST
:
789 case LTTNG_CONSUMER64_UST
:
790 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
792 ERR("Unknown consumer_data type");
800 * Get the produced position
802 * Returns 0 on success, < 0 on error
804 int lttng_consumer_get_produced_snapshot(
805 struct lttng_consumer_local_data
*ctx
,
806 struct lttng_consumer_stream
*stream
,
809 switch (consumer_data
.type
) {
810 case LTTNG_CONSUMER_KERNEL
:
811 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
812 case LTTNG_CONSUMER32_UST
:
813 case LTTNG_CONSUMER64_UST
:
814 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
816 ERR("Unknown consumer_data type");
822 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
823 int sock
, struct pollfd
*consumer_sockpoll
)
825 switch (consumer_data
.type
) {
826 case LTTNG_CONSUMER_KERNEL
:
827 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
828 case LTTNG_CONSUMER32_UST
:
829 case LTTNG_CONSUMER64_UST
:
830 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
832 ERR("Unknown consumer_data type");
839 * This thread polls the fds in the set to consume the data and write
840 * it to tracefile if necessary.
842 void *lttng_consumer_thread_poll_fds(void *data
)
844 int num_rdy
, num_hup
, high_prio
, ret
, i
;
845 struct pollfd
*pollfd
= NULL
;
846 /* local view of the streams */
847 struct lttng_consumer_stream
**local_stream
= NULL
;
848 /* local view of consumer_data.fds_count */
852 struct lttng_consumer_local_data
*ctx
= data
;
854 rcu_register_thread();
856 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
863 * the fds set has been updated, we need to update our
864 * local array as well
866 pthread_mutex_lock(&consumer_data
.lock
);
867 if (consumer_data
.need_update
) {
868 if (pollfd
!= NULL
) {
872 if (local_stream
!= NULL
) {
877 /* allocate for all fds + 1 for the consumer_poll_pipe */
878 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
879 if (pollfd
== NULL
) {
880 perror("pollfd malloc");
881 pthread_mutex_unlock(&consumer_data
.lock
);
885 /* allocate for all fds + 1 for the consumer_poll_pipe */
886 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
887 sizeof(struct lttng_consumer_stream
));
888 if (local_stream
== NULL
) {
889 perror("local_stream malloc");
890 pthread_mutex_unlock(&consumer_data
.lock
);
893 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
895 ERR("Error in allocating pollfd or local_outfds");
896 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
897 pthread_mutex_unlock(&consumer_data
.lock
);
901 consumer_data
.need_update
= 0;
903 pthread_mutex_unlock(&consumer_data
.lock
);
905 /* poll on the array of fds */
907 DBG("polling on %d fd", nb_fd
+ 1);
908 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
909 DBG("poll num_rdy : %d", num_rdy
);
912 * Restart interrupted system call.
914 if (errno
== EINTR
) {
917 perror("Poll error");
918 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
920 } else if (num_rdy
== 0) {
921 DBG("Polling thread timed out");
925 /* No FDs and consumer_quit, consumer_cleanup the thread */
926 if (nb_fd
== 0 && consumer_quit
== 1) {
931 * If the consumer_poll_pipe triggered poll go
932 * directly to the beginning of the loop to update the
933 * array. We want to prioritize array update over
934 * low-priority reads.
936 if (pollfd
[nb_fd
].revents
& POLLIN
) {
937 DBG("consumer_poll_pipe wake up");
938 tmp2
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
940 perror("read consumer poll");
945 /* Take care of high priority channels first. */
946 for (i
= 0; i
< nb_fd
; i
++) {
947 if (pollfd
[i
].revents
& POLLPRI
) {
948 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
950 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
951 /* it's ok to have an unavailable sub-buffer */
955 } else if (pollfd
[i
].revents
& POLLERR
) {
956 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
958 consumer_del_stream_rcu(&local_stream
[i
]->node
.head
);
961 } else if (pollfd
[i
].revents
& POLLNVAL
) {
962 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
964 consumer_del_stream_rcu(&local_stream
[i
]->node
.head
);
967 } else if ((pollfd
[i
].revents
& POLLHUP
) &&
968 !(pollfd
[i
].revents
& POLLIN
)) {
969 if (consumer_data
.type
== LTTNG_CONSUMER32_UST
970 || consumer_data
.type
== LTTNG_CONSUMER64_UST
) {
971 DBG("Polling fd %d tells it has hung up. Attempting flush and read.",
973 if (!local_stream
[i
]->hangup_flush_done
) {
974 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
975 /* read after flush */
977 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
978 } while (ret
== EAGAIN
);
981 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
984 consumer_del_stream_rcu(&local_stream
[i
]->node
.head
);
990 /* If every buffer FD has hung up, we end the read loop here */
991 if (nb_fd
> 0 && num_hup
== nb_fd
) {
992 DBG("every buffer FD has hung up\n");
993 if (consumer_quit
== 1) {
999 /* Take care of low priority channels. */
1000 if (high_prio
== 0) {
1001 for (i
= 0; i
< nb_fd
; i
++) {
1002 if (pollfd
[i
].revents
& POLLIN
) {
1003 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1004 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1005 /* it's ok to have an unavailable subbuffer */
1006 if (ret
== EAGAIN
) {
1014 DBG("polling thread exiting");
1015 if (pollfd
!= NULL
) {
1019 if (local_stream
!= NULL
) {
1021 local_stream
= NULL
;
1023 rcu_unregister_thread();
1028 * This thread listens on the consumerd socket and receives the file
1029 * descriptors from the session daemon.
1031 void *lttng_consumer_thread_receive_fds(void *data
)
1033 int sock
, client_socket
, ret
;
1035 * structure to poll for incoming data on communication socket avoids
1036 * making blocking sockets.
1038 struct pollfd consumer_sockpoll
[2];
1039 struct lttng_consumer_local_data
*ctx
= data
;
1041 rcu_register_thread();
1043 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
1044 unlink(ctx
->consumer_command_sock_path
);
1045 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
1046 if (client_socket
< 0) {
1047 ERR("Cannot create command socket");
1051 ret
= lttcomm_listen_unix_sock(client_socket
);
1056 DBG("Sending ready command to lttng-sessiond");
1057 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
1058 /* return < 0 on error, but == 0 is not fatal */
1060 ERR("Error sending ready command to lttng-sessiond");
1064 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1066 perror("fcntl O_NONBLOCK");
1070 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1071 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1072 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1073 consumer_sockpoll
[1].fd
= client_socket
;
1074 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1076 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1079 DBG("Connection on client_socket");
1081 /* Blocking call, waiting for transmission */
1082 sock
= lttcomm_accept_unix_sock(client_socket
);
1087 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1089 perror("fcntl O_NONBLOCK");
1093 /* update the polling structure to poll on the established socket */
1094 consumer_sockpoll
[1].fd
= sock
;
1095 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1098 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1101 DBG("Incoming command on sock");
1102 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1103 if (ret
== -ENOENT
) {
1104 DBG("Received STOP command");
1108 ERR("Communication interrupted on command socket");
1111 if (consumer_quit
) {
1112 DBG("consumer_thread_receive_fds received quit from signal");
1115 DBG("received fds on sock");
1118 DBG("consumer_thread_receive_fds exiting");
1121 * when all fds have hung up, the polling thread
1127 * 2s of grace period, if no polling events occur during
1128 * this period, the polling thread will exit even if there
1129 * are still open FDs (should not happen, but safety mechanism).
1131 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1133 /* wake up the polling thread */
1134 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
1136 perror("poll pipe write");
1138 rcu_unregister_thread();
1142 int lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1143 struct lttng_consumer_local_data
*ctx
)
1145 switch (consumer_data
.type
) {
1146 case LTTNG_CONSUMER_KERNEL
:
1147 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1148 case LTTNG_CONSUMER32_UST
:
1149 case LTTNG_CONSUMER64_UST
:
1150 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1152 ERR("Unknown consumer_data type");
1158 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1160 switch (consumer_data
.type
) {
1161 case LTTNG_CONSUMER_KERNEL
:
1162 return lttng_kconsumer_on_recv_stream(stream
);
1163 case LTTNG_CONSUMER32_UST
:
1164 case LTTNG_CONSUMER64_UST
:
1165 return lttng_ustconsumer_on_recv_stream(stream
);
1167 ERR("Unknown consumer_data type");
1174 * Allocate and set consumer data hash tables.
1176 void lttng_consumer_init(void)
1178 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1179 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);