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/consumer.h>
28 #include <common/defaults.h>
31 #include "health-sessiond.h"
32 #include "ust-consumer.h"
33 #include "buffer-registry.h"
35 #include "lttng-sessiond.h"
38 * Return allocated full pathname of the session using the consumer trace path
39 * and subdir if available.
41 * The caller can safely free(3) the returned value. On error, NULL is
44 static char *setup_channel_trace_path(struct consumer_output
*consumer
,
45 struct ust_app_session
*ua_sess
)
56 * Allocate the string ourself to make sure we never exceed
59 pathname
= zmalloc(LTTNG_PATH_MAX
);
64 /* Get correct path name destination */
65 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
66 /* Set application path to the destination path */
67 ret
= snprintf(pathname
, LTTNG_PATH_MAX
, "%s%s",
68 consumer
->domain_subdir
, ua_sess
->path
);
69 DBG3("Userspace local consumer trace path relative to current trace chunk: \"%s\"",
72 ret
= snprintf(pathname
, LTTNG_PATH_MAX
, "%s%s/%s%s",
73 consumer
->dst
.net
.base_dir
,
75 consumer
->domain_subdir
,
79 PERROR("Failed to format channel path");
81 } else if (ret
>= LTTNG_PATH_MAX
) {
82 ERR("Truncation occurred while formatting channel path");
95 * Send a single channel to the consumer using command ASK_CHANNEL_CREATION.
97 * Consumer socket lock MUST be acquired before calling this.
99 static int ask_channel_creation(struct ust_app_session
*ua_sess
,
100 struct ust_app_channel
*ua_chan
,
101 struct consumer_output
*consumer
,
102 struct consumer_socket
*socket
,
103 struct ust_registry_session
*registry
,
104 struct lttng_trace_chunk
*trace_chunk
)
108 uint64_t key
, chan_reg_key
;
109 char *pathname
= NULL
;
110 struct lttcomm_consumer_msg msg
;
111 struct ust_registry_channel
*chan_reg
;
112 char shm_path
[PATH_MAX
] = "";
113 char root_shm_path
[PATH_MAX
] = "";
122 DBG2("Asking UST consumer for channel");
124 is_local_trace
= consumer
->net_seq_index
== -1ULL;
125 /* Format the channel's path (relative to the current trace chunk). */
126 pathname
= setup_channel_trace_path(consumer
, ua_sess
);
132 if (is_local_trace
&& trace_chunk
) {
133 enum lttng_trace_chunk_status chunk_status
;
134 char *pathname_index
;
136 ret
= asprintf(&pathname_index
, "%s/" DEFAULT_INDEX_DIR
,
139 ERR("Failed to format channel index directory");
145 * Create the index subdirectory which will take care
146 * of implicitly creating the channel's path.
148 chunk_status
= lttng_trace_chunk_create_subdirectory(
149 trace_chunk
, pathname_index
);
150 free(pathname_index
);
151 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
157 /* Depending on the buffer type, a different channel key is used. */
158 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
159 chan_reg_key
= ua_chan
->tracing_channel_id
;
161 chan_reg_key
= ua_chan
->key
;
164 if (ua_chan
->attr
.type
== LTTNG_UST_CHAN_METADATA
) {
167 * Metadata channels shm_path (buffers) are handled within
168 * session daemon. Consumer daemon should not try to create
169 * those buffer files.
172 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
174 chan_id
= chan_reg
->chan_id
;
175 if (ua_sess
->shm_path
[0]) {
176 strncpy(shm_path
, ua_sess
->shm_path
, sizeof(shm_path
));
177 shm_path
[sizeof(shm_path
) - 1] = '\0';
178 strncat(shm_path
, "/",
179 sizeof(shm_path
) - strlen(shm_path
) - 1);
180 strncat(shm_path
, ua_chan
->name
,
181 sizeof(shm_path
) - strlen(shm_path
) - 1);
182 strncat(shm_path
, "_",
183 sizeof(shm_path
) - strlen(shm_path
) - 1);
185 strncpy(root_shm_path
, ua_sess
->root_shm_path
, sizeof(root_shm_path
));
186 root_shm_path
[sizeof(root_shm_path
) - 1] = '\0';
189 switch (ua_chan
->attr
.output
) {
192 output
= LTTNG_EVENT_MMAP
;
196 consumer_init_ask_channel_comm_msg(&msg
,
197 ua_chan
->attr
.subbuf_size
,
198 ua_chan
->attr
.num_subbuf
,
199 ua_chan
->attr
.overwrite
,
200 ua_chan
->attr
.switch_timer_interval
,
201 ua_chan
->attr
.read_timer_interval
,
202 ua_sess
->live_timer_interval
,
203 ua_chan
->monitor_timer_interval
,
205 (int) ua_chan
->attr
.type
,
209 consumer
->net_seq_index
,
213 ua_chan
->tracefile_size
,
214 ua_chan
->tracefile_count
,
216 ua_sess
->output_traces
,
217 ua_sess
->real_credentials
.uid
,
218 ua_chan
->attr
.blocking_timeout
,
219 root_shm_path
, shm_path
,
221 &ua_sess
->effective_credentials
);
223 health_code_update();
225 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
230 ret
= consumer_recv_status_channel(socket
, &key
,
231 &ua_chan
->expected_stream_count
);
235 /* Communication protocol error. */
236 assert(key
== ua_chan
->key
);
237 /* We need at least one where 1 stream for 1 cpu. */
238 if (ua_sess
->output_traces
) {
239 assert(ua_chan
->expected_stream_count
> 0);
242 DBG2("UST ask channel %" PRIu64
" successfully done with %u stream(s)", key
,
243 ua_chan
->expected_stream_count
);
247 health_code_update();
252 * Ask consumer to create a channel for a given session.
254 * Session list and rcu read side locks must be held by the caller.
256 * Returns 0 on success else a negative value.
258 int ust_consumer_ask_channel(struct ust_app_session
*ua_sess
,
259 struct ust_app_channel
*ua_chan
,
260 struct consumer_output
*consumer
,
261 struct consumer_socket
*socket
,
262 struct ust_registry_session
*registry
,
263 struct lttng_trace_chunk
* trace_chunk
)
273 if (!consumer
->enabled
) {
274 ret
= -LTTNG_ERR_NO_CONSUMER
;
275 DBG3("Consumer is disabled");
279 pthread_mutex_lock(socket
->lock
);
280 ret
= ask_channel_creation(ua_sess
, ua_chan
, consumer
, socket
, registry
,
282 pthread_mutex_unlock(socket
->lock
);
284 ERR("ask_channel_creation consumer command failed");
293 * Send a get channel command to consumer using the given channel key. The
294 * channel object is populated and the stream list.
296 * Return 0 on success else a negative value.
298 int ust_consumer_get_channel(struct consumer_socket
*socket
,
299 struct ust_app_channel
*ua_chan
)
302 struct lttcomm_consumer_msg msg
;
307 memset(&msg
, 0, sizeof(msg
));
308 msg
.cmd_type
= LTTNG_CONSUMER_GET_CHANNEL
;
309 msg
.u
.get_channel
.key
= ua_chan
->key
;
311 pthread_mutex_lock(socket
->lock
);
312 health_code_update();
314 /* Send command and wait for OK reply. */
315 ret
= consumer_send_msg(socket
, &msg
);
320 /* First, get the channel from consumer. */
321 ret
= ustctl_recv_channel_from_consumer(*socket
->fd_ptr
, &ua_chan
->obj
);
324 ERR("Error recv channel from consumer %d with ret %d",
325 *socket
->fd_ptr
, ret
);
327 DBG3("UST app recv channel from consumer. Consumer is dead.");
332 /* Next, get all streams. */
334 struct ust_app_stream
*stream
;
336 /* Create UST stream */
337 stream
= ust_app_alloc_stream();
338 if (stream
== NULL
) {
343 /* Stream object is populated by this call if successful. */
344 ret
= ustctl_recv_stream_from_consumer(*socket
->fd_ptr
, &stream
->obj
);
347 if (ret
== -LTTNG_UST_ERR_NOENT
) {
348 DBG3("UST app consumer has no more stream available");
352 ERR("Recv stream from consumer %d with ret %d",
353 *socket
->fd_ptr
, ret
);
355 DBG3("UST app recv stream from consumer. Consumer is dead.");
360 /* Order is important this is why a list is used. */
361 cds_list_add_tail(&stream
->list
, &ua_chan
->streams
.head
);
362 ua_chan
->streams
.count
++;
364 DBG2("UST app stream %d received successfully", ua_chan
->streams
.count
);
367 /* This MUST match or else we have a synchronization problem. */
368 assert(ua_chan
->expected_stream_count
== ua_chan
->streams
.count
);
370 /* Wait for confirmation that we can proceed with the streams. */
371 ret
= consumer_recv_status_reply(socket
);
377 health_code_update();
378 pthread_mutex_unlock(socket
->lock
);
383 * Send a destroy channel command to consumer using the given channel key.
385 * Note that this command MUST be used prior to a successful
386 * LTTNG_CONSUMER_GET_CHANNEL because once this command is done successfully,
387 * the streams are dispatched to the consumer threads and MUST be teardown
388 * through the hang up process.
390 * Return 0 on success else a negative value.
392 int ust_consumer_destroy_channel(struct consumer_socket
*socket
,
393 struct ust_app_channel
*ua_chan
)
396 struct lttcomm_consumer_msg msg
;
401 memset(&msg
, 0, sizeof(msg
));
402 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
403 msg
.u
.destroy_channel
.key
= ua_chan
->key
;
405 pthread_mutex_lock(socket
->lock
);
406 health_code_update();
408 ret
= consumer_send_msg(socket
, &msg
);
414 health_code_update();
415 pthread_mutex_unlock(socket
->lock
);
420 * Send a given stream to UST tracer.
422 * On success return 0 else a negative value.
424 int ust_consumer_send_stream_to_ust(struct ust_app
*app
,
425 struct ust_app_channel
*channel
, struct ust_app_stream
*stream
)
433 DBG2("UST consumer send stream to app %d", app
->sock
);
435 /* Relay stream to application. */
436 pthread_mutex_lock(&app
->sock_lock
);
437 ret
= ustctl_send_stream_to_ust(app
->sock
, channel
->obj
, stream
->obj
);
438 pthread_mutex_unlock(&app
->sock_lock
);
440 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
441 ERR("ustctl send stream handle %d to app pid: %d with ret %d",
442 stream
->obj
->handle
, app
->pid
, ret
);
444 DBG3("UST app send stream to ust failed. Application is dead.");
448 channel
->handle
= channel
->obj
->handle
;
455 * Send channel previously received from the consumer to the UST tracer.
457 * On success return 0 else a negative value.
459 int ust_consumer_send_channel_to_ust(struct ust_app
*app
,
460 struct ust_app_session
*ua_sess
, struct ust_app_channel
*channel
)
467 assert(channel
->obj
);
469 DBG2("UST app send channel to sock %d pid %d (name: %s, key: %" PRIu64
")",
470 app
->sock
, app
->pid
, channel
->name
, channel
->tracing_channel_id
);
472 /* Send stream to application. */
473 pthread_mutex_lock(&app
->sock_lock
);
474 ret
= ustctl_send_channel_to_ust(app
->sock
, ua_sess
->handle
, channel
->obj
);
475 pthread_mutex_unlock(&app
->sock_lock
);
477 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
478 ERR("Error ustctl send channel %s to app pid: %d with ret %d",
479 channel
->name
, app
->pid
, ret
);
481 DBG3("UST app send channel to ust failed. Application is dead.");
491 * Handle the metadata requests from the UST consumer
493 * Return 0 on success else a negative value.
495 int ust_consumer_metadata_request(struct consumer_socket
*socket
)
499 struct lttcomm_metadata_request_msg request
;
500 struct buffer_reg_uid
*reg_uid
;
501 struct ust_registry_session
*ust_reg
;
502 struct lttcomm_consumer_msg msg
;
507 health_code_update();
509 /* Wait for a metadata request */
510 pthread_mutex_lock(socket
->lock
);
511 ret
= consumer_socket_recv(socket
, &request
, sizeof(request
));
512 pthread_mutex_unlock(socket
->lock
);
517 DBG("Metadata request received for session %" PRIu64
", key %" PRIu64
,
518 request
.session_id
, request
.key
);
520 reg_uid
= buffer_reg_uid_find(request
.session_id
,
521 request
.bits_per_long
, request
.uid
);
523 ust_reg
= reg_uid
->registry
->reg
.ust
;
525 struct buffer_reg_pid
*reg_pid
=
526 buffer_reg_pid_find(request
.session_id_per_pid
);
528 DBG("PID registry not found for session id %" PRIu64
,
529 request
.session_id_per_pid
);
531 memset(&msg
, 0, sizeof(msg
));
532 msg
.cmd_type
= LTTNG_ERR_UND
;
533 pthread_mutex_lock(socket
->lock
);
534 (void) consumer_send_msg(socket
, &msg
);
535 pthread_mutex_unlock(socket
->lock
);
537 * This is possible since the session might have been destroyed
538 * during a consumer metadata request. So here, return gracefully
539 * because the destroy session will push the remaining metadata to
545 ust_reg
= reg_pid
->registry
->reg
.ust
;
549 pthread_mutex_lock(&ust_reg
->lock
);
550 ret_push
= ust_app_push_metadata(ust_reg
, socket
, 1);
551 pthread_mutex_unlock(&ust_reg
->lock
);
552 if (ret_push
== -EPIPE
) {
553 DBG("Application or relay closed while pushing metadata");
554 } else if (ret_push
< 0) {
555 ERR("Pushing metadata");
559 DBG("UST Consumer metadata pushed successfully");