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
;
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;
102 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 channel_attr_extended
->monitor_timer_interval
,
160 ksession
->current_trace_chunk
);
162 health_code_update();
164 ret
= consumer_send_channel(sock
, &lkm
);
169 health_code_update();
171 session
= session_find_by_id(ksession
->id
);
173 assert(pthread_mutex_trylock(&session
->lock
));
174 assert(session_trylock_list());
176 status
= notification_thread_command_add_channel(
177 notification_thread_handle
, session
->name
,
178 ksession
->uid
, ksession
->gid
,
179 channel
->channel
->name
, channel
->key
,
181 channel
->channel
->attr
.subbuf_size
* channel
->channel
->attr
.num_subbuf
);
183 if (status
!= LTTNG_OK
) {
188 channel
->published_to_notification_thread
= true;
192 session_put(session
);
199 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
201 * The consumer socket lock must be held by the caller.
203 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
204 struct ltt_kernel_session
*ksession
, unsigned int monitor
)
207 struct lttcomm_consumer_msg lkm
;
208 struct consumer_output
*consumer
;
214 assert(ksession
->consumer
);
217 DBG("Sending metadata %d to kernel consumer",
218 ksession
->metadata_stream_fd
);
220 /* Get consumer output pointer */
221 consumer
= ksession
->consumer
;
223 /* Prep channel message structure */
224 consumer_init_add_channel_comm_msg(&lkm
,
225 ksession
->metadata
->key
,
230 consumer
->net_seq_index
,
231 DEFAULT_METADATA_NAME
,
233 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
234 CONSUMER_CHANNEL_TYPE_METADATA
,
236 monitor
, 0, 0, ksession
->current_trace_chunk
);
238 health_code_update();
240 ret
= consumer_send_channel(sock
, &lkm
);
245 health_code_update();
247 /* Prep stream message structure */
248 consumer_init_add_stream_comm_msg(&lkm
,
249 ksession
->metadata
->key
,
250 ksession
->metadata_stream_fd
,
251 0 /* CPU: 0 for metadata. */);
253 health_code_update();
255 /* Send stream and file descriptor */
256 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
257 &ksession
->metadata_stream_fd
, 1);
262 health_code_update();
270 * Sending a single stream to the consumer with command ADD_STREAM.
273 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
274 struct ltt_kernel_channel
*channel
,
275 struct ltt_kernel_stream
*stream
,
276 struct ltt_kernel_session
*session
, unsigned int monitor
)
279 struct lttcomm_consumer_msg lkm
;
280 struct consumer_output
*consumer
;
285 assert(session
->consumer
);
288 DBG("Sending stream %d of channel %s to kernel consumer",
289 stream
->fd
, 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
,
300 health_code_update();
302 /* Send stream and file descriptor */
303 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
308 health_code_update();
315 * Sending the notification that all streams were sent with STREAMS_SENT.
317 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
318 struct ltt_kernel_session
*session
, uint64_t channel_key
)
321 struct lttcomm_consumer_msg lkm
;
322 struct consumer_output
*consumer
;
327 DBG("Sending streams_sent");
328 /* Get consumer output pointer */
329 consumer
= session
->consumer
;
331 /* Prep stream consumer message */
332 consumer_init_streams_sent_comm_msg(&lkm
,
333 LTTNG_CONSUMER_STREAMS_SENT
,
334 channel_key
, consumer
->net_seq_index
);
336 health_code_update();
338 /* Send stream and file descriptor */
339 ret
= consumer_send_msg(sock
, &lkm
);
349 * Send all stream fds of kernel channel to the consumer.
351 * The consumer socket lock must be held by the caller.
353 int kernel_consumer_send_channel_streams(struct consumer_socket
*sock
,
354 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*ksession
,
355 unsigned int monitor
)
358 struct ltt_kernel_stream
*stream
;
363 assert(ksession
->consumer
);
368 /* Bail out if consumer is disabled */
369 if (!ksession
->consumer
->enabled
) {
374 DBG("Sending streams of channel %s to kernel consumer",
375 channel
->channel
->name
);
377 if (!channel
->sent_to_consumer
) {
378 ret
= kernel_consumer_add_channel(sock
, channel
, ksession
, monitor
);
382 channel
->sent_to_consumer
= true;
386 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
387 if (!stream
->fd
|| stream
->sent_to_consumer
) {
391 /* Add stream on the kernel consumer side. */
392 ret
= kernel_consumer_add_stream(sock
, channel
, stream
,
397 stream
->sent_to_consumer
= true;
406 * Send all stream fds of the kernel session to the consumer.
408 * The consumer socket lock must be held by the caller.
410 int kernel_consumer_send_session(struct consumer_socket
*sock
,
411 struct ltt_kernel_session
*session
)
413 int ret
, monitor
= 0;
414 struct ltt_kernel_channel
*chan
;
418 assert(session
->consumer
);
421 /* Bail out if consumer is disabled */
422 if (!session
->consumer
->enabled
) {
427 /* Don't monitor the streams on the consumer if in flight recorder. */
428 if (session
->output_traces
) {
432 DBG("Sending session stream to kernel consumer");
434 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
435 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
441 /* Send channel and streams of it */
442 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
443 ret
= kernel_consumer_send_channel_streams(sock
, chan
, session
,
450 * Inform the relay that all the streams for the
453 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->key
);
460 DBG("Kernel consumer FDs of metadata and channel streams sent");
462 session
->consumer_fds_sent
= 1;
469 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
470 struct ltt_kernel_channel
*channel
)
473 struct lttcomm_consumer_msg msg
;
478 DBG("Sending kernel consumer destroy channel key %" PRIu64
, channel
->key
);
480 memset(&msg
, 0, sizeof(msg
));
481 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
482 msg
.u
.destroy_channel
.key
= channel
->key
;
484 pthread_mutex_lock(socket
->lock
);
485 health_code_update();
487 ret
= consumer_send_msg(socket
, &msg
);
493 health_code_update();
494 pthread_mutex_unlock(socket
->lock
);
498 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
499 struct ltt_kernel_metadata
*metadata
)
502 struct lttcomm_consumer_msg msg
;
507 DBG("Sending kernel consumer destroy channel key %" PRIu64
, metadata
->key
);
509 memset(&msg
, 0, sizeof(msg
));
510 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
511 msg
.u
.destroy_channel
.key
= metadata
->key
;
513 pthread_mutex_lock(socket
->lock
);
514 health_code_update();
516 ret
= consumer_send_msg(socket
, &msg
);
522 health_code_update();
523 pthread_mutex_unlock(socket
->lock
);