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