Fix: sessiond: ust session is inactive during ust_app_global_update
[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/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>
26 #include <lttng/rotate-internal.h>
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"
34 #include "timer.h"
35 #include "notification-thread-commands.h"
36 #include "utils.h"
37 #include "thread.h"
38
39 #include <urcu.h>
40 #include <urcu/list.h>
41
42 struct lttng_notification_channel *rotate_notification_channel = NULL;
43
44 struct rotation_thread {
45 struct lttng_poll_event events;
46 };
47
48 struct rotation_thread_job {
49 enum rotation_thread_job_type type;
50 struct ltt_session *session;
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 */
59 struct rotation_thread_timer_queue {
60 struct lttng_pipe *event_pipe;
61 struct cds_list_head list;
62 pthread_mutex_t lock;
63 };
64
65 struct rotation_thread_handle {
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;
69 /* Thread-specific quit pipe. */
70 struct lttng_pipe *quit_pipe;
71 };
72
73 static
74 const char *get_job_type_str(enum rotation_thread_job_type job_type)
75 {
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 }
84 }
85
86 struct rotation_thread_timer_queue *rotation_thread_timer_queue_create(void)
87 {
88 struct rotation_thread_timer_queue *queue = NULL;
89
90 queue = zmalloc(sizeof(*queue));
91 if (!queue) {
92 PERROR("Failed to allocate timer rotate queue");
93 goto end;
94 }
95
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);
99 end:
100 return queue;
101 }
102
103 void rotation_thread_timer_queue_destroy(
104 struct rotation_thread_timer_queue *queue)
105 {
106 if (!queue) {
107 return;
108 }
109
110 lttng_pipe_destroy(queue->event_pipe);
111
112 pthread_mutex_lock(&queue->lock);
113 assert(cds_list_empty(&queue->list));
114 pthread_mutex_unlock(&queue->lock);
115 pthread_mutex_destroy(&queue->lock);
116 free(queue);
117 }
118
119 /*
120 * Destroy the thread data previously created by the init function.
121 */
122 void rotation_thread_handle_destroy(
123 struct rotation_thread_handle *handle)
124 {
125 lttng_pipe_destroy(handle->quit_pipe);
126 free(handle);
127 }
128
129 struct rotation_thread_handle *rotation_thread_handle_create(
130 struct rotation_thread_timer_queue *rotation_timer_queue,
131 struct notification_thread_handle *notification_thread_handle)
132 {
133 struct rotation_thread_handle *handle;
134
135 handle = zmalloc(sizeof(*handle));
136 if (!handle) {
137 goto end;
138 }
139
140 handle->rotation_timer_queue = rotation_timer_queue;
141 handle->notification_thread_handle = notification_thread_handle;
142 handle->quit_pipe = lttng_pipe_open(FD_CLOEXEC);
143 if (!handle->quit_pipe) {
144 goto error;
145 }
146
147 end:
148 return handle;
149 error:
150 rotation_thread_handle_destroy(handle);
151 return NULL;
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 */
158 static
159 bool timer_job_exists(const struct rotation_thread_timer_queue *queue,
160 enum rotation_thread_job_type job_type,
161 struct ltt_session *session)
162 {
163 bool exists = false;
164 struct rotation_thread_job *job;
165
166 cds_list_for_each_entry(job, &queue->list, head) {
167 if (job->session == session && job->type == job_type) {
168 exists = true;
169 goto end;
170 }
171 }
172 end:
173 return exists;
174 }
175
176 void rotation_thread_enqueue_job(struct rotation_thread_timer_queue *queue,
177 enum rotation_thread_job_type job_type,
178 struct ltt_session *session)
179 {
180 int ret;
181 const char dummy = '!';
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);
186 if (timer_job_exists(queue, job_type, session)) {
187 /*
188 * This timer job is already pending, we don't need to add
189 * it.
190 */
191 goto end;
192 }
193
194 job = zmalloc(sizeof(struct rotation_thread_job));
195 if (!job) {
196 PERROR("Failed to allocate rotation thread job of type \"%s\" for session \"%s\"",
197 job_type_str, session->name);
198 goto end;
199 }
200 /* No reason for this to fail as the caller must hold a reference. */
201 (void) session_get(session);
202
203 job->session = session;
204 job->type = job_type;
205 cds_list_add_tail(&job->head, &queue->list);
206
207 ret = lttng_write(lttng_pipe_get_writefd(queue->event_pipe), &dummy,
208 sizeof(dummy));
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;
224 }
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);
227 goto end;
228 }
229
230 end:
231 pthread_mutex_unlock(&queue->lock);
232 }
233
234 static
235 int init_poll_set(struct lttng_poll_event *poll_set,
236 struct rotation_thread_handle *handle)
237 {
238 int ret;
239
240 /*
241 * Create pollset with size 3:
242 * - rotation thread quit pipe,
243 * - rotation thread timer queue pipe,
244 * - notification channel sock,
245 */
246 ret = lttng_poll_create(poll_set, 5, LTTNG_CLOEXEC);
247 if (ret < 0) {
248 goto error;
249 }
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("Failed to add quit pipe read fd to poll set");
256 goto error;
257 }
258
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) {
263 ERR("Failed to add rotate_pending fd to poll set");
264 goto error;
265 }
266
267 return ret;
268 error:
269 lttng_poll_clean(poll_set);
270 return ret;
271 }
272
273 static
274 void fini_thread_state(struct rotation_thread *state)
275 {
276 lttng_poll_clean(&state->events);
277 if (rotate_notification_channel) {
278 lttng_notification_channel_destroy(rotate_notification_channel);
279 }
280 }
281
282 static
283 int init_thread_state(struct rotation_thread_handle *handle,
284 struct rotation_thread *state)
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("Failed to initialize rotation thread poll set");
294 goto end;
295 }
296
297 rotate_notification_channel = lttng_notification_channel_create(
298 lttng_session_daemon_notification_endpoint);
299 if (!rotate_notification_channel) {
300 ERR("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("Failed to add notification fd to pollset");
308 goto end;
309 }
310
311 end:
312 return ret;
313 }
314
315 static
316 void check_session_rotation_pending_on_consumers(struct ltt_session *session,
317 bool *_rotation_completed)
318 {
319 int ret = 0;
320 struct consumer_socket *socket;
321 struct cds_lfht_iter iter;
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);
328
329 /*
330 * Check for a local pending rotation on all consumers (32-bit
331 * user space, 64-bit user space, and kernel).
332 */
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) {
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);
350 ERR("Error occurred while checking rotation status on consumer daemon");
351 goto end;
352 }
353
354 if (exists_status != CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK) {
355 pthread_mutex_unlock(socket->lock);
356 chunk_exists_on_peer = true;
357 goto end;
358 }
359 pthread_mutex_unlock(socket->lock);
360 }
361
362 skip_ust:
363 if (!session->kernel_session) {
364 goto skip_kernel;
365 }
366 cds_lfht_for_each_entry(session->kernel_session->consumer->socks->ht,
367 &iter, socket, node.node) {
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);
379 ERR("Error occurred while checking rotation status on consumer daemon");
380 goto end;
381 }
382
383 if (exists_status != CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK) {
384 pthread_mutex_unlock(socket->lock);
385 chunk_exists_on_peer = true;
386 goto end;
387 }
388 pthread_mutex_unlock(socket->lock);
389 }
390 skip_kernel:
391 end:
392 rcu_read_unlock();
393
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 of trace archive %" PRIu64 " of session \"%s\" is complete on all consumers",
402 chunk_being_archived_id,
403 session->name);
404 }
405 *_rotation_completed = !chunk_exists_on_peer;
406 if (ret) {
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 }
413 }
414 }
415
416 /*
417 * Check if the last rotation was completed, called with session lock held.
418 * Should only return non-zero in the event of a fatal error. Doing so will
419 * shutdown the thread.
420 */
421 static
422 int check_session_rotation_pending(struct ltt_session *session,
423 struct notification_thread_handle *notification_thread_handle)
424 {
425 int ret;
426 struct lttng_trace_archive_location *location;
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
432 if (!session->chunk_being_archived) {
433 ret = 0;
434 goto end;
435 }
436
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);
440
441 DBG("Checking for pending rotation on session \"%s\", trace archive %" PRIu64,
442 session->name, chunk_being_archived_id);
443
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) {
454 goto check_ongoing_rotation;
455 }
456
457 check_session_rotation_pending_on_consumers(session,
458 &rotation_completed);
459 if (!rotation_completed ||
460 session->rotation_state == LTTNG_ROTATION_STATE_ERROR) {
461 goto check_ongoing_rotation;
462 }
463
464 /*
465 * Now we can clear the "ONGOING" state in the session. New
466 * rotations can start now.
467 */
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);
477
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("Failed to notify notification thread of completed rotation for session %s",
490 session->name);
491 }
492 }
493
494 ret = 0;
495 check_ongoing_rotation:
496 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
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
502 DBG("Rotation of trace archive %" PRIu64 " is still pending for session %s",
503 chunk_being_archived_id, session->name);
504 ret = timer_session_rotation_pending_check_start(session,
505 DEFAULT_ROTATE_PENDING_TIMER);
506 if (ret) {
507 ERR("Failed to re-enable rotation pending timer");
508 ret = -1;
509 goto end;
510 }
511 }
512
513 end:
514 return ret;
515 }
516
517 /* Call with the session and session_list locks held. */
518 static
519 int launch_session_rotation(struct ltt_session *session)
520 {
521 int ret;
522 struct lttng_rotate_session_return rotation_return;
523
524 DBG("Launching scheduled time-based rotation on session \"%s\"",
525 session->name);
526
527 ret = cmd_rotate_session(session, &rotation_return, false,
528 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
529 if (ret == LTTNG_OK) {
530 DBG("Scheduled time-based rotation successfully launched on session \"%s\"",
531 session->name);
532 } else {
533 /* Don't consider errors as fatal. */
534 DBG("Scheduled time-based rotation aborted for session %s: %s",
535 session->name, lttng_strerror(ret));
536 }
537 return 0;
538 }
539
540 static
541 int run_job(struct rotation_thread_job *job, struct ltt_session *session,
542 struct notification_thread_handle *notification_thread_handle)
543 {
544 int ret;
545
546 switch (job->type) {
547 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION:
548 ret = launch_session_rotation(session);
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();
556 }
557 return ret;
558 }
559
560 static
561 int handle_job_queue(struct rotation_thread_handle *handle,
562 struct rotation_thread *state,
563 struct rotation_thread_timer_queue *queue)
564 {
565 int ret = 0;
566
567 for (;;) {
568 struct ltt_session *session;
569 struct rotation_thread_job *job;
570
571 /* Take the queue lock only to pop an element from the list. */
572 pthread_mutex_lock(&queue->lock);
573 if (cds_list_empty(&queue->list)) {
574 pthread_mutex_unlock(&queue->lock);
575 break;
576 }
577 job = cds_list_first_entry(&queue->list,
578 typeof(*job), head);
579 cds_list_del(&job->head);
580 pthread_mutex_unlock(&queue->lock);
581
582 session_lock_list();
583 session = job->session;
584 if (!session) {
585 DBG("Session \"%s\" not found",
586 session->name);
587 /*
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.
596 */
597 free(job);
598 session_put(session);
599 session_unlock_list();
600 continue;
601 }
602
603 session_lock(session);
604 ret = run_job(job, session, handle->notification_thread_handle);
605 session_unlock(session);
606 /* Release reference held by the job. */
607 session_put(session);
608 session_unlock_list();
609 free(job);
610 if (ret) {
611 goto end;
612 }
613 }
614
615 ret = 0;
616
617 end:
618 return ret;
619 }
620
621 static
622 int handle_condition(const struct lttng_condition *condition,
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("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("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("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("Session \"%s\" not found",
664 condition_session_name);
665 goto end;
666 }
667 session_lock(session);
668
669 ret = unsubscribe_session_consumed_size_rotation(session,
670 notification_thread_handle);
671 if (ret) {
672 goto end_unlock;
673 }
674
675 ret = cmd_rotate_session(session, NULL, false,
676 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
677 if (ret == -LTTNG_ERR_ROTATION_PENDING) {
678 DBG("Rotate already pending, subscribe to the next threshold value");
679 } else if (ret != LTTNG_OK) {
680 ERR("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("Failed to subscribe to session consumed size condition");
690 goto end_unlock;
691 }
692 ret = 0;
693
694 end_unlock:
695 session_unlock(session);
696 session_put(session);
697 session_unlock_list();
698 end:
699 return ret;
700 }
701
702 static
703 int handle_notification_channel(int fd,
704 struct rotation_thread_handle *handle,
705 struct rotation_thread *state)
706 {
707 int ret;
708 bool notification_pending;
709 struct lttng_notification *notification = NULL;
710 enum lttng_notification_channel_status status;
711 const struct lttng_evaluation *notification_evaluation;
712 const struct lttng_condition *notification_condition;
713
714 status = lttng_notification_channel_has_pending_notification(
715 rotate_notification_channel, &notification_pending);
716 if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
717 ERR("Error occurred while checking for pending notification");
718 ret = -1;
719 goto end;
720 }
721
722 if (!notification_pending) {
723 ret = 0;
724 goto end;
725 }
726
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
756 end:
757 lttng_notification_destroy(notification);
758 return ret;
759 }
760
761 static
762 void *thread_rotation(void *data)
763 {
764 int ret;
765 struct rotation_thread_handle *handle = data;
766 struct rotation_thread thread;
767 int queue_pipe_fd;
768
769 DBG("Started rotation thread");
770 rcu_register_thread();
771 rcu_thread_online();
772 health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_ROTATION);
773 health_code_update();
774
775 if (!handle) {
776 ERR("Invalid thread context provided");
777 goto end;
778 }
779
780 queue_pipe_fd = lttng_pipe_get_readfd(
781 handle->rotation_timer_queue->event_pipe);
782
783
784 ret = init_thread_state(handle, &thread);
785 if (ret) {
786 goto error;
787 }
788
789 while (true) {
790 int fd_count, i;
791
792 health_poll_entry();
793 DBG("Entering poll wait");
794 ret = lttng_poll_wait(&thread.events, -1);
795 DBG("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("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++) {
810 int fd = LTTNG_POLL_GETFD(&thread.events, i);
811 uint32_t revents = LTTNG_POLL_GETEV(&thread.events, i);
812
813 DBG("Handling fd (%i) activity (%u)",
814 fd, revents);
815
816 if (revents & LPOLLERR) {
817 ERR("Polling returned an error on fd %i", fd);
818 goto error;
819 }
820
821 if (fd == rotate_notification_channel->socket) {
822 ret = handle_notification_channel(fd, handle,
823 &thread);
824 if (ret) {
825 ERR("Error occurred while handling activity on notification channel socket");
826 goto error;
827 }
828 } else {
829 /* Job queue or quit pipe activity. */
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 */
837 ret = handle_job_queue(handle, &thread,
838 handle->rotation_timer_queue);
839 if (ret) {
840 ERR("Failed to handle rotation timer pipe event");
841 goto error;
842 }
843
844 if (fd == queue_pipe_fd) {
845 char buf;
846
847 ret = lttng_read(fd, &buf, 1);
848 if (ret != 1) {
849 ERR("Failed to read from wakeup pipe (fd = %i)", fd);
850 goto error;
851 }
852 } else {
853 DBG("Quit pipe activity");
854 goto exit;
855 }
856 }
857 }
858 }
859 exit:
860 error:
861 DBG("Thread exit");
862 fini_thread_state(&thread);
863 end:
864 health_unregister(the_health_sessiond);
865 rcu_thread_offline();
866 rcu_unregister_thread();
867 return NULL;
868 }
869
870 static
871 bool 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
879 bool 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;
893 error:
894 return false;
895 }
This page took 0.048374 seconds and 4 git commands to generate.