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