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>
30 #include "kernel-consumer.h"
33 * Sending a single channel to the consumer with command ADD_CHANNEL.
35 int kernel_consumer_add_channel(struct consumer_socket
*sock
,
36 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
)
39 char tmp_path
[PATH_MAX
];
41 struct lttcomm_consumer_msg lkm
;
42 struct consumer_output
*consumer
;
47 assert(session
->consumer
);
49 consumer
= session
->consumer
;
51 DBG("Kernel consumer adding channel %s to kernel consumer",
52 channel
->channel
->name
);
54 /* Get the right path name destination */
55 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
56 /* Set application path to the destination path */
57 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
58 consumer
->dst
.trace_path
, consumer
->subdir
);
60 PERROR("snprintf metadata path");
65 /* Create directory */
66 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
67 session
->uid
, session
->gid
);
70 ERR("Trace directory creation error");
74 DBG3("Kernel local consumer tracefile path: %s", pathname
);
76 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
78 PERROR("snprintf metadata path");
82 DBG3("Kernel network consumer subdir path: %s", pathname
);
85 /* Prep channel message structure */
86 consumer_init_channel_comm_msg(&lkm
,
87 LTTNG_CONSUMER_ADD_CHANNEL
,
93 consumer
->net_seq_index
,
94 channel
->channel
->name
,
95 channel
->stream_count
,
96 channel
->channel
->attr
.output
,
97 CONSUMER_CHANNEL_TYPE_DATA
);
101 ret
= consumer_send_channel(sock
, &lkm
);
106 health_code_update();
113 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
115 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
116 struct ltt_kernel_session
*session
)
119 char tmp_path
[PATH_MAX
];
120 const char *pathname
;
121 struct lttcomm_consumer_msg lkm
;
122 struct consumer_output
*consumer
;
126 assert(session
->consumer
);
129 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
131 /* Get consumer output pointer */
132 consumer
= session
->consumer
;
134 /* Get the right path name destination */
135 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
136 /* Set application path to the destination path */
137 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
138 consumer
->dst
.trace_path
, consumer
->subdir
);
140 PERROR("snprintf metadata path");
145 /* Create directory */
146 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
147 session
->uid
, session
->gid
);
149 if (ret
!= -EEXIST
) {
150 ERR("Trace directory creation error");
154 DBG3("Kernel local consumer tracefile path: %s", pathname
);
156 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
158 PERROR("snprintf metadata path");
162 DBG3("Kernel network consumer subdir path: %s", pathname
);
165 /* Prep channel message structure */
166 consumer_init_channel_comm_msg(&lkm
,
167 LTTNG_CONSUMER_ADD_CHANNEL
,
168 session
->metadata
->fd
,
173 consumer
->net_seq_index
,
174 DEFAULT_METADATA_NAME
,
176 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
177 CONSUMER_CHANNEL_TYPE_METADATA
);
179 health_code_update();
181 ret
= consumer_send_channel(sock
, &lkm
);
186 health_code_update();
188 /* Prep stream message structure */
189 consumer_init_stream_comm_msg(&lkm
,
190 LTTNG_CONSUMER_ADD_STREAM
,
191 session
->metadata
->fd
,
192 session
->metadata_stream_fd
,
193 0); /* CPU: 0 for metadata. */
195 health_code_update();
197 /* Send stream and file descriptor */
198 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
199 &session
->metadata_stream_fd
, 1);
204 health_code_update();
211 * Sending a single stream to the consumer with command ADD_STREAM.
213 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
214 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
215 struct ltt_kernel_session
*session
)
218 struct lttcomm_consumer_msg lkm
;
219 struct consumer_output
*consumer
;
224 assert(session
->consumer
);
227 DBG("Sending stream %d of channel %s to kernel consumer",
228 stream
->fd
, channel
->channel
->name
);
230 /* Get consumer output pointer */
231 consumer
= session
->consumer
;
233 /* Prep stream consumer message */
234 consumer_init_stream_comm_msg(&lkm
,
235 LTTNG_CONSUMER_ADD_STREAM
,
240 health_code_update();
242 /* Send stream and file descriptor */
243 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
248 health_code_update();
255 * Send all stream fds of kernel channel to the consumer.
257 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
258 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
)
261 struct ltt_kernel_stream
*stream
;
266 assert(session
->consumer
);
269 /* Bail out if consumer is disabled */
270 if (!session
->consumer
->enabled
) {
275 DBG("Sending streams of channel %s to kernel consumer",
276 channel
->channel
->name
);
278 ret
= kernel_consumer_add_channel(sock
, channel
, session
);
284 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
289 /* Add stream on the kernel consumer side. */
290 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
);
301 * Send all stream fds of the kernel session to the consumer.
303 int kernel_consumer_send_session(struct consumer_socket
*sock
,
304 struct ltt_kernel_session
*session
)
307 struct ltt_kernel_channel
*chan
;
311 assert(session
->consumer
);
314 /* Bail out if consumer is disabled */
315 if (!session
->consumer
->enabled
) {
320 DBG("Sending session stream to kernel consumer");
322 if (session
->metadata_stream_fd
>= 0) {
323 ret
= kernel_consumer_add_metadata(sock
, session
);
328 /* Flag that at least the metadata has been sent to the consumer. */
329 session
->consumer_fds_sent
= 1;
332 /* Send channel and streams of it */
333 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
334 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
);
340 DBG("Kernel consumer FDs of metadata and channel streams sent");