Commit | Line | Data |
---|---|---|
db66e574 JD |
1 | /* |
2 | * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com> | |
92816cc3 | 3 | * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
db66e574 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 <lttng/trigger/trigger.h> | |
21 | #include <common/error.h> | |
22 | #include <common/config/session-config.h> | |
23 | #include <common/defaults.h> | |
24 | #include <common/utils.h> | |
25 | #include <common/futex.h> | |
26 | #include <common/align.h> | |
27 | #include <common/time.h> | |
28 | #include <common/hashtable/utils.h> | |
29 | #include <sys/eventfd.h> | |
30 | #include <sys/stat.h> | |
31 | #include <time.h> | |
32 | #include <signal.h> | |
33 | #include <inttypes.h> | |
34 | ||
35 | #include <common/kernel-ctl/kernel-ctl.h> | |
36 | #include <lttng/notification/channel-internal.h> | |
5c408ad8 | 37 | #include <lttng/rotate-internal.h> |
db66e574 JD |
38 | |
39 | #include "rotation-thread.h" | |
40 | #include "lttng-sessiond.h" | |
41 | #include "health-sessiond.h" | |
42 | #include "rotate.h" | |
43 | #include "cmd.h" | |
44 | #include "session.h" | |
8e319828 | 45 | #include "timer.h" |
17dd1232 | 46 | #include "notification-thread-commands.h" |
db66e574 JD |
47 | |
48 | #include <urcu.h> | |
49 | #include <urcu/list.h> | |
db66e574 | 50 | |
90936dcf JD |
51 | struct lttng_notification_channel *rotate_notification_channel = NULL; |
52 | ||
92816cc3 | 53 | struct rotation_thread { |
db66e574 JD |
54 | struct lttng_poll_event events; |
55 | }; | |
56 | ||
92816cc3 JG |
57 | struct rotation_thread_job { |
58 | enum rotation_thread_job_type type; | |
c7031a2c | 59 | struct ltt_session *session; |
92816cc3 JG |
60 | /* List member in struct rotation_thread_timer_queue. */ |
61 | struct cds_list_head head; | |
62 | }; | |
63 | ||
64 | /* | |
65 | * The timer thread enqueues jobs and wakes up the rotation thread. | |
66 | * When the rotation thread wakes up, it empties the queue. | |
67 | */ | |
68 | struct rotation_thread_timer_queue { | |
69 | struct lttng_pipe *event_pipe; | |
70 | struct cds_list_head list; | |
71 | pthread_mutex_t lock; | |
72 | }; | |
73 | ||
74 | struct rotation_thread_handle { | |
92816cc3 JG |
75 | struct rotation_thread_timer_queue *rotation_timer_queue; |
76 | /* Access to the notification thread cmd_queue */ | |
77 | struct notification_thread_handle *notification_thread_handle; | |
78 | sem_t *notification_thread_ready; | |
79 | }; | |
80 | ||
db66e574 | 81 | static |
92816cc3 | 82 | const char *get_job_type_str(enum rotation_thread_job_type job_type) |
db66e574 | 83 | { |
92816cc3 JG |
84 | switch (job_type) { |
85 | case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION: | |
86 | return "CHECK_PENDING_ROTATION"; | |
87 | case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION: | |
88 | return "SCHEDULED_ROTATION"; | |
89 | default: | |
90 | abort(); | |
91 | } | |
db66e574 JD |
92 | } |
93 | ||
92816cc3 | 94 | struct rotation_thread_timer_queue *rotation_thread_timer_queue_create(void) |
db66e574 | 95 | { |
92816cc3 | 96 | struct rotation_thread_timer_queue *queue = NULL; |
db66e574 | 97 | |
92816cc3 JG |
98 | queue = zmalloc(sizeof(*queue)); |
99 | if (!queue) { | |
100 | PERROR("Failed to allocate timer rotate queue"); | |
101 | goto end; | |
102 | } | |
db66e574 | 103 | |
92816cc3 JG |
104 | queue->event_pipe = lttng_pipe_open(FD_CLOEXEC | O_NONBLOCK); |
105 | CDS_INIT_LIST_HEAD(&queue->list); | |
106 | pthread_mutex_init(&queue->lock, NULL); | |
107 | end: | |
108 | return queue; | |
db66e574 JD |
109 | } |
110 | ||
92816cc3 | 111 | void log_job_destruction(const struct rotation_thread_job *job) |
db66e574 | 112 | { |
92816cc3 JG |
113 | enum lttng_error_level log_level; |
114 | const char *job_type_str = get_job_type_str(job->type); | |
115 | ||
116 | switch (job->type) { | |
117 | case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION: | |
118 | /* | |
119 | * Not a problem, the scheduled rotation is racing with the teardown | |
120 | * of the daemon. In this case, the rotation will not happen, which | |
121 | * is not a problem (or at least, not important enough to delay | |
122 | * the shutdown of the session daemon). | |
123 | */ | |
124 | log_level = PRINT_DBG; | |
125 | break; | |
126 | case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION: | |
127 | /* This is not expected to happen; warn the user. */ | |
128 | log_level = PRINT_WARN; | |
129 | break; | |
130 | default: | |
131 | abort(); | |
db66e574 JD |
132 | } |
133 | ||
c7031a2c JG |
134 | LOG(log_level, "Rotation thread timer queue still contains job of type %s targeting session \"%s\" on destruction", |
135 | job_type_str, job->session->name); | |
db66e574 JD |
136 | } |
137 | ||
92816cc3 JG |
138 | void rotation_thread_timer_queue_destroy( |
139 | struct rotation_thread_timer_queue *queue) | |
db66e574 | 140 | { |
92816cc3 | 141 | struct rotation_thread_job *job, *tmp_job; |
db66e574 | 142 | |
92816cc3 JG |
143 | if (!queue) { |
144 | return; | |
db66e574 JD |
145 | } |
146 | ||
92816cc3 JG |
147 | lttng_pipe_destroy(queue->event_pipe); |
148 | ||
149 | pthread_mutex_lock(&queue->lock); | |
150 | /* Empty wait queue. */ | |
151 | cds_list_for_each_entry_safe(job, tmp_job, &queue->list, head) { | |
152 | log_job_destruction(job); | |
153 | cds_list_del(&job->head); | |
154 | free(job); | |
db66e574 | 155 | } |
92816cc3 JG |
156 | pthread_mutex_unlock(&queue->lock); |
157 | pthread_mutex_destroy(&queue->lock); | |
158 | free(queue); | |
159 | } | |
db66e574 | 160 | |
92816cc3 JG |
161 | /* |
162 | * Destroy the thread data previously created by the init function. | |
163 | */ | |
164 | void rotation_thread_handle_destroy( | |
165 | struct rotation_thread_handle *handle) | |
166 | { | |
db66e574 JD |
167 | free(handle); |
168 | } | |
169 | ||
170 | struct rotation_thread_handle *rotation_thread_handle_create( | |
90936dcf JD |
171 | struct rotation_thread_timer_queue *rotation_timer_queue, |
172 | struct notification_thread_handle *notification_thread_handle, | |
173 | sem_t *notification_thread_ready) | |
db66e574 JD |
174 | { |
175 | struct rotation_thread_handle *handle; | |
176 | ||
177 | handle = zmalloc(sizeof(*handle)); | |
178 | if (!handle) { | |
179 | goto end; | |
180 | } | |
181 | ||
92816cc3 JG |
182 | handle->rotation_timer_queue = rotation_timer_queue; |
183 | handle->notification_thread_handle = notification_thread_handle; | |
184 | handle->notification_thread_ready = notification_thread_ready; | |
185 | ||
186 | end: | |
187 | return handle; | |
188 | } | |
189 | ||
190 | /* | |
191 | * Called with the rotation_thread_timer_queue lock held. | |
192 | * Return true if the same timer job already exists in the queue, false if not. | |
193 | */ | |
194 | static | |
195 | bool timer_job_exists(const struct rotation_thread_timer_queue *queue, | |
c7031a2c JG |
196 | enum rotation_thread_job_type job_type, |
197 | struct ltt_session *session) | |
92816cc3 JG |
198 | { |
199 | bool exists = false; | |
200 | struct rotation_thread_job *job; | |
201 | ||
202 | cds_list_for_each_entry(job, &queue->list, head) { | |
c7031a2c | 203 | if (job->session == session && job->type == job_type) { |
92816cc3 JG |
204 | exists = true; |
205 | goto end; | |
db66e574 | 206 | } |
db66e574 | 207 | } |
92816cc3 JG |
208 | end: |
209 | return exists; | |
210 | } | |
211 | ||
212 | void rotation_thread_enqueue_job(struct rotation_thread_timer_queue *queue, | |
c7031a2c JG |
213 | enum rotation_thread_job_type job_type, |
214 | struct ltt_session *session) | |
92816cc3 JG |
215 | { |
216 | int ret; | |
217 | const char * const dummy = "!"; | |
218 | struct rotation_thread_job *job = NULL; | |
219 | const char *job_type_str = get_job_type_str(job_type); | |
220 | ||
221 | pthread_mutex_lock(&queue->lock); | |
c7031a2c | 222 | if (timer_job_exists(queue, job_type, session)) { |
92816cc3 JG |
223 | /* |
224 | * This timer job is already pending, we don't need to add | |
225 | * it. | |
226 | */ | |
227 | goto end; | |
db66e574 | 228 | } |
92816cc3 JG |
229 | |
230 | job = zmalloc(sizeof(struct rotation_thread_job)); | |
231 | if (!job) { | |
c7031a2c JG |
232 | PERROR("Failed to allocate rotation thread job of type \"%s\" for session \"%s\"", |
233 | job_type_str, session->name); | |
92816cc3 JG |
234 | goto end; |
235 | } | |
c7031a2c JG |
236 | /* No reason for this to fail as the caller must hold a reference. */ |
237 | (void) session_get(session); | |
238 | ||
239 | job->session = session; | |
92816cc3 | 240 | job->type = job_type; |
92816cc3 JG |
241 | cds_list_add_tail(&job->head, &queue->list); |
242 | ||
243 | ret = lttng_write(lttng_pipe_get_writefd(queue->event_pipe), dummy, | |
244 | 1); | |
245 | if (ret < 0) { | |
246 | /* | |
247 | * We do not want to block in the timer handler, the job has | |
248 | * been enqueued in the list, the wakeup pipe is probably full, | |
249 | * the job will be processed when the rotation_thread catches | |
250 | * up. | |
251 | */ | |
252 | if (errno == EAGAIN || errno == EWOULDBLOCK) { | |
253 | /* | |
254 | * Not an error, but would be surprising and indicate | |
255 | * that the rotation thread can't keep up with the | |
256 | * current load. | |
257 | */ | |
258 | DBG("Wake-up pipe of rotation thread job queue is full"); | |
259 | goto end; | |
db66e574 | 260 | } |
c7031a2c JG |
261 | PERROR("Failed to wake-up the rotation thread after pushing a job of type \"%s\" for session \"%s\"", |
262 | job_type_str, session->name); | |
92816cc3 | 263 | goto end; |
db66e574 | 264 | } |
db66e574 JD |
265 | |
266 | end: | |
92816cc3 | 267 | pthread_mutex_unlock(&queue->lock); |
db66e574 JD |
268 | } |
269 | ||
270 | static | |
271 | int init_poll_set(struct lttng_poll_event *poll_set, | |
272 | struct rotation_thread_handle *handle) | |
273 | { | |
274 | int ret; | |
275 | ||
276 | /* | |
92816cc3 JG |
277 | * Create pollset with size 2: |
278 | * - quit pipe, | |
279 | * - rotation thread timer queue pipe, | |
db66e574 | 280 | */ |
a7333da7 JG |
281 | ret = sessiond_set_thread_pollset(poll_set, 2); |
282 | if (ret) { | |
db66e574 JD |
283 | goto error; |
284 | } | |
d086f507 JD |
285 | ret = lttng_poll_add(poll_set, |
286 | lttng_pipe_get_readfd(handle->rotation_timer_queue->event_pipe), | |
287 | LPOLLIN | LPOLLERR); | |
288 | if (ret < 0) { | |
289 | ERR("[rotation-thread] Failed to add rotate_pending fd to pollset"); | |
290 | goto error; | |
291 | } | |
db66e574 | 292 | |
db66e574 JD |
293 | return ret; |
294 | error: | |
295 | lttng_poll_clean(poll_set); | |
296 | return ret; | |
297 | } | |
298 | ||
299 | static | |
92816cc3 | 300 | void fini_thread_state(struct rotation_thread *state) |
db66e574 JD |
301 | { |
302 | lttng_poll_clean(&state->events); | |
90936dcf JD |
303 | if (rotate_notification_channel) { |
304 | lttng_notification_channel_destroy(rotate_notification_channel); | |
305 | } | |
db66e574 JD |
306 | } |
307 | ||
308 | static | |
309 | int init_thread_state(struct rotation_thread_handle *handle, | |
92816cc3 | 310 | struct rotation_thread *state) |
db66e574 JD |
311 | { |
312 | int ret; | |
313 | ||
314 | memset(state, 0, sizeof(*state)); | |
315 | lttng_poll_init(&state->events); | |
316 | ||
317 | ret = init_poll_set(&state->events, handle); | |
318 | if (ret) { | |
319 | ERR("[rotation-thread] Failed to initialize rotation thread poll set"); | |
320 | goto end; | |
321 | } | |
322 | ||
90936dcf JD |
323 | /* |
324 | * We wait until the notification thread is ready to create the | |
325 | * notification channel and add it to the poll_set. | |
326 | */ | |
327 | sem_wait(handle->notification_thread_ready); | |
328 | rotate_notification_channel = lttng_notification_channel_create( | |
329 | lttng_session_daemon_notification_endpoint); | |
330 | if (!rotate_notification_channel) { | |
331 | ERR("[rotation-thread] Could not create notification channel"); | |
332 | ret = -1; | |
333 | goto end; | |
334 | } | |
335 | ret = lttng_poll_add(&state->events, rotate_notification_channel->socket, | |
336 | LPOLLIN | LPOLLERR); | |
337 | if (ret < 0) { | |
338 | ERR("[rotation-thread] Failed to add notification fd to pollset"); | |
339 | goto end; | |
340 | } | |
341 | ||
db66e574 JD |
342 | end: |
343 | return ret; | |
344 | } | |
345 | ||
346 | static | |
92816cc3 JG |
347 | int check_session_rotation_pending_local_on_consumer( |
348 | const struct ltt_session *session, | |
349 | struct consumer_socket *socket, bool *rotation_completed) | |
db66e574 | 350 | { |
92816cc3 JG |
351 | int ret; |
352 | ||
353 | pthread_mutex_lock(socket->lock); | |
354 | DBG("[rotation-thread] Checking for locally pending rotation on the %s consumer for session %s", | |
355 | lttng_consumer_type_str(socket->type), | |
356 | session->name); | |
357 | ret = consumer_check_rotation_pending_local(socket, | |
358 | session->id, | |
359 | session->current_archive_id - 1); | |
360 | pthread_mutex_unlock(socket->lock); | |
361 | ||
362 | if (ret == 0) { | |
363 | /* Rotation was completed on this consumer. */ | |
364 | DBG("[rotation-thread] Local rotation of trace archive %" PRIu64 " of session \"%s\" was completed on the %s consumer", | |
365 | session->current_archive_id - 1, | |
366 | session->name, | |
367 | lttng_consumer_type_str(socket->type)); | |
368 | *rotation_completed = true; | |
369 | } else if (ret == 1) { | |
370 | /* Rotation pending on this consumer. */ | |
371 | DBG("[rotation-thread] Local rotation of trace archive %" PRIu64 " of session \"%s\" is pending on the %s consumer", | |
372 | session->current_archive_id - 1, | |
373 | session->name, | |
374 | lttng_consumer_type_str(socket->type)); | |
375 | *rotation_completed = false; | |
376 | ret = 0; | |
db66e574 | 377 | } else { |
92816cc3 JG |
378 | /* Not a fatal error. */ |
379 | ERR("[rotation-thread] Encountered an error when checking if local rotation of trace archive %" PRIu64 " of session \"%s\" is pending on the %s consumer", | |
380 | session->current_archive_id - 1, | |
381 | session->name, | |
382 | lttng_consumer_type_str(socket->type)); | |
383 | *rotation_completed = false; | |
db66e574 | 384 | } |
92816cc3 JG |
385 | return ret; |
386 | } | |
db66e574 | 387 | |
92816cc3 JG |
388 | static |
389 | int check_session_rotation_pending_local(struct ltt_session *session) | |
390 | { | |
db582e11 | 391 | int ret = 0; |
92816cc3 JG |
392 | struct consumer_socket *socket; |
393 | struct cds_lfht_iter iter; | |
394 | bool rotation_completed = true; | |
395 | ||
396 | /* | |
397 | * Check for a local pending rotation on all consumers (32-bit | |
398 | * user space, 64-bit user space, and kernel). | |
399 | */ | |
400 | DBG("[rotation-thread] Checking for pending local rotation on session \"%s\", trace archive %" PRIu64, | |
401 | session->name, session->current_archive_id - 1); | |
402 | ||
403 | rcu_read_lock(); | |
404 | if (!session->ust_session) { | |
405 | goto skip_ust; | |
406 | } | |
407 | cds_lfht_for_each_entry(session->ust_session->consumer->socks->ht, | |
408 | &iter, socket, node.node) { | |
409 | ret = check_session_rotation_pending_local_on_consumer(session, | |
410 | socket, &rotation_completed); | |
411 | if (ret || !rotation_completed) { | |
412 | goto end; | |
db66e574 | 413 | } |
db66e574 JD |
414 | } |
415 | ||
92816cc3 JG |
416 | skip_ust: |
417 | if (!session->kernel_session) { | |
418 | goto skip_kernel; | |
db66e574 | 419 | } |
92816cc3 JG |
420 | cds_lfht_for_each_entry(session->kernel_session->consumer->socks->ht, |
421 | &iter, socket, node.node) { | |
422 | ret = check_session_rotation_pending_local_on_consumer(session, | |
423 | socket, &rotation_completed); | |
424 | if (ret || !rotation_completed) { | |
425 | goto end; | |
426 | } | |
427 | } | |
428 | skip_kernel: | |
429 | end: | |
430 | rcu_read_unlock(); | |
db66e574 | 431 | |
92816cc3 JG |
432 | if (rotation_completed) { |
433 | DBG("[rotation-thread] Local rotation of trace archive %" PRIu64 " of session \"%s\" is complete on all consumers", | |
434 | session->current_archive_id - 1, | |
435 | session->name); | |
436 | session->rotation_pending_local = false; | |
db66e574 | 437 | } |
92816cc3 | 438 | if (ret) { |
2961f09e JG |
439 | ret = session_reset_rotation_state(session, |
440 | LTTNG_ROTATION_STATE_ERROR); | |
441 | if (ret) { | |
442 | ERR("Failed to reset rotation state of session \"%s\"", | |
443 | session->name); | |
444 | } | |
db66e574 | 445 | } |
92816cc3 JG |
446 | return 0; |
447 | } | |
db66e574 | 448 | |
92816cc3 JG |
449 | static |
450 | int check_session_rotation_pending_relay(struct ltt_session *session) | |
451 | { | |
452 | int ret; | |
453 | struct consumer_socket *socket; | |
454 | struct cds_lfht_iter iter; | |
455 | bool rotation_completed = true; | |
456 | const struct consumer_output *output; | |
db66e574 | 457 | |
92816cc3 JG |
458 | /* |
459 | * Check for a pending rotation on any consumer as we only use | |
460 | * it as a "tunnel" to the relayd. | |
461 | */ | |
17dd1232 | 462 | |
92816cc3 JG |
463 | rcu_read_lock(); |
464 | if (session->ust_session) { | |
465 | cds_lfht_first(session->ust_session->consumer->socks->ht, | |
466 | &iter); | |
467 | output = session->ust_session->consumer; | |
468 | } else { | |
469 | cds_lfht_first(session->kernel_session->consumer->socks->ht, | |
470 | &iter); | |
471 | output = session->kernel_session->consumer; | |
db66e574 | 472 | } |
92816cc3 | 473 | assert(cds_lfht_iter_get_node(&iter)); |
db66e574 | 474 | |
92816cc3 JG |
475 | socket = caa_container_of(cds_lfht_iter_get_node(&iter), |
476 | typeof(*socket), node.node); | |
477 | ||
478 | pthread_mutex_lock(socket->lock); | |
479 | DBG("[rotation-thread] Checking for pending relay rotation on session \"%s\", trace archive %" PRIu64 " through the %s consumer", | |
480 | session->name, session->current_archive_id - 1, | |
481 | lttng_consumer_type_str(socket->type)); | |
482 | ret = consumer_check_rotation_pending_relay(socket, | |
483 | output, | |
484 | session->id, | |
485 | session->current_archive_id - 1); | |
486 | pthread_mutex_unlock(socket->lock); | |
487 | ||
488 | if (ret == 0) { | |
489 | /* Rotation was completed on the relay. */ | |
490 | DBG("[rotation-thread] Relay rotation of trace archive %" PRIu64 " of session \"%s\" was completed", | |
491 | session->current_archive_id - 1, | |
492 | session->name); | |
493 | } else if (ret == 1) { | |
494 | /* Rotation pending on relay. */ | |
495 | DBG("[rotation-thread] Relay rotation of trace archive %" PRIu64 " of session \"%s\" is pending", | |
496 | session->current_archive_id - 1, | |
497 | session->name); | |
498 | rotation_completed = false; | |
499 | } else { | |
500 | /* Not a fatal error. */ | |
501 | ERR("[rotation-thread] Encountered an error when checking if rotation of trace archive %" PRIu64 " of session \"%s\" is pending on the relay", | |
502 | session->current_archive_id - 1, | |
503 | session->name); | |
2961f09e JG |
504 | ret = session_reset_rotation_state(session, |
505 | LTTNG_ROTATION_STATE_ERROR); | |
506 | if (ret) { | |
507 | ERR("Failed to reset rotation state of session \"%s\"", | |
508 | session->name); | |
509 | } | |
92816cc3 JG |
510 | rotation_completed = false; |
511 | } | |
db66e574 | 512 | |
db66e574 | 513 | rcu_read_unlock(); |
92816cc3 JG |
514 | |
515 | if (rotation_completed) { | |
cb70f0d5 | 516 | DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " of session \"%s\" is complete on the relay", |
92816cc3 JG |
517 | session->current_archive_id - 1, |
518 | session->name); | |
519 | session->rotation_pending_relay = false; | |
520 | } | |
521 | return 0; | |
db66e574 JD |
522 | } |
523 | ||
d88744a4 | 524 | /* |
92816cc3 | 525 | * Check if the last rotation was completed, called with session lock held. |
d88744a4 JD |
526 | */ |
527 | static | |
92816cc3 JG |
528 | int check_session_rotation_pending(struct ltt_session *session, |
529 | struct notification_thread_handle *notification_thread_handle) | |
d88744a4 JD |
530 | { |
531 | int ret; | |
92816cc3 JG |
532 | struct lttng_trace_archive_location *location; |
533 | time_t now; | |
d88744a4 | 534 | |
92816cc3 JG |
535 | DBG("[rotation-thread] Checking for pending rotation on session \"%s\", trace archive %" PRIu64, |
536 | session->name, session->current_archive_id - 1); | |
537 | ||
538 | if (session->rotation_pending_local) { | |
539 | /* Updates session->rotation_pending_local as needed. */ | |
540 | ret = check_session_rotation_pending_local(session); | |
541 | if (ret) { | |
542 | goto end; | |
543 | } | |
17dd1232 | 544 | |
d88744a4 | 545 | /* |
92816cc3 JG |
546 | * No need to check for a pending rotation on the relay |
547 | * since the rotation is not even completed locally yet. | |
d88744a4 | 548 | */ |
92816cc3 JG |
549 | if (session->rotation_pending_local) { |
550 | goto end; | |
17dd1232 | 551 | } |
92816cc3 JG |
552 | } |
553 | ||
554 | if (session->rotation_pending_relay) { | |
555 | /* Updates session->rotation_pending_relay as needed. */ | |
556 | ret = check_session_rotation_pending_relay(session); | |
d88744a4 | 557 | if (ret) { |
92816cc3 JG |
558 | goto end; |
559 | } | |
560 | ||
561 | if (session->rotation_pending_relay) { | |
d88744a4 JD |
562 | goto end; |
563 | } | |
564 | } | |
565 | ||
92816cc3 JG |
566 | DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " completed for " |
567 | "session %s", session->current_archive_id - 1, | |
568 | session->name); | |
d88744a4 | 569 | |
92816cc3 JG |
570 | /* Rename the completed trace archive's location. */ |
571 | now = time(NULL); | |
572 | if (now == (time_t) -1) { | |
2961f09e JG |
573 | ret = session_reset_rotation_state(session, |
574 | LTTNG_ROTATION_STATE_ERROR); | |
575 | if (ret) { | |
576 | ERR("Failed to reset rotation state of session \"%s\"", | |
577 | session->name); | |
578 | } | |
92816cc3 JG |
579 | ret = LTTNG_ERR_UNK; |
580 | goto end; | |
581 | } | |
582 | ||
583 | ret = rename_completed_chunk(session, now); | |
584 | if (ret < 0) { | |
585 | ERR("Failed to rename completed rotation chunk"); | |
586 | goto end; | |
587 | } | |
588 | session->last_chunk_start_ts = session->current_chunk_start_ts; | |
589 | ||
590 | /* | |
591 | * Now we can clear the "ONGOING" state in the session. New | |
592 | * rotations can start now. | |
593 | */ | |
594 | session->rotation_state = LTTNG_ROTATION_STATE_COMPLETED; | |
595 | ||
596 | /* Ownership of location is transferred. */ | |
597 | location = session_get_trace_archive_location(session); | |
598 | ret = notification_thread_command_session_rotation_completed( | |
599 | notification_thread_handle, | |
600 | session->name, | |
601 | session->uid, | |
602 | session->gid, | |
603 | session->current_archive_id, | |
604 | location); | |
605 | if (ret != LTTNG_OK) { | |
606 | ERR("[rotation-thread] Failed to notify notification thread of completed rotation for session %s", | |
607 | session->name); | |
608 | } | |
609 | ||
9402c5b9 JG |
610 | if (!session->active) { |
611 | /* | |
612 | * A stop command was issued during the rotation, it is | |
613 | * up to the rotation completion check to perform the | |
614 | * renaming of the last chunk that was produced. | |
615 | */ | |
616 | ret = notification_thread_command_session_rotation_ongoing( | |
617 | notification_thread_handle, | |
618 | session->name, | |
619 | session->uid, | |
620 | session->gid, | |
621 | session->current_archive_id); | |
622 | if (ret != LTTNG_OK) { | |
623 | ERR("[rotation-thread] Failed to notify notification thread of completed rotation for session %s", | |
624 | session->name); | |
625 | } | |
626 | ||
627 | ret = rename_active_chunk(session); | |
628 | if (ret < 0) { | |
629 | ERR("[rotation-thread] Failed to rename active rotation chunk"); | |
630 | goto end; | |
631 | } | |
632 | ||
633 | /* Ownership of location is transferred. */ | |
634 | location = session_get_trace_archive_location(session); | |
635 | ret = notification_thread_command_session_rotation_completed( | |
636 | notification_thread_handle, | |
637 | session->name, | |
638 | session->uid, | |
639 | session->gid, | |
640 | session->current_archive_id, | |
641 | location); | |
642 | if (ret != LTTNG_OK) { | |
643 | ERR("[rotation-thread] Failed to notify notification thread of completed rotation for session %s", | |
644 | session->name); | |
645 | } | |
646 | } | |
647 | ||
92816cc3 | 648 | ret = 0; |
d88744a4 | 649 | end: |
92816cc3 JG |
650 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
651 | DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " is still pending for session %s", | |
652 | session->current_archive_id - 1, session->name); | |
653 | ret = timer_session_rotation_pending_check_start(session, | |
654 | DEFAULT_ROTATE_PENDING_TIMER); | |
655 | if (ret) { | |
656 | ERR("Re-enabling rotate pending timer"); | |
657 | ret = -1; | |
658 | goto end; | |
659 | } | |
660 | } | |
661 | ||
d88744a4 JD |
662 | return ret; |
663 | } | |
664 | ||
ed1e52a3 | 665 | /* Call with the session and session_list locks held. */ |
259c2674 | 666 | static |
92816cc3 | 667 | int launch_session_rotation(struct ltt_session *session) |
259c2674 JD |
668 | { |
669 | int ret; | |
92816cc3 | 670 | struct lttng_rotate_session_return rotation_return; |
259c2674 | 671 | |
92816cc3 JG |
672 | DBG("[rotation-thread] Launching scheduled time-based rotation on session \"%s\"", |
673 | session->name); | |
259c2674 | 674 | |
92816cc3 JG |
675 | ret = cmd_rotate_session(session, &rotation_return); |
676 | if (ret == LTTNG_OK) { | |
677 | DBG("[rotation-thread] Scheduled time-based rotation successfully launched on session \"%s\"", | |
678 | session->name); | |
679 | } else { | |
680 | /* Don't consider errors as fatal. */ | |
681 | DBG("[rotation-thread] Scheduled time-based rotation aborted for session %s: %s", | |
682 | session->name, lttng_strerror(ret)); | |
259c2674 | 683 | } |
92816cc3 JG |
684 | return 0; |
685 | } | |
259c2674 | 686 | |
92816cc3 JG |
687 | static |
688 | int run_job(struct rotation_thread_job *job, struct ltt_session *session, | |
689 | struct notification_thread_handle *notification_thread_handle) | |
690 | { | |
691 | int ret; | |
259c2674 | 692 | |
92816cc3 JG |
693 | switch (job->type) { |
694 | case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION: | |
695 | ret = launch_session_rotation(session); | |
696 | break; | |
697 | case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION: | |
698 | ret = check_session_rotation_pending(session, | |
699 | notification_thread_handle); | |
700 | break; | |
701 | default: | |
702 | abort(); | |
259c2674 | 703 | } |
259c2674 JD |
704 | return ret; |
705 | } | |
706 | ||
d88744a4 | 707 | static |
92816cc3 JG |
708 | int handle_job_queue(struct rotation_thread_handle *handle, |
709 | struct rotation_thread *state, | |
d88744a4 JD |
710 | struct rotation_thread_timer_queue *queue) |
711 | { | |
712 | int ret = 0; | |
713 | int fd = lttng_pipe_get_readfd(queue->event_pipe); | |
92816cc3 | 714 | char buf; |
d88744a4 | 715 | |
92816cc3 | 716 | ret = lttng_read(fd, &buf, 1); |
d88744a4 JD |
717 | if (ret != 1) { |
718 | ERR("[rotation-thread] Failed to read from wakeup pipe (fd = %i)", fd); | |
719 | ret = -1; | |
720 | goto end; | |
721 | } | |
722 | ||
723 | for (;;) { | |
e32d7f27 | 724 | struct ltt_session *session; |
92816cc3 | 725 | struct rotation_thread_job *job; |
d88744a4 | 726 | |
92816cc3 | 727 | /* Take the queue lock only to pop an element from the list. */ |
d88744a4 JD |
728 | pthread_mutex_lock(&queue->lock); |
729 | if (cds_list_empty(&queue->list)) { | |
730 | pthread_mutex_unlock(&queue->lock); | |
731 | break; | |
732 | } | |
92816cc3 JG |
733 | job = cds_list_first_entry(&queue->list, |
734 | typeof(*job), head); | |
735 | cds_list_del(&job->head); | |
d88744a4 JD |
736 | pthread_mutex_unlock(&queue->lock); |
737 | ||
d88744a4 | 738 | session_lock_list(); |
c7031a2c | 739 | session = job->session; |
d88744a4 | 740 | if (!session) { |
c7031a2c JG |
741 | DBG("[rotation-thread] Session \"%s\" not found", |
742 | session->name); | |
d88744a4 | 743 | /* |
92816cc3 JG |
744 | * This is a non-fatal error, and we cannot report it to |
745 | * the user (timer), so just print the error and | |
746 | * continue the processing. | |
747 | * | |
748 | * While the timer thread will purge pending signals for | |
749 | * a session on the session's destruction, it is | |
750 | * possible for a job targeting that session to have | |
751 | * already been queued before it was destroyed. | |
d88744a4 JD |
752 | */ |
753 | session_unlock_list(); | |
92816cc3 | 754 | free(job); |
e32d7f27 | 755 | session_put(session); |
d88744a4 JD |
756 | continue; |
757 | } | |
758 | ||
d88744a4 | 759 | session_lock(session); |
92816cc3 | 760 | ret = run_job(job, session, handle->notification_thread_handle); |
d88744a4 | 761 | session_unlock(session); |
e32d7f27 | 762 | session_put(session); |
ed1e52a3 | 763 | session_unlock_list(); |
92816cc3 | 764 | free(job); |
d88744a4 | 765 | if (ret) { |
d88744a4 JD |
766 | goto end; |
767 | } | |
768 | } | |
769 | ||
770 | ret = 0; | |
771 | ||
772 | end: | |
773 | return ret; | |
774 | } | |
775 | ||
92816cc3 JG |
776 | static |
777 | int handle_condition(const struct lttng_condition *condition, | |
90936dcf JD |
778 | const struct lttng_evaluation *evaluation, |
779 | struct notification_thread_handle *notification_thread_handle) | |
780 | { | |
781 | int ret = 0; | |
782 | const char *condition_session_name = NULL; | |
783 | enum lttng_condition_type condition_type; | |
784 | enum lttng_condition_status condition_status; | |
785 | enum lttng_evaluation_status evaluation_status; | |
786 | uint64_t consumed; | |
787 | struct ltt_session *session; | |
788 | ||
789 | condition_type = lttng_condition_get_type(condition); | |
790 | ||
791 | if (condition_type != LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE) { | |
792 | ret = -1; | |
793 | ERR("[rotation-thread] Condition type and session usage type are not the same"); | |
794 | goto end; | |
795 | } | |
796 | ||
797 | /* Fetch info to test */ | |
798 | condition_status = lttng_condition_session_consumed_size_get_session_name( | |
799 | condition, &condition_session_name); | |
800 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
801 | ERR("[rotation-thread] Session name could not be fetched"); | |
802 | ret = -1; | |
803 | goto end; | |
804 | } | |
805 | evaluation_status = lttng_evaluation_session_consumed_size_get_consumed_size(evaluation, | |
806 | &consumed); | |
807 | if (evaluation_status != LTTNG_EVALUATION_STATUS_OK) { | |
808 | ERR("[rotation-thread] Failed to get evaluation"); | |
809 | ret = -1; | |
810 | goto end; | |
811 | } | |
812 | ||
813 | session_lock_list(); | |
814 | session = session_find_by_name(condition_session_name); | |
815 | if (!session) { | |
816 | ret = -1; | |
817 | session_unlock_list(); | |
818 | ERR("[rotation-thread] Session \"%s\" not found", | |
819 | condition_session_name); | |
820 | goto end; | |
821 | } | |
822 | session_lock(session); | |
823 | session_unlock_list(); | |
824 | ||
825 | ret = unsubscribe_session_consumed_size_rotation(session, | |
826 | notification_thread_handle); | |
827 | if (ret) { | |
490b3229 | 828 | goto end_unlock; |
90936dcf JD |
829 | } |
830 | ||
831 | ret = cmd_rotate_session(session, NULL); | |
832 | if (ret == -LTTNG_ERR_ROTATION_PENDING) { | |
833 | DBG("Rotate already pending, subscribe to the next threshold value"); | |
90936dcf JD |
834 | } else if (ret != LTTNG_OK) { |
835 | ERR("[rotation-thread] Failed to rotate on size notification with error: %s", | |
836 | lttng_strerror(ret)); | |
837 | ret = -1; | |
838 | goto end_unlock; | |
839 | } | |
840 | ret = subscribe_session_consumed_size_rotation(session, | |
841 | consumed + session->rotate_size, | |
842 | notification_thread_handle); | |
843 | if (ret) { | |
844 | ERR("[rotation-thread] Failed to subscribe to session consumed size condition"); | |
845 | goto end_unlock; | |
846 | } | |
847 | ret = 0; | |
848 | ||
849 | end_unlock: | |
850 | session_unlock(session); | |
e32d7f27 | 851 | session_put(session); |
90936dcf JD |
852 | end: |
853 | return ret; | |
854 | } | |
855 | ||
856 | static | |
92816cc3 | 857 | int handle_notification_channel(int fd, |
90936dcf | 858 | struct rotation_thread_handle *handle, |
92816cc3 | 859 | struct rotation_thread *state) |
90936dcf JD |
860 | { |
861 | int ret; | |
d73ee93f JG |
862 | bool notification_pending; |
863 | struct lttng_notification *notification = NULL; | |
90936dcf JD |
864 | enum lttng_notification_channel_status status; |
865 | const struct lttng_evaluation *notification_evaluation; | |
866 | const struct lttng_condition *notification_condition; | |
867 | ||
d73ee93f JG |
868 | status = lttng_notification_channel_has_pending_notification( |
869 | rotate_notification_channel, ¬ification_pending); | |
870 | if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) { | |
a9577b76 | 871 | ERR("[rotation-thread ]Error occurred while checking for pending notification"); |
d73ee93f JG |
872 | ret = -1; |
873 | goto end; | |
874 | } | |
875 | ||
876 | if (!notification_pending) { | |
877 | ret = 0; | |
878 | goto end; | |
879 | } | |
880 | ||
90936dcf JD |
881 | /* Receive the next notification. */ |
882 | status = lttng_notification_channel_get_next_notification( | |
883 | rotate_notification_channel, | |
884 | ¬ification); | |
885 | ||
886 | switch (status) { | |
887 | case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK: | |
888 | break; | |
889 | case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED: | |
890 | /* Not an error, we will wait for the next one */ | |
891 | ret = 0; | |
892 | goto end;; | |
893 | case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED: | |
894 | ERR("Notification channel was closed"); | |
895 | ret = -1; | |
896 | goto end; | |
897 | default: | |
898 | /* Unhandled conditions / errors. */ | |
899 | ERR("Unknown notification channel status"); | |
900 | ret = -1; | |
901 | goto end; | |
902 | } | |
903 | ||
904 | notification_condition = lttng_notification_get_condition(notification); | |
905 | notification_evaluation = lttng_notification_get_evaluation(notification); | |
906 | ||
907 | ret = handle_condition(notification_condition, notification_evaluation, | |
908 | handle->notification_thread_handle); | |
909 | ||
910 | end: | |
911 | lttng_notification_destroy(notification); | |
90936dcf JD |
912 | return ret; |
913 | } | |
914 | ||
db66e574 JD |
915 | void *thread_rotation(void *data) |
916 | { | |
917 | int ret; | |
918 | struct rotation_thread_handle *handle = data; | |
92816cc3 | 919 | struct rotation_thread thread; |
db66e574 JD |
920 | |
921 | DBG("[rotation-thread] Started rotation thread"); | |
922 | ||
923 | if (!handle) { | |
924 | ERR("[rotation-thread] Invalid thread context provided"); | |
925 | goto end; | |
926 | } | |
927 | ||
03732c32 JG |
928 | rcu_register_thread(); |
929 | rcu_thread_online(); | |
930 | ||
db66e574 JD |
931 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_ROTATION); |
932 | health_code_update(); | |
933 | ||
92816cc3 | 934 | ret = init_thread_state(handle, &thread); |
db66e574 | 935 | if (ret) { |
f5f8e5cd | 936 | goto error; |
db66e574 JD |
937 | } |
938 | ||
939 | /* Ready to handle client connections. */ | |
940 | sessiond_notify_ready(); | |
941 | ||
942 | while (true) { | |
943 | int fd_count, i; | |
944 | ||
945 | health_poll_entry(); | |
946 | DBG("[rotation-thread] Entering poll wait"); | |
92816cc3 | 947 | ret = lttng_poll_wait(&thread.events, -1); |
db66e574 JD |
948 | DBG("[rotation-thread] Poll wait returned (%i)", ret); |
949 | health_poll_exit(); | |
950 | if (ret < 0) { | |
951 | /* | |
952 | * Restart interrupted system call. | |
953 | */ | |
954 | if (errno == EINTR) { | |
955 | continue; | |
956 | } | |
957 | ERR("[rotation-thread] Error encountered during lttng_poll_wait (%i)", ret); | |
958 | goto error; | |
959 | } | |
960 | ||
961 | fd_count = ret; | |
962 | for (i = 0; i < fd_count; i++) { | |
92816cc3 JG |
963 | int fd = LTTNG_POLL_GETFD(&thread.events, i); |
964 | uint32_t revents = LTTNG_POLL_GETEV(&thread.events, i); | |
db66e574 JD |
965 | |
966 | DBG("[rotation-thread] Handling fd (%i) activity (%u)", | |
967 | fd, revents); | |
968 | ||
92816cc3 JG |
969 | if (revents & LPOLLERR) { |
970 | ERR("[rotation-thread] Polling returned an error on fd %i", fd); | |
971 | goto error; | |
972 | } | |
973 | ||
a7333da7 | 974 | if (sessiond_check_thread_quit_pipe(fd, revents)) { |
db66e574 | 975 | DBG("[rotation-thread] Quit pipe activity"); |
92816cc3 | 976 | /* TODO flush the queue. */ |
db66e574 | 977 | goto exit; |
d88744a4 | 978 | } else if (fd == lttng_pipe_get_readfd(handle->rotation_timer_queue->event_pipe)) { |
92816cc3 JG |
979 | ret = handle_job_queue(handle, &thread, |
980 | handle->rotation_timer_queue); | |
d88744a4 JD |
981 | if (ret) { |
982 | ERR("[rotation-thread] Failed to handle rotation timer pipe event"); | |
983 | goto error; | |
984 | } | |
90936dcf | 985 | } else if (fd == rotate_notification_channel->socket) { |
92816cc3 JG |
986 | ret = handle_notification_channel(fd, handle, |
987 | &thread); | |
90936dcf | 988 | if (ret) { |
a9577b76 | 989 | ERR("[rotation-thread] Error occurred while handling activity on notification channel socket"); |
90936dcf JD |
990 | goto error; |
991 | } | |
db66e574 JD |
992 | } |
993 | } | |
994 | } | |
995 | exit: | |
996 | error: | |
997 | DBG("[rotation-thread] Exit"); | |
92816cc3 | 998 | fini_thread_state(&thread); |
db66e574 | 999 | health_unregister(health_sessiond); |
03732c32 JG |
1000 | rcu_thread_offline(); |
1001 | rcu_unregister_thread(); | |
db66e574 JD |
1002 | end: |
1003 | return NULL; | |
1004 | } |