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
));
52 /* Create directory */
53 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
, uid
, gid
);
56 ERR("Trace directory creation error");
60 DBG3("Kernel local consumer tracefile path: %s", pathname
);
62 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
64 PERROR("snprintf kernel metadata path");
67 pathname
= strndup(tmp_path
, sizeof(tmp_path
));
68 DBG3("Kernel network consumer subdir path: %s", pathname
);
79 * Sending a single channel to the consumer with command ADD_CHANNEL.
81 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
82 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
87 struct lttcomm_consumer_msg lkm
;
88 struct consumer_output
*consumer
;
93 assert(session
->consumer
);
95 consumer
= session
->consumer
;
97 DBG("Kernel consumer adding channel %s to kernel consumer",
98 channel
->channel
->name
);
101 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
111 /* Prep channel message structure */
112 consumer_init_channel_comm_msg(&lkm
,
113 LTTNG_CONSUMER_ADD_CHANNEL
,
119 consumer
->net_seq_index
,
120 channel
->channel
->name
,
121 channel
->stream_count
,
122 channel
->channel
->attr
.output
,
123 CONSUMER_CHANNEL_TYPE_DATA
,
124 channel
->channel
->attr
.tracefile_size
,
125 channel
->channel
->attr
.tracefile_count
,
127 channel
->channel
->attr
.live_timer_interval
);
129 health_code_update();
131 ret
= consumer_send_channel(sock
, &lkm
);
136 health_code_update();
143 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
145 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
146 struct ltt_kernel_session
*session
, unsigned int monitor
)
150 struct lttcomm_consumer_msg lkm
;
151 struct consumer_output
*consumer
;
155 assert(session
->consumer
);
158 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
160 /* Get consumer output pointer */
161 consumer
= session
->consumer
;
164 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
174 /* Prep channel message structure */
175 consumer_init_channel_comm_msg(&lkm
,
176 LTTNG_CONSUMER_ADD_CHANNEL
,
177 session
->metadata
->fd
,
182 consumer
->net_seq_index
,
183 DEFAULT_METADATA_NAME
,
185 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
186 CONSUMER_CHANNEL_TYPE_METADATA
,
190 health_code_update();
192 ret
= consumer_send_channel(sock
, &lkm
);
197 health_code_update();
199 /* Prep stream message structure */
200 consumer_init_stream_comm_msg(&lkm
,
201 LTTNG_CONSUMER_ADD_STREAM
,
202 session
->metadata
->fd
,
203 session
->metadata_stream_fd
,
204 0); /* CPU: 0 for metadata. */
206 health_code_update();
208 /* Send stream and file descriptor */
209 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
210 &session
->metadata_stream_fd
, 1);
215 health_code_update();
222 * Sending a single stream to the consumer with command ADD_STREAM.
224 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
225 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
226 struct ltt_kernel_session
*session
, unsigned int monitor
)
229 struct lttcomm_consumer_msg lkm
;
230 struct consumer_output
*consumer
;
235 assert(session
->consumer
);
238 DBG("Sending stream %d of channel %s to kernel consumer",
239 stream
->fd
, channel
->channel
->name
);
241 /* Get consumer output pointer */
242 consumer
= session
->consumer
;
244 /* Prep stream consumer message */
245 consumer_init_stream_comm_msg(&lkm
,
246 LTTNG_CONSUMER_ADD_STREAM
,
251 health_code_update();
253 /* Send stream and file descriptor */
254 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
259 health_code_update();
266 * Sending the notification that all streams were sent with STREAMS_SENT.
268 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
269 struct ltt_kernel_session
*session
, uint64_t channel_key
)
272 struct lttcomm_consumer_msg lkm
;
273 struct consumer_output
*consumer
;
278 DBG("Sending streams_sent");
279 /* Get consumer output pointer */
280 consumer
= session
->consumer
;
282 /* Prep stream consumer message */
283 consumer_init_streams_sent_comm_msg(&lkm
,
284 LTTNG_CONSUMER_STREAMS_SENT
,
285 channel_key
, consumer
->net_seq_index
);
287 health_code_update();
289 /* Send stream and file descriptor */
290 ret
= consumer_send_msg(sock
, &lkm
);
300 * Send all stream fds of kernel channel to the consumer.
302 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
303 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
304 unsigned int monitor
)
307 struct ltt_kernel_stream
*stream
;
312 assert(session
->consumer
);
315 /* Bail out if consumer is disabled */
316 if (!session
->consumer
->enabled
) {
321 DBG("Sending streams of channel %s to kernel consumer",
322 channel
->channel
->name
);
324 ret
= kernel_consumer_add_channel(sock
, channel
, session
, monitor
);
330 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
335 /* Add stream on the kernel consumer side. */
336 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
,
348 * Send all stream fds of the kernel session to the consumer.
350 int kernel_consumer_send_session(struct consumer_socket
*sock
,
351 struct ltt_kernel_session
*session
)
353 int ret
, monitor
= 0;
354 struct ltt_kernel_channel
*chan
;
358 assert(session
->consumer
);
361 /* Bail out if consumer is disabled */
362 if (!session
->consumer
->enabled
) {
367 /* Don't monitor the streams on the consumer if in flight recorder. */
368 if (session
->output_traces
) {
372 DBG("Sending session stream to kernel consumer");
374 if (session
->metadata_stream_fd
>= 0) {
375 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
380 /* Flag that at least the metadata has been sent to the consumer. */
381 session
->consumer_fds_sent
= 1;
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");
411 int kernel_consumer_destroy_channel(struct consumer_socket
*socket
,
412 struct ltt_kernel_channel
*channel
)
415 struct lttcomm_consumer_msg msg
;
420 DBG("Sending kernel consumer destroy channel key %d", channel
->fd
);
422 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
423 msg
.u
.destroy_channel
.key
= channel
->fd
;
425 pthread_mutex_lock(socket
->lock
);
426 health_code_update();
428 ret
= consumer_send_msg(socket
, &msg
);
434 health_code_update();
435 pthread_mutex_unlock(socket
->lock
);
439 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
440 struct ltt_kernel_metadata
*metadata
)
443 struct lttcomm_consumer_msg msg
;
448 DBG("Sending kernel consumer destroy channel key %d", metadata
->fd
);
450 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
451 msg
.u
.destroy_channel
.key
= metadata
->fd
;
453 pthread_mutex_lock(socket
->lock
);
454 health_code_update();
456 ret
= consumer_send_msg(socket
, &msg
);
462 health_code_update();
463 pthread_mutex_unlock(socket
->lock
);