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
,
37 size_t *consumer_path_offset
)
40 char tmp_path
[PATH_MAX
];
41 char *pathname
= NULL
;
45 /* Get the right path name destination */
46 if (consumer
->type
== CONSUMER_DST_LOCAL
||
47 (consumer
->type
== CONSUMER_DST_NET
&&
48 consumer
->relay_major_version
== 2 &&
49 consumer
->relay_minor_version
>= 11)) {
50 pathname
= strdup(consumer
->domain_subdir
);
52 PERROR("Failed to copy domain subdirectory string %s",
53 consumer
->domain_subdir
);
56 *consumer_path_offset
= strlen(consumer
->domain_subdir
);
57 DBG3("Kernel local consumer trace path relative to current trace chunk: \"%s\"",
60 /* Network output, relayd < 2.11. */
61 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s%s",
62 consumer
->dst
.net
.base_dir
,
63 consumer
->domain_subdir
);
65 PERROR("snprintf kernel metadata path");
67 } else if (ret
>= sizeof(tmp_path
)) {
68 ERR("Kernel channel path exceeds the maximal allowed length of of %zu bytes (%i bytes required) with path \"%s%s\"",
69 sizeof(tmp_path
), ret
,
70 consumer
->dst
.net
.base_dir
,
71 consumer
->domain_subdir
);
74 pathname
= lttng_strndup(tmp_path
, sizeof(tmp_path
));
76 PERROR("lttng_strndup");
79 *consumer_path_offset
= 0;
80 DBG3("Kernel network consumer subdir path: %s", pathname
);
91 * Sending a single channel to the consumer with command ADD_CHANNEL.
94 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
95 struct ltt_kernel_channel
*channel
,
96 struct ltt_kernel_session
*ksession
,
100 char *pathname
= NULL
;
101 struct lttcomm_consumer_msg lkm
;
102 struct consumer_output
*consumer
;
103 enum lttng_error_code status
;
104 struct ltt_session
*session
= NULL
;
105 struct lttng_channel_extended
*channel_attr_extended
;
107 size_t consumer_path_offset
= 0;
112 assert(ksession
->consumer
);
114 consumer
= ksession
->consumer
;
115 channel_attr_extended
= (struct lttng_channel_extended
*)
116 channel
->channel
->attr
.extended
.ptr
;
118 DBG("Kernel consumer adding channel %s to kernel consumer",
119 channel
->channel
->name
);
120 is_local_trace
= consumer
->net_seq_index
== -1ULL;
122 pathname
= create_channel_path(consumer
, &consumer_path_offset
);
128 if (is_local_trace
&& ksession
->current_trace_chunk
) {
129 enum lttng_trace_chunk_status chunk_status
;
130 char *pathname_index
;
132 ret
= asprintf(&pathname_index
, "%s/" DEFAULT_INDEX_DIR
,
135 ERR("Failed to format channel index directory");
141 * Create the index subdirectory which will take care
142 * of implicitly creating the channel's path.
144 chunk_status
= lttng_trace_chunk_create_subdirectory(
145 ksession
->current_trace_chunk
, pathname_index
);
146 free(pathname_index
);
147 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
153 /* Prep channel message structure */
154 consumer_init_add_channel_comm_msg(&lkm
,
157 &pathname
[consumer_path_offset
],
160 consumer
->net_seq_index
,
161 channel
->channel
->name
,
162 channel
->stream_count
,
163 channel
->channel
->attr
.output
,
164 CONSUMER_CHANNEL_TYPE_DATA
,
165 channel
->channel
->attr
.tracefile_size
,
166 channel
->channel
->attr
.tracefile_count
,
168 channel
->channel
->attr
.live_timer_interval
,
169 channel_attr_extended
->monitor_timer_interval
,
170 ksession
->current_trace_chunk
);
172 health_code_update();
174 ret
= consumer_send_channel(sock
, &lkm
);
179 health_code_update();
181 session
= session_find_by_id(ksession
->id
);
183 assert(pthread_mutex_trylock(&session
->lock
));
184 assert(session_trylock_list());
186 status
= notification_thread_command_add_channel(
187 notification_thread_handle
, session
->name
,
188 ksession
->uid
, ksession
->gid
,
189 channel
->channel
->name
, channel
->key
,
191 channel
->channel
->attr
.subbuf_size
* channel
->channel
->attr
.num_subbuf
);
193 if (status
!= LTTNG_OK
) {
198 channel
->published_to_notification_thread
= true;
202 session_put(session
);
209 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
211 * The consumer socket lock must be held by the caller.
213 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
214 struct ltt_kernel_session
*ksession
, unsigned int monitor
)
217 struct lttcomm_consumer_msg lkm
;
218 struct consumer_output
*consumer
;
224 assert(ksession
->consumer
);
227 DBG("Sending metadata %d to kernel consumer",
228 ksession
->metadata_stream_fd
);
230 /* Get consumer output pointer */
231 consumer
= ksession
->consumer
;
233 /* Prep channel message structure */
234 consumer_init_add_channel_comm_msg(&lkm
,
235 ksession
->metadata
->key
,
240 consumer
->net_seq_index
,
241 DEFAULT_METADATA_NAME
,
243 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
244 CONSUMER_CHANNEL_TYPE_METADATA
,
246 monitor
, 0, 0, ksession
->current_trace_chunk
);
248 health_code_update();
250 ret
= consumer_send_channel(sock
, &lkm
);
255 health_code_update();
257 /* Prep stream message structure */
258 consumer_init_add_stream_comm_msg(&lkm
,
259 ksession
->metadata
->key
,
260 ksession
->metadata_stream_fd
,
261 0 /* CPU: 0 for metadata. */);
263 health_code_update();
265 /* Send stream and file descriptor */
266 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
267 &ksession
->metadata_stream_fd
, 1);
272 health_code_update();
280 * Sending a single stream to the consumer with command ADD_STREAM.
283 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
284 struct ltt_kernel_channel
*channel
,
285 struct ltt_kernel_stream
*stream
,
286 struct ltt_kernel_session
*session
, unsigned int monitor
)
289 struct lttcomm_consumer_msg lkm
;
290 struct consumer_output
*consumer
;
295 assert(session
->consumer
);
298 DBG("Sending stream %d of channel %s to kernel consumer",
299 stream
->fd
, channel
->channel
->name
);
301 /* Get consumer output pointer */
302 consumer
= session
->consumer
;
304 /* Prep stream consumer message */
305 consumer_init_add_stream_comm_msg(&lkm
,
310 health_code_update();
312 /* Send stream and file descriptor */
313 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
318 health_code_update();
325 * Sending the notification that all streams were sent with STREAMS_SENT.
327 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
328 struct ltt_kernel_session
*session
, uint64_t channel_key
)
331 struct lttcomm_consumer_msg lkm
;
332 struct consumer_output
*consumer
;
337 DBG("Sending streams_sent");
338 /* Get consumer output pointer */
339 consumer
= session
->consumer
;
341 /* Prep stream consumer message */
342 consumer_init_streams_sent_comm_msg(&lkm
,
343 LTTNG_CONSUMER_STREAMS_SENT
,
344 channel_key
, consumer
->net_seq_index
);
346 health_code_update();
348 /* Send stream and file descriptor */
349 ret
= consumer_send_msg(sock
, &lkm
);
359 * Send all stream fds of kernel channel to the consumer.
361 * The consumer socket lock must be held by the caller.
363 int kernel_consumer_send_channel_streams(struct consumer_socket
*sock
,
364 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*ksession
,
365 unsigned int monitor
)
368 struct ltt_kernel_stream
*stream
;
373 assert(ksession
->consumer
);
378 /* Bail out if consumer is disabled */
379 if (!ksession
->consumer
->enabled
) {
384 DBG("Sending streams of channel %s to kernel consumer",
385 channel
->channel
->name
);
387 if (!channel
->sent_to_consumer
) {
388 ret
= kernel_consumer_add_channel(sock
, channel
, ksession
, monitor
);
392 channel
->sent_to_consumer
= true;
396 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
397 if (!stream
->fd
|| stream
->sent_to_consumer
) {
401 /* Add stream on the kernel consumer side. */
402 ret
= kernel_consumer_add_stream(sock
, channel
, stream
,
407 stream
->sent_to_consumer
= true;
416 * Send all stream fds of the kernel session to the consumer.
418 * The consumer socket lock must be held by the caller.
420 int kernel_consumer_send_session(struct consumer_socket
*sock
,
421 struct ltt_kernel_session
*session
)
423 int ret
, monitor
= 0;
424 struct ltt_kernel_channel
*chan
;
428 assert(session
->consumer
);
431 /* Bail out if consumer is disabled */
432 if (!session
->consumer
->enabled
) {
437 /* Don't monitor the streams on the consumer if in flight recorder. */
438 if (session
->output_traces
) {
442 DBG("Sending session stream to kernel consumer");
444 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
445 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
451 /* Send channel and streams of it */
452 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
453 ret
= kernel_consumer_send_channel_streams(sock
, chan
, session
,
460 * Inform the relay that all the streams for the
463 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->key
);
470 DBG("Kernel consumer FDs of metadata and channel streams sent");
472 session
->consumer_fds_sent
= 1;
479 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
480 struct ltt_kernel_channel
*channel
)
483 struct lttcomm_consumer_msg msg
;
488 DBG("Sending kernel consumer destroy channel key %" PRIu64
, channel
->key
);
490 memset(&msg
, 0, sizeof(msg
));
491 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
492 msg
.u
.destroy_channel
.key
= channel
->key
;
494 pthread_mutex_lock(socket
->lock
);
495 health_code_update();
497 ret
= consumer_send_msg(socket
, &msg
);
503 health_code_update();
504 pthread_mutex_unlock(socket
->lock
);
508 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
509 struct ltt_kernel_metadata
*metadata
)
512 struct lttcomm_consumer_msg msg
;
517 DBG("Sending kernel consumer destroy channel key %" PRIu64
, metadata
->key
);
519 memset(&msg
, 0, sizeof(msg
));
520 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
521 msg
.u
.destroy_channel
.key
= metadata
->key
;
523 pthread_mutex_lock(socket
->lock
);
524 health_code_update();
526 ret
= consumer_send_msg(socket
, &msg
);
532 health_code_update();
533 pthread_mutex_unlock(socket
->lock
);