Commit | Line | Data |
---|---|---|
db66e574 JD |
1 | /* |
2 | * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com> | |
92816cc3 | 3 | * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
db66e574 JD |
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> | |
5c408ad8 | 37 | #include <lttng/rotate-internal.h> |
db66e574 JD |
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" | |
8e319828 | 45 | #include "timer.h" |
17dd1232 | 46 | #include "notification-thread-commands.h" |
db66e574 JD |
47 | |
48 | #include <urcu.h> | |
49 | #include <urcu/list.h> | |
db66e574 | 50 | |
90936dcf JD |
51 | struct lttng_notification_channel *rotate_notification_channel = NULL; |
52 | ||
92816cc3 | 53 | struct rotation_thread { |
db66e574 JD |
54 | struct lttng_poll_event events; |
55 | }; | |
56 | ||
92816cc3 JG |
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 { | |
92816cc3 JG |
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 | ||
db66e574 | 81 | static |
92816cc3 | 82 | const char *get_job_type_str(enum rotation_thread_job_type job_type) |
db66e574 | 83 | { |
92816cc3 JG |
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 | } | |
db66e574 JD |
92 | } |
93 | ||
92816cc3 | 94 | struct rotation_thread_timer_queue *rotation_thread_timer_queue_create(void) |
db66e574 | 95 | { |
92816cc3 | 96 | struct rotation_thread_timer_queue *queue = NULL; |
db66e574 | 97 | |
92816cc3 JG |
98 | queue = zmalloc(sizeof(*queue)); |
99 | if (!queue) { | |
100 | PERROR("Failed to allocate timer rotate queue"); | |
101 | goto end; | |
102 | } | |
db66e574 | 103 | |
92816cc3 JG |
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; | |
db66e574 JD |
109 | } |
110 | ||
92816cc3 | 111 | void log_job_destruction(const struct rotation_thread_job *job) |
db66e574 | 112 | { |
92816cc3 JG |
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(); | |
db66e574 JD |
132 | } |
133 | ||
92816cc3 JG |
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); | |
db66e574 JD |
136 | } |
137 | ||
92816cc3 JG |
138 | void rotation_thread_timer_queue_destroy( |
139 | struct rotation_thread_timer_queue *queue) | |
db66e574 | 140 | { |
92816cc3 | 141 | struct rotation_thread_job *job, *tmp_job; |
db66e574 | 142 | |
92816cc3 JG |
143 | if (!queue) { |
144 | return; | |
db66e574 JD |
145 | } |
146 | ||
92816cc3 JG |
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); | |
db66e574 | 155 | } |
92816cc3 JG |
156 | pthread_mutex_unlock(&queue->lock); |
157 | pthread_mutex_destroy(&queue->lock); | |
158 | free(queue); | |
159 | } | |
db66e574 | 160 | |
92816cc3 JG |
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 | { | |
db66e574 JD |
167 | free(handle); |
168 | } | |
169 | ||
170 | struct rotation_thread_handle *rotation_thread_handle_create( | |
90936dcf JD |
171 | struct rotation_thread_timer_queue *rotation_timer_queue, |
172 | struct notification_thread_handle *notification_thread_handle, | |
173 | sem_t *notification_thread_ready) | |
db66e574 JD |
174 | { |
175 | struct rotation_thread_handle *handle; | |
176 | ||
177 | handle = zmalloc(sizeof(*handle)); | |
178 | if (!handle) { | |
179 | goto end; | |
180 | } | |
181 | ||
92816cc3 JG |
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; | |
db66e574 | 205 | } |
db66e574 | 206 | } |
92816cc3 JG |
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; | |
db66e574 | 226 | } |
92816cc3 JG |
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; | |
db66e574 | 255 | } |
92816cc3 JG |
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; | |
db66e574 | 259 | } |
db66e574 JD |
260 | |
261 | end: | |
92816cc3 | 262 | pthread_mutex_unlock(&queue->lock); |
db66e574 JD |
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 | /* | |
92816cc3 JG |
272 | * Create pollset with size 2: |
273 | * - quit pipe, | |
274 | * - rotation thread timer queue pipe, | |
db66e574 | 275 | */ |
a7333da7 JG |
276 | ret = sessiond_set_thread_pollset(poll_set, 2); |
277 | if (ret) { | |
db66e574 JD |
278 | goto error; |
279 | } | |
d086f507 JD |
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 | } | |
db66e574 | 287 | |
db66e574 JD |
288 | return ret; |
289 | error: | |
290 | lttng_poll_clean(poll_set); | |
291 | return ret; | |
292 | } | |
293 | ||
294 | static | |
92816cc3 | 295 | void fini_thread_state(struct rotation_thread *state) |
db66e574 JD |
296 | { |
297 | lttng_poll_clean(&state->events); | |
90936dcf JD |
298 | if (rotate_notification_channel) { |
299 | lttng_notification_channel_destroy(rotate_notification_channel); | |
300 | } | |
db66e574 JD |
301 | } |
302 | ||
303 | static | |
304 | int init_thread_state(struct rotation_thread_handle *handle, | |
92816cc3 | 305 | struct rotation_thread *state) |
db66e574 JD |
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 | ||
90936dcf JD |
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 | ||
db66e574 JD |
337 | end: |
338 | return ret; | |
339 | } | |
340 | ||
341 | static | |
92816cc3 JG |
342 | int check_session_rotation_pending_local_on_consumer( |
343 | const struct ltt_session *session, | |
344 | struct consumer_socket *socket, bool *rotation_completed) | |
db66e574 | 345 | { |
92816cc3 JG |
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; | |
db66e574 | 372 | } else { |
92816cc3 JG |
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; | |
db66e574 | 379 | } |
92816cc3 JG |
380 | return ret; |
381 | } | |
db66e574 | 382 | |
92816cc3 JG |
383 | static |
384 | int check_session_rotation_pending_local(struct ltt_session *session) | |
385 | { | |
db582e11 | 386 | int ret = 0; |
92816cc3 JG |
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; | |
db66e574 | 408 | } |
db66e574 JD |
409 | } |
410 | ||
92816cc3 JG |
411 | skip_ust: |
412 | if (!session->kernel_session) { | |
413 | goto skip_kernel; | |
db66e574 | 414 | } |
92816cc3 JG |
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(); | |
db66e574 | 426 | |
92816cc3 JG |
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; | |
db66e574 | 432 | } |
92816cc3 JG |
433 | if (ret) { |
434 | session->rotation_state = LTTNG_ROTATION_STATE_ERROR; | |
db66e574 | 435 | } |
92816cc3 JG |
436 | return 0; |
437 | } | |
db66e574 | 438 | |
92816cc3 JG |
439 | static |
440 | int check_session_rotation_pending_relay(struct ltt_session *session) | |
441 | { | |
442 | int ret; | |
443 | struct consumer_socket *socket; | |
444 | struct cds_lfht_iter iter; | |
445 | bool rotation_completed = true; | |
446 | const struct consumer_output *output; | |
db66e574 | 447 | |
92816cc3 JG |
448 | /* |
449 | * Check for a pending rotation on any consumer as we only use | |
450 | * it as a "tunnel" to the relayd. | |
451 | */ | |
17dd1232 | 452 | |
92816cc3 JG |
453 | rcu_read_lock(); |
454 | if (session->ust_session) { | |
455 | cds_lfht_first(session->ust_session->consumer->socks->ht, | |
456 | &iter); | |
457 | output = session->ust_session->consumer; | |
458 | } else { | |
459 | cds_lfht_first(session->kernel_session->consumer->socks->ht, | |
460 | &iter); | |
461 | output = session->kernel_session->consumer; | |
db66e574 | 462 | } |
92816cc3 | 463 | assert(cds_lfht_iter_get_node(&iter)); |
db66e574 | 464 | |
92816cc3 JG |
465 | socket = caa_container_of(cds_lfht_iter_get_node(&iter), |
466 | typeof(*socket), node.node); | |
467 | ||
468 | pthread_mutex_lock(socket->lock); | |
469 | DBG("[rotation-thread] Checking for pending relay rotation on session \"%s\", trace archive %" PRIu64 " through the %s consumer", | |
470 | session->name, session->current_archive_id - 1, | |
471 | lttng_consumer_type_str(socket->type)); | |
472 | ret = consumer_check_rotation_pending_relay(socket, | |
473 | output, | |
474 | session->id, | |
475 | session->current_archive_id - 1); | |
476 | pthread_mutex_unlock(socket->lock); | |
477 | ||
478 | if (ret == 0) { | |
479 | /* Rotation was completed on the relay. */ | |
480 | DBG("[rotation-thread] Relay rotation of trace archive %" PRIu64 " of session \"%s\" was completed", | |
481 | session->current_archive_id - 1, | |
482 | session->name); | |
483 | } else if (ret == 1) { | |
484 | /* Rotation pending on relay. */ | |
485 | DBG("[rotation-thread] Relay rotation of trace archive %" PRIu64 " of session \"%s\" is pending", | |
486 | session->current_archive_id - 1, | |
487 | session->name); | |
488 | rotation_completed = false; | |
489 | } else { | |
490 | /* Not a fatal error. */ | |
491 | ERR("[rotation-thread] Encountered an error when checking if rotation of trace archive %" PRIu64 " of session \"%s\" is pending on the relay", | |
492 | session->current_archive_id - 1, | |
493 | session->name); | |
494 | session->rotation_state = LTTNG_ROTATION_STATE_ERROR; | |
495 | rotation_completed = false; | |
496 | } | |
db66e574 | 497 | |
db66e574 | 498 | rcu_read_unlock(); |
92816cc3 JG |
499 | |
500 | if (rotation_completed) { | |
501 | DBG("[rotation-thread] Totation of trace archive %" PRIu64 " of session \"%s\" is complete on the relay", | |
502 | session->current_archive_id - 1, | |
503 | session->name); | |
504 | session->rotation_pending_relay = false; | |
505 | } | |
506 | return 0; | |
db66e574 JD |
507 | } |
508 | ||
d88744a4 | 509 | /* |
92816cc3 | 510 | * Check if the last rotation was completed, called with session lock held. |
d88744a4 JD |
511 | */ |
512 | static | |
92816cc3 JG |
513 | int check_session_rotation_pending(struct ltt_session *session, |
514 | struct notification_thread_handle *notification_thread_handle) | |
d88744a4 JD |
515 | { |
516 | int ret; | |
92816cc3 JG |
517 | struct lttng_trace_archive_location *location; |
518 | time_t now; | |
d88744a4 | 519 | |
92816cc3 JG |
520 | DBG("[rotation-thread] Checking for pending rotation on session \"%s\", trace archive %" PRIu64, |
521 | session->name, session->current_archive_id - 1); | |
522 | ||
523 | if (session->rotation_pending_local) { | |
524 | /* Updates session->rotation_pending_local as needed. */ | |
525 | ret = check_session_rotation_pending_local(session); | |
526 | if (ret) { | |
527 | goto end; | |
528 | } | |
17dd1232 | 529 | |
d88744a4 | 530 | /* |
92816cc3 JG |
531 | * No need to check for a pending rotation on the relay |
532 | * since the rotation is not even completed locally yet. | |
d88744a4 | 533 | */ |
92816cc3 JG |
534 | if (session->rotation_pending_local) { |
535 | goto end; | |
17dd1232 | 536 | } |
92816cc3 JG |
537 | } |
538 | ||
539 | if (session->rotation_pending_relay) { | |
540 | /* Updates session->rotation_pending_relay as needed. */ | |
541 | ret = check_session_rotation_pending_relay(session); | |
d88744a4 | 542 | if (ret) { |
92816cc3 JG |
543 | goto end; |
544 | } | |
545 | ||
546 | if (session->rotation_pending_relay) { | |
d88744a4 JD |
547 | goto end; |
548 | } | |
549 | } | |
550 | ||
92816cc3 JG |
551 | DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " completed for " |
552 | "session %s", session->current_archive_id - 1, | |
553 | session->name); | |
d88744a4 | 554 | |
92816cc3 JG |
555 | /* Rename the completed trace archive's location. */ |
556 | now = time(NULL); | |
557 | if (now == (time_t) -1) { | |
558 | session->rotation_state = LTTNG_ROTATION_STATE_ERROR; | |
559 | ret = LTTNG_ERR_UNK; | |
560 | goto end; | |
561 | } | |
562 | ||
563 | ret = rename_completed_chunk(session, now); | |
564 | if (ret < 0) { | |
565 | ERR("Failed to rename completed rotation chunk"); | |
566 | goto end; | |
567 | } | |
568 | session->last_chunk_start_ts = session->current_chunk_start_ts; | |
569 | ||
570 | /* | |
571 | * Now we can clear the "ONGOING" state in the session. New | |
572 | * rotations can start now. | |
573 | */ | |
574 | session->rotation_state = LTTNG_ROTATION_STATE_COMPLETED; | |
575 | ||
576 | /* Ownership of location is transferred. */ | |
577 | location = session_get_trace_archive_location(session); | |
578 | ret = notification_thread_command_session_rotation_completed( | |
579 | notification_thread_handle, | |
580 | session->name, | |
581 | session->uid, | |
582 | session->gid, | |
583 | session->current_archive_id, | |
584 | location); | |
585 | if (ret != LTTNG_OK) { | |
586 | ERR("[rotation-thread] Failed to notify notification thread of completed rotation for session %s", | |
587 | session->name); | |
588 | } | |
589 | ||
9402c5b9 JG |
590 | if (!session->active) { |
591 | /* | |
592 | * A stop command was issued during the rotation, it is | |
593 | * up to the rotation completion check to perform the | |
594 | * renaming of the last chunk that was produced. | |
595 | */ | |
596 | ret = notification_thread_command_session_rotation_ongoing( | |
597 | notification_thread_handle, | |
598 | session->name, | |
599 | session->uid, | |
600 | session->gid, | |
601 | session->current_archive_id); | |
602 | if (ret != LTTNG_OK) { | |
603 | ERR("[rotation-thread] Failed to notify notification thread of completed rotation for session %s", | |
604 | session->name); | |
605 | } | |
606 | ||
607 | ret = rename_active_chunk(session); | |
608 | if (ret < 0) { | |
609 | ERR("[rotation-thread] Failed to rename active rotation chunk"); | |
610 | goto end; | |
611 | } | |
612 | ||
613 | /* Ownership of location is transferred. */ | |
614 | location = session_get_trace_archive_location(session); | |
615 | ret = notification_thread_command_session_rotation_completed( | |
616 | notification_thread_handle, | |
617 | session->name, | |
618 | session->uid, | |
619 | session->gid, | |
620 | session->current_archive_id, | |
621 | location); | |
622 | if (ret != LTTNG_OK) { | |
623 | ERR("[rotation-thread] Failed to notify notification thread of completed rotation for session %s", | |
624 | session->name); | |
625 | } | |
626 | } | |
627 | ||
92816cc3 | 628 | ret = 0; |
d88744a4 | 629 | end: |
92816cc3 JG |
630 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
631 | DBG("[rotation-thread] Rotation of trace archive %" PRIu64 " is still pending for session %s", | |
632 | session->current_archive_id - 1, session->name); | |
633 | ret = timer_session_rotation_pending_check_start(session, | |
634 | DEFAULT_ROTATE_PENDING_TIMER); | |
635 | if (ret) { | |
636 | ERR("Re-enabling rotate pending timer"); | |
637 | ret = -1; | |
638 | goto end; | |
639 | } | |
640 | } | |
641 | ||
d88744a4 JD |
642 | return ret; |
643 | } | |
644 | ||
92816cc3 | 645 | /* Call with the session lock held. */ |
259c2674 | 646 | static |
92816cc3 | 647 | int launch_session_rotation(struct ltt_session *session) |
259c2674 JD |
648 | { |
649 | int ret; | |
92816cc3 | 650 | struct lttng_rotate_session_return rotation_return; |
259c2674 | 651 | |
92816cc3 JG |
652 | DBG("[rotation-thread] Launching scheduled time-based rotation on session \"%s\"", |
653 | session->name); | |
259c2674 | 654 | |
92816cc3 JG |
655 | ret = cmd_rotate_session(session, &rotation_return); |
656 | if (ret == LTTNG_OK) { | |
657 | DBG("[rotation-thread] Scheduled time-based rotation successfully launched on session \"%s\"", | |
658 | session->name); | |
659 | } else { | |
660 | /* Don't consider errors as fatal. */ | |
661 | DBG("[rotation-thread] Scheduled time-based rotation aborted for session %s: %s", | |
662 | session->name, lttng_strerror(ret)); | |
259c2674 | 663 | } |
92816cc3 JG |
664 | return 0; |
665 | } | |
259c2674 | 666 | |
92816cc3 JG |
667 | static |
668 | int run_job(struct rotation_thread_job *job, struct ltt_session *session, | |
669 | struct notification_thread_handle *notification_thread_handle) | |
670 | { | |
671 | int ret; | |
259c2674 | 672 | |
92816cc3 JG |
673 | switch (job->type) { |
674 | case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION: | |
675 | ret = launch_session_rotation(session); | |
676 | break; | |
677 | case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION: | |
678 | ret = check_session_rotation_pending(session, | |
679 | notification_thread_handle); | |
680 | break; | |
681 | default: | |
682 | abort(); | |
259c2674 | 683 | } |
259c2674 JD |
684 | return ret; |
685 | } | |
686 | ||
d88744a4 | 687 | static |
92816cc3 JG |
688 | int handle_job_queue(struct rotation_thread_handle *handle, |
689 | struct rotation_thread *state, | |
d88744a4 JD |
690 | struct rotation_thread_timer_queue *queue) |
691 | { | |
692 | int ret = 0; | |
693 | int fd = lttng_pipe_get_readfd(queue->event_pipe); | |
694 | struct ltt_session *session; | |
92816cc3 | 695 | char buf; |
d88744a4 | 696 | |
92816cc3 | 697 | ret = lttng_read(fd, &buf, 1); |
d88744a4 JD |
698 | if (ret != 1) { |
699 | ERR("[rotation-thread] Failed to read from wakeup pipe (fd = %i)", fd); | |
700 | ret = -1; | |
701 | goto end; | |
702 | } | |
703 | ||
704 | for (;;) { | |
92816cc3 | 705 | struct rotation_thread_job *job; |
d88744a4 | 706 | |
92816cc3 | 707 | /* Take the queue lock only to pop an element from the list. */ |
d88744a4 JD |
708 | pthread_mutex_lock(&queue->lock); |
709 | if (cds_list_empty(&queue->list)) { | |
710 | pthread_mutex_unlock(&queue->lock); | |
711 | break; | |
712 | } | |
92816cc3 JG |
713 | job = cds_list_first_entry(&queue->list, |
714 | typeof(*job), head); | |
715 | cds_list_del(&job->head); | |
d88744a4 JD |
716 | pthread_mutex_unlock(&queue->lock); |
717 | ||
d88744a4 | 718 | session_lock_list(); |
92816cc3 | 719 | session = session_find_by_id(job->session_id); |
d88744a4 JD |
720 | if (!session) { |
721 | DBG("[rotation-thread] Session %" PRIu64 " not found", | |
92816cc3 | 722 | job->session_id); |
d88744a4 | 723 | /* |
92816cc3 JG |
724 | * This is a non-fatal error, and we cannot report it to |
725 | * the user (timer), so just print the error and | |
726 | * continue the processing. | |
727 | * | |
728 | * While the timer thread will purge pending signals for | |
729 | * a session on the session's destruction, it is | |
730 | * possible for a job targeting that session to have | |
731 | * already been queued before it was destroyed. | |
d88744a4 JD |
732 | */ |
733 | session_unlock_list(); | |
92816cc3 | 734 | free(job); |
d88744a4 JD |
735 | continue; |
736 | } | |
737 | ||
d88744a4 JD |
738 | session_lock(session); |
739 | session_unlock_list(); | |
740 | ||
92816cc3 | 741 | ret = run_job(job, session, handle->notification_thread_handle); |
d88744a4 | 742 | session_unlock(session); |
92816cc3 | 743 | free(job); |
d88744a4 | 744 | if (ret) { |
d88744a4 JD |
745 | goto end; |
746 | } | |
747 | } | |
748 | ||
749 | ret = 0; | |
750 | ||
751 | end: | |
752 | return ret; | |
753 | } | |
754 | ||
92816cc3 JG |
755 | static |
756 | int handle_condition(const struct lttng_condition *condition, | |
90936dcf JD |
757 | const struct lttng_evaluation *evaluation, |
758 | struct notification_thread_handle *notification_thread_handle) | |
759 | { | |
760 | int ret = 0; | |
761 | const char *condition_session_name = NULL; | |
762 | enum lttng_condition_type condition_type; | |
763 | enum lttng_condition_status condition_status; | |
764 | enum lttng_evaluation_status evaluation_status; | |
765 | uint64_t consumed; | |
766 | struct ltt_session *session; | |
767 | ||
768 | condition_type = lttng_condition_get_type(condition); | |
769 | ||
770 | if (condition_type != LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE) { | |
771 | ret = -1; | |
772 | ERR("[rotation-thread] Condition type and session usage type are not the same"); | |
773 | goto end; | |
774 | } | |
775 | ||
776 | /* Fetch info to test */ | |
777 | condition_status = lttng_condition_session_consumed_size_get_session_name( | |
778 | condition, &condition_session_name); | |
779 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
780 | ERR("[rotation-thread] Session name could not be fetched"); | |
781 | ret = -1; | |
782 | goto end; | |
783 | } | |
784 | evaluation_status = lttng_evaluation_session_consumed_size_get_consumed_size(evaluation, | |
785 | &consumed); | |
786 | if (evaluation_status != LTTNG_EVALUATION_STATUS_OK) { | |
787 | ERR("[rotation-thread] Failed to get evaluation"); | |
788 | ret = -1; | |
789 | goto end; | |
790 | } | |
791 | ||
792 | session_lock_list(); | |
793 | session = session_find_by_name(condition_session_name); | |
794 | if (!session) { | |
795 | ret = -1; | |
796 | session_unlock_list(); | |
797 | ERR("[rotation-thread] Session \"%s\" not found", | |
798 | condition_session_name); | |
799 | goto end; | |
800 | } | |
801 | session_lock(session); | |
802 | session_unlock_list(); | |
803 | ||
804 | ret = unsubscribe_session_consumed_size_rotation(session, | |
805 | notification_thread_handle); | |
806 | if (ret) { | |
490b3229 | 807 | goto end_unlock; |
90936dcf JD |
808 | } |
809 | ||
810 | ret = cmd_rotate_session(session, NULL); | |
811 | if (ret == -LTTNG_ERR_ROTATION_PENDING) { | |
812 | DBG("Rotate already pending, subscribe to the next threshold value"); | |
90936dcf JD |
813 | } else if (ret != LTTNG_OK) { |
814 | ERR("[rotation-thread] Failed to rotate on size notification with error: %s", | |
815 | lttng_strerror(ret)); | |
816 | ret = -1; | |
817 | goto end_unlock; | |
818 | } | |
819 | ret = subscribe_session_consumed_size_rotation(session, | |
820 | consumed + session->rotate_size, | |
821 | notification_thread_handle); | |
822 | if (ret) { | |
823 | ERR("[rotation-thread] Failed to subscribe to session consumed size condition"); | |
824 | goto end_unlock; | |
825 | } | |
826 | ret = 0; | |
827 | ||
828 | end_unlock: | |
829 | session_unlock(session); | |
830 | end: | |
831 | return ret; | |
832 | } | |
833 | ||
834 | static | |
92816cc3 | 835 | int handle_notification_channel(int fd, |
90936dcf | 836 | struct rotation_thread_handle *handle, |
92816cc3 | 837 | struct rotation_thread *state) |
90936dcf JD |
838 | { |
839 | int ret; | |
d73ee93f JG |
840 | bool notification_pending; |
841 | struct lttng_notification *notification = NULL; | |
90936dcf JD |
842 | enum lttng_notification_channel_status status; |
843 | const struct lttng_evaluation *notification_evaluation; | |
844 | const struct lttng_condition *notification_condition; | |
845 | ||
d73ee93f JG |
846 | status = lttng_notification_channel_has_pending_notification( |
847 | rotate_notification_channel, ¬ification_pending); | |
848 | if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) { | |
849 | ERR("[rotation-thread ]Error occured while checking for pending notification"); | |
850 | ret = -1; | |
851 | goto end; | |
852 | } | |
853 | ||
854 | if (!notification_pending) { | |
855 | ret = 0; | |
856 | goto end; | |
857 | } | |
858 | ||
90936dcf JD |
859 | /* Receive the next notification. */ |
860 | status = lttng_notification_channel_get_next_notification( | |
861 | rotate_notification_channel, | |
862 | ¬ification); | |
863 | ||
864 | switch (status) { | |
865 | case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK: | |
866 | break; | |
867 | case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED: | |
868 | /* Not an error, we will wait for the next one */ | |
869 | ret = 0; | |
870 | goto end;; | |
871 | case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED: | |
872 | ERR("Notification channel was closed"); | |
873 | ret = -1; | |
874 | goto end; | |
875 | default: | |
876 | /* Unhandled conditions / errors. */ | |
877 | ERR("Unknown notification channel status"); | |
878 | ret = -1; | |
879 | goto end; | |
880 | } | |
881 | ||
882 | notification_condition = lttng_notification_get_condition(notification); | |
883 | notification_evaluation = lttng_notification_get_evaluation(notification); | |
884 | ||
885 | ret = handle_condition(notification_condition, notification_evaluation, | |
886 | handle->notification_thread_handle); | |
887 | ||
888 | end: | |
889 | lttng_notification_destroy(notification); | |
90936dcf JD |
890 | return ret; |
891 | } | |
892 | ||
db66e574 JD |
893 | void *thread_rotation(void *data) |
894 | { | |
895 | int ret; | |
896 | struct rotation_thread_handle *handle = data; | |
92816cc3 | 897 | struct rotation_thread thread; |
db66e574 JD |
898 | |
899 | DBG("[rotation-thread] Started rotation thread"); | |
900 | ||
901 | if (!handle) { | |
902 | ERR("[rotation-thread] Invalid thread context provided"); | |
903 | goto end; | |
904 | } | |
905 | ||
03732c32 JG |
906 | rcu_register_thread(); |
907 | rcu_thread_online(); | |
908 | ||
db66e574 JD |
909 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_ROTATION); |
910 | health_code_update(); | |
911 | ||
92816cc3 | 912 | ret = init_thread_state(handle, &thread); |
db66e574 | 913 | if (ret) { |
f5f8e5cd | 914 | goto error; |
db66e574 JD |
915 | } |
916 | ||
917 | /* Ready to handle client connections. */ | |
918 | sessiond_notify_ready(); | |
919 | ||
920 | while (true) { | |
921 | int fd_count, i; | |
922 | ||
923 | health_poll_entry(); | |
924 | DBG("[rotation-thread] Entering poll wait"); | |
92816cc3 | 925 | ret = lttng_poll_wait(&thread.events, -1); |
db66e574 JD |
926 | DBG("[rotation-thread] Poll wait returned (%i)", ret); |
927 | health_poll_exit(); | |
928 | if (ret < 0) { | |
929 | /* | |
930 | * Restart interrupted system call. | |
931 | */ | |
932 | if (errno == EINTR) { | |
933 | continue; | |
934 | } | |
935 | ERR("[rotation-thread] Error encountered during lttng_poll_wait (%i)", ret); | |
936 | goto error; | |
937 | } | |
938 | ||
939 | fd_count = ret; | |
940 | for (i = 0; i < fd_count; i++) { | |
92816cc3 JG |
941 | int fd = LTTNG_POLL_GETFD(&thread.events, i); |
942 | uint32_t revents = LTTNG_POLL_GETEV(&thread.events, i); | |
db66e574 JD |
943 | |
944 | DBG("[rotation-thread] Handling fd (%i) activity (%u)", | |
945 | fd, revents); | |
946 | ||
92816cc3 JG |
947 | if (revents & LPOLLERR) { |
948 | ERR("[rotation-thread] Polling returned an error on fd %i", fd); | |
949 | goto error; | |
950 | } | |
951 | ||
a7333da7 | 952 | if (sessiond_check_thread_quit_pipe(fd, revents)) { |
db66e574 | 953 | DBG("[rotation-thread] Quit pipe activity"); |
92816cc3 | 954 | /* TODO flush the queue. */ |
db66e574 | 955 | goto exit; |
d88744a4 | 956 | } else if (fd == lttng_pipe_get_readfd(handle->rotation_timer_queue->event_pipe)) { |
92816cc3 JG |
957 | ret = handle_job_queue(handle, &thread, |
958 | handle->rotation_timer_queue); | |
d88744a4 JD |
959 | if (ret) { |
960 | ERR("[rotation-thread] Failed to handle rotation timer pipe event"); | |
961 | goto error; | |
962 | } | |
90936dcf | 963 | } else if (fd == rotate_notification_channel->socket) { |
92816cc3 JG |
964 | ret = handle_notification_channel(fd, handle, |
965 | &thread); | |
90936dcf JD |
966 | if (ret) { |
967 | ERR("[rotation-thread] Error occured while handling activity on notification channel socket"); | |
968 | goto error; | |
969 | } | |
db66e574 JD |
970 | } |
971 | } | |
972 | } | |
973 | exit: | |
974 | error: | |
975 | DBG("[rotation-thread] Exit"); | |
92816cc3 | 976 | fini_thread_state(&thread); |
db66e574 | 977 | health_unregister(health_sessiond); |
03732c32 JG |
978 | rcu_thread_offline(); |
979 | rcu_unregister_thread(); | |
db66e574 JD |
980 | end: |
981 | return NULL; | |
982 | } |