2 * Copyright (C) 2012 - Julien Desfossez <julien.desfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * 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 with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <bin/lttng-sessiond/ust-ctl.h>
25 #include <bin/lttng-consumerd/health-consumerd.h>
26 #include <common/common.h>
27 #include <common/compat/endian.h>
28 #include <common/kernel-ctl/kernel-ctl.h>
29 #include <common/kernel-consumer/kernel-consumer.h>
30 #include <common/consumer/consumer-stream.h>
31 #include <common/consumer/consumer-timer.h>
32 #include <common/consumer/consumer-testpoint.h>
33 #include <common/ust-consumer/ust-consumer.h>
35 typedef int (*sample_positions_cb
)(struct lttng_consumer_stream
*stream
);
36 typedef int (*get_consumed_cb
)(struct lttng_consumer_stream
*stream
,
37 unsigned long *consumed
);
38 typedef int (*get_produced_cb
)(struct lttng_consumer_stream
*stream
,
39 unsigned long *produced
);
41 static struct timer_signal_data timer_signal
= {
45 .lock
= PTHREAD_MUTEX_INITIALIZER
,
49 * Set custom signal mask to current thread.
51 static void setmask(sigset_t
*mask
)
55 ret
= sigemptyset(mask
);
57 PERROR("sigemptyset");
59 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_SWITCH
);
61 PERROR("sigaddset switch");
63 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_TEARDOWN
);
65 PERROR("sigaddset teardown");
67 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_LIVE
);
69 PERROR("sigaddset live");
71 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_MONITOR
);
73 PERROR("sigaddset monitor");
77 static int channel_monitor_pipe
= -1;
80 * Execute action on a timer switch.
82 * Beware: metadata_switch_timer() should *never* take a mutex also held
83 * while consumer_timer_switch_stop() is called. It would result in
86 static void metadata_switch_timer(struct lttng_consumer_local_data
*ctx
,
87 int sig
, siginfo_t
*si
)
90 struct lttng_consumer_channel
*channel
;
92 channel
= si
->si_value
.sival_ptr
;
95 if (channel
->switch_timer_error
) {
99 DBG("Switch timer for channel %" PRIu64
, channel
->key
);
101 case LTTNG_CONSUMER32_UST
:
102 case LTTNG_CONSUMER64_UST
:
104 * Locks taken by lttng_ustconsumer_request_metadata():
105 * - metadata_socket_lock
106 * - Calling lttng_ustconsumer_recv_metadata():
107 * - channel->metadata_cache->lock
108 * - Calling consumer_metadata_cache_flushed():
109 * - channel->timer_lock
110 * - channel->metadata_cache->lock
112 * Ensure that neither consumer_data.lock nor
113 * channel->lock are taken within this function, since
114 * they are held while consumer_timer_switch_stop() is
117 ret
= lttng_ustconsumer_request_metadata(ctx
, channel
, 1, 1);
119 channel
->switch_timer_error
= 1;
122 case LTTNG_CONSUMER_KERNEL
:
123 case LTTNG_CONSUMER_UNKNOWN
:
129 static int send_empty_index(struct lttng_consumer_stream
*stream
, uint64_t ts
,
133 struct ctf_packet_index index
;
135 memset(&index
, 0, sizeof(index
));
136 index
.stream_id
= htobe64(stream_id
);
137 index
.timestamp_end
= htobe64(ts
);
138 ret
= consumer_stream_write_index(stream
, &index
);
147 int consumer_flush_kernel_index(struct lttng_consumer_stream
*stream
)
149 uint64_t ts
, stream_id
;
152 ret
= kernctl_get_current_timestamp(stream
->wait_fd
, &ts
);
154 ERR("Failed to get the current timestamp");
157 ret
= kernctl_buffer_flush(stream
->wait_fd
);
159 ERR("Failed to flush kernel stream");
162 ret
= kernctl_snapshot(stream
->wait_fd
);
164 if (ret
!= -EAGAIN
&& ret
!= -ENODATA
) {
165 PERROR("live timer kernel snapshot");
169 ret
= kernctl_get_stream_id(stream
->wait_fd
, &stream_id
);
171 PERROR("kernctl_get_stream_id");
174 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
175 ret
= send_empty_index(stream
, ts
, stream_id
);
185 static int check_kernel_stream(struct lttng_consumer_stream
*stream
)
190 * While holding the stream mutex, try to take a snapshot, if it
191 * succeeds, it means that data is ready to be sent, just let the data
192 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
193 * means that there is no data to read after the flush, so we can
194 * safely send the empty index.
196 * Doing a trylock and checking if waiting on metadata if
197 * trylock fails. Bail out of the stream is indeed waiting for
198 * metadata to be pushed. Busy wait on trylock otherwise.
201 ret
= pthread_mutex_trylock(&stream
->lock
);
204 break; /* We have the lock. */
206 pthread_mutex_lock(&stream
->metadata_timer_lock
);
207 if (stream
->waiting_on_metadata
) {
209 stream
->missed_metadata_flush
= true;
210 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
211 goto end
; /* Bail out. */
213 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
218 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
224 ret
= consumer_flush_kernel_index(stream
);
225 pthread_mutex_unlock(&stream
->lock
);
230 int consumer_flush_ust_index(struct lttng_consumer_stream
*stream
)
232 uint64_t ts
, stream_id
;
235 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
240 ret
= lttng_ustconsumer_get_current_timestamp(stream
, &ts
);
242 ERR("Failed to get the current timestamp");
245 lttng_ustconsumer_flush_buffer(stream
, 1);
246 ret
= lttng_ustconsumer_take_snapshot(stream
);
248 if (ret
!= -EAGAIN
) {
249 ERR("Taking UST snapshot");
253 ret
= lttng_ustconsumer_get_stream_id(stream
, &stream_id
);
255 PERROR("ustctl_get_stream_id");
258 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
259 ret
= send_empty_index(stream
, ts
, stream_id
);
269 static int check_ust_stream(struct lttng_consumer_stream
*stream
)
274 assert(stream
->ustream
);
276 * While holding the stream mutex, try to take a snapshot, if it
277 * succeeds, it means that data is ready to be sent, just let the data
278 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
279 * means that there is no data to read after the flush, so we can
280 * safely send the empty index.
282 * Doing a trylock and checking if waiting on metadata if
283 * trylock fails. Bail out of the stream is indeed waiting for
284 * metadata to be pushed. Busy wait on trylock otherwise.
287 ret
= pthread_mutex_trylock(&stream
->lock
);
290 break; /* We have the lock. */
292 pthread_mutex_lock(&stream
->metadata_timer_lock
);
293 if (stream
->waiting_on_metadata
) {
295 stream
->missed_metadata_flush
= true;
296 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
297 goto end
; /* Bail out. */
299 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
304 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
310 ret
= consumer_flush_ust_index(stream
);
311 pthread_mutex_unlock(&stream
->lock
);
317 * Execute action on a live timer
319 static void live_timer(struct lttng_consumer_local_data
*ctx
,
320 int sig
, siginfo_t
*si
)
323 struct lttng_consumer_channel
*channel
;
324 struct lttng_consumer_stream
*stream
;
326 struct lttng_ht_iter iter
;
328 channel
= si
->si_value
.sival_ptr
;
331 if (channel
->switch_timer_error
) {
334 ht
= consumer_data
.stream_per_chan_id_ht
;
336 DBG("Live timer for channel %" PRIu64
, channel
->key
);
340 case LTTNG_CONSUMER32_UST
:
341 case LTTNG_CONSUMER64_UST
:
342 cds_lfht_for_each_entry_duplicate(ht
->ht
,
343 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
344 ht
->match_fct
, &channel
->key
, &iter
.iter
,
345 stream
, node_channel_id
.node
) {
346 ret
= check_ust_stream(stream
);
352 case LTTNG_CONSUMER_KERNEL
:
353 cds_lfht_for_each_entry_duplicate(ht
->ht
,
354 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
355 ht
->match_fct
, &channel
->key
, &iter
.iter
,
356 stream
, node_channel_id
.node
) {
357 ret
= check_kernel_stream(stream
);
363 case LTTNG_CONSUMER_UNKNOWN
:
376 void consumer_timer_signal_thread_qs(unsigned int signr
)
378 sigset_t pending_set
;
382 * We need to be the only thread interacting with the thread
383 * that manages signals for teardown synchronization.
385 pthread_mutex_lock(&timer_signal
.lock
);
387 /* Ensure we don't have any signal queued for this channel. */
389 ret
= sigemptyset(&pending_set
);
391 PERROR("sigemptyset");
393 ret
= sigpending(&pending_set
);
395 PERROR("sigpending");
397 if (!sigismember(&pending_set
, LTTNG_CONSUMER_SIG_SWITCH
)) {
404 * From this point, no new signal handler will be fired that would try to
405 * access "chan". However, we still need to wait for any currently
406 * executing handler to complete.
409 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
413 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
416 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
418 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
423 pthread_mutex_unlock(&timer_signal
.lock
);
427 * Start a timer channel timer which will fire at a given interval
428 * (timer_interval_us)and fire a given signal (signal).
430 * Returns a negative value on error, 0 if a timer was created, and
431 * a positive value if no timer was created (not an error).
434 int consumer_channel_timer_start(timer_t
*timer_id
,
435 struct lttng_consumer_channel
*channel
,
436 unsigned int timer_interval_us
, int signal
)
438 int ret
= 0, delete_ret
;
440 struct itimerspec its
;
443 assert(channel
->key
);
445 if (timer_interval_us
== 0) {
446 /* No creation needed; not an error. */
451 sev
.sigev_notify
= SIGEV_SIGNAL
;
452 sev
.sigev_signo
= signal
;
453 sev
.sigev_value
.sival_ptr
= channel
;
454 ret
= timer_create(CLOCKID
, &sev
, timer_id
);
456 PERROR("timer_create");
460 its
.it_value
.tv_sec
= timer_interval_us
/ 1000000;
461 its
.it_value
.tv_nsec
= (timer_interval_us
% 1000000) * 1000;
462 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
463 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
465 ret
= timer_settime(*timer_id
, 0, &its
, NULL
);
467 PERROR("timer_settime");
468 goto error_destroy_timer
;
473 delete_ret
= timer_delete(*timer_id
);
474 if (delete_ret
== -1) {
475 PERROR("timer_delete");
481 int consumer_channel_timer_stop(timer_t
*timer_id
, int signal
)
485 ret
= timer_delete(*timer_id
);
487 PERROR("timer_delete");
491 consumer_timer_signal_thread_qs(signal
);
498 * Set the channel's switch timer.
500 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
501 unsigned int switch_timer_interval_us
)
506 assert(channel
->key
);
508 ret
= consumer_channel_timer_start(&channel
->switch_timer
, channel
,
509 switch_timer_interval_us
, LTTNG_CONSUMER_SIG_SWITCH
);
511 channel
->switch_timer_enabled
= !!(ret
== 0);
515 * Stop and delete the channel's switch timer.
517 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
523 ret
= consumer_channel_timer_stop(&channel
->switch_timer
,
524 LTTNG_CONSUMER_SIG_SWITCH
);
526 ERR("Failed to stop switch timer");
529 channel
->switch_timer_enabled
= 0;
533 * Set the channel's live timer.
535 void consumer_timer_live_start(struct lttng_consumer_channel
*channel
,
536 unsigned int live_timer_interval_us
)
541 assert(channel
->key
);
543 ret
= consumer_channel_timer_start(&channel
->live_timer
, channel
,
544 live_timer_interval_us
, LTTNG_CONSUMER_SIG_LIVE
);
546 channel
->live_timer_enabled
= !!(ret
== 0);
550 * Stop and delete the channel's live timer.
552 void consumer_timer_live_stop(struct lttng_consumer_channel
*channel
)
558 ret
= consumer_channel_timer_stop(&channel
->live_timer
,
559 LTTNG_CONSUMER_SIG_LIVE
);
561 ERR("Failed to stop live timer");
564 channel
->live_timer_enabled
= 0;
568 * Set the channel's monitoring timer.
570 * Returns a negative value on error, 0 if a timer was created, and
571 * a positive value if no timer was created (not an error).
573 int consumer_timer_monitor_start(struct lttng_consumer_channel
*channel
,
574 unsigned int monitor_timer_interval_us
)
579 assert(channel
->key
);
580 assert(!channel
->monitor_timer_enabled
);
582 ret
= consumer_channel_timer_start(&channel
->monitor_timer
, channel
,
583 monitor_timer_interval_us
, LTTNG_CONSUMER_SIG_MONITOR
);
584 channel
->monitor_timer_enabled
= !!(ret
== 0);
589 * Stop and delete the channel's monitoring timer.
591 int consumer_timer_monitor_stop(struct lttng_consumer_channel
*channel
)
596 assert(channel
->monitor_timer_enabled
);
598 ret
= consumer_channel_timer_stop(&channel
->monitor_timer
,
599 LTTNG_CONSUMER_SIG_MONITOR
);
601 ERR("Failed to stop live timer");
605 channel
->monitor_timer_enabled
= 0;
611 * Block the RT signals for the entire process. It must be called from the
612 * consumer main before creating the threads
614 int consumer_signal_init(void)
619 /* Block signal for entire process, so only our thread processes it. */
621 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
624 PERROR("pthread_sigmask");
631 int sample_channel_positions(struct lttng_consumer_channel
*channel
,
632 uint64_t *_highest_use
, uint64_t *_lowest_use
,
633 sample_positions_cb sample
, get_consumed_cb get_consumed
,
634 get_produced_cb get_produced
)
637 struct lttng_ht_iter iter
;
638 struct lttng_consumer_stream
*stream
;
639 bool empty_channel
= true;
640 uint64_t high
= 0, low
= UINT64_MAX
;
641 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
645 cds_lfht_for_each_entry_duplicate(ht
->ht
,
646 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
647 ht
->match_fct
, &channel
->key
,
648 &iter
.iter
, stream
, node_channel_id
.node
) {
649 unsigned long produced
, consumed
, usage
;
651 empty_channel
= false;
653 pthread_mutex_lock(&stream
->lock
);
654 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
658 ret
= sample(stream
);
660 ERR("Failed to take buffer position snapshot in monitor timer (ret = %d)", ret
);
661 pthread_mutex_unlock(&stream
->lock
);
664 ret
= get_consumed(stream
, &consumed
);
666 ERR("Failed to get buffer consumed position in monitor timer");
667 pthread_mutex_unlock(&stream
->lock
);
670 ret
= get_produced(stream
, &produced
);
672 ERR("Failed to get buffer produced position in monitor timer");
673 pthread_mutex_unlock(&stream
->lock
);
677 usage
= produced
- consumed
;
678 high
= (usage
> high
) ? usage
: high
;
679 low
= (usage
< low
) ? usage
: low
;
681 pthread_mutex_unlock(&stream
->lock
);
684 *_highest_use
= high
;
695 * Execute action on a monitor timer.
698 void monitor_timer(struct lttng_consumer_local_data
*ctx
,
699 struct lttng_consumer_channel
*channel
)
702 int channel_monitor_pipe
=
703 consumer_timer_thread_get_channel_monitor_pipe();
704 struct lttcomm_consumer_channel_monitor_msg msg
= {
707 sample_positions_cb sample
;
708 get_consumed_cb get_consumed
;
709 get_produced_cb get_produced
;
712 pthread_mutex_lock(&consumer_data
.lock
);
714 if (channel_monitor_pipe
< 0) {
718 switch (consumer_data
.type
) {
719 case LTTNG_CONSUMER_KERNEL
:
720 sample
= lttng_kconsumer_sample_snapshot_positions
;
721 get_consumed
= lttng_kconsumer_get_consumed_snapshot
;
722 get_produced
= lttng_kconsumer_get_produced_snapshot
;
724 case LTTNG_CONSUMER32_UST
:
725 case LTTNG_CONSUMER64_UST
:
726 sample
= lttng_ustconsumer_sample_snapshot_positions
;
727 get_consumed
= lttng_ustconsumer_get_consumed_snapshot
;
728 get_produced
= lttng_ustconsumer_get_produced_snapshot
;
734 ret
= sample_channel_positions(channel
, &msg
.highest
, &msg
.lowest
,
735 sample
, get_consumed
, get_produced
);
741 * Writes performed here are assumed to be atomic which is only
742 * guaranteed for sizes < than PIPE_BUF.
744 assert(sizeof(msg
) <= PIPE_BUF
);
747 ret
= write(channel_monitor_pipe
, &msg
, sizeof(msg
));
748 } while (ret
== -1 && errno
== EINTR
);
750 if (errno
== EAGAIN
) {
751 /* Not an error, the sample is merely dropped. */
752 DBG("Channel monitor pipe is full; dropping sample for channel key = %"PRIu64
,
755 PERROR("write to the channel monitor pipe");
758 DBG("Sent channel monitoring sample for channel key %" PRIu64
759 ", (highest = %" PRIu64
", lowest = %"PRIu64
")",
760 channel
->key
, msg
.highest
, msg
.lowest
);
763 pthread_mutex_unlock(&consumer_data
.lock
);
766 int consumer_timer_thread_get_channel_monitor_pipe(void)
768 return uatomic_read(&channel_monitor_pipe
);
771 int consumer_timer_thread_set_channel_monitor_pipe(int fd
)
775 ret
= uatomic_cmpxchg(&channel_monitor_pipe
, -1, fd
);
786 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
787 * LTTNG_CONSUMER_SIG_TEARDOWN, LTTNG_CONSUMER_SIG_LIVE, and
788 * LTTNG_CONSUMER_SIG_MONITOR.
790 void *consumer_timer_thread(void *data
)
795 struct lttng_consumer_local_data
*ctx
= data
;
797 rcu_register_thread();
799 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
801 if (testpoint(consumerd_thread_metadata_timer
)) {
802 goto error_testpoint
;
805 health_code_update();
807 /* Only self thread will receive signal mask. */
809 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
812 health_code_update();
815 signr
= sigwaitinfo(&mask
, &info
);
819 * NOTE: cascading conditions are used instead of a switch case
820 * since the use of SIGRTMIN in the definition of the signals'
821 * values prevents the reduction to an integer constant.
824 if (errno
!= EINTR
) {
825 PERROR("sigwaitinfo");
828 } else if (signr
== LTTNG_CONSUMER_SIG_SWITCH
) {
829 metadata_switch_timer(ctx
, info
.si_signo
, &info
);
830 } else if (signr
== LTTNG_CONSUMER_SIG_TEARDOWN
) {
832 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
834 DBG("Signal timer metadata thread teardown");
835 } else if (signr
== LTTNG_CONSUMER_SIG_LIVE
) {
836 live_timer(ctx
, info
.si_signo
, &info
);
837 } else if (signr
== LTTNG_CONSUMER_SIG_MONITOR
) {
838 struct lttng_consumer_channel
*channel
;
840 channel
= info
.si_value
.sival_ptr
;
841 monitor_timer(ctx
, channel
);
843 ERR("Unexpected signal %d\n", info
.si_signo
);
848 /* Only reached in testpoint error */
850 health_unregister(health_consumerd
);
852 rcu_unregister_thread();