Commit | Line | Data |
---|---|---|
d086f507 JD |
1 | /* |
2 | * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com> | |
92816cc3 | 3 | * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
d086f507 JD |
4 | * |
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. | |
8 | * | |
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 | |
12 | * more details. | |
13 | * | |
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. | |
17 | */ | |
18 | ||
19 | #define _LGPL_SOURCE | |
20 | #include <assert.h> | |
21 | #include <inttypes.h> | |
22 | #include <signal.h> | |
23 | ||
8e319828 | 24 | #include "timer.h" |
d086f507 JD |
25 | #include "health-sessiond.h" |
26 | #include "rotation-thread.h" | |
27 | ||
92816cc3 JG |
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 | |
32 | ||
33 | #define UINT_TO_PTR(value) \ | |
34 | ({ \ | |
35 | assert(value <= UINTPTR_MAX); \ | |
36 | (void *) (uintptr_t) value; \ | |
37 | }) | |
38 | #define PTR_TO_UINT(ptr) ((uintptr_t) ptr) | |
39 | ||
40 | /* | |
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 | |
44 | * exclusion. | |
45 | */ | |
d086f507 | 46 | static |
92816cc3 JG |
47 | struct timer_signal_data { |
48 | /* Thread managing signals. */ | |
49 | pthread_t tid; | |
50 | int qs_done; | |
51 | pthread_mutex_t lock; | |
52 | } timer_signal = { | |
d086f507 JD |
53 | .tid = 0, |
54 | .qs_done = 0, | |
55 | .lock = PTHREAD_MUTEX_INITIALIZER, | |
56 | }; | |
57 | ||
58 | /* | |
59 | * Set custom signal mask to current thread. | |
60 | */ | |
61 | static | |
62 | void setmask(sigset_t *mask) | |
63 | { | |
64 | int ret; | |
65 | ||
66 | ret = sigemptyset(mask); | |
67 | if (ret) { | |
68 | PERROR("sigemptyset"); | |
69 | } | |
92816cc3 | 70 | ret = sigaddset(mask, LTTNG_SESSIOND_SIG_QS); |
d086f507 JD |
71 | if (ret) { |
72 | PERROR("sigaddset teardown"); | |
73 | } | |
74 | ret = sigaddset(mask, LTTNG_SESSIOND_SIG_EXIT); | |
75 | if (ret) { | |
76 | PERROR("sigaddset exit"); | |
77 | } | |
92816cc3 | 78 | ret = sigaddset(mask, LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK); |
d88744a4 | 79 | if (ret) { |
92816cc3 | 80 | PERROR("sigaddset pending rotation check"); |
d88744a4 | 81 | } |
92816cc3 | 82 | ret = sigaddset(mask, LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION); |
259c2674 | 83 | if (ret) { |
92816cc3 | 84 | PERROR("sigaddset scheduled rotation"); |
259c2674 | 85 | } |
d086f507 JD |
86 | } |
87 | ||
88 | /* | |
92816cc3 | 89 | * This is the same function as timer_signal_thread_qs, when it |
d086f507 JD |
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. | |
92 | */ | |
93 | static | |
92816cc3 | 94 | void timer_signal_thread_qs(unsigned int signr) |
d086f507 JD |
95 | { |
96 | sigset_t pending_set; | |
97 | int ret; | |
98 | ||
99 | /* | |
100 | * We need to be the only thread interacting with the thread | |
101 | * that manages signals for teardown synchronization. | |
102 | */ | |
103 | pthread_mutex_lock(&timer_signal.lock); | |
104 | ||
105 | /* Ensure we don't have any signal queued for this session. */ | |
106 | for (;;) { | |
107 | ret = sigemptyset(&pending_set); | |
108 | if (ret == -1) { | |
109 | PERROR("sigemptyset"); | |
110 | } | |
111 | ret = sigpending(&pending_set); | |
112 | if (ret == -1) { | |
113 | PERROR("sigpending"); | |
114 | } | |
115 | if (!sigismember(&pending_set, signr)) { | |
116 | break; | |
117 | } | |
118 | caa_cpu_relax(); | |
119 | } | |
120 | ||
121 | /* | |
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. | |
125 | */ | |
126 | cmm_smp_mb(); | |
127 | CMM_STORE_SHARED(timer_signal.qs_done, 0); | |
128 | cmm_smp_mb(); | |
129 | ||
130 | /* | |
92816cc3 | 131 | * Kill with LTTNG_SESSIOND_SIG_QS, so signal management thread |
d086f507 JD |
132 | * wakes up. |
133 | */ | |
92816cc3 | 134 | kill(getpid(), LTTNG_SESSIOND_SIG_QS); |
d086f507 JD |
135 | |
136 | while (!CMM_LOAD_SHARED(timer_signal.qs_done)) { | |
137 | caa_cpu_relax(); | |
138 | } | |
139 | cmm_smp_mb(); | |
140 | ||
141 | pthread_mutex_unlock(&timer_signal.lock); | |
142 | } | |
143 | ||
144 | /* | |
145 | * Start a timer on a session that will fire at a given interval | |
146 | * (timer_interval_us) and fire a given signal (signal). | |
147 | * | |
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). | |
150 | */ | |
151 | static | |
92816cc3 | 152 | int timer_start(timer_t *timer_id, uint64_t session_id, |
d086f507 JD |
153 | unsigned int timer_interval_us, int signal, bool one_shot) |
154 | { | |
155 | int ret = 0, delete_ret; | |
156 | struct sigevent sev; | |
157 | struct itimerspec its; | |
158 | ||
d086f507 JD |
159 | sev.sigev_notify = SIGEV_SIGNAL; |
160 | sev.sigev_signo = signal; | |
92816cc3 JG |
161 | sev.sigev_value.sival_ptr = UINT_TO_PTR(session_id); |
162 | ret = timer_create(CLOCK_MONOTONIC, &sev, timer_id); | |
d086f507 JD |
163 | if (ret == -1) { |
164 | PERROR("timer_create"); | |
165 | goto end; | |
166 | } | |
167 | ||
168 | its.it_value.tv_sec = timer_interval_us / 1000000; | |
169 | its.it_value.tv_nsec = (timer_interval_us % 1000000) * 1000; | |
170 | if (one_shot) { | |
171 | its.it_interval.tv_sec = 0; | |
172 | its.it_interval.tv_nsec = 0; | |
173 | } else { | |
174 | its.it_interval.tv_sec = its.it_value.tv_sec; | |
175 | its.it_interval.tv_nsec = its.it_value.tv_nsec; | |
176 | } | |
177 | ||
178 | ret = timer_settime(*timer_id, 0, &its, NULL); | |
179 | if (ret == -1) { | |
180 | PERROR("timer_settime"); | |
181 | goto error_destroy_timer; | |
182 | } | |
183 | goto end; | |
184 | ||
185 | error_destroy_timer: | |
186 | delete_ret = timer_delete(*timer_id); | |
187 | if (delete_ret == -1) { | |
188 | PERROR("timer_delete"); | |
189 | } | |
190 | ||
191 | end: | |
192 | return ret; | |
193 | } | |
194 | ||
195 | static | |
92816cc3 | 196 | int timer_stop(timer_t *timer_id, int signal) |
d086f507 JD |
197 | { |
198 | int ret = 0; | |
199 | ||
200 | ret = timer_delete(*timer_id); | |
201 | if (ret == -1) { | |
202 | PERROR("timer_delete"); | |
203 | goto end; | |
204 | } | |
205 | ||
92816cc3 | 206 | timer_signal_thread_qs(signal); |
d086f507 JD |
207 | *timer_id = 0; |
208 | end: | |
209 | return ret; | |
210 | } | |
211 | ||
92816cc3 | 212 | int timer_session_rotation_pending_check_start(struct ltt_session *session, |
d88744a4 JD |
213 | unsigned int interval_us) |
214 | { | |
215 | int ret; | |
216 | ||
92816cc3 JG |
217 | DBG("Enabling session rotation pending check timer on session %" PRIu64, |
218 | session->id); | |
d88744a4 JD |
219 | /* |
220 | * We arm this timer in a one-shot mode so we don't have to disable it | |
92816cc3 JG |
221 | * explicitly (which could deadlock if the timer thread is blocked |
222 | * writing in the rotation_timer_pipe). | |
223 | * | |
d88744a4 | 224 | * Instead, we re-arm it if needed after the rotation_pending check as |
92816cc3 JG |
225 | * returned. Also, this timer is usually only needed once, so there is |
226 | * no need to go through the whole signal teardown scheme everytime. | |
d88744a4 | 227 | */ |
92816cc3 JG |
228 | ret = timer_start(&session->rotation_pending_check_timer, |
229 | session->id, interval_us, | |
230 | LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK, | |
d88744a4 JD |
231 | /* one-shot */ true); |
232 | if (ret == 0) { | |
92816cc3 | 233 | session->rotation_pending_check_timer_enabled = true; |
d88744a4 JD |
234 | } |
235 | ||
236 | return ret; | |
237 | } | |
238 | ||
239 | /* | |
92816cc3 | 240 | * Call with session and session_list locks held. |
d88744a4 | 241 | */ |
92816cc3 | 242 | int timer_session_rotation_pending_check_stop(struct ltt_session *session) |
d88744a4 JD |
243 | { |
244 | int ret; | |
245 | ||
246 | assert(session); | |
247 | ||
92816cc3 JG |
248 | DBG("Disabling session rotation pending check timer on session %" PRIu64, |
249 | session->id); | |
250 | ret = timer_stop(&session->rotation_pending_check_timer, | |
251 | LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK); | |
d88744a4 | 252 | if (ret == -1) { |
92816cc3 | 253 | ERR("Failed to stop rotate_pending_check timer"); |
259c2674 | 254 | } else { |
92816cc3 | 255 | session->rotation_pending_check_timer_enabled = false; |
259c2674 JD |
256 | } |
257 | return ret; | |
258 | } | |
259 | ||
92816cc3 JG |
260 | /* |
261 | * Call with session and session_list locks held. | |
262 | */ | |
263 | int timer_session_rotation_schedule_timer_start(struct ltt_session *session, | |
259c2674 JD |
264 | unsigned int interval_us) |
265 | { | |
266 | int ret; | |
267 | ||
92816cc3 | 268 | DBG("Enabling scheduled rotation timer on session \"%s\" (%ui µs)", session->name, |
259c2674 | 269 | interval_us); |
92816cc3 JG |
270 | ret = timer_start(&session->rotation_schedule_timer, session->id, |
271 | interval_us, LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION, | |
272 | /* one-shot */ false); | |
259c2674 JD |
273 | if (ret < 0) { |
274 | goto end; | |
275 | } | |
92816cc3 | 276 | session->rotation_schedule_timer_enabled = true; |
259c2674 JD |
277 | end: |
278 | return ret; | |
279 | } | |
280 | ||
281 | /* | |
92816cc3 | 282 | * Call with session and session_list locks held. |
259c2674 | 283 | */ |
92816cc3 | 284 | int timer_session_rotation_schedule_timer_stop(struct ltt_session *session) |
259c2674 JD |
285 | { |
286 | int ret = 0; | |
287 | ||
288 | assert(session); | |
289 | ||
92816cc3 | 290 | if (!session->rotation_schedule_timer_enabled) { |
259c2674 JD |
291 | goto end; |
292 | } | |
293 | ||
92816cc3 JG |
294 | DBG("Disabling scheduled rotation timer on session %s", session->name); |
295 | ret = timer_stop(&session->rotation_schedule_timer, | |
296 | LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION); | |
259c2674 | 297 | if (ret < 0) { |
92816cc3 | 298 | ERR("Failed to stop scheduled rotation timer of session \"%s\"", |
259c2674 JD |
299 | session->name); |
300 | goto end; | |
d88744a4 JD |
301 | } |
302 | ||
92816cc3 | 303 | session->rotation_schedule_timer_enabled = false; |
259c2674 JD |
304 | ret = 0; |
305 | end: | |
306 | return ret; | |
d88744a4 JD |
307 | } |
308 | ||
d086f507 JD |
309 | /* |
310 | * Block the RT signals for the entire process. It must be called from the | |
311 | * sessiond main before creating the threads | |
312 | */ | |
92816cc3 | 313 | int timer_signal_init(void) |
d086f507 JD |
314 | { |
315 | int ret; | |
316 | sigset_t mask; | |
317 | ||
318 | /* Block signal for entire process, so only our thread processes it. */ | |
319 | setmask(&mask); | |
320 | ret = pthread_sigmask(SIG_BLOCK, &mask, NULL); | |
321 | if (ret) { | |
322 | errno = ret; | |
323 | PERROR("pthread_sigmask"); | |
324 | return -1; | |
325 | } | |
326 | return 0; | |
327 | } | |
328 | ||
329 | /* | |
330 | * This thread is the sighandler for the timer signals. | |
331 | */ | |
92816cc3 | 332 | void *timer_thread_func(void *data) |
d086f507 JD |
333 | { |
334 | int signr; | |
335 | sigset_t mask; | |
336 | siginfo_t info; | |
337 | struct timer_thread_parameters *ctx = data; | |
338 | ||
339 | rcu_register_thread(); | |
340 | rcu_thread_online(); | |
341 | ||
342 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_TIMER); | |
d086f507 JD |
343 | health_code_update(); |
344 | ||
345 | /* Only self thread will receive signal mask. */ | |
346 | setmask(&mask); | |
347 | CMM_STORE_SHARED(timer_signal.tid, pthread_self()); | |
348 | ||
349 | while (1) { | |
350 | health_code_update(); | |
351 | ||
352 | health_poll_entry(); | |
353 | signr = sigwaitinfo(&mask, &info); | |
354 | health_poll_exit(); | |
355 | ||
356 | /* | |
357 | * NOTE: cascading conditions are used instead of a switch case | |
358 | * since the use of SIGRTMIN in the definition of the signals' | |
359 | * values prevents the reduction to an integer constant. | |
360 | */ | |
361 | if (signr == -1) { | |
362 | if (errno != EINTR) { | |
363 | PERROR("sigwaitinfo"); | |
364 | } | |
365 | continue; | |
92816cc3 | 366 | } else if (signr == LTTNG_SESSIOND_SIG_QS) { |
d086f507 JD |
367 | cmm_smp_mb(); |
368 | CMM_STORE_SHARED(timer_signal.qs_done, 1); | |
369 | cmm_smp_mb(); | |
d086f507 JD |
370 | } else if (signr == LTTNG_SESSIOND_SIG_EXIT) { |
371 | goto end; | |
92816cc3 JG |
372 | } else if (signr == LTTNG_SESSIOND_SIG_PENDING_ROTATION_CHECK) { |
373 | rotation_thread_enqueue_job(ctx->rotation_thread_job_queue, | |
374 | ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION, | |
375 | /* session_id */ PTR_TO_UINT(info.si_value.sival_ptr)); | |
376 | } else if (signr == LTTNG_SESSIOND_SIG_SCHEDULED_ROTATION) { | |
377 | rotation_thread_enqueue_job(ctx->rotation_thread_job_queue, | |
378 | ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION, | |
379 | /* session_id */ PTR_TO_UINT(info.si_value.sival_ptr)); | |
d086f507 JD |
380 | } else { |
381 | ERR("Unexpected signal %d\n", info.si_signo); | |
382 | } | |
383 | } | |
384 | ||
385 | end: | |
386 | DBG("[timer-thread] Exit"); | |
387 | health_unregister(health_sessiond); | |
388 | rcu_thread_offline(); | |
389 | rcu_unregister_thread(); | |
390 | return NULL; | |
391 | } | |
92816cc3 JG |
392 | |
393 | void timer_exit(void) | |
394 | { | |
395 | kill(getpid(), LTTNG_SESSIOND_SIG_EXIT); | |
396 | } |