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