| 1 | /* |
| 2 | * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License, version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 16 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _LGPL_SOURCE |
| 20 | #include <lttng/trigger/trigger.h> |
| 21 | #include <common/error.h> |
| 22 | #include <common/config/session-config.h> |
| 23 | #include <common/defaults.h> |
| 24 | #include <common/utils.h> |
| 25 | #include <common/futex.h> |
| 26 | #include <common/align.h> |
| 27 | #include <common/time.h> |
| 28 | #include <common/hashtable/utils.h> |
| 29 | #include <sys/eventfd.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <time.h> |
| 32 | #include <signal.h> |
| 33 | #include <inttypes.h> |
| 34 | |
| 35 | #include <common/kernel-ctl/kernel-ctl.h> |
| 36 | #include <lttng/notification/channel-internal.h> |
| 37 | #include <lttng/rotate-internal.h> |
| 38 | |
| 39 | #include "rotation-thread.h" |
| 40 | #include "lttng-sessiond.h" |
| 41 | #include "health-sessiond.h" |
| 42 | #include "rotate.h" |
| 43 | #include "cmd.h" |
| 44 | #include "session.h" |
| 45 | #include "timer.h" |
| 46 | #include "notification-thread-commands.h" |
| 47 | |
| 48 | #include <urcu.h> |
| 49 | #include <urcu/list.h> |
| 50 | |
| 51 | struct lttng_notification_channel *rotate_notification_channel = NULL; |
| 52 | |
| 53 | struct rotation_thread { |
| 54 | struct lttng_poll_event events; |
| 55 | }; |
| 56 | |
| 57 | struct rotation_thread_job { |
| 58 | enum rotation_thread_job_type type; |
| 59 | uint64_t session_id; |
| 60 | /* List member in struct rotation_thread_timer_queue. */ |
| 61 | struct cds_list_head head; |
| 62 | }; |
| 63 | |
| 64 | /* |
| 65 | * The timer thread enqueues jobs and wakes up the rotation thread. |
| 66 | * When the rotation thread wakes up, it empties the queue. |
| 67 | */ |
| 68 | struct rotation_thread_timer_queue { |
| 69 | struct lttng_pipe *event_pipe; |
| 70 | struct cds_list_head list; |
| 71 | pthread_mutex_t lock; |
| 72 | }; |
| 73 | |
| 74 | struct rotation_thread_handle { |
| 75 | struct rotation_thread_timer_queue *rotation_timer_queue; |
| 76 | /* Access to the notification thread cmd_queue */ |
| 77 | struct notification_thread_handle *notification_thread_handle; |
| 78 | sem_t *notification_thread_ready; |
| 79 | }; |
| 80 | |
| 81 | static |
| 82 | const char *get_job_type_str(enum rotation_thread_job_type job_type) |
| 83 | { |
| 84 | switch (job_type) { |
| 85 | case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION: |
| 86 | return "CHECK_PENDING_ROTATION"; |
| 87 | case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION: |
| 88 | return "SCHEDULED_ROTATION"; |
| 89 | default: |
| 90 | abort(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | struct rotation_thread_timer_queue *rotation_thread_timer_queue_create(void) |
| 95 | { |
| 96 | struct rotation_thread_timer_queue *queue = NULL; |
| 97 | |
| 98 | queue = zmalloc(sizeof(*queue)); |
| 99 | if (!queue) { |
| 100 | PERROR("Failed to allocate timer rotate queue"); |
| 101 | goto end; |
| 102 | } |
| 103 | |
| 104 | queue->event_pipe = lttng_pipe_open(FD_CLOEXEC | O_NONBLOCK); |
| 105 | CDS_INIT_LIST_HEAD(&queue->list); |
| 106 | pthread_mutex_init(&queue->lock, NULL); |
| 107 | end: |
| 108 | return queue; |
| 109 | } |
| 110 | |
| 111 | void log_job_destruction(const struct rotation_thread_job *job) |
| 112 | { |
| 113 | enum lttng_error_level log_level; |
| 114 | const char *job_type_str = get_job_type_str(job->type); |
| 115 | |
| 116 | switch (job->type) { |
| 117 | case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION: |
| 118 | /* |
| 119 | * Not a problem, the scheduled rotation is racing with the teardown |
| 120 | * of the daemon. In this case, the rotation will not happen, which |
| 121 | * is not a problem (or at least, not important enough to delay |
| 122 | * the shutdown of the session daemon). |
| 123 | */ |
| 124 | log_level = PRINT_DBG; |
| 125 | break; |
| 126 | case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION: |
| 127 | /* This is not expected to happen; warn the user. */ |
| 128 | log_level = PRINT_WARN; |
| 129 | break; |
| 130 | default: |
| 131 | abort(); |
| 132 | } |
| 133 | |
| 134 | LOG(log_level, "Rotation thread timer queue still contains job of type %s targeting session %" PRIu64 " on destruction", |
| 135 | job_type_str, job->session_id); |
| 136 | } |
| 137 | |
| 138 | void rotation_thread_timer_queue_destroy( |
| 139 | struct rotation_thread_timer_queue *queue) |
| 140 | { |
| 141 | struct rotation_thread_job *job, *tmp_job; |
| 142 | |
| 143 | if (!queue) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | lttng_pipe_destroy(queue->event_pipe); |
| 148 | |
| 149 | pthread_mutex_lock(&queue->lock); |
| 150 | /* Empty wait queue. */ |
| 151 | cds_list_for_each_entry_safe(job, tmp_job, &queue->list, head) { |
| 152 | log_job_destruction(job); |
| 153 | cds_list_del(&job->head); |
| 154 | free(job); |
| 155 | } |
| 156 | pthread_mutex_unlock(&queue->lock); |
| 157 | pthread_mutex_destroy(&queue->lock); |
| 158 | free(queue); |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Destroy the thread data previously created by the init function. |
| 163 | */ |
| 164 | void rotation_thread_handle_destroy( |
| 165 | struct rotation_thread_handle *handle) |
| 166 | { |
| 167 | free(handle); |
| 168 | } |
| 169 | |
| 170 | struct rotation_thread_handle *rotation_thread_handle_create( |
| 171 | struct rotation_thread_timer_queue *rotation_timer_queue, |
| 172 | struct notification_thread_handle *notification_thread_handle, |
| 173 | sem_t *notification_thread_ready) |
| 174 | { |
| 175 | struct rotation_thread_handle *handle; |
| 176 | |
| 177 | handle = zmalloc(sizeof(*handle)); |
| 178 | if (!handle) { |
| 179 | goto end; |
| 180 | } |
| 181 | |
| 182 | handle->rotation_timer_queue = rotation_timer_queue; |
| 183 | handle->notification_thread_handle = notification_thread_handle; |
| 184 | handle->notification_thread_ready = notification_thread_ready; |
| 185 | |
| 186 | end: |
| 187 | return handle; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Called with the rotation_thread_timer_queue lock held. |
| 192 | * Return true if the same timer job already exists in the queue, false if not. |
| 193 | */ |
| 194 | static |
| 195 | bool timer_job_exists(const struct rotation_thread_timer_queue *queue, |
| 196 | enum rotation_thread_job_type job_type, uint64_t session_id) |
| 197 | { |
| 198 | bool exists = false; |
| 199 | struct rotation_thread_job *job; |
| 200 | |
| 201 | cds_list_for_each_entry(job, &queue->list, head) { |
| 202 | if (job->session_id == session_id && job->type == job_type) { |
| 203 | exists = true; |
| 204 | goto end; |
| 205 | } |
| 206 | } |
| 207 | end: |
| 208 | return exists; |
| 209 | } |
| 210 | |
| 211 | void rotation_thread_enqueue_job(struct rotation_thread_timer_queue *queue, |
| 212 | enum rotation_thread_job_type job_type, uint64_t session_id) |
| 213 | { |
| 214 | int ret; |
| 215 | const char * const dummy = "!"; |
| 216 | struct rotation_thread_job *job = NULL; |
| 217 | const char *job_type_str = get_job_type_str(job_type); |
| 218 | |
| 219 | pthread_mutex_lock(&queue->lock); |
| 220 | if (timer_job_exists(queue, session_id, job_type)) { |
| 221 | /* |
| 222 | * This timer job is already pending, we don't need to add |
| 223 | * it. |
| 224 | */ |
| 225 | goto end; |
| 226 | } |
| 227 | |
| 228 | job = zmalloc(sizeof(struct rotation_thread_job)); |
| 229 | if (!job) { |
| 230 | PERROR("Failed to allocate rotation thread job of type \"%s\" for session id %" PRIu64, |
| 231 | job_type_str, session_id); |
| 232 | goto end; |
| 233 | } |
| 234 | job->type = job_type; |
| 235 | job->session_id = session_id; |
| 236 | cds_list_add_tail(&job->head, &queue->list); |
| 237 | |
| 238 | ret = lttng_write(lttng_pipe_get_writefd(queue->event_pipe), dummy, |
| 239 | 1); |
| 240 | if (ret < 0) { |
| 241 | /* |
| 242 | * We do not want to block in the timer handler, the job has |
| 243 | * been enqueued in the list, the wakeup pipe is probably full, |
| 244 | * the job will be processed when the rotation_thread catches |
| 245 | * up. |
| 246 | */ |
| 247 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 248 | /* |
| 249 | * Not an error, but would be surprising and indicate |
| 250 | * that the rotation thread can't keep up with the |
| 251 | * current load. |
| 252 | */ |
| 253 | DBG("Wake-up pipe of rotation thread job queue is full"); |
| 254 | goto end; |
| 255 | } |
| 256 | PERROR("Failed to wake-up the rotation thread after pushing a job of type \"%s\" for session id %" PRIu64, |
| 257 | job_type_str, session_id); |
| 258 | goto end; |
| 259 | } |
| 260 | |
| 261 | end: |
| 262 | pthread_mutex_unlock(&queue->lock); |
| 263 | } |
| 264 | |
| 265 | static |
| 266 | int init_poll_set(struct lttng_poll_event *poll_set, |
| 267 | struct rotation_thread_handle *handle) |
| 268 | { |
| 269 | int ret; |
| 270 | |
| 271 | /* |
| 272 | * Create pollset with size 2: |
| 273 | * - quit pipe, |
| 274 | * - rotation thread timer queue pipe, |
| 275 | */ |
| 276 | ret = sessiond_set_thread_pollset(poll_set, 2); |
| 277 | if (ret) { |
| 278 | goto error; |
| 279 | } |
| 280 | ret = lttng_poll_add(poll_set, |
| 281 | lttng_pipe_get_readfd(handle->rotation_timer_queue->event_pipe), |
| 282 | LPOLLIN | LPOLLERR); |
| 283 | if (ret < 0) { |
| 284 | ERR("[rotation-thread] Failed to add rotate_pending fd to pollset"); |
| 285 | goto error; |
| 286 | } |
| 287 | |
| 288 | return ret; |
| 289 | error: |
| 290 | lttng_poll_clean(poll_set); |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | static |
| 295 | void fini_thread_state(struct rotation_thread *state) |
| 296 | { |
| 297 | lttng_poll_clean(&state->events); |
| 298 | if (rotate_notification_channel) { |
| 299 | lttng_notification_channel_destroy(rotate_notification_channel); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | static |
| 304 | int init_thread_state(struct rotation_thread_handle *handle, |
| 305 | struct rotation_thread *state) |
| 306 | { |
| 307 | int ret; |
| 308 | |
| 309 | memset(state, 0, sizeof(*state)); |
| 310 | lttng_poll_init(&state->events); |
| 311 | |
| 312 | ret = init_poll_set(&state->events, handle); |
| 313 | if (ret) { |
| 314 | ERR("[rotation-thread] Failed to initialize rotation thread poll set"); |
| 315 | goto end; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * We wait until the notification thread is ready to create the |
| 320 | * notification channel and add it to the poll_set. |
| 321 | */ |
| 322 | sem_wait(handle->notification_thread_ready); |
| 323 | rotate_notification_channel = lttng_notification_channel_create( |
| 324 | lttng_session_daemon_notification_endpoint); |
| 325 | if (!rotate_notification_channel) { |
| 326 | ERR("[rotation-thread] Could not create notification channel"); |
| 327 | ret = -1; |
| 328 | goto end; |
| 329 | } |
| 330 | ret = lttng_poll_add(&state->events, rotate_notification_channel->socket, |
| 331 | LPOLLIN | LPOLLERR); |
| 332 | if (ret < 0) { |
| 333 | ERR("[rotation-thread] Failed to add notification fd to pollset"); |
| 334 | goto end; |
| 335 | } |
| 336 | |
| 337 | end: |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | static |
| 342 | int check_session_rotation_pending_local_on_consumer( |
| 343 | const struct ltt_session *session, |
| 344 | struct consumer_socket *socket, bool *rotation_completed) |
| 345 | { |
| 346 | int ret; |
| 347 | |
| 348 | pthread_mutex_lock(socket->lock); |
| 349 | DBG("[rotation-thread] Checking for locally pending rotation on the %s consumer for session %s", |
| 350 | lttng_consumer_type_str(socket->type), |
| 351 | session->name); |
| 352 | ret = consumer_check_rotation_pending_local(socket, |
| 353 | session->id, |
| 354 | session->current_archive_id - 1); |
| 355 | pthread_mutex_unlock(socket->lock); |
| 356 | |
| 357 | if (ret == 0) { |
| 358 | /* Rotation was completed on this consumer. */ |
| 359 | DBG("[rotation-thread] Local rotation of trace archive %" PRIu64 " of session \"%s\" was completed on the %s consumer", |
| 360 | session->current_archive_id - 1, |
| 361 | session->name, |
| 362 | lttng_consumer_type_str(socket->type)); |
| 363 | *rotation_completed = true; |
| 364 | } else if (ret == 1) { |
| 365 | /* Rotation pending on this consumer. */ |
| 366 | DBG("[rotation-thread] Local rotation of trace archive %" PRIu64 " of session \"%s\" is pending on the %s consumer", |
| 367 | session->current_archive_id - 1, |
| 368 | session->name, |
| 369 | lttng_consumer_type_str(socket->type)); |
| 370 | *rotation_completed = false; |
| 371 | ret = 0; |
| 372 | } else { |
| 373 | /* Not a fatal error. */ |
| 374 | ERR("[rotation-thread] Encountered an error when checking if local rotation of trace archive %" PRIu64 " of session \"%s\" is pending on the %s consumer", |
| 375 | session->current_archive_id - 1, |
| 376 | session->name, |
| 377 | lttng_consumer_type_str(socket->type)); |
| 378 | *rotation_completed = false; |
| 379 | } |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | static |
| 384 | int check_session_rotation_pending_local(struct ltt_session *session) |
| 385 | { |
| 386 | int ret = 0; |
| 387 | struct consumer_socket *socket; |
| 388 | struct cds_lfht_iter iter; |
| 389 | bool rotation_completed = true; |
| 390 | |
| 391 | /* |
| 392 | * Check for a local pending rotation on all consumers (32-bit |
| 393 | * user space, 64-bit user space, and kernel). |
| 394 | */ |
| 395 | DBG("[rotation-thread] Checking for pending local rotation on session \"%s\", trace archive %" PRIu64, |
| 396 | session->name, session->current_archive_id - 1); |
| 397 | |
| 398 | rcu_read_lock(); |
| 399 | if (!session->ust_session) { |
| 400 | goto skip_ust; |
| 401 | } |
| 402 | cds_lfht_for_each_entry(session->ust_session->consumer->socks->ht, |
| 403 | &iter, socket, node.node) { |
| 404 | ret = check_session_rotation_pending_local_on_consumer(session, |
| 405 | socket, &rotation_completed); |
| 406 | if (ret || !rotation_completed) { |
| 407 | goto end; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | skip_ust: |
| 412 | if (!session->kernel_session) { |
| 413 | goto skip_kernel; |
| 414 | } |
| 415 | cds_lfht_for_each_entry(session->kernel_session->consumer->socks->ht, |
| 416 | &iter, socket, node.node) { |
| 417 | ret = check_session_rotation_pending_local_on_consumer(session, |
| 418 | socket, &rotation_completed); |
| 419 | if (ret || !rotation_completed) { |
| 420 | goto end; |
| 421 | } |
| 422 | } |
| 423 | skip_kernel: |
| 424 | end: |
| 425 | rcu_read_unlock(); |
| 426 | |
| 427 | if (rotation_completed) { |
| 428 | DBG("[rotation-thread] Local rotation of trace archive %" PRIu64 " of session \"%s\" is complete on all consumers", |
| 429 | session->current_archive_id - 1, |
| 430 | session->name); |
| 431 | session->rotation_pending_local = false; |
| 432 | } |
| 433 | if (ret) { |
| 434 | ret = session_reset_rotation_state(session, |
| 435 | LTTNG_ROTATION_STATE_ERROR); |
| 436 | if (ret) { |
| 437 | ERR("Failed to reset rotation state of session \"%s\"", |
| 438 | session->name); |
| 439 | } |
| 440 | } |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static |
| 445 | int check_session_rotation_pending_relay(struct ltt_session *session) |
| 446 | { |
| 447 | int ret; |
| 448 | struct consumer_socket *socket; |
| 449 | struct cds_lfht_iter iter; |
| 450 | bool rotation_completed = true; |
| 451 | const struct consumer_output *output; |
| 452 | |
| 453 | /* |
| 454 | * Check for a pending rotation on any consumer as we only use |
| 455 | * it as a "tunnel" to the relayd. |
| 456 | */ |
| 457 | |
| 458 | rcu_read_lock(); |
| 459 | if (session->ust_session) { |
| 460 | cds_lfht_first(session->ust_session->consumer->socks->ht, |
| 461 | &iter); |
| 462 | output = session->ust_session->consumer; |
| 463 | } else { |
| 464 | cds_lfht_first(session->kernel_session->consumer->socks->ht, |
| 465 | &iter); |
| 466 | output = session->kernel_session->consumer; |
| 467 | } |
| 468 | assert(cds_lfht_iter_get_node(&iter)); |
| 469 | |
| 470 | socket = caa_container_of(cds_lfht_iter_get_node(&iter), |
| 471 | typeof(*socket), node.node); |
| 472 | |
| 473 | pthread_mutex_lock(socket->lock); |
| 474 | DBG("[rotation-thread] Checking for pending relay rotation on session \"%s\", trace archive %" PRIu64 " through the %s consumer", |
| 475 | session->name, session->current_archive_id - 1, |
| 476 | lttng_consumer_type_str(socket->type)); |
| 477 | ret = consumer_check_rotation_pending_relay(socket, |
| 478 | output, |
| 479 | session->id, |
| 480 | session->current_archive_id - 1); |
| 481 | pthread_mutex_unlock(socket->lock); |
| 482 | |
| 483 | if (ret == 0) { |
| 484 | /* Rotation was completed on the relay. */ |
| 485 | DBG("[rotation-thread] Relay rotation of trace archive %" PRIu64 " of session \"%s\" was completed", |
| 486 | session->current_archive_id - 1, |
| 487 | session->name); |
| 488 | } else if (ret == 1) { |
| 489 | /* Rotation pending on relay. */ |
| 490 | DBG("[rotation-thread] Relay rotation of trace archive %" PRIu64 " of session \"%s\" is pending", |
| 491 | session->current_archive_id - 1, |
| 492 | session->name); |
| 493 | rotation_completed = false; |
| 494 | } else { |
| 495 | /* Not a fatal error. */ |
| 496 | ERR("[rotation-thread] Encountered an error when checking if rotation of trace archive %" PRIu64 " of session \"%s\" is pending on the relay", |
| 497 | session->current_archive_id - 1, |
| 498 | session->name); |
| 499 | ret = session_reset_rotation_state(session, |
| 500 | LTTNG_ROTATION_STATE_ERROR); |
| 501 | if (ret) { |
| 502 | ERR("Failed to reset rotation state of session \"%s\"", |
| 503 | session->name); |
| 504 | } |
| 505 | rotation_completed = false; |
| 506 | } |
| 507 | |
| 508 | rcu_read_unlock(); |
| 509 | |
| 510 | if (rotation_completed) { |
| 511 | DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " of session \"%s\" is complete on the relay", |
| 512 | session->current_archive_id - 1, |
| 513 | session->name); |
| 514 | session->rotation_pending_relay = false; |
| 515 | } |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Check if the last rotation was completed, called with session lock held. |
| 521 | */ |
| 522 | static |
| 523 | int check_session_rotation_pending(struct ltt_session *session, |
| 524 | struct notification_thread_handle *notification_thread_handle) |
| 525 | { |
| 526 | int ret; |
| 527 | struct lttng_trace_archive_location *location; |
| 528 | time_t now; |
| 529 | |
| 530 | DBG("[rotation-thread] Checking for pending rotation on session \"%s\", trace archive %" PRIu64, |
| 531 | session->name, session->current_archive_id - 1); |
| 532 | |
| 533 | if (session->rotation_pending_local) { |
| 534 | /* Updates session->rotation_pending_local as needed. */ |
| 535 | ret = check_session_rotation_pending_local(session); |
| 536 | if (ret) { |
| 537 | goto end; |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | * No need to check for a pending rotation on the relay |
| 542 | * since the rotation is not even completed locally yet. |
| 543 | */ |
| 544 | if (session->rotation_pending_local) { |
| 545 | goto end; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | if (session->rotation_pending_relay) { |
| 550 | /* Updates session->rotation_pending_relay as needed. */ |
| 551 | ret = check_session_rotation_pending_relay(session); |
| 552 | if (ret) { |
| 553 | goto end; |
| 554 | } |
| 555 | |
| 556 | if (session->rotation_pending_relay) { |
| 557 | goto end; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " completed for " |
| 562 | "session %s", session->current_archive_id - 1, |
| 563 | session->name); |
| 564 | |
| 565 | /* Rename the completed trace archive's location. */ |
| 566 | now = time(NULL); |
| 567 | if (now == (time_t) -1) { |
| 568 | ret = session_reset_rotation_state(session, |
| 569 | LTTNG_ROTATION_STATE_ERROR); |
| 570 | if (ret) { |
| 571 | ERR("Failed to reset rotation state of session \"%s\"", |
| 572 | session->name); |
| 573 | } |
| 574 | ret = LTTNG_ERR_UNK; |
| 575 | goto end; |
| 576 | } |
| 577 | |
| 578 | ret = rename_completed_chunk(session, now); |
| 579 | if (ret < 0) { |
| 580 | ERR("Failed to rename completed rotation chunk"); |
| 581 | goto end; |
| 582 | } |
| 583 | session->last_chunk_start_ts = session->current_chunk_start_ts; |
| 584 | |
| 585 | /* |
| 586 | * Now we can clear the "ONGOING" state in the session. New |
| 587 | * rotations can start now. |
| 588 | */ |
| 589 | session->rotation_state = LTTNG_ROTATION_STATE_COMPLETED; |
| 590 | |
| 591 | /* Ownership of location is transferred. */ |
| 592 | location = session_get_trace_archive_location(session); |
| 593 | ret = notification_thread_command_session_rotation_completed( |
| 594 | notification_thread_handle, |
| 595 | session->name, |
| 596 | session->uid, |
| 597 | session->gid, |
| 598 | session->current_archive_id, |
| 599 | location); |
| 600 | if (ret != LTTNG_OK) { |
| 601 | ERR("[rotation-thread] Failed to notify notification thread of completed rotation for session %s", |
| 602 | session->name); |
| 603 | } |
| 604 | |
| 605 | if (!session->active) { |
| 606 | /* |
| 607 | * A stop command was issued during the rotation, it is |
| 608 | * up to the rotation completion check to perform the |
| 609 | * renaming of the last chunk that was produced. |
| 610 | */ |
| 611 | ret = notification_thread_command_session_rotation_ongoing( |
| 612 | notification_thread_handle, |
| 613 | session->name, |
| 614 | session->uid, |
| 615 | session->gid, |
| 616 | session->current_archive_id); |
| 617 | if (ret != LTTNG_OK) { |
| 618 | ERR("[rotation-thread] Failed to notify notification thread of completed rotation for session %s", |
| 619 | session->name); |
| 620 | } |
| 621 | |
| 622 | ret = rename_active_chunk(session); |
| 623 | if (ret < 0) { |
| 624 | ERR("[rotation-thread] Failed to rename active rotation chunk"); |
| 625 | goto end; |
| 626 | } |
| 627 | |
| 628 | /* Ownership of location is transferred. */ |
| 629 | location = session_get_trace_archive_location(session); |
| 630 | ret = notification_thread_command_session_rotation_completed( |
| 631 | notification_thread_handle, |
| 632 | session->name, |
| 633 | session->uid, |
| 634 | session->gid, |
| 635 | session->current_archive_id, |
| 636 | location); |
| 637 | if (ret != LTTNG_OK) { |
| 638 | ERR("[rotation-thread] Failed to notify notification thread of completed rotation for session %s", |
| 639 | session->name); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | ret = 0; |
| 644 | end: |
| 645 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
| 646 | DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " is still pending for session %s", |
| 647 | session->current_archive_id - 1, session->name); |
| 648 | ret = timer_session_rotation_pending_check_start(session, |
| 649 | DEFAULT_ROTATE_PENDING_TIMER); |
| 650 | if (ret) { |
| 651 | ERR("Re-enabling rotate pending timer"); |
| 652 | ret = -1; |
| 653 | goto end; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | /* Call with the session and session_list locks held. */ |
| 661 | static |
| 662 | int launch_session_rotation(struct ltt_session *session) |
| 663 | { |
| 664 | int ret; |
| 665 | struct lttng_rotate_session_return rotation_return; |
| 666 | |
| 667 | DBG("[rotation-thread] Launching scheduled time-based rotation on session \"%s\"", |
| 668 | session->name); |
| 669 | |
| 670 | ret = cmd_rotate_session(session, &rotation_return); |
| 671 | if (ret == LTTNG_OK) { |
| 672 | DBG("[rotation-thread] Scheduled time-based rotation successfully launched on session \"%s\"", |
| 673 | session->name); |
| 674 | } else { |
| 675 | /* Don't consider errors as fatal. */ |
| 676 | DBG("[rotation-thread] Scheduled time-based rotation aborted for session %s: %s", |
| 677 | session->name, lttng_strerror(ret)); |
| 678 | } |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | static |
| 683 | int run_job(struct rotation_thread_job *job, struct ltt_session *session, |
| 684 | struct notification_thread_handle *notification_thread_handle) |
| 685 | { |
| 686 | int ret; |
| 687 | |
| 688 | switch (job->type) { |
| 689 | case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION: |
| 690 | ret = launch_session_rotation(session); |
| 691 | break; |
| 692 | case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION: |
| 693 | ret = check_session_rotation_pending(session, |
| 694 | notification_thread_handle); |
| 695 | break; |
| 696 | default: |
| 697 | abort(); |
| 698 | } |
| 699 | return ret; |
| 700 | } |
| 701 | |
| 702 | static |
| 703 | int handle_job_queue(struct rotation_thread_handle *handle, |
| 704 | struct rotation_thread *state, |
| 705 | struct rotation_thread_timer_queue *queue) |
| 706 | { |
| 707 | int ret = 0; |
| 708 | int fd = lttng_pipe_get_readfd(queue->event_pipe); |
| 709 | char buf; |
| 710 | |
| 711 | ret = lttng_read(fd, &buf, 1); |
| 712 | if (ret != 1) { |
| 713 | ERR("[rotation-thread] Failed to read from wakeup pipe (fd = %i)", fd); |
| 714 | ret = -1; |
| 715 | goto end; |
| 716 | } |
| 717 | |
| 718 | for (;;) { |
| 719 | struct ltt_session *session; |
| 720 | struct rotation_thread_job *job; |
| 721 | |
| 722 | /* Take the queue lock only to pop an element from the list. */ |
| 723 | pthread_mutex_lock(&queue->lock); |
| 724 | if (cds_list_empty(&queue->list)) { |
| 725 | pthread_mutex_unlock(&queue->lock); |
| 726 | break; |
| 727 | } |
| 728 | job = cds_list_first_entry(&queue->list, |
| 729 | typeof(*job), head); |
| 730 | cds_list_del(&job->head); |
| 731 | pthread_mutex_unlock(&queue->lock); |
| 732 | |
| 733 | session_lock_list(); |
| 734 | session = session_find_by_id(job->session_id); |
| 735 | if (!session) { |
| 736 | DBG("[rotation-thread] Session %" PRIu64 " not found", |
| 737 | job->session_id); |
| 738 | /* |
| 739 | * This is a non-fatal error, and we cannot report it to |
| 740 | * the user (timer), so just print the error and |
| 741 | * continue the processing. |
| 742 | * |
| 743 | * While the timer thread will purge pending signals for |
| 744 | * a session on the session's destruction, it is |
| 745 | * possible for a job targeting that session to have |
| 746 | * already been queued before it was destroyed. |
| 747 | */ |
| 748 | session_unlock_list(); |
| 749 | free(job); |
| 750 | session_put(session); |
| 751 | continue; |
| 752 | } |
| 753 | |
| 754 | session_lock(session); |
| 755 | ret = run_job(job, session, handle->notification_thread_handle); |
| 756 | session_unlock(session); |
| 757 | session_put(session); |
| 758 | session_unlock_list(); |
| 759 | free(job); |
| 760 | if (ret) { |
| 761 | goto end; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | ret = 0; |
| 766 | |
| 767 | end: |
| 768 | return ret; |
| 769 | } |
| 770 | |
| 771 | static |
| 772 | int handle_condition(const struct lttng_condition *condition, |
| 773 | const struct lttng_evaluation *evaluation, |
| 774 | struct notification_thread_handle *notification_thread_handle) |
| 775 | { |
| 776 | int ret = 0; |
| 777 | const char *condition_session_name = NULL; |
| 778 | enum lttng_condition_type condition_type; |
| 779 | enum lttng_condition_status condition_status; |
| 780 | enum lttng_evaluation_status evaluation_status; |
| 781 | uint64_t consumed; |
| 782 | struct ltt_session *session; |
| 783 | |
| 784 | condition_type = lttng_condition_get_type(condition); |
| 785 | |
| 786 | if (condition_type != LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE) { |
| 787 | ret = -1; |
| 788 | ERR("[rotation-thread] Condition type and session usage type are not the same"); |
| 789 | goto end; |
| 790 | } |
| 791 | |
| 792 | /* Fetch info to test */ |
| 793 | condition_status = lttng_condition_session_consumed_size_get_session_name( |
| 794 | condition, &condition_session_name); |
| 795 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { |
| 796 | ERR("[rotation-thread] Session name could not be fetched"); |
| 797 | ret = -1; |
| 798 | goto end; |
| 799 | } |
| 800 | evaluation_status = lttng_evaluation_session_consumed_size_get_consumed_size(evaluation, |
| 801 | &consumed); |
| 802 | if (evaluation_status != LTTNG_EVALUATION_STATUS_OK) { |
| 803 | ERR("[rotation-thread] Failed to get evaluation"); |
| 804 | ret = -1; |
| 805 | goto end; |
| 806 | } |
| 807 | |
| 808 | session_lock_list(); |
| 809 | session = session_find_by_name(condition_session_name); |
| 810 | if (!session) { |
| 811 | ret = -1; |
| 812 | session_unlock_list(); |
| 813 | ERR("[rotation-thread] Session \"%s\" not found", |
| 814 | condition_session_name); |
| 815 | goto end; |
| 816 | } |
| 817 | session_lock(session); |
| 818 | session_unlock_list(); |
| 819 | |
| 820 | ret = unsubscribe_session_consumed_size_rotation(session, |
| 821 | notification_thread_handle); |
| 822 | if (ret) { |
| 823 | goto end_unlock; |
| 824 | } |
| 825 | |
| 826 | ret = cmd_rotate_session(session, NULL); |
| 827 | if (ret == -LTTNG_ERR_ROTATION_PENDING) { |
| 828 | DBG("Rotate already pending, subscribe to the next threshold value"); |
| 829 | } else if (ret != LTTNG_OK) { |
| 830 | ERR("[rotation-thread] Failed to rotate on size notification with error: %s", |
| 831 | lttng_strerror(ret)); |
| 832 | ret = -1; |
| 833 | goto end_unlock; |
| 834 | } |
| 835 | ret = subscribe_session_consumed_size_rotation(session, |
| 836 | consumed + session->rotate_size, |
| 837 | notification_thread_handle); |
| 838 | if (ret) { |
| 839 | ERR("[rotation-thread] Failed to subscribe to session consumed size condition"); |
| 840 | goto end_unlock; |
| 841 | } |
| 842 | ret = 0; |
| 843 | |
| 844 | end_unlock: |
| 845 | session_unlock(session); |
| 846 | session_put(session); |
| 847 | end: |
| 848 | return ret; |
| 849 | } |
| 850 | |
| 851 | static |
| 852 | int handle_notification_channel(int fd, |
| 853 | struct rotation_thread_handle *handle, |
| 854 | struct rotation_thread *state) |
| 855 | { |
| 856 | int ret; |
| 857 | bool notification_pending; |
| 858 | struct lttng_notification *notification = NULL; |
| 859 | enum lttng_notification_channel_status status; |
| 860 | const struct lttng_evaluation *notification_evaluation; |
| 861 | const struct lttng_condition *notification_condition; |
| 862 | |
| 863 | status = lttng_notification_channel_has_pending_notification( |
| 864 | rotate_notification_channel, ¬ification_pending); |
| 865 | if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) { |
| 866 | ERR("[rotation-thread ]Error occurred while checking for pending notification"); |
| 867 | ret = -1; |
| 868 | goto end; |
| 869 | } |
| 870 | |
| 871 | if (!notification_pending) { |
| 872 | ret = 0; |
| 873 | goto end; |
| 874 | } |
| 875 | |
| 876 | /* Receive the next notification. */ |
| 877 | status = lttng_notification_channel_get_next_notification( |
| 878 | rotate_notification_channel, |
| 879 | ¬ification); |
| 880 | |
| 881 | switch (status) { |
| 882 | case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK: |
| 883 | break; |
| 884 | case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED: |
| 885 | /* Not an error, we will wait for the next one */ |
| 886 | ret = 0; |
| 887 | goto end;; |
| 888 | case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED: |
| 889 | ERR("Notification channel was closed"); |
| 890 | ret = -1; |
| 891 | goto end; |
| 892 | default: |
| 893 | /* Unhandled conditions / errors. */ |
| 894 | ERR("Unknown notification channel status"); |
| 895 | ret = -1; |
| 896 | goto end; |
| 897 | } |
| 898 | |
| 899 | notification_condition = lttng_notification_get_condition(notification); |
| 900 | notification_evaluation = lttng_notification_get_evaluation(notification); |
| 901 | |
| 902 | ret = handle_condition(notification_condition, notification_evaluation, |
| 903 | handle->notification_thread_handle); |
| 904 | |
| 905 | end: |
| 906 | lttng_notification_destroy(notification); |
| 907 | return ret; |
| 908 | } |
| 909 | |
| 910 | void *thread_rotation(void *data) |
| 911 | { |
| 912 | int ret; |
| 913 | struct rotation_thread_handle *handle = data; |
| 914 | struct rotation_thread thread; |
| 915 | |
| 916 | DBG("[rotation-thread] Started rotation thread"); |
| 917 | |
| 918 | if (!handle) { |
| 919 | ERR("[rotation-thread] Invalid thread context provided"); |
| 920 | goto end; |
| 921 | } |
| 922 | |
| 923 | rcu_register_thread(); |
| 924 | rcu_thread_online(); |
| 925 | |
| 926 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_ROTATION); |
| 927 | health_code_update(); |
| 928 | |
| 929 | ret = init_thread_state(handle, &thread); |
| 930 | if (ret) { |
| 931 | goto error; |
| 932 | } |
| 933 | |
| 934 | /* Ready to handle client connections. */ |
| 935 | sessiond_notify_ready(); |
| 936 | |
| 937 | while (true) { |
| 938 | int fd_count, i; |
| 939 | |
| 940 | health_poll_entry(); |
| 941 | DBG("[rotation-thread] Entering poll wait"); |
| 942 | ret = lttng_poll_wait(&thread.events, -1); |
| 943 | DBG("[rotation-thread] Poll wait returned (%i)", ret); |
| 944 | health_poll_exit(); |
| 945 | if (ret < 0) { |
| 946 | /* |
| 947 | * Restart interrupted system call. |
| 948 | */ |
| 949 | if (errno == EINTR) { |
| 950 | continue; |
| 951 | } |
| 952 | ERR("[rotation-thread] Error encountered during lttng_poll_wait (%i)", ret); |
| 953 | goto error; |
| 954 | } |
| 955 | |
| 956 | fd_count = ret; |
| 957 | for (i = 0; i < fd_count; i++) { |
| 958 | int fd = LTTNG_POLL_GETFD(&thread.events, i); |
| 959 | uint32_t revents = LTTNG_POLL_GETEV(&thread.events, i); |
| 960 | |
| 961 | DBG("[rotation-thread] Handling fd (%i) activity (%u)", |
| 962 | fd, revents); |
| 963 | |
| 964 | if (revents & LPOLLERR) { |
| 965 | ERR("[rotation-thread] Polling returned an error on fd %i", fd); |
| 966 | goto error; |
| 967 | } |
| 968 | |
| 969 | if (sessiond_check_thread_quit_pipe(fd, revents)) { |
| 970 | DBG("[rotation-thread] Quit pipe activity"); |
| 971 | /* TODO flush the queue. */ |
| 972 | goto exit; |
| 973 | } else if (fd == lttng_pipe_get_readfd(handle->rotation_timer_queue->event_pipe)) { |
| 974 | ret = handle_job_queue(handle, &thread, |
| 975 | handle->rotation_timer_queue); |
| 976 | if (ret) { |
| 977 | ERR("[rotation-thread] Failed to handle rotation timer pipe event"); |
| 978 | goto error; |
| 979 | } |
| 980 | } else if (fd == rotate_notification_channel->socket) { |
| 981 | ret = handle_notification_channel(fd, handle, |
| 982 | &thread); |
| 983 | if (ret) { |
| 984 | ERR("[rotation-thread] Error occurred while handling activity on notification channel socket"); |
| 985 | goto error; |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | exit: |
| 991 | error: |
| 992 | DBG("[rotation-thread] Exit"); |
| 993 | fini_thread_state(&thread); |
| 994 | health_unregister(health_sessiond); |
| 995 | rcu_thread_offline(); |
| 996 | rcu_unregister_thread(); |
| 997 | end: |
| 998 | return NULL; |
| 999 | } |