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/kernel-ctl/kernel-ctl.h>
27 #include <common/kernel-consumer/kernel-consumer.h>
28 #include <common/consumer-stream.h>
30 #include "consumer-timer.h"
31 #include "ust-consumer/ust-consumer.h"
33 static struct timer_signal_data timer_signal
= {
37 .lock
= PTHREAD_MUTEX_INITIALIZER
,
41 * Set custom signal mask to current thread.
43 static void setmask(sigset_t
*mask
)
47 ret
= sigemptyset(mask
);
49 PERROR("sigemptyset");
51 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_SWITCH
);
53 PERROR("sigaddset switch");
55 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_TEARDOWN
);
57 PERROR("sigaddset teardown");
59 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_LIVE
);
61 PERROR("sigaddset live");
66 * Execute action on a timer switch.
68 * Beware: metadata_switch_timer() should *never* take a mutex also held
69 * while consumer_timer_switch_stop() is called. It would result in
72 static void metadata_switch_timer(struct lttng_consumer_local_data
*ctx
,
73 int sig
, siginfo_t
*si
, void *uc
)
76 struct lttng_consumer_channel
*channel
;
78 channel
= si
->si_value
.sival_ptr
;
81 if (channel
->switch_timer_error
) {
85 DBG("Switch timer for channel %" PRIu64
, channel
->key
);
87 case LTTNG_CONSUMER32_UST
:
88 case LTTNG_CONSUMER64_UST
:
90 * Locks taken by lttng_ustconsumer_request_metadata():
91 * - metadata_socket_lock
92 * - Calling lttng_ustconsumer_recv_metadata():
93 * - channel->metadata_cache->lock
94 * - Calling consumer_metadata_cache_flushed():
95 * - channel->timer_lock
96 * - channel->metadata_cache->lock
98 * Ensure that neither consumer_data.lock nor
99 * channel->lock are taken within this function, since
100 * they are held while consumer_timer_switch_stop() is
103 ret
= lttng_ustconsumer_request_metadata(ctx
, channel
, 1, 1);
105 channel
->switch_timer_error
= 1;
108 case LTTNG_CONSUMER_KERNEL
:
109 case LTTNG_CONSUMER_UNKNOWN
:
115 static int send_empty_index(struct lttng_consumer_stream
*stream
, uint64_t ts
)
118 struct lttng_packet_index index
;
120 memset(&index
, 0, sizeof(index
));
121 index
.timestamp_end
= htobe64(ts
);
122 ret
= consumer_stream_write_index(stream
, &index
);
131 static int check_kernel_stream(struct lttng_consumer_stream
*stream
)
137 * While holding the stream mutex, try to take a snapshot, if it
138 * succeeds, it means that data is ready to be sent, just let the data
139 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
140 * means that there is no data to read after the flush, so we can
141 * safely send the empty index.
143 pthread_mutex_lock(&stream
->lock
);
144 ret
= kernctl_get_current_timestamp(stream
->wait_fd
, &ts
);
146 ERR("Failed to get the current timestamp");
149 ret
= kernctl_buffer_flush(stream
->wait_fd
);
151 ERR("Failed to flush kernel stream");
154 ret
= kernctl_snapshot(stream
->wait_fd
);
156 if (errno
!= EAGAIN
) {
157 ERR("Taking kernel snapshot");
161 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
162 ret
= send_empty_index(stream
, ts
);
170 pthread_mutex_unlock(&stream
->lock
);
174 static int check_ust_stream(struct lttng_consumer_stream
*stream
)
180 assert(stream
->ustream
);
182 * While holding the stream mutex, try to take a snapshot, if it
183 * succeeds, it means that data is ready to be sent, just let the data
184 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
185 * means that there is no data to read after the flush, so we can
186 * safely send the empty index.
188 pthread_mutex_lock(&stream
->lock
);
189 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
194 ret
= lttng_ustconsumer_get_current_timestamp(stream
, &ts
);
196 ERR("Failed to get the current timestamp");
199 lttng_ustconsumer_flush_buffer(stream
, 1);
200 ret
= lttng_ustconsumer_take_snapshot(stream
);
202 if (ret
!= -EAGAIN
) {
203 ERR("Taking UST snapshot");
207 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
208 ret
= send_empty_index(stream
, ts
);
216 pthread_mutex_unlock(&stream
->lock
);
221 * Execute action on a live timer
223 static void live_timer(struct lttng_consumer_local_data
*ctx
,
224 int sig
, siginfo_t
*si
, void *uc
)
227 struct lttng_consumer_channel
*channel
;
228 struct lttng_consumer_stream
*stream
;
230 struct lttng_ht_iter iter
;
232 channel
= si
->si_value
.sival_ptr
;
235 if (channel
->switch_timer_error
) {
238 ht
= consumer_data
.stream_per_chan_id_ht
;
240 DBG("Live timer for channel %" PRIu64
, channel
->key
);
244 case LTTNG_CONSUMER32_UST
:
245 case LTTNG_CONSUMER64_UST
:
246 cds_lfht_for_each_entry_duplicate(ht
->ht
,
247 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
248 ht
->match_fct
, &channel
->key
, &iter
.iter
,
249 stream
, node_channel_id
.node
) {
250 ret
= check_ust_stream(stream
);
256 case LTTNG_CONSUMER_KERNEL
:
257 cds_lfht_for_each_entry_duplicate(ht
->ht
,
258 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
259 ht
->match_fct
, &channel
->key
, &iter
.iter
,
260 stream
, node_channel_id
.node
) {
261 ret
= check_kernel_stream(stream
);
267 case LTTNG_CONSUMER_UNKNOWN
:
280 void consumer_timer_signal_thread_qs(unsigned int signr
)
282 sigset_t pending_set
;
286 * We need to be the only thread interacting with the thread
287 * that manages signals for teardown synchronization.
289 pthread_mutex_lock(&timer_signal
.lock
);
291 /* Ensure we don't have any signal queued for this channel. */
293 ret
= sigemptyset(&pending_set
);
295 PERROR("sigemptyset");
297 ret
= sigpending(&pending_set
);
299 PERROR("sigpending");
301 if (!sigismember(&pending_set
, LTTNG_CONSUMER_SIG_SWITCH
)) {
308 * From this point, no new signal handler will be fired that would try to
309 * access "chan". However, we still need to wait for any currently
310 * executing handler to complete.
313 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
317 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
320 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
322 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
327 pthread_mutex_unlock(&timer_signal
.lock
);
331 * Set the timer for periodical metadata flush.
333 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
334 unsigned int switch_timer_interval
)
338 struct itimerspec its
;
341 assert(channel
->key
);
343 if (switch_timer_interval
== 0) {
347 sev
.sigev_notify
= SIGEV_SIGNAL
;
348 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_SWITCH
;
349 sev
.sigev_value
.sival_ptr
= channel
;
350 ret
= timer_create(CLOCKID
, &sev
, &channel
->switch_timer
);
352 PERROR("timer_create");
354 channel
->switch_timer_enabled
= 1;
356 its
.it_value
.tv_sec
= switch_timer_interval
/ 1000000;
357 its
.it_value
.tv_nsec
= switch_timer_interval
% 1000000;
358 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
359 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
361 ret
= timer_settime(channel
->switch_timer
, 0, &its
, NULL
);
363 PERROR("timer_settime");
368 * Stop and delete timer.
370 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
376 ret
= timer_delete(channel
->switch_timer
);
378 PERROR("timer_delete");
381 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_SWITCH
);
383 channel
->switch_timer
= 0;
384 channel
->switch_timer_enabled
= 0;
388 * Set the timer for the live mode.
390 void consumer_timer_live_start(struct lttng_consumer_channel
*channel
,
391 int live_timer_interval
)
395 struct itimerspec its
;
398 assert(channel
->key
);
400 if (live_timer_interval
<= 0) {
404 sev
.sigev_notify
= SIGEV_SIGNAL
;
405 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_LIVE
;
406 sev
.sigev_value
.sival_ptr
= channel
;
407 ret
= timer_create(CLOCKID
, &sev
, &channel
->live_timer
);
409 PERROR("timer_create");
411 channel
->live_timer_enabled
= 1;
413 its
.it_value
.tv_sec
= live_timer_interval
/ 1000000;
414 its
.it_value
.tv_nsec
= live_timer_interval
% 1000000;
415 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
416 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
418 ret
= timer_settime(channel
->live_timer
, 0, &its
, NULL
);
420 PERROR("timer_settime");
425 * Stop and delete timer.
427 void consumer_timer_live_stop(struct lttng_consumer_channel
*channel
)
433 ret
= timer_delete(channel
->live_timer
);
435 PERROR("timer_delete");
438 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_LIVE
);
440 channel
->live_timer
= 0;
441 channel
->live_timer_enabled
= 0;
445 * Block the RT signals for the entire process. It must be called from the
446 * consumer main before creating the threads
448 void consumer_signal_init(void)
453 /* Block signal for entire process, so only our thread processes it. */
455 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
458 PERROR("pthread_sigmask");
463 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
464 * LTTNG_CONSUMER_SIG_TEARDOWN and LTTNG_CONSUMER_SIG_LIVE.
466 void *consumer_timer_thread(void *data
)
471 struct lttng_consumer_local_data
*ctx
= data
;
473 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
475 health_code_update();
477 /* Only self thread will receive signal mask. */
479 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
482 health_code_update();
485 signr
= sigwaitinfo(&mask
, &info
);
488 if (errno
!= EINTR
) {
489 PERROR("sigwaitinfo");
492 } else if (signr
== LTTNG_CONSUMER_SIG_SWITCH
) {
493 metadata_switch_timer(ctx
, info
.si_signo
, &info
, NULL
);
494 } else if (signr
== LTTNG_CONSUMER_SIG_TEARDOWN
) {
496 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
498 DBG("Signal timer metadata thread teardown");
499 } else if (signr
== LTTNG_CONSUMER_SIG_LIVE
) {
500 live_timer(ctx
, info
.si_signo
, &info
, NULL
);
502 ERR("Unexpected signal %d\n", info
.si_signo
);
506 /* Currently never reached */
507 health_unregister(health_consumerd
);