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