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