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 modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/socket.h>
27 #include <sys/types.h>
30 #include <common/common.h>
31 #include <common/kernel-ctl/kernel-ctl.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
33 #include <common/kernel-consumer/kernel-consumer.h>
34 #include <common/ust-consumer/ust-consumer.h>
38 struct lttng_consumer_global_data consumer_data
= {
41 .type
= LTTNG_CONSUMER_UNKNOWN
,
44 /* timeout parameter, to control the polling thread grace period. */
45 int consumer_poll_timeout
= -1;
48 * Flag to inform the polling thread to quit when all fd hung up. Updated by
49 * the consumer_thread_receive_fds when it notices that all fds has hung up.
50 * Also updated by the signal handler (consumer_should_exit()). Read by the
53 volatile int consumer_quit
= 0;
56 * Find a stream. The consumer_data.lock must be locked during this
59 static struct lttng_consumer_stream
*consumer_find_stream(int key
)
61 struct lttng_ht_iter iter
;
62 struct lttng_ht_node_ulong
*node
;
63 struct lttng_consumer_stream
*stream
= NULL
;
65 /* Negative keys are lookup failures */
71 lttng_ht_lookup(consumer_data
.stream_ht
, (void *)((unsigned long) key
),
73 node
= lttng_ht_iter_get_node_ulong(&iter
);
75 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
83 static void consumer_steal_stream_key(int key
)
85 struct lttng_consumer_stream
*stream
;
88 stream
= consumer_find_stream(key
);
92 * We don't want the lookup to match, but we still need
93 * to iterate on this stream when iterating over the hash table. Just
94 * change the node key.
96 stream
->node
.key
= -1;
101 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
103 struct lttng_ht_iter iter
;
104 struct lttng_ht_node_ulong
*node
;
105 struct lttng_consumer_channel
*channel
= NULL
;
107 /* Negative keys are lookup failures */
113 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
115 node
= lttng_ht_iter_get_node_ulong(&iter
);
117 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
125 static void consumer_steal_channel_key(int key
)
127 struct lttng_consumer_channel
*channel
;
130 channel
= consumer_find_channel(key
);
134 * We don't want the lookup to match, but we still need
135 * to iterate on this channel when iterating over the hash table. Just
136 * change the node key.
138 channel
->node
.key
= -1;
144 void consumer_free_stream(struct rcu_head
*head
)
146 struct lttng_ht_node_ulong
*node
=
147 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
148 struct lttng_consumer_stream
*stream
=
149 caa_container_of(node
, struct lttng_consumer_stream
, node
);
155 * Remove a stream from the global list protected by a mutex. This
156 * function is also responsible for freeing its data structures.
158 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
161 struct lttng_ht_iter iter
;
162 struct lttng_consumer_channel
*free_chan
= NULL
;
164 pthread_mutex_lock(&consumer_data
.lock
);
166 switch (consumer_data
.type
) {
167 case LTTNG_CONSUMER_KERNEL
:
168 if (stream
->mmap_base
!= NULL
) {
169 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
175 case LTTNG_CONSUMER32_UST
:
176 case LTTNG_CONSUMER64_UST
:
177 lttng_ustconsumer_del_stream(stream
);
180 ERR("Unknown consumer_data type");
186 iter
.iter
.node
= &stream
->node
.node
;
187 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
192 if (consumer_data
.stream_count
<= 0) {
195 consumer_data
.stream_count
--;
199 if (stream
->out_fd
>= 0) {
200 ret
= close(stream
->out_fd
);
205 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
206 ret
= close(stream
->wait_fd
);
211 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
212 ret
= close(stream
->shm_fd
);
217 if (!--stream
->chan
->refcount
)
218 free_chan
= stream
->chan
;
220 call_rcu(&stream
->node
.head
, consumer_free_stream
);
222 consumer_data
.need_update
= 1;
223 pthread_mutex_unlock(&consumer_data
.lock
);
226 consumer_del_channel(free_chan
);
229 struct lttng_consumer_stream
*consumer_allocate_stream(
230 int channel_key
, int stream_key
,
231 int shm_fd
, int wait_fd
,
232 enum lttng_consumer_stream_state state
,
234 enum lttng_event_output output
,
235 const char *path_name
,
239 struct lttng_consumer_stream
*stream
;
242 stream
= zmalloc(sizeof(*stream
));
243 if (stream
== NULL
) {
244 perror("malloc struct lttng_consumer_stream");
247 stream
->chan
= consumer_find_channel(channel_key
);
249 perror("Unable to find channel key");
252 stream
->chan
->refcount
++;
253 stream
->key
= stream_key
;
254 stream
->shm_fd
= shm_fd
;
255 stream
->wait_fd
= wait_fd
;
257 stream
->out_fd_offset
= 0;
258 stream
->state
= state
;
259 stream
->mmap_len
= mmap_len
;
260 stream
->mmap_base
= NULL
;
261 stream
->output
= output
;
264 strncpy(stream
->path_name
, path_name
, PATH_MAX
- 1);
265 stream
->path_name
[PATH_MAX
- 1] = '\0';
266 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
268 switch (consumer_data
.type
) {
269 case LTTNG_CONSUMER_KERNEL
:
271 case LTTNG_CONSUMER32_UST
:
272 case LTTNG_CONSUMER64_UST
:
273 stream
->cpu
= stream
->chan
->cpucount
++;
274 ret
= lttng_ustconsumer_allocate_stream(stream
);
281 ERR("Unknown consumer_data type");
285 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
286 stream
->path_name
, stream
->key
,
289 (unsigned long long) stream
->mmap_len
,
296 * Add a stream to the global list protected by a mutex.
298 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
301 struct lttng_ht_node_ulong
*node
;
302 struct lttng_ht_iter iter
;
304 pthread_mutex_lock(&consumer_data
.lock
);
305 /* Steal stream identifier, for UST */
306 consumer_steal_stream_key(stream
->key
);
309 lttng_ht_lookup(consumer_data
.stream_ht
,
310 (void *)((unsigned long) stream
->key
), &iter
);
311 node
= lttng_ht_iter_get_node_ulong(&iter
);
314 /* Stream already exist. Ignore the insertion */
318 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
320 consumer_data
.stream_count
++;
321 consumer_data
.need_update
= 1;
323 switch (consumer_data
.type
) {
324 case LTTNG_CONSUMER_KERNEL
:
326 case LTTNG_CONSUMER32_UST
:
327 case LTTNG_CONSUMER64_UST
:
328 /* Streams are in CPU number order (we rely on this) */
329 stream
->cpu
= stream
->chan
->nr_streams
++;
332 ERR("Unknown consumer_data type");
338 pthread_mutex_unlock(&consumer_data
.lock
);
344 * Update a stream according to what we just received.
346 void consumer_change_stream_state(int stream_key
,
347 enum lttng_consumer_stream_state state
)
349 struct lttng_consumer_stream
*stream
;
351 pthread_mutex_lock(&consumer_data
.lock
);
352 stream
= consumer_find_stream(stream_key
);
354 stream
->state
= state
;
356 consumer_data
.need_update
= 1;
357 pthread_mutex_unlock(&consumer_data
.lock
);
361 void consumer_free_channel(struct rcu_head
*head
)
363 struct lttng_ht_node_ulong
*node
=
364 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
365 struct lttng_consumer_channel
*channel
=
366 caa_container_of(node
, struct lttng_consumer_channel
, node
);
372 * Remove a channel from the global list protected by a mutex. This
373 * function is also responsible for freeing its data structures.
375 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
378 struct lttng_ht_iter iter
;
380 pthread_mutex_lock(&consumer_data
.lock
);
382 switch (consumer_data
.type
) {
383 case LTTNG_CONSUMER_KERNEL
:
385 case LTTNG_CONSUMER32_UST
:
386 case LTTNG_CONSUMER64_UST
:
387 lttng_ustconsumer_del_channel(channel
);
390 ERR("Unknown consumer_data type");
396 iter
.iter
.node
= &channel
->node
.node
;
397 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
401 if (channel
->mmap_base
!= NULL
) {
402 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
407 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
408 ret
= close(channel
->wait_fd
);
413 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
414 ret
= close(channel
->shm_fd
);
420 call_rcu(&channel
->node
.head
, consumer_free_channel
);
422 pthread_mutex_unlock(&consumer_data
.lock
);
425 struct lttng_consumer_channel
*consumer_allocate_channel(
427 int shm_fd
, int wait_fd
,
429 uint64_t max_sb_size
)
431 struct lttng_consumer_channel
*channel
;
434 channel
= zmalloc(sizeof(*channel
));
435 if (channel
== NULL
) {
436 perror("malloc struct lttng_consumer_channel");
439 channel
->key
= channel_key
;
440 channel
->shm_fd
= shm_fd
;
441 channel
->wait_fd
= wait_fd
;
442 channel
->mmap_len
= mmap_len
;
443 channel
->max_sb_size
= max_sb_size
;
444 channel
->refcount
= 0;
445 channel
->nr_streams
= 0;
446 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
448 switch (consumer_data
.type
) {
449 case LTTNG_CONSUMER_KERNEL
:
450 channel
->mmap_base
= NULL
;
451 channel
->mmap_len
= 0;
453 case LTTNG_CONSUMER32_UST
:
454 case LTTNG_CONSUMER64_UST
:
455 ret
= lttng_ustconsumer_allocate_channel(channel
);
462 ERR("Unknown consumer_data type");
466 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
470 (unsigned long long) channel
->mmap_len
,
471 (unsigned long long) channel
->max_sb_size
);
477 * Add a channel to the global list protected by a mutex.
479 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
481 struct lttng_ht_node_ulong
*node
;
482 struct lttng_ht_iter iter
;
484 pthread_mutex_lock(&consumer_data
.lock
);
485 /* Steal channel identifier, for UST */
486 consumer_steal_channel_key(channel
->key
);
489 lttng_ht_lookup(consumer_data
.channel_ht
,
490 (void *)((unsigned long) channel
->key
), &iter
);
491 node
= lttng_ht_iter_get_node_ulong(&iter
);
493 /* Channel already exist. Ignore the insertion */
497 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
501 pthread_mutex_unlock(&consumer_data
.lock
);
507 * Allocate the pollfd structure and the local view of the out fds to avoid
508 * doing a lookup in the linked list and concurrency issues when writing is
509 * needed. Called with consumer_data.lock held.
511 * Returns the number of fds in the structures.
513 int consumer_update_poll_array(
514 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
515 struct lttng_consumer_stream
**local_stream
)
518 struct lttng_ht_iter iter
;
519 struct lttng_consumer_stream
*stream
;
521 DBG("Updating poll fd array");
523 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
525 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
528 DBG("Active FD %d", stream
->wait_fd
);
529 (*pollfd
)[i
].fd
= stream
->wait_fd
;
530 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
531 local_stream
[i
] = stream
;
537 * Insert the consumer_poll_pipe at the end of the array and don't
538 * increment i so nb_fd is the number of real FD.
540 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
541 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
546 * Poll on the should_quit pipe and the command socket return -1 on error and
547 * should exit, 0 if data is available on the command socket
549 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
554 num_rdy
= poll(consumer_sockpoll
, 2, -1);
557 * Restart interrupted system call.
559 if (errno
== EINTR
) {
562 perror("Poll error");
565 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
566 DBG("consumer_should_quit wake up");
576 * Set the error socket.
578 void lttng_consumer_set_error_sock(
579 struct lttng_consumer_local_data
*ctx
, int sock
)
581 ctx
->consumer_error_socket
= sock
;
585 * Set the command socket path.
588 void lttng_consumer_set_command_sock_path(
589 struct lttng_consumer_local_data
*ctx
, char *sock
)
591 ctx
->consumer_command_sock_path
= sock
;
595 * Send return code to the session daemon.
596 * If the socket is not defined, we return 0, it is not a fatal error
598 int lttng_consumer_send_error(
599 struct lttng_consumer_local_data
*ctx
, int cmd
)
601 if (ctx
->consumer_error_socket
> 0) {
602 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
603 sizeof(enum lttcomm_sessiond_command
));
610 * Close all the tracefiles and stream fds, should be called when all instances
613 void lttng_consumer_cleanup(void)
615 struct lttng_ht_iter iter
;
616 struct lttng_ht_node_ulong
*node
;
621 * close all outfd. Called when there are no more threads running (after
622 * joining on the threads), no need to protect list iteration with mutex.
624 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
626 struct lttng_consumer_stream
*stream
=
627 caa_container_of(node
, struct lttng_consumer_stream
, node
);
628 consumer_del_stream(stream
);
631 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
633 struct lttng_consumer_channel
*channel
=
634 caa_container_of(node
, struct lttng_consumer_channel
, node
);
635 consumer_del_channel(channel
);
642 * Called from signal handler.
644 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
648 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
650 perror("write consumer quit");
654 void lttng_consumer_sync_trace_file(
655 struct lttng_consumer_stream
*stream
, off_t orig_offset
)
657 int outfd
= stream
->out_fd
;
660 * This does a blocking write-and-wait on any page that belongs to the
661 * subbuffer prior to the one we just wrote.
662 * Don't care about error values, as these are just hints and ways to
663 * limit the amount of page cache used.
665 if (orig_offset
< stream
->chan
->max_sb_size
) {
668 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
669 stream
->chan
->max_sb_size
,
670 SYNC_FILE_RANGE_WAIT_BEFORE
671 | SYNC_FILE_RANGE_WRITE
672 | SYNC_FILE_RANGE_WAIT_AFTER
);
674 * Give hints to the kernel about how we access the file:
675 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
678 * We need to call fadvise again after the file grows because the
679 * kernel does not seem to apply fadvise to non-existing parts of the
682 * Call fadvise _after_ having waited for the page writeback to
683 * complete because the dirty page writeback semantic is not well
684 * defined. So it can be expected to lead to lower throughput in
687 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
688 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
692 * Initialise the necessary environnement :
693 * - create a new context
694 * - create the poll_pipe
695 * - create the should_quit pipe (for signal handler)
696 * - create the thread pipe (for splice)
698 * Takes a function pointer as argument, this function is called when data is
699 * available on a buffer. This function is responsible to do the
700 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
701 * buffer configuration and then kernctl_put_next_subbuf at the end.
703 * Returns a pointer to the new context or NULL on error.
705 struct lttng_consumer_local_data
*lttng_consumer_create(
706 enum lttng_consumer_type type
,
707 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
708 struct lttng_consumer_local_data
*ctx
),
709 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
710 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
711 int (*update_stream
)(int stream_key
, uint32_t state
))
714 struct lttng_consumer_local_data
*ctx
;
716 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
717 consumer_data
.type
== type
);
718 consumer_data
.type
= type
;
720 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
722 perror("allocating context");
726 ctx
->consumer_error_socket
= -1;
727 /* assign the callbacks */
728 ctx
->on_buffer_ready
= buffer_ready
;
729 ctx
->on_recv_channel
= recv_channel
;
730 ctx
->on_recv_stream
= recv_stream
;
731 ctx
->on_update_stream
= update_stream
;
733 ret
= pipe(ctx
->consumer_poll_pipe
);
735 perror("Error creating poll pipe");
736 goto error_poll_pipe
;
739 /* set read end of the pipe to non-blocking */
740 ret
= fcntl(ctx
->consumer_poll_pipe
[0], F_SETFL
, O_NONBLOCK
);
742 perror("fcntl O_NONBLOCK");
743 goto error_poll_fcntl
;
746 /* set write end of the pipe to non-blocking */
747 ret
= fcntl(ctx
->consumer_poll_pipe
[1], F_SETFL
, O_NONBLOCK
);
749 perror("fcntl O_NONBLOCK");
750 goto error_poll_fcntl
;
753 ret
= pipe(ctx
->consumer_should_quit
);
755 perror("Error creating recv pipe");
756 goto error_quit_pipe
;
759 ret
= pipe(ctx
->consumer_thread_pipe
);
761 perror("Error creating thread pipe");
762 goto error_thread_pipe
;
769 for (i
= 0; i
< 2; i
++) {
772 err
= close(ctx
->consumer_should_quit
[i
]);
779 for (i
= 0; i
< 2; i
++) {
782 err
= close(ctx
->consumer_poll_pipe
[i
]);
794 * Close all fds associated with the instance and free the context.
796 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
800 ret
= close(ctx
->consumer_error_socket
);
804 ret
= close(ctx
->consumer_thread_pipe
[0]);
808 ret
= close(ctx
->consumer_thread_pipe
[1]);
812 ret
= close(ctx
->consumer_poll_pipe
[0]);
816 ret
= close(ctx
->consumer_poll_pipe
[1]);
820 ret
= close(ctx
->consumer_should_quit
[0]);
824 ret
= close(ctx
->consumer_should_quit
[1]);
828 unlink(ctx
->consumer_command_sock_path
);
833 * Mmap the ring buffer, read it and write the data to the tracefile.
835 * Returns the number of bytes written
837 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
838 struct lttng_consumer_local_data
*ctx
,
839 struct lttng_consumer_stream
*stream
, unsigned long len
)
841 switch (consumer_data
.type
) {
842 case LTTNG_CONSUMER_KERNEL
:
843 return lttng_kconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
844 case LTTNG_CONSUMER32_UST
:
845 case LTTNG_CONSUMER64_UST
:
846 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
848 ERR("Unknown consumer_data type");
856 * Splice the data from the ring buffer to the tracefile.
858 * Returns the number of bytes spliced.
860 ssize_t
lttng_consumer_on_read_subbuffer_splice(
861 struct lttng_consumer_local_data
*ctx
,
862 struct lttng_consumer_stream
*stream
, unsigned long len
)
864 switch (consumer_data
.type
) {
865 case LTTNG_CONSUMER_KERNEL
:
866 return lttng_kconsumer_on_read_subbuffer_splice(ctx
, stream
, len
);
867 case LTTNG_CONSUMER32_UST
:
868 case LTTNG_CONSUMER64_UST
:
871 ERR("Unknown consumer_data type");
879 * Take a snapshot for a specific fd
881 * Returns 0 on success, < 0 on error
883 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
884 struct lttng_consumer_stream
*stream
)
886 switch (consumer_data
.type
) {
887 case LTTNG_CONSUMER_KERNEL
:
888 return lttng_kconsumer_take_snapshot(ctx
, stream
);
889 case LTTNG_CONSUMER32_UST
:
890 case LTTNG_CONSUMER64_UST
:
891 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
893 ERR("Unknown consumer_data type");
901 * Get the produced position
903 * Returns 0 on success, < 0 on error
905 int lttng_consumer_get_produced_snapshot(
906 struct lttng_consumer_local_data
*ctx
,
907 struct lttng_consumer_stream
*stream
,
910 switch (consumer_data
.type
) {
911 case LTTNG_CONSUMER_KERNEL
:
912 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
913 case LTTNG_CONSUMER32_UST
:
914 case LTTNG_CONSUMER64_UST
:
915 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
917 ERR("Unknown consumer_data type");
923 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
924 int sock
, struct pollfd
*consumer_sockpoll
)
926 switch (consumer_data
.type
) {
927 case LTTNG_CONSUMER_KERNEL
:
928 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
929 case LTTNG_CONSUMER32_UST
:
930 case LTTNG_CONSUMER64_UST
:
931 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
933 ERR("Unknown consumer_data type");
940 * This thread polls the fds in the set to consume the data and write
941 * it to tracefile if necessary.
943 void *lttng_consumer_thread_poll_fds(void *data
)
945 int num_rdy
, num_hup
, high_prio
, ret
, i
;
946 struct pollfd
*pollfd
= NULL
;
947 /* local view of the streams */
948 struct lttng_consumer_stream
**local_stream
= NULL
;
949 /* local view of consumer_data.fds_count */
951 struct lttng_consumer_local_data
*ctx
= data
;
953 rcu_register_thread();
955 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
962 * the fds set has been updated, we need to update our
963 * local array as well
965 pthread_mutex_lock(&consumer_data
.lock
);
966 if (consumer_data
.need_update
) {
967 if (pollfd
!= NULL
) {
971 if (local_stream
!= NULL
) {
976 /* allocate for all fds + 1 for the consumer_poll_pipe */
977 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
978 if (pollfd
== NULL
) {
979 perror("pollfd malloc");
980 pthread_mutex_unlock(&consumer_data
.lock
);
984 /* allocate for all fds + 1 for the consumer_poll_pipe */
985 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
986 sizeof(struct lttng_consumer_stream
));
987 if (local_stream
== NULL
) {
988 perror("local_stream malloc");
989 pthread_mutex_unlock(&consumer_data
.lock
);
992 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
994 ERR("Error in allocating pollfd or local_outfds");
995 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
996 pthread_mutex_unlock(&consumer_data
.lock
);
1000 consumer_data
.need_update
= 0;
1002 pthread_mutex_unlock(&consumer_data
.lock
);
1004 /* No FDs and consumer_quit, consumer_cleanup the thread */
1005 if (nb_fd
== 0 && consumer_quit
== 1) {
1008 /* poll on the array of fds */
1010 DBG("polling on %d fd", nb_fd
+ 1);
1011 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
1012 DBG("poll num_rdy : %d", num_rdy
);
1013 if (num_rdy
== -1) {
1015 * Restart interrupted system call.
1017 if (errno
== EINTR
) {
1020 perror("Poll error");
1021 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
1023 } else if (num_rdy
== 0) {
1024 DBG("Polling thread timed out");
1029 * If the consumer_poll_pipe triggered poll go
1030 * directly to the beginning of the loop to update the
1031 * array. We want to prioritize array update over
1032 * low-priority reads.
1034 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
1035 size_t pipe_readlen
;
1038 DBG("consumer_poll_pipe wake up");
1039 /* Consume 1 byte of pipe data */
1041 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
1042 } while (pipe_readlen
== -1 && errno
== EINTR
);
1046 /* Take care of high priority channels first. */
1047 for (i
= 0; i
< nb_fd
; i
++) {
1048 if (pollfd
[i
].revents
& POLLPRI
) {
1051 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
1053 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1054 /* it's ok to have an unavailable sub-buffer */
1055 if (len
< 0 && len
!= -EAGAIN
) {
1057 } else if (len
> 0) {
1058 local_stream
[i
]->data_read
= 1;
1064 * If we read high prio channel in this loop, try again
1065 * for more high prio data.
1071 /* Take care of low priority channels. */
1072 for (i
= 0; i
< nb_fd
; i
++) {
1073 if ((pollfd
[i
].revents
& POLLIN
) ||
1074 local_stream
[i
]->hangup_flush_done
) {
1077 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1078 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1079 /* it's ok to have an unavailable sub-buffer */
1080 if (len
< 0 && len
!= -EAGAIN
) {
1082 } else if (len
> 0) {
1083 local_stream
[i
]->data_read
= 1;
1088 /* Handle hangup and errors */
1089 for (i
= 0; i
< nb_fd
; i
++) {
1090 if (!local_stream
[i
]->hangup_flush_done
1091 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1092 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1093 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1094 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1096 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
1097 /* Attempt read again, for the data we just flushed. */
1098 local_stream
[i
]->data_read
= 1;
1101 * If the poll flag is HUP/ERR/NVAL and we have
1102 * read no data in this pass, we can remove the
1103 * stream from its hash table.
1105 if ((pollfd
[i
].revents
& POLLHUP
)) {
1106 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
1107 if (!local_stream
[i
]->data_read
) {
1108 consumer_del_stream(local_stream
[i
]);
1111 } else if (pollfd
[i
].revents
& POLLERR
) {
1112 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
1113 if (!local_stream
[i
]->data_read
) {
1114 consumer_del_stream(local_stream
[i
]);
1117 } else if (pollfd
[i
].revents
& POLLNVAL
) {
1118 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
1119 if (!local_stream
[i
]->data_read
) {
1120 consumer_del_stream(local_stream
[i
]);
1124 local_stream
[i
]->data_read
= 0;
1128 DBG("polling thread exiting");
1129 if (pollfd
!= NULL
) {
1133 if (local_stream
!= NULL
) {
1135 local_stream
= NULL
;
1137 rcu_unregister_thread();
1142 * This thread listens on the consumerd socket and receives the file
1143 * descriptors from the session daemon.
1145 void *lttng_consumer_thread_receive_fds(void *data
)
1147 int sock
, client_socket
, ret
;
1149 * structure to poll for incoming data on communication socket avoids
1150 * making blocking sockets.
1152 struct pollfd consumer_sockpoll
[2];
1153 struct lttng_consumer_local_data
*ctx
= data
;
1155 rcu_register_thread();
1157 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
1158 unlink(ctx
->consumer_command_sock_path
);
1159 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
1160 if (client_socket
< 0) {
1161 ERR("Cannot create command socket");
1165 ret
= lttcomm_listen_unix_sock(client_socket
);
1170 DBG("Sending ready command to lttng-sessiond");
1171 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
1172 /* return < 0 on error, but == 0 is not fatal */
1174 ERR("Error sending ready command to lttng-sessiond");
1178 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1180 perror("fcntl O_NONBLOCK");
1184 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1185 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1186 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1187 consumer_sockpoll
[1].fd
= client_socket
;
1188 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1190 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1193 DBG("Connection on client_socket");
1195 /* Blocking call, waiting for transmission */
1196 sock
= lttcomm_accept_unix_sock(client_socket
);
1201 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1203 perror("fcntl O_NONBLOCK");
1207 /* update the polling structure to poll on the established socket */
1208 consumer_sockpoll
[1].fd
= sock
;
1209 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1212 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1215 DBG("Incoming command on sock");
1216 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1217 if (ret
== -ENOENT
) {
1218 DBG("Received STOP command");
1222 ERR("Communication interrupted on command socket");
1225 if (consumer_quit
) {
1226 DBG("consumer_thread_receive_fds received quit from signal");
1229 DBG("received fds on sock");
1232 DBG("consumer_thread_receive_fds exiting");
1235 * when all fds have hung up, the polling thread
1241 * 2s of grace period, if no polling events occur during
1242 * this period, the polling thread will exit even if there
1243 * are still open FDs (should not happen, but safety mechanism).
1245 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1248 * Wake-up the other end by writing a null byte in the pipe
1249 * (non-blocking). Important note: Because writing into the
1250 * pipe is non-blocking (and therefore we allow dropping wakeup
1251 * data, as long as there is wakeup data present in the pipe
1252 * buffer to wake up the other end), the other end should
1253 * perform the following sequence for waiting:
1254 * 1) empty the pipe (reads).
1255 * 2) perform update operation.
1256 * 3) wait on the pipe (poll).
1259 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
1260 } while (ret
== -1UL && errno
== EINTR
);
1261 rcu_unregister_thread();
1265 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1266 struct lttng_consumer_local_data
*ctx
)
1268 switch (consumer_data
.type
) {
1269 case LTTNG_CONSUMER_KERNEL
:
1270 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1271 case LTTNG_CONSUMER32_UST
:
1272 case LTTNG_CONSUMER64_UST
:
1273 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1275 ERR("Unknown consumer_data type");
1281 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1283 switch (consumer_data
.type
) {
1284 case LTTNG_CONSUMER_KERNEL
:
1285 return lttng_kconsumer_on_recv_stream(stream
);
1286 case LTTNG_CONSUMER32_UST
:
1287 case LTTNG_CONSUMER64_UST
:
1288 return lttng_ustconsumer_on_recv_stream(stream
);
1290 ERR("Unknown consumer_data type");
1297 * Allocate and set consumer data hash tables.
1299 void lttng_consumer_init(void)
1301 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1302 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);