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.
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-stream.h>
32 #include "consumer-timer.h"
33 #include "consumer-testpoint.h"
34 #include "ust-consumer/ust-consumer.h"
36 static struct timer_signal_data timer_signal
= {
40 .lock
= PTHREAD_MUTEX_INITIALIZER
,
44 * Set custom signal mask to current thread.
46 static void setmask(sigset_t
*mask
)
50 ret
= sigemptyset(mask
);
52 PERROR("sigemptyset");
54 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_SWITCH
);
56 PERROR("sigaddset switch");
58 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_TEARDOWN
);
60 PERROR("sigaddset teardown");
62 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_LIVE
);
64 PERROR("sigaddset live");
69 * Execute action on a timer switch.
71 * Beware: metadata_switch_timer() should *never* take a mutex also held
72 * while consumer_timer_switch_stop() is called. It would result in
75 static void metadata_switch_timer(struct lttng_consumer_local_data
*ctx
,
76 int sig
, siginfo_t
*si
, void *uc
)
79 struct lttng_consumer_channel
*channel
;
81 channel
= si
->si_value
.sival_ptr
;
84 if (channel
->switch_timer_error
) {
88 DBG("Switch timer for channel %" PRIu64
, channel
->key
);
90 case LTTNG_CONSUMER32_UST
:
91 case LTTNG_CONSUMER64_UST
:
93 * Locks taken by lttng_ustconsumer_request_metadata():
94 * - metadata_socket_lock
95 * - Calling lttng_ustconsumer_recv_metadata():
96 * - channel->metadata_cache->lock
97 * - Calling consumer_metadata_cache_flushed():
98 * - channel->timer_lock
99 * - channel->metadata_cache->lock
101 * Ensure that neither consumer_data.lock nor
102 * channel->lock are taken within this function, since
103 * they are held while consumer_timer_switch_stop() is
106 ret
= lttng_ustconsumer_request_metadata(ctx
, channel
, 1, 1);
108 channel
->switch_timer_error
= 1;
111 case LTTNG_CONSUMER_KERNEL
:
112 case LTTNG_CONSUMER_UNKNOWN
:
118 static int send_empty_index(struct lttng_consumer_stream
*stream
, uint64_t ts
,
122 struct ctf_packet_index index
;
124 memset(&index
, 0, sizeof(index
));
125 index
.stream_id
= htobe64(stream_id
);
126 index
.timestamp_end
= htobe64(ts
);
127 ret
= consumer_stream_write_index(stream
, &index
);
136 int consumer_flush_kernel_index(struct lttng_consumer_stream
*stream
)
138 uint64_t ts
, stream_id
;
141 ret
= kernctl_get_current_timestamp(stream
->wait_fd
, &ts
);
143 ERR("Failed to get the current timestamp");
146 ret
= kernctl_buffer_flush(stream
->wait_fd
);
148 ERR("Failed to flush kernel stream");
151 ret
= kernctl_snapshot(stream
->wait_fd
);
153 if (errno
!= EAGAIN
&& errno
!= ENODATA
) {
154 PERROR("live timer kernel snapshot");
158 ret
= kernctl_get_stream_id(stream
->wait_fd
, &stream_id
);
160 PERROR("kernctl_get_stream_id");
163 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
164 ret
= send_empty_index(stream
, ts
, stream_id
);
174 static int check_kernel_stream(struct lttng_consumer_stream
*stream
)
179 * While holding the stream mutex, try to take a snapshot, if it
180 * succeeds, it means that data is ready to be sent, just let the data
181 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
182 * means that there is no data to read after the flush, so we can
183 * safely send the empty index.
185 * Doing a trylock and checking if waiting on metadata if
186 * trylock fails. Bail out of the stream is indeed waiting for
187 * metadata to be pushed. Busy wait on trylock otherwise.
190 ret
= pthread_mutex_trylock(&stream
->lock
);
193 break; /* We have the lock. */
195 pthread_mutex_lock(&stream
->metadata_timer_lock
);
196 if (stream
->waiting_on_metadata
) {
198 stream
->missed_metadata_flush
= true;
199 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
200 goto end
; /* Bail out. */
202 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
207 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
213 ret
= consumer_flush_kernel_index(stream
);
214 pthread_mutex_unlock(&stream
->lock
);
219 int consumer_flush_ust_index(struct lttng_consumer_stream
*stream
)
221 uint64_t ts
, stream_id
;
224 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
229 ret
= lttng_ustconsumer_get_current_timestamp(stream
, &ts
);
231 ERR("Failed to get the current timestamp");
234 lttng_ustconsumer_flush_buffer(stream
, 1);
235 ret
= lttng_ustconsumer_take_snapshot(stream
);
237 if (ret
!= -EAGAIN
) {
238 ERR("Taking UST snapshot");
242 ret
= lttng_ustconsumer_get_stream_id(stream
, &stream_id
);
244 PERROR("ustctl_get_stream_id");
247 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
248 ret
= send_empty_index(stream
, ts
, stream_id
);
258 static int check_ust_stream(struct lttng_consumer_stream
*stream
)
263 assert(stream
->ustream
);
265 * While holding the stream mutex, try to take a snapshot, if it
266 * succeeds, it means that data is ready to be sent, just let the data
267 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
268 * means that there is no data to read after the flush, so we can
269 * safely send the empty index.
271 * Doing a trylock and checking if waiting on metadata if
272 * trylock fails. Bail out of the stream is indeed waiting for
273 * metadata to be pushed. Busy wait on trylock otherwise.
276 ret
= pthread_mutex_trylock(&stream
->lock
);
279 break; /* We have the lock. */
281 pthread_mutex_lock(&stream
->metadata_timer_lock
);
282 if (stream
->waiting_on_metadata
) {
284 stream
->missed_metadata_flush
= true;
285 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
286 goto end
; /* Bail out. */
288 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
293 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
299 ret
= consumer_flush_ust_index(stream
);
300 pthread_mutex_unlock(&stream
->lock
);
306 * Execute action on a live timer
308 static void live_timer(struct lttng_consumer_local_data
*ctx
,
309 int sig
, siginfo_t
*si
, void *uc
)
312 struct lttng_consumer_channel
*channel
;
313 struct lttng_consumer_stream
*stream
;
315 struct lttng_ht_iter iter
;
317 channel
= si
->si_value
.sival_ptr
;
320 if (channel
->switch_timer_error
) {
323 ht
= consumer_data
.stream_per_chan_id_ht
;
325 DBG("Live timer for channel %" PRIu64
, channel
->key
);
329 case LTTNG_CONSUMER32_UST
:
330 case LTTNG_CONSUMER64_UST
:
331 cds_lfht_for_each_entry_duplicate(ht
->ht
,
332 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
333 ht
->match_fct
, &channel
->key
, &iter
.iter
,
334 stream
, node_channel_id
.node
) {
335 ret
= check_ust_stream(stream
);
341 case LTTNG_CONSUMER_KERNEL
:
342 cds_lfht_for_each_entry_duplicate(ht
->ht
,
343 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
344 ht
->match_fct
, &channel
->key
, &iter
.iter
,
345 stream
, node_channel_id
.node
) {
346 ret
= check_kernel_stream(stream
);
352 case LTTNG_CONSUMER_UNKNOWN
:
365 void consumer_timer_signal_thread_qs(unsigned int signr
)
367 sigset_t pending_set
;
371 * We need to be the only thread interacting with the thread
372 * that manages signals for teardown synchronization.
374 pthread_mutex_lock(&timer_signal
.lock
);
376 /* Ensure we don't have any signal queued for this channel. */
378 ret
= sigemptyset(&pending_set
);
380 PERROR("sigemptyset");
382 ret
= sigpending(&pending_set
);
384 PERROR("sigpending");
386 if (!sigismember(&pending_set
, LTTNG_CONSUMER_SIG_SWITCH
)) {
393 * From this point, no new signal handler will be fired that would try to
394 * access "chan". However, we still need to wait for any currently
395 * executing handler to complete.
398 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
402 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
405 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
407 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
412 pthread_mutex_unlock(&timer_signal
.lock
);
416 * Set the timer for periodical metadata flush.
418 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
419 unsigned int switch_timer_interval
)
423 struct itimerspec its
;
426 assert(channel
->key
);
428 if (switch_timer_interval
== 0) {
432 sev
.sigev_notify
= SIGEV_SIGNAL
;
433 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_SWITCH
;
434 sev
.sigev_value
.sival_ptr
= channel
;
435 ret
= timer_create(CLOCKID
, &sev
, &channel
->switch_timer
);
437 PERROR("timer_create");
439 channel
->switch_timer_enabled
= 1;
441 its
.it_value
.tv_sec
= switch_timer_interval
/ 1000000;
442 its
.it_value
.tv_nsec
= switch_timer_interval
% 1000000;
443 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
444 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
446 ret
= timer_settime(channel
->switch_timer
, 0, &its
, NULL
);
448 PERROR("timer_settime");
453 * Stop and delete timer.
455 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
461 ret
= timer_delete(channel
->switch_timer
);
463 PERROR("timer_delete");
466 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_SWITCH
);
468 channel
->switch_timer
= 0;
469 channel
->switch_timer_enabled
= 0;
473 * Set the timer for the live mode.
475 void consumer_timer_live_start(struct lttng_consumer_channel
*channel
,
476 int live_timer_interval
)
480 struct itimerspec its
;
483 assert(channel
->key
);
485 if (live_timer_interval
<= 0) {
489 sev
.sigev_notify
= SIGEV_SIGNAL
;
490 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_LIVE
;
491 sev
.sigev_value
.sival_ptr
= channel
;
492 ret
= timer_create(CLOCKID
, &sev
, &channel
->live_timer
);
494 PERROR("timer_create");
496 channel
->live_timer_enabled
= 1;
498 its
.it_value
.tv_sec
= live_timer_interval
/ 1000000;
499 its
.it_value
.tv_nsec
= live_timer_interval
% 1000000;
500 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
501 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
503 ret
= timer_settime(channel
->live_timer
, 0, &its
, NULL
);
505 PERROR("timer_settime");
510 * Stop and delete timer.
512 void consumer_timer_live_stop(struct lttng_consumer_channel
*channel
)
518 ret
= timer_delete(channel
->live_timer
);
520 PERROR("timer_delete");
523 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_LIVE
);
525 channel
->live_timer
= 0;
526 channel
->live_timer_enabled
= 0;
530 * Block the RT signals for the entire process. It must be called from the
531 * consumer main before creating the threads
533 int consumer_signal_init(void)
538 /* Block signal for entire process, so only our thread processes it. */
540 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
543 PERROR("pthread_sigmask");
550 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
551 * LTTNG_CONSUMER_SIG_TEARDOWN and LTTNG_CONSUMER_SIG_LIVE.
553 void *consumer_timer_thread(void *data
)
558 struct lttng_consumer_local_data
*ctx
= data
;
560 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
562 if (testpoint(consumerd_thread_metadata_timer
)) {
563 goto error_testpoint
;
566 health_code_update();
568 /* Only self thread will receive signal mask. */
570 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
573 health_code_update();
576 signr
= sigwaitinfo(&mask
, &info
);
579 if (errno
!= EINTR
) {
580 PERROR("sigwaitinfo");
583 } else if (signr
== LTTNG_CONSUMER_SIG_SWITCH
) {
584 metadata_switch_timer(ctx
, info
.si_signo
, &info
, NULL
);
585 } else if (signr
== LTTNG_CONSUMER_SIG_TEARDOWN
) {
587 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
589 DBG("Signal timer metadata thread teardown");
590 } else if (signr
== LTTNG_CONSUMER_SIG_LIVE
) {
591 live_timer(ctx
, info
.si_signo
, &info
, NULL
);
593 ERR("Unexpected signal %d\n", info
.si_signo
);
598 /* Only reached in testpoint error */
600 health_unregister(health_consumerd
);