2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
10 #include <common/pipe.h>
11 #include <common/utils.h>
13 #include "manage-kernel.h"
14 #include "testpoint.h"
15 #include "health-sessiond.h"
19 #include "kernel-consumer.h"
21 struct thread_notifiers
{
22 struct lttng_pipe
*quit_pipe
;
23 int kernel_poll_pipe_read_fd
;
27 * Update the kernel poll set of all channel fd available over all tracing
28 * session. Add the wakeup pipe at the end of the set.
30 static int update_kernel_poll(struct lttng_poll_event
*events
)
33 struct ltt_kernel_channel
*channel
;
34 struct ltt_session
*session
;
35 const struct ltt_session_list
*session_list
= session_get_list();
37 DBG("Updating kernel poll set");
40 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
41 if (!session_get(session
)) {
44 session_lock(session
);
45 if (session
->kernel_session
== NULL
) {
46 session_unlock(session
);
51 cds_list_for_each_entry(channel
,
52 &session
->kernel_session
->channel_list
.head
, list
) {
53 /* Add channel fd to the kernel poll set */
54 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
56 session_unlock(session
);
60 DBG("Channel fd %d added to kernel set", channel
->fd
);
62 session_unlock(session
);
65 session_unlock_list();
70 session_unlock_list();
75 * Find the channel fd from 'fd' over all tracing session. When found, check
76 * for new channel stream and send those stream fds to the kernel consumer.
78 * Useful for CPU hotplug feature.
80 static int update_kernel_stream(int fd
)
83 struct ltt_session
*session
;
84 struct ltt_kernel_session
*ksess
;
85 struct ltt_kernel_channel
*channel
;
86 const struct ltt_session_list
*session_list
= session_get_list();
88 DBG("Updating kernel streams for channel fd %d", fd
);
91 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
92 if (!session_get(session
)) {
95 session_lock(session
);
96 if (session
->kernel_session
== NULL
) {
97 session_unlock(session
);
101 ksess
= session
->kernel_session
;
103 cds_list_for_each_entry(channel
,
104 &ksess
->channel_list
.head
, list
) {
105 struct lttng_ht_iter iter
;
106 struct consumer_socket
*socket
;
108 if (channel
->fd
!= fd
) {
111 DBG("Channel found, updating kernel streams");
112 ret
= kernel_open_channel_stream(channel
);
116 /* Update the stream global counter */
117 ksess
->stream_count_global
+= ret
;
120 * Have we already sent fds to the consumer? If yes, it
121 * means that tracing is started so it is safe to send
122 * our updated stream fds.
124 if (ksess
->consumer_fds_sent
!= 1
125 || ksess
->consumer
== NULL
) {
131 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
132 &iter
.iter
, socket
, node
.node
) {
133 pthread_mutex_lock(socket
->lock
);
134 ret
= kernel_consumer_send_channel_streams(socket
,
136 session
->output_traces
? 1 : 0);
137 pthread_mutex_unlock(socket
->lock
);
145 session_unlock(session
);
146 session_put(session
);
148 session_unlock_list();
152 session_unlock(session
);
153 session_put(session
);
154 session_unlock_list();
159 * This thread manage event coming from the kernel.
161 * Features supported in this thread:
164 static void *thread_kernel_management(void *data
)
166 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
167 uint32_t revents
, nb_fd
;
169 struct lttng_poll_event events
;
170 struct thread_notifiers
*notifiers
= data
;
171 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
173 DBG("[thread] Thread manage kernel started");
175 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
178 * This first step of the while is to clean this structure which could free
179 * non NULL pointers so initialize it before the loop.
181 lttng_poll_init(&events
);
183 if (testpoint(sessiond_thread_manage_kernel
)) {
184 goto error_testpoint
;
187 health_code_update();
189 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
190 goto error_testpoint
;
194 health_code_update();
196 if (update_poll_flag
== 1) {
197 /* Clean events object. We are about to populate it again. */
198 lttng_poll_clean(&events
);
200 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
202 goto error_poll_create
;
205 ret
= lttng_poll_add(&events
,
206 notifiers
->kernel_poll_pipe_read_fd
,
212 ret
= lttng_poll_add(&events
,
219 /* This will add the available kernel channel if any. */
220 ret
= update_kernel_poll(&events
);
224 update_poll_flag
= 0;
227 DBG("Thread kernel polling");
229 /* Poll infinite value of time */
232 ret
= lttng_poll_wait(&events
, -1);
233 DBG("Thread kernel return from poll on %d fds",
234 LTTNG_POLL_GETNB(&events
));
238 * Restart interrupted system call.
240 if (errno
== EINTR
) {
244 } else if (ret
== 0) {
245 /* Should not happen since timeout is infinite */
246 ERR("Return value of poll is 0 with an infinite timeout.\n"
247 "This should not have happened! Continuing...");
253 for (i
= 0; i
< nb_fd
; i
++) {
254 /* Fetch once the poll data */
255 revents
= LTTNG_POLL_GETEV(&events
, i
);
256 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
258 health_code_update();
260 if (pollfd
== quit_pipe_read_fd
) {
265 /* Check for data on kernel pipe */
266 if (revents
& LPOLLIN
) {
267 if (pollfd
== notifiers
->kernel_poll_pipe_read_fd
) {
268 (void) lttng_read(notifiers
->kernel_poll_pipe_read_fd
,
271 * Ret value is useless here, if this pipe gets any actions an
272 * update is required anyway.
274 update_poll_flag
= 1;
278 * New CPU detected by the kernel. Adding kernel stream to
279 * kernel session and updating the kernel consumer
281 ret
= update_kernel_stream(pollfd
);
287 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
288 update_poll_flag
= 1;
291 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
299 lttng_poll_clean(&events
);
304 ERR("Health error occurred in %s", __func__
);
305 WARN("Kernel thread died unexpectedly. "
306 "Kernel tracing can continue but CPU hotplug is disabled.");
308 health_unregister(the_health_sessiond
);
309 DBG("Kernel thread dying");
313 static bool shutdown_kernel_management_thread(void *data
)
315 struct thread_notifiers
*notifiers
= data
;
316 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
318 return notify_thread_pipe(write_fd
) == 1;
321 static void cleanup_kernel_management_thread(void *data
)
323 struct thread_notifiers
*notifiers
= data
;
325 lttng_pipe_destroy(notifiers
->quit_pipe
);
329 bool launch_kernel_management_thread(int kernel_poll_pipe_read_fd
)
331 struct lttng_pipe
*quit_pipe
;
332 struct thread_notifiers
*notifiers
= NULL
;
333 struct lttng_thread
*thread
;
335 notifiers
= zmalloc(sizeof(*notifiers
));
339 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
343 notifiers
->quit_pipe
= quit_pipe
;
344 notifiers
->kernel_poll_pipe_read_fd
= kernel_poll_pipe_read_fd
;
346 thread
= lttng_thread_create("Kernel management",
347 thread_kernel_management
,
348 shutdown_kernel_management_thread
,
349 cleanup_kernel_management_thread
,
354 lttng_thread_put(thread
);
357 cleanup_kernel_management_thread(notifiers
);