2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
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"
16 #include <common/common.hpp>
17 #include <common/compat/string.hpp>
18 #include <common/defaults.hpp>
26 static char *create_channel_path(struct consumer_output
*consumer
, size_t *consumer_path_offset
)
29 char tmp_path
[PATH_MAX
];
30 char *pathname
= nullptr;
32 LTTNG_ASSERT(consumer
);
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
);
40 PERROR("Failed to copy domain subdirectory string %s",
41 consumer
->domain_subdir
);
44 *consumer_path_offset
= strlen(consumer
->domain_subdir
);
45 DBG3("Kernel local consumer trace path relative to current trace chunk: \"%s\"",
48 /* Network output, relayd < 2.11. */
49 ret
= snprintf(tmp_path
,
52 consumer
->dst
.net
.base_dir
,
53 consumer
->domain_subdir
);
55 PERROR("snprintf kernel metadata path");
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\"",
61 consumer
->dst
.net
.base_dir
,
62 consumer
->domain_subdir
);
65 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
67 PERROR("lttng_strndup");
70 *consumer_path_offset
= 0;
71 DBG3("Kernel network consumer subdir path: %s", pathname
);
82 * Sending a single channel to the consumer with command ADD_CHANNEL.
84 static int kernel_consumer_add_channel(struct consumer_socket
*sock
,
85 struct ltt_kernel_channel
*channel
,
86 struct ltt_kernel_session
*ksession
,
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
;
97 size_t consumer_path_offset
= 0;
100 LTTNG_ASSERT(channel
);
101 LTTNG_ASSERT(ksession
);
102 LTTNG_ASSERT(ksession
->consumer
);
104 consumer
= ksession
->consumer
;
105 channel_attr_extended
=
106 (struct lttng_channel_extended
*) channel
->channel
->attr
.extended
.ptr
;
108 DBG("Kernel consumer adding channel %s to kernel consumer", channel
->channel
->name
);
109 is_local_trace
= consumer
->net_seq_index
== -1ULL;
111 pathname
= create_channel_path(consumer
, &consumer_path_offset
);
117 if (is_local_trace
&& ksession
->current_trace_chunk
) {
118 enum lttng_trace_chunk_status chunk_status
;
119 char *pathname_index
;
121 ret
= asprintf(&pathname_index
, "%s/" DEFAULT_INDEX_DIR
, pathname
);
123 ERR("Failed to format channel index directory");
129 * Create the index subdirectory which will take care
130 * of implicitly creating the channel's path.
132 chunk_status
= lttng_trace_chunk_create_subdirectory(ksession
->current_trace_chunk
,
134 free(pathname_index
);
135 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
141 /* Prep channel message structure */
142 consumer_init_add_channel_comm_msg(&lkm
,
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
,
154 channel
->channel
->attr
.live_timer_interval
,
155 ksession
->is_live_session
,
156 channel_attr_extended
->monitor_timer_interval
,
157 ksession
->current_trace_chunk
);
159 health_code_update();
161 ret
= consumer_send_channel(sock
, &lkm
);
166 health_code_update();
168 session
= session_find_by_id(ksession
->id
);
169 LTTNG_ASSERT(session
);
170 ASSERT_LOCKED(session
->lock
);
171 ASSERT_SESSION_LIST_LOCKED();
173 status
= notification_thread_command_add_channel(the_notification_thread_handle
,
175 channel
->channel
->name
,
178 channel
->channel
->attr
.subbuf_size
*
179 channel
->channel
->attr
.num_subbuf
);
181 if (status
!= LTTNG_OK
) {
186 channel
->published_to_notification_thread
= true;
190 session_put(session
);
197 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
199 * The consumer socket lock must be held by the caller.
201 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
202 struct ltt_kernel_session
*ksession
,
203 unsigned int monitor
)
206 struct lttcomm_consumer_msg lkm
;
207 struct consumer_output
*consumer
;
212 LTTNG_ASSERT(ksession
);
213 LTTNG_ASSERT(ksession
->consumer
);
216 DBG("Sending metadata %d to kernel consumer", ksession
->metadata_stream_fd
);
218 /* Get consumer output pointer */
219 consumer
= ksession
->consumer
;
221 /* Prep channel message structure */
222 consumer_init_add_channel_comm_msg(&lkm
,
223 ksession
->metadata
->key
,
226 consumer
->net_seq_index
,
227 ksession
->metadata
->conf
->name
,
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
,
234 ksession
->metadata
->conf
->attr
.live_timer_interval
,
235 ksession
->is_live_session
,
237 ksession
->current_trace_chunk
);
239 health_code_update();
241 ret
= consumer_send_channel(sock
, &lkm
);
246 health_code_update();
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. */);
254 health_code_update();
256 /* Send stream and file descriptor */
257 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &ksession
->metadata_stream_fd
, 1);
262 health_code_update();
270 * Sending a single stream to the consumer with command ADD_STREAM.
272 static 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
)
278 struct lttcomm_consumer_msg lkm
;
279 struct consumer_output
*consumer
;
281 LTTNG_ASSERT(channel
);
282 LTTNG_ASSERT(stream
);
283 LTTNG_ASSERT(session
);
284 LTTNG_ASSERT(session
->consumer
);
287 DBG("Sending stream %d of channel %s to kernel consumer",
289 channel
->channel
->name
);
291 /* Get consumer output pointer */
292 consumer
= session
->consumer
;
294 /* Prep stream consumer message */
295 consumer_init_add_stream_comm_msg(&lkm
, channel
->key
, stream
->fd
, stream
->cpu
);
297 health_code_update();
299 /* Send stream and file descriptor */
300 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
305 health_code_update();
312 * Sending the notification that all streams were sent with STREAMS_SENT.
314 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
315 struct ltt_kernel_session
*session
,
316 uint64_t channel_key
)
319 struct lttcomm_consumer_msg lkm
;
320 struct consumer_output
*consumer
;
323 LTTNG_ASSERT(session
);
325 DBG("Sending streams_sent");
326 /* Get consumer output pointer */
327 consumer
= session
->consumer
;
329 /* Prep stream consumer message */
330 consumer_init_streams_sent_comm_msg(
331 &lkm
, LTTNG_CONSUMER_STREAMS_SENT
, channel_key
, consumer
->net_seq_index
);
333 health_code_update();
335 /* Send stream and file descriptor */
336 ret
= consumer_send_msg(sock
, &lkm
);
346 * Send all stream fds of kernel channel to the consumer.
348 * The consumer socket lock must be held by the caller.
350 int 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
)
356 struct ltt_kernel_stream
*stream
;
359 LTTNG_ASSERT(channel
);
360 LTTNG_ASSERT(ksession
);
361 LTTNG_ASSERT(ksession
->consumer
);
366 /* Bail out if consumer is disabled */
367 if (!ksession
->consumer
->enabled
) {
372 DBG("Sending streams of channel %s to kernel consumer", channel
->channel
->name
);
374 if (!channel
->sent_to_consumer
) {
375 ret
= kernel_consumer_add_channel(sock
, channel
, ksession
, monitor
);
379 channel
->sent_to_consumer
= true;
383 cds_list_for_each_entry (stream
, &channel
->stream_list
.head
, list
) {
384 if (!stream
->fd
|| stream
->sent_to_consumer
) {
388 /* Add stream on the kernel consumer side. */
389 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, ksession
);
393 stream
->sent_to_consumer
= true;
402 * Send all stream fds of the kernel session to the consumer.
404 * The consumer socket lock must be held by the caller.
406 int kernel_consumer_send_session(struct consumer_socket
*sock
, struct ltt_kernel_session
*session
)
408 int ret
, monitor
= 0;
409 struct ltt_kernel_channel
*chan
;
412 LTTNG_ASSERT(session
);
413 LTTNG_ASSERT(session
->consumer
);
416 /* Bail out if consumer is disabled */
417 if (!session
->consumer
->enabled
) {
422 /* Don't monitor the streams on the consumer if in flight recorder. */
423 if (session
->output_traces
) {
427 DBG("Sending session stream to kernel consumer");
429 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
430 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
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
);
444 * Inform the relay that all the streams for the
447 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->key
);
454 DBG("Kernel consumer FDs of metadata and channel streams sent");
456 session
->consumer_fds_sent
= 1;
463 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
464 struct ltt_kernel_channel
*channel
)
467 struct lttcomm_consumer_msg msg
;
469 LTTNG_ASSERT(channel
);
470 LTTNG_ASSERT(socket
);
472 DBG("Sending kernel consumer destroy channel key %" PRIu64
, channel
->key
);
474 memset(&msg
, 0, sizeof(msg
));
475 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
476 msg
.u
.destroy_channel
.key
= channel
->key
;
478 pthread_mutex_lock(socket
->lock
);
479 health_code_update();
481 ret
= consumer_send_msg(socket
, &msg
);
487 health_code_update();
488 pthread_mutex_unlock(socket
->lock
);
492 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
493 struct ltt_kernel_metadata
*metadata
)
496 struct lttcomm_consumer_msg msg
;
498 LTTNG_ASSERT(metadata
);
499 LTTNG_ASSERT(socket
);
501 DBG("Sending kernel consumer destroy channel key %" PRIu64
, metadata
->key
);
503 memset(&msg
, 0, sizeof(msg
));
504 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
505 msg
.u
.destroy_channel
.key
= metadata
->key
;
507 pthread_mutex_lock(socket
->lock
);
508 health_code_update();
510 ret
= consumer_send_msg(socket
, &msg
);
516 health_code_update();
517 pthread_mutex_unlock(socket
->lock
);