2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <common/common.h>
26 #include <common/defaults.h>
27 #include <common/compat/string.h>
30 #include "health-sessiond.h"
31 #include "kernel-consumer.h"
32 #include "notification-thread-commands.h"
34 #include "lttng-sessiond.h"
36 static char *create_channel_path(struct consumer_output
*consumer
)
39 char tmp_path
[PATH_MAX
];
40 char *pathname
= NULL
;
44 /* Get the right path name destination */
45 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
46 pathname
= strdup(consumer
->domain_subdir
);
48 PERROR("Failed to copy domain subdirectory string %s",
49 consumer
->domain_subdir
);
52 DBG3("Kernel local consumer trace path relative to current trace chunk: \"%s\"",
56 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s%s",
57 consumer
->dst
.net
.base_dir
,
58 consumer
->domain_subdir
);
60 PERROR("snprintf kernel metadata path");
62 } else if (ret
>= sizeof(tmp_path
)) {
63 ERR("Kernel channel path exceeds the maximal allowed length of of %zu bytes (%i bytes required) with path \"%s%s\"",
64 sizeof(tmp_path
), ret
,
65 consumer
->dst
.net
.base_dir
,
66 consumer
->domain_subdir
);
69 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
71 PERROR("lttng_strndup");
74 DBG3("Kernel network consumer subdir path: %s", pathname
);
85 * Sending a single channel to the consumer with command ADD_CHANNEL.
88 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
89 struct ltt_kernel_channel
*channel
,
90 struct ltt_kernel_session
*ksession
,
94 char *pathname
= NULL
;
95 struct lttcomm_consumer_msg lkm
;
96 struct consumer_output
*consumer
;
97 enum lttng_error_code status
;
98 struct ltt_session
*session
= NULL
;
99 struct lttng_channel_extended
*channel_attr_extended
;
105 assert(ksession
->consumer
);
107 consumer
= ksession
->consumer
;
108 channel_attr_extended
= (struct lttng_channel_extended
*)
109 channel
->channel
->attr
.extended
.ptr
;
111 DBG("Kernel consumer adding channel %s to kernel consumer",
112 channel
->channel
->name
);
113 is_local_trace
= consumer
->net_seq_index
== -1ULL;
115 pathname
= create_channel_path(consumer
);
121 if (is_local_trace
&& ksession
->current_trace_chunk
) {
122 enum lttng_trace_chunk_status chunk_status
;
123 char *pathname_index
;
125 ret
= asprintf(&pathname_index
, "%s/" DEFAULT_INDEX_DIR
,
128 ERR("Failed to format channel index directory");
134 * Create the index subdirectory which will take care
135 * of implicitly creating the channel's path.
137 chunk_status
= lttng_trace_chunk_create_subdirectory(
138 ksession
->current_trace_chunk
, pathname_index
);
139 free(pathname_index
);
140 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
146 /* Prep channel message structure */
147 consumer_init_add_channel_comm_msg(&lkm
,
153 consumer
->net_seq_index
,
154 channel
->channel
->name
,
155 channel
->stream_count
,
156 channel
->channel
->attr
.output
,
157 CONSUMER_CHANNEL_TYPE_DATA
,
158 channel
->channel
->attr
.tracefile_size
,
159 channel
->channel
->attr
.tracefile_count
,
161 channel
->channel
->attr
.live_timer_interval
,
162 channel_attr_extended
->monitor_timer_interval
,
163 ksession
->current_trace_chunk
);
165 health_code_update();
167 ret
= consumer_send_channel(sock
, &lkm
);
172 health_code_update();
174 session
= session_find_by_id(ksession
->id
);
176 assert(pthread_mutex_trylock(&session
->lock
));
177 assert(session_trylock_list());
179 status
= notification_thread_command_add_channel(
180 notification_thread_handle
, session
->name
,
181 ksession
->uid
, ksession
->gid
,
182 channel
->channel
->name
, channel
->key
,
184 channel
->channel
->attr
.subbuf_size
* channel
->channel
->attr
.num_subbuf
);
186 if (status
!= LTTNG_OK
) {
191 channel
->published_to_notification_thread
= true;
195 session_put(session
);
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
, unsigned int monitor
)
210 struct lttcomm_consumer_msg lkm
;
211 struct consumer_output
*consumer
;
217 assert(ksession
->consumer
);
220 DBG("Sending metadata %d to kernel consumer",
221 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
,
230 DEFAULT_KERNEL_TRACE_DIR
,
233 consumer
->net_seq_index
,
234 DEFAULT_METADATA_NAME
,
236 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
237 CONSUMER_CHANNEL_TYPE_METADATA
,
239 monitor
, 0, 0, ksession
->current_trace_chunk
);
241 health_code_update();
243 ret
= consumer_send_channel(sock
, &lkm
);
248 health_code_update();
250 /* Prep stream message structure */
251 consumer_init_add_stream_comm_msg(&lkm
,
252 ksession
->metadata
->key
,
253 ksession
->metadata_stream_fd
,
254 0 /* CPU: 0 for metadata. */);
256 health_code_update();
258 /* Send stream and file descriptor */
259 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
260 &ksession
->metadata_stream_fd
, 1);
265 health_code_update();
273 * Sending a single stream to the consumer with command ADD_STREAM.
276 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
, unsigned int monitor
)
282 struct lttcomm_consumer_msg lkm
;
283 struct consumer_output
*consumer
;
288 assert(session
->consumer
);
291 DBG("Sending stream %d of channel %s to kernel consumer",
292 stream
->fd
, channel
->channel
->name
);
294 /* Get consumer output pointer */
295 consumer
= session
->consumer
;
297 /* Prep stream consumer message */
298 consumer_init_add_stream_comm_msg(&lkm
,
303 health_code_update();
305 /* Send stream and file descriptor */
306 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
311 health_code_update();
318 * Sending the notification that all streams were sent with STREAMS_SENT.
320 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
321 struct ltt_kernel_session
*session
, uint64_t channel_key
)
324 struct lttcomm_consumer_msg lkm
;
325 struct consumer_output
*consumer
;
330 DBG("Sending streams_sent");
331 /* Get consumer output pointer */
332 consumer
= session
->consumer
;
334 /* Prep stream consumer message */
335 consumer_init_streams_sent_comm_msg(&lkm
,
336 LTTNG_CONSUMER_STREAMS_SENT
,
337 channel_key
, consumer
->net_seq_index
);
339 health_code_update();
341 /* Send stream and file descriptor */
342 ret
= consumer_send_msg(sock
, &lkm
);
352 * Send all stream fds of kernel channel to the consumer.
354 * The consumer socket lock must be held by the caller.
356 int kernel_consumer_send_channel_streams(struct consumer_socket
*sock
,
357 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*ksession
,
358 unsigned int monitor
)
361 struct ltt_kernel_stream
*stream
;
366 assert(ksession
->consumer
);
371 /* Bail out if consumer is disabled */
372 if (!ksession
->consumer
->enabled
) {
377 DBG("Sending streams of channel %s to kernel consumer",
378 channel
->channel
->name
);
380 if (!channel
->sent_to_consumer
) {
381 ret
= kernel_consumer_add_channel(sock
, channel
, ksession
, monitor
);
385 channel
->sent_to_consumer
= true;
389 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
390 if (!stream
->fd
|| stream
->sent_to_consumer
) {
394 /* Add stream on the kernel consumer side. */
395 ret
= kernel_consumer_add_stream(sock
, channel
, stream
,
400 stream
->sent_to_consumer
= true;
409 * Send all stream fds of the kernel session to the consumer.
411 * The consumer socket lock must be held by the caller.
413 int kernel_consumer_send_session(struct consumer_socket
*sock
,
414 struct ltt_kernel_session
*session
)
416 int ret
, monitor
= 0;
417 struct ltt_kernel_channel
*chan
;
421 assert(session
->consumer
);
424 /* Bail out if consumer is disabled */
425 if (!session
->consumer
->enabled
) {
430 /* Don't monitor the streams on the consumer if in flight recorder. */
431 if (session
->output_traces
) {
435 DBG("Sending session stream to kernel consumer");
437 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
438 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
444 /* Send channel and streams of it */
445 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
446 ret
= kernel_consumer_send_channel_streams(sock
, chan
, session
,
453 * Inform the relay that all the streams for the
456 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->key
);
463 DBG("Kernel consumer FDs of metadata and channel streams sent");
465 session
->consumer_fds_sent
= 1;
472 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
473 struct ltt_kernel_channel
*channel
)
476 struct lttcomm_consumer_msg msg
;
481 DBG("Sending kernel consumer destroy channel key %" PRIu64
, channel
->key
);
483 memset(&msg
, 0, sizeof(msg
));
484 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
485 msg
.u
.destroy_channel
.key
= channel
->key
;
487 pthread_mutex_lock(socket
->lock
);
488 health_code_update();
490 ret
= consumer_send_msg(socket
, &msg
);
496 health_code_update();
497 pthread_mutex_unlock(socket
->lock
);
501 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
502 struct ltt_kernel_metadata
*metadata
)
505 struct lttcomm_consumer_msg msg
;
510 DBG("Sending kernel consumer destroy channel key %" PRIu64
, metadata
->key
);
512 memset(&msg
, 0, sizeof(msg
));
513 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
514 msg
.u
.destroy_channel
.key
= metadata
->key
;
516 pthread_mutex_lock(socket
->lock
);
517 health_code_update();
519 ret
= consumer_send_msg(socket
, &msg
);
525 health_code_update();
526 pthread_mutex_unlock(socket
->lock
);