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