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>
31 #include "consumer-timer.h"
32 #include "consumer-testpoint.h"
33 #include "ust-consumer/ust-consumer.h"
35 static struct timer_signal_data timer_signal
= {
39 .lock
= PTHREAD_MUTEX_INITIALIZER
,
43 * Set custom signal mask to current thread.
45 static void setmask(sigset_t
*mask
)
49 ret
= sigemptyset(mask
);
51 PERROR("sigemptyset");
53 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_SWITCH
);
55 PERROR("sigaddset switch");
57 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_TEARDOWN
);
59 PERROR("sigaddset teardown");
61 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_LIVE
);
63 PERROR("sigaddset live");
68 * Execute action on a timer switch.
70 * Beware: metadata_switch_timer() should *never* take a mutex also held
71 * while consumer_timer_switch_stop() is called. It would result in
74 static void metadata_switch_timer(struct lttng_consumer_local_data
*ctx
,
75 int sig
, siginfo_t
*si
, void *uc
)
78 struct lttng_consumer_channel
*channel
;
80 channel
= si
->si_value
.sival_ptr
;
83 if (channel
->switch_timer_error
) {
87 DBG("Switch timer for channel %" PRIu64
, channel
->key
);
89 case LTTNG_CONSUMER32_UST
:
90 case LTTNG_CONSUMER64_UST
:
92 * Locks taken by lttng_ustconsumer_request_metadata():
93 * - metadata_socket_lock
94 * - Calling lttng_ustconsumer_recv_metadata():
95 * - channel->metadata_cache->lock
96 * - Calling consumer_metadata_cache_flushed():
97 * - channel->timer_lock
98 * - channel->metadata_cache->lock
100 * Ensure that neither consumer_data.lock nor
101 * channel->lock are taken within this function, since
102 * they are held while consumer_timer_switch_stop() is
105 ret
= lttng_ustconsumer_request_metadata(ctx
, channel
, 1, 1);
107 channel
->switch_timer_error
= 1;
110 case LTTNG_CONSUMER_KERNEL
:
111 case LTTNG_CONSUMER_UNKNOWN
:
117 static int send_empty_index(struct lttng_consumer_stream
*stream
, uint64_t ts
,
121 struct ctf_packet_index index
;
123 memset(&index
, 0, sizeof(index
));
124 index
.stream_id
= htobe64(stream_id
);
125 index
.timestamp_end
= htobe64(ts
);
126 ret
= consumer_stream_write_index(stream
, &index
);
135 static int check_kernel_stream(struct lttng_consumer_stream
*stream
)
137 uint64_t ts
, stream_id
;
141 * While holding the stream mutex, try to take a snapshot, if it
142 * succeeds, it means that data is ready to be sent, just let the data
143 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
144 * means that there is no data to read after the flush, so we can
145 * safely send the empty index.
147 pthread_mutex_lock(&stream
->lock
);
148 ret
= kernctl_get_current_timestamp(stream
->wait_fd
, &ts
);
150 ERR("Failed to get the current timestamp");
153 ret
= kernctl_buffer_flush(stream
->wait_fd
);
155 ERR("Failed to flush kernel stream");
158 ret
= kernctl_snapshot(stream
->wait_fd
);
160 if (errno
!= EAGAIN
&& errno
!= ENODATA
) {
161 PERROR("live timer kernel snapshot");
165 ret
= kernctl_get_stream_id(stream
->wait_fd
, &stream_id
);
167 PERROR("kernctl_get_stream_id");
170 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
171 ret
= send_empty_index(stream
, ts
, stream_id
);
179 pthread_mutex_unlock(&stream
->lock
);
183 static int check_ust_stream(struct lttng_consumer_stream
*stream
)
185 uint64_t ts
, stream_id
;
189 assert(stream
->ustream
);
191 * While holding the stream mutex, try to take a snapshot, if it
192 * succeeds, it means that data is ready to be sent, just let the data
193 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
194 * means that there is no data to read after the flush, so we can
195 * safely send the empty index.
197 pthread_mutex_lock(&stream
->lock
);
198 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
203 ret
= lttng_ustconsumer_get_current_timestamp(stream
, &ts
);
205 ERR("Failed to get the current timestamp");
208 lttng_ustconsumer_flush_buffer(stream
, 1);
209 ret
= lttng_ustconsumer_take_snapshot(stream
);
211 if (ret
!= -EAGAIN
) {
212 ERR("Taking UST snapshot");
216 ret
= lttng_ustconsumer_get_stream_id(stream
, &stream_id
);
218 PERROR("ustctl_get_stream_id");
221 DBG("Stream %" PRIu64
" empty, sending beacon", stream
->key
);
222 ret
= send_empty_index(stream
, ts
, stream_id
);
230 pthread_mutex_unlock(&stream
->lock
);
235 * Execute action on a live timer
237 static void live_timer(struct lttng_consumer_local_data
*ctx
,
238 int sig
, siginfo_t
*si
, void *uc
)
241 struct lttng_consumer_channel
*channel
;
242 struct lttng_consumer_stream
*stream
;
244 struct lttng_ht_iter iter
;
246 channel
= si
->si_value
.sival_ptr
;
249 if (channel
->switch_timer_error
) {
252 ht
= consumer_data
.stream_per_chan_id_ht
;
254 DBG("Live timer for channel %" PRIu64
, channel
->key
);
258 case LTTNG_CONSUMER32_UST
:
259 case LTTNG_CONSUMER64_UST
:
260 cds_lfht_for_each_entry_duplicate(ht
->ht
,
261 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
262 ht
->match_fct
, &channel
->key
, &iter
.iter
,
263 stream
, node_channel_id
.node
) {
264 ret
= check_ust_stream(stream
);
270 case LTTNG_CONSUMER_KERNEL
:
271 cds_lfht_for_each_entry_duplicate(ht
->ht
,
272 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
273 ht
->match_fct
, &channel
->key
, &iter
.iter
,
274 stream
, node_channel_id
.node
) {
275 ret
= check_kernel_stream(stream
);
281 case LTTNG_CONSUMER_UNKNOWN
:
294 void consumer_timer_signal_thread_qs(unsigned int signr
)
296 sigset_t pending_set
;
300 * We need to be the only thread interacting with the thread
301 * that manages signals for teardown synchronization.
303 pthread_mutex_lock(&timer_signal
.lock
);
305 /* Ensure we don't have any signal queued for this channel. */
307 ret
= sigemptyset(&pending_set
);
309 PERROR("sigemptyset");
311 ret
= sigpending(&pending_set
);
313 PERROR("sigpending");
315 if (!sigismember(&pending_set
, LTTNG_CONSUMER_SIG_SWITCH
)) {
322 * From this point, no new signal handler will be fired that would try to
323 * access "chan". However, we still need to wait for any currently
324 * executing handler to complete.
327 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
331 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
334 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
336 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
341 pthread_mutex_unlock(&timer_signal
.lock
);
345 * Set the timer for periodical metadata flush.
347 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
348 unsigned int switch_timer_interval
)
352 struct itimerspec its
;
355 assert(channel
->key
);
357 if (switch_timer_interval
== 0) {
361 sev
.sigev_notify
= SIGEV_SIGNAL
;
362 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_SWITCH
;
363 sev
.sigev_value
.sival_ptr
= channel
;
364 ret
= timer_create(CLOCKID
, &sev
, &channel
->switch_timer
);
366 PERROR("timer_create");
368 channel
->switch_timer_enabled
= 1;
370 its
.it_value
.tv_sec
= switch_timer_interval
/ 1000000;
371 its
.it_value
.tv_nsec
= switch_timer_interval
% 1000000;
372 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
373 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
375 ret
= timer_settime(channel
->switch_timer
, 0, &its
, NULL
);
377 PERROR("timer_settime");
382 * Stop and delete timer.
384 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
390 ret
= timer_delete(channel
->switch_timer
);
392 PERROR("timer_delete");
395 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_SWITCH
);
397 channel
->switch_timer
= 0;
398 channel
->switch_timer_enabled
= 0;
402 * Set the timer for the live mode.
404 void consumer_timer_live_start(struct lttng_consumer_channel
*channel
,
405 int live_timer_interval
)
409 struct itimerspec its
;
412 assert(channel
->key
);
414 if (live_timer_interval
<= 0) {
418 sev
.sigev_notify
= SIGEV_SIGNAL
;
419 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_LIVE
;
420 sev
.sigev_value
.sival_ptr
= channel
;
421 ret
= timer_create(CLOCKID
, &sev
, &channel
->live_timer
);
423 PERROR("timer_create");
425 channel
->live_timer_enabled
= 1;
427 its
.it_value
.tv_sec
= live_timer_interval
/ 1000000;
428 its
.it_value
.tv_nsec
= live_timer_interval
% 1000000;
429 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
430 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
432 ret
= timer_settime(channel
->live_timer
, 0, &its
, NULL
);
434 PERROR("timer_settime");
439 * Stop and delete timer.
441 void consumer_timer_live_stop(struct lttng_consumer_channel
*channel
)
447 ret
= timer_delete(channel
->live_timer
);
449 PERROR("timer_delete");
452 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_LIVE
);
454 channel
->live_timer
= 0;
455 channel
->live_timer_enabled
= 0;
459 * Block the RT signals for the entire process. It must be called from the
460 * consumer main before creating the threads
462 void consumer_signal_init(void)
467 /* Block signal for entire process, so only our thread processes it. */
469 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
472 PERROR("pthread_sigmask");
477 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
478 * LTTNG_CONSUMER_SIG_TEARDOWN and LTTNG_CONSUMER_SIG_LIVE.
480 void *consumer_timer_thread(void *data
)
485 struct lttng_consumer_local_data
*ctx
= data
;
487 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
489 if (testpoint(consumerd_thread_metadata_timer
)) {
490 goto error_testpoint
;
493 health_code_update();
495 /* Only self thread will receive signal mask. */
497 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
500 health_code_update();
503 signr
= sigwaitinfo(&mask
, &info
);
506 if (errno
!= EINTR
) {
507 PERROR("sigwaitinfo");
510 } else if (signr
== LTTNG_CONSUMER_SIG_SWITCH
) {
511 metadata_switch_timer(ctx
, info
.si_signo
, &info
, NULL
);
512 } else if (signr
== LTTNG_CONSUMER_SIG_TEARDOWN
) {
514 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
516 DBG("Signal timer metadata thread teardown");
517 } else if (signr
== LTTNG_CONSUMER_SIG_LIVE
) {
518 live_timer(ctx
, info
.si_signo
, &info
, NULL
);
520 ERR("Unexpected signal %d\n", info
.si_signo
);
525 /* Only reached in testpoint error */
527 health_unregister(health_consumerd
);