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.
26 #include <common/common.h>
27 #include <common/consumer.h>
28 #include <common/defaults.h>
31 #include "ust-consumer.h"
34 * Return allocated full pathname of the session using the consumer trace path
35 * and subdir if available. On a successful allocation, the directory of the
36 * trace is created with the session credentials.
38 * The caller can safely free(3) the returned value. On error, NULL is
41 static char *setup_trace_path(struct consumer_output
*consumer
,
42 struct ust_app_session
*ua_sess
)
52 /* Allocate our self the string to make sure we never exceed PATH_MAX. */
53 pathname
= zmalloc(PATH_MAX
);
58 /* Get correct path name destination */
59 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
60 /* Set application path to the destination path */
61 ret
= snprintf(pathname
, PATH_MAX
, "%s/%s/%s",
62 consumer
->dst
.trace_path
, consumer
->subdir
, ua_sess
->path
);
64 PERROR("snprintf channel path");
68 /* Create directory. Ignore if exist. */
69 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
, ua_sess
->uid
,
73 ERR("Trace directory creation error");
78 ret
= snprintf(pathname
, PATH_MAX
, "%s/%s", consumer
->subdir
,
81 PERROR("snprintf channel path");
94 * Send a single channel to the consumer using command ADD_CHANNEL.
96 * Consumer socket MUST be acquired before calling this.
98 static int ask_channel_creation(struct ust_app_session
*ua_sess
,
99 struct ust_app_channel
*ua_chan
, struct consumer_output
*consumer
,
100 struct consumer_socket
*socket
)
104 char *pathname
= NULL
;
105 struct lttcomm_consumer_msg msg
;
112 DBG2("Asking UST consumer for channel");
114 /* Get and create full trace path of session. */
115 pathname
= setup_trace_path(consumer
, ua_sess
);
121 consumer_init_ask_channel_comm_msg(&msg
,
122 ua_chan
->attr
.subbuf_size
,
123 ua_chan
->attr
.num_subbuf
,
124 ua_chan
->attr
.overwrite
,
125 ua_chan
->attr
.switch_timer_interval
,
126 ua_chan
->attr
.read_timer_interval
,
127 (int) ua_chan
->attr
.output
,
128 (int) ua_chan
->attr
.type
,
134 consumer
->net_seq_index
,
136 ua_sess
->registry
.uuid
);
138 health_code_update();
140 ret
= lttcomm_send_unix_sock(socket
->fd
, &msg
, sizeof(msg
));
145 ret
= consumer_recv_status_channel(socket
, &key
,
146 &ua_chan
->expected_stream_count
);
150 /* Communication protocol error. */
151 assert(key
== ua_chan
->key
);
152 /* We need at least one where 1 stream for 1 cpu. */
153 assert(ua_chan
->expected_stream_count
> 0);
155 DBG2("UST ask channel %" PRIu64
" successfully done with %u stream(s)", key
,
156 ua_chan
->expected_stream_count
);
160 health_code_update();
165 * Ask consumer to create a channel for a given session.
167 * Returns 0 on success else a negative value.
169 int ust_consumer_ask_channel(struct ust_app_session
*ua_sess
,
170 struct ust_app_channel
*ua_chan
, struct consumer_output
*consumer
,
171 struct consumer_socket
*socket
)
179 assert(socket
->fd
>= 0);
181 pthread_mutex_lock(socket
->lock
);
183 ret
= ask_channel_creation(ua_sess
, ua_chan
, consumer
, socket
);
189 pthread_mutex_unlock(socket
->lock
);
194 * Send a get channel command to consumer using the given channel key. The
195 * channel object is populated and the stream list.
197 * Return 0 on success else a negative value.
199 int ust_consumer_get_channel(struct consumer_socket
*socket
,
200 struct ust_app_channel
*ua_chan
)
203 struct lttcomm_consumer_msg msg
;
207 assert(socket
->fd
>= 0);
209 msg
.cmd_type
= LTTNG_CONSUMER_GET_CHANNEL
;
210 msg
.u
.get_channel
.key
= ua_chan
->key
;
212 pthread_mutex_lock(socket
->lock
);
213 health_code_update();
215 /* Send command and wait for OK reply. */
216 ret
= consumer_send_msg(socket
, &msg
);
221 /* First, get the channel from consumer. */
222 ret
= ustctl_recv_channel_from_consumer(socket
->fd
, &ua_chan
->obj
);
225 ERR("Error recv channel from consumer %d with ret %d",
228 DBG3("UST app recv channel from consumer. Consumer is dead.");
233 /* Next, get all streams. */
235 struct ust_app_stream
*stream
;
237 /* Create UST stream */
238 stream
= ust_app_alloc_stream();
239 if (stream
== NULL
) {
244 /* Stream object is populated by this call if successful. */
245 ret
= ustctl_recv_stream_from_consumer(socket
->fd
, &stream
->obj
);
248 if (ret
== -LTTNG_UST_ERR_NOENT
) {
249 DBG3("UST app consumer has no more stream available");
254 ERR("Recv stream from consumer %d with ret %d",
257 DBG3("UST app recv stream from consumer. Consumer is dead.");
262 /* Order is important this is why a list is used. */
263 cds_list_add_tail(&stream
->list
, &ua_chan
->streams
.head
);
264 ua_chan
->streams
.count
++;
266 DBG2("UST app stream %d received succesfully", ua_chan
->streams
.count
);
269 /* This MUST match or else we have a synchronization problem. */
270 assert(ua_chan
->expected_stream_count
== ua_chan
->streams
.count
);
272 /* Wait for confirmation that we can proceed with the streams. */
273 ret
= consumer_recv_status_reply(socket
);
279 health_code_update();
280 pthread_mutex_unlock(socket
->lock
);
285 * Send a destroy channel command to consumer using the given channel key.
287 * Note that this command MUST be used prior to a successful
288 * LTTNG_CONSUMER_GET_CHANNEL because once this command is done successfully,
289 * the streams are dispatched to the consumer threads and MUST be teardown
290 * through the hang up process.
292 * Return 0 on success else a negative value.
294 int ust_consumer_destroy_channel(struct consumer_socket
*socket
,
295 struct ust_app_channel
*ua_chan
)
298 struct lttcomm_consumer_msg msg
;
302 assert(socket
->fd
>= 0);
304 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
305 msg
.u
.destroy_channel
.key
= ua_chan
->key
;
307 pthread_mutex_lock(socket
->lock
);
308 health_code_update();
310 ret
= consumer_send_msg(socket
, &msg
);
316 health_code_update();
317 pthread_mutex_unlock(socket
->lock
);
322 * Send a given stream to UST tracer.
324 * On success return 0 else a negative value.
326 int ust_consumer_send_stream_to_ust(struct ust_app
*app
,
327 struct ust_app_channel
*channel
, struct ust_app_stream
*stream
)
335 DBG2("UST consumer send stream to app %d", app
->sock
);
337 /* Relay stream to application. */
338 ret
= ustctl_send_stream_to_ust(app
->sock
, channel
->obj
, stream
->obj
);
340 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
341 ERR("Error ustctl send stream %s to app pid: %d with ret %d",
342 stream
->name
, app
->pid
, ret
);
344 DBG3("UST app send stream to ust failed. Application is dead.");
348 channel
->handle
= channel
->obj
->handle
;
355 * Send channel previously received from the consumer to the UST tracer.
357 * On success return 0 else a negative value.
359 int ust_consumer_send_channel_to_ust(struct ust_app
*app
,
360 struct ust_app_session
*ua_sess
, struct ust_app_channel
*channel
)
367 assert(channel
->obj
);
369 DBG2("UST app send channel to app sock %d pid %d (name: %s, key: %lu)",
370 app
->sock
, app
->pid
, channel
->name
, channel
->key
);
372 /* Send stream to application. */
373 ret
= ustctl_send_channel_to_ust(app
->sock
, ua_sess
->handle
, channel
->obj
);
375 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
376 ERR("Error ustctl send channel %s to app pid: %d with ret %d",
377 channel
->name
, app
->pid
, ret
);
379 DBG3("UST app send channel to ust failed. Application is dead.");
389 * Send metadata string to consumer.
391 * Return 0 on success else a negative value.
393 int ust_consumer_push_metadata(struct consumer_socket
*socket
,
394 struct ust_app_session
*ua_sess
, char *metadata_str
,
395 size_t len
, size_t target_offset
)
398 struct lttcomm_consumer_msg msg
;
401 assert(socket
->fd
>= 0);
403 assert(ua_sess
->metadata
);
405 DBG2("UST consumer push metadata to consumer socket %d", socket
->fd
);
407 msg
.cmd_type
= LTTNG_CONSUMER_PUSH_METADATA
;
408 msg
.u
.push_metadata
.key
= ua_sess
->metadata
->key
;
409 msg
.u
.push_metadata
.target_offset
= target_offset
;
410 msg
.u
.push_metadata
.len
= len
;
413 * TODO: reenable these locks when the consumerd gets the ability to
414 * reorder the metadata it receives. This fits with locking in
415 * src/bin/lttng-sessiond/ust-app.c:push_metadata()
417 * pthread_mutex_lock(socket->lock);
420 health_code_update();
421 ret
= consumer_send_msg(socket
, &msg
);
426 DBG3("UST consumer push metadata on sock %d of len %lu", socket
->fd
, len
);
428 ret
= lttcomm_send_unix_sock(socket
->fd
, metadata_str
, len
);
430 fprintf(stderr
, "send error: %d\n", ret
);
434 health_code_update();
435 ret
= consumer_recv_status_reply(socket
);
441 health_code_update();
443 * pthread_mutex_unlock(socket->lock);
449 * Send a close metdata command to consumer using the given channel key.
451 * Return 0 on success else a negative value.
453 int ust_consumer_close_metadata(struct consumer_socket
*socket
,
454 struct ust_app_channel
*ua_chan
)
457 struct lttcomm_consumer_msg msg
;
461 assert(socket
->fd
>= 0);
463 DBG2("UST consumer close metadata channel key %lu", ua_chan
->key
);
465 msg
.cmd_type
= LTTNG_CONSUMER_CLOSE_METADATA
;
466 msg
.u
.close_metadata
.key
= ua_chan
->key
;
468 pthread_mutex_lock(socket
->lock
);
469 health_code_update();
471 ret
= consumer_send_msg(socket
, &msg
);
477 health_code_update();
478 pthread_mutex_unlock(socket
->lock
);
483 * Send a setup metdata command to consumer using the given channel key.
485 * Return 0 on success else a negative value.
487 int ust_consumer_setup_metadata(struct consumer_socket
*socket
,
488 struct ust_app_channel
*ua_chan
)
491 struct lttcomm_consumer_msg msg
;
495 assert(socket
->fd
>= 0);
497 DBG2("UST consumer setup metadata channel key %lu", ua_chan
->key
);
499 msg
.cmd_type
= LTTNG_CONSUMER_SETUP_METADATA
;
500 msg
.u
.setup_metadata
.key
= ua_chan
->key
;
502 pthread_mutex_lock(socket
->lock
);
503 health_code_update();
505 ret
= consumer_send_msg(socket
, &msg
);
511 health_code_update();
512 pthread_mutex_unlock(socket
->lock
);