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.hpp>
11 #include <common/utils.hpp>
13 #include "manage-kernel.hpp"
14 #include "testpoint.hpp"
15 #include "health-sessiond.hpp"
19 #include "kernel-consumer.hpp"
22 struct thread_notifiers
{
23 struct lttng_pipe
*quit_pipe
;
24 int kernel_poll_pipe_read_fd
;
29 * Update the kernel poll set of all channel fd available over all tracing
30 * session. Add the wakeup pipe at the end of the set.
32 static int update_kernel_poll(struct lttng_poll_event
*events
)
35 struct ltt_kernel_channel
*channel
;
36 struct ltt_session
*session
;
37 const struct ltt_session_list
*session_list
= session_get_list();
39 DBG("Updating kernel poll set");
42 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
43 if (!session_get(session
)) {
46 session_lock(session
);
47 if (session
->kernel_session
== NULL
) {
48 session_unlock(session
);
53 cds_list_for_each_entry(channel
,
54 &session
->kernel_session
->channel_list
.head
, list
) {
55 /* Add channel fd to the kernel poll set */
56 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
58 session_unlock(session
);
62 DBG("Channel fd %d added to kernel set", channel
->fd
);
64 session_unlock(session
);
67 session_unlock_list();
72 session_unlock_list();
77 * Find the channel fd from 'fd' over all tracing session. When found, check
78 * for new channel stream and send those stream fds to the kernel consumer.
80 * Useful for CPU hotplug feature.
82 static int update_kernel_stream(int fd
)
85 struct ltt_session
*session
;
86 struct ltt_kernel_session
*ksess
;
87 struct ltt_kernel_channel
*channel
;
88 const struct ltt_session_list
*session_list
= session_get_list();
90 DBG("Updating kernel streams for channel fd %d", fd
);
93 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
94 if (!session_get(session
)) {
97 session_lock(session
);
98 if (session
->kernel_session
== NULL
) {
99 session_unlock(session
);
100 session_put(session
);
103 ksess
= session
->kernel_session
;
105 cds_list_for_each_entry(channel
,
106 &ksess
->channel_list
.head
, list
) {
107 struct lttng_ht_iter iter
;
108 struct consumer_socket
*socket
;
110 if (channel
->fd
!= fd
) {
113 DBG("Channel found, updating kernel streams");
114 ret
= kernel_open_channel_stream(channel
);
118 /* Update the stream global counter */
119 ksess
->stream_count_global
+= ret
;
122 * Have we already sent fds to the consumer? If yes, it
123 * means that tracing is started so it is safe to send
124 * our updated stream fds.
126 if (ksess
->consumer_fds_sent
!= 1
127 || ksess
->consumer
== NULL
) {
133 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
134 &iter
.iter
, socket
, node
.node
) {
135 pthread_mutex_lock(socket
->lock
);
136 ret
= kernel_consumer_send_channel_streams(socket
,
138 session
->output_traces
? 1 : 0);
139 pthread_mutex_unlock(socket
->lock
);
147 session_unlock(session
);
148 session_put(session
);
150 session_unlock_list();
154 session_unlock(session
);
155 session_put(session
);
156 session_unlock_list();
161 * This thread manage event coming from the kernel.
163 * Features supported in this thread:
166 static void *thread_kernel_management(void *data
)
168 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
169 uint32_t revents
, nb_fd
;
171 struct lttng_poll_event events
;
172 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
173 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
175 DBG("[thread] Thread manage kernel started");
177 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
180 * This first step of the while is to clean this structure which could free
181 * non NULL pointers so initialize it before the loop.
183 lttng_poll_init(&events
);
185 if (testpoint(sessiond_thread_manage_kernel
)) {
186 goto error_testpoint
;
189 health_code_update();
191 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
192 goto error_testpoint
;
196 health_code_update();
198 if (update_poll_flag
== 1) {
199 /* Clean events object. We are about to populate it again. */
200 lttng_poll_clean(&events
);
202 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
204 goto error_poll_create
;
207 ret
= lttng_poll_add(&events
,
208 notifiers
->kernel_poll_pipe_read_fd
,
214 ret
= lttng_poll_add(&events
,
221 /* This will add the available kernel channel if any. */
222 ret
= update_kernel_poll(&events
);
226 update_poll_flag
= 0;
229 DBG("Thread kernel polling");
231 /* Poll infinite value of time */
234 ret
= lttng_poll_wait(&events
, -1);
235 DBG("Thread kernel return from poll on %d fds",
236 LTTNG_POLL_GETNB(&events
));
240 * Restart interrupted system call.
242 if (errno
== EINTR
) {
246 } else if (ret
== 0) {
247 /* Should not happen since timeout is infinite */
248 ERR("Return value of poll is 0 with an infinite timeout.\n"
249 "This should not have happened! Continuing...");
255 for (i
= 0; i
< nb_fd
; i
++) {
256 /* Fetch once the poll data */
257 revents
= LTTNG_POLL_GETEV(&events
, i
);
258 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
260 health_code_update();
262 if (pollfd
== quit_pipe_read_fd
) {
267 /* Check for data on kernel pipe */
268 if (revents
& LPOLLIN
) {
269 if (pollfd
== notifiers
->kernel_poll_pipe_read_fd
) {
270 (void) lttng_read(notifiers
->kernel_poll_pipe_read_fd
,
273 * Ret value is useless here, if this pipe gets any actions an
274 * update is required anyway.
276 update_poll_flag
= 1;
280 * New CPU detected by the kernel. Adding kernel stream to
281 * kernel session and updating the kernel consumer
283 ret
= update_kernel_stream(pollfd
);
289 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
290 update_poll_flag
= 1;
293 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
301 lttng_poll_clean(&events
);
306 ERR("Health error occurred in %s", __func__
);
307 WARN("Kernel thread died unexpectedly. "
308 "Kernel tracing can continue but CPU hotplug is disabled.");
310 health_unregister(the_health_sessiond
);
311 DBG("Kernel thread dying");
315 static bool shutdown_kernel_management_thread(void *data
)
317 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
318 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
320 return notify_thread_pipe(write_fd
) == 1;
323 static void cleanup_kernel_management_thread(void *data
)
325 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
327 lttng_pipe_destroy(notifiers
->quit_pipe
);
331 bool launch_kernel_management_thread(int kernel_poll_pipe_read_fd
)
333 struct lttng_pipe
*quit_pipe
;
334 struct thread_notifiers
*notifiers
= NULL
;
335 struct lttng_thread
*thread
;
337 notifiers
= zmalloc
<thread_notifiers
>();
341 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
345 notifiers
->quit_pipe
= quit_pipe
;
346 notifiers
->kernel_poll_pipe_read_fd
= kernel_poll_pipe_read_fd
;
348 thread
= lttng_thread_create("Kernel management",
349 thread_kernel_management
,
350 shutdown_kernel_management_thread
,
351 cleanup_kernel_management_thread
,
356 lttng_thread_put(thread
);
359 cleanup_kernel_management_thread(notifiers
);