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 <lttng-kernel-ctl.h>
33 #include <lttng-sessiond-comm.h>
34 #include <lttng/lttng-consumer.h>
35 #include <lttng/lttng-kconsumer.h>
36 #include <lttng/lttng-ustconsumer.h>
39 struct lttng_consumer_global_data consumer_data
= {
40 .stream_list
.head
= CDS_LIST_HEAD_INIT(consumer_data
.stream_list
.head
),
41 .channel_list
.head
= CDS_LIST_HEAD_INIT(consumer_data
.channel_list
.head
),
44 .type
= LTTNG_CONSUMER_UNKNOWN
,
47 /* timeout parameter, to control the polling thread grace period. */
48 int consumer_poll_timeout
= -1;
51 * Flag to inform the polling thread to quit when all fd hung up. Updated by
52 * the consumer_thread_receive_fds when it notices that all fds has hung up.
53 * Also updated by the signal handler (consumer_should_exit()). Read by the
56 volatile int consumer_quit
= 0;
59 * Find a stream. The consumer_data.lock must be locked during this
62 static struct lttng_consumer_stream
*consumer_find_stream(int key
)
64 struct lttng_consumer_stream
*iter
;
66 /* Negative keys are lookup failures */
69 cds_list_for_each_entry(iter
, &consumer_data
.stream_list
.head
, list
) {
70 if (iter
->key
== key
) {
71 DBG("Found stream key %d", key
);
78 static void consumer_steal_stream_key(int key
)
80 struct lttng_consumer_stream
*stream
;
82 stream
= consumer_find_stream(key
);
87 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
89 struct lttng_consumer_channel
*iter
;
91 /* Negative keys are lookup failures */
94 cds_list_for_each_entry(iter
, &consumer_data
.channel_list
.head
, list
) {
95 if (iter
->key
== key
) {
96 DBG("Found channel key %d", key
);
103 static void consumer_steal_channel_key(int key
)
105 struct lttng_consumer_channel
*channel
;
107 channel
= consumer_find_channel(key
);
113 * Remove a stream from the global list protected by a mutex. This
114 * function is also responsible for freeing its data structures.
116 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
119 struct lttng_consumer_channel
*free_chan
= NULL
;
121 pthread_mutex_lock(&consumer_data
.lock
);
123 switch (consumer_data
.type
) {
124 case LTTNG_CONSUMER_KERNEL
:
125 if (stream
->mmap_base
!= NULL
) {
126 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
132 case LTTNG_CONSUMER_UST
:
133 lttng_ustconsumer_del_stream(stream
);
136 ERR("Unknown consumer_data type");
141 cds_list_del(&stream
->list
);
142 if (consumer_data
.stream_count
<= 0) {
145 consumer_data
.stream_count
--;
149 if (stream
->out_fd
>= 0) {
150 close(stream
->out_fd
);
152 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
153 close(stream
->wait_fd
);
155 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
156 && !stream
->shm_fd_is_copy
) {
157 close(stream
->shm_fd
);
159 if (!--stream
->chan
->refcount
)
160 free_chan
= stream
->chan
;
163 consumer_data
.need_update
= 1;
164 pthread_mutex_unlock(&consumer_data
.lock
);
167 consumer_del_channel(free_chan
);
170 struct lttng_consumer_stream
*consumer_allocate_stream(
171 int channel_key
, int stream_key
,
172 int shm_fd
, int wait_fd
,
173 enum lttng_consumer_stream_state state
,
175 enum lttng_event_output output
,
176 const char *path_name
)
178 struct lttng_consumer_stream
*stream
;
181 stream
= zmalloc(sizeof(*stream
));
182 if (stream
== NULL
) {
183 perror("malloc struct lttng_consumer_stream");
186 stream
->chan
= consumer_find_channel(channel_key
);
188 perror("Unable to find channel key");
191 stream
->chan
->refcount
++;
192 stream
->key
= stream_key
;
193 stream
->shm_fd
= shm_fd
;
194 stream
->wait_fd
= wait_fd
;
196 stream
->out_fd_offset
= 0;
197 stream
->state
= state
;
198 stream
->mmap_len
= mmap_len
;
199 stream
->mmap_base
= NULL
;
200 stream
->output
= output
;
201 strncpy(stream
->path_name
, path_name
, PATH_MAX
- 1);
202 stream
->path_name
[PATH_MAX
- 1] = '\0';
204 switch (consumer_data
.type
) {
205 case LTTNG_CONSUMER_KERNEL
:
207 case LTTNG_CONSUMER_UST
:
208 stream
->cpu
= stream
->chan
->cpucount
++;
209 ret
= lttng_ustconsumer_allocate_stream(stream
);
216 ERR("Unknown consumer_data type");
220 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
221 stream
->path_name
, stream
->key
,
224 (unsigned long long) stream
->mmap_len
,
231 * Add a stream to the global list protected by a mutex.
233 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
237 pthread_mutex_lock(&consumer_data
.lock
);
238 /* Steal stream identifier, for UST */
239 consumer_steal_stream_key(stream
->key
);
240 cds_list_add(&stream
->list
, &consumer_data
.stream_list
.head
);
241 consumer_data
.stream_count
++;
242 consumer_data
.need_update
= 1;
244 switch (consumer_data
.type
) {
245 case LTTNG_CONSUMER_KERNEL
:
247 case LTTNG_CONSUMER_UST
:
248 /* Streams are in CPU number order (we rely on this) */
249 stream
->cpu
= stream
->chan
->nr_streams
++;
252 ERR("Unknown consumer_data type");
258 pthread_mutex_unlock(&consumer_data
.lock
);
263 * Update a stream according to what we just received.
265 void consumer_change_stream_state(int stream_key
,
266 enum lttng_consumer_stream_state state
)
268 struct lttng_consumer_stream
*stream
;
270 pthread_mutex_lock(&consumer_data
.lock
);
271 stream
= consumer_find_stream(stream_key
);
273 stream
->state
= state
;
275 consumer_data
.need_update
= 1;
276 pthread_mutex_unlock(&consumer_data
.lock
);
280 * Remove a channel from the global list protected by a mutex. This
281 * function is also responsible for freeing its data structures.
283 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
287 pthread_mutex_lock(&consumer_data
.lock
);
289 switch (consumer_data
.type
) {
290 case LTTNG_CONSUMER_KERNEL
:
292 case LTTNG_CONSUMER_UST
:
293 lttng_ustconsumer_del_channel(channel
);
296 ERR("Unknown consumer_data type");
301 cds_list_del(&channel
->list
);
302 if (channel
->mmap_base
!= NULL
) {
303 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
308 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
309 close(channel
->wait_fd
);
311 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
312 && !channel
->shm_fd_is_copy
) {
313 close(channel
->shm_fd
);
317 pthread_mutex_unlock(&consumer_data
.lock
);
320 struct lttng_consumer_channel
*consumer_allocate_channel(
322 int shm_fd
, int wait_fd
,
324 uint64_t max_sb_size
)
326 struct lttng_consumer_channel
*channel
;
329 channel
= zmalloc(sizeof(*channel
));
330 if (channel
== NULL
) {
331 perror("malloc struct lttng_consumer_channel");
334 channel
->key
= channel_key
;
335 channel
->shm_fd
= shm_fd
;
336 channel
->wait_fd
= wait_fd
;
337 channel
->mmap_len
= mmap_len
;
338 channel
->max_sb_size
= max_sb_size
;
339 channel
->refcount
= 0;
340 channel
->nr_streams
= 0;
342 switch (consumer_data
.type
) {
343 case LTTNG_CONSUMER_KERNEL
:
344 channel
->mmap_base
= NULL
;
345 channel
->mmap_len
= 0;
347 case LTTNG_CONSUMER_UST
:
348 ret
= lttng_ustconsumer_allocate_channel(channel
);
355 ERR("Unknown consumer_data type");
359 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
363 (unsigned long long) channel
->mmap_len
,
364 (unsigned long long) channel
->max_sb_size
);
370 * Add a channel to the global list protected by a mutex.
372 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
374 pthread_mutex_lock(&consumer_data
.lock
);
375 /* Steal channel identifier, for UST */
376 consumer_steal_channel_key(channel
->key
);
377 cds_list_add(&channel
->list
, &consumer_data
.channel_list
.head
);
378 pthread_mutex_unlock(&consumer_data
.lock
);
383 * Allocate the pollfd structure and the local view of the out fds to avoid
384 * doing a lookup in the linked list and concurrency issues when writing is
385 * needed. Called with consumer_data.lock held.
387 * Returns the number of fds in the structures.
389 int consumer_update_poll_array(
390 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
391 struct lttng_consumer_stream
**local_stream
)
393 struct lttng_consumer_stream
*iter
;
396 DBG("Updating poll fd array");
397 cds_list_for_each_entry(iter
, &consumer_data
.stream_list
.head
, list
) {
398 if (iter
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
401 DBG("Active FD %d", iter
->wait_fd
);
402 (*pollfd
)[i
].fd
= iter
->wait_fd
;
403 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
404 local_stream
[i
] = iter
;
409 * Insert the consumer_poll_pipe at the end of the array and don't
410 * increment i so nb_fd is the number of real FD.
412 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
413 (*pollfd
)[i
].events
= POLLIN
;
418 * Poll on the should_quit pipe and the command socket return -1 on error and
419 * should exit, 0 if data is available on the command socket
421 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
425 num_rdy
= poll(consumer_sockpoll
, 2, -1);
427 perror("Poll error");
430 if (consumer_sockpoll
[0].revents
== POLLIN
) {
431 DBG("consumer_should_quit wake up");
441 * Set the error socket.
443 void lttng_consumer_set_error_sock(
444 struct lttng_consumer_local_data
*ctx
, int sock
)
446 ctx
->consumer_error_socket
= sock
;
450 * Set the command socket path.
453 void lttng_consumer_set_command_sock_path(
454 struct lttng_consumer_local_data
*ctx
, char *sock
)
456 ctx
->consumer_command_sock_path
= sock
;
460 * Send return code to the session daemon.
461 * If the socket is not defined, we return 0, it is not a fatal error
463 int lttng_consumer_send_error(
464 struct lttng_consumer_local_data
*ctx
, int cmd
)
466 if (ctx
->consumer_error_socket
> 0) {
467 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
468 sizeof(enum lttcomm_sessiond_command
));
475 * Close all the tracefiles and stream fds, should be called when all instances
478 void lttng_consumer_cleanup(void)
480 struct lttng_consumer_stream
*iter
, *tmp
;
481 struct lttng_consumer_channel
*citer
, *ctmp
;
484 * close all outfd. Called when there are no more threads
485 * running (after joining on the threads), no need to protect
486 * list iteration with mutex.
488 cds_list_for_each_entry_safe(iter
, tmp
,
489 &consumer_data
.stream_list
.head
, list
) {
490 consumer_del_stream(iter
);
492 cds_list_for_each_entry_safe(citer
, ctmp
,
493 &consumer_data
.channel_list
.head
, list
) {
494 consumer_del_channel(citer
);
499 * Called from signal handler.
501 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
505 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
507 perror("write consumer quit");
511 void lttng_consumer_sync_trace_file(
512 struct lttng_consumer_stream
*stream
, off_t orig_offset
)
514 int outfd
= stream
->out_fd
;
517 * This does a blocking write-and-wait on any page that belongs to the
518 * subbuffer prior to the one we just wrote.
519 * Don't care about error values, as these are just hints and ways to
520 * limit the amount of page cache used.
522 if (orig_offset
< stream
->chan
->max_sb_size
) {
525 sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
526 stream
->chan
->max_sb_size
,
527 SYNC_FILE_RANGE_WAIT_BEFORE
528 | SYNC_FILE_RANGE_WRITE
529 | SYNC_FILE_RANGE_WAIT_AFTER
);
531 * Give hints to the kernel about how we access the file:
532 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
535 * We need to call fadvise again after the file grows because the
536 * kernel does not seem to apply fadvise to non-existing parts of the
539 * Call fadvise _after_ having waited for the page writeback to
540 * complete because the dirty page writeback semantic is not well
541 * defined. So it can be expected to lead to lower throughput in
544 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
545 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
549 * Initialise the necessary environnement :
550 * - create a new context
551 * - create the poll_pipe
552 * - create the should_quit pipe (for signal handler)
553 * - create the thread pipe (for splice)
555 * Takes a function pointer as argument, this function is called when data is
556 * available on a buffer. This function is responsible to do the
557 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
558 * buffer configuration and then kernctl_put_next_subbuf at the end.
560 * Returns a pointer to the new context or NULL on error.
562 struct lttng_consumer_local_data
*lttng_consumer_create(
563 enum lttng_consumer_type type
,
564 int (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
565 struct lttng_consumer_local_data
*ctx
),
566 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
567 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
568 int (*update_stream
)(int stream_key
, uint32_t state
))
571 struct lttng_consumer_local_data
*ctx
;
573 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
574 consumer_data
.type
== type
);
575 consumer_data
.type
= type
;
577 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
579 perror("allocating context");
583 ctx
->consumer_error_socket
= -1;
584 /* assign the callbacks */
585 ctx
->on_buffer_ready
= buffer_ready
;
586 ctx
->on_recv_channel
= recv_channel
;
587 ctx
->on_recv_stream
= recv_stream
;
588 ctx
->on_update_stream
= update_stream
;
590 ret
= pipe(ctx
->consumer_poll_pipe
);
592 perror("Error creating poll pipe");
593 goto error_poll_pipe
;
596 ret
= pipe(ctx
->consumer_should_quit
);
598 perror("Error creating recv pipe");
599 goto error_quit_pipe
;
602 ret
= pipe(ctx
->consumer_thread_pipe
);
604 perror("Error creating thread pipe");
605 goto error_thread_pipe
;
612 for (i
= 0; i
< 2; i
++) {
615 err
= close(ctx
->consumer_should_quit
[i
]);
619 for (i
= 0; i
< 2; i
++) {
622 err
= close(ctx
->consumer_poll_pipe
[i
]);
632 * Close all fds associated with the instance and free the context.
634 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
636 close(ctx
->consumer_error_socket
);
637 close(ctx
->consumer_thread_pipe
[0]);
638 close(ctx
->consumer_thread_pipe
[1]);
639 close(ctx
->consumer_poll_pipe
[0]);
640 close(ctx
->consumer_poll_pipe
[1]);
641 close(ctx
->consumer_should_quit
[0]);
642 close(ctx
->consumer_should_quit
[1]);
643 unlink(ctx
->consumer_command_sock_path
);
648 * Mmap the ring buffer, read it and write the data to the tracefile.
650 * Returns the number of bytes written
652 int lttng_consumer_on_read_subbuffer_mmap(
653 struct lttng_consumer_local_data
*ctx
,
654 struct lttng_consumer_stream
*stream
, unsigned long len
)
656 switch (consumer_data
.type
) {
657 case LTTNG_CONSUMER_KERNEL
:
658 return lttng_kconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
659 case LTTNG_CONSUMER_UST
:
660 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
662 ERR("Unknown consumer_data type");
668 * Splice the data from the ring buffer to the tracefile.
670 * Returns the number of bytes spliced.
672 int lttng_consumer_on_read_subbuffer_splice(
673 struct lttng_consumer_local_data
*ctx
,
674 struct lttng_consumer_stream
*stream
, unsigned long len
)
676 switch (consumer_data
.type
) {
677 case LTTNG_CONSUMER_KERNEL
:
678 return lttng_kconsumer_on_read_subbuffer_splice(ctx
, stream
, len
);
679 case LTTNG_CONSUMER_UST
:
682 ERR("Unknown consumer_data type");
690 * Take a snapshot for a specific fd
692 * Returns 0 on success, < 0 on error
694 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
695 struct lttng_consumer_stream
*stream
)
697 switch (consumer_data
.type
) {
698 case LTTNG_CONSUMER_KERNEL
:
699 return lttng_kconsumer_take_snapshot(ctx
, stream
);
700 case LTTNG_CONSUMER_UST
:
701 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
703 ERR("Unknown consumer_data type");
711 * Get the produced position
713 * Returns 0 on success, < 0 on error
715 int lttng_consumer_get_produced_snapshot(
716 struct lttng_consumer_local_data
*ctx
,
717 struct lttng_consumer_stream
*stream
,
720 switch (consumer_data
.type
) {
721 case LTTNG_CONSUMER_KERNEL
:
722 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
723 case LTTNG_CONSUMER_UST
:
724 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
726 ERR("Unknown consumer_data type");
732 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
733 int sock
, struct pollfd
*consumer_sockpoll
)
735 switch (consumer_data
.type
) {
736 case LTTNG_CONSUMER_KERNEL
:
737 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
738 case LTTNG_CONSUMER_UST
:
739 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
741 ERR("Unknown consumer_data type");
748 * This thread polls the fds in the ltt_fd_list to consume the data and write
749 * it to tracefile if necessary.
751 void *lttng_consumer_thread_poll_fds(void *data
)
753 int num_rdy
, num_hup
, high_prio
, ret
, i
;
754 struct pollfd
*pollfd
= NULL
;
755 /* local view of the streams */
756 struct lttng_consumer_stream
**local_stream
= NULL
;
757 /* local view of consumer_data.fds_count */
761 struct lttng_consumer_local_data
*ctx
= data
;
763 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
770 * the ltt_fd_list has been updated, we need to update our
771 * local array as well
773 pthread_mutex_lock(&consumer_data
.lock
);
774 if (consumer_data
.need_update
) {
775 if (pollfd
!= NULL
) {
779 if (local_stream
!= NULL
) {
784 /* allocate for all fds + 1 for the consumer_poll_pipe */
785 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
786 if (pollfd
== NULL
) {
787 perror("pollfd malloc");
788 pthread_mutex_unlock(&consumer_data
.lock
);
792 /* allocate for all fds + 1 for the consumer_poll_pipe */
793 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
794 sizeof(struct lttng_consumer_stream
));
795 if (local_stream
== NULL
) {
796 perror("local_stream malloc");
797 pthread_mutex_unlock(&consumer_data
.lock
);
800 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
802 ERR("Error in allocating pollfd or local_outfds");
803 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
804 pthread_mutex_unlock(&consumer_data
.lock
);
808 consumer_data
.need_update
= 0;
810 pthread_mutex_unlock(&consumer_data
.lock
);
812 /* poll on the array of fds */
813 DBG("polling on %d fd", nb_fd
+ 1);
814 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
815 DBG("poll num_rdy : %d", num_rdy
);
817 perror("Poll error");
818 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
820 } else if (num_rdy
== 0) {
821 DBG("Polling thread timed out");
825 /* No FDs and consumer_quit, consumer_cleanup the thread */
826 if (nb_fd
== 0 && consumer_quit
== 1) {
831 * If the consumer_poll_pipe triggered poll go
832 * directly to the beginning of the loop to update the
833 * array. We want to prioritize array update over
834 * low-priority reads.
836 if (pollfd
[nb_fd
].revents
& POLLIN
) {
837 DBG("consumer_poll_pipe wake up");
838 tmp2
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
840 perror("read consumer poll");
845 /* Take care of high priority channels first. */
846 for (i
= 0; i
< nb_fd
; i
++) {
847 if (pollfd
[i
].revents
& POLLPRI
) {
848 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
850 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
851 /* it's ok to have an unavailable sub-buffer */
855 } else if (pollfd
[i
].revents
& POLLERR
) {
856 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
857 consumer_del_stream(local_stream
[i
]);
859 } else if (pollfd
[i
].revents
& POLLNVAL
) {
860 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
861 consumer_del_stream(local_stream
[i
]);
863 } else if ((pollfd
[i
].revents
& POLLHUP
) &&
864 !(pollfd
[i
].revents
& POLLIN
)) {
865 if (consumer_data
.type
== LTTNG_CONSUMER_UST
) {
866 DBG("Polling fd %d tells it has hung up. Attempting flush and read.",
868 if (!local_stream
[i
]->hangup_flush_done
) {
869 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
870 /* read after flush */
872 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
873 } while (ret
== EAGAIN
);
876 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
878 consumer_del_stream(local_stream
[i
]);
883 /* If every buffer FD has hung up, we end the read loop here */
884 if (nb_fd
> 0 && num_hup
== nb_fd
) {
885 DBG("every buffer FD has hung up\n");
886 if (consumer_quit
== 1) {
892 /* Take care of low priority channels. */
893 if (high_prio
== 0) {
894 for (i
= 0; i
< nb_fd
; i
++) {
895 if (pollfd
[i
].revents
& POLLIN
) {
896 DBG("Normal read on fd %d", pollfd
[i
].fd
);
897 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
898 /* it's ok to have an unavailable subbuffer */
907 DBG("polling thread exiting");
908 if (pollfd
!= NULL
) {
912 if (local_stream
!= NULL
) {
920 * This thread listens on the consumerd socket and receives the file
921 * descriptors from the session daemon.
923 void *lttng_consumer_thread_receive_fds(void *data
)
925 int sock
, client_socket
, ret
;
927 * structure to poll for incoming data on communication socket avoids
928 * making blocking sockets.
930 struct pollfd consumer_sockpoll
[2];
931 struct lttng_consumer_local_data
*ctx
= data
;
933 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
934 unlink(ctx
->consumer_command_sock_path
);
935 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
936 if (client_socket
< 0) {
937 ERR("Cannot create command socket");
941 ret
= lttcomm_listen_unix_sock(client_socket
);
946 DBG("Sending ready command to lttng-sessiond");
947 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
948 /* return < 0 on error, but == 0 is not fatal */
950 ERR("Error sending ready command to lttng-sessiond");
954 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
956 perror("fcntl O_NONBLOCK");
960 /* prepare the FDs to poll : to client socket and the should_quit pipe */
961 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
962 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
963 consumer_sockpoll
[1].fd
= client_socket
;
964 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
966 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
969 DBG("Connection on client_socket");
971 /* Blocking call, waiting for transmission */
972 sock
= lttcomm_accept_unix_sock(client_socket
);
977 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
979 perror("fcntl O_NONBLOCK");
983 /* update the polling structure to poll on the established socket */
984 consumer_sockpoll
[1].fd
= sock
;
985 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
988 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
991 DBG("Incoming command on sock");
992 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
993 if (ret
== -ENOENT
) {
994 DBG("Received STOP command");
998 ERR("Communication interrupted on command socket");
1001 if (consumer_quit
) {
1002 DBG("consumer_thread_receive_fds received quit from signal");
1005 DBG("received fds on sock");
1008 DBG("consumer_thread_receive_fds exiting");
1011 * when all fds have hung up, the polling thread
1017 * 2s of grace period, if no polling events occur during
1018 * this period, the polling thread will exit even if there
1019 * are still open FDs (should not happen, but safety mechanism).
1021 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1023 /* wake up the polling thread */
1024 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
1026 perror("poll pipe write");
1031 int lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1032 struct lttng_consumer_local_data
*ctx
)
1034 switch (consumer_data
.type
) {
1035 case LTTNG_CONSUMER_KERNEL
:
1036 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1037 case LTTNG_CONSUMER_UST
:
1038 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1040 ERR("Unknown consumer_data type");
1046 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1048 switch (consumer_data
.type
) {
1049 case LTTNG_CONSUMER_KERNEL
:
1050 return lttng_kconsumer_on_recv_stream(stream
);
1051 case LTTNG_CONSUMER_UST
:
1052 return lttng_ustconsumer_on_recv_stream(stream
);
1054 ERR("Unknown consumer_data type");