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 <common/common.h>
26 #include "consumer-timer.h"
27 #include "ust-consumer/ust-consumer.h"
29 static struct timer_signal_data timer_signal
= {
33 .lock
= PTHREAD_MUTEX_INITIALIZER
,
37 * Set custom signal mask to current thread.
39 static void setmask(sigset_t
*mask
)
43 ret
= sigemptyset(mask
);
45 PERROR("sigemptyset");
47 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_SWITCH
);
51 ret
= sigaddset(mask
, LTTNG_CONSUMER_SIG_TEARDOWN
);
58 * Execute action on a timer switch.
60 static void metadata_switch_timer(struct lttng_consumer_local_data
*ctx
,
61 int sig
, siginfo_t
*si
, void *uc
)
64 struct lttng_consumer_channel
*channel
;
66 channel
= si
->si_value
.sival_ptr
;
69 if (channel
->switch_timer_error
) {
73 DBG("Switch timer for channel %" PRIu64
, channel
->key
);
75 case LTTNG_CONSUMER32_UST
:
76 case LTTNG_CONSUMER64_UST
:
77 ret
= lttng_ustconsumer_request_metadata(ctx
, channel
);
79 channel
->switch_timer_error
= 1;
82 case LTTNG_CONSUMER_KERNEL
:
83 case LTTNG_CONSUMER_UNKNOWN
:
90 void consumer_timer_signal_thread_qs(unsigned int signr
)
96 * We need to be the only thread interacting with the thread
97 * that manages signals for teardown synchronization.
99 pthread_mutex_lock(&timer_signal
.lock
);
101 /* Ensure we don't have any signal queued for this channel. */
103 ret
= sigemptyset(&pending_set
);
105 PERROR("sigemptyset");
107 ret
= sigpending(&pending_set
);
109 PERROR("sigpending");
111 if (!sigismember(&pending_set
, LTTNG_CONSUMER_SIG_SWITCH
)) {
118 * From this point, no new signal handler will be fired that would try to
119 * access "chan". However, we still need to wait for any currently
120 * executing handler to complete.
123 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
127 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
130 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN
);
132 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
137 pthread_mutex_unlock(&timer_signal
.lock
);
141 * Set the timer for periodical metadata flush.
143 void consumer_timer_switch_start(struct lttng_consumer_channel
*channel
,
144 unsigned int switch_timer_interval
)
148 struct itimerspec its
;
151 assert(channel
->key
);
153 if (switch_timer_interval
== 0) {
157 sev
.sigev_notify
= SIGEV_SIGNAL
;
158 sev
.sigev_signo
= LTTNG_CONSUMER_SIG_SWITCH
;
159 sev
.sigev_value
.sival_ptr
= channel
;
160 ret
= timer_create(CLOCKID
, &sev
, &channel
->switch_timer
);
162 PERROR("timer_create");
164 channel
->switch_timer_enabled
= 1;
166 its
.it_value
.tv_sec
= switch_timer_interval
/ 1000000;
167 its
.it_value
.tv_nsec
= switch_timer_interval
% 1000000;
168 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
169 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
171 ret
= timer_settime(channel
->switch_timer
, 0, &its
, NULL
);
173 PERROR("timer_settime");
178 * Stop and delete timer.
180 void consumer_timer_switch_stop(struct lttng_consumer_channel
*channel
)
186 ret
= timer_delete(channel
->switch_timer
);
188 PERROR("timer_delete");
191 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_SWITCH
);
193 channel
->switch_timer
= 0;
194 channel
->switch_timer_enabled
= 0;
198 * Block the RT signals for the entire process. It must be called from the
199 * consumer main before creating the threads
201 void consumer_signal_init(void)
206 /* Block signal for entire process, so only our thread processes it. */
208 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
211 PERROR("pthread_sigmask");
216 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH and
217 * LTTNG_CONSUMER_SIG_TEARDOWN that are emitted by the periodic timer to check
218 * if new metadata is available.
220 void *consumer_timer_metadata_thread(void *data
)
225 struct lttng_consumer_local_data
*ctx
= data
;
227 /* Only self thread will receive signal mask. */
229 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
232 signr
= sigwaitinfo(&mask
, &info
);
234 if (errno
!= EINTR
) {
235 PERROR("sigwaitinfo");
238 } else if (signr
== LTTNG_CONSUMER_SIG_SWITCH
) {
239 metadata_switch_timer(ctx
, info
.si_signo
, &info
, NULL
);
240 } else if (signr
== LTTNG_CONSUMER_SIG_TEARDOWN
) {
242 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
244 DBG("Signal timer metadata thread teardown");
246 ERR("Unexpected signal %d\n", info
.si_signo
);