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