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
);
75 session_unlock_list();
80 session_unlock_list();
85 * Find the channel fd from 'fd' over all tracing session. When found, check
86 * for new channel stream and send those stream fds to the kernel consumer.
88 * Useful for CPU hotplug feature.
90 static int update_kernel_stream(int fd
)
93 struct ltt_session
*session
;
94 struct ltt_kernel_session
*ksess
;
95 struct ltt_kernel_channel
*channel
;
96 const struct ltt_session_list
*session_list
= session_get_list();
98 DBG("Updating kernel streams for channel fd %d", fd
);
101 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
102 if (!session_get(session
)) {
105 session_lock(session
);
106 if (session
->kernel_session
== NULL
) {
107 session_unlock(session
);
108 session_put(session
);
111 ksess
= session
->kernel_session
;
113 cds_list_for_each_entry(channel
,
114 &ksess
->channel_list
.head
, list
) {
115 struct lttng_ht_iter iter
;
116 struct consumer_socket
*socket
;
118 if (channel
->fd
!= fd
) {
121 DBG("Channel found, updating kernel streams");
122 ret
= kernel_open_channel_stream(channel
);
126 /* Update the stream global counter */
127 ksess
->stream_count_global
+= ret
;
130 * Have we already sent fds to the consumer? If yes, it
131 * means that tracing is started so it is safe to send
132 * our updated stream fds.
134 if (ksess
->consumer_fds_sent
!= 1
135 || ksess
->consumer
== NULL
) {
141 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
142 &iter
.iter
, socket
, node
.node
) {
143 pthread_mutex_lock(socket
->lock
);
144 ret
= kernel_consumer_send_channel_streams(socket
,
146 session
->output_traces
? 1 : 0);
147 pthread_mutex_unlock(socket
->lock
);
155 session_unlock(session
);
156 session_put(session
);
158 session_unlock_list();
162 session_unlock(session
);
163 session_put(session
);
164 session_unlock_list();
169 * This thread manage event coming from the kernel.
171 * Features supported in this thread:
174 static void *thread_kernel_management(void *data
)
176 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
177 uint32_t revents
, nb_fd
;
179 struct lttng_poll_event events
;
180 struct thread_notifiers
*notifiers
= data
;
181 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
183 DBG("[thread] Thread manage kernel started");
185 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
188 * This first step of the while is to clean this structure which could free
189 * non NULL pointers so initialize it before the loop.
191 lttng_poll_init(&events
);
193 if (testpoint(sessiond_thread_manage_kernel
)) {
194 goto error_testpoint
;
197 health_code_update();
199 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
200 goto error_testpoint
;
204 health_code_update();
206 if (update_poll_flag
== 1) {
207 /* Clean events object. We are about to populate it again. */
208 lttng_poll_clean(&events
);
210 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
212 goto error_poll_create
;
215 ret
= lttng_poll_add(&events
,
216 notifiers
->kernel_poll_pipe_read_fd
,
222 ret
= lttng_poll_add(&events
,
229 /* This will add the available kernel channel if any. */
230 ret
= update_kernel_poll(&events
);
234 update_poll_flag
= 0;
237 DBG("Thread kernel polling");
239 /* Poll infinite value of time */
242 ret
= lttng_poll_wait(&events
, -1);
243 DBG("Thread kernel return from poll on %d fds",
244 LTTNG_POLL_GETNB(&events
));
248 * Restart interrupted system call.
250 if (errno
== EINTR
) {
254 } else if (ret
== 0) {
255 /* Should not happen since timeout is infinite */
256 ERR("Return value of poll is 0 with an infinite timeout.\n"
257 "This should not have happened! Continuing...");
263 for (i
= 0; i
< nb_fd
; i
++) {
264 /* Fetch once the poll data */
265 revents
= LTTNG_POLL_GETEV(&events
, i
);
266 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
268 health_code_update();
270 if (pollfd
== quit_pipe_read_fd
) {
275 /* Check for data on kernel pipe */
276 if (revents
& LPOLLIN
) {
277 if (pollfd
== notifiers
->kernel_poll_pipe_read_fd
) {
278 (void) lttng_read(notifiers
->kernel_poll_pipe_read_fd
,
281 * Ret value is useless here, if this pipe gets any actions an
282 * update is required anyway.
284 update_poll_flag
= 1;
288 * New CPU detected by the kernel. Adding kernel stream to
289 * kernel session and updating the kernel consumer
291 ret
= update_kernel_stream(pollfd
);
297 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
298 update_poll_flag
= 1;
301 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
309 lttng_poll_clean(&events
);
314 ERR("Health error occurred in %s", __func__
);
315 WARN("Kernel thread died unexpectedly. "
316 "Kernel tracing can continue but CPU hotplug is disabled.");
318 health_unregister(health_sessiond
);
319 DBG("Kernel thread dying");
323 static bool shutdown_kernel_management_thread(void *data
)
325 struct thread_notifiers
*notifiers
= data
;
326 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
328 return notify_thread_pipe(write_fd
) == 1;
331 static void cleanup_kernel_management_thread(void *data
)
333 struct thread_notifiers
*notifiers
= data
;
335 lttng_pipe_destroy(notifiers
->quit_pipe
);
339 bool launch_kernel_management_thread(int kernel_poll_pipe_read_fd
)
341 struct lttng_pipe
*quit_pipe
;
342 struct thread_notifiers
*notifiers
= NULL
;
343 struct lttng_thread
*thread
;
345 notifiers
= zmalloc(sizeof(*notifiers
));
349 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
353 notifiers
->quit_pipe
= quit_pipe
;
354 notifiers
->kernel_poll_pipe_read_fd
= kernel_poll_pipe_read_fd
;
356 thread
= lttng_thread_create("Kernel management",
357 thread_kernel_management
,
358 shutdown_kernel_management_thread
,
359 cleanup_kernel_management_thread
,
364 lttng_thread_put(thread
);
367 cleanup_kernel_management_thread(notifiers
);