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
13 #include <bin/lttng-consumerd/health-consumerd.h>
14 #include <common/common.h>
15 #include <common/compat/endian.h>
16 #include <common/kernel-ctl/kernel-ctl.h>
17 #include <common/kernel-consumer/kernel-consumer.h>
18 #include <common/consumer/consumer-stream.h>
19 #include <common/consumer/consumer-timer.h>
20 #include <common/consumer/consumer-testpoint.h>
21 #include <common/ust-consumer/ust-consumer.h>
23 typedef int (*sample_positions_cb
)(struct lttng_consumer_stream
*stream
);
24 typedef int (*get_consumed_cb
)(struct lttng_consumer_stream
*stream
,
25 unsigned long *consumed
);
26 typedef int (*get_produced_cb
)(struct lttng_consumer_stream
*stream
,
27 unsigned long *produced
);
28 typedef int (*flush_index_cb
)(struct lttng_consumer_stream
*stream
);
30 static struct timer_signal_data timer_signal
= {
34 .lock
= PTHREAD_MUTEX_INITIALIZER
,
38 * Set custom signal mask to current thread.
40 static void setmask(sigset_t
*mask
)
44 ret
= sigemptyset(mask
);
46 PERROR("sigemptyset");
48 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_SWITCH
);
50 PERROR("sigaddset switch");
52 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_TEARDOWN
);
54 PERROR("sigaddset teardown");
56 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_LIVE
);
58 PERROR("sigaddset live");
60 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_MONITOR
);
62 PERROR("sigaddset monitor");
64 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_EXIT
);
66 PERROR("sigaddset exit");
70 static int the_channel_monitor_pipe
= -1;
73 * Execute action on a timer switch.
75 * Beware: metadata_switch_timer() should *never* take a mutex also held
76 * while consumer_timer_switch_stop() is called. It would result in
79 static void metadata_switch_timer(struct lttng_consumer_local_data
*ctx
,
83 struct lttng_consumer_channel
*channel
;
85 channel
= si
->si_value
.sival_ptr
;
86 LTTNG_ASSERT(channel
);
88 if (channel
->switch_timer_error
) {
92 DBG("Switch timer for channel %" PRIu64
, channel
->key
);
94 case LTTNG_CONSUMER32_UST
:
95 case LTTNG_CONSUMER64_UST
:
97 * Locks taken by lttng_ustconsumer_request_metadata():
98 * - metadata_socket_lock
99 * - Calling lttng_ustconsumer_recv_metadata():
100 * - channel->metadata_cache->lock
101 * - Calling consumer_metadata_cache_flushed():
102 * - channel->timer_lock
103 * - channel->metadata_cache->lock
105 * Ensure that neither consumer_data.lock nor
106 * channel->lock are taken within this function, since
107 * they are held while consumer_timer_switch_stop() is
110 ret
= lttng_ustconsumer_request_metadata(ctx
, channel
, 1, 1);
112 channel
->switch_timer_error
= 1;
115 case LTTNG_CONSUMER_KERNEL
:
116 case LTTNG_CONSUMER_UNKNOWN
:
122 static int send_empty_index(struct lttng_consumer_stream
*stream
, uint64_t ts
,
126 struct ctf_packet_index index
;
128 memset(&index
, 0, sizeof(index
));
129 index
.stream_id
= htobe64(stream_id
);
130 index
.timestamp_end
= htobe64(ts
);
131 ret
= consumer_stream_write_index(stream
, &index
);
140 int consumer_flush_kernel_index(struct lttng_consumer_stream
*stream
)
142 uint64_t ts
, stream_id
;
145 ret
= kernctl_get_current_timestamp(stream
->wait_fd
, &ts
);
147 ERR("Failed to get the current timestamp");
150 ret
= kernctl_buffer_flush(stream
->wait_fd
);
152 ERR("Failed to flush kernel stream");
155 ret
= kernctl_snapshot(stream
->wait_fd
);
157 if (ret
!= -EAGAIN
&& ret
!= -ENODATA
) {
158 PERROR("live timer kernel snapshot");
162 ret
= kernctl_get_stream_id(stream
->wait_fd
, &stream_id
);
164 PERROR("kernctl_get_stream_id");
167 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
168 ret
= send_empty_index(stream
, ts
, stream_id
);
178 static int check_stream(struct lttng_consumer_stream
*stream
,
179 flush_index_cb flush_index
)
184 * While holding the stream mutex, try to take a snapshot, if it
185 * succeeds, it means that data is ready to be sent, just let the data
186 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
187 * means that there is no data to read after the flush, so we can
188 * safely send the empty index.
190 * Doing a trylock and checking if waiting on metadata if
191 * trylock fails. Bail out of the stream is indeed waiting for
192 * metadata to be pushed. Busy wait on trylock otherwise.
195 ret
= pthread_mutex_trylock(&stream
->lock
);
198 break; /* We have the lock. */
200 pthread_mutex_lock(&stream
->metadata_timer_lock
);
201 if (stream
->waiting_on_metadata
) {
203 stream
->missed_metadata_flush
= true;
204 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
205 goto end
; /* Bail out. */
207 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
212 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
218 ret
= flush_index(stream
);
219 pthread_mutex_unlock(&stream
->lock
);
224 int consumer_flush_ust_index(struct lttng_consumer_stream
*stream
)
226 uint64_t ts
, stream_id
;
229 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
234 ret
= lttng_ustconsumer_get_current_timestamp(stream
, &ts
);
236 ERR("Failed to get the current timestamp");
239 ret
= lttng_ustconsumer_flush_buffer(stream
, 1);
241 ERR("Failed to flush buffer while flushing index");
244 ret
= lttng_ustconsumer_take_snapshot(stream
);
246 if (ret
!= -EAGAIN
) {
247 ERR("Taking UST snapshot");
251 ret
= lttng_ustconsumer_get_stream_id(stream
, &stream_id
);
253 PERROR("lttng_ust_ctl_get_stream_id");
256 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
257 ret
= send_empty_index(stream
, ts
, stream_id
);
268 * Execute action on a live timer
270 static void live_timer(struct lttng_consumer_local_data
*ctx
,
274 struct lttng_consumer_channel
*channel
;
275 struct lttng_consumer_stream
*stream
;
276 struct lttng_ht_iter iter
;
277 const struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
278 const flush_index_cb flush_index
=
279 ctx
->type
== LTTNG_CONSUMER_KERNEL
?
280 consumer_flush_kernel_index
:
281 consumer_flush_ust_index
;
283 channel
= si
->si_value
.sival_ptr
;
284 LTTNG_ASSERT(channel
);
286 if (channel
->switch_timer_error
) {
290 DBG("Live timer for channel %" PRIu64
, channel
->key
);
293 cds_lfht_for_each_entry_duplicate(ht
->ht
,
294 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
295 ht
->match_fct
, &channel
->key
, &iter
.iter
,
296 stream
, node_channel_id
.node
) {
297 ret
= check_stream(stream
, flush_index
);
311 void consumer_timer_signal_thread_qs(unsigned int signr
)
313 sigset_t pending_set
;
317 * We need to be the only thread interacting with the thread
318 * that manages signals for teardown synchronization.
320 pthread_mutex_lock(&timer_signal
.lock
);
322 /* Ensure we don't have any signal queued for this channel. */
324 ret
= sigemptyset(&pending_set
);
326 PERROR("sigemptyset");
328 ret
= sigpending(&pending_set
);
330 PERROR("sigpending");
332 if (!sigismember(&pending_set
, signr
)) {
339 * From this point, no new signal handler will be fired that would try to
340 * access "chan". However, we still need to wait for any currently
341 * executing handler to complete.
344 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
348 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
351 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
353 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
358 pthread_mutex_unlock(&timer_signal
.lock
);
362 * Start a timer channel timer which will fire at a given interval
363 * (timer_interval_us)and fire a given signal (signal).
365 * Returns a negative value on error, 0 if a timer was created, and
366 * a positive value if no timer was created (not an error).
369 int consumer_channel_timer_start(timer_t
*timer_id
,
370 struct lttng_consumer_channel
*channel
,
371 unsigned int timer_interval_us
, int signal
)
373 int ret
= 0, delete_ret
;
374 struct sigevent sev
= {};
375 struct itimerspec its
;
377 LTTNG_ASSERT(channel
);
378 LTTNG_ASSERT(channel
->key
);
380 if (timer_interval_us
== 0) {
381 /* No creation needed; not an error. */
386 sev
.sigev_notify
= SIGEV_SIGNAL
;
387 sev
.sigev_signo
= signal
;
388 sev
.sigev_value
.sival_ptr
= channel
;
389 ret
= timer_create(CLOCKID
, &sev
, timer_id
);
391 PERROR("timer_create");
395 its
.it_value
.tv_sec
= timer_interval_us
/ 1000000;
396 its
.it_value
.tv_nsec
= (timer_interval_us
% 1000000) * 1000;
397 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
398 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
400 ret
= timer_settime(*timer_id
, 0, &its
, NULL
);
402 PERROR("timer_settime");
403 goto error_destroy_timer
;
408 delete_ret
= timer_delete(*timer_id
);
409 if (delete_ret
== -1) {
410 PERROR("timer_delete");
416 int consumer_channel_timer_stop(timer_t
*timer_id
, int signal
)
420 ret
= timer_delete(*timer_id
);
422 PERROR("timer_delete");
426 consumer_timer_signal_thread_qs(signal
);
433 * Set the channel's switch timer.
435 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
436 unsigned int switch_timer_interval_us
)
440 LTTNG_ASSERT(channel
);
441 LTTNG_ASSERT(channel
->key
);
443 ret
= consumer_channel_timer_start(&channel
->switch_timer
, channel
,
444 switch_timer_interval_us
, LTTNG_CONSUMER_SIG_SWITCH
);
446 channel
->switch_timer_enabled
= !!(ret
== 0);
450 * Stop and delete the channel's switch timer.
452 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
456 LTTNG_ASSERT(channel
);
458 ret
= consumer_channel_timer_stop(&channel
->switch_timer
,
459 LTTNG_CONSUMER_SIG_SWITCH
);
461 ERR("Failed to stop switch timer");
464 channel
->switch_timer_enabled
= 0;
468 * Set the channel's live timer.
470 void consumer_timer_live_start(struct lttng_consumer_channel
*channel
,
471 unsigned int live_timer_interval_us
)
475 LTTNG_ASSERT(channel
);
476 LTTNG_ASSERT(channel
->key
);
478 ret
= consumer_channel_timer_start(&channel
->live_timer
, channel
,
479 live_timer_interval_us
, LTTNG_CONSUMER_SIG_LIVE
);
481 channel
->live_timer_enabled
= !!(ret
== 0);
485 * Stop and delete the channel's live timer.
487 void consumer_timer_live_stop(struct lttng_consumer_channel
*channel
)
491 LTTNG_ASSERT(channel
);
493 ret
= consumer_channel_timer_stop(&channel
->live_timer
,
494 LTTNG_CONSUMER_SIG_LIVE
);
496 ERR("Failed to stop live timer");
499 channel
->live_timer_enabled
= 0;
503 * Set the channel's monitoring timer.
505 * Returns a negative value on error, 0 if a timer was created, and
506 * a positive value if no timer was created (not an error).
508 int consumer_timer_monitor_start(struct lttng_consumer_channel
*channel
,
509 unsigned int monitor_timer_interval_us
)
513 LTTNG_ASSERT(channel
);
514 LTTNG_ASSERT(channel
->key
);
515 LTTNG_ASSERT(!channel
->monitor_timer_enabled
);
517 ret
= consumer_channel_timer_start(&channel
->monitor_timer
, channel
,
518 monitor_timer_interval_us
, LTTNG_CONSUMER_SIG_MONITOR
);
519 channel
->monitor_timer_enabled
= !!(ret
== 0);
524 * Stop and delete the channel's monitoring timer.
526 int consumer_timer_monitor_stop(struct lttng_consumer_channel
*channel
)
530 LTTNG_ASSERT(channel
);
531 LTTNG_ASSERT(channel
->monitor_timer_enabled
);
533 ret
= consumer_channel_timer_stop(&channel
->monitor_timer
,
534 LTTNG_CONSUMER_SIG_MONITOR
);
536 ERR("Failed to stop live timer");
540 channel
->monitor_timer_enabled
= 0;
546 * Block the RT signals for the entire process. It must be called from the
547 * consumer main before creating the threads
549 int consumer_signal_init(void)
554 /* Block signal for entire process, so only our thread processes it. */
556 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
559 PERROR("pthread_sigmask");
566 int sample_channel_positions(struct lttng_consumer_channel
*channel
,
567 uint64_t *_highest_use
, uint64_t *_lowest_use
, uint64_t *_total_consumed
,
568 sample_positions_cb sample
, get_consumed_cb get_consumed
,
569 get_produced_cb get_produced
)
572 struct lttng_ht_iter iter
;
573 struct lttng_consumer_stream
*stream
;
574 bool empty_channel
= true;
575 uint64_t high
= 0, low
= UINT64_MAX
;
576 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
578 *_total_consumed
= 0;
582 cds_lfht_for_each_entry_duplicate(ht
->ht
,
583 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
584 ht
->match_fct
, &channel
->key
,
585 &iter
.iter
, stream
, node_channel_id
.node
) {
586 unsigned long produced
, consumed
, usage
;
588 empty_channel
= false;
590 pthread_mutex_lock(&stream
->lock
);
591 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
595 ret
= sample(stream
);
597 ERR("Failed to take buffer position snapshot in monitor timer (ret = %d)", ret
);
598 pthread_mutex_unlock(&stream
->lock
);
601 ret
= get_consumed(stream
, &consumed
);
603 ERR("Failed to get buffer consumed position in monitor timer");
604 pthread_mutex_unlock(&stream
->lock
);
607 ret
= get_produced(stream
, &produced
);
609 ERR("Failed to get buffer produced position in monitor timer");
610 pthread_mutex_unlock(&stream
->lock
);
614 usage
= produced
- consumed
;
615 high
= (usage
> high
) ? usage
: high
;
616 low
= (usage
< low
) ? usage
: low
;
619 * We don't use consumed here for 2 reasons:
620 * - output_written takes into account the padding written in the
621 * tracefiles when we stop the session;
622 * - the consumed position is not the accurate representation of what
623 * was extracted from a buffer in overwrite mode.
625 *_total_consumed
+= stream
->output_written
;
627 pthread_mutex_unlock(&stream
->lock
);
630 *_highest_use
= high
;
641 * Execute action on a monitor timer.
644 void monitor_timer(struct lttng_consumer_channel
*channel
)
647 int channel_monitor_pipe
=
648 consumer_timer_thread_get_channel_monitor_pipe();
649 struct lttcomm_consumer_channel_monitor_msg msg
= {
652 sample_positions_cb sample
;
653 get_consumed_cb get_consumed
;
654 get_produced_cb get_produced
;
655 uint64_t lowest
= 0, highest
= 0, total_consumed
= 0;
657 LTTNG_ASSERT(channel
);
659 if (channel_monitor_pipe
< 0) {
663 switch (the_consumer_data
.type
) {
664 case LTTNG_CONSUMER_KERNEL
:
665 sample
= lttng_kconsumer_sample_snapshot_positions
;
666 get_consumed
= lttng_kconsumer_get_consumed_snapshot
;
667 get_produced
= lttng_kconsumer_get_produced_snapshot
;
669 case LTTNG_CONSUMER32_UST
:
670 case LTTNG_CONSUMER64_UST
:
671 sample
= lttng_ustconsumer_sample_snapshot_positions
;
672 get_consumed
= lttng_ustconsumer_get_consumed_snapshot
;
673 get_produced
= lttng_ustconsumer_get_produced_snapshot
;
679 ret
= sample_channel_positions(channel
, &highest
, &lowest
,
680 &total_consumed
, sample
, get_consumed
, get_produced
);
684 msg
.highest
= highest
;
686 msg
.total_consumed
= total_consumed
;
689 * Writes performed here are assumed to be atomic which is only
690 * guaranteed for sizes < than PIPE_BUF.
692 LTTNG_ASSERT(sizeof(msg
) <= PIPE_BUF
);
695 ret
= write(channel_monitor_pipe
, &msg
, sizeof(msg
));
696 } while (ret
== -1 && errno
== EINTR
);
698 if (errno
== EAGAIN
) {
699 /* Not an error, the sample is merely dropped. */
700 DBG("Channel monitor pipe is full; dropping sample for channel key = %"PRIu64
,
703 PERROR("write to the channel monitor pipe");
706 DBG("Sent channel monitoring sample for channel key %" PRIu64
707 ", (highest = %" PRIu64
", lowest = %"PRIu64
")",
708 channel
->key
, msg
.highest
, msg
.lowest
);
712 int consumer_timer_thread_get_channel_monitor_pipe(void)
714 return uatomic_read(&the_channel_monitor_pipe
);
717 int consumer_timer_thread_set_channel_monitor_pipe(int fd
)
721 ret
= uatomic_cmpxchg(&the_channel_monitor_pipe
, -1, fd
);
732 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
733 * LTTNG_CONSUMER_SIG_TEARDOWN, LTTNG_CONSUMER_SIG_LIVE, and
734 * LTTNG_CONSUMER_SIG_MONITOR, LTTNG_CONSUMER_SIG_EXIT.
736 void *consumer_timer_thread(void *data
)
741 struct lttng_consumer_local_data
*ctx
= data
;
743 rcu_register_thread();
745 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
747 if (testpoint(consumerd_thread_metadata_timer
)) {
748 goto error_testpoint
;
751 health_code_update();
753 /* Only self thread will receive signal mask. */
755 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
758 health_code_update();
761 signr
= sigwaitinfo(&mask
, &info
);
765 * NOTE: cascading conditions are used instead of a switch case
766 * since the use of SIGRTMIN in the definition of the signals'
767 * values prevents the reduction to an integer constant.
770 if (errno
!= EINTR
) {
771 PERROR("sigwaitinfo");
774 } else if (signr
== LTTNG_CONSUMER_SIG_SWITCH
) {
775 metadata_switch_timer(ctx
, &info
);
776 } else if (signr
== LTTNG_CONSUMER_SIG_TEARDOWN
) {
778 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
780 DBG("Signal timer metadata thread teardown");
781 } else if (signr
== LTTNG_CONSUMER_SIG_LIVE
) {
782 live_timer(ctx
, &info
);
783 } else if (signr
== LTTNG_CONSUMER_SIG_MONITOR
) {
784 struct lttng_consumer_channel
*channel
;
786 channel
= info
.si_value
.sival_ptr
;
787 monitor_timer(channel
);
788 } else if (signr
== LTTNG_CONSUMER_SIG_EXIT
) {
789 LTTNG_ASSERT(CMM_LOAD_SHARED(consumer_quit
));
792 ERR("Unexpected signal %d\n", info
.si_signo
);
797 /* Only reached in testpoint error */
800 health_unregister(health_consumerd
);
801 rcu_unregister_thread();