Propagate trace format all the way to the consumer
[lttng-tools.git] / src / common / ust-consumer / ust-consumer.cpp
CommitLineData
3bd1e081 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa
MJ
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3bd1e081 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
3bd1e081 7 *
3bd1e081
MD
8 */
9
6c1c0768 10#define _LGPL_SOURCE
f02e1e8a 11#include <lttng/ust-ctl.h>
881fc67f 12#include <lttng/ust-sigbus.h>
3bd1e081
MD
13#include <poll.h>
14#include <pthread.h>
15#include <stdlib.h>
16#include <string.h>
17#include <sys/mman.h>
18#include <sys/socket.h>
dbb5dfe6 19#include <sys/stat.h>
3bd1e081 20#include <sys/types.h>
77c7c900 21#include <inttypes.h>
3bd1e081 22#include <unistd.h>
ffe60014 23#include <urcu/list.h>
331744e3 24#include <signal.h>
02d02e31 25#include <stdbool.h>
6f9449c2 26#include <stdint.h>
0857097f 27
c9e313bc
SM
28#include <bin/lttng-consumerd/health-consumerd.hpp>
29#include <common/common.hpp>
30#include <common/sessiond-comm/sessiond-comm.hpp>
31#include <common/relayd/relayd.hpp>
32#include <common/compat/fcntl.hpp>
33#include <common/compat/endian.hpp>
34#include <common/consumer/consumer-metadata-cache.hpp>
35#include <common/consumer/consumer-stream.hpp>
36#include <common/consumer/consumer-timer.hpp>
37#include <common/utils.hpp>
38#include <common/index/index.hpp>
39#include <common/consumer/consumer.hpp>
40#include <common/shm.hpp>
41#include <common/optional.hpp>
10a8a223 42
c9e313bc 43#include "ust-consumer.hpp"
3bd1e081 44
45863397 45#define INT_MAX_STR_LEN 12 /* includes \0 */
4628484a 46
fa29bfbf 47extern struct lttng_consumer_global_data the_consumer_data;
3bd1e081 48extern int consumer_poll_timeout;
3bd1e081 49
4bd69c5f 50LTTNG_EXPORT DEFINE_LTTNG_UST_SIGBUS_STATE();
881fc67f 51
3bd1e081 52/*
ffe60014 53 * Add channel to internal consumer state.
3bd1e081 54 *
ffe60014 55 * Returns 0 on success or else a negative value.
3bd1e081 56 */
ffe60014
DG
57static int add_channel(struct lttng_consumer_channel *channel,
58 struct lttng_consumer_local_data *ctx)
3bd1e081
MD
59{
60 int ret = 0;
61
a0377dfe
FD
62 LTTNG_ASSERT(channel);
63 LTTNG_ASSERT(ctx);
ffe60014
DG
64
65 if (ctx->on_recv_channel != NULL) {
66 ret = ctx->on_recv_channel(channel);
67 if (ret == 0) {
d8ef542d 68 ret = consumer_add_channel(channel, ctx);
ffe60014
DG
69 } else if (ret < 0) {
70 /* Most likely an ENOMEM. */
71 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
72 goto error;
73 }
74 } else {
d8ef542d 75 ret = consumer_add_channel(channel, ctx);
3bd1e081
MD
76 }
77
d88aee68 78 DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key);
ffe60014
DG
79
80error:
3bd1e081
MD
81 return ret;
82}
83
ffe60014
DG
84/*
85 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
86 * error value if applicable is set in it else it is kept untouched.
3bd1e081 87 *
ffe60014 88 * Return NULL on error else the newly allocated stream object.
3bd1e081 89 */
ffe60014
DG
90static struct lttng_consumer_stream *allocate_stream(int cpu, int key,
91 struct lttng_consumer_channel *channel,
d2956687 92 struct lttng_consumer_local_data *ctx, int *_alloc_ret)
ffe60014
DG
93{
94 int alloc_ret;
95 struct lttng_consumer_stream *stream = NULL;
96
a0377dfe
FD
97 LTTNG_ASSERT(channel);
98 LTTNG_ASSERT(ctx);
ffe60014 99
8696b40b
JR
100 stream = consumer_stream_create(channel, channel->key, key, channel->name,
101 channel->relayd_id, channel->session_id, channel->trace_chunk, cpu,
102 &alloc_ret, channel->type, channel->monitor, channel->trace_format);
ffe60014
DG
103 if (stream == NULL) {
104 switch (alloc_ret) {
105 case -ENOENT:
106 /*
107 * We could not find the channel. Can happen if cpu hotplug
108 * happens while tearing down.
109 */
110 DBG3("Could not find channel");
111 break;
112 case -ENOMEM:
113 case -EINVAL:
114 default:
115 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
116 break;
117 }
118 goto error;
119 }
120
d9a2e16e 121 consumer_stream_update_channel_attributes(stream, channel);
ffe60014
DG
122
123error:
124 if (_alloc_ret) {
125 *_alloc_ret = alloc_ret;
126 }
127 return stream;
128}
129
130/*
131 * Send the given stream pointer to the corresponding thread.
132 *
133 * Returns 0 on success else a negative value.
134 */
135static int send_stream_to_thread(struct lttng_consumer_stream *stream,
136 struct lttng_consumer_local_data *ctx)
137{
dae10966
DG
138 int ret;
139 struct lttng_pipe *stream_pipe;
ffe60014
DG
140
141 /* Get the right pipe where the stream will be sent. */
142 if (stream->metadata_flag) {
66d583dc 143 consumer_add_metadata_stream(stream);
dae10966 144 stream_pipe = ctx->consumer_metadata_pipe;
ffe60014 145 } else {
66d583dc 146 consumer_add_data_stream(stream);
dae10966 147 stream_pipe = ctx->consumer_data_pipe;
ffe60014
DG
148 }
149
5ab66908
MD
150 /*
151 * From this point on, the stream's ownership has been moved away from
a8086cf4
JR
152 * the channel and it becomes globally visible. Hence, remove it from
153 * the local stream list to prevent the stream from being both local and
154 * global.
5ab66908
MD
155 */
156 stream->globally_visible = 1;
5c5e3d71 157 cds_list_del_init(&stream->send_node);
5ab66908 158
dae10966 159 ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream));
ffe60014 160 if (ret < 0) {
dae10966
DG
161 ERR("Consumer write %s stream to pipe %d",
162 stream->metadata_flag ? "metadata" : "data",
163 lttng_pipe_get_writefd(stream_pipe));
5ab66908
MD
164 if (stream->metadata_flag) {
165 consumer_del_stream_for_metadata(stream);
166 } else {
167 consumer_del_stream_for_data(stream);
168 }
a8086cf4 169 goto error;
ffe60014 170 }
a8086cf4 171
5ab66908 172error:
ffe60014
DG
173 return ret;
174}
175
4628484a
MD
176static
177int get_stream_shm_path(char *stream_shm_path, const char *shm_path, int cpu)
178{
45863397 179 char cpu_nr[INT_MAX_STR_LEN]; /* int max len */
4628484a
MD
180 int ret;
181
182 strncpy(stream_shm_path, shm_path, PATH_MAX);
183 stream_shm_path[PATH_MAX - 1] = '\0';
45863397 184 ret = snprintf(cpu_nr, INT_MAX_STR_LEN, "%i", cpu);
67f8cb8d
MD
185 if (ret < 0) {
186 PERROR("snprintf");
4628484a
MD
187 goto end;
188 }
189 strncat(stream_shm_path, cpu_nr,
190 PATH_MAX - strlen(stream_shm_path) - 1);
191 ret = 0;
192end:
193 return ret;
194}
195
d88aee68
DG
196/*
197 * Create streams for the given channel using liblttng-ust-ctl.
d2956687 198 * The channel lock must be acquired by the caller.
d88aee68
DG
199 *
200 * Return 0 on success else a negative value.
201 */
ffe60014 202static int create_ust_streams(struct lttng_consumer_channel *channel,
d2956687 203 struct lttng_consumer_local_data *ctx)
ffe60014
DG
204{
205 int ret, cpu = 0;
b623cb6a 206 struct lttng_ust_ctl_consumer_stream *ustream;
ffe60014 207 struct lttng_consumer_stream *stream;
d2956687 208 pthread_mutex_t *current_stream_lock = NULL;
ffe60014 209
a0377dfe
FD
210 LTTNG_ASSERT(channel);
211 LTTNG_ASSERT(ctx);
ffe60014
DG
212
213 /*
214 * While a stream is available from ustctl. When NULL is returned, we've
215 * reached the end of the possible stream for the channel.
216 */
b623cb6a 217 while ((ustream = lttng_ust_ctl_create_stream(channel->uchan, cpu))) {
ffe60014 218 int wait_fd;
04ef1097 219 int ust_metadata_pipe[2];
ffe60014 220
9ce5646a
MD
221 health_code_update();
222
04ef1097
MD
223 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && channel->monitor) {
224 ret = utils_create_pipe_cloexec_nonblock(ust_metadata_pipe);
225 if (ret < 0) {
226 ERR("Create ust metadata poll pipe");
227 goto error;
228 }
229 wait_fd = ust_metadata_pipe[0];
230 } else {
b623cb6a 231 wait_fd = lttng_ust_ctl_stream_get_wait_fd(ustream);
04ef1097 232 }
ffe60014
DG
233
234 /* Allocate consumer stream object. */
d2956687 235 stream = allocate_stream(cpu, wait_fd, channel, ctx, &ret);
ffe60014
DG
236 if (!stream) {
237 goto error_alloc;
238 }
239 stream->ustream = ustream;
240 /*
241 * Store it so we can save multiple function calls afterwards since
242 * this value is used heavily in the stream threads. This is UST
243 * specific so this is why it's done after allocation.
244 */
245 stream->wait_fd = wait_fd;
246
b31398bb
DG
247 /*
248 * Increment channel refcount since the channel reference has now been
249 * assigned in the allocation process above.
250 */
10a50311
JD
251 if (stream->chan->monitor) {
252 uatomic_inc(&stream->chan->refcount);
253 }
b31398bb 254
d2956687
JG
255 pthread_mutex_lock(&stream->lock);
256 current_stream_lock = &stream->lock;
ffe60014
DG
257 /*
258 * Order is important this is why a list is used. On error, the caller
259 * should clean this list.
260 */
261 cds_list_add_tail(&stream->send_node, &channel->streams.head);
262
b623cb6a 263 ret = lttng_ust_ctl_get_max_subbuf_size(stream->ustream,
ffe60014
DG
264 &stream->max_sb_size);
265 if (ret < 0) {
b623cb6a 266 ERR("lttng_ust_ctl_get_max_subbuf_size failed for stream %s",
ffe60014
DG
267 stream->name);
268 goto error;
269 }
270
271 /* Do actions once stream has been received. */
272 if (ctx->on_recv_stream) {
273 ret = ctx->on_recv_stream(stream);
274 if (ret < 0) {
275 goto error;
276 }
277 }
278
d88aee68 279 DBG("UST consumer add stream %s (key: %" PRIu64 ") with relayd id %" PRIu64,
ffe60014
DG
280 stream->name, stream->key, stream->relayd_stream_id);
281
282 /* Set next CPU stream. */
283 channel->streams.count = ++cpu;
d88aee68
DG
284
285 /* Keep stream reference when creating metadata. */
286 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) {
287 channel->metadata_stream = stream;
8de4f941
JG
288 if (channel->monitor) {
289 /* Set metadata poll pipe if we created one */
290 memcpy(stream->ust_metadata_poll_pipe,
291 ust_metadata_pipe,
292 sizeof(ust_metadata_pipe));
293 }
d88aee68 294 }
d2956687
JG
295 pthread_mutex_unlock(&stream->lock);
296 current_stream_lock = NULL;
ffe60014
DG
297 }
298
299 return 0;
300
301error:
302error_alloc:
d2956687
JG
303 if (current_stream_lock) {
304 pthread_mutex_unlock(current_stream_lock);
305 }
ffe60014
DG
306 return ret;
307}
308
d2956687
JG
309static int open_ust_stream_fd(struct lttng_consumer_channel *channel, int cpu,
310 const struct lttng_credentials *session_credentials)
4628484a
MD
311{
312 char shm_path[PATH_MAX];
313 int ret;
314
315 if (!channel->shm_path[0]) {
b7fc068d 316 return shm_create_anonymous("ust-consumer");
4628484a
MD
317 }
318 ret = get_stream_shm_path(shm_path, channel->shm_path, cpu);
319 if (ret) {
320 goto error_shm_path;
321 }
322 return run_as_open(shm_path,
323 O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR,
ff588497
JR
324 lttng_credentials_get_uid(session_credentials),
325 lttng_credentials_get_gid(session_credentials));
4628484a
MD
326
327error_shm_path:
328 return -1;
329}
330
ffe60014
DG
331/*
332 * Create an UST channel with the given attributes and send it to the session
333 * daemon using the ust ctl API.
334 *
335 * Return 0 on success or else a negative value.
336 */
4628484a 337static int create_ust_channel(struct lttng_consumer_channel *channel,
b623cb6a
MJ
338 struct lttng_ust_ctl_consumer_channel_attr *attr,
339 struct lttng_ust_ctl_consumer_channel **ust_chanp)
ffe60014 340{
4628484a
MD
341 int ret, nr_stream_fds, i, j;
342 int *stream_fds;
b623cb6a 343 struct lttng_ust_ctl_consumer_channel *ust_channel;
ffe60014 344
a0377dfe
FD
345 LTTNG_ASSERT(channel);
346 LTTNG_ASSERT(attr);
347 LTTNG_ASSERT(ust_chanp);
348 LTTNG_ASSERT(channel->buffer_credentials.is_set);
ffe60014
DG
349
350 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
351 "subbuf_size: %" PRIu64 ", num_subbuf: %" PRIu64 ", "
352 "switch_timer_interval: %u, read_timer_interval: %u, "
353 "output: %d, type: %d", attr->overwrite, attr->subbuf_size,
354 attr->num_subbuf, attr->switch_timer_interval,
355 attr->read_timer_interval, attr->output, attr->type);
356
4628484a
MD
357 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA)
358 nr_stream_fds = 1;
359 else
b623cb6a 360 nr_stream_fds = lttng_ust_ctl_get_nr_stream_per_channel();
64803277 361 stream_fds = calloc<int>(nr_stream_fds);
4628484a
MD
362 if (!stream_fds) {
363 ret = -1;
364 goto error_alloc;
365 }
366 for (i = 0; i < nr_stream_fds; i++) {
d2956687
JG
367 stream_fds[i] = open_ust_stream_fd(channel, i,
368 &channel->buffer_credentials.value);
4628484a
MD
369 if (stream_fds[i] < 0) {
370 ret = -1;
371 goto error_open;
372 }
373 }
b623cb6a 374 ust_channel = lttng_ust_ctl_create_channel(attr, stream_fds, nr_stream_fds);
4628484a 375 if (!ust_channel) {
ffe60014
DG
376 ret = -1;
377 goto error_create;
378 }
4628484a
MD
379 channel->nr_stream_fds = nr_stream_fds;
380 channel->stream_fds = stream_fds;
381 *ust_chanp = ust_channel;
ffe60014
DG
382
383 return 0;
384
385error_create:
4628484a
MD
386error_open:
387 for (j = i - 1; j >= 0; j--) {
388 int closeret;
389
390 closeret = close(stream_fds[j]);
391 if (closeret) {
392 PERROR("close");
393 }
394 if (channel->shm_path[0]) {
395 char shm_path[PATH_MAX];
396
397 closeret = get_stream_shm_path(shm_path,
398 channel->shm_path, j);
399 if (closeret) {
400 ERR("Cannot get stream shm path");
401 }
402 closeret = run_as_unlink(shm_path,
ff588497
JR
403 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
404 channel->buffer_credentials)),
405 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
406 channel->buffer_credentials)));
4628484a 407 if (closeret) {
4628484a
MD
408 PERROR("unlink %s", shm_path);
409 }
410 }
411 }
412 /* Try to rmdir all directories under shm_path root. */
413 if (channel->root_shm_path[0]) {
602766ec 414 (void) run_as_rmdir_recursive(channel->root_shm_path,
ff588497
JR
415 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
416 channel->buffer_credentials)),
417 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
418 channel->buffer_credentials)),
f75c5439 419 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
4628484a
MD
420 }
421 free(stream_fds);
422error_alloc:
ffe60014
DG
423 return ret;
424}
425
d88aee68
DG
426/*
427 * Send a single given stream to the session daemon using the sock.
428 *
429 * Return 0 on success else a negative value.
430 */
ffe60014
DG
431static int send_sessiond_stream(int sock, struct lttng_consumer_stream *stream)
432{
433 int ret;
434
a0377dfe
FD
435 LTTNG_ASSERT(stream);
436 LTTNG_ASSERT(sock >= 0);
ffe60014 437
3eb914c0 438 DBG("UST consumer sending stream %" PRIu64 " to sessiond", stream->key);
ffe60014
DG
439
440 /* Send stream to session daemon. */
b623cb6a 441 ret = lttng_ust_ctl_send_stream_to_sessiond(sock, stream->ustream);
ffe60014
DG
442 if (ret < 0) {
443 goto error;
444 }
445
ffe60014
DG
446error:
447 return ret;
448}
449
450/*
a3a86f35 451 * Send channel to sessiond and relayd if applicable.
ffe60014 452 *
d88aee68 453 * Return 0 on success or else a negative value.
ffe60014 454 */
a3a86f35 455static int send_channel_to_sessiond_and_relayd(int sock,
ffe60014
DG
456 struct lttng_consumer_channel *channel,
457 struct lttng_consumer_local_data *ctx, int *relayd_error)
458{
0c759fc9 459 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
ffe60014 460 struct lttng_consumer_stream *stream;
a4baae1b 461 uint64_t net_seq_idx = -1ULL;
ffe60014 462
a0377dfe
FD
463 LTTNG_ASSERT(channel);
464 LTTNG_ASSERT(ctx);
465 LTTNG_ASSERT(sock >= 0);
ffe60014
DG
466
467 DBG("UST consumer sending channel %s to sessiond", channel->name);
468
62285ea4
DG
469 if (channel->relayd_id != (uint64_t) -1ULL) {
470 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
9ce5646a
MD
471
472 health_code_update();
473
62285ea4 474 /* Try to send the stream to the relayd if one is available. */
a3a86f35
JG
475 DBG("Sending stream %" PRIu64 " of channel \"%s\" to relayd",
476 stream->key, channel->name);
62285ea4
DG
477 ret = consumer_send_relayd_stream(stream, stream->chan->pathname);
478 if (ret < 0) {
479 /*
480 * Flag that the relayd was the problem here probably due to a
481 * communicaton error on the socket.
482 */
483 if (relayd_error) {
484 *relayd_error = 1;
485 }
725d28b2 486 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
ffe60014 487 }
a4baae1b
JD
488 if (net_seq_idx == -1ULL) {
489 net_seq_idx = stream->net_seq_idx;
490 }
491 }
f2a444f1 492 }
ffe60014 493
f2a444f1
DG
494 /* Inform sessiond that we are about to send channel and streams. */
495 ret = consumer_send_status_msg(sock, ret_code);
0c759fc9 496 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
f2a444f1
DG
497 /*
498 * Either the session daemon is not responding or the relayd died so we
499 * stop now.
500 */
501 goto error;
502 }
503
504 /* Send channel to sessiond. */
b623cb6a 505 ret = lttng_ust_ctl_send_channel_to_sessiond(sock, channel->uchan);
f2a444f1
DG
506 if (ret < 0) {
507 goto error;
508 }
509
b623cb6a 510 ret = lttng_ust_ctl_channel_close_wakeup_fd(channel->uchan);
f2a444f1
DG
511 if (ret < 0) {
512 goto error;
513 }
514
515 /* The channel was sent successfully to the sessiond at this point. */
516 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
9ce5646a
MD
517
518 health_code_update();
519
ffe60014
DG
520 /* Send stream to session daemon. */
521 ret = send_sessiond_stream(sock, stream);
522 if (ret < 0) {
523 goto error;
524 }
525 }
526
527 /* Tell sessiond there is no more stream. */
b623cb6a 528 ret = lttng_ust_ctl_send_stream_to_sessiond(sock, NULL);
ffe60014
DG
529 if (ret < 0) {
530 goto error;
531 }
532
533 DBG("UST consumer NULL stream sent to sessiond");
534
535 return 0;
536
537error:
0c759fc9 538 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
f2a444f1
DG
539 ret = -1;
540 }
ffe60014
DG
541 return ret;
542}
543
544/*
545 * Creates a channel and streams and add the channel it to the channel internal
546 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
547 * received.
548 *
549 * Return 0 on success or else, a negative value is returned and the channel
550 * MUST be destroyed by consumer_del_channel().
551 */
75cfe9e6 552static int ask_channel(struct lttng_consumer_local_data *ctx,
ffe60014 553 struct lttng_consumer_channel *channel,
b623cb6a 554 struct lttng_ust_ctl_consumer_channel_attr *attr)
3bd1e081
MD
555{
556 int ret;
557
a0377dfe
FD
558 LTTNG_ASSERT(ctx);
559 LTTNG_ASSERT(channel);
560 LTTNG_ASSERT(attr);
ffe60014
DG
561
562 /*
563 * This value is still used by the kernel consumer since for the kernel,
564 * the stream ownership is not IN the consumer so we need to have the
565 * number of left stream that needs to be initialized so we can know when
566 * to delete the channel (see consumer.c).
567 *
568 * As for the user space tracer now, the consumer creates and sends the
569 * stream to the session daemon which only sends them to the application
570 * once every stream of a channel is received making this value useless
571 * because we they will be added to the poll thread before the application
572 * receives them. This ensures that a stream can not hang up during
573 * initilization of a channel.
574 */
575 channel->nb_init_stream_left = 0;
576
577 /* The reply msg status is handled in the following call. */
4628484a 578 ret = create_ust_channel(channel, attr, &channel->uchan);
ffe60014 579 if (ret < 0) {
10a50311 580 goto end;
3bd1e081
MD
581 }
582
b623cb6a 583 channel->wait_fd = lttng_ust_ctl_channel_get_wait_fd(channel->uchan);
d8ef542d 584
10a50311
JD
585 /*
586 * For the snapshots (no monitor), we create the metadata streams
587 * on demand, not during the channel creation.
588 */
589 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && !channel->monitor) {
590 ret = 0;
591 goto end;
592 }
593
ffe60014 594 /* Open all streams for this channel. */
d2956687
JG
595 pthread_mutex_lock(&channel->lock);
596 ret = create_ust_streams(channel, ctx);
597 pthread_mutex_unlock(&channel->lock);
ffe60014 598 if (ret < 0) {
10a50311 599 goto end;
ffe60014
DG
600 }
601
10a50311 602end:
3bd1e081
MD
603 return ret;
604}
605
d88aee68
DG
606/*
607 * Send all stream of a channel to the right thread handling it.
608 *
609 * On error, return a negative value else 0 on success.
610 */
611static int send_streams_to_thread(struct lttng_consumer_channel *channel,
612 struct lttng_consumer_local_data *ctx)
613{
614 int ret = 0;
615 struct lttng_consumer_stream *stream, *stmp;
616
a0377dfe
FD
617 LTTNG_ASSERT(channel);
618 LTTNG_ASSERT(ctx);
d88aee68
DG
619
620 /* Send streams to the corresponding thread. */
621 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
622 send_node) {
9ce5646a
MD
623
624 health_code_update();
625
d88aee68
DG
626 /* Sending the stream to the thread. */
627 ret = send_stream_to_thread(stream, ctx);
628 if (ret < 0) {
629 /*
630 * If we are unable to send the stream to the thread, there is
631 * a big problem so just stop everything.
632 */
633 goto error;
634 }
d88aee68
DG
635 }
636
637error:
638 return ret;
639}
640
7972aab2
DG
641/*
642 * Flush channel's streams using the given key to retrieve the channel.
643 *
644 * Return 0 on success else an LTTng error code.
645 */
646static int flush_channel(uint64_t chan_key)
647{
648 int ret = 0;
649 struct lttng_consumer_channel *channel;
650 struct lttng_consumer_stream *stream;
651 struct lttng_ht *ht;
652 struct lttng_ht_iter iter;
653
8fd623e0 654 DBG("UST consumer flush channel key %" PRIu64, chan_key);
7972aab2 655
a500c257 656 rcu_read_lock();
7972aab2
DG
657 channel = consumer_find_channel(chan_key);
658 if (!channel) {
8fd623e0 659 ERR("UST consumer flush channel %" PRIu64 " not found", chan_key);
7972aab2
DG
660 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
661 goto error;
662 }
663
fa29bfbf 664 ht = the_consumer_data.stream_per_chan_id_ht;
7972aab2
DG
665
666 /* For each stream of the channel id, flush it. */
7972aab2
DG
667 cds_lfht_for_each_entry_duplicate(ht->ht,
668 ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct,
669 &channel->key, &iter.iter, stream, node_channel_id.node) {
9ce5646a
MD
670
671 health_code_update();
672
0dd01979 673 pthread_mutex_lock(&stream->lock);
5cfcab67
JR
674
675 /*
676 * Protect against concurrent teardown of a stream.
677 */
678 if (cds_lfht_is_node_deleted(&stream->node.node)) {
679 goto next;
680 }
681
0dd01979 682 if (!stream->quiescent) {
881fc67f
MD
683 ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0);
684 if (ret) {
685 ERR("Failed to flush buffer while flushing channel: channel key = %" PRIu64 ", channel name = '%s'",
686 chan_key, channel->name);
687 ret = LTTNG_ERR_BUFFER_FLUSH_FAILED;
688 pthread_mutex_unlock(&stream->lock);
689 goto error;
690 }
0dd01979
MD
691 stream->quiescent = true;
692 }
5cfcab67 693next:
0dd01979
MD
694 pthread_mutex_unlock(&stream->lock);
695 }
9cc4ae91
JG
696
697 /*
698 * Send one last buffer statistics update to the session daemon. This
699 * ensures that the session daemon gets at least one statistics update
700 * per channel even in the case of short-lived channels, such as when a
701 * short-lived app is traced in per-pid mode.
702 */
703 sample_and_send_channel_buffer_stats(channel);
0dd01979
MD
704error:
705 rcu_read_unlock();
706 return ret;
707}
708
709/*
710 * Clear quiescent state from channel's streams using the given key to
711 * retrieve the channel.
712 *
713 * Return 0 on success else an LTTng error code.
714 */
715static int clear_quiescent_channel(uint64_t chan_key)
716{
717 int ret = 0;
718 struct lttng_consumer_channel *channel;
719 struct lttng_consumer_stream *stream;
720 struct lttng_ht *ht;
721 struct lttng_ht_iter iter;
722
723 DBG("UST consumer clear quiescent channel key %" PRIu64, chan_key);
724
725 rcu_read_lock();
726 channel = consumer_find_channel(chan_key);
727 if (!channel) {
728 ERR("UST consumer clear quiescent channel %" PRIu64 " not found", chan_key);
729 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
730 goto error;
731 }
732
fa29bfbf 733 ht = the_consumer_data.stream_per_chan_id_ht;
0dd01979
MD
734
735 /* For each stream of the channel id, clear quiescent state. */
736 cds_lfht_for_each_entry_duplicate(ht->ht,
737 ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct,
738 &channel->key, &iter.iter, stream, node_channel_id.node) {
739
740 health_code_update();
741
742 pthread_mutex_lock(&stream->lock);
743 stream->quiescent = false;
744 pthread_mutex_unlock(&stream->lock);
7972aab2 745 }
7972aab2 746error:
a500c257 747 rcu_read_unlock();
7972aab2
DG
748 return ret;
749}
750
d88aee68
DG
751/*
752 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
753 *
754 * Return 0 on success else an LTTng error code.
755 */
756static int close_metadata(uint64_t chan_key)
757{
ea88ca2a 758 int ret = 0;
d88aee68 759 struct lttng_consumer_channel *channel;
f65a74be 760 unsigned int channel_monitor;
d88aee68 761
8fd623e0 762 DBG("UST consumer close metadata key %" PRIu64, chan_key);
d88aee68
DG
763
764 channel = consumer_find_channel(chan_key);
765 if (!channel) {
84cc9aa0
DG
766 /*
767 * This is possible if the metadata thread has issue a delete because
768 * the endpoint point of the stream hung up. There is no way the
769 * session daemon can know about it thus use a DBG instead of an actual
770 * error.
771 */
772 DBG("UST consumer close metadata %" PRIu64 " not found", chan_key);
d88aee68
DG
773 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
774 goto error;
775 }
776
fa29bfbf 777 pthread_mutex_lock(&the_consumer_data.lock);
a9838785 778 pthread_mutex_lock(&channel->lock);
f65a74be 779 channel_monitor = channel->monitor;
73811ecc
DG
780 if (cds_lfht_is_node_deleted(&channel->node.node)) {
781 goto error_unlock;
782 }
783
6d574024 784 lttng_ustconsumer_close_metadata(channel);
f65a74be 785 pthread_mutex_unlock(&channel->lock);
fa29bfbf 786 pthread_mutex_unlock(&the_consumer_data.lock);
d88aee68 787
f65a74be
JG
788 /*
789 * The ownership of a metadata channel depends on the type of
790 * session to which it belongs. In effect, the monitor flag is checked
791 * to determine if this metadata channel is in "snapshot" mode or not.
792 *
793 * In the non-snapshot case, the metadata channel is created along with
794 * a single stream which will remain present until the metadata channel
795 * is destroyed (on the destruction of its session). In this case, the
796 * metadata stream in "monitored" by the metadata poll thread and holds
797 * the ownership of its channel.
798 *
799 * Closing the metadata will cause the metadata stream's "metadata poll
800 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
801 * thread which will teardown the metadata stream which, in return,
802 * deletes the metadata channel.
803 *
804 * In the snapshot case, the metadata stream is created and destroyed
805 * on every snapshot record. Since the channel doesn't have an owner
806 * other than the session daemon, it is safe to destroy it immediately
807 * on reception of the CLOSE_METADATA command.
808 */
809 if (!channel_monitor) {
810 /*
811 * The channel and consumer_data locks must be
812 * released before this call since consumer_del_channel
813 * re-acquires the channel and consumer_data locks to teardown
814 * the channel and queue its reclamation by the "call_rcu"
815 * worker thread.
816 */
817 consumer_del_channel(channel);
818 }
819
820 return ret;
ea88ca2a 821error_unlock:
a9838785 822 pthread_mutex_unlock(&channel->lock);
fa29bfbf 823 pthread_mutex_unlock(&the_consumer_data.lock);
d88aee68
DG
824error:
825 return ret;
826}
827
828/*
829 * RCU read side lock MUST be acquired before calling this function.
830 *
831 * Return 0 on success else an LTTng error code.
832 */
833static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key)
834{
835 int ret;
836 struct lttng_consumer_channel *metadata;
837
48b7cdc2
FD
838 ASSERT_RCU_READ_LOCKED();
839
8fd623e0 840 DBG("UST consumer setup metadata key %" PRIu64, key);
d88aee68
DG
841
842 metadata = consumer_find_channel(key);
843 if (!metadata) {
844 ERR("UST consumer push metadata %" PRIu64 " not found", key);
845 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
10a50311
JD
846 goto end;
847 }
848
849 /*
850 * In no monitor mode, the metadata channel has no stream(s) so skip the
851 * ownership transfer to the metadata thread.
852 */
853 if (!metadata->monitor) {
854 DBG("Metadata channel in no monitor");
855 ret = 0;
856 goto end;
d88aee68
DG
857 }
858
859 /*
860 * Send metadata stream to relayd if one available. Availability is
861 * known if the stream is still in the list of the channel.
862 */
863 if (cds_list_empty(&metadata->streams.head)) {
864 ERR("Metadata channel key %" PRIu64 ", no stream available.", key);
865 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
f5a0c9cf 866 goto error_no_stream;
d88aee68
DG
867 }
868
869 /* Send metadata stream to relayd if needed. */
62285ea4
DG
870 if (metadata->metadata_stream->net_seq_idx != (uint64_t) -1ULL) {
871 ret = consumer_send_relayd_stream(metadata->metadata_stream,
872 metadata->pathname);
873 if (ret < 0) {
874 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
875 goto error;
876 }
601262d6
JD
877 ret = consumer_send_relayd_streams_sent(
878 metadata->metadata_stream->net_seq_idx);
879 if (ret < 0) {
880 ret = LTTCOMM_CONSUMERD_RELAYD_FAIL;
881 goto error;
882 }
d88aee68
DG
883 }
884
a8086cf4
JR
885 /*
886 * Ownership of metadata stream is passed along. Freeing is handled by
887 * the callee.
888 */
d88aee68
DG
889 ret = send_streams_to_thread(metadata, ctx);
890 if (ret < 0) {
891 /*
892 * If we are unable to send the stream to the thread, there is
893 * a big problem so just stop everything.
894 */
895 ret = LTTCOMM_CONSUMERD_FATAL;
a8086cf4 896 goto send_streams_error;
d88aee68
DG
897 }
898 /* List MUST be empty after or else it could be reused. */
a0377dfe 899 LTTNG_ASSERT(cds_list_empty(&metadata->streams.head));
d88aee68 900
10a50311
JD
901 ret = 0;
902 goto end;
d88aee68
DG
903
904error:
f2a444f1
DG
905 /*
906 * Delete metadata channel on error. At this point, the metadata stream can
907 * NOT be monitored by the metadata thread thus having the guarantee that
908 * the stream is still in the local stream list of the channel. This call
909 * will make sure to clean that list.
910 */
f5a0c9cf 911 consumer_stream_destroy(metadata->metadata_stream, NULL);
212d67a2 912 metadata->metadata_stream = NULL;
a8086cf4 913send_streams_error:
f5a0c9cf 914error_no_stream:
10a50311
JD
915end:
916 return ret;
917}
918
919/*
920 * Snapshot the whole metadata.
d2956687 921 * RCU read-side lock must be held by the caller.
10a50311
JD
922 *
923 * Returns 0 on success, < 0 on error
924 */
3eb928aa
MD
925static int snapshot_metadata(struct lttng_consumer_channel *metadata_channel,
926 uint64_t key, char *path, uint64_t relayd_id,
d2956687 927 struct lttng_consumer_local_data *ctx)
10a50311
JD
928{
929 int ret = 0;
10a50311
JD
930 struct lttng_consumer_stream *metadata_stream;
931
a0377dfe
FD
932 LTTNG_ASSERT(path);
933 LTTNG_ASSERT(ctx);
48b7cdc2 934 ASSERT_RCU_READ_LOCKED();
10a50311
JD
935
936 DBG("UST consumer snapshot metadata with key %" PRIu64 " at path %s",
937 key, path);
938
939 rcu_read_lock();
940
a0377dfe 941 LTTNG_ASSERT(!metadata_channel->monitor);
10a50311 942
9ce5646a
MD
943 health_code_update();
944
10a50311
JD
945 /*
946 * Ask the sessiond if we have new metadata waiting and update the
947 * consumer metadata cache.
948 */
94d49140 949 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0, 1);
10a50311
JD
950 if (ret < 0) {
951 goto error;
952 }
953
9ce5646a
MD
954 health_code_update();
955
10a50311
JD
956 /*
957 * The metadata stream is NOT created in no monitor mode when the channel
958 * is created on a sessiond ask channel command.
959 */
d2956687 960 ret = create_ust_streams(metadata_channel, ctx);
10a50311
JD
961 if (ret < 0) {
962 goto error;
963 }
964
965 metadata_stream = metadata_channel->metadata_stream;
a0377dfe 966 LTTNG_ASSERT(metadata_stream);
10a50311 967
947bd097 968 metadata_stream->read_subbuffer_ops.lock(metadata_stream);
10a50311
JD
969 if (relayd_id != (uint64_t) -1ULL) {
970 metadata_stream->net_seq_idx = relayd_id;
971 ret = consumer_send_relayd_stream(metadata_stream, path);
10a50311 972 } else {
d2956687
JG
973 ret = consumer_stream_create_output_files(metadata_stream,
974 false);
975 }
d2956687
JG
976 if (ret < 0) {
977 goto error_stream;
10a50311
JD
978 }
979
04ef1097 980 do {
9ce5646a 981 health_code_update();
6f9449c2 982 ret = lttng_consumer_read_subbuffer(metadata_stream, ctx, true);
10a50311 983 if (ret < 0) {
94d49140 984 goto error_stream;
10a50311 985 }
04ef1097 986 } while (ret > 0);
10a50311 987
10a50311 988error_stream:
947bd097 989 metadata_stream->read_subbuffer_ops.unlock(metadata_stream);
10a50311 990 /*
947bd097
JR
991 * Clean up the stream completely because the next snapshot will use a
992 * new metadata stream.
10a50311 993 */
10a50311
JD
994 consumer_stream_destroy(metadata_stream, NULL);
995 metadata_channel->metadata_stream = NULL;
996
997error:
998 rcu_read_unlock();
999 return ret;
1000}
1001
128708c3
JG
1002static
1003int get_current_subbuf_addr(struct lttng_consumer_stream *stream,
1004 const char **addr)
1005{
1006 int ret;
1007 unsigned long mmap_offset;
1008 const char *mmap_base;
1009
97535efa 1010 mmap_base = (const char *) lttng_ust_ctl_get_mmap_base(stream->ustream);
128708c3
JG
1011 if (!mmap_base) {
1012 ERR("Failed to get mmap base for stream `%s`",
1013 stream->name);
1014 ret = -EPERM;
1015 goto error;
1016 }
1017
b623cb6a 1018 ret = lttng_ust_ctl_get_mmap_read_offset(stream->ustream, &mmap_offset);
128708c3
JG
1019 if (ret != 0) {
1020 ERR("Failed to get mmap offset for stream `%s`", stream->name);
1021 ret = -EINVAL;
1022 goto error;
1023 }
1024
1025 *addr = mmap_base + mmap_offset;
1026error:
1027 return ret;
1028
1029}
1030
10a50311
JD
1031/*
1032 * Take a snapshot of all the stream of a channel.
d2956687 1033 * RCU read-side lock and the channel lock must be held by the caller.
10a50311
JD
1034 *
1035 * Returns 0 on success, < 0 on error
1036 */
3eb928aa
MD
1037static int snapshot_channel(struct lttng_consumer_channel *channel,
1038 uint64_t key, char *path, uint64_t relayd_id,
e098433c
JG
1039 uint64_t nb_packets_per_stream,
1040 struct lttng_consumer_local_data *ctx)
10a50311
JD
1041{
1042 int ret;
1043 unsigned use_relayd = 0;
1044 unsigned long consumed_pos, produced_pos;
10a50311
JD
1045 struct lttng_consumer_stream *stream;
1046
a0377dfe
FD
1047 LTTNG_ASSERT(path);
1048 LTTNG_ASSERT(ctx);
48b7cdc2 1049 ASSERT_RCU_READ_LOCKED();
10a50311
JD
1050
1051 rcu_read_lock();
1052
1053 if (relayd_id != (uint64_t) -1ULL) {
1054 use_relayd = 1;
1055 }
1056
a0377dfe 1057 LTTNG_ASSERT(!channel->monitor);
6a00837f 1058 DBG("UST consumer snapshot channel %" PRIu64, key);
10a50311
JD
1059
1060 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
9ce5646a
MD
1061 health_code_update();
1062
10a50311
JD
1063 /* Lock stream because we are about to change its state. */
1064 pthread_mutex_lock(&stream->lock);
a0377dfe 1065 LTTNG_ASSERT(channel->trace_chunk);
d2956687
JG
1066 if (!lttng_trace_chunk_get(channel->trace_chunk)) {
1067 /*
1068 * Can't happen barring an internal error as the channel
1069 * holds a reference to the trace chunk.
1070 */
1071 ERR("Failed to acquire reference to channel's trace chunk");
1072 ret = -1;
1073 goto error_unlock;
1074 }
a0377dfe 1075 LTTNG_ASSERT(!stream->trace_chunk);
d2956687
JG
1076 stream->trace_chunk = channel->trace_chunk;
1077
10a50311
JD
1078 stream->net_seq_idx = relayd_id;
1079
1080 if (use_relayd) {
1081 ret = consumer_send_relayd_stream(stream, path);
1082 if (ret < 0) {
1083 goto error_unlock;
1084 }
1085 } else {
d2956687
JG
1086 ret = consumer_stream_create_output_files(stream,
1087 false);
10a50311
JD
1088 if (ret < 0) {
1089 goto error_unlock;
1090 }
d2956687
JG
1091 DBG("UST consumer snapshot stream (%" PRIu64 ")",
1092 stream->key);
10a50311
JD
1093 }
1094
d4d80f77
MD
1095 /*
1096 * If tracing is active, we want to perform a "full" buffer flush.
1097 * Else, if quiescent, it has already been done by the prior stop.
1098 */
1099 if (!stream->quiescent) {
881fc67f
MD
1100 ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0);
1101 if (ret < 0) {
1102 ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64 ", channel name = '%s'",
1103 channel->key, channel->name);
1104 goto error_unlock;
1105 }
d4d80f77 1106 }
10a50311
JD
1107
1108 ret = lttng_ustconsumer_take_snapshot(stream);
1109 if (ret < 0) {
1110 ERR("Taking UST snapshot");
1111 goto error_unlock;
1112 }
1113
1114 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
1115 if (ret < 0) {
1116 ERR("Produced UST snapshot position");
1117 goto error_unlock;
1118 }
1119
1120 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
1121 if (ret < 0) {
1122 ERR("Consumerd UST snapshot position");
1123 goto error_unlock;
1124 }
1125
5c786ded
JD
1126 /*
1127 * The original value is sent back if max stream size is larger than
d07ceecd 1128 * the possible size of the snapshot. Also, we assume that the session
5c786ded
JD
1129 * daemon should never send a maximum stream size that is lower than
1130 * subbuffer size.
1131 */
d07ceecd
MD
1132 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
1133 produced_pos, nb_packets_per_stream,
1134 stream->max_sb_size);
5c786ded 1135
9377d830 1136 while ((long) (consumed_pos - produced_pos) < 0) {
10a50311
JD
1137 ssize_t read_len;
1138 unsigned long len, padded_len;
128708c3 1139 const char *subbuf_addr;
fd424d99 1140 struct lttng_buffer_view subbuf_view;
10a50311 1141
9ce5646a
MD
1142 health_code_update();
1143
10a50311
JD
1144 DBG("UST consumer taking snapshot at pos %lu", consumed_pos);
1145
b623cb6a 1146 ret = lttng_ust_ctl_get_subbuf(stream->ustream, &consumed_pos);
10a50311
JD
1147 if (ret < 0) {
1148 if (ret != -EAGAIN) {
b623cb6a 1149 PERROR("lttng_ust_ctl_get_subbuf snapshot");
10a50311
JD
1150 goto error_close_stream;
1151 }
1152 DBG("UST consumer get subbuf failed. Skipping it.");
1153 consumed_pos += stream->max_sb_size;
ddc93ee4 1154 stream->chan->lost_packets++;
10a50311
JD
1155 continue;
1156 }
1157
b623cb6a 1158 ret = lttng_ust_ctl_get_subbuf_size(stream->ustream, &len);
10a50311 1159 if (ret < 0) {
b623cb6a 1160 ERR("Snapshot lttng_ust_ctl_get_subbuf_size");
10a50311
JD
1161 goto error_put_subbuf;
1162 }
1163
b623cb6a 1164 ret = lttng_ust_ctl_get_padded_subbuf_size(stream->ustream, &padded_len);
10a50311 1165 if (ret < 0) {
b623cb6a 1166 ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size");
10a50311
JD
1167 goto error_put_subbuf;
1168 }
1169
128708c3
JG
1170 ret = get_current_subbuf_addr(stream, &subbuf_addr);
1171 if (ret) {
1172 goto error_put_subbuf;
1173 }
1174
fd424d99
JG
1175 subbuf_view = lttng_buffer_view_init(
1176 subbuf_addr, 0, padded_len);
f5ba75b4 1177 read_len = lttng_consumer_on_read_subbuffer_mmap(
6f9449c2 1178 stream, &subbuf_view, padded_len - len);
10a50311
JD
1179 if (use_relayd) {
1180 if (read_len != len) {
56591bac 1181 ret = -EPERM;
10a50311
JD
1182 goto error_put_subbuf;
1183 }
1184 } else {
1185 if (read_len != padded_len) {
56591bac 1186 ret = -EPERM;
10a50311
JD
1187 goto error_put_subbuf;
1188 }
1189 }
1190
b623cb6a 1191 ret = lttng_ust_ctl_put_subbuf(stream->ustream);
10a50311 1192 if (ret < 0) {
b623cb6a 1193 ERR("Snapshot lttng_ust_ctl_put_subbuf");
10a50311
JD
1194 goto error_close_stream;
1195 }
1196 consumed_pos += stream->max_sb_size;
1197 }
1198
1199 /* Simply close the stream so we can use it on the next snapshot. */
1200 consumer_stream_close(stream);
1201 pthread_mutex_unlock(&stream->lock);
1202 }
1203
1204 rcu_read_unlock();
1205 return 0;
1206
1207error_put_subbuf:
b623cb6a
MJ
1208 if (lttng_ust_ctl_put_subbuf(stream->ustream) < 0) {
1209 ERR("Snapshot lttng_ust_ctl_put_subbuf");
10a50311
JD
1210 }
1211error_close_stream:
1212 consumer_stream_close(stream);
1213error_unlock:
1214 pthread_mutex_unlock(&stream->lock);
10a50311 1215 rcu_read_unlock();
d88aee68
DG
1216 return ret;
1217}
1218
b1316da1
JG
1219static
1220void metadata_stream_reset_cache_consumed_position(
1221 struct lttng_consumer_stream *stream)
1222{
1223 ASSERT_LOCKED(stream->lock);
1224
1225 DBG("Reset metadata cache of session %" PRIu64,
1226 stream->chan->session_id);
1227 stream->ust_metadata_pushed = 0;
1228}
1229
331744e3 1230/*
c585821b
MD
1231 * Receive the metadata updates from the sessiond. Supports receiving
1232 * overlapping metadata, but is needs to always belong to a contiguous
1233 * range starting from 0.
1234 * Be careful about the locks held when calling this function: it needs
1235 * the metadata cache flush to concurrently progress in order to
1236 * complete.
331744e3
JD
1237 */
1238int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset,
93ec662e
JD
1239 uint64_t len, uint64_t version,
1240 struct lttng_consumer_channel *channel, int timer, int wait)
331744e3 1241{
0c759fc9 1242 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
331744e3 1243 char *metadata_str;
b1316da1 1244 enum consumer_metadata_cache_write_status cache_write_status;
331744e3 1245
8fd623e0 1246 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len);
331744e3 1247
64803277 1248 metadata_str = calloc<char>(len);
331744e3
JD
1249 if (!metadata_str) {
1250 PERROR("zmalloc metadata string");
1251 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
1252 goto end;
1253 }
1254
9ce5646a
MD
1255 health_code_update();
1256
331744e3
JD
1257 /* Receive metadata string. */
1258 ret = lttcomm_recv_unix_sock(sock, metadata_str, len);
1259 if (ret < 0) {
1260 /* Session daemon is dead so return gracefully. */
1261 ret_code = ret;
1262 goto end_free;
1263 }
1264
9ce5646a
MD
1265 health_code_update();
1266
331744e3 1267 pthread_mutex_lock(&channel->metadata_cache->lock);
b1316da1 1268 cache_write_status = consumer_metadata_cache_write(
a25d34bc
JG
1269 channel->metadata_cache, offset, len, version,
1270 metadata_str);
3bdc49f3 1271 pthread_mutex_unlock(&channel->metadata_cache->lock);
b1316da1
JG
1272 switch (cache_write_status) {
1273 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE:
1274 /*
1275 * The write entirely overlapped with existing contents of the
1276 * same metadata version (same content); there is nothing to do.
1277 */
1278 break;
1279 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED:
1280 /*
1281 * The metadata cache was invalidated (previously pushed
1282 * content has been overwritten). Reset the stream's consumed
1283 * metadata position to ensure the metadata poll thread consumes
1284 * the whole cache.
1285 */
947bd097
JR
1286
1287 /*
1288 * channel::metadata_stream can be null when the metadata
1289 * channel is under a snapshot session type. No need to update
1290 * the stream position in that scenario.
1291 */
1292 if (channel->metadata_stream != NULL) {
1293 pthread_mutex_lock(&channel->metadata_stream->lock);
1294 metadata_stream_reset_cache_consumed_position(
1295 channel->metadata_stream);
1296 pthread_mutex_unlock(&channel->metadata_stream->lock);
1297 } else {
1298 /* Validate we are in snapshot mode. */
1299 LTTNG_ASSERT(!channel->monitor);
1300 }
b1316da1
JG
1301 /* Fall-through. */
1302 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT:
1303 /*
1304 * In both cases, the metadata poll thread has new data to
1305 * consume.
1306 */
1307 ret = consumer_metadata_wakeup_pipe(channel);
1308 if (ret) {
1309 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1310 goto end_free;
1311 }
1312 break;
1313 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR:
331744e3
JD
1314 /* Unable to handle metadata. Notify session daemon. */
1315 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
a32bd775
DG
1316 /*
1317 * Skip metadata flush on write error since the offset and len might
1318 * not have been updated which could create an infinite loop below when
1319 * waiting for the metadata cache to be flushed.
1320 */
a32bd775 1321 goto end_free;
b1316da1
JG
1322 default:
1323 abort();
331744e3 1324 }
331744e3 1325
94d49140
JD
1326 if (!wait) {
1327 goto end_free;
1328 }
5e41ebe1 1329 while (consumer_metadata_cache_flushed(channel, offset + len, timer)) {
331744e3 1330 DBG("Waiting for metadata to be flushed");
9ce5646a
MD
1331
1332 health_code_update();
1333
331744e3
JD
1334 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME);
1335 }
1336
1337end_free:
1338 free(metadata_str);
1339end:
1340 return ret_code;
1341}
1342
4cbc1a04
DG
1343/*
1344 * Receive command from session daemon and process it.
1345 *
1346 * Return 1 on success else a negative value or 0.
1347 */
3bd1e081
MD
1348int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1349 int sock, struct pollfd *consumer_sockpoll)
1350{
594c7c00 1351 int ret_func;
0c759fc9 1352 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3bd1e081 1353 struct lttcomm_consumer_msg msg;
ffe60014 1354 struct lttng_consumer_channel *channel = NULL;
3bd1e081 1355
9ce5646a
MD
1356 health_code_update();
1357
594c7c00
SM
1358 {
1359 ssize_t ret_recv;
1360
1361 ret_recv = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1362 if (ret_recv != sizeof(msg)) {
1363 DBG("Consumer received unexpected message size %zd (expects %zu)",
1364 ret_recv, sizeof(msg));
1365 /*
1366 * The ret value might 0 meaning an orderly shutdown but this is ok
1367 * since the caller handles this.
1368 */
1369 if (ret_recv > 0) {
1370 lttng_consumer_send_error(ctx,
1371 LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1372 ret_recv = -1;
1373 }
1374 return ret_recv;
489f70e9 1375 }
3bd1e081 1376 }
9ce5646a
MD
1377
1378 health_code_update();
1379
84382d49 1380 /* deprecated */
a0377dfe 1381 LTTNG_ASSERT(msg.cmd_type != LTTNG_CONSUMER_STOP);
3bd1e081 1382
9ce5646a
MD
1383 health_code_update();
1384
3f8e211f 1385 /* relayd needs RCU read-side lock */
b0b335c8
MD
1386 rcu_read_lock();
1387
3bd1e081 1388 switch (msg.cmd_type) {
00e2e675
DG
1389 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
1390 {
4222116f
JR
1391 uint32_t major = msg.u.relayd_sock.major;
1392 uint32_t minor = msg.u.relayd_sock.minor;
1393 enum lttcomm_sock_proto protocol =
1394 (enum lttcomm_sock_proto) msg.u.relayd_sock
1395 .relayd_socket_protocol;
1396
f50f23d9 1397 /* Session daemon status message are handled in the following call. */
2527bf85 1398 consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
4222116f
JR
1399 msg.u.relayd_sock.type, ctx, sock,
1400 consumer_sockpoll, msg.u.relayd_sock.session_id,
1401 msg.u.relayd_sock.relayd_session_id, major,
1402 minor, protocol);
00e2e675
DG
1403 goto end_nosignal;
1404 }
173af62f
DG
1405 case LTTNG_CONSUMER_DESTROY_RELAYD:
1406 {
a6ba4fe1 1407 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
173af62f
DG
1408 struct consumer_relayd_sock_pair *relayd;
1409
a6ba4fe1 1410 DBG("UST consumer destroying relayd %" PRIu64, index);
173af62f
DG
1411
1412 /* Get relayd reference if exists. */
a6ba4fe1 1413 relayd = consumer_find_relayd(index);
173af62f 1414 if (relayd == NULL) {
3448e266 1415 DBG("Unable to find relayd %" PRIu64, index);
e462382a 1416 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
173af62f
DG
1417 }
1418
a6ba4fe1
DG
1419 /*
1420 * Each relayd socket pair has a refcount of stream attached to it
1421 * which tells if the relayd is still active or not depending on the
1422 * refcount value.
1423 *
1424 * This will set the destroy flag of the relayd object and destroy it
1425 * if the refcount reaches zero when called.
1426 *
1427 * The destroy can happen either here or when a stream fd hangs up.
1428 */
f50f23d9
DG
1429 if (relayd) {
1430 consumer_flag_relayd_for_destroy(relayd);
1431 }
1432
d88aee68 1433 goto end_msg_sessiond;
173af62f 1434 }
3bd1e081
MD
1435 case LTTNG_CONSUMER_UPDATE_STREAM:
1436 {
3f8e211f 1437 rcu_read_unlock();
7ad0a0cb 1438 return -ENOSYS;
3bd1e081 1439 }
6d805429 1440 case LTTNG_CONSUMER_DATA_PENDING:
53632229 1441 {
594c7c00
SM
1442 int is_data_pending;
1443 ssize_t ret_send;
6d805429 1444 uint64_t id = msg.u.data_pending.session_id;
ca22feea 1445
6d805429 1446 DBG("UST consumer data pending command for id %" PRIu64, id);
ca22feea 1447
3be74084 1448 is_data_pending = consumer_data_pending(id);
ca22feea
DG
1449
1450 /* Send back returned value to session daemon */
594c7c00 1451 ret_send = lttcomm_send_unix_sock(sock, &is_data_pending,
3be74084 1452 sizeof(is_data_pending));
594c7c00
SM
1453 if (ret_send < 0) {
1454 DBG("Error when sending the data pending ret code: %zd",
1455 ret_send);
489f70e9 1456 goto error_fatal;
ca22feea 1457 }
f50f23d9
DG
1458
1459 /*
1460 * No need to send back a status message since the data pending
1461 * returned value is the response.
1462 */
ca22feea 1463 break;
53632229 1464 }
ffe60014
DG
1465 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION:
1466 {
594c7c00 1467 int ret_ask_channel, ret_add_channel, ret_send;
b623cb6a 1468 struct lttng_ust_ctl_consumer_channel_attr attr;
d2956687
JG
1469 const uint64_t chunk_id = msg.u.ask_channel.chunk_id.value;
1470 const struct lttng_credentials buffer_credentials = {
ff588497
JR
1471 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.uid),
1472 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.gid),
d2956687 1473 };
ffe60014
DG
1474
1475 /* Create a plain object and reserve a channel key. */
8696b40b 1476 channel = consumer_allocate_channel(msg.u.ask_channel.key,
a2814ea7 1477 msg.u.ask_channel.session_id,
8696b40b
JR
1478 msg.u.ask_channel.chunk_id.is_set ? &chunk_id : NULL,
1479 msg.u.ask_channel.pathname, msg.u.ask_channel.name,
d2956687 1480 msg.u.ask_channel.relayd_id,
1624d5b7 1481 (enum lttng_event_output) msg.u.ask_channel.output,
8696b40b
JR
1482 msg.u.ask_channel.tracefile_size, msg.u.ask_channel.tracefile_count,
1483 msg.u.ask_channel.session_id_per_pid, msg.u.ask_channel.monitor,
1484 msg.u.ask_channel.live_timer_interval, msg.u.ask_channel.is_live,
1485 msg.u.ask_channel.root_shm_path, msg.u.ask_channel.shm_path,
1486 msg.u.ask_channel.trace_format);
ffe60014
DG
1487 if (!channel) {
1488 goto end_channel_error;
1489 }
1490
d2956687
JG
1491 LTTNG_OPTIONAL_SET(&channel->buffer_credentials,
1492 buffer_credentials);
1493
567eb353
DG
1494 /*
1495 * Assign UST application UID to the channel. This value is ignored for
1496 * per PID buffers. This is specific to UST thus setting this after the
1497 * allocation.
1498 */
1499 channel->ust_app_uid = msg.u.ask_channel.ust_app_uid;
1500
ffe60014
DG
1501 /* Build channel attributes from received message. */
1502 attr.subbuf_size = msg.u.ask_channel.subbuf_size;
1503 attr.num_subbuf = msg.u.ask_channel.num_subbuf;
1504 attr.overwrite = msg.u.ask_channel.overwrite;
1505 attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval;
1506 attr.read_timer_interval = msg.u.ask_channel.read_timer_interval;
7972aab2 1507 attr.chan_id = msg.u.ask_channel.chan_id;
ffe60014 1508 memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid));
491d1539 1509 attr.blocking_timeout= msg.u.ask_channel.blocking_timeout;
ffe60014 1510
0c759fc9
DG
1511 /* Match channel buffer type to the UST abi. */
1512 switch (msg.u.ask_channel.output) {
1513 case LTTNG_EVENT_MMAP:
1514 default:
fc4b93fa 1515 attr.output = LTTNG_UST_ABI_MMAP;
0c759fc9
DG
1516 break;
1517 }
1518
ffe60014
DG
1519 /* Translate and save channel type. */
1520 switch (msg.u.ask_channel.type) {
fc4b93fa 1521 case LTTNG_UST_ABI_CHAN_PER_CPU:
ffe60014 1522 channel->type = CONSUMER_CHANNEL_TYPE_DATA;
fc4b93fa 1523 attr.type = LTTNG_UST_ABI_CHAN_PER_CPU;
8633d6e3
MD
1524 /*
1525 * Set refcount to 1 for owner. Below, we will
1526 * pass ownership to the
1527 * consumer_thread_channel_poll() thread.
1528 */
1529 channel->refcount = 1;
ffe60014 1530 break;
fc4b93fa 1531 case LTTNG_UST_ABI_CHAN_METADATA:
ffe60014 1532 channel->type = CONSUMER_CHANNEL_TYPE_METADATA;
fc4b93fa 1533 attr.type = LTTNG_UST_ABI_CHAN_METADATA;
ffe60014
DG
1534 break;
1535 default:
a0377dfe 1536 abort();
ffe60014
DG
1537 goto error_fatal;
1538 };
1539
9ce5646a
MD
1540 health_code_update();
1541
594c7c00
SM
1542 ret_ask_channel = ask_channel(ctx, channel, &attr);
1543 if (ret_ask_channel < 0) {
ffe60014
DG
1544 goto end_channel_error;
1545 }
1546
fc4b93fa 1547 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
594c7c00
SM
1548 int ret_allocate;
1549
1550 ret_allocate = consumer_metadata_cache_allocate(
1551 channel);
1552 if (ret_allocate < 0) {
fc643247
MD
1553 ERR("Allocating metadata cache");
1554 goto end_channel_error;
1555 }
1556 consumer_timer_switch_start(channel, attr.switch_timer_interval);
1557 attr.switch_timer_interval = 0;
94d49140 1558 } else {
e9404c27
JG
1559 int monitor_start_ret;
1560
94d49140
JD
1561 consumer_timer_live_start(channel,
1562 msg.u.ask_channel.live_timer_interval);
e9404c27
JG
1563 monitor_start_ret = consumer_timer_monitor_start(
1564 channel,
1565 msg.u.ask_channel.monitor_timer_interval);
1566 if (monitor_start_ret < 0) {
1567 ERR("Starting channel monitoring timer failed");
1568 goto end_channel_error;
1569 }
fc643247
MD
1570 }
1571
9ce5646a
MD
1572 health_code_update();
1573
ffe60014
DG
1574 /*
1575 * Add the channel to the internal state AFTER all streams were created
1576 * and successfully sent to session daemon. This way, all streams must
1577 * be ready before this channel is visible to the threads.
fc643247
MD
1578 * If add_channel succeeds, ownership of the channel is
1579 * passed to consumer_thread_channel_poll().
ffe60014 1580 */
594c7c00
SM
1581 ret_add_channel = add_channel(channel, ctx);
1582 if (ret_add_channel < 0) {
fc4b93fa 1583 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
ea88ca2a
MD
1584 if (channel->switch_timer_enabled == 1) {
1585 consumer_timer_switch_stop(channel);
1586 }
1587 consumer_metadata_cache_destroy(channel);
1588 }
d3e2ba59
JD
1589 if (channel->live_timer_enabled == 1) {
1590 consumer_timer_live_stop(channel);
1591 }
e9404c27
JG
1592 if (channel->monitor_timer_enabled == 1) {
1593 consumer_timer_monitor_stop(channel);
1594 }
ffe60014
DG
1595 goto end_channel_error;
1596 }
1597
9ce5646a
MD
1598 health_code_update();
1599
ffe60014
DG
1600 /*
1601 * Channel and streams are now created. Inform the session daemon that
1602 * everything went well and should wait to receive the channel and
1603 * streams with ustctl API.
1604 */
594c7c00
SM
1605 ret_send = consumer_send_status_channel(sock, channel);
1606 if (ret_send < 0) {
ffe60014 1607 /*
489f70e9 1608 * There is probably a problem on the socket.
ffe60014 1609 */
489f70e9 1610 goto error_fatal;
ffe60014
DG
1611 }
1612
1613 break;
1614 }
1615 case LTTNG_CONSUMER_GET_CHANNEL:
1616 {
1617 int ret, relayd_err = 0;
d88aee68 1618 uint64_t key = msg.u.get_channel.key;
594c7c00 1619 struct lttng_consumer_channel *found_channel;
ffe60014 1620
594c7c00
SM
1621 found_channel = consumer_find_channel(key);
1622 if (!found_channel) {
8fd623e0 1623 ERR("UST consumer get channel key %" PRIu64 " not found", key);
e462382a 1624 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
f3a1fc7b 1625 goto end_get_channel;
ffe60014
DG
1626 }
1627
9ce5646a
MD
1628 health_code_update();
1629
a3a86f35 1630 /* Send the channel to sessiond (and relayd, if applicable). */
594c7c00
SM
1631 ret = send_channel_to_sessiond_and_relayd(
1632 sock, found_channel, ctx, &relayd_err);
ffe60014
DG
1633 if (ret < 0) {
1634 if (relayd_err) {
1635 /*
1636 * We were unable to send to the relayd the stream so avoid
1637 * sending back a fatal error to the thread since this is OK
f2a444f1
DG
1638 * and the consumer can continue its work. The above call
1639 * has sent the error status message to the sessiond.
ffe60014 1640 */
f3a1fc7b 1641 goto end_get_channel_nosignal;
ffe60014
DG
1642 }
1643 /*
1644 * The communicaton was broken hence there is a bad state between
1645 * the consumer and sessiond so stop everything.
1646 */
f3a1fc7b 1647 goto error_get_channel_fatal;
ffe60014
DG
1648 }
1649
9ce5646a
MD
1650 health_code_update();
1651
10a50311
JD
1652 /*
1653 * In no monitor mode, the streams ownership is kept inside the channel
1654 * so don't send them to the data thread.
1655 */
594c7c00 1656 if (!found_channel->monitor) {
f3a1fc7b 1657 goto end_get_channel;
10a50311
JD
1658 }
1659
594c7c00 1660 ret = send_streams_to_thread(found_channel, ctx);
d88aee68
DG
1661 if (ret < 0) {
1662 /*
1663 * If we are unable to send the stream to the thread, there is
1664 * a big problem so just stop everything.
1665 */
f3a1fc7b 1666 goto error_get_channel_fatal;
ffe60014 1667 }
ffe60014 1668 /* List MUST be empty after or else it could be reused. */
a0377dfe 1669 LTTNG_ASSERT(cds_list_empty(&found_channel->streams.head));
f3a1fc7b 1670end_get_channel:
d88aee68 1671 goto end_msg_sessiond;
f3a1fc7b
JG
1672error_get_channel_fatal:
1673 goto error_fatal;
1674end_get_channel_nosignal:
1675 goto end_nosignal;
d88aee68
DG
1676 }
1677 case LTTNG_CONSUMER_DESTROY_CHANNEL:
1678 {
1679 uint64_t key = msg.u.destroy_channel.key;
d88aee68 1680
a0cbdd2e
MD
1681 /*
1682 * Only called if streams have not been sent to stream
1683 * manager thread. However, channel has been sent to
1684 * channel manager thread.
1685 */
1686 notify_thread_del_channel(ctx, key);
d88aee68 1687 goto end_msg_sessiond;
ffe60014 1688 }
d88aee68
DG
1689 case LTTNG_CONSUMER_CLOSE_METADATA:
1690 {
1691 int ret;
1692
1693 ret = close_metadata(msg.u.close_metadata.key);
1694 if (ret != 0) {
97535efa 1695 ret_code = (lttcomm_return_code) ret;
d88aee68
DG
1696 }
1697
1698 goto end_msg_sessiond;
1699 }
7972aab2
DG
1700 case LTTNG_CONSUMER_FLUSH_CHANNEL:
1701 {
1702 int ret;
1703
1704 ret = flush_channel(msg.u.flush_channel.key);
1705 if (ret != 0) {
97535efa 1706 ret_code = (lttcomm_return_code) ret;
7972aab2
DG
1707 }
1708
1709 goto end_msg_sessiond;
1710 }
0dd01979
MD
1711 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL:
1712 {
1713 int ret;
1714
1715 ret = clear_quiescent_channel(
1716 msg.u.clear_quiescent_channel.key);
1717 if (ret != 0) {
97535efa 1718 ret_code = (lttcomm_return_code) ret;
0dd01979
MD
1719 }
1720
1721 goto end_msg_sessiond;
1722 }
d88aee68 1723 case LTTNG_CONSUMER_PUSH_METADATA:
ffe60014
DG
1724 {
1725 int ret;
d88aee68 1726 uint64_t len = msg.u.push_metadata.len;
d88aee68 1727 uint64_t key = msg.u.push_metadata.key;
331744e3 1728 uint64_t offset = msg.u.push_metadata.target_offset;
93ec662e 1729 uint64_t version = msg.u.push_metadata.version;
594c7c00 1730 struct lttng_consumer_channel *found_channel;
ffe60014 1731
8fd623e0
DG
1732 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key,
1733 len);
ffe60014 1734
594c7c00
SM
1735 found_channel = consumer_find_channel(key);
1736 if (!found_channel) {
000baf6a
DG
1737 /*
1738 * This is possible if the metadata creation on the consumer side
1739 * is in flight vis-a-vis a concurrent push metadata from the
1740 * session daemon. Simply return that the channel failed and the
1741 * session daemon will handle that message correctly considering
1742 * that this race is acceptable thus the DBG() statement here.
1743 */
1744 DBG("UST consumer push metadata %" PRIu64 " not found", key);
1745 ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
a8ffe244 1746 goto end_push_metadata_msg_sessiond;
d88aee68
DG
1747 }
1748
9ce5646a
MD
1749 health_code_update();
1750
c585821b
MD
1751 if (!len) {
1752 /*
1753 * There is nothing to receive. We have simply
1754 * checked whether the channel can be found.
1755 */
1756 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
a8ffe244 1757 goto end_push_metadata_msg_sessiond;
c585821b
MD
1758 }
1759
d88aee68 1760 /* Tell session daemon we are ready to receive the metadata. */
0c759fc9 1761 ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
ffe60014
DG
1762 if (ret < 0) {
1763 /* Somehow, the session daemon is not responding anymore. */
a8ffe244 1764 goto error_push_metadata_fatal;
d88aee68
DG
1765 }
1766
9ce5646a
MD
1767 health_code_update();
1768
d88aee68 1769 /* Wait for more data. */
9ce5646a
MD
1770 health_poll_entry();
1771 ret = lttng_consumer_poll_socket(consumer_sockpoll);
1772 health_poll_exit();
84382d49 1773 if (ret) {
a8ffe244 1774 goto error_push_metadata_fatal;
d88aee68
DG
1775 }
1776
9ce5646a
MD
1777 health_code_update();
1778
594c7c00
SM
1779 ret = lttng_ustconsumer_recv_metadata(sock, key, offset, len,
1780 version, found_channel, 0, 1);
d88aee68 1781 if (ret < 0) {
331744e3 1782 /* error receiving from sessiond */
a8ffe244 1783 goto error_push_metadata_fatal;
331744e3 1784 } else {
97535efa 1785 ret_code = (lttcomm_return_code) ret;
a8ffe244 1786 goto end_push_metadata_msg_sessiond;
d88aee68 1787 }
a8ffe244
JG
1788end_push_metadata_msg_sessiond:
1789 goto end_msg_sessiond;
1790error_push_metadata_fatal:
1791 goto error_fatal;
d88aee68
DG
1792 }
1793 case LTTNG_CONSUMER_SETUP_METADATA:
1794 {
1795 int ret;
1796
1797 ret = setup_metadata(ctx, msg.u.setup_metadata.key);
1798 if (ret) {
97535efa 1799 ret_code = (lttcomm_return_code) ret;
d88aee68
DG
1800 }
1801 goto end_msg_sessiond;
ffe60014 1802 }
6dc3064a
DG
1803 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
1804 {
594c7c00 1805 struct lttng_consumer_channel *found_channel;
3eb928aa 1806 uint64_t key = msg.u.snapshot_channel.key;
594c7c00 1807 int ret_send;
3eb928aa 1808
594c7c00
SM
1809 found_channel = consumer_find_channel(key);
1810 if (!found_channel) {
3eb928aa
MD
1811 DBG("UST snapshot channel not found for key %" PRIu64, key);
1812 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
10a50311 1813 } else {
3eb928aa 1814 if (msg.u.snapshot_channel.metadata) {
594c7c00
SM
1815 int ret_snapshot;
1816
1817 ret_snapshot = snapshot_metadata(found_channel,
1818 key,
3eb928aa
MD
1819 msg.u.snapshot_channel.pathname,
1820 msg.u.snapshot_channel.relayd_id,
d2956687 1821 ctx);
594c7c00 1822 if (ret_snapshot < 0) {
3eb928aa
MD
1823 ERR("Snapshot metadata failed");
1824 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1825 }
1826 } else {
594c7c00
SM
1827 int ret_snapshot;
1828
1829 ret_snapshot = snapshot_channel(found_channel,
1830 key,
3eb928aa
MD
1831 msg.u.snapshot_channel.pathname,
1832 msg.u.snapshot_channel.relayd_id,
594c7c00
SM
1833 msg.u.snapshot_channel
1834 .nb_packets_per_stream,
3eb928aa 1835 ctx);
594c7c00 1836 if (ret_snapshot < 0) {
3eb928aa
MD
1837 ERR("Snapshot channel failed");
1838 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1839 }
10a50311
JD
1840 }
1841 }
9ce5646a 1842 health_code_update();
594c7c00
SM
1843 ret_send = consumer_send_status_msg(sock, ret_code);
1844 if (ret_send < 0) {
6dc3064a
DG
1845 /* Somehow, the session daemon is not responding anymore. */
1846 goto end_nosignal;
1847 }
9ce5646a 1848 health_code_update();
6dc3064a
DG
1849 break;
1850 }
fb83fe64
JD
1851 case LTTNG_CONSUMER_DISCARDED_EVENTS:
1852 {
beb59458
MJ
1853 int ret = 0;
1854 uint64_t discarded_events;
fb83fe64
JD
1855 struct lttng_ht_iter iter;
1856 struct lttng_ht *ht;
1857 struct lttng_consumer_stream *stream;
1858 uint64_t id = msg.u.discarded_events.session_id;
1859 uint64_t key = msg.u.discarded_events.channel_key;
1860
1861 DBG("UST consumer discarded events command for session id %"
1862 PRIu64, id);
1863 rcu_read_lock();
fa29bfbf 1864 pthread_mutex_lock(&the_consumer_data.lock);
fb83fe64 1865
fa29bfbf 1866 ht = the_consumer_data.stream_list_ht;
fb83fe64
JD
1867
1868 /*
1869 * We only need a reference to the channel, but they are not
1870 * directly indexed, so we just use the first matching stream
1871 * to extract the information we need, we default to 0 if not
1872 * found (no events are dropped if the channel is not yet in
1873 * use).
1874 */
beb59458 1875 discarded_events = 0;
fb83fe64
JD
1876 cds_lfht_for_each_entry_duplicate(ht->ht,
1877 ht->hash_fct(&id, lttng_ht_seed),
1878 ht->match_fct, &id,
1879 &iter.iter, stream, node_session_id.node) {
1880 if (stream->chan->key == key) {
beb59458 1881 discarded_events = stream->chan->discarded_events;
fb83fe64
JD
1882 break;
1883 }
1884 }
fa29bfbf 1885 pthread_mutex_unlock(&the_consumer_data.lock);
fb83fe64
JD
1886 rcu_read_unlock();
1887
1888 DBG("UST consumer discarded events command for session id %"
1889 PRIu64 ", channel key %" PRIu64, id, key);
1890
1891 health_code_update();
1892
1893 /* Send back returned value to session daemon */
beb59458 1894 ret = lttcomm_send_unix_sock(sock, &discarded_events, sizeof(discarded_events));
fb83fe64
JD
1895 if (ret < 0) {
1896 PERROR("send discarded events");
1897 goto error_fatal;
1898 }
1899
1900 break;
1901 }
1902 case LTTNG_CONSUMER_LOST_PACKETS:
1903 {
9a06e8d4
JG
1904 int ret;
1905 uint64_t lost_packets;
fb83fe64
JD
1906 struct lttng_ht_iter iter;
1907 struct lttng_ht *ht;
1908 struct lttng_consumer_stream *stream;
1909 uint64_t id = msg.u.lost_packets.session_id;
1910 uint64_t key = msg.u.lost_packets.channel_key;
1911
1912 DBG("UST consumer lost packets command for session id %"
1913 PRIu64, id);
1914 rcu_read_lock();
fa29bfbf 1915 pthread_mutex_lock(&the_consumer_data.lock);
fb83fe64 1916
fa29bfbf 1917 ht = the_consumer_data.stream_list_ht;
fb83fe64
JD
1918
1919 /*
1920 * We only need a reference to the channel, but they are not
1921 * directly indexed, so we just use the first matching stream
1922 * to extract the information we need, we default to 0 if not
1923 * found (no packets lost if the channel is not yet in use).
1924 */
9a06e8d4 1925 lost_packets = 0;
fb83fe64
JD
1926 cds_lfht_for_each_entry_duplicate(ht->ht,
1927 ht->hash_fct(&id, lttng_ht_seed),
1928 ht->match_fct, &id,
1929 &iter.iter, stream, node_session_id.node) {
1930 if (stream->chan->key == key) {
9a06e8d4 1931 lost_packets = stream->chan->lost_packets;
fb83fe64
JD
1932 break;
1933 }
1934 }
fa29bfbf 1935 pthread_mutex_unlock(&the_consumer_data.lock);
fb83fe64
JD
1936 rcu_read_unlock();
1937
1938 DBG("UST consumer lost packets command for session id %"
1939 PRIu64 ", channel key %" PRIu64, id, key);
1940
1941 health_code_update();
1942
1943 /* Send back returned value to session daemon */
9a06e8d4
JG
1944 ret = lttcomm_send_unix_sock(sock, &lost_packets,
1945 sizeof(lost_packets));
fb83fe64
JD
1946 if (ret < 0) {
1947 PERROR("send lost packets");
1948 goto error_fatal;
1949 }
1950
1951 break;
1952 }
b3530820
JG
1953 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
1954 {
594c7c00
SM
1955 int channel_monitor_pipe, ret_send,
1956 ret_set_channel_monitor_pipe;
1957 ssize_t ret_recv;
b3530820
JG
1958
1959 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1960 /* Successfully received the command's type. */
594c7c00
SM
1961 ret_send = consumer_send_status_msg(sock, ret_code);
1962 if (ret_send < 0) {
b3530820
JG
1963 goto error_fatal;
1964 }
1965
594c7c00
SM
1966 ret_recv = lttcomm_recv_fds_unix_sock(
1967 sock, &channel_monitor_pipe, 1);
1968 if (ret_recv != sizeof(channel_monitor_pipe)) {
b3530820
JG
1969 ERR("Failed to receive channel monitor pipe");
1970 goto error_fatal;
1971 }
1972
1973 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe);
594c7c00
SM
1974 ret_set_channel_monitor_pipe =
1975 consumer_timer_thread_set_channel_monitor_pipe(
1976 channel_monitor_pipe);
1977 if (!ret_set_channel_monitor_pipe) {
b3530820 1978 int flags;
594c7c00 1979 int ret_fcntl;
b3530820
JG
1980
1981 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1982 /* Set the pipe as non-blocking. */
594c7c00
SM
1983 ret_fcntl = fcntl(channel_monitor_pipe, F_GETFL, 0);
1984 if (ret_fcntl == -1) {
b3530820
JG
1985 PERROR("fcntl get flags of the channel monitoring pipe");
1986 goto error_fatal;
1987 }
594c7c00 1988 flags = ret_fcntl;
b3530820 1989
594c7c00 1990 ret_fcntl = fcntl(channel_monitor_pipe, F_SETFL,
b3530820 1991 flags | O_NONBLOCK);
594c7c00 1992 if (ret_fcntl == -1) {
b3530820
JG
1993 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1994 goto error_fatal;
1995 }
1996 DBG("Channel monitor pipe set as non-blocking");
1997 } else {
1998 ret_code = LTTCOMM_CONSUMERD_ALREADY_SET;
1999 }
2000 goto end_msg_sessiond;
2001 }
b99a8d42
JD
2002 case LTTNG_CONSUMER_ROTATE_CHANNEL:
2003 {
594c7c00 2004 struct lttng_consumer_channel *found_channel;
92b7a7f8 2005 uint64_t key = msg.u.rotate_channel.key;
594c7c00 2006 int ret_send_status;
b99a8d42 2007
594c7c00
SM
2008 found_channel = consumer_find_channel(key);
2009 if (!found_channel) {
92b7a7f8
MD
2010 DBG("Channel %" PRIu64 " not found", key);
2011 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2012 } else {
594c7c00
SM
2013 int rotate_channel;
2014
92b7a7f8
MD
2015 /*
2016 * Sample the rotate position of all the streams in
2017 * this channel.
2018 */
594c7c00
SM
2019 rotate_channel = lttng_consumer_rotate_channel(
2020 found_channel, key,
f46376a1 2021 msg.u.rotate_channel.relayd_id);
594c7c00 2022 if (rotate_channel < 0) {
92b7a7f8
MD
2023 ERR("Rotate channel failed");
2024 ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL;
2025 }
b99a8d42 2026
92b7a7f8
MD
2027 health_code_update();
2028 }
594c7c00
SM
2029
2030 ret_send_status = consumer_send_status_msg(sock, ret_code);
2031 if (ret_send_status < 0) {
b99a8d42 2032 /* Somehow, the session daemon is not responding anymore. */
41c7a76d 2033 goto end_rotate_channel_nosignal;
b99a8d42
JD
2034 }
2035
2036 /*
2037 * Rotate the streams that are ready right now.
2038 * FIXME: this is a second consecutive iteration over the
2039 * streams in a channel, there is probably a better way to
2040 * handle this, but it needs to be after the
2041 * consumer_send_status_msg() call.
2042 */
594c7c00
SM
2043 if (found_channel) {
2044 int ret_rotate_read_streams;
2045
2046 ret_rotate_read_streams =
2047 lttng_consumer_rotate_ready_streams(
f46376a1 2048 found_channel, key);
594c7c00 2049 if (ret_rotate_read_streams < 0) {
92b7a7f8
MD
2050 ERR("Rotate channel failed");
2051 }
b99a8d42
JD
2052 }
2053 break;
41c7a76d
JG
2054end_rotate_channel_nosignal:
2055 goto end_nosignal;
b99a8d42 2056 }
5f3aff8b
MD
2057 case LTTNG_CONSUMER_CLEAR_CHANNEL:
2058 {
594c7c00 2059 struct lttng_consumer_channel *found_channel;
5f3aff8b 2060 uint64_t key = msg.u.clear_channel.key;
594c7c00 2061 int ret_send_status;
5f3aff8b 2062
594c7c00
SM
2063 found_channel = consumer_find_channel(key);
2064 if (!found_channel) {
5f3aff8b
MD
2065 DBG("Channel %" PRIu64 " not found", key);
2066 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2067 } else {
594c7c00
SM
2068 int ret_clear_channel;
2069
2070 ret_clear_channel = lttng_consumer_clear_channel(
2071 found_channel);
2072 if (ret_clear_channel) {
5f3aff8b 2073 ERR("Clear channel failed key %" PRIu64, key);
97535efa 2074 ret_code = (lttcomm_return_code) ret_clear_channel;
5f3aff8b
MD
2075 }
2076
2077 health_code_update();
2078 }
594c7c00
SM
2079 ret_send_status = consumer_send_status_msg(sock, ret_code);
2080 if (ret_send_status < 0) {
5f3aff8b
MD
2081 /* Somehow, the session daemon is not responding anymore. */
2082 goto end_nosignal;
2083 }
2084 break;
2085 }
d2956687 2086 case LTTNG_CONSUMER_INIT:
00fb02ac 2087 {
594c7c00 2088 int ret_send_status;
328c2fe7 2089 lttng_uuid sessiond_uuid;
594c7c00 2090
328c2fe7
JG
2091 std::copy(std::begin(msg.u.init.sessiond_uuid), std::end(msg.u.init.sessiond_uuid),
2092 sessiond_uuid.begin());
2093 ret_code = lttng_consumer_init_command(ctx, sessiond_uuid);
d88744a4 2094 health_code_update();
594c7c00
SM
2095 ret_send_status = consumer_send_status_msg(sock, ret_code);
2096 if (ret_send_status < 0) {
d88744a4
JD
2097 /* Somehow, the session daemon is not responding anymore. */
2098 goto end_nosignal;
2099 }
2100 break;
2101 }
d2956687 2102 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK:
d88744a4 2103 {
d2956687 2104 const struct lttng_credentials credentials = {
ff588497
JR
2105 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.uid),
2106 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.gid),
d2956687
JG
2107 };
2108 const bool is_local_trace =
2109 !msg.u.create_trace_chunk.relayd_id.is_set;
2110 const uint64_t relayd_id =
2111 msg.u.create_trace_chunk.relayd_id.value;
2112 const char *chunk_override_name =
2113 *msg.u.create_trace_chunk.override_name ?
2114 msg.u.create_trace_chunk.override_name :
2115 NULL;
cbf53d23 2116 struct lttng_directory_handle *chunk_directory_handle = NULL;
d88744a4 2117
d2956687
JG
2118 /*
2119 * The session daemon will only provide a chunk directory file
2120 * descriptor for local traces.
2121 */
2122 if (is_local_trace) {
2123 int chunk_dirfd;
594c7c00
SM
2124 int ret_send_status;
2125 ssize_t ret_recv;
19990ed5 2126
d2956687 2127 /* Acnowledge the reception of the command. */
594c7c00
SM
2128 ret_send_status = consumer_send_status_msg(
2129 sock, LTTCOMM_CONSUMERD_SUCCESS);
2130 if (ret_send_status < 0) {
d2956687
JG
2131 /* Somehow, the session daemon is not responding anymore. */
2132 goto end_nosignal;
2133 }
92816cc3 2134
5da88b0f
MD
2135 /*
2136 * Receive trace chunk domain dirfd.
2137 */
594c7c00
SM
2138 ret_recv = lttcomm_recv_fds_unix_sock(
2139 sock, &chunk_dirfd, 1);
2140 if (ret_recv != sizeof(chunk_dirfd)) {
5da88b0f 2141 ERR("Failed to receive trace chunk domain directory file descriptor");
d2956687
JG
2142 goto error_fatal;
2143 }
92816cc3 2144
5da88b0f 2145 DBG("Received trace chunk domain directory fd (%d)",
d2956687 2146 chunk_dirfd);
cbf53d23 2147 chunk_directory_handle = lttng_directory_handle_create_from_dirfd(
d2956687 2148 chunk_dirfd);
cbf53d23 2149 if (!chunk_directory_handle) {
5da88b0f 2150 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
d2956687
JG
2151 if (close(chunk_dirfd)) {
2152 PERROR("Failed to close chunk directory file descriptor");
2153 }
2154 goto error_fatal;
2155 }
92816cc3
JG
2156 }
2157
d2956687
JG
2158 ret_code = lttng_consumer_create_trace_chunk(
2159 !is_local_trace ? &relayd_id : NULL,
2160 msg.u.create_trace_chunk.session_id,
2161 msg.u.create_trace_chunk.chunk_id,
e5add6d0
JG
2162 (time_t) msg.u.create_trace_chunk
2163 .creation_timestamp,
d2956687 2164 chunk_override_name,
e5add6d0
JG
2165 msg.u.create_trace_chunk.credentials.is_set ?
2166 &credentials :
2167 NULL,
cbf53d23
JG
2168 chunk_directory_handle);
2169 lttng_directory_handle_put(chunk_directory_handle);
d2956687 2170 goto end_msg_sessiond;
00fb02ac 2171 }
d2956687 2172 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK:
a1ae2ea5 2173 {
bbc4768c 2174 enum lttng_trace_chunk_command_type close_command =
97535efa 2175 (lttng_trace_chunk_command_type)
bbc4768c 2176 msg.u.close_trace_chunk.close_command.value;
d2956687
JG
2177 const uint64_t relayd_id =
2178 msg.u.close_trace_chunk.relayd_id.value;
ecd1a12f 2179 struct lttcomm_consumer_close_trace_chunk_reply reply;
d00fb490 2180 char closed_trace_chunk_path[LTTNG_PATH_MAX] = {};
ecd1a12f 2181 int ret;
d2956687
JG
2182
2183 ret_code = lttng_consumer_close_trace_chunk(
2184 msg.u.close_trace_chunk.relayd_id.is_set ?
bbc4768c
JG
2185 &relayd_id :
2186 NULL,
d2956687
JG
2187 msg.u.close_trace_chunk.session_id,
2188 msg.u.close_trace_chunk.chunk_id,
bbc4768c
JG
2189 (time_t) msg.u.close_trace_chunk.close_timestamp,
2190 msg.u.close_trace_chunk.close_command.is_set ?
2191 &close_command :
ecd1a12f
MD
2192 NULL, closed_trace_chunk_path);
2193 reply.ret_code = ret_code;
2194 reply.path_length = strlen(closed_trace_chunk_path) + 1;
2195 ret = lttcomm_send_unix_sock(sock, &reply, sizeof(reply));
2196 if (ret != sizeof(reply)) {
2197 goto error_fatal;
2198 }
2199 ret = lttcomm_send_unix_sock(sock, closed_trace_chunk_path,
2200 reply.path_length);
2201 if (ret != reply.path_length) {
2202 goto error_fatal;
2203 }
2204 goto end_nosignal;
a1ae2ea5 2205 }
d2956687 2206 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS:
3654ed19 2207 {
d2956687
JG
2208 const uint64_t relayd_id =
2209 msg.u.trace_chunk_exists.relayd_id.value;
2210
2211 ret_code = lttng_consumer_trace_chunk_exists(
2212 msg.u.trace_chunk_exists.relayd_id.is_set ?
2213 &relayd_id : NULL,
2214 msg.u.trace_chunk_exists.session_id,
2215 msg.u.trace_chunk_exists.chunk_id);
2216 goto end_msg_sessiond;
04ed9e10
JG
2217 }
2218 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS:
2219 {
2220 const uint64_t key = msg.u.open_channel_packets.key;
594c7c00 2221 struct lttng_consumer_channel *found_channel =
04ed9e10
JG
2222 consumer_find_channel(key);
2223
594c7c00
SM
2224 if (found_channel) {
2225 pthread_mutex_lock(&found_channel->lock);
2226 ret_code = lttng_consumer_open_channel_packets(
2227 found_channel);
2228 pthread_mutex_unlock(&found_channel->lock);
04ed9e10
JG
2229 } else {
2230 /*
2231 * The channel could have disappeared in per-pid
2232 * buffering mode.
2233 */
2234 DBG("Channel %" PRIu64 " not found", key);
2235 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2236 }
2237
2238 health_code_update();
2239 goto end_msg_sessiond;
3654ed19 2240 }
3bd1e081
MD
2241 default:
2242 break;
2243 }
3f8e211f 2244
3bd1e081 2245end_nosignal:
4cbc1a04
DG
2246 /*
2247 * Return 1 to indicate success since the 0 value can be a socket
2248 * shutdown during the recv() or send() call.
2249 */
594c7c00 2250 ret_func = 1;
f3a1fc7b 2251 goto end;
ffe60014
DG
2252
2253end_msg_sessiond:
2254 /*
2255 * The returned value here is not useful since either way we'll return 1 to
2256 * the caller because the session daemon socket management is done
2257 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2258 */
594c7c00
SM
2259 {
2260 int ret_send_status;
2261
2262 ret_send_status = consumer_send_status_msg(sock, ret_code);
2263 if (ret_send_status < 0) {
2264 goto error_fatal;
2265 }
489f70e9 2266 }
594c7c00
SM
2267
2268 ret_func = 1;
f3a1fc7b 2269 goto end;
9ce5646a 2270
ffe60014
DG
2271end_channel_error:
2272 if (channel) {
a00932c8 2273 consumer_del_channel(channel);
ffe60014
DG
2274 }
2275 /* We have to send a status channel message indicating an error. */
594c7c00
SM
2276 {
2277 int ret_send_status;
2278
2279 ret_send_status = consumer_send_status_channel(sock, NULL);
2280 if (ret_send_status < 0) {
2281 /* Stop everything if session daemon can not be notified. */
2282 goto error_fatal;
2283 }
ffe60014 2284 }
594c7c00
SM
2285
2286 ret_func = 1;
f3a1fc7b 2287 goto end;
9ce5646a 2288
ffe60014 2289error_fatal:
ffe60014 2290 /* This will issue a consumer stop. */
594c7c00 2291 ret_func = -1;
f3a1fc7b
JG
2292 goto end;
2293
2294end:
2295 rcu_read_unlock();
2296 health_code_update();
594c7c00 2297 return ret_func;
3bd1e081
MD
2298}
2299
881fc67f
MD
2300int lttng_ust_flush_buffer(struct lttng_consumer_stream *stream,
2301 int producer_active)
fc6d7a51 2302{
a0377dfe
FD
2303 LTTNG_ASSERT(stream);
2304 LTTNG_ASSERT(stream->ustream);
fc6d7a51 2305
881fc67f 2306 return lttng_ust_ctl_flush_buffer(stream->ustream, producer_active);
fc6d7a51
JD
2307}
2308
ffe60014 2309/*
e9404c27 2310 * Take a snapshot for a specific stream.
ffe60014
DG
2311 *
2312 * Returns 0 on success, < 0 on error
2313 */
2314int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081 2315{
a0377dfe
FD
2316 LTTNG_ASSERT(stream);
2317 LTTNG_ASSERT(stream->ustream);
ffe60014 2318
b623cb6a 2319 return lttng_ust_ctl_snapshot(stream->ustream);
3bd1e081
MD
2320}
2321
e9404c27
JG
2322/*
2323 * Sample consumed and produced positions for a specific stream.
2324 *
2325 * Returns 0 on success, < 0 on error.
2326 */
2327int lttng_ustconsumer_sample_snapshot_positions(
2328 struct lttng_consumer_stream *stream)
2329{
a0377dfe
FD
2330 LTTNG_ASSERT(stream);
2331 LTTNG_ASSERT(stream->ustream);
e9404c27 2332
b623cb6a 2333 return lttng_ust_ctl_snapshot_sample_positions(stream->ustream);
e9404c27
JG
2334}
2335
ffe60014
DG
2336/*
2337 * Get the produced position
2338 *
2339 * Returns 0 on success, < 0 on error
2340 */
2341int lttng_ustconsumer_get_produced_snapshot(
2342 struct lttng_consumer_stream *stream, unsigned long *pos)
3bd1e081 2343{
a0377dfe
FD
2344 LTTNG_ASSERT(stream);
2345 LTTNG_ASSERT(stream->ustream);
2346 LTTNG_ASSERT(pos);
7a57cf92 2347
b623cb6a 2348 return lttng_ust_ctl_snapshot_get_produced(stream->ustream, pos);
ffe60014 2349}
7a57cf92 2350
10a50311
JD
2351/*
2352 * Get the consumed position
2353 *
2354 * Returns 0 on success, < 0 on error
2355 */
2356int lttng_ustconsumer_get_consumed_snapshot(
2357 struct lttng_consumer_stream *stream, unsigned long *pos)
2358{
a0377dfe
FD
2359 LTTNG_ASSERT(stream);
2360 LTTNG_ASSERT(stream->ustream);
2361 LTTNG_ASSERT(pos);
10a50311 2362
b623cb6a 2363 return lttng_ust_ctl_snapshot_get_consumed(stream->ustream, pos);
10a50311
JD
2364}
2365
881fc67f 2366int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream *stream,
84a182ce
DG
2367 int producer)
2368{
a0377dfe
FD
2369 LTTNG_ASSERT(stream);
2370 LTTNG_ASSERT(stream->ustream);
84a182ce 2371
881fc67f 2372 return lttng_ust_ctl_flush_buffer(stream->ustream, producer);
84a182ce
DG
2373}
2374
881fc67f 2375int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream *stream)
214f70e0 2376{
a0377dfe
FD
2377 LTTNG_ASSERT(stream);
2378 LTTNG_ASSERT(stream->ustream);
214f70e0 2379
881fc67f 2380 return lttng_ust_ctl_clear_buffer(stream->ustream);
214f70e0
JR
2381}
2382
84a182ce
DG
2383int lttng_ustconsumer_get_current_timestamp(
2384 struct lttng_consumer_stream *stream, uint64_t *ts)
2385{
a0377dfe
FD
2386 LTTNG_ASSERT(stream);
2387 LTTNG_ASSERT(stream->ustream);
2388 LTTNG_ASSERT(ts);
84a182ce 2389
b623cb6a 2390 return lttng_ust_ctl_get_current_timestamp(stream->ustream, ts);
84a182ce
DG
2391}
2392
fb83fe64
JD
2393int lttng_ustconsumer_get_sequence_number(
2394 struct lttng_consumer_stream *stream, uint64_t *seq)
2395{
a0377dfe
FD
2396 LTTNG_ASSERT(stream);
2397 LTTNG_ASSERT(stream->ustream);
2398 LTTNG_ASSERT(seq);
fb83fe64 2399
b623cb6a 2400 return lttng_ust_ctl_get_sequence_number(stream->ustream, seq);
fb83fe64
JD
2401}
2402
ffe60014 2403/*
0dd01979 2404 * Called when the stream signals the consumer that it has hung up.
ffe60014
DG
2405 */
2406void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
2407{
a0377dfe
FD
2408 LTTNG_ASSERT(stream);
2409 LTTNG_ASSERT(stream->ustream);
2c1dd183 2410
0dd01979
MD
2411 pthread_mutex_lock(&stream->lock);
2412 if (!stream->quiescent) {
881fc67f
MD
2413 if (lttng_ust_ctl_flush_buffer(stream->ustream, 0) < 0) {
2414 ERR("Failed to flush buffer on stream hang-up");
2415 } else {
2416 stream->quiescent = true;
2417 }
0dd01979 2418 }
d9ab8c66 2419
ffe60014 2420 stream->hangup_flush_done = 1;
d9ab8c66 2421 pthread_mutex_unlock(&stream->lock);
ffe60014 2422}
ee77a7b0 2423
ffe60014
DG
2424void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
2425{
4628484a
MD
2426 int i;
2427
a0377dfe
FD
2428 LTTNG_ASSERT(chan);
2429 LTTNG_ASSERT(chan->uchan);
2430 LTTNG_ASSERT(chan->buffer_credentials.is_set);
e316aad5 2431
ea88ca2a
MD
2432 if (chan->switch_timer_enabled == 1) {
2433 consumer_timer_switch_stop(chan);
2434 }
4628484a
MD
2435 for (i = 0; i < chan->nr_stream_fds; i++) {
2436 int ret;
2437
2438 ret = close(chan->stream_fds[i]);
2439 if (ret) {
2440 PERROR("close");
2441 }
2442 if (chan->shm_path[0]) {
2443 char shm_path[PATH_MAX];
2444
2445 ret = get_stream_shm_path(shm_path, chan->shm_path, i);
2446 if (ret) {
2447 ERR("Cannot get stream shm path");
2448 }
d2956687 2449 ret = run_as_unlink(shm_path,
ff588497
JR
2450 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2451 chan->buffer_credentials)),
2452 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2453 chan->buffer_credentials)));
4628484a 2454 if (ret) {
4628484a
MD
2455 PERROR("unlink %s", shm_path);
2456 }
2457 }
2458 }
3bd1e081
MD
2459}
2460
b83e03c4
MD
2461void lttng_ustconsumer_free_channel(struct lttng_consumer_channel *chan)
2462{
a0377dfe
FD
2463 LTTNG_ASSERT(chan);
2464 LTTNG_ASSERT(chan->uchan);
2465 LTTNG_ASSERT(chan->buffer_credentials.is_set);
b83e03c4
MD
2466
2467 consumer_metadata_cache_destroy(chan);
b623cb6a 2468 lttng_ust_ctl_destroy_channel(chan->uchan);
ea853771
JR
2469 /* Try to rmdir all directories under shm_path root. */
2470 if (chan->root_shm_path[0]) {
602766ec 2471 (void) run_as_rmdir_recursive(chan->root_shm_path,
ff588497
JR
2472 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2473 chan->buffer_credentials)),
2474 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2475 chan->buffer_credentials)),
f75c5439 2476 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
ea853771 2477 }
b83e03c4
MD
2478 free(chan->stream_fds);
2479}
2480
3bd1e081
MD
2481void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
2482{
a0377dfe
FD
2483 LTTNG_ASSERT(stream);
2484 LTTNG_ASSERT(stream->ustream);
d41f73b7 2485
ea88ca2a
MD
2486 if (stream->chan->switch_timer_enabled == 1) {
2487 consumer_timer_switch_stop(stream->chan);
2488 }
b623cb6a 2489 lttng_ust_ctl_destroy_stream(stream->ustream);
ffe60014 2490}
d41f73b7 2491
6d574024
DG
2492int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream *stream)
2493{
a0377dfe
FD
2494 LTTNG_ASSERT(stream);
2495 LTTNG_ASSERT(stream->ustream);
6d574024 2496
b623cb6a 2497 return lttng_ust_ctl_stream_get_wakeup_fd(stream->ustream);
6d574024
DG
2498}
2499
2500int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream *stream)
2501{
a0377dfe
FD
2502 LTTNG_ASSERT(stream);
2503 LTTNG_ASSERT(stream->ustream);
6d574024 2504
b623cb6a 2505 return lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream);
6d574024
DG
2506}
2507
94d49140
JD
2508/*
2509 * Write up to one packet from the metadata cache to the channel.
2510 *
577eea73
JG
2511 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2512 * negative value on error.
94d49140
JD
2513 */
2514static
2515int commit_one_metadata_packet(struct lttng_consumer_stream *stream)
2516{
2517 ssize_t write_len;
2518 int ret;
2519
2520 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
9eac9828
JG
2521 if (stream->chan->metadata_cache->contents.size ==
2522 stream->ust_metadata_pushed) {
55954e07
JG
2523 /*
2524 * In the context of a user space metadata channel, a
2525 * change in version can be detected in two ways:
2526 * 1) During the pre-consume of the `read_subbuffer` loop,
2527 * 2) When populating the metadata ring buffer (i.e. here).
2528 *
2529 * This function is invoked when there is no metadata
2530 * available in the ring-buffer. If all data was consumed
2531 * up to the size of the metadata cache, there is no metadata
2532 * to insert in the ring-buffer.
2533 *
2534 * However, the metadata version could still have changed (a
2535 * regeneration without any new data will yield the same cache
2536 * size).
2537 *
2538 * The cache's version is checked for a version change and the
2539 * consumed position is reset if one occurred.
2540 *
2541 * This check is only necessary for the user space domain as
2542 * it has to manage the cache explicitly. If this reset was not
2543 * performed, no metadata would be consumed (and no reset would
2544 * occur as part of the pre-consume) until the metadata size
2545 * exceeded the cache size.
2546 */
2547 if (stream->metadata_version !=
2548 stream->chan->metadata_cache->version) {
2549 metadata_stream_reset_cache_consumed_position(stream);
2550 consumer_stream_metadata_set_version(stream,
2551 stream->chan->metadata_cache->version);
2552 } else {
2553 ret = 0;
2554 goto end;
2555 }
94d49140
JD
2556 }
2557
b623cb6a 2558 write_len = lttng_ust_ctl_write_one_packet_to_channel(stream->chan->uchan,
9eac9828
JG
2559 &stream->chan->metadata_cache->contents.data[stream->ust_metadata_pushed],
2560 stream->chan->metadata_cache->contents.size -
2561 stream->ust_metadata_pushed);
a0377dfe 2562 LTTNG_ASSERT(write_len != 0);
94d49140
JD
2563 if (write_len < 0) {
2564 ERR("Writing one metadata packet");
f5ba75b4 2565 ret = write_len;
94d49140
JD
2566 goto end;
2567 }
2568 stream->ust_metadata_pushed += write_len;
2569
a0377dfe 2570 LTTNG_ASSERT(stream->chan->metadata_cache->contents.size >=
94d49140
JD
2571 stream->ust_metadata_pushed);
2572 ret = write_len;
2573
0d88e046
JG
2574 /*
2575 * Switch packet (but don't open the next one) on every commit of
2576 * a metadata packet. Since the subbuffer is fully filled (with padding,
2577 * if needed), the stream is "quiescent" after this commit.
2578 */
881fc67f 2579 if (lttng_ust_ctl_flush_buffer(stream->ustream, 1)) {
edb555b5 2580 ERR("Failed to flush buffer while committing one metadata packet");
881fc67f
MD
2581 ret = -EIO;
2582 } else {
2583 stream->quiescent = true;
2584 }
94d49140
JD
2585end:
2586 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
2587 return ret;
2588}
2589
309167d2 2590
94d49140
JD
2591/*
2592 * Sync metadata meaning request them to the session daemon and snapshot to the
2593 * metadata thread can consumer them.
2594 *
c585821b
MD
2595 * Metadata stream lock is held here, but we need to release it when
2596 * interacting with sessiond, else we cause a deadlock with live
2597 * awaiting on metadata to be pushed out.
94d49140 2598 *
cdb72e4e 2599 * The RCU read side lock must be held by the caller.
94d49140 2600 */
577eea73
JG
2601enum sync_metadata_status lttng_ustconsumer_sync_metadata(
2602 struct lttng_consumer_local_data *ctx,
cdb72e4e 2603 struct lttng_consumer_stream *metadata_stream)
94d49140
JD
2604{
2605 int ret;
577eea73 2606 enum sync_metadata_status status;
cdb72e4e 2607 struct lttng_consumer_channel *metadata_channel;
94d49140 2608
a0377dfe
FD
2609 LTTNG_ASSERT(ctx);
2610 LTTNG_ASSERT(metadata_stream);
48b7cdc2 2611 ASSERT_RCU_READ_LOCKED();
94d49140 2612
cdb72e4e
JG
2613 metadata_channel = metadata_stream->chan;
2614 pthread_mutex_unlock(&metadata_stream->lock);
94d49140
JD
2615 /*
2616 * Request metadata from the sessiond, but don't wait for the flush
2617 * because we locked the metadata thread.
2618 */
cdb72e4e
JG
2619 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0, 0);
2620 pthread_mutex_lock(&metadata_stream->lock);
94d49140 2621 if (ret < 0) {
577eea73 2622 status = SYNC_METADATA_STATUS_ERROR;
94d49140
JD
2623 goto end;
2624 }
2625
cdb72e4e
JG
2626 /*
2627 * The metadata stream and channel can be deleted while the
2628 * metadata stream lock was released. The streamed is checked
2629 * for deletion before we use it further.
2630 *
2631 * Note that it is safe to access a logically-deleted stream since its
2632 * existence is still guaranteed by the RCU read side lock. However,
2633 * it should no longer be used. The close/deletion of the metadata
2634 * channel and stream already guarantees that all metadata has been
2635 * consumed. Therefore, there is nothing left to do in this function.
2636 */
2637 if (consumer_stream_is_deleted(metadata_stream)) {
2638 DBG("Metadata stream %" PRIu64 " was deleted during the metadata synchronization",
2639 metadata_stream->key);
577eea73 2640 status = SYNC_METADATA_STATUS_NO_DATA;
cdb72e4e
JG
2641 goto end;
2642 }
2643
2644 ret = commit_one_metadata_packet(metadata_stream);
577eea73
JG
2645 if (ret < 0) {
2646 status = SYNC_METADATA_STATUS_ERROR;
94d49140
JD
2647 goto end;
2648 } else if (ret > 0) {
577eea73
JG
2649 status = SYNC_METADATA_STATUS_NEW_DATA;
2650 } else /* ret == 0 */ {
2651 status = SYNC_METADATA_STATUS_NO_DATA;
2652 goto end;
94d49140
JD
2653 }
2654
b623cb6a 2655 ret = lttng_ust_ctl_snapshot(metadata_stream->ustream);
94d49140 2656 if (ret < 0) {
577eea73
JG
2657 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret);
2658 status = SYNC_METADATA_STATUS_ERROR;
94d49140
JD
2659 goto end;
2660 }
2661
94d49140 2662end:
577eea73 2663 return status;
94d49140
JD
2664}
2665
02b3d176
DG
2666/*
2667 * Return 0 on success else a negative value.
2668 */
2669static int notify_if_more_data(struct lttng_consumer_stream *stream,
2670 struct lttng_consumer_local_data *ctx)
2671{
2672 int ret;
b623cb6a 2673 struct lttng_ust_ctl_consumer_stream *ustream;
02b3d176 2674
a0377dfe
FD
2675 LTTNG_ASSERT(stream);
2676 LTTNG_ASSERT(ctx);
02b3d176
DG
2677
2678 ustream = stream->ustream;
2679
2680 /*
2681 * First, we are going to check if there is a new subbuffer available
2682 * before reading the stream wait_fd.
2683 */
2684 /* Get the next subbuffer */
b623cb6a 2685 ret = lttng_ust_ctl_get_next_subbuf(ustream);
02b3d176
DG
2686 if (ret) {
2687 /* No more data found, flag the stream. */
2688 stream->has_data = 0;
2689 ret = 0;
2690 goto end;
2691 }
2692
b623cb6a 2693 ret = lttng_ust_ctl_put_subbuf(ustream);
a0377dfe 2694 LTTNG_ASSERT(!ret);
02b3d176
DG
2695
2696 /* This stream still has data. Flag it and wake up the data thread. */
2697 stream->has_data = 1;
2698
2699 if (stream->monitor && !stream->hangup_flush_done && !ctx->has_wakeup) {
2700 ssize_t writelen;
2701
2702 writelen = lttng_pipe_write(ctx->consumer_wakeup_pipe, "!", 1);
2703 if (writelen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2704 ret = writelen;
2705 goto end;
2706 }
2707
2708 /* The wake up pipe has been notified. */
2709 ctx->has_wakeup = 1;
2710 }
2711 ret = 0;
2712
2713end:
2714 return ret;
2715}
2716
6f9449c2 2717static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream *stream)
fb83fe64 2718{
6f9449c2 2719 int ret = 0;
fb83fe64 2720
fb83fe64 2721 /*
6f9449c2
JG
2722 * We can consume the 1 byte written into the wait_fd by
2723 * UST. Don't trigger error if we cannot read this one byte
2724 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2725 *
2726 * This is only done when the stream is monitored by a thread,
2727 * before the flush is done after a hangup and if the stream
2728 * is not flagged with data since there might be nothing to
2729 * consume in the wait fd but still have data available
2730 * flagged by the consumer wake up pipe.
fb83fe64 2731 */
6f9449c2
JG
2732 if (stream->monitor && !stream->hangup_flush_done && !stream->has_data) {
2733 char dummy;
2734 ssize_t readlen;
2735
2736 readlen = lttng_read(stream->wait_fd, &dummy, 1);
2737 if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2738 ret = readlen;
2739 }
fb83fe64 2740 }
fb83fe64 2741
6f9449c2
JG
2742 return ret;
2743}
2744
2745static int extract_common_subbuffer_info(struct lttng_consumer_stream *stream,
2746 struct stream_subbuffer *subbuf)
2747{
2748 int ret;
2749
b623cb6a 2750 ret = lttng_ust_ctl_get_subbuf_size(
6f9449c2
JG
2751 stream->ustream, &subbuf->info.data.subbuf_size);
2752 if (ret) {
fb83fe64
JD
2753 goto end;
2754 }
6f9449c2 2755
b623cb6a 2756 ret = lttng_ust_ctl_get_padded_subbuf_size(
6f9449c2
JG
2757 stream->ustream, &subbuf->info.data.padded_subbuf_size);
2758 if (ret) {
2759 goto end;
fb83fe64 2760 }
fb83fe64
JD
2761
2762end:
2763 return ret;
2764}
2765
6f9449c2
JG
2766static int extract_metadata_subbuffer_info(struct lttng_consumer_stream *stream,
2767 struct stream_subbuffer *subbuf)
d41f73b7 2768{
6f9449c2 2769 int ret;
ffe60014 2770
6f9449c2
JG
2771 ret = extract_common_subbuffer_info(stream, subbuf);
2772 if (ret) {
2773 goto end;
2774 }
d41f73b7 2775
55954e07 2776 subbuf->info.metadata.version = stream->metadata_version;
ffe60014 2777
6f9449c2
JG
2778end:
2779 return ret;
2780}
d41f73b7 2781
6f9449c2
JG
2782static int extract_data_subbuffer_info(struct lttng_consumer_stream *stream,
2783 struct stream_subbuffer *subbuf)
2784{
2785 int ret;
c617c0c6 2786
6f9449c2
JG
2787 ret = extract_common_subbuffer_info(stream, subbuf);
2788 if (ret) {
2789 goto end;
02d02e31
JD
2790 }
2791
b623cb6a 2792 ret = lttng_ust_ctl_get_packet_size(
6f9449c2
JG
2793 stream->ustream, &subbuf->info.data.packet_size);
2794 if (ret < 0) {
2795 PERROR("Failed to get sub-buffer packet size");
2796 goto end;
2797 }
04ef1097 2798
b623cb6a 2799 ret = lttng_ust_ctl_get_content_size(
6f9449c2
JG
2800 stream->ustream, &subbuf->info.data.content_size);
2801 if (ret < 0) {
2802 PERROR("Failed to get sub-buffer content size");
2803 goto end;
d41f73b7 2804 }
309167d2 2805
b623cb6a 2806 ret = lttng_ust_ctl_get_timestamp_begin(
6f9449c2
JG
2807 stream->ustream, &subbuf->info.data.timestamp_begin);
2808 if (ret < 0) {
2809 PERROR("Failed to get sub-buffer begin timestamp");
2810 goto end;
2811 }
fb83fe64 2812
b623cb6a 2813 ret = lttng_ust_ctl_get_timestamp_end(
6f9449c2
JG
2814 stream->ustream, &subbuf->info.data.timestamp_end);
2815 if (ret < 0) {
2816 PERROR("Failed to get sub-buffer end timestamp");
2817 goto end;
2818 }
2819
b623cb6a 2820 ret = lttng_ust_ctl_get_events_discarded(
6f9449c2
JG
2821 stream->ustream, &subbuf->info.data.events_discarded);
2822 if (ret) {
2823 PERROR("Failed to get sub-buffer events discarded count");
2824 goto end;
2825 }
2826
b623cb6a 2827 ret = lttng_ust_ctl_get_sequence_number(stream->ustream,
6f9449c2
JG
2828 &subbuf->info.data.sequence_number.value);
2829 if (ret) {
2830 /* May not be supported by older LTTng-modules. */
2831 if (ret != -ENOTTY) {
2832 PERROR("Failed to get sub-buffer sequence number");
2833 goto end;
fb83fe64 2834 }
1c20f0e2 2835 } else {
6f9449c2 2836 subbuf->info.data.sequence_number.is_set = true;
309167d2
JD
2837 }
2838
b623cb6a 2839 ret = lttng_ust_ctl_get_stream_id(
6f9449c2
JG
2840 stream->ustream, &subbuf->info.data.stream_id);
2841 if (ret < 0) {
2842 PERROR("Failed to get stream id");
2843 goto end;
2844 }
1d4dfdef 2845
b623cb6a 2846 ret = lttng_ust_ctl_get_instance_id(stream->ustream,
6f9449c2
JG
2847 &subbuf->info.data.stream_instance_id.value);
2848 if (ret) {
2849 /* May not be supported by older LTTng-modules. */
2850 if (ret != -ENOTTY) {
2851 PERROR("Failed to get stream instance id");
2852 goto end;
2853 }
2854 } else {
2855 subbuf->info.data.stream_instance_id.is_set = true;
2856 }
2857end:
2858 return ret;
2859}
1d4dfdef 2860
6f9449c2
JG
2861static int get_next_subbuffer_common(struct lttng_consumer_stream *stream,
2862 struct stream_subbuffer *subbuffer)
2863{
2864 int ret;
2865 const char *addr;
1d4dfdef 2866
6f9449c2
JG
2867 ret = stream->read_subbuffer_ops.extract_subbuffer_info(
2868 stream, subbuffer);
2869 if (ret) {
2870 goto end;
2871 }
02d02e31 2872
6f9449c2 2873 ret = get_current_subbuf_addr(stream, &addr);
128708c3 2874 if (ret) {
6f9449c2 2875 goto end;
128708c3
JG
2876 }
2877
6f9449c2
JG
2878 subbuffer->buffer.buffer = lttng_buffer_view_init(
2879 addr, 0, subbuffer->info.data.padded_subbuf_size);
a0377dfe 2880 LTTNG_ASSERT(subbuffer->buffer.buffer.data != NULL);
6f9449c2
JG
2881end:
2882 return ret;
2883}
fd424d99 2884
b6797c8e
JG
2885static enum get_next_subbuffer_status get_next_subbuffer(
2886 struct lttng_consumer_stream *stream,
6f9449c2
JG
2887 struct stream_subbuffer *subbuffer)
2888{
2889 int ret;
b6797c8e 2890 enum get_next_subbuffer_status status;
331744e3 2891
b623cb6a 2892 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
b6797c8e
JG
2893 switch (ret) {
2894 case 0:
2895 status = GET_NEXT_SUBBUFFER_STATUS_OK;
2896 break;
2897 case -ENODATA:
2898 case -EAGAIN:
2899 /*
2900 * The caller only expects -ENODATA when there is no data to
2901 * read, but the kernel tracer returns -EAGAIN when there is
2902 * currently no data for a non-finalized stream, and -ENODATA
2903 * when there is no data for a finalized stream. Those can be
2904 * combined into a -ENODATA return value.
2905 */
2906 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
2907 goto end;
2908 default:
2909 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
6f9449c2 2910 goto end;
02b3d176
DG
2911 }
2912
6f9449c2
JG
2913 ret = get_next_subbuffer_common(stream, subbuffer);
2914 if (ret) {
b6797c8e 2915 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
23d56598 2916 goto end;
1c20f0e2 2917 }
6f9449c2 2918end:
b6797c8e 2919 return status;
6f9449c2 2920}
1c20f0e2 2921
b6797c8e
JG
2922static enum get_next_subbuffer_status get_next_subbuffer_metadata(
2923 struct lttng_consumer_stream *stream,
6f9449c2
JG
2924 struct stream_subbuffer *subbuffer)
2925{
2926 int ret;
f5ba75b4
JG
2927 bool cache_empty;
2928 bool got_subbuffer;
2929 bool coherent;
2930 bool buffer_empty;
2931 unsigned long consumed_pos, produced_pos;
b6797c8e 2932 enum get_next_subbuffer_status status;
6f9449c2 2933
f5ba75b4 2934 do {
b623cb6a 2935 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
f5ba75b4
JG
2936 if (ret == 0) {
2937 got_subbuffer = true;
2938 } else {
2939 got_subbuffer = false;
2940 if (ret != -EAGAIN) {
2941 /* Fatal error. */
b6797c8e 2942 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
f5ba75b4
JG
2943 goto end;
2944 }
c585821b
MD
2945 }
2946
f5ba75b4
JG
2947 /*
2948 * Determine if the cache is empty and ensure that a sub-buffer
2949 * is made available if the cache is not empty.
2950 */
2951 if (!got_subbuffer) {
2952 ret = commit_one_metadata_packet(stream);
2953 if (ret < 0 && ret != -ENOBUFS) {
b6797c8e 2954 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
f5ba75b4
JG
2955 goto end;
2956 } else if (ret == 0) {
2957 /* Not an error, the cache is empty. */
2958 cache_empty = true;
b6797c8e 2959 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
f5ba75b4
JG
2960 goto end;
2961 } else {
2962 cache_empty = false;
2963 }
2964 } else {
2965 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
9eac9828
JG
2966 cache_empty = stream->chan->metadata_cache->contents.size ==
2967 stream->ust_metadata_pushed;
f5ba75b4 2968 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
94d49140 2969 }
f5ba75b4 2970 } while (!got_subbuffer);
94d49140 2971
f5ba75b4 2972 /* Populate sub-buffer infos and view. */
6f9449c2
JG
2973 ret = get_next_subbuffer_common(stream, subbuffer);
2974 if (ret) {
b6797c8e 2975 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
6f9449c2 2976 goto end;
309167d2 2977 }
f5ba75b4
JG
2978
2979 ret = lttng_ustconsumer_sample_snapshot_positions(stream);
2980 if (ret < 0) {
2981 /*
2982 * -EAGAIN is not expected since we got a sub-buffer and haven't
2983 * pushed the consumption position yet (on put_next).
2984 */
2985 PERROR("Failed to take a snapshot of metadata buffer positions");
b6797c8e 2986 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
f5ba75b4
JG
2987 goto end;
2988 }
2989
2990 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
2991 if (ret) {
2992 PERROR("Failed to get metadata consumed position");
b6797c8e 2993 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
f5ba75b4
JG
2994 goto end;
2995 }
2996
2997 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
2998 if (ret) {
2999 PERROR("Failed to get metadata produced position");
b6797c8e 3000 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
f5ba75b4
JG
3001 goto end;
3002 }
3003
3004 /* Last sub-buffer of the ring buffer ? */
3005 buffer_empty = (consumed_pos + stream->max_sb_size) == produced_pos;
3006
3007 /*
3008 * The sessiond registry lock ensures that coherent units of metadata
3009 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
3010 * acquired, the cache is empty, and it is the only available sub-buffer
3011 * available, it is safe to assume that it is "coherent".
3012 */
3013 coherent = got_subbuffer && cache_empty && buffer_empty;
3014
3015 LTTNG_OPTIONAL_SET(&subbuffer->info.metadata.coherent, coherent);
b6797c8e 3016 status = GET_NEXT_SUBBUFFER_STATUS_OK;
23d56598 3017end:
b6797c8e 3018 return status;
d41f73b7
MD
3019}
3020
6f9449c2 3021static int put_next_subbuffer(struct lttng_consumer_stream *stream,
f46376a1 3022 struct stream_subbuffer *subbuffer __attribute__((unused)))
6f9449c2 3023{
b623cb6a 3024 const int ret = lttng_ust_ctl_put_next_subbuf(stream->ustream);
6f9449c2 3025
a0377dfe 3026 LTTNG_ASSERT(ret == 0);
6f9449c2
JG
3027 return ret;
3028}
3029
3030static int signal_metadata(struct lttng_consumer_stream *stream,
f46376a1 3031 struct lttng_consumer_local_data *ctx __attribute__((unused)))
6f9449c2 3032{
8db3acaf 3033 ASSERT_LOCKED(stream->metadata_rdv_lock);
6f9449c2
JG
3034 return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0;
3035}
3036
f5ba75b4 3037static int lttng_ustconsumer_set_stream_ops(
6f9449c2
JG
3038 struct lttng_consumer_stream *stream)
3039{
f5ba75b4
JG
3040 int ret = 0;
3041
6f9449c2
JG
3042 stream->read_subbuffer_ops.on_wake_up = consumer_stream_ust_on_wake_up;
3043 if (stream->metadata_flag) {
3044 stream->read_subbuffer_ops.get_next_subbuffer =
3045 get_next_subbuffer_metadata;
3046 stream->read_subbuffer_ops.extract_subbuffer_info =
3047 extract_metadata_subbuffer_info;
3048 stream->read_subbuffer_ops.reset_metadata =
55954e07 3049 metadata_stream_reset_cache_consumed_position;
f5ba75b4
JG
3050 if (stream->chan->is_live) {
3051 stream->read_subbuffer_ops.on_sleep = signal_metadata;
3052 ret = consumer_stream_enable_metadata_bucketization(
3053 stream);
3054 if (ret) {
3055 goto end;
3056 }
3057 }
6f9449c2
JG
3058 } else {
3059 stream->read_subbuffer_ops.get_next_subbuffer =
3060 get_next_subbuffer;
3061 stream->read_subbuffer_ops.extract_subbuffer_info =
3062 extract_data_subbuffer_info;
3063 stream->read_subbuffer_ops.on_sleep = notify_if_more_data;
3064 if (stream->chan->is_live) {
3065 stream->read_subbuffer_ops.send_live_beacon =
3066 consumer_flush_ust_index;
3067 }
3068 }
3069
3070 stream->read_subbuffer_ops.put_next_subbuffer = put_next_subbuffer;
f5ba75b4
JG
3071end:
3072 return ret;
6f9449c2
JG
3073}
3074
ffe60014
DG
3075/*
3076 * Called when a stream is created.
fe4477ee
JD
3077 *
3078 * Return 0 on success or else a negative value.
ffe60014 3079 */
d41f73b7
MD
3080int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
3081{
fe4477ee
JD
3082 int ret;
3083
a0377dfe 3084 LTTNG_ASSERT(stream);
10a50311 3085
d2956687
JG
3086 /*
3087 * Don't create anything if this is set for streaming or if there is
3088 * no current trace chunk on the parent channel.
3089 */
3090 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor &&
3091 stream->chan->trace_chunk) {
3092 ret = consumer_stream_create_output_files(stream, true);
3093 if (ret) {
fe4477ee
JD
3094 goto error;
3095 }
fe4477ee 3096 }
6f9449c2
JG
3097
3098 lttng_ustconsumer_set_stream_ops(stream);
fe4477ee
JD
3099 ret = 0;
3100
3101error:
3102 return ret;
d41f73b7 3103}
ca22feea
DG
3104
3105/*
3106 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
3107 * stream. Consumer data lock MUST be acquired before calling this function
3108 * and the stream lock.
ca22feea 3109 *
6d805429 3110 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
3111 * data is available for trace viewer reading.
3112 */
6d805429 3113int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
3114{
3115 int ret;
3116
a0377dfe
FD
3117 LTTNG_ASSERT(stream);
3118 LTTNG_ASSERT(stream->ustream);
b1316da1 3119 ASSERT_LOCKED(stream->lock);
ca22feea 3120
6d805429 3121 DBG("UST consumer checking data pending");
c8f59ee5 3122
ca6b395f
MD
3123 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
3124 ret = 0;
3125 goto end;
3126 }
3127
04ef1097 3128 if (stream->chan->type == CONSUMER_CHANNEL_TYPE_METADATA) {
e6ee4eab
DG
3129 uint64_t contiguous, pushed;
3130
3131 /* Ease our life a bit. */
934ba8fd 3132 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
9eac9828 3133 contiguous = stream->chan->metadata_cache->contents.size;
934ba8fd 3134 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
e6ee4eab
DG
3135 pushed = stream->ust_metadata_pushed;
3136
04ef1097
MD
3137 /*
3138 * We can simply check whether all contiguously available data
3139 * has been pushed to the ring buffer, since the push operation
3140 * is performed within get_next_subbuf(), and because both
3141 * get_next_subbuf() and put_next_subbuf() are issued atomically
3142 * thanks to the stream lock within
3143 * lttng_ustconsumer_read_subbuffer(). This basically means that
3144 * whetnever ust_metadata_pushed is incremented, the associated
3145 * metadata has been consumed from the metadata stream.
3146 */
3147 DBG("UST consumer metadata pending check: contiguous %" PRIu64 " vs pushed %" PRIu64,
e6ee4eab 3148 contiguous, pushed);
a0377dfe 3149 LTTNG_ASSERT(((int64_t) (contiguous - pushed)) >= 0);
e6ee4eab 3150 if ((contiguous != pushed) ||
6acdf328 3151 (((int64_t) contiguous - pushed) > 0 || contiguous == 0)) {
04ef1097
MD
3152 ret = 1; /* Data is pending */
3153 goto end;
3154 }
3155 } else {
b623cb6a 3156 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
04ef1097
MD
3157 if (ret == 0) {
3158 /*
3159 * There is still data so let's put back this
3160 * subbuffer.
3161 */
b623cb6a 3162 ret = lttng_ust_ctl_put_subbuf(stream->ustream);
a0377dfe 3163 LTTNG_ASSERT(ret == 0);
04ef1097
MD
3164 ret = 1; /* Data is pending */
3165 goto end;
3166 }
ca22feea
DG
3167 }
3168
6d805429
DG
3169 /* Data is NOT pending so ready to be read. */
3170 ret = 0;
ca22feea 3171
6efae65e
DG
3172end:
3173 return ret;
ca22feea 3174}
d88aee68 3175
6d574024
DG
3176/*
3177 * Stop a given metadata channel timer if enabled and close the wait fd which
3178 * is the poll pipe of the metadata stream.
3179 *
d2c82a5a 3180 * This MUST be called with the metadata channel lock acquired.
6d574024
DG
3181 */
3182void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel *metadata)
3183{
3184 int ret;
3185
a0377dfe
FD
3186 LTTNG_ASSERT(metadata);
3187 LTTNG_ASSERT(metadata->type == CONSUMER_CHANNEL_TYPE_METADATA);
6d574024
DG
3188
3189 DBG("Closing metadata channel key %" PRIu64, metadata->key);
3190
3191 if (metadata->switch_timer_enabled == 1) {
3192 consumer_timer_switch_stop(metadata);
3193 }
3194
3195 if (!metadata->metadata_stream) {
3196 goto end;
3197 }
3198
3199 /*
3200 * Closing write side so the thread monitoring the stream wakes up if any
3201 * and clean the metadata stream.
3202 */
3203 if (metadata->metadata_stream->ust_metadata_poll_pipe[1] >= 0) {
3204 ret = close(metadata->metadata_stream->ust_metadata_poll_pipe[1]);
3205 if (ret < 0) {
3206 PERROR("closing metadata pipe write side");
3207 }
3208 metadata->metadata_stream->ust_metadata_poll_pipe[1] = -1;
3209 }
3210
3211end:
3212 return;
3213}
3214
d88aee68
DG
3215/*
3216 * Close every metadata stream wait fd of the metadata hash table. This
3217 * function MUST be used very carefully so not to run into a race between the
3218 * metadata thread handling streams and this function closing their wait fd.
3219 *
3220 * For UST, this is used when the session daemon hangs up. Its the metadata
3221 * producer so calling this is safe because we are assured that no state change
3222 * can occur in the metadata thread for the streams in the hash table.
3223 */
6d574024 3224void lttng_ustconsumer_close_all_metadata(struct lttng_ht *metadata_ht)
d88aee68 3225{
d88aee68
DG
3226 struct lttng_ht_iter iter;
3227 struct lttng_consumer_stream *stream;
3228
a0377dfe
FD
3229 LTTNG_ASSERT(metadata_ht);
3230 LTTNG_ASSERT(metadata_ht->ht);
d88aee68
DG
3231
3232 DBG("UST consumer closing all metadata streams");
3233
3234 rcu_read_lock();
3235 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream,
3236 node.node) {
9ce5646a
MD
3237
3238 health_code_update();
3239
be2b50c7 3240 pthread_mutex_lock(&stream->chan->lock);
6d574024 3241 lttng_ustconsumer_close_metadata(stream->chan);
be2b50c7
DG
3242 pthread_mutex_unlock(&stream->chan->lock);
3243
d88aee68
DG
3244 }
3245 rcu_read_unlock();
3246}
d8ef542d
MD
3247
3248void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream)
3249{
3250 int ret;
3251
b623cb6a 3252 ret = lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream);
d8ef542d
MD
3253 if (ret < 0) {
3254 ERR("Unable to close wakeup fd");
3255 }
3256}
331744e3 3257
f666ae70
MD
3258/*
3259 * Please refer to consumer-timer.c before adding any lock within this
3260 * function or any of its callees. Timers have a very strict locking
3261 * semantic with respect to teardown. Failure to respect this semantic
3262 * introduces deadlocks.
c585821b
MD
3263 *
3264 * DON'T hold the metadata lock when calling this function, else this
3265 * can cause deadlock involving consumer awaiting for metadata to be
3266 * pushed out due to concurrent interaction with the session daemon.
f666ae70 3267 */
331744e3 3268int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx,
94d49140 3269 struct lttng_consumer_channel *channel, int timer, int wait)
331744e3
JD
3270{
3271 struct lttcomm_metadata_request_msg request;
3272 struct lttcomm_consumer_msg msg;
0c759fc9 3273 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
93ec662e 3274 uint64_t len, key, offset, version;
331744e3
JD
3275 int ret;
3276
a0377dfe
FD
3277 LTTNG_ASSERT(channel);
3278 LTTNG_ASSERT(channel->metadata_cache);
331744e3 3279
53efb85a
MD
3280 memset(&request, 0, sizeof(request));
3281
331744e3 3282 /* send the metadata request to sessiond */
fa29bfbf 3283 switch (the_consumer_data.type) {
331744e3
JD
3284 case LTTNG_CONSUMER64_UST:
3285 request.bits_per_long = 64;
3286 break;
3287 case LTTNG_CONSUMER32_UST:
3288 request.bits_per_long = 32;
3289 break;
3290 default:
3291 request.bits_per_long = 0;
3292 break;
3293 }
3294
3295 request.session_id = channel->session_id;
1950109e 3296 request.session_id_per_pid = channel->session_id_per_pid;
567eb353
DG
3297 /*
3298 * Request the application UID here so the metadata of that application can
3299 * be sent back. The channel UID corresponds to the user UID of the session
3300 * used for the rights on the stream file(s).
3301 */
3302 request.uid = channel->ust_app_uid;
331744e3 3303 request.key = channel->key;
567eb353 3304
1950109e 3305 DBG("Sending metadata request to sessiond, session id %" PRIu64
3e25d926 3306 ", per-pid %" PRIu64 ", app UID %u and channel key %" PRIu64,
567eb353
DG
3307 request.session_id, request.session_id_per_pid, request.uid,
3308 request.key);
331744e3 3309
75d83e50 3310 pthread_mutex_lock(&ctx->metadata_socket_lock);
9ce5646a
MD
3311
3312 health_code_update();
3313
331744e3
JD
3314 ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request,
3315 sizeof(request));
3316 if (ret < 0) {
3317 ERR("Asking metadata to sessiond");
3318 goto end;
3319 }
3320
9ce5646a
MD
3321 health_code_update();
3322
331744e3
JD
3323 /* Receive the metadata from sessiond */
3324 ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg,
3325 sizeof(msg));
3326 if (ret != sizeof(msg)) {
8fd623e0 3327 DBG("Consumer received unexpected message size %d (expects %zu)",
331744e3
JD
3328 ret, sizeof(msg));
3329 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
3330 /*
3331 * The ret value might 0 meaning an orderly shutdown but this is ok
3332 * since the caller handles this.
3333 */
3334 goto end;
3335 }
3336
9ce5646a
MD
3337 health_code_update();
3338
331744e3
JD
3339 if (msg.cmd_type == LTTNG_ERR_UND) {
3340 /* No registry found */
3341 (void) consumer_send_status_msg(ctx->consumer_metadata_socket,
3342 ret_code);
3343 ret = 0;
3344 goto end;
3345 } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) {
3346 ERR("Unexpected cmd_type received %d", msg.cmd_type);
3347 ret = -1;
3348 goto end;
3349 }
3350
3351 len = msg.u.push_metadata.len;
3352 key = msg.u.push_metadata.key;
3353 offset = msg.u.push_metadata.target_offset;
93ec662e 3354 version = msg.u.push_metadata.version;
331744e3 3355
a0377dfe 3356 LTTNG_ASSERT(key == channel->key);
331744e3
JD
3357 if (len == 0) {
3358 DBG("No new metadata to receive for key %" PRIu64, key);
3359 }
3360
9ce5646a
MD
3361 health_code_update();
3362
331744e3
JD
3363 /* Tell session daemon we are ready to receive the metadata. */
3364 ret = consumer_send_status_msg(ctx->consumer_metadata_socket,
0c759fc9 3365 LTTCOMM_CONSUMERD_SUCCESS);
331744e3
JD
3366 if (ret < 0 || len == 0) {
3367 /*
3368 * Somehow, the session daemon is not responding anymore or there is
3369 * nothing to receive.
3370 */
3371 goto end;
3372 }
3373
9ce5646a
MD
3374 health_code_update();
3375
1eb682be 3376 ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket,
93ec662e 3377 key, offset, len, version, channel, timer, wait);
1eb682be 3378 if (ret >= 0) {
f2a444f1
DG
3379 /*
3380 * Only send the status msg if the sessiond is alive meaning a positive
3381 * ret code.
3382 */
1eb682be 3383 (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret);
f2a444f1 3384 }
331744e3
JD
3385 ret = 0;
3386
3387end:
9ce5646a
MD
3388 health_code_update();
3389
75d83e50 3390 pthread_mutex_unlock(&ctx->metadata_socket_lock);
331744e3
JD
3391 return ret;
3392}
70190e1c
DG
3393
3394/*
3395 * Return the ustctl call for the get stream id.
3396 */
3397int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream *stream,
3398 uint64_t *stream_id)
3399{
a0377dfe
FD
3400 LTTNG_ASSERT(stream);
3401 LTTNG_ASSERT(stream_id);
70190e1c 3402
b623cb6a 3403 return lttng_ust_ctl_get_stream_id(stream->ustream, stream_id);
70190e1c 3404}
881fc67f
MD
3405
3406void lttng_ustconsumer_sigbus_handle(void *addr)
3407{
3408 lttng_ust_ctl_sigbus_handle(addr);
3409}
This page took 0.325804 seconds and 5 git commands to generate.