SoW-2019-0002: Dynamic Snapshot
[lttng-tools.git] / src / bin / lttng-sessiond / ust-consumer.c
CommitLineData
48842b30 1/*
ab5be9fa 2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
48842b30 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
48842b30 5 *
48842b30
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
48842b30
DG
9#include <errno.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <unistd.h>
d88aee68 14#include <inttypes.h>
48842b30 15
990570ed 16#include <common/common.h>
c8fea79c 17#include <common/consumer/consumer.h>
990570ed 18#include <common/defaults.h>
48842b30 19
00e2e675 20#include "consumer.h"
8782cc74 21#include "health-sessiond.h"
48842b30 22#include "ust-consumer.h"
75018ab6 23#include "lttng-ust-error.h"
331744e3
JD
24#include "buffer-registry.h"
25#include "session.h"
e9404c27 26#include "lttng-sessiond.h"
48842b30 27
37278a1e 28/*
e9404c27 29 * Send a single channel to the consumer using command ASK_CHANNEL_CREATION.
ffe60014 30 *
7972aab2 31 * Consumer socket lock MUST be acquired before calling this.
37278a1e 32 */
ffe60014 33static int ask_channel_creation(struct ust_app_session *ua_sess,
e098433c
JG
34 struct ust_app_channel *ua_chan,
35 struct consumer_output *consumer,
36 struct consumer_socket *socket,
37 struct ust_registry_session *registry,
d2956687 38 struct lttng_trace_chunk *trace_chunk)
37278a1e 39{
0c759fc9 40 int ret, output;
7972aab2
DG
41 uint32_t chan_id;
42 uint64_t key, chan_reg_key;
ffe60014 43 char *pathname = NULL;
37278a1e 44 struct lttcomm_consumer_msg msg;
7972aab2 45 struct ust_registry_channel *chan_reg;
d7ba1388 46 char shm_path[PATH_MAX] = "";
3d071855 47 char root_shm_path[PATH_MAX] = "";
d2956687 48 bool is_local_trace;
5da88b0f 49 size_t consumer_path_offset = 0;
37278a1e 50
ffe60014
DG
51 assert(ua_sess);
52 assert(ua_chan);
53 assert(socket);
37278a1e 54 assert(consumer);
7972aab2 55 assert(registry);
ffe60014
DG
56
57 DBG2("Asking UST consumer for channel");
58
d2956687
JG
59 is_local_trace = consumer->net_seq_index == -1ULL;
60 /* Format the channel's path (relative to the current trace chunk). */
5da88b0f
MD
61 pathname = setup_channel_trace_path(consumer, ua_sess->path,
62 &consumer_path_offset);
d2956687
JG
63 if (!pathname) {
64 ret = -1;
65 goto error;
66 }
67
68 if (is_local_trace && trace_chunk) {
69 enum lttng_trace_chunk_status chunk_status;
70 char *pathname_index;
71
72 ret = asprintf(&pathname_index, "%s/" DEFAULT_INDEX_DIR,
73 pathname);
74 if (ret < 0) {
75 ERR("Failed to format channel index directory");
76 ret = -1;
77 goto error;
78 }
79
80 /*
81 * Create the index subdirectory which will take care
82 * of implicitly creating the channel's path.
83 */
84 chunk_status = lttng_trace_chunk_create_subdirectory(
85 trace_chunk, pathname_index);
86 free(pathname_index);
87 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
10a50311
JD
88 ret = -1;
89 goto error;
90 }
ffe60014
DG
91 }
92
7972aab2
DG
93 /* Depending on the buffer type, a different channel key is used. */
94 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
95 chan_reg_key = ua_chan->tracing_channel_id;
96 } else {
97 chan_reg_key = ua_chan->key;
98 }
99
100 if (ua_chan->attr.type == LTTNG_UST_CHAN_METADATA) {
101 chan_id = -1U;
d7ba1388
MD
102 /*
103 * Metadata channels shm_path (buffers) are handled within
104 * session daemon. Consumer daemon should not try to create
105 * those buffer files.
106 */
7972aab2
DG
107 } else {
108 chan_reg = ust_registry_channel_find(registry, chan_reg_key);
109 assert(chan_reg);
110 chan_id = chan_reg->chan_id;
d7ba1388
MD
111 if (ua_sess->shm_path[0]) {
112 strncpy(shm_path, ua_sess->shm_path, sizeof(shm_path));
113 shm_path[sizeof(shm_path) - 1] = '\0';
114 strncat(shm_path, "/",
115 sizeof(shm_path) - strlen(shm_path) - 1);
116 strncat(shm_path, ua_chan->name,
117 sizeof(shm_path) - strlen(shm_path) - 1);
118 strncat(shm_path, "_",
119 sizeof(shm_path) - strlen(shm_path) - 1);
120 }
3d071855
MD
121 strncpy(root_shm_path, ua_sess->root_shm_path, sizeof(root_shm_path));
122 root_shm_path[sizeof(root_shm_path) - 1] = '\0';
7972aab2
DG
123 }
124
0c759fc9
DG
125 switch (ua_chan->attr.output) {
126 case LTTNG_UST_MMAP:
127 default:
128 output = LTTNG_EVENT_MMAP;
129 break;
130 }
131
ffe60014
DG
132 consumer_init_ask_channel_comm_msg(&msg,
133 ua_chan->attr.subbuf_size,
134 ua_chan->attr.num_subbuf,
135 ua_chan->attr.overwrite,
136 ua_chan->attr.switch_timer_interval,
137 ua_chan->attr.read_timer_interval,
ecc48a90 138 ua_sess->live_timer_interval,
e9404c27 139 ua_chan->monitor_timer_interval,
0c759fc9 140 output,
ffe60014 141 (int) ua_chan->attr.type,
7972aab2 142 ua_sess->tracing_id,
5da88b0f 143 &pathname[consumer_path_offset],
ffe60014 144 ua_chan->name,
ffe60014
DG
145 consumer->net_seq_index,
146 ua_chan->key,
7972aab2 147 registry->uuid,
1624d5b7
JD
148 chan_id,
149 ua_chan->tracefile_size,
2bba9e53 150 ua_chan->tracefile_count,
1950109e 151 ua_sess->id,
567eb353 152 ua_sess->output_traces,
470cc211 153 ua_sess->real_credentials.uid,
491d1539 154 ua_chan->attr.blocking_timeout,
e098433c 155 root_shm_path, shm_path,
470cc211
JG
156 trace_chunk,
157 &ua_sess->effective_credentials);
37278a1e 158
840cb59c 159 health_code_update();
ca03de58 160
52898cb1 161 ret = consumer_socket_send(socket, &msg, sizeof(msg));
37278a1e
DG
162 if (ret < 0) {
163 goto error;
164 }
165
ffe60014
DG
166 ret = consumer_recv_status_channel(socket, &key,
167 &ua_chan->expected_stream_count);
168 if (ret < 0) {
169 goto error;
170 }
171 /* Communication protocol error. */
172 assert(key == ua_chan->key);
173 /* We need at least one where 1 stream for 1 cpu. */
10a50311
JD
174 if (ua_sess->output_traces) {
175 assert(ua_chan->expected_stream_count > 0);
176 }
ffe60014 177
d88aee68 178 DBG2("UST ask channel %" PRIu64 " successfully done with %u stream(s)", key,
ffe60014 179 ua_chan->expected_stream_count);
ca03de58 180
37278a1e 181error:
ffe60014
DG
182 free(pathname);
183 health_code_update();
37278a1e
DG
184 return ret;
185}
186
187/*
ffe60014
DG
188 * Ask consumer to create a channel for a given session.
189 *
e9404c27
JG
190 * Session list and rcu read side locks must be held by the caller.
191 *
ffe60014 192 * Returns 0 on success else a negative value.
37278a1e 193 */
ffe60014 194int ust_consumer_ask_channel(struct ust_app_session *ua_sess,
e098433c
JG
195 struct ust_app_channel *ua_chan,
196 struct consumer_output *consumer,
197 struct consumer_socket *socket,
198 struct ust_registry_session *registry,
d2956687 199 struct lttng_trace_chunk * trace_chunk)
37278a1e
DG
200{
201 int ret;
37278a1e 202
ffe60014
DG
203 assert(ua_sess);
204 assert(ua_chan);
205 assert(consumer);
206 assert(socket);
7972aab2 207 assert(registry);
f50f23d9 208
d9078d0c
DG
209 if (!consumer->enabled) {
210 ret = -LTTNG_ERR_NO_CONSUMER;
211 DBG3("Consumer is disabled");
212 goto error;
213 }
214
ffe60014 215 pthread_mutex_lock(socket->lock);
e098433c 216 ret = ask_channel_creation(ua_sess, ua_chan, consumer, socket, registry,
d2956687 217 trace_chunk);
2898de39 218 pthread_mutex_unlock(socket->lock);
37278a1e 219 if (ret < 0) {
e9404c27 220 ERR("ask_channel_creation consumer command failed");
37278a1e
DG
221 goto error;
222 }
223
48842b30
DG
224error:
225 return ret;
226}
227
228/*
ffe60014
DG
229 * Send a get channel command to consumer using the given channel key. The
230 * channel object is populated and the stream list.
231 *
232 * Return 0 on success else a negative value.
48842b30 233 */
ffe60014
DG
234int ust_consumer_get_channel(struct consumer_socket *socket,
235 struct ust_app_channel *ua_chan)
48842b30 236{
ffe60014 237 int ret;
37278a1e 238 struct lttcomm_consumer_msg msg;
48842b30 239
ffe60014
DG
240 assert(ua_chan);
241 assert(socket);
48842b30 242
53efb85a 243 memset(&msg, 0, sizeof(msg));
ffe60014
DG
244 msg.cmd_type = LTTNG_CONSUMER_GET_CHANNEL;
245 msg.u.get_channel.key = ua_chan->key;
37278a1e 246
ffe60014 247 pthread_mutex_lock(socket->lock);
840cb59c 248 health_code_update();
ca03de58 249
ffe60014
DG
250 /* Send command and wait for OK reply. */
251 ret = consumer_send_msg(socket, &msg);
37278a1e
DG
252 if (ret < 0) {
253 goto error;
254 }
255
ffe60014 256 /* First, get the channel from consumer. */
9363801e 257 ret = ustctl_recv_channel_from_consumer(*socket->fd_ptr, &ua_chan->obj);
37278a1e 258 if (ret < 0) {
ffe60014
DG
259 if (ret != -EPIPE) {
260 ERR("Error recv channel from consumer %d with ret %d",
9363801e 261 *socket->fd_ptr, ret);
ffe60014
DG
262 } else {
263 DBG3("UST app recv channel from consumer. Consumer is dead.");
264 }
37278a1e
DG
265 goto error;
266 }
00e2e675 267
ffe60014
DG
268 /* Next, get all streams. */
269 while (1) {
270 struct ust_app_stream *stream;
ca03de58 271
ffe60014
DG
272 /* Create UST stream */
273 stream = ust_app_alloc_stream();
274 if (stream == NULL) {
275 ret = -ENOMEM;
48842b30
DG
276 goto error;
277 }
278
ffe60014 279 /* Stream object is populated by this call if successful. */
9363801e 280 ret = ustctl_recv_stream_from_consumer(*socket->fd_ptr, &stream->obj);
37278a1e 281 if (ret < 0) {
ffe60014
DG
282 free(stream);
283 if (ret == -LTTNG_UST_ERR_NOENT) {
284 DBG3("UST app consumer has no more stream available");
ffe60014
DG
285 break;
286 }
287 if (ret != -EPIPE) {
288 ERR("Recv stream from consumer %d with ret %d",
9363801e 289 *socket->fd_ptr, ret);
ffe60014
DG
290 } else {
291 DBG3("UST app recv stream from consumer. Consumer is dead.");
00e2e675 292 }
48842b30
DG
293 goto error;
294 }
37278a1e 295
ffe60014
DG
296 /* Order is important this is why a list is used. */
297 cds_list_add_tail(&stream->list, &ua_chan->streams.head);
298 ua_chan->streams.count++;
37278a1e 299
5368d366 300 DBG2("UST app stream %d received successfully", ua_chan->streams.count);
ffe60014
DG
301 }
302
303 /* This MUST match or else we have a synchronization problem. */
304 assert(ua_chan->expected_stream_count == ua_chan->streams.count);
ca03de58 305
ffe60014
DG
306 /* Wait for confirmation that we can proceed with the streams. */
307 ret = consumer_recv_status_reply(socket);
37278a1e
DG
308 if (ret < 0) {
309 goto error;
310 }
311
312error:
ffe60014
DG
313 health_code_update();
314 pthread_mutex_unlock(socket->lock);
37278a1e
DG
315 return ret;
316}
317
318/*
ffe60014
DG
319 * Send a destroy channel command to consumer using the given channel key.
320 *
321 * Note that this command MUST be used prior to a successful
322 * LTTNG_CONSUMER_GET_CHANNEL because once this command is done successfully,
323 * the streams are dispatched to the consumer threads and MUST be teardown
324 * through the hang up process.
325 *
326 * Return 0 on success else a negative value.
37278a1e 327 */
ffe60014
DG
328int ust_consumer_destroy_channel(struct consumer_socket *socket,
329 struct ust_app_channel *ua_chan)
37278a1e 330{
ffe60014
DG
331 int ret;
332 struct lttcomm_consumer_msg msg;
a4b92340 333
ffe60014
DG
334 assert(ua_chan);
335 assert(socket);
37278a1e 336
53efb85a 337 memset(&msg, 0, sizeof(msg));
ffe60014
DG
338 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
339 msg.u.destroy_channel.key = ua_chan->key;
173af62f 340
ffe60014
DG
341 pthread_mutex_lock(socket->lock);
342 health_code_update();
37278a1e 343
ffe60014 344 ret = consumer_send_msg(socket, &msg);
37278a1e
DG
345 if (ret < 0) {
346 goto error;
48842b30
DG
347 }
348
ffe60014
DG
349error:
350 health_code_update();
351 pthread_mutex_unlock(socket->lock);
352 return ret;
353}
aeb96892 354
ffe60014
DG
355/*
356 * Send a given stream to UST tracer.
357 *
358 * On success return 0 else a negative value.
359 */
360int ust_consumer_send_stream_to_ust(struct ust_app *app,
361 struct ust_app_channel *channel, struct ust_app_stream *stream)
362{
363 int ret;
364
365 assert(app);
366 assert(stream);
367 assert(channel);
368
369 DBG2("UST consumer send stream to app %d", app->sock);
370
371 /* Relay stream to application. */
fb45065e 372 pthread_mutex_lock(&app->sock_lock);
ffe60014 373 ret = ustctl_send_stream_to_ust(app->sock, channel->obj, stream->obj);
fb45065e 374 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
375 if (ret < 0) {
376 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
8cf93def
DG
377 ERR("ustctl send stream handle %d to app pid: %d with ret %d",
378 stream->obj->handle, app->pid, ret);
ffe60014
DG
379 } else {
380 DBG3("UST app send stream to ust failed. Application is dead.");
48842b30 381 }
ffe60014 382 goto error;
48842b30 383 }
d0b96690 384 channel->handle = channel->obj->handle;
48842b30 385
ffe60014
DG
386error:
387 return ret;
388}
389
390/*
391 * Send channel previously received from the consumer to the UST tracer.
392 *
393 * On success return 0 else a negative value.
394 */
395int ust_consumer_send_channel_to_ust(struct ust_app *app,
396 struct ust_app_session *ua_sess, struct ust_app_channel *channel)
397{
398 int ret;
399
400 assert(app);
401 assert(ua_sess);
402 assert(channel);
403 assert(channel->obj);
404
7972aab2
DG
405 DBG2("UST app send channel to sock %d pid %d (name: %s, key: %" PRIu64 ")",
406 app->sock, app->pid, channel->name, channel->tracing_channel_id);
48842b30 407
ffe60014 408 /* Send stream to application. */
fb45065e 409 pthread_mutex_lock(&app->sock_lock);
ffe60014 410 ret = ustctl_send_channel_to_ust(app->sock, ua_sess->handle, channel->obj);
fb45065e 411 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
412 if (ret < 0) {
413 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
414 ERR("Error ustctl send channel %s to app pid: %d with ret %d",
415 channel->name, app->pid, ret);
416 } else {
417 DBG3("UST app send channel to ust failed. Application is dead.");
418 }
419 goto error;
420 }
48842b30
DG
421
422error:
423 return ret;
424}
331744e3
JD
425
426/*
427 * Handle the metadata requests from the UST consumer
428 *
429 * Return 0 on success else a negative value.
430 */
431int ust_consumer_metadata_request(struct consumer_socket *socket)
432{
433 int ret;
434 ssize_t ret_push;
435 struct lttcomm_metadata_request_msg request;
436 struct buffer_reg_uid *reg_uid;
437 struct ust_registry_session *ust_reg;
438 struct lttcomm_consumer_msg msg;
439
440 assert(socket);
441
442 rcu_read_lock();
331744e3
JD
443 health_code_update();
444
445 /* Wait for a metadata request */
dc2bbdae 446 pthread_mutex_lock(socket->lock);
52898cb1 447 ret = consumer_socket_recv(socket, &request, sizeof(request));
dc2bbdae 448 pthread_mutex_unlock(socket->lock);
52898cb1 449 if (ret < 0) {
331744e3
JD
450 goto end;
451 }
452
1950109e 453 DBG("Metadata request received for session %" PRIu64 ", key %" PRIu64,
331744e3
JD
454 request.session_id, request.key);
455
456 reg_uid = buffer_reg_uid_find(request.session_id,
457 request.bits_per_long, request.uid);
458 if (reg_uid) {
459 ust_reg = reg_uid->registry->reg.ust;
460 } else {
461 struct buffer_reg_pid *reg_pid =
1950109e 462 buffer_reg_pid_find(request.session_id_per_pid);
331744e3 463 if (!reg_pid) {
1950109e
JD
464 DBG("PID registry not found for session id %" PRIu64,
465 request.session_id_per_pid);
331744e3 466
53efb85a 467 memset(&msg, 0, sizeof(msg));
331744e3 468 msg.cmd_type = LTTNG_ERR_UND;
cb7d882c 469 pthread_mutex_lock(socket->lock);
331744e3 470 (void) consumer_send_msg(socket, &msg);
cb7d882c 471 pthread_mutex_unlock(socket->lock);
331744e3
JD
472 /*
473 * This is possible since the session might have been destroyed
474 * during a consumer metadata request. So here, return gracefully
475 * because the destroy session will push the remaining metadata to
476 * the consumer.
477 */
478 ret = 0;
479 goto end;
480 }
481 ust_reg = reg_pid->registry->reg.ust;
482 }
483 assert(ust_reg);
484
dc2bbdae 485 pthread_mutex_lock(&ust_reg->lock);
331744e3 486 ret_push = ust_app_push_metadata(ust_reg, socket, 1);
dc2bbdae 487 pthread_mutex_unlock(&ust_reg->lock);
2c57e06d
MD
488 if (ret_push == -EPIPE) {
489 DBG("Application or relay closed while pushing metadata");
490 } else if (ret_push < 0) {
331744e3
JD
491 ERR("Pushing metadata");
492 ret = -1;
493 goto end;
2c57e06d
MD
494 } else {
495 DBG("UST Consumer metadata pushed successfully");
331744e3 496 }
331744e3
JD
497 ret = 0;
498
499end:
331744e3
JD
500 rcu_read_unlock();
501 return ret;
502}
This page took 0.105153 seconds and 5 git commands to generate.