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 void consumer_free_stream(struct rcu_head
*head
)
129 struct lttng_ht_node_ulong
*node
=
130 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
131 struct lttng_consumer_stream
*stream
=
132 caa_container_of(node
, struct lttng_consumer_stream
, node
);
138 * Remove a stream from the global list protected by a mutex. This
139 * function is also responsible for freeing its data structures.
141 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
144 struct lttng_ht_iter iter
;
145 struct lttng_consumer_channel
*free_chan
= NULL
;
147 pthread_mutex_lock(&consumer_data
.lock
);
149 switch (consumer_data
.type
) {
150 case LTTNG_CONSUMER_KERNEL
:
151 if (stream
->mmap_base
!= NULL
) {
152 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
158 case LTTNG_CONSUMER32_UST
:
159 case LTTNG_CONSUMER64_UST
:
160 lttng_ustconsumer_del_stream(stream
);
163 ERR("Unknown consumer_data type");
170 /* Get stream node from hash table */
171 lttng_ht_lookup(consumer_data
.stream_ht
,
172 (void *)((unsigned long) stream
->key
), &iter
);
174 * Remove stream node from hash table. It can fail if it's been
175 * replaced due to key reuse.
177 (void) lttng_ht_del(consumer_data
.stream_ht
, &iter
);
181 if (consumer_data
.stream_count
<= 0) {
184 consumer_data
.stream_count
--;
188 if (stream
->out_fd
>= 0) {
189 ret
= close(stream
->out_fd
);
194 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
195 ret
= close(stream
->wait_fd
);
200 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
201 ret
= close(stream
->shm_fd
);
206 if (!--stream
->chan
->refcount
)
207 free_chan
= stream
->chan
;
209 call_rcu(&stream
->node
.head
, consumer_free_stream
);
211 consumer_data
.need_update
= 1;
212 pthread_mutex_unlock(&consumer_data
.lock
);
215 consumer_del_channel(free_chan
);
218 struct lttng_consumer_stream
*consumer_allocate_stream(
219 int channel_key
, int stream_key
,
220 int shm_fd
, int wait_fd
,
221 enum lttng_consumer_stream_state state
,
223 enum lttng_event_output output
,
224 const char *path_name
,
228 struct lttng_consumer_stream
*stream
;
231 stream
= zmalloc(sizeof(*stream
));
232 if (stream
== NULL
) {
233 perror("malloc struct lttng_consumer_stream");
236 stream
->chan
= consumer_find_channel(channel_key
);
238 perror("Unable to find channel key");
241 stream
->chan
->refcount
++;
242 stream
->key
= stream_key
;
243 stream
->shm_fd
= shm_fd
;
244 stream
->wait_fd
= wait_fd
;
246 stream
->out_fd_offset
= 0;
247 stream
->state
= state
;
248 stream
->mmap_len
= mmap_len
;
249 stream
->mmap_base
= NULL
;
250 stream
->output
= output
;
253 strncpy(stream
->path_name
, path_name
, PATH_MAX
- 1);
254 stream
->path_name
[PATH_MAX
- 1] = '\0';
255 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
257 switch (consumer_data
.type
) {
258 case LTTNG_CONSUMER_KERNEL
:
260 case LTTNG_CONSUMER32_UST
:
261 case LTTNG_CONSUMER64_UST
:
262 stream
->cpu
= stream
->chan
->cpucount
++;
263 ret
= lttng_ustconsumer_allocate_stream(stream
);
270 ERR("Unknown consumer_data type");
274 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
275 stream
->path_name
, stream
->key
,
278 (unsigned long long) stream
->mmap_len
,
285 * Add a stream to the global list protected by a mutex.
287 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
291 pthread_mutex_lock(&consumer_data
.lock
);
292 /* Steal stream identifier, for UST */
293 consumer_steal_stream_key(stream
->key
);
296 * We simply remove the old channel from the hash table. It's
297 * ok, since we know for sure the sessiond wants to replace it
298 * with the new version, because the key has been reused.
300 (void) lttng_ht_add_replace_ulong(consumer_data
.stream_ht
, &stream
->node
);
302 consumer_data
.stream_count
++;
303 consumer_data
.need_update
= 1;
305 switch (consumer_data
.type
) {
306 case LTTNG_CONSUMER_KERNEL
:
308 case LTTNG_CONSUMER32_UST
:
309 case LTTNG_CONSUMER64_UST
:
310 /* Streams are in CPU number order (we rely on this) */
311 stream
->cpu
= stream
->chan
->nr_streams
++;
314 ERR("Unknown consumer_data type");
320 pthread_mutex_unlock(&consumer_data
.lock
);
326 * Update a stream according to what we just received.
328 void consumer_change_stream_state(int stream_key
,
329 enum lttng_consumer_stream_state state
)
331 struct lttng_consumer_stream
*stream
;
333 pthread_mutex_lock(&consumer_data
.lock
);
334 stream
= consumer_find_stream(stream_key
);
336 stream
->state
= state
;
338 consumer_data
.need_update
= 1;
339 pthread_mutex_unlock(&consumer_data
.lock
);
343 void consumer_free_channel(struct rcu_head
*head
)
345 struct lttng_ht_node_ulong
*node
=
346 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
347 struct lttng_consumer_channel
*channel
=
348 caa_container_of(node
, struct lttng_consumer_channel
, node
);
354 * Remove a channel from the global list protected by a mutex. This
355 * function is also responsible for freeing its data structures.
357 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
360 struct lttng_ht_iter iter
;
362 pthread_mutex_lock(&consumer_data
.lock
);
364 switch (consumer_data
.type
) {
365 case LTTNG_CONSUMER_KERNEL
:
367 case LTTNG_CONSUMER32_UST
:
368 case LTTNG_CONSUMER64_UST
:
369 lttng_ustconsumer_del_channel(channel
);
372 ERR("Unknown consumer_data type");
379 lttng_ht_lookup(consumer_data
.channel_ht
,
380 (void *)((unsigned long) channel
->key
), &iter
);
383 * Remove channel node from hash table. It can fail if it's been
384 * replaced due to key reuse.
386 (void) 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
);
475 * We simply remove the old channel from the hash table. It's
476 * ok, since we know for sure the sessiond wants to replace it
477 * with the new version, because the key has been reused.
479 (void) lttng_ht_add_replace_ulong(consumer_data
.channel_ht
, &channel
->node
);
481 pthread_mutex_unlock(&consumer_data
.lock
);
487 * Allocate the pollfd structure and the local view of the out fds to avoid
488 * doing a lookup in the linked list and concurrency issues when writing is
489 * needed. Called with consumer_data.lock held.
491 * Returns the number of fds in the structures.
493 int consumer_update_poll_array(
494 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
495 struct lttng_consumer_stream
**local_stream
)
498 struct lttng_ht_iter iter
;
499 struct lttng_consumer_stream
*stream
;
501 DBG("Updating poll fd array");
502 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
504 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
507 DBG("Active FD %d", stream
->wait_fd
);
508 (*pollfd
)[i
].fd
= stream
->wait_fd
;
509 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
510 local_stream
[i
] = stream
;
515 * Insert the consumer_poll_pipe at the end of the array and don't
516 * increment i so nb_fd is the number of real FD.
518 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
519 (*pollfd
)[i
].events
= POLLIN
;
524 * Poll on the should_quit pipe and the command socket return -1 on error and
525 * should exit, 0 if data is available on the command socket
527 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
532 num_rdy
= poll(consumer_sockpoll
, 2, -1);
535 * Restart interrupted system call.
537 if (errno
== EINTR
) {
540 perror("Poll error");
543 if (consumer_sockpoll
[0].revents
== POLLIN
) {
544 DBG("consumer_should_quit wake up");
554 * Set the error socket.
556 void lttng_consumer_set_error_sock(
557 struct lttng_consumer_local_data
*ctx
, int sock
)
559 ctx
->consumer_error_socket
= sock
;
563 * Set the command socket path.
566 void lttng_consumer_set_command_sock_path(
567 struct lttng_consumer_local_data
*ctx
, char *sock
)
569 ctx
->consumer_command_sock_path
= sock
;
573 * Send return code to the session daemon.
574 * If the socket is not defined, we return 0, it is not a fatal error
576 int lttng_consumer_send_error(
577 struct lttng_consumer_local_data
*ctx
, int cmd
)
579 if (ctx
->consumer_error_socket
> 0) {
580 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
581 sizeof(enum lttcomm_sessiond_command
));
588 * Close all the tracefiles and stream fds, should be called when all instances
591 void lttng_consumer_cleanup(void)
593 struct lttng_ht_iter iter
;
594 struct lttng_ht_node_ulong
*node
;
599 * close all outfd. Called when there are no more threads running (after
600 * joining on the threads), no need to protect list iteration with mutex.
602 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
604 struct lttng_consumer_stream
*stream
=
605 caa_container_of(node
, struct lttng_consumer_stream
, node
);
606 consumer_del_stream(stream
);
609 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
611 struct lttng_consumer_channel
*channel
=
612 caa_container_of(node
, struct lttng_consumer_channel
, node
);
613 consumer_del_channel(channel
);
620 * Called from signal handler.
622 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
626 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
628 perror("write consumer quit");
632 void lttng_consumer_sync_trace_file(
633 struct lttng_consumer_stream
*stream
, off_t orig_offset
)
635 int outfd
= stream
->out_fd
;
638 * This does a blocking write-and-wait on any page that belongs to the
639 * subbuffer prior to the one we just wrote.
640 * Don't care about error values, as these are just hints and ways to
641 * limit the amount of page cache used.
643 if (orig_offset
< stream
->chan
->max_sb_size
) {
646 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
647 stream
->chan
->max_sb_size
,
648 SYNC_FILE_RANGE_WAIT_BEFORE
649 | SYNC_FILE_RANGE_WRITE
650 | SYNC_FILE_RANGE_WAIT_AFTER
);
652 * Give hints to the kernel about how we access the file:
653 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
656 * We need to call fadvise again after the file grows because the
657 * kernel does not seem to apply fadvise to non-existing parts of the
660 * Call fadvise _after_ having waited for the page writeback to
661 * complete because the dirty page writeback semantic is not well
662 * defined. So it can be expected to lead to lower throughput in
665 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
666 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
670 * Initialise the necessary environnement :
671 * - create a new context
672 * - create the poll_pipe
673 * - create the should_quit pipe (for signal handler)
674 * - create the thread pipe (for splice)
676 * Takes a function pointer as argument, this function is called when data is
677 * available on a buffer. This function is responsible to do the
678 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
679 * buffer configuration and then kernctl_put_next_subbuf at the end.
681 * Returns a pointer to the new context or NULL on error.
683 struct lttng_consumer_local_data
*lttng_consumer_create(
684 enum lttng_consumer_type type
,
685 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
686 struct lttng_consumer_local_data
*ctx
),
687 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
688 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
689 int (*update_stream
)(int stream_key
, uint32_t state
))
692 struct lttng_consumer_local_data
*ctx
;
694 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
695 consumer_data
.type
== type
);
696 consumer_data
.type
= type
;
698 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
700 perror("allocating context");
704 ctx
->consumer_error_socket
= -1;
705 /* assign the callbacks */
706 ctx
->on_buffer_ready
= buffer_ready
;
707 ctx
->on_recv_channel
= recv_channel
;
708 ctx
->on_recv_stream
= recv_stream
;
709 ctx
->on_update_stream
= update_stream
;
711 ret
= pipe(ctx
->consumer_poll_pipe
);
713 perror("Error creating poll pipe");
714 goto error_poll_pipe
;
717 ret
= pipe(ctx
->consumer_should_quit
);
719 perror("Error creating recv pipe");
720 goto error_quit_pipe
;
723 ret
= pipe(ctx
->consumer_thread_pipe
);
725 perror("Error creating thread pipe");
726 goto error_thread_pipe
;
733 for (i
= 0; i
< 2; i
++) {
736 err
= close(ctx
->consumer_should_quit
[i
]);
742 for (i
= 0; i
< 2; i
++) {
745 err
= close(ctx
->consumer_poll_pipe
[i
]);
757 * Close all fds associated with the instance and free the context.
759 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
763 ret
= close(ctx
->consumer_error_socket
);
767 ret
= close(ctx
->consumer_thread_pipe
[0]);
771 ret
= close(ctx
->consumer_thread_pipe
[1]);
775 ret
= close(ctx
->consumer_poll_pipe
[0]);
779 ret
= close(ctx
->consumer_poll_pipe
[1]);
783 ret
= close(ctx
->consumer_should_quit
[0]);
787 ret
= close(ctx
->consumer_should_quit
[1]);
791 unlink(ctx
->consumer_command_sock_path
);
796 * Mmap the ring buffer, read it and write the data to the tracefile.
798 * Returns the number of bytes written
800 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
801 struct lttng_consumer_local_data
*ctx
,
802 struct lttng_consumer_stream
*stream
, unsigned long len
)
804 switch (consumer_data
.type
) {
805 case LTTNG_CONSUMER_KERNEL
:
806 return lttng_kconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
807 case LTTNG_CONSUMER32_UST
:
808 case LTTNG_CONSUMER64_UST
:
809 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
811 ERR("Unknown consumer_data type");
819 * Splice the data from the ring buffer to the tracefile.
821 * Returns the number of bytes spliced.
823 ssize_t
lttng_consumer_on_read_subbuffer_splice(
824 struct lttng_consumer_local_data
*ctx
,
825 struct lttng_consumer_stream
*stream
, unsigned long len
)
827 switch (consumer_data
.type
) {
828 case LTTNG_CONSUMER_KERNEL
:
829 return lttng_kconsumer_on_read_subbuffer_splice(ctx
, stream
, len
);
830 case LTTNG_CONSUMER32_UST
:
831 case LTTNG_CONSUMER64_UST
:
834 ERR("Unknown consumer_data type");
842 * Take a snapshot for a specific fd
844 * Returns 0 on success, < 0 on error
846 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
847 struct lttng_consumer_stream
*stream
)
849 switch (consumer_data
.type
) {
850 case LTTNG_CONSUMER_KERNEL
:
851 return lttng_kconsumer_take_snapshot(ctx
, stream
);
852 case LTTNG_CONSUMER32_UST
:
853 case LTTNG_CONSUMER64_UST
:
854 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
856 ERR("Unknown consumer_data type");
864 * Get the produced position
866 * Returns 0 on success, < 0 on error
868 int lttng_consumer_get_produced_snapshot(
869 struct lttng_consumer_local_data
*ctx
,
870 struct lttng_consumer_stream
*stream
,
873 switch (consumer_data
.type
) {
874 case LTTNG_CONSUMER_KERNEL
:
875 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
876 case LTTNG_CONSUMER32_UST
:
877 case LTTNG_CONSUMER64_UST
:
878 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
880 ERR("Unknown consumer_data type");
886 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
887 int sock
, struct pollfd
*consumer_sockpoll
)
889 switch (consumer_data
.type
) {
890 case LTTNG_CONSUMER_KERNEL
:
891 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
892 case LTTNG_CONSUMER32_UST
:
893 case LTTNG_CONSUMER64_UST
:
894 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
896 ERR("Unknown consumer_data type");
903 * This thread polls the fds in the set to consume the data and write
904 * it to tracefile if necessary.
906 void *lttng_consumer_thread_poll_fds(void *data
)
908 int num_rdy
, num_hup
, high_prio
, ret
, i
;
909 struct pollfd
*pollfd
= NULL
;
910 /* local view of the streams */
911 struct lttng_consumer_stream
**local_stream
= NULL
;
912 /* local view of consumer_data.fds_count */
916 struct lttng_consumer_local_data
*ctx
= data
;
918 rcu_register_thread();
920 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
927 * the fds set has been updated, we need to update our
928 * local array as well
930 pthread_mutex_lock(&consumer_data
.lock
);
931 if (consumer_data
.need_update
) {
932 if (pollfd
!= NULL
) {
936 if (local_stream
!= NULL
) {
941 /* allocate for all fds + 1 for the consumer_poll_pipe */
942 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
943 if (pollfd
== NULL
) {
944 perror("pollfd malloc");
945 pthread_mutex_unlock(&consumer_data
.lock
);
949 /* allocate for all fds + 1 for the consumer_poll_pipe */
950 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
951 sizeof(struct lttng_consumer_stream
));
952 if (local_stream
== NULL
) {
953 perror("local_stream malloc");
954 pthread_mutex_unlock(&consumer_data
.lock
);
957 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
959 ERR("Error in allocating pollfd or local_outfds");
960 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
961 pthread_mutex_unlock(&consumer_data
.lock
);
965 consumer_data
.need_update
= 0;
967 pthread_mutex_unlock(&consumer_data
.lock
);
969 /* No FDs and consumer_quit, consumer_cleanup the thread */
970 if (nb_fd
== 0 && consumer_quit
== 1) {
973 /* poll on the array of fds */
975 DBG("polling on %d fd", nb_fd
+ 1);
976 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
977 DBG("poll num_rdy : %d", num_rdy
);
980 * Restart interrupted system call.
982 if (errno
== EINTR
) {
985 perror("Poll error");
986 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
988 } else if (num_rdy
== 0) {
989 DBG("Polling thread timed out");
994 * If the consumer_poll_pipe triggered poll go
995 * directly to the beginning of the loop to update the
996 * array. We want to prioritize array update over
997 * low-priority reads.
999 if (pollfd
[nb_fd
].revents
& POLLIN
) {
1000 DBG("consumer_poll_pipe wake up");
1001 tmp2
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
1003 perror("read consumer poll");
1008 /* Take care of high priority channels first. */
1009 for (i
= 0; i
< nb_fd
; i
++) {
1010 if (pollfd
[i
].revents
& POLLPRI
) {
1013 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
1015 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1016 /* it's ok to have an unavailable sub-buffer */
1017 if (len
< 0 && len
!= -EAGAIN
) {
1019 } else if (len
> 0) {
1020 local_stream
[i
]->data_read
= 1;
1026 * If we read high prio channel in this loop, try again
1027 * for more high prio data.
1033 /* Take care of low priority channels. */
1034 for (i
= 0; i
< nb_fd
; i
++) {
1035 if ((pollfd
[i
].revents
& POLLIN
) ||
1036 local_stream
[i
]->hangup_flush_done
) {
1039 assert(!(pollfd
[i
].revents
& POLLERR
));
1040 assert(!(pollfd
[i
].revents
& POLLNVAL
));
1041 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1042 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1043 /* it's ok to have an unavailable sub-buffer */
1044 if (len
< 0 && len
!= -EAGAIN
) {
1046 } else if (len
> 0) {
1047 local_stream
[i
]->data_read
= 1;
1052 /* Handle hangup and errors */
1053 for (i
= 0; i
< nb_fd
; i
++) {
1054 if (!local_stream
[i
]->hangup_flush_done
1055 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1056 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1057 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1058 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1060 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
1061 /* Attempt read again, for the data we just flushed. */
1062 local_stream
[i
]->data_read
= 1;
1065 * If the poll flag is HUP/ERR/NVAL and we have
1066 * read no data in this pass, we can remove the
1067 * stream from its hash table.
1069 if ((pollfd
[i
].revents
& POLLHUP
)) {
1070 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
1071 if (!local_stream
[i
]->data_read
) {
1072 consumer_del_stream(local_stream
[i
]);
1075 } else if (pollfd
[i
].revents
& POLLERR
) {
1076 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
1077 if (!local_stream
[i
]->data_read
) {
1078 consumer_del_stream(local_stream
[i
]);
1081 } else if (pollfd
[i
].revents
& POLLNVAL
) {
1082 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
1083 if (!local_stream
[i
]->data_read
) {
1084 consumer_del_stream(local_stream
[i
]);
1088 local_stream
[i
]->data_read
= 0;
1092 DBG("polling thread exiting");
1093 if (pollfd
!= NULL
) {
1097 if (local_stream
!= NULL
) {
1099 local_stream
= NULL
;
1101 rcu_unregister_thread();
1106 * This thread listens on the consumerd socket and receives the file
1107 * descriptors from the session daemon.
1109 void *lttng_consumer_thread_receive_fds(void *data
)
1111 int sock
, client_socket
, ret
;
1113 * structure to poll for incoming data on communication socket avoids
1114 * making blocking sockets.
1116 struct pollfd consumer_sockpoll
[2];
1117 struct lttng_consumer_local_data
*ctx
= data
;
1119 rcu_register_thread();
1121 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
1122 unlink(ctx
->consumer_command_sock_path
);
1123 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
1124 if (client_socket
< 0) {
1125 ERR("Cannot create command socket");
1129 ret
= lttcomm_listen_unix_sock(client_socket
);
1134 DBG("Sending ready command to lttng-sessiond");
1135 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
1136 /* return < 0 on error, but == 0 is not fatal */
1138 ERR("Error sending ready command to lttng-sessiond");
1142 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1144 perror("fcntl O_NONBLOCK");
1148 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1149 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1150 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1151 consumer_sockpoll
[1].fd
= client_socket
;
1152 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1154 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1157 DBG("Connection on client_socket");
1159 /* Blocking call, waiting for transmission */
1160 sock
= lttcomm_accept_unix_sock(client_socket
);
1165 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1167 perror("fcntl O_NONBLOCK");
1171 /* update the polling structure to poll on the established socket */
1172 consumer_sockpoll
[1].fd
= sock
;
1173 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1176 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1179 DBG("Incoming command on sock");
1180 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1181 if (ret
== -ENOENT
) {
1182 DBG("Received STOP command");
1186 ERR("Communication interrupted on command socket");
1189 if (consumer_quit
) {
1190 DBG("consumer_thread_receive_fds received quit from signal");
1193 DBG("received fds on sock");
1196 DBG("consumer_thread_receive_fds exiting");
1199 * when all fds have hung up, the polling thread
1205 * 2s of grace period, if no polling events occur during
1206 * this period, the polling thread will exit even if there
1207 * are still open FDs (should not happen, but safety mechanism).
1209 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1211 /* wake up the polling thread */
1212 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
1214 perror("poll pipe write");
1216 rcu_unregister_thread();
1220 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1221 struct lttng_consumer_local_data
*ctx
)
1223 switch (consumer_data
.type
) {
1224 case LTTNG_CONSUMER_KERNEL
:
1225 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1226 case LTTNG_CONSUMER32_UST
:
1227 case LTTNG_CONSUMER64_UST
:
1228 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1230 ERR("Unknown consumer_data type");
1236 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1238 switch (consumer_data
.type
) {
1239 case LTTNG_CONSUMER_KERNEL
:
1240 return lttng_kconsumer_on_recv_stream(stream
);
1241 case LTTNG_CONSUMER32_UST
:
1242 case LTTNG_CONSUMER64_UST
:
1243 return lttng_ustconsumer_on_recv_stream(stream
);
1245 ERR("Unknown consumer_data type");
1252 * Allocate and set consumer data hash tables.
1254 void lttng_consumer_init(void)
1256 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1257 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);