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-stream.h>
30 #include <lttng/ust-ctl.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 static int check_kernel_stream(struct lttng_consumer_stream
*stream
)
138 uint64_t ts
, stream_id
;
142 * While holding the stream mutex, try to take a snapshot, if it
143 * succeeds, it means that data is ready to be sent, just let the data
144 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
145 * means that there is no data to read after the flush, so we can
146 * safely send the empty index.
148 pthread_mutex_lock(&stream
->lock
);
149 ret
= kernctl_get_current_timestamp(stream
->wait_fd
, &ts
);
151 ERR("Failed to get the current timestamp");
154 ret
= kernctl_buffer_flush(stream
->wait_fd
);
156 ERR("Failed to flush kernel stream");
159 ret
= kernctl_snapshot(stream
->wait_fd
);
161 if (errno
!= EAGAIN
&& errno
!= ENODATA
) {
162 PERROR("live timer kernel snapshot");
166 ret
= kernctl_get_stream_id(stream
->wait_fd
, &stream_id
);
168 PERROR("kernctl_get_stream_id");
171 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
172 ret
= send_empty_index(stream
, ts
, stream_id
);
180 pthread_mutex_unlock(&stream
->lock
);
184 static int check_ust_stream(struct lttng_consumer_stream
*stream
)
186 uint64_t ts
, stream_id
;
190 assert(stream
->ustream
);
192 * While holding the stream mutex, try to take a snapshot, if it
193 * succeeds, it means that data is ready to be sent, just let the data
194 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
195 * means that there is no data to read after the flush, so we can
196 * safely send the empty index.
198 pthread_mutex_lock(&stream
->lock
);
199 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
204 ret
= lttng_ustconsumer_get_current_timestamp(stream
, &ts
);
206 ERR("Failed to get the current timestamp");
209 lttng_ustconsumer_flush_buffer(stream
, 1);
210 ret
= lttng_ustconsumer_take_snapshot(stream
);
212 if (ret
!= -EAGAIN
) {
213 ERR("Taking UST snapshot");
217 ret
= ustctl_get_stream_id(stream
->ustream
, &stream_id
);
219 PERROR("ustctl_get_stream_id");
222 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
223 ret
= send_empty_index(stream
, ts
, stream_id
);
231 pthread_mutex_unlock(&stream
->lock
);
236 * Execute action on a live timer
238 static void live_timer(struct lttng_consumer_local_data
*ctx
,
239 int sig
, siginfo_t
*si
, void *uc
)
242 struct lttng_consumer_channel
*channel
;
243 struct lttng_consumer_stream
*stream
;
245 struct lttng_ht_iter iter
;
247 channel
= si
->si_value
.sival_ptr
;
250 if (channel
->switch_timer_error
) {
253 ht
= consumer_data
.stream_per_chan_id_ht
;
255 DBG("Live timer for channel %" PRIu64
, channel
->key
);
259 case LTTNG_CONSUMER32_UST
:
260 case LTTNG_CONSUMER64_UST
:
261 cds_lfht_for_each_entry_duplicate(ht
->ht
,
262 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
263 ht
->match_fct
, &channel
->key
, &iter
.iter
,
264 stream
, node_channel_id
.node
) {
265 ret
= check_ust_stream(stream
);
271 case LTTNG_CONSUMER_KERNEL
:
272 cds_lfht_for_each_entry_duplicate(ht
->ht
,
273 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
274 ht
->match_fct
, &channel
->key
, &iter
.iter
,
275 stream
, node_channel_id
.node
) {
276 ret
= check_kernel_stream(stream
);
282 case LTTNG_CONSUMER_UNKNOWN
:
295 void consumer_timer_signal_thread_qs(unsigned int signr
)
297 sigset_t pending_set
;
301 * We need to be the only thread interacting with the thread
302 * that manages signals for teardown synchronization.
304 pthread_mutex_lock(&timer_signal
.lock
);
306 /* Ensure we don't have any signal queued for this channel. */
308 ret
= sigemptyset(&pending_set
);
310 PERROR("sigemptyset");
312 ret
= sigpending(&pending_set
);
314 PERROR("sigpending");
316 if (!sigismember(&pending_set
, LTTNG_CONSUMER_SIG_SWITCH
)) {
323 * From this point, no new signal handler will be fired that would try to
324 * access "chan". However, we still need to wait for any currently
325 * executing handler to complete.
328 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
332 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
335 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
337 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
342 pthread_mutex_unlock(&timer_signal
.lock
);
346 * Set the timer for periodical metadata flush.
348 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
349 unsigned int switch_timer_interval
)
353 struct itimerspec its
;
356 assert(channel
->key
);
358 if (switch_timer_interval
== 0) {
362 sev
.sigev_notify
= SIGEV_SIGNAL
;
363 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_SWITCH
;
364 sev
.sigev_value
.sival_ptr
= channel
;
365 ret
= timer_create(CLOCKID
, &sev
, &channel
->switch_timer
);
367 PERROR("timer_create");
369 channel
->switch_timer_enabled
= 1;
371 its
.it_value
.tv_sec
= switch_timer_interval
/ 1000000;
372 its
.it_value
.tv_nsec
= switch_timer_interval
% 1000000;
373 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
374 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
376 ret
= timer_settime(channel
->switch_timer
, 0, &its
, NULL
);
378 PERROR("timer_settime");
383 * Stop and delete timer.
385 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
391 ret
= timer_delete(channel
->switch_timer
);
393 PERROR("timer_delete");
396 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_SWITCH
);
398 channel
->switch_timer
= 0;
399 channel
->switch_timer_enabled
= 0;
403 * Set the timer for the live mode.
405 void consumer_timer_live_start(struct lttng_consumer_channel
*channel
,
406 int live_timer_interval
)
410 struct itimerspec its
;
413 assert(channel
->key
);
415 if (live_timer_interval
<= 0) {
419 sev
.sigev_notify
= SIGEV_SIGNAL
;
420 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_LIVE
;
421 sev
.sigev_value
.sival_ptr
= channel
;
422 ret
= timer_create(CLOCKID
, &sev
, &channel
->live_timer
);
424 PERROR("timer_create");
426 channel
->live_timer_enabled
= 1;
428 its
.it_value
.tv_sec
= live_timer_interval
/ 1000000;
429 its
.it_value
.tv_nsec
= live_timer_interval
% 1000000;
430 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
431 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
433 ret
= timer_settime(channel
->live_timer
, 0, &its
, NULL
);
435 PERROR("timer_settime");
440 * Stop and delete timer.
442 void consumer_timer_live_stop(struct lttng_consumer_channel
*channel
)
448 ret
= timer_delete(channel
->live_timer
);
450 PERROR("timer_delete");
453 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_LIVE
);
455 channel
->live_timer
= 0;
456 channel
->live_timer_enabled
= 0;
460 * Block the RT signals for the entire process. It must be called from the
461 * consumer main before creating the threads
463 void consumer_signal_init(void)
468 /* Block signal for entire process, so only our thread processes it. */
470 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
473 PERROR("pthread_sigmask");
478 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
479 * LTTNG_CONSUMER_SIG_TEARDOWN and LTTNG_CONSUMER_SIG_LIVE.
481 void *consumer_timer_thread(void *data
)
486 struct lttng_consumer_local_data
*ctx
= data
;
488 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
490 if (testpoint(consumerd_thread_metadata_timer
)) {
491 goto error_testpoint
;
494 health_code_update();
496 /* Only self thread will receive signal mask. */
498 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
501 health_code_update();
504 signr
= sigwaitinfo(&mask
, &info
);
507 if (errno
!= EINTR
) {
508 PERROR("sigwaitinfo");
511 } else if (signr
== LTTNG_CONSUMER_SIG_SWITCH
) {
512 metadata_switch_timer(ctx
, info
.si_signo
, &info
, NULL
);
513 } else if (signr
== LTTNG_CONSUMER_SIG_TEARDOWN
) {
515 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
517 DBG("Signal timer metadata thread teardown");
518 } else if (signr
== LTTNG_CONSUMER_SIG_LIVE
) {
519 live_timer(ctx
, info
.si_signo
, &info
, NULL
);
521 ERR("Unexpected signal %d\n", info
.si_signo
);
526 /* Only reached in testpoint error */
528 health_unregister(health_consumerd
);