clang-tidy: add a subset of cppcoreguidelines and other style checks
[lttng-tools.git] / src / bin / lttng-sessiond / manage-kernel.cpp
CommitLineData
5b093681 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa
MJ
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5b093681 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
5b093681 7 *
5b093681
JG
8 */
9
28ab034a
JG
10#include "health-sessiond.hpp"
11#include "kernel-consumer.hpp"
12#include "kernel.hpp"
c9e313bc
SM
13#include "manage-kernel.hpp"
14#include "testpoint.hpp"
c9e313bc 15#include "thread.hpp"
28ab034a
JG
16#include "utils.hpp"
17
18#include <common/pipe.hpp>
19#include <common/utils.hpp>
5b093681 20
f1494934 21namespace {
5b093681
JG
22struct thread_notifiers {
23 struct lttng_pipe *quit_pipe;
24 int kernel_poll_pipe_read_fd;
25};
f1494934 26} /* namespace */
5b093681
JG
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 */
32static 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();
28ab034a 42 cds_list_for_each_entry (session, &session_list->head, list) {
5b093681
JG
43 if (!session_get(session)) {
44 continue;
45 }
46 session_lock(session);
cd9adb8b 47 if (session->kernel_session == nullptr) {
5b093681
JG
48 session_unlock(session);
49 session_put(session);
50 continue;
51 }
52
28ab034a
JG
53 cds_list_for_each_entry (
54 channel, &session->kernel_session->channel_list.head, list) {
5b093681
JG
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);
0318876b 65 session_put(session);
5b093681
JG
66 }
67 session_unlock_list();
68
69 return 0;
70
71error:
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 */
82static 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();
28ab034a 93 cds_list_for_each_entry (session, &session_list->head, list) {
5b093681
JG
94 if (!session_get(session)) {
95 continue;
96 }
97 session_lock(session);
cd9adb8b 98 if (session->kernel_session == nullptr) {
5b093681
JG
99 session_unlock(session);
100 session_put(session);
101 continue;
102 }
103 ksess = session->kernel_session;
104
28ab034a 105 cds_list_for_each_entry (channel, &ksess->channel_list.head, list) {
5b093681
JG
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 */
cd9adb8b 125 if (ksess->consumer_fds_sent != 1 || ksess->consumer == nullptr) {
5b093681
JG
126 ret = -1;
127 goto error;
128 }
129
130 rcu_read_lock();
28ab034a
JG
131 cds_lfht_for_each_entry (
132 ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
5b093681 133 pthread_mutex_lock(socket->lock);
28ab034a
JG
134 ret = kernel_consumer_send_channel_streams(
135 socket, channel, ksess, session->output_traces ? 1 : 0);
5b093681
JG
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
150error:
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 */
163static void *thread_kernel_management(void *data)
164{
8a00688e
MJ
165 int ret, i, update_poll_flag = 1, err = -1;
166 uint32_t nb_fd;
5b093681
JG
167 char tmp;
168 struct lttng_poll_event events;
7966af57 169 struct thread_notifiers *notifiers = (thread_notifiers *) data;
8a00688e 170 const auto thread_quit_pipe_fd = lttng_pipe_get_readfd(notifiers->quit_pipe);
5b093681
JG
171
172 DBG("[thread] Thread manage kernel started");
173
412d7227 174 health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
5b093681
JG
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
cd9adb8b 192 while (true) {
5b093681
JG
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
28ab034a 204 ret = lttng_poll_add(&events, notifiers->kernel_poll_pipe_read_fd, LPOLLIN);
5b093681
JG
205 if (ret < 0) {
206 goto error;
207 }
208
28ab034a 209 ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN);
5b093681
JG
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);
28ab034a 228 DBG("Thread kernel return from poll on %d fds", LTTNG_POLL_GETNB(&events));
5b093681
JG
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"
28ab034a 241 "This should not have happened! Continuing...");
5b093681
JG
242 continue;
243 }
244
245 nb_fd = ret;
246
247 for (i = 0; i < nb_fd; i++) {
248 /* Fetch once the poll data */
8a00688e
MJ
249 const auto revents = LTTNG_POLL_GETEV(&events, i);
250 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
5b093681
JG
251
252 health_code_update();
253
8a00688e
MJ
254 /* Activity on thread quit pipe, exiting. */
255 if (pollfd == thread_quit_pipe_fd) {
256 DBG("Activity on thread quit pipe");
5b093681
JG
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) {
28ab034a
JG
264 (void) lttng_read(
265 notifiers->kernel_poll_pipe_read_fd, &tmp, 1);
5b093681 266 /*
28ab034a
JG
267 * Ret value is useless here, if this pipe gets any actions
268 * an update is required anyway.
5b093681
JG
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
293exit:
294error:
295 lttng_poll_clean(&events);
296error_poll_create:
297error_testpoint:
298 if (err) {
299 health_error();
300 ERR("Health error occurred in %s", __func__);
301 WARN("Kernel thread died unexpectedly. "
28ab034a 302 "Kernel tracing can continue but CPU hotplug is disabled.");
5b093681 303 }
412d7227 304 health_unregister(the_health_sessiond);
5b093681 305 DBG("Kernel thread dying");
cd9adb8b 306 return nullptr;
5b093681
JG
307}
308
309static bool shutdown_kernel_management_thread(void *data)
310{
7966af57 311 struct thread_notifiers *notifiers = (thread_notifiers *) data;
5b093681
JG
312 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
313
314 return notify_thread_pipe(write_fd) == 1;
315}
316
317static void cleanup_kernel_management_thread(void *data)
318{
7966af57 319 struct thread_notifiers *notifiers = (thread_notifiers *) data;
5b093681
JG
320
321 lttng_pipe_destroy(notifiers->quit_pipe);
322 free(notifiers);
323}
324
325bool launch_kernel_management_thread(int kernel_poll_pipe_read_fd)
326{
327 struct lttng_pipe *quit_pipe;
cd9adb8b 328 struct thread_notifiers *notifiers = nullptr;
5b093681
JG
329 struct lttng_thread *thread;
330
64803277 331 notifiers = zmalloc<thread_notifiers>();
5b093681 332 if (!notifiers) {
21fa020e
JG
333 goto error_alloc;
334 }
335 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
336 if (!quit_pipe) {
5b093681
JG
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",
28ab034a
JG
343 thread_kernel_management,
344 shutdown_kernel_management_thread,
345 cleanup_kernel_management_thread,
346 notifiers);
5b093681
JG
347 if (!thread) {
348 goto error;
349 }
350 lttng_thread_put(thread);
351 return true;
352error:
353 cleanup_kernel_management_thread(notifiers);
21fa020e 354error_alloc:
5b093681
JG
355 return false;
356}
This page took 0.071905 seconds and 4 git commands to generate.