fcbd83fb37b53e60c0bfe7fa47133b09b4f70d48
[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 * 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>
37 #include <lttng/rotate-internal.h>
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"
45 #include "timer.h"
46 #include "notification-thread-commands.h"
47 #include "utils.h"
48 #include "thread.h"
49
50 #include <urcu.h>
51 #include <urcu/list.h>
52
53 struct lttng_notification_channel *rotate_notification_channel = NULL;
54
55 struct rotation_thread {
56 struct lttng_poll_event events;
57 };
58
59 struct rotation_thread_job {
60 enum rotation_thread_job_type type;
61 struct ltt_session *session;
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 */
70 struct rotation_thread_timer_queue {
71 struct lttng_pipe *event_pipe;
72 struct cds_list_head list;
73 pthread_mutex_t lock;
74 };
75
76 struct rotation_thread_handle {
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;
80 /* Thread-specific quit pipe. */
81 struct lttng_pipe *quit_pipe;
82 };
83
84 static
85 const char *get_job_type_str(enum rotation_thread_job_type job_type)
86 {
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 }
95 }
96
97 struct rotation_thread_timer_queue *rotation_thread_timer_queue_create(void)
98 {
99 struct rotation_thread_timer_queue *queue = NULL;
100
101 queue = zmalloc(sizeof(*queue));
102 if (!queue) {
103 PERROR("Failed to allocate timer rotate queue");
104 goto end;
105 }
106
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);
110 end:
111 return queue;
112 }
113
114 void log_job_destruction(const struct rotation_thread_job *job)
115 {
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();
135 }
136
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);
139 }
140
141 void rotation_thread_timer_queue_destroy(
142 struct rotation_thread_timer_queue *queue)
143 {
144 if (!queue) {
145 return;
146 }
147
148 lttng_pipe_destroy(queue->event_pipe);
149
150 pthread_mutex_lock(&queue->lock);
151 assert(cds_list_empty(&queue->list));
152 pthread_mutex_unlock(&queue->lock);
153 pthread_mutex_destroy(&queue->lock);
154 free(queue);
155 }
156
157 /*
158 * Destroy the thread data previously created by the init function.
159 */
160 void rotation_thread_handle_destroy(
161 struct rotation_thread_handle *handle)
162 {
163 lttng_pipe_destroy(handle->quit_pipe);
164 free(handle);
165 }
166
167 struct rotation_thread_handle *rotation_thread_handle_create(
168 struct rotation_thread_timer_queue *rotation_timer_queue,
169 struct notification_thread_handle *notification_thread_handle)
170 {
171 struct rotation_thread_handle *handle;
172
173 handle = zmalloc(sizeof(*handle));
174 if (!handle) {
175 goto end;
176 }
177
178 handle->rotation_timer_queue = rotation_timer_queue;
179 handle->notification_thread_handle = notification_thread_handle;
180 handle->quit_pipe = lttng_pipe_open(FD_CLOEXEC);
181 if (!handle->quit_pipe) {
182 goto error;
183 }
184
185 end:
186 return handle;
187 error:
188 rotation_thread_handle_destroy(handle);
189 return NULL;
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 */
196 static
197 bool timer_job_exists(const struct rotation_thread_timer_queue *queue,
198 enum rotation_thread_job_type job_type,
199 struct ltt_session *session)
200 {
201 bool exists = false;
202 struct rotation_thread_job *job;
203
204 cds_list_for_each_entry(job, &queue->list, head) {
205 if (job->session == session && job->type == job_type) {
206 exists = true;
207 goto end;
208 }
209 }
210 end:
211 return exists;
212 }
213
214 void rotation_thread_enqueue_job(struct rotation_thread_timer_queue *queue,
215 enum rotation_thread_job_type job_type,
216 struct ltt_session *session)
217 {
218 int ret;
219 const char dummy = '!';
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);
224 if (timer_job_exists(queue, job_type, session)) {
225 /*
226 * This timer job is already pending, we don't need to add
227 * it.
228 */
229 goto end;
230 }
231
232 job = zmalloc(sizeof(struct rotation_thread_job));
233 if (!job) {
234 PERROR("Failed to allocate rotation thread job of type \"%s\" for session \"%s\"",
235 job_type_str, session->name);
236 goto end;
237 }
238 /* No reason for this to fail as the caller must hold a reference. */
239 (void) session_get(session);
240
241 job->session = session;
242 job->type = job_type;
243 cds_list_add_tail(&job->head, &queue->list);
244
245 ret = lttng_write(lttng_pipe_get_writefd(queue->event_pipe), &dummy,
246 sizeof(dummy));
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;
262 }
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);
265 goto end;
266 }
267
268 end:
269 pthread_mutex_unlock(&queue->lock);
270 }
271
272 static
273 int init_poll_set(struct lttng_poll_event *poll_set,
274 struct rotation_thread_handle *handle)
275 {
276 int ret;
277
278 /*
279 * Create pollset with size 3:
280 * - rotation thread quit pipe,
281 * - rotation thread timer queue pipe,
282 * - notification channel sock,
283 */
284 ret = lttng_poll_create(poll_set, 5, LTTNG_CLOEXEC);
285 if (ret < 0) {
286 goto error;
287 }
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
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) {
301 ERR("[rotation-thread] Failed to add rotate_pending fd to poll set");
302 goto error;
303 }
304
305 return ret;
306 error:
307 lttng_poll_clean(poll_set);
308 return ret;
309 }
310
311 static
312 void fini_thread_state(struct rotation_thread *state)
313 {
314 lttng_poll_clean(&state->events);
315 if (rotate_notification_channel) {
316 lttng_notification_channel_destroy(rotate_notification_channel);
317 }
318 }
319
320 static
321 int init_thread_state(struct rotation_thread_handle *handle,
322 struct rotation_thread *state)
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
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
349 end:
350 return ret;
351 }
352
353 static
354 void check_session_rotation_pending_on_consumers(struct ltt_session *session,
355 bool *_rotation_completed)
356 {
357 int ret = 0;
358 struct consumer_socket *socket;
359 struct cds_lfht_iter iter;
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);
366
367 /*
368 * Check for a local pending rotation on all consumers (32-bit
369 * user space, 64-bit user space, and kernel).
370 */
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) {
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 occurred while checking rotation status on consumer daemon");
389 goto end;
390 }
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 }
399
400 skip_ust:
401 if (!session->kernel_session) {
402 goto skip_kernel;
403 }
404 cds_lfht_for_each_entry(session->kernel_session->consumer->socks->ht,
405 &iter, socket, node.node) {
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 occurred while checking rotation status on consumer daemon");
418 goto end;
419 }
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);
427 }
428 skip_kernel:
429 end:
430 rcu_read_unlock();
431
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,
441 session->name);
442 }
443 *_rotation_completed = !chunk_exists_on_peer;
444 if (ret) {
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 }
451 }
452 }
453
454 /*
455 * Check if the last rotation was completed, called with session lock held.
456 * Should only return non-zero in the event of a fatal error. Doing so will
457 * shutdown the thread.
458 */
459 static
460 int check_session_rotation_pending(struct ltt_session *session,
461 struct notification_thread_handle *notification_thread_handle)
462 {
463 int ret;
464 struct lttng_trace_archive_location *location;
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 if (!session->chunk_being_archived) {
471 ret = 0;
472 goto end;
473 }
474
475 chunk_status = lttng_trace_chunk_get_id(session->chunk_being_archived,
476 &chunk_being_archived_id);
477 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
478
479 DBG("[rotation-thread] Checking for pending rotation on session \"%s\", trace archive %" PRIu64,
480 session->name, chunk_being_archived_id);
481
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 check_ongoing_rotation;
493 }
494
495 check_session_rotation_pending_on_consumers(session,
496 &rotation_completed);
497 if (!rotation_completed ||
498 session->rotation_state == LTTNG_ROTATION_STATE_ERROR) {
499 goto check_ongoing_rotation;
500 }
501
502 /*
503 * Now we can clear the "ONGOING" state in the session. New
504 * rotations can start now.
505 */
506 chunk_status = lttng_trace_chunk_get_name(session->chunk_being_archived,
507 &archived_chunk_name, NULL);
508 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
509 free(session->last_archived_chunk_name);
510 session->last_archived_chunk_name = strdup(archived_chunk_name);
511 if (!session->last_archived_chunk_name) {
512 PERROR("Failed to duplicate archived chunk name");
513 }
514 session_reset_rotation_state(session, LTTNG_ROTATION_STATE_COMPLETED);
515
516 if (!session->quiet_rotation) {
517 location = session_get_trace_archive_location(session);
518 /* Ownership of location is transferred. */
519 ret = notification_thread_command_session_rotation_completed(
520 notification_thread_handle,
521 session->name,
522 session->uid,
523 session->gid,
524 session->last_archived_chunk_id.value,
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 }
531
532 ret = 0;
533 check_ongoing_rotation:
534 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
535 uint64_t chunk_being_archived_id;
536
537 chunk_status = lttng_trace_chunk_get_id(
538 session->chunk_being_archived,
539 &chunk_being_archived_id);
540 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
541
542 DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " is still pending for session %s",
543 chunk_being_archived_id, session->name);
544 ret = timer_session_rotation_pending_check_start(session,
545 DEFAULT_ROTATE_PENDING_TIMER);
546 if (ret) {
547 ERR("Failed to re-enable rotation pending timer");
548 ret = -1;
549 goto end;
550 }
551 }
552
553 end:
554 return ret;
555 }
556
557 /* Call with the session and session_list locks held. */
558 static
559 int launch_session_rotation(struct ltt_session *session)
560 {
561 int ret;
562 struct lttng_rotate_session_return rotation_return;
563
564 DBG("[rotation-thread] Launching scheduled time-based rotation on session \"%s\"",
565 session->name);
566
567 ret = cmd_rotate_session(session, &rotation_return, false);
568 if (ret == LTTNG_OK) {
569 DBG("[rotation-thread] Scheduled time-based rotation successfully launched on session \"%s\"",
570 session->name);
571 } else {
572 /* Don't consider errors as fatal. */
573 DBG("[rotation-thread] Scheduled time-based rotation aborted for session %s: %s",
574 session->name, lttng_strerror(ret));
575 }
576 return 0;
577 }
578
579 static
580 int run_job(struct rotation_thread_job *job, struct ltt_session *session,
581 struct notification_thread_handle *notification_thread_handle)
582 {
583 int ret;
584
585 switch (job->type) {
586 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION:
587 ret = launch_session_rotation(session);
588 break;
589 case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION:
590 ret = check_session_rotation_pending(session,
591 notification_thread_handle);
592 break;
593 default:
594 abort();
595 }
596 return ret;
597 }
598
599 static
600 int handle_job_queue(struct rotation_thread_handle *handle,
601 struct rotation_thread *state,
602 struct rotation_thread_timer_queue *queue)
603 {
604 int ret = 0;
605
606 for (;;) {
607 struct ltt_session *session;
608 struct rotation_thread_job *job;
609
610 /* Take the queue lock only to pop an element from the list. */
611 pthread_mutex_lock(&queue->lock);
612 if (cds_list_empty(&queue->list)) {
613 pthread_mutex_unlock(&queue->lock);
614 break;
615 }
616 job = cds_list_first_entry(&queue->list,
617 typeof(*job), head);
618 cds_list_del(&job->head);
619 pthread_mutex_unlock(&queue->lock);
620
621 session_lock_list();
622 session = job->session;
623 if (!session) {
624 DBG("[rotation-thread] Session \"%s\" not found",
625 session->name);
626 /*
627 * This is a non-fatal error, and we cannot report it to
628 * the user (timer), so just print the error and
629 * continue the processing.
630 *
631 * While the timer thread will purge pending signals for
632 * a session on the session's destruction, it is
633 * possible for a job targeting that session to have
634 * already been queued before it was destroyed.
635 */
636 free(job);
637 session_put(session);
638 session_unlock_list();
639 continue;
640 }
641
642 session_lock(session);
643 ret = run_job(job, session, handle->notification_thread_handle);
644 session_unlock(session);
645 /* Release reference held by the job. */
646 session_put(session);
647 session_unlock_list();
648 free(job);
649 if (ret) {
650 goto end;
651 }
652 }
653
654 ret = 0;
655
656 end:
657 return ret;
658 }
659
660 static
661 int handle_condition(const struct lttng_condition *condition,
662 const struct lttng_evaluation *evaluation,
663 struct notification_thread_handle *notification_thread_handle)
664 {
665 int ret = 0;
666 const char *condition_session_name = NULL;
667 enum lttng_condition_type condition_type;
668 enum lttng_condition_status condition_status;
669 enum lttng_evaluation_status evaluation_status;
670 uint64_t consumed;
671 struct ltt_session *session;
672
673 condition_type = lttng_condition_get_type(condition);
674
675 if (condition_type != LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE) {
676 ret = -1;
677 ERR("[rotation-thread] Condition type and session usage type are not the same");
678 goto end;
679 }
680
681 /* Fetch info to test */
682 condition_status = lttng_condition_session_consumed_size_get_session_name(
683 condition, &condition_session_name);
684 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
685 ERR("[rotation-thread] Session name could not be fetched");
686 ret = -1;
687 goto end;
688 }
689 evaluation_status = lttng_evaluation_session_consumed_size_get_consumed_size(evaluation,
690 &consumed);
691 if (evaluation_status != LTTNG_EVALUATION_STATUS_OK) {
692 ERR("[rotation-thread] Failed to get evaluation");
693 ret = -1;
694 goto end;
695 }
696
697 session_lock_list();
698 session = session_find_by_name(condition_session_name);
699 if (!session) {
700 ret = -1;
701 session_unlock_list();
702 ERR("[rotation-thread] Session \"%s\" not found",
703 condition_session_name);
704 goto end;
705 }
706 session_lock(session);
707
708 ret = unsubscribe_session_consumed_size_rotation(session,
709 notification_thread_handle);
710 if (ret) {
711 goto end_unlock;
712 }
713
714 ret = cmd_rotate_session(session, NULL, false);
715 if (ret == -LTTNG_ERR_ROTATION_PENDING) {
716 DBG("Rotate already pending, subscribe to the next threshold value");
717 } else if (ret != LTTNG_OK) {
718 ERR("[rotation-thread] Failed to rotate on size notification with error: %s",
719 lttng_strerror(ret));
720 ret = -1;
721 goto end_unlock;
722 }
723 ret = subscribe_session_consumed_size_rotation(session,
724 consumed + session->rotate_size,
725 notification_thread_handle);
726 if (ret) {
727 ERR("[rotation-thread] Failed to subscribe to session consumed size condition");
728 goto end_unlock;
729 }
730 ret = 0;
731
732 end_unlock:
733 session_unlock(session);
734 session_put(session);
735 session_unlock_list();
736 end:
737 return ret;
738 }
739
740 static
741 int handle_notification_channel(int fd,
742 struct rotation_thread_handle *handle,
743 struct rotation_thread *state)
744 {
745 int ret;
746 bool notification_pending;
747 struct lttng_notification *notification = NULL;
748 enum lttng_notification_channel_status status;
749 const struct lttng_evaluation *notification_evaluation;
750 const struct lttng_condition *notification_condition;
751
752 status = lttng_notification_channel_has_pending_notification(
753 rotate_notification_channel, &notification_pending);
754 if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
755 ERR("[rotation-thread ]Error occurred while checking for pending notification");
756 ret = -1;
757 goto end;
758 }
759
760 if (!notification_pending) {
761 ret = 0;
762 goto end;
763 }
764
765 /* Receive the next notification. */
766 status = lttng_notification_channel_get_next_notification(
767 rotate_notification_channel,
768 &notification);
769
770 switch (status) {
771 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK:
772 break;
773 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED:
774 /* Not an error, we will wait for the next one */
775 ret = 0;
776 goto end;;
777 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED:
778 ERR("Notification channel was closed");
779 ret = -1;
780 goto end;
781 default:
782 /* Unhandled conditions / errors. */
783 ERR("Unknown notification channel status");
784 ret = -1;
785 goto end;
786 }
787
788 notification_condition = lttng_notification_get_condition(notification);
789 notification_evaluation = lttng_notification_get_evaluation(notification);
790
791 ret = handle_condition(notification_condition, notification_evaluation,
792 handle->notification_thread_handle);
793
794 end:
795 lttng_notification_destroy(notification);
796 return ret;
797 }
798
799 void *thread_rotation(void *data)
800 {
801 int ret;
802 struct rotation_thread_handle *handle = data;
803 struct rotation_thread thread;
804 int queue_pipe_fd;
805
806 DBG("[rotation-thread] Started rotation thread");
807 rcu_register_thread();
808 rcu_thread_online();
809 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_ROTATION);
810 health_code_update();
811
812 if (!handle) {
813 ERR("[rotation-thread] Invalid thread context provided");
814 goto end;
815 }
816
817 queue_pipe_fd = lttng_pipe_get_readfd(
818 handle->rotation_timer_queue->event_pipe);
819
820
821 ret = init_thread_state(handle, &thread);
822 if (ret) {
823 goto error;
824 }
825
826 while (true) {
827 int fd_count, i;
828
829 health_poll_entry();
830 DBG("[rotation-thread] Entering poll wait");
831 ret = lttng_poll_wait(&thread.events, -1);
832 DBG("[rotation-thread] Poll wait returned (%i)", ret);
833 health_poll_exit();
834 if (ret < 0) {
835 /*
836 * Restart interrupted system call.
837 */
838 if (errno == EINTR) {
839 continue;
840 }
841 ERR("[rotation-thread] Error encountered during lttng_poll_wait (%i)", ret);
842 goto error;
843 }
844
845 fd_count = ret;
846 for (i = 0; i < fd_count; i++) {
847 int fd = LTTNG_POLL_GETFD(&thread.events, i);
848 uint32_t revents = LTTNG_POLL_GETEV(&thread.events, i);
849
850 DBG("[rotation-thread] Handling fd (%i) activity (%u)",
851 fd, revents);
852
853 if (revents & LPOLLERR) {
854 ERR("[rotation-thread] Polling returned an error on fd %i", fd);
855 goto error;
856 }
857
858 if (fd == rotate_notification_channel->socket) {
859 ret = handle_notification_channel(fd, handle,
860 &thread);
861 if (ret) {
862 ERR("[rotation-thread] Error occurred while handling activity on notification channel socket");
863 goto error;
864 }
865 } else {
866 /* Job queue or quit pipe activity. */
867
868 /*
869 * The job queue is serviced if there is
870 * activity on the quit pipe to ensure it is
871 * flushed and all references held in the queue
872 * are released.
873 */
874 ret = handle_job_queue(handle, &thread,
875 handle->rotation_timer_queue);
876 if (ret) {
877 ERR("[rotation-thread] Failed to handle rotation timer pipe event");
878 goto error;
879 }
880
881 if (fd == queue_pipe_fd) {
882 char buf;
883
884 ret = lttng_read(fd, &buf, 1);
885 if (ret != 1) {
886 ERR("[rotation-thread] Failed to read from wakeup pipe (fd = %i)", fd);
887 goto error;
888 }
889 } else {
890 DBG("[rotation-thread] Quit pipe activity");
891 goto exit;
892 }
893 }
894 }
895 }
896 exit:
897 error:
898 DBG("[rotation-thread] Exit");
899 fini_thread_state(&thread);
900 end:
901 health_unregister(health_sessiond);
902 rcu_thread_offline();
903 rcu_unregister_thread();
904 return NULL;
905 }
906
907 static
908 bool shutdown_rotation_thread(void *thread_data)
909 {
910 struct rotation_thread_handle *handle = thread_data;
911 const int write_fd = lttng_pipe_get_writefd(handle->quit_pipe);
912
913 return notify_thread_pipe(write_fd) == 1;
914 }
915
916 bool launch_rotation_thread(struct rotation_thread_handle *handle)
917 {
918 struct lttng_thread *thread;
919
920 thread = lttng_thread_create("Rotation",
921 thread_rotation,
922 shutdown_rotation_thread,
923 NULL,
924 handle);
925 if (!thread) {
926 goto error;
927 }
928 lttng_thread_put(thread);
929 return true;
930 error:
931 return false;
932 }
This page took 0.046044 seconds and 3 git commands to generate.