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
));
57 /* Create directory */
58 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
, uid
, gid
);
61 ERR("Trace directory creation error");
65 DBG3("Kernel local consumer tracefile path: %s", pathname
);
67 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
69 PERROR("snprintf kernel metadata path");
72 pathname
= strndup(tmp_path
, sizeof(tmp_path
));
77 DBG3("Kernel network consumer subdir path: %s", pathname
);
88 * Sending a single channel to the consumer with command ADD_CHANNEL.
90 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
91 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
96 struct lttcomm_consumer_msg lkm
;
97 struct consumer_output
*consumer
;
102 assert(session
->consumer
);
104 consumer
= session
->consumer
;
106 DBG("Kernel consumer adding channel %s to kernel consumer",
107 channel
->channel
->name
);
110 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
113 pathname
= strdup("");
120 /* Prep channel message structure */
121 consumer_init_channel_comm_msg(&lkm
,
122 LTTNG_CONSUMER_ADD_CHANNEL
,
128 consumer
->net_seq_index
,
129 channel
->channel
->name
,
130 channel
->stream_count
,
131 channel
->channel
->attr
.output
,
132 CONSUMER_CHANNEL_TYPE_DATA
,
133 channel
->channel
->attr
.tracefile_size
,
134 channel
->channel
->attr
.tracefile_count
,
136 channel
->channel
->attr
.live_timer_interval
);
138 health_code_update();
140 ret
= consumer_send_channel(sock
, &lkm
);
145 health_code_update();
153 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
155 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
156 struct ltt_kernel_session
*session
, unsigned int monitor
)
160 struct lttcomm_consumer_msg lkm
;
161 struct consumer_output
*consumer
;
165 assert(session
->consumer
);
168 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
170 /* Get consumer output pointer */
171 consumer
= session
->consumer
;
174 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
177 pathname
= strdup("");
184 /* Prep channel message structure */
185 consumer_init_channel_comm_msg(&lkm
,
186 LTTNG_CONSUMER_ADD_CHANNEL
,
187 session
->metadata
->fd
,
192 consumer
->net_seq_index
,
193 DEFAULT_METADATA_NAME
,
195 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
196 CONSUMER_CHANNEL_TYPE_METADATA
,
200 health_code_update();
202 ret
= consumer_send_channel(sock
, &lkm
);
207 health_code_update();
209 /* Prep stream message structure */
210 consumer_init_stream_comm_msg(&lkm
,
211 LTTNG_CONSUMER_ADD_STREAM
,
212 session
->metadata
->fd
,
213 session
->metadata_stream_fd
,
214 0); /* CPU: 0 for metadata. */
216 health_code_update();
218 /* Send stream and file descriptor */
219 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
220 &session
->metadata_stream_fd
, 1);
225 health_code_update();
233 * Sending a single stream to the consumer with command ADD_STREAM.
235 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
236 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
237 struct ltt_kernel_session
*session
, unsigned int monitor
)
240 struct lttcomm_consumer_msg lkm
;
241 struct consumer_output
*consumer
;
246 assert(session
->consumer
);
249 DBG("Sending stream %d of channel %s to kernel consumer",
250 stream
->fd
, channel
->channel
->name
);
252 /* Get consumer output pointer */
253 consumer
= session
->consumer
;
255 /* Prep stream consumer message */
256 consumer_init_stream_comm_msg(&lkm
,
257 LTTNG_CONSUMER_ADD_STREAM
,
262 health_code_update();
264 /* Send stream and file descriptor */
265 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
270 health_code_update();
277 * Sending the notification that all streams were sent with STREAMS_SENT.
279 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
280 struct ltt_kernel_session
*session
, uint64_t channel_key
)
283 struct lttcomm_consumer_msg lkm
;
284 struct consumer_output
*consumer
;
289 DBG("Sending streams_sent");
290 /* Get consumer output pointer */
291 consumer
= session
->consumer
;
293 /* Prep stream consumer message */
294 consumer_init_streams_sent_comm_msg(&lkm
,
295 LTTNG_CONSUMER_STREAMS_SENT
,
296 channel_key
, consumer
->net_seq_index
);
298 health_code_update();
300 /* Send stream and file descriptor */
301 ret
= consumer_send_msg(sock
, &lkm
);
311 * Send all stream fds of kernel channel to the consumer.
313 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
314 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
315 unsigned int monitor
)
318 struct ltt_kernel_stream
*stream
;
323 assert(session
->consumer
);
326 /* Bail out if consumer is disabled */
327 if (!session
->consumer
->enabled
) {
332 DBG("Sending streams of channel %s to kernel consumer",
333 channel
->channel
->name
);
335 ret
= kernel_consumer_add_channel(sock
, channel
, session
, monitor
);
341 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
346 /* Add stream on the kernel consumer side. */
347 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
,
359 * Send all stream fds of the kernel session to the consumer.
361 int kernel_consumer_send_session(struct consumer_socket
*sock
,
362 struct ltt_kernel_session
*session
)
364 int ret
, monitor
= 0;
365 struct ltt_kernel_channel
*chan
;
369 assert(session
->consumer
);
372 /* Bail out if consumer is disabled */
373 if (!session
->consumer
->enabled
) {
378 /* Don't monitor the streams on the consumer if in flight recorder. */
379 if (session
->output_traces
) {
383 DBG("Sending session stream to kernel consumer");
385 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
386 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
392 /* Send channel and streams of it */
393 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
394 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
,
401 * Inform the relay that all the streams for the
404 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->fd
);
411 DBG("Kernel consumer FDs of metadata and channel streams sent");
413 session
->consumer_fds_sent
= 1;
420 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
421 struct ltt_kernel_channel
*channel
)
424 struct lttcomm_consumer_msg msg
;
429 DBG("Sending kernel consumer destroy channel key %d", channel
->fd
);
431 memset(&msg
, 0, sizeof(msg
));
432 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
433 msg
.u
.destroy_channel
.key
= channel
->fd
;
435 pthread_mutex_lock(socket
->lock
);
436 health_code_update();
438 ret
= consumer_send_msg(socket
, &msg
);
444 health_code_update();
445 pthread_mutex_unlock(socket
->lock
);
449 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
450 struct ltt_kernel_metadata
*metadata
)
453 struct lttcomm_consumer_msg msg
;
458 DBG("Sending kernel consumer destroy channel key %d", metadata
->fd
);
460 memset(&msg
, 0, sizeof(msg
));
461 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
462 msg
.u
.destroy_channel
.key
= metadata
->fd
;
464 pthread_mutex_lock(socket
->lock
);
465 health_code_update();
467 ret
= consumer_send_msg(socket
, &msg
);
473 health_code_update();
474 pthread_mutex_unlock(socket
->lock
);