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