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
)
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
;
176 rotation_thread_handle_destroy(handle
);
181 int init_poll_set(struct lttng_poll_event
*poll_set
,
182 struct rotation_thread_handle
*handle
)
187 * Create pollset with size 4:
188 * - sessiond quit pipe
189 * - consumerd (32-bit user space) channel rotate pipe,
190 * - consumerd (64-bit user space) channel rotate pipe,
191 * - consumerd (kernel) channel rotate pipe.
193 ret
= lttng_poll_create(poll_set
, 4, LTTNG_CLOEXEC
);
198 ret
= lttng_poll_add(poll_set
, handle
->thread_quit_pipe
,
201 ERR("[rotation-thread] Failed to add thread_quit_pipe fd to pollset");
204 ret
= lttng_poll_add(poll_set
, handle
->ust32_consumer
,
207 ERR("[rotation-thread] Failed to add ust-32 channel rotation pipe fd to pollset");
210 ret
= lttng_poll_add(poll_set
, handle
->ust64_consumer
,
213 ERR("[rotation-thread] Failed to add ust-64 channel rotation pipe fd to pollset");
216 if (handle
->kernel_consumer
< 0) {
219 ret
= lttng_poll_add(poll_set
, handle
->kernel_consumer
,
222 ERR("[rotation-thread] Failed to add kernel channel rotation pipe fd to pollset");
229 lttng_poll_clean(poll_set
);
234 void fini_thread_state(struct rotation_thread_state
*state
)
236 lttng_poll_clean(&state
->events
);
240 int init_thread_state(struct rotation_thread_handle
*handle
,
241 struct rotation_thread_state
*state
)
245 memset(state
, 0, sizeof(*state
));
246 lttng_poll_init(&state
->events
);
248 ret
= init_poll_set(&state
->events
, handle
);
253 channel_pending_rotate_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
254 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
255 if (!channel_pending_rotate_ht
) {
264 int handle_channel_rotation_pipe(int fd
, uint32_t revents
,
265 struct rotation_thread_handle
*handle
,
266 struct rotation_thread_state
*state
)
269 enum lttng_domain_type domain
;
270 struct rotation_channel_info
*channel_info
;
273 if (fd
== handle
->ust32_consumer
||
274 fd
== handle
->ust64_consumer
) {
275 domain
= LTTNG_DOMAIN_UST
;
276 } else if (fd
== handle
->kernel_consumer
) {
277 domain
= LTTNG_DOMAIN_KERNEL
;
282 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
283 ret
= lttng_poll_del(&state
->events
, fd
);
285 ERR("[rotation-thread] Failed to remove consumer rotation pipe from poll set");
291 ret
= read(fd
, &key
, sizeof(key
));
292 } while (ret
== -1 && errno
== EINTR
);
293 if (ret
!= sizeof(key
)) {
294 ERR("[rotation-thread] Failed to read from pipe (fd = %i)",
300 DBG("[rotation-thread] Received notification for chan %" PRIu64
301 ", domain %d\n", key
, domain
);
303 channel_info
= lookup_channel_pending(key
, domain
);
305 ERR("[rotation-thread] Failed to find channel_info (key = %"
311 if (--channel_info
->session
->nr_chan_rotate_pending
== 0) {
312 time_t now
= time(NULL
);
314 if (now
== (time_t) -1) {
315 ret
= LTTNG_ERR_ROTATE_NOT_AVAILABLE
;
319 ret
= rename_complete_chunk(channel_info
->session
, now
);
321 ERR("Failed to rename completed rotation chunk");
326 channel_rotation_info_destroy(channel_info
);
334 void *thread_rotation(void *data
)
337 struct rotation_thread_handle
*handle
= data
;
338 struct rotation_thread_state state
;
340 DBG("[rotation-thread] Started rotation thread");
343 ERR("[rotation-thread] Invalid thread context provided");
347 rcu_register_thread();
350 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_ROTATION
);
351 health_code_update();
353 ret
= init_thread_state(handle
, &state
);
358 /* Ready to handle client connections. */
359 sessiond_notify_ready();
365 DBG("[rotation-thread] Entering poll wait");
366 ret
= lttng_poll_wait(&state
.events
, -1);
367 DBG("[rotation-thread] Poll wait returned (%i)", ret
);
371 * Restart interrupted system call.
373 if (errno
== EINTR
) {
376 ERR("[rotation-thread] Error encountered during lttng_poll_wait (%i)", ret
);
381 for (i
= 0; i
< fd_count
; i
++) {
382 int fd
= LTTNG_POLL_GETFD(&state
.events
, i
);
383 uint32_t revents
= LTTNG_POLL_GETEV(&state
.events
, i
);
385 DBG("[rotation-thread] Handling fd (%i) activity (%u)",
388 if (fd
== handle
->thread_quit_pipe
) {
389 DBG("[rotation-thread] Quit pipe activity");
391 } else if (fd
== handle
->ust32_consumer
||
392 fd
== handle
->ust64_consumer
||
393 fd
== handle
->kernel_consumer
) {
394 ret
= handle_channel_rotation_pipe(fd
,
395 revents
, handle
, &state
);
397 ERR("[rotation-thread] Exit main loop");
405 fini_thread_state(&state
);
406 health_unregister(health_sessiond
);
407 rcu_thread_offline();
408 rcu_unregister_thread();