Fix: missing session reference release on kernel poll update
[lttng-tools.git] / src / bin / lttng-sessiond / manage-kernel.c
CommitLineData
5b093681
JG
1/*
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>
5 *
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.
9 *
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.
14 *
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.
18 */
19
20#include <common/pipe.h>
21#include <common/utils.h>
22
23#include "manage-kernel.h"
24#include "testpoint.h"
25#include "health-sessiond.h"
26#include "utils.h"
27#include "thread.h"
28#include "kernel.h"
29#include "kernel-consumer.h"
30
31struct thread_notifiers {
32 struct lttng_pipe *quit_pipe;
33 int kernel_poll_pipe_read_fd;
34};
35
36/*
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.
39 */
40static int update_kernel_poll(struct lttng_poll_event *events)
41{
42 int ret;
43 struct ltt_kernel_channel *channel;
44 struct ltt_session *session;
45 const struct ltt_session_list *session_list = session_get_list();
46
47 DBG("Updating kernel poll set");
48
49 session_lock_list();
50 cds_list_for_each_entry(session, &session_list->head, list) {
51 if (!session_get(session)) {
52 continue;
53 }
54 session_lock(session);
55 if (session->kernel_session == NULL) {
56 session_unlock(session);
57 session_put(session);
58 continue;
59 }
60
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);
65 if (ret < 0) {
66 session_unlock(session);
67 session_put(session);
68 goto error;
69 }
70 DBG("Channel fd %d added to kernel set", channel->fd);
71 }
72 session_unlock(session);
0318876b 73 session_put(session);
5b093681
JG
74 }
75 session_unlock_list();
76
77 return 0;
78
79error:
80 session_unlock_list();
81 return -1;
82}
83
84/*
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.
87 *
88 * Useful for CPU hotplug feature.
89 */
90static int update_kernel_stream(int fd)
91{
92 int ret = 0;
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();
97
98 DBG("Updating kernel streams for channel fd %d", fd);
99
100 session_lock_list();
101 cds_list_for_each_entry(session, &session_list->head, list) {
102 if (!session_get(session)) {
103 continue;
104 }
105 session_lock(session);
106 if (session->kernel_session == NULL) {
107 session_unlock(session);
108 session_put(session);
109 continue;
110 }
111 ksess = session->kernel_session;
112
113 cds_list_for_each_entry(channel,
114 &ksess->channel_list.head, list) {
115 struct lttng_ht_iter iter;
116 struct consumer_socket *socket;
117
118 if (channel->fd != fd) {
119 continue;
120 }
121 DBG("Channel found, updating kernel streams");
122 ret = kernel_open_channel_stream(channel);
123 if (ret < 0) {
124 goto error;
125 }
126 /* Update the stream global counter */
127 ksess->stream_count_global += ret;
128
129 /*
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.
133 */
134 if (ksess->consumer_fds_sent != 1
135 || ksess->consumer == NULL) {
136 ret = -1;
137 goto error;
138 }
139
140 rcu_read_lock();
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,
145 channel, ksess,
146 session->output_traces ? 1 : 0);
147 pthread_mutex_unlock(socket->lock);
148 if (ret < 0) {
149 rcu_read_unlock();
150 goto error;
151 }
152 }
153 rcu_read_unlock();
154 }
155 session_unlock(session);
156 session_put(session);
157 }
158 session_unlock_list();
159 return ret;
160
161error:
162 session_unlock(session);
163 session_put(session);
164 session_unlock_list();
165 return ret;
166}
167
168/*
169 * This thread manage event coming from the kernel.
170 *
171 * Features supported in this thread:
172 * -) CPU Hotplug
173 */
174static void *thread_kernel_management(void *data)
175{
176 int ret, i, pollfd, update_poll_flag = 1, err = -1;
177 uint32_t revents, nb_fd;
178 char tmp;
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);
182
183 DBG("[thread] Thread manage kernel started");
184
185 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
186
187 /*
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.
190 */
191 lttng_poll_init(&events);
192
193 if (testpoint(sessiond_thread_manage_kernel)) {
194 goto error_testpoint;
195 }
196
197 health_code_update();
198
199 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
200 goto error_testpoint;
201 }
202
203 while (1) {
204 health_code_update();
205
206 if (update_poll_flag == 1) {
207 /* Clean events object. We are about to populate it again. */
208 lttng_poll_clean(&events);
209
210 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
211 if (ret < 0) {
212 goto error_poll_create;
213 }
214
215 ret = lttng_poll_add(&events,
216 notifiers->kernel_poll_pipe_read_fd,
217 LPOLLIN);
218 if (ret < 0) {
219 goto error;
220 }
221
222 ret = lttng_poll_add(&events,
223 quit_pipe_read_fd,
224 LPOLLIN);
225 if (ret < 0) {
226 goto error;
227 }
228
229 /* This will add the available kernel channel if any. */
230 ret = update_kernel_poll(&events);
231 if (ret < 0) {
232 goto error;
233 }
234 update_poll_flag = 0;
235 }
236
237 DBG("Thread kernel polling");
238
239 /* Poll infinite value of time */
240 restart:
241 health_poll_entry();
242 ret = lttng_poll_wait(&events, -1);
243 DBG("Thread kernel return from poll on %d fds",
244 LTTNG_POLL_GETNB(&events));
245 health_poll_exit();
246 if (ret < 0) {
247 /*
248 * Restart interrupted system call.
249 */
250 if (errno == EINTR) {
251 goto restart;
252 }
253 goto error;
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...");
258 continue;
259 }
260
261 nb_fd = ret;
262
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);
267
268 health_code_update();
269
270 if (!revents) {
271 /* No activity for this FD (poll implementation). */
272 continue;
273 }
274
275 if (pollfd == quit_pipe_read_fd) {
276 err = 0;
277 goto exit;
278 }
279
280 /* Check for data on kernel pipe */
281 if (revents & LPOLLIN) {
282 if (pollfd == notifiers->kernel_poll_pipe_read_fd) {
283 (void) lttng_read(notifiers->kernel_poll_pipe_read_fd,
284 &tmp, 1);
285 /*
286 * Ret value is useless here, if this pipe gets any actions an
287 * update is required anyway.
288 */
289 update_poll_flag = 1;
290 continue;
291 } else {
292 /*
293 * New CPU detected by the kernel. Adding kernel stream to
294 * kernel session and updating the kernel consumer
295 */
296 ret = update_kernel_stream(pollfd);
297 if (ret < 0) {
298 continue;
299 }
300 break;
301 }
302 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
303 update_poll_flag = 1;
304 continue;
305 } else {
306 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
307 goto error;
308 }
309 }
310 }
311
312exit:
313error:
314 lttng_poll_clean(&events);
315error_poll_create:
316error_testpoint:
317 if (err) {
318 health_error();
319 ERR("Health error occurred in %s", __func__);
320 WARN("Kernel thread died unexpectedly. "
321 "Kernel tracing can continue but CPU hotplug is disabled.");
322 }
323 health_unregister(health_sessiond);
324 DBG("Kernel thread dying");
325 return NULL;
326}
327
328static bool shutdown_kernel_management_thread(void *data)
329{
330 struct thread_notifiers *notifiers = data;
331 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
332
333 return notify_thread_pipe(write_fd) == 1;
334}
335
336static void cleanup_kernel_management_thread(void *data)
337{
338 struct thread_notifiers *notifiers = data;
339
340 lttng_pipe_destroy(notifiers->quit_pipe);
341 free(notifiers);
342}
343
344bool launch_kernel_management_thread(int kernel_poll_pipe_read_fd)
345{
346 struct lttng_pipe *quit_pipe;
347 struct thread_notifiers *notifiers = NULL;
348 struct lttng_thread *thread;
349
350 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
351 if (!quit_pipe) {
352 goto error;
353 }
354
355 notifiers = zmalloc(sizeof(*notifiers));
356 if (!notifiers) {
357 goto error;
358 }
359 notifiers->quit_pipe = quit_pipe;
360 notifiers->kernel_poll_pipe_read_fd = kernel_poll_pipe_read_fd;
361
362 thread = lttng_thread_create("Kernel management",
363 thread_kernel_management,
364 shutdown_kernel_management_thread,
365 cleanup_kernel_management_thread,
366 notifiers);
367 if (!thread) {
368 goto error;
369 }
370 lttng_thread_put(thread);
371 return true;
372error:
373 cleanup_kernel_management_thread(notifiers);
374 return false;
375}
This page took 0.036699 seconds and 5 git commands to generate.