Cleanup: rotation-thread: enforce conding standard following fix
[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 "cmd.hpp"
11 #include "health-sessiond.hpp"
12 #include "lttng-sessiond.hpp"
13 #include "notification-thread-commands.hpp"
14 #include "rotation-thread.hpp"
15 #include "session.hpp"
16 #include "thread.hpp"
17 #include "timer.hpp"
18 #include "utils.hpp"
19
20 #include <common/align.hpp>
21 #include <common/config/session-config.hpp>
22 #include <common/defaults.hpp>
23 #include <common/error.hpp>
24 #include <common/eventfd.hpp>
25 #include <common/exception.hpp>
26 #include <common/file-descriptor.hpp>
27 #include <common/format.hpp>
28 #include <common/futex.hpp>
29 #include <common/hashtable/utils.hpp>
30 #include <common/kernel-ctl/kernel-ctl.hpp>
31 #include <common/locked-reference.hpp>
32 #include <common/make-unique-wrapper.hpp>
33 #include <common/pthread-lock.hpp>
34 #include <common/scope-exit.hpp>
35 #include <common/time.hpp>
36 #include <common/urcu.hpp>
37 #include <common/utils.hpp>
38
39 #include <lttng/action/action-internal.hpp>
40 #include <lttng/condition/condition-internal.hpp>
41 #include <lttng/location-internal.hpp>
42 #include <lttng/notification/channel-internal.hpp>
43 #include <lttng/notification/notification-internal.hpp>
44 #include <lttng/rotate-internal.hpp>
45 #include <lttng/trigger/trigger.h>
46
47 #include <inttypes.h>
48 #include <memory>
49 #include <signal.h>
50 #include <sys/eventfd.h>
51 #include <sys/stat.h>
52 #include <time.h>
53 #include <urcu.h>
54 #include <urcu/list.h>
55
56 namespace ls = lttng::sessiond;
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 ls::rotation_thread_timer_queue {
63 struct lttng_pipe *event_pipe;
64 struct cds_list_head list;
65 pthread_mutex_t lock;
66 };
67
68 namespace {
69 struct rotation_thread_job {
70 using uptr = std::unique_ptr<
71 rotation_thread_job,
72 lttng::details::create_unique_class<rotation_thread_job, lttng::free>>;
73
74 enum ls::rotation_thread_job_type type;
75 struct ltt_session *session;
76 /* List member in struct rotation_thread_timer_queue. */
77 struct cds_list_head head;
78 };
79
80 const char *get_job_type_str(enum ls::rotation_thread_job_type job_type)
81 {
82 switch (job_type) {
83 case ls::rotation_thread_job_type::CHECK_PENDING_ROTATION:
84 return "CHECK_PENDING_ROTATION";
85 case ls::rotation_thread_job_type::SCHEDULED_ROTATION:
86 return "SCHEDULED_ROTATION";
87 default:
88 abort();
89 }
90 }
91
92 /*
93 * Called with the rotation_thread_timer_queue lock held.
94 * Return true if the same timer job already exists in the queue, false if not.
95 */
96 bool timer_job_exists(const ls::rotation_thread_timer_queue *queue,
97 ls::rotation_thread_job_type job_type,
98 ltt_session *session)
99 {
100 bool exists = false;
101 struct rotation_thread_job *job;
102
103 cds_list_for_each_entry (job, &queue->list, head) {
104 if (job->session == session && job->type == job_type) {
105 exists = true;
106 goto end;
107 }
108 }
109 end:
110 return exists;
111 }
112
113 void check_session_rotation_pending_on_consumers(ltt_session& session, bool& _rotation_completed)
114 {
115 int ret = 0;
116 struct consumer_socket *socket;
117 struct cds_lfht_iter iter;
118 enum consumer_trace_chunk_exists_status exists_status;
119 uint64_t relayd_id;
120 bool chunk_exists_on_peer = false;
121 enum lttng_trace_chunk_status chunk_status;
122 lttng::urcu::read_lock_guard read_lock;
123
124 LTTNG_ASSERT(session.chunk_being_archived);
125
126 /*
127 * Check for a local pending rotation on all consumers (32-bit
128 * user space, 64-bit user space, and kernel).
129 */
130 if (!session.ust_session) {
131 goto skip_ust;
132 }
133
134 cds_lfht_for_each_entry (
135 session.ust_session->consumer->socks->ht, &iter, socket, node.node) {
136 relayd_id = session.ust_session->consumer->type == CONSUMER_DST_LOCAL ?
137 -1ULL :
138 session.ust_session->consumer->net_seq_index;
139
140 lttng::pthread::lock_guard socket_lock(*socket->lock);
141 ret = consumer_trace_chunk_exists(socket,
142 relayd_id,
143 session.id,
144 session.chunk_being_archived,
145 &exists_status);
146 if (ret) {
147 ERR("Error occurred while checking rotation status on consumer daemon");
148 goto end;
149 }
150
151 if (exists_status != CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK) {
152 chunk_exists_on_peer = true;
153 goto end;
154 }
155 }
156
157 skip_ust:
158 if (!session.kernel_session) {
159 goto skip_kernel;
160 }
161
162 cds_lfht_for_each_entry (
163 session.kernel_session->consumer->socks->ht, &iter, socket, node.node) {
164 lttng::pthread::lock_guard socket_lock(*socket->lock);
165
166 relayd_id = session.kernel_session->consumer->type == CONSUMER_DST_LOCAL ?
167 -1ULL :
168 session.kernel_session->consumer->net_seq_index;
169
170 ret = consumer_trace_chunk_exists(socket,
171 relayd_id,
172 session.id,
173 session.chunk_being_archived,
174 &exists_status);
175 if (ret) {
176 ERR("Error occurred while checking rotation status on consumer daemon");
177 goto end;
178 }
179
180 if (exists_status != CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK) {
181 chunk_exists_on_peer = true;
182 goto end;
183 }
184 }
185 skip_kernel:
186 end:
187
188 if (!chunk_exists_on_peer) {
189 uint64_t chunk_being_archived_id;
190
191 chunk_status = lttng_trace_chunk_get_id(session.chunk_being_archived,
192 &chunk_being_archived_id);
193 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
194 DBG("Rotation of trace archive %" PRIu64
195 " of session \"%s\" is complete on all consumers",
196 chunk_being_archived_id,
197 session.name);
198 }
199
200 _rotation_completed = !chunk_exists_on_peer;
201 if (ret) {
202 ret = session_reset_rotation_state(session, LTTNG_ROTATION_STATE_ERROR);
203 if (ret) {
204 ERR("Failed to reset rotation state of session \"%s\"", session.name);
205 }
206 }
207 }
208
209 /*
210 * Check if the last rotation was completed, called with session lock held.
211 * Should only return non-zero in the event of a fatal error. Doing so will
212 * shutdown the thread.
213 */
214 int check_session_rotation_pending(ltt_session& session,
215 notification_thread_handle& notification_thread_handle)
216 {
217 int ret;
218 struct lttng_trace_archive_location *location;
219 enum lttng_trace_chunk_status chunk_status;
220 bool rotation_completed = false;
221 const char *archived_chunk_name;
222 uint64_t chunk_being_archived_id;
223
224 if (!session.chunk_being_archived) {
225 ret = 0;
226 goto end;
227 }
228
229 chunk_status =
230 lttng_trace_chunk_get_id(session.chunk_being_archived, &chunk_being_archived_id);
231 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
232
233 DBG("Checking for pending rotation on session \"%s\", trace archive %" PRIu64,
234 session.name,
235 chunk_being_archived_id);
236
237 /*
238 * The rotation-pending check timer of a session is launched in
239 * one-shot mode. If the rotation is incomplete, the rotation
240 * thread will re-enable the pending-check timer.
241 *
242 * The timer thread can't stop the timer itself since it is involved
243 * in the check for the timer's quiescence.
244 */
245 ret = timer_session_rotation_pending_check_stop(session);
246 if (ret) {
247 goto check_ongoing_rotation;
248 }
249
250 check_session_rotation_pending_on_consumers(session, rotation_completed);
251 if (!rotation_completed || session.rotation_state == LTTNG_ROTATION_STATE_ERROR) {
252 goto check_ongoing_rotation;
253 }
254
255 /*
256 * Now we can clear the "ONGOING" state in the session. New
257 * rotations can start now.
258 */
259 chunk_status = lttng_trace_chunk_get_name(
260 session.chunk_being_archived, &archived_chunk_name, nullptr);
261 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
262 free(session.last_archived_chunk_name);
263 session.last_archived_chunk_name = strdup(archived_chunk_name);
264 if (!session.last_archived_chunk_name) {
265 PERROR("Failed to duplicate archived chunk name");
266 }
267
268 session_reset_rotation_state(session, LTTNG_ROTATION_STATE_COMPLETED);
269
270 if (!session.quiet_rotation) {
271 location = session_get_trace_archive_location(&session);
272 ret = notification_thread_command_session_rotation_completed(
273 &notification_thread_handle,
274 session.id,
275 session.last_archived_chunk_id.value,
276 location);
277 lttng_trace_archive_location_put(location);
278 if (ret != LTTNG_OK) {
279 ERR("Failed to notify notification thread of completed rotation for session %s",
280 session.name);
281 }
282 }
283
284 ret = 0;
285 check_ongoing_rotation:
286 if (session.rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
287 chunk_status = lttng_trace_chunk_get_id(session.chunk_being_archived,
288 &chunk_being_archived_id);
289 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
290
291 DBG("Rotation of trace archive %" PRIu64 " is still pending for session %s",
292 chunk_being_archived_id,
293 session.name);
294 ret = timer_session_rotation_pending_check_start(&session,
295 DEFAULT_ROTATE_PENDING_TIMER);
296 if (ret) {
297 ERR("Failed to re-enable rotation pending timer");
298 ret = -1;
299 goto end;
300 }
301 }
302
303 end:
304 return ret;
305 }
306
307 /* Call with the session and session_list locks held. */
308 int launch_session_rotation(ltt_session& session)
309 {
310 int ret;
311 struct lttng_rotate_session_return rotation_return;
312
313 DBG("Launching scheduled time-based rotation on session \"%s\"", session.name);
314
315 ASSERT_SESSION_LIST_LOCKED();
316 ASSERT_LOCKED(session.lock);
317
318 ret = cmd_rotate_session(&session,
319 &rotation_return,
320 false,
321 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
322 if (ret == LTTNG_OK) {
323 DBG("Scheduled time-based rotation successfully launched on session \"%s\"",
324 session.name);
325 } else {
326 /* Don't consider errors as fatal. */
327 DBG("Scheduled time-based rotation aborted for session %s: %s",
328 session.name,
329 lttng_strerror(ret));
330 }
331
332 return 0;
333 }
334
335 int run_job(const rotation_thread_job& job,
336 ltt_session& session,
337 notification_thread_handle& notification_thread_handle)
338 {
339 int ret;
340
341 switch (job.type) {
342 case ls::rotation_thread_job_type::SCHEDULED_ROTATION:
343 ret = launch_session_rotation(session);
344 break;
345 case ls::rotation_thread_job_type::CHECK_PENDING_ROTATION:
346 ret = check_session_rotation_pending(session, notification_thread_handle);
347 break;
348 default:
349 abort();
350 }
351
352 return ret;
353 }
354
355 bool shutdown_rotation_thread(void *thread_data)
356 {
357 auto *handle = reinterpret_cast<const ls::rotation_thread *>(thread_data);
358
359 return handle->shutdown();
360 }
361 } /* namespace */
362
363 ls::rotation_thread_timer_queue *ls::rotation_thread_timer_queue_create()
364 {
365 auto queue = zmalloc<ls::rotation_thread_timer_queue>();
366 if (!queue) {
367 PERROR("Failed to allocate timer rotate queue");
368 goto end;
369 }
370
371 queue->event_pipe = lttng_pipe_open(FD_CLOEXEC | O_NONBLOCK);
372 CDS_INIT_LIST_HEAD(&queue->list);
373 pthread_mutex_init(&queue->lock, nullptr);
374 end:
375 return queue;
376 }
377
378 void ls::rotation_thread_timer_queue_destroy(struct rotation_thread_timer_queue *queue)
379 {
380 if (!queue) {
381 return;
382 }
383
384 lttng_pipe_destroy(queue->event_pipe);
385
386 {
387 lttng::pthread::lock_guard queue_lock(queue->lock);
388
389 LTTNG_ASSERT(cds_list_empty(&queue->list));
390 }
391
392 pthread_mutex_destroy(&queue->lock);
393 free(queue);
394 }
395
396 ls::rotation_thread::rotation_thread(
397 rotation_thread_timer_queue& rotation_timer_queue,
398 notification_thread_handle& notification_thread_handle) :
399 _rotation_timer_queue{ rotation_timer_queue },
400 _notification_thread_handle{ notification_thread_handle }
401 {
402 _quit_pipe.reset([]() {
403 auto raw_pipe = lttng_pipe_open(FD_CLOEXEC);
404 if (!raw_pipe) {
405 LTTNG_THROW_POSIX("Failed to rotation thread's quit pipe", errno);
406 }
407
408 return raw_pipe;
409 }());
410
411 _notification_channel.reset([]() {
412 auto channel = lttng_notification_channel_create(
413 lttng_session_daemon_notification_endpoint);
414 if (!channel) {
415 LTTNG_THROW_ERROR(
416 "Failed to create notification channel of rotation thread");
417 }
418
419 return channel;
420 }());
421
422 lttng_poll_init(&_events);
423
424 /*
425 * Create pollset with size 4:
426 * - rotation thread quit pipe,
427 * - rotation thread timer queue pipe,
428 * - notification channel sock,
429 * - subscribtion change event fd
430 */
431 if (lttng_poll_create(&_events, 4, LTTNG_CLOEXEC) < 0) {
432 LTTNG_THROW_ERROR("Failed to create poll object for rotation thread");
433 }
434
435 if (lttng_poll_add(&_events, lttng_pipe_get_readfd(_quit_pipe.get()), LPOLLIN) < 0) {
436 LTTNG_THROW_ERROR("Failed to add quit pipe read fd to poll set");
437 }
438
439 if (lttng_poll_add(&_events,
440 lttng_pipe_get_readfd(_rotation_timer_queue.event_pipe),
441 LPOLLIN) < 0) {
442 LTTNG_THROW_ERROR("Failed to add rotation timer queue event pipe fd to poll set");
443 }
444
445 if (lttng_poll_add(&_events,
446 _notification_channel_subscribtion_change_eventfd.fd(),
447 LPOLLIN) < 0) {
448 LTTNG_THROW_ERROR(
449 "Failed to add rotation thread notification channel subscription change eventfd to poll set");
450 }
451
452 if (lttng_poll_add(&_events, _notification_channel->socket, LPOLLIN) < 0) {
453 LTTNG_THROW_ERROR("Failed to add notification channel socket fd to pollset");
454 }
455 }
456
457 ls::rotation_thread::~rotation_thread()
458 {
459 lttng_poll_clean(&_events);
460 }
461
462 void ls::rotation_thread_enqueue_job(ls::rotation_thread_timer_queue *queue,
463 ls::rotation_thread_job_type job_type,
464 ltt_session *session)
465 {
466 const char dummy = '!';
467 struct rotation_thread_job *job = nullptr;
468 const char *job_type_str = get_job_type_str(job_type);
469 lttng::pthread::lock_guard queue_lock(queue->lock);
470
471 if (timer_job_exists(queue, job_type, session)) {
472 /*
473 * This timer job is already pending, we don't need to add
474 * it.
475 */
476 return;
477 }
478
479 job = zmalloc<rotation_thread_job>();
480 if (!job) {
481 PERROR("Failed to allocate rotation thread job of type \"%s\" for session \"%s\"",
482 job_type_str,
483 session->name);
484 return;
485 }
486
487 /* No reason for this to fail as the caller must hold a reference. */
488 (void) session_get(session);
489
490 job->session = session;
491 job->type = job_type;
492 cds_list_add_tail(&job->head, &queue->list);
493
494 const int write_ret =
495 lttng_write(lttng_pipe_get_writefd(queue->event_pipe), &dummy, sizeof(dummy));
496 if (write_ret < 0) {
497 /*
498 * We do not want to block in the timer handler, the job has
499 * been enqueued in the list, the wakeup pipe is probably full,
500 * the job will be processed when the rotation_thread catches
501 * up.
502 */
503 DIAGNOSTIC_PUSH
504 DIAGNOSTIC_IGNORE_LOGICAL_OP
505 if (errno == EAGAIN || errno == EWOULDBLOCK) {
506 DIAGNOSTIC_POP
507 /*
508 * Not an error, but would be surprising and indicate
509 * that the rotation thread can't keep up with the
510 * current load.
511 */
512 DBG("Wake-up pipe of rotation thread job queue is full");
513 return;
514 }
515
516 PERROR("Failed to wake-up the rotation thread after pushing a job of type \"%s\" for session \"%s\"",
517 job_type_str,
518 session->name);
519 return;
520 }
521 }
522
523 void ls::rotation_thread::_handle_job_queue()
524 {
525 for (;;) {
526 rotation_thread_job::uptr job;
527
528 {
529 /* Take the queue lock only to pop an element from the list. */
530 lttng::pthread::lock_guard rotation_timer_queue_lock(
531 _rotation_timer_queue.lock);
532 if (cds_list_empty(&_rotation_timer_queue.list)) {
533 break;
534 }
535
536 job.reset(cds_list_first_entry(
537 &_rotation_timer_queue.list, typeof(rotation_thread_job), head));
538 cds_list_del(&job->head);
539 }
540
541 session_lock_list();
542 const auto unlock_list = lttng::make_scope_exit([]() noexcept { session_unlock_list(); });
543
544 /* locked_ptr will unlock the session and release the ref held by the job. */
545 session_lock(job->session);
546 auto session = ltt_session::locked_ptr(job->session);
547
548 if (run_job(*job, *session, _notification_thread_handle)) {
549 return;
550 }
551 }
552 }
553
554 void ls::rotation_thread::_handle_notification(const lttng_notification &notification)
555 {
556 int ret = 0;
557 const char *condition_session_name = nullptr;
558 enum lttng_condition_status condition_status;
559 enum lttng_evaluation_status evaluation_status;
560 uint64_t consumed;
561 auto *condition = lttng_notification_get_const_condition(&notification);
562 auto *evaluation = lttng_notification_get_const_evaluation(&notification);
563 const auto condition_type = lttng_condition_get_type(condition);
564
565 if (condition_type != LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE) {
566 LTTNG_THROW_ERROR("Unexpected condition type");
567 }
568
569 /* Fetch info to test. */
570 condition_status = lttng_condition_session_consumed_size_get_session_name(
571 condition, &condition_session_name);
572 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
573 LTTNG_THROW_ERROR("Session name could not be fetched from notification");
574 }
575
576 evaluation_status =
577 lttng_evaluation_session_consumed_size_get_consumed_size(evaluation, &consumed);
578 if (evaluation_status != LTTNG_EVALUATION_STATUS_OK) {
579 LTTNG_THROW_ERROR("Failed to get consumed size from evaluation");
580 }
581
582 DBG_FMT("Handling session consumed size condition: session_name=`{}`, consumed_size={}",
583 condition_session_name,
584 consumed);
585
586 session_lock_list();
587 const auto unlock_list = lttng::make_scope_exit([]() noexcept { session_unlock_list(); });
588
589 ltt_session::locked_ptr session{ [&condition_session_name]() {
590 auto raw_session_ptr = session_find_by_name(condition_session_name);
591
592 if (raw_session_ptr) {
593 session_lock(raw_session_ptr);
594 }
595
596 return raw_session_ptr;
597 }() };
598 if (!session) {
599 DBG_FMT("Failed to find session while handling notification: notification_type={}, session name=`{}`",
600 lttng_condition_type_str(condition_type),
601 condition_session_name);
602 /*
603 * Not a fatal error: a session can be destroyed before we get
604 * the chance to handle the notification.
605 */
606 return;
607 }
608
609 if (!lttng_trigger_is_equal(session->rotate_trigger,
610 lttng_notification_get_const_trigger(&notification))) {
611 DBG("Notification does not originate from the internal size-based scheduled rotation trigger, skipping");
612 return;
613 }
614
615 unsubscribe_session_consumed_size_rotation(*session);
616
617 ret = cmd_rotate_session(
618 session.get(), nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
619 switch (ret) {
620 case LTTNG_OK:
621 break;
622 case -LTTNG_ERR_ROTATION_PENDING:
623 DBG("Rotate already pending, subscribe to the next threshold value");
624 break;
625 case -LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP:
626 DBG("Rotation already happened since last stop, subscribe to the next threshold value");
627 break;
628 case -LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR:
629 DBG("Rotation already happened since last stop and clear, subscribe to the next threshold value");
630 break;
631 default:
632 LTTNG_THROW_CTL("Failed to rotate on consumed size notification",
633 static_cast<lttng_error_code>(-ret));
634 }
635
636 subscribe_session_consumed_size_rotation(*session, consumed + session->rotate_size);
637 }
638
639 void ls::rotation_thread::_handle_notification_channel_activity()
640 {
641 bool notification_pending = true;
642
643 /*
644 * A notification channel may have multiple notifications queued-up internally in
645 * its buffers. This is because a notification channel multiplexes command replies
646 * and notifications. The current protocol specifies that multiple notifications can be
647 * received before the reply to a command.
648 *
649 * In such cases, the notification channel client implementation internally queues them and
650 * provides them on the next calls to lttng_notification_channel_get_next_notification().
651 * This is correct with respect to the public API, which is intended to be used in "blocking
652 * mode".
653 *
654 * However, this internal user relies on poll/epoll to wake-up when data is available
655 * on the notification channel's socket. As such, it can't assume that a wake-up means only
656 * one notification is available for consumption since many of them may have been queued in
657 * the channel's internal buffers.
658 */
659 while (notification_pending) {
660 const auto pending_status = lttng_notification_channel_has_pending_notification(
661 _notification_channel.get(), &notification_pending);
662 if (pending_status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
663 LTTNG_THROW_ERROR("Error occurred while checking for pending notification");
664 }
665
666 if (!notification_pending) {
667 return;
668 }
669
670 /* Receive the next notification. */
671 lttng_notification::uptr notification;
672 enum lttng_notification_channel_status next_notification_status;
673
674 {
675 struct lttng_notification *raw_notification_ptr;
676
677 next_notification_status = lttng_notification_channel_get_next_notification(
678 _notification_channel.get(), &raw_notification_ptr);
679 notification.reset(raw_notification_ptr);
680 }
681
682 switch (next_notification_status) {
683 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK:
684 break;
685 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED:
686 WARN("Dropped notification detected on notification channel used by the rotation management thread.");
687 return;
688 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED:
689 LTTNG_THROW_ERROR("Notification channel was closed");
690 default:
691 /* Unhandled conditions / errors. */
692 LTTNG_THROW_ERROR("Unknown notification channel status");
693 }
694
695 _handle_notification(*notification);
696 }
697 }
698
699 void ls::rotation_thread::_thread_function() noexcept
700 {
701 DBG("Started rotation thread");
702
703 try {
704 _run();
705 } catch (const std::exception& e) {
706 ERR_FMT("Fatal rotation thread error: {}", e.what());
707 }
708
709 DBG("Thread exit");
710 }
711
712 void ls::rotation_thread::_run()
713 {
714 rcu_register_thread();
715 const auto unregister_rcu_thread =
716 lttng::make_scope_exit([]() noexcept { rcu_unregister_thread(); });
717
718 rcu_thread_online();
719 const auto offline_rcu_thread =
720 lttng::make_scope_exit([]() noexcept { rcu_thread_offline(); });
721
722 health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_ROTATION);
723 health_code_update();
724 const auto unregister_health =
725 lttng::make_scope_exit([]() noexcept { health_unregister(the_health_sessiond); });
726
727 const auto queue_pipe_fd = lttng_pipe_get_readfd(_rotation_timer_queue.event_pipe);
728
729 while (true) {
730 health_poll_entry();
731 DBG("Entering poll wait");
732 auto poll_wait_ret = lttng_poll_wait(&_events, -1);
733 DBG_FMT("Poll wait returned: ret={}", poll_wait_ret);
734 health_poll_exit();
735 if (poll_wait_ret < 0) {
736 /*
737 * Restart interrupted system call.
738 */
739 if (errno == EINTR) {
740 continue;
741 }
742
743 LTTNG_THROW_POSIX("Error encountered during lttng_poll_wait", errno);
744 }
745
746 const auto fd_count = poll_wait_ret;
747 for (int i = 0; i < fd_count; i++) {
748 const auto fd = LTTNG_POLL_GETFD(&_events, i);
749 const auto revents = LTTNG_POLL_GETEV(&_events, i);
750
751 DBG_FMT("Handling descriptor activity: fd={}, events={:b}", fd, revents);
752
753 if (revents & LPOLLERR) {
754 LTTNG_THROW_ERROR(
755 fmt::format("Polling returned an error on fd: fd={}", fd));
756 }
757
758 if (fd == _notification_channel->socket ||
759 fd == _notification_channel_subscribtion_change_eventfd.fd()) {
760 try {
761 _handle_notification_channel_activity();
762 } catch (const lttng::ctl::error& e) {
763 /*
764 * The only non-fatal error (rotation failed), others
765 * are caught at the top-level.
766 */
767 DBG_FMT("Control error occurred while handling activity on notification channel socket: {}",
768 e.what());
769 continue;
770 }
771
772 if (fd == _notification_channel_subscribtion_change_eventfd.fd()) {
773 _notification_channel_subscribtion_change_eventfd.decrement();
774 }
775 } else {
776 /* Job queue or quit pipe activity. */
777
778 /*
779 * The job queue is serviced if there is
780 * activity on the quit pipe to ensure it is
781 * flushed and all references held in the queue
782 * are released.
783 */
784 _handle_job_queue();
785 if (fd == queue_pipe_fd) {
786 char buf;
787
788 if (lttng_read(fd, &buf, 1) != 1) {
789 LTTNG_THROW_POSIX(
790 fmt::format(
791 "Failed to read from wakeup pipe: fd={}",
792 fd),
793 errno);
794 }
795 } else {
796 DBG("Quit pipe activity");
797 return;
798 }
799 }
800 }
801 }
802 }
803
804 bool ls::rotation_thread::shutdown() const noexcept
805 {
806 const int write_fd = lttng_pipe_get_writefd(_quit_pipe.get());
807
808 return notify_thread_pipe(write_fd) == 1;
809 }
810
811 void ls::rotation_thread::launch_thread()
812 {
813 auto thread = lttng_thread_create(
814 "Rotation",
815 [](void *ptr) {
816 auto handle = reinterpret_cast<rotation_thread *>(ptr);
817
818 handle->_thread_function();
819 return static_cast<void *>(nullptr);
820 },
821 shutdown_rotation_thread,
822 nullptr,
823 this);
824 if (!thread) {
825 LTTNG_THROW_ERROR("Failed to launch rotation thread");
826 }
827
828 lttng_thread_put(thread);
829 }
830
831 void ls::rotation_thread::subscribe_session_consumed_size_rotation(ltt_session& session,
832 std::uint64_t size)
833 {
834 const struct lttng_credentials session_creds = {
835 .uid = LTTNG_OPTIONAL_INIT_VALUE(session.uid),
836 .gid = LTTNG_OPTIONAL_INIT_VALUE(session.gid),
837 };
838
839 ASSERT_LOCKED(session.lock);
840
841 auto rotate_condition = lttng::make_unique_wrapper<lttng_condition, lttng_condition_put>(
842 lttng_condition_session_consumed_size_create());
843 if (!rotate_condition) {
844 LTTNG_THROW_POSIX("Failed to create session consumed size condition object", errno);
845 }
846
847 auto condition_status =
848 lttng_condition_session_consumed_size_set_threshold(rotate_condition.get(), size);
849 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
850 LTTNG_THROW_ERROR(fmt::format(
851 "Could not set session consumed size condition threshold: size={}", size));
852 }
853
854 condition_status = lttng_condition_session_consumed_size_set_session_name(rotate_condition.get(),
855 session.name);
856 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
857 LTTNG_THROW_ERROR(fmt::format(
858 "Could not set session consumed size condition session name: name=`{}`",
859 session.name));
860 }
861
862 auto notify_action = lttng::make_unique_wrapper<lttng_action, lttng_action_put>(
863 lttng_action_notify_create());
864 if (!notify_action) {
865 LTTNG_THROW_POSIX("Could not create notify action", errno);
866 }
867
868 LTTNG_ASSERT(!session.rotate_trigger);
869 /* trigger acquires its own reference to condition and action on success. */
870 auto trigger = lttng::make_unique_wrapper<lttng_trigger, lttng_trigger_put>(
871 lttng_trigger_create(rotate_condition.get(), notify_action.get()));
872 if (!trigger)
873 {
874 LTTNG_THROW_POSIX("Could not create size-based rotation trigger", errno);
875 }
876
877 /* Ensure this trigger is not visible to external users. */
878 lttng_trigger_set_hidden(trigger.get());
879 lttng_trigger_set_credentials(trigger.get(), &session_creds);
880
881 auto nc_status =
882 lttng_notification_channel_subscribe(_notification_channel.get(), rotate_condition.get());
883 if (nc_status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
884 LTTNG_THROW_ERROR("Could not subscribe to session consumed size notification");
885 }
886
887 /*
888 * Ensure any notification queued during the subscription are consumed by queueing an
889 * event.
890 */
891 _notification_channel_subscribtion_change_eventfd.increment();
892
893 const auto register_ret = notification_thread_command_register_trigger(
894 &_notification_thread_handle, trigger.get(), true);
895 if (register_ret != LTTNG_OK) {
896 LTTNG_THROW_CTL(
897 fmt::format(
898 "Failed to register trigger for automatic size-based rotation: session_name{}, size={}",
899 session.name,
900 size),
901 register_ret);
902 }
903
904 /* Ownership transferred to the session. */
905 session.rotate_trigger = trigger.release();
906 }
907
908 void ls::rotation_thread::unsubscribe_session_consumed_size_rotation(ltt_session& session)
909 {
910 LTTNG_ASSERT(session.rotate_trigger);
911
912 const auto remove_session_trigger = lttng::make_scope_exit([&session]() noexcept {
913 lttng_trigger_put(session.rotate_trigger);
914 session.rotate_trigger = nullptr;
915 });
916
917 const auto unsubscribe_status = lttng_notification_channel_unsubscribe(
918 _notification_channel.get(),
919 lttng_trigger_get_const_condition(session.rotate_trigger));
920 if (unsubscribe_status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
921 LTTNG_THROW_ERROR(fmt::format(
922 "Failed to unsubscribe from consumed size condition used to control automatic size-based rotations: session_name=`{}` return_code={}",
923 session.name,
924 static_cast<int>(unsubscribe_status)));
925 }
926
927 /*
928 * Ensure any notification queued during the un-subscription are consumed by queueing an
929 * event.
930 */
931 _notification_channel_subscribtion_change_eventfd.increment();
932
933 const auto unregister_status = notification_thread_command_unregister_trigger(
934 &_notification_thread_handle, session.rotate_trigger);
935 if (unregister_status != LTTNG_OK) {
936 LTTNG_THROW_CTL(
937 fmt::format(
938 "Failed to unregister trigger for automatic size-based rotation: session_name{}",
939 session.name),
940 unregister_status);
941 }
942 }
This page took 0.0477 seconds and 5 git commands to generate.