2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <common/common.h>
26 #include <common/consumer.h>
27 #include <common/defaults.h>
30 #include "ust-consumer.h"
33 * Send a single channel to the consumer using command ADD_CHANNEL.
35 static int send_channel(struct consumer_socket
*sock
,
36 struct ust_app_channel
*uchan
)
39 struct lttcomm_consumer_msg msg
;
50 DBG2("Sending channel %s to UST consumer", uchan
->name
);
52 consumer_init_channel_comm_msg(&msg
,
53 LTTNG_CONSUMER_ADD_CHANNEL
,
55 uchan
->attr
.subbuf_size
,
56 uchan
->obj
->memory_map_size
,
58 uchan
->streams
.count
);
60 ret
= consumer_send_channel(sock
, &msg
);
65 fd
= uchan
->obj
->shm_fd
;
66 ret
= consumer_send_fds(sock
, &fd
, 1);
76 * Send a single stream to the consumer using ADD_STREAM command.
78 static int send_channel_stream(struct consumer_socket
*sock
,
79 struct ust_app_channel
*uchan
, struct ust_app_session
*usess
,
80 struct ltt_ust_stream
*stream
, struct consumer_output
*consumer
,
84 struct lttcomm_consumer_msg msg
;
93 DBG2("Sending stream %d of channel %s to kernel consumer",
94 stream
->obj
->shm_fd
, uchan
->name
);
96 consumer_init_stream_comm_msg(&msg
,
97 LTTNG_CONSUMER_ADD_STREAM
,
100 LTTNG_CONSUMER_ACTIVE_STREAM
,
101 DEFAULT_UST_CHANNEL_OUTPUT
,
102 stream
->obj
->memory_map_size
,
105 consumer
->net_seq_index
,
106 0, /* Metadata flag unset */
111 /* Send stream and file descriptor */
112 fds
[0] = stream
->obj
->shm_fd
;
113 fds
[1] = stream
->obj
->wait_fd
;
114 ret
= consumer_send_stream(sock
, consumer
, &msg
, fds
, 2);
124 * Send all stream fds of UST channel to the consumer.
126 static int send_channel_streams(struct consumer_socket
*sock
,
127 struct ust_app_channel
*uchan
, struct ust_app_session
*usess
,
128 struct consumer_output
*consumer
)
131 char tmp_path
[PATH_MAX
];
132 const char *pathname
;
133 struct ltt_ust_stream
*stream
, *tmp
;
137 DBG("Sending streams of channel %s to UST consumer", uchan
->name
);
139 ret
= send_channel(sock
, uchan
);
144 /* Get the right path name destination */
145 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
146 /* Set application path to the destination path */
147 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s/%s",
148 consumer
->dst
.trace_path
, consumer
->subdir
, usess
->path
);
150 PERROR("snprintf stream path");
154 DBG3("UST local consumer tracefile path: %s", pathname
);
156 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
157 consumer
->subdir
, usess
->path
);
159 PERROR("snprintf stream path");
163 DBG3("UST network consumer subdir path: %s", pathname
);
166 cds_list_for_each_entry_safe(stream
, tmp
, &uchan
->streams
.head
, list
) {
167 if (!stream
->obj
->shm_fd
) {
171 ret
= send_channel_stream(sock
, uchan
, usess
, stream
, consumer
,
178 DBG("UST consumer channel streams sent");
187 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
189 static int send_metadata(struct consumer_socket
*sock
,
190 struct ust_app_session
*usess
, struct consumer_output
*consumer
)
193 char tmp_path
[PATH_MAX
];
194 const char *pathname
;
195 struct lttcomm_consumer_msg msg
;
203 ERR("Consumer socket is negative (%d)", sock
->fd
);
207 if (usess
->metadata
->obj
->shm_fd
== 0) {
208 ERR("Metadata obj shm_fd is 0");
213 DBG("UST consumer sending metadata stream fd");
215 consumer_init_channel_comm_msg(&msg
,
216 LTTNG_CONSUMER_ADD_CHANNEL
,
217 usess
->metadata
->obj
->shm_fd
,
218 usess
->metadata
->attr
.subbuf_size
,
219 usess
->metadata
->obj
->memory_map_size
,
223 ret
= consumer_send_channel(sock
, &msg
);
228 /* Sending metadata shared memory fd */
229 fd
= usess
->metadata
->obj
->shm_fd
;
230 ret
= consumer_send_fds(sock
, &fd
, 1);
235 /* Get correct path name destination */
236 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
237 /* Set application path to the destination path */
238 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s/%s",
239 consumer
->dst
.trace_path
, consumer
->subdir
, usess
->path
);
241 PERROR("snprintf stream path");
246 /* Create directory */
247 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
248 usess
->uid
, usess
->gid
);
250 if (ret
!= -EEXIST
) {
251 ERR("Trace directory creation error");
256 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
257 consumer
->subdir
, usess
->path
);
259 PERROR("snprintf metadata path");
265 consumer_init_stream_comm_msg(&msg
,
266 LTTNG_CONSUMER_ADD_STREAM
,
267 usess
->metadata
->obj
->shm_fd
,
268 usess
->metadata
->stream_obj
->shm_fd
,
269 LTTNG_CONSUMER_ACTIVE_STREAM
,
270 DEFAULT_UST_CHANNEL_OUTPUT
,
271 usess
->metadata
->stream_obj
->memory_map_size
,
274 consumer
->net_seq_index
,
275 1, /* Flag metadata set */
280 /* Send stream and file descriptor */
281 fds
[0] = usess
->metadata
->stream_obj
->shm_fd
;
282 fds
[1] = usess
->metadata
->stream_obj
->wait_fd
;
283 ret
= consumer_send_stream(sock
, consumer
, &msg
, fds
, 2);
293 * Send all stream fds of the UST session to the consumer.
295 int ust_consumer_send_session(struct ust_app_session
*usess
,
296 struct consumer_output
*consumer
, struct consumer_socket
*sock
)
299 struct lttng_ht_iter iter
;
300 struct ust_app_channel
*ua_chan
;
304 if (consumer
== NULL
|| sock
== NULL
) {
305 /* There is no consumer so just ignoring the command. */
306 DBG("UST consumer does not exist. Not sending streams");
310 DBG("Sending metadata stream fd to consumer on %d", sock
->fd
);
312 pthread_mutex_lock(sock
->lock
);
314 /* Sending metadata information to the consumer */
315 ret
= send_metadata(sock
, usess
, consumer
);
320 /* Send each channel fd streams of session */
322 cds_lfht_for_each_entry(usess
->channels
->ht
, &iter
.iter
, ua_chan
,
325 * Indicate that the channel was not created on the tracer side so skip
326 * sending unexisting streams.
328 if (ua_chan
->obj
== NULL
) {
332 ret
= send_channel_streams(sock
, ua_chan
, usess
, consumer
);
340 DBG("consumer fds (metadata and channel streams) sent");
346 pthread_mutex_unlock(sock
->lock
);