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