2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <common/common.h>
16 #include <common/defaults.h>
17 #include <common/compat/string.h>
20 #include "health-sessiond.h"
21 #include "kernel-consumer.h"
22 #include "notification-thread-commands.h"
24 #include "lttng-sessiond.h"
26 static char *create_channel_path(struct consumer_output
*consumer
,
27 size_t *consumer_path_offset
)
30 char tmp_path
[PATH_MAX
];
31 char *pathname
= NULL
;
33 LTTNG_ASSERT(consumer
);
35 /* Get the right path name destination */
36 if (consumer
->type
== CONSUMER_DST_LOCAL
||
37 (consumer
->type
== CONSUMER_DST_NET
&&
38 consumer
->relay_major_version
== 2 &&
39 consumer
->relay_minor_version
>= 11)) {
40 pathname
= strdup(consumer
->domain_subdir
);
42 PERROR("Failed to copy domain subdirectory string %s",
43 consumer
->domain_subdir
);
46 *consumer_path_offset
= strlen(consumer
->domain_subdir
);
47 DBG3("Kernel local consumer trace path relative to current trace chunk: \"%s\"",
50 /* Network output, relayd < 2.11. */
51 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s%s",
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\"",
59 sizeof(tmp_path
), ret
,
60 consumer
->dst
.net
.base_dir
,
61 consumer
->domain_subdir
);
64 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
66 PERROR("lttng_strndup");
69 *consumer_path_offset
= 0;
70 DBG3("Kernel network consumer subdir path: %s", pathname
);
81 * Sending a single channel to the consumer with command ADD_CHANNEL.
84 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
85 struct ltt_kernel_channel
*channel
,
86 struct ltt_kernel_session
*ksession
,
90 char *pathname
= NULL
;
91 struct lttcomm_consumer_msg lkm
;
92 struct consumer_output
*consumer
;
93 enum lttng_error_code status
;
94 struct ltt_session
*session
= NULL
;
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
= (struct lttng_channel_extended
*)
106 channel
->channel
->attr
.extended
.ptr
;
108 DBG("Kernel consumer adding channel %s to kernel consumer",
109 channel
->channel
->name
);
110 is_local_trace
= consumer
->net_seq_index
== -1ULL;
112 pathname
= create_channel_path(consumer
, &consumer_path_offset
);
118 if (is_local_trace
&& ksession
->current_trace_chunk
) {
119 enum lttng_trace_chunk_status chunk_status
;
120 char *pathname_index
;
122 ret
= asprintf(&pathname_index
, "%s/" DEFAULT_INDEX_DIR
,
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(
135 ksession
->current_trace_chunk
, pathname_index
);
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
],
150 consumer
->net_seq_index
,
151 channel
->channel
->name
,
152 channel
->stream_count
,
153 channel
->channel
->attr
.output
,
154 CONSUMER_CHANNEL_TYPE_DATA
,
155 channel
->channel
->attr
.tracefile_size
,
156 channel
->channel
->attr
.tracefile_count
,
158 channel
->channel
->attr
.live_timer_interval
,
159 ksession
->is_live_session
,
160 channel_attr_extended
->monitor_timer_interval
,
161 ksession
->current_trace_chunk
);
163 health_code_update();
165 ret
= consumer_send_channel(sock
, &lkm
);
170 health_code_update();
172 session
= session_find_by_id(ksession
->id
);
173 LTTNG_ASSERT(session
);
174 LTTNG_ASSERT(pthread_mutex_trylock(&session
->lock
));
175 LTTNG_ASSERT(session_trylock_list());
177 status
= notification_thread_command_add_channel(
178 the_notification_thread_handle
, session
->name
,
179 ksession
->uid
, ksession
->gid
, channel
->channel
->name
,
180 channel
->key
, LTTNG_DOMAIN_KERNEL
,
181 channel
->channel
->attr
.subbuf_size
*
182 channel
->channel
->attr
.num_subbuf
);
184 if (status
!= LTTNG_OK
) {
189 channel
->published_to_notification_thread
= true;
193 session_put(session
);
200 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
202 * The consumer socket lock must be held by the caller.
204 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
205 struct ltt_kernel_session
*ksession
, unsigned int monitor
)
208 struct lttcomm_consumer_msg lkm
;
209 struct consumer_output
*consumer
;
214 LTTNG_ASSERT(ksession
);
215 LTTNG_ASSERT(ksession
->consumer
);
218 DBG("Sending metadata %d to kernel consumer",
219 ksession
->metadata_stream_fd
);
221 /* Get consumer output pointer */
222 consumer
= ksession
->consumer
;
224 /* Prep channel message structure */
225 consumer_init_add_channel_comm_msg(&lkm
,
226 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
,
263 &ksession
->metadata_stream_fd
, 1);
268 health_code_update();
276 * Sending a single stream to the consumer with command ADD_STREAM.
279 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
280 struct ltt_kernel_channel
*channel
,
281 struct ltt_kernel_stream
*stream
,
282 struct ltt_kernel_session
*session
, unsigned int monitor
)
285 struct lttcomm_consumer_msg lkm
;
286 struct consumer_output
*consumer
;
288 LTTNG_ASSERT(channel
);
289 LTTNG_ASSERT(stream
);
290 LTTNG_ASSERT(session
);
291 LTTNG_ASSERT(session
->consumer
);
294 DBG("Sending stream %d of channel %s to kernel consumer",
295 stream
->fd
, channel
->channel
->name
);
297 /* Get consumer output pointer */
298 consumer
= session
->consumer
;
300 /* Prep stream consumer message */
301 consumer_init_add_stream_comm_msg(&lkm
,
306 health_code_update();
308 /* Send stream and file descriptor */
309 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
314 health_code_update();
321 * Sending the notification that all streams were sent with STREAMS_SENT.
323 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
324 struct ltt_kernel_session
*session
, uint64_t channel_key
)
327 struct lttcomm_consumer_msg lkm
;
328 struct consumer_output
*consumer
;
331 LTTNG_ASSERT(session
);
333 DBG("Sending streams_sent");
334 /* Get consumer output pointer */
335 consumer
= session
->consumer
;
337 /* Prep stream consumer message */
338 consumer_init_streams_sent_comm_msg(&lkm
,
339 LTTNG_CONSUMER_STREAMS_SENT
,
340 channel_key
, consumer
->net_seq_index
);
342 health_code_update();
344 /* Send stream and file descriptor */
345 ret
= consumer_send_msg(sock
, &lkm
);
355 * Send all stream fds of kernel channel to the consumer.
357 * The consumer socket lock must be held by the caller.
359 int kernel_consumer_send_channel_streams(struct consumer_socket
*sock
,
360 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*ksession
,
361 unsigned int monitor
)
364 struct ltt_kernel_stream
*stream
;
367 LTTNG_ASSERT(channel
);
368 LTTNG_ASSERT(ksession
);
369 LTTNG_ASSERT(ksession
->consumer
);
374 /* Bail out if consumer is disabled */
375 if (!ksession
->consumer
->enabled
) {
380 DBG("Sending streams of channel %s to kernel consumer",
381 channel
->channel
->name
);
383 if (!channel
->sent_to_consumer
) {
384 ret
= kernel_consumer_add_channel(sock
, channel
, ksession
, monitor
);
388 channel
->sent_to_consumer
= true;
392 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
393 if (!stream
->fd
|| stream
->sent_to_consumer
) {
397 /* Add stream on the kernel consumer side. */
398 ret
= kernel_consumer_add_stream(sock
, channel
, stream
,
403 stream
->sent_to_consumer
= true;
412 * Send all stream fds of the kernel session to the consumer.
414 * The consumer socket lock must be held by the caller.
416 int kernel_consumer_send_session(struct consumer_socket
*sock
,
417 struct ltt_kernel_session
*session
)
419 int ret
, monitor
= 0;
420 struct ltt_kernel_channel
*chan
;
423 LTTNG_ASSERT(session
);
424 LTTNG_ASSERT(session
->consumer
);
427 /* Bail out if consumer is disabled */
428 if (!session
->consumer
->enabled
) {
433 /* Don't monitor the streams on the consumer if in flight recorder. */
434 if (session
->output_traces
) {
438 DBG("Sending session stream to kernel consumer");
440 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
441 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
447 /* Send channel and streams of it */
448 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
449 ret
= kernel_consumer_send_channel_streams(sock
, chan
, session
,
456 * Inform the relay that all the streams for the
459 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->key
);
466 DBG("Kernel consumer FDs of metadata and channel streams sent");
468 session
->consumer_fds_sent
= 1;
475 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
476 struct ltt_kernel_channel
*channel
)
479 struct lttcomm_consumer_msg msg
;
481 LTTNG_ASSERT(channel
);
482 LTTNG_ASSERT(socket
);
484 DBG("Sending kernel consumer destroy channel key %" PRIu64
, channel
->key
);
486 memset(&msg
, 0, sizeof(msg
));
487 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
488 msg
.u
.destroy_channel
.key
= channel
->key
;
490 pthread_mutex_lock(socket
->lock
);
491 health_code_update();
493 ret
= consumer_send_msg(socket
, &msg
);
499 health_code_update();
500 pthread_mutex_unlock(socket
->lock
);
504 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
505 struct ltt_kernel_metadata
*metadata
)
508 struct lttcomm_consumer_msg msg
;
510 LTTNG_ASSERT(metadata
);
511 LTTNG_ASSERT(socket
);
513 DBG("Sending kernel consumer destroy channel key %" PRIu64
, metadata
->key
);
515 memset(&msg
, 0, sizeof(msg
));
516 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
517 msg
.u
.destroy_channel
.key
= metadata
->key
;
519 pthread_mutex_lock(socket
->lock
);
520 health_code_update();
522 ret
= consumer_send_msg(socket
, &msg
);
528 health_code_update();
529 pthread_mutex_unlock(socket
->lock
);