Cleanup: ust-app.c: Dead assignment
[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 chunk_status = lttng_trace_chunk_get_id(
498 session->chunk_being_archived,
499 &chunk_being_archived_id);
500 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
501
92816cc3 502 DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " is still pending for session %s",
d2956687 503 chunk_being_archived_id, session->name);
92816cc3
JG
504 ret = timer_session_rotation_pending_check_start(session,
505 DEFAULT_ROTATE_PENDING_TIMER);
506 if (ret) {
d2956687 507 ERR("Failed to re-enable rotation pending timer");
92816cc3
JG
508 ret = -1;
509 goto end;
510 }
511 }
512
6ae1bf46 513end:
d88744a4
JD
514 return ret;
515}
516
ed1e52a3 517/* Call with the session and session_list locks held. */
259c2674 518static
92816cc3 519int launch_session_rotation(struct ltt_session *session)
259c2674
JD
520{
521 int ret;
92816cc3 522 struct lttng_rotate_session_return rotation_return;
259c2674 523
92816cc3
JG
524 DBG("[rotation-thread] Launching scheduled time-based rotation on session \"%s\"",
525 session->name);
259c2674 526
343defc2
MD
527 ret = cmd_rotate_session(session, &rotation_return, false,
528 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
92816cc3
JG
529 if (ret == LTTNG_OK) {
530 DBG("[rotation-thread] Scheduled time-based rotation successfully launched on session \"%s\"",
531 session->name);
532 } else {
533 /* Don't consider errors as fatal. */
534 DBG("[rotation-thread] Scheduled time-based rotation aborted for session %s: %s",
535 session->name, lttng_strerror(ret));
259c2674 536 }
92816cc3
JG
537 return 0;
538}
259c2674 539
92816cc3
JG
540static
541int run_job(struct rotation_thread_job *job, struct ltt_session *session,
542 struct notification_thread_handle *notification_thread_handle)
543{
544 int ret;
259c2674 545
92816cc3
JG
546 switch (job->type) {
547 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION:
16100d7a 548 ret = launch_session_rotation(session);
92816cc3
JG
549 break;
550 case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION:
551 ret = check_session_rotation_pending(session,
552 notification_thread_handle);
553 break;
554 default:
555 abort();
259c2674 556 }
259c2674
JD
557 return ret;
558}
559
d88744a4 560static
92816cc3
JG
561int handle_job_queue(struct rotation_thread_handle *handle,
562 struct rotation_thread *state,
d88744a4
JD
563 struct rotation_thread_timer_queue *queue)
564{
565 int ret = 0;
d88744a4
JD
566
567 for (;;) {
e32d7f27 568 struct ltt_session *session;
92816cc3 569 struct rotation_thread_job *job;
d88744a4 570
92816cc3 571 /* Take the queue lock only to pop an element from the list. */
d88744a4
JD
572 pthread_mutex_lock(&queue->lock);
573 if (cds_list_empty(&queue->list)) {
574 pthread_mutex_unlock(&queue->lock);
575 break;
576 }
92816cc3
JG
577 job = cds_list_first_entry(&queue->list,
578 typeof(*job), head);
579 cds_list_del(&job->head);
d88744a4
JD
580 pthread_mutex_unlock(&queue->lock);
581
d88744a4 582 session_lock_list();
c7031a2c 583 session = job->session;
d88744a4 584 if (!session) {
c7031a2c
JG
585 DBG("[rotation-thread] Session \"%s\" not found",
586 session->name);
d88744a4 587 /*
92816cc3
JG
588 * This is a non-fatal error, and we cannot report it to
589 * the user (timer), so just print the error and
590 * continue the processing.
591 *
592 * While the timer thread will purge pending signals for
593 * a session on the session's destruction, it is
594 * possible for a job targeting that session to have
595 * already been queued before it was destroyed.
d88744a4 596 */
92816cc3 597 free(job);
e32d7f27 598 session_put(session);
5b8139c6 599 session_unlock_list();
d88744a4
JD
600 continue;
601 }
602
d88744a4 603 session_lock(session);
16100d7a 604 ret = run_job(job, session, handle->notification_thread_handle);
d88744a4 605 session_unlock(session);
faf1bdcf 606 /* Release reference held by the job. */
e32d7f27 607 session_put(session);
ed1e52a3 608 session_unlock_list();
92816cc3 609 free(job);
d88744a4 610 if (ret) {
d88744a4
JD
611 goto end;
612 }
613 }
614
615 ret = 0;
616
617end:
618 return ret;
619}
620
92816cc3
JG
621static
622int handle_condition(const struct lttng_condition *condition,
90936dcf
JD
623 const struct lttng_evaluation *evaluation,
624 struct notification_thread_handle *notification_thread_handle)
625{
626 int ret = 0;
627 const char *condition_session_name = NULL;
628 enum lttng_condition_type condition_type;
629 enum lttng_condition_status condition_status;
630 enum lttng_evaluation_status evaluation_status;
631 uint64_t consumed;
632 struct ltt_session *session;
633
634 condition_type = lttng_condition_get_type(condition);
635
636 if (condition_type != LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE) {
637 ret = -1;
638 ERR("[rotation-thread] Condition type and session usage type are not the same");
639 goto end;
640 }
641
642 /* Fetch info to test */
643 condition_status = lttng_condition_session_consumed_size_get_session_name(
644 condition, &condition_session_name);
645 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
646 ERR("[rotation-thread] Session name could not be fetched");
647 ret = -1;
648 goto end;
649 }
650 evaluation_status = lttng_evaluation_session_consumed_size_get_consumed_size(evaluation,
651 &consumed);
652 if (evaluation_status != LTTNG_EVALUATION_STATUS_OK) {
653 ERR("[rotation-thread] Failed to get evaluation");
654 ret = -1;
655 goto end;
656 }
657
658 session_lock_list();
659 session = session_find_by_name(condition_session_name);
660 if (!session) {
661 ret = -1;
662 session_unlock_list();
663 ERR("[rotation-thread] Session \"%s\" not found",
664 condition_session_name);
665 goto end;
666 }
667 session_lock(session);
90936dcf
JD
668
669 ret = unsubscribe_session_consumed_size_rotation(session,
670 notification_thread_handle);
671 if (ret) {
490b3229 672 goto end_unlock;
90936dcf
JD
673 }
674
343defc2
MD
675 ret = cmd_rotate_session(session, NULL, false,
676 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
90936dcf
JD
677 if (ret == -LTTNG_ERR_ROTATION_PENDING) {
678 DBG("Rotate already pending, subscribe to the next threshold value");
90936dcf
JD
679 } else if (ret != LTTNG_OK) {
680 ERR("[rotation-thread] Failed to rotate on size notification with error: %s",
681 lttng_strerror(ret));
682 ret = -1;
683 goto end_unlock;
684 }
685 ret = subscribe_session_consumed_size_rotation(session,
686 consumed + session->rotate_size,
687 notification_thread_handle);
688 if (ret) {
689 ERR("[rotation-thread] Failed to subscribe to session consumed size condition");
690 goto end_unlock;
691 }
692 ret = 0;
693
694end_unlock:
695 session_unlock(session);
e32d7f27 696 session_put(session);
5b8139c6 697 session_unlock_list();
90936dcf
JD
698end:
699 return ret;
700}
701
702static
92816cc3 703int handle_notification_channel(int fd,
90936dcf 704 struct rotation_thread_handle *handle,
92816cc3 705 struct rotation_thread *state)
90936dcf
JD
706{
707 int ret;
d73ee93f
JG
708 bool notification_pending;
709 struct lttng_notification *notification = NULL;
90936dcf
JD
710 enum lttng_notification_channel_status status;
711 const struct lttng_evaluation *notification_evaluation;
712 const struct lttng_condition *notification_condition;
713
d73ee93f
JG
714 status = lttng_notification_channel_has_pending_notification(
715 rotate_notification_channel, &notification_pending);
716 if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
e275709f 717 ERR("[rotation-thread] Error occurred while checking for pending notification");
d73ee93f
JG
718 ret = -1;
719 goto end;
720 }
721
722 if (!notification_pending) {
723 ret = 0;
724 goto end;
725 }
726
90936dcf
JD
727 /* Receive the next notification. */
728 status = lttng_notification_channel_get_next_notification(
729 rotate_notification_channel,
730 &notification);
731
732 switch (status) {
733 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK:
734 break;
735 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED:
736 /* Not an error, we will wait for the next one */
737 ret = 0;
738 goto end;;
739 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED:
740 ERR("Notification channel was closed");
741 ret = -1;
742 goto end;
743 default:
744 /* Unhandled conditions / errors. */
745 ERR("Unknown notification channel status");
746 ret = -1;
747 goto end;
748 }
749
750 notification_condition = lttng_notification_get_condition(notification);
751 notification_evaluation = lttng_notification_get_evaluation(notification);
752
753 ret = handle_condition(notification_condition, notification_evaluation,
754 handle->notification_thread_handle);
755
756end:
757 lttng_notification_destroy(notification);
90936dcf
JD
758 return ret;
759}
760
2c0c9bbc 761static
db66e574
JD
762void *thread_rotation(void *data)
763{
764 int ret;
765 struct rotation_thread_handle *handle = data;
92816cc3 766 struct rotation_thread thread;
87380d40 767 int queue_pipe_fd;
db66e574
JD
768
769 DBG("[rotation-thread] Started rotation thread");
f620cc28
JG
770 rcu_register_thread();
771 rcu_thread_online();
412d7227 772 health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_ROTATION);
f620cc28 773 health_code_update();
db66e574
JD
774
775 if (!handle) {
776 ERR("[rotation-thread] Invalid thread context provided");
777 goto end;
778 }
779
87380d40
JR
780 queue_pipe_fd = lttng_pipe_get_readfd(
781 handle->rotation_timer_queue->event_pipe);
782
db66e574 783
92816cc3 784 ret = init_thread_state(handle, &thread);
db66e574 785 if (ret) {
f5f8e5cd 786 goto error;
db66e574
JD
787 }
788
db66e574
JD
789 while (true) {
790 int fd_count, i;
791
792 health_poll_entry();
793 DBG("[rotation-thread] Entering poll wait");
92816cc3 794 ret = lttng_poll_wait(&thread.events, -1);
db66e574
JD
795 DBG("[rotation-thread] Poll wait returned (%i)", ret);
796 health_poll_exit();
797 if (ret < 0) {
798 /*
799 * Restart interrupted system call.
800 */
801 if (errno == EINTR) {
802 continue;
803 }
804 ERR("[rotation-thread] Error encountered during lttng_poll_wait (%i)", ret);
805 goto error;
806 }
807
808 fd_count = ret;
809 for (i = 0; i < fd_count; i++) {
92816cc3
JG
810 int fd = LTTNG_POLL_GETFD(&thread.events, i);
811 uint32_t revents = LTTNG_POLL_GETEV(&thread.events, i);
db66e574
JD
812
813 DBG("[rotation-thread] Handling fd (%i) activity (%u)",
814 fd, revents);
815
92816cc3
JG
816 if (revents & LPOLLERR) {
817 ERR("[rotation-thread] Polling returned an error on fd %i", fd);
818 goto error;
819 }
820
85e17b27
JG
821 if (fd == rotate_notification_channel->socket) {
822 ret = handle_notification_channel(fd, handle,
823 &thread);
824 if (ret) {
825 ERR("[rotation-thread] Error occurred while handling activity on notification channel socket");
826 goto error;
827 }
828 } else {
829 /* Job queue or quit pipe activity. */
85e17b27
JG
830
831 /*
832 * The job queue is serviced if there is
833 * activity on the quit pipe to ensure it is
834 * flushed and all references held in the queue
835 * are released.
836 */
92816cc3
JG
837 ret = handle_job_queue(handle, &thread,
838 handle->rotation_timer_queue);
d88744a4
JD
839 if (ret) {
840 ERR("[rotation-thread] Failed to handle rotation timer pipe event");
841 goto error;
842 }
85e17b27 843
64d9b072
JG
844 if (fd == queue_pipe_fd) {
845 char buf;
846
847 ret = lttng_read(fd, &buf, 1);
848 if (ret != 1) {
849 ERR("[rotation-thread] Failed to read from wakeup pipe (fd = %i)", fd);
64d9b072
JG
850 goto error;
851 }
852 } else {
85e17b27
JG
853 DBG("[rotation-thread] Quit pipe activity");
854 goto exit;
90936dcf 855 }
db66e574
JD
856 }
857 }
858 }
859exit:
860error:
861 DBG("[rotation-thread] Exit");
92816cc3 862 fini_thread_state(&thread);
f620cc28 863end:
412d7227 864 health_unregister(the_health_sessiond);
03732c32
JG
865 rcu_thread_offline();
866 rcu_unregister_thread();
db66e574
JD
867 return NULL;
868}
64d9b072
JG
869
870static
871bool shutdown_rotation_thread(void *thread_data)
872{
873 struct rotation_thread_handle *handle = thread_data;
874 const int write_fd = lttng_pipe_get_writefd(handle->quit_pipe);
875
876 return notify_thread_pipe(write_fd) == 1;
877}
878
879bool launch_rotation_thread(struct rotation_thread_handle *handle)
880{
881 struct lttng_thread *thread;
882
883 thread = lttng_thread_create("Rotation",
884 thread_rotation,
885 shutdown_rotation_thread,
886 NULL,
887 handle);
888 if (!thread) {
889 goto error;
890 }
891 lttng_thread_put(thread);
892 return true;
893error:
894 return false;
895}
This page took 0.084468 seconds and 4 git commands to generate.