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 "kernel-consumer.h"
32 * Sending a single channel to the consumer with command ADD_CHANNEL.
34 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
35 struct ltt_kernel_channel
*channel
)
38 struct lttcomm_consumer_msg lkm
;
43 DBG("Kernel consumer adding channel %s to kernel consumer",
44 channel
->channel
->name
);
46 /* Prep channel message structure */
47 consumer_init_channel_comm_msg(&lkm
,
48 LTTNG_CONSUMER_ADD_CHANNEL
,
50 channel
->channel
->attr
.subbuf_size
,
52 channel
->channel
->name
,
53 channel
->stream_count
);
55 health_code_update(&health_thread_kernel
);
57 ret
= consumer_send_channel(sock
, &lkm
);
62 health_code_update(&health_thread_kernel
);
69 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
71 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
72 struct ltt_kernel_session
*session
)
75 char tmp_path
[PATH_MAX
];
77 struct lttcomm_consumer_msg lkm
;
78 struct consumer_output
*consumer
;
82 assert(session
->consumer
);
85 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
87 /* Get consumer output pointer */
88 consumer
= session
->consumer
;
90 /* Get the right path name destination */
91 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
92 /* Set application path to the destination path */
93 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
94 consumer
->dst
.trace_path
, consumer
->subdir
);
96 PERROR("snprintf metadata path");
101 /* Create directory */
102 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
103 session
->uid
, session
->gid
);
105 if (ret
!= -EEXIST
) {
106 ERR("Trace directory creation error");
110 DBG3("Kernel local consumer tracefile path: %s", pathname
);
112 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
114 PERROR("snprintf metadata path");
118 DBG3("Kernel network consumer subdir path: %s", pathname
);
121 /* Prep channel message structure */
122 consumer_init_channel_comm_msg(&lkm
,
123 LTTNG_CONSUMER_ADD_CHANNEL
,
124 session
->metadata
->fd
,
125 session
->metadata
->conf
->attr
.subbuf_size
,
130 health_code_update(&health_thread_kernel
);
132 ret
= consumer_send_channel(sock
, &lkm
);
137 health_code_update(&health_thread_kernel
);
139 /* Prep stream message structure */
140 consumer_init_stream_comm_msg(&lkm
,
141 LTTNG_CONSUMER_ADD_STREAM
,
142 session
->metadata
->fd
,
143 session
->metadata_stream_fd
,
144 LTTNG_CONSUMER_ACTIVE_STREAM
,
145 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
149 consumer
->net_seq_index
,
150 1, /* Metadata flag set */
155 health_code_update(&health_thread_kernel
);
157 /* Send stream and file descriptor */
158 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
159 &session
->metadata_stream_fd
, 1);
164 health_code_update(&health_thread_kernel
);
171 * Sending a single stream to the consumer with command ADD_STREAM.
173 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
174 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
175 struct ltt_kernel_session
*session
)
178 char tmp_path
[PATH_MAX
];
179 const char *pathname
;
180 struct lttcomm_consumer_msg lkm
;
181 struct consumer_output
*consumer
;
186 assert(session
->consumer
);
189 DBG("Sending stream %d of channel %s to kernel consumer",
190 stream
->fd
, channel
->channel
->name
);
192 /* Get consumer output pointer */
193 consumer
= session
->consumer
;
195 /* Get the right path name destination */
196 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
197 /* Set application path to the destination path */
198 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
199 consumer
->dst
.trace_path
, consumer
->subdir
);
201 PERROR("snprintf stream path");
205 DBG3("Kernel local consumer tracefile path: %s", pathname
);
207 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
209 PERROR("snprintf stream path");
213 DBG3("Kernel network consumer subdir path: %s", pathname
);
216 /* Prep stream consumer message */
217 consumer_init_stream_comm_msg(&lkm
, LTTNG_CONSUMER_ADD_STREAM
,
221 channel
->channel
->attr
.output
,
225 consumer
->net_seq_index
,
226 0, /* Metadata flag unset */
231 health_code_update(&health_thread_kernel
);
233 /* Send stream and file descriptor */
234 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
239 health_code_update(&health_thread_kernel
);
246 * Send all stream fds of kernel channel to the consumer.
248 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
249 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
)
252 struct ltt_kernel_stream
*stream
;
257 assert(session
->consumer
);
260 /* Bail out if consumer is disabled */
261 if (!session
->consumer
->enabled
) {
266 DBG("Sending streams of channel %s to kernel consumer",
267 channel
->channel
->name
);
269 ret
= kernel_consumer_add_channel(sock
, channel
);
275 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
280 /* Add stream on the kernel consumer side. */
281 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
);
292 * Send all stream fds of the kernel session to the consumer.
294 int kernel_consumer_send_session(struct consumer_socket
*sock
,
295 struct ltt_kernel_session
*session
)
298 struct ltt_kernel_channel
*chan
;
302 assert(session
->consumer
);
305 /* Bail out if consumer is disabled */
306 if (!session
->consumer
->enabled
) {
311 DBG("Sending session stream to kernel consumer");
313 if (session
->metadata_stream_fd
>= 0) {
314 ret
= kernel_consumer_add_metadata(sock
, session
);
319 /* Flag that at least the metadata has been sent to the consumer. */
320 session
->consumer_fds_sent
= 1;
323 /* Send channel and streams of it */
324 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
325 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
);
331 DBG("Kernel consumer FDs of metadata and channel streams sent");