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
, struct ltt_kernel_session
*session
)
38 char tmp_path
[PATH_MAX
];
40 struct lttcomm_consumer_msg lkm
;
41 struct consumer_output
*consumer
;
46 assert(session
->consumer
);
48 consumer
= session
->consumer
;
50 DBG("Kernel consumer adding channel %s to kernel consumer",
51 channel
->channel
->name
);
53 /* Get the right path name destination */
54 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
55 /* Set application path to the destination path */
56 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
57 consumer
->dst
.trace_path
, consumer
->subdir
);
59 PERROR("snprintf metadata path");
64 /* Create directory */
65 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
66 session
->uid
, session
->gid
);
69 ERR("Trace directory creation error");
73 DBG3("Kernel local consumer tracefile path: %s", pathname
);
75 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
77 PERROR("snprintf metadata path");
81 DBG3("Kernel network consumer subdir path: %s", pathname
);
84 /* Prep channel message structure */
85 consumer_init_channel_comm_msg(&lkm
,
86 LTTNG_CONSUMER_ADD_CHANNEL
,
92 consumer
->net_seq_index
,
93 channel
->channel
->name
,
94 channel
->stream_count
,
95 channel
->channel
->attr
.output
,
96 CONSUMER_CHANNEL_TYPE_DATA
);
100 ret
= consumer_send_channel(sock
, &lkm
);
105 health_code_update();
112 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
114 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
115 struct ltt_kernel_session
*session
)
118 char tmp_path
[PATH_MAX
];
119 const char *pathname
;
120 struct lttcomm_consumer_msg lkm
;
121 struct consumer_output
*consumer
;
125 assert(session
->consumer
);
128 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
130 /* Get consumer output pointer */
131 consumer
= session
->consumer
;
133 /* Get the right path name destination */
134 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
135 /* Set application path to the destination path */
136 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
137 consumer
->dst
.trace_path
, consumer
->subdir
);
139 PERROR("snprintf metadata path");
144 /* Create directory */
145 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
146 session
->uid
, session
->gid
);
148 if (ret
!= -EEXIST
) {
149 ERR("Trace directory creation error");
153 DBG3("Kernel local consumer tracefile path: %s", pathname
);
155 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
157 PERROR("snprintf metadata path");
161 DBG3("Kernel network consumer subdir path: %s", pathname
);
164 /* Prep channel message structure */
165 consumer_init_channel_comm_msg(&lkm
,
166 LTTNG_CONSUMER_ADD_CHANNEL
,
167 session
->metadata
->fd
,
172 consumer
->net_seq_index
,
173 DEFAULT_METADATA_NAME
,
175 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
176 CONSUMER_CHANNEL_TYPE_METADATA
);
178 health_code_update();
180 ret
= consumer_send_channel(sock
, &lkm
);
185 health_code_update();
187 /* Prep stream message structure */
188 consumer_init_stream_comm_msg(&lkm
,
189 LTTNG_CONSUMER_ADD_STREAM
,
190 session
->metadata
->fd
,
191 session
->metadata_stream_fd
,
192 0); /* CPU: 0 for metadata. */
194 health_code_update();
196 /* Send stream and file descriptor */
197 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
198 &session
->metadata_stream_fd
, 1);
203 health_code_update();
210 * Sending a single stream to the consumer with command ADD_STREAM.
212 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
213 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
214 struct ltt_kernel_session
*session
)
217 struct lttcomm_consumer_msg lkm
;
218 struct consumer_output
*consumer
;
223 assert(session
->consumer
);
226 DBG("Sending stream %d of channel %s to kernel consumer",
227 stream
->fd
, channel
->channel
->name
);
229 /* Get consumer output pointer */
230 consumer
= session
->consumer
;
232 /* Prep stream consumer message */
233 consumer_init_stream_comm_msg(&lkm
,
234 LTTNG_CONSUMER_ADD_STREAM
,
239 health_code_update();
241 /* Send stream and file descriptor */
242 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
247 health_code_update();
254 * Send all stream fds of kernel channel to the consumer.
256 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
257 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
)
260 struct ltt_kernel_stream
*stream
;
265 assert(session
->consumer
);
268 /* Bail out if consumer is disabled */
269 if (!session
->consumer
->enabled
) {
274 DBG("Sending streams of channel %s to kernel consumer",
275 channel
->channel
->name
);
277 ret
= kernel_consumer_add_channel(sock
, channel
, session
);
283 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
288 /* Add stream on the kernel consumer side. */
289 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
);
300 * Send all stream fds of the kernel session to the consumer.
302 int kernel_consumer_send_session(struct consumer_socket
*sock
,
303 struct ltt_kernel_session
*session
)
306 struct ltt_kernel_channel
*chan
;
310 assert(session
->consumer
);
313 /* Bail out if consumer is disabled */
314 if (!session
->consumer
->enabled
) {
319 DBG("Sending session stream to kernel consumer");
321 if (session
->metadata_stream_fd
>= 0) {
322 ret
= kernel_consumer_add_metadata(sock
, session
);
327 /* Flag that at least the metadata has been sent to the consumer. */
328 session
->consumer_fds_sent
= 1;
331 /* Send channel and streams of it */
332 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
333 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
);
339 DBG("Kernel consumer FDs of metadata and channel streams sent");