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 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 lttng_ustconsumer_flush_buffer(stream
, 1);
241 ret
= lttng_ustconsumer_take_snapshot(stream
);
243 if (ret
!= -EAGAIN
) {
244 ERR("Taking UST snapshot");
248 ret
= lttng_ustconsumer_get_stream_id(stream
, &stream_id
);
250 PERROR("ustctl_get_stream_id");
253 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
254 ret
= send_empty_index(stream
, ts
, stream_id
);
265 * Execute action on a live timer
267 static void live_timer(struct lttng_consumer_local_data
*ctx
,
271 struct lttng_consumer_channel
*channel
;
272 struct lttng_consumer_stream
*stream
;
273 struct lttng_ht_iter iter
;
274 const struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
275 const flush_index_cb flush_index
=
276 ctx
->type
== LTTNG_CONSUMER_KERNEL
?
277 consumer_flush_kernel_index
:
278 consumer_flush_ust_index
;
280 channel
= si
->si_value
.sival_ptr
;
283 if (channel
->switch_timer_error
) {
287 DBG("Live timer for channel %" PRIu64
, channel
->key
);
290 cds_lfht_for_each_entry_duplicate(ht
->ht
,
291 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
292 ht
->match_fct
, &channel
->key
, &iter
.iter
,
293 stream
, node_channel_id
.node
) {
294 ret
= check_stream(stream
, flush_index
);
308 void consumer_timer_signal_thread_qs(unsigned int signr
)
310 sigset_t pending_set
;
314 * We need to be the only thread interacting with the thread
315 * that manages signals for teardown synchronization.
317 pthread_mutex_lock(&timer_signal
.lock
);
319 /* Ensure we don't have any signal queued for this channel. */
321 ret
= sigemptyset(&pending_set
);
323 PERROR("sigemptyset");
325 ret
= sigpending(&pending_set
);
327 PERROR("sigpending");
329 if (!sigismember(&pending_set
, signr
)) {
336 * From this point, no new signal handler will be fired that would try to
337 * access "chan". However, we still need to wait for any currently
338 * executing handler to complete.
341 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
345 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
348 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
350 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
355 pthread_mutex_unlock(&timer_signal
.lock
);
359 * Start a timer channel timer which will fire at a given interval
360 * (timer_interval_us)and fire a given signal (signal).
362 * Returns a negative value on error, 0 if a timer was created, and
363 * a positive value if no timer was created (not an error).
366 int consumer_channel_timer_start(timer_t
*timer_id
,
367 struct lttng_consumer_channel
*channel
,
368 unsigned int timer_interval_us
, int signal
)
370 int ret
= 0, delete_ret
;
372 struct itimerspec its
;
375 assert(channel
->key
);
377 if (timer_interval_us
== 0) {
378 /* No creation needed; not an error. */
383 sev
.sigev_notify
= SIGEV_SIGNAL
;
384 sev
.sigev_signo
= signal
;
385 sev
.sigev_value
.sival_ptr
= channel
;
386 ret
= timer_create(CLOCKID
, &sev
, timer_id
);
388 PERROR("timer_create");
392 its
.it_value
.tv_sec
= timer_interval_us
/ 1000000;
393 its
.it_value
.tv_nsec
= (timer_interval_us
% 1000000) * 1000;
394 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
395 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
397 ret
= timer_settime(*timer_id
, 0, &its
, NULL
);
399 PERROR("timer_settime");
400 goto error_destroy_timer
;
405 delete_ret
= timer_delete(*timer_id
);
406 if (delete_ret
== -1) {
407 PERROR("timer_delete");
413 int consumer_channel_timer_stop(timer_t
*timer_id
, int signal
)
417 ret
= timer_delete(*timer_id
);
419 PERROR("timer_delete");
423 consumer_timer_signal_thread_qs(signal
);
430 * Set the channel's switch timer.
432 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
433 unsigned int switch_timer_interval_us
)
438 assert(channel
->key
);
440 ret
= consumer_channel_timer_start(&channel
->switch_timer
, channel
,
441 switch_timer_interval_us
, LTTNG_CONSUMER_SIG_SWITCH
);
443 channel
->switch_timer_enabled
= !!(ret
== 0);
447 * Stop and delete the channel's switch timer.
449 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
455 ret
= consumer_channel_timer_stop(&channel
->switch_timer
,
456 LTTNG_CONSUMER_SIG_SWITCH
);
458 ERR("Failed to stop switch timer");
461 channel
->switch_timer_enabled
= 0;
465 * Set the channel's live timer.
467 void consumer_timer_live_start(struct lttng_consumer_channel
*channel
,
468 unsigned int live_timer_interval_us
)
473 assert(channel
->key
);
475 ret
= consumer_channel_timer_start(&channel
->live_timer
, channel
,
476 live_timer_interval_us
, LTTNG_CONSUMER_SIG_LIVE
);
478 channel
->live_timer_enabled
= !!(ret
== 0);
482 * Stop and delete the channel's live timer.
484 void consumer_timer_live_stop(struct lttng_consumer_channel
*channel
)
490 ret
= consumer_channel_timer_stop(&channel
->live_timer
,
491 LTTNG_CONSUMER_SIG_LIVE
);
493 ERR("Failed to stop live timer");
496 channel
->live_timer_enabled
= 0;
500 * Set the channel's monitoring timer.
502 * Returns a negative value on error, 0 if a timer was created, and
503 * a positive value if no timer was created (not an error).
505 int consumer_timer_monitor_start(struct lttng_consumer_channel
*channel
,
506 unsigned int monitor_timer_interval_us
)
511 assert(channel
->key
);
512 assert(!channel
->monitor_timer_enabled
);
514 ret
= consumer_channel_timer_start(&channel
->monitor_timer
, channel
,
515 monitor_timer_interval_us
, LTTNG_CONSUMER_SIG_MONITOR
);
516 channel
->monitor_timer_enabled
= !!(ret
== 0);
521 * Stop and delete the channel's monitoring timer.
523 int consumer_timer_monitor_stop(struct lttng_consumer_channel
*channel
)
528 assert(channel
->monitor_timer_enabled
);
530 ret
= consumer_channel_timer_stop(&channel
->monitor_timer
,
531 LTTNG_CONSUMER_SIG_MONITOR
);
533 ERR("Failed to stop live timer");
537 channel
->monitor_timer_enabled
= 0;
543 * Block the RT signals for the entire process. It must be called from the
544 * consumer main before creating the threads
546 int consumer_signal_init(void)
551 /* Block signal for entire process, so only our thread processes it. */
553 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
556 PERROR("pthread_sigmask");
563 int sample_channel_positions(struct lttng_consumer_channel
*channel
,
564 uint64_t *_highest_use
, uint64_t *_lowest_use
, uint64_t *_total_consumed
,
565 sample_positions_cb sample
, get_consumed_cb get_consumed
,
566 get_produced_cb get_produced
)
569 struct lttng_ht_iter iter
;
570 struct lttng_consumer_stream
*stream
;
571 bool empty_channel
= true;
572 uint64_t high
= 0, low
= UINT64_MAX
;
573 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
575 *_total_consumed
= 0;
579 cds_lfht_for_each_entry_duplicate(ht
->ht
,
580 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
581 ht
->match_fct
, &channel
->key
,
582 &iter
.iter
, stream
, node_channel_id
.node
) {
583 unsigned long produced
, consumed
, usage
;
585 empty_channel
= false;
587 pthread_mutex_lock(&stream
->lock
);
588 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
592 ret
= sample(stream
);
594 ERR("Failed to take buffer position snapshot in monitor timer (ret = %d)", ret
);
595 pthread_mutex_unlock(&stream
->lock
);
598 ret
= get_consumed(stream
, &consumed
);
600 ERR("Failed to get buffer consumed position in monitor timer");
601 pthread_mutex_unlock(&stream
->lock
);
604 ret
= get_produced(stream
, &produced
);
606 ERR("Failed to get buffer produced position in monitor timer");
607 pthread_mutex_unlock(&stream
->lock
);
611 usage
= produced
- consumed
;
612 high
= (usage
> high
) ? usage
: high
;
613 low
= (usage
< low
) ? usage
: low
;
616 * We don't use consumed here for 2 reasons:
617 * - output_written takes into account the padding written in the
618 * tracefiles when we stop the session;
619 * - the consumed position is not the accurate representation of what
620 * was extracted from a buffer in overwrite mode.
622 *_total_consumed
+= stream
->output_written
;
624 pthread_mutex_unlock(&stream
->lock
);
627 *_highest_use
= high
;
638 * Execute action on a monitor timer.
641 void monitor_timer(struct lttng_consumer_channel
*channel
)
644 int channel_monitor_pipe
=
645 consumer_timer_thread_get_channel_monitor_pipe();
646 struct lttcomm_consumer_channel_monitor_msg msg
= {
649 sample_positions_cb sample
;
650 get_consumed_cb get_consumed
;
651 get_produced_cb get_produced
;
652 uint64_t lowest
= 0, highest
= 0, total_consumed
= 0;
656 if (channel_monitor_pipe
< 0) {
660 switch (consumer_data
.type
) {
661 case LTTNG_CONSUMER_KERNEL
:
662 sample
= lttng_kconsumer_sample_snapshot_positions
;
663 get_consumed
= lttng_kconsumer_get_consumed_snapshot
;
664 get_produced
= lttng_kconsumer_get_produced_snapshot
;
666 case LTTNG_CONSUMER32_UST
:
667 case LTTNG_CONSUMER64_UST
:
668 sample
= lttng_ustconsumer_sample_snapshot_positions
;
669 get_consumed
= lttng_ustconsumer_get_consumed_snapshot
;
670 get_produced
= lttng_ustconsumer_get_produced_snapshot
;
676 ret
= sample_channel_positions(channel
, &highest
, &lowest
,
677 &total_consumed
, sample
, get_consumed
, get_produced
);
681 msg
.highest
= highest
;
683 msg
.total_consumed
= total_consumed
;
686 * Writes performed here are assumed to be atomic which is only
687 * guaranteed for sizes < than PIPE_BUF.
689 assert(sizeof(msg
) <= PIPE_BUF
);
692 ret
= write(channel_monitor_pipe
, &msg
, sizeof(msg
));
693 } while (ret
== -1 && errno
== EINTR
);
695 if (errno
== EAGAIN
) {
696 /* Not an error, the sample is merely dropped. */
697 DBG("Channel monitor pipe is full; dropping sample for channel key = %"PRIu64
,
700 PERROR("write to the channel monitor pipe");
703 DBG("Sent channel monitoring sample for channel key %" PRIu64
704 ", (highest = %" PRIu64
", lowest = %"PRIu64
")",
705 channel
->key
, msg
.highest
, msg
.lowest
);
709 int consumer_timer_thread_get_channel_monitor_pipe(void)
711 return uatomic_read(&channel_monitor_pipe
);
714 int consumer_timer_thread_set_channel_monitor_pipe(int fd
)
718 ret
= uatomic_cmpxchg(&channel_monitor_pipe
, -1, fd
);
729 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
730 * LTTNG_CONSUMER_SIG_TEARDOWN, LTTNG_CONSUMER_SIG_LIVE, and
731 * LTTNG_CONSUMER_SIG_MONITOR, LTTNG_CONSUMER_SIG_EXIT.
733 void *consumer_timer_thread(void *data
)
738 struct lttng_consumer_local_data
*ctx
= data
;
740 rcu_register_thread();
742 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
744 if (testpoint(consumerd_thread_metadata_timer
)) {
745 goto error_testpoint
;
748 health_code_update();
750 /* Only self thread will receive signal mask. */
752 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
755 health_code_update();
758 signr
= sigwaitinfo(&mask
, &info
);
762 * NOTE: cascading conditions are used instead of a switch case
763 * since the use of SIGRTMIN in the definition of the signals'
764 * values prevents the reduction to an integer constant.
767 if (errno
!= EINTR
) {
768 PERROR("sigwaitinfo");
771 } else if (signr
== LTTNG_CONSUMER_SIG_SWITCH
) {
772 metadata_switch_timer(ctx
, &info
);
773 } else if (signr
== LTTNG_CONSUMER_SIG_TEARDOWN
) {
775 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
777 DBG("Signal timer metadata thread teardown");
778 } else if (signr
== LTTNG_CONSUMER_SIG_LIVE
) {
779 live_timer(ctx
, &info
);
780 } else if (signr
== LTTNG_CONSUMER_SIG_MONITOR
) {
781 struct lttng_consumer_channel
*channel
;
783 channel
= info
.si_value
.sival_ptr
;
784 monitor_timer(channel
);
785 } else if (signr
== LTTNG_CONSUMER_SIG_EXIT
) {
786 assert(CMM_LOAD_SHARED(consumer_quit
));
789 ERR("Unexpected signal %d\n", info
.si_signo
);
794 /* Only reached in testpoint error */
797 health_unregister(health_consumerd
);
798 rcu_unregister_thread();