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>
29 #include "health-sessiond.h"
30 #include "kernel-consumer.h"
32 static char *create_channel_path(struct consumer_output
*consumer
,
36 char tmp_path
[PATH_MAX
];
37 char *pathname
= NULL
;
41 /* Get the right path name destination */
42 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
43 /* Set application path to the destination path */
44 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s%s",
45 consumer
->dst
.trace_path
, consumer
->subdir
);
47 PERROR("snprintf kernel channel path");
50 pathname
= strndup(tmp_path
, sizeof(tmp_path
));
56 /* Create directory */
57 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
, uid
, gid
);
60 ERR("Trace directory creation error");
64 DBG3("Kernel local consumer tracefile path: %s", pathname
);
66 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
68 PERROR("snprintf kernel metadata path");
71 pathname
= strndup(tmp_path
, sizeof(tmp_path
));
76 DBG3("Kernel network consumer subdir path: %s", pathname
);
87 * Sending a single channel to the consumer with command ADD_CHANNEL.
89 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
90 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
95 struct lttcomm_consumer_msg lkm
;
96 struct consumer_output
*consumer
;
101 assert(session
->consumer
);
103 consumer
= session
->consumer
;
105 DBG("Kernel consumer adding channel %s to kernel consumer",
106 channel
->channel
->name
);
109 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
112 pathname
= strdup("");
119 /* Prep channel message structure */
120 consumer_init_channel_comm_msg(&lkm
,
121 LTTNG_CONSUMER_ADD_CHANNEL
,
127 consumer
->net_seq_index
,
128 channel
->channel
->name
,
129 channel
->stream_count
,
130 channel
->channel
->attr
.output
,
131 CONSUMER_CHANNEL_TYPE_DATA
,
132 channel
->channel
->attr
.tracefile_size
,
133 channel
->channel
->attr
.tracefile_count
,
135 channel
->channel
->attr
.live_timer_interval
);
137 health_code_update();
139 ret
= consumer_send_channel(sock
, &lkm
);
144 health_code_update();
152 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
154 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
155 struct ltt_kernel_session
*session
, unsigned int monitor
)
159 struct lttcomm_consumer_msg lkm
;
160 struct consumer_output
*consumer
;
164 assert(session
->consumer
);
167 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
169 /* Get consumer output pointer */
170 consumer
= session
->consumer
;
173 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
176 pathname
= strdup("");
183 /* Prep channel message structure */
184 consumer_init_channel_comm_msg(&lkm
,
185 LTTNG_CONSUMER_ADD_CHANNEL
,
186 session
->metadata
->fd
,
191 consumer
->net_seq_index
,
192 DEFAULT_METADATA_NAME
,
194 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
195 CONSUMER_CHANNEL_TYPE_METADATA
,
199 health_code_update();
201 ret
= consumer_send_channel(sock
, &lkm
);
206 health_code_update();
208 /* Prep stream message structure */
209 consumer_init_stream_comm_msg(&lkm
,
210 LTTNG_CONSUMER_ADD_STREAM
,
211 session
->metadata
->fd
,
212 session
->metadata_stream_fd
,
213 0); /* CPU: 0 for metadata. */
215 health_code_update();
217 /* Send stream and file descriptor */
218 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
219 &session
->metadata_stream_fd
, 1);
224 health_code_update();
232 * Sending a single stream to the consumer with command ADD_STREAM.
234 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
235 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
236 struct ltt_kernel_session
*session
, unsigned int monitor
)
239 struct lttcomm_consumer_msg lkm
;
240 struct consumer_output
*consumer
;
245 assert(session
->consumer
);
248 DBG("Sending stream %d of channel %s to kernel consumer",
249 stream
->fd
, channel
->channel
->name
);
251 /* Get consumer output pointer */
252 consumer
= session
->consumer
;
254 /* Prep stream consumer message */
255 consumer_init_stream_comm_msg(&lkm
,
256 LTTNG_CONSUMER_ADD_STREAM
,
261 health_code_update();
263 /* Send stream and file descriptor */
264 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
269 health_code_update();
276 * Sending the notification that all streams were sent with STREAMS_SENT.
278 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
279 struct ltt_kernel_session
*session
, uint64_t channel_key
)
282 struct lttcomm_consumer_msg lkm
;
283 struct consumer_output
*consumer
;
288 DBG("Sending streams_sent");
289 /* Get consumer output pointer */
290 consumer
= session
->consumer
;
292 /* Prep stream consumer message */
293 consumer_init_streams_sent_comm_msg(&lkm
,
294 LTTNG_CONSUMER_STREAMS_SENT
,
295 channel_key
, consumer
->net_seq_index
);
297 health_code_update();
299 /* Send stream and file descriptor */
300 ret
= consumer_send_msg(sock
, &lkm
);
310 * Send all stream fds of kernel channel to the consumer.
312 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
313 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
314 unsigned int monitor
)
317 struct ltt_kernel_stream
*stream
;
322 assert(session
->consumer
);
325 /* Bail out if consumer is disabled */
326 if (!session
->consumer
->enabled
) {
331 DBG("Sending streams of channel %s to kernel consumer",
332 channel
->channel
->name
);
334 ret
= kernel_consumer_add_channel(sock
, channel
, session
, monitor
);
340 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
345 /* Add stream on the kernel consumer side. */
346 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
,
358 * Send all stream fds of the kernel session to the consumer.
360 int kernel_consumer_send_session(struct consumer_socket
*sock
,
361 struct ltt_kernel_session
*session
)
363 int ret
, monitor
= 0;
364 struct ltt_kernel_channel
*chan
;
368 assert(session
->consumer
);
371 /* Bail out if consumer is disabled */
372 if (!session
->consumer
->enabled
) {
377 /* Don't monitor the streams on the consumer if in flight recorder. */
378 if (session
->output_traces
) {
382 DBG("Sending session stream to kernel consumer");
384 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
385 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
391 /* Send channel and streams of it */
392 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
393 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
,
400 * Inform the relay that all the streams for the
403 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->fd
);
410 DBG("Kernel consumer FDs of metadata and channel streams sent");
412 session
->consumer_fds_sent
= 1;
419 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
420 struct ltt_kernel_channel
*channel
)
423 struct lttcomm_consumer_msg msg
;
428 DBG("Sending kernel consumer destroy channel key %d", channel
->fd
);
430 memset(&msg
, 0, sizeof(msg
));
431 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
432 msg
.u
.destroy_channel
.key
= channel
->fd
;
434 pthread_mutex_lock(socket
->lock
);
435 health_code_update();
437 ret
= consumer_send_msg(socket
, &msg
);
443 health_code_update();
444 pthread_mutex_unlock(socket
->lock
);
448 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
449 struct ltt_kernel_metadata
*metadata
)
452 struct lttcomm_consumer_msg msg
;
457 DBG("Sending kernel consumer destroy channel key %d", metadata
->fd
);
459 memset(&msg
, 0, sizeof(msg
));
460 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
461 msg
.u
.destroy_channel
.key
= metadata
->fd
;
463 pthread_mutex_lock(socket
->lock
);
464 health_code_update();
466 ret
= consumer_send_msg(socket
, &msg
);
472 health_code_update();
473 pthread_mutex_unlock(socket
->lock
);