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>
19 #include <common/urcu.hpp>
27 static char *create_channel_path(struct consumer_output
*consumer
, size_t *consumer_path_offset
)
30 char tmp_path
[PATH_MAX
];
31 char *pathname
= nullptr;
33 LTTNG_ASSERT(consumer
);
35 /* Get the right path name destination */
36 if (consumer
->type
== CONSUMER_DST_LOCAL
||
37 (consumer
->type
== CONSUMER_DST_NET
&& consumer
->relay_major_version
== 2 &&
38 consumer
->relay_minor_version
>= 11)) {
39 pathname
= strdup(consumer
->domain_subdir
);
41 PERROR("Failed to copy domain subdirectory string %s",
42 consumer
->domain_subdir
);
45 *consumer_path_offset
= strlen(consumer
->domain_subdir
);
46 DBG3("Kernel local consumer trace path relative to current trace chunk: \"%s\"",
49 /* Network output, relayd < 2.11. */
50 ret
= snprintf(tmp_path
,
53 consumer
->dst
.net
.base_dir
,
54 consumer
->domain_subdir
);
56 PERROR("snprintf kernel metadata path");
58 } else if (ret
>= sizeof(tmp_path
)) {
59 ERR("Kernel channel path exceeds the maximal allowed length of of %zu bytes (%i bytes required) with path \"%s%s\"",
62 consumer
->dst
.net
.base_dir
,
63 consumer
->domain_subdir
);
66 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
68 PERROR("lttng_strndup");
71 *consumer_path_offset
= 0;
72 DBG3("Kernel network consumer subdir path: %s", pathname
);
83 * Sending a single channel to the consumer with command ADD_CHANNEL.
85 static int kernel_consumer_add_channel(struct consumer_socket
*sock
,
86 struct ltt_kernel_channel
*channel
,
87 struct ltt_kernel_session
*ksession
,
91 char *pathname
= nullptr;
92 struct lttcomm_consumer_msg lkm
;
93 struct consumer_output
*consumer
;
94 enum lttng_error_code status
;
95 struct lttng_channel_extended
*channel_attr_extended
;
97 size_t consumer_path_offset
= 0;
98 lttng::urcu::read_lock_guard read_lock
;
99 ltt_session::ref session
;
102 LTTNG_ASSERT(channel
);
103 LTTNG_ASSERT(ksession
);
104 LTTNG_ASSERT(ksession
->consumer
);
106 consumer
= ksession
->consumer
;
107 channel_attr_extended
=
108 (struct lttng_channel_extended
*) channel
->channel
->attr
.extended
.ptr
;
110 DBG("Kernel consumer adding channel %s to kernel consumer", channel
->channel
->name
);
111 is_local_trace
= consumer
->net_seq_index
== -1ULL;
113 pathname
= create_channel_path(consumer
, &consumer_path_offset
);
119 if (is_local_trace
&& ksession
->current_trace_chunk
) {
120 enum lttng_trace_chunk_status chunk_status
;
121 char *pathname_index
;
123 ret
= asprintf(&pathname_index
, "%s/" DEFAULT_INDEX_DIR
, pathname
);
125 ERR("Failed to format channel index directory");
131 * Create the index subdirectory which will take care
132 * of implicitly creating the channel's path.
134 chunk_status
= lttng_trace_chunk_create_subdirectory(ksession
->current_trace_chunk
,
136 free(pathname_index
);
137 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
143 /* Prep channel message structure */
144 consumer_init_add_channel_comm_msg(&lkm
,
147 &pathname
[consumer_path_offset
],
148 consumer
->net_seq_index
,
149 channel
->channel
->name
,
150 channel
->stream_count
,
151 channel
->channel
->attr
.output
,
152 CONSUMER_CHANNEL_TYPE_DATA
,
153 channel
->channel
->attr
.tracefile_size
,
154 channel
->channel
->attr
.tracefile_count
,
156 channel
->channel
->attr
.live_timer_interval
,
157 ksession
->is_live_session
,
158 channel_attr_extended
->monitor_timer_interval
,
159 ksession
->current_trace_chunk
);
161 health_code_update();
163 ret
= consumer_send_channel(sock
, &lkm
);
168 health_code_update();
171 session
= ltt_session::find_session(ksession
->id
);
172 } catch (const lttng::sessiond::exceptions::session_not_found_error
& ex
) {
173 ERR_FMT("Fatal error during the creation of a kernel channel: {}, location='{}'",
179 ASSERT_LOCKED(session
->_lock
);
180 ASSERT_SESSION_LIST_LOCKED();
182 status
= notification_thread_command_add_channel(the_notification_thread_handle
,
184 channel
->channel
->name
,
187 channel
->channel
->attr
.subbuf_size
*
188 channel
->channel
->attr
.num_subbuf
);
189 if (status
!= LTTNG_OK
) {
194 channel
->published_to_notification_thread
= true;
202 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
204 * The consumer socket lock must be held by the caller.
206 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
207 struct ltt_kernel_session
*ksession
,
208 unsigned int monitor
)
211 struct lttcomm_consumer_msg lkm
;
212 struct consumer_output
*consumer
;
214 lttng::urcu::read_lock_guard read_lock
;
217 LTTNG_ASSERT(ksession
);
218 LTTNG_ASSERT(ksession
->consumer
);
221 DBG("Sending metadata %d to kernel consumer", ksession
->metadata_stream_fd
);
223 /* Get consumer output pointer */
224 consumer
= ksession
->consumer
;
226 /* Prep channel message structure */
227 consumer_init_add_channel_comm_msg(&lkm
,
228 ksession
->metadata
->key
,
231 consumer
->net_seq_index
,
232 ksession
->metadata
->conf
->name
,
234 ksession
->metadata
->conf
->attr
.output
,
235 CONSUMER_CHANNEL_TYPE_METADATA
,
236 ksession
->metadata
->conf
->attr
.tracefile_size
,
237 ksession
->metadata
->conf
->attr
.tracefile_count
,
239 ksession
->metadata
->conf
->attr
.live_timer_interval
,
240 ksession
->is_live_session
,
242 ksession
->current_trace_chunk
);
244 health_code_update();
246 ret
= consumer_send_channel(sock
, &lkm
);
251 health_code_update();
253 /* Prep stream message structure */
254 consumer_init_add_stream_comm_msg(&lkm
,
255 ksession
->metadata
->key
,
256 ksession
->metadata_stream_fd
,
257 0 /* CPU: 0 for metadata. */);
259 health_code_update();
261 /* Send stream and file descriptor */
262 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &ksession
->metadata_stream_fd
, 1);
267 health_code_update();
274 * Sending a single stream to the consumer with command ADD_STREAM.
276 static int kernel_consumer_add_stream(struct consumer_socket
*sock
,
277 struct ltt_kernel_channel
*channel
,
278 struct ltt_kernel_stream
*stream
,
279 struct ltt_kernel_session
*session
)
282 struct lttcomm_consumer_msg lkm
;
283 struct consumer_output
*consumer
;
285 LTTNG_ASSERT(channel
);
286 LTTNG_ASSERT(stream
);
287 LTTNG_ASSERT(session
);
288 LTTNG_ASSERT(session
->consumer
);
291 DBG("Sending stream %d of channel %s to kernel consumer",
293 channel
->channel
->name
);
295 /* Get consumer output pointer */
296 consumer
= session
->consumer
;
298 /* Prep stream consumer message */
299 consumer_init_add_stream_comm_msg(&lkm
, channel
->key
, stream
->fd
, stream
->cpu
);
301 health_code_update();
303 /* Send stream and file descriptor */
304 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
309 health_code_update();
316 * Sending the notification that all streams were sent with STREAMS_SENT.
318 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
319 struct ltt_kernel_session
*session
,
320 uint64_t channel_key
)
323 struct lttcomm_consumer_msg lkm
;
324 struct consumer_output
*consumer
;
327 LTTNG_ASSERT(session
);
329 DBG("Sending streams_sent");
330 /* Get consumer output pointer */
331 consumer
= session
->consumer
;
333 /* Prep stream consumer message */
334 consumer_init_streams_sent_comm_msg(
335 &lkm
, LTTNG_CONSUMER_STREAMS_SENT
, channel_key
, consumer
->net_seq_index
);
337 health_code_update();
339 /* Send stream and file descriptor */
340 ret
= consumer_send_msg(sock
, &lkm
);
350 * Send all stream fds of kernel channel to the consumer.
352 * The consumer socket lock must be held by the caller.
354 int kernel_consumer_send_channel_streams(struct consumer_socket
*sock
,
355 struct ltt_kernel_channel
*channel
,
356 struct ltt_kernel_session
*ksession
,
357 unsigned int monitor
)
360 struct ltt_kernel_stream
*stream
;
363 LTTNG_ASSERT(channel
);
364 LTTNG_ASSERT(ksession
);
365 LTTNG_ASSERT(ksession
->consumer
);
368 lttng::urcu::read_lock_guard read_lock
;
370 /* Bail out if consumer is disabled */
371 if (!ksession
->consumer
->enabled
) {
376 DBG("Sending streams of channel %s to kernel consumer", channel
->channel
->name
);
378 if (!channel
->sent_to_consumer
) {
379 ret
= kernel_consumer_add_channel(sock
, channel
, ksession
, monitor
);
383 channel
->sent_to_consumer
= true;
387 cds_list_for_each_entry (stream
, &channel
->stream_list
.head
, list
) {
388 if (!stream
->fd
|| stream
->sent_to_consumer
) {
392 /* Add stream on the kernel consumer side. */
393 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, ksession
);
397 stream
->sent_to_consumer
= true;
405 * Send all stream fds of the kernel session to the consumer.
407 * The consumer socket lock must be held by the caller.
409 int kernel_consumer_send_session(struct consumer_socket
*sock
, struct ltt_kernel_session
*session
)
411 int ret
, monitor
= 0;
412 struct ltt_kernel_channel
*chan
;
415 LTTNG_ASSERT(session
);
416 LTTNG_ASSERT(session
->consumer
);
419 /* Bail out if consumer is disabled */
420 if (!session
->consumer
->enabled
) {
425 /* Don't monitor the streams on the consumer if in flight recorder. */
426 if (session
->output_traces
) {
430 DBG("Sending session stream to kernel consumer");
432 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
433 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
439 /* Send channel and streams of it */
440 cds_list_for_each_entry (chan
, &session
->channel_list
.head
, list
) {
441 ret
= kernel_consumer_send_channel_streams(sock
, chan
, session
, monitor
);
447 * Inform the relay that all the streams for the
450 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->key
);
457 DBG("Kernel consumer FDs of metadata and channel streams sent");
459 session
->consumer_fds_sent
= 1;
466 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
467 struct ltt_kernel_channel
*channel
)
470 struct lttcomm_consumer_msg msg
;
472 LTTNG_ASSERT(channel
);
473 LTTNG_ASSERT(socket
);
475 DBG("Sending kernel consumer destroy channel key %" PRIu64
, channel
->key
);
477 memset(&msg
, 0, sizeof(msg
));
478 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
479 msg
.u
.destroy_channel
.key
= channel
->key
;
481 pthread_mutex_lock(socket
->lock
);
482 health_code_update();
484 ret
= consumer_send_msg(socket
, &msg
);
490 health_code_update();
491 pthread_mutex_unlock(socket
->lock
);
495 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
496 struct ltt_kernel_metadata
*metadata
)
499 struct lttcomm_consumer_msg msg
;
501 LTTNG_ASSERT(metadata
);
502 LTTNG_ASSERT(socket
);
504 DBG("Sending kernel consumer destroy channel key %" PRIu64
, metadata
->key
);
506 memset(&msg
, 0, sizeof(msg
));
507 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
508 msg
.u
.destroy_channel
.key
= metadata
->key
;
510 pthread_mutex_lock(socket
->lock
);
511 health_code_update();
513 ret
= consumer_send_msg(socket
, &msg
);
519 health_code_update();
520 pthread_mutex_unlock(socket
->lock
);