Sync ax_have_epoll.m4 with autoconf-archive
[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
JG
114void rotation_thread_timer_queue_destroy(
115 struct rotation_thread_timer_queue *queue)
db66e574 116{
92816cc3
JG
117 if (!queue) {
118 return;
db66e574
JD
119 }
120
92816cc3
JG
121 lttng_pipe_destroy(queue->event_pipe);
122
123 pthread_mutex_lock(&queue->lock);
64d9b072 124 assert(cds_list_empty(&queue->list));
92816cc3
JG
125 pthread_mutex_unlock(&queue->lock);
126 pthread_mutex_destroy(&queue->lock);
127 free(queue);
128}
db66e574 129
92816cc3
JG
130/*
131 * Destroy the thread data previously created by the init function.
132 */
133void rotation_thread_handle_destroy(
134 struct rotation_thread_handle *handle)
135{
64d9b072 136 lttng_pipe_destroy(handle->quit_pipe);
db66e574
JD
137 free(handle);
138}
139
140struct rotation_thread_handle *rotation_thread_handle_create(
90936dcf 141 struct rotation_thread_timer_queue *rotation_timer_queue,
c8a9de5a 142 struct notification_thread_handle *notification_thread_handle)
db66e574
JD
143{
144 struct rotation_thread_handle *handle;
145
146 handle = zmalloc(sizeof(*handle));
147 if (!handle) {
148 goto end;
149 }
150
92816cc3
JG
151 handle->rotation_timer_queue = rotation_timer_queue;
152 handle->notification_thread_handle = notification_thread_handle;
64d9b072
JG
153 handle->quit_pipe = lttng_pipe_open(FD_CLOEXEC);
154 if (!handle->quit_pipe) {
155 goto error;
156 }
92816cc3
JG
157
158end:
159 return handle;
64d9b072
JG
160error:
161 rotation_thread_handle_destroy(handle);
162 return NULL;
92816cc3
JG
163}
164
165/*
166 * Called with the rotation_thread_timer_queue lock held.
167 * Return true if the same timer job already exists in the queue, false if not.
168 */
169static
170bool timer_job_exists(const struct rotation_thread_timer_queue *queue,
c7031a2c
JG
171 enum rotation_thread_job_type job_type,
172 struct ltt_session *session)
92816cc3
JG
173{
174 bool exists = false;
175 struct rotation_thread_job *job;
176
177 cds_list_for_each_entry(job, &queue->list, head) {
c7031a2c 178 if (job->session == session && job->type == job_type) {
92816cc3
JG
179 exists = true;
180 goto end;
db66e574 181 }
db66e574 182 }
92816cc3
JG
183end:
184 return exists;
185}
186
187void rotation_thread_enqueue_job(struct rotation_thread_timer_queue *queue,
c7031a2c
JG
188 enum rotation_thread_job_type job_type,
189 struct ltt_session *session)
92816cc3
JG
190{
191 int ret;
be2956e7 192 const char dummy = '!';
92816cc3
JG
193 struct rotation_thread_job *job = NULL;
194 const char *job_type_str = get_job_type_str(job_type);
195
196 pthread_mutex_lock(&queue->lock);
c7031a2c 197 if (timer_job_exists(queue, job_type, session)) {
92816cc3
JG
198 /*
199 * This timer job is already pending, we don't need to add
200 * it.
201 */
202 goto end;
db66e574 203 }
92816cc3
JG
204
205 job = zmalloc(sizeof(struct rotation_thread_job));
206 if (!job) {
c7031a2c
JG
207 PERROR("Failed to allocate rotation thread job of type \"%s\" for session \"%s\"",
208 job_type_str, session->name);
92816cc3
JG
209 goto end;
210 }
c7031a2c
JG
211 /* No reason for this to fail as the caller must hold a reference. */
212 (void) session_get(session);
213
214 job->session = session;
92816cc3 215 job->type = job_type;
92816cc3
JG
216 cds_list_add_tail(&job->head, &queue->list);
217
be2956e7
JG
218 ret = lttng_write(lttng_pipe_get_writefd(queue->event_pipe), &dummy,
219 sizeof(dummy));
92816cc3
JG
220 if (ret < 0) {
221 /*
222 * We do not want to block in the timer handler, the job has
223 * been enqueued in the list, the wakeup pipe is probably full,
224 * the job will be processed when the rotation_thread catches
225 * up.
226 */
227 if (errno == EAGAIN || errno == EWOULDBLOCK) {
228 /*
229 * Not an error, but would be surprising and indicate
230 * that the rotation thread can't keep up with the
231 * current load.
232 */
233 DBG("Wake-up pipe of rotation thread job queue is full");
234 goto end;
db66e574 235 }
c7031a2c
JG
236 PERROR("Failed to wake-up the rotation thread after pushing a job of type \"%s\" for session \"%s\"",
237 job_type_str, session->name);
92816cc3 238 goto end;
db66e574 239 }
db66e574
JD
240
241end:
92816cc3 242 pthread_mutex_unlock(&queue->lock);
db66e574
JD
243}
244
245static
246int init_poll_set(struct lttng_poll_event *poll_set,
247 struct rotation_thread_handle *handle)
248{
249 int ret;
250
251 /*
64d9b072
JG
252 * Create pollset with size 3:
253 * - rotation thread quit pipe,
92816cc3 254 * - rotation thread timer queue pipe,
64d9b072 255 * - notification channel sock,
db66e574 256 */
64d9b072
JG
257 ret = lttng_poll_create(poll_set, 5, LTTNG_CLOEXEC);
258 if (ret < 0) {
db66e574
JD
259 goto error;
260 }
64d9b072
JG
261
262 ret = lttng_poll_add(poll_set,
263 lttng_pipe_get_readfd(handle->quit_pipe),
264 LPOLLIN | LPOLLERR);
265 if (ret < 0) {
266 ERR("[rotation-thread] Failed to add quit pipe read fd to poll set");
267 goto error;
268 }
269
d086f507
JD
270 ret = lttng_poll_add(poll_set,
271 lttng_pipe_get_readfd(handle->rotation_timer_queue->event_pipe),
272 LPOLLIN | LPOLLERR);
273 if (ret < 0) {
64d9b072 274 ERR("[rotation-thread] Failed to add rotate_pending fd to poll set");
d086f507
JD
275 goto error;
276 }
db66e574 277
db66e574
JD
278 return ret;
279error:
280 lttng_poll_clean(poll_set);
281 return ret;
282}
283
284static
92816cc3 285void fini_thread_state(struct rotation_thread *state)
db66e574
JD
286{
287 lttng_poll_clean(&state->events);
90936dcf
JD
288 if (rotate_notification_channel) {
289 lttng_notification_channel_destroy(rotate_notification_channel);
290 }
db66e574
JD
291}
292
293static
294int init_thread_state(struct rotation_thread_handle *handle,
92816cc3 295 struct rotation_thread *state)
db66e574
JD
296{
297 int ret;
298
299 memset(state, 0, sizeof(*state));
300 lttng_poll_init(&state->events);
301
302 ret = init_poll_set(&state->events, handle);
303 if (ret) {
304 ERR("[rotation-thread] Failed to initialize rotation thread poll set");
305 goto end;
306 }
307
90936dcf
JD
308 rotate_notification_channel = lttng_notification_channel_create(
309 lttng_session_daemon_notification_endpoint);
310 if (!rotate_notification_channel) {
311 ERR("[rotation-thread] Could not create notification channel");
312 ret = -1;
313 goto end;
314 }
315 ret = lttng_poll_add(&state->events, rotate_notification_channel->socket,
316 LPOLLIN | LPOLLERR);
317 if (ret < 0) {
318 ERR("[rotation-thread] Failed to add notification fd to pollset");
319 goto end;
320 }
321
db66e574
JD
322end:
323 return ret;
324}
325
326static
d2956687
JG
327void check_session_rotation_pending_on_consumers(struct ltt_session *session,
328 bool *_rotation_completed)
92816cc3 329{
db582e11 330 int ret = 0;
92816cc3
JG
331 struct consumer_socket *socket;
332 struct cds_lfht_iter iter;
d2956687
JG
333 enum consumer_trace_chunk_exists_status exists_status;
334 uint64_t relayd_id;
335 bool chunk_exists_on_peer = false;
336 enum lttng_trace_chunk_status chunk_status;
337
338 assert(session->chunk_being_archived);
92816cc3
JG
339
340 /*
341 * Check for a local pending rotation on all consumers (32-bit
342 * user space, 64-bit user space, and kernel).
343 */
92816cc3
JG
344 rcu_read_lock();
345 if (!session->ust_session) {
346 goto skip_ust;
347 }
348 cds_lfht_for_each_entry(session->ust_session->consumer->socks->ht,
349 &iter, socket, node.node) {
d2956687
JG
350 relayd_id = session->ust_session->consumer->type == CONSUMER_DST_LOCAL ?
351 -1ULL :
352 session->ust_session->consumer->net_seq_index;
353
354 pthread_mutex_lock(socket->lock);
355 ret = consumer_trace_chunk_exists(socket,
356 relayd_id,
357 session->id, session->chunk_being_archived,
358 &exists_status);
359 if (ret) {
360 pthread_mutex_unlock(socket->lock);
83ed9e90 361 ERR("Error occurred while checking rotation status on consumer daemon");
92816cc3 362 goto end;
db66e574 363 }
d2956687
JG
364
365 if (exists_status != CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK) {
366 pthread_mutex_unlock(socket->lock);
367 chunk_exists_on_peer = true;
368 goto end;
369 }
370 pthread_mutex_unlock(socket->lock);
371 }
db66e574 372
92816cc3
JG
373skip_ust:
374 if (!session->kernel_session) {
375 goto skip_kernel;
db66e574 376 }
92816cc3
JG
377 cds_lfht_for_each_entry(session->kernel_session->consumer->socks->ht,
378 &iter, socket, node.node) {
d2956687
JG
379 pthread_mutex_lock(socket->lock);
380 relayd_id = session->kernel_session->consumer->type == CONSUMER_DST_LOCAL ?
381 -1ULL :
382 session->kernel_session->consumer->net_seq_index;
383
384 ret = consumer_trace_chunk_exists(socket,
385 relayd_id,
386 session->id, session->chunk_being_archived,
387 &exists_status);
388 if (ret) {
389 pthread_mutex_unlock(socket->lock);
83ed9e90 390 ERR("Error occurred while checking rotation status on consumer daemon");
92816cc3
JG
391 goto end;
392 }
d2956687
JG
393
394 if (exists_status != CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK) {
395 pthread_mutex_unlock(socket->lock);
396 chunk_exists_on_peer = true;
397 goto end;
398 }
399 pthread_mutex_unlock(socket->lock);
92816cc3
JG
400 }
401skip_kernel:
402end:
403 rcu_read_unlock();
db66e574 404
d2956687
JG
405 if (!chunk_exists_on_peer) {
406 uint64_t chunk_being_archived_id;
407
408 chunk_status = lttng_trace_chunk_get_id(
409 session->chunk_being_archived,
410 &chunk_being_archived_id);
411 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
412 DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " of session \"%s\" is complete on all consumers",
413 chunk_being_archived_id,
92816cc3 414 session->name);
db66e574 415 }
d2956687 416 *_rotation_completed = !chunk_exists_on_peer;
92816cc3 417 if (ret) {
2961f09e
JG
418 ret = session_reset_rotation_state(session,
419 LTTNG_ROTATION_STATE_ERROR);
420 if (ret) {
421 ERR("Failed to reset rotation state of session \"%s\"",
422 session->name);
423 }
db66e574 424 }
db66e574
JD
425}
426
d88744a4 427/*
92816cc3 428 * Check if the last rotation was completed, called with session lock held.
d2956687
JG
429 * Should only return non-zero in the event of a fatal error. Doing so will
430 * shutdown the thread.
d88744a4
JD
431 */
432static
92816cc3
JG
433int check_session_rotation_pending(struct ltt_session *session,
434 struct notification_thread_handle *notification_thread_handle)
d88744a4
JD
435{
436 int ret;
92816cc3 437 struct lttng_trace_archive_location *location;
d2956687
JG
438 enum lttng_trace_chunk_status chunk_status;
439 bool rotation_completed = false;
440 const char *archived_chunk_name;
441 uint64_t chunk_being_archived_id;
442
dc1d5967
FD
443 if (!session->chunk_being_archived) {
444 ret = 0;
445 goto end;
446 }
447
d2956687
JG
448 chunk_status = lttng_trace_chunk_get_id(session->chunk_being_archived,
449 &chunk_being_archived_id);
450 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
d88744a4 451
92816cc3 452 DBG("[rotation-thread] Checking for pending rotation on session \"%s\", trace archive %" PRIu64,
d2956687
JG
453 session->name, chunk_being_archived_id);
454
faf1bdcf
JG
455 /*
456 * The rotation-pending check timer of a session is launched in
457 * one-shot mode. If the rotation is incomplete, the rotation
458 * thread will re-enable the pending-check timer.
459 *
460 * The timer thread can't stop the timer itself since it is involved
461 * in the check for the timer's quiescence.
462 */
463 ret = timer_session_rotation_pending_check_stop(session);
464 if (ret) {
6ae1bf46 465 goto check_ongoing_rotation;
faf1bdcf
JG
466 }
467
d2956687
JG
468 check_session_rotation_pending_on_consumers(session,
469 &rotation_completed);
d2956687
JG
470 if (!rotation_completed ||
471 session->rotation_state == LTTNG_ROTATION_STATE_ERROR) {
6ae1bf46 472 goto check_ongoing_rotation;
92816cc3
JG
473 }
474
92816cc3
JG
475 /*
476 * Now we can clear the "ONGOING" state in the session. New
477 * rotations can start now.
478 */
d2956687
JG
479 chunk_status = lttng_trace_chunk_get_name(session->chunk_being_archived,
480 &archived_chunk_name, NULL);
481 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
482 free(session->last_archived_chunk_name);
483 session->last_archived_chunk_name = strdup(archived_chunk_name);
484 if (!session->last_archived_chunk_name) {
485 PERROR("Failed to duplicate archived chunk name");
486 }
487 session_reset_rotation_state(session, LTTNG_ROTATION_STATE_COMPLETED);
92816cc3 488
7fdbed1c
JG
489 if (!session->quiet_rotation) {
490 location = session_get_trace_archive_location(session);
491 /* Ownership of location is transferred. */
492 ret = notification_thread_command_session_rotation_completed(
493 notification_thread_handle,
494 session->name,
495 session->uid,
496 session->gid,
497 session->last_archived_chunk_id.value,
498 location);
499 if (ret != LTTNG_OK) {
500 ERR("[rotation-thread] Failed to notify notification thread of completed rotation for session %s",
501 session->name);
502 }
92816cc3
JG
503 }
504
505 ret = 0;
6ae1bf46 506check_ongoing_rotation:
92816cc3 507 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
d2956687
JG
508 uint64_t chunk_being_archived_id;
509
510 chunk_status = lttng_trace_chunk_get_id(
511 session->chunk_being_archived,
512 &chunk_being_archived_id);
513 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
514
92816cc3 515 DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " is still pending for session %s",
d2956687 516 chunk_being_archived_id, session->name);
92816cc3
JG
517 ret = timer_session_rotation_pending_check_start(session,
518 DEFAULT_ROTATE_PENDING_TIMER);
519 if (ret) {
d2956687 520 ERR("Failed to re-enable rotation pending timer");
92816cc3
JG
521 ret = -1;
522 goto end;
523 }
524 }
525
6ae1bf46 526end:
d88744a4
JD
527 return ret;
528}
529
ed1e52a3 530/* Call with the session and session_list locks held. */
259c2674 531static
92816cc3 532int launch_session_rotation(struct ltt_session *session)
259c2674
JD
533{
534 int ret;
92816cc3 535 struct lttng_rotate_session_return rotation_return;
259c2674 536
92816cc3
JG
537 DBG("[rotation-thread] Launching scheduled time-based rotation on session \"%s\"",
538 session->name);
259c2674 539
343defc2
MD
540 ret = cmd_rotate_session(session, &rotation_return, false,
541 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
92816cc3
JG
542 if (ret == LTTNG_OK) {
543 DBG("[rotation-thread] Scheduled time-based rotation successfully launched on session \"%s\"",
544 session->name);
545 } else {
546 /* Don't consider errors as fatal. */
547 DBG("[rotation-thread] Scheduled time-based rotation aborted for session %s: %s",
548 session->name, lttng_strerror(ret));
259c2674 549 }
92816cc3
JG
550 return 0;
551}
259c2674 552
92816cc3
JG
553static
554int run_job(struct rotation_thread_job *job, struct ltt_session *session,
555 struct notification_thread_handle *notification_thread_handle)
556{
557 int ret;
259c2674 558
92816cc3
JG
559 switch (job->type) {
560 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION:
561 ret = launch_session_rotation(session);
562 break;
563 case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION:
564 ret = check_session_rotation_pending(session,
565 notification_thread_handle);
566 break;
567 default:
568 abort();
259c2674 569 }
259c2674
JD
570 return ret;
571}
572
d88744a4 573static
92816cc3
JG
574int handle_job_queue(struct rotation_thread_handle *handle,
575 struct rotation_thread *state,
d88744a4
JD
576 struct rotation_thread_timer_queue *queue)
577{
578 int ret = 0;
d88744a4
JD
579
580 for (;;) {
e32d7f27 581 struct ltt_session *session;
92816cc3 582 struct rotation_thread_job *job;
d88744a4 583
92816cc3 584 /* Take the queue lock only to pop an element from the list. */
d88744a4
JD
585 pthread_mutex_lock(&queue->lock);
586 if (cds_list_empty(&queue->list)) {
587 pthread_mutex_unlock(&queue->lock);
588 break;
589 }
92816cc3
JG
590 job = cds_list_first_entry(&queue->list,
591 typeof(*job), head);
592 cds_list_del(&job->head);
d88744a4
JD
593 pthread_mutex_unlock(&queue->lock);
594
d88744a4 595 session_lock_list();
c7031a2c 596 session = job->session;
d88744a4 597 if (!session) {
c7031a2c
JG
598 DBG("[rotation-thread] Session \"%s\" not found",
599 session->name);
d88744a4 600 /*
92816cc3
JG
601 * This is a non-fatal error, and we cannot report it to
602 * the user (timer), so just print the error and
603 * continue the processing.
604 *
605 * While the timer thread will purge pending signals for
606 * a session on the session's destruction, it is
607 * possible for a job targeting that session to have
608 * already been queued before it was destroyed.
d88744a4 609 */
92816cc3 610 free(job);
e32d7f27 611 session_put(session);
5b8139c6 612 session_unlock_list();
d88744a4
JD
613 continue;
614 }
615
d88744a4 616 session_lock(session);
92816cc3 617 ret = run_job(job, session, handle->notification_thread_handle);
d88744a4 618 session_unlock(session);
faf1bdcf 619 /* Release reference held by the job. */
e32d7f27 620 session_put(session);
ed1e52a3 621 session_unlock_list();
92816cc3 622 free(job);
d88744a4 623 if (ret) {
d88744a4
JD
624 goto end;
625 }
626 }
627
628 ret = 0;
629
630end:
631 return ret;
632}
633
92816cc3
JG
634static
635int handle_condition(const struct lttng_condition *condition,
90936dcf
JD
636 const struct lttng_evaluation *evaluation,
637 struct notification_thread_handle *notification_thread_handle)
638{
639 int ret = 0;
640 const char *condition_session_name = NULL;
641 enum lttng_condition_type condition_type;
642 enum lttng_condition_status condition_status;
643 enum lttng_evaluation_status evaluation_status;
644 uint64_t consumed;
645 struct ltt_session *session;
646
647 condition_type = lttng_condition_get_type(condition);
648
649 if (condition_type != LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE) {
650 ret = -1;
651 ERR("[rotation-thread] Condition type and session usage type are not the same");
652 goto end;
653 }
654
655 /* Fetch info to test */
656 condition_status = lttng_condition_session_consumed_size_get_session_name(
657 condition, &condition_session_name);
658 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
659 ERR("[rotation-thread] Session name could not be fetched");
660 ret = -1;
661 goto end;
662 }
663 evaluation_status = lttng_evaluation_session_consumed_size_get_consumed_size(evaluation,
664 &consumed);
665 if (evaluation_status != LTTNG_EVALUATION_STATUS_OK) {
666 ERR("[rotation-thread] Failed to get evaluation");
667 ret = -1;
668 goto end;
669 }
670
671 session_lock_list();
672 session = session_find_by_name(condition_session_name);
673 if (!session) {
674 ret = -1;
675 session_unlock_list();
676 ERR("[rotation-thread] Session \"%s\" not found",
677 condition_session_name);
678 goto end;
679 }
680 session_lock(session);
90936dcf
JD
681
682 ret = unsubscribe_session_consumed_size_rotation(session,
683 notification_thread_handle);
684 if (ret) {
490b3229 685 goto end_unlock;
90936dcf
JD
686 }
687
343defc2
MD
688 ret = cmd_rotate_session(session, NULL, false,
689 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
90936dcf
JD
690 if (ret == -LTTNG_ERR_ROTATION_PENDING) {
691 DBG("Rotate already pending, subscribe to the next threshold value");
90936dcf
JD
692 } else if (ret != LTTNG_OK) {
693 ERR("[rotation-thread] Failed to rotate on size notification with error: %s",
694 lttng_strerror(ret));
695 ret = -1;
696 goto end_unlock;
697 }
698 ret = subscribe_session_consumed_size_rotation(session,
699 consumed + session->rotate_size,
700 notification_thread_handle);
701 if (ret) {
702 ERR("[rotation-thread] Failed to subscribe to session consumed size condition");
703 goto end_unlock;
704 }
705 ret = 0;
706
707end_unlock:
708 session_unlock(session);
e32d7f27 709 session_put(session);
5b8139c6 710 session_unlock_list();
90936dcf
JD
711end:
712 return ret;
713}
714
715static
92816cc3 716int handle_notification_channel(int fd,
90936dcf 717 struct rotation_thread_handle *handle,
92816cc3 718 struct rotation_thread *state)
90936dcf
JD
719{
720 int ret;
d73ee93f
JG
721 bool notification_pending;
722 struct lttng_notification *notification = NULL;
90936dcf
JD
723 enum lttng_notification_channel_status status;
724 const struct lttng_evaluation *notification_evaluation;
725 const struct lttng_condition *notification_condition;
726
d73ee93f
JG
727 status = lttng_notification_channel_has_pending_notification(
728 rotate_notification_channel, &notification_pending);
729 if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
a9577b76 730 ERR("[rotation-thread ]Error occurred while checking for pending notification");
d73ee93f
JG
731 ret = -1;
732 goto end;
733 }
734
735 if (!notification_pending) {
736 ret = 0;
737 goto end;
738 }
739
90936dcf
JD
740 /* Receive the next notification. */
741 status = lttng_notification_channel_get_next_notification(
742 rotate_notification_channel,
743 &notification);
744
745 switch (status) {
746 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK:
747 break;
748 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED:
749 /* Not an error, we will wait for the next one */
750 ret = 0;
751 goto end;;
752 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED:
753 ERR("Notification channel was closed");
754 ret = -1;
755 goto end;
756 default:
757 /* Unhandled conditions / errors. */
758 ERR("Unknown notification channel status");
759 ret = -1;
760 goto end;
761 }
762
763 notification_condition = lttng_notification_get_condition(notification);
764 notification_evaluation = lttng_notification_get_evaluation(notification);
765
766 ret = handle_condition(notification_condition, notification_evaluation,
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.076786 seconds and 4 git commands to generate.