2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <lttng/trigger/trigger.h>
20 #include <common/error.h>
21 #include <common/config/session-config.h>
22 #include <common/defaults.h>
23 #include <common/utils.h>
24 #include <common/futex.h>
25 #include <common/align.h>
26 #include <common/time.h>
27 #include <common/hashtable/utils.h>
28 #include <sys/eventfd.h>
34 #include <common/kernel-ctl/kernel-ctl.h>
35 #include "rotation-thread.h"
36 #include "lttng-sessiond.h"
37 #include "health-sessiond.h"
42 #include <urcu/list.h>
43 #include <urcu/rculfhash.h>
45 struct cds_lfht
*channel_pending_rotate_ht
;
48 void channel_rotation_info_destroy(struct rotation_channel_info
*channel_info
)
55 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
57 struct rotation_channel_key
*channel_key
= (struct rotation_channel_key
*) key
;
58 struct rotation_channel_info
*channel_info
;
60 channel_info
= caa_container_of(node
, struct rotation_channel_info
,
61 rotate_channels_ht_node
);
63 return !!((channel_key
->key
== channel_info
->channel_key
.key
) &&
64 (channel_key
->domain
== channel_info
->channel_key
.domain
));
68 struct rotation_channel_info
*lookup_channel_pending(uint64_t key
,
69 enum lttng_domain_type domain
)
71 struct cds_lfht_iter iter
;
72 struct cds_lfht_node
*node
;
73 struct rotation_channel_info
*channel_info
= NULL
;
74 struct rotation_channel_key channel_key
= { .key
= key
,
77 cds_lfht_lookup(channel_pending_rotate_ht
,
78 hash_channel_key(&channel_key
),
81 node
= cds_lfht_iter_get_node(&iter
);
86 channel_info
= caa_container_of(node
, struct rotation_channel_info
,
87 rotate_channels_ht_node
);
88 cds_lfht_del(channel_pending_rotate_ht
, node
);
94 * Destroy the thread data previously created by the init function.
96 void rotation_thread_handle_destroy(
97 struct rotation_thread_handle
*handle
)
105 if (handle
->ust32_consumer
>= 0) {
106 ret
= close(handle
->ust32_consumer
);
108 PERROR("close 32-bit consumer channel rotation pipe");
111 if (handle
->ust64_consumer
>= 0) {
112 ret
= close(handle
->ust64_consumer
);
114 PERROR("close 64-bit consumer channel rotation pipe");
117 if (handle
->kernel_consumer
>= 0) {
118 ret
= close(handle
->kernel_consumer
);
120 PERROR("close kernel consumer channel rotation pipe");
128 struct rotation_thread_handle
*rotation_thread_handle_create(
129 struct lttng_pipe
*ust32_channel_rotate_pipe
,
130 struct lttng_pipe
*ust64_channel_rotate_pipe
,
131 struct lttng_pipe
*kernel_channel_rotate_pipe
,
132 int thread_quit_pipe
, int rotate_timer_pipe
)
134 struct rotation_thread_handle
*handle
;
136 handle
= zmalloc(sizeof(*handle
));
141 if (ust32_channel_rotate_pipe
) {
142 handle
->ust32_consumer
=
143 lttng_pipe_release_readfd(
144 ust32_channel_rotate_pipe
);
145 if (handle
->ust32_consumer
< 0) {
149 handle
->ust32_consumer
= -1;
151 if (ust64_channel_rotate_pipe
) {
152 handle
->ust64_consumer
=
153 lttng_pipe_release_readfd(
154 ust64_channel_rotate_pipe
);
155 if (handle
->ust64_consumer
< 0) {
159 handle
->ust64_consumer
= -1;
161 if (kernel_channel_rotate_pipe
) {
162 handle
->kernel_consumer
=
163 lttng_pipe_release_readfd(
164 kernel_channel_rotate_pipe
);
165 if (handle
->kernel_consumer
< 0) {
169 handle
->kernel_consumer
= -1;
171 handle
->thread_quit_pipe
= thread_quit_pipe
;
172 handle
->rotate_timer_pipe
= rotate_timer_pipe
;
177 rotation_thread_handle_destroy(handle
);
182 int init_poll_set(struct lttng_poll_event
*poll_set
,
183 struct rotation_thread_handle
*handle
)
188 * Create pollset with size 5:
189 * - sessiond quit pipe
190 * - consumerd (32-bit user space) channel rotate pipe,
191 * - consumerd (64-bit user space) channel rotate pipe,
192 * - consumerd (kernel) channel rotate pipe.
193 * - sessiond rotate pending pipe
195 ret
= lttng_poll_create(poll_set
, 5, LTTNG_CLOEXEC
);
200 ret
= lttng_poll_add(poll_set
, handle
->thread_quit_pipe
,
203 ERR("[rotation-thread] Failed to add thread_quit_pipe fd to pollset");
206 ret
= lttng_poll_add(poll_set
, handle
->rotate_timer_pipe
,
209 ERR("[rotation-thread] Failed to add rotate_pending fd to pollset");
212 ret
= lttng_poll_add(poll_set
, handle
->ust32_consumer
,
215 ERR("[rotation-thread] Failed to add ust-32 channel rotation pipe fd to pollset");
218 ret
= lttng_poll_add(poll_set
, handle
->ust64_consumer
,
221 ERR("[rotation-thread] Failed to add ust-64 channel rotation pipe fd to pollset");
224 if (handle
->kernel_consumer
< 0) {
227 ret
= lttng_poll_add(poll_set
, handle
->kernel_consumer
,
230 ERR("[rotation-thread] Failed to add kernel channel rotation pipe fd to pollset");
237 lttng_poll_clean(poll_set
);
242 void fini_thread_state(struct rotation_thread_state
*state
)
244 lttng_poll_clean(&state
->events
);
248 int init_thread_state(struct rotation_thread_handle
*handle
,
249 struct rotation_thread_state
*state
)
253 memset(state
, 0, sizeof(*state
));
254 lttng_poll_init(&state
->events
);
256 ret
= init_poll_set(&state
->events
, handle
);
261 channel_pending_rotate_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
262 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
263 if (!channel_pending_rotate_ht
) {
272 int handle_channel_rotation_pipe(int fd
, uint32_t revents
,
273 struct rotation_thread_handle
*handle
,
274 struct rotation_thread_state
*state
)
277 enum lttng_domain_type domain
;
278 struct rotation_channel_info
*channel_info
;
281 if (fd
== handle
->ust32_consumer
||
282 fd
== handle
->ust64_consumer
) {
283 domain
= LTTNG_DOMAIN_UST
;
284 } else if (fd
== handle
->kernel_consumer
) {
285 domain
= LTTNG_DOMAIN_KERNEL
;
290 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
291 ret
= lttng_poll_del(&state
->events
, fd
);
293 ERR("[rotation-thread] Failed to remove consumer "
294 "rotation pipe from poll set");
300 ret
= read(fd
, &key
, sizeof(key
));
301 } while (ret
== -1 && errno
== EINTR
);
302 if (ret
!= sizeof(key
)) {
303 ERR("[rotation-thread] Failed to read from pipe (fd = %i)",
309 DBG("[rotation-thread] Received notification for chan %" PRIu64
310 ", domain %d\n", key
, domain
);
312 channel_info
= lookup_channel_pending(key
, domain
);
314 ERR("[rotation-thread] Failed to find channel_info (key = %"
320 if (--channel_info
->session
->nr_chan_rotate_pending
== 0) {
321 time_t now
= time(NULL
);
323 if (now
== (time_t) -1) {
324 channel_info
->session
->rotate_status
= LTTNG_ROTATE_ERROR
;
325 ret
= LTTNG_ERR_ROTATE_NOT_AVAILABLE
;
329 ret
= rename_complete_chunk(channel_info
->session
, now
);
331 ERR("Failed to rename completed rotation chunk");
334 channel_info
->session
->rotate_pending
= false;
337 channel_rotation_info_destroy(channel_info
);
346 int handle_rotate_timer_pipe(int fd
, uint32_t revents
,
347 struct rotation_thread_handle
*handle
,
348 struct rotation_thread_state
*state
)
352 struct ltt_session
*session
;
354 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
355 ret
= lttng_poll_del(&state
->events
, fd
);
357 ERR("[rotation-thread] Failed to remove consumer "
358 "rotate pending pipe from poll set");
364 ret
= read(fd
, &session_id
, sizeof(session_id
));
365 } while (ret
== -1 && errno
== EINTR
);
366 if (ret
!= sizeof(session_id
)) {
367 ERR("[rotation-thread] Failed to read from pipe (fd = %i)",
373 session
= session_find_by_id(session_id
);
375 ERR("[rotation-thread] Session %" PRIu64
" not found",
381 DBG("[rotation-thread] Check rotate pending on session %" PRIu64
,
383 ret
= relay_rotate_pending(session
, session
->rotate_count
- 1);
385 ERR("[rotation-thread] Check relay rotate pending");
389 DBG("[rotation-thread] Rotation completed on the relay for "
390 "session %" PRIu64
, session_id
);
391 /* TODO: delete timer */
392 } else if (ret
== 1) {
393 DBG("[rotation-thread] Rotation still pending on the relay for "
394 "session %" PRIu64
, session_id
);
396 fprintf(stderr
, "RET PENDING: %d\n", ret
);
404 void *thread_rotation(void *data
)
407 struct rotation_thread_handle
*handle
= data
;
408 struct rotation_thread_state state
;
410 DBG("[rotation-thread] Started rotation thread");
413 ERR("[rotation-thread] Invalid thread context provided");
417 rcu_register_thread();
420 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_ROTATION
);
421 health_code_update();
423 ret
= init_thread_state(handle
, &state
);
428 /* Ready to handle client connections. */
429 sessiond_notify_ready();
435 DBG("[rotation-thread] Entering poll wait");
436 ret
= lttng_poll_wait(&state
.events
, -1);
437 DBG("[rotation-thread] Poll wait returned (%i)", ret
);
441 * Restart interrupted system call.
443 if (errno
== EINTR
) {
446 ERR("[rotation-thread] Error encountered during lttng_poll_wait (%i)", ret
);
451 for (i
= 0; i
< fd_count
; i
++) {
452 int fd
= LTTNG_POLL_GETFD(&state
.events
, i
);
453 uint32_t revents
= LTTNG_POLL_GETEV(&state
.events
, i
);
455 DBG("[rotation-thread] Handling fd (%i) activity (%u)",
458 if (fd
== handle
->thread_quit_pipe
) {
459 DBG("[rotation-thread] Quit pipe activity");
461 } else if (fd
== handle
->rotate_timer_pipe
) {
462 ret
= handle_rotate_timer_pipe(fd
, revents
,
465 ERR("[rotation-thread] Rotate pending");
468 } else if (fd
== handle
->ust32_consumer
||
469 fd
== handle
->ust64_consumer
||
470 fd
== handle
->kernel_consumer
) {
471 ret
= handle_channel_rotation_pipe(fd
,
472 revents
, handle
, &state
);
474 ERR("[rotation-thread] Exit main loop");
482 fini_thread_state(&state
);
483 health_unregister(health_sessiond
);
484 rcu_thread_offline();
485 rcu_unregister_thread();