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 static struct timer_signal_data timer_signal
= {
38 .lock
= PTHREAD_MUTEX_INITIALIZER
,
42 * Set custom signal mask to current thread.
44 static void setmask(sigset_t
*mask
)
48 ret
= sigemptyset(mask
);
50 PERROR("sigemptyset");
52 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_SWITCH
);
54 PERROR("sigaddset switch");
56 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_TEARDOWN
);
58 PERROR("sigaddset teardown");
60 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_LIVE
);
62 PERROR("sigaddset live");
64 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_EXIT
);
66 PERROR("sigaddset exit");
71 * Execute action on a timer switch.
73 * Beware: metadata_switch_timer() should *never* take a mutex also held
74 * while consumer_timer_switch_stop() is called. It would result in
77 static void metadata_switch_timer(struct lttng_consumer_local_data
*ctx
,
78 int sig
, siginfo_t
*si
, void *uc
)
81 struct lttng_consumer_channel
*channel
;
83 channel
= si
->si_value
.sival_ptr
;
86 if (channel
->switch_timer_error
) {
90 DBG("Switch timer for channel %" PRIu64
, channel
->key
);
92 case LTTNG_CONSUMER32_UST
:
93 case LTTNG_CONSUMER64_UST
:
95 * Locks taken by lttng_ustconsumer_request_metadata():
96 * - metadata_socket_lock
97 * - Calling lttng_ustconsumer_recv_metadata():
98 * - channel->metadata_cache->lock
99 * - Calling consumer_metadata_cache_flushed():
100 * - channel->timer_lock
101 * - channel->metadata_cache->lock
103 * Ensure that neither consumer_data.lock nor
104 * channel->lock are taken within this function, since
105 * they are held while consumer_timer_switch_stop() is
108 ret
= lttng_ustconsumer_request_metadata(ctx
, channel
, 1, 1);
110 channel
->switch_timer_error
= 1;
113 case LTTNG_CONSUMER_KERNEL
:
114 case LTTNG_CONSUMER_UNKNOWN
:
120 static int send_empty_index(struct lttng_consumer_stream
*stream
, uint64_t ts
,
124 struct ctf_packet_index index
;
126 memset(&index
, 0, sizeof(index
));
127 index
.stream_id
= htobe64(stream_id
);
128 index
.timestamp_end
= htobe64(ts
);
129 ret
= consumer_stream_write_index(stream
, &index
);
138 int consumer_flush_kernel_index(struct lttng_consumer_stream
*stream
)
140 uint64_t ts
, stream_id
;
143 ret
= kernctl_get_current_timestamp(stream
->wait_fd
, &ts
);
145 ERR("Failed to get the current timestamp");
148 ret
= kernctl_buffer_flush(stream
->wait_fd
);
150 ERR("Failed to flush kernel stream");
153 ret
= kernctl_snapshot(stream
->wait_fd
);
155 if (errno
!= EAGAIN
&& errno
!= ENODATA
) {
156 PERROR("live timer kernel snapshot");
160 ret
= kernctl_get_stream_id(stream
->wait_fd
, &stream_id
);
162 PERROR("kernctl_get_stream_id");
165 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
166 ret
= send_empty_index(stream
, ts
, stream_id
);
176 static int check_kernel_stream(struct lttng_consumer_stream
*stream
)
181 * While holding the stream mutex, try to take a snapshot, if it
182 * succeeds, it means that data is ready to be sent, just let the data
183 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
184 * means that there is no data to read after the flush, so we can
185 * safely send the empty index.
187 * Doing a trylock and checking if waiting on metadata if
188 * trylock fails. Bail out of the stream is indeed waiting for
189 * metadata to be pushed. Busy wait on trylock otherwise.
192 ret
= pthread_mutex_trylock(&stream
->lock
);
195 break; /* We have the lock. */
197 pthread_mutex_lock(&stream
->metadata_timer_lock
);
198 if (stream
->waiting_on_metadata
) {
200 stream
->missed_metadata_flush
= true;
201 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
202 goto end
; /* Bail out. */
204 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
209 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
215 ret
= consumer_flush_kernel_index(stream
);
216 pthread_mutex_unlock(&stream
->lock
);
221 int consumer_flush_ust_index(struct lttng_consumer_stream
*stream
)
223 uint64_t ts
, stream_id
;
226 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
231 ret
= lttng_ustconsumer_get_current_timestamp(stream
, &ts
);
233 ERR("Failed to get the current timestamp");
236 lttng_ustconsumer_flush_buffer(stream
, 1);
237 ret
= lttng_ustconsumer_take_snapshot(stream
);
239 if (ret
!= -EAGAIN
) {
240 ERR("Taking UST snapshot");
244 ret
= lttng_ustconsumer_get_stream_id(stream
, &stream_id
);
246 PERROR("ustctl_get_stream_id");
249 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
250 ret
= send_empty_index(stream
, ts
, stream_id
);
260 static int check_ust_stream(struct lttng_consumer_stream
*stream
)
265 assert(stream
->ustream
);
267 * While holding the stream mutex, try to take a snapshot, if it
268 * succeeds, it means that data is ready to be sent, just let the data
269 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
270 * means that there is no data to read after the flush, so we can
271 * safely send the empty index.
273 * Doing a trylock and checking if waiting on metadata if
274 * trylock fails. Bail out of the stream is indeed waiting for
275 * metadata to be pushed. Busy wait on trylock otherwise.
278 ret
= pthread_mutex_trylock(&stream
->lock
);
281 break; /* We have the lock. */
283 pthread_mutex_lock(&stream
->metadata_timer_lock
);
284 if (stream
->waiting_on_metadata
) {
286 stream
->missed_metadata_flush
= true;
287 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
288 goto end
; /* Bail out. */
290 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
295 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
301 ret
= consumer_flush_ust_index(stream
);
302 pthread_mutex_unlock(&stream
->lock
);
308 * Execute action on a live timer
310 static void live_timer(struct lttng_consumer_local_data
*ctx
,
311 int sig
, siginfo_t
*si
, void *uc
)
314 struct lttng_consumer_channel
*channel
;
315 struct lttng_consumer_stream
*stream
;
317 struct lttng_ht_iter iter
;
319 channel
= si
->si_value
.sival_ptr
;
322 if (channel
->switch_timer_error
) {
325 ht
= consumer_data
.stream_per_chan_id_ht
;
327 DBG("Live timer for channel %" PRIu64
, channel
->key
);
331 case LTTNG_CONSUMER32_UST
:
332 case LTTNG_CONSUMER64_UST
:
333 cds_lfht_for_each_entry_duplicate(ht
->ht
,
334 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
335 ht
->match_fct
, &channel
->key
, &iter
.iter
,
336 stream
, node_channel_id
.node
) {
337 ret
= check_ust_stream(stream
);
343 case LTTNG_CONSUMER_KERNEL
:
344 cds_lfht_for_each_entry_duplicate(ht
->ht
,
345 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
346 ht
->match_fct
, &channel
->key
, &iter
.iter
,
347 stream
, node_channel_id
.node
) {
348 ret
= check_kernel_stream(stream
);
354 case LTTNG_CONSUMER_UNKNOWN
:
367 void consumer_timer_signal_thread_qs(unsigned int signr
)
369 sigset_t pending_set
;
373 * We need to be the only thread interacting with the thread
374 * that manages signals for teardown synchronization.
376 pthread_mutex_lock(&timer_signal
.lock
);
378 /* Ensure we don't have any signal queued for this channel. */
380 ret
= sigemptyset(&pending_set
);
382 PERROR("sigemptyset");
384 ret
= sigpending(&pending_set
);
386 PERROR("sigpending");
388 if (!sigismember(&pending_set
, signr
)) {
395 * From this point, no new signal handler will be fired that would try to
396 * access "chan". However, we still need to wait for any currently
397 * executing handler to complete.
400 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
404 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
407 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
409 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
414 pthread_mutex_unlock(&timer_signal
.lock
);
418 * Set the timer for periodical metadata flush.
420 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
421 unsigned int switch_timer_interval
)
425 struct itimerspec its
;
428 assert(channel
->key
);
430 if (switch_timer_interval
== 0) {
434 sev
.sigev_notify
= SIGEV_SIGNAL
;
435 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_SWITCH
;
436 sev
.sigev_value
.sival_ptr
= channel
;
437 ret
= timer_create(CLOCKID
, &sev
, &channel
->switch_timer
);
439 PERROR("timer_create");
441 channel
->switch_timer_enabled
= 1;
443 its
.it_value
.tv_sec
= switch_timer_interval
/ 1000000;
444 its
.it_value
.tv_nsec
= (switch_timer_interval
% 1000000) * 1000;
445 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
446 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
448 ret
= timer_settime(channel
->switch_timer
, 0, &its
, NULL
);
450 PERROR("timer_settime");
455 * Stop and delete timer.
457 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
463 ret
= timer_delete(channel
->switch_timer
);
465 PERROR("timer_delete");
468 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_SWITCH
);
470 channel
->switch_timer
= 0;
471 channel
->switch_timer_enabled
= 0;
475 * Set the timer for the live mode.
477 void consumer_timer_live_start(struct lttng_consumer_channel
*channel
,
478 int live_timer_interval
)
482 struct itimerspec its
;
485 assert(channel
->key
);
487 if (live_timer_interval
<= 0) {
491 sev
.sigev_notify
= SIGEV_SIGNAL
;
492 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_LIVE
;
493 sev
.sigev_value
.sival_ptr
= channel
;
494 ret
= timer_create(CLOCKID
, &sev
, &channel
->live_timer
);
496 PERROR("timer_create");
498 channel
->live_timer_enabled
= 1;
500 its
.it_value
.tv_sec
= live_timer_interval
/ 1000000;
501 its
.it_value
.tv_nsec
= (live_timer_interval
% 1000000) * 1000;
502 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
503 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
505 ret
= timer_settime(channel
->live_timer
, 0, &its
, NULL
);
507 PERROR("timer_settime");
512 * Stop and delete timer.
514 void consumer_timer_live_stop(struct lttng_consumer_channel
*channel
)
520 ret
= timer_delete(channel
->live_timer
);
522 PERROR("timer_delete");
525 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_LIVE
);
527 channel
->live_timer
= 0;
528 channel
->live_timer_enabled
= 0;
532 * Block the RT signals for the entire process. It must be called from the
533 * consumer main before creating the threads
535 int consumer_signal_init(void)
540 /* Block signal for entire process, so only our thread processes it. */
542 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
545 PERROR("pthread_sigmask");
552 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
553 * LTTNG_CONSUMER_SIG_TEARDOWN, LTTNG_CONSUMER_SIG_LIVE, and
554 * LTTNG_CONSUMER_SIG_EXIT.
556 void *consumer_timer_thread(void *data
)
561 struct lttng_consumer_local_data
*ctx
= data
;
563 rcu_register_thread();
565 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
567 if (testpoint(consumerd_thread_metadata_timer
)) {
568 goto error_testpoint
;
571 health_code_update();
573 /* Only self thread will receive signal mask. */
575 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
578 health_code_update();
581 signr
= sigwaitinfo(&mask
, &info
);
584 if (errno
!= EINTR
) {
585 PERROR("sigwaitinfo");
588 } else if (signr
== LTTNG_CONSUMER_SIG_SWITCH
) {
589 metadata_switch_timer(ctx
, info
.si_signo
, &info
, NULL
);
590 } else if (signr
== LTTNG_CONSUMER_SIG_TEARDOWN
) {
592 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
594 DBG("Signal timer metadata thread teardown");
595 } else if (signr
== LTTNG_CONSUMER_SIG_LIVE
) {
596 live_timer(ctx
, info
.si_signo
, &info
, NULL
);
597 } else if (signr
== LTTNG_CONSUMER_SIG_EXIT
) {
598 assert(consumer_quit
);
601 ERR("Unexpected signal %d\n", info
.si_signo
);
606 /* Only reached in testpoint error */
609 health_unregister(health_consumerd
);
610 rcu_unregister_thread();