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
)
495 num_rdy
= poll(consumer_sockpoll
, 2, -1);
497 perror("Poll error");
500 if (consumer_sockpoll
[0].revents
== POLLIN
) {
501 DBG("consumer_should_quit wake up");
511 * Set the error socket.
513 void lttng_consumer_set_error_sock(
514 struct lttng_consumer_local_data
*ctx
, int sock
)
516 ctx
->consumer_error_socket
= sock
;
520 * Set the command socket path.
523 void lttng_consumer_set_command_sock_path(
524 struct lttng_consumer_local_data
*ctx
, char *sock
)
526 ctx
->consumer_command_sock_path
= sock
;
530 * Send return code to the session daemon.
531 * If the socket is not defined, we return 0, it is not a fatal error
533 int lttng_consumer_send_error(
534 struct lttng_consumer_local_data
*ctx
, int cmd
)
536 if (ctx
->consumer_error_socket
> 0) {
537 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
538 sizeof(enum lttcomm_sessiond_command
));
545 * Close all the tracefiles and stream fds, should be called when all instances
548 void lttng_consumer_cleanup(void)
551 struct lttng_ht_iter iter
;
552 struct lttng_ht_node_ulong
*node
;
557 * close all outfd. Called when there are no more threads running (after
558 * joining on the threads), no need to protect list iteration with mutex.
560 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
562 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
564 call_rcu(&node
->head
, consumer_del_stream_rcu
);
567 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
569 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
571 call_rcu(&node
->head
, consumer_del_channel_rcu
);
578 * Called from signal handler.
580 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
584 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
586 perror("write consumer quit");
590 void lttng_consumer_sync_trace_file(
591 struct lttng_consumer_stream
*stream
, off_t orig_offset
)
593 int outfd
= stream
->out_fd
;
596 * This does a blocking write-and-wait on any page that belongs to the
597 * subbuffer prior to the one we just wrote.
598 * Don't care about error values, as these are just hints and ways to
599 * limit the amount of page cache used.
601 if (orig_offset
< stream
->chan
->max_sb_size
) {
604 sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
605 stream
->chan
->max_sb_size
,
606 SYNC_FILE_RANGE_WAIT_BEFORE
607 | SYNC_FILE_RANGE_WRITE
608 | SYNC_FILE_RANGE_WAIT_AFTER
);
610 * Give hints to the kernel about how we access the file:
611 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
614 * We need to call fadvise again after the file grows because the
615 * kernel does not seem to apply fadvise to non-existing parts of the
618 * Call fadvise _after_ having waited for the page writeback to
619 * complete because the dirty page writeback semantic is not well
620 * defined. So it can be expected to lead to lower throughput in
623 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
624 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
628 * Initialise the necessary environnement :
629 * - create a new context
630 * - create the poll_pipe
631 * - create the should_quit pipe (for signal handler)
632 * - create the thread pipe (for splice)
634 * Takes a function pointer as argument, this function is called when data is
635 * available on a buffer. This function is responsible to do the
636 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
637 * buffer configuration and then kernctl_put_next_subbuf at the end.
639 * Returns a pointer to the new context or NULL on error.
641 struct lttng_consumer_local_data
*lttng_consumer_create(
642 enum lttng_consumer_type type
,
643 int (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
644 struct lttng_consumer_local_data
*ctx
),
645 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
646 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
647 int (*update_stream
)(int stream_key
, uint32_t state
))
650 struct lttng_consumer_local_data
*ctx
;
652 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
653 consumer_data
.type
== type
);
654 consumer_data
.type
= type
;
656 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
658 perror("allocating context");
662 ctx
->consumer_error_socket
= -1;
663 /* assign the callbacks */
664 ctx
->on_buffer_ready
= buffer_ready
;
665 ctx
->on_recv_channel
= recv_channel
;
666 ctx
->on_recv_stream
= recv_stream
;
667 ctx
->on_update_stream
= update_stream
;
669 ret
= pipe(ctx
->consumer_poll_pipe
);
671 perror("Error creating poll pipe");
672 goto error_poll_pipe
;
675 ret
= pipe(ctx
->consumer_should_quit
);
677 perror("Error creating recv pipe");
678 goto error_quit_pipe
;
681 ret
= pipe(ctx
->consumer_thread_pipe
);
683 perror("Error creating thread pipe");
684 goto error_thread_pipe
;
691 for (i
= 0; i
< 2; i
++) {
694 err
= close(ctx
->consumer_should_quit
[i
]);
698 for (i
= 0; i
< 2; i
++) {
701 err
= close(ctx
->consumer_poll_pipe
[i
]);
711 * Close all fds associated with the instance and free the context.
713 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
715 close(ctx
->consumer_error_socket
);
716 close(ctx
->consumer_thread_pipe
[0]);
717 close(ctx
->consumer_thread_pipe
[1]);
718 close(ctx
->consumer_poll_pipe
[0]);
719 close(ctx
->consumer_poll_pipe
[1]);
720 close(ctx
->consumer_should_quit
[0]);
721 close(ctx
->consumer_should_quit
[1]);
722 unlink(ctx
->consumer_command_sock_path
);
727 * Mmap the ring buffer, read it and write the data to the tracefile.
729 * Returns the number of bytes written
731 int lttng_consumer_on_read_subbuffer_mmap(
732 struct lttng_consumer_local_data
*ctx
,
733 struct lttng_consumer_stream
*stream
, unsigned long len
)
735 switch (consumer_data
.type
) {
736 case LTTNG_CONSUMER_KERNEL
:
737 return lttng_kconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
738 case LTTNG_CONSUMER32_UST
:
739 case LTTNG_CONSUMER64_UST
:
740 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
742 ERR("Unknown consumer_data type");
748 * Splice the data from the ring buffer to the tracefile.
750 * Returns the number of bytes spliced.
752 int lttng_consumer_on_read_subbuffer_splice(
753 struct lttng_consumer_local_data
*ctx
,
754 struct lttng_consumer_stream
*stream
, unsigned long len
)
756 switch (consumer_data
.type
) {
757 case LTTNG_CONSUMER_KERNEL
:
758 return lttng_kconsumer_on_read_subbuffer_splice(ctx
, stream
, len
);
759 case LTTNG_CONSUMER32_UST
:
760 case LTTNG_CONSUMER64_UST
:
763 ERR("Unknown consumer_data type");
771 * Take a snapshot for a specific fd
773 * Returns 0 on success, < 0 on error
775 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
776 struct lttng_consumer_stream
*stream
)
778 switch (consumer_data
.type
) {
779 case LTTNG_CONSUMER_KERNEL
:
780 return lttng_kconsumer_take_snapshot(ctx
, stream
);
781 case LTTNG_CONSUMER32_UST
:
782 case LTTNG_CONSUMER64_UST
:
783 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
785 ERR("Unknown consumer_data type");
793 * Get the produced position
795 * Returns 0 on success, < 0 on error
797 int lttng_consumer_get_produced_snapshot(
798 struct lttng_consumer_local_data
*ctx
,
799 struct lttng_consumer_stream
*stream
,
802 switch (consumer_data
.type
) {
803 case LTTNG_CONSUMER_KERNEL
:
804 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
805 case LTTNG_CONSUMER32_UST
:
806 case LTTNG_CONSUMER64_UST
:
807 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
809 ERR("Unknown consumer_data type");
815 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
816 int sock
, struct pollfd
*consumer_sockpoll
)
818 switch (consumer_data
.type
) {
819 case LTTNG_CONSUMER_KERNEL
:
820 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
821 case LTTNG_CONSUMER32_UST
:
822 case LTTNG_CONSUMER64_UST
:
823 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
825 ERR("Unknown consumer_data type");
832 * This thread polls the fds in the set to consume the data and write
833 * it to tracefile if necessary.
835 void *lttng_consumer_thread_poll_fds(void *data
)
837 int num_rdy
, num_hup
, high_prio
, ret
, i
;
838 struct pollfd
*pollfd
= NULL
;
839 /* local view of the streams */
840 struct lttng_consumer_stream
**local_stream
= NULL
;
841 /* local view of consumer_data.fds_count */
845 struct lttng_consumer_local_data
*ctx
= data
;
847 rcu_register_thread();
849 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
856 * the fds set has been updated, we need to update our
857 * local array as well
859 pthread_mutex_lock(&consumer_data
.lock
);
860 if (consumer_data
.need_update
) {
861 if (pollfd
!= NULL
) {
865 if (local_stream
!= NULL
) {
870 /* allocate for all fds + 1 for the consumer_poll_pipe */
871 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
872 if (pollfd
== NULL
) {
873 perror("pollfd malloc");
874 pthread_mutex_unlock(&consumer_data
.lock
);
878 /* allocate for all fds + 1 for the consumer_poll_pipe */
879 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
880 sizeof(struct lttng_consumer_stream
));
881 if (local_stream
== NULL
) {
882 perror("local_stream malloc");
883 pthread_mutex_unlock(&consumer_data
.lock
);
886 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
888 ERR("Error in allocating pollfd or local_outfds");
889 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
890 pthread_mutex_unlock(&consumer_data
.lock
);
894 consumer_data
.need_update
= 0;
896 pthread_mutex_unlock(&consumer_data
.lock
);
898 /* poll on the array of fds */
899 DBG("polling on %d fd", nb_fd
+ 1);
900 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
901 DBG("poll num_rdy : %d", num_rdy
);
903 perror("Poll error");
904 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
906 } else if (num_rdy
== 0) {
907 DBG("Polling thread timed out");
911 /* No FDs and consumer_quit, consumer_cleanup the thread */
912 if (nb_fd
== 0 && consumer_quit
== 1) {
917 * If the consumer_poll_pipe triggered poll go
918 * directly to the beginning of the loop to update the
919 * array. We want to prioritize array update over
920 * low-priority reads.
922 if (pollfd
[nb_fd
].revents
& POLLIN
) {
923 DBG("consumer_poll_pipe wake up");
924 tmp2
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
926 perror("read consumer poll");
931 /* Take care of high priority channels first. */
932 for (i
= 0; i
< nb_fd
; i
++) {
933 if (pollfd
[i
].revents
& POLLPRI
) {
934 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
936 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
937 /* it's ok to have an unavailable sub-buffer */
941 } else if (pollfd
[i
].revents
& POLLERR
) {
942 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
944 consumer_del_stream_rcu(&local_stream
[i
]->node
.head
);
947 } else if (pollfd
[i
].revents
& POLLNVAL
) {
948 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
950 consumer_del_stream_rcu(&local_stream
[i
]->node
.head
);
953 } else if ((pollfd
[i
].revents
& POLLHUP
) &&
954 !(pollfd
[i
].revents
& POLLIN
)) {
955 if (consumer_data
.type
== LTTNG_CONSUMER32_UST
956 || consumer_data
.type
== LTTNG_CONSUMER64_UST
) {
957 DBG("Polling fd %d tells it has hung up. Attempting flush and read.",
959 if (!local_stream
[i
]->hangup_flush_done
) {
960 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
961 /* read after flush */
963 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
964 } while (ret
== EAGAIN
);
967 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
970 consumer_del_stream_rcu(&local_stream
[i
]->node
.head
);
976 /* If every buffer FD has hung up, we end the read loop here */
977 if (nb_fd
> 0 && num_hup
== nb_fd
) {
978 DBG("every buffer FD has hung up\n");
979 if (consumer_quit
== 1) {
985 /* Take care of low priority channels. */
986 if (high_prio
== 0) {
987 for (i
= 0; i
< nb_fd
; i
++) {
988 if (pollfd
[i
].revents
& POLLIN
) {
989 DBG("Normal read on fd %d", pollfd
[i
].fd
);
990 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
991 /* it's ok to have an unavailable subbuffer */
1000 DBG("polling thread exiting");
1001 if (pollfd
!= NULL
) {
1005 if (local_stream
!= NULL
) {
1007 local_stream
= NULL
;
1009 rcu_unregister_thread();
1014 * This thread listens on the consumerd socket and receives the file
1015 * descriptors from the session daemon.
1017 void *lttng_consumer_thread_receive_fds(void *data
)
1019 int sock
, client_socket
, ret
;
1021 * structure to poll for incoming data on communication socket avoids
1022 * making blocking sockets.
1024 struct pollfd consumer_sockpoll
[2];
1025 struct lttng_consumer_local_data
*ctx
= data
;
1027 rcu_register_thread();
1029 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
1030 unlink(ctx
->consumer_command_sock_path
);
1031 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
1032 if (client_socket
< 0) {
1033 ERR("Cannot create command socket");
1037 ret
= lttcomm_listen_unix_sock(client_socket
);
1042 DBG("Sending ready command to lttng-sessiond");
1043 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
1044 /* return < 0 on error, but == 0 is not fatal */
1046 ERR("Error sending ready command to lttng-sessiond");
1050 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1052 perror("fcntl O_NONBLOCK");
1056 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1057 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1058 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1059 consumer_sockpoll
[1].fd
= client_socket
;
1060 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1062 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1065 DBG("Connection on client_socket");
1067 /* Blocking call, waiting for transmission */
1068 sock
= lttcomm_accept_unix_sock(client_socket
);
1073 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1075 perror("fcntl O_NONBLOCK");
1079 /* update the polling structure to poll on the established socket */
1080 consumer_sockpoll
[1].fd
= sock
;
1081 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1084 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1087 DBG("Incoming command on sock");
1088 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1089 if (ret
== -ENOENT
) {
1090 DBG("Received STOP command");
1094 ERR("Communication interrupted on command socket");
1097 if (consumer_quit
) {
1098 DBG("consumer_thread_receive_fds received quit from signal");
1101 DBG("received fds on sock");
1104 DBG("consumer_thread_receive_fds exiting");
1107 * when all fds have hung up, the polling thread
1113 * 2s of grace period, if no polling events occur during
1114 * this period, the polling thread will exit even if there
1115 * are still open FDs (should not happen, but safety mechanism).
1117 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1119 /* wake up the polling thread */
1120 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
1122 perror("poll pipe write");
1124 rcu_unregister_thread();
1128 int lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1129 struct lttng_consumer_local_data
*ctx
)
1131 switch (consumer_data
.type
) {
1132 case LTTNG_CONSUMER_KERNEL
:
1133 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1134 case LTTNG_CONSUMER32_UST
:
1135 case LTTNG_CONSUMER64_UST
:
1136 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1138 ERR("Unknown consumer_data type");
1144 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1146 switch (consumer_data
.type
) {
1147 case LTTNG_CONSUMER_KERNEL
:
1148 return lttng_kconsumer_on_recv_stream(stream
);
1149 case LTTNG_CONSUMER32_UST
:
1150 case LTTNG_CONSUMER64_UST
:
1151 return lttng_ustconsumer_on_recv_stream(stream
);
1153 ERR("Unknown consumer_data type");
1160 * Allocate and set consumer data hash tables.
1162 void lttng_consumer_init(void)
1164 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1165 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);