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
= {
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 */
70 lttng_ht_lookup(consumer_data
.stream_ht
, (void *)((unsigned long) key
),
72 node
= lttng_ht_iter_get_node_ulong(&iter
);
74 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
80 static void consumer_steal_stream_key(int key
)
82 struct lttng_consumer_stream
*stream
;
84 stream
= consumer_find_stream(key
);
89 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
91 struct lttng_ht_iter iter
;
92 struct lttng_ht_node_ulong
*node
;
93 struct lttng_consumer_channel
*channel
= NULL
;
95 /* Negative keys are lookup failures */
99 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
101 node
= lttng_ht_iter_get_node_ulong(&iter
);
103 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
109 static void consumer_steal_channel_key(int key
)
111 struct lttng_consumer_channel
*channel
;
113 channel
= consumer_find_channel(key
);
119 * Remove a stream from the global list protected by a mutex. This
120 * function is also responsible for freeing its data structures.
122 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
125 struct lttng_ht_iter iter
;
126 struct lttng_consumer_channel
*free_chan
= NULL
;
128 pthread_mutex_lock(&consumer_data
.lock
);
130 switch (consumer_data
.type
) {
131 case LTTNG_CONSUMER_KERNEL
:
132 if (stream
->mmap_base
!= NULL
) {
133 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
139 case LTTNG_CONSUMER32_UST
:
140 case LTTNG_CONSUMER64_UST
:
141 lttng_ustconsumer_del_stream(stream
);
144 ERR("Unknown consumer_data type");
149 /* Get stream node from hash table */
150 lttng_ht_lookup(consumer_data
.stream_ht
,
151 (void *)((unsigned long) stream
->key
), &iter
);
152 /* Remove stream node from hash table */
153 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
156 if (consumer_data
.stream_count
<= 0) {
159 consumer_data
.stream_count
--;
163 if (stream
->out_fd
>= 0) {
164 close(stream
->out_fd
);
166 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
167 close(stream
->wait_fd
);
169 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
170 && !stream
->shm_fd_is_copy
) {
171 close(stream
->shm_fd
);
173 if (!--stream
->chan
->refcount
)
174 free_chan
= stream
->chan
;
177 consumer_data
.need_update
= 1;
178 pthread_mutex_unlock(&consumer_data
.lock
);
181 consumer_del_channel(free_chan
);
184 struct lttng_consumer_stream
*consumer_allocate_stream(
185 int channel_key
, int stream_key
,
186 int shm_fd
, int wait_fd
,
187 enum lttng_consumer_stream_state state
,
189 enum lttng_event_output output
,
190 const char *path_name
,
194 struct lttng_consumer_stream
*stream
;
197 stream
= zmalloc(sizeof(*stream
));
198 if (stream
== NULL
) {
199 perror("malloc struct lttng_consumer_stream");
202 stream
->chan
= consumer_find_channel(channel_key
);
204 perror("Unable to find channel key");
207 stream
->chan
->refcount
++;
208 stream
->key
= stream_key
;
209 stream
->shm_fd
= shm_fd
;
210 stream
->wait_fd
= wait_fd
;
212 stream
->out_fd_offset
= 0;
213 stream
->state
= state
;
214 stream
->mmap_len
= mmap_len
;
215 stream
->mmap_base
= NULL
;
216 stream
->output
= output
;
219 strncpy(stream
->path_name
, path_name
, PATH_MAX
- 1);
220 stream
->path_name
[PATH_MAX
- 1] = '\0';
221 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
223 switch (consumer_data
.type
) {
224 case LTTNG_CONSUMER_KERNEL
:
226 case LTTNG_CONSUMER32_UST
:
227 case LTTNG_CONSUMER64_UST
:
228 stream
->cpu
= stream
->chan
->cpucount
++;
229 ret
= lttng_ustconsumer_allocate_stream(stream
);
236 ERR("Unknown consumer_data type");
240 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
241 stream
->path_name
, stream
->key
,
244 (unsigned long long) stream
->mmap_len
,
251 * Add a stream to the global list protected by a mutex.
253 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
257 pthread_mutex_lock(&consumer_data
.lock
);
258 /* Steal stream identifier, for UST */
259 consumer_steal_stream_key(stream
->key
);
260 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
261 consumer_data
.stream_count
++;
262 consumer_data
.need_update
= 1;
264 switch (consumer_data
.type
) {
265 case LTTNG_CONSUMER_KERNEL
:
267 case LTTNG_CONSUMER32_UST
:
268 case LTTNG_CONSUMER64_UST
:
269 /* Streams are in CPU number order (we rely on this) */
270 stream
->cpu
= stream
->chan
->nr_streams
++;
273 ERR("Unknown consumer_data type");
279 pthread_mutex_unlock(&consumer_data
.lock
);
284 * Update a stream according to what we just received.
286 void consumer_change_stream_state(int stream_key
,
287 enum lttng_consumer_stream_state state
)
289 struct lttng_consumer_stream
*stream
;
291 pthread_mutex_lock(&consumer_data
.lock
);
292 stream
= consumer_find_stream(stream_key
);
294 stream
->state
= state
;
296 consumer_data
.need_update
= 1;
297 pthread_mutex_unlock(&consumer_data
.lock
);
301 * Remove a channel from the global list protected by a mutex. This
302 * function is also responsible for freeing its data structures.
304 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
307 struct lttng_ht_iter iter
;
309 pthread_mutex_lock(&consumer_data
.lock
);
311 switch (consumer_data
.type
) {
312 case LTTNG_CONSUMER_KERNEL
:
314 case LTTNG_CONSUMER32_UST
:
315 case LTTNG_CONSUMER64_UST
:
316 lttng_ustconsumer_del_channel(channel
);
319 ERR("Unknown consumer_data type");
324 lttng_ht_lookup(consumer_data
.channel_ht
,
325 (void *)((unsigned long) channel
->key
), &iter
);
326 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
329 if (channel
->mmap_base
!= NULL
) {
330 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
335 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
336 close(channel
->wait_fd
);
338 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
339 && !channel
->shm_fd_is_copy
) {
340 close(channel
->shm_fd
);
344 pthread_mutex_unlock(&consumer_data
.lock
);
347 struct lttng_consumer_channel
*consumer_allocate_channel(
349 int shm_fd
, int wait_fd
,
351 uint64_t max_sb_size
)
353 struct lttng_consumer_channel
*channel
;
356 channel
= zmalloc(sizeof(*channel
));
357 if (channel
== NULL
) {
358 perror("malloc struct lttng_consumer_channel");
361 channel
->key
= channel_key
;
362 channel
->shm_fd
= shm_fd
;
363 channel
->wait_fd
= wait_fd
;
364 channel
->mmap_len
= mmap_len
;
365 channel
->max_sb_size
= max_sb_size
;
366 channel
->refcount
= 0;
367 channel
->nr_streams
= 0;
368 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
370 switch (consumer_data
.type
) {
371 case LTTNG_CONSUMER_KERNEL
:
372 channel
->mmap_base
= NULL
;
373 channel
->mmap_len
= 0;
375 case LTTNG_CONSUMER32_UST
:
376 case LTTNG_CONSUMER64_UST
:
377 ret
= lttng_ustconsumer_allocate_channel(channel
);
384 ERR("Unknown consumer_data type");
388 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
392 (unsigned long long) channel
->mmap_len
,
393 (unsigned long long) channel
->max_sb_size
);
399 * Add a channel to the global list protected by a mutex.
401 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
403 pthread_mutex_lock(&consumer_data
.lock
);
404 /* Steal channel identifier, for UST */
405 consumer_steal_channel_key(channel
->key
);
406 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
407 pthread_mutex_unlock(&consumer_data
.lock
);
412 * Allocate the pollfd structure and the local view of the out fds to avoid
413 * doing a lookup in the linked list and concurrency issues when writing is
414 * needed. Called with consumer_data.lock held.
416 * Returns the number of fds in the structures.
418 int consumer_update_poll_array(
419 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
420 struct lttng_consumer_stream
**local_stream
)
423 struct lttng_ht_iter iter
;
424 struct lttng_consumer_stream
*stream
;
426 DBG("Updating poll fd array");
427 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
429 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
432 DBG("Active FD %d", stream
->wait_fd
);
433 (*pollfd
)[i
].fd
= stream
->wait_fd
;
434 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
435 local_stream
[i
] = stream
;
440 * Insert the consumer_poll_pipe at the end of the array and don't
441 * increment i so nb_fd is the number of real FD.
443 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
444 (*pollfd
)[i
].events
= POLLIN
;
449 * Poll on the should_quit pipe and the command socket return -1 on error and
450 * should exit, 0 if data is available on the command socket
452 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
456 num_rdy
= poll(consumer_sockpoll
, 2, -1);
458 perror("Poll error");
461 if (consumer_sockpoll
[0].revents
== POLLIN
) {
462 DBG("consumer_should_quit wake up");
472 * Set the error socket.
474 void lttng_consumer_set_error_sock(
475 struct lttng_consumer_local_data
*ctx
, int sock
)
477 ctx
->consumer_error_socket
= sock
;
481 * Set the command socket path.
484 void lttng_consumer_set_command_sock_path(
485 struct lttng_consumer_local_data
*ctx
, char *sock
)
487 ctx
->consumer_command_sock_path
= sock
;
491 * Send return code to the session daemon.
492 * If the socket is not defined, we return 0, it is not a fatal error
494 int lttng_consumer_send_error(
495 struct lttng_consumer_local_data
*ctx
, int cmd
)
497 if (ctx
->consumer_error_socket
> 0) {
498 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
499 sizeof(enum lttcomm_sessiond_command
));
506 * Close all the tracefiles and stream fds, should be called when all instances
509 void lttng_consumer_cleanup(void)
512 struct lttng_ht_iter iter
;
513 struct lttng_consumer_stream
*stream
;
514 struct lttng_consumer_channel
*channel
;
517 * close all outfd. Called when there are no more threads
518 * running (after joining on the threads), no need to protect
519 * list iteration with mutex.
521 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
523 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
525 consumer_del_stream(stream
);
528 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
530 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
532 consumer_del_channel(channel
);
537 * Called from signal handler.
539 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
543 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
545 perror("write consumer quit");
549 void lttng_consumer_sync_trace_file(
550 struct lttng_consumer_stream
*stream
, off_t orig_offset
)
552 int outfd
= stream
->out_fd
;
555 * This does a blocking write-and-wait on any page that belongs to the
556 * subbuffer prior to the one we just wrote.
557 * Don't care about error values, as these are just hints and ways to
558 * limit the amount of page cache used.
560 if (orig_offset
< stream
->chan
->max_sb_size
) {
563 sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
564 stream
->chan
->max_sb_size
,
565 SYNC_FILE_RANGE_WAIT_BEFORE
566 | SYNC_FILE_RANGE_WRITE
567 | SYNC_FILE_RANGE_WAIT_AFTER
);
569 * Give hints to the kernel about how we access the file:
570 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
573 * We need to call fadvise again after the file grows because the
574 * kernel does not seem to apply fadvise to non-existing parts of the
577 * Call fadvise _after_ having waited for the page writeback to
578 * complete because the dirty page writeback semantic is not well
579 * defined. So it can be expected to lead to lower throughput in
582 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
583 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
587 * Initialise the necessary environnement :
588 * - create a new context
589 * - create the poll_pipe
590 * - create the should_quit pipe (for signal handler)
591 * - create the thread pipe (for splice)
593 * Takes a function pointer as argument, this function is called when data is
594 * available on a buffer. This function is responsible to do the
595 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
596 * buffer configuration and then kernctl_put_next_subbuf at the end.
598 * Returns a pointer to the new context or NULL on error.
600 struct lttng_consumer_local_data
*lttng_consumer_create(
601 enum lttng_consumer_type type
,
602 int (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
603 struct lttng_consumer_local_data
*ctx
),
604 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
605 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
606 int (*update_stream
)(int stream_key
, uint32_t state
))
609 struct lttng_consumer_local_data
*ctx
;
611 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
612 consumer_data
.type
== type
);
613 consumer_data
.type
= type
;
615 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
617 perror("allocating context");
621 ctx
->consumer_error_socket
= -1;
622 /* assign the callbacks */
623 ctx
->on_buffer_ready
= buffer_ready
;
624 ctx
->on_recv_channel
= recv_channel
;
625 ctx
->on_recv_stream
= recv_stream
;
626 ctx
->on_update_stream
= update_stream
;
628 ret
= pipe(ctx
->consumer_poll_pipe
);
630 perror("Error creating poll pipe");
631 goto error_poll_pipe
;
634 ret
= pipe(ctx
->consumer_should_quit
);
636 perror("Error creating recv pipe");
637 goto error_quit_pipe
;
640 ret
= pipe(ctx
->consumer_thread_pipe
);
642 perror("Error creating thread pipe");
643 goto error_thread_pipe
;
650 for (i
= 0; i
< 2; i
++) {
653 err
= close(ctx
->consumer_should_quit
[i
]);
657 for (i
= 0; i
< 2; i
++) {
660 err
= close(ctx
->consumer_poll_pipe
[i
]);
670 * Close all fds associated with the instance and free the context.
672 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
674 close(ctx
->consumer_error_socket
);
675 close(ctx
->consumer_thread_pipe
[0]);
676 close(ctx
->consumer_thread_pipe
[1]);
677 close(ctx
->consumer_poll_pipe
[0]);
678 close(ctx
->consumer_poll_pipe
[1]);
679 close(ctx
->consumer_should_quit
[0]);
680 close(ctx
->consumer_should_quit
[1]);
681 unlink(ctx
->consumer_command_sock_path
);
686 * Mmap the ring buffer, read it and write the data to the tracefile.
688 * Returns the number of bytes written
690 int lttng_consumer_on_read_subbuffer_mmap(
691 struct lttng_consumer_local_data
*ctx
,
692 struct lttng_consumer_stream
*stream
, unsigned long len
)
694 switch (consumer_data
.type
) {
695 case LTTNG_CONSUMER_KERNEL
:
696 return lttng_kconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
697 case LTTNG_CONSUMER32_UST
:
698 case LTTNG_CONSUMER64_UST
:
699 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
701 ERR("Unknown consumer_data type");
707 * Splice the data from the ring buffer to the tracefile.
709 * Returns the number of bytes spliced.
711 int lttng_consumer_on_read_subbuffer_splice(
712 struct lttng_consumer_local_data
*ctx
,
713 struct lttng_consumer_stream
*stream
, unsigned long len
)
715 switch (consumer_data
.type
) {
716 case LTTNG_CONSUMER_KERNEL
:
717 return lttng_kconsumer_on_read_subbuffer_splice(ctx
, stream
, len
);
718 case LTTNG_CONSUMER32_UST
:
719 case LTTNG_CONSUMER64_UST
:
722 ERR("Unknown consumer_data type");
730 * Take a snapshot for a specific fd
732 * Returns 0 on success, < 0 on error
734 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
735 struct lttng_consumer_stream
*stream
)
737 switch (consumer_data
.type
) {
738 case LTTNG_CONSUMER_KERNEL
:
739 return lttng_kconsumer_take_snapshot(ctx
, stream
);
740 case LTTNG_CONSUMER32_UST
:
741 case LTTNG_CONSUMER64_UST
:
742 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
744 ERR("Unknown consumer_data type");
752 * Get the produced position
754 * Returns 0 on success, < 0 on error
756 int lttng_consumer_get_produced_snapshot(
757 struct lttng_consumer_local_data
*ctx
,
758 struct lttng_consumer_stream
*stream
,
761 switch (consumer_data
.type
) {
762 case LTTNG_CONSUMER_KERNEL
:
763 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
764 case LTTNG_CONSUMER32_UST
:
765 case LTTNG_CONSUMER64_UST
:
766 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
768 ERR("Unknown consumer_data type");
774 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
775 int sock
, struct pollfd
*consumer_sockpoll
)
777 switch (consumer_data
.type
) {
778 case LTTNG_CONSUMER_KERNEL
:
779 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
780 case LTTNG_CONSUMER32_UST
:
781 case LTTNG_CONSUMER64_UST
:
782 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
784 ERR("Unknown consumer_data type");
791 * This thread polls the fds in the set to consume the data and write
792 * it to tracefile if necessary.
794 void *lttng_consumer_thread_poll_fds(void *data
)
796 int num_rdy
, num_hup
, high_prio
, ret
, i
;
797 struct pollfd
*pollfd
= NULL
;
798 /* local view of the streams */
799 struct lttng_consumer_stream
**local_stream
= NULL
;
800 /* local view of consumer_data.fds_count */
804 struct lttng_consumer_local_data
*ctx
= data
;
806 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
813 * the fds set has been updated, we need to update our
814 * local array as well
816 pthread_mutex_lock(&consumer_data
.lock
);
817 if (consumer_data
.need_update
) {
818 if (pollfd
!= NULL
) {
822 if (local_stream
!= NULL
) {
827 /* allocate for all fds + 1 for the consumer_poll_pipe */
828 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
829 if (pollfd
== NULL
) {
830 perror("pollfd malloc");
831 pthread_mutex_unlock(&consumer_data
.lock
);
835 /* allocate for all fds + 1 for the consumer_poll_pipe */
836 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
837 sizeof(struct lttng_consumer_stream
));
838 if (local_stream
== NULL
) {
839 perror("local_stream malloc");
840 pthread_mutex_unlock(&consumer_data
.lock
);
843 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
845 ERR("Error in allocating pollfd or local_outfds");
846 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
847 pthread_mutex_unlock(&consumer_data
.lock
);
851 consumer_data
.need_update
= 0;
853 pthread_mutex_unlock(&consumer_data
.lock
);
855 /* poll on the array of fds */
856 DBG("polling on %d fd", nb_fd
+ 1);
857 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
858 DBG("poll num_rdy : %d", num_rdy
);
860 perror("Poll error");
861 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
863 } else if (num_rdy
== 0) {
864 DBG("Polling thread timed out");
868 /* No FDs and consumer_quit, consumer_cleanup the thread */
869 if (nb_fd
== 0 && consumer_quit
== 1) {
874 * If the consumer_poll_pipe triggered poll go
875 * directly to the beginning of the loop to update the
876 * array. We want to prioritize array update over
877 * low-priority reads.
879 if (pollfd
[nb_fd
].revents
& POLLIN
) {
880 DBG("consumer_poll_pipe wake up");
881 tmp2
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
883 perror("read consumer poll");
888 /* Take care of high priority channels first. */
889 for (i
= 0; i
< nb_fd
; i
++) {
890 if (pollfd
[i
].revents
& POLLPRI
) {
891 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
893 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
894 /* it's ok to have an unavailable sub-buffer */
898 } else if (pollfd
[i
].revents
& POLLERR
) {
899 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
900 consumer_del_stream(local_stream
[i
]);
902 } else if (pollfd
[i
].revents
& POLLNVAL
) {
903 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
904 consumer_del_stream(local_stream
[i
]);
906 } else if ((pollfd
[i
].revents
& POLLHUP
) &&
907 !(pollfd
[i
].revents
& POLLIN
)) {
908 if (consumer_data
.type
== LTTNG_CONSUMER32_UST
909 || consumer_data
.type
== LTTNG_CONSUMER64_UST
) {
910 DBG("Polling fd %d tells it has hung up. Attempting flush and read.",
912 if (!local_stream
[i
]->hangup_flush_done
) {
913 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
914 /* read after flush */
916 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
917 } while (ret
== EAGAIN
);
920 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
922 consumer_del_stream(local_stream
[i
]);
927 /* If every buffer FD has hung up, we end the read loop here */
928 if (nb_fd
> 0 && num_hup
== nb_fd
) {
929 DBG("every buffer FD has hung up\n");
930 if (consumer_quit
== 1) {
936 /* Take care of low priority channels. */
937 if (high_prio
== 0) {
938 for (i
= 0; i
< nb_fd
; i
++) {
939 if (pollfd
[i
].revents
& POLLIN
) {
940 DBG("Normal read on fd %d", pollfd
[i
].fd
);
941 ret
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
942 /* it's ok to have an unavailable subbuffer */
951 DBG("polling thread exiting");
952 if (pollfd
!= NULL
) {
956 if (local_stream
!= NULL
) {
964 * This thread listens on the consumerd socket and receives the file
965 * descriptors from the session daemon.
967 void *lttng_consumer_thread_receive_fds(void *data
)
969 int sock
, client_socket
, ret
;
971 * structure to poll for incoming data on communication socket avoids
972 * making blocking sockets.
974 struct pollfd consumer_sockpoll
[2];
975 struct lttng_consumer_local_data
*ctx
= data
;
977 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
978 unlink(ctx
->consumer_command_sock_path
);
979 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
980 if (client_socket
< 0) {
981 ERR("Cannot create command socket");
985 ret
= lttcomm_listen_unix_sock(client_socket
);
990 DBG("Sending ready command to lttng-sessiond");
991 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
992 /* return < 0 on error, but == 0 is not fatal */
994 ERR("Error sending ready command to lttng-sessiond");
998 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1000 perror("fcntl O_NONBLOCK");
1004 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1005 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1006 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1007 consumer_sockpoll
[1].fd
= client_socket
;
1008 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1010 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1013 DBG("Connection on client_socket");
1015 /* Blocking call, waiting for transmission */
1016 sock
= lttcomm_accept_unix_sock(client_socket
);
1021 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1023 perror("fcntl O_NONBLOCK");
1027 /* update the polling structure to poll on the established socket */
1028 consumer_sockpoll
[1].fd
= sock
;
1029 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1032 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1035 DBG("Incoming command on sock");
1036 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1037 if (ret
== -ENOENT
) {
1038 DBG("Received STOP command");
1042 ERR("Communication interrupted on command socket");
1045 if (consumer_quit
) {
1046 DBG("consumer_thread_receive_fds received quit from signal");
1049 DBG("received fds on sock");
1052 DBG("consumer_thread_receive_fds exiting");
1055 * when all fds have hung up, the polling thread
1061 * 2s of grace period, if no polling events occur during
1062 * this period, the polling thread will exit even if there
1063 * are still open FDs (should not happen, but safety mechanism).
1065 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1067 /* wake up the polling thread */
1068 ret
= write(ctx
->consumer_poll_pipe
[1], "4", 1);
1070 perror("poll pipe write");
1075 int lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1076 struct lttng_consumer_local_data
*ctx
)
1078 switch (consumer_data
.type
) {
1079 case LTTNG_CONSUMER_KERNEL
:
1080 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1081 case LTTNG_CONSUMER32_UST
:
1082 case LTTNG_CONSUMER64_UST
:
1083 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1085 ERR("Unknown consumer_data type");
1091 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1093 switch (consumer_data
.type
) {
1094 case LTTNG_CONSUMER_KERNEL
:
1095 return lttng_kconsumer_on_recv_stream(stream
);
1096 case LTTNG_CONSUMER32_UST
:
1097 case LTTNG_CONSUMER64_UST
:
1098 return lttng_ustconsumer_on_recv_stream(stream
);
1100 ERR("Unknown consumer_data type");
1107 * Allocate and set consumer data hash tables.
1109 void lttng_consumer_init(void)
1111 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1112 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);