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