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