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-consumerd/health-consumerd.h>
25 #include <common/common.h>
26 #include <common/compat/endian.h>
27 #include <common/kernel-ctl/kernel-ctl.h>
28 #include <common/kernel-consumer/kernel-consumer.h>
29 #include <common/consumer/consumer-stream.h>
30 #include <common/consumer/consumer-timer.h>
31 #include <common/consumer/consumer-testpoint.h>
32 #include <common/ust-consumer/ust-consumer.h>
34 typedef int (*sample_positions_cb
)(struct lttng_consumer_stream
*stream
);
35 typedef int (*get_consumed_cb
)(struct lttng_consumer_stream
*stream
,
36 unsigned long *consumed
);
37 typedef int (*get_produced_cb
)(struct lttng_consumer_stream
*stream
,
38 unsigned long *produced
);
39 typedef int (*flush_index_cb
)(struct lttng_consumer_stream
*stream
);
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");
75 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_EXIT
);
77 PERROR("sigaddset exit");
81 static int channel_monitor_pipe
= -1;
84 * Execute action on a timer switch.
86 * Beware: metadata_switch_timer() should *never* take a mutex also held
87 * while consumer_timer_switch_stop() is called. It would result in
90 static void metadata_switch_timer(struct lttng_consumer_local_data
*ctx
,
94 struct lttng_consumer_channel
*channel
;
96 channel
= si
->si_value
.sival_ptr
;
99 if (channel
->switch_timer_error
) {
103 DBG("Switch timer for channel %" PRIu64
, channel
->key
);
105 case LTTNG_CONSUMER32_UST
:
106 case LTTNG_CONSUMER64_UST
:
108 * Locks taken by lttng_ustconsumer_request_metadata():
109 * - metadata_socket_lock
110 * - Calling lttng_ustconsumer_recv_metadata():
111 * - channel->metadata_cache->lock
112 * - Calling consumer_metadata_cache_flushed():
113 * - channel->timer_lock
114 * - channel->metadata_cache->lock
116 * Ensure that neither consumer_data.lock nor
117 * channel->lock are taken within this function, since
118 * they are held while consumer_timer_switch_stop() is
121 ret
= lttng_ustconsumer_request_metadata(ctx
, channel
, 1, 1);
123 channel
->switch_timer_error
= 1;
126 case LTTNG_CONSUMER_KERNEL
:
127 case LTTNG_CONSUMER_UNKNOWN
:
133 static int send_empty_index(struct lttng_consumer_stream
*stream
, uint64_t ts
,
137 struct ctf_packet_index index
;
139 memset(&index
, 0, sizeof(index
));
140 index
.stream_id
= htobe64(stream_id
);
141 index
.timestamp_end
= htobe64(ts
);
142 ret
= consumer_stream_write_index(stream
, &index
);
151 int consumer_flush_kernel_index(struct lttng_consumer_stream
*stream
)
153 uint64_t ts
, stream_id
;
156 ret
= kernctl_get_current_timestamp(stream
->wait_fd
, &ts
);
158 ERR("Failed to get the current timestamp");
161 ret
= kernctl_buffer_flush(stream
->wait_fd
);
163 ERR("Failed to flush kernel stream");
166 ret
= kernctl_snapshot(stream
->wait_fd
);
168 if (ret
!= -EAGAIN
&& ret
!= -ENODATA
) {
169 PERROR("live timer kernel snapshot");
173 ret
= kernctl_get_stream_id(stream
->wait_fd
, &stream_id
);
175 PERROR("kernctl_get_stream_id");
178 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
179 ret
= send_empty_index(stream
, ts
, stream_id
);
189 static int check_stream(struct lttng_consumer_stream
*stream
,
190 flush_index_cb flush_index
)
195 * While holding the stream mutex, try to take a snapshot, if it
196 * succeeds, it means that data is ready to be sent, just let the data
197 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
198 * means that there is no data to read after the flush, so we can
199 * safely send the empty index.
201 * Doing a trylock and checking if waiting on metadata if
202 * trylock fails. Bail out of the stream is indeed waiting for
203 * metadata to be pushed. Busy wait on trylock otherwise.
206 ret
= pthread_mutex_trylock(&stream
->lock
);
209 break; /* We have the lock. */
211 pthread_mutex_lock(&stream
->metadata_timer_lock
);
212 if (stream
->waiting_on_metadata
) {
214 stream
->missed_metadata_flush
= true;
215 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
216 goto end
; /* Bail out. */
218 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
223 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
229 ret
= flush_index(stream
);
230 pthread_mutex_unlock(&stream
->lock
);
235 int consumer_flush_ust_index(struct lttng_consumer_stream
*stream
)
237 uint64_t ts
, stream_id
;
240 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
245 ret
= lttng_ustconsumer_get_current_timestamp(stream
, &ts
);
247 ERR("Failed to get the current timestamp");
250 lttng_ustconsumer_flush_buffer(stream
, 1);
251 ret
= lttng_ustconsumer_take_snapshot(stream
);
253 if (ret
!= -EAGAIN
) {
254 ERR("Taking UST snapshot");
258 ret
= lttng_ustconsumer_get_stream_id(stream
, &stream_id
);
260 PERROR("ustctl_get_stream_id");
263 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
264 ret
= send_empty_index(stream
, ts
, stream_id
);
275 * Execute action on a live timer
277 static void live_timer(struct lttng_consumer_local_data
*ctx
,
281 struct lttng_consumer_channel
*channel
;
282 struct lttng_consumer_stream
*stream
;
283 struct lttng_ht_iter iter
;
284 const struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
285 const flush_index_cb flush_index
=
286 ctx
->type
== LTTNG_CONSUMER_KERNEL
?
287 consumer_flush_kernel_index
:
288 consumer_flush_ust_index
;
290 channel
= si
->si_value
.sival_ptr
;
293 if (channel
->switch_timer_error
) {
297 DBG("Live timer for channel %" PRIu64
, channel
->key
);
300 cds_lfht_for_each_entry_duplicate(ht
->ht
,
301 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
302 ht
->match_fct
, &channel
->key
, &iter
.iter
,
303 stream
, node_channel_id
.node
) {
304 ret
= check_stream(stream
, flush_index
);
318 void consumer_timer_signal_thread_qs(unsigned int signr
)
320 sigset_t pending_set
;
324 * We need to be the only thread interacting with the thread
325 * that manages signals for teardown synchronization.
327 pthread_mutex_lock(&timer_signal
.lock
);
329 /* Ensure we don't have any signal queued for this channel. */
331 ret
= sigemptyset(&pending_set
);
333 PERROR("sigemptyset");
335 ret
= sigpending(&pending_set
);
337 PERROR("sigpending");
339 if (!sigismember(&pending_set
, signr
)) {
346 * From this point, no new signal handler will be fired that would try to
347 * access "chan". However, we still need to wait for any currently
348 * executing handler to complete.
351 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
355 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
358 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
360 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
365 pthread_mutex_unlock(&timer_signal
.lock
);
369 * Start a timer channel timer which will fire at a given interval
370 * (timer_interval_us)and fire a given signal (signal).
372 * Returns a negative value on error, 0 if a timer was created, and
373 * a positive value if no timer was created (not an error).
376 int consumer_channel_timer_start(timer_t
*timer_id
,
377 struct lttng_consumer_channel
*channel
,
378 unsigned int timer_interval_us
, int signal
)
380 int ret
= 0, delete_ret
;
382 struct itimerspec its
;
385 assert(channel
->key
);
387 if (timer_interval_us
== 0) {
388 /* No creation needed; not an error. */
393 sev
.sigev_notify
= SIGEV_SIGNAL
;
394 sev
.sigev_signo
= signal
;
395 sev
.sigev_value
.sival_ptr
= channel
;
396 ret
= timer_create(CLOCKID
, &sev
, timer_id
);
398 PERROR("timer_create");
402 its
.it_value
.tv_sec
= timer_interval_us
/ 1000000;
403 its
.it_value
.tv_nsec
= (timer_interval_us
% 1000000) * 1000;
404 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
405 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
407 ret
= timer_settime(*timer_id
, 0, &its
, NULL
);
409 PERROR("timer_settime");
410 goto error_destroy_timer
;
415 delete_ret
= timer_delete(*timer_id
);
416 if (delete_ret
== -1) {
417 PERROR("timer_delete");
423 int consumer_channel_timer_stop(timer_t
*timer_id
, int signal
)
427 ret
= timer_delete(*timer_id
);
429 PERROR("timer_delete");
433 consumer_timer_signal_thread_qs(signal
);
440 * Set the channel's switch timer.
442 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
443 unsigned int switch_timer_interval_us
)
448 assert(channel
->key
);
450 ret
= consumer_channel_timer_start(&channel
->switch_timer
, channel
,
451 switch_timer_interval_us
, LTTNG_CONSUMER_SIG_SWITCH
);
453 channel
->switch_timer_enabled
= !!(ret
== 0);
457 * Stop and delete the channel's switch timer.
459 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
465 ret
= consumer_channel_timer_stop(&channel
->switch_timer
,
466 LTTNG_CONSUMER_SIG_SWITCH
);
468 ERR("Failed to stop switch timer");
471 channel
->switch_timer_enabled
= 0;
475 * Set the channel's live timer.
477 void consumer_timer_live_start(struct lttng_consumer_channel
*channel
,
478 unsigned int live_timer_interval_us
)
483 assert(channel
->key
);
485 ret
= consumer_channel_timer_start(&channel
->live_timer
, channel
,
486 live_timer_interval_us
, LTTNG_CONSUMER_SIG_LIVE
);
488 channel
->live_timer_enabled
= !!(ret
== 0);
492 * Stop and delete the channel's live timer.
494 void consumer_timer_live_stop(struct lttng_consumer_channel
*channel
)
500 ret
= consumer_channel_timer_stop(&channel
->live_timer
,
501 LTTNG_CONSUMER_SIG_LIVE
);
503 ERR("Failed to stop live timer");
506 channel
->live_timer_enabled
= 0;
510 * Set the channel's monitoring timer.
512 * Returns a negative value on error, 0 if a timer was created, and
513 * a positive value if no timer was created (not an error).
515 int consumer_timer_monitor_start(struct lttng_consumer_channel
*channel
,
516 unsigned int monitor_timer_interval_us
)
521 assert(channel
->key
);
522 assert(!channel
->monitor_timer_enabled
);
524 ret
= consumer_channel_timer_start(&channel
->monitor_timer
, channel
,
525 monitor_timer_interval_us
, LTTNG_CONSUMER_SIG_MONITOR
);
526 channel
->monitor_timer_enabled
= !!(ret
== 0);
531 * Stop and delete the channel's monitoring timer.
533 int consumer_timer_monitor_stop(struct lttng_consumer_channel
*channel
)
538 assert(channel
->monitor_timer_enabled
);
540 ret
= consumer_channel_timer_stop(&channel
->monitor_timer
,
541 LTTNG_CONSUMER_SIG_MONITOR
);
543 ERR("Failed to stop live timer");
547 channel
->monitor_timer_enabled
= 0;
553 * Block the RT signals for the entire process. It must be called from the
554 * consumer main before creating the threads
556 int consumer_signal_init(void)
561 /* Block signal for entire process, so only our thread processes it. */
563 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
566 PERROR("pthread_sigmask");
573 int sample_channel_positions(struct lttng_consumer_channel
*channel
,
574 uint64_t *_highest_use
, uint64_t *_lowest_use
, uint64_t *_total_consumed
,
575 sample_positions_cb sample
, get_consumed_cb get_consumed
,
576 get_produced_cb get_produced
)
579 struct lttng_ht_iter iter
;
580 struct lttng_consumer_stream
*stream
;
581 bool empty_channel
= true;
582 uint64_t high
= 0, low
= UINT64_MAX
;
583 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
585 *_total_consumed
= 0;
589 cds_lfht_for_each_entry_duplicate(ht
->ht
,
590 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
591 ht
->match_fct
, &channel
->key
,
592 &iter
.iter
, stream
, node_channel_id
.node
) {
593 unsigned long produced
, consumed
, usage
;
595 empty_channel
= false;
597 pthread_mutex_lock(&stream
->lock
);
598 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
602 ret
= sample(stream
);
604 ERR("Failed to take buffer position snapshot in monitor timer (ret = %d)", ret
);
605 pthread_mutex_unlock(&stream
->lock
);
608 ret
= get_consumed(stream
, &consumed
);
610 ERR("Failed to get buffer consumed position in monitor timer");
611 pthread_mutex_unlock(&stream
->lock
);
614 ret
= get_produced(stream
, &produced
);
616 ERR("Failed to get buffer produced position in monitor timer");
617 pthread_mutex_unlock(&stream
->lock
);
621 usage
= produced
- consumed
;
622 high
= (usage
> high
) ? usage
: high
;
623 low
= (usage
< low
) ? usage
: low
;
626 * We don't use consumed here for 2 reasons:
627 * - output_written takes into account the padding written in the
628 * tracefiles when we stop the session;
629 * - the consumed position is not the accurate representation of what
630 * was extracted from a buffer in overwrite mode.
632 *_total_consumed
+= stream
->output_written
;
634 pthread_mutex_unlock(&stream
->lock
);
637 *_highest_use
= high
;
648 * Execute action on a monitor timer.
651 void monitor_timer(struct lttng_consumer_channel
*channel
)
654 int channel_monitor_pipe
=
655 consumer_timer_thread_get_channel_monitor_pipe();
656 struct lttcomm_consumer_channel_monitor_msg msg
= {
659 sample_positions_cb sample
;
660 get_consumed_cb get_consumed
;
661 get_produced_cb get_produced
;
662 uint64_t lowest
= 0, highest
= 0, total_consumed
= 0;
666 if (channel_monitor_pipe
< 0) {
670 switch (consumer_data
.type
) {
671 case LTTNG_CONSUMER_KERNEL
:
672 sample
= lttng_kconsumer_sample_snapshot_positions
;
673 get_consumed
= lttng_kconsumer_get_consumed_snapshot
;
674 get_produced
= lttng_kconsumer_get_produced_snapshot
;
676 case LTTNG_CONSUMER32_UST
:
677 case LTTNG_CONSUMER64_UST
:
678 sample
= lttng_ustconsumer_sample_snapshot_positions
;
679 get_consumed
= lttng_ustconsumer_get_consumed_snapshot
;
680 get_produced
= lttng_ustconsumer_get_produced_snapshot
;
686 ret
= sample_channel_positions(channel
, &highest
, &lowest
,
687 &total_consumed
, sample
, get_consumed
, get_produced
);
691 msg
.highest
= highest
;
693 msg
.total_consumed
= total_consumed
;
696 * Writes performed here are assumed to be atomic which is only
697 * guaranteed for sizes < than PIPE_BUF.
699 assert(sizeof(msg
) <= PIPE_BUF
);
702 ret
= write(channel_monitor_pipe
, &msg
, sizeof(msg
));
703 } while (ret
== -1 && errno
== EINTR
);
705 if (errno
== EAGAIN
) {
706 /* Not an error, the sample is merely dropped. */
707 DBG("Channel monitor pipe is full; dropping sample for channel key = %"PRIu64
,
710 PERROR("write to the channel monitor pipe");
713 DBG("Sent channel monitoring sample for channel key %" PRIu64
714 ", (highest = %" PRIu64
", lowest = %"PRIu64
")",
715 channel
->key
, msg
.highest
, msg
.lowest
);
719 int consumer_timer_thread_get_channel_monitor_pipe(void)
721 return uatomic_read(&channel_monitor_pipe
);
724 int consumer_timer_thread_set_channel_monitor_pipe(int fd
)
728 ret
= uatomic_cmpxchg(&channel_monitor_pipe
, -1, fd
);
739 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
740 * LTTNG_CONSUMER_SIG_TEARDOWN, LTTNG_CONSUMER_SIG_LIVE, and
741 * LTTNG_CONSUMER_SIG_MONITOR, LTTNG_CONSUMER_SIG_EXIT.
743 void *consumer_timer_thread(void *data
)
748 struct lttng_consumer_local_data
*ctx
= data
;
750 rcu_register_thread();
752 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
754 if (testpoint(consumerd_thread_metadata_timer
)) {
755 goto error_testpoint
;
758 health_code_update();
760 /* Only self thread will receive signal mask. */
762 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
765 health_code_update();
768 signr
= sigwaitinfo(&mask
, &info
);
772 * NOTE: cascading conditions are used instead of a switch case
773 * since the use of SIGRTMIN in the definition of the signals'
774 * values prevents the reduction to an integer constant.
777 if (errno
!= EINTR
) {
778 PERROR("sigwaitinfo");
781 } else if (signr
== LTTNG_CONSUMER_SIG_SWITCH
) {
782 metadata_switch_timer(ctx
, &info
);
783 } else if (signr
== LTTNG_CONSUMER_SIG_TEARDOWN
) {
785 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
787 DBG("Signal timer metadata thread teardown");
788 } else if (signr
== LTTNG_CONSUMER_SIG_LIVE
) {
789 live_timer(ctx
, &info
);
790 } else if (signr
== LTTNG_CONSUMER_SIG_MONITOR
) {
791 struct lttng_consumer_channel
*channel
;
793 channel
= info
.si_value
.sival_ptr
;
794 monitor_timer(channel
);
795 } else if (signr
== LTTNG_CONSUMER_SIG_EXIT
) {
796 assert(CMM_LOAD_SHARED(consumer_quit
));
799 ERR("Unexpected signal %d\n", info
.si_signo
);
804 /* Only reached in testpoint error */
807 health_unregister(health_consumerd
);
808 rcu_unregister_thread();