2 * Copyright (C) 2012 Julien Desfossez <julien.desfossez@efficios.com>
3 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
14 #include <bin/lttng-consumerd/health-consumerd.h>
15 #include <common/common.h>
16 #include <common/compat/endian.h>
17 #include <common/kernel-ctl/kernel-ctl.h>
18 #include <common/kernel-consumer/kernel-consumer.h>
19 #include <common/consumer/consumer-stream.h>
20 #include <common/consumer/consumer-timer.h>
21 #include <common/consumer/consumer-testpoint.h>
22 #include <common/ust-consumer/ust-consumer.h>
24 typedef int (*sample_positions_cb
)(struct lttng_consumer_stream
*stream
);
25 typedef int (*get_consumed_cb
)(struct lttng_consumer_stream
*stream
,
26 unsigned long *consumed
);
27 typedef int (*get_produced_cb
)(struct lttng_consumer_stream
*stream
,
28 unsigned long *produced
);
29 typedef int (*flush_index_cb
)(struct lttng_consumer_stream
*stream
);
31 static struct timer_signal_data timer_signal
= {
35 .lock
= PTHREAD_MUTEX_INITIALIZER
,
39 * Set custom signal mask to current thread.
41 static void setmask(sigset_t
*mask
)
45 ret
= sigemptyset(mask
);
47 PERROR("sigemptyset");
49 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_SWITCH
);
51 PERROR("sigaddset switch");
53 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_TEARDOWN
);
55 PERROR("sigaddset teardown");
57 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_LIVE
);
59 PERROR("sigaddset live");
61 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_MONITOR
);
63 PERROR("sigaddset monitor");
65 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_EXIT
);
67 PERROR("sigaddset exit");
71 static int the_channel_monitor_pipe
= -1;
74 * Execute action on a timer switch.
76 * Beware: metadata_switch_timer() should *never* take a mutex also held
77 * while consumer_timer_switch_stop() is called. It would result in
80 static void metadata_switch_timer(struct lttng_consumer_local_data
*ctx
,
84 struct lttng_consumer_channel
*channel
;
86 channel
= si
->si_value
.sival_ptr
;
89 if (channel
->switch_timer_error
) {
93 DBG("Switch timer for channel %" PRIu64
, channel
->key
);
95 case LTTNG_CONSUMER32_UST
:
96 case LTTNG_CONSUMER64_UST
:
98 * Locks taken by lttng_ustconsumer_request_metadata():
99 * - metadata_socket_lock
100 * - Calling lttng_ustconsumer_recv_metadata():
101 * - channel->metadata_cache->lock
102 * - Calling consumer_metadata_cache_flushed():
103 * - channel->timer_lock
104 * - channel->metadata_cache->lock
106 * Ensure that neither consumer_data.lock nor
107 * channel->lock are taken within this function, since
108 * they are held while consumer_timer_switch_stop() is
111 ret
= lttng_ustconsumer_request_metadata(ctx
, channel
, 1, 1);
113 channel
->switch_timer_error
= 1;
116 case LTTNG_CONSUMER_KERNEL
:
117 case LTTNG_CONSUMER_UNKNOWN
:
123 static int send_empty_index(struct lttng_consumer_stream
*stream
, uint64_t ts
,
127 struct ctf_packet_index index
;
129 memset(&index
, 0, sizeof(index
));
130 index
.stream_id
= htobe64(stream_id
);
131 index
.timestamp_end
= htobe64(ts
);
132 ret
= consumer_stream_write_index(stream
, &index
);
141 int consumer_flush_kernel_index(struct lttng_consumer_stream
*stream
)
143 uint64_t ts
, stream_id
;
146 ret
= kernctl_get_current_timestamp(stream
->wait_fd
, &ts
);
148 ERR("Failed to get the current timestamp");
151 ret
= kernctl_buffer_flush(stream
->wait_fd
);
153 ERR("Failed to flush kernel stream");
156 ret
= kernctl_snapshot(stream
->wait_fd
);
158 if (ret
!= -EAGAIN
&& ret
!= -ENODATA
) {
159 PERROR("live timer kernel snapshot");
163 ret
= kernctl_get_stream_id(stream
->wait_fd
, &stream_id
);
165 PERROR("kernctl_get_stream_id");
168 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
169 ret
= send_empty_index(stream
, ts
, stream_id
);
179 static int check_stream(struct lttng_consumer_stream
*stream
,
180 flush_index_cb flush_index
)
185 * While holding the stream mutex, try to take a snapshot, if it
186 * succeeds, it means that data is ready to be sent, just let the data
187 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
188 * means that there is no data to read after the flush, so we can
189 * safely send the empty index.
191 * Doing a trylock and checking if waiting on metadata if
192 * trylock fails. Bail out of the stream is indeed waiting for
193 * metadata to be pushed. Busy wait on trylock otherwise.
196 ret
= pthread_mutex_trylock(&stream
->lock
);
199 break; /* We have the lock. */
201 pthread_mutex_lock(&stream
->metadata_timer_lock
);
202 if (stream
->waiting_on_metadata
) {
204 stream
->missed_metadata_flush
= true;
205 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
206 goto end
; /* Bail out. */
208 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
213 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
219 ret
= flush_index(stream
);
220 pthread_mutex_unlock(&stream
->lock
);
225 int consumer_flush_ust_index(struct lttng_consumer_stream
*stream
)
227 uint64_t ts
, stream_id
;
230 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
235 ret
= lttng_ustconsumer_get_current_timestamp(stream
, &ts
);
237 ERR("Failed to get the current timestamp");
240 ret
= lttng_ustconsumer_flush_buffer(stream
, 1);
242 ERR("Failed to flush buffer while flushing index");
245 ret
= lttng_ustconsumer_take_snapshot(stream
);
247 if (ret
!= -EAGAIN
) {
248 ERR("Taking UST snapshot");
252 ret
= lttng_ustconsumer_get_stream_id(stream
, &stream_id
);
254 PERROR("lttng_ust_ctl_get_stream_id");
257 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
258 ret
= send_empty_index(stream
, ts
, stream_id
);
269 * Execute action on a live timer
271 static void live_timer(struct lttng_consumer_local_data
*ctx
,
275 struct lttng_consumer_channel
*channel
;
276 struct lttng_consumer_stream
*stream
;
277 struct lttng_ht_iter iter
;
278 const struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
279 const flush_index_cb flush_index
=
280 ctx
->type
== LTTNG_CONSUMER_KERNEL
?
281 consumer_flush_kernel_index
:
282 consumer_flush_ust_index
;
284 channel
= si
->si_value
.sival_ptr
;
287 if (channel
->switch_timer_error
) {
291 DBG("Live timer for channel %" PRIu64
, channel
->key
);
294 cds_lfht_for_each_entry_duplicate(ht
->ht
,
295 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
296 ht
->match_fct
, &channel
->key
, &iter
.iter
,
297 stream
, node_channel_id
.node
) {
298 ret
= check_stream(stream
, flush_index
);
312 void consumer_timer_signal_thread_qs(unsigned int signr
)
314 sigset_t pending_set
;
318 * We need to be the only thread interacting with the thread
319 * that manages signals for teardown synchronization.
321 pthread_mutex_lock(&timer_signal
.lock
);
323 /* Ensure we don't have any signal queued for this channel. */
325 ret
= sigemptyset(&pending_set
);
327 PERROR("sigemptyset");
329 ret
= sigpending(&pending_set
);
331 PERROR("sigpending");
333 if (!sigismember(&pending_set
, signr
)) {
340 * From this point, no new signal handler will be fired that would try to
341 * access "chan". However, we still need to wait for any currently
342 * executing handler to complete.
345 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
349 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
352 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
354 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
359 pthread_mutex_unlock(&timer_signal
.lock
);
363 * Start a timer channel timer which will fire at a given interval
364 * (timer_interval_us)and fire a given signal (signal).
366 * Returns a negative value on error, 0 if a timer was created, and
367 * a positive value if no timer was created (not an error).
370 int consumer_channel_timer_start(timer_t
*timer_id
,
371 struct lttng_consumer_channel
*channel
,
372 unsigned int timer_interval_us
, int signal
)
374 int ret
= 0, delete_ret
;
375 struct sigevent sev
= {};
376 struct itimerspec its
;
379 assert(channel
->key
);
381 if (timer_interval_us
== 0) {
382 /* No creation needed; not an error. */
387 sev
.sigev_notify
= SIGEV_SIGNAL
;
388 sev
.sigev_signo
= signal
;
389 sev
.sigev_value
.sival_ptr
= channel
;
390 ret
= timer_create(CLOCKID
, &sev
, timer_id
);
392 PERROR("timer_create");
396 its
.it_value
.tv_sec
= timer_interval_us
/ 1000000;
397 its
.it_value
.tv_nsec
= (timer_interval_us
% 1000000) * 1000;
398 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
399 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
401 ret
= timer_settime(*timer_id
, 0, &its
, NULL
);
403 PERROR("timer_settime");
404 goto error_destroy_timer
;
409 delete_ret
= timer_delete(*timer_id
);
410 if (delete_ret
== -1) {
411 PERROR("timer_delete");
417 int consumer_channel_timer_stop(timer_t
*timer_id
, int signal
)
421 ret
= timer_delete(*timer_id
);
423 PERROR("timer_delete");
427 consumer_timer_signal_thread_qs(signal
);
434 * Set the channel's switch timer.
436 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
437 unsigned int switch_timer_interval_us
)
442 assert(channel
->key
);
444 ret
= consumer_channel_timer_start(&channel
->switch_timer
, channel
,
445 switch_timer_interval_us
, LTTNG_CONSUMER_SIG_SWITCH
);
447 channel
->switch_timer_enabled
= !!(ret
== 0);
451 * Stop and delete the channel's switch timer.
453 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
459 ret
= consumer_channel_timer_stop(&channel
->switch_timer
,
460 LTTNG_CONSUMER_SIG_SWITCH
);
462 ERR("Failed to stop switch timer");
465 channel
->switch_timer_enabled
= 0;
469 * Set the channel's live timer.
471 void consumer_timer_live_start(struct lttng_consumer_channel
*channel
,
472 unsigned int live_timer_interval_us
)
477 assert(channel
->key
);
479 ret
= consumer_channel_timer_start(&channel
->live_timer
, channel
,
480 live_timer_interval_us
, LTTNG_CONSUMER_SIG_LIVE
);
482 channel
->live_timer_enabled
= !!(ret
== 0);
486 * Stop and delete the channel's live timer.
488 void consumer_timer_live_stop(struct lttng_consumer_channel
*channel
)
494 ret
= consumer_channel_timer_stop(&channel
->live_timer
,
495 LTTNG_CONSUMER_SIG_LIVE
);
497 ERR("Failed to stop live timer");
500 channel
->live_timer_enabled
= 0;
504 * Set the channel's monitoring timer.
506 * Returns a negative value on error, 0 if a timer was created, and
507 * a positive value if no timer was created (not an error).
509 int consumer_timer_monitor_start(struct lttng_consumer_channel
*channel
,
510 unsigned int monitor_timer_interval_us
)
515 assert(channel
->key
);
516 assert(!channel
->monitor_timer_enabled
);
518 ret
= consumer_channel_timer_start(&channel
->monitor_timer
, channel
,
519 monitor_timer_interval_us
, LTTNG_CONSUMER_SIG_MONITOR
);
520 channel
->monitor_timer_enabled
= !!(ret
== 0);
525 * Stop and delete the channel's monitoring timer.
527 int consumer_timer_monitor_stop(struct lttng_consumer_channel
*channel
)
532 assert(channel
->monitor_timer_enabled
);
534 ret
= consumer_channel_timer_stop(&channel
->monitor_timer
,
535 LTTNG_CONSUMER_SIG_MONITOR
);
537 ERR("Failed to stop live timer");
541 channel
->monitor_timer_enabled
= 0;
547 * Block the RT signals for the entire process. It must be called from the
548 * consumer main before creating the threads
550 int consumer_signal_init(void)
555 /* Block signal for entire process, so only our thread processes it. */
557 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
560 PERROR("pthread_sigmask");
567 int sample_channel_positions(struct lttng_consumer_channel
*channel
,
568 uint64_t *_highest_use
, uint64_t *_lowest_use
, uint64_t *_total_consumed
,
569 sample_positions_cb sample
, get_consumed_cb get_consumed
,
570 get_produced_cb get_produced
)
573 struct lttng_ht_iter iter
;
574 struct lttng_consumer_stream
*stream
;
575 bool empty_channel
= true;
576 uint64_t high
= 0, low
= UINT64_MAX
;
577 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
579 *_total_consumed
= 0;
583 cds_lfht_for_each_entry_duplicate(ht
->ht
,
584 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
585 ht
->match_fct
, &channel
->key
,
586 &iter
.iter
, stream
, node_channel_id
.node
) {
587 unsigned long produced
, consumed
, usage
;
589 empty_channel
= false;
591 pthread_mutex_lock(&stream
->lock
);
592 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
596 ret
= sample(stream
);
598 ERR("Failed to take buffer position snapshot in monitor timer (ret = %d)", ret
);
599 pthread_mutex_unlock(&stream
->lock
);
602 ret
= get_consumed(stream
, &consumed
);
604 ERR("Failed to get buffer consumed position in monitor timer");
605 pthread_mutex_unlock(&stream
->lock
);
608 ret
= get_produced(stream
, &produced
);
610 ERR("Failed to get buffer produced position in monitor timer");
611 pthread_mutex_unlock(&stream
->lock
);
615 usage
= produced
- consumed
;
616 high
= (usage
> high
) ? usage
: high
;
617 low
= (usage
< low
) ? usage
: low
;
620 * We don't use consumed here for 2 reasons:
621 * - output_written takes into account the padding written in the
622 * tracefiles when we stop the session;
623 * - the consumed position is not the accurate representation of what
624 * was extracted from a buffer in overwrite mode.
626 *_total_consumed
+= stream
->output_written
;
628 pthread_mutex_unlock(&stream
->lock
);
631 *_highest_use
= high
;
642 * Execute action on a monitor timer.
645 void monitor_timer(struct lttng_consumer_channel
*channel
)
648 int channel_monitor_pipe
=
649 consumer_timer_thread_get_channel_monitor_pipe();
650 struct lttcomm_consumer_channel_monitor_msg msg
= {
653 sample_positions_cb sample
;
654 get_consumed_cb get_consumed
;
655 get_produced_cb get_produced
;
656 uint64_t lowest
= 0, highest
= 0, total_consumed
= 0;
660 if (channel_monitor_pipe
< 0) {
664 switch (the_consumer_data
.type
) {
665 case LTTNG_CONSUMER_KERNEL
:
666 sample
= lttng_kconsumer_sample_snapshot_positions
;
667 get_consumed
= lttng_kconsumer_get_consumed_snapshot
;
668 get_produced
= lttng_kconsumer_get_produced_snapshot
;
670 case LTTNG_CONSUMER32_UST
:
671 case LTTNG_CONSUMER64_UST
:
672 sample
= lttng_ustconsumer_sample_snapshot_positions
;
673 get_consumed
= lttng_ustconsumer_get_consumed_snapshot
;
674 get_produced
= lttng_ustconsumer_get_produced_snapshot
;
680 ret
= sample_channel_positions(channel
, &highest
, &lowest
,
681 &total_consumed
, sample
, get_consumed
, get_produced
);
685 msg
.highest
= highest
;
687 msg
.total_consumed
= total_consumed
;
690 * Writes performed here are assumed to be atomic which is only
691 * guaranteed for sizes < than PIPE_BUF.
693 assert(sizeof(msg
) <= PIPE_BUF
);
696 ret
= write(channel_monitor_pipe
, &msg
, sizeof(msg
));
697 } while (ret
== -1 && errno
== EINTR
);
699 if (errno
== EAGAIN
) {
700 /* Not an error, the sample is merely dropped. */
701 DBG("Channel monitor pipe is full; dropping sample for channel key = %"PRIu64
,
704 PERROR("write to the channel monitor pipe");
707 DBG("Sent channel monitoring sample for channel key %" PRIu64
708 ", (highest = %" PRIu64
", lowest = %"PRIu64
")",
709 channel
->key
, msg
.highest
, msg
.lowest
);
713 int consumer_timer_thread_get_channel_monitor_pipe(void)
715 return uatomic_read(&the_channel_monitor_pipe
);
718 int consumer_timer_thread_set_channel_monitor_pipe(int fd
)
722 ret
= uatomic_cmpxchg(&the_channel_monitor_pipe
, -1, fd
);
733 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
734 * LTTNG_CONSUMER_SIG_TEARDOWN, LTTNG_CONSUMER_SIG_LIVE, and
735 * LTTNG_CONSUMER_SIG_MONITOR, LTTNG_CONSUMER_SIG_EXIT.
737 void *consumer_timer_thread(void *data
)
742 struct lttng_consumer_local_data
*ctx
= data
;
744 rcu_register_thread();
746 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
748 if (testpoint(consumerd_thread_metadata_timer
)) {
749 goto error_testpoint
;
752 health_code_update();
754 /* Only self thread will receive signal mask. */
756 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
759 health_code_update();
762 signr
= sigwaitinfo(&mask
, &info
);
766 * NOTE: cascading conditions are used instead of a switch case
767 * since the use of SIGRTMIN in the definition of the signals'
768 * values prevents the reduction to an integer constant.
771 if (errno
!= EINTR
) {
772 PERROR("sigwaitinfo");
775 } else if (signr
== LTTNG_CONSUMER_SIG_SWITCH
) {
776 metadata_switch_timer(ctx
, &info
);
777 } else if (signr
== LTTNG_CONSUMER_SIG_TEARDOWN
) {
779 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
781 DBG("Signal timer metadata thread teardown");
782 } else if (signr
== LTTNG_CONSUMER_SIG_LIVE
) {
783 live_timer(ctx
, &info
);
784 } else if (signr
== LTTNG_CONSUMER_SIG_MONITOR
) {
785 struct lttng_consumer_channel
*channel
;
787 channel
= info
.si_value
.sival_ptr
;
788 monitor_timer(channel
);
789 } else if (signr
== LTTNG_CONSUMER_SIG_EXIT
) {
790 assert(CMM_LOAD_SHARED(consumer_quit
));
793 ERR("Unexpected signal %d\n", info
.si_signo
);
798 /* Only reached in testpoint error */
801 health_unregister(health_consumerd
);
802 rcu_unregister_thread();