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
);
108 pathname
= strdup("");
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();
144 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
146 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
147 struct ltt_kernel_session
*session
, unsigned int monitor
)
151 struct lttcomm_consumer_msg lkm
;
152 struct consumer_output
*consumer
;
156 assert(session
->consumer
);
159 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
161 /* Get consumer output pointer */
162 consumer
= session
->consumer
;
165 pathname
= create_channel_path(consumer
, session
->uid
, session
->gid
);
172 pathname
= strdup("");
175 /* Prep channel message structure */
176 consumer_init_channel_comm_msg(&lkm
,
177 LTTNG_CONSUMER_ADD_CHANNEL
,
178 session
->metadata
->fd
,
183 consumer
->net_seq_index
,
184 DEFAULT_METADATA_NAME
,
186 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
187 CONSUMER_CHANNEL_TYPE_METADATA
,
191 health_code_update();
193 ret
= consumer_send_channel(sock
, &lkm
);
198 health_code_update();
200 /* Prep stream message structure */
201 consumer_init_stream_comm_msg(&lkm
,
202 LTTNG_CONSUMER_ADD_STREAM
,
203 session
->metadata
->fd
,
204 session
->metadata_stream_fd
,
205 0); /* CPU: 0 for metadata. */
207 health_code_update();
209 /* Send stream and file descriptor */
210 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
211 &session
->metadata_stream_fd
, 1);
216 health_code_update();
224 * Sending a single stream to the consumer with command ADD_STREAM.
226 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
227 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
228 struct ltt_kernel_session
*session
, unsigned int monitor
)
231 struct lttcomm_consumer_msg lkm
;
232 struct consumer_output
*consumer
;
237 assert(session
->consumer
);
240 DBG("Sending stream %d of channel %s to kernel consumer",
241 stream
->fd
, channel
->channel
->name
);
243 /* Get consumer output pointer */
244 consumer
= session
->consumer
;
246 /* Prep stream consumer message */
247 consumer_init_stream_comm_msg(&lkm
,
248 LTTNG_CONSUMER_ADD_STREAM
,
253 health_code_update();
255 /* Send stream and file descriptor */
256 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
261 health_code_update();
268 * Sending the notification that all streams were sent with STREAMS_SENT.
270 int kernel_consumer_streams_sent(struct consumer_socket
*sock
,
271 struct ltt_kernel_session
*session
, uint64_t channel_key
)
274 struct lttcomm_consumer_msg lkm
;
275 struct consumer_output
*consumer
;
280 DBG("Sending streams_sent");
281 /* Get consumer output pointer */
282 consumer
= session
->consumer
;
284 /* Prep stream consumer message */
285 consumer_init_streams_sent_comm_msg(&lkm
,
286 LTTNG_CONSUMER_STREAMS_SENT
,
287 channel_key
, consumer
->net_seq_index
);
289 health_code_update();
291 /* Send stream and file descriptor */
292 ret
= consumer_send_msg(sock
, &lkm
);
302 * Send all stream fds of kernel channel to the consumer.
304 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
305 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
,
306 unsigned int monitor
)
309 struct ltt_kernel_stream
*stream
;
314 assert(session
->consumer
);
317 /* Bail out if consumer is disabled */
318 if (!session
->consumer
->enabled
) {
323 DBG("Sending streams of channel %s to kernel consumer",
324 channel
->channel
->name
);
326 ret
= kernel_consumer_add_channel(sock
, channel
, session
, monitor
);
332 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
337 /* Add stream on the kernel consumer side. */
338 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
,
350 * Send all stream fds of the kernel session to the consumer.
352 int kernel_consumer_send_session(struct consumer_socket
*sock
,
353 struct ltt_kernel_session
*session
)
355 int ret
, monitor
= 0;
356 struct ltt_kernel_channel
*chan
;
360 assert(session
->consumer
);
363 /* Bail out if consumer is disabled */
364 if (!session
->consumer
->enabled
) {
369 /* Don't monitor the streams on the consumer if in flight recorder. */
370 if (session
->output_traces
) {
374 DBG("Sending session stream to kernel consumer");
376 if (session
->metadata_stream_fd
>= 0 && session
->metadata
) {
377 ret
= kernel_consumer_add_metadata(sock
, session
, monitor
);
383 /* Send channel and streams of it */
384 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
385 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
,
392 * Inform the relay that all the streams for the
395 ret
= kernel_consumer_streams_sent(sock
, session
, chan
->fd
);
402 DBG("Kernel consumer FDs of metadata and channel streams sent");
404 session
->consumer_fds_sent
= 1;
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 memset(&msg
, 0, sizeof(msg
));
423 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
424 msg
.u
.destroy_channel
.key
= channel
->fd
;
426 pthread_mutex_lock(socket
->lock
);
427 health_code_update();
429 ret
= consumer_send_msg(socket
, &msg
);
435 health_code_update();
436 pthread_mutex_unlock(socket
->lock
);
440 int kernel_consumer_destroy_metadata(struct consumer_socket
*socket
,
441 struct ltt_kernel_metadata
*metadata
)
444 struct lttcomm_consumer_msg msg
;
449 DBG("Sending kernel consumer destroy channel key %d", metadata
->fd
);
451 memset(&msg
, 0, sizeof(msg
));
452 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
453 msg
.u
.destroy_channel
.key
= metadata
->fd
;
455 pthread_mutex_lock(socket
->lock
);
456 health_code_update();
458 ret
= consumer_send_msg(socket
, &msg
);
464 health_code_update();
465 pthread_mutex_unlock(socket
->lock
);