clang-tidy: add Chrome-inspired checks
[lttng-tools.git] / src / bin / lttng-sessiond / kernel-consumer.cpp
... / ...
CommitLineData
1/*
2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8#define _LGPL_SOURCE
9#include "consumer.hpp"
10#include "health-sessiond.hpp"
11#include "kernel-consumer.hpp"
12#include "lttng-sessiond.hpp"
13#include "notification-thread-commands.hpp"
14#include "session.hpp"
15
16#include <common/common.hpp>
17#include <common/compat/string.hpp>
18#include <common/defaults.hpp>
19
20#include <inttypes.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/stat.h>
24#include <unistd.h>
25
26static char *create_channel_path(struct consumer_output *consumer, size_t *consumer_path_offset)
27{
28 int ret;
29 char tmp_path[PATH_MAX];
30 char *pathname = nullptr;
31
32 LTTNG_ASSERT(consumer);
33
34 /* Get the right path name destination */
35 if (consumer->type == CONSUMER_DST_LOCAL ||
36 (consumer->type == CONSUMER_DST_NET && consumer->relay_major_version == 2 &&
37 consumer->relay_minor_version >= 11)) {
38 pathname = strdup(consumer->domain_subdir);
39 if (!pathname) {
40 PERROR("Failed to copy domain subdirectory string %s",
41 consumer->domain_subdir);
42 goto error;
43 }
44 *consumer_path_offset = strlen(consumer->domain_subdir);
45 DBG3("Kernel local consumer trace path relative to current trace chunk: \"%s\"",
46 pathname);
47 } else {
48 /* Network output, relayd < 2.11. */
49 ret = snprintf(tmp_path,
50 sizeof(tmp_path),
51 "%s%s",
52 consumer->dst.net.base_dir,
53 consumer->domain_subdir);
54 if (ret < 0) {
55 PERROR("snprintf kernel metadata path");
56 goto error;
57 } else if (ret >= sizeof(tmp_path)) {
58 ERR("Kernel channel path exceeds the maximal allowed length of of %zu bytes (%i bytes required) with path \"%s%s\"",
59 sizeof(tmp_path),
60 ret,
61 consumer->dst.net.base_dir,
62 consumer->domain_subdir);
63 goto error;
64 }
65 pathname = lttng_strndup(tmp_path, sizeof(tmp_path));
66 if (!pathname) {
67 PERROR("lttng_strndup");
68 goto error;
69 }
70 *consumer_path_offset = 0;
71 DBG3("Kernel network consumer subdir path: %s", pathname);
72 }
73
74 return pathname;
75
76error:
77 free(pathname);
78 return nullptr;
79}
80
81/*
82 * Sending a single channel to the consumer with command ADD_CHANNEL.
83 */
84static int kernel_consumer_add_channel(struct consumer_socket *sock,
85 struct ltt_kernel_channel *channel,
86 struct ltt_kernel_session *ksession,
87 unsigned int monitor)
88{
89 int ret;
90 char *pathname = nullptr;
91 struct lttcomm_consumer_msg lkm;
92 struct consumer_output *consumer;
93 enum lttng_error_code status;
94 struct ltt_session *session = nullptr;
95 struct lttng_channel_extended *channel_attr_extended;
96 bool is_local_trace;
97 size_t consumer_path_offset = 0;
98
99 /* Safety net */
100 LTTNG_ASSERT(channel);
101 LTTNG_ASSERT(ksession);
102 LTTNG_ASSERT(ksession->consumer);
103
104 consumer = ksession->consumer;
105 channel_attr_extended =
106 (struct lttng_channel_extended *) channel->channel->attr.extended.ptr;
107
108 DBG("Kernel consumer adding channel %s to kernel consumer", channel->channel->name);
109 is_local_trace = consumer->net_seq_index == -1ULL;
110
111 pathname = create_channel_path(consumer, &consumer_path_offset);
112 if (!pathname) {
113 ret = -1;
114 goto error;
115 }
116
117 if (is_local_trace && ksession->current_trace_chunk) {
118 enum lttng_trace_chunk_status chunk_status;
119 char *pathname_index;
120
121 ret = asprintf(&pathname_index, "%s/" DEFAULT_INDEX_DIR, pathname);
122 if (ret < 0) {
123 ERR("Failed to format channel index directory");
124 ret = -1;
125 goto error;
126 }
127
128 /*
129 * Create the index subdirectory which will take care
130 * of implicitly creating the channel's path.
131 */
132 chunk_status = lttng_trace_chunk_create_subdirectory(ksession->current_trace_chunk,
133 pathname_index);
134 free(pathname_index);
135 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
136 ret = -1;
137 goto error;
138 }
139 }
140
141 /* Prep channel message structure */
142 consumer_init_add_channel_comm_msg(&lkm,
143 channel->key,
144 ksession->id,
145 &pathname[consumer_path_offset],
146 consumer->net_seq_index,
147 channel->channel->name,
148 channel->stream_count,
149 channel->channel->attr.output,
150 CONSUMER_CHANNEL_TYPE_DATA,
151 channel->channel->attr.tracefile_size,
152 channel->channel->attr.tracefile_count,
153 monitor,
154 channel->channel->attr.live_timer_interval,
155 ksession->is_live_session,
156 channel_attr_extended->monitor_timer_interval,
157 ksession->current_trace_chunk);
158
159 health_code_update();
160
161 ret = consumer_send_channel(sock, &lkm);
162 if (ret < 0) {
163 goto error;
164 }
165
166 health_code_update();
167 rcu_read_lock();
168 session = session_find_by_id(ksession->id);
169 LTTNG_ASSERT(session);
170 ASSERT_LOCKED(session->lock);
171 ASSERT_SESSION_LIST_LOCKED();
172
173 status = notification_thread_command_add_channel(the_notification_thread_handle,
174 session->id,
175 channel->channel->name,
176 channel->key,
177 LTTNG_DOMAIN_KERNEL,
178 channel->channel->attr.subbuf_size *
179 channel->channel->attr.num_subbuf);
180 rcu_read_unlock();
181 if (status != LTTNG_OK) {
182 ret = -1;
183 goto error;
184 }
185
186 channel->published_to_notification_thread = true;
187
188error:
189 if (session) {
190 session_put(session);
191 }
192 free(pathname);
193 return ret;
194}
195
196/*
197 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
198 *
199 * The consumer socket lock must be held by the caller.
200 */
201int kernel_consumer_add_metadata(struct consumer_socket *sock,
202 struct ltt_kernel_session *ksession,
203 unsigned int monitor)
204{
205 int ret;
206 struct lttcomm_consumer_msg lkm;
207 struct consumer_output *consumer;
208
209 rcu_read_lock();
210
211 /* Safety net */
212 LTTNG_ASSERT(ksession);
213 LTTNG_ASSERT(ksession->consumer);
214 LTTNG_ASSERT(sock);
215
216 DBG("Sending metadata %d to kernel consumer", ksession->metadata_stream_fd);
217
218 /* Get consumer output pointer */
219 consumer = ksession->consumer;
220
221 /* Prep channel message structure */
222 consumer_init_add_channel_comm_msg(&lkm,
223 ksession->metadata->key,
224 ksession->id,
225 "",
226 consumer->net_seq_index,
227 ksession->metadata->conf->name,
228 1,
229 ksession->metadata->conf->attr.output,
230 CONSUMER_CHANNEL_TYPE_METADATA,
231 ksession->metadata->conf->attr.tracefile_size,
232 ksession->metadata->conf->attr.tracefile_count,
233 monitor,
234 ksession->metadata->conf->attr.live_timer_interval,
235 ksession->is_live_session,
236 0,
237 ksession->current_trace_chunk);
238
239 health_code_update();
240
241 ret = consumer_send_channel(sock, &lkm);
242 if (ret < 0) {
243 goto error;
244 }
245
246 health_code_update();
247
248 /* Prep stream message structure */
249 consumer_init_add_stream_comm_msg(&lkm,
250 ksession->metadata->key,
251 ksession->metadata_stream_fd,
252 0 /* CPU: 0 for metadata. */);
253
254 health_code_update();
255
256 /* Send stream and file descriptor */
257 ret = consumer_send_stream(sock, consumer, &lkm, &ksession->metadata_stream_fd, 1);
258 if (ret < 0) {
259 goto error;
260 }
261
262 health_code_update();
263
264error:
265 rcu_read_unlock();
266 return ret;
267}
268
269/*
270 * Sending a single stream to the consumer with command ADD_STREAM.
271 */
272static int kernel_consumer_add_stream(struct consumer_socket *sock,
273 struct ltt_kernel_channel *channel,
274 struct ltt_kernel_stream *stream,
275 struct ltt_kernel_session *session)
276{
277 int ret;
278 struct lttcomm_consumer_msg lkm;
279 struct consumer_output *consumer;
280
281 LTTNG_ASSERT(channel);
282 LTTNG_ASSERT(stream);
283 LTTNG_ASSERT(session);
284 LTTNG_ASSERT(session->consumer);
285 LTTNG_ASSERT(sock);
286
287 DBG("Sending stream %d of channel %s to kernel consumer",
288 stream->fd,
289 channel->channel->name);
290
291 /* Get consumer output pointer */
292 consumer = session->consumer;
293
294 /* Prep stream consumer message */
295 consumer_init_add_stream_comm_msg(&lkm, channel->key, stream->fd, stream->cpu);
296
297 health_code_update();
298
299 /* Send stream and file descriptor */
300 ret = consumer_send_stream(sock, consumer, &lkm, &stream->fd, 1);
301 if (ret < 0) {
302 goto error;
303 }
304
305 health_code_update();
306
307error:
308 return ret;
309}
310
311/*
312 * Sending the notification that all streams were sent with STREAMS_SENT.
313 */
314int kernel_consumer_streams_sent(struct consumer_socket *sock,
315 struct ltt_kernel_session *session,
316 uint64_t channel_key)
317{
318 int ret;
319 struct lttcomm_consumer_msg lkm;
320 struct consumer_output *consumer;
321
322 LTTNG_ASSERT(sock);
323 LTTNG_ASSERT(session);
324
325 DBG("Sending streams_sent");
326 /* Get consumer output pointer */
327 consumer = session->consumer;
328
329 /* Prep stream consumer message */
330 consumer_init_streams_sent_comm_msg(
331 &lkm, LTTNG_CONSUMER_STREAMS_SENT, channel_key, consumer->net_seq_index);
332
333 health_code_update();
334
335 /* Send stream and file descriptor */
336 ret = consumer_send_msg(sock, &lkm);
337 if (ret < 0) {
338 goto error;
339 }
340
341error:
342 return ret;
343}
344
345/*
346 * Send all stream fds of kernel channel to the consumer.
347 *
348 * The consumer socket lock must be held by the caller.
349 */
350int kernel_consumer_send_channel_streams(struct consumer_socket *sock,
351 struct ltt_kernel_channel *channel,
352 struct ltt_kernel_session *ksession,
353 unsigned int monitor)
354{
355 int ret = LTTNG_OK;
356 struct ltt_kernel_stream *stream;
357
358 /* Safety net */
359 LTTNG_ASSERT(channel);
360 LTTNG_ASSERT(ksession);
361 LTTNG_ASSERT(ksession->consumer);
362 LTTNG_ASSERT(sock);
363
364 rcu_read_lock();
365
366 /* Bail out if consumer is disabled */
367 if (!ksession->consumer->enabled) {
368 ret = LTTNG_OK;
369 goto error;
370 }
371
372 DBG("Sending streams of channel %s to kernel consumer", channel->channel->name);
373
374 if (!channel->sent_to_consumer) {
375 ret = kernel_consumer_add_channel(sock, channel, ksession, monitor);
376 if (ret < 0) {
377 goto error;
378 }
379 channel->sent_to_consumer = true;
380 }
381
382 /* Send streams */
383 cds_list_for_each_entry (stream, &channel->stream_list.head, list) {
384 if (!stream->fd || stream->sent_to_consumer) {
385 continue;
386 }
387
388 /* Add stream on the kernel consumer side. */
389 ret = kernel_consumer_add_stream(sock, channel, stream, ksession);
390 if (ret < 0) {
391 goto error;
392 }
393 stream->sent_to_consumer = true;
394 }
395
396error:
397 rcu_read_unlock();
398 return ret;
399}
400
401/*
402 * Send all stream fds of the kernel session to the consumer.
403 *
404 * The consumer socket lock must be held by the caller.
405 */
406int kernel_consumer_send_session(struct consumer_socket *sock, struct ltt_kernel_session *session)
407{
408 int ret, monitor = 0;
409 struct ltt_kernel_channel *chan;
410
411 /* Safety net */
412 LTTNG_ASSERT(session);
413 LTTNG_ASSERT(session->consumer);
414 LTTNG_ASSERT(sock);
415
416 /* Bail out if consumer is disabled */
417 if (!session->consumer->enabled) {
418 ret = LTTNG_OK;
419 goto error;
420 }
421
422 /* Don't monitor the streams on the consumer if in flight recorder. */
423 if (session->output_traces) {
424 monitor = 1;
425 }
426
427 DBG("Sending session stream to kernel consumer");
428
429 if (session->metadata_stream_fd >= 0 && session->metadata) {
430 ret = kernel_consumer_add_metadata(sock, session, monitor);
431 if (ret < 0) {
432 goto error;
433 }
434 }
435
436 /* Send channel and streams of it */
437 cds_list_for_each_entry (chan, &session->channel_list.head, list) {
438 ret = kernel_consumer_send_channel_streams(sock, chan, session, monitor);
439 if (ret < 0) {
440 goto error;
441 }
442 if (monitor) {
443 /*
444 * Inform the relay that all the streams for the
445 * channel were sent.
446 */
447 ret = kernel_consumer_streams_sent(sock, session, chan->key);
448 if (ret < 0) {
449 goto error;
450 }
451 }
452 }
453
454 DBG("Kernel consumer FDs of metadata and channel streams sent");
455
456 session->consumer_fds_sent = 1;
457 return 0;
458
459error:
460 return ret;
461}
462
463int kernel_consumer_destroy_channel(struct consumer_socket *socket,
464 struct ltt_kernel_channel *channel)
465{
466 int ret;
467 struct lttcomm_consumer_msg msg;
468
469 LTTNG_ASSERT(channel);
470 LTTNG_ASSERT(socket);
471
472 DBG("Sending kernel consumer destroy channel key %" PRIu64, channel->key);
473
474 memset(&msg, 0, sizeof(msg));
475 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
476 msg.u.destroy_channel.key = channel->key;
477
478 pthread_mutex_lock(socket->lock);
479 health_code_update();
480
481 ret = consumer_send_msg(socket, &msg);
482 if (ret < 0) {
483 goto error;
484 }
485
486error:
487 health_code_update();
488 pthread_mutex_unlock(socket->lock);
489 return ret;
490}
491
492int kernel_consumer_destroy_metadata(struct consumer_socket *socket,
493 struct ltt_kernel_metadata *metadata)
494{
495 int ret;
496 struct lttcomm_consumer_msg msg;
497
498 LTTNG_ASSERT(metadata);
499 LTTNG_ASSERT(socket);
500
501 DBG("Sending kernel consumer destroy channel key %" PRIu64, metadata->key);
502
503 memset(&msg, 0, sizeof(msg));
504 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
505 msg.u.destroy_channel.key = metadata->key;
506
507 pthread_mutex_lock(socket->lock);
508 health_code_update();
509
510 ret = consumer_send_msg(socket, &msg);
511 if (ret < 0) {
512 goto error;
513 }
514
515error:
516 health_code_update();
517 pthread_mutex_unlock(socket->lock);
518 return ret;
519}
This page took 0.024105 seconds and 4 git commands to generate.