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 "health-sessiond.hpp"
11 #include "kernel-consumer.hpp"
13 #include "manage-kernel.hpp"
14 #include "testpoint.hpp"
18 #include <common/pipe.hpp>
19 #include <common/utils.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
== nullptr) {
48 session_unlock(session
);
53 cds_list_for_each_entry (
54 channel
, &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
== nullptr) {
99 session_unlock(session
);
100 session_put(session
);
103 ksess
= session
->kernel_session
;
105 cds_list_for_each_entry (channel
, &ksess
->channel_list
.head
, list
) {
106 struct lttng_ht_iter iter
;
107 struct consumer_socket
*socket
;
109 if (channel
->fd
!= fd
) {
112 DBG("Channel found, updating kernel streams");
113 ret
= kernel_open_channel_stream(channel
);
117 /* Update the stream global counter */
118 ksess
->stream_count_global
+= ret
;
121 * Have we already sent fds to the consumer? If yes, it
122 * means that tracing is started so it is safe to send
123 * our updated stream fds.
125 if (ksess
->consumer_fds_sent
!= 1 || ksess
->consumer
== nullptr) {
131 cds_lfht_for_each_entry (
132 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
133 pthread_mutex_lock(socket
->lock
);
134 ret
= kernel_consumer_send_channel_streams(
135 socket
, channel
, ksess
, session
->output_traces
? 1 : 0);
136 pthread_mutex_unlock(socket
->lock
);
144 session_unlock(session
);
145 session_put(session
);
147 session_unlock_list();
151 session_unlock(session
);
152 session_put(session
);
153 session_unlock_list();
158 * This thread manage event coming from the kernel.
160 * Features supported in this thread:
163 static void *thread_kernel_management(void *data
)
165 int ret
, i
, update_poll_flag
= 1, err
= -1;
168 struct lttng_poll_event events
;
169 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
170 const auto thread_quit_pipe_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
172 DBG("[thread] Thread manage kernel started");
174 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
177 * This first step of the while is to clean this structure which could free
178 * non NULL pointers so initialize it before the loop.
180 lttng_poll_init(&events
);
182 if (testpoint(sessiond_thread_manage_kernel
)) {
183 goto error_testpoint
;
186 health_code_update();
188 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
189 goto error_testpoint
;
193 health_code_update();
195 if (update_poll_flag
== 1) {
196 /* Clean events object. We are about to populate it again. */
197 lttng_poll_clean(&events
);
199 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
201 goto error_poll_create
;
204 ret
= lttng_poll_add(&events
, notifiers
->kernel_poll_pipe_read_fd
, LPOLLIN
);
209 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
);
214 /* This will add the available kernel channel if any. */
215 ret
= update_kernel_poll(&events
);
219 update_poll_flag
= 0;
222 DBG("Thread kernel polling");
224 /* Poll infinite value of time */
227 ret
= lttng_poll_wait(&events
, -1);
228 DBG("Thread kernel return from poll on %d fds", LTTNG_POLL_GETNB(&events
));
232 * Restart interrupted system call.
234 if (errno
== EINTR
) {
238 } else if (ret
== 0) {
239 /* Should not happen since timeout is infinite */
240 ERR("Return value of poll is 0 with an infinite timeout.\n"
241 "This should not have happened! Continuing...");
247 for (i
= 0; i
< nb_fd
; i
++) {
248 /* Fetch once the poll data */
249 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
250 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
252 health_code_update();
254 /* Activity on thread quit pipe, exiting. */
255 if (pollfd
== thread_quit_pipe_fd
) {
256 DBG("Activity on thread quit pipe");
261 /* Check for data on kernel pipe */
262 if (revents
& LPOLLIN
) {
263 if (pollfd
== notifiers
->kernel_poll_pipe_read_fd
) {
265 notifiers
->kernel_poll_pipe_read_fd
, &tmp
, 1);
267 * Ret value is useless here, if this pipe gets any actions
268 * an update is required anyway.
270 update_poll_flag
= 1;
274 * New CPU detected by the kernel. Adding kernel stream to
275 * kernel session and updating the kernel consumer
277 ret
= update_kernel_stream(pollfd
);
283 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
284 update_poll_flag
= 1;
287 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
295 lttng_poll_clean(&events
);
300 ERR("Health error occurred in %s", __func__
);
301 WARN("Kernel thread died unexpectedly. "
302 "Kernel tracing can continue but CPU hotplug is disabled.");
304 health_unregister(the_health_sessiond
);
305 DBG("Kernel thread dying");
309 static bool shutdown_kernel_management_thread(void *data
)
311 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
312 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
314 return notify_thread_pipe(write_fd
) == 1;
317 static void cleanup_kernel_management_thread(void *data
)
319 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
321 lttng_pipe_destroy(notifiers
->quit_pipe
);
325 bool launch_kernel_management_thread(int kernel_poll_pipe_read_fd
)
327 struct lttng_pipe
*quit_pipe
;
328 struct thread_notifiers
*notifiers
= nullptr;
329 struct lttng_thread
*thread
;
331 notifiers
= zmalloc
<thread_notifiers
>();
335 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
339 notifiers
->quit_pipe
= quit_pipe
;
340 notifiers
->kernel_poll_pipe_read_fd
= kernel_poll_pipe_read_fd
;
342 thread
= lttng_thread_create("Kernel management",
343 thread_kernel_management
,
344 shutdown_kernel_management_thread
,
345 cleanup_kernel_management_thread
,
350 lttng_thread_put(thread
);
353 cleanup_kernel_management_thread(notifiers
);