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