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 <lttng/notification/channel-internal.h>
37 #include "rotation-thread.h"
38 #include "lttng-sessiond.h"
39 #include "health-sessiond.h"
45 #include <urcu/list.h>
46 #include <urcu/rculfhash.h>
49 * Store a struct rotation_channel_info for each channel that is currently
50 * being rotated by the consumer.
52 struct cds_lfht
*channel_pending_rotate_ht
;
54 struct rotation_thread_state
{
55 struct lttng_poll_event events
;
59 void channel_rotation_info_destroy(struct rotation_channel_info
*channel_info
)
66 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
68 struct rotation_channel_key
*channel_key
= (struct rotation_channel_key
*) key
;
69 struct rotation_channel_info
*channel_info
;
71 channel_info
= caa_container_of(node
, struct rotation_channel_info
,
72 rotate_channels_ht_node
);
74 return !!((channel_key
->key
== channel_info
->channel_key
.key
) &&
75 (channel_key
->domain
== channel_info
->channel_key
.domain
));
79 struct rotation_channel_info
*lookup_channel_pending(uint64_t key
,
80 enum lttng_domain_type domain
)
82 struct cds_lfht_iter iter
;
83 struct cds_lfht_node
*node
;
84 struct rotation_channel_info
*channel_info
= NULL
;
85 struct rotation_channel_key channel_key
= { .key
= key
,
88 cds_lfht_lookup(channel_pending_rotate_ht
,
89 hash_channel_key(&channel_key
),
92 node
= cds_lfht_iter_get_node(&iter
);
97 channel_info
= caa_container_of(node
, struct rotation_channel_info
,
98 rotate_channels_ht_node
);
99 cds_lfht_del(channel_pending_rotate_ht
, node
);
105 * Destroy the thread data previously created by the init function.
107 void rotation_thread_handle_destroy(
108 struct rotation_thread_handle
*handle
)
116 if (handle
->ust32_consumer
>= 0) {
117 ret
= close(handle
->ust32_consumer
);
119 PERROR("close 32-bit consumer channel rotation pipe");
122 if (handle
->ust64_consumer
>= 0) {
123 ret
= close(handle
->ust64_consumer
);
125 PERROR("close 64-bit consumer channel rotation pipe");
128 if (handle
->kernel_consumer
>= 0) {
129 ret
= close(handle
->kernel_consumer
);
131 PERROR("close kernel consumer channel rotation pipe");
139 struct rotation_thread_handle
*rotation_thread_handle_create(
140 struct lttng_pipe
*ust32_channel_rotate_pipe
,
141 struct lttng_pipe
*ust64_channel_rotate_pipe
,
142 struct lttng_pipe
*kernel_channel_rotate_pipe
,
143 int thread_quit_pipe
)
145 struct rotation_thread_handle
*handle
;
147 handle
= zmalloc(sizeof(*handle
));
152 if (ust32_channel_rotate_pipe
) {
153 handle
->ust32_consumer
=
154 lttng_pipe_release_readfd(
155 ust32_channel_rotate_pipe
);
156 if (handle
->ust32_consumer
< 0) {
160 handle
->ust32_consumer
= -1;
162 if (ust64_channel_rotate_pipe
) {
163 handle
->ust64_consumer
=
164 lttng_pipe_release_readfd(
165 ust64_channel_rotate_pipe
);
166 if (handle
->ust64_consumer
< 0) {
170 handle
->ust64_consumer
= -1;
172 if (kernel_channel_rotate_pipe
) {
173 handle
->kernel_consumer
=
174 lttng_pipe_release_readfd(
175 kernel_channel_rotate_pipe
);
176 if (handle
->kernel_consumer
< 0) {
180 handle
->kernel_consumer
= -1;
182 handle
->thread_quit_pipe
= thread_quit_pipe
;
187 rotation_thread_handle_destroy(handle
);
192 int init_poll_set(struct lttng_poll_event
*poll_set
,
193 struct rotation_thread_handle
*handle
)
198 * Create pollset with size 4:
199 * - sessiond quit pipe
200 * - consumerd (32-bit user space) channel rotate pipe,
201 * - consumerd (64-bit user space) channel rotate pipe,
202 * - consumerd (kernel) channel rotate pipe,
204 ret
= lttng_poll_create(poll_set
, 4, LTTNG_CLOEXEC
);
209 ret
= lttng_poll_add(poll_set
, handle
->thread_quit_pipe
,
212 ERR("[rotation-thread] Failed to add thread_quit_pipe fd to pollset");
215 ret
= lttng_poll_add(poll_set
, handle
->ust32_consumer
,
218 ERR("[rotation-thread] Failed to add ust-32 channel rotation pipe fd to pollset");
221 ret
= lttng_poll_add(poll_set
, handle
->ust64_consumer
,
224 ERR("[rotation-thread] Failed to add ust-64 channel rotation pipe fd to pollset");
227 if (handle
->kernel_consumer
>= 0) {
228 ret
= lttng_poll_add(poll_set
, handle
->kernel_consumer
,
231 ERR("[rotation-thread] Failed to add kernel channel rotation pipe fd to pollset");
239 lttng_poll_clean(poll_set
);
244 void fini_thread_state(struct rotation_thread_state
*state
)
246 lttng_poll_clean(&state
->events
);
247 cds_lfht_destroy(channel_pending_rotate_ht
, NULL
);
251 int init_thread_state(struct rotation_thread_handle
*handle
,
252 struct rotation_thread_state
*state
)
256 memset(state
, 0, sizeof(*state
));
257 lttng_poll_init(&state
->events
);
259 ret
= init_poll_set(&state
->events
, handle
);
261 ERR("[rotation-thread] Failed to initialize rotation thread poll set");
265 channel_pending_rotate_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
266 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
267 if (!channel_pending_rotate_ht
) {
268 ERR("[rotation-thread] Failed to create channel pending rotation hash table");
278 int handle_channel_rotation_pipe(int fd
, uint32_t revents
,
279 struct rotation_thread_handle
*handle
,
280 struct rotation_thread_state
*state
)
283 enum lttng_domain_type domain
;
284 struct rotation_channel_info
*channel_info
;
285 struct ltt_session
*session
= NULL
;
288 if (fd
== handle
->ust32_consumer
||
289 fd
== handle
->ust64_consumer
) {
290 domain
= LTTNG_DOMAIN_UST
;
291 } else if (fd
== handle
->kernel_consumer
) {
292 domain
= LTTNG_DOMAIN_KERNEL
;
294 ERR("[rotation-thread] Unknown channel rotation pipe fd %d",
299 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
300 ret
= lttng_poll_del(&state
->events
, fd
);
302 ERR("[rotation-thread] Failed to remove consumer "
303 "rotation pipe from poll set");
309 ret
= read(fd
, &key
, sizeof(key
));
310 } while (ret
== -1 && errno
== EINTR
);
311 if (ret
!= sizeof(key
)) {
312 ERR("[rotation-thread] Failed to read from pipe (fd = %i)",
318 DBG("[rotation-thread] Received notification for chan %" PRIu64
319 ", domain %d\n", key
, domain
);
321 channel_info
= lookup_channel_pending(key
, domain
);
323 ERR("[rotation-thread] Failed to find channel_info (key = %"
330 session
= session_find_by_id(channel_info
->session_id
);
333 * The session may have been destroyed before we had a chance to
334 * perform this action, return gracefully.
336 DBG("[rotation-thread] Session %" PRIu64
" not found",
337 channel_info
->session_id
);
339 goto end_unlock_session_list
;
342 session_lock(session
);
343 if (--session
->nr_chan_rotate_pending
== 0) {
344 time_t now
= time(NULL
);
346 if (now
== (time_t) -1) {
347 session
->rotation_status
= LTTNG_ROTATION_STATUS_ERROR
;
349 goto end_unlock_session
;
352 ret
= rename_complete_chunk(session
, now
);
354 ERR("Failed to rename completed rotation chunk");
355 goto end_unlock_session
;
357 session
->rotate_pending
= false;
358 session
->rotation_status
= LTTNG_ROTATION_STATUS_COMPLETED
;
359 session
->last_chunk_start_ts
= session
->current_chunk_start_ts
;
360 DBG("Rotation completed for session %s", session
->name
);
366 channel_rotation_info_destroy(channel_info
);
367 session_unlock(session
);
368 end_unlock_session_list
:
369 session_unlock_list();
375 void *thread_rotation(void *data
)
378 struct rotation_thread_handle
*handle
= data
;
379 struct rotation_thread_state state
;
381 DBG("[rotation-thread] Started rotation thread");
384 ERR("[rotation-thread] Invalid thread context provided");
388 rcu_register_thread();
391 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_ROTATION
);
392 health_code_update();
394 ret
= init_thread_state(handle
, &state
);
399 /* Ready to handle client connections. */
400 sessiond_notify_ready();
406 DBG("[rotation-thread] Entering poll wait");
407 ret
= lttng_poll_wait(&state
.events
, -1);
408 DBG("[rotation-thread] Poll wait returned (%i)", ret
);
412 * Restart interrupted system call.
414 if (errno
== EINTR
) {
417 ERR("[rotation-thread] Error encountered during lttng_poll_wait (%i)", ret
);
422 for (i
= 0; i
< fd_count
; i
++) {
423 int fd
= LTTNG_POLL_GETFD(&state
.events
, i
);
424 uint32_t revents
= LTTNG_POLL_GETEV(&state
.events
, i
);
426 DBG("[rotation-thread] Handling fd (%i) activity (%u)",
429 if (fd
== handle
->thread_quit_pipe
) {
430 DBG("[rotation-thread] Quit pipe activity");
432 } else if (fd
== handle
->ust32_consumer
||
433 fd
== handle
->ust64_consumer
||
434 fd
== handle
->kernel_consumer
) {
435 ret
= handle_channel_rotation_pipe(fd
,
436 revents
, handle
, &state
);
438 ERR("[rotation-thread] Handle channel rotation pipe");
446 DBG("[rotation-thread] Exit");
447 fini_thread_state(&state
);
448 health_unregister(health_sessiond
);
449 rcu_thread_offline();
450 rcu_unregister_thread();