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.
26 #include <common/common.h>
27 #include <common/defaults.h>
30 #include "health-sessiond.h"
31 #include "kernel-consumer.h"
33 static char *create_channel_path(struct consumer_output
*consumer
,
37 char tmp_path
[PATH_MAX
];
38 char *pathname
= NULL
;
42 /* Get the right path name destination */
43 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
44 /* Set application path to the destination path */
45 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s%s",
46 consumer
->dst
.trace_path
, consumer
->subdir
);
48 PERROR("snprintf kernel channel path");
51 pathname
= strndup(tmp_path
, sizeof(tmp_path
));
53 /* Create directory */
54 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
, uid
, gid
);
57 ERR("Trace directory creation error");
61 DBG3("Kernel local consumer tracefile path: %s", pathname
);
63 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
65 PERROR("snprintf kernel metadata path");
68 pathname
= strndup(tmp_path
, sizeof(tmp_path
));
69 DBG3("Kernel network consumer subdir path: %s", pathname
);
80 * Sending a single channel to the consumer with command ADD_CHANNEL.
82 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
83 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
88 struct lttcomm_consumer_msg lkm
;
89 struct consumer_output
*consumer
;
94 assert(session
->consumer
);
96 consumer
= session
->consumer
;
98 DBG("Kernel consumer adding channel %s to kernel consumer",
99 channel
->channel
->name
);
102 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
109 pathname
= strdup("");
112 /* Prep channel message structure */
113 consumer_init_channel_comm_msg(&lkm
,
114 LTTNG_CONSUMER_ADD_CHANNEL
,
120 consumer
->net_seq_index
,
121 channel
->channel
->name
,
122 channel
->stream_count
,
123 channel
->channel
->attr
.output
,
124 CONSUMER_CHANNEL_TYPE_DATA
,
125 channel
->channel
->attr
.tracefile_size
,
126 channel
->channel
->attr
.tracefile_count
,
128 channel
->channel
->attr
.live_timer_interval
);
130 health_code_update();
132 ret
= consumer_send_channel(sock
, &lkm
);
137 health_code_update();
145 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
147 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
148 struct ltt_kernel_session
*session
, unsigned int monitor
)
152 struct lttcomm_consumer_msg lkm
;
153 struct consumer_output
*consumer
;
157 assert(session
->consumer
);
160 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
162 /* Get consumer output pointer */
163 consumer
= session
->consumer
;
166 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
173 pathname
= strdup("");
176 /* Prep channel message structure */
177 consumer_init_channel_comm_msg(&lkm
,
178 LTTNG_CONSUMER_ADD_CHANNEL
,
179 session
->metadata
->fd
,
184 consumer
->net_seq_index
,
185 DEFAULT_METADATA_NAME
,
187 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
188 CONSUMER_CHANNEL_TYPE_METADATA
,
192 health_code_update();
194 ret
= consumer_send_channel(sock
, &lkm
);
199 health_code_update();
201 /* Prep stream message structure */
202 consumer_init_stream_comm_msg(&lkm
,
203 LTTNG_CONSUMER_ADD_STREAM
,
204 session
->metadata
->fd
,
205 session
->metadata_stream_fd
,
206 0); /* CPU: 0 for metadata. */
208 health_code_update();
210 /* Send stream and file descriptor */
211 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
212 &session
->metadata_stream_fd
, 1);
217 health_code_update();
225 * Sending a single stream to the consumer with command ADD_STREAM.
227 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
228 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
229 struct ltt_kernel_session
*session
, unsigned int monitor
)
232 struct lttcomm_consumer_msg lkm
;
233 struct consumer_output
*consumer
;
238 assert(session
->consumer
);
241 DBG("Sending stream %d of channel %s to kernel consumer",
242 stream
->fd
, channel
->channel
->name
);
244 /* Get consumer output pointer */
245 consumer
= session
->consumer
;
247 /* Prep stream consumer message */
248 consumer_init_stream_comm_msg(&lkm
,
249 LTTNG_CONSUMER_ADD_STREAM
,
254 health_code_update();
256 /* Send stream and file descriptor */
257 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
262 health_code_update();
269 * Sending the notification that all streams were sent with STREAMS_SENT.
271 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
272 struct ltt_kernel_session
*session
, uint64_t channel_key
)
275 struct lttcomm_consumer_msg lkm
;
276 struct consumer_output
*consumer
;
281 DBG("Sending streams_sent");
282 /* Get consumer output pointer */
283 consumer
= session
->consumer
;
285 /* Prep stream consumer message */
286 consumer_init_streams_sent_comm_msg(&lkm
,
287 LTTNG_CONSUMER_STREAMS_SENT
,
288 channel_key
, consumer
->net_seq_index
);
290 health_code_update();
292 /* Send stream and file descriptor */
293 ret
= consumer_send_msg(sock
, &lkm
);
303 * Send all stream fds of kernel channel to the consumer.
305 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
306 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
307 unsigned int monitor
)
310 struct ltt_kernel_stream
*stream
;
315 assert(session
->consumer
);
318 /* Bail out if consumer is disabled */
319 if (!session
->consumer
->enabled
) {
324 DBG("Sending streams of channel %s to kernel consumer",
325 channel
->channel
->name
);
327 ret
= kernel_consumer_add_channel(sock
, channel
, session
, monitor
);
333 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
338 /* Add stream on the kernel consumer side. */
339 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
,
351 * Send all stream fds of the kernel session to the consumer.
353 int kernel_consumer_send_session(struct consumer_socket
*sock
,
354 struct ltt_kernel_session
*session
)
356 int ret
, monitor
= 0;
357 struct ltt_kernel_channel
*chan
;
361 assert(session
->consumer
);
364 /* Bail out if consumer is disabled */
365 if (!session
->consumer
->enabled
) {
370 /* Don't monitor the streams on the consumer if in flight recorder. */
371 if (session
->output_traces
) {
375 DBG("Sending session stream to kernel consumer");
377 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
378 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
384 /* Send channel and streams of it */
385 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
386 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
,
393 * Inform the relay that all the streams for the
396 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->fd
);
403 DBG("Kernel consumer FDs of metadata and channel streams sent");
405 session
->consumer_fds_sent
= 1;
412 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
413 struct ltt_kernel_channel
*channel
)
416 struct lttcomm_consumer_msg msg
;
421 DBG("Sending kernel consumer destroy channel key %d", channel
->fd
);
423 memset(&msg
, 0, sizeof(msg
));
424 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
425 msg
.u
.destroy_channel
.key
= channel
->fd
;
427 pthread_mutex_lock(socket
->lock
);
428 health_code_update();
430 ret
= consumer_send_msg(socket
, &msg
);
436 health_code_update();
437 pthread_mutex_unlock(socket
->lock
);
441 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
442 struct ltt_kernel_metadata
*metadata
)
445 struct lttcomm_consumer_msg msg
;
450 DBG("Sending kernel consumer destroy channel key %d", metadata
->fd
);
452 memset(&msg
, 0, sizeof(msg
));
453 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
454 msg
.u
.destroy_channel
.key
= metadata
->fd
;
456 pthread_mutex_lock(socket
->lock
);
457 health_code_update();
459 ret
= consumer_send_msg(socket
, &msg
);
465 health_code_update();
466 pthread_mutex_unlock(socket
->lock
);