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
,
98 channel
->channel
->attr
.tracefile_size
,
99 channel
->channel
->attr
.tracefile_count
);
101 health_code_update();
103 ret
= consumer_send_channel(sock
, &lkm
);
108 health_code_update();
115 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
117 int kernel_consumer_add_metadata(struct consumer_socket
*sock
,
118 struct ltt_kernel_session
*session
)
121 char tmp_path
[PATH_MAX
];
122 const char *pathname
;
123 struct lttcomm_consumer_msg lkm
;
124 struct consumer_output
*consumer
;
128 assert(session
->consumer
);
131 DBG("Sending metadata %d to kernel consumer", session
->metadata_stream_fd
);
133 /* Get consumer output pointer */
134 consumer
= session
->consumer
;
136 /* Get the right path name destination */
137 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
138 /* Set application path to the destination path */
139 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s%s",
140 consumer
->dst
.trace_path
, consumer
->subdir
);
142 PERROR("snprintf metadata path");
147 /* Create directory */
148 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
149 session
->uid
, session
->gid
);
151 if (ret
!= -EEXIST
) {
152 ERR("Trace directory creation error");
156 DBG3("Kernel local consumer tracefile path: %s", pathname
);
158 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s", consumer
->subdir
);
160 PERROR("snprintf metadata path");
164 DBG3("Kernel network consumer subdir path: %s", pathname
);
167 /* Prep channel message structure */
168 consumer_init_channel_comm_msg(&lkm
,
169 LTTNG_CONSUMER_ADD_CHANNEL
,
170 session
->metadata
->fd
,
175 consumer
->net_seq_index
,
176 DEFAULT_METADATA_NAME
,
178 DEFAULT_KERNEL_CHANNEL_OUTPUT
,
179 CONSUMER_CHANNEL_TYPE_METADATA
,
182 health_code_update();
184 ret
= consumer_send_channel(sock
, &lkm
);
189 health_code_update();
191 /* Prep stream message structure */
192 consumer_init_stream_comm_msg(&lkm
,
193 LTTNG_CONSUMER_ADD_STREAM
,
194 session
->metadata
->fd
,
195 session
->metadata_stream_fd
,
196 0); /* CPU: 0 for metadata. */
198 health_code_update();
200 /* Send stream and file descriptor */
201 ret
= consumer_send_stream(sock
, consumer
, &lkm
,
202 &session
->metadata_stream_fd
, 1);
207 health_code_update();
214 * Sending a single stream to the consumer with command ADD_STREAM.
216 int kernel_consumer_add_stream(struct consumer_socket
*sock
,
217 struct ltt_kernel_channel
*channel
, struct ltt_kernel_stream
*stream
,
218 struct ltt_kernel_session
*session
)
221 struct lttcomm_consumer_msg lkm
;
222 struct consumer_output
*consumer
;
227 assert(session
->consumer
);
230 DBG("Sending stream %d of channel %s to kernel consumer",
231 stream
->fd
, channel
->channel
->name
);
233 /* Get consumer output pointer */
234 consumer
= session
->consumer
;
236 /* Prep stream consumer message */
237 consumer_init_stream_comm_msg(&lkm
,
238 LTTNG_CONSUMER_ADD_STREAM
,
243 health_code_update();
245 /* Send stream and file descriptor */
246 ret
= consumer_send_stream(sock
, consumer
, &lkm
, &stream
->fd
, 1);
251 health_code_update();
258 * Send all stream fds of kernel channel to the consumer.
260 int kernel_consumer_send_channel_stream(struct consumer_socket
*sock
,
261 struct ltt_kernel_channel
*channel
, struct ltt_kernel_session
*session
)
264 struct ltt_kernel_stream
*stream
;
269 assert(session
->consumer
);
272 /* Bail out if consumer is disabled */
273 if (!session
->consumer
->enabled
) {
278 DBG("Sending streams of channel %s to kernel consumer",
279 channel
->channel
->name
);
281 ret
= kernel_consumer_add_channel(sock
, channel
, session
);
287 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
292 /* Add stream on the kernel consumer side. */
293 ret
= kernel_consumer_add_stream(sock
, channel
, stream
, session
);
304 * Send all stream fds of the kernel session to the consumer.
306 int kernel_consumer_send_session(struct consumer_socket
*sock
,
307 struct ltt_kernel_session
*session
)
310 struct ltt_kernel_channel
*chan
;
314 assert(session
->consumer
);
317 /* Bail out if consumer is disabled */
318 if (!session
->consumer
->enabled
) {
323 DBG("Sending session stream to kernel consumer");
325 if (session
->metadata_stream_fd
>= 0) {
326 ret
= kernel_consumer_add_metadata(sock
, session
);
331 /* Flag that at least the metadata has been sent to the consumer. */
332 session
->consumer_fds_sent
= 1;
335 /* Send channel and streams of it */
336 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
337 ret
= kernel_consumer_send_channel_stream(sock
, chan
, session
);
343 DBG("Kernel consumer FDs of metadata and channel streams sent");