2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@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.
25 #include "health-sessiond.h"
26 #include "rotation-thread.h"
28 #define LTTNG_SESSIOND_SIG_QS SIGRTMIN + 10
29 #define LTTNG_SESSIOND_SIG_EXIT SIGRTMIN + 11
30 #define LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK SIGRTMIN + 12
31 #define LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION SIGRTMIN + 13
33 #define UINT_TO_PTR(value) \
35 assert(value <= UINTPTR_MAX); \
36 (void *) (uintptr_t) value; \
38 #define PTR_TO_UINT(ptr) ((uintptr_t) ptr)
41 * Handle timer teardown race wrt memory free of private data by sessiond
42 * signals are handled by a single thread, which permits a synchronization
43 * point between handling of each signal. Internal lock ensures mutual
47 struct timer_signal_data
{
48 /* Thread managing signals. */
55 .lock
= PTHREAD_MUTEX_INITIALIZER
,
59 * Set custom signal mask to current thread.
62 void setmask(sigset_t
*mask
)
66 ret
= sigemptyset(mask
);
68 PERROR("sigemptyset");
70 ret
= sigaddset(mask
, LTTNG_SESSIOND_SIG_QS
);
72 PERROR("sigaddset teardown");
74 ret
= sigaddset(mask
, LTTNG_SESSIOND_SIG_EXIT
);
76 PERROR("sigaddset exit");
78 ret
= sigaddset(mask
, LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK
);
80 PERROR("sigaddset pending rotation check");
82 ret
= sigaddset(mask
, LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION
);
84 PERROR("sigaddset scheduled rotation");
89 * This is the same function as timer_signal_thread_qs, when it
90 * returns, it means that no timer signr is currently pending or being handled
91 * by the timer thread. This cannot be called from the timer thread.
94 void timer_signal_thread_qs(unsigned int signr
)
100 * We need to be the only thread interacting with the thread
101 * that manages signals for teardown synchronization.
103 pthread_mutex_lock(&timer_signal
.lock
);
105 /* Ensure we don't have any signal queued for this session. */
107 ret
= sigemptyset(&pending_set
);
109 PERROR("sigemptyset");
111 ret
= sigpending(&pending_set
);
113 PERROR("sigpending");
115 if (!sigismember(&pending_set
, signr
)) {
122 * From this point, no new signal handler will be fired that would try to
123 * access "session". However, we still need to wait for any currently
124 * executing handler to complete.
127 CMM_STORE_SHARED(timer_signal
.qs_done
, 0);
131 * Kill with LTTNG_SESSIOND_SIG_QS, so signal management thread
134 kill(getpid(), LTTNG_SESSIOND_SIG_QS
);
136 while (!CMM_LOAD_SHARED(timer_signal
.qs_done
)) {
141 pthread_mutex_unlock(&timer_signal
.lock
);
145 * Start a timer on a session that will fire at a given interval
146 * (timer_interval_us) and fire a given signal (signal).
148 * Returns a negative value on error, 0 if a timer was created, and
149 * a positive value if no timer was created (not an error).
152 int timer_start(timer_t
*timer_id
, struct ltt_session
*session
,
153 unsigned int timer_interval_us
, int signal
, bool one_shot
)
155 int ret
= 0, delete_ret
;
157 struct itimerspec its
;
159 sev
.sigev_notify
= SIGEV_SIGNAL
;
160 sev
.sigev_signo
= signal
;
161 sev
.sigev_value
.sival_ptr
= session
;
162 ret
= timer_create(CLOCK_MONOTONIC
, &sev
, timer_id
);
164 PERROR("timer_create");
168 its
.it_value
.tv_sec
= timer_interval_us
/ 1000000;
169 its
.it_value
.tv_nsec
= (timer_interval_us
% 1000000) * 1000;
171 its
.it_interval
.tv_sec
= 0;
172 its
.it_interval
.tv_nsec
= 0;
174 its
.it_interval
.tv_sec
= its
.it_value
.tv_sec
;
175 its
.it_interval
.tv_nsec
= its
.it_value
.tv_nsec
;
178 ret
= timer_settime(*timer_id
, 0, &its
, NULL
);
180 PERROR("timer_settime");
181 goto error_destroy_timer
;
186 delete_ret
= timer_delete(*timer_id
);
187 if (delete_ret
== -1) {
188 PERROR("timer_delete");
196 int timer_stop(timer_t
*timer_id
, int signal
)
200 ret
= timer_delete(*timer_id
);
202 PERROR("timer_delete");
206 timer_signal_thread_qs(signal
);
212 int timer_session_rotation_pending_check_start(struct ltt_session
*session
,
213 unsigned int interval_us
)
217 if (!session_get(session
)) {
221 DBG("Enabling session rotation pending check timer on session %" PRIu64
,
224 * We arm this timer in a one-shot mode so we don't have to disable it
225 * explicitly (which could deadlock if the timer thread is blocked
226 * writing in the rotation_timer_pipe).
228 * Instead, we re-arm it if needed after the rotation_pending check as
229 * returned. Also, this timer is usually only needed once, so there is
230 * no need to go through the whole signal teardown scheme everytime.
232 ret
= timer_start(&session
->rotation_pending_check_timer
,
233 session
, interval_us
,
234 LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK
,
235 /* one-shot */ true);
237 session
->rotation_pending_check_timer_enabled
= true;
244 * Call with session and session_list locks held.
246 int timer_session_rotation_pending_check_stop(struct ltt_session
*session
)
251 assert(session
->rotation_pending_check_timer_enabled
);
253 DBG("Disabling session rotation pending check timer on session %" PRIu64
,
255 ret
= timer_stop(&session
->rotation_pending_check_timer
,
256 LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK
);
258 ERR("Failed to stop rotate_pending_check timer");
260 session
->rotation_pending_check_timer_enabled
= false;
262 * The timer's reference to the session can be released safely.
264 session_put(session
);
270 * Call with session and session_list locks held.
272 int timer_session_rotation_schedule_timer_start(struct ltt_session
*session
,
273 unsigned int interval_us
)
277 if (!session_get(session
)) {
281 DBG("Enabling scheduled rotation timer on session \"%s\" (%ui µs)", session
->name
,
283 ret
= timer_start(&session
->rotation_schedule_timer
, session
,
284 interval_us
, LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION
,
285 /* one-shot */ false);
289 session
->rotation_schedule_timer_enabled
= true;
295 * Call with session and session_list locks held.
297 int timer_session_rotation_schedule_timer_stop(struct ltt_session
*session
)
303 if (!session
->rotation_schedule_timer_enabled
) {
307 DBG("Disabling scheduled rotation timer on session %s", session
->name
);
308 ret
= timer_stop(&session
->rotation_schedule_timer
,
309 LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION
);
311 ERR("Failed to stop scheduled rotation timer of session \"%s\"",
316 session
->rotation_schedule_timer_enabled
= false;
317 /* The timer's reference to the session can be released safely. */
318 session_put(session
);
325 * Block the RT signals for the entire process. It must be called from the
326 * sessiond main before creating the threads
328 int timer_signal_init(void)
333 /* Block signal for entire process, so only our thread processes it. */
335 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
338 PERROR("pthread_sigmask");
345 * This thread is the sighandler for the timer signals.
347 void *timer_thread_func(void *data
)
352 struct timer_thread_parameters
*ctx
= data
;
354 rcu_register_thread();
357 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_TIMER
);
358 health_code_update();
360 /* Only self thread will receive signal mask. */
362 CMM_STORE_SHARED(timer_signal
.tid
, pthread_self());
365 health_code_update();
368 signr
= sigwaitinfo(&mask
, &info
);
372 * NOTE: cascading conditions are used instead of a switch case
373 * since the use of SIGRTMIN in the definition of the signals'
374 * values prevents the reduction to an integer constant.
377 if (errno
!= EINTR
) {
378 PERROR("sigwaitinfo");
381 } else if (signr
== LTTNG_SESSIOND_SIG_QS
) {
383 CMM_STORE_SHARED(timer_signal
.qs_done
, 1);
385 } else if (signr
== LTTNG_SESSIOND_SIG_EXIT
) {
387 } else if (signr
== LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK
) {
388 struct ltt_session
*session
=
389 (struct ltt_session
*) info
.si_value
.sival_ptr
;
391 rotation_thread_enqueue_job(ctx
->rotation_thread_job_queue
,
392 ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION
,
394 } else if (signr
== LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION
) {
395 rotation_thread_enqueue_job(ctx
->rotation_thread_job_queue
,
396 ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION
,
397 (struct ltt_session
*) info
.si_value
.sival_ptr
);
399 * The scheduled periodic rotation timer is not in
400 * "one-shot" mode. The reference to the session is not
401 * released since the timer is still enabled and can
405 ERR("Unexpected signal %d\n", info
.si_signo
);
410 DBG("[timer-thread] Exit");
411 health_unregister(health_sessiond
);
412 rcu_thread_offline();
413 rcu_unregister_thread();
417 void timer_exit(void)
419 kill(getpid(), LTTNG_SESSIOND_SIG_EXIT
);