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