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
;
88 stream
= consumer_find_stream(key
);
93 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
95 struct lttng_ht_iter iter
;
96 struct lttng_ht_node_ulong
*node
;
97 struct lttng_consumer_channel
*channel
= NULL
;
99 /* Negative keys are lookup failures */
105 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
107 node
= lttng_ht_iter_get_node_ulong(&iter
);
109 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
117 static void consumer_steal_channel_key(int key
)
119 struct lttng_consumer_channel
*channel
;
121 channel
= consumer_find_channel(key
);
127 * Remove a stream from the global list protected by a mutex. This
128 * function is also responsible for freeing its data structures.
130 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
133 struct lttng_ht_iter iter
;
134 struct lttng_consumer_channel
*free_chan
= NULL
;
136 pthread_mutex_lock(&consumer_data
.lock
);
138 switch (consumer_data
.type
) {
139 case LTTNG_CONSUMER_KERNEL
:
140 if (stream
->mmap_base
!= NULL
) {
141 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
147 case LTTNG_CONSUMER32_UST
:
148 case LTTNG_CONSUMER64_UST
:
149 lttng_ustconsumer_del_stream(stream
);
152 ERR("Unknown consumer_data type");
159 /* Get stream node from hash table */
160 lttng_ht_lookup(consumer_data
.stream_ht
,
161 (void *)((unsigned long) stream
->key
), &iter
);
162 /* Remove stream node from hash table */
163 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
168 if (consumer_data
.stream_count
<= 0) {
171 consumer_data
.stream_count
--;
175 if (stream
->out_fd
>= 0) {
176 close(stream
->out_fd
);
178 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
179 close(stream
->wait_fd
);
181 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
182 close(stream
->shm_fd
);
184 if (!--stream
->chan
->refcount
)
185 free_chan
= stream
->chan
;
188 consumer_data
.need_update
= 1;
189 pthread_mutex_unlock(&consumer_data
.lock
);
192 consumer_del_channel(free_chan
);
195 static void consumer_del_stream_rcu(struct rcu_head
*head
)
197 struct lttng_ht_node_ulong
*node
=
198 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
199 struct lttng_consumer_stream
*stream
=
200 caa_container_of(node
, struct lttng_consumer_stream
, node
);
202 consumer_del_stream(stream
);
205 struct lttng_consumer_stream
*consumer_allocate_stream(
206 int channel_key
, int stream_key
,
207 int shm_fd
, int wait_fd
,
208 enum lttng_consumer_stream_state state
,
210 enum lttng_event_output output
,
211 const char *path_name
,
215 struct lttng_consumer_stream
*stream
;
218 stream
= zmalloc(sizeof(*stream
));
219 if (stream
== NULL
) {
220 perror("malloc struct lttng_consumer_stream");
223 stream
->chan
= consumer_find_channel(channel_key
);
225 perror("Unable to find channel key");
228 stream
->chan
->refcount
++;
229 stream
->key
= stream_key
;
230 stream
->shm_fd
= shm_fd
;
231 stream
->wait_fd
= wait_fd
;
233 stream
->out_fd_offset
= 0;
234 stream
->state
= state
;
235 stream
->mmap_len
= mmap_len
;
236 stream
->mmap_base
= NULL
;
237 stream
->output
= output
;
240 strncpy(stream
->path_name
, path_name
, PATH_MAX
- 1);
241 stream
->path_name
[PATH_MAX
- 1] = '\0';
242 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
244 switch (consumer_data
.type
) {
245 case LTTNG_CONSUMER_KERNEL
:
247 case LTTNG_CONSUMER32_UST
:
248 case LTTNG_CONSUMER64_UST
:
249 stream
->cpu
= stream
->chan
->cpucount
++;
250 ret
= lttng_ustconsumer_allocate_stream(stream
);
257 ERR("Unknown consumer_data type");
261 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
262 stream
->path_name
, stream
->key
,
265 (unsigned long long) stream
->mmap_len
,
272 * Add a stream to the global list protected by a mutex.
274 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
278 pthread_mutex_lock(&consumer_data
.lock
);
279 /* Steal stream identifier, for UST */
280 consumer_steal_stream_key(stream
->key
);
282 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
284 consumer_data
.stream_count
++;
285 consumer_data
.need_update
= 1;
287 switch (consumer_data
.type
) {
288 case LTTNG_CONSUMER_KERNEL
:
290 case LTTNG_CONSUMER32_UST
:
291 case LTTNG_CONSUMER64_UST
:
292 /* Streams are in CPU number order (we rely on this) */
293 stream
->cpu
= stream
->chan
->nr_streams
++;
296 ERR("Unknown consumer_data type");
302 pthread_mutex_unlock(&consumer_data
.lock
);
307 * Update a stream according to what we just received.
309 void consumer_change_stream_state(int stream_key
,
310 enum lttng_consumer_stream_state state
)
312 struct lttng_consumer_stream
*stream
;
314 pthread_mutex_lock(&consumer_data
.lock
);
315 stream
= consumer_find_stream(stream_key
);
317 stream
->state
= state
;
319 consumer_data
.need_update
= 1;
320 pthread_mutex_unlock(&consumer_data
.lock
);
324 * Remove a channel from the global list protected by a mutex. This
325 * function is also responsible for freeing its data structures.
327 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
330 struct lttng_ht_iter iter
;
332 pthread_mutex_lock(&consumer_data
.lock
);
334 switch (consumer_data
.type
) {
335 case LTTNG_CONSUMER_KERNEL
:
337 case LTTNG_CONSUMER32_UST
:
338 case LTTNG_CONSUMER64_UST
:
339 lttng_ustconsumer_del_channel(channel
);
342 ERR("Unknown consumer_data type");
349 lttng_ht_lookup(consumer_data
.channel_ht
,
350 (void *)((unsigned long) channel
->key
), &iter
);
351 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
356 if (channel
->mmap_base
!= NULL
) {
357 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
362 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
363 close(channel
->wait_fd
);
365 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
366 close(channel
->shm_fd
);
370 pthread_mutex_unlock(&consumer_data
.lock
);
373 static void consumer_del_channel_rcu(struct rcu_head
*head
)
375 struct lttng_ht_node_ulong
*node
=
376 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
377 struct lttng_consumer_channel
*channel
=
378 caa_container_of(node
, struct lttng_consumer_channel
, node
);
380 consumer_del_channel(channel
);
383 struct lttng_consumer_channel
*consumer_allocate_channel(
385 int shm_fd
, int wait_fd
,
387 uint64_t max_sb_size
)
389 struct lttng_consumer_channel
*channel
;
392 channel
= zmalloc(sizeof(*channel
));
393 if (channel
== NULL
) {
394 perror("malloc struct lttng_consumer_channel");
397 channel
->key
= channel_key
;
398 channel
->shm_fd
= shm_fd
;
399 channel
->wait_fd
= wait_fd
;
400 channel
->mmap_len
= mmap_len
;
401 channel
->max_sb_size
= max_sb_size
;
402 channel
->refcount
= 0;
403 channel
->nr_streams
= 0;
404 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
406 switch (consumer_data
.type
) {
407 case LTTNG_CONSUMER_KERNEL
:
408 channel
->mmap_base
= NULL
;
409 channel
->mmap_len
= 0;
411 case LTTNG_CONSUMER32_UST
:
412 case LTTNG_CONSUMER64_UST
:
413 ret
= lttng_ustconsumer_allocate_channel(channel
);
420 ERR("Unknown consumer_data type");
424 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
428 (unsigned long long) channel
->mmap_len
,
429 (unsigned long long) channel
->max_sb_size
);
435 * Add a channel to the global list protected by a mutex.
437 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
439 pthread_mutex_lock(&consumer_data
.lock
);
440 /* Steal channel identifier, for UST */
441 consumer_steal_channel_key(channel
->key
);
443 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
445 pthread_mutex_unlock(&consumer_data
.lock
);
450 * Allocate the pollfd structure and the local view of the out fds to avoid
451 * doing a lookup in the linked list and concurrency issues when writing is
452 * needed. Called with consumer_data.lock held.
454 * Returns the number of fds in the structures.
456 int consumer_update_poll_array(
457 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
458 struct lttng_consumer_stream
**local_stream
)
461 struct lttng_ht_iter iter
;
462 struct lttng_consumer_stream
*stream
;
464 DBG("Updating poll fd array");
465 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
467 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
470 DBG("Active FD %d", stream
->wait_fd
);
471 (*pollfd
)[i
].fd
= stream
->wait_fd
;
472 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
473 local_stream
[i
] = stream
;
478 * Insert the consumer_poll_pipe at the end of the array and don't
479 * increment i so nb_fd is the number of real FD.
481 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
482 (*pollfd
)[i
].events
= POLLIN
;
487 * Poll on the should_quit pipe and the command socket return -1 on error and
488 * should exit, 0 if data is available on the command socket
490 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
494 num_rdy
= poll(consumer_sockpoll
, 2, -1);
496 perror("Poll error");
499 if (consumer_sockpoll
[0].revents
== POLLIN
) {
500 DBG("consumer_should_quit wake up");
510 * Set the error socket.
512 void lttng_consumer_set_error_sock(
513 struct lttng_consumer_local_data
*ctx
, int sock
)
515 ctx
->consumer_error_socket
= sock
;
519 * Set the command socket path.
522 void lttng_consumer_set_command_sock_path(
523 struct lttng_consumer_local_data
*ctx
, char *sock
)
525 ctx
->consumer_command_sock_path
= sock
;
529 * Send return code to the session daemon.
530 * If the socket is not defined, we return 0, it is not a fatal error
532 int lttng_consumer_send_error(
533 struct lttng_consumer_local_data
*ctx
, int cmd
)
535 if (ctx
->consumer_error_socket
> 0) {
536 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
537 sizeof(enum lttcomm_sessiond_command
));
544 * Close all the tracefiles and stream fds, should be called when all instances
547 void lttng_consumer_cleanup(void)
550 struct lttng_ht_iter iter
;
551 struct lttng_ht_node_ulong
*node
;
556 * close all outfd. Called when there are no more threads running (after
557 * joining on the threads), no need to protect list iteration with mutex.
559 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
561 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
563 call_rcu(&node
->head
, consumer_del_stream_rcu
);
566 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
568 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
570 call_rcu(&node
->head
, consumer_del_channel_rcu
);
577 * Called from signal handler.
579 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
583 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
585 perror("write consumer quit");
589 void lttng_consumer_sync_trace_file(
590 struct lttng_consumer_stream
*stream
, off_t orig_offset
)
592 int outfd
= stream
->out_fd
;
595 * This does a blocking write-and-wait on any page that belongs to the
596 * subbuffer prior to the one we just wrote.
597 * Don't care about error values, as these are just hints and ways to
598 * limit the amount of page cache used.
600 if (orig_offset
< stream
->chan
->max_sb_size
) {
603 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
604 stream
->chan
->max_sb_size
,
605 SYNC_FILE_RANGE_WAIT_BEFORE
606 | SYNC_FILE_RANGE_WRITE
607 | SYNC_FILE_RANGE_WAIT_AFTER
);
609 * Give hints to the kernel about how we access the file:
610 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
613 * We need to call fadvise again after the file grows because the
614 * kernel does not seem to apply fadvise to non-existing parts of the
617 * Call fadvise _after_ having waited for the page writeback to
618 * complete because the dirty page writeback semantic is not well
619 * defined. So it can be expected to lead to lower throughput in
622 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
623 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
627 * Initialise the necessary environnement :
628 * - create a new context
629 * - create the poll_pipe
630 * - create the should_quit pipe (for signal handler)
631 * - create the thread pipe (for splice)
633 * Takes a function pointer as argument, this function is called when data is
634 * available on a buffer. This function is responsible to do the
635 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
636 * buffer configuration and then kernctl_put_next_subbuf at the end.
638 * Returns a pointer to the new context or NULL on error.
640 struct lttng_consumer_local_data
*lttng_consumer_create(
641 enum lttng_consumer_type type
,
642 int (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
643 struct lttng_consumer_local_data
*ctx
),
644 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
645 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
646 int (*update_stream
)(int stream_key
, uint32_t state
))
649 struct lttng_consumer_local_data
*ctx
;
651 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
652 consumer_data
.type
== type
);
653 consumer_data
.type
= type
;
655 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
657 perror("allocating context");
661 ctx
->consumer_error_socket
= -1;
662 /* assign the callbacks */
663 ctx
->on_buffer_ready
= buffer_ready
;
664 ctx
->on_recv_channel
= recv_channel
;
665 ctx
->on_recv_stream
= recv_stream
;
666 ctx
->on_update_stream
= update_stream
;
668 ret
= pipe(ctx
->consumer_poll_pipe
);
670 perror("Error creating poll pipe");
671 goto error_poll_pipe
;
674 ret
= pipe(ctx
->consumer_should_quit
);
676 perror("Error creating recv pipe");
677 goto error_quit_pipe
;
680 ret
= pipe(ctx
->consumer_thread_pipe
);
682 perror("Error creating thread pipe");
683 goto error_thread_pipe
;
690 for (i
= 0; i
< 2; i
++) {
693 err
= close(ctx
->consumer_should_quit
[i
]);
697 for (i
= 0; i
< 2; i
++) {
700 err
= close(ctx
->consumer_poll_pipe
[i
]);
710 * Close all fds associated with the instance and free the context.
712 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
714 close(ctx
->consumer_error_socket
);
715 close(ctx
->consumer_thread_pipe
[0]);
716 close(ctx
->consumer_thread_pipe
[1]);
717 close(ctx
->consumer_poll_pipe
[0]);
718 close(ctx
->consumer_poll_pipe
[1]);
719 close(ctx
->consumer_should_quit
[0]);
720 close(ctx
->consumer_should_quit
[1]);
721 unlink(ctx
->consumer_command_sock_path
);
726 * Mmap the ring buffer, read it and write the data to the tracefile.
728 * Returns the number of bytes written
730 int lttng_consumer_on_read_subbuffer_mmap(
731 struct lttng_consumer_local_data
*ctx
,
732 struct lttng_consumer_stream
*stream
, unsigned long len
)
734 switch (consumer_data
.type
) {
735 case LTTNG_CONSUMER_KERNEL
:
736 return lttng_kconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
737 case LTTNG_CONSUMER32_UST
:
738 case LTTNG_CONSUMER64_UST
:
739 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
741 ERR("Unknown consumer_data type");
749 * Splice the data from the ring buffer to the tracefile.
751 * Returns the number of bytes spliced.
753 int lttng_consumer_on_read_subbuffer_splice(
754 struct lttng_consumer_local_data
*ctx
,
755 struct lttng_consumer_stream
*stream
, unsigned long len
)
757 switch (consumer_data
.type
) {
758 case LTTNG_CONSUMER_KERNEL
:
759 return lttng_kconsumer_on_read_subbuffer_splice(ctx
, stream
, len
);
760 case LTTNG_CONSUMER32_UST
:
761 case LTTNG_CONSUMER64_UST
:
764 ERR("Unknown consumer_data type");
772 * Take a snapshot for a specific fd
774 * Returns 0 on success, < 0 on error
776 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
777 struct lttng_consumer_stream
*stream
)
779 switch (consumer_data
.type
) {
780 case LTTNG_CONSUMER_KERNEL
:
781 return lttng_kconsumer_take_snapshot(ctx
, stream
);
782 case LTTNG_CONSUMER32_UST
:
783 case LTTNG_CONSUMER64_UST
:
784 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
786 ERR("Unknown consumer_data type");
794 * Get the produced position
796 * Returns 0 on success, < 0 on error
798 int lttng_consumer_get_produced_snapshot(
799 struct lttng_consumer_local_data
*ctx
,
800 struct lttng_consumer_stream
*stream
,
803 switch (consumer_data
.type
) {
804 case LTTNG_CONSUMER_KERNEL
:
805 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
806 case LTTNG_CONSUMER32_UST
:
807 case LTTNG_CONSUMER64_UST
:
808 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
810 ERR("Unknown consumer_data type");
816 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
817 int sock
, struct pollfd
*consumer_sockpoll
)
819 switch (consumer_data
.type
) {
820 case LTTNG_CONSUMER_KERNEL
:
821 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
822 case LTTNG_CONSUMER32_UST
:
823 case LTTNG_CONSUMER64_UST
:
824 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
826 ERR("Unknown consumer_data type");
833 * This thread polls the fds in the set to consume the data and write
834 * it to tracefile if necessary.
836 void *lttng_consumer_thread_poll_fds(void *data
)
838 int num_rdy
, num_hup
, high_prio
, ret
, i
;
839 struct pollfd
*pollfd
= NULL
;
840 /* local view of the streams */
841 struct lttng_consumer_stream
**local_stream
= NULL
;
842 /* local view of consumer_data.fds_count */
846 struct lttng_consumer_local_data
*ctx
= data
;
848 rcu_register_thread();
850 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
857 * the fds set has been updated, we need to update our
858 * local array as well
860 pthread_mutex_lock(&consumer_data
.lock
);
861 if (consumer_data
.need_update
) {
862 if (pollfd
!= NULL
) {
866 if (local_stream
!= NULL
) {
871 /* allocate for all fds + 1 for the consumer_poll_pipe */
872 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
873 if (pollfd
== NULL
) {
874 perror("pollfd malloc");
875 pthread_mutex_unlock(&consumer_data
.lock
);
879 /* allocate for all fds + 1 for the consumer_poll_pipe */
880 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
881 sizeof(struct lttng_consumer_stream
));
882 if (local_stream
== NULL
) {
883 perror("local_stream malloc");
884 pthread_mutex_unlock(&consumer_data
.lock
);
887 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
889 ERR("Error in allocating pollfd or local_outfds");
890 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
891 pthread_mutex_unlock(&consumer_data
.lock
);
895 consumer_data
.need_update
= 0;
897 pthread_mutex_unlock(&consumer_data
.lock
);
899 /* poll on the array of fds */
900 DBG("polling on %d fd", nb_fd
+ 1);
901 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
902 DBG("poll num_rdy : %d", num_rdy
);
904 perror("Poll error");
905 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
907 } else if (num_rdy
== 0) {
908 DBG("Polling thread timed out");
912 /* No FDs and consumer_quit, consumer_cleanup the thread */
913 if (nb_fd
== 0 && consumer_quit
== 1) {
918 * If the consumer_poll_pipe triggered poll go
919 * directly to the beginning of the loop to update the
920 * array. We want to prioritize array update over
921 * low-priority reads.
923 if (pollfd
[nb_fd
].revents
& POLLIN
) {
924 DBG("consumer_poll_pipe wake up");
925 tmp2
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
927 perror("read consumer poll");
932 /* Take care of high priority channels first. */
933 for (i
= 0; i
< nb_fd
; i
++) {
934 if (pollfd
[i
].revents
& POLLPRI
) {
935 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
937 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
938 /* it's ok to have an unavailable sub-buffer */
942 } else if (pollfd
[i
].revents
& POLLERR
) {
943 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
945 consumer_del_stream_rcu(&local_stream
[i
]->node
.head
);
948 } else if (pollfd
[i
].revents
& POLLNVAL
) {
949 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
951 consumer_del_stream_rcu(&local_stream
[i
]->node
.head
);
954 } else if ((pollfd
[i
].revents
& POLLHUP
) &&
955 !(pollfd
[i
].revents
& POLLIN
)) {
956 if (consumer_data
.type
== LTTNG_CONSUMER32_UST
957 || consumer_data
.type
== LTTNG_CONSUMER64_UST
) {
958 DBG("Polling fd %d tells it has hung up. Attempting flush and read.",
960 if (!local_stream
[i
]->hangup_flush_done
) {
961 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
962 /* read after flush */
964 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
965 } while (ret
== EAGAIN
);
968 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
971 consumer_del_stream_rcu(&local_stream
[i
]->node
.head
);
977 /* If every buffer FD has hung up, we end the read loop here */
978 if (nb_fd
> 0 && num_hup
== nb_fd
) {
979 DBG("every buffer FD has hung up\n");
980 if (consumer_quit
== 1) {
986 /* Take care of low priority channels. */
987 if (high_prio
== 0) {
988 for (i
= 0; i
< nb_fd
; i
++) {
989 if (pollfd
[i
].revents
& POLLIN
) {
990 DBG("Normal read on fd %d", pollfd
[i
].fd
);
991 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
992 /* it's ok to have an unavailable subbuffer */
1001 DBG("polling thread exiting");
1002 if (pollfd
!= NULL
) {
1006 if (local_stream
!= NULL
) {
1008 local_stream
= NULL
;
1010 rcu_unregister_thread();
1015 * This thread listens on the consumerd socket and receives the file
1016 * descriptors from the session daemon.
1018 void *lttng_consumer_thread_receive_fds(void *data
)
1020 int sock
, client_socket
, ret
;
1022 * structure to poll for incoming data on communication socket avoids
1023 * making blocking sockets.
1025 struct pollfd consumer_sockpoll
[2];
1026 struct lttng_consumer_local_data
*ctx
= data
;
1028 rcu_register_thread();
1030 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
1031 unlink(ctx
->consumer_command_sock_path
);
1032 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
1033 if (client_socket
< 0) {
1034 ERR("Cannot create command socket");
1038 ret
= lttcomm_listen_unix_sock(client_socket
);
1043 DBG("Sending ready command to lttng-sessiond");
1044 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
1045 /* return < 0 on error, but == 0 is not fatal */
1047 ERR("Error sending ready command to lttng-sessiond");
1051 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1053 perror("fcntl O_NONBLOCK");
1057 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1058 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1059 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1060 consumer_sockpoll
[1].fd
= client_socket
;
1061 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1063 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1066 DBG("Connection on client_socket");
1068 /* Blocking call, waiting for transmission */
1069 sock
= lttcomm_accept_unix_sock(client_socket
);
1074 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1076 perror("fcntl O_NONBLOCK");
1080 /* update the polling structure to poll on the established socket */
1081 consumer_sockpoll
[1].fd
= sock
;
1082 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1085 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1088 DBG("Incoming command on sock");
1089 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1090 if (ret
== -ENOENT
) {
1091 DBG("Received STOP command");
1095 ERR("Communication interrupted on command socket");
1098 if (consumer_quit
) {
1099 DBG("consumer_thread_receive_fds received quit from signal");
1102 DBG("received fds on sock");
1105 DBG("consumer_thread_receive_fds exiting");
1108 * when all fds have hung up, the polling thread
1114 * 2s of grace period, if no polling events occur during
1115 * this period, the polling thread will exit even if there
1116 * are still open FDs (should not happen, but safety mechanism).
1118 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1120 /* wake up the polling thread */
1121 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
1123 perror("poll pipe write");
1125 rcu_unregister_thread();
1129 int lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1130 struct lttng_consumer_local_data
*ctx
)
1132 switch (consumer_data
.type
) {
1133 case LTTNG_CONSUMER_KERNEL
:
1134 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1135 case LTTNG_CONSUMER32_UST
:
1136 case LTTNG_CONSUMER64_UST
:
1137 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1139 ERR("Unknown consumer_data type");
1145 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1147 switch (consumer_data
.type
) {
1148 case LTTNG_CONSUMER_KERNEL
:
1149 return lttng_kconsumer_on_recv_stream(stream
);
1150 case LTTNG_CONSUMER32_UST
:
1151 case LTTNG_CONSUMER64_UST
:
1152 return lttng_ustconsumer_on_recv_stream(stream
);
1154 ERR("Unknown consumer_data type");
1161 * Allocate and set consumer data hash tables.
1163 void lttng_consumer_init(void)
1165 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1166 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);