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(int sock
, struct ust_app_channel
*uchan
)
38 struct lttcomm_consumer_msg msg
;
48 DBG2("Sending channel %s to UST consumer", uchan
->name
);
50 consumer_init_channel_comm_msg(&msg
,
51 LTTNG_CONSUMER_ADD_CHANNEL
,
53 uchan
->attr
.subbuf_size
,
54 uchan
->obj
->memory_map_size
,
57 ret
= consumer_send_channel(sock
, &msg
);
62 fd
= uchan
->obj
->shm_fd
;
63 ret
= consumer_send_fds(sock
, &fd
, 1);
73 * Send a single stream to the consumer using ADD_STREAM command.
75 static int send_channel_stream(int sock
, struct ust_app_channel
*uchan
,
76 struct ust_app_session
*usess
, struct ltt_ust_stream
*stream
,
77 struct consumer_output
*consumer
, const char *pathname
)
80 struct lttcomm_consumer_msg msg
;
88 DBG2("Sending stream %d of channel %s to kernel consumer",
89 stream
->obj
->shm_fd
, uchan
->name
);
91 consumer_init_stream_comm_msg(&msg
,
92 LTTNG_CONSUMER_ADD_STREAM
,
95 LTTNG_CONSUMER_ACTIVE_STREAM
,
96 DEFAULT_UST_CHANNEL_OUTPUT
,
97 stream
->obj
->memory_map_size
,
100 consumer
->net_seq_index
,
101 0, /* Metadata flag unset */
105 /* Send stream and file descriptor */
106 fds
[0] = stream
->obj
->shm_fd
;
107 fds
[1] = stream
->obj
->wait_fd
;
108 ret
= consumer_send_stream(sock
, consumer
, &msg
, fds
, 2);
118 * Send all stream fds of UST channel to the consumer.
120 static int send_channel_streams(int sock
,
121 struct ust_app_channel
*uchan
, struct ust_app_session
*usess
,
122 struct consumer_output
*consumer
)
125 char tmp_path
[PATH_MAX
];
126 const char *pathname
;
127 struct ltt_ust_stream
*stream
, *tmp
;
129 DBG("Sending streams of channel %s to UST consumer", uchan
->name
);
131 ret
= send_channel(sock
, uchan
);
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/%s",
140 consumer
->dst
.trace_path
, consumer
->subdir
, usess
->path
);
142 PERROR("snprintf stream path");
146 DBG3("UST local consumer tracefile path: %s", pathname
);
148 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
149 consumer
->subdir
, usess
->path
);
151 PERROR("snprintf stream path");
155 DBG3("UST network consumer subdir path: %s", pathname
);
158 cds_list_for_each_entry_safe(stream
, tmp
, &uchan
->streams
.head
, list
) {
159 if (!stream
->obj
->shm_fd
) {
163 ret
= send_channel_stream(sock
, uchan
, usess
, stream
, consumer
,
170 DBG("UST consumer channel streams sent");
179 * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
181 static int send_metadata(int sock
, struct ust_app_session
*usess
,
182 struct consumer_output
*consumer
)
185 char tmp_path
[PATH_MAX
];
186 const char *pathname
;
187 struct lttcomm_consumer_msg msg
;
194 ERR("Consumer socket is negative (%d)", sock
);
198 if (usess
->metadata
->obj
->shm_fd
== 0) {
199 ERR("Metadata obj shm_fd is 0");
204 DBG("UST consumer sending metadata stream fd");
206 consumer_init_channel_comm_msg(&msg
,
207 LTTNG_CONSUMER_ADD_CHANNEL
,
208 usess
->metadata
->obj
->shm_fd
,
209 usess
->metadata
->attr
.subbuf_size
,
210 usess
->metadata
->obj
->memory_map_size
,
213 ret
= consumer_send_channel(sock
, &msg
);
218 /* Sending metadata shared memory fd */
219 fd
= usess
->metadata
->obj
->shm_fd
;
220 ret
= consumer_send_fds(sock
, &fd
, 1);
225 /* Get correct path name destination */
226 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
227 /* Set application path to the destination path */
228 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s/%s",
229 consumer
->dst
.trace_path
, consumer
->subdir
, usess
->path
);
231 PERROR("snprintf stream path");
236 /* Create directory */
237 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
238 usess
->uid
, usess
->gid
);
240 if (ret
!= -EEXIST
) {
241 ERR("Trace directory creation error");
246 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
247 consumer
->subdir
, usess
->path
);
249 PERROR("snprintf metadata path");
255 consumer_init_stream_comm_msg(&msg
,
256 LTTNG_CONSUMER_ADD_STREAM
,
257 usess
->metadata
->obj
->shm_fd
,
258 usess
->metadata
->stream_obj
->shm_fd
,
259 LTTNG_CONSUMER_ACTIVE_STREAM
,
260 DEFAULT_UST_CHANNEL_OUTPUT
,
261 usess
->metadata
->stream_obj
->memory_map_size
,
264 consumer
->net_seq_index
,
265 1, /* Flag metadata set */
269 /* Send stream and file descriptor */
270 fds
[0] = usess
->metadata
->stream_obj
->shm_fd
;
271 fds
[1] = usess
->metadata
->stream_obj
->wait_fd
;
272 ret
= consumer_send_stream(sock
, consumer
, &msg
, fds
, 2);
282 * Send all stream fds of the UST session to the consumer.
284 int ust_consumer_send_session(struct ust_app_session
*usess
,
285 struct consumer_output
*consumer
, struct consumer_socket
*sock
)
288 struct lttng_ht_iter iter
;
289 struct ust_app_channel
*ua_chan
;
293 if (consumer
== NULL
|| sock
== NULL
) {
294 /* There is no consumer so just ignoring the command. */
295 DBG("UST consumer does not exist. Not sending streams");
299 DBG("Sending metadata stream fd to consumer on %d", sock
->fd
);
301 pthread_mutex_lock(sock
->lock
);
303 /* Sending metadata information to the consumer */
304 ret
= send_metadata(sock
->fd
, usess
, consumer
);
309 /* Send each channel fd streams of session */
311 cds_lfht_for_each_entry(usess
->channels
->ht
, &iter
.iter
, ua_chan
,
314 * Indicate that the channel was not created on the tracer side so skip
315 * sending unexisting streams.
317 if (ua_chan
->obj
== NULL
) {
321 ret
= send_channel_streams(sock
->fd
, ua_chan
, usess
, consumer
);
329 DBG("consumer fds (metadata and channel streams) sent");
335 pthread_mutex_unlock(sock
->lock
);