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");
67 * Execute action on a timer switch.
69 * Beware: metadata_switch_timer() should *never* take a mutex also held
70 * while consumer_timer_switch_stop() is called. It would result in
73 static void metadata_switch_timer(struct lttng_consumer_local_data
*ctx
,
74 int sig
, siginfo_t
*si
, void *uc
)
77 struct lttng_consumer_channel
*channel
;
79 channel
= si
->si_value
.sival_ptr
;
82 if (channel
->switch_timer_error
) {
86 DBG("Switch timer for channel %" PRIu64
, channel
->key
);
88 case LTTNG_CONSUMER32_UST
:
89 case LTTNG_CONSUMER64_UST
:
91 * Locks taken by lttng_ustconsumer_request_metadata():
92 * - metadata_socket_lock
93 * - Calling lttng_ustconsumer_recv_metadata():
94 * - channel->metadata_cache->lock
95 * - Calling consumer_metadata_cache_flushed():
96 * - channel->timer_lock
97 * - channel->metadata_cache->lock
99 * Ensure that neither consumer_data.lock nor
100 * channel->lock are taken within this function, since
101 * they are held while consumer_timer_switch_stop() is
104 ret
= lttng_ustconsumer_request_metadata(ctx
, channel
, 1, 1);
106 channel
->switch_timer_error
= 1;
109 case LTTNG_CONSUMER_KERNEL
:
110 case LTTNG_CONSUMER_UNKNOWN
:
116 static int send_empty_index(struct lttng_consumer_stream
*stream
, uint64_t ts
,
120 struct ctf_packet_index index
;
122 memset(&index
, 0, sizeof(index
));
123 index
.stream_id
= htobe64(stream_id
);
124 index
.timestamp_end
= htobe64(ts
);
125 ret
= consumer_stream_write_index(stream
, &index
);
134 int consumer_flush_kernel_index(struct lttng_consumer_stream
*stream
)
136 uint64_t ts
, stream_id
;
139 ret
= kernctl_get_current_timestamp(stream
->wait_fd
, &ts
);
141 ERR("Failed to get the current timestamp");
144 ret
= kernctl_buffer_flush(stream
->wait_fd
);
146 ERR("Failed to flush kernel stream");
149 ret
= kernctl_snapshot(stream
->wait_fd
);
151 if (errno
!= EAGAIN
&& errno
!= ENODATA
) {
152 PERROR("live timer kernel snapshot");
156 ret
= kernctl_get_stream_id(stream
->wait_fd
, &stream_id
);
158 PERROR("kernctl_get_stream_id");
161 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
162 ret
= send_empty_index(stream
, ts
, stream_id
);
172 static int check_kernel_stream(struct lttng_consumer_stream
*stream
)
177 * While holding the stream mutex, try to take a snapshot, if it
178 * succeeds, it means that data is ready to be sent, just let the data
179 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
180 * means that there is no data to read after the flush, so we can
181 * safely send the empty index.
183 * Doing a trylock and checking if waiting on metadata if
184 * trylock fails. Bail out of the stream is indeed waiting for
185 * metadata to be pushed. Busy wait on trylock otherwise.
188 ret
= pthread_mutex_trylock(&stream
->lock
);
191 break; /* We have the lock. */
193 pthread_mutex_lock(&stream
->metadata_timer_lock
);
194 if (stream
->waiting_on_metadata
) {
196 stream
->missed_metadata_flush
= true;
197 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
198 goto end
; /* Bail out. */
200 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
205 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
211 ret
= consumer_flush_kernel_index(stream
);
212 pthread_mutex_unlock(&stream
->lock
);
217 int consumer_flush_ust_index(struct lttng_consumer_stream
*stream
)
219 uint64_t ts
, stream_id
;
222 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
227 ret
= lttng_ustconsumer_get_current_timestamp(stream
, &ts
);
229 ERR("Failed to get the current timestamp");
232 lttng_ustconsumer_flush_buffer(stream
, 1);
233 ret
= lttng_ustconsumer_take_snapshot(stream
);
235 if (ret
!= -EAGAIN
) {
236 ERR("Taking UST snapshot");
240 ret
= lttng_ustconsumer_get_stream_id(stream
, &stream_id
);
242 PERROR("ustctl_get_stream_id");
245 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
246 ret
= send_empty_index(stream
, ts
, stream_id
);
256 static int check_ust_stream(struct lttng_consumer_stream
*stream
)
261 assert(stream
->ustream
);
263 * While holding the stream mutex, try to take a snapshot, if it
264 * succeeds, it means that data is ready to be sent, just let the data
265 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
266 * means that there is no data to read after the flush, so we can
267 * safely send the empty index.
269 * Doing a trylock and checking if waiting on metadata if
270 * trylock fails. Bail out of the stream is indeed waiting for
271 * metadata to be pushed. Busy wait on trylock otherwise.
274 ret
= pthread_mutex_trylock(&stream
->lock
);
277 break; /* We have the lock. */
279 pthread_mutex_lock(&stream
->metadata_timer_lock
);
280 if (stream
->waiting_on_metadata
) {
282 stream
->missed_metadata_flush
= true;
283 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
284 goto end
; /* Bail out. */
286 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
291 ERR("Unexpected pthread_mutex_trylock error %d", ret
);
297 ret
= consumer_flush_ust_index(stream
);
298 pthread_mutex_unlock(&stream
->lock
);
304 * Execute action on a live timer
306 static void live_timer(struct lttng_consumer_local_data
*ctx
,
307 int sig
, siginfo_t
*si
, void *uc
)
310 struct lttng_consumer_channel
*channel
;
311 struct lttng_consumer_stream
*stream
;
313 struct lttng_ht_iter iter
;
315 channel
= si
->si_value
.sival_ptr
;
318 if (channel
->switch_timer_error
) {
321 ht
= consumer_data
.stream_per_chan_id_ht
;
323 DBG("Live timer for channel %" PRIu64
, channel
->key
);
327 case LTTNG_CONSUMER32_UST
:
328 case LTTNG_CONSUMER64_UST
:
329 cds_lfht_for_each_entry_duplicate(ht
->ht
,
330 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
331 ht
->match_fct
, &channel
->key
, &iter
.iter
,
332 stream
, node_channel_id
.node
) {
333 ret
= check_ust_stream(stream
);
339 case LTTNG_CONSUMER_KERNEL
:
340 cds_lfht_for_each_entry_duplicate(ht
->ht
,
341 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
342 ht
->match_fct
, &channel
->key
, &iter
.iter
,
343 stream
, node_channel_id
.node
) {
344 ret
= check_kernel_stream(stream
);
350 case LTTNG_CONSUMER_UNKNOWN
:
363 void consumer_timer_signal_thread_qs(unsigned int signr
)
365 sigset_t pending_set
;
369 * We need to be the only thread interacting with the thread
370 * that manages signals for teardown synchronization.
372 pthread_mutex_lock(&timer_signal
.lock
);
374 /* Ensure we don't have any signal queued for this channel. */
376 ret
= sigemptyset(&pending_set
);
378 PERROR("sigemptyset");
380 ret
= sigpending(&pending_set
);
382 PERROR("sigpending");
384 if (!sigismember(&pending_set
, LTTNG_CONSUMER_SIG_SWITCH
)) {
391 * From this point, no new signal handler will be fired that would try to
392 * access "chan". However, we still need to wait for any currently
393 * executing handler to complete.
396 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
400 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
403 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
405 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
410 pthread_mutex_unlock(&timer_signal
.lock
);
414 * Set the timer for periodical metadata flush.
416 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
417 unsigned int switch_timer_interval
)
421 struct itimerspec its
;
424 assert(channel
->key
);
426 if (switch_timer_interval
== 0) {
430 sev
.sigev_notify
= SIGEV_SIGNAL
;
431 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_SWITCH
;
432 sev
.sigev_value
.sival_ptr
= channel
;
433 ret
= timer_create(CLOCKID
, &sev
, &channel
->switch_timer
);
435 PERROR("timer_create");
437 channel
->switch_timer_enabled
= 1;
439 its
.it_value
.tv_sec
= switch_timer_interval
/ 1000000;
440 its
.it_value
.tv_nsec
= switch_timer_interval
% 1000000;
441 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
442 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
444 ret
= timer_settime(channel
->switch_timer
, 0, &its
, NULL
);
446 PERROR("timer_settime");
451 * Stop and delete timer.
453 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
459 ret
= timer_delete(channel
->switch_timer
);
461 PERROR("timer_delete");
464 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_SWITCH
);
466 channel
->switch_timer
= 0;
467 channel
->switch_timer_enabled
= 0;
471 * Set the timer for the live mode.
473 void consumer_timer_live_start(struct lttng_consumer_channel
*channel
,
474 int live_timer_interval
)
478 struct itimerspec its
;
481 assert(channel
->key
);
483 if (live_timer_interval
<= 0) {
487 sev
.sigev_notify
= SIGEV_SIGNAL
;
488 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_LIVE
;
489 sev
.sigev_value
.sival_ptr
= channel
;
490 ret
= timer_create(CLOCKID
, &sev
, &channel
->live_timer
);
492 PERROR("timer_create");
494 channel
->live_timer_enabled
= 1;
496 its
.it_value
.tv_sec
= live_timer_interval
/ 1000000;
497 its
.it_value
.tv_nsec
= live_timer_interval
% 1000000;
498 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
499 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
501 ret
= timer_settime(channel
->live_timer
, 0, &its
, NULL
);
503 PERROR("timer_settime");
508 * Stop and delete timer.
510 void consumer_timer_live_stop(struct lttng_consumer_channel
*channel
)
516 ret
= timer_delete(channel
->live_timer
);
518 PERROR("timer_delete");
521 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_LIVE
);
523 channel
->live_timer
= 0;
524 channel
->live_timer_enabled
= 0;
528 * Block the RT signals for the entire process. It must be called from the
529 * consumer main before creating the threads
531 int consumer_signal_init(void)
536 /* Block signal for entire process, so only our thread processes it. */
538 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
541 PERROR("pthread_sigmask");
548 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
549 * LTTNG_CONSUMER_SIG_TEARDOWN and LTTNG_CONSUMER_SIG_LIVE.
551 void *consumer_timer_thread(void *data
)
556 struct lttng_consumer_local_data
*ctx
= data
;
558 rcu_register_thread();
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
);
602 rcu_unregister_thread();