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