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