2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <common/pipe.h>
21 #include <common/utils.h>
23 #include "manage-kernel.h"
24 #include "testpoint.h"
25 #include "health-sessiond.h"
29 #include "kernel-consumer.h"
31 struct thread_notifiers
{
32 struct lttng_pipe
*quit_pipe
;
33 int kernel_poll_pipe_read_fd
;
37 * Update the kernel poll set of all channel fd available over all tracing
38 * session. Add the wakeup pipe at the end of the set.
40 static int update_kernel_poll(struct lttng_poll_event
*events
)
43 struct ltt_kernel_channel
*channel
;
44 struct ltt_session
*session
;
45 const struct ltt_session_list
*session_list
= session_get_list();
47 DBG("Updating kernel poll set");
50 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
51 if (!session_get(session
)) {
54 session_lock(session
);
55 if (session
->kernel_session
== NULL
) {
56 session_unlock(session
);
61 cds_list_for_each_entry(channel
,
62 &session
->kernel_session
->channel_list
.head
, list
) {
63 /* Add channel fd to the kernel poll set */
64 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
66 session_unlock(session
);
70 DBG("Channel fd %d added to kernel set", channel
->fd
);
72 session_unlock(session
);
74 session_unlock_list();
79 session_unlock_list();
84 * Find the channel fd from 'fd' over all tracing session. When found, check
85 * for new channel stream and send those stream fds to the kernel consumer.
87 * Useful for CPU hotplug feature.
89 static int update_kernel_stream(int fd
)
92 struct ltt_session
*session
;
93 struct ltt_kernel_session
*ksess
;
94 struct ltt_kernel_channel
*channel
;
95 const struct ltt_session_list
*session_list
= session_get_list();
97 DBG("Updating kernel streams for channel fd %d", fd
);
100 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
101 if (!session_get(session
)) {
104 session_lock(session
);
105 if (session
->kernel_session
== NULL
) {
106 session_unlock(session
);
107 session_put(session
);
110 ksess
= session
->kernel_session
;
112 cds_list_for_each_entry(channel
,
113 &ksess
->channel_list
.head
, list
) {
114 struct lttng_ht_iter iter
;
115 struct consumer_socket
*socket
;
117 if (channel
->fd
!= fd
) {
120 DBG("Channel found, updating kernel streams");
121 ret
= kernel_open_channel_stream(channel
);
125 /* Update the stream global counter */
126 ksess
->stream_count_global
+= ret
;
129 * Have we already sent fds to the consumer? If yes, it
130 * means that tracing is started so it is safe to send
131 * our updated stream fds.
133 if (ksess
->consumer_fds_sent
!= 1
134 || ksess
->consumer
== NULL
) {
140 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
141 &iter
.iter
, socket
, node
.node
) {
142 pthread_mutex_lock(socket
->lock
);
143 ret
= kernel_consumer_send_channel_streams(socket
,
145 session
->output_traces
? 1 : 0);
146 pthread_mutex_unlock(socket
->lock
);
154 session_unlock(session
);
155 session_put(session
);
157 session_unlock_list();
161 session_unlock(session
);
162 session_put(session
);
163 session_unlock_list();
168 * This thread manage event coming from the kernel.
170 * Features supported in this thread:
173 static void *thread_kernel_management(void *data
)
175 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
176 uint32_t revents
, nb_fd
;
178 struct lttng_poll_event events
;
179 struct thread_notifiers
*notifiers
= data
;
180 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
182 DBG("[thread] Thread manage kernel started");
184 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
187 * This first step of the while is to clean this structure which could free
188 * non NULL pointers so initialize it before the loop.
190 lttng_poll_init(&events
);
192 if (testpoint(sessiond_thread_manage_kernel
)) {
193 goto error_testpoint
;
196 health_code_update();
198 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
199 goto error_testpoint
;
203 health_code_update();
205 if (update_poll_flag
== 1) {
206 /* Clean events object. We are about to populate it again. */
207 lttng_poll_clean(&events
);
209 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
211 goto error_poll_create
;
214 ret
= lttng_poll_add(&events
,
215 notifiers
->kernel_poll_pipe_read_fd
,
221 ret
= lttng_poll_add(&events
,
228 /* This will add the available kernel channel if any. */
229 ret
= update_kernel_poll(&events
);
233 update_poll_flag
= 0;
236 DBG("Thread kernel polling");
238 /* Poll infinite value of time */
241 ret
= lttng_poll_wait(&events
, -1);
242 DBG("Thread kernel return from poll on %d fds",
243 LTTNG_POLL_GETNB(&events
));
247 * Restart interrupted system call.
249 if (errno
== EINTR
) {
253 } else if (ret
== 0) {
254 /* Should not happen since timeout is infinite */
255 ERR("Return value of poll is 0 with an infinite timeout.\n"
256 "This should not have happened! Continuing...");
262 for (i
= 0; i
< nb_fd
; i
++) {
263 /* Fetch once the poll data */
264 revents
= LTTNG_POLL_GETEV(&events
, i
);
265 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
267 health_code_update();
270 /* No activity for this FD (poll implementation). */
274 if (pollfd
== quit_pipe_read_fd
) {
279 /* Check for data on kernel pipe */
280 if (revents
& LPOLLIN
) {
281 if (pollfd
== notifiers
->kernel_poll_pipe_read_fd
) {
282 (void) lttng_read(notifiers
->kernel_poll_pipe_read_fd
,
285 * Ret value is useless here, if this pipe gets any actions an
286 * update is required anyway.
288 update_poll_flag
= 1;
292 * New CPU detected by the kernel. Adding kernel stream to
293 * kernel session and updating the kernel consumer
295 ret
= update_kernel_stream(pollfd
);
301 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
302 update_poll_flag
= 1;
305 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
313 lttng_poll_clean(&events
);
318 ERR("Health error occurred in %s", __func__
);
319 WARN("Kernel thread died unexpectedly. "
320 "Kernel tracing can continue but CPU hotplug is disabled.");
322 health_unregister(health_sessiond
);
323 DBG("Kernel thread dying");
327 static bool shutdown_kernel_management_thread(void *data
)
329 struct thread_notifiers
*notifiers
= data
;
330 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
332 return notify_thread_pipe(write_fd
) == 1;
335 static void cleanup_kernel_management_thread(void *data
)
337 struct thread_notifiers
*notifiers
= data
;
339 lttng_pipe_destroy(notifiers
->quit_pipe
);
343 bool launch_kernel_management_thread(int kernel_poll_pipe_read_fd
)
345 struct lttng_pipe
*quit_pipe
;
346 struct thread_notifiers
*notifiers
= NULL
;
347 struct lttng_thread
*thread
;
349 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
354 notifiers
= zmalloc(sizeof(*notifiers
));
358 notifiers
->quit_pipe
= quit_pipe
;
359 notifiers
->kernel_poll_pipe_read_fd
= kernel_poll_pipe_read_fd
;
361 thread
= lttng_thread_create("Kernel management",
362 thread_kernel_management
,
363 shutdown_kernel_management_thread
,
364 cleanup_kernel_management_thread
,
369 lttng_thread_put(thread
);
372 cleanup_kernel_management_thread(notifiers
);