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");
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_kernel_stream(struct lttng_consumer_stream
*stream
)
194 * While holding the stream mutex, try to take a snapshot, if it
195 * succeeds, it means that data is ready to be sent, just let the data
196 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
197 * means that there is no data to read after the flush, so we can
198 * safely send the empty index.
200 * Doing a trylock and checking if waiting on metadata if
201 * trylock fails. Bail out of the stream is indeed waiting for
202 * metadata to be pushed. Busy wait on trylock otherwise.
205 ret
= pthread_mutex_trylock(&stream
->lock
);
208 break; /* We have the lock. */
210 pthread_mutex_lock(&stream
->metadata_timer_lock
);
211 if (stream
->waiting_on_metadata
) {
213 stream
->missed_metadata_flush
= true;
214 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
215 goto end
; /* Bail out. */
217 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
222 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
228 ret
= consumer_flush_kernel_index(stream
);
229 pthread_mutex_unlock(&stream
->lock
);
234 int consumer_flush_ust_index(struct lttng_consumer_stream
*stream
)
236 uint64_t ts
, stream_id
;
239 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
244 ret
= lttng_ustconsumer_get_current_timestamp(stream
, &ts
);
246 ERR("Failed to get the current timestamp");
249 lttng_ustconsumer_flush_buffer(stream
, 1);
250 ret
= lttng_ustconsumer_take_snapshot(stream
);
252 if (ret
!= -EAGAIN
) {
253 ERR("Taking UST snapshot");
257 ret
= lttng_ustconsumer_get_stream_id(stream
, &stream_id
);
259 PERROR("ustctl_get_stream_id");
262 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
263 ret
= send_empty_index(stream
, ts
, stream_id
);
273 static int check_ust_stream(struct lttng_consumer_stream
*stream
)
278 assert(stream
->ustream
);
280 * While holding the stream mutex, try to take a snapshot, if it
281 * succeeds, it means that data is ready to be sent, just let the data
282 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
283 * means that there is no data to read after the flush, so we can
284 * safely send the empty index.
286 * Doing a trylock and checking if waiting on metadata if
287 * trylock fails. Bail out of the stream is indeed waiting for
288 * metadata to be pushed. Busy wait on trylock otherwise.
291 ret
= pthread_mutex_trylock(&stream
->lock
);
294 break; /* We have the lock. */
296 pthread_mutex_lock(&stream
->metadata_timer_lock
);
297 if (stream
->waiting_on_metadata
) {
299 stream
->missed_metadata_flush
= true;
300 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
301 goto end
; /* Bail out. */
303 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
308 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
314 ret
= consumer_flush_ust_index(stream
);
315 pthread_mutex_unlock(&stream
->lock
);
321 * Execute action on a live timer
323 static void live_timer(struct lttng_consumer_local_data
*ctx
,
327 struct lttng_consumer_channel
*channel
;
328 struct lttng_consumer_stream
*stream
;
330 struct lttng_ht_iter iter
;
332 channel
= si
->si_value
.sival_ptr
;
335 if (channel
->switch_timer_error
) {
338 ht
= consumer_data
.stream_per_chan_id_ht
;
340 DBG("Live timer for channel %" PRIu64
, channel
->key
);
344 case LTTNG_CONSUMER32_UST
:
345 case LTTNG_CONSUMER64_UST
:
346 cds_lfht_for_each_entry_duplicate(ht
->ht
,
347 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
348 ht
->match_fct
, &channel
->key
, &iter
.iter
,
349 stream
, node_channel_id
.node
) {
350 ret
= check_ust_stream(stream
);
356 case LTTNG_CONSUMER_KERNEL
:
357 cds_lfht_for_each_entry_duplicate(ht
->ht
,
358 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
359 ht
->match_fct
, &channel
->key
, &iter
.iter
,
360 stream
, node_channel_id
.node
) {
361 ret
= check_kernel_stream(stream
);
367 case LTTNG_CONSUMER_UNKNOWN
:
380 void consumer_timer_signal_thread_qs(unsigned int signr
)
382 sigset_t pending_set
;
386 * We need to be the only thread interacting with the thread
387 * that manages signals for teardown synchronization.
389 pthread_mutex_lock(&timer_signal
.lock
);
391 /* Ensure we don't have any signal queued for this channel. */
393 ret
= sigemptyset(&pending_set
);
395 PERROR("sigemptyset");
397 ret
= sigpending(&pending_set
);
399 PERROR("sigpending");
401 if (!sigismember(&pending_set
, signr
)) {
408 * From this point, no new signal handler will be fired that would try to
409 * access "chan". However, we still need to wait for any currently
410 * executing handler to complete.
413 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
417 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
420 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
422 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
427 pthread_mutex_unlock(&timer_signal
.lock
);
431 * Start a timer channel timer which will fire at a given interval
432 * (timer_interval_us)and fire a given signal (signal).
434 * Returns a negative value on error, 0 if a timer was created, and
435 * a positive value if no timer was created (not an error).
438 int consumer_channel_timer_start(timer_t
*timer_id
,
439 struct lttng_consumer_channel
*channel
,
440 unsigned int timer_interval_us
, int signal
)
442 int ret
= 0, delete_ret
;
444 struct itimerspec its
;
447 assert(channel
->key
);
449 if (timer_interval_us
== 0) {
450 /* No creation needed; not an error. */
455 sev
.sigev_notify
= SIGEV_SIGNAL
;
456 sev
.sigev_signo
= signal
;
457 sev
.sigev_value
.sival_ptr
= channel
;
458 ret
= timer_create(CLOCKID
, &sev
, timer_id
);
460 PERROR("timer_create");
464 its
.it_value
.tv_sec
= timer_interval_us
/ 1000000;
465 its
.it_value
.tv_nsec
= (timer_interval_us
% 1000000) * 1000;
466 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
467 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
469 ret
= timer_settime(*timer_id
, 0, &its
, NULL
);
471 PERROR("timer_settime");
472 goto error_destroy_timer
;
477 delete_ret
= timer_delete(*timer_id
);
478 if (delete_ret
== -1) {
479 PERROR("timer_delete");
485 int consumer_channel_timer_stop(timer_t
*timer_id
, int signal
)
489 ret
= timer_delete(*timer_id
);
491 PERROR("timer_delete");
495 consumer_timer_signal_thread_qs(signal
);
502 * Set the channel's switch timer.
504 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
505 unsigned int switch_timer_interval_us
)
510 assert(channel
->key
);
512 ret
= consumer_channel_timer_start(&channel
->switch_timer
, channel
,
513 switch_timer_interval_us
, LTTNG_CONSUMER_SIG_SWITCH
);
515 channel
->switch_timer_enabled
= !!(ret
== 0);
519 * Stop and delete the channel's switch timer.
521 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
527 ret
= consumer_channel_timer_stop(&channel
->switch_timer
,
528 LTTNG_CONSUMER_SIG_SWITCH
);
530 ERR("Failed to stop switch timer");
533 channel
->switch_timer_enabled
= 0;
537 * Set the channel's live timer.
539 void consumer_timer_live_start(struct lttng_consumer_channel
*channel
,
540 unsigned int live_timer_interval_us
)
545 assert(channel
->key
);
547 ret
= consumer_channel_timer_start(&channel
->live_timer
, channel
,
548 live_timer_interval_us
, LTTNG_CONSUMER_SIG_LIVE
);
550 channel
->live_timer_enabled
= !!(ret
== 0);
554 * Stop and delete the channel's live timer.
556 void consumer_timer_live_stop(struct lttng_consumer_channel
*channel
)
562 ret
= consumer_channel_timer_stop(&channel
->live_timer
,
563 LTTNG_CONSUMER_SIG_LIVE
);
565 ERR("Failed to stop live timer");
568 channel
->live_timer_enabled
= 0;
572 * Set the channel's monitoring timer.
574 * Returns a negative value on error, 0 if a timer was created, and
575 * a positive value if no timer was created (not an error).
577 int consumer_timer_monitor_start(struct lttng_consumer_channel
*channel
,
578 unsigned int monitor_timer_interval_us
)
583 assert(channel
->key
);
584 assert(!channel
->monitor_timer_enabled
);
586 ret
= consumer_channel_timer_start(&channel
->monitor_timer
, channel
,
587 monitor_timer_interval_us
, LTTNG_CONSUMER_SIG_MONITOR
);
588 channel
->monitor_timer_enabled
= !!(ret
== 0);
593 * Stop and delete the channel's monitoring timer.
595 int consumer_timer_monitor_stop(struct lttng_consumer_channel
*channel
)
600 assert(channel
->monitor_timer_enabled
);
602 ret
= consumer_channel_timer_stop(&channel
->monitor_timer
,
603 LTTNG_CONSUMER_SIG_MONITOR
);
605 ERR("Failed to stop live timer");
609 channel
->monitor_timer_enabled
= 0;
615 * Block the RT signals for the entire process. It must be called from the
616 * consumer main before creating the threads
618 int consumer_signal_init(void)
623 /* Block signal for entire process, so only our thread processes it. */
625 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
628 PERROR("pthread_sigmask");
635 int sample_channel_positions(struct lttng_consumer_channel
*channel
,
636 uint64_t *_highest_use
, uint64_t *_lowest_use
, uint64_t *_total_consumed
,
637 sample_positions_cb sample
, get_consumed_cb get_consumed
,
638 get_produced_cb get_produced
)
641 struct lttng_ht_iter iter
;
642 struct lttng_consumer_stream
*stream
;
643 bool empty_channel
= true;
644 uint64_t high
= 0, low
= UINT64_MAX
;
645 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
647 *_total_consumed
= 0;
651 cds_lfht_for_each_entry_duplicate(ht
->ht
,
652 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
653 ht
->match_fct
, &channel
->key
,
654 &iter
.iter
, stream
, node_channel_id
.node
) {
655 unsigned long produced
, consumed
, usage
;
657 empty_channel
= false;
659 pthread_mutex_lock(&stream
->lock
);
660 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
664 ret
= sample(stream
);
666 ERR("Failed to take buffer position snapshot in monitor timer (ret = %d)", ret
);
667 pthread_mutex_unlock(&stream
->lock
);
670 ret
= get_consumed(stream
, &consumed
);
672 ERR("Failed to get buffer consumed position in monitor timer");
673 pthread_mutex_unlock(&stream
->lock
);
676 ret
= get_produced(stream
, &produced
);
678 ERR("Failed to get buffer produced position in monitor timer");
679 pthread_mutex_unlock(&stream
->lock
);
683 usage
= produced
- consumed
;
684 high
= (usage
> high
) ? usage
: high
;
685 low
= (usage
< low
) ? usage
: low
;
688 * We don't use consumed here for 2 reasons:
689 * - output_written takes into account the padding written in the
690 * tracefiles when we stop the session;
691 * - the consumed position is not the accurate representation of what
692 * was extracted from a buffer in overwrite mode.
694 *_total_consumed
+= stream
->output_written
;
696 pthread_mutex_unlock(&stream
->lock
);
699 *_highest_use
= high
;
710 * Execute action on a monitor timer.
713 void monitor_timer(struct lttng_consumer_channel
*channel
)
716 int channel_monitor_pipe
=
717 consumer_timer_thread_get_channel_monitor_pipe();
718 struct lttcomm_consumer_channel_monitor_msg msg
= {
721 sample_positions_cb sample
;
722 get_consumed_cb get_consumed
;
723 get_produced_cb get_produced
;
727 if (channel_monitor_pipe
< 0) {
731 switch (consumer_data
.type
) {
732 case LTTNG_CONSUMER_KERNEL
:
733 sample
= lttng_kconsumer_sample_snapshot_positions
;
734 get_consumed
= lttng_kconsumer_get_consumed_snapshot
;
735 get_produced
= lttng_kconsumer_get_produced_snapshot
;
737 case LTTNG_CONSUMER32_UST
:
738 case LTTNG_CONSUMER64_UST
:
739 sample
= lttng_ustconsumer_sample_snapshot_positions
;
740 get_consumed
= lttng_ustconsumer_get_consumed_snapshot
;
741 get_produced
= lttng_ustconsumer_get_produced_snapshot
;
747 ret
= sample_channel_positions(channel
, &msg
.highest
, &msg
.lowest
,
748 &msg
.total_consumed
, sample
, get_consumed
, get_produced
);
754 * Writes performed here are assumed to be atomic which is only
755 * guaranteed for sizes < than PIPE_BUF.
757 assert(sizeof(msg
) <= PIPE_BUF
);
760 ret
= write(channel_monitor_pipe
, &msg
, sizeof(msg
));
761 } while (ret
== -1 && errno
== EINTR
);
763 if (errno
== EAGAIN
) {
764 /* Not an error, the sample is merely dropped. */
765 DBG("Channel monitor pipe is full; dropping sample for channel key = %"PRIu64
,
768 PERROR("write to the channel monitor pipe");
771 DBG("Sent channel monitoring sample for channel key %" PRIu64
772 ", (highest = %" PRIu64
", lowest = %"PRIu64
")",
773 channel
->key
, msg
.highest
, msg
.lowest
);
777 int consumer_timer_thread_get_channel_monitor_pipe(void)
779 return uatomic_read(&channel_monitor_pipe
);
782 int consumer_timer_thread_set_channel_monitor_pipe(int fd
)
786 ret
= uatomic_cmpxchg(&channel_monitor_pipe
, -1, fd
);
797 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
798 * LTTNG_CONSUMER_SIG_TEARDOWN, LTTNG_CONSUMER_SIG_LIVE, and
799 * LTTNG_CONSUMER_SIG_MONITOR, LTTNG_CONSUMER_SIG_EXIT.
801 void *consumer_timer_thread(void *data
)
806 struct lttng_consumer_local_data
*ctx
= data
;
808 rcu_register_thread();
810 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
812 if (testpoint(consumerd_thread_metadata_timer
)) {
813 goto error_testpoint
;
816 health_code_update();
818 /* Only self thread will receive signal mask. */
820 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
823 health_code_update();
826 signr
= sigwaitinfo(&mask
, &info
);
830 * NOTE: cascading conditions are used instead of a switch case
831 * since the use of SIGRTMIN in the definition of the signals'
832 * values prevents the reduction to an integer constant.
835 if (errno
!= EINTR
) {
836 PERROR("sigwaitinfo");
839 } else if (signr
== LTTNG_CONSUMER_SIG_SWITCH
) {
840 metadata_switch_timer(ctx
, &info
);
841 } else if (signr
== LTTNG_CONSUMER_SIG_TEARDOWN
) {
843 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
845 DBG("Signal timer metadata thread teardown");
846 } else if (signr
== LTTNG_CONSUMER_SIG_LIVE
) {
847 live_timer(ctx
, &info
);
848 } else if (signr
== LTTNG_CONSUMER_SIG_MONITOR
) {
849 struct lttng_consumer_channel
*channel
;
851 channel
= info
.si_value
.sival_ptr
;
852 monitor_timer(channel
);
853 } else if (signr
== LTTNG_CONSUMER_SIG_EXIT
) {
854 assert(CMM_LOAD_SHARED(consumer_quit
));
857 ERR("Unexpected signal %d\n", info
.si_signo
);
862 /* Only reached in testpoint error */
865 health_unregister(health_consumerd
);
866 rcu_unregister_thread();