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 * Send a single channel to the consumer using command ASK_CHANNEL_CREATION.
40 * Consumer socket lock MUST be acquired before calling this.
42 static int ask_channel_creation(struct ust_app_session
*ua_sess
,
43 struct ust_app_channel
*ua_chan
,
44 struct consumer_output
*consumer
,
45 struct consumer_socket
*socket
,
46 struct ust_registry_session
*registry
,
47 struct lttng_trace_chunk
*trace_chunk
)
51 uint64_t key
, chan_reg_key
;
52 char *pathname
= NULL
;
53 struct lttcomm_consumer_msg msg
;
54 struct ust_registry_channel
*chan_reg
;
55 char shm_path
[PATH_MAX
] = "";
56 char root_shm_path
[PATH_MAX
] = "";
65 DBG2("Asking UST consumer for channel");
67 is_local_trace
= consumer
->net_seq_index
== -1ULL;
68 /* Format the channel's path (relative to the current trace chunk). */
69 pathname
= setup_channel_trace_path(consumer
, ua_sess
->path
);
75 if (is_local_trace
&& trace_chunk
) {
76 enum lttng_trace_chunk_status chunk_status
;
79 ret
= asprintf(&pathname_index
, "%s/" DEFAULT_INDEX_DIR
,
82 ERR("Failed to format channel index directory");
88 * Create the index subdirectory which will take care
89 * of implicitly creating the channel's path.
91 chunk_status
= lttng_trace_chunk_create_subdirectory(
92 trace_chunk
, pathname_index
);
94 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
100 /* Depending on the buffer type, a different channel key is used. */
101 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
102 chan_reg_key
= ua_chan
->tracing_channel_id
;
104 chan_reg_key
= ua_chan
->key
;
107 if (ua_chan
->attr
.type
== LTTNG_UST_CHAN_METADATA
) {
110 * Metadata channels shm_path (buffers) are handled within
111 * session daemon. Consumer daemon should not try to create
112 * those buffer files.
115 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
117 chan_id
= chan_reg
->chan_id
;
118 if (ua_sess
->shm_path
[0]) {
119 strncpy(shm_path
, ua_sess
->shm_path
, sizeof(shm_path
));
120 shm_path
[sizeof(shm_path
) - 1] = '\0';
121 strncat(shm_path
, "/",
122 sizeof(shm_path
) - strlen(shm_path
) - 1);
123 strncat(shm_path
, ua_chan
->name
,
124 sizeof(shm_path
) - strlen(shm_path
) - 1);
125 strncat(shm_path
, "_",
126 sizeof(shm_path
) - strlen(shm_path
) - 1);
128 strncpy(root_shm_path
, ua_sess
->root_shm_path
, sizeof(root_shm_path
));
129 root_shm_path
[sizeof(root_shm_path
) - 1] = '\0';
132 switch (ua_chan
->attr
.output
) {
135 output
= LTTNG_EVENT_MMAP
;
139 consumer_init_ask_channel_comm_msg(&msg
,
140 ua_chan
->attr
.subbuf_size
,
141 ua_chan
->attr
.num_subbuf
,
142 ua_chan
->attr
.overwrite
,
143 ua_chan
->attr
.switch_timer_interval
,
144 ua_chan
->attr
.read_timer_interval
,
145 ua_sess
->live_timer_interval
,
146 ua_chan
->monitor_timer_interval
,
148 (int) ua_chan
->attr
.type
,
152 consumer
->net_seq_index
,
156 ua_chan
->tracefile_size
,
157 ua_chan
->tracefile_count
,
159 ua_sess
->output_traces
,
160 ua_sess
->real_credentials
.uid
,
161 ua_chan
->attr
.blocking_timeout
,
162 root_shm_path
, shm_path
,
164 &ua_sess
->effective_credentials
);
166 health_code_update();
168 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
173 ret
= consumer_recv_status_channel(socket
, &key
,
174 &ua_chan
->expected_stream_count
);
178 /* Communication protocol error. */
179 assert(key
== ua_chan
->key
);
180 /* We need at least one where 1 stream for 1 cpu. */
181 if (ua_sess
->output_traces
) {
182 assert(ua_chan
->expected_stream_count
> 0);
185 DBG2("UST ask channel %" PRIu64
" successfully done with %u stream(s)", key
,
186 ua_chan
->expected_stream_count
);
190 health_code_update();
195 * Ask consumer to create a channel for a given session.
197 * Session list and rcu read side locks must be held by the caller.
199 * Returns 0 on success else a negative value.
201 int ust_consumer_ask_channel(struct ust_app_session
*ua_sess
,
202 struct ust_app_channel
*ua_chan
,
203 struct consumer_output
*consumer
,
204 struct consumer_socket
*socket
,
205 struct ust_registry_session
*registry
,
206 struct lttng_trace_chunk
* trace_chunk
)
216 if (!consumer
->enabled
) {
217 ret
= -LTTNG_ERR_NO_CONSUMER
;
218 DBG3("Consumer is disabled");
222 pthread_mutex_lock(socket
->lock
);
223 ret
= ask_channel_creation(ua_sess
, ua_chan
, consumer
, socket
, registry
,
225 pthread_mutex_unlock(socket
->lock
);
227 ERR("ask_channel_creation consumer command failed");
236 * Send a get channel command to consumer using the given channel key. The
237 * channel object is populated and the stream list.
239 * Return 0 on success else a negative value.
241 int ust_consumer_get_channel(struct consumer_socket
*socket
,
242 struct ust_app_channel
*ua_chan
)
245 struct lttcomm_consumer_msg msg
;
250 memset(&msg
, 0, sizeof(msg
));
251 msg
.cmd_type
= LTTNG_CONSUMER_GET_CHANNEL
;
252 msg
.u
.get_channel
.key
= ua_chan
->key
;
254 pthread_mutex_lock(socket
->lock
);
255 health_code_update();
257 /* Send command and wait for OK reply. */
258 ret
= consumer_send_msg(socket
, &msg
);
263 /* First, get the channel from consumer. */
264 ret
= ustctl_recv_channel_from_consumer(*socket
->fd_ptr
, &ua_chan
->obj
);
267 ERR("Error recv channel from consumer %d with ret %d",
268 *socket
->fd_ptr
, ret
);
270 DBG3("UST app recv channel from consumer. Consumer is dead.");
275 /* Next, get all streams. */
277 struct ust_app_stream
*stream
;
279 /* Create UST stream */
280 stream
= ust_app_alloc_stream();
281 if (stream
== NULL
) {
286 /* Stream object is populated by this call if successful. */
287 ret
= ustctl_recv_stream_from_consumer(*socket
->fd_ptr
, &stream
->obj
);
290 if (ret
== -LTTNG_UST_ERR_NOENT
) {
291 DBG3("UST app consumer has no more stream available");
295 ERR("Recv stream from consumer %d with ret %d",
296 *socket
->fd_ptr
, ret
);
298 DBG3("UST app recv stream from consumer. Consumer is dead.");
303 /* Order is important this is why a list is used. */
304 cds_list_add_tail(&stream
->list
, &ua_chan
->streams
.head
);
305 ua_chan
->streams
.count
++;
307 DBG2("UST app stream %d received successfully", ua_chan
->streams
.count
);
310 /* This MUST match or else we have a synchronization problem. */
311 assert(ua_chan
->expected_stream_count
== ua_chan
->streams
.count
);
313 /* Wait for confirmation that we can proceed with the streams. */
314 ret
= consumer_recv_status_reply(socket
);
320 health_code_update();
321 pthread_mutex_unlock(socket
->lock
);
326 * Send a destroy channel command to consumer using the given channel key.
328 * Note that this command MUST be used prior to a successful
329 * LTTNG_CONSUMER_GET_CHANNEL because once this command is done successfully,
330 * the streams are dispatched to the consumer threads and MUST be teardown
331 * through the hang up process.
333 * Return 0 on success else a negative value.
335 int ust_consumer_destroy_channel(struct consumer_socket
*socket
,
336 struct ust_app_channel
*ua_chan
)
339 struct lttcomm_consumer_msg msg
;
344 memset(&msg
, 0, sizeof(msg
));
345 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
346 msg
.u
.destroy_channel
.key
= ua_chan
->key
;
348 pthread_mutex_lock(socket
->lock
);
349 health_code_update();
351 ret
= consumer_send_msg(socket
, &msg
);
357 health_code_update();
358 pthread_mutex_unlock(socket
->lock
);
363 * Send a given stream to UST tracer.
365 * On success return 0 else a negative value.
367 int ust_consumer_send_stream_to_ust(struct ust_app
*app
,
368 struct ust_app_channel
*channel
, struct ust_app_stream
*stream
)
376 DBG2("UST consumer send stream to app %d", app
->sock
);
378 /* Relay stream to application. */
379 pthread_mutex_lock(&app
->sock_lock
);
380 ret
= ustctl_send_stream_to_ust(app
->sock
, channel
->obj
, stream
->obj
);
381 pthread_mutex_unlock(&app
->sock_lock
);
383 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
384 ERR("ustctl send stream handle %d to app pid: %d with ret %d",
385 stream
->obj
->handle
, app
->pid
, ret
);
387 DBG3("UST app send stream to ust failed. Application is dead.");
391 channel
->handle
= channel
->obj
->handle
;
398 * Send channel previously received from the consumer to the UST tracer.
400 * On success return 0 else a negative value.
402 int ust_consumer_send_channel_to_ust(struct ust_app
*app
,
403 struct ust_app_session
*ua_sess
, struct ust_app_channel
*channel
)
410 assert(channel
->obj
);
412 DBG2("UST app send channel to sock %d pid %d (name: %s, key: %" PRIu64
")",
413 app
->sock
, app
->pid
, channel
->name
, channel
->tracing_channel_id
);
415 /* Send stream to application. */
416 pthread_mutex_lock(&app
->sock_lock
);
417 ret
= ustctl_send_channel_to_ust(app
->sock
, ua_sess
->handle
, channel
->obj
);
418 pthread_mutex_unlock(&app
->sock_lock
);
420 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
421 ERR("Error ustctl send channel %s to app pid: %d with ret %d",
422 channel
->name
, app
->pid
, ret
);
424 DBG3("UST app send channel to ust failed. Application is dead.");
434 * Handle the metadata requests from the UST consumer
436 * Return 0 on success else a negative value.
438 int ust_consumer_metadata_request(struct consumer_socket
*socket
)
442 struct lttcomm_metadata_request_msg request
;
443 struct buffer_reg_uid
*reg_uid
;
444 struct ust_registry_session
*ust_reg
;
445 struct lttcomm_consumer_msg msg
;
450 health_code_update();
452 /* Wait for a metadata request */
453 pthread_mutex_lock(socket
->lock
);
454 ret
= consumer_socket_recv(socket
, &request
, sizeof(request
));
455 pthread_mutex_unlock(socket
->lock
);
460 DBG("Metadata request received for session %" PRIu64
", key %" PRIu64
,
461 request
.session_id
, request
.key
);
463 reg_uid
= buffer_reg_uid_find(request
.session_id
,
464 request
.bits_per_long
, request
.uid
);
466 ust_reg
= reg_uid
->registry
->reg
.ust
;
468 struct buffer_reg_pid
*reg_pid
=
469 buffer_reg_pid_find(request
.session_id_per_pid
);
471 DBG("PID registry not found for session id %" PRIu64
,
472 request
.session_id_per_pid
);
474 memset(&msg
, 0, sizeof(msg
));
475 msg
.cmd_type
= LTTNG_ERR_UND
;
476 pthread_mutex_lock(socket
->lock
);
477 (void) consumer_send_msg(socket
, &msg
);
478 pthread_mutex_unlock(socket
->lock
);
480 * This is possible since the session might have been destroyed
481 * during a consumer metadata request. So here, return gracefully
482 * because the destroy session will push the remaining metadata to
488 ust_reg
= reg_pid
->registry
->reg
.ust
;
492 pthread_mutex_lock(&ust_reg
->lock
);
493 ret_push
= ust_app_push_metadata(ust_reg
, socket
, 1);
494 pthread_mutex_unlock(&ust_reg
->lock
);
495 if (ret_push
== -EPIPE
) {
496 DBG("Application or relay closed while pushing metadata");
497 } else if (ret_push
< 0) {
498 ERR("Pushing metadata");
502 DBG("UST Consumer metadata pushed successfully");